pci-xgene-msi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * APM X-Gene MSI Driver
  4. *
  5. * Copyright (c) 2014, Applied Micro Circuits Corporation
  6. * Author: Tanmay Inamdar <[email protected]>
  7. * Duc Dang <[email protected]>
  8. */
  9. #include <linux/cpu.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/module.h>
  12. #include <linux/msi.h>
  13. #include <linux/of_irq.h>
  14. #include <linux/irqchip/chained_irq.h>
  15. #include <linux/pci.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/of_pci.h>
  18. #define MSI_IR0 0x000000
  19. #define MSI_INT0 0x800000
  20. #define IDX_PER_GROUP 8
  21. #define IRQS_PER_IDX 16
  22. #define NR_HW_IRQS 16
  23. #define NR_MSI_VEC (IDX_PER_GROUP * IRQS_PER_IDX * NR_HW_IRQS)
  24. struct xgene_msi_group {
  25. struct xgene_msi *msi;
  26. int gic_irq;
  27. u32 msi_grp;
  28. };
  29. struct xgene_msi {
  30. struct device_node *node;
  31. struct irq_domain *inner_domain;
  32. struct irq_domain *msi_domain;
  33. u64 msi_addr;
  34. void __iomem *msi_regs;
  35. unsigned long *bitmap;
  36. struct mutex bitmap_lock;
  37. struct xgene_msi_group *msi_groups;
  38. int num_cpus;
  39. };
  40. /* Global data */
  41. static struct xgene_msi xgene_msi_ctrl;
  42. static struct irq_chip xgene_msi_top_irq_chip = {
  43. .name = "X-Gene1 MSI",
  44. .irq_enable = pci_msi_unmask_irq,
  45. .irq_disable = pci_msi_mask_irq,
  46. .irq_mask = pci_msi_mask_irq,
  47. .irq_unmask = pci_msi_unmask_irq,
  48. };
  49. static struct msi_domain_info xgene_msi_domain_info = {
  50. .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  51. MSI_FLAG_PCI_MSIX),
  52. .chip = &xgene_msi_top_irq_chip,
  53. };
  54. /*
  55. * X-Gene v1 has 16 groups of MSI termination registers MSInIRx, where
  56. * n is group number (0..F), x is index of registers in each group (0..7)
  57. * The register layout is as follows:
  58. * MSI0IR0 base_addr
  59. * MSI0IR1 base_addr + 0x10000
  60. * ... ...
  61. * MSI0IR6 base_addr + 0x60000
  62. * MSI0IR7 base_addr + 0x70000
  63. * MSI1IR0 base_addr + 0x80000
  64. * MSI1IR1 base_addr + 0x90000
  65. * ... ...
  66. * MSI1IR7 base_addr + 0xF0000
  67. * MSI2IR0 base_addr + 0x100000
  68. * ... ...
  69. * MSIFIR0 base_addr + 0x780000
  70. * MSIFIR1 base_addr + 0x790000
  71. * ... ...
  72. * MSIFIR7 base_addr + 0x7F0000
  73. * MSIINT0 base_addr + 0x800000
  74. * MSIINT1 base_addr + 0x810000
  75. * ... ...
  76. * MSIINTF base_addr + 0x8F0000
  77. *
  78. * Each index register supports 16 MSI vectors (0..15) to generate interrupt.
  79. * There are total 16 GIC IRQs assigned for these 16 groups of MSI termination
  80. * registers.
  81. *
  82. * Each MSI termination group has 1 MSIINTn register (n is 0..15) to indicate
  83. * the MSI pending status caused by 1 of its 8 index registers.
  84. */
  85. /* MSInIRx read helper */
  86. static u32 xgene_msi_ir_read(struct xgene_msi *msi,
  87. u32 msi_grp, u32 msir_idx)
  88. {
  89. return readl_relaxed(msi->msi_regs + MSI_IR0 +
  90. (msi_grp << 19) + (msir_idx << 16));
  91. }
  92. /* MSIINTn read helper */
  93. static u32 xgene_msi_int_read(struct xgene_msi *msi, u32 msi_grp)
  94. {
  95. return readl_relaxed(msi->msi_regs + MSI_INT0 + (msi_grp << 16));
  96. }
  97. /*
  98. * With 2048 MSI vectors supported, the MSI message can be constructed using
  99. * following scheme:
  100. * - Divide into 8 256-vector groups
  101. * Group 0: 0-255
  102. * Group 1: 256-511
  103. * Group 2: 512-767
  104. * ...
  105. * Group 7: 1792-2047
  106. * - Each 256-vector group is divided into 16 16-vector groups
  107. * As an example: 16 16-vector groups for 256-vector group 0-255 is
  108. * Group 0: 0-15
  109. * Group 1: 16-32
  110. * ...
  111. * Group 15: 240-255
  112. * - The termination address of MSI vector in 256-vector group n and 16-vector
  113. * group x is the address of MSIxIRn
  114. * - The data for MSI vector in 16-vector group x is x
  115. */
  116. static u32 hwirq_to_reg_set(unsigned long hwirq)
  117. {
  118. return (hwirq / (NR_HW_IRQS * IRQS_PER_IDX));
  119. }
  120. static u32 hwirq_to_group(unsigned long hwirq)
  121. {
  122. return (hwirq % NR_HW_IRQS);
  123. }
  124. static u32 hwirq_to_msi_data(unsigned long hwirq)
  125. {
  126. return ((hwirq / NR_HW_IRQS) % IRQS_PER_IDX);
  127. }
  128. static void xgene_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
  129. {
  130. struct xgene_msi *msi = irq_data_get_irq_chip_data(data);
  131. u32 reg_set = hwirq_to_reg_set(data->hwirq);
  132. u32 group = hwirq_to_group(data->hwirq);
  133. u64 target_addr = msi->msi_addr + (((8 * group) + reg_set) << 16);
  134. msg->address_hi = upper_32_bits(target_addr);
  135. msg->address_lo = lower_32_bits(target_addr);
  136. msg->data = hwirq_to_msi_data(data->hwirq);
  137. }
  138. /*
  139. * X-Gene v1 only has 16 MSI GIC IRQs for 2048 MSI vectors. To maintain
  140. * the expected behaviour of .set_affinity for each MSI interrupt, the 16
  141. * MSI GIC IRQs are statically allocated to 8 X-Gene v1 cores (2 GIC IRQs
  142. * for each core). The MSI vector is moved fom 1 MSI GIC IRQ to another
  143. * MSI GIC IRQ to steer its MSI interrupt to correct X-Gene v1 core. As a
  144. * consequence, the total MSI vectors that X-Gene v1 supports will be
  145. * reduced to 256 (2048/8) vectors.
  146. */
  147. static int hwirq_to_cpu(unsigned long hwirq)
  148. {
  149. return (hwirq % xgene_msi_ctrl.num_cpus);
  150. }
  151. static unsigned long hwirq_to_canonical_hwirq(unsigned long hwirq)
  152. {
  153. return (hwirq - hwirq_to_cpu(hwirq));
  154. }
  155. static int xgene_msi_set_affinity(struct irq_data *irqdata,
  156. const struct cpumask *mask, bool force)
  157. {
  158. int target_cpu = cpumask_first(mask);
  159. int curr_cpu;
  160. curr_cpu = hwirq_to_cpu(irqdata->hwirq);
  161. if (curr_cpu == target_cpu)
  162. return IRQ_SET_MASK_OK_DONE;
  163. /* Update MSI number to target the new CPU */
  164. irqdata->hwirq = hwirq_to_canonical_hwirq(irqdata->hwirq) + target_cpu;
  165. return IRQ_SET_MASK_OK;
  166. }
  167. static struct irq_chip xgene_msi_bottom_irq_chip = {
  168. .name = "MSI",
  169. .irq_set_affinity = xgene_msi_set_affinity,
  170. .irq_compose_msi_msg = xgene_compose_msi_msg,
  171. };
  172. static int xgene_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  173. unsigned int nr_irqs, void *args)
  174. {
  175. struct xgene_msi *msi = domain->host_data;
  176. int msi_irq;
  177. mutex_lock(&msi->bitmap_lock);
  178. msi_irq = bitmap_find_next_zero_area(msi->bitmap, NR_MSI_VEC, 0,
  179. msi->num_cpus, 0);
  180. if (msi_irq < NR_MSI_VEC)
  181. bitmap_set(msi->bitmap, msi_irq, msi->num_cpus);
  182. else
  183. msi_irq = -ENOSPC;
  184. mutex_unlock(&msi->bitmap_lock);
  185. if (msi_irq < 0)
  186. return msi_irq;
  187. irq_domain_set_info(domain, virq, msi_irq,
  188. &xgene_msi_bottom_irq_chip, domain->host_data,
  189. handle_simple_irq, NULL, NULL);
  190. return 0;
  191. }
  192. static void xgene_irq_domain_free(struct irq_domain *domain,
  193. unsigned int virq, unsigned int nr_irqs)
  194. {
  195. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  196. struct xgene_msi *msi = irq_data_get_irq_chip_data(d);
  197. u32 hwirq;
  198. mutex_lock(&msi->bitmap_lock);
  199. hwirq = hwirq_to_canonical_hwirq(d->hwirq);
  200. bitmap_clear(msi->bitmap, hwirq, msi->num_cpus);
  201. mutex_unlock(&msi->bitmap_lock);
  202. irq_domain_free_irqs_parent(domain, virq, nr_irqs);
  203. }
  204. static const struct irq_domain_ops msi_domain_ops = {
  205. .alloc = xgene_irq_domain_alloc,
  206. .free = xgene_irq_domain_free,
  207. };
  208. static int xgene_allocate_domains(struct xgene_msi *msi)
  209. {
  210. msi->inner_domain = irq_domain_add_linear(NULL, NR_MSI_VEC,
  211. &msi_domain_ops, msi);
  212. if (!msi->inner_domain)
  213. return -ENOMEM;
  214. msi->msi_domain = pci_msi_create_irq_domain(of_node_to_fwnode(msi->node),
  215. &xgene_msi_domain_info,
  216. msi->inner_domain);
  217. if (!msi->msi_domain) {
  218. irq_domain_remove(msi->inner_domain);
  219. return -ENOMEM;
  220. }
  221. return 0;
  222. }
  223. static void xgene_free_domains(struct xgene_msi *msi)
  224. {
  225. if (msi->msi_domain)
  226. irq_domain_remove(msi->msi_domain);
  227. if (msi->inner_domain)
  228. irq_domain_remove(msi->inner_domain);
  229. }
  230. static int xgene_msi_init_allocator(struct xgene_msi *xgene_msi)
  231. {
  232. xgene_msi->bitmap = bitmap_zalloc(NR_MSI_VEC, GFP_KERNEL);
  233. if (!xgene_msi->bitmap)
  234. return -ENOMEM;
  235. mutex_init(&xgene_msi->bitmap_lock);
  236. xgene_msi->msi_groups = kcalloc(NR_HW_IRQS,
  237. sizeof(struct xgene_msi_group),
  238. GFP_KERNEL);
  239. if (!xgene_msi->msi_groups)
  240. return -ENOMEM;
  241. return 0;
  242. }
  243. static void xgene_msi_isr(struct irq_desc *desc)
  244. {
  245. struct irq_chip *chip = irq_desc_get_chip(desc);
  246. struct xgene_msi_group *msi_groups;
  247. struct xgene_msi *xgene_msi;
  248. int msir_index, msir_val, hw_irq, ret;
  249. u32 intr_index, grp_select, msi_grp;
  250. chained_irq_enter(chip, desc);
  251. msi_groups = irq_desc_get_handler_data(desc);
  252. xgene_msi = msi_groups->msi;
  253. msi_grp = msi_groups->msi_grp;
  254. /*
  255. * MSIINTn (n is 0..F) indicates if there is a pending MSI interrupt
  256. * If bit x of this register is set (x is 0..7), one or more interrupts
  257. * corresponding to MSInIRx is set.
  258. */
  259. grp_select = xgene_msi_int_read(xgene_msi, msi_grp);
  260. while (grp_select) {
  261. msir_index = ffs(grp_select) - 1;
  262. /*
  263. * Calculate MSInIRx address to read to check for interrupts
  264. * (refer to termination address and data assignment
  265. * described in xgene_compose_msi_msg() )
  266. */
  267. msir_val = xgene_msi_ir_read(xgene_msi, msi_grp, msir_index);
  268. while (msir_val) {
  269. intr_index = ffs(msir_val) - 1;
  270. /*
  271. * Calculate MSI vector number (refer to the termination
  272. * address and data assignment described in
  273. * xgene_compose_msi_msg function)
  274. */
  275. hw_irq = (((msir_index * IRQS_PER_IDX) + intr_index) *
  276. NR_HW_IRQS) + msi_grp;
  277. /*
  278. * As we have multiple hw_irq that maps to single MSI,
  279. * always look up the virq using the hw_irq as seen from
  280. * CPU0
  281. */
  282. hw_irq = hwirq_to_canonical_hwirq(hw_irq);
  283. ret = generic_handle_domain_irq(xgene_msi->inner_domain, hw_irq);
  284. WARN_ON_ONCE(ret);
  285. msir_val &= ~(1 << intr_index);
  286. }
  287. grp_select &= ~(1 << msir_index);
  288. if (!grp_select) {
  289. /*
  290. * We handled all interrupts happened in this group,
  291. * resample this group MSI_INTx register in case
  292. * something else has been made pending in the meantime
  293. */
  294. grp_select = xgene_msi_int_read(xgene_msi, msi_grp);
  295. }
  296. }
  297. chained_irq_exit(chip, desc);
  298. }
  299. static enum cpuhp_state pci_xgene_online;
  300. static int xgene_msi_remove(struct platform_device *pdev)
  301. {
  302. struct xgene_msi *msi = platform_get_drvdata(pdev);
  303. if (pci_xgene_online)
  304. cpuhp_remove_state(pci_xgene_online);
  305. cpuhp_remove_state(CPUHP_PCI_XGENE_DEAD);
  306. kfree(msi->msi_groups);
  307. bitmap_free(msi->bitmap);
  308. msi->bitmap = NULL;
  309. xgene_free_domains(msi);
  310. return 0;
  311. }
  312. static int xgene_msi_hwirq_alloc(unsigned int cpu)
  313. {
  314. struct xgene_msi *msi = &xgene_msi_ctrl;
  315. struct xgene_msi_group *msi_group;
  316. cpumask_var_t mask;
  317. int i;
  318. int err;
  319. for (i = cpu; i < NR_HW_IRQS; i += msi->num_cpus) {
  320. msi_group = &msi->msi_groups[i];
  321. if (!msi_group->gic_irq)
  322. continue;
  323. irq_set_chained_handler_and_data(msi_group->gic_irq,
  324. xgene_msi_isr, msi_group);
  325. /*
  326. * Statically allocate MSI GIC IRQs to each CPU core.
  327. * With 8-core X-Gene v1, 2 MSI GIC IRQs are allocated
  328. * to each core.
  329. */
  330. if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
  331. cpumask_clear(mask);
  332. cpumask_set_cpu(cpu, mask);
  333. err = irq_set_affinity(msi_group->gic_irq, mask);
  334. if (err)
  335. pr_err("failed to set affinity for GIC IRQ");
  336. free_cpumask_var(mask);
  337. } else {
  338. pr_err("failed to alloc CPU mask for affinity\n");
  339. err = -EINVAL;
  340. }
  341. if (err) {
  342. irq_set_chained_handler_and_data(msi_group->gic_irq,
  343. NULL, NULL);
  344. return err;
  345. }
  346. }
  347. return 0;
  348. }
  349. static int xgene_msi_hwirq_free(unsigned int cpu)
  350. {
  351. struct xgene_msi *msi = &xgene_msi_ctrl;
  352. struct xgene_msi_group *msi_group;
  353. int i;
  354. for (i = cpu; i < NR_HW_IRQS; i += msi->num_cpus) {
  355. msi_group = &msi->msi_groups[i];
  356. if (!msi_group->gic_irq)
  357. continue;
  358. irq_set_chained_handler_and_data(msi_group->gic_irq, NULL,
  359. NULL);
  360. }
  361. return 0;
  362. }
  363. static const struct of_device_id xgene_msi_match_table[] = {
  364. {.compatible = "apm,xgene1-msi"},
  365. {},
  366. };
  367. static int xgene_msi_probe(struct platform_device *pdev)
  368. {
  369. struct resource *res;
  370. int rc, irq_index;
  371. struct xgene_msi *xgene_msi;
  372. int virt_msir;
  373. u32 msi_val, msi_idx;
  374. xgene_msi = &xgene_msi_ctrl;
  375. platform_set_drvdata(pdev, xgene_msi);
  376. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  377. xgene_msi->msi_regs = devm_ioremap_resource(&pdev->dev, res);
  378. if (IS_ERR(xgene_msi->msi_regs)) {
  379. rc = PTR_ERR(xgene_msi->msi_regs);
  380. goto error;
  381. }
  382. xgene_msi->msi_addr = res->start;
  383. xgene_msi->node = pdev->dev.of_node;
  384. xgene_msi->num_cpus = num_possible_cpus();
  385. rc = xgene_msi_init_allocator(xgene_msi);
  386. if (rc) {
  387. dev_err(&pdev->dev, "Error allocating MSI bitmap\n");
  388. goto error;
  389. }
  390. rc = xgene_allocate_domains(xgene_msi);
  391. if (rc) {
  392. dev_err(&pdev->dev, "Failed to allocate MSI domain\n");
  393. goto error;
  394. }
  395. for (irq_index = 0; irq_index < NR_HW_IRQS; irq_index++) {
  396. virt_msir = platform_get_irq(pdev, irq_index);
  397. if (virt_msir < 0) {
  398. rc = virt_msir;
  399. goto error;
  400. }
  401. xgene_msi->msi_groups[irq_index].gic_irq = virt_msir;
  402. xgene_msi->msi_groups[irq_index].msi_grp = irq_index;
  403. xgene_msi->msi_groups[irq_index].msi = xgene_msi;
  404. }
  405. /*
  406. * MSInIRx registers are read-to-clear; before registering
  407. * interrupt handlers, read all of them to clear spurious
  408. * interrupts that may occur before the driver is probed.
  409. */
  410. for (irq_index = 0; irq_index < NR_HW_IRQS; irq_index++) {
  411. for (msi_idx = 0; msi_idx < IDX_PER_GROUP; msi_idx++)
  412. xgene_msi_ir_read(xgene_msi, irq_index, msi_idx);
  413. /* Read MSIINTn to confirm */
  414. msi_val = xgene_msi_int_read(xgene_msi, irq_index);
  415. if (msi_val) {
  416. dev_err(&pdev->dev, "Failed to clear spurious IRQ\n");
  417. rc = -EINVAL;
  418. goto error;
  419. }
  420. }
  421. rc = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "pci/xgene:online",
  422. xgene_msi_hwirq_alloc, NULL);
  423. if (rc < 0)
  424. goto err_cpuhp;
  425. pci_xgene_online = rc;
  426. rc = cpuhp_setup_state(CPUHP_PCI_XGENE_DEAD, "pci/xgene:dead", NULL,
  427. xgene_msi_hwirq_free);
  428. if (rc)
  429. goto err_cpuhp;
  430. dev_info(&pdev->dev, "APM X-Gene PCIe MSI driver loaded\n");
  431. return 0;
  432. err_cpuhp:
  433. dev_err(&pdev->dev, "failed to add CPU MSI notifier\n");
  434. error:
  435. xgene_msi_remove(pdev);
  436. return rc;
  437. }
  438. static struct platform_driver xgene_msi_driver = {
  439. .driver = {
  440. .name = "xgene-msi",
  441. .of_match_table = xgene_msi_match_table,
  442. },
  443. .probe = xgene_msi_probe,
  444. .remove = xgene_msi_remove,
  445. };
  446. static int __init xgene_pcie_msi_init(void)
  447. {
  448. return platform_driver_register(&xgene_msi_driver);
  449. }
  450. subsys_initcall(xgene_pcie_msi_init);