1 // SPDX-License-Identifier: GPL-2.0
3 * Synopsys DesignWare PCIe Endpoint controller driver
5 * Copyright (C) 2017 Texas Instruments
6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
10 #include <linux/platform_device.h>
12 #include "pcie-designware.h"
13 #include <linux/pci-epc.h>
14 #include <linux/pci-epf.h>
16 #include "../../pci.h"
18 void dw_pcie_ep_linkup(struct dw_pcie_ep *ep)
20 struct pci_epc *epc = ep->epc;
24 EXPORT_SYMBOL_GPL(dw_pcie_ep_linkup);
26 void dw_pcie_ep_init_notify(struct dw_pcie_ep *ep)
28 struct pci_epc *epc = ep->epc;
30 pci_epc_init_notify(epc);
32 EXPORT_SYMBOL_GPL(dw_pcie_ep_init_notify);
34 struct dw_pcie_ep_func *
35 dw_pcie_ep_get_func_from_ep(struct dw_pcie_ep *ep, u8 func_no)
37 struct dw_pcie_ep_func *ep_func;
39 list_for_each_entry(ep_func, &ep->func_list, list) {
40 if (ep_func->func_no == func_no)
47 static unsigned int dw_pcie_ep_func_select(struct dw_pcie_ep *ep, u8 func_no)
49 unsigned int func_offset = 0;
51 if (ep->ops->func_conf_select)
52 func_offset = ep->ops->func_conf_select(ep, func_no);
57 static void __dw_pcie_ep_reset_bar(struct dw_pcie *pci, u8 func_no,
58 enum pci_barno bar, int flags)
61 unsigned int func_offset = 0;
62 struct dw_pcie_ep *ep = &pci->ep;
64 func_offset = dw_pcie_ep_func_select(ep, func_no);
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);
74 dw_pcie_dbi_ro_wr_dis(pci);
77 void dw_pcie_ep_reset_bar(struct dw_pcie *pci, enum pci_barno bar)
81 funcs = pci->ep.epc->max_functions;
83 for (func_no = 0; func_no < funcs; func_no++)
84 __dw_pcie_ep_reset_bar(pci, func_no, bar, 0);
86 EXPORT_SYMBOL_GPL(dw_pcie_ep_reset_bar);
88 static u8 __dw_pcie_ep_find_next_cap(struct dw_pcie_ep *ep, u8 func_no,
91 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
92 unsigned int func_offset = 0;
93 u8 cap_id, next_cap_ptr;
99 func_offset = dw_pcie_ep_func_select(ep, func_no);
101 reg = dw_pcie_readw_dbi(pci, func_offset + cap_ptr);
102 cap_id = (reg & 0x00ff);
104 if (cap_id > PCI_CAP_ID_MAX)
110 next_cap_ptr = (reg & 0xff00) >> 8;
111 return __dw_pcie_ep_find_next_cap(ep, func_no, next_cap_ptr, cap);
114 static u8 dw_pcie_ep_find_capability(struct dw_pcie_ep *ep, u8 func_no, u8 cap)
116 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
117 unsigned int func_offset = 0;
121 func_offset = dw_pcie_ep_func_select(ep, func_no);
123 reg = dw_pcie_readw_dbi(pci, func_offset + PCI_CAPABILITY_LIST);
124 next_cap_ptr = (reg & 0x00ff);
126 return __dw_pcie_ep_find_next_cap(ep, func_no, next_cap_ptr, cap);
129 static int dw_pcie_ep_write_header(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
130 struct pci_epf_header *hdr)
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;
136 func_offset = dw_pcie_ep_func_select(ep, func_no);
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,
152 dw_pcie_dbi_ro_wr_dis(pci);
157 static int dw_pcie_ep_inbound_atu(struct dw_pcie_ep *ep, u8 func_no, int type,
158 dma_addr_t cpu_addr, enum pci_barno bar)
162 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
164 free_win = find_first_zero_bit(ep->ib_window_map, pci->num_ib_windows);
165 if (free_win >= pci->num_ib_windows) {
166 dev_err(pci->dev, "No free inbound window\n");
170 ret = dw_pcie_prog_inbound_atu(pci, func_no, free_win, type,
173 dev_err(pci->dev, "Failed to program IB window\n");
177 ep->bar_to_atu[bar] = free_win;
178 set_bit(free_win, ep->ib_window_map);
183 static int dw_pcie_ep_outbound_atu(struct dw_pcie_ep *ep, u8 func_no,
184 phys_addr_t phys_addr,
185 u64 pci_addr, size_t size)
187 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
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");
197 ret = dw_pcie_prog_ep_outbound_atu(pci, func_no, free_win, PCIE_ATU_TYPE_MEM,
198 phys_addr, pci_addr, size);
202 set_bit(free_win, ep->ob_window_map);
203 ep->outbound_addr[free_win] = phys_addr;
208 static void dw_pcie_ep_clear_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
209 struct pci_epf_bar *epf_bar)
211 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
212 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
213 enum pci_barno bar = epf_bar->barno;
214 u32 atu_index = ep->bar_to_atu[bar];
216 __dw_pcie_ep_reset_bar(pci, func_no, bar, epf_bar->flags);
218 dw_pcie_disable_atu(pci, PCIE_ATU_REGION_DIR_IB, atu_index);
219 clear_bit(atu_index, ep->ib_window_map);
220 ep->epf_bar[bar] = NULL;
223 static int dw_pcie_ep_set_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
224 struct pci_epf_bar *epf_bar)
226 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
227 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
228 enum pci_barno bar = epf_bar->barno;
229 size_t size = epf_bar->size;
230 int flags = epf_bar->flags;
231 unsigned int func_offset = 0;
235 func_offset = dw_pcie_ep_func_select(ep, func_no);
237 reg = PCI_BASE_ADDRESS_0 + (4 * bar) + func_offset;
239 if (!(flags & PCI_BASE_ADDRESS_SPACE))
240 type = PCIE_ATU_TYPE_MEM;
242 type = PCIE_ATU_TYPE_IO;
244 ret = dw_pcie_ep_inbound_atu(ep, func_no, type, epf_bar->phys_addr, bar);
248 dw_pcie_dbi_ro_wr_en(pci);
250 dw_pcie_writel_dbi2(pci, reg, lower_32_bits(size - 1));
251 dw_pcie_writel_dbi(pci, reg, flags);
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);
258 ep->epf_bar[bar] = epf_bar;
259 dw_pcie_dbi_ro_wr_dis(pci);
264 static int dw_pcie_find_index(struct dw_pcie_ep *ep, phys_addr_t addr,
268 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
270 for (index = 0; index < pci->num_ob_windows; index++) {
271 if (ep->outbound_addr[index] != addr)
280 static void dw_pcie_ep_unmap_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
285 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
286 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
288 ret = dw_pcie_find_index(ep, addr, &atu_index);
292 dw_pcie_disable_atu(pci, PCIE_ATU_REGION_DIR_OB, atu_index);
293 clear_bit(atu_index, ep->ob_window_map);
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)
300 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
301 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
303 ret = dw_pcie_ep_outbound_atu(ep, func_no, addr, pci_addr, size);
305 dev_err(pci->dev, "Failed to enable address\n");
312 static int dw_pcie_ep_get_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
314 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
315 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
317 unsigned int func_offset = 0;
318 struct dw_pcie_ep_func *ep_func;
320 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
321 if (!ep_func || !ep_func->msi_cap)
324 func_offset = dw_pcie_ep_func_select(ep, func_no);
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))
331 val = (val & PCI_MSI_FLAGS_QSIZE) >> 4;
336 static int dw_pcie_ep_set_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
339 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
340 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
342 unsigned int func_offset = 0;
343 struct dw_pcie_ep_func *ep_func;
345 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
346 if (!ep_func || !ep_func->msi_cap)
349 func_offset = dw_pcie_ep_func_select(ep, func_no);
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);
362 static int dw_pcie_ep_get_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
364 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
365 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
367 unsigned int func_offset = 0;
368 struct dw_pcie_ep_func *ep_func;
370 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
371 if (!ep_func || !ep_func->msix_cap)
374 func_offset = dw_pcie_ep_func_select(ep, func_no);
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))
381 val &= PCI_MSIX_FLAGS_QSIZE;
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)
389 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
390 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
392 unsigned int func_offset = 0;
393 struct dw_pcie_ep_func *ep_func;
395 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
396 if (!ep_func || !ep_func->msix_cap)
399 dw_pcie_dbi_ro_wr_en(pci);
401 func_offset = dw_pcie_ep_func_select(ep, func_no);
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;
407 dw_pcie_writew_dbi(pci, reg, val);
409 reg = ep_func->msix_cap + func_offset + PCI_MSIX_TABLE;
411 dw_pcie_writel_dbi(pci, reg, val);
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);
417 dw_pcie_dbi_ro_wr_dis(pci);
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)
425 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
427 if (!ep->ops->raise_irq)
430 return ep->ops->raise_irq(ep, func_no, type, interrupt_num);
433 static void dw_pcie_ep_stop(struct pci_epc *epc)
435 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
436 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
438 dw_pcie_stop_link(pci);
441 static int dw_pcie_ep_start(struct pci_epc *epc)
443 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
444 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
446 return dw_pcie_start_link(pci);
449 static const struct pci_epc_features*
450 dw_pcie_ep_get_features(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
452 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
454 if (!ep->ops->get_features)
457 return ep->ops->get_features(ep);
460 static const struct pci_epc_ops epc_ops = {
461 .write_header = dw_pcie_ep_write_header,
462 .set_bar = dw_pcie_ep_set_bar,
463 .clear_bar = dw_pcie_ep_clear_bar,
464 .map_addr = dw_pcie_ep_map_addr,
465 .unmap_addr = dw_pcie_ep_unmap_addr,
466 .set_msi = dw_pcie_ep_set_msi,
467 .get_msi = dw_pcie_ep_get_msi,
468 .set_msix = dw_pcie_ep_set_msix,
469 .get_msix = dw_pcie_ep_get_msix,
470 .raise_irq = dw_pcie_ep_raise_irq,
471 .start = dw_pcie_ep_start,
472 .stop = dw_pcie_ep_stop,
473 .get_features = dw_pcie_ep_get_features,
476 int dw_pcie_ep_raise_legacy_irq(struct dw_pcie_ep *ep, u8 func_no)
478 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
479 struct device *dev = pci->dev;
481 dev_err(dev, "EP cannot trigger legacy IRQs\n");
485 EXPORT_SYMBOL_GPL(dw_pcie_ep_raise_legacy_irq);
487 int dw_pcie_ep_raise_msi_irq(struct dw_pcie_ep *ep, u8 func_no,
490 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
491 struct dw_pcie_ep_func *ep_func;
492 struct pci_epc *epc = ep->epc;
493 unsigned int aligned_offset;
494 unsigned int func_offset = 0;
495 u16 msg_ctrl, msg_data;
496 u32 msg_addr_lower, msg_addr_upper, reg;
501 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
502 if (!ep_func || !ep_func->msi_cap)
505 func_offset = dw_pcie_ep_func_select(ep, func_no);
507 /* Raise MSI per the PCI Local Bus Specification Revision 3.0, 6.8.1. */
508 reg = ep_func->msi_cap + func_offset + PCI_MSI_FLAGS;
509 msg_ctrl = dw_pcie_readw_dbi(pci, reg);
510 has_upper = !!(msg_ctrl & PCI_MSI_FLAGS_64BIT);
511 reg = ep_func->msi_cap + func_offset + PCI_MSI_ADDRESS_LO;
512 msg_addr_lower = dw_pcie_readl_dbi(pci, reg);
514 reg = ep_func->msi_cap + func_offset + PCI_MSI_ADDRESS_HI;
515 msg_addr_upper = dw_pcie_readl_dbi(pci, reg);
516 reg = ep_func->msi_cap + func_offset + PCI_MSI_DATA_64;
517 msg_data = dw_pcie_readw_dbi(pci, reg);
520 reg = ep_func->msi_cap + func_offset + PCI_MSI_DATA_32;
521 msg_data = dw_pcie_readw_dbi(pci, reg);
523 aligned_offset = msg_addr_lower & (epc->mem->window.page_size - 1);
524 msg_addr = ((u64)msg_addr_upper) << 32 |
525 (msg_addr_lower & ~aligned_offset);
526 ret = dw_pcie_ep_map_addr(epc, func_no, 0, ep->msi_mem_phys, msg_addr,
527 epc->mem->window.page_size);
531 writel(msg_data | (interrupt_num - 1), ep->msi_mem + aligned_offset);
533 dw_pcie_ep_unmap_addr(epc, func_no, 0, ep->msi_mem_phys);
537 EXPORT_SYMBOL_GPL(dw_pcie_ep_raise_msi_irq);
539 int dw_pcie_ep_raise_msix_irq_doorbell(struct dw_pcie_ep *ep, u8 func_no,
542 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
543 struct dw_pcie_ep_func *ep_func;
546 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
547 if (!ep_func || !ep_func->msix_cap)
550 msg_data = (func_no << PCIE_MSIX_DOORBELL_PF_SHIFT) |
553 dw_pcie_writel_dbi(pci, PCIE_MSIX_DOORBELL, msg_data);
558 int dw_pcie_ep_raise_msix_irq(struct dw_pcie_ep *ep, u8 func_no,
561 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
562 struct dw_pcie_ep_func *ep_func;
563 struct pci_epf_msix_tbl *msix_tbl;
564 struct pci_epc *epc = ep->epc;
565 unsigned int func_offset = 0;
566 u32 reg, msg_data, vec_ctrl;
567 unsigned int aligned_offset;
573 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
574 if (!ep_func || !ep_func->msix_cap)
577 func_offset = dw_pcie_ep_func_select(ep, func_no);
579 reg = ep_func->msix_cap + func_offset + PCI_MSIX_TABLE;
580 tbl_offset = dw_pcie_readl_dbi(pci, reg);
581 bir = (tbl_offset & PCI_MSIX_TABLE_BIR);
582 tbl_offset &= PCI_MSIX_TABLE_OFFSET;
584 msix_tbl = ep->epf_bar[bir]->addr + tbl_offset;
585 msg_addr = msix_tbl[(interrupt_num - 1)].msg_addr;
586 msg_data = msix_tbl[(interrupt_num - 1)].msg_data;
587 vec_ctrl = msix_tbl[(interrupt_num - 1)].vector_ctrl;
589 if (vec_ctrl & PCI_MSIX_ENTRY_CTRL_MASKBIT) {
590 dev_dbg(pci->dev, "MSI-X entry ctrl set\n");
594 aligned_offset = msg_addr & (epc->mem->window.page_size - 1);
595 ret = dw_pcie_ep_map_addr(epc, func_no, 0, ep->msi_mem_phys, msg_addr,
596 epc->mem->window.page_size);
600 writel(msg_data, ep->msi_mem + aligned_offset);
602 dw_pcie_ep_unmap_addr(epc, func_no, 0, ep->msi_mem_phys);
607 void dw_pcie_ep_exit(struct dw_pcie_ep *ep)
609 struct pci_epc *epc = ep->epc;
611 pci_epc_mem_free_addr(epc, ep->msi_mem_phys, ep->msi_mem,
612 epc->mem->window.page_size);
614 pci_epc_mem_exit(epc);
617 static unsigned int dw_pcie_ep_find_ext_capability(struct dw_pcie *pci, int cap)
620 int pos = PCI_CFG_SPACE_SIZE;
623 header = dw_pcie_readl_dbi(pci, pos);
624 if (PCI_EXT_CAP_ID(header) == cap)
627 pos = PCI_EXT_CAP_NEXT(header);
635 int dw_pcie_ep_init_complete(struct dw_pcie_ep *ep)
637 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
644 hdr_type = dw_pcie_readb_dbi(pci, PCI_HEADER_TYPE) &
645 PCI_HEADER_TYPE_MASK;
646 if (hdr_type != PCI_HEADER_TYPE_NORMAL) {
648 "PCIe controller is not set to EP mode (hdr_type:0x%x)!\n",
653 offset = dw_pcie_ep_find_ext_capability(pci, PCI_EXT_CAP_ID_REBAR);
655 dw_pcie_dbi_ro_wr_en(pci);
658 reg = dw_pcie_readl_dbi(pci, offset + PCI_REBAR_CTRL);
659 nbars = (reg & PCI_REBAR_CTRL_NBAR_MASK) >>
660 PCI_REBAR_CTRL_NBAR_SHIFT;
662 for (i = 0; i < nbars; i++, offset += PCI_REBAR_CTRL)
663 dw_pcie_writel_dbi(pci, offset + PCI_REBAR_CAP, 0x0);
667 dw_pcie_dbi_ro_wr_dis(pci);
671 EXPORT_SYMBOL_GPL(dw_pcie_ep_init_complete);
673 int dw_pcie_ep_init(struct dw_pcie_ep *ep)
678 struct resource *res;
680 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
681 struct device *dev = pci->dev;
682 struct platform_device *pdev = to_platform_device(dev);
683 struct device_node *np = dev->of_node;
684 const struct pci_epc_features *epc_features;
685 struct dw_pcie_ep_func *ep_func;
687 INIT_LIST_HEAD(&ep->func_list);
689 if (!pci->dbi_base) {
690 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbi");
691 pci->dbi_base = devm_pci_remap_cfg_resource(dev, res);
692 if (IS_ERR(pci->dbi_base))
693 return PTR_ERR(pci->dbi_base);
696 if (!pci->dbi_base2) {
697 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbi2");
699 pci->dbi_base2 = pci->dbi_base + SZ_4K;
701 pci->dbi_base2 = devm_pci_remap_cfg_resource(dev, res);
702 if (IS_ERR(pci->dbi_base2))
703 return PTR_ERR(pci->dbi_base2);
707 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "addr_space");
711 ep->phys_base = res->start;
712 ep->addr_size = resource_size(res);
714 dw_pcie_version_detect(pci);
716 dw_pcie_iatu_detect(pci);
718 ep->ib_window_map = devm_bitmap_zalloc(dev, pci->num_ib_windows,
720 if (!ep->ib_window_map)
723 ep->ob_window_map = devm_bitmap_zalloc(dev, pci->num_ob_windows,
725 if (!ep->ob_window_map)
728 addr = devm_kcalloc(dev, pci->num_ob_windows, sizeof(phys_addr_t),
732 ep->outbound_addr = addr;
734 if (pci->link_gen < 1)
735 pci->link_gen = of_pci_get_max_link_speed(np);
737 epc = devm_pci_epc_create(dev, &epc_ops);
739 dev_err(dev, "Failed to create epc device\n");
744 epc_set_drvdata(epc, ep);
746 ret = of_property_read_u8(np, "max-functions", &epc->max_functions);
748 epc->max_functions = 1;
750 for (func_no = 0; func_no < epc->max_functions; func_no++) {
751 ep_func = devm_kzalloc(dev, sizeof(*ep_func), GFP_KERNEL);
755 ep_func->func_no = func_no;
756 ep_func->msi_cap = dw_pcie_ep_find_capability(ep, func_no,
758 ep_func->msix_cap = dw_pcie_ep_find_capability(ep, func_no,
761 list_add_tail(&ep_func->list, &ep->func_list);
764 if (ep->ops->ep_init)
765 ep->ops->ep_init(ep);
767 ret = pci_epc_mem_init(epc, ep->phys_base, ep->addr_size,
770 dev_err(dev, "Failed to initialize address space\n");
774 ep->msi_mem = pci_epc_mem_alloc_addr(epc, &ep->msi_mem_phys,
775 epc->mem->window.page_size);
778 dev_err(dev, "Failed to reserve memory for MSI/MSI-X\n");
779 goto err_exit_epc_mem;
782 if (ep->ops->get_features) {
783 epc_features = ep->ops->get_features(ep);
784 if (epc_features->core_init_notifier)
788 ret = dw_pcie_ep_init_complete(ep);
790 goto err_free_epc_mem;
795 pci_epc_mem_free_addr(epc, ep->msi_mem_phys, ep->msi_mem,
796 epc->mem->window.page_size);
799 pci_epc_mem_exit(epc);
803 EXPORT_SYMBOL_GPL(dw_pcie_ep_init);