]> Git Repo - linux.git/blob - drivers/pci/controller/dwc/pcie-designware-ep.c
Merge tag 'for-5.19-rc7-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave...
[linux.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 <[email protected]>
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,
158                                   enum pci_barno bar, dma_addr_t cpu_addr,
159                                   enum dw_pcie_as_type as_type)
160 {
161         int ret;
162         u32 free_win;
163         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
164
165         free_win = find_first_zero_bit(ep->ib_window_map, pci->num_ib_windows);
166         if (free_win >= pci->num_ib_windows) {
167                 dev_err(pci->dev, "No free inbound window\n");
168                 return -EINVAL;
169         }
170
171         ret = dw_pcie_prog_inbound_atu(pci, func_no, free_win, bar, cpu_addr,
172                                        as_type);
173         if (ret < 0) {
174                 dev_err(pci->dev, "Failed to program IB window\n");
175                 return ret;
176         }
177
178         ep->bar_to_atu[bar] = free_win;
179         set_bit(free_win, ep->ib_window_map);
180
181         return 0;
182 }
183
184 static int dw_pcie_ep_outbound_atu(struct dw_pcie_ep *ep, u8 func_no,
185                                    phys_addr_t phys_addr,
186                                    u64 pci_addr, size_t size)
187 {
188         u32 free_win;
189         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
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         dw_pcie_prog_ep_outbound_atu(pci, func_no, free_win, PCIE_ATU_TYPE_MEM,
198                                      phys_addr, pci_addr, size);
199
200         set_bit(free_win, ep->ob_window_map);
201         ep->outbound_addr[free_win] = phys_addr;
202
203         return 0;
204 }
205
206 static void dw_pcie_ep_clear_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
207                                  struct pci_epf_bar *epf_bar)
208 {
209         struct dw_pcie_ep *ep = epc_get_drvdata(epc);
210         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
211         enum pci_barno bar = epf_bar->barno;
212         u32 atu_index = ep->bar_to_atu[bar];
213
214         __dw_pcie_ep_reset_bar(pci, func_no, bar, epf_bar->flags);
215
216         dw_pcie_disable_atu(pci, atu_index, DW_PCIE_REGION_INBOUND);
217         clear_bit(atu_index, ep->ib_window_map);
218         ep->epf_bar[bar] = NULL;
219 }
220
221 static int dw_pcie_ep_set_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
222                               struct pci_epf_bar *epf_bar)
223 {
224         int ret;
225         struct dw_pcie_ep *ep = epc_get_drvdata(epc);
226         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
227         enum pci_barno bar = epf_bar->barno;
228         size_t size = epf_bar->size;
229         int flags = epf_bar->flags;
230         enum dw_pcie_as_type as_type;
231         u32 reg;
232         unsigned int func_offset = 0;
233
234         func_offset = dw_pcie_ep_func_select(ep, func_no);
235
236         reg = PCI_BASE_ADDRESS_0 + (4 * bar) + func_offset;
237
238         if (!(flags & PCI_BASE_ADDRESS_SPACE))
239                 as_type = DW_PCIE_AS_MEM;
240         else
241                 as_type = DW_PCIE_AS_IO;
242
243         ret = dw_pcie_ep_inbound_atu(ep, func_no, bar,
244                                      epf_bar->phys_addr, as_type);
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, atu_index, DW_PCIE_REGION_OUTBOUND);
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         if (pci->ops && pci->ops->stop_link)
439                 pci->ops->stop_link(pci);
440 }
441
442 static int dw_pcie_ep_start(struct pci_epc *epc)
443 {
444         struct dw_pcie_ep *ep = epc_get_drvdata(epc);
445         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
446
447         if (!pci->ops || !pci->ops->start_link)
448                 return -EINVAL;
449
450         return pci->ops->start_link(pci);
451 }
452
453 static const struct pci_epc_features*
454 dw_pcie_ep_get_features(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
455 {
456         struct dw_pcie_ep *ep = epc_get_drvdata(epc);
457
458         if (!ep->ops->get_features)
459                 return NULL;
460
461         return ep->ops->get_features(ep);
462 }
463
464 static const struct pci_epc_ops epc_ops = {
465         .write_header           = dw_pcie_ep_write_header,
466         .set_bar                = dw_pcie_ep_set_bar,
467         .clear_bar              = dw_pcie_ep_clear_bar,
468         .map_addr               = dw_pcie_ep_map_addr,
469         .unmap_addr             = dw_pcie_ep_unmap_addr,
470         .set_msi                = dw_pcie_ep_set_msi,
471         .get_msi                = dw_pcie_ep_get_msi,
472         .set_msix               = dw_pcie_ep_set_msix,
473         .get_msix               = dw_pcie_ep_get_msix,
474         .raise_irq              = dw_pcie_ep_raise_irq,
475         .start                  = dw_pcie_ep_start,
476         .stop                   = dw_pcie_ep_stop,
477         .get_features           = dw_pcie_ep_get_features,
478 };
479
480 int dw_pcie_ep_raise_legacy_irq(struct dw_pcie_ep *ep, u8 func_no)
481 {
482         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
483         struct device *dev = pci->dev;
484
485         dev_err(dev, "EP cannot trigger legacy IRQs\n");
486
487         return -EINVAL;
488 }
489 EXPORT_SYMBOL_GPL(dw_pcie_ep_raise_legacy_irq);
490
491 int dw_pcie_ep_raise_msi_irq(struct dw_pcie_ep *ep, u8 func_no,
492                              u8 interrupt_num)
493 {
494         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
495         struct dw_pcie_ep_func *ep_func;
496         struct pci_epc *epc = ep->epc;
497         unsigned int aligned_offset;
498         unsigned int func_offset = 0;
499         u16 msg_ctrl, msg_data;
500         u32 msg_addr_lower, msg_addr_upper, reg;
501         u64 msg_addr;
502         bool has_upper;
503         int ret;
504
505         ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
506         if (!ep_func || !ep_func->msi_cap)
507                 return -EINVAL;
508
509         func_offset = dw_pcie_ep_func_select(ep, func_no);
510
511         /* Raise MSI per the PCI Local Bus Specification Revision 3.0, 6.8.1. */
512         reg = ep_func->msi_cap + func_offset + PCI_MSI_FLAGS;
513         msg_ctrl = dw_pcie_readw_dbi(pci, reg);
514         has_upper = !!(msg_ctrl & PCI_MSI_FLAGS_64BIT);
515         reg = ep_func->msi_cap + func_offset + PCI_MSI_ADDRESS_LO;
516         msg_addr_lower = dw_pcie_readl_dbi(pci, reg);
517         if (has_upper) {
518                 reg = ep_func->msi_cap + func_offset + PCI_MSI_ADDRESS_HI;
519                 msg_addr_upper = dw_pcie_readl_dbi(pci, reg);
520                 reg = ep_func->msi_cap + func_offset + PCI_MSI_DATA_64;
521                 msg_data = dw_pcie_readw_dbi(pci, reg);
522         } else {
523                 msg_addr_upper = 0;
524                 reg = ep_func->msi_cap + func_offset + PCI_MSI_DATA_32;
525                 msg_data = dw_pcie_readw_dbi(pci, reg);
526         }
527         aligned_offset = msg_addr_lower & (epc->mem->window.page_size - 1);
528         msg_addr = ((u64)msg_addr_upper) << 32 |
529                         (msg_addr_lower & ~aligned_offset);
530         ret = dw_pcie_ep_map_addr(epc, func_no, 0, ep->msi_mem_phys, msg_addr,
531                                   epc->mem->window.page_size);
532         if (ret)
533                 return ret;
534
535         writel(msg_data | (interrupt_num - 1), ep->msi_mem + aligned_offset);
536
537         dw_pcie_ep_unmap_addr(epc, func_no, 0, ep->msi_mem_phys);
538
539         return 0;
540 }
541 EXPORT_SYMBOL_GPL(dw_pcie_ep_raise_msi_irq);
542
543 int dw_pcie_ep_raise_msix_irq_doorbell(struct dw_pcie_ep *ep, u8 func_no,
544                                        u16 interrupt_num)
545 {
546         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
547         struct dw_pcie_ep_func *ep_func;
548         u32 msg_data;
549
550         ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
551         if (!ep_func || !ep_func->msix_cap)
552                 return -EINVAL;
553
554         msg_data = (func_no << PCIE_MSIX_DOORBELL_PF_SHIFT) |
555                    (interrupt_num - 1);
556
557         dw_pcie_writel_dbi(pci, PCIE_MSIX_DOORBELL, msg_data);
558
559         return 0;
560 }
561
562 int dw_pcie_ep_raise_msix_irq(struct dw_pcie_ep *ep, u8 func_no,
563                               u16 interrupt_num)
564 {
565         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
566         struct dw_pcie_ep_func *ep_func;
567         struct pci_epf_msix_tbl *msix_tbl;
568         struct pci_epc *epc = ep->epc;
569         unsigned int func_offset = 0;
570         u32 reg, msg_data, vec_ctrl;
571         unsigned int aligned_offset;
572         u32 tbl_offset;
573         u64 msg_addr;
574         int ret;
575         u8 bir;
576
577         ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
578         if (!ep_func || !ep_func->msix_cap)
579                 return -EINVAL;
580
581         func_offset = dw_pcie_ep_func_select(ep, func_no);
582
583         reg = ep_func->msix_cap + func_offset + PCI_MSIX_TABLE;
584         tbl_offset = dw_pcie_readl_dbi(pci, reg);
585         bir = (tbl_offset & PCI_MSIX_TABLE_BIR);
586         tbl_offset &= PCI_MSIX_TABLE_OFFSET;
587
588         msix_tbl = ep->epf_bar[bir]->addr + tbl_offset;
589         msg_addr = msix_tbl[(interrupt_num - 1)].msg_addr;
590         msg_data = msix_tbl[(interrupt_num - 1)].msg_data;
591         vec_ctrl = msix_tbl[(interrupt_num - 1)].vector_ctrl;
592
593         if (vec_ctrl & PCI_MSIX_ENTRY_CTRL_MASKBIT) {
594                 dev_dbg(pci->dev, "MSI-X entry ctrl set\n");
595                 return -EPERM;
596         }
597
598         aligned_offset = msg_addr & (epc->mem->window.page_size - 1);
599         ret = dw_pcie_ep_map_addr(epc, func_no, 0, ep->msi_mem_phys, msg_addr,
600                                   epc->mem->window.page_size);
601         if (ret)
602                 return ret;
603
604         writel(msg_data, ep->msi_mem + aligned_offset);
605
606         dw_pcie_ep_unmap_addr(epc, func_no, 0, ep->msi_mem_phys);
607
608         return 0;
609 }
610
611 void dw_pcie_ep_exit(struct dw_pcie_ep *ep)
612 {
613         struct pci_epc *epc = ep->epc;
614
615         pci_epc_mem_free_addr(epc, ep->msi_mem_phys, ep->msi_mem,
616                               epc->mem->window.page_size);
617
618         pci_epc_mem_exit(epc);
619 }
620
621 static unsigned int dw_pcie_ep_find_ext_capability(struct dw_pcie *pci, int cap)
622 {
623         u32 header;
624         int pos = PCI_CFG_SPACE_SIZE;
625
626         while (pos) {
627                 header = dw_pcie_readl_dbi(pci, pos);
628                 if (PCI_EXT_CAP_ID(header) == cap)
629                         return pos;
630
631                 pos = PCI_EXT_CAP_NEXT(header);
632                 if (!pos)
633                         break;
634         }
635
636         return 0;
637 }
638
639 int dw_pcie_ep_init_complete(struct dw_pcie_ep *ep)
640 {
641         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
642         unsigned int offset;
643         unsigned int nbars;
644         u8 hdr_type;
645         u32 reg;
646         int i;
647
648         hdr_type = dw_pcie_readb_dbi(pci, PCI_HEADER_TYPE) &
649                    PCI_HEADER_TYPE_MASK;
650         if (hdr_type != PCI_HEADER_TYPE_NORMAL) {
651                 dev_err(pci->dev,
652                         "PCIe controller is not set to EP mode (hdr_type:0x%x)!\n",
653                         hdr_type);
654                 return -EIO;
655         }
656
657         offset = dw_pcie_ep_find_ext_capability(pci, PCI_EXT_CAP_ID_REBAR);
658
659         dw_pcie_dbi_ro_wr_en(pci);
660
661         if (offset) {
662                 reg = dw_pcie_readl_dbi(pci, offset + PCI_REBAR_CTRL);
663                 nbars = (reg & PCI_REBAR_CTRL_NBAR_MASK) >>
664                         PCI_REBAR_CTRL_NBAR_SHIFT;
665
666                 for (i = 0; i < nbars; i++, offset += PCI_REBAR_CTRL)
667                         dw_pcie_writel_dbi(pci, offset + PCI_REBAR_CAP, 0x0);
668         }
669
670         dw_pcie_setup(pci);
671         dw_pcie_dbi_ro_wr_dis(pci);
672
673         return 0;
674 }
675 EXPORT_SYMBOL_GPL(dw_pcie_ep_init_complete);
676
677 int dw_pcie_ep_init(struct dw_pcie_ep *ep)
678 {
679         int ret;
680         void *addr;
681         u8 func_no;
682         struct resource *res;
683         struct pci_epc *epc;
684         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
685         struct device *dev = pci->dev;
686         struct platform_device *pdev = to_platform_device(dev);
687         struct device_node *np = dev->of_node;
688         const struct pci_epc_features *epc_features;
689         struct dw_pcie_ep_func *ep_func;
690
691         INIT_LIST_HEAD(&ep->func_list);
692
693         if (!pci->dbi_base) {
694                 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbi");
695                 pci->dbi_base = devm_pci_remap_cfg_resource(dev, res);
696                 if (IS_ERR(pci->dbi_base))
697                         return PTR_ERR(pci->dbi_base);
698         }
699
700         if (!pci->dbi_base2) {
701                 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbi2");
702                 if (!res)
703                         pci->dbi_base2 = pci->dbi_base + SZ_4K;
704                 else {
705                         pci->dbi_base2 = devm_pci_remap_cfg_resource(dev, res);
706                         if (IS_ERR(pci->dbi_base2))
707                                 return PTR_ERR(pci->dbi_base2);
708                 }
709         }
710
711         dw_pcie_iatu_detect(pci);
712
713         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "addr_space");
714         if (!res)
715                 return -EINVAL;
716
717         ep->phys_base = res->start;
718         ep->addr_size = resource_size(res);
719
720         ep->ib_window_map = devm_kcalloc(dev,
721                                          BITS_TO_LONGS(pci->num_ib_windows),
722                                          sizeof(long),
723                                          GFP_KERNEL);
724         if (!ep->ib_window_map)
725                 return -ENOMEM;
726
727         ep->ob_window_map = devm_kcalloc(dev,
728                                          BITS_TO_LONGS(pci->num_ob_windows),
729                                          sizeof(long),
730                                          GFP_KERNEL);
731         if (!ep->ob_window_map)
732                 return -ENOMEM;
733
734         addr = devm_kcalloc(dev, pci->num_ob_windows, sizeof(phys_addr_t),
735                             GFP_KERNEL);
736         if (!addr)
737                 return -ENOMEM;
738         ep->outbound_addr = addr;
739
740         if (pci->link_gen < 1)
741                 pci->link_gen = of_pci_get_max_link_speed(np);
742
743         epc = devm_pci_epc_create(dev, &epc_ops);
744         if (IS_ERR(epc)) {
745                 dev_err(dev, "Failed to create epc device\n");
746                 return PTR_ERR(epc);
747         }
748
749         ep->epc = epc;
750         epc_set_drvdata(epc, ep);
751
752         ret = of_property_read_u8(np, "max-functions", &epc->max_functions);
753         if (ret < 0)
754                 epc->max_functions = 1;
755
756         for (func_no = 0; func_no < epc->max_functions; func_no++) {
757                 ep_func = devm_kzalloc(dev, sizeof(*ep_func), GFP_KERNEL);
758                 if (!ep_func)
759                         return -ENOMEM;
760
761                 ep_func->func_no = func_no;
762                 ep_func->msi_cap = dw_pcie_ep_find_capability(ep, func_no,
763                                                               PCI_CAP_ID_MSI);
764                 ep_func->msix_cap = dw_pcie_ep_find_capability(ep, func_no,
765                                                                PCI_CAP_ID_MSIX);
766
767                 list_add_tail(&ep_func->list, &ep->func_list);
768         }
769
770         if (ep->ops->ep_init)
771                 ep->ops->ep_init(ep);
772
773         ret = pci_epc_mem_init(epc, ep->phys_base, ep->addr_size,
774                                ep->page_size);
775         if (ret < 0) {
776                 dev_err(dev, "Failed to initialize address space\n");
777                 return ret;
778         }
779
780         ep->msi_mem = pci_epc_mem_alloc_addr(epc, &ep->msi_mem_phys,
781                                              epc->mem->window.page_size);
782         if (!ep->msi_mem) {
783                 dev_err(dev, "Failed to reserve memory for MSI/MSI-X\n");
784                 return -ENOMEM;
785         }
786
787         if (ep->ops->get_features) {
788                 epc_features = ep->ops->get_features(ep);
789                 if (epc_features->core_init_notifier)
790                         return 0;
791         }
792
793         return dw_pcie_ep_init_complete(ep);
794 }
795 EXPORT_SYMBOL_GPL(dw_pcie_ep_init);
This page took 0.08795 seconds and 4 git commands to generate.