fsl_msi.c 9.8 KB

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