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