fsl_msi.c 9.6 KB

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