pcie-cadence-ep.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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>
  5. #include <linux/delay.h>
  6. #include <linux/kernel.h>
  7. #include <linux/of.h>
  8. #include <linux/pci-epc.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/pm_runtime.h>
  11. #include <linux/sizes.h>
  12. #include "pcie-cadence.h"
  13. #define CDNS_PCIE_EP_MIN_APERTURE 128 /* 128 bytes */
  14. #define CDNS_PCIE_EP_IRQ_PCI_ADDR_NONE 0x1
  15. #define CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY 0x3
  16. /**
  17. * struct cdns_pcie_ep - private data for this PCIe endpoint controller driver
  18. * @pcie: Cadence PCIe controller
  19. * @max_regions: maximum number of regions supported by hardware
  20. * @ob_region_map: bitmask of mapped outbound regions
  21. * @ob_addr: base addresses in the AXI bus where the outbound regions start
  22. * @irq_phys_addr: base address on the AXI bus where the MSI/legacy IRQ
  23. * dedicated outbound regions is mapped.
  24. * @irq_cpu_addr: base address in the CPU space where a write access triggers
  25. * the sending of a memory write (MSI) / normal message (legacy
  26. * IRQ) TLP through the PCIe bus.
  27. * @irq_pci_addr: used to save the current mapping of the MSI/legacy IRQ
  28. * dedicated outbound region.
  29. * @irq_pci_fn: the latest PCI function that has updated the mapping of
  30. * the MSI/legacy IRQ dedicated outbound region.
  31. * @irq_pending: bitmask of asserted legacy IRQs.
  32. */
  33. struct cdns_pcie_ep {
  34. struct cdns_pcie pcie;
  35. u32 max_regions;
  36. unsigned long ob_region_map;
  37. phys_addr_t *ob_addr;
  38. phys_addr_t irq_phys_addr;
  39. void __iomem *irq_cpu_addr;
  40. u64 irq_pci_addr;
  41. u8 irq_pci_fn;
  42. u8 irq_pending;
  43. };
  44. static int cdns_pcie_ep_write_header(struct pci_epc *epc, u8 fn,
  45. struct pci_epf_header *hdr)
  46. {
  47. struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
  48. struct cdns_pcie *pcie = &ep->pcie;
  49. cdns_pcie_ep_fn_writew(pcie, fn, PCI_DEVICE_ID, hdr->deviceid);
  50. cdns_pcie_ep_fn_writeb(pcie, fn, PCI_REVISION_ID, hdr->revid);
  51. cdns_pcie_ep_fn_writeb(pcie, fn, PCI_CLASS_PROG, hdr->progif_code);
  52. cdns_pcie_ep_fn_writew(pcie, fn, PCI_CLASS_DEVICE,
  53. hdr->subclass_code | hdr->baseclass_code << 8);
  54. cdns_pcie_ep_fn_writeb(pcie, fn, PCI_CACHE_LINE_SIZE,
  55. hdr->cache_line_size);
  56. cdns_pcie_ep_fn_writew(pcie, fn, PCI_SUBSYSTEM_ID, hdr->subsys_id);
  57. cdns_pcie_ep_fn_writeb(pcie, fn, PCI_INTERRUPT_PIN, hdr->interrupt_pin);
  58. /*
  59. * Vendor ID can only be modified from function 0, all other functions
  60. * use the same vendor ID as function 0.
  61. */
  62. if (fn == 0) {
  63. /* Update the vendor IDs. */
  64. u32 id = CDNS_PCIE_LM_ID_VENDOR(hdr->vendorid) |
  65. CDNS_PCIE_LM_ID_SUBSYS(hdr->subsys_vendor_id);
  66. cdns_pcie_writel(pcie, CDNS_PCIE_LM_ID, id);
  67. }
  68. return 0;
  69. }
  70. static int cdns_pcie_ep_set_bar(struct pci_epc *epc, u8 fn,
  71. struct pci_epf_bar *epf_bar)
  72. {
  73. struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
  74. struct cdns_pcie *pcie = &ep->pcie;
  75. dma_addr_t bar_phys = epf_bar->phys_addr;
  76. enum pci_barno bar = epf_bar->barno;
  77. int flags = epf_bar->flags;
  78. u32 addr0, addr1, reg, cfg, b, aperture, ctrl;
  79. u64 sz;
  80. /* BAR size is 2^(aperture + 7) */
  81. sz = max_t(size_t, epf_bar->size, CDNS_PCIE_EP_MIN_APERTURE);
  82. /*
  83. * roundup_pow_of_two() returns an unsigned long, which is not suited
  84. * for 64bit values.
  85. */
  86. sz = 1ULL << fls64(sz - 1);
  87. aperture = ilog2(sz) - 7; /* 128B -> 0, 256B -> 1, 512B -> 2, ... */
  88. if ((flags & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
  89. ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_IO_32BITS;
  90. } else {
  91. bool is_prefetch = !!(flags & PCI_BASE_ADDRESS_MEM_PREFETCH);
  92. bool is_64bits = sz > SZ_2G;
  93. if (is_64bits && (bar & 1))
  94. return -EINVAL;
  95. if (is_64bits && !(flags & PCI_BASE_ADDRESS_MEM_TYPE_64))
  96. epf_bar->flags |= PCI_BASE_ADDRESS_MEM_TYPE_64;
  97. if (is_64bits && is_prefetch)
  98. ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_PREFETCH_MEM_64BITS;
  99. else if (is_prefetch)
  100. ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_PREFETCH_MEM_32BITS;
  101. else if (is_64bits)
  102. ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_MEM_64BITS;
  103. else
  104. ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_MEM_32BITS;
  105. }
  106. addr0 = lower_32_bits(bar_phys);
  107. addr1 = upper_32_bits(bar_phys);
  108. cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR0(fn, bar),
  109. addr0);
  110. cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR1(fn, bar),
  111. addr1);
  112. if (bar < BAR_4) {
  113. reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG0(fn);
  114. b = bar;
  115. } else {
  116. reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG1(fn);
  117. b = bar - BAR_4;
  118. }
  119. cfg = cdns_pcie_readl(pcie, reg);
  120. cfg &= ~(CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MASK(b) |
  121. CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(b));
  122. cfg |= (CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE(b, aperture) |
  123. CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL(b, ctrl));
  124. cdns_pcie_writel(pcie, reg, cfg);
  125. return 0;
  126. }
  127. static void cdns_pcie_ep_clear_bar(struct pci_epc *epc, u8 fn,
  128. struct pci_epf_bar *epf_bar)
  129. {
  130. struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
  131. struct cdns_pcie *pcie = &ep->pcie;
  132. enum pci_barno bar = epf_bar->barno;
  133. u32 reg, cfg, b, ctrl;
  134. if (bar < BAR_4) {
  135. reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG0(fn);
  136. b = bar;
  137. } else {
  138. reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG1(fn);
  139. b = bar - BAR_4;
  140. }
  141. ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_DISABLED;
  142. cfg = cdns_pcie_readl(pcie, reg);
  143. cfg &= ~(CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MASK(b) |
  144. CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(b));
  145. cfg |= CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL(b, ctrl);
  146. cdns_pcie_writel(pcie, reg, cfg);
  147. cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR0(fn, bar), 0);
  148. cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR1(fn, bar), 0);
  149. }
  150. static int cdns_pcie_ep_map_addr(struct pci_epc *epc, u8 fn, phys_addr_t addr,
  151. u64 pci_addr, size_t size)
  152. {
  153. struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
  154. struct cdns_pcie *pcie = &ep->pcie;
  155. u32 r;
  156. r = find_first_zero_bit(&ep->ob_region_map,
  157. sizeof(ep->ob_region_map) * BITS_PER_LONG);
  158. if (r >= ep->max_regions - 1) {
  159. dev_err(&epc->dev, "no free outbound region\n");
  160. return -EINVAL;
  161. }
  162. cdns_pcie_set_outbound_region(pcie, fn, r, false, addr, pci_addr, size);
  163. set_bit(r, &ep->ob_region_map);
  164. ep->ob_addr[r] = addr;
  165. return 0;
  166. }
  167. static void cdns_pcie_ep_unmap_addr(struct pci_epc *epc, u8 fn,
  168. phys_addr_t addr)
  169. {
  170. struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
  171. struct cdns_pcie *pcie = &ep->pcie;
  172. u32 r;
  173. for (r = 0; r < ep->max_regions - 1; r++)
  174. if (ep->ob_addr[r] == addr)
  175. break;
  176. if (r == ep->max_regions - 1)
  177. return;
  178. cdns_pcie_reset_outbound_region(pcie, r);
  179. ep->ob_addr[r] = 0;
  180. clear_bit(r, &ep->ob_region_map);
  181. }
  182. static int cdns_pcie_ep_set_msi(struct pci_epc *epc, u8 fn, u8 mmc)
  183. {
  184. struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
  185. struct cdns_pcie *pcie = &ep->pcie;
  186. u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
  187. u16 flags;
  188. /*
  189. * Set the Multiple Message Capable bitfield into the Message Control
  190. * register.
  191. */
  192. flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
  193. flags = (flags & ~PCI_MSI_FLAGS_QMASK) | (mmc << 1);
  194. flags |= PCI_MSI_FLAGS_64BIT;
  195. flags &= ~PCI_MSI_FLAGS_MASKBIT;
  196. cdns_pcie_ep_fn_writew(pcie, fn, cap + PCI_MSI_FLAGS, flags);
  197. return 0;
  198. }
  199. static int cdns_pcie_ep_get_msi(struct pci_epc *epc, u8 fn)
  200. {
  201. struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
  202. struct cdns_pcie *pcie = &ep->pcie;
  203. u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
  204. u16 flags, mmc, mme;
  205. /* Validate that the MSI feature is actually enabled. */
  206. flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
  207. if (!(flags & PCI_MSI_FLAGS_ENABLE))
  208. return -EINVAL;
  209. /*
  210. * Get the Multiple Message Enable bitfield from the Message Control
  211. * register.
  212. */
  213. mmc = (flags & PCI_MSI_FLAGS_QMASK) >> 1;
  214. mme = (flags & PCI_MSI_FLAGS_QSIZE) >> 4;
  215. return mme;
  216. }
  217. static void cdns_pcie_ep_assert_intx(struct cdns_pcie_ep *ep, u8 fn,
  218. u8 intx, bool is_asserted)
  219. {
  220. struct cdns_pcie *pcie = &ep->pcie;
  221. u32 r = ep->max_regions - 1;
  222. u32 offset;
  223. u16 status;
  224. u8 msg_code;
  225. intx &= 3;
  226. /* Set the outbound region if needed. */
  227. if (unlikely(ep->irq_pci_addr != CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY ||
  228. ep->irq_pci_fn != fn)) {
  229. /* Last region was reserved for IRQ writes. */
  230. cdns_pcie_set_outbound_region_for_normal_msg(pcie, fn, r,
  231. ep->irq_phys_addr);
  232. ep->irq_pci_addr = CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY;
  233. ep->irq_pci_fn = fn;
  234. }
  235. if (is_asserted) {
  236. ep->irq_pending |= BIT(intx);
  237. msg_code = MSG_CODE_ASSERT_INTA + intx;
  238. } else {
  239. ep->irq_pending &= ~BIT(intx);
  240. msg_code = MSG_CODE_DEASSERT_INTA + intx;
  241. }
  242. status = cdns_pcie_ep_fn_readw(pcie, fn, PCI_STATUS);
  243. if (((status & PCI_STATUS_INTERRUPT) != 0) ^ (ep->irq_pending != 0)) {
  244. status ^= PCI_STATUS_INTERRUPT;
  245. cdns_pcie_ep_fn_writew(pcie, fn, PCI_STATUS, status);
  246. }
  247. offset = CDNS_PCIE_NORMAL_MSG_ROUTING(MSG_ROUTING_LOCAL) |
  248. CDNS_PCIE_NORMAL_MSG_CODE(msg_code) |
  249. CDNS_PCIE_MSG_NO_DATA;
  250. writel(0, ep->irq_cpu_addr + offset);
  251. }
  252. static int cdns_pcie_ep_send_legacy_irq(struct cdns_pcie_ep *ep, u8 fn, u8 intx)
  253. {
  254. u16 cmd;
  255. cmd = cdns_pcie_ep_fn_readw(&ep->pcie, fn, PCI_COMMAND);
  256. if (cmd & PCI_COMMAND_INTX_DISABLE)
  257. return -EINVAL;
  258. cdns_pcie_ep_assert_intx(ep, fn, intx, true);
  259. /*
  260. * The mdelay() value was taken from dra7xx_pcie_raise_legacy_irq()
  261. * from drivers/pci/dwc/pci-dra7xx.c
  262. */
  263. mdelay(1);
  264. cdns_pcie_ep_assert_intx(ep, fn, intx, false);
  265. return 0;
  266. }
  267. static int cdns_pcie_ep_send_msi_irq(struct cdns_pcie_ep *ep, u8 fn,
  268. u8 interrupt_num)
  269. {
  270. struct cdns_pcie *pcie = &ep->pcie;
  271. u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
  272. u16 flags, mme, data, data_mask;
  273. u8 msi_count;
  274. u64 pci_addr, pci_addr_mask = 0xff;
  275. /* Check whether the MSI feature has been enabled by the PCI host. */
  276. flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
  277. if (!(flags & PCI_MSI_FLAGS_ENABLE))
  278. return -EINVAL;
  279. /* Get the number of enabled MSIs */
  280. mme = (flags & PCI_MSI_FLAGS_QSIZE) >> 4;
  281. msi_count = 1 << mme;
  282. if (!interrupt_num || interrupt_num > msi_count)
  283. return -EINVAL;
  284. /* Compute the data value to be written. */
  285. data_mask = msi_count - 1;
  286. data = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_DATA_64);
  287. data = (data & ~data_mask) | ((interrupt_num - 1) & data_mask);
  288. /* Get the PCI address where to write the data into. */
  289. pci_addr = cdns_pcie_ep_fn_readl(pcie, fn, cap + PCI_MSI_ADDRESS_HI);
  290. pci_addr <<= 32;
  291. pci_addr |= cdns_pcie_ep_fn_readl(pcie, fn, cap + PCI_MSI_ADDRESS_LO);
  292. pci_addr &= GENMASK_ULL(63, 2);
  293. /* Set the outbound region if needed. */
  294. if (unlikely(ep->irq_pci_addr != (pci_addr & ~pci_addr_mask) ||
  295. ep->irq_pci_fn != fn)) {
  296. /* Last region was reserved for IRQ writes. */
  297. cdns_pcie_set_outbound_region(pcie, fn, ep->max_regions - 1,
  298. false,
  299. ep->irq_phys_addr,
  300. pci_addr & ~pci_addr_mask,
  301. pci_addr_mask + 1);
  302. ep->irq_pci_addr = (pci_addr & ~pci_addr_mask);
  303. ep->irq_pci_fn = fn;
  304. }
  305. writew(data, ep->irq_cpu_addr + (pci_addr & pci_addr_mask));
  306. return 0;
  307. }
  308. static int cdns_pcie_ep_raise_irq(struct pci_epc *epc, u8 fn,
  309. enum pci_epc_irq_type type, u8 interrupt_num)
  310. {
  311. struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
  312. switch (type) {
  313. case PCI_EPC_IRQ_LEGACY:
  314. return cdns_pcie_ep_send_legacy_irq(ep, fn, 0);
  315. case PCI_EPC_IRQ_MSI:
  316. return cdns_pcie_ep_send_msi_irq(ep, fn, interrupt_num);
  317. default:
  318. break;
  319. }
  320. return -EINVAL;
  321. }
  322. static int cdns_pcie_ep_start(struct pci_epc *epc)
  323. {
  324. struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
  325. struct cdns_pcie *pcie = &ep->pcie;
  326. struct pci_epf *epf;
  327. u32 cfg;
  328. /*
  329. * BIT(0) is hardwired to 1, hence function 0 is always enabled
  330. * and can't be disabled anyway.
  331. */
  332. cfg = BIT(0);
  333. list_for_each_entry(epf, &epc->pci_epf, list)
  334. cfg |= BIT(epf->func_no);
  335. cdns_pcie_writel(pcie, CDNS_PCIE_LM_EP_FUNC_CFG, cfg);
  336. /*
  337. * The PCIe links are automatically established by the controller
  338. * once for all at powerup: the software can neither start nor stop
  339. * those links later at runtime.
  340. *
  341. * Then we only have to notify the EP core that our links are already
  342. * established. However we don't call directly pci_epc_linkup() because
  343. * we've already locked the epc->lock.
  344. */
  345. list_for_each_entry(epf, &epc->pci_epf, list)
  346. pci_epf_linkup(epf);
  347. return 0;
  348. }
  349. static const struct pci_epc_ops cdns_pcie_epc_ops = {
  350. .write_header = cdns_pcie_ep_write_header,
  351. .set_bar = cdns_pcie_ep_set_bar,
  352. .clear_bar = cdns_pcie_ep_clear_bar,
  353. .map_addr = cdns_pcie_ep_map_addr,
  354. .unmap_addr = cdns_pcie_ep_unmap_addr,
  355. .set_msi = cdns_pcie_ep_set_msi,
  356. .get_msi = cdns_pcie_ep_get_msi,
  357. .raise_irq = cdns_pcie_ep_raise_irq,
  358. .start = cdns_pcie_ep_start,
  359. };
  360. static const struct of_device_id cdns_pcie_ep_of_match[] = {
  361. { .compatible = "cdns,cdns-pcie-ep" },
  362. { },
  363. };
  364. static int cdns_pcie_ep_probe(struct platform_device *pdev)
  365. {
  366. struct device *dev = &pdev->dev;
  367. struct device_node *np = dev->of_node;
  368. struct cdns_pcie_ep *ep;
  369. struct cdns_pcie *pcie;
  370. struct pci_epc *epc;
  371. struct resource *res;
  372. int ret;
  373. ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL);
  374. if (!ep)
  375. return -ENOMEM;
  376. pcie = &ep->pcie;
  377. pcie->is_rc = false;
  378. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "reg");
  379. pcie->reg_base = devm_ioremap_resource(dev, res);
  380. if (IS_ERR(pcie->reg_base)) {
  381. dev_err(dev, "missing \"reg\"\n");
  382. return PTR_ERR(pcie->reg_base);
  383. }
  384. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem");
  385. if (!res) {
  386. dev_err(dev, "missing \"mem\"\n");
  387. return -EINVAL;
  388. }
  389. pcie->mem_res = res;
  390. ret = of_property_read_u32(np, "cdns,max-outbound-regions",
  391. &ep->max_regions);
  392. if (ret < 0) {
  393. dev_err(dev, "missing \"cdns,max-outbound-regions\"\n");
  394. return ret;
  395. }
  396. ep->ob_addr = devm_kzalloc(dev, ep->max_regions * sizeof(*ep->ob_addr),
  397. GFP_KERNEL);
  398. if (!ep->ob_addr)
  399. return -ENOMEM;
  400. pm_runtime_enable(dev);
  401. ret = pm_runtime_get_sync(dev);
  402. if (ret < 0) {
  403. dev_err(dev, "pm_runtime_get_sync() failed\n");
  404. goto err_get_sync;
  405. }
  406. /* Disable all but function 0 (anyway BIT(0) is hardwired to 1). */
  407. cdns_pcie_writel(pcie, CDNS_PCIE_LM_EP_FUNC_CFG, BIT(0));
  408. epc = devm_pci_epc_create(dev, &cdns_pcie_epc_ops);
  409. if (IS_ERR(epc)) {
  410. dev_err(dev, "failed to create epc device\n");
  411. ret = PTR_ERR(epc);
  412. goto err_init;
  413. }
  414. epc_set_drvdata(epc, ep);
  415. if (of_property_read_u8(np, "max-functions", &epc->max_functions) < 0)
  416. epc->max_functions = 1;
  417. ret = pci_epc_mem_init(epc, pcie->mem_res->start,
  418. resource_size(pcie->mem_res));
  419. if (ret < 0) {
  420. dev_err(dev, "failed to initialize the memory space\n");
  421. goto err_init;
  422. }
  423. ep->irq_cpu_addr = pci_epc_mem_alloc_addr(epc, &ep->irq_phys_addr,
  424. SZ_128K);
  425. if (!ep->irq_cpu_addr) {
  426. dev_err(dev, "failed to reserve memory space for MSI\n");
  427. ret = -ENOMEM;
  428. goto free_epc_mem;
  429. }
  430. ep->irq_pci_addr = CDNS_PCIE_EP_IRQ_PCI_ADDR_NONE;
  431. return 0;
  432. free_epc_mem:
  433. pci_epc_mem_exit(epc);
  434. err_init:
  435. pm_runtime_put_sync(dev);
  436. err_get_sync:
  437. pm_runtime_disable(dev);
  438. return ret;
  439. }
  440. static void cdns_pcie_ep_shutdown(struct platform_device *pdev)
  441. {
  442. struct device *dev = &pdev->dev;
  443. int ret;
  444. ret = pm_runtime_put_sync(dev);
  445. if (ret < 0)
  446. dev_dbg(dev, "pm_runtime_put_sync failed\n");
  447. pm_runtime_disable(dev);
  448. /* The PCIe controller can't be disabled. */
  449. }
  450. static struct platform_driver cdns_pcie_ep_driver = {
  451. .driver = {
  452. .name = "cdns-pcie-ep",
  453. .of_match_table = cdns_pcie_ep_of_match,
  454. },
  455. .probe = cdns_pcie_ep_probe,
  456. .shutdown = cdns_pcie_ep_shutdown,
  457. };
  458. builtin_platform_driver(cdns_pcie_ep_driver);