drm: add modifiers for MediaTek tiled formats
[drm/drm-misc.git] / drivers / pci / controller / dwc / pcie-designware-ep.c
blobf3ac7d46a855b25737465e55247b1d354a6a2133
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Synopsys DesignWare PCIe Endpoint controller driver
5 * Copyright (C) 2017 Texas Instruments
6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
7 */
9 #include <linux/align.h>
10 #include <linux/bitfield.h>
11 #include <linux/of.h>
12 #include <linux/platform_device.h>
14 #include "pcie-designware.h"
15 #include <linux/pci-epc.h>
16 #include <linux/pci-epf.h>
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
24 * Return: struct dw_pcie_ep_func if success, NULL otherwise.
26 struct dw_pcie_ep_func *
27 dw_pcie_ep_get_func_from_ep(struct dw_pcie_ep *ep, u8 func_no)
29 struct dw_pcie_ep_func *ep_func;
31 list_for_each_entry(ep_func, &ep->func_list, list) {
32 if (ep_func->func_no == func_no)
33 return ep_func;
36 return NULL;
39 static void __dw_pcie_ep_reset_bar(struct dw_pcie *pci, u8 func_no,
40 enum pci_barno bar, int flags)
42 struct dw_pcie_ep *ep = &pci->ep;
43 u32 reg;
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);
53 dw_pcie_dbi_ro_wr_dis(pci);
56 /**
57 * dw_pcie_ep_reset_bar - Reset endpoint BAR
58 * @pci: DWC PCI device
59 * @bar: BAR number of the endpoint
61 void dw_pcie_ep_reset_bar(struct dw_pcie *pci, enum pci_barno bar)
63 u8 func_no, funcs;
65 funcs = pci->ep.epc->max_functions;
67 for (func_no = 0; func_no < funcs; func_no++)
68 __dw_pcie_ep_reset_bar(pci, func_no, bar, 0);
70 EXPORT_SYMBOL_GPL(dw_pcie_ep_reset_bar);
72 static u8 __dw_pcie_ep_find_next_cap(struct dw_pcie_ep *ep, u8 func_no,
73 u8 cap_ptr, u8 cap)
75 u8 cap_id, next_cap_ptr;
76 u16 reg;
78 if (!cap_ptr)
79 return 0;
81 reg = dw_pcie_ep_readw_dbi(ep, func_no, cap_ptr);
82 cap_id = (reg & 0x00ff);
84 if (cap_id > PCI_CAP_ID_MAX)
85 return 0;
87 if (cap_id == cap)
88 return cap_ptr;
90 next_cap_ptr = (reg & 0xff00) >> 8;
91 return __dw_pcie_ep_find_next_cap(ep, func_no, next_cap_ptr, cap);
94 static u8 dw_pcie_ep_find_capability(struct dw_pcie_ep *ep, u8 func_no, u8 cap)
96 u8 next_cap_ptr;
97 u16 reg;
99 reg = dw_pcie_ep_readw_dbi(ep, func_no, PCI_CAPABILITY_LIST);
100 next_cap_ptr = (reg & 0x00ff);
102 return __dw_pcie_ep_find_next_cap(ep, func_no, next_cap_ptr, cap);
105 static int dw_pcie_ep_write_header(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
106 struct pci_epf_header *hdr)
108 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
109 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
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);
127 return 0;
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)
133 int ret;
134 u32 free_win;
135 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
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;
142 if (free_win >= pci->num_ib_windows) {
143 dev_err(pci->dev, "No free inbound window\n");
144 return -EINVAL;
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;
155 * Always increment free_win before assignment, since value 0 is used to identify
156 * unallocated mapping.
158 ep->bar_to_atu[bar] = free_win + 1;
159 set_bit(free_win, ep->ib_window_map);
161 return 0;
164 static int dw_pcie_ep_outbound_atu(struct dw_pcie_ep *ep,
165 struct dw_pcie_ob_atu_cfg *atu)
167 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
168 u32 free_win;
169 int ret;
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;
177 atu->index = free_win;
178 ret = dw_pcie_prog_outbound_atu(pci, atu);
179 if (ret)
180 return ret;
182 set_bit(free_win, ep->ob_window_map);
183 ep->outbound_addr[free_win] = atu->cpu_addr;
185 return 0;
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)
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;
196 if (!ep->bar_to_atu[bar])
197 return;
199 __dw_pcie_ep_reset_bar(pci, func_no, bar, epf_bar->flags);
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;
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)
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;
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.
222 if ((flags & PCI_BASE_ADDRESS_MEM_TYPE_64) && (bar & 1))
223 return -EINVAL;
225 reg = PCI_BASE_ADDRESS_0 + (4 * bar);
227 if (!(flags & PCI_BASE_ADDRESS_SPACE))
228 type = PCIE_ATU_TYPE_MEM;
229 else
230 type = PCIE_ATU_TYPE_IO;
232 ret = dw_pcie_ep_inbound_atu(ep, func_no, type, epf_bar->phys_addr, bar);
233 if (ret)
234 return ret;
236 if (ep->epf_bar[bar])
237 return 0;
239 dw_pcie_dbi_ro_wr_en(pci);
241 dw_pcie_ep_writel_dbi2(ep, func_no, reg, lower_32_bits(size - 1));
242 dw_pcie_ep_writel_dbi(ep, func_no, reg, flags);
244 if (flags & PCI_BASE_ADDRESS_MEM_TYPE_64) {
245 dw_pcie_ep_writel_dbi2(ep, func_no, reg + 4, upper_32_bits(size - 1));
246 dw_pcie_ep_writel_dbi(ep, func_no, reg + 4, 0);
249 ep->epf_bar[bar] = epf_bar;
250 dw_pcie_dbi_ro_wr_dis(pci);
252 return 0;
255 static int dw_pcie_find_index(struct dw_pcie_ep *ep, phys_addr_t addr,
256 u32 *atu_index)
258 u32 index;
259 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
261 for (index = 0; index < pci->num_ob_windows; index++) {
262 if (ep->outbound_addr[index] != addr)
263 continue;
264 *atu_index = index;
265 return 0;
268 return -EINVAL;
271 static u64 dw_pcie_ep_align_addr(struct pci_epc *epc, u64 pci_addr,
272 size_t *pci_size, size_t *offset)
274 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
275 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
276 u64 mask = pci->region_align - 1;
277 size_t ofst = pci_addr & mask;
279 *pci_size = ALIGN(ofst + *pci_size, epc->mem->window.page_size);
280 *offset = ofst;
282 return pci_addr & ~mask;
285 static void dw_pcie_ep_unmap_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
286 phys_addr_t addr)
288 int ret;
289 u32 atu_index;
290 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
291 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
293 ret = dw_pcie_find_index(ep, addr, &atu_index);
294 if (ret < 0)
295 return;
297 ep->outbound_addr[atu_index] = 0;
298 dw_pcie_disable_atu(pci, PCIE_ATU_REGION_DIR_OB, atu_index);
299 clear_bit(atu_index, ep->ob_window_map);
302 static int dw_pcie_ep_map_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
303 phys_addr_t addr, u64 pci_addr, size_t size)
305 int ret;
306 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
307 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
308 struct dw_pcie_ob_atu_cfg atu = { 0 };
310 atu.func_no = func_no;
311 atu.type = PCIE_ATU_TYPE_MEM;
312 atu.cpu_addr = addr;
313 atu.pci_addr = pci_addr;
314 atu.size = size;
315 ret = dw_pcie_ep_outbound_atu(ep, &atu);
316 if (ret) {
317 dev_err(pci->dev, "Failed to enable address\n");
318 return ret;
321 return 0;
324 static int dw_pcie_ep_get_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
326 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
327 struct dw_pcie_ep_func *ep_func;
328 u32 val, reg;
330 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
331 if (!ep_func || !ep_func->msi_cap)
332 return -EINVAL;
334 reg = ep_func->msi_cap + PCI_MSI_FLAGS;
335 val = dw_pcie_ep_readw_dbi(ep, func_no, reg);
336 if (!(val & PCI_MSI_FLAGS_ENABLE))
337 return -EINVAL;
339 val = FIELD_GET(PCI_MSI_FLAGS_QSIZE, val);
341 return val;
344 static int dw_pcie_ep_set_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
345 u8 interrupts)
347 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
348 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
349 struct dw_pcie_ep_func *ep_func;
350 u32 val, reg;
352 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
353 if (!ep_func || !ep_func->msi_cap)
354 return -EINVAL;
356 reg = ep_func->msi_cap + PCI_MSI_FLAGS;
357 val = dw_pcie_ep_readw_dbi(ep, func_no, reg);
358 val &= ~PCI_MSI_FLAGS_QMASK;
359 val |= FIELD_PREP(PCI_MSI_FLAGS_QMASK, interrupts);
360 dw_pcie_dbi_ro_wr_en(pci);
361 dw_pcie_ep_writew_dbi(ep, func_no, reg, val);
362 dw_pcie_dbi_ro_wr_dis(pci);
364 return 0;
367 static int dw_pcie_ep_get_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
369 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
370 struct dw_pcie_ep_func *ep_func;
371 u32 val, reg;
373 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
374 if (!ep_func || !ep_func->msix_cap)
375 return -EINVAL;
377 reg = ep_func->msix_cap + PCI_MSIX_FLAGS;
378 val = dw_pcie_ep_readw_dbi(ep, func_no, reg);
379 if (!(val & PCI_MSIX_FLAGS_ENABLE))
380 return -EINVAL;
382 val &= PCI_MSIX_FLAGS_QSIZE;
384 return val;
387 static int dw_pcie_ep_set_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
388 u16 interrupts, enum pci_barno bir, u32 offset)
390 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
391 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
392 struct dw_pcie_ep_func *ep_func;
393 u32 val, reg;
395 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
396 if (!ep_func || !ep_func->msix_cap)
397 return -EINVAL;
399 dw_pcie_dbi_ro_wr_en(pci);
401 reg = ep_func->msix_cap + PCI_MSIX_FLAGS;
402 val = dw_pcie_ep_readw_dbi(ep, func_no, reg);
403 val &= ~PCI_MSIX_FLAGS_QSIZE;
404 val |= interrupts;
405 dw_pcie_writew_dbi(pci, reg, val);
407 reg = ep_func->msix_cap + PCI_MSIX_TABLE;
408 val = offset | bir;
409 dw_pcie_ep_writel_dbi(ep, func_no, reg, val);
411 reg = ep_func->msix_cap + PCI_MSIX_PBA;
412 val = (offset + (interrupts * PCI_MSIX_ENTRY_SIZE)) | bir;
413 dw_pcie_ep_writel_dbi(ep, func_no, reg, val);
415 dw_pcie_dbi_ro_wr_dis(pci);
417 return 0;
420 static int dw_pcie_ep_raise_irq(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
421 unsigned int type, u16 interrupt_num)
423 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
425 if (!ep->ops->raise_irq)
426 return -EINVAL;
428 return ep->ops->raise_irq(ep, func_no, type, interrupt_num);
431 static void dw_pcie_ep_stop(struct pci_epc *epc)
433 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
434 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
436 dw_pcie_stop_link(pci);
439 static int dw_pcie_ep_start(struct pci_epc *epc)
441 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
442 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
444 return dw_pcie_start_link(pci);
447 static const struct pci_epc_features*
448 dw_pcie_ep_get_features(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
450 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
452 if (!ep->ops->get_features)
453 return NULL;
455 return ep->ops->get_features(ep);
458 static const struct pci_epc_ops epc_ops = {
459 .write_header = dw_pcie_ep_write_header,
460 .set_bar = dw_pcie_ep_set_bar,
461 .clear_bar = dw_pcie_ep_clear_bar,
462 .align_addr = dw_pcie_ep_align_addr,
463 .map_addr = dw_pcie_ep_map_addr,
464 .unmap_addr = dw_pcie_ep_unmap_addr,
465 .set_msi = dw_pcie_ep_set_msi,
466 .get_msi = dw_pcie_ep_get_msi,
467 .set_msix = dw_pcie_ep_set_msix,
468 .get_msix = dw_pcie_ep_get_msix,
469 .raise_irq = dw_pcie_ep_raise_irq,
470 .start = dw_pcie_ep_start,
471 .stop = dw_pcie_ep_stop,
472 .get_features = dw_pcie_ep_get_features,
476 * dw_pcie_ep_raise_intx_irq - Raise INTx IRQ to the host
477 * @ep: DWC EP device
478 * @func_no: Function number of the endpoint
480 * Return: 0 if success, errono otherwise.
482 int dw_pcie_ep_raise_intx_irq(struct dw_pcie_ep *ep, u8 func_no)
484 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
485 struct device *dev = pci->dev;
487 dev_err(dev, "EP cannot raise INTX IRQs\n");
489 return -EINVAL;
491 EXPORT_SYMBOL_GPL(dw_pcie_ep_raise_intx_irq);
494 * dw_pcie_ep_raise_msi_irq - Raise MSI IRQ to the host
495 * @ep: DWC EP device
496 * @func_no: Function number of the endpoint
497 * @interrupt_num: Interrupt number to be raised
499 * Return: 0 if success, errono otherwise.
501 int dw_pcie_ep_raise_msi_irq(struct dw_pcie_ep *ep, u8 func_no,
502 u8 interrupt_num)
504 u32 msg_addr_lower, msg_addr_upper, reg;
505 struct dw_pcie_ep_func *ep_func;
506 struct pci_epc *epc = ep->epc;
507 size_t map_size = sizeof(u32);
508 size_t offset;
509 u16 msg_ctrl, msg_data;
510 bool has_upper;
511 u64 msg_addr;
512 int ret;
514 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
515 if (!ep_func || !ep_func->msi_cap)
516 return -EINVAL;
518 /* Raise MSI per the PCI Local Bus Specification Revision 3.0, 6.8.1. */
519 reg = ep_func->msi_cap + PCI_MSI_FLAGS;
520 msg_ctrl = dw_pcie_ep_readw_dbi(ep, func_no, reg);
521 has_upper = !!(msg_ctrl & PCI_MSI_FLAGS_64BIT);
522 reg = ep_func->msi_cap + PCI_MSI_ADDRESS_LO;
523 msg_addr_lower = dw_pcie_ep_readl_dbi(ep, func_no, reg);
524 if (has_upper) {
525 reg = ep_func->msi_cap + PCI_MSI_ADDRESS_HI;
526 msg_addr_upper = dw_pcie_ep_readl_dbi(ep, func_no, reg);
527 reg = ep_func->msi_cap + PCI_MSI_DATA_64;
528 msg_data = dw_pcie_ep_readw_dbi(ep, func_no, reg);
529 } else {
530 msg_addr_upper = 0;
531 reg = ep_func->msi_cap + PCI_MSI_DATA_32;
532 msg_data = dw_pcie_ep_readw_dbi(ep, func_no, reg);
534 msg_addr = ((u64)msg_addr_upper) << 32 | msg_addr_lower;
536 msg_addr = dw_pcie_ep_align_addr(epc, msg_addr, &map_size, &offset);
537 ret = dw_pcie_ep_map_addr(epc, func_no, 0, ep->msi_mem_phys, msg_addr,
538 map_size);
539 if (ret)
540 return ret;
542 writel(msg_data | (interrupt_num - 1), ep->msi_mem + offset);
544 dw_pcie_ep_unmap_addr(epc, func_no, 0, ep->msi_mem_phys);
546 return 0;
548 EXPORT_SYMBOL_GPL(dw_pcie_ep_raise_msi_irq);
551 * dw_pcie_ep_raise_msix_irq_doorbell - Raise MSI-X to the host using Doorbell
552 * method
553 * @ep: DWC EP device
554 * @func_no: Function number of the endpoint device
555 * @interrupt_num: Interrupt number to be raised
557 * Return: 0 if success, errno otherwise.
559 int dw_pcie_ep_raise_msix_irq_doorbell(struct dw_pcie_ep *ep, u8 func_no,
560 u16 interrupt_num)
562 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
563 struct dw_pcie_ep_func *ep_func;
564 u32 msg_data;
566 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
567 if (!ep_func || !ep_func->msix_cap)
568 return -EINVAL;
570 msg_data = (func_no << PCIE_MSIX_DOORBELL_PF_SHIFT) |
571 (interrupt_num - 1);
573 dw_pcie_writel_dbi(pci, PCIE_MSIX_DOORBELL, msg_data);
575 return 0;
579 * dw_pcie_ep_raise_msix_irq - Raise MSI-X to the host
580 * @ep: DWC EP device
581 * @func_no: Function number of the endpoint device
582 * @interrupt_num: Interrupt number to be raised
584 * Return: 0 if success, errno otherwise.
586 int dw_pcie_ep_raise_msix_irq(struct dw_pcie_ep *ep, u8 func_no,
587 u16 interrupt_num)
589 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
590 struct pci_epf_msix_tbl *msix_tbl;
591 struct dw_pcie_ep_func *ep_func;
592 struct pci_epc *epc = ep->epc;
593 size_t map_size = sizeof(u32);
594 size_t offset;
595 u32 reg, msg_data, vec_ctrl;
596 u32 tbl_offset;
597 u64 msg_addr;
598 int ret;
599 u8 bir;
601 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
602 if (!ep_func || !ep_func->msix_cap)
603 return -EINVAL;
605 reg = ep_func->msix_cap + PCI_MSIX_TABLE;
606 tbl_offset = dw_pcie_ep_readl_dbi(ep, func_no, reg);
607 bir = FIELD_GET(PCI_MSIX_TABLE_BIR, tbl_offset);
608 tbl_offset &= PCI_MSIX_TABLE_OFFSET;
610 msix_tbl = ep->epf_bar[bir]->addr + tbl_offset;
611 msg_addr = msix_tbl[(interrupt_num - 1)].msg_addr;
612 msg_data = msix_tbl[(interrupt_num - 1)].msg_data;
613 vec_ctrl = msix_tbl[(interrupt_num - 1)].vector_ctrl;
615 if (vec_ctrl & PCI_MSIX_ENTRY_CTRL_MASKBIT) {
616 dev_dbg(pci->dev, "MSI-X entry ctrl set\n");
617 return -EPERM;
620 msg_addr = dw_pcie_ep_align_addr(epc, msg_addr, &map_size, &offset);
621 ret = dw_pcie_ep_map_addr(epc, func_no, 0, ep->msi_mem_phys, msg_addr,
622 map_size);
623 if (ret)
624 return ret;
626 writel(msg_data, ep->msi_mem + offset);
628 dw_pcie_ep_unmap_addr(epc, func_no, 0, ep->msi_mem_phys);
630 return 0;
634 * dw_pcie_ep_cleanup - Cleanup DWC EP resources after fundamental reset
635 * @ep: DWC EP device
637 * Cleans up the DWC EP specific resources like eDMA etc... after fundamental
638 * reset like PERST#. Note that this API is only applicable for drivers
639 * supporting PERST# or any other methods of fundamental reset.
641 void dw_pcie_ep_cleanup(struct dw_pcie_ep *ep)
643 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
645 dw_pcie_edma_remove(pci);
647 EXPORT_SYMBOL_GPL(dw_pcie_ep_cleanup);
650 * dw_pcie_ep_deinit - Deinitialize the endpoint device
651 * @ep: DWC EP device
653 * Deinitialize the endpoint device. EPC device is not destroyed since that will
654 * be taken care by Devres.
656 void dw_pcie_ep_deinit(struct dw_pcie_ep *ep)
658 struct pci_epc *epc = ep->epc;
660 dw_pcie_ep_cleanup(ep);
662 pci_epc_mem_free_addr(epc, ep->msi_mem_phys, ep->msi_mem,
663 epc->mem->window.page_size);
665 pci_epc_mem_exit(epc);
667 EXPORT_SYMBOL_GPL(dw_pcie_ep_deinit);
669 static unsigned int dw_pcie_ep_find_ext_capability(struct dw_pcie *pci, int cap)
671 u32 header;
672 int pos = PCI_CFG_SPACE_SIZE;
674 while (pos) {
675 header = dw_pcie_readl_dbi(pci, pos);
676 if (PCI_EXT_CAP_ID(header) == cap)
677 return pos;
679 pos = PCI_EXT_CAP_NEXT(header);
680 if (!pos)
681 break;
684 return 0;
687 static void dw_pcie_ep_init_non_sticky_registers(struct dw_pcie *pci)
689 unsigned int offset;
690 unsigned int nbars;
691 u32 reg, i;
693 offset = dw_pcie_ep_find_ext_capability(pci, PCI_EXT_CAP_ID_REBAR);
695 dw_pcie_dbi_ro_wr_en(pci);
697 if (offset) {
698 reg = dw_pcie_readl_dbi(pci, offset + PCI_REBAR_CTRL);
699 nbars = (reg & PCI_REBAR_CTRL_NBAR_MASK) >>
700 PCI_REBAR_CTRL_NBAR_SHIFT;
703 * PCIe r6.0, sec 7.8.6.2 require us to support at least one
704 * size in the range from 1 MB to 512 GB. Advertise support
705 * for 1 MB BAR size only.
707 for (i = 0; i < nbars; i++, offset += PCI_REBAR_CTRL)
708 dw_pcie_writel_dbi(pci, offset + PCI_REBAR_CAP, BIT(4));
711 dw_pcie_setup(pci);
712 dw_pcie_dbi_ro_wr_dis(pci);
716 * dw_pcie_ep_init_registers - Initialize DWC EP specific registers
717 * @ep: DWC EP device
719 * Initialize the registers (CSRs) specific to DWC EP. This API should be called
720 * only when the endpoint receives an active refclk (either from host or
721 * generated locally).
723 int dw_pcie_ep_init_registers(struct dw_pcie_ep *ep)
725 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
726 struct dw_pcie_ep_func *ep_func;
727 struct device *dev = pci->dev;
728 struct pci_epc *epc = ep->epc;
729 u32 ptm_cap_base, reg;
730 u8 hdr_type;
731 u8 func_no;
732 void *addr;
733 int ret;
735 hdr_type = dw_pcie_readb_dbi(pci, PCI_HEADER_TYPE) &
736 PCI_HEADER_TYPE_MASK;
737 if (hdr_type != PCI_HEADER_TYPE_NORMAL) {
738 dev_err(pci->dev,
739 "PCIe controller is not set to EP mode (hdr_type:0x%x)!\n",
740 hdr_type);
741 return -EIO;
744 dw_pcie_version_detect(pci);
746 dw_pcie_iatu_detect(pci);
748 ret = dw_pcie_edma_detect(pci);
749 if (ret)
750 return ret;
752 if (!ep->ib_window_map) {
753 ep->ib_window_map = devm_bitmap_zalloc(dev, pci->num_ib_windows,
754 GFP_KERNEL);
755 if (!ep->ib_window_map)
756 goto err_remove_edma;
759 if (!ep->ob_window_map) {
760 ep->ob_window_map = devm_bitmap_zalloc(dev, pci->num_ob_windows,
761 GFP_KERNEL);
762 if (!ep->ob_window_map)
763 goto err_remove_edma;
766 if (!ep->outbound_addr) {
767 addr = devm_kcalloc(dev, pci->num_ob_windows, sizeof(phys_addr_t),
768 GFP_KERNEL);
769 if (!addr)
770 goto err_remove_edma;
771 ep->outbound_addr = addr;
774 for (func_no = 0; func_no < epc->max_functions; func_no++) {
776 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
777 if (ep_func)
778 continue;
780 ep_func = devm_kzalloc(dev, sizeof(*ep_func), GFP_KERNEL);
781 if (!ep_func)
782 goto err_remove_edma;
784 ep_func->func_no = func_no;
785 ep_func->msi_cap = dw_pcie_ep_find_capability(ep, func_no,
786 PCI_CAP_ID_MSI);
787 ep_func->msix_cap = dw_pcie_ep_find_capability(ep, func_no,
788 PCI_CAP_ID_MSIX);
790 list_add_tail(&ep_func->list, &ep->func_list);
793 if (ep->ops->init)
794 ep->ops->init(ep);
796 ptm_cap_base = dw_pcie_ep_find_ext_capability(pci, PCI_EXT_CAP_ID_PTM);
799 * PTM responder capability can be disabled only after disabling
800 * PTM root capability.
802 if (ptm_cap_base) {
803 dw_pcie_dbi_ro_wr_en(pci);
804 reg = dw_pcie_readl_dbi(pci, ptm_cap_base + PCI_PTM_CAP);
805 reg &= ~PCI_PTM_CAP_ROOT;
806 dw_pcie_writel_dbi(pci, ptm_cap_base + PCI_PTM_CAP, reg);
808 reg = dw_pcie_readl_dbi(pci, ptm_cap_base + PCI_PTM_CAP);
809 reg &= ~(PCI_PTM_CAP_RES | PCI_PTM_GRANULARITY_MASK);
810 dw_pcie_writel_dbi(pci, ptm_cap_base + PCI_PTM_CAP, reg);
811 dw_pcie_dbi_ro_wr_dis(pci);
814 dw_pcie_ep_init_non_sticky_registers(pci);
816 return 0;
818 err_remove_edma:
819 dw_pcie_edma_remove(pci);
821 return ret;
823 EXPORT_SYMBOL_GPL(dw_pcie_ep_init_registers);
826 * dw_pcie_ep_linkup - Notify EPF drivers about Link Up event
827 * @ep: DWC EP device
829 void dw_pcie_ep_linkup(struct dw_pcie_ep *ep)
831 struct pci_epc *epc = ep->epc;
833 pci_epc_linkup(epc);
835 EXPORT_SYMBOL_GPL(dw_pcie_ep_linkup);
838 * dw_pcie_ep_linkdown - Notify EPF drivers about Link Down event
839 * @ep: DWC EP device
841 * Non-sticky registers are also initialized before sending the notification to
842 * the EPF drivers. This is needed since the registers need to be initialized
843 * before the link comes back again.
845 void dw_pcie_ep_linkdown(struct dw_pcie_ep *ep)
847 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
848 struct pci_epc *epc = ep->epc;
851 * Initialize the non-sticky DWC registers as they would've reset post
852 * Link Down. This is specifically needed for drivers not supporting
853 * PERST# as they have no way to reinitialize the registers before the
854 * link comes back again.
856 dw_pcie_ep_init_non_sticky_registers(pci);
858 pci_epc_linkdown(epc);
860 EXPORT_SYMBOL_GPL(dw_pcie_ep_linkdown);
863 * dw_pcie_ep_init - Initialize the endpoint device
864 * @ep: DWC EP device
866 * Initialize the endpoint device. Allocate resources and create the EPC
867 * device with the endpoint framework.
869 * Return: 0 if success, errno otherwise.
871 int dw_pcie_ep_init(struct dw_pcie_ep *ep)
873 int ret;
874 struct resource *res;
875 struct pci_epc *epc;
876 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
877 struct device *dev = pci->dev;
878 struct platform_device *pdev = to_platform_device(dev);
879 struct device_node *np = dev->of_node;
881 INIT_LIST_HEAD(&ep->func_list);
883 ret = dw_pcie_get_resources(pci);
884 if (ret)
885 return ret;
887 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "addr_space");
888 if (!res)
889 return -EINVAL;
891 ep->phys_base = res->start;
892 ep->addr_size = resource_size(res);
894 if (ep->ops->pre_init)
895 ep->ops->pre_init(ep);
897 epc = devm_pci_epc_create(dev, &epc_ops);
898 if (IS_ERR(epc)) {
899 dev_err(dev, "Failed to create epc device\n");
900 return PTR_ERR(epc);
903 ep->epc = epc;
904 epc_set_drvdata(epc, ep);
906 ret = of_property_read_u8(np, "max-functions", &epc->max_functions);
907 if (ret < 0)
908 epc->max_functions = 1;
910 ret = pci_epc_mem_init(epc, ep->phys_base, ep->addr_size,
911 ep->page_size);
912 if (ret < 0) {
913 dev_err(dev, "Failed to initialize address space\n");
914 return ret;
917 ep->msi_mem = pci_epc_mem_alloc_addr(epc, &ep->msi_mem_phys,
918 epc->mem->window.page_size);
919 if (!ep->msi_mem) {
920 ret = -ENOMEM;
921 dev_err(dev, "Failed to reserve memory for MSI/MSI-X\n");
922 goto err_exit_epc_mem;
925 return 0;
927 err_exit_epc_mem:
928 pci_epc_mem_exit(epc);
930 return ret;
932 EXPORT_SYMBOL_GPL(dw_pcie_ep_init);