Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[linux-2.6-microblaze.git] / drivers / pci / controller / dwc / pcie-designware-ep.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Synopsys DesignWare PCIe Endpoint controller driver
4  *
5  * Copyright (C) 2017 Texas Instruments
6  * Author: Kishon Vijay Abraham I <kishon@ti.com>
7  */
8
9 #include <linux/of.h>
10
11 #include "pcie-designware.h"
12 #include <linux/pci-epc.h>
13 #include <linux/pci-epf.h>
14
15 #include "../../pci.h"
16
17 void dw_pcie_ep_linkup(struct dw_pcie_ep *ep)
18 {
19         struct pci_epc *epc = ep->epc;
20
21         pci_epc_linkup(epc);
22 }
23 EXPORT_SYMBOL_GPL(dw_pcie_ep_linkup);
24
25 void dw_pcie_ep_init_notify(struct dw_pcie_ep *ep)
26 {
27         struct pci_epc *epc = ep->epc;
28
29         pci_epc_init_notify(epc);
30 }
31 EXPORT_SYMBOL_GPL(dw_pcie_ep_init_notify);
32
33 struct dw_pcie_ep_func *
34 dw_pcie_ep_get_func_from_ep(struct dw_pcie_ep *ep, u8 func_no)
35 {
36         struct dw_pcie_ep_func *ep_func;
37
38         list_for_each_entry(ep_func, &ep->func_list, list) {
39                 if (ep_func->func_no == func_no)
40                         return ep_func;
41         }
42
43         return NULL;
44 }
45
46 static unsigned int dw_pcie_ep_func_select(struct dw_pcie_ep *ep, u8 func_no)
47 {
48         unsigned int func_offset = 0;
49
50         if (ep->ops->func_conf_select)
51                 func_offset = ep->ops->func_conf_select(ep, func_no);
52
53         return func_offset;
54 }
55
56 static void __dw_pcie_ep_reset_bar(struct dw_pcie *pci, u8 func_no,
57                                    enum pci_barno bar, int flags)
58 {
59         u32 reg;
60         unsigned int func_offset = 0;
61         struct dw_pcie_ep *ep = &pci->ep;
62
63         func_offset = dw_pcie_ep_func_select(ep, func_no);
64
65         reg = func_offset + PCI_BASE_ADDRESS_0 + (4 * bar);
66         dw_pcie_dbi_ro_wr_en(pci);
67         dw_pcie_writel_dbi2(pci, reg, 0x0);
68         dw_pcie_writel_dbi(pci, reg, 0x0);
69         if (flags & PCI_BASE_ADDRESS_MEM_TYPE_64) {
70                 dw_pcie_writel_dbi2(pci, reg + 4, 0x0);
71                 dw_pcie_writel_dbi(pci, reg + 4, 0x0);
72         }
73         dw_pcie_dbi_ro_wr_dis(pci);
74 }
75
76 void dw_pcie_ep_reset_bar(struct dw_pcie *pci, enum pci_barno bar)
77 {
78         u8 func_no, funcs;
79
80         funcs = pci->ep.epc->max_functions;
81
82         for (func_no = 0; func_no < funcs; func_no++)
83                 __dw_pcie_ep_reset_bar(pci, func_no, bar, 0);
84 }
85
86 static u8 __dw_pcie_ep_find_next_cap(struct dw_pcie_ep *ep, u8 func_no,
87                 u8 cap_ptr, u8 cap)
88 {
89         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
90         unsigned int func_offset = 0;
91         u8 cap_id, next_cap_ptr;
92         u16 reg;
93
94         if (!cap_ptr)
95                 return 0;
96
97         func_offset = dw_pcie_ep_func_select(ep, func_no);
98
99         reg = dw_pcie_readw_dbi(pci, func_offset + cap_ptr);
100         cap_id = (reg & 0x00ff);
101
102         if (cap_id > PCI_CAP_ID_MAX)
103                 return 0;
104
105         if (cap_id == cap)
106                 return cap_ptr;
107
108         next_cap_ptr = (reg & 0xff00) >> 8;
109         return __dw_pcie_ep_find_next_cap(ep, func_no, next_cap_ptr, cap);
110 }
111
112 static u8 dw_pcie_ep_find_capability(struct dw_pcie_ep *ep, u8 func_no, u8 cap)
113 {
114         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
115         unsigned int func_offset = 0;
116         u8 next_cap_ptr;
117         u16 reg;
118
119         func_offset = dw_pcie_ep_func_select(ep, func_no);
120
121         reg = dw_pcie_readw_dbi(pci, func_offset + PCI_CAPABILITY_LIST);
122         next_cap_ptr = (reg & 0x00ff);
123
124         return __dw_pcie_ep_find_next_cap(ep, func_no, next_cap_ptr, cap);
125 }
126
127 static int dw_pcie_ep_write_header(struct pci_epc *epc, u8 func_no,
128                                    struct pci_epf_header *hdr)
129 {
130         struct dw_pcie_ep *ep = epc_get_drvdata(epc);
131         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
132         unsigned int func_offset = 0;
133
134         func_offset = dw_pcie_ep_func_select(ep, func_no);
135
136         dw_pcie_dbi_ro_wr_en(pci);
137         dw_pcie_writew_dbi(pci, func_offset + PCI_VENDOR_ID, hdr->vendorid);
138         dw_pcie_writew_dbi(pci, func_offset + PCI_DEVICE_ID, hdr->deviceid);
139         dw_pcie_writeb_dbi(pci, func_offset + PCI_REVISION_ID, hdr->revid);
140         dw_pcie_writeb_dbi(pci, func_offset + PCI_CLASS_PROG, hdr->progif_code);
141         dw_pcie_writew_dbi(pci, func_offset + PCI_CLASS_DEVICE,
142                            hdr->subclass_code | hdr->baseclass_code << 8);
143         dw_pcie_writeb_dbi(pci, func_offset + PCI_CACHE_LINE_SIZE,
144                            hdr->cache_line_size);
145         dw_pcie_writew_dbi(pci, func_offset + PCI_SUBSYSTEM_VENDOR_ID,
146                            hdr->subsys_vendor_id);
147         dw_pcie_writew_dbi(pci, func_offset + PCI_SUBSYSTEM_ID, hdr->subsys_id);
148         dw_pcie_writeb_dbi(pci, func_offset + PCI_INTERRUPT_PIN,
149                            hdr->interrupt_pin);
150         dw_pcie_dbi_ro_wr_dis(pci);
151
152         return 0;
153 }
154
155 static int dw_pcie_ep_inbound_atu(struct dw_pcie_ep *ep, u8 func_no,
156                                   enum pci_barno bar, dma_addr_t cpu_addr,
157                                   enum dw_pcie_as_type as_type)
158 {
159         int ret;
160         u32 free_win;
161         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
162
163         free_win = find_first_zero_bit(ep->ib_window_map, ep->num_ib_windows);
164         if (free_win >= ep->num_ib_windows) {
165                 dev_err(pci->dev, "No free inbound window\n");
166                 return -EINVAL;
167         }
168
169         ret = dw_pcie_prog_inbound_atu(pci, func_no, free_win, bar, cpu_addr,
170                                        as_type);
171         if (ret < 0) {
172                 dev_err(pci->dev, "Failed to program IB window\n");
173                 return ret;
174         }
175
176         ep->bar_to_atu[bar] = free_win;
177         set_bit(free_win, ep->ib_window_map);
178
179         return 0;
180 }
181
182 static int dw_pcie_ep_outbound_atu(struct dw_pcie_ep *ep, u8 func_no,
183                                    phys_addr_t phys_addr,
184                                    u64 pci_addr, size_t size)
185 {
186         u32 free_win;
187         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
188
189         free_win = find_first_zero_bit(ep->ob_window_map, ep->num_ob_windows);
190         if (free_win >= ep->num_ob_windows) {
191                 dev_err(pci->dev, "No free outbound window\n");
192                 return -EINVAL;
193         }
194
195         dw_pcie_prog_ep_outbound_atu(pci, func_no, free_win, PCIE_ATU_TYPE_MEM,
196                                      phys_addr, pci_addr, size);
197
198         set_bit(free_win, ep->ob_window_map);
199         ep->outbound_addr[free_win] = phys_addr;
200
201         return 0;
202 }
203
204 static void dw_pcie_ep_clear_bar(struct pci_epc *epc, u8 func_no,
205                                  struct pci_epf_bar *epf_bar)
206 {
207         struct dw_pcie_ep *ep = epc_get_drvdata(epc);
208         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
209         enum pci_barno bar = epf_bar->barno;
210         u32 atu_index = ep->bar_to_atu[bar];
211
212         __dw_pcie_ep_reset_bar(pci, func_no, bar, epf_bar->flags);
213
214         dw_pcie_disable_atu(pci, atu_index, DW_PCIE_REGION_INBOUND);
215         clear_bit(atu_index, ep->ib_window_map);
216         ep->epf_bar[bar] = NULL;
217 }
218
219 static int dw_pcie_ep_set_bar(struct pci_epc *epc, u8 func_no,
220                               struct pci_epf_bar *epf_bar)
221 {
222         int ret;
223         struct dw_pcie_ep *ep = epc_get_drvdata(epc);
224         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
225         enum pci_barno bar = epf_bar->barno;
226         size_t size = epf_bar->size;
227         int flags = epf_bar->flags;
228         enum dw_pcie_as_type as_type;
229         u32 reg;
230         unsigned int func_offset = 0;
231
232         func_offset = dw_pcie_ep_func_select(ep, func_no);
233
234         reg = PCI_BASE_ADDRESS_0 + (4 * bar) + func_offset;
235
236         if (!(flags & PCI_BASE_ADDRESS_SPACE))
237                 as_type = DW_PCIE_AS_MEM;
238         else
239                 as_type = DW_PCIE_AS_IO;
240
241         ret = dw_pcie_ep_inbound_atu(ep, func_no, bar,
242                                      epf_bar->phys_addr, as_type);
243         if (ret)
244                 return ret;
245
246         dw_pcie_dbi_ro_wr_en(pci);
247
248         dw_pcie_writel_dbi2(pci, reg, lower_32_bits(size - 1));
249         dw_pcie_writel_dbi(pci, reg, flags);
250
251         if (flags & PCI_BASE_ADDRESS_MEM_TYPE_64) {
252                 dw_pcie_writel_dbi2(pci, reg + 4, upper_32_bits(size - 1));
253                 dw_pcie_writel_dbi(pci, reg + 4, 0);
254         }
255
256         ep->epf_bar[bar] = epf_bar;
257         dw_pcie_dbi_ro_wr_dis(pci);
258
259         return 0;
260 }
261
262 static int dw_pcie_find_index(struct dw_pcie_ep *ep, phys_addr_t addr,
263                               u32 *atu_index)
264 {
265         u32 index;
266
267         for (index = 0; index < ep->num_ob_windows; index++) {
268                 if (ep->outbound_addr[index] != addr)
269                         continue;
270                 *atu_index = index;
271                 return 0;
272         }
273
274         return -EINVAL;
275 }
276
277 static void dw_pcie_ep_unmap_addr(struct pci_epc *epc, u8 func_no,
278                                   phys_addr_t addr)
279 {
280         int ret;
281         u32 atu_index;
282         struct dw_pcie_ep *ep = epc_get_drvdata(epc);
283         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
284
285         ret = dw_pcie_find_index(ep, addr, &atu_index);
286         if (ret < 0)
287                 return;
288
289         dw_pcie_disable_atu(pci, atu_index, DW_PCIE_REGION_OUTBOUND);
290         clear_bit(atu_index, ep->ob_window_map);
291 }
292
293 static int dw_pcie_ep_map_addr(struct pci_epc *epc, u8 func_no,
294                                phys_addr_t addr,
295                                u64 pci_addr, size_t size)
296 {
297         int ret;
298         struct dw_pcie_ep *ep = epc_get_drvdata(epc);
299         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
300
301         ret = dw_pcie_ep_outbound_atu(ep, func_no, addr, pci_addr, size);
302         if (ret) {
303                 dev_err(pci->dev, "Failed to enable address\n");
304                 return ret;
305         }
306
307         return 0;
308 }
309
310 static int dw_pcie_ep_get_msi(struct pci_epc *epc, u8 func_no)
311 {
312         struct dw_pcie_ep *ep = epc_get_drvdata(epc);
313         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
314         u32 val, reg;
315         unsigned int func_offset = 0;
316         struct dw_pcie_ep_func *ep_func;
317
318         ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
319         if (!ep_func || !ep_func->msi_cap)
320                 return -EINVAL;
321
322         func_offset = dw_pcie_ep_func_select(ep, func_no);
323
324         reg = ep_func->msi_cap + func_offset + PCI_MSI_FLAGS;
325         val = dw_pcie_readw_dbi(pci, reg);
326         if (!(val & PCI_MSI_FLAGS_ENABLE))
327                 return -EINVAL;
328
329         val = (val & PCI_MSI_FLAGS_QSIZE) >> 4;
330
331         return val;
332 }
333
334 static int dw_pcie_ep_set_msi(struct pci_epc *epc, u8 func_no, u8 interrupts)
335 {
336         struct dw_pcie_ep *ep = epc_get_drvdata(epc);
337         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
338         u32 val, reg;
339         unsigned int func_offset = 0;
340         struct dw_pcie_ep_func *ep_func;
341
342         ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
343         if (!ep_func || !ep_func->msi_cap)
344                 return -EINVAL;
345
346         func_offset = dw_pcie_ep_func_select(ep, func_no);
347
348         reg = ep_func->msi_cap + func_offset + PCI_MSI_FLAGS;
349         val = dw_pcie_readw_dbi(pci, reg);
350         val &= ~PCI_MSI_FLAGS_QMASK;
351         val |= (interrupts << 1) & PCI_MSI_FLAGS_QMASK;
352         dw_pcie_dbi_ro_wr_en(pci);
353         dw_pcie_writew_dbi(pci, reg, val);
354         dw_pcie_dbi_ro_wr_dis(pci);
355
356         return 0;
357 }
358
359 static int dw_pcie_ep_get_msix(struct pci_epc *epc, u8 func_no)
360 {
361         struct dw_pcie_ep *ep = epc_get_drvdata(epc);
362         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
363         u32 val, reg;
364         unsigned int func_offset = 0;
365         struct dw_pcie_ep_func *ep_func;
366
367         ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
368         if (!ep_func || !ep_func->msix_cap)
369                 return -EINVAL;
370
371         func_offset = dw_pcie_ep_func_select(ep, func_no);
372
373         reg = ep_func->msix_cap + func_offset + PCI_MSIX_FLAGS;
374         val = dw_pcie_readw_dbi(pci, reg);
375         if (!(val & PCI_MSIX_FLAGS_ENABLE))
376                 return -EINVAL;
377
378         val &= PCI_MSIX_FLAGS_QSIZE;
379
380         return val;
381 }
382
383 static int dw_pcie_ep_set_msix(struct pci_epc *epc, u8 func_no, u16 interrupts,
384                                enum pci_barno bir, u32 offset)
385 {
386         struct dw_pcie_ep *ep = epc_get_drvdata(epc);
387         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
388         u32 val, reg;
389         unsigned int func_offset = 0;
390         struct dw_pcie_ep_func *ep_func;
391
392         ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
393         if (!ep_func || !ep_func->msix_cap)
394                 return -EINVAL;
395
396         dw_pcie_dbi_ro_wr_en(pci);
397
398         func_offset = dw_pcie_ep_func_select(ep, func_no);
399
400         reg = ep_func->msix_cap + func_offset + PCI_MSIX_FLAGS;
401         val = dw_pcie_readw_dbi(pci, reg);
402         val &= ~PCI_MSIX_FLAGS_QSIZE;
403         val |= interrupts;
404         dw_pcie_writew_dbi(pci, reg, val);
405
406         reg = ep_func->msix_cap + func_offset + PCI_MSIX_TABLE;
407         val = offset | bir;
408         dw_pcie_writel_dbi(pci, reg, val);
409
410         reg = ep_func->msix_cap + func_offset + PCI_MSIX_PBA;
411         val = (offset + (interrupts * PCI_MSIX_ENTRY_SIZE)) | bir;
412         dw_pcie_writel_dbi(pci, reg, val);
413
414         dw_pcie_dbi_ro_wr_dis(pci);
415
416         return 0;
417 }
418
419 static int dw_pcie_ep_raise_irq(struct pci_epc *epc, u8 func_no,
420                                 enum pci_epc_irq_type type, u16 interrupt_num)
421 {
422         struct dw_pcie_ep *ep = epc_get_drvdata(epc);
423
424         if (!ep->ops->raise_irq)
425                 return -EINVAL;
426
427         return ep->ops->raise_irq(ep, func_no, type, interrupt_num);
428 }
429
430 static void dw_pcie_ep_stop(struct pci_epc *epc)
431 {
432         struct dw_pcie_ep *ep = epc_get_drvdata(epc);
433         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
434
435         if (!pci->ops->stop_link)
436                 return;
437
438         pci->ops->stop_link(pci);
439 }
440
441 static int dw_pcie_ep_start(struct pci_epc *epc)
442 {
443         struct dw_pcie_ep *ep = epc_get_drvdata(epc);
444         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
445
446         if (!pci->ops->start_link)
447                 return -EINVAL;
448
449         return pci->ops->start_link(pci);
450 }
451
452 static const struct pci_epc_features*
453 dw_pcie_ep_get_features(struct pci_epc *epc, u8 func_no)
454 {
455         struct dw_pcie_ep *ep = epc_get_drvdata(epc);
456
457         if (!ep->ops->get_features)
458                 return NULL;
459
460         return ep->ops->get_features(ep);
461 }
462
463 static const struct pci_epc_ops epc_ops = {
464         .write_header           = dw_pcie_ep_write_header,
465         .set_bar                = dw_pcie_ep_set_bar,
466         .clear_bar              = dw_pcie_ep_clear_bar,
467         .map_addr               = dw_pcie_ep_map_addr,
468         .unmap_addr             = dw_pcie_ep_unmap_addr,
469         .set_msi                = dw_pcie_ep_set_msi,
470         .get_msi                = dw_pcie_ep_get_msi,
471         .set_msix               = dw_pcie_ep_set_msix,
472         .get_msix               = dw_pcie_ep_get_msix,
473         .raise_irq              = dw_pcie_ep_raise_irq,
474         .start                  = dw_pcie_ep_start,
475         .stop                   = dw_pcie_ep_stop,
476         .get_features           = dw_pcie_ep_get_features,
477 };
478
479 int dw_pcie_ep_raise_legacy_irq(struct dw_pcie_ep *ep, u8 func_no)
480 {
481         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
482         struct device *dev = pci->dev;
483
484         dev_err(dev, "EP cannot trigger legacy IRQs\n");
485
486         return -EINVAL;
487 }
488
489 int dw_pcie_ep_raise_msi_irq(struct dw_pcie_ep *ep, u8 func_no,
490                              u8 interrupt_num)
491 {
492         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
493         struct dw_pcie_ep_func *ep_func;
494         struct pci_epc *epc = ep->epc;
495         unsigned int aligned_offset;
496         unsigned int func_offset = 0;
497         u16 msg_ctrl, msg_data;
498         u32 msg_addr_lower, msg_addr_upper, reg;
499         u64 msg_addr;
500         bool has_upper;
501         int ret;
502
503         ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
504         if (!ep_func || !ep_func->msi_cap)
505                 return -EINVAL;
506
507         func_offset = dw_pcie_ep_func_select(ep, func_no);
508
509         /* Raise MSI per the PCI Local Bus Specification Revision 3.0, 6.8.1. */
510         reg = ep_func->msi_cap + func_offset + PCI_MSI_FLAGS;
511         msg_ctrl = dw_pcie_readw_dbi(pci, reg);
512         has_upper = !!(msg_ctrl & PCI_MSI_FLAGS_64BIT);
513         reg = ep_func->msi_cap + func_offset + PCI_MSI_ADDRESS_LO;
514         msg_addr_lower = dw_pcie_readl_dbi(pci, reg);
515         if (has_upper) {
516                 reg = ep_func->msi_cap + func_offset + PCI_MSI_ADDRESS_HI;
517                 msg_addr_upper = dw_pcie_readl_dbi(pci, reg);
518                 reg = ep_func->msi_cap + func_offset + PCI_MSI_DATA_64;
519                 msg_data = dw_pcie_readw_dbi(pci, reg);
520         } else {
521                 msg_addr_upper = 0;
522                 reg = ep_func->msi_cap + func_offset + PCI_MSI_DATA_32;
523                 msg_data = dw_pcie_readw_dbi(pci, reg);
524         }
525         aligned_offset = msg_addr_lower & (epc->mem->window.page_size - 1);
526         msg_addr = ((u64)msg_addr_upper) << 32 |
527                         (msg_addr_lower & ~aligned_offset);
528         ret = dw_pcie_ep_map_addr(epc, func_no, ep->msi_mem_phys, msg_addr,
529                                   epc->mem->window.page_size);
530         if (ret)
531                 return ret;
532
533         writel(msg_data | (interrupt_num - 1), ep->msi_mem + aligned_offset);
534
535         dw_pcie_ep_unmap_addr(epc, func_no, ep->msi_mem_phys);
536
537         return 0;
538 }
539
540 int dw_pcie_ep_raise_msix_irq_doorbell(struct dw_pcie_ep *ep, u8 func_no,
541                                        u16 interrupt_num)
542 {
543         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
544         struct dw_pcie_ep_func *ep_func;
545         u32 msg_data;
546
547         ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
548         if (!ep_func || !ep_func->msix_cap)
549                 return -EINVAL;
550
551         msg_data = (func_no << PCIE_MSIX_DOORBELL_PF_SHIFT) |
552                    (interrupt_num - 1);
553
554         dw_pcie_writel_dbi(pci, PCIE_MSIX_DOORBELL, msg_data);
555
556         return 0;
557 }
558
559 int dw_pcie_ep_raise_msix_irq(struct dw_pcie_ep *ep, u8 func_no,
560                               u16 interrupt_num)
561 {
562         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
563         struct dw_pcie_ep_func *ep_func;
564         struct pci_epf_msix_tbl *msix_tbl;
565         struct pci_epc *epc = ep->epc;
566         unsigned int func_offset = 0;
567         u32 reg, msg_data, vec_ctrl;
568         unsigned int aligned_offset;
569         u32 tbl_offset;
570         u64 msg_addr;
571         int ret;
572         u8 bir;
573
574         ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
575         if (!ep_func || !ep_func->msix_cap)
576                 return -EINVAL;
577
578         func_offset = dw_pcie_ep_func_select(ep, func_no);
579
580         reg = ep_func->msix_cap + func_offset + PCI_MSIX_TABLE;
581         tbl_offset = dw_pcie_readl_dbi(pci, reg);
582         bir = (tbl_offset & PCI_MSIX_TABLE_BIR);
583         tbl_offset &= PCI_MSIX_TABLE_OFFSET;
584
585         msix_tbl = ep->epf_bar[bir]->addr + tbl_offset;
586         msg_addr = msix_tbl[(interrupt_num - 1)].msg_addr;
587         msg_data = msix_tbl[(interrupt_num - 1)].msg_data;
588         vec_ctrl = msix_tbl[(interrupt_num - 1)].vector_ctrl;
589
590         if (vec_ctrl & PCI_MSIX_ENTRY_CTRL_MASKBIT) {
591                 dev_dbg(pci->dev, "MSI-X entry ctrl set\n");
592                 return -EPERM;
593         }
594
595         aligned_offset = msg_addr & (epc->mem->window.page_size - 1);
596         ret = dw_pcie_ep_map_addr(epc, func_no, ep->msi_mem_phys,  msg_addr,
597                                   epc->mem->window.page_size);
598         if (ret)
599                 return ret;
600
601         writel(msg_data, ep->msi_mem + aligned_offset);
602
603         dw_pcie_ep_unmap_addr(epc, func_no, ep->msi_mem_phys);
604
605         return 0;
606 }
607
608 void dw_pcie_ep_exit(struct dw_pcie_ep *ep)
609 {
610         struct pci_epc *epc = ep->epc;
611
612         pci_epc_mem_free_addr(epc, ep->msi_mem_phys, ep->msi_mem,
613                               epc->mem->window.page_size);
614
615         pci_epc_mem_exit(epc);
616 }
617
618 static unsigned int dw_pcie_ep_find_ext_capability(struct dw_pcie *pci, int cap)
619 {
620         u32 header;
621         int pos = PCI_CFG_SPACE_SIZE;
622
623         while (pos) {
624                 header = dw_pcie_readl_dbi(pci, pos);
625                 if (PCI_EXT_CAP_ID(header) == cap)
626                         return pos;
627
628                 pos = PCI_EXT_CAP_NEXT(header);
629                 if (!pos)
630                         break;
631         }
632
633         return 0;
634 }
635
636 int dw_pcie_ep_init_complete(struct dw_pcie_ep *ep)
637 {
638         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
639         unsigned int offset;
640         unsigned int nbars;
641         u8 hdr_type;
642         u32 reg;
643         int i;
644
645         hdr_type = dw_pcie_readb_dbi(pci, PCI_HEADER_TYPE) &
646                    PCI_HEADER_TYPE_MASK;
647         if (hdr_type != PCI_HEADER_TYPE_NORMAL) {
648                 dev_err(pci->dev,
649                         "PCIe controller is not set to EP mode (hdr_type:0x%x)!\n",
650                         hdr_type);
651                 return -EIO;
652         }
653
654         offset = dw_pcie_ep_find_ext_capability(pci, PCI_EXT_CAP_ID_REBAR);
655
656         dw_pcie_dbi_ro_wr_en(pci);
657
658         if (offset) {
659                 reg = dw_pcie_readl_dbi(pci, offset + PCI_REBAR_CTRL);
660                 nbars = (reg & PCI_REBAR_CTRL_NBAR_MASK) >>
661                         PCI_REBAR_CTRL_NBAR_SHIFT;
662
663                 for (i = 0; i < nbars; i++, offset += PCI_REBAR_CTRL)
664                         dw_pcie_writel_dbi(pci, offset + PCI_REBAR_CAP, 0x0);
665         }
666
667         dw_pcie_setup(pci);
668         dw_pcie_dbi_ro_wr_dis(pci);
669
670         return 0;
671 }
672 EXPORT_SYMBOL_GPL(dw_pcie_ep_init_complete);
673
674 int dw_pcie_ep_init(struct dw_pcie_ep *ep)
675 {
676         int ret;
677         void *addr;
678         u8 func_no;
679         struct pci_epc *epc;
680         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
681         struct device *dev = pci->dev;
682         struct device_node *np = dev->of_node;
683         const struct pci_epc_features *epc_features;
684         struct dw_pcie_ep_func *ep_func;
685
686         INIT_LIST_HEAD(&ep->func_list);
687
688         if (!pci->dbi_base || !pci->dbi_base2) {
689                 dev_err(dev, "dbi_base/dbi_base2 is not populated\n");
690                 return -EINVAL;
691         }
692
693         ret = of_property_read_u32(np, "num-ib-windows", &ep->num_ib_windows);
694         if (ret < 0) {
695                 dev_err(dev, "Unable to read *num-ib-windows* property\n");
696                 return ret;
697         }
698         if (ep->num_ib_windows > MAX_IATU_IN) {
699                 dev_err(dev, "Invalid *num-ib-windows*\n");
700                 return -EINVAL;
701         }
702
703         ret = of_property_read_u32(np, "num-ob-windows", &ep->num_ob_windows);
704         if (ret < 0) {
705                 dev_err(dev, "Unable to read *num-ob-windows* property\n");
706                 return ret;
707         }
708         if (ep->num_ob_windows > MAX_IATU_OUT) {
709                 dev_err(dev, "Invalid *num-ob-windows*\n");
710                 return -EINVAL;
711         }
712
713         ep->ib_window_map = devm_kcalloc(dev,
714                                          BITS_TO_LONGS(ep->num_ib_windows),
715                                          sizeof(long),
716                                          GFP_KERNEL);
717         if (!ep->ib_window_map)
718                 return -ENOMEM;
719
720         ep->ob_window_map = devm_kcalloc(dev,
721                                          BITS_TO_LONGS(ep->num_ob_windows),
722                                          sizeof(long),
723                                          GFP_KERNEL);
724         if (!ep->ob_window_map)
725                 return -ENOMEM;
726
727         addr = devm_kcalloc(dev, ep->num_ob_windows, sizeof(phys_addr_t),
728                             GFP_KERNEL);
729         if (!addr)
730                 return -ENOMEM;
731         ep->outbound_addr = addr;
732
733         if (pci->link_gen < 1)
734                 pci->link_gen = of_pci_get_max_link_speed(np);
735
736         epc = devm_pci_epc_create(dev, &epc_ops);
737         if (IS_ERR(epc)) {
738                 dev_err(dev, "Failed to create epc device\n");
739                 return PTR_ERR(epc);
740         }
741
742         ep->epc = epc;
743         epc_set_drvdata(epc, ep);
744
745         ret = of_property_read_u8(np, "max-functions", &epc->max_functions);
746         if (ret < 0)
747                 epc->max_functions = 1;
748
749         for (func_no = 0; func_no < epc->max_functions; func_no++) {
750                 ep_func = devm_kzalloc(dev, sizeof(*ep_func), GFP_KERNEL);
751                 if (!ep_func)
752                         return -ENOMEM;
753
754                 ep_func->func_no = func_no;
755                 ep_func->msi_cap = dw_pcie_ep_find_capability(ep, func_no,
756                                                               PCI_CAP_ID_MSI);
757                 ep_func->msix_cap = dw_pcie_ep_find_capability(ep, func_no,
758                                                                PCI_CAP_ID_MSIX);
759
760                 list_add_tail(&ep_func->list, &ep->func_list);
761         }
762
763         if (ep->ops->ep_init)
764                 ep->ops->ep_init(ep);
765
766         ret = pci_epc_mem_init(epc, ep->phys_base, ep->addr_size,
767                                ep->page_size);
768         if (ret < 0) {
769                 dev_err(dev, "Failed to initialize address space\n");
770                 return ret;
771         }
772
773         ep->msi_mem = pci_epc_mem_alloc_addr(epc, &ep->msi_mem_phys,
774                                              epc->mem->window.page_size);
775         if (!ep->msi_mem) {
776                 dev_err(dev, "Failed to reserve memory for MSI/MSI-X\n");
777                 return -ENOMEM;
778         }
779
780         if (ep->ops->get_features) {
781                 epc_features = ep->ops->get_features(ep);
782                 if (epc_features->core_init_notifier)
783                         return 0;
784         }
785
786         return dw_pcie_ep_init_complete(ep);
787 }
788 EXPORT_SYMBOL_GPL(dw_pcie_ep_init);