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