fsl_msi.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Copyright (C) 2007-2008 Freescale Semiconductor, Inc. All rights reserved.
  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/of_platform.h>
  20. #include <sysdev/fsl_soc.h>
  21. #include <asm/prom.h>
  22. #include <asm/hw_irq.h>
  23. #include <asm/ppc-pci.h>
  24. #include "fsl_msi.h"
  25. struct fsl_msi_feature {
  26. u32 fsl_pic_ip;
  27. u32 msiir_offset;
  28. };
  29. static struct fsl_msi *fsl_msi;
  30. static inline u32 fsl_msi_read(u32 __iomem *base, unsigned int reg)
  31. {
  32. return in_be32(base + (reg >> 2));
  33. }
  34. /*
  35. * We do not need this actually. The MSIR register has been read once
  36. * in the cascade interrupt. So, this MSI interrupt has been acked
  37. */
  38. static void fsl_msi_end_irq(unsigned int virq)
  39. {
  40. }
  41. static struct irq_chip fsl_msi_chip = {
  42. .mask = mask_msi_irq,
  43. .unmask = unmask_msi_irq,
  44. .ack = fsl_msi_end_irq,
  45. .typename = " FSL-MSI ",
  46. };
  47. static int fsl_msi_host_map(struct irq_host *h, unsigned int virq,
  48. irq_hw_number_t hw)
  49. {
  50. struct irq_chip *chip = &fsl_msi_chip;
  51. get_irq_desc(virq)->status |= IRQ_TYPE_EDGE_FALLING;
  52. set_irq_chip_and_handler(virq, chip, handle_edge_irq);
  53. return 0;
  54. }
  55. static struct irq_host_ops fsl_msi_host_ops = {
  56. .map = fsl_msi_host_map,
  57. };
  58. static int fsl_msi_init_allocator(struct fsl_msi *msi_data)
  59. {
  60. int rc;
  61. rc = msi_bitmap_alloc(&msi_data->bitmap, NR_MSI_IRQS,
  62. msi_data->irqhost->of_node);
  63. if (rc)
  64. return rc;
  65. rc = msi_bitmap_reserve_dt_hwirqs(&msi_data->bitmap);
  66. if (rc < 0) {
  67. msi_bitmap_free(&msi_data->bitmap);
  68. return rc;
  69. }
  70. return 0;
  71. }
  72. static int fsl_msi_check_device(struct pci_dev *pdev, int nvec, int type)
  73. {
  74. if (type == PCI_CAP_ID_MSIX)
  75. pr_debug("fslmsi: MSI-X untested, trying anyway.\n");
  76. return 0;
  77. }
  78. static void fsl_teardown_msi_irqs(struct pci_dev *pdev)
  79. {
  80. struct msi_desc *entry;
  81. struct fsl_msi *msi_data = fsl_msi;
  82. list_for_each_entry(entry, &pdev->msi_list, list) {
  83. if (entry->irq == NO_IRQ)
  84. continue;
  85. set_irq_msi(entry->irq, NULL);
  86. msi_bitmap_free_hwirqs(&msi_data->bitmap,
  87. virq_to_hw(entry->irq), 1);
  88. irq_dispose_mapping(entry->irq);
  89. }
  90. return;
  91. }
  92. static void fsl_compose_msi_msg(struct pci_dev *pdev, int hwirq,
  93. struct msi_msg *msg)
  94. {
  95. struct fsl_msi *msi_data = fsl_msi;
  96. msg->address_lo = msi_data->msi_addr_lo;
  97. msg->address_hi = msi_data->msi_addr_hi;
  98. msg->data = hwirq;
  99. pr_debug("%s: allocated srs: %d, ibs: %d\n",
  100. __func__, hwirq / IRQS_PER_MSI_REG, hwirq % IRQS_PER_MSI_REG);
  101. }
  102. static int fsl_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
  103. {
  104. int rc, hwirq;
  105. unsigned int virq;
  106. struct msi_desc *entry;
  107. struct msi_msg msg;
  108. struct fsl_msi *msi_data = fsl_msi;
  109. list_for_each_entry(entry, &pdev->msi_list, list) {
  110. hwirq = msi_bitmap_alloc_hwirqs(&msi_data->bitmap, 1);
  111. if (hwirq < 0) {
  112. rc = hwirq;
  113. pr_debug("%s: fail allocating msi interrupt\n",
  114. __func__);
  115. goto out_free;
  116. }
  117. virq = irq_create_mapping(msi_data->irqhost, hwirq);
  118. if (virq == NO_IRQ) {
  119. pr_debug("%s: fail mapping hwirq 0x%x\n",
  120. __func__, hwirq);
  121. msi_bitmap_free_hwirqs(&msi_data->bitmap, hwirq, 1);
  122. rc = -ENOSPC;
  123. goto out_free;
  124. }
  125. set_irq_msi(virq, entry);
  126. fsl_compose_msi_msg(pdev, hwirq, &msg);
  127. write_msi_msg(virq, &msg);
  128. }
  129. return 0;
  130. out_free:
  131. return rc;
  132. }
  133. static void fsl_msi_cascade(unsigned int irq, struct irq_desc *desc)
  134. {
  135. unsigned int cascade_irq;
  136. struct fsl_msi *msi_data = fsl_msi;
  137. int msir_index = -1;
  138. u32 msir_value = 0;
  139. u32 intr_index;
  140. u32 have_shift = 0;
  141. spin_lock(&desc->lock);
  142. if ((msi_data->feature & FSL_PIC_IP_MASK) == FSL_PIC_IP_IPIC) {
  143. if (desc->chip->mask_ack)
  144. desc->chip->mask_ack(irq);
  145. else {
  146. desc->chip->mask(irq);
  147. desc->chip->ack(irq);
  148. }
  149. }
  150. if (unlikely(desc->status & IRQ_INPROGRESS))
  151. goto unlock;
  152. msir_index = (int)desc->handler_data;
  153. if (msir_index >= NR_MSI_REG)
  154. cascade_irq = NO_IRQ;
  155. desc->status |= IRQ_INPROGRESS;
  156. switch (fsl_msi->feature & FSL_PIC_IP_MASK) {
  157. case FSL_PIC_IP_MPIC:
  158. msir_value = fsl_msi_read(msi_data->msi_regs,
  159. msir_index * 0x10);
  160. break;
  161. case FSL_PIC_IP_IPIC:
  162. msir_value = fsl_msi_read(msi_data->msi_regs, msir_index * 0x4);
  163. break;
  164. }
  165. while (msir_value) {
  166. intr_index = ffs(msir_value) - 1;
  167. cascade_irq = irq_linear_revmap(msi_data->irqhost,
  168. msir_index * IRQS_PER_MSI_REG +
  169. intr_index + have_shift);
  170. if (cascade_irq != NO_IRQ)
  171. generic_handle_irq(cascade_irq);
  172. have_shift += intr_index + 1;
  173. msir_value = msir_value >> (intr_index + 1);
  174. }
  175. desc->status &= ~IRQ_INPROGRESS;
  176. switch (msi_data->feature & FSL_PIC_IP_MASK) {
  177. case FSL_PIC_IP_MPIC:
  178. desc->chip->eoi(irq);
  179. break;
  180. case FSL_PIC_IP_IPIC:
  181. if (!(desc->status & IRQ_DISABLED) && desc->chip->unmask)
  182. desc->chip->unmask(irq);
  183. break;
  184. }
  185. unlock:
  186. spin_unlock(&desc->lock);
  187. }
  188. static int __devinit fsl_of_msi_probe(struct of_device *dev,
  189. const struct of_device_id *match)
  190. {
  191. struct fsl_msi *msi;
  192. struct resource res;
  193. int err, i, count;
  194. int rc;
  195. int virt_msir;
  196. const u32 *p;
  197. struct fsl_msi_feature *features = match->data;
  198. printk(KERN_DEBUG "Setting up Freescale MSI support\n");
  199. msi = kzalloc(sizeof(struct fsl_msi), GFP_KERNEL);
  200. if (!msi) {
  201. dev_err(&dev->dev, "No memory for MSI structure\n");
  202. err = -ENOMEM;
  203. goto error_out;
  204. }
  205. msi->irqhost = irq_alloc_host(dev->node, IRQ_HOST_MAP_LINEAR,
  206. NR_MSI_IRQS, &fsl_msi_host_ops, 0);
  207. if (msi->irqhost == NULL) {
  208. dev_err(&dev->dev, "No memory for MSI irqhost\n");
  209. err = -ENOMEM;
  210. goto error_out;
  211. }
  212. /* Get the MSI reg base */
  213. err = of_address_to_resource(dev->node, 0, &res);
  214. if (err) {
  215. dev_err(&dev->dev, "%s resource error!\n",
  216. dev->node->full_name);
  217. goto error_out;
  218. }
  219. msi->msi_regs = ioremap(res.start, res.end - res.start + 1);
  220. if (!msi->msi_regs) {
  221. dev_err(&dev->dev, "ioremap problem failed\n");
  222. goto error_out;
  223. }
  224. msi->feature = features->fsl_pic_ip;
  225. msi->irqhost->host_data = msi;
  226. msi->msi_addr_hi = 0x0;
  227. msi->msi_addr_lo = res.start + features->msiir_offset;
  228. rc = fsl_msi_init_allocator(msi);
  229. if (rc) {
  230. dev_err(&dev->dev, "Error allocating MSI bitmap\n");
  231. goto error_out;
  232. }
  233. p = of_get_property(dev->node, "interrupts", &count);
  234. if (!p) {
  235. dev_err(&dev->dev, "no interrupts property found on %s\n",
  236. dev->node->full_name);
  237. err = -ENODEV;
  238. goto error_out;
  239. }
  240. if (count % 8 != 0) {
  241. dev_err(&dev->dev, "Malformed interrupts property on %s\n",
  242. dev->node->full_name);
  243. err = -EINVAL;
  244. goto error_out;
  245. }
  246. count /= sizeof(u32);
  247. for (i = 0; i < count / 2; i++) {
  248. if (i > NR_MSI_REG)
  249. break;
  250. virt_msir = irq_of_parse_and_map(dev->node, i);
  251. if (virt_msir != NO_IRQ) {
  252. set_irq_data(virt_msir, (void *)i);
  253. set_irq_chained_handler(virt_msir, fsl_msi_cascade);
  254. }
  255. }
  256. fsl_msi = msi;
  257. WARN_ON(ppc_md.setup_msi_irqs);
  258. ppc_md.setup_msi_irqs = fsl_setup_msi_irqs;
  259. ppc_md.teardown_msi_irqs = fsl_teardown_msi_irqs;
  260. ppc_md.msi_check_device = fsl_msi_check_device;
  261. return 0;
  262. error_out:
  263. kfree(msi);
  264. return err;
  265. }
  266. static const struct fsl_msi_feature mpic_msi_feature = {
  267. .fsl_pic_ip = FSL_PIC_IP_MPIC,
  268. .msiir_offset = 0x140,
  269. };
  270. static const struct fsl_msi_feature ipic_msi_feature = {
  271. .fsl_pic_ip = FSL_PIC_IP_IPIC,
  272. .msiir_offset = 0x38,
  273. };
  274. static const struct of_device_id fsl_of_msi_ids[] = {
  275. {
  276. .compatible = "fsl,mpic-msi",
  277. .data = (void *)&mpic_msi_feature,
  278. },
  279. {
  280. .compatible = "fsl,ipic-msi",
  281. .data = (void *)&ipic_msi_feature,
  282. },
  283. {}
  284. };
  285. static struct of_platform_driver fsl_of_msi_driver = {
  286. .name = "fsl-msi",
  287. .match_table = fsl_of_msi_ids,
  288. .probe = fsl_of_msi_probe,
  289. };
  290. static __init int fsl_of_msi_init(void)
  291. {
  292. return of_register_platform_driver(&fsl_of_msi_driver);
  293. }
  294. subsys_initcall(fsl_of_msi_init);