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 /**
19 * dw_pcie_ep_get_func_from_ep - Get the struct dw_pcie_ep_func corresponding to
20 * the endpoint function
21 * @ep: DWC EP device
22 * @func_no: Function number of the endpoint device
23 *
24 * Return: struct dw_pcie_ep_func if success, NULL otherwise.
25 */
26 struct dw_pcie_ep_func *
dw_pcie_ep_get_func_from_ep(struct dw_pcie_ep * ep,u8 func_no)27 dw_pcie_ep_get_func_from_ep(struct dw_pcie_ep *ep, u8 func_no)
28 {
29 struct dw_pcie_ep_func *ep_func;
30
31 list_for_each_entry(ep_func, &ep->func_list, list) {
32 if (ep_func->func_no == func_no)
33 return ep_func;
34 }
35
36 return NULL;
37 }
38
__dw_pcie_ep_reset_bar(struct dw_pcie * pci,u8 func_no,enum pci_barno bar,int flags)39 static void __dw_pcie_ep_reset_bar(struct dw_pcie *pci, u8 func_no,
40 enum pci_barno bar, int flags)
41 {
42 struct dw_pcie_ep *ep = &pci->ep;
43 u32 reg;
44
45 reg = PCI_BASE_ADDRESS_0 + (4 * bar);
46 dw_pcie_dbi_ro_wr_en(pci);
47 dw_pcie_ep_writel_dbi2(ep, func_no, reg, 0x0);
48 dw_pcie_ep_writel_dbi(ep, func_no, reg, 0x0);
49 if (flags & PCI_BASE_ADDRESS_MEM_TYPE_64) {
50 dw_pcie_ep_writel_dbi2(ep, func_no, reg + 4, 0x0);
51 dw_pcie_ep_writel_dbi(ep, func_no, reg + 4, 0x0);
52 }
53 dw_pcie_dbi_ro_wr_dis(pci);
54 }
55
56 /**
57 * dw_pcie_ep_reset_bar - Reset endpoint BAR
58 * @pci: DWC PCI device
59 * @bar: BAR number of the endpoint
60 */
dw_pcie_ep_reset_bar(struct dw_pcie * pci,enum pci_barno bar)61 void dw_pcie_ep_reset_bar(struct dw_pcie *pci, enum pci_barno bar)
62 {
63 u8 func_no, funcs;
64
65 funcs = pci->ep.epc->max_functions;
66
67 for (func_no = 0; func_no < funcs; func_no++)
68 __dw_pcie_ep_reset_bar(pci, func_no, bar, 0);
69 }
70 EXPORT_SYMBOL_GPL(dw_pcie_ep_reset_bar);
71
__dw_pcie_ep_find_next_cap(struct dw_pcie_ep * ep,u8 func_no,u8 cap_ptr,u8 cap)72 static u8 __dw_pcie_ep_find_next_cap(struct dw_pcie_ep *ep, u8 func_no,
73 u8 cap_ptr, u8 cap)
74 {
75 u8 cap_id, next_cap_ptr;
76 u16 reg;
77
78 if (!cap_ptr)
79 return 0;
80
81 reg = dw_pcie_ep_readw_dbi(ep, func_no, cap_ptr);
82 cap_id = (reg & 0x00ff);
83
84 if (cap_id > PCI_CAP_ID_MAX)
85 return 0;
86
87 if (cap_id == cap)
88 return cap_ptr;
89
90 next_cap_ptr = (reg & 0xff00) >> 8;
91 return __dw_pcie_ep_find_next_cap(ep, func_no, next_cap_ptr, cap);
92 }
93
dw_pcie_ep_find_capability(struct dw_pcie_ep * ep,u8 func_no,u8 cap)94 static u8 dw_pcie_ep_find_capability(struct dw_pcie_ep *ep, u8 func_no, u8 cap)
95 {
96 u8 next_cap_ptr;
97 u16 reg;
98
99 reg = dw_pcie_ep_readw_dbi(ep, func_no, PCI_CAPABILITY_LIST);
100 next_cap_ptr = (reg & 0x00ff);
101
102 return __dw_pcie_ep_find_next_cap(ep, func_no, next_cap_ptr, cap);
103 }
104
dw_pcie_ep_write_header(struct pci_epc * epc,u8 func_no,u8 vfunc_no,struct pci_epf_header * hdr)105 static int dw_pcie_ep_write_header(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
106 struct pci_epf_header *hdr)
107 {
108 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
109 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
110
111 dw_pcie_dbi_ro_wr_en(pci);
112 dw_pcie_ep_writew_dbi(ep, func_no, PCI_VENDOR_ID, hdr->vendorid);
113 dw_pcie_ep_writew_dbi(ep, func_no, PCI_DEVICE_ID, hdr->deviceid);
114 dw_pcie_ep_writeb_dbi(ep, func_no, PCI_REVISION_ID, hdr->revid);
115 dw_pcie_ep_writeb_dbi(ep, func_no, PCI_CLASS_PROG, hdr->progif_code);
116 dw_pcie_ep_writew_dbi(ep, func_no, PCI_CLASS_DEVICE,
117 hdr->subclass_code | hdr->baseclass_code << 8);
118 dw_pcie_ep_writeb_dbi(ep, func_no, PCI_CACHE_LINE_SIZE,
119 hdr->cache_line_size);
120 dw_pcie_ep_writew_dbi(ep, func_no, PCI_SUBSYSTEM_VENDOR_ID,
121 hdr->subsys_vendor_id);
122 dw_pcie_ep_writew_dbi(ep, func_no, PCI_SUBSYSTEM_ID, hdr->subsys_id);
123 dw_pcie_ep_writeb_dbi(ep, func_no, PCI_INTERRUPT_PIN,
124 hdr->interrupt_pin);
125 dw_pcie_dbi_ro_wr_dis(pci);
126
127 return 0;
128 }
129
dw_pcie_ep_inbound_atu(struct dw_pcie_ep * ep,u8 func_no,int type,dma_addr_t cpu_addr,enum pci_barno bar)130 static int dw_pcie_ep_inbound_atu(struct dw_pcie_ep *ep, u8 func_no, int type,
131 dma_addr_t cpu_addr, enum pci_barno bar)
132 {
133 int ret;
134 u32 free_win;
135 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
136
137 if (!ep->bar_to_atu[bar])
138 free_win = find_first_zero_bit(ep->ib_window_map, pci->num_ib_windows);
139 else
140 free_win = ep->bar_to_atu[bar] - 1;
141
142 if (free_win >= pci->num_ib_windows) {
143 dev_err(pci->dev, "No free inbound window\n");
144 return -EINVAL;
145 }
146
147 ret = dw_pcie_prog_ep_inbound_atu(pci, func_no, free_win, type,
148 cpu_addr, bar);
149 if (ret < 0) {
150 dev_err(pci->dev, "Failed to program IB window\n");
151 return ret;
152 }
153
154 /*
155 * Always increment free_win before assignment, since value 0 is used to identify
156 * unallocated mapping.
157 */
158 ep->bar_to_atu[bar] = free_win + 1;
159 set_bit(free_win, ep->ib_window_map);
160
161 return 0;
162 }
163
dw_pcie_ep_outbound_atu(struct dw_pcie_ep * ep,struct dw_pcie_ob_atu_cfg * atu)164 static int dw_pcie_ep_outbound_atu(struct dw_pcie_ep *ep,
165 struct dw_pcie_ob_atu_cfg *atu)
166 {
167 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
168 u32 free_win;
169 int ret;
170
171 free_win = find_first_zero_bit(ep->ob_window_map, pci->num_ob_windows);
172 if (free_win >= pci->num_ob_windows) {
173 dev_err(pci->dev, "No free outbound window\n");
174 return -EINVAL;
175 }
176
177 atu->index = free_win;
178 ret = dw_pcie_prog_outbound_atu(pci, atu);
179 if (ret)
180 return ret;
181
182 set_bit(free_win, ep->ob_window_map);
183 ep->outbound_addr[free_win] = atu->cpu_addr;
184
185 return 0;
186 }
187
dw_pcie_ep_clear_bar(struct pci_epc * epc,u8 func_no,u8 vfunc_no,struct pci_epf_bar * epf_bar)188 static void dw_pcie_ep_clear_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
189 struct pci_epf_bar *epf_bar)
190 {
191 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
192 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
193 enum pci_barno bar = epf_bar->barno;
194 u32 atu_index = ep->bar_to_atu[bar] - 1;
195
196 if (!ep->bar_to_atu[bar])
197 return;
198
199 __dw_pcie_ep_reset_bar(pci, func_no, bar, epf_bar->flags);
200
201 dw_pcie_disable_atu(pci, PCIE_ATU_REGION_DIR_IB, atu_index);
202 clear_bit(atu_index, ep->ib_window_map);
203 ep->epf_bar[bar] = NULL;
204 ep->bar_to_atu[bar] = 0;
205 }
206
dw_pcie_ep_set_bar(struct pci_epc * epc,u8 func_no,u8 vfunc_no,struct pci_epf_bar * epf_bar)207 static int dw_pcie_ep_set_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
208 struct pci_epf_bar *epf_bar)
209 {
210 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
211 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
212 enum pci_barno bar = epf_bar->barno;
213 size_t size = epf_bar->size;
214 int flags = epf_bar->flags;
215 int ret, type;
216 u32 reg;
217
218 /*
219 * DWC does not allow BAR pairs to overlap, e.g. you cannot combine BARs
220 * 1 and 2 to form a 64-bit BAR.
221 */
222 if ((flags & PCI_BASE_ADDRESS_MEM_TYPE_64) && (bar & 1))
223 return -EINVAL;
224
225 /*
226 * Certain EPF drivers dynamically change the physical address of a BAR
227 * (i.e. they call set_bar() twice, without ever calling clear_bar(), as
228 * calling clear_bar() would clear the BAR's PCI address assigned by the
229 * host).
230 */
231 if (ep->epf_bar[bar]) {
232 /*
233 * We can only dynamically change a BAR if the new BAR size and
234 * BAR flags do not differ from the existing configuration.
235 */
236 if (ep->epf_bar[bar]->barno != bar ||
237 ep->epf_bar[bar]->size != size ||
238 ep->epf_bar[bar]->flags != flags)
239 return -EINVAL;
240
241 /*
242 * When dynamically changing a BAR, skip writing the BAR reg, as
243 * that would clear the BAR's PCI address assigned by the host.
244 */
245 goto config_atu;
246 }
247
248 reg = PCI_BASE_ADDRESS_0 + (4 * bar);
249
250 dw_pcie_dbi_ro_wr_en(pci);
251
252 dw_pcie_ep_writel_dbi2(ep, func_no, reg, lower_32_bits(size - 1));
253 dw_pcie_ep_writel_dbi(ep, func_no, reg, flags);
254
255 if (flags & PCI_BASE_ADDRESS_MEM_TYPE_64) {
256 dw_pcie_ep_writel_dbi2(ep, func_no, reg + 4, upper_32_bits(size - 1));
257 dw_pcie_ep_writel_dbi(ep, func_no, reg + 4, 0);
258 }
259
260 dw_pcie_dbi_ro_wr_dis(pci);
261
262 config_atu:
263 if (!(flags & PCI_BASE_ADDRESS_SPACE))
264 type = PCIE_ATU_TYPE_MEM;
265 else
266 type = PCIE_ATU_TYPE_IO;
267
268 ret = dw_pcie_ep_inbound_atu(ep, func_no, type, epf_bar->phys_addr, bar);
269 if (ret)
270 return ret;
271
272 ep->epf_bar[bar] = epf_bar;
273
274 return 0;
275 }
276
dw_pcie_find_index(struct dw_pcie_ep * ep,phys_addr_t addr,u32 * atu_index)277 static int dw_pcie_find_index(struct dw_pcie_ep *ep, phys_addr_t addr,
278 u32 *atu_index)
279 {
280 u32 index;
281 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
282
283 for_each_set_bit(index, ep->ob_window_map, pci->num_ob_windows) {
284 if (ep->outbound_addr[index] != addr)
285 continue;
286 *atu_index = index;
287 return 0;
288 }
289
290 return -EINVAL;
291 }
292
dw_pcie_ep_unmap_addr(struct pci_epc * epc,u8 func_no,u8 vfunc_no,phys_addr_t addr)293 static void dw_pcie_ep_unmap_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
294 phys_addr_t addr)
295 {
296 int ret;
297 u32 atu_index;
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_find_index(ep, addr, &atu_index);
302 if (ret < 0)
303 return;
304
305 dw_pcie_disable_atu(pci, PCIE_ATU_REGION_DIR_OB, atu_index);
306 clear_bit(atu_index, ep->ob_window_map);
307 }
308
dw_pcie_ep_map_addr(struct pci_epc * epc,u8 func_no,u8 vfunc_no,phys_addr_t addr,u64 pci_addr,size_t size)309 static int dw_pcie_ep_map_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
310 phys_addr_t addr, u64 pci_addr, size_t size)
311 {
312 int ret;
313 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
314 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
315 struct dw_pcie_ob_atu_cfg atu = { 0 };
316
317 atu.func_no = func_no;
318 atu.type = PCIE_ATU_TYPE_MEM;
319 atu.cpu_addr = addr;
320 atu.pci_addr = pci_addr;
321 atu.size = size;
322 ret = dw_pcie_ep_outbound_atu(ep, &atu);
323 if (ret) {
324 dev_err(pci->dev, "Failed to enable address\n");
325 return ret;
326 }
327
328 return 0;
329 }
330
dw_pcie_ep_get_msi(struct pci_epc * epc,u8 func_no,u8 vfunc_no)331 static int dw_pcie_ep_get_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
332 {
333 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
334 struct dw_pcie_ep_func *ep_func;
335 u32 val, reg;
336
337 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
338 if (!ep_func || !ep_func->msi_cap)
339 return -EINVAL;
340
341 reg = ep_func->msi_cap + PCI_MSI_FLAGS;
342 val = dw_pcie_ep_readw_dbi(ep, func_no, reg);
343 if (!(val & PCI_MSI_FLAGS_ENABLE))
344 return -EINVAL;
345
346 val = FIELD_GET(PCI_MSI_FLAGS_QSIZE, val);
347
348 return val;
349 }
350
dw_pcie_ep_set_msi(struct pci_epc * epc,u8 func_no,u8 vfunc_no,u8 interrupts)351 static int dw_pcie_ep_set_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
352 u8 interrupts)
353 {
354 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
355 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
356 struct dw_pcie_ep_func *ep_func;
357 u32 val, reg;
358
359 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
360 if (!ep_func || !ep_func->msi_cap)
361 return -EINVAL;
362
363 reg = ep_func->msi_cap + PCI_MSI_FLAGS;
364 val = dw_pcie_ep_readw_dbi(ep, func_no, reg);
365 val &= ~PCI_MSI_FLAGS_QMASK;
366 val |= FIELD_PREP(PCI_MSI_FLAGS_QMASK, interrupts);
367 dw_pcie_dbi_ro_wr_en(pci);
368 dw_pcie_ep_writew_dbi(ep, func_no, reg, val);
369 dw_pcie_dbi_ro_wr_dis(pci);
370
371 return 0;
372 }
373
dw_pcie_ep_get_msix(struct pci_epc * epc,u8 func_no,u8 vfunc_no)374 static int dw_pcie_ep_get_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
375 {
376 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
377 struct dw_pcie_ep_func *ep_func;
378 u32 val, reg;
379
380 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
381 if (!ep_func || !ep_func->msix_cap)
382 return -EINVAL;
383
384 reg = ep_func->msix_cap + PCI_MSIX_FLAGS;
385 val = dw_pcie_ep_readw_dbi(ep, func_no, reg);
386 if (!(val & PCI_MSIX_FLAGS_ENABLE))
387 return -EINVAL;
388
389 val &= PCI_MSIX_FLAGS_QSIZE;
390
391 return val;
392 }
393
dw_pcie_ep_set_msix(struct pci_epc * epc,u8 func_no,u8 vfunc_no,u16 interrupts,enum pci_barno bir,u32 offset)394 static int dw_pcie_ep_set_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
395 u16 interrupts, enum pci_barno bir, u32 offset)
396 {
397 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
398 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
399 struct dw_pcie_ep_func *ep_func;
400 u32 val, reg;
401 u16 actual_interrupts = interrupts + 1;
402
403 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
404 if (!ep_func || !ep_func->msix_cap)
405 return -EINVAL;
406
407 dw_pcie_dbi_ro_wr_en(pci);
408
409 reg = ep_func->msix_cap + PCI_MSIX_FLAGS;
410 val = dw_pcie_ep_readw_dbi(ep, func_no, reg);
411 val &= ~PCI_MSIX_FLAGS_QSIZE;
412 val |= interrupts; /* 0's based value */
413 dw_pcie_writew_dbi(pci, reg, val);
414
415 reg = ep_func->msix_cap + PCI_MSIX_TABLE;
416 val = offset | bir;
417 dw_pcie_ep_writel_dbi(ep, func_no, reg, val);
418
419 reg = ep_func->msix_cap + PCI_MSIX_PBA;
420 val = (offset + (actual_interrupts * PCI_MSIX_ENTRY_SIZE)) | bir;
421 dw_pcie_ep_writel_dbi(ep, func_no, reg, val);
422
423 dw_pcie_dbi_ro_wr_dis(pci);
424
425 return 0;
426 }
427
dw_pcie_ep_raise_irq(struct pci_epc * epc,u8 func_no,u8 vfunc_no,unsigned int type,u16 interrupt_num)428 static int dw_pcie_ep_raise_irq(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
429 unsigned int type, u16 interrupt_num)
430 {
431 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
432
433 if (!ep->ops->raise_irq)
434 return -EINVAL;
435
436 return ep->ops->raise_irq(ep, func_no, type, interrupt_num);
437 }
438
dw_pcie_ep_stop(struct pci_epc * epc)439 static void dw_pcie_ep_stop(struct pci_epc *epc)
440 {
441 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
442 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
443
444 dw_pcie_stop_link(pci);
445 }
446
dw_pcie_ep_start(struct pci_epc * epc)447 static int dw_pcie_ep_start(struct pci_epc *epc)
448 {
449 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
450 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
451
452 return dw_pcie_start_link(pci);
453 }
454
455 static const struct pci_epc_features*
dw_pcie_ep_get_features(struct pci_epc * epc,u8 func_no,u8 vfunc_no)456 dw_pcie_ep_get_features(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
457 {
458 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
459
460 if (!ep->ops->get_features)
461 return NULL;
462
463 return ep->ops->get_features(ep);
464 }
465
466 static const struct pci_epc_ops epc_ops = {
467 .write_header = dw_pcie_ep_write_header,
468 .set_bar = dw_pcie_ep_set_bar,
469 .clear_bar = dw_pcie_ep_clear_bar,
470 .map_addr = dw_pcie_ep_map_addr,
471 .unmap_addr = dw_pcie_ep_unmap_addr,
472 .set_msi = dw_pcie_ep_set_msi,
473 .get_msi = dw_pcie_ep_get_msi,
474 .set_msix = dw_pcie_ep_set_msix,
475 .get_msix = dw_pcie_ep_get_msix,
476 .raise_irq = dw_pcie_ep_raise_irq,
477 .start = dw_pcie_ep_start,
478 .stop = dw_pcie_ep_stop,
479 .get_features = dw_pcie_ep_get_features,
480 };
481
482 /**
483 * dw_pcie_ep_raise_intx_irq - Raise INTx IRQ to the host
484 * @ep: DWC EP device
485 * @func_no: Function number of the endpoint
486 *
487 * Return: 0 if success, errono otherwise.
488 */
dw_pcie_ep_raise_intx_irq(struct dw_pcie_ep * ep,u8 func_no)489 int dw_pcie_ep_raise_intx_irq(struct dw_pcie_ep *ep, u8 func_no)
490 {
491 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
492 struct device *dev = pci->dev;
493
494 dev_err(dev, "EP cannot raise INTX IRQs\n");
495
496 return -EINVAL;
497 }
498 EXPORT_SYMBOL_GPL(dw_pcie_ep_raise_intx_irq);
499
500 /**
501 * dw_pcie_ep_raise_msi_irq - Raise MSI IRQ to the host
502 * @ep: DWC EP device
503 * @func_no: Function number of the endpoint
504 * @interrupt_num: Interrupt number to be raised
505 *
506 * Return: 0 if success, errono otherwise.
507 */
dw_pcie_ep_raise_msi_irq(struct dw_pcie_ep * ep,u8 func_no,u8 interrupt_num)508 int dw_pcie_ep_raise_msi_irq(struct dw_pcie_ep *ep, u8 func_no,
509 u8 interrupt_num)
510 {
511 u32 msg_addr_lower, msg_addr_upper, reg;
512 struct dw_pcie_ep_func *ep_func;
513 struct pci_epc *epc = ep->epc;
514 unsigned int aligned_offset;
515 u16 msg_ctrl, msg_data;
516 bool has_upper;
517 u64 msg_addr;
518 int ret;
519
520 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
521 if (!ep_func || !ep_func->msi_cap)
522 return -EINVAL;
523
524 /* Raise MSI per the PCI Local Bus Specification Revision 3.0, 6.8.1. */
525 reg = ep_func->msi_cap + PCI_MSI_FLAGS;
526 msg_ctrl = dw_pcie_ep_readw_dbi(ep, func_no, reg);
527 has_upper = !!(msg_ctrl & PCI_MSI_FLAGS_64BIT);
528 reg = ep_func->msi_cap + PCI_MSI_ADDRESS_LO;
529 msg_addr_lower = dw_pcie_ep_readl_dbi(ep, func_no, reg);
530 if (has_upper) {
531 reg = ep_func->msi_cap + PCI_MSI_ADDRESS_HI;
532 msg_addr_upper = dw_pcie_ep_readl_dbi(ep, func_no, reg);
533 reg = ep_func->msi_cap + PCI_MSI_DATA_64;
534 msg_data = dw_pcie_ep_readw_dbi(ep, func_no, reg);
535 } else {
536 msg_addr_upper = 0;
537 reg = ep_func->msi_cap + PCI_MSI_DATA_32;
538 msg_data = dw_pcie_ep_readw_dbi(ep, func_no, reg);
539 }
540 msg_addr = ((u64)msg_addr_upper) << 32 | msg_addr_lower;
541
542 aligned_offset = msg_addr & (epc->mem->window.page_size - 1);
543 msg_addr = ALIGN_DOWN(msg_addr, epc->mem->window.page_size);
544 ret = dw_pcie_ep_map_addr(epc, func_no, 0, ep->msi_mem_phys, msg_addr,
545 epc->mem->window.page_size);
546 if (ret)
547 return ret;
548
549 writel(msg_data | (interrupt_num - 1), ep->msi_mem + aligned_offset);
550
551 dw_pcie_ep_unmap_addr(epc, func_no, 0, ep->msi_mem_phys);
552
553 return 0;
554 }
555 EXPORT_SYMBOL_GPL(dw_pcie_ep_raise_msi_irq);
556
557 /**
558 * dw_pcie_ep_raise_msix_irq_doorbell - Raise MSI-X to the host using Doorbell
559 * method
560 * @ep: DWC EP device
561 * @func_no: Function number of the endpoint device
562 * @interrupt_num: Interrupt number to be raised
563 *
564 * Return: 0 if success, errno otherwise.
565 */
dw_pcie_ep_raise_msix_irq_doorbell(struct dw_pcie_ep * ep,u8 func_no,u16 interrupt_num)566 int dw_pcie_ep_raise_msix_irq_doorbell(struct dw_pcie_ep *ep, u8 func_no,
567 u16 interrupt_num)
568 {
569 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
570 struct dw_pcie_ep_func *ep_func;
571 u32 msg_data;
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 msg_data = (func_no << PCIE_MSIX_DOORBELL_PF_SHIFT) |
578 (interrupt_num - 1);
579
580 dw_pcie_writel_dbi(pci, PCIE_MSIX_DOORBELL, msg_data);
581
582 return 0;
583 }
584
585 /**
586 * dw_pcie_ep_raise_msix_irq - Raise MSI-X to the host
587 * @ep: DWC EP device
588 * @func_no: Function number of the endpoint device
589 * @interrupt_num: Interrupt number to be raised
590 *
591 * Return: 0 if success, errno otherwise.
592 */
dw_pcie_ep_raise_msix_irq(struct dw_pcie_ep * ep,u8 func_no,u16 interrupt_num)593 int dw_pcie_ep_raise_msix_irq(struct dw_pcie_ep *ep, u8 func_no,
594 u16 interrupt_num)
595 {
596 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
597 struct pci_epf_msix_tbl *msix_tbl;
598 struct dw_pcie_ep_func *ep_func;
599 struct pci_epc *epc = ep->epc;
600 u32 reg, msg_data, vec_ctrl;
601 unsigned int aligned_offset;
602 u32 tbl_offset;
603 u64 msg_addr;
604 int ret;
605 u8 bir;
606
607 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
608 if (!ep_func || !ep_func->msix_cap)
609 return -EINVAL;
610
611 reg = ep_func->msix_cap + PCI_MSIX_TABLE;
612 tbl_offset = dw_pcie_ep_readl_dbi(ep, func_no, reg);
613 bir = FIELD_GET(PCI_MSIX_TABLE_BIR, tbl_offset);
614 tbl_offset &= PCI_MSIX_TABLE_OFFSET;
615
616 msix_tbl = ep->epf_bar[bir]->addr + tbl_offset;
617 msg_addr = msix_tbl[(interrupt_num - 1)].msg_addr;
618 msg_data = msix_tbl[(interrupt_num - 1)].msg_data;
619 vec_ctrl = msix_tbl[(interrupt_num - 1)].vector_ctrl;
620
621 if (vec_ctrl & PCI_MSIX_ENTRY_CTRL_MASKBIT) {
622 dev_dbg(pci->dev, "MSI-X entry ctrl set\n");
623 return -EPERM;
624 }
625
626 aligned_offset = msg_addr & (epc->mem->window.page_size - 1);
627 msg_addr = ALIGN_DOWN(msg_addr, epc->mem->window.page_size);
628 ret = dw_pcie_ep_map_addr(epc, func_no, 0, ep->msi_mem_phys, msg_addr,
629 epc->mem->window.page_size);
630 if (ret)
631 return ret;
632
633 writel(msg_data, ep->msi_mem + aligned_offset);
634
635 dw_pcie_ep_unmap_addr(epc, func_no, 0, ep->msi_mem_phys);
636
637 return 0;
638 }
639
640 /**
641 * dw_pcie_ep_cleanup - Cleanup DWC EP resources after fundamental reset
642 * @ep: DWC EP device
643 *
644 * Cleans up the DWC EP specific resources like eDMA etc... after fundamental
645 * reset like PERST#. Note that this API is only applicable for drivers
646 * supporting PERST# or any other methods of fundamental reset.
647 */
dw_pcie_ep_cleanup(struct dw_pcie_ep * ep)648 void dw_pcie_ep_cleanup(struct dw_pcie_ep *ep)
649 {
650 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
651
652 dw_pcie_edma_remove(pci);
653 }
654 EXPORT_SYMBOL_GPL(dw_pcie_ep_cleanup);
655
656 /**
657 * dw_pcie_ep_deinit - Deinitialize the endpoint device
658 * @ep: DWC EP device
659 *
660 * Deinitialize the endpoint device. EPC device is not destroyed since that will
661 * be taken care by Devres.
662 */
dw_pcie_ep_deinit(struct dw_pcie_ep * ep)663 void dw_pcie_ep_deinit(struct dw_pcie_ep *ep)
664 {
665 struct pci_epc *epc = ep->epc;
666
667 dw_pcie_ep_cleanup(ep);
668
669 pci_epc_mem_free_addr(epc, ep->msi_mem_phys, ep->msi_mem,
670 epc->mem->window.page_size);
671
672 pci_epc_mem_exit(epc);
673 }
674 EXPORT_SYMBOL_GPL(dw_pcie_ep_deinit);
675
dw_pcie_ep_find_ext_capability(struct dw_pcie * pci,int cap)676 static unsigned int dw_pcie_ep_find_ext_capability(struct dw_pcie *pci, int cap)
677 {
678 u32 header;
679 int pos = PCI_CFG_SPACE_SIZE;
680
681 while (pos) {
682 header = dw_pcie_readl_dbi(pci, pos);
683 if (PCI_EXT_CAP_ID(header) == cap)
684 return pos;
685
686 pos = PCI_EXT_CAP_NEXT(header);
687 if (!pos)
688 break;
689 }
690
691 return 0;
692 }
693
dw_pcie_ep_init_non_sticky_registers(struct dw_pcie * pci)694 static void dw_pcie_ep_init_non_sticky_registers(struct dw_pcie *pci)
695 {
696 unsigned int offset;
697 unsigned int nbars;
698 u32 reg, i;
699
700 offset = dw_pcie_ep_find_ext_capability(pci, PCI_EXT_CAP_ID_REBAR);
701
702 dw_pcie_dbi_ro_wr_en(pci);
703
704 if (offset) {
705 reg = dw_pcie_readl_dbi(pci, offset + PCI_REBAR_CTRL);
706 nbars = (reg & PCI_REBAR_CTRL_NBAR_MASK) >>
707 PCI_REBAR_CTRL_NBAR_SHIFT;
708
709 /*
710 * PCIe r6.0, sec 7.8.6.2 require us to support at least one
711 * size in the range from 1 MB to 512 GB. Advertise support
712 * for 1 MB BAR size only.
713 */
714 for (i = 0; i < nbars; i++, offset += PCI_REBAR_CTRL)
715 dw_pcie_writel_dbi(pci, offset + PCI_REBAR_CAP, BIT(4));
716 }
717
718 dw_pcie_setup(pci);
719 dw_pcie_dbi_ro_wr_dis(pci);
720 }
721
722 /**
723 * dw_pcie_ep_init_registers - Initialize DWC EP specific registers
724 * @ep: DWC EP device
725 *
726 * Initialize the registers (CSRs) specific to DWC EP. This API should be called
727 * only when the endpoint receives an active refclk (either from host or
728 * generated locally).
729 */
dw_pcie_ep_init_registers(struct dw_pcie_ep * ep)730 int dw_pcie_ep_init_registers(struct dw_pcie_ep *ep)
731 {
732 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
733 struct dw_pcie_ep_func *ep_func;
734 struct device *dev = pci->dev;
735 struct pci_epc *epc = ep->epc;
736 u32 ptm_cap_base, reg;
737 u8 hdr_type;
738 u8 func_no;
739 void *addr;
740 int ret;
741
742 hdr_type = dw_pcie_readb_dbi(pci, PCI_HEADER_TYPE) &
743 PCI_HEADER_TYPE_MASK;
744 if (hdr_type != PCI_HEADER_TYPE_NORMAL) {
745 dev_err(pci->dev,
746 "PCIe controller is not set to EP mode (hdr_type:0x%x)!\n",
747 hdr_type);
748 return -EIO;
749 }
750
751 dw_pcie_version_detect(pci);
752
753 dw_pcie_iatu_detect(pci);
754
755 ret = dw_pcie_edma_detect(pci);
756 if (ret)
757 return ret;
758
759 ret = -ENOMEM;
760 if (!ep->ib_window_map) {
761 ep->ib_window_map = devm_bitmap_zalloc(dev, pci->num_ib_windows,
762 GFP_KERNEL);
763 if (!ep->ib_window_map)
764 goto err_remove_edma;
765 }
766
767 if (!ep->ob_window_map) {
768 ep->ob_window_map = devm_bitmap_zalloc(dev, pci->num_ob_windows,
769 GFP_KERNEL);
770 if (!ep->ob_window_map)
771 goto err_remove_edma;
772 }
773
774 if (!ep->outbound_addr) {
775 addr = devm_kcalloc(dev, pci->num_ob_windows, sizeof(phys_addr_t),
776 GFP_KERNEL);
777 if (!addr)
778 goto err_remove_edma;
779 ep->outbound_addr = addr;
780 }
781
782 for (func_no = 0; func_no < epc->max_functions; func_no++) {
783
784 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
785 if (ep_func)
786 continue;
787
788 ep_func = devm_kzalloc(dev, sizeof(*ep_func), GFP_KERNEL);
789 if (!ep_func)
790 goto err_remove_edma;
791
792 ep_func->func_no = func_no;
793 ep_func->msi_cap = dw_pcie_ep_find_capability(ep, func_no,
794 PCI_CAP_ID_MSI);
795 ep_func->msix_cap = dw_pcie_ep_find_capability(ep, func_no,
796 PCI_CAP_ID_MSIX);
797
798 list_add_tail(&ep_func->list, &ep->func_list);
799 }
800
801 if (ep->ops->init)
802 ep->ops->init(ep);
803
804 ptm_cap_base = dw_pcie_ep_find_ext_capability(pci, PCI_EXT_CAP_ID_PTM);
805
806 /*
807 * PTM responder capability can be disabled only after disabling
808 * PTM root capability.
809 */
810 if (ptm_cap_base) {
811 dw_pcie_dbi_ro_wr_en(pci);
812 reg = dw_pcie_readl_dbi(pci, ptm_cap_base + PCI_PTM_CAP);
813 reg &= ~PCI_PTM_CAP_ROOT;
814 dw_pcie_writel_dbi(pci, ptm_cap_base + PCI_PTM_CAP, reg);
815
816 reg = dw_pcie_readl_dbi(pci, ptm_cap_base + PCI_PTM_CAP);
817 reg &= ~(PCI_PTM_CAP_RES | PCI_PTM_GRANULARITY_MASK);
818 dw_pcie_writel_dbi(pci, ptm_cap_base + PCI_PTM_CAP, reg);
819 dw_pcie_dbi_ro_wr_dis(pci);
820 }
821
822 dw_pcie_ep_init_non_sticky_registers(pci);
823
824 return 0;
825
826 err_remove_edma:
827 dw_pcie_edma_remove(pci);
828
829 return ret;
830 }
831 EXPORT_SYMBOL_GPL(dw_pcie_ep_init_registers);
832
833 /**
834 * dw_pcie_ep_linkup - Notify EPF drivers about Link Up event
835 * @ep: DWC EP device
836 */
dw_pcie_ep_linkup(struct dw_pcie_ep * ep)837 void dw_pcie_ep_linkup(struct dw_pcie_ep *ep)
838 {
839 struct pci_epc *epc = ep->epc;
840
841 pci_epc_linkup(epc);
842 }
843 EXPORT_SYMBOL_GPL(dw_pcie_ep_linkup);
844
845 /**
846 * dw_pcie_ep_linkdown - Notify EPF drivers about Link Down event
847 * @ep: DWC EP device
848 *
849 * Non-sticky registers are also initialized before sending the notification to
850 * the EPF drivers. This is needed since the registers need to be initialized
851 * before the link comes back again.
852 */
dw_pcie_ep_linkdown(struct dw_pcie_ep * ep)853 void dw_pcie_ep_linkdown(struct dw_pcie_ep *ep)
854 {
855 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
856 struct pci_epc *epc = ep->epc;
857
858 /*
859 * Initialize the non-sticky DWC registers as they would've reset post
860 * Link Down. This is specifically needed for drivers not supporting
861 * PERST# as they have no way to reinitialize the registers before the
862 * link comes back again.
863 */
864 dw_pcie_ep_init_non_sticky_registers(pci);
865
866 pci_epc_linkdown(epc);
867 }
868 EXPORT_SYMBOL_GPL(dw_pcie_ep_linkdown);
869
870 /**
871 * dw_pcie_ep_init - Initialize the endpoint device
872 * @ep: DWC EP device
873 *
874 * Initialize the endpoint device. Allocate resources and create the EPC
875 * device with the endpoint framework.
876 *
877 * Return: 0 if success, errno otherwise.
878 */
dw_pcie_ep_init(struct dw_pcie_ep * ep)879 int dw_pcie_ep_init(struct dw_pcie_ep *ep)
880 {
881 int ret;
882 struct resource *res;
883 struct pci_epc *epc;
884 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
885 struct device *dev = pci->dev;
886 struct platform_device *pdev = to_platform_device(dev);
887 struct device_node *np = dev->of_node;
888
889 INIT_LIST_HEAD(&ep->func_list);
890
891 ret = dw_pcie_get_resources(pci);
892 if (ret)
893 return ret;
894
895 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "addr_space");
896 if (!res)
897 return -EINVAL;
898
899 ep->phys_base = res->start;
900 ep->addr_size = resource_size(res);
901
902 if (ep->ops->pre_init)
903 ep->ops->pre_init(ep);
904
905 epc = devm_pci_epc_create(dev, &epc_ops);
906 if (IS_ERR(epc)) {
907 dev_err(dev, "Failed to create epc device\n");
908 return PTR_ERR(epc);
909 }
910
911 ep->epc = epc;
912 epc_set_drvdata(epc, ep);
913
914 ret = of_property_read_u8(np, "max-functions", &epc->max_functions);
915 if (ret < 0)
916 epc->max_functions = 1;
917
918 ret = pci_epc_mem_init(epc, ep->phys_base, ep->addr_size,
919 ep->page_size);
920 if (ret < 0) {
921 dev_err(dev, "Failed to initialize address space\n");
922 return ret;
923 }
924
925 ep->msi_mem = pci_epc_mem_alloc_addr(epc, &ep->msi_mem_phys,
926 epc->mem->window.page_size);
927 if (!ep->msi_mem) {
928 ret = -ENOMEM;
929 dev_err(dev, "Failed to reserve memory for MSI/MSI-X\n");
930 goto err_exit_epc_mem;
931 }
932
933 return 0;
934
935 err_exit_epc_mem:
936 pci_epc_mem_exit(epc);
937
938 return ret;
939 }
940 EXPORT_SYMBOL_GPL(dw_pcie_ep_init);
941