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>
11 #include "pcie-designware.h"
12 #include <linux/pci-epc.h>
13 #include <linux/pci-epf.h>
15 #include "../../pci.h"
17 void dw_pcie_ep_linkup(struct dw_pcie_ep *ep)
19 struct pci_epc *epc = ep->epc;
23 EXPORT_SYMBOL_GPL(dw_pcie_ep_linkup);
25 void dw_pcie_ep_init_notify(struct dw_pcie_ep *ep)
27 struct pci_epc *epc = ep->epc;
29 pci_epc_init_notify(epc);
31 EXPORT_SYMBOL_GPL(dw_pcie_ep_init_notify);
33 struct dw_pcie_ep_func *
34 dw_pcie_ep_get_func_from_ep(struct dw_pcie_ep *ep, u8 func_no)
36 struct dw_pcie_ep_func *ep_func;
38 list_for_each_entry(ep_func, &ep->func_list, list) {
39 if (ep_func->func_no == func_no)
46 static unsigned int dw_pcie_ep_func_select(struct dw_pcie_ep *ep, u8 func_no)
48 unsigned int func_offset = 0;
50 if (ep->ops->func_conf_select)
51 func_offset = ep->ops->func_conf_select(ep, func_no);
56 static void __dw_pcie_ep_reset_bar(struct dw_pcie *pci, u8 func_no,
57 enum pci_barno bar, int flags)
60 unsigned int func_offset = 0;
61 struct dw_pcie_ep *ep = &pci->ep;
63 func_offset = dw_pcie_ep_func_select(ep, func_no);
65 reg = func_offset + PCI_BASE_ADDRESS_0 + (4 * bar);
66 dw_pcie_dbi_ro_wr_en(pci);
67 dw_pcie_writel_dbi2(pci, reg, 0x0);
68 dw_pcie_writel_dbi(pci, reg, 0x0);
69 if (flags & PCI_BASE_ADDRESS_MEM_TYPE_64) {
70 dw_pcie_writel_dbi2(pci, reg + 4, 0x0);
71 dw_pcie_writel_dbi(pci, reg + 4, 0x0);
73 dw_pcie_dbi_ro_wr_dis(pci);
76 void dw_pcie_ep_reset_bar(struct dw_pcie *pci, enum pci_barno bar)
80 funcs = pci->ep.epc->max_functions;
82 for (func_no = 0; func_no < funcs; func_no++)
83 __dw_pcie_ep_reset_bar(pci, func_no, bar, 0);
86 static u8 __dw_pcie_ep_find_next_cap(struct dw_pcie_ep *ep, u8 func_no,
89 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
90 unsigned int func_offset = 0;
91 u8 cap_id, next_cap_ptr;
97 func_offset = dw_pcie_ep_func_select(ep, func_no);
99 reg = dw_pcie_readw_dbi(pci, func_offset + cap_ptr);
100 cap_id = (reg & 0x00ff);
102 if (cap_id > PCI_CAP_ID_MAX)
108 next_cap_ptr = (reg & 0xff00) >> 8;
109 return __dw_pcie_ep_find_next_cap(ep, func_no, next_cap_ptr, cap);
112 static u8 dw_pcie_ep_find_capability(struct dw_pcie_ep *ep, u8 func_no, u8 cap)
114 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
115 unsigned int func_offset = 0;
119 func_offset = dw_pcie_ep_func_select(ep, func_no);
121 reg = dw_pcie_readw_dbi(pci, func_offset + PCI_CAPABILITY_LIST);
122 next_cap_ptr = (reg & 0x00ff);
124 return __dw_pcie_ep_find_next_cap(ep, func_no, next_cap_ptr, cap);
127 static int dw_pcie_ep_write_header(struct pci_epc *epc, u8 func_no,
128 struct pci_epf_header *hdr)
130 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
131 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
132 unsigned int func_offset = 0;
134 func_offset = dw_pcie_ep_func_select(ep, func_no);
136 dw_pcie_dbi_ro_wr_en(pci);
137 dw_pcie_writew_dbi(pci, func_offset + PCI_VENDOR_ID, hdr->vendorid);
138 dw_pcie_writew_dbi(pci, func_offset + PCI_DEVICE_ID, hdr->deviceid);
139 dw_pcie_writeb_dbi(pci, func_offset + PCI_REVISION_ID, hdr->revid);
140 dw_pcie_writeb_dbi(pci, func_offset + PCI_CLASS_PROG, hdr->progif_code);
141 dw_pcie_writew_dbi(pci, func_offset + PCI_CLASS_DEVICE,
142 hdr->subclass_code | hdr->baseclass_code << 8);
143 dw_pcie_writeb_dbi(pci, func_offset + PCI_CACHE_LINE_SIZE,
144 hdr->cache_line_size);
145 dw_pcie_writew_dbi(pci, func_offset + PCI_SUBSYSTEM_VENDOR_ID,
146 hdr->subsys_vendor_id);
147 dw_pcie_writew_dbi(pci, func_offset + PCI_SUBSYSTEM_ID, hdr->subsys_id);
148 dw_pcie_writeb_dbi(pci, func_offset + PCI_INTERRUPT_PIN,
150 dw_pcie_dbi_ro_wr_dis(pci);
155 static int dw_pcie_ep_inbound_atu(struct dw_pcie_ep *ep, u8 func_no,
156 enum pci_barno bar, dma_addr_t cpu_addr,
157 enum dw_pcie_as_type as_type)
161 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
163 free_win = find_first_zero_bit(ep->ib_window_map, ep->num_ib_windows);
164 if (free_win >= ep->num_ib_windows) {
165 dev_err(pci->dev, "No free inbound window\n");
169 ret = dw_pcie_prog_inbound_atu(pci, func_no, free_win, bar, cpu_addr,
172 dev_err(pci->dev, "Failed to program IB window\n");
176 ep->bar_to_atu[bar] = free_win;
177 set_bit(free_win, ep->ib_window_map);
182 static int dw_pcie_ep_outbound_atu(struct dw_pcie_ep *ep, u8 func_no,
183 phys_addr_t phys_addr,
184 u64 pci_addr, size_t size)
187 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
189 free_win = find_first_zero_bit(ep->ob_window_map, ep->num_ob_windows);
190 if (free_win >= ep->num_ob_windows) {
191 dev_err(pci->dev, "No free outbound window\n");
195 dw_pcie_prog_ep_outbound_atu(pci, func_no, free_win, PCIE_ATU_TYPE_MEM,
196 phys_addr, pci_addr, size);
198 set_bit(free_win, ep->ob_window_map);
199 ep->outbound_addr[free_win] = phys_addr;
204 static void dw_pcie_ep_clear_bar(struct pci_epc *epc, u8 func_no,
205 struct pci_epf_bar *epf_bar)
207 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
208 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
209 enum pci_barno bar = epf_bar->barno;
210 u32 atu_index = ep->bar_to_atu[bar];
212 __dw_pcie_ep_reset_bar(pci, func_no, bar, epf_bar->flags);
214 dw_pcie_disable_atu(pci, atu_index, DW_PCIE_REGION_INBOUND);
215 clear_bit(atu_index, ep->ib_window_map);
216 ep->epf_bar[bar] = NULL;
219 static int dw_pcie_ep_set_bar(struct pci_epc *epc, u8 func_no,
220 struct pci_epf_bar *epf_bar)
223 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
224 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
225 enum pci_barno bar = epf_bar->barno;
226 size_t size = epf_bar->size;
227 int flags = epf_bar->flags;
228 enum dw_pcie_as_type as_type;
230 unsigned int func_offset = 0;
232 func_offset = dw_pcie_ep_func_select(ep, func_no);
234 reg = PCI_BASE_ADDRESS_0 + (4 * bar) + func_offset;
236 if (!(flags & PCI_BASE_ADDRESS_SPACE))
237 as_type = DW_PCIE_AS_MEM;
239 as_type = DW_PCIE_AS_IO;
241 ret = dw_pcie_ep_inbound_atu(ep, func_no, bar,
242 epf_bar->phys_addr, as_type);
246 dw_pcie_dbi_ro_wr_en(pci);
248 dw_pcie_writel_dbi2(pci, reg, lower_32_bits(size - 1));
249 dw_pcie_writel_dbi(pci, reg, flags);
251 if (flags & PCI_BASE_ADDRESS_MEM_TYPE_64) {
252 dw_pcie_writel_dbi2(pci, reg + 4, upper_32_bits(size - 1));
253 dw_pcie_writel_dbi(pci, reg + 4, 0);
256 ep->epf_bar[bar] = epf_bar;
257 dw_pcie_dbi_ro_wr_dis(pci);
262 static int dw_pcie_find_index(struct dw_pcie_ep *ep, phys_addr_t addr,
267 for (index = 0; index < ep->num_ob_windows; index++) {
268 if (ep->outbound_addr[index] != addr)
277 static void dw_pcie_ep_unmap_addr(struct pci_epc *epc, u8 func_no,
282 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
283 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
285 ret = dw_pcie_find_index(ep, addr, &atu_index);
289 dw_pcie_disable_atu(pci, atu_index, DW_PCIE_REGION_OUTBOUND);
290 clear_bit(atu_index, ep->ob_window_map);
293 static int dw_pcie_ep_map_addr(struct pci_epc *epc, u8 func_no,
295 u64 pci_addr, size_t size)
298 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
299 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
301 ret = dw_pcie_ep_outbound_atu(ep, func_no, addr, pci_addr, size);
303 dev_err(pci->dev, "Failed to enable address\n");
310 static int dw_pcie_ep_get_msi(struct pci_epc *epc, u8 func_no)
312 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
313 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
315 unsigned int func_offset = 0;
316 struct dw_pcie_ep_func *ep_func;
318 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
319 if (!ep_func || !ep_func->msi_cap)
322 func_offset = dw_pcie_ep_func_select(ep, func_no);
324 reg = ep_func->msi_cap + func_offset + PCI_MSI_FLAGS;
325 val = dw_pcie_readw_dbi(pci, reg);
326 if (!(val & PCI_MSI_FLAGS_ENABLE))
329 val = (val & PCI_MSI_FLAGS_QSIZE) >> 4;
334 static int dw_pcie_ep_set_msi(struct pci_epc *epc, u8 func_no, u8 interrupts)
336 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
337 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
339 unsigned int func_offset = 0;
340 struct dw_pcie_ep_func *ep_func;
342 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
343 if (!ep_func || !ep_func->msi_cap)
346 func_offset = dw_pcie_ep_func_select(ep, func_no);
348 reg = ep_func->msi_cap + func_offset + PCI_MSI_FLAGS;
349 val = dw_pcie_readw_dbi(pci, reg);
350 val &= ~PCI_MSI_FLAGS_QMASK;
351 val |= (interrupts << 1) & PCI_MSI_FLAGS_QMASK;
352 dw_pcie_dbi_ro_wr_en(pci);
353 dw_pcie_writew_dbi(pci, reg, val);
354 dw_pcie_dbi_ro_wr_dis(pci);
359 static int dw_pcie_ep_get_msix(struct pci_epc *epc, u8 func_no)
361 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
362 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
364 unsigned int func_offset = 0;
365 struct dw_pcie_ep_func *ep_func;
367 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
368 if (!ep_func || !ep_func->msix_cap)
371 func_offset = dw_pcie_ep_func_select(ep, func_no);
373 reg = ep_func->msix_cap + func_offset + PCI_MSIX_FLAGS;
374 val = dw_pcie_readw_dbi(pci, reg);
375 if (!(val & PCI_MSIX_FLAGS_ENABLE))
378 val &= PCI_MSIX_FLAGS_QSIZE;
383 static int dw_pcie_ep_set_msix(struct pci_epc *epc, u8 func_no, u16 interrupts,
384 enum pci_barno bir, u32 offset)
386 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
387 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
389 unsigned int func_offset = 0;
390 struct dw_pcie_ep_func *ep_func;
392 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
393 if (!ep_func || !ep_func->msix_cap)
396 dw_pcie_dbi_ro_wr_en(pci);
398 func_offset = dw_pcie_ep_func_select(ep, func_no);
400 reg = ep_func->msix_cap + func_offset + PCI_MSIX_FLAGS;
401 val = dw_pcie_readw_dbi(pci, reg);
402 val &= ~PCI_MSIX_FLAGS_QSIZE;
404 dw_pcie_writew_dbi(pci, reg, val);
406 reg = ep_func->msix_cap + func_offset + PCI_MSIX_TABLE;
408 dw_pcie_writel_dbi(pci, reg, val);
410 reg = ep_func->msix_cap + func_offset + PCI_MSIX_PBA;
411 val = (offset + (interrupts * PCI_MSIX_ENTRY_SIZE)) | bir;
412 dw_pcie_writel_dbi(pci, reg, val);
414 dw_pcie_dbi_ro_wr_dis(pci);
419 static int dw_pcie_ep_raise_irq(struct pci_epc *epc, u8 func_no,
420 enum pci_epc_irq_type type, u16 interrupt_num)
422 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
424 if (!ep->ops->raise_irq)
427 return ep->ops->raise_irq(ep, func_no, type, interrupt_num);
430 static void dw_pcie_ep_stop(struct pci_epc *epc)
432 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
433 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
435 if (!pci->ops->stop_link)
438 pci->ops->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 if (!pci->ops->start_link)
449 return pci->ops->start_link(pci);
452 static const struct pci_epc_features*
453 dw_pcie_ep_get_features(struct pci_epc *epc, u8 func_no)
455 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
457 if (!ep->ops->get_features)
460 return ep->ops->get_features(ep);
463 static const struct pci_epc_ops epc_ops = {
464 .write_header = dw_pcie_ep_write_header,
465 .set_bar = dw_pcie_ep_set_bar,
466 .clear_bar = dw_pcie_ep_clear_bar,
467 .map_addr = dw_pcie_ep_map_addr,
468 .unmap_addr = dw_pcie_ep_unmap_addr,
469 .set_msi = dw_pcie_ep_set_msi,
470 .get_msi = dw_pcie_ep_get_msi,
471 .set_msix = dw_pcie_ep_set_msix,
472 .get_msix = dw_pcie_ep_get_msix,
473 .raise_irq = dw_pcie_ep_raise_irq,
474 .start = dw_pcie_ep_start,
475 .stop = dw_pcie_ep_stop,
476 .get_features = dw_pcie_ep_get_features,
479 int dw_pcie_ep_raise_legacy_irq(struct dw_pcie_ep *ep, u8 func_no)
481 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
482 struct device *dev = pci->dev;
484 dev_err(dev, "EP cannot trigger legacy IRQs\n");
489 int dw_pcie_ep_raise_msi_irq(struct dw_pcie_ep *ep, u8 func_no,
492 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
493 struct dw_pcie_ep_func *ep_func;
494 struct pci_epc *epc = ep->epc;
495 unsigned int aligned_offset;
496 unsigned int func_offset = 0;
497 u16 msg_ctrl, msg_data;
498 u32 msg_addr_lower, msg_addr_upper, reg;
503 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
504 if (!ep_func || !ep_func->msi_cap)
507 func_offset = dw_pcie_ep_func_select(ep, func_no);
509 /* Raise MSI per the PCI Local Bus Specification Revision 3.0, 6.8.1. */
510 reg = ep_func->msi_cap + func_offset + PCI_MSI_FLAGS;
511 msg_ctrl = dw_pcie_readw_dbi(pci, reg);
512 has_upper = !!(msg_ctrl & PCI_MSI_FLAGS_64BIT);
513 reg = ep_func->msi_cap + func_offset + PCI_MSI_ADDRESS_LO;
514 msg_addr_lower = dw_pcie_readl_dbi(pci, reg);
516 reg = ep_func->msi_cap + func_offset + PCI_MSI_ADDRESS_HI;
517 msg_addr_upper = dw_pcie_readl_dbi(pci, reg);
518 reg = ep_func->msi_cap + func_offset + PCI_MSI_DATA_64;
519 msg_data = dw_pcie_readw_dbi(pci, reg);
522 reg = ep_func->msi_cap + func_offset + PCI_MSI_DATA_32;
523 msg_data = dw_pcie_readw_dbi(pci, reg);
525 aligned_offset = msg_addr_lower & (epc->mem->window.page_size - 1);
526 msg_addr = ((u64)msg_addr_upper) << 32 |
527 (msg_addr_lower & ~aligned_offset);
528 ret = dw_pcie_ep_map_addr(epc, func_no, ep->msi_mem_phys, msg_addr,
529 epc->mem->window.page_size);
533 writel(msg_data | (interrupt_num - 1), ep->msi_mem + aligned_offset);
535 dw_pcie_ep_unmap_addr(epc, func_no, ep->msi_mem_phys);
540 int dw_pcie_ep_raise_msix_irq_doorbell(struct dw_pcie_ep *ep, u8 func_no,
543 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
544 struct dw_pcie_ep_func *ep_func;
547 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
548 if (!ep_func || !ep_func->msix_cap)
551 msg_data = (func_no << PCIE_MSIX_DOORBELL_PF_SHIFT) |
554 dw_pcie_writel_dbi(pci, PCIE_MSIX_DOORBELL, msg_data);
559 int dw_pcie_ep_raise_msix_irq(struct dw_pcie_ep *ep, u8 func_no,
562 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
563 struct dw_pcie_ep_func *ep_func;
564 struct pci_epf_msix_tbl *msix_tbl;
565 struct pci_epc *epc = ep->epc;
566 unsigned int func_offset = 0;
567 u32 reg, msg_data, vec_ctrl;
568 unsigned int aligned_offset;
574 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
575 if (!ep_func || !ep_func->msix_cap)
578 func_offset = dw_pcie_ep_func_select(ep, func_no);
580 reg = ep_func->msix_cap + func_offset + PCI_MSIX_TABLE;
581 tbl_offset = dw_pcie_readl_dbi(pci, reg);
582 bir = (tbl_offset & PCI_MSIX_TABLE_BIR);
583 tbl_offset &= PCI_MSIX_TABLE_OFFSET;
585 msix_tbl = ep->epf_bar[bir]->addr + tbl_offset;
586 msg_addr = msix_tbl[(interrupt_num - 1)].msg_addr;
587 msg_data = msix_tbl[(interrupt_num - 1)].msg_data;
588 vec_ctrl = msix_tbl[(interrupt_num - 1)].vector_ctrl;
590 if (vec_ctrl & PCI_MSIX_ENTRY_CTRL_MASKBIT) {
591 dev_dbg(pci->dev, "MSI-X entry ctrl set\n");
595 aligned_offset = msg_addr & (epc->mem->window.page_size - 1);
596 ret = dw_pcie_ep_map_addr(epc, func_no, ep->msi_mem_phys, msg_addr,
597 epc->mem->window.page_size);
601 writel(msg_data, ep->msi_mem + aligned_offset);
603 dw_pcie_ep_unmap_addr(epc, func_no, ep->msi_mem_phys);
608 void dw_pcie_ep_exit(struct dw_pcie_ep *ep)
610 struct pci_epc *epc = ep->epc;
612 pci_epc_mem_free_addr(epc, ep->msi_mem_phys, ep->msi_mem,
613 epc->mem->window.page_size);
615 pci_epc_mem_exit(epc);
618 static unsigned int dw_pcie_ep_find_ext_capability(struct dw_pcie *pci, int cap)
621 int pos = PCI_CFG_SPACE_SIZE;
624 header = dw_pcie_readl_dbi(pci, pos);
625 if (PCI_EXT_CAP_ID(header) == cap)
628 pos = PCI_EXT_CAP_NEXT(header);
636 int dw_pcie_ep_init_complete(struct dw_pcie_ep *ep)
638 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
645 hdr_type = dw_pcie_readb_dbi(pci, PCI_HEADER_TYPE) &
646 PCI_HEADER_TYPE_MASK;
647 if (hdr_type != PCI_HEADER_TYPE_NORMAL) {
649 "PCIe controller is not set to EP mode (hdr_type:0x%x)!\n",
654 offset = dw_pcie_ep_find_ext_capability(pci, PCI_EXT_CAP_ID_REBAR);
656 dw_pcie_dbi_ro_wr_en(pci);
659 reg = dw_pcie_readl_dbi(pci, offset + PCI_REBAR_CTRL);
660 nbars = (reg & PCI_REBAR_CTRL_NBAR_MASK) >>
661 PCI_REBAR_CTRL_NBAR_SHIFT;
663 for (i = 0; i < nbars; i++, offset += PCI_REBAR_CTRL)
664 dw_pcie_writel_dbi(pci, offset + PCI_REBAR_CAP, 0x0);
668 dw_pcie_dbi_ro_wr_dis(pci);
672 EXPORT_SYMBOL_GPL(dw_pcie_ep_init_complete);
674 int dw_pcie_ep_init(struct dw_pcie_ep *ep)
680 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
681 struct device *dev = pci->dev;
682 struct device_node *np = dev->of_node;
683 const struct pci_epc_features *epc_features;
684 struct dw_pcie_ep_func *ep_func;
686 INIT_LIST_HEAD(&ep->func_list);
688 if (!pci->dbi_base || !pci->dbi_base2) {
689 dev_err(dev, "dbi_base/dbi_base2 is not populated\n");
693 ret = of_property_read_u32(np, "num-ib-windows", &ep->num_ib_windows);
695 dev_err(dev, "Unable to read *num-ib-windows* property\n");
698 if (ep->num_ib_windows > MAX_IATU_IN) {
699 dev_err(dev, "Invalid *num-ib-windows*\n");
703 ret = of_property_read_u32(np, "num-ob-windows", &ep->num_ob_windows);
705 dev_err(dev, "Unable to read *num-ob-windows* property\n");
708 if (ep->num_ob_windows > MAX_IATU_OUT) {
709 dev_err(dev, "Invalid *num-ob-windows*\n");
713 ep->ib_window_map = devm_kcalloc(dev,
714 BITS_TO_LONGS(ep->num_ib_windows),
717 if (!ep->ib_window_map)
720 ep->ob_window_map = devm_kcalloc(dev,
721 BITS_TO_LONGS(ep->num_ob_windows),
724 if (!ep->ob_window_map)
727 addr = devm_kcalloc(dev, ep->num_ob_windows, sizeof(phys_addr_t),
731 ep->outbound_addr = addr;
733 if (pci->link_gen < 1)
734 pci->link_gen = of_pci_get_max_link_speed(np);
736 epc = devm_pci_epc_create(dev, &epc_ops);
738 dev_err(dev, "Failed to create epc device\n");
743 epc_set_drvdata(epc, ep);
745 ret = of_property_read_u8(np, "max-functions", &epc->max_functions);
747 epc->max_functions = 1;
749 for (func_no = 0; func_no < epc->max_functions; func_no++) {
750 ep_func = devm_kzalloc(dev, sizeof(*ep_func), GFP_KERNEL);
754 ep_func->func_no = func_no;
755 ep_func->msi_cap = dw_pcie_ep_find_capability(ep, func_no,
757 ep_func->msix_cap = dw_pcie_ep_find_capability(ep, func_no,
760 list_add_tail(&ep_func->list, &ep->func_list);
763 if (ep->ops->ep_init)
764 ep->ops->ep_init(ep);
766 ret = pci_epc_mem_init(epc, ep->phys_base, ep->addr_size,
769 dev_err(dev, "Failed to initialize address space\n");
773 ep->msi_mem = pci_epc_mem_alloc_addr(epc, &ep->msi_mem_phys,
774 epc->mem->window.page_size);
777 dev_err(dev, "Failed to reserve memory for MSI/MSI-X\n");
778 goto err_exit_epc_mem;
781 if (ep->ops->get_features) {
782 epc_features = ep->ops->get_features(ep);
783 if (epc_features->core_init_notifier)
787 ret = dw_pcie_ep_init_complete(ep);
789 goto err_free_epc_mem;
794 pci_epc_mem_free_addr(epc, ep->msi_mem_phys, ep->msi_mem,
795 epc->mem->window.page_size);
798 pci_epc_mem_exit(epc);
802 EXPORT_SYMBOL_GPL(dw_pcie_ep_init);