fsl_msi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /*
  2. * Copyright (C) 2007-2011 Freescale Semiconductor, Inc.
  3. *
  4. * Author: Tony Li <tony.li@freescale.com>
  5. * Jason Jin <Jason.jin@freescale.com>
  6. *
  7. * The hwirq alloc and free code reuse from sysdev/mpic_msi.c
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; version 2 of the
  12. * License.
  13. *
  14. */
  15. #include <linux/irq.h>
  16. #include <linux/bootmem.h>
  17. #include <linux/msi.h>
  18. #include <linux/pci.h>
  19. #include <linux/slab.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/seq_file.h>
  23. #include <sysdev/fsl_soc.h>
  24. #include <asm/prom.h>
  25. #include <asm/hw_irq.h>
  26. #include <asm/ppc-pci.h>
  27. #include <asm/mpic.h>
  28. #include <asm/fsl_hcalls.h>
  29. #include "fsl_msi.h"
  30. #include "fsl_pci.h"
  31. #define MSIIR_OFFSET_MASK 0xfffff
  32. #define MSIIR_IBS_SHIFT 0
  33. #define MSIIR_SRS_SHIFT 5
  34. #define MSIIR1_IBS_SHIFT 4
  35. #define MSIIR1_SRS_SHIFT 0
  36. #define MSI_SRS_MASK 0xf
  37. #define MSI_IBS_MASK 0x1f
  38. #define msi_hwirq(msi, msir_index, intr_index) \
  39. ((msir_index) << (msi)->srs_shift | \
  40. ((intr_index) << (msi)->ibs_shift))
  41. static LIST_HEAD(msi_head);
  42. struct fsl_msi_feature {
  43. u32 fsl_pic_ip;
  44. u32 msiir_offset; /* Offset of MSIIR, relative to start of MSIR bank */
  45. };
  46. struct fsl_msi_cascade_data {
  47. struct fsl_msi *msi_data;
  48. int index;
  49. int virq;
  50. };
  51. static inline u32 fsl_msi_read(u32 __iomem *base, unsigned int reg)
  52. {
  53. return in_be32(base + (reg >> 2));
  54. }
  55. /*
  56. * We do not need this actually. The MSIR register has been read once
  57. * in the cascade interrupt. So, this MSI interrupt has been acked
  58. */
  59. static void fsl_msi_end_irq(struct irq_data *d)
  60. {
  61. }
  62. static void fsl_msi_print_chip(struct irq_data *irqd, struct seq_file *p)
  63. {
  64. struct fsl_msi *msi_data = irqd->domain->host_data;
  65. irq_hw_number_t hwirq = irqd_to_hwirq(irqd);
  66. int cascade_virq, srs;
  67. srs = (hwirq >> msi_data->srs_shift) & MSI_SRS_MASK;
  68. cascade_virq = msi_data->cascade_array[srs]->virq;
  69. seq_printf(p, " fsl-msi-%d", cascade_virq);
  70. }
  71. static struct irq_chip fsl_msi_chip = {
  72. .irq_mask = mask_msi_irq,
  73. .irq_unmask = unmask_msi_irq,
  74. .irq_ack = fsl_msi_end_irq,
  75. .irq_print_chip = fsl_msi_print_chip,
  76. };
  77. static int fsl_msi_host_map(struct irq_domain *h, unsigned int virq,
  78. irq_hw_number_t hw)
  79. {
  80. struct fsl_msi *msi_data = h->host_data;
  81. struct irq_chip *chip = &fsl_msi_chip;
  82. irq_set_status_flags(virq, IRQ_TYPE_EDGE_FALLING);
  83. irq_set_chip_data(virq, msi_data);
  84. irq_set_chip_and_handler(virq, chip, handle_edge_irq);
  85. return 0;
  86. }
  87. static const struct irq_domain_ops fsl_msi_host_ops = {
  88. .map = fsl_msi_host_map,
  89. };
  90. static int fsl_msi_init_allocator(struct fsl_msi *msi_data)
  91. {
  92. int rc, hwirq;
  93. rc = msi_bitmap_alloc(&msi_data->bitmap, NR_MSI_IRQS_MAX,
  94. msi_data->irqhost->of_node);
  95. if (rc)
  96. return rc;
  97. /*
  98. * Reserve all the hwirqs
  99. * The available hwirqs will be released in fsl_msi_setup_hwirq()
  100. */
  101. for (hwirq = 0; hwirq < NR_MSI_IRQS_MAX; hwirq++)
  102. msi_bitmap_reserve_hwirq(&msi_data->bitmap, hwirq);
  103. return 0;
  104. }
  105. static int fsl_msi_check_device(struct pci_dev *pdev, int nvec, int type)
  106. {
  107. if (type == PCI_CAP_ID_MSIX)
  108. pr_debug("fslmsi: MSI-X untested, trying anyway.\n");
  109. return 0;
  110. }
  111. static void fsl_teardown_msi_irqs(struct pci_dev *pdev)
  112. {
  113. struct msi_desc *entry;
  114. struct fsl_msi *msi_data;
  115. list_for_each_entry(entry, &pdev->msi_list, list) {
  116. if (entry->irq == NO_IRQ)
  117. continue;
  118. msi_data = irq_get_chip_data(entry->irq);
  119. irq_set_msi_desc(entry->irq, NULL);
  120. msi_bitmap_free_hwirqs(&msi_data->bitmap,
  121. virq_to_hw(entry->irq), 1);
  122. irq_dispose_mapping(entry->irq);
  123. }
  124. return;
  125. }
  126. static void fsl_compose_msi_msg(struct pci_dev *pdev, int hwirq,
  127. struct msi_msg *msg,
  128. struct fsl_msi *fsl_msi_data)
  129. {
  130. struct fsl_msi *msi_data = fsl_msi_data;
  131. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  132. u64 address; /* Physical address of the MSIIR */
  133. int len;
  134. const __be64 *reg;
  135. /* If the msi-address-64 property exists, then use it */
  136. reg = of_get_property(hose->dn, "msi-address-64", &len);
  137. if (reg && (len == sizeof(u64)))
  138. address = be64_to_cpup(reg);
  139. else
  140. address = fsl_pci_immrbar_base(hose) + msi_data->msiir_offset;
  141. msg->address_lo = lower_32_bits(address);
  142. msg->address_hi = upper_32_bits(address);
  143. msg->data = hwirq;
  144. pr_debug("%s: allocated srs: %d, ibs: %d\n", __func__,
  145. (hwirq >> msi_data->srs_shift) & MSI_SRS_MASK,
  146. (hwirq >> msi_data->ibs_shift) & MSI_IBS_MASK);
  147. }
  148. static int fsl_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
  149. {
  150. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  151. struct device_node *np;
  152. phandle phandle = 0;
  153. int rc, hwirq = -ENOMEM;
  154. unsigned int virq;
  155. struct msi_desc *entry;
  156. struct msi_msg msg;
  157. struct fsl_msi *msi_data;
  158. /*
  159. * If the PCI node has an fsl,msi property, then we need to use it
  160. * to find the specific MSI.
  161. */
  162. np = of_parse_phandle(hose->dn, "fsl,msi", 0);
  163. if (np) {
  164. if (of_device_is_compatible(np, "fsl,mpic-msi") ||
  165. of_device_is_compatible(np, "fsl,vmpic-msi") ||
  166. of_device_is_compatible(np, "fsl,vmpic-msi-v4.3"))
  167. phandle = np->phandle;
  168. else {
  169. dev_err(&pdev->dev,
  170. "node %s has an invalid fsl,msi phandle %u\n",
  171. hose->dn->full_name, np->phandle);
  172. return -EINVAL;
  173. }
  174. }
  175. list_for_each_entry(entry, &pdev->msi_list, list) {
  176. /*
  177. * Loop over all the MSI devices until we find one that has an
  178. * available interrupt.
  179. */
  180. list_for_each_entry(msi_data, &msi_head, list) {
  181. /*
  182. * If the PCI node has an fsl,msi property, then we
  183. * restrict our search to the corresponding MSI node.
  184. * The simplest way is to skip over MSI nodes with the
  185. * wrong phandle. Under the Freescale hypervisor, this
  186. * has the additional benefit of skipping over MSI
  187. * nodes that are not mapped in the PAMU.
  188. */
  189. if (phandle && (phandle != msi_data->phandle))
  190. continue;
  191. hwirq = msi_bitmap_alloc_hwirqs(&msi_data->bitmap, 1);
  192. if (hwirq >= 0)
  193. break;
  194. }
  195. if (hwirq < 0) {
  196. rc = hwirq;
  197. dev_err(&pdev->dev, "could not allocate MSI interrupt\n");
  198. goto out_free;
  199. }
  200. virq = irq_create_mapping(msi_data->irqhost, hwirq);
  201. if (virq == NO_IRQ) {
  202. dev_err(&pdev->dev, "fail mapping hwirq %i\n", hwirq);
  203. msi_bitmap_free_hwirqs(&msi_data->bitmap, hwirq, 1);
  204. rc = -ENOSPC;
  205. goto out_free;
  206. }
  207. /* chip_data is msi_data via host->hostdata in host->map() */
  208. irq_set_msi_desc(virq, entry);
  209. fsl_compose_msi_msg(pdev, hwirq, &msg, msi_data);
  210. write_msi_msg(virq, &msg);
  211. }
  212. return 0;
  213. out_free:
  214. /* free by the caller of this function */
  215. return rc;
  216. }
  217. static irqreturn_t fsl_msi_cascade(int irq, void *data)
  218. {
  219. unsigned int cascade_irq;
  220. struct fsl_msi *msi_data;
  221. int msir_index = -1;
  222. u32 msir_value = 0;
  223. u32 intr_index;
  224. u32 have_shift = 0;
  225. struct fsl_msi_cascade_data *cascade_data = data;
  226. irqreturn_t ret = IRQ_NONE;
  227. msi_data = cascade_data->msi_data;
  228. msir_index = cascade_data->index;
  229. if (msir_index >= NR_MSI_REG_MAX)
  230. cascade_irq = NO_IRQ;
  231. switch (msi_data->feature & FSL_PIC_IP_MASK) {
  232. case FSL_PIC_IP_MPIC:
  233. msir_value = fsl_msi_read(msi_data->msi_regs,
  234. msir_index * 0x10);
  235. break;
  236. case FSL_PIC_IP_IPIC:
  237. msir_value = fsl_msi_read(msi_data->msi_regs, msir_index * 0x4);
  238. break;
  239. #ifdef CONFIG_EPAPR_PARAVIRT
  240. case FSL_PIC_IP_VMPIC: {
  241. unsigned int ret;
  242. ret = fh_vmpic_get_msir(virq_to_hw(irq), &msir_value);
  243. if (ret) {
  244. pr_err("fsl-msi: fh_vmpic_get_msir() failed for "
  245. "irq %u (ret=%u)\n", irq, ret);
  246. msir_value = 0;
  247. }
  248. break;
  249. }
  250. #endif
  251. }
  252. while (msir_value) {
  253. intr_index = ffs(msir_value) - 1;
  254. cascade_irq = irq_linear_revmap(msi_data->irqhost,
  255. msi_hwirq(msi_data, msir_index,
  256. intr_index + have_shift));
  257. if (cascade_irq != NO_IRQ) {
  258. generic_handle_irq(cascade_irq);
  259. ret = IRQ_HANDLED;
  260. }
  261. have_shift += intr_index + 1;
  262. msir_value = msir_value >> (intr_index + 1);
  263. }
  264. return ret;
  265. }
  266. static int fsl_of_msi_remove(struct platform_device *ofdev)
  267. {
  268. struct fsl_msi *msi = platform_get_drvdata(ofdev);
  269. int virq, i;
  270. if (msi->list.prev != NULL)
  271. list_del(&msi->list);
  272. for (i = 0; i < NR_MSI_REG_MAX; i++) {
  273. if (msi->cascade_array[i]) {
  274. virq = msi->cascade_array[i]->virq;
  275. BUG_ON(virq == NO_IRQ);
  276. free_irq(virq, msi->cascade_array[i]);
  277. kfree(msi->cascade_array[i]);
  278. irq_dispose_mapping(virq);
  279. }
  280. }
  281. if (msi->bitmap.bitmap)
  282. msi_bitmap_free(&msi->bitmap);
  283. if ((msi->feature & FSL_PIC_IP_MASK) != FSL_PIC_IP_VMPIC)
  284. iounmap(msi->msi_regs);
  285. kfree(msi);
  286. return 0;
  287. }
  288. static struct lock_class_key fsl_msi_irq_class;
  289. static int fsl_msi_setup_hwirq(struct fsl_msi *msi, struct platform_device *dev,
  290. int offset, int irq_index)
  291. {
  292. struct fsl_msi_cascade_data *cascade_data = NULL;
  293. int virt_msir, i, ret;
  294. virt_msir = irq_of_parse_and_map(dev->dev.of_node, irq_index);
  295. if (virt_msir == NO_IRQ) {
  296. dev_err(&dev->dev, "%s: Cannot translate IRQ index %d\n",
  297. __func__, irq_index);
  298. return 0;
  299. }
  300. cascade_data = kzalloc(sizeof(struct fsl_msi_cascade_data), GFP_KERNEL);
  301. if (!cascade_data) {
  302. dev_err(&dev->dev, "No memory for MSI cascade data\n");
  303. return -ENOMEM;
  304. }
  305. irq_set_lockdep_class(virt_msir, &fsl_msi_irq_class);
  306. cascade_data->index = offset;
  307. cascade_data->msi_data = msi;
  308. cascade_data->virq = virt_msir;
  309. msi->cascade_array[irq_index] = cascade_data;
  310. ret = request_irq(virt_msir, fsl_msi_cascade, 0,
  311. "fsl-msi-cascade", cascade_data);
  312. if (ret) {
  313. dev_err(&dev->dev, "failed to request_irq(%d), ret = %d\n",
  314. virt_msir, ret);
  315. return ret;
  316. }
  317. /* Release the hwirqs corresponding to this MSI register */
  318. for (i = 0; i < IRQS_PER_MSI_REG; i++)
  319. msi_bitmap_free_hwirqs(&msi->bitmap,
  320. msi_hwirq(msi, offset, i), 1);
  321. return 0;
  322. }
  323. static const struct of_device_id fsl_of_msi_ids[];
  324. static int fsl_of_msi_probe(struct platform_device *dev)
  325. {
  326. const struct of_device_id *match;
  327. struct fsl_msi *msi;
  328. struct resource res, msiir;
  329. int err, i, j, irq_index, count;
  330. const u32 *p;
  331. const struct fsl_msi_feature *features;
  332. int len;
  333. u32 offset;
  334. match = of_match_device(fsl_of_msi_ids, &dev->dev);
  335. if (!match)
  336. return -EINVAL;
  337. features = match->data;
  338. printk(KERN_DEBUG "Setting up Freescale MSI support\n");
  339. msi = kzalloc(sizeof(struct fsl_msi), GFP_KERNEL);
  340. if (!msi) {
  341. dev_err(&dev->dev, "No memory for MSI structure\n");
  342. return -ENOMEM;
  343. }
  344. platform_set_drvdata(dev, msi);
  345. msi->irqhost = irq_domain_add_linear(dev->dev.of_node,
  346. NR_MSI_IRQS_MAX, &fsl_msi_host_ops, msi);
  347. if (msi->irqhost == NULL) {
  348. dev_err(&dev->dev, "No memory for MSI irqhost\n");
  349. err = -ENOMEM;
  350. goto error_out;
  351. }
  352. /*
  353. * Under the Freescale hypervisor, the msi nodes don't have a 'reg'
  354. * property. Instead, we use hypercalls to access the MSI.
  355. */
  356. if ((features->fsl_pic_ip & FSL_PIC_IP_MASK) != FSL_PIC_IP_VMPIC) {
  357. err = of_address_to_resource(dev->dev.of_node, 0, &res);
  358. if (err) {
  359. dev_err(&dev->dev, "invalid resource for node %s\n",
  360. dev->dev.of_node->full_name);
  361. goto error_out;
  362. }
  363. msi->msi_regs = ioremap(res.start, resource_size(&res));
  364. if (!msi->msi_regs) {
  365. err = -ENOMEM;
  366. dev_err(&dev->dev, "could not map node %s\n",
  367. dev->dev.of_node->full_name);
  368. goto error_out;
  369. }
  370. msi->msiir_offset =
  371. features->msiir_offset + (res.start & 0xfffff);
  372. /*
  373. * First read the MSIIR/MSIIR1 offset from dts
  374. * On failure use the hardcode MSIIR offset
  375. */
  376. if (of_address_to_resource(dev->dev.of_node, 1, &msiir))
  377. msi->msiir_offset = features->msiir_offset +
  378. (res.start & MSIIR_OFFSET_MASK);
  379. else
  380. msi->msiir_offset = msiir.start & MSIIR_OFFSET_MASK;
  381. }
  382. msi->feature = features->fsl_pic_ip;
  383. /*
  384. * Remember the phandle, so that we can match with any PCI nodes
  385. * that have an "fsl,msi" property.
  386. */
  387. msi->phandle = dev->dev.of_node->phandle;
  388. err = fsl_msi_init_allocator(msi);
  389. if (err) {
  390. dev_err(&dev->dev, "Error allocating MSI bitmap\n");
  391. goto error_out;
  392. }
  393. p = of_get_property(dev->dev.of_node, "msi-available-ranges", &len);
  394. if (of_device_is_compatible(dev->dev.of_node, "fsl,mpic-msi-v4.3") ||
  395. of_device_is_compatible(dev->dev.of_node, "fsl,vmpic-msi-v4.3")) {
  396. msi->srs_shift = MSIIR1_SRS_SHIFT;
  397. msi->ibs_shift = MSIIR1_IBS_SHIFT;
  398. if (p)
  399. dev_warn(&dev->dev, "%s: dose not support msi-available-ranges property\n",
  400. __func__);
  401. for (irq_index = 0; irq_index < NR_MSI_REG_MSIIR1;
  402. irq_index++) {
  403. err = fsl_msi_setup_hwirq(msi, dev,
  404. irq_index, irq_index);
  405. if (err)
  406. goto error_out;
  407. }
  408. } else {
  409. static const u32 all_avail[] =
  410. { 0, NR_MSI_REG_MSIIR * IRQS_PER_MSI_REG };
  411. msi->srs_shift = MSIIR_SRS_SHIFT;
  412. msi->ibs_shift = MSIIR_IBS_SHIFT;
  413. if (p && len % (2 * sizeof(u32)) != 0) {
  414. dev_err(&dev->dev, "%s: Malformed msi-available-ranges property\n",
  415. __func__);
  416. err = -EINVAL;
  417. goto error_out;
  418. }
  419. if (!p) {
  420. p = all_avail;
  421. len = sizeof(all_avail);
  422. }
  423. for (irq_index = 0, i = 0; i < len / (2 * sizeof(u32)); i++) {
  424. if (p[i * 2] % IRQS_PER_MSI_REG ||
  425. p[i * 2 + 1] % IRQS_PER_MSI_REG) {
  426. pr_warn("%s: %s: msi available range of %u at %u is not IRQ-aligned\n",
  427. __func__, dev->dev.of_node->full_name,
  428. p[i * 2 + 1], p[i * 2]);
  429. err = -EINVAL;
  430. goto error_out;
  431. }
  432. offset = p[i * 2] / IRQS_PER_MSI_REG;
  433. count = p[i * 2 + 1] / IRQS_PER_MSI_REG;
  434. for (j = 0; j < count; j++, irq_index++) {
  435. err = fsl_msi_setup_hwirq(msi, dev, offset + j,
  436. irq_index);
  437. if (err)
  438. goto error_out;
  439. }
  440. }
  441. }
  442. list_add_tail(&msi->list, &msi_head);
  443. /* The multiple setting ppc_md.setup_msi_irqs will not harm things */
  444. if (!ppc_md.setup_msi_irqs) {
  445. ppc_md.setup_msi_irqs = fsl_setup_msi_irqs;
  446. ppc_md.teardown_msi_irqs = fsl_teardown_msi_irqs;
  447. ppc_md.msi_check_device = fsl_msi_check_device;
  448. } else if (ppc_md.setup_msi_irqs != fsl_setup_msi_irqs) {
  449. dev_err(&dev->dev, "Different MSI driver already installed!\n");
  450. err = -ENODEV;
  451. goto error_out;
  452. }
  453. return 0;
  454. error_out:
  455. fsl_of_msi_remove(dev);
  456. return err;
  457. }
  458. static const struct fsl_msi_feature mpic_msi_feature = {
  459. .fsl_pic_ip = FSL_PIC_IP_MPIC,
  460. .msiir_offset = 0x140,
  461. };
  462. static const struct fsl_msi_feature ipic_msi_feature = {
  463. .fsl_pic_ip = FSL_PIC_IP_IPIC,
  464. .msiir_offset = 0x38,
  465. };
  466. static const struct fsl_msi_feature vmpic_msi_feature = {
  467. .fsl_pic_ip = FSL_PIC_IP_VMPIC,
  468. .msiir_offset = 0,
  469. };
  470. static const struct of_device_id fsl_of_msi_ids[] = {
  471. {
  472. .compatible = "fsl,mpic-msi",
  473. .data = &mpic_msi_feature,
  474. },
  475. {
  476. .compatible = "fsl,mpic-msi-v4.3",
  477. .data = &mpic_msi_feature,
  478. },
  479. {
  480. .compatible = "fsl,ipic-msi",
  481. .data = &ipic_msi_feature,
  482. },
  483. #ifdef CONFIG_EPAPR_PARAVIRT
  484. {
  485. .compatible = "fsl,vmpic-msi",
  486. .data = &vmpic_msi_feature,
  487. },
  488. {
  489. .compatible = "fsl,vmpic-msi-v4.3",
  490. .data = &vmpic_msi_feature,
  491. },
  492. #endif
  493. {}
  494. };
  495. static struct platform_driver fsl_of_msi_driver = {
  496. .driver = {
  497. .name = "fsl-msi",
  498. .owner = THIS_MODULE,
  499. .of_match_table = fsl_of_msi_ids,
  500. },
  501. .probe = fsl_of_msi_probe,
  502. .remove = fsl_of_msi_remove,
  503. };
  504. static __init int fsl_of_msi_init(void)
  505. {
  506. return platform_driver_register(&fsl_of_msi_driver);
  507. }
  508. subsys_initcall(fsl_of_msi_init);