fsl_msi.c 15 KB

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