pci-xgene-msi.c 15 KB

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