pcie-xilinx-dma-pl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * PCIe host controller driver for Xilinx XDMA PCIe Bridge
  4. *
  5. * Copyright (C) 2023 Xilinx, Inc. All rights reserved.
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/irq.h>
  10. #include <linux/irqchip/irq-msi-lib.h>
  11. #include <linux/irqdomain.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/msi.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_pci.h>
  17. #include "../pci.h"
  18. #include "pcie-xilinx-common.h"
  19. /* Register definitions */
  20. #define XILINX_PCIE_DMA_REG_IDR 0x00000138
  21. #define XILINX_PCIE_DMA_REG_IMR 0x0000013c
  22. #define XILINX_PCIE_DMA_REG_PSCR 0x00000144
  23. #define XILINX_PCIE_DMA_REG_RPSC 0x00000148
  24. #define XILINX_PCIE_DMA_REG_MSIBASE1 0x0000014c
  25. #define XILINX_PCIE_DMA_REG_MSIBASE2 0x00000150
  26. #define XILINX_PCIE_DMA_REG_RPEFR 0x00000154
  27. #define XILINX_PCIE_DMA_REG_IDRN 0x00000160
  28. #define XILINX_PCIE_DMA_REG_IDRN_MASK 0x00000164
  29. #define XILINX_PCIE_DMA_REG_MSI_LOW 0x00000170
  30. #define XILINX_PCIE_DMA_REG_MSI_HI 0x00000174
  31. #define XILINX_PCIE_DMA_REG_MSI_LOW_MASK 0x00000178
  32. #define XILINX_PCIE_DMA_REG_MSI_HI_MASK 0x0000017c
  33. #define IMR(x) BIT(XILINX_PCIE_INTR_ ##x)
  34. #define XILINX_PCIE_INTR_IMR_ALL_MASK \
  35. ( \
  36. IMR(LINK_DOWN) | \
  37. IMR(HOT_RESET) | \
  38. IMR(CFG_TIMEOUT) | \
  39. IMR(CORRECTABLE) | \
  40. IMR(NONFATAL) | \
  41. IMR(FATAL) | \
  42. IMR(INTX) | \
  43. IMR(MSI) | \
  44. IMR(SLV_UNSUPP) | \
  45. IMR(SLV_UNEXP) | \
  46. IMR(SLV_COMPL) | \
  47. IMR(SLV_ERRP) | \
  48. IMR(SLV_CMPABT) | \
  49. IMR(SLV_ILLBUR) | \
  50. IMR(MST_DECERR) | \
  51. IMR(MST_SLVERR) | \
  52. )
  53. #define XILINX_PCIE_DMA_IMR_ALL_MASK 0x0ff30fe9
  54. #define XILINX_PCIE_DMA_IDR_ALL_MASK 0xffffffff
  55. #define XILINX_PCIE_DMA_IDRN_MASK GENMASK(19, 16)
  56. /* Root Port Error Register definitions */
  57. #define XILINX_PCIE_DMA_RPEFR_ERR_VALID BIT(18)
  58. #define XILINX_PCIE_DMA_RPEFR_REQ_ID GENMASK(15, 0)
  59. #define XILINX_PCIE_DMA_RPEFR_ALL_MASK 0xffffffff
  60. /* Root Port Interrupt Register definitions */
  61. #define XILINX_PCIE_DMA_IDRN_SHIFT 16
  62. /* Root Port Status/control Register definitions */
  63. #define XILINX_PCIE_DMA_REG_RPSC_BEN BIT(0)
  64. /* Phy Status/Control Register definitions */
  65. #define XILINX_PCIE_DMA_REG_PSCR_LNKUP BIT(11)
  66. #define QDMA_BRIDGE_BASE_OFF 0xcd8
  67. /* Number of MSI IRQs */
  68. #define XILINX_NUM_MSI_IRQS 64
  69. enum xilinx_pl_dma_version {
  70. XDMA,
  71. QDMA,
  72. };
  73. /**
  74. * struct xilinx_pl_dma_variant - PL DMA PCIe variant information
  75. * @version: DMA version
  76. */
  77. struct xilinx_pl_dma_variant {
  78. enum xilinx_pl_dma_version version;
  79. };
  80. struct xilinx_msi {
  81. unsigned long *bitmap;
  82. struct irq_domain *dev_domain;
  83. struct mutex lock; /* Protect bitmap variable */
  84. int irq_msi0;
  85. int irq_msi1;
  86. };
  87. /**
  88. * struct pl_dma_pcie - PCIe port information
  89. * @dev: Device pointer
  90. * @reg_base: IO Mapped Register Base
  91. * @cfg_base: IO Mapped Configuration Base
  92. * @irq: Interrupt number
  93. * @cfg: Holds mappings of config space window
  94. * @phys_reg_base: Physical address of reg base
  95. * @intx_domain: Legacy IRQ domain pointer
  96. * @pldma_domain: PL DMA IRQ domain pointer
  97. * @resources: Bus Resources
  98. * @msi: MSI information
  99. * @intx_irq: INTx error interrupt number
  100. * @lock: Lock protecting shared register access
  101. * @variant: PL DMA PCIe version check pointer
  102. */
  103. struct pl_dma_pcie {
  104. struct device *dev;
  105. void __iomem *reg_base;
  106. void __iomem *cfg_base;
  107. int irq;
  108. struct pci_config_window *cfg;
  109. phys_addr_t phys_reg_base;
  110. struct irq_domain *intx_domain;
  111. struct irq_domain *pldma_domain;
  112. struct list_head resources;
  113. struct xilinx_msi msi;
  114. int intx_irq;
  115. raw_spinlock_t lock;
  116. const struct xilinx_pl_dma_variant *variant;
  117. };
  118. static inline u32 pcie_read(struct pl_dma_pcie *port, u32 reg)
  119. {
  120. if (port->variant->version == QDMA)
  121. return readl(port->reg_base + reg + QDMA_BRIDGE_BASE_OFF);
  122. return readl(port->reg_base + reg);
  123. }
  124. static inline void pcie_write(struct pl_dma_pcie *port, u32 val, u32 reg)
  125. {
  126. if (port->variant->version == QDMA)
  127. writel(val, port->reg_base + reg + QDMA_BRIDGE_BASE_OFF);
  128. else
  129. writel(val, port->reg_base + reg);
  130. }
  131. static inline bool xilinx_pl_dma_pcie_link_up(struct pl_dma_pcie *port)
  132. {
  133. return (pcie_read(port, XILINX_PCIE_DMA_REG_PSCR) &
  134. XILINX_PCIE_DMA_REG_PSCR_LNKUP) ? true : false;
  135. }
  136. static void xilinx_pl_dma_pcie_clear_err_interrupts(struct pl_dma_pcie *port)
  137. {
  138. unsigned long val = pcie_read(port, XILINX_PCIE_DMA_REG_RPEFR);
  139. if (val & XILINX_PCIE_DMA_RPEFR_ERR_VALID) {
  140. dev_dbg(port->dev, "Requester ID %lu\n",
  141. val & XILINX_PCIE_DMA_RPEFR_REQ_ID);
  142. pcie_write(port, XILINX_PCIE_DMA_RPEFR_ALL_MASK,
  143. XILINX_PCIE_DMA_REG_RPEFR);
  144. }
  145. }
  146. static bool xilinx_pl_dma_pcie_valid_device(struct pci_bus *bus,
  147. unsigned int devfn)
  148. {
  149. struct pl_dma_pcie *port = bus->sysdata;
  150. if (!pci_is_root_bus(bus)) {
  151. /*
  152. * Checking whether the link is up is the last line of
  153. * defense, and this check is inherently racy by definition.
  154. * Sending a PIO request to a downstream device when the link is
  155. * down causes an unrecoverable error, and a reset of the entire
  156. * PCIe controller will be needed. We can reduce the likelihood
  157. * of that unrecoverable error by checking whether the link is
  158. * up, but we can't completely prevent it because the link may
  159. * go down between the link-up check and the PIO request.
  160. */
  161. if (!xilinx_pl_dma_pcie_link_up(port))
  162. return false;
  163. } else if (devfn > 0)
  164. /* Only one device down on each root port */
  165. return false;
  166. return true;
  167. }
  168. static void __iomem *xilinx_pl_dma_pcie_map_bus(struct pci_bus *bus,
  169. unsigned int devfn, int where)
  170. {
  171. struct pl_dma_pcie *port = bus->sysdata;
  172. if (!xilinx_pl_dma_pcie_valid_device(bus, devfn))
  173. return NULL;
  174. if (port->variant->version == QDMA)
  175. return port->cfg_base + PCIE_ECAM_OFFSET(bus->number, devfn, where);
  176. return port->reg_base + PCIE_ECAM_OFFSET(bus->number, devfn, where);
  177. }
  178. /* PCIe operations */
  179. static struct pci_ecam_ops xilinx_pl_dma_pcie_ops = {
  180. .pci_ops = {
  181. .map_bus = xilinx_pl_dma_pcie_map_bus,
  182. .read = pci_generic_config_read,
  183. .write = pci_generic_config_write,
  184. }
  185. };
  186. static void xilinx_pl_dma_pcie_enable_msi(struct pl_dma_pcie *port)
  187. {
  188. phys_addr_t msi_addr = port->phys_reg_base;
  189. pcie_write(port, upper_32_bits(msi_addr), XILINX_PCIE_DMA_REG_MSIBASE1);
  190. pcie_write(port, lower_32_bits(msi_addr), XILINX_PCIE_DMA_REG_MSIBASE2);
  191. }
  192. static void xilinx_mask_intx_irq(struct irq_data *data)
  193. {
  194. struct pl_dma_pcie *port = irq_data_get_irq_chip_data(data);
  195. unsigned long flags;
  196. u32 mask, val;
  197. mask = BIT(data->hwirq + XILINX_PCIE_DMA_IDRN_SHIFT);
  198. raw_spin_lock_irqsave(&port->lock, flags);
  199. val = pcie_read(port, XILINX_PCIE_DMA_REG_IDRN_MASK);
  200. pcie_write(port, (val & (~mask)), XILINX_PCIE_DMA_REG_IDRN_MASK);
  201. raw_spin_unlock_irqrestore(&port->lock, flags);
  202. }
  203. static void xilinx_unmask_intx_irq(struct irq_data *data)
  204. {
  205. struct pl_dma_pcie *port = irq_data_get_irq_chip_data(data);
  206. unsigned long flags;
  207. u32 mask, val;
  208. mask = BIT(data->hwirq + XILINX_PCIE_DMA_IDRN_SHIFT);
  209. raw_spin_lock_irqsave(&port->lock, flags);
  210. val = pcie_read(port, XILINX_PCIE_DMA_REG_IDRN_MASK);
  211. pcie_write(port, (val | mask), XILINX_PCIE_DMA_REG_IDRN_MASK);
  212. raw_spin_unlock_irqrestore(&port->lock, flags);
  213. }
  214. static struct irq_chip xilinx_leg_irq_chip = {
  215. .name = "pl_dma:INTx",
  216. .irq_mask = xilinx_mask_intx_irq,
  217. .irq_unmask = xilinx_unmask_intx_irq,
  218. };
  219. static int xilinx_pl_dma_pcie_intx_map(struct irq_domain *domain,
  220. unsigned int irq, irq_hw_number_t hwirq)
  221. {
  222. irq_set_chip_and_handler(irq, &xilinx_leg_irq_chip, handle_level_irq);
  223. irq_set_chip_data(irq, domain->host_data);
  224. irq_set_status_flags(irq, IRQ_LEVEL);
  225. return 0;
  226. }
  227. /* INTx IRQ Domain operations */
  228. static const struct irq_domain_ops intx_domain_ops = {
  229. .map = xilinx_pl_dma_pcie_intx_map,
  230. };
  231. static irqreturn_t xilinx_pl_dma_pcie_msi_handler_high(int irq, void *args)
  232. {
  233. struct xilinx_msi *msi;
  234. unsigned long status;
  235. u32 bit, virq;
  236. struct pl_dma_pcie *port = args;
  237. msi = &port->msi;
  238. while ((status = pcie_read(port, XILINX_PCIE_DMA_REG_MSI_HI)) != 0) {
  239. for_each_set_bit(bit, &status, 32) {
  240. pcie_write(port, 1 << bit, XILINX_PCIE_DMA_REG_MSI_HI);
  241. bit = bit + 32;
  242. virq = irq_find_mapping(msi->dev_domain, bit);
  243. if (virq)
  244. generic_handle_irq(virq);
  245. }
  246. }
  247. return IRQ_HANDLED;
  248. }
  249. static irqreturn_t xilinx_pl_dma_pcie_msi_handler_low(int irq, void *args)
  250. {
  251. struct pl_dma_pcie *port = args;
  252. struct xilinx_msi *msi;
  253. unsigned long status;
  254. u32 bit, virq;
  255. msi = &port->msi;
  256. while ((status = pcie_read(port, XILINX_PCIE_DMA_REG_MSI_LOW)) != 0) {
  257. for_each_set_bit(bit, &status, 32) {
  258. pcie_write(port, 1 << bit, XILINX_PCIE_DMA_REG_MSI_LOW);
  259. virq = irq_find_mapping(msi->dev_domain, bit);
  260. if (virq)
  261. generic_handle_irq(virq);
  262. }
  263. }
  264. return IRQ_HANDLED;
  265. }
  266. static irqreturn_t xilinx_pl_dma_pcie_event_flow(int irq, void *args)
  267. {
  268. struct pl_dma_pcie *port = args;
  269. unsigned long val;
  270. int i;
  271. val = pcie_read(port, XILINX_PCIE_DMA_REG_IDR);
  272. val &= pcie_read(port, XILINX_PCIE_DMA_REG_IMR);
  273. for_each_set_bit(i, &val, 32)
  274. generic_handle_domain_irq(port->pldma_domain, i);
  275. pcie_write(port, val, XILINX_PCIE_DMA_REG_IDR);
  276. return IRQ_HANDLED;
  277. }
  278. #define _IC(x, s) \
  279. [XILINX_PCIE_INTR_ ## x] = { __stringify(x), s }
  280. static const struct {
  281. const char *sym;
  282. const char *str;
  283. } intr_cause[32] = {
  284. _IC(LINK_DOWN, "Link Down"),
  285. _IC(HOT_RESET, "Hot reset"),
  286. _IC(CFG_TIMEOUT, "ECAM access timeout"),
  287. _IC(CORRECTABLE, "Correctable error message"),
  288. _IC(NONFATAL, "Non fatal error message"),
  289. _IC(FATAL, "Fatal error message"),
  290. _IC(SLV_UNSUPP, "Slave unsupported request"),
  291. _IC(SLV_UNEXP, "Slave unexpected completion"),
  292. _IC(SLV_COMPL, "Slave completion timeout"),
  293. _IC(SLV_ERRP, "Slave Error Poison"),
  294. _IC(SLV_CMPABT, "Slave Completer Abort"),
  295. _IC(SLV_ILLBUR, "Slave Illegal Burst"),
  296. _IC(MST_DECERR, "Master decode error"),
  297. _IC(MST_SLVERR, "Master slave error"),
  298. };
  299. static irqreturn_t xilinx_pl_dma_pcie_intr_handler(int irq, void *dev_id)
  300. {
  301. struct pl_dma_pcie *port = (struct pl_dma_pcie *)dev_id;
  302. struct device *dev = port->dev;
  303. struct irq_data *d;
  304. d = irq_domain_get_irq_data(port->pldma_domain, irq);
  305. switch (d->hwirq) {
  306. case XILINX_PCIE_INTR_CORRECTABLE:
  307. case XILINX_PCIE_INTR_NONFATAL:
  308. case XILINX_PCIE_INTR_FATAL:
  309. xilinx_pl_dma_pcie_clear_err_interrupts(port);
  310. fallthrough;
  311. default:
  312. if (intr_cause[d->hwirq].str)
  313. dev_warn(dev, "%s\n", intr_cause[d->hwirq].str);
  314. else
  315. dev_warn(dev, "Unknown IRQ %ld\n", d->hwirq);
  316. }
  317. return IRQ_HANDLED;
  318. }
  319. #define XILINX_MSI_FLAGS_REQUIRED (MSI_FLAG_USE_DEF_DOM_OPS | \
  320. MSI_FLAG_USE_DEF_CHIP_OPS | \
  321. MSI_FLAG_NO_AFFINITY)
  322. #define XILINX_MSI_FLAGS_SUPPORTED (MSI_GENERIC_FLAGS_MASK | \
  323. MSI_FLAG_MULTI_PCI_MSI)
  324. static const struct msi_parent_ops xilinx_msi_parent_ops = {
  325. .required_flags = XILINX_MSI_FLAGS_REQUIRED,
  326. .supported_flags = XILINX_MSI_FLAGS_SUPPORTED,
  327. .bus_select_token = DOMAIN_BUS_PCI_MSI,
  328. .prefix = "pl_dma-",
  329. .init_dev_msi_info = msi_lib_init_dev_msi_info,
  330. };
  331. static void xilinx_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
  332. {
  333. struct pl_dma_pcie *pcie = irq_data_get_irq_chip_data(data);
  334. phys_addr_t msi_addr = pcie->phys_reg_base;
  335. msg->address_lo = lower_32_bits(msi_addr);
  336. msg->address_hi = upper_32_bits(msi_addr);
  337. msg->data = data->hwirq;
  338. }
  339. static struct irq_chip xilinx_irq_chip = {
  340. .name = "pl_dma:MSI",
  341. .irq_compose_msi_msg = xilinx_compose_msi_msg,
  342. };
  343. static int xilinx_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  344. unsigned int nr_irqs, void *args)
  345. {
  346. struct pl_dma_pcie *pcie = domain->host_data;
  347. struct xilinx_msi *msi = &pcie->msi;
  348. int bit, i;
  349. mutex_lock(&msi->lock);
  350. bit = bitmap_find_free_region(msi->bitmap, XILINX_NUM_MSI_IRQS,
  351. get_count_order(nr_irqs));
  352. if (bit < 0) {
  353. mutex_unlock(&msi->lock);
  354. return -ENOSPC;
  355. }
  356. for (i = 0; i < nr_irqs; i++) {
  357. irq_domain_set_info(domain, virq + i, bit + i, &xilinx_irq_chip,
  358. domain->host_data, handle_simple_irq,
  359. NULL, NULL);
  360. }
  361. mutex_unlock(&msi->lock);
  362. return 0;
  363. }
  364. static void xilinx_irq_domain_free(struct irq_domain *domain, unsigned int virq,
  365. unsigned int nr_irqs)
  366. {
  367. struct irq_data *data = irq_domain_get_irq_data(domain, virq);
  368. struct pl_dma_pcie *pcie = irq_data_get_irq_chip_data(data);
  369. struct xilinx_msi *msi = &pcie->msi;
  370. mutex_lock(&msi->lock);
  371. bitmap_release_region(msi->bitmap, data->hwirq,
  372. get_count_order(nr_irqs));
  373. mutex_unlock(&msi->lock);
  374. }
  375. static const struct irq_domain_ops dev_msi_domain_ops = {
  376. .alloc = xilinx_irq_domain_alloc,
  377. .free = xilinx_irq_domain_free,
  378. };
  379. static void xilinx_pl_dma_pcie_free_irq_domains(struct pl_dma_pcie *port)
  380. {
  381. struct xilinx_msi *msi = &port->msi;
  382. if (port->intx_domain) {
  383. irq_domain_remove(port->intx_domain);
  384. port->intx_domain = NULL;
  385. }
  386. if (msi->dev_domain) {
  387. irq_domain_remove(msi->dev_domain);
  388. msi->dev_domain = NULL;
  389. }
  390. }
  391. static int xilinx_pl_dma_pcie_init_msi_irq_domain(struct pl_dma_pcie *port)
  392. {
  393. struct device *dev = port->dev;
  394. struct xilinx_msi *msi = &port->msi;
  395. int size = BITS_TO_LONGS(XILINX_NUM_MSI_IRQS) * sizeof(long);
  396. struct irq_domain_info info = {
  397. .fwnode = dev_fwnode(port->dev),
  398. .ops = &dev_msi_domain_ops,
  399. .host_data = port,
  400. .size = XILINX_NUM_MSI_IRQS,
  401. };
  402. msi->dev_domain = msi_create_parent_irq_domain(&info, &xilinx_msi_parent_ops);
  403. if (!msi->dev_domain)
  404. goto out;
  405. mutex_init(&msi->lock);
  406. msi->bitmap = kzalloc(size, GFP_KERNEL);
  407. if (!msi->bitmap)
  408. goto out;
  409. raw_spin_lock_init(&port->lock);
  410. xilinx_pl_dma_pcie_enable_msi(port);
  411. return 0;
  412. out:
  413. xilinx_pl_dma_pcie_free_irq_domains(port);
  414. dev_err(dev, "Failed to allocate MSI IRQ domains\n");
  415. return -ENOMEM;
  416. }
  417. /*
  418. * INTx error interrupts are Xilinx controller specific interrupt, used to
  419. * notify user about errors such as cfg timeout, slave unsupported requests,
  420. * fatal and non fatal error etc.
  421. */
  422. static irqreturn_t xilinx_pl_dma_pcie_intx_flow(int irq, void *args)
  423. {
  424. unsigned long val;
  425. int i;
  426. struct pl_dma_pcie *port = args;
  427. val = FIELD_GET(XILINX_PCIE_DMA_IDRN_MASK,
  428. pcie_read(port, XILINX_PCIE_DMA_REG_IDRN));
  429. for_each_set_bit(i, &val, PCI_NUM_INTX)
  430. generic_handle_domain_irq(port->intx_domain, i);
  431. return IRQ_HANDLED;
  432. }
  433. static void xilinx_pl_dma_pcie_mask_event_irq(struct irq_data *d)
  434. {
  435. struct pl_dma_pcie *port = irq_data_get_irq_chip_data(d);
  436. u32 val;
  437. raw_spin_lock(&port->lock);
  438. val = pcie_read(port, XILINX_PCIE_DMA_REG_IMR);
  439. val &= ~BIT(d->hwirq);
  440. pcie_write(port, val, XILINX_PCIE_DMA_REG_IMR);
  441. raw_spin_unlock(&port->lock);
  442. }
  443. static void xilinx_pl_dma_pcie_unmask_event_irq(struct irq_data *d)
  444. {
  445. struct pl_dma_pcie *port = irq_data_get_irq_chip_data(d);
  446. u32 val;
  447. raw_spin_lock(&port->lock);
  448. val = pcie_read(port, XILINX_PCIE_DMA_REG_IMR);
  449. val |= BIT(d->hwirq);
  450. pcie_write(port, val, XILINX_PCIE_DMA_REG_IMR);
  451. raw_spin_unlock(&port->lock);
  452. }
  453. static struct irq_chip xilinx_pl_dma_pcie_event_irq_chip = {
  454. .name = "pl_dma:RC-Event",
  455. .irq_mask = xilinx_pl_dma_pcie_mask_event_irq,
  456. .irq_unmask = xilinx_pl_dma_pcie_unmask_event_irq,
  457. };
  458. static int xilinx_pl_dma_pcie_event_map(struct irq_domain *domain,
  459. unsigned int irq, irq_hw_number_t hwirq)
  460. {
  461. irq_set_chip_and_handler(irq, &xilinx_pl_dma_pcie_event_irq_chip,
  462. handle_level_irq);
  463. irq_set_chip_data(irq, domain->host_data);
  464. irq_set_status_flags(irq, IRQ_LEVEL);
  465. return 0;
  466. }
  467. static const struct irq_domain_ops event_domain_ops = {
  468. .map = xilinx_pl_dma_pcie_event_map,
  469. };
  470. /**
  471. * xilinx_pl_dma_pcie_init_irq_domain - Initialize IRQ domain
  472. * @port: PCIe port information
  473. *
  474. * Return: '0' on success and error value on failure.
  475. */
  476. static int xilinx_pl_dma_pcie_init_irq_domain(struct pl_dma_pcie *port)
  477. {
  478. struct device *dev = port->dev;
  479. struct device_node *node = dev->of_node;
  480. struct device_node *pcie_intc_node;
  481. int ret;
  482. /* Setup INTx */
  483. pcie_intc_node = of_get_child_by_name(node, "interrupt-controller");
  484. if (!pcie_intc_node) {
  485. dev_err(dev, "No PCIe Intc node found\n");
  486. return -EINVAL;
  487. }
  488. port->pldma_domain = irq_domain_create_linear(of_fwnode_handle(pcie_intc_node), 32,
  489. &event_domain_ops, port);
  490. if (!port->pldma_domain)
  491. return -ENOMEM;
  492. irq_domain_update_bus_token(port->pldma_domain, DOMAIN_BUS_NEXUS);
  493. port->intx_domain = irq_domain_create_linear(of_fwnode_handle(pcie_intc_node), PCI_NUM_INTX,
  494. &intx_domain_ops, port);
  495. if (!port->intx_domain) {
  496. dev_err(dev, "Failed to get a INTx IRQ domain\n");
  497. return -ENOMEM;
  498. }
  499. irq_domain_update_bus_token(port->intx_domain, DOMAIN_BUS_WIRED);
  500. ret = xilinx_pl_dma_pcie_init_msi_irq_domain(port);
  501. if (ret != 0) {
  502. irq_domain_remove(port->intx_domain);
  503. return -ENOMEM;
  504. }
  505. of_node_put(pcie_intc_node);
  506. raw_spin_lock_init(&port->lock);
  507. return 0;
  508. }
  509. static int xilinx_pl_dma_pcie_setup_irq(struct pl_dma_pcie *port)
  510. {
  511. struct device *dev = port->dev;
  512. struct platform_device *pdev = to_platform_device(dev);
  513. int i, irq, err;
  514. port->irq = platform_get_irq(pdev, 0);
  515. if (port->irq < 0)
  516. return port->irq;
  517. for (i = 0; i < ARRAY_SIZE(intr_cause); i++) {
  518. int err;
  519. if (!intr_cause[i].str)
  520. continue;
  521. irq = irq_create_mapping(port->pldma_domain, i);
  522. if (!irq) {
  523. dev_err(dev, "Failed to map interrupt\n");
  524. return -ENXIO;
  525. }
  526. err = devm_request_irq(dev, irq,
  527. xilinx_pl_dma_pcie_intr_handler,
  528. IRQF_SHARED | IRQF_NO_THREAD,
  529. intr_cause[i].sym, port);
  530. if (err) {
  531. dev_err(dev, "Failed to request IRQ %d\n", irq);
  532. return err;
  533. }
  534. }
  535. port->intx_irq = irq_create_mapping(port->pldma_domain,
  536. XILINX_PCIE_INTR_INTX);
  537. if (!port->intx_irq) {
  538. dev_err(dev, "Failed to map INTx interrupt\n");
  539. return -ENXIO;
  540. }
  541. err = devm_request_irq(dev, port->intx_irq, xilinx_pl_dma_pcie_intx_flow,
  542. IRQF_SHARED | IRQF_NO_THREAD, NULL, port);
  543. if (err) {
  544. dev_err(dev, "Failed to request INTx IRQ %d\n", port->intx_irq);
  545. return err;
  546. }
  547. err = devm_request_irq(dev, port->irq, xilinx_pl_dma_pcie_event_flow,
  548. IRQF_SHARED | IRQF_NO_THREAD, NULL, port);
  549. if (err) {
  550. dev_err(dev, "Failed to request event IRQ %d\n", port->irq);
  551. return err;
  552. }
  553. return 0;
  554. }
  555. static void xilinx_pl_dma_pcie_init_port(struct pl_dma_pcie *port)
  556. {
  557. if (xilinx_pl_dma_pcie_link_up(port))
  558. dev_info(port->dev, "PCIe Link is UP\n");
  559. else
  560. dev_info(port->dev, "PCIe Link is DOWN\n");
  561. /* Disable all interrupts */
  562. pcie_write(port, ~XILINX_PCIE_DMA_IDR_ALL_MASK,
  563. XILINX_PCIE_DMA_REG_IMR);
  564. /* Clear pending interrupts */
  565. pcie_write(port, pcie_read(port, XILINX_PCIE_DMA_REG_IDR) &
  566. XILINX_PCIE_DMA_IMR_ALL_MASK,
  567. XILINX_PCIE_DMA_REG_IDR);
  568. /* Needed for MSI DECODE MODE */
  569. pcie_write(port, XILINX_PCIE_DMA_IDR_ALL_MASK,
  570. XILINX_PCIE_DMA_REG_MSI_LOW_MASK);
  571. pcie_write(port, XILINX_PCIE_DMA_IDR_ALL_MASK,
  572. XILINX_PCIE_DMA_REG_MSI_HI_MASK);
  573. /* Set the Bridge enable bit */
  574. pcie_write(port, pcie_read(port, XILINX_PCIE_DMA_REG_RPSC) |
  575. XILINX_PCIE_DMA_REG_RPSC_BEN,
  576. XILINX_PCIE_DMA_REG_RPSC);
  577. }
  578. static int xilinx_request_msi_irq(struct pl_dma_pcie *port)
  579. {
  580. struct device *dev = port->dev;
  581. struct platform_device *pdev = to_platform_device(dev);
  582. int ret;
  583. port->msi.irq_msi0 = platform_get_irq_byname(pdev, "msi0");
  584. if (port->msi.irq_msi0 <= 0)
  585. return port->msi.irq_msi0;
  586. ret = devm_request_irq(dev, port->msi.irq_msi0, xilinx_pl_dma_pcie_msi_handler_low,
  587. IRQF_SHARED | IRQF_NO_THREAD, "xlnx-pcie-dma-pl",
  588. port);
  589. if (ret) {
  590. dev_err(dev, "Failed to register interrupt\n");
  591. return ret;
  592. }
  593. port->msi.irq_msi1 = platform_get_irq_byname(pdev, "msi1");
  594. if (port->msi.irq_msi1 <= 0)
  595. return port->msi.irq_msi1;
  596. ret = devm_request_irq(dev, port->msi.irq_msi1, xilinx_pl_dma_pcie_msi_handler_high,
  597. IRQF_SHARED | IRQF_NO_THREAD, "xlnx-pcie-dma-pl",
  598. port);
  599. if (ret) {
  600. dev_err(dev, "Failed to register interrupt\n");
  601. return ret;
  602. }
  603. return 0;
  604. }
  605. static int xilinx_pl_dma_pcie_parse_dt(struct pl_dma_pcie *port,
  606. struct resource *bus_range)
  607. {
  608. struct device *dev = port->dev;
  609. struct platform_device *pdev = to_platform_device(dev);
  610. struct resource *res;
  611. int err;
  612. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  613. if (!res) {
  614. dev_err(dev, "Missing \"reg\" property\n");
  615. return -ENXIO;
  616. }
  617. port->phys_reg_base = res->start;
  618. port->cfg = pci_ecam_create(dev, res, bus_range, &xilinx_pl_dma_pcie_ops);
  619. if (IS_ERR(port->cfg))
  620. return PTR_ERR(port->cfg);
  621. port->reg_base = port->cfg->win;
  622. if (port->variant->version == QDMA) {
  623. port->cfg_base = port->cfg->win;
  624. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "breg");
  625. port->reg_base = devm_ioremap_resource(dev, res);
  626. if (IS_ERR(port->reg_base))
  627. return PTR_ERR(port->reg_base);
  628. port->phys_reg_base = res->start;
  629. }
  630. err = xilinx_request_msi_irq(port);
  631. if (err) {
  632. pci_ecam_free(port->cfg);
  633. return err;
  634. }
  635. return 0;
  636. }
  637. static int xilinx_pl_dma_pcie_probe(struct platform_device *pdev)
  638. {
  639. struct device *dev = &pdev->dev;
  640. struct pl_dma_pcie *port;
  641. struct pci_host_bridge *bridge;
  642. struct resource_entry *bus;
  643. int err;
  644. bridge = devm_pci_alloc_host_bridge(dev, sizeof(*port));
  645. if (!bridge)
  646. return -ENODEV;
  647. port = pci_host_bridge_priv(bridge);
  648. port->dev = dev;
  649. bus = resource_list_first_type(&bridge->windows, IORESOURCE_BUS);
  650. if (!bus)
  651. return -ENODEV;
  652. port->variant = of_device_get_match_data(dev);
  653. err = xilinx_pl_dma_pcie_parse_dt(port, bus->res);
  654. if (err) {
  655. dev_err(dev, "Parsing DT failed\n");
  656. return err;
  657. }
  658. xilinx_pl_dma_pcie_init_port(port);
  659. err = xilinx_pl_dma_pcie_init_irq_domain(port);
  660. if (err)
  661. goto err_irq_domain;
  662. err = xilinx_pl_dma_pcie_setup_irq(port);
  663. bridge->sysdata = port;
  664. bridge->ops = &xilinx_pl_dma_pcie_ops.pci_ops;
  665. err = pci_host_probe(bridge);
  666. if (err < 0)
  667. goto err_host_bridge;
  668. return 0;
  669. err_host_bridge:
  670. xilinx_pl_dma_pcie_free_irq_domains(port);
  671. err_irq_domain:
  672. pci_ecam_free(port->cfg);
  673. return err;
  674. }
  675. static const struct xilinx_pl_dma_variant xdma_host = {
  676. .version = XDMA,
  677. };
  678. static const struct xilinx_pl_dma_variant qdma_host = {
  679. .version = QDMA,
  680. };
  681. static const struct of_device_id xilinx_pl_dma_pcie_of_match[] = {
  682. {
  683. .compatible = "xlnx,xdma-host-3.00",
  684. .data = &xdma_host,
  685. },
  686. {
  687. .compatible = "xlnx,qdma-host-3.00",
  688. .data = &qdma_host,
  689. },
  690. {}
  691. };
  692. static struct platform_driver xilinx_pl_dma_pcie_driver = {
  693. .driver = {
  694. .name = "xilinx-xdma-pcie",
  695. .of_match_table = xilinx_pl_dma_pcie_of_match,
  696. .suppress_bind_attrs = true,
  697. },
  698. .probe = xilinx_pl_dma_pcie_probe,
  699. };
  700. builtin_platform_driver(xilinx_pl_dma_pcie_driver);