pcie-iproc-msi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2015 Broadcom Corporation
  4. */
  5. #include <linux/interrupt.h>
  6. #include <linux/irqchip/chained_irq.h>
  7. #include <linux/irqchip/irq-msi-lib.h>
  8. #include <linux/irqdomain.h>
  9. #include <linux/msi.h>
  10. #include <linux/of_irq.h>
  11. #include <linux/of_pci.h>
  12. #include <linux/pci.h>
  13. #include "pcie-iproc.h"
  14. #define IPROC_MSI_INTR_EN_SHIFT 11
  15. #define IPROC_MSI_INTR_EN BIT(IPROC_MSI_INTR_EN_SHIFT)
  16. #define IPROC_MSI_INT_N_EVENT_SHIFT 1
  17. #define IPROC_MSI_INT_N_EVENT BIT(IPROC_MSI_INT_N_EVENT_SHIFT)
  18. #define IPROC_MSI_EQ_EN_SHIFT 0
  19. #define IPROC_MSI_EQ_EN BIT(IPROC_MSI_EQ_EN_SHIFT)
  20. #define IPROC_MSI_EQ_MASK 0x3f
  21. /* Max number of GIC interrupts */
  22. #define NR_HW_IRQS 6
  23. /* Number of entries in each event queue */
  24. #define EQ_LEN 64
  25. /* Size of each event queue memory region */
  26. #define EQ_MEM_REGION_SIZE SZ_4K
  27. /* Size of each MSI address region */
  28. #define MSI_MEM_REGION_SIZE SZ_4K
  29. enum iproc_msi_reg {
  30. IPROC_MSI_EQ_PAGE = 0,
  31. IPROC_MSI_EQ_PAGE_UPPER,
  32. IPROC_MSI_PAGE,
  33. IPROC_MSI_PAGE_UPPER,
  34. IPROC_MSI_CTRL,
  35. IPROC_MSI_EQ_HEAD,
  36. IPROC_MSI_EQ_TAIL,
  37. IPROC_MSI_INTS_EN,
  38. IPROC_MSI_REG_SIZE,
  39. };
  40. struct iproc_msi;
  41. /**
  42. * struct iproc_msi_grp - iProc MSI group
  43. *
  44. * One MSI group is allocated per GIC interrupt, serviced by one iProc MSI
  45. * event queue.
  46. *
  47. * @msi: pointer to iProc MSI data
  48. * @gic_irq: GIC interrupt
  49. * @eq: Event queue number
  50. */
  51. struct iproc_msi_grp {
  52. struct iproc_msi *msi;
  53. int gic_irq;
  54. unsigned int eq;
  55. };
  56. /**
  57. * struct iproc_msi - iProc event queue based MSI
  58. *
  59. * Only meant to be used on platforms without MSI support integrated into the
  60. * GIC.
  61. *
  62. * @pcie: pointer to iProc PCIe data
  63. * @reg_offsets: MSI register offsets
  64. * @grps: MSI groups
  65. * @nr_irqs: number of total interrupts connected to GIC
  66. * @nr_cpus: number of toal CPUs
  67. * @has_inten_reg: indicates the MSI interrupt enable register needs to be
  68. * set explicitly (required for some legacy platforms)
  69. * @bitmap: MSI vector bitmap
  70. * @bitmap_lock: lock to protect access to the MSI bitmap
  71. * @nr_msi_vecs: total number of MSI vectors
  72. * @inner_domain: inner IRQ domain
  73. * @nr_eq_region: required number of 4K aligned memory region for MSI event
  74. * queues
  75. * @nr_msi_region: required number of 4K aligned address region for MSI posted
  76. * writes
  77. * @eq_cpu: pointer to allocated memory region for MSI event queues
  78. * @eq_dma: DMA address of MSI event queues
  79. * @msi_addr: MSI address
  80. */
  81. struct iproc_msi {
  82. struct iproc_pcie *pcie;
  83. const u16 (*reg_offsets)[IPROC_MSI_REG_SIZE];
  84. struct iproc_msi_grp *grps;
  85. int nr_irqs;
  86. int nr_cpus;
  87. bool has_inten_reg;
  88. unsigned long *bitmap;
  89. struct mutex bitmap_lock;
  90. unsigned int nr_msi_vecs;
  91. struct irq_domain *inner_domain;
  92. unsigned int nr_eq_region;
  93. unsigned int nr_msi_region;
  94. void *eq_cpu;
  95. dma_addr_t eq_dma;
  96. phys_addr_t msi_addr;
  97. };
  98. static const u16 iproc_msi_reg_paxb[NR_HW_IRQS][IPROC_MSI_REG_SIZE] = {
  99. { 0x200, 0x2c0, 0x204, 0x2c4, 0x210, 0x250, 0x254, 0x208 },
  100. { 0x200, 0x2c0, 0x204, 0x2c4, 0x214, 0x258, 0x25c, 0x208 },
  101. { 0x200, 0x2c0, 0x204, 0x2c4, 0x218, 0x260, 0x264, 0x208 },
  102. { 0x200, 0x2c0, 0x204, 0x2c4, 0x21c, 0x268, 0x26c, 0x208 },
  103. { 0x200, 0x2c0, 0x204, 0x2c4, 0x220, 0x270, 0x274, 0x208 },
  104. { 0x200, 0x2c0, 0x204, 0x2c4, 0x224, 0x278, 0x27c, 0x208 },
  105. };
  106. static const u16 iproc_msi_reg_paxc[NR_HW_IRQS][IPROC_MSI_REG_SIZE] = {
  107. { 0xc00, 0xc04, 0xc08, 0xc0c, 0xc40, 0xc50, 0xc60 },
  108. { 0xc10, 0xc14, 0xc18, 0xc1c, 0xc44, 0xc54, 0xc64 },
  109. { 0xc20, 0xc24, 0xc28, 0xc2c, 0xc48, 0xc58, 0xc68 },
  110. { 0xc30, 0xc34, 0xc38, 0xc3c, 0xc4c, 0xc5c, 0xc6c },
  111. };
  112. static inline u32 iproc_msi_read_reg(struct iproc_msi *msi,
  113. enum iproc_msi_reg reg,
  114. unsigned int eq)
  115. {
  116. struct iproc_pcie *pcie = msi->pcie;
  117. return readl_relaxed(pcie->base + msi->reg_offsets[eq][reg]);
  118. }
  119. static inline void iproc_msi_write_reg(struct iproc_msi *msi,
  120. enum iproc_msi_reg reg,
  121. int eq, u32 val)
  122. {
  123. struct iproc_pcie *pcie = msi->pcie;
  124. writel_relaxed(val, pcie->base + msi->reg_offsets[eq][reg]);
  125. }
  126. static inline u32 hwirq_to_group(struct iproc_msi *msi, unsigned long hwirq)
  127. {
  128. return (hwirq % msi->nr_irqs);
  129. }
  130. static inline unsigned int iproc_msi_addr_offset(struct iproc_msi *msi,
  131. unsigned long hwirq)
  132. {
  133. if (msi->nr_msi_region > 1)
  134. return hwirq_to_group(msi, hwirq) * MSI_MEM_REGION_SIZE;
  135. else
  136. return hwirq_to_group(msi, hwirq) * sizeof(u32);
  137. }
  138. static inline unsigned int iproc_msi_eq_offset(struct iproc_msi *msi, u32 eq)
  139. {
  140. if (msi->nr_eq_region > 1)
  141. return eq * EQ_MEM_REGION_SIZE;
  142. else
  143. return eq * EQ_LEN * sizeof(u32);
  144. }
  145. #define IPROC_MSI_FLAGS_REQUIRED (MSI_FLAG_USE_DEF_DOM_OPS | \
  146. MSI_FLAG_USE_DEF_CHIP_OPS)
  147. #define IPROC_MSI_FLAGS_SUPPORTED (MSI_GENERIC_FLAGS_MASK | \
  148. MSI_FLAG_PCI_MSIX)
  149. static struct msi_parent_ops iproc_msi_parent_ops = {
  150. .required_flags = IPROC_MSI_FLAGS_REQUIRED,
  151. .supported_flags = IPROC_MSI_FLAGS_SUPPORTED,
  152. .bus_select_token = DOMAIN_BUS_PCI_MSI,
  153. .prefix = "iProc-",
  154. .init_dev_msi_info = msi_lib_init_dev_msi_info,
  155. };
  156. /*
  157. * In iProc PCIe core, each MSI group is serviced by a GIC interrupt and a
  158. * dedicated event queue. Each MSI group can support up to 64 MSI vectors.
  159. *
  160. * The number of MSI groups varies between different iProc SoCs. The total
  161. * number of CPU cores also varies. To support MSI IRQ affinity, we
  162. * distribute GIC interrupts across all available CPUs. MSI vector is moved
  163. * from one GIC interrupt to another to steer to the target CPU.
  164. *
  165. * Assuming:
  166. * - the number of MSI groups is M
  167. * - the number of CPU cores is N
  168. * - M is always a multiple of N
  169. *
  170. * Total number of raw MSI vectors = M * 64
  171. * Total number of supported MSI vectors = (M * 64) / N
  172. */
  173. static inline int hwirq_to_cpu(struct iproc_msi *msi, unsigned long hwirq)
  174. {
  175. return (hwirq % msi->nr_cpus);
  176. }
  177. static inline unsigned long hwirq_to_canonical_hwirq(struct iproc_msi *msi,
  178. unsigned long hwirq)
  179. {
  180. return (hwirq - hwirq_to_cpu(msi, hwirq));
  181. }
  182. static int iproc_msi_irq_set_affinity(struct irq_data *data,
  183. const struct cpumask *mask, bool force)
  184. {
  185. struct iproc_msi *msi = irq_data_get_irq_chip_data(data);
  186. int target_cpu = cpumask_first(mask);
  187. int curr_cpu;
  188. int ret;
  189. curr_cpu = hwirq_to_cpu(msi, data->hwirq);
  190. if (curr_cpu == target_cpu)
  191. ret = IRQ_SET_MASK_OK_DONE;
  192. else {
  193. /* steer MSI to the target CPU */
  194. data->hwirq = hwirq_to_canonical_hwirq(msi, data->hwirq) + target_cpu;
  195. ret = IRQ_SET_MASK_OK;
  196. }
  197. irq_data_update_effective_affinity(data, cpumask_of(target_cpu));
  198. return ret;
  199. }
  200. static void iproc_msi_irq_compose_msi_msg(struct irq_data *data,
  201. struct msi_msg *msg)
  202. {
  203. struct iproc_msi *msi = irq_data_get_irq_chip_data(data);
  204. dma_addr_t addr;
  205. addr = msi->msi_addr + iproc_msi_addr_offset(msi, data->hwirq);
  206. msg->address_lo = lower_32_bits(addr);
  207. msg->address_hi = upper_32_bits(addr);
  208. msg->data = data->hwirq << 5;
  209. }
  210. static struct irq_chip iproc_msi_bottom_irq_chip = {
  211. .name = "MSI",
  212. .irq_set_affinity = iproc_msi_irq_set_affinity,
  213. .irq_compose_msi_msg = iproc_msi_irq_compose_msi_msg,
  214. };
  215. static int iproc_msi_irq_domain_alloc(struct irq_domain *domain,
  216. unsigned int virq, unsigned int nr_irqs,
  217. void *args)
  218. {
  219. struct iproc_msi *msi = domain->host_data;
  220. int hwirq, i;
  221. if (msi->nr_cpus > 1 && nr_irqs > 1)
  222. return -EINVAL;
  223. mutex_lock(&msi->bitmap_lock);
  224. /*
  225. * Allocate 'nr_irqs' multiplied by 'nr_cpus' number of MSI vectors
  226. * each time
  227. */
  228. hwirq = bitmap_find_free_region(msi->bitmap, msi->nr_msi_vecs,
  229. order_base_2(msi->nr_cpus * nr_irqs));
  230. mutex_unlock(&msi->bitmap_lock);
  231. if (hwirq < 0)
  232. return -ENOSPC;
  233. for (i = 0; i < nr_irqs; i++) {
  234. irq_domain_set_info(domain, virq + i, hwirq + i,
  235. &iproc_msi_bottom_irq_chip,
  236. domain->host_data, handle_simple_irq,
  237. NULL, NULL);
  238. }
  239. return 0;
  240. }
  241. static void iproc_msi_irq_domain_free(struct irq_domain *domain,
  242. unsigned int virq, unsigned int nr_irqs)
  243. {
  244. struct irq_data *data = irq_domain_get_irq_data(domain, virq);
  245. struct iproc_msi *msi = irq_data_get_irq_chip_data(data);
  246. unsigned int hwirq;
  247. mutex_lock(&msi->bitmap_lock);
  248. hwirq = hwirq_to_canonical_hwirq(msi, data->hwirq);
  249. bitmap_release_region(msi->bitmap, hwirq,
  250. order_base_2(msi->nr_cpus * nr_irqs));
  251. mutex_unlock(&msi->bitmap_lock);
  252. irq_domain_free_irqs_parent(domain, virq, nr_irqs);
  253. }
  254. static const struct irq_domain_ops msi_domain_ops = {
  255. .alloc = iproc_msi_irq_domain_alloc,
  256. .free = iproc_msi_irq_domain_free,
  257. };
  258. static inline u32 decode_msi_hwirq(struct iproc_msi *msi, u32 eq, u32 head)
  259. {
  260. u32 __iomem *msg;
  261. u32 hwirq;
  262. unsigned int offs;
  263. offs = iproc_msi_eq_offset(msi, eq) + head * sizeof(u32);
  264. msg = (u32 __iomem *)(msi->eq_cpu + offs);
  265. hwirq = readl(msg);
  266. hwirq = (hwirq >> 5) + (hwirq & 0x1f);
  267. /*
  268. * Since we have multiple hwirq mapped to a single MSI vector,
  269. * now we need to derive the hwirq at CPU0. It can then be used to
  270. * mapped back to virq.
  271. */
  272. return hwirq_to_canonical_hwirq(msi, hwirq);
  273. }
  274. static void iproc_msi_handler(struct irq_desc *desc)
  275. {
  276. struct irq_chip *chip = irq_desc_get_chip(desc);
  277. struct iproc_msi_grp *grp;
  278. struct iproc_msi *msi;
  279. u32 eq, head, tail, nr_events;
  280. unsigned long hwirq;
  281. chained_irq_enter(chip, desc);
  282. grp = irq_desc_get_handler_data(desc);
  283. msi = grp->msi;
  284. eq = grp->eq;
  285. /*
  286. * iProc MSI event queue is tracked by head and tail pointers. Head
  287. * pointer indicates the next entry (MSI data) to be consumed by SW in
  288. * the queue and needs to be updated by SW. iProc MSI core uses the
  289. * tail pointer as the next data insertion point.
  290. *
  291. * Entries between head and tail pointers contain valid MSI data. MSI
  292. * data is guaranteed to be in the event queue memory before the tail
  293. * pointer is updated by the iProc MSI core.
  294. */
  295. head = iproc_msi_read_reg(msi, IPROC_MSI_EQ_HEAD,
  296. eq) & IPROC_MSI_EQ_MASK;
  297. do {
  298. tail = iproc_msi_read_reg(msi, IPROC_MSI_EQ_TAIL,
  299. eq) & IPROC_MSI_EQ_MASK;
  300. /*
  301. * Figure out total number of events (MSI data) to be
  302. * processed.
  303. */
  304. nr_events = (tail < head) ?
  305. (EQ_LEN - (head - tail)) : (tail - head);
  306. if (!nr_events)
  307. break;
  308. /* process all outstanding events */
  309. while (nr_events--) {
  310. hwirq = decode_msi_hwirq(msi, eq, head);
  311. generic_handle_domain_irq(msi->inner_domain, hwirq);
  312. head++;
  313. head %= EQ_LEN;
  314. }
  315. /*
  316. * Now all outstanding events have been processed. Update the
  317. * head pointer.
  318. */
  319. iproc_msi_write_reg(msi, IPROC_MSI_EQ_HEAD, eq, head);
  320. /*
  321. * Now go read the tail pointer again to see if there are new
  322. * outstanding events that came in during the above window.
  323. */
  324. } while (true);
  325. chained_irq_exit(chip, desc);
  326. }
  327. static void iproc_msi_enable(struct iproc_msi *msi)
  328. {
  329. int i, eq;
  330. u32 val;
  331. /* Program memory region for each event queue */
  332. for (i = 0; i < msi->nr_eq_region; i++) {
  333. dma_addr_t addr = msi->eq_dma + (i * EQ_MEM_REGION_SIZE);
  334. iproc_msi_write_reg(msi, IPROC_MSI_EQ_PAGE, i,
  335. lower_32_bits(addr));
  336. iproc_msi_write_reg(msi, IPROC_MSI_EQ_PAGE_UPPER, i,
  337. upper_32_bits(addr));
  338. }
  339. /* Program address region for MSI posted writes */
  340. for (i = 0; i < msi->nr_msi_region; i++) {
  341. phys_addr_t addr = msi->msi_addr + (i * MSI_MEM_REGION_SIZE);
  342. iproc_msi_write_reg(msi, IPROC_MSI_PAGE, i,
  343. lower_32_bits(addr));
  344. iproc_msi_write_reg(msi, IPROC_MSI_PAGE_UPPER, i,
  345. upper_32_bits(addr));
  346. }
  347. for (eq = 0; eq < msi->nr_irqs; eq++) {
  348. /* Enable MSI event queue */
  349. val = IPROC_MSI_INTR_EN | IPROC_MSI_INT_N_EVENT |
  350. IPROC_MSI_EQ_EN;
  351. iproc_msi_write_reg(msi, IPROC_MSI_CTRL, eq, val);
  352. /*
  353. * Some legacy platforms require the MSI interrupt enable
  354. * register to be set explicitly.
  355. */
  356. if (msi->has_inten_reg) {
  357. val = iproc_msi_read_reg(msi, IPROC_MSI_INTS_EN, eq);
  358. val |= BIT(eq);
  359. iproc_msi_write_reg(msi, IPROC_MSI_INTS_EN, eq, val);
  360. }
  361. }
  362. }
  363. static void iproc_msi_disable(struct iproc_msi *msi)
  364. {
  365. u32 eq, val;
  366. for (eq = 0; eq < msi->nr_irqs; eq++) {
  367. if (msi->has_inten_reg) {
  368. val = iproc_msi_read_reg(msi, IPROC_MSI_INTS_EN, eq);
  369. val &= ~BIT(eq);
  370. iproc_msi_write_reg(msi, IPROC_MSI_INTS_EN, eq, val);
  371. }
  372. val = iproc_msi_read_reg(msi, IPROC_MSI_CTRL, eq);
  373. val &= ~(IPROC_MSI_INTR_EN | IPROC_MSI_INT_N_EVENT |
  374. IPROC_MSI_EQ_EN);
  375. iproc_msi_write_reg(msi, IPROC_MSI_CTRL, eq, val);
  376. }
  377. }
  378. static int iproc_msi_alloc_domains(struct device_node *node,
  379. struct iproc_msi *msi)
  380. {
  381. struct irq_domain_info info = {
  382. .fwnode = of_fwnode_handle(node),
  383. .ops = &msi_domain_ops,
  384. .host_data = msi,
  385. .size = msi->nr_msi_vecs,
  386. };
  387. msi->inner_domain = msi_create_parent_irq_domain(&info, &iproc_msi_parent_ops);
  388. if (!msi->inner_domain)
  389. return -ENOMEM;
  390. return 0;
  391. }
  392. static void iproc_msi_free_domains(struct iproc_msi *msi)
  393. {
  394. if (msi->inner_domain)
  395. irq_domain_remove(msi->inner_domain);
  396. }
  397. static void iproc_msi_irq_free(struct iproc_msi *msi, unsigned int cpu)
  398. {
  399. int i;
  400. for (i = cpu; i < msi->nr_irqs; i += msi->nr_cpus) {
  401. irq_set_chained_handler_and_data(msi->grps[i].gic_irq,
  402. NULL, NULL);
  403. }
  404. }
  405. static int iproc_msi_irq_setup(struct iproc_msi *msi, unsigned int cpu)
  406. {
  407. int i, ret;
  408. cpumask_var_t mask;
  409. struct iproc_pcie *pcie = msi->pcie;
  410. for (i = cpu; i < msi->nr_irqs; i += msi->nr_cpus) {
  411. irq_set_chained_handler_and_data(msi->grps[i].gic_irq,
  412. iproc_msi_handler,
  413. &msi->grps[i]);
  414. /* Dedicate GIC interrupt to each CPU core */
  415. if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
  416. cpumask_clear(mask);
  417. cpumask_set_cpu(cpu, mask);
  418. ret = irq_set_affinity(msi->grps[i].gic_irq, mask);
  419. if (ret)
  420. dev_err(pcie->dev,
  421. "failed to set affinity for IRQ%d\n",
  422. msi->grps[i].gic_irq);
  423. free_cpumask_var(mask);
  424. } else {
  425. dev_err(pcie->dev, "failed to alloc CPU mask\n");
  426. ret = -EINVAL;
  427. }
  428. if (ret) {
  429. /* Free all configured/unconfigured IRQs */
  430. iproc_msi_irq_free(msi, cpu);
  431. return ret;
  432. }
  433. }
  434. return 0;
  435. }
  436. int iproc_msi_init(struct iproc_pcie *pcie, struct device_node *node)
  437. {
  438. struct iproc_msi *msi;
  439. int i, ret;
  440. unsigned int cpu;
  441. if (!of_device_is_compatible(node, "brcm,iproc-msi"))
  442. return -ENODEV;
  443. if (!of_property_read_bool(node, "msi-controller"))
  444. return -ENODEV;
  445. if (pcie->msi)
  446. return -EBUSY;
  447. msi = devm_kzalloc(pcie->dev, sizeof(*msi), GFP_KERNEL);
  448. if (!msi)
  449. return -ENOMEM;
  450. msi->pcie = pcie;
  451. pcie->msi = msi;
  452. msi->msi_addr = pcie->base_addr;
  453. mutex_init(&msi->bitmap_lock);
  454. msi->nr_cpus = num_possible_cpus();
  455. if (msi->nr_cpus == 1)
  456. iproc_msi_parent_ops.supported_flags |= MSI_FLAG_MULTI_PCI_MSI;
  457. msi->nr_irqs = of_irq_count(node);
  458. if (!msi->nr_irqs) {
  459. dev_err(pcie->dev, "found no MSI GIC interrupt\n");
  460. return -ENODEV;
  461. }
  462. if (msi->nr_irqs > NR_HW_IRQS) {
  463. dev_warn(pcie->dev, "too many MSI GIC interrupts defined %d\n",
  464. msi->nr_irqs);
  465. msi->nr_irqs = NR_HW_IRQS;
  466. }
  467. if (msi->nr_irqs < msi->nr_cpus) {
  468. dev_err(pcie->dev,
  469. "not enough GIC interrupts for MSI affinity\n");
  470. return -EINVAL;
  471. }
  472. if (msi->nr_irqs % msi->nr_cpus != 0) {
  473. msi->nr_irqs -= msi->nr_irqs % msi->nr_cpus;
  474. dev_warn(pcie->dev, "Reducing number of interrupts to %d\n",
  475. msi->nr_irqs);
  476. }
  477. switch (pcie->type) {
  478. case IPROC_PCIE_PAXB_BCMA:
  479. case IPROC_PCIE_PAXB:
  480. msi->reg_offsets = iproc_msi_reg_paxb;
  481. msi->nr_eq_region = 1;
  482. msi->nr_msi_region = 1;
  483. break;
  484. case IPROC_PCIE_PAXC:
  485. msi->reg_offsets = iproc_msi_reg_paxc;
  486. msi->nr_eq_region = msi->nr_irqs;
  487. msi->nr_msi_region = msi->nr_irqs;
  488. break;
  489. default:
  490. dev_err(pcie->dev, "incompatible iProc PCIe interface\n");
  491. return -EINVAL;
  492. }
  493. msi->has_inten_reg = of_property_read_bool(node, "brcm,pcie-msi-inten");
  494. msi->nr_msi_vecs = msi->nr_irqs * EQ_LEN;
  495. msi->bitmap = devm_bitmap_zalloc(pcie->dev, msi->nr_msi_vecs,
  496. GFP_KERNEL);
  497. if (!msi->bitmap)
  498. return -ENOMEM;
  499. msi->grps = devm_kcalloc(pcie->dev, msi->nr_irqs, sizeof(*msi->grps),
  500. GFP_KERNEL);
  501. if (!msi->grps)
  502. return -ENOMEM;
  503. for (i = 0; i < msi->nr_irqs; i++) {
  504. unsigned int irq = irq_of_parse_and_map(node, i);
  505. if (!irq) {
  506. dev_err(pcie->dev, "unable to parse/map interrupt\n");
  507. ret = -ENODEV;
  508. goto free_irqs;
  509. }
  510. msi->grps[i].gic_irq = irq;
  511. msi->grps[i].msi = msi;
  512. msi->grps[i].eq = i;
  513. }
  514. /* Reserve memory for event queue and make sure memories are zeroed */
  515. msi->eq_cpu = dma_alloc_coherent(pcie->dev,
  516. msi->nr_eq_region * EQ_MEM_REGION_SIZE,
  517. &msi->eq_dma, GFP_KERNEL);
  518. if (!msi->eq_cpu) {
  519. ret = -ENOMEM;
  520. goto free_irqs;
  521. }
  522. ret = iproc_msi_alloc_domains(node, msi);
  523. if (ret) {
  524. dev_err(pcie->dev, "failed to create MSI domains\n");
  525. goto free_eq_dma;
  526. }
  527. for_each_online_cpu(cpu) {
  528. ret = iproc_msi_irq_setup(msi, cpu);
  529. if (ret)
  530. goto free_msi_irq;
  531. }
  532. iproc_msi_enable(msi);
  533. return 0;
  534. free_msi_irq:
  535. for_each_online_cpu(cpu)
  536. iproc_msi_irq_free(msi, cpu);
  537. iproc_msi_free_domains(msi);
  538. free_eq_dma:
  539. dma_free_coherent(pcie->dev, msi->nr_eq_region * EQ_MEM_REGION_SIZE,
  540. msi->eq_cpu, msi->eq_dma);
  541. free_irqs:
  542. for (i = 0; i < msi->nr_irqs; i++) {
  543. if (msi->grps[i].gic_irq)
  544. irq_dispose_mapping(msi->grps[i].gic_irq);
  545. }
  546. pcie->msi = NULL;
  547. return ret;
  548. }
  549. EXPORT_SYMBOL(iproc_msi_init);
  550. void iproc_msi_exit(struct iproc_pcie *pcie)
  551. {
  552. struct iproc_msi *msi = pcie->msi;
  553. unsigned int i, cpu;
  554. if (!msi)
  555. return;
  556. iproc_msi_disable(msi);
  557. for_each_online_cpu(cpu)
  558. iproc_msi_irq_free(msi, cpu);
  559. iproc_msi_free_domains(msi);
  560. dma_free_coherent(pcie->dev, msi->nr_eq_region * EQ_MEM_REGION_SIZE,
  561. msi->eq_cpu, msi->eq_dma);
  562. for (i = 0; i < msi->nr_irqs; i++) {
  563. if (msi->grps[i].gic_irq)
  564. irq_dispose_mapping(msi->grps[i].gic_irq);
  565. }
  566. }
  567. EXPORT_SYMBOL(iproc_msi_exit);