1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2017 Cadence
3 // Cadence PCIe endpoint controller driver.
4 // Author: Cyrille Pitchen <cyrille.pitchen@free-electrons.com>
6 #include <linux/delay.h>
7 #include <linux/kernel.h>
9 #include <linux/pci-epc.h>
10 #include <linux/platform_device.h>
11 #include <linux/sizes.h>
13 #include "pcie-cadence.h"
15 #define CDNS_PCIE_EP_MIN_APERTURE 128 /* 128 bytes */
16 #define CDNS_PCIE_EP_IRQ_PCI_ADDR_NONE 0x1
17 #define CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY 0x3
19 static u8 cdns_pcie_get_fn_from_vfn(struct cdns_pcie *pcie, u8 fn, u8 vfn)
21 u32 cap = CDNS_PCIE_EP_FUNC_SRIOV_CAP_OFFSET;
22 u32 first_vf_offset, stride;
27 first_vf_offset = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_SRIOV_VF_OFFSET);
28 stride = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_SRIOV_VF_STRIDE);
29 fn = fn + first_vf_offset + ((vfn - 1) * stride);
34 static int cdns_pcie_ep_write_header(struct pci_epc *epc, u8 fn, u8 vfn,
35 struct pci_epf_header *hdr)
37 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
38 u32 cap = CDNS_PCIE_EP_FUNC_SRIOV_CAP_OFFSET;
39 struct cdns_pcie *pcie = &ep->pcie;
43 dev_err(&epc->dev, "Only Virtual Function #1 has deviceID\n");
45 } else if (vfn == 1) {
46 reg = cap + PCI_SRIOV_VF_DID;
47 cdns_pcie_ep_fn_writew(pcie, fn, reg, hdr->deviceid);
51 cdns_pcie_ep_fn_writew(pcie, fn, PCI_DEVICE_ID, hdr->deviceid);
52 cdns_pcie_ep_fn_writeb(pcie, fn, PCI_REVISION_ID, hdr->revid);
53 cdns_pcie_ep_fn_writeb(pcie, fn, PCI_CLASS_PROG, hdr->progif_code);
54 cdns_pcie_ep_fn_writew(pcie, fn, PCI_CLASS_DEVICE,
55 hdr->subclass_code | hdr->baseclass_code << 8);
56 cdns_pcie_ep_fn_writeb(pcie, fn, PCI_CACHE_LINE_SIZE,
57 hdr->cache_line_size);
58 cdns_pcie_ep_fn_writew(pcie, fn, PCI_SUBSYSTEM_ID, hdr->subsys_id);
59 cdns_pcie_ep_fn_writeb(pcie, fn, PCI_INTERRUPT_PIN, hdr->interrupt_pin);
62 * Vendor ID can only be modified from function 0, all other functions
63 * use the same vendor ID as function 0.
66 /* Update the vendor IDs. */
67 u32 id = CDNS_PCIE_LM_ID_VENDOR(hdr->vendorid) |
68 CDNS_PCIE_LM_ID_SUBSYS(hdr->subsys_vendor_id);
70 cdns_pcie_writel(pcie, CDNS_PCIE_LM_ID, id);
76 static int cdns_pcie_ep_set_bar(struct pci_epc *epc, u8 fn, u8 vfn,
77 struct pci_epf_bar *epf_bar)
79 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
80 struct cdns_pcie_epf *epf = &ep->epf[fn];
81 struct cdns_pcie *pcie = &ep->pcie;
82 dma_addr_t bar_phys = epf_bar->phys_addr;
83 enum pci_barno bar = epf_bar->barno;
84 int flags = epf_bar->flags;
85 u32 addr0, addr1, reg, cfg, b, aperture, ctrl;
88 /* BAR size is 2^(aperture + 7) */
89 sz = max_t(size_t, epf_bar->size, CDNS_PCIE_EP_MIN_APERTURE);
91 * roundup_pow_of_two() returns an unsigned long, which is not suited
94 sz = 1ULL << fls64(sz - 1);
95 aperture = ilog2(sz) - 7; /* 128B -> 0, 256B -> 1, 512B -> 2, ... */
97 if ((flags & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
98 ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_IO_32BITS;
100 bool is_prefetch = !!(flags & PCI_BASE_ADDRESS_MEM_PREFETCH);
101 bool is_64bits = sz > SZ_2G;
103 if (is_64bits && (bar & 1))
106 if (is_64bits && !(flags & PCI_BASE_ADDRESS_MEM_TYPE_64))
107 epf_bar->flags |= PCI_BASE_ADDRESS_MEM_TYPE_64;
109 if (is_64bits && is_prefetch)
110 ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_PREFETCH_MEM_64BITS;
111 else if (is_prefetch)
112 ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_PREFETCH_MEM_32BITS;
114 ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_MEM_64BITS;
116 ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_MEM_32BITS;
119 addr0 = lower_32_bits(bar_phys);
120 addr1 = upper_32_bits(bar_phys);
123 reg = CDNS_PCIE_LM_EP_VFUNC_BAR_CFG(bar, fn);
125 reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG(bar, fn);
126 b = (bar < BAR_4) ? bar : bar - BAR_4;
128 if (vfn == 0 || vfn == 1) {
129 cfg = cdns_pcie_readl(pcie, reg);
130 cfg &= ~(CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MASK(b) |
131 CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(b));
132 cfg |= (CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE(b, aperture) |
133 CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL(b, ctrl));
134 cdns_pcie_writel(pcie, reg, cfg);
137 fn = cdns_pcie_get_fn_from_vfn(pcie, fn, vfn);
138 cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR0(fn, bar),
140 cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR1(fn, bar),
144 epf = &epf->epf[vfn - 1];
145 epf->epf_bar[bar] = epf_bar;
150 static void cdns_pcie_ep_clear_bar(struct pci_epc *epc, u8 fn, u8 vfn,
151 struct pci_epf_bar *epf_bar)
153 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
154 struct cdns_pcie_epf *epf = &ep->epf[fn];
155 struct cdns_pcie *pcie = &ep->pcie;
156 enum pci_barno bar = epf_bar->barno;
157 u32 reg, cfg, b, ctrl;
160 reg = CDNS_PCIE_LM_EP_VFUNC_BAR_CFG(bar, fn);
162 reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG(bar, fn);
163 b = (bar < BAR_4) ? bar : bar - BAR_4;
165 if (vfn == 0 || vfn == 1) {
166 ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_DISABLED;
167 cfg = cdns_pcie_readl(pcie, reg);
168 cfg &= ~(CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MASK(b) |
169 CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(b));
170 cfg |= CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL(b, ctrl);
171 cdns_pcie_writel(pcie, reg, cfg);
174 fn = cdns_pcie_get_fn_from_vfn(pcie, fn, vfn);
175 cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR0(fn, bar), 0);
176 cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR1(fn, bar), 0);
179 epf = &epf->epf[vfn - 1];
180 epf->epf_bar[bar] = NULL;
183 static int cdns_pcie_ep_map_addr(struct pci_epc *epc, u8 fn, u8 vfn,
184 phys_addr_t addr, u64 pci_addr, size_t size)
186 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
187 struct cdns_pcie *pcie = &ep->pcie;
190 r = find_first_zero_bit(&ep->ob_region_map,
191 sizeof(ep->ob_region_map) * BITS_PER_LONG);
192 if (r >= ep->max_regions - 1) {
193 dev_err(&epc->dev, "no free outbound region\n");
197 fn = cdns_pcie_get_fn_from_vfn(pcie, fn, vfn);
198 cdns_pcie_set_outbound_region(pcie, 0, fn, r, false, addr, pci_addr, size);
200 set_bit(r, &ep->ob_region_map);
201 ep->ob_addr[r] = addr;
206 static void cdns_pcie_ep_unmap_addr(struct pci_epc *epc, u8 fn, u8 vfn,
209 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
210 struct cdns_pcie *pcie = &ep->pcie;
213 for (r = 0; r < ep->max_regions - 1; r++)
214 if (ep->ob_addr[r] == addr)
217 if (r == ep->max_regions - 1)
220 cdns_pcie_reset_outbound_region(pcie, r);
223 clear_bit(r, &ep->ob_region_map);
226 static int cdns_pcie_ep_set_msi(struct pci_epc *epc, u8 fn, u8 vfn, u8 mmc)
228 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
229 struct cdns_pcie *pcie = &ep->pcie;
230 u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
233 fn = cdns_pcie_get_fn_from_vfn(pcie, fn, vfn);
236 * Set the Multiple Message Capable bitfield into the Message Control
239 flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
240 flags = (flags & ~PCI_MSI_FLAGS_QMASK) | (mmc << 1);
241 flags |= PCI_MSI_FLAGS_64BIT;
242 flags &= ~PCI_MSI_FLAGS_MASKBIT;
243 cdns_pcie_ep_fn_writew(pcie, fn, cap + PCI_MSI_FLAGS, flags);
248 static int cdns_pcie_ep_get_msi(struct pci_epc *epc, u8 fn, u8 vfn)
250 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
251 struct cdns_pcie *pcie = &ep->pcie;
252 u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
255 fn = cdns_pcie_get_fn_from_vfn(pcie, fn, vfn);
257 /* Validate that the MSI feature is actually enabled. */
258 flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
259 if (!(flags & PCI_MSI_FLAGS_ENABLE))
263 * Get the Multiple Message Enable bitfield from the Message Control
266 mme = (flags & PCI_MSI_FLAGS_QSIZE) >> 4;
271 static int cdns_pcie_ep_get_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
273 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
274 struct cdns_pcie *pcie = &ep->pcie;
275 u32 cap = CDNS_PCIE_EP_FUNC_MSIX_CAP_OFFSET;
278 func_no = cdns_pcie_get_fn_from_vfn(pcie, func_no, vfunc_no);
280 reg = cap + PCI_MSIX_FLAGS;
281 val = cdns_pcie_ep_fn_readw(pcie, func_no, reg);
282 if (!(val & PCI_MSIX_FLAGS_ENABLE))
285 val &= PCI_MSIX_FLAGS_QSIZE;
290 static int cdns_pcie_ep_set_msix(struct pci_epc *epc, u8 fn, u8 vfn,
291 u16 interrupts, enum pci_barno bir,
294 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
295 struct cdns_pcie *pcie = &ep->pcie;
296 u32 cap = CDNS_PCIE_EP_FUNC_MSIX_CAP_OFFSET;
299 fn = cdns_pcie_get_fn_from_vfn(pcie, fn, vfn);
301 reg = cap + PCI_MSIX_FLAGS;
302 val = cdns_pcie_ep_fn_readw(pcie, fn, reg);
303 val &= ~PCI_MSIX_FLAGS_QSIZE;
305 cdns_pcie_ep_fn_writew(pcie, fn, reg, val);
307 /* Set MSIX BAR and offset */
308 reg = cap + PCI_MSIX_TABLE;
310 cdns_pcie_ep_fn_writel(pcie, fn, reg, val);
312 /* Set PBA BAR and offset. BAR must match MSIX BAR */
313 reg = cap + PCI_MSIX_PBA;
314 val = (offset + (interrupts * PCI_MSIX_ENTRY_SIZE)) | bir;
315 cdns_pcie_ep_fn_writel(pcie, fn, reg, val);
320 static void cdns_pcie_ep_assert_intx(struct cdns_pcie_ep *ep, u8 fn, u8 intx,
323 struct cdns_pcie *pcie = &ep->pcie;
331 /* Set the outbound region if needed. */
332 if (unlikely(ep->irq_pci_addr != CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY ||
333 ep->irq_pci_fn != fn)) {
334 /* First region was reserved for IRQ writes. */
335 cdns_pcie_set_outbound_region_for_normal_msg(pcie, 0, fn, 0,
337 ep->irq_pci_addr = CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY;
342 ep->irq_pending |= BIT(intx);
343 msg_code = MSG_CODE_ASSERT_INTA + intx;
345 ep->irq_pending &= ~BIT(intx);
346 msg_code = MSG_CODE_DEASSERT_INTA + intx;
349 spin_lock_irqsave(&ep->lock, flags);
350 status = cdns_pcie_ep_fn_readw(pcie, fn, PCI_STATUS);
351 if (((status & PCI_STATUS_INTERRUPT) != 0) ^ (ep->irq_pending != 0)) {
352 status ^= PCI_STATUS_INTERRUPT;
353 cdns_pcie_ep_fn_writew(pcie, fn, PCI_STATUS, status);
355 spin_unlock_irqrestore(&ep->lock, flags);
357 offset = CDNS_PCIE_NORMAL_MSG_ROUTING(MSG_ROUTING_LOCAL) |
358 CDNS_PCIE_NORMAL_MSG_CODE(msg_code) |
359 CDNS_PCIE_MSG_NO_DATA;
360 writel(0, ep->irq_cpu_addr + offset);
363 static int cdns_pcie_ep_send_legacy_irq(struct cdns_pcie_ep *ep, u8 fn, u8 vfn,
368 cmd = cdns_pcie_ep_fn_readw(&ep->pcie, fn, PCI_COMMAND);
369 if (cmd & PCI_COMMAND_INTX_DISABLE)
372 cdns_pcie_ep_assert_intx(ep, fn, intx, true);
374 * The mdelay() value was taken from dra7xx_pcie_raise_legacy_irq()
377 cdns_pcie_ep_assert_intx(ep, fn, intx, false);
381 static int cdns_pcie_ep_send_msi_irq(struct cdns_pcie_ep *ep, u8 fn, u8 vfn,
384 struct cdns_pcie *pcie = &ep->pcie;
385 u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
386 u16 flags, mme, data, data_mask;
388 u64 pci_addr, pci_addr_mask = 0xff;
390 fn = cdns_pcie_get_fn_from_vfn(pcie, fn, vfn);
392 /* Check whether the MSI feature has been enabled by the PCI host. */
393 flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
394 if (!(flags & PCI_MSI_FLAGS_ENABLE))
397 /* Get the number of enabled MSIs */
398 mme = (flags & PCI_MSI_FLAGS_QSIZE) >> 4;
399 msi_count = 1 << mme;
400 if (!interrupt_num || interrupt_num > msi_count)
403 /* Compute the data value to be written. */
404 data_mask = msi_count - 1;
405 data = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_DATA_64);
406 data = (data & ~data_mask) | ((interrupt_num - 1) & data_mask);
408 /* Get the PCI address where to write the data into. */
409 pci_addr = cdns_pcie_ep_fn_readl(pcie, fn, cap + PCI_MSI_ADDRESS_HI);
411 pci_addr |= cdns_pcie_ep_fn_readl(pcie, fn, cap + PCI_MSI_ADDRESS_LO);
412 pci_addr &= GENMASK_ULL(63, 2);
414 /* Set the outbound region if needed. */
415 if (unlikely(ep->irq_pci_addr != (pci_addr & ~pci_addr_mask) ||
416 ep->irq_pci_fn != fn)) {
417 /* First region was reserved for IRQ writes. */
418 cdns_pcie_set_outbound_region(pcie, 0, fn, 0,
421 pci_addr & ~pci_addr_mask,
423 ep->irq_pci_addr = (pci_addr & ~pci_addr_mask);
426 writel(data, ep->irq_cpu_addr + (pci_addr & pci_addr_mask));
431 static int cdns_pcie_ep_map_msi_irq(struct pci_epc *epc, u8 fn, u8 vfn,
432 phys_addr_t addr, u8 interrupt_num,
433 u32 entry_size, u32 *msi_data,
434 u32 *msi_addr_offset)
436 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
437 u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
438 struct cdns_pcie *pcie = &ep->pcie;
439 u64 pci_addr, pci_addr_mask = 0xff;
440 u16 flags, mme, data, data_mask;
445 fn = cdns_pcie_get_fn_from_vfn(pcie, fn, vfn);
447 /* Check whether the MSI feature has been enabled by the PCI host. */
448 flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
449 if (!(flags & PCI_MSI_FLAGS_ENABLE))
452 /* Get the number of enabled MSIs */
453 mme = (flags & PCI_MSI_FLAGS_QSIZE) >> 4;
454 msi_count = 1 << mme;
455 if (!interrupt_num || interrupt_num > msi_count)
458 /* Compute the data value to be written. */
459 data_mask = msi_count - 1;
460 data = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_DATA_64);
461 data = data & ~data_mask;
463 /* Get the PCI address where to write the data into. */
464 pci_addr = cdns_pcie_ep_fn_readl(pcie, fn, cap + PCI_MSI_ADDRESS_HI);
466 pci_addr |= cdns_pcie_ep_fn_readl(pcie, fn, cap + PCI_MSI_ADDRESS_LO);
467 pci_addr &= GENMASK_ULL(63, 2);
469 for (i = 0; i < interrupt_num; i++) {
470 ret = cdns_pcie_ep_map_addr(epc, fn, vfn, addr,
471 pci_addr & ~pci_addr_mask,
475 addr = addr + entry_size;
479 *msi_addr_offset = pci_addr & pci_addr_mask;
484 static int cdns_pcie_ep_send_msix_irq(struct cdns_pcie_ep *ep, u8 fn, u8 vfn,
487 u32 cap = CDNS_PCIE_EP_FUNC_MSIX_CAP_OFFSET;
488 u32 tbl_offset, msg_data, reg;
489 struct cdns_pcie *pcie = &ep->pcie;
490 struct pci_epf_msix_tbl *msix_tbl;
491 struct cdns_pcie_epf *epf;
492 u64 pci_addr_mask = 0xff;
499 epf = &epf->epf[vfn - 1];
501 fn = cdns_pcie_get_fn_from_vfn(pcie, fn, vfn);
503 /* Check whether the MSI-X feature has been enabled by the PCI host. */
504 flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSIX_FLAGS);
505 if (!(flags & PCI_MSIX_FLAGS_ENABLE))
508 reg = cap + PCI_MSIX_TABLE;
509 tbl_offset = cdns_pcie_ep_fn_readl(pcie, fn, reg);
510 bir = tbl_offset & PCI_MSIX_TABLE_BIR;
511 tbl_offset &= PCI_MSIX_TABLE_OFFSET;
513 msix_tbl = epf->epf_bar[bir]->addr + tbl_offset;
514 msg_addr = msix_tbl[(interrupt_num - 1)].msg_addr;
515 msg_data = msix_tbl[(interrupt_num - 1)].msg_data;
517 /* Set the outbound region if needed. */
518 if (ep->irq_pci_addr != (msg_addr & ~pci_addr_mask) ||
519 ep->irq_pci_fn != fn) {
520 /* First region was reserved for IRQ writes. */
521 cdns_pcie_set_outbound_region(pcie, 0, fn, 0,
524 msg_addr & ~pci_addr_mask,
526 ep->irq_pci_addr = (msg_addr & ~pci_addr_mask);
529 writel(msg_data, ep->irq_cpu_addr + (msg_addr & pci_addr_mask));
534 static int cdns_pcie_ep_raise_irq(struct pci_epc *epc, u8 fn, u8 vfn,
535 enum pci_epc_irq_type type,
538 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
539 struct cdns_pcie *pcie = &ep->pcie;
540 struct device *dev = pcie->dev;
543 case PCI_EPC_IRQ_LEGACY:
545 dev_err(dev, "Cannot raise legacy interrupts for VF\n");
548 return cdns_pcie_ep_send_legacy_irq(ep, fn, vfn, 0);
550 case PCI_EPC_IRQ_MSI:
551 return cdns_pcie_ep_send_msi_irq(ep, fn, vfn, interrupt_num);
553 case PCI_EPC_IRQ_MSIX:
554 return cdns_pcie_ep_send_msix_irq(ep, fn, vfn, interrupt_num);
563 static int cdns_pcie_ep_start(struct pci_epc *epc)
565 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
566 struct cdns_pcie *pcie = &ep->pcie;
567 struct device *dev = pcie->dev;
571 * BIT(0) is hardwired to 1, hence function 0 is always enabled
572 * and can't be disabled anyway.
574 cdns_pcie_writel(pcie, CDNS_PCIE_LM_EP_FUNC_CFG, epc->function_num_map);
576 ret = cdns_pcie_start_link(pcie);
578 dev_err(dev, "Failed to start link\n");
585 static const struct pci_epc_features cdns_pcie_epc_vf_features = {
586 .linkup_notifier = false,
588 .msix_capable = true,
592 static const struct pci_epc_features cdns_pcie_epc_features = {
593 .linkup_notifier = false,
595 .msix_capable = true,
599 static const struct pci_epc_features*
600 cdns_pcie_ep_get_features(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
603 return &cdns_pcie_epc_features;
605 return &cdns_pcie_epc_vf_features;
608 static const struct pci_epc_ops cdns_pcie_epc_ops = {
609 .write_header = cdns_pcie_ep_write_header,
610 .set_bar = cdns_pcie_ep_set_bar,
611 .clear_bar = cdns_pcie_ep_clear_bar,
612 .map_addr = cdns_pcie_ep_map_addr,
613 .unmap_addr = cdns_pcie_ep_unmap_addr,
614 .set_msi = cdns_pcie_ep_set_msi,
615 .get_msi = cdns_pcie_ep_get_msi,
616 .set_msix = cdns_pcie_ep_set_msix,
617 .get_msix = cdns_pcie_ep_get_msix,
618 .raise_irq = cdns_pcie_ep_raise_irq,
619 .map_msi_irq = cdns_pcie_ep_map_msi_irq,
620 .start = cdns_pcie_ep_start,
621 .get_features = cdns_pcie_ep_get_features,
625 int cdns_pcie_ep_setup(struct cdns_pcie_ep *ep)
627 struct device *dev = ep->pcie.dev;
628 struct platform_device *pdev = to_platform_device(dev);
629 struct device_node *np = dev->of_node;
630 struct cdns_pcie *pcie = &ep->pcie;
631 struct cdns_pcie_epf *epf;
632 struct resource *res;
639 pcie->reg_base = devm_platform_ioremap_resource_byname(pdev, "reg");
640 if (IS_ERR(pcie->reg_base)) {
641 dev_err(dev, "missing \"reg\"\n");
642 return PTR_ERR(pcie->reg_base);
645 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem");
647 dev_err(dev, "missing \"mem\"\n");
652 ep->max_regions = CDNS_PCIE_MAX_OB;
653 of_property_read_u32(np, "cdns,max-outbound-regions", &ep->max_regions);
655 ep->ob_addr = devm_kcalloc(dev,
656 ep->max_regions, sizeof(*ep->ob_addr),
661 /* Disable all but function 0 (anyway BIT(0) is hardwired to 1). */
662 cdns_pcie_writel(pcie, CDNS_PCIE_LM_EP_FUNC_CFG, BIT(0));
664 epc = devm_pci_epc_create(dev, &cdns_pcie_epc_ops);
666 dev_err(dev, "failed to create epc device\n");
670 epc_set_drvdata(epc, ep);
672 if (of_property_read_u8(np, "max-functions", &epc->max_functions) < 0)
673 epc->max_functions = 1;
675 ep->epf = devm_kcalloc(dev, epc->max_functions, sizeof(*ep->epf),
680 epc->max_vfs = devm_kcalloc(dev, epc->max_functions,
681 sizeof(*epc->max_vfs), GFP_KERNEL);
685 ret = of_property_read_u8_array(np, "max-virtual-functions",
686 epc->max_vfs, epc->max_functions);
688 for (i = 0; i < epc->max_functions; i++) {
690 if (epc->max_vfs[i] == 0)
692 epf->epf = devm_kcalloc(dev, epc->max_vfs[i],
693 sizeof(*ep->epf), GFP_KERNEL);
699 ret = pci_epc_mem_init(epc, pcie->mem_res->start,
700 resource_size(pcie->mem_res), PAGE_SIZE);
702 dev_err(dev, "failed to initialize the memory space\n");
706 ep->irq_cpu_addr = pci_epc_mem_alloc_addr(epc, &ep->irq_phys_addr,
708 if (!ep->irq_cpu_addr) {
709 dev_err(dev, "failed to reserve memory space for MSI\n");
713 ep->irq_pci_addr = CDNS_PCIE_EP_IRQ_PCI_ADDR_NONE;
714 /* Reserve region 0 for IRQs */
715 set_bit(0, &ep->ob_region_map);
717 if (ep->quirk_detect_quiet_flag)
718 cdns_pcie_detect_quiet_min_delay_set(&ep->pcie);
720 spin_lock_init(&ep->lock);
725 pci_epc_mem_exit(epc);