fsl_msi.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. * Copyright (C) 2007-2010 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 "fsl_msi.h"
  27. LIST_HEAD(msi_head);
  28. struct fsl_msi_feature {
  29. u32 fsl_pic_ip;
  30. u32 msiir_offset;
  31. };
  32. struct fsl_msi_cascade_data {
  33. struct fsl_msi *msi_data;
  34. int index;
  35. };
  36. static inline u32 fsl_msi_read(u32 __iomem *base, unsigned int reg)
  37. {
  38. return in_be32(base + (reg >> 2));
  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. .name = "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 fsl_msi *msi_data = h->host_data;
  57. struct irq_chip *chip = &fsl_msi_chip;
  58. irq_to_desc(virq)->status |= IRQ_TYPE_EDGE_FALLING;
  59. set_irq_chip_data(virq, msi_data);
  60. set_irq_chip_and_handler(virq, chip, handle_edge_irq);
  61. return 0;
  62. }
  63. static struct irq_host_ops fsl_msi_host_ops = {
  64. .map = fsl_msi_host_map,
  65. };
  66. static int fsl_msi_init_allocator(struct fsl_msi *msi_data)
  67. {
  68. int rc;
  69. rc = msi_bitmap_alloc(&msi_data->bitmap, NR_MSI_IRQS,
  70. msi_data->irqhost->of_node);
  71. if (rc)
  72. return rc;
  73. rc = msi_bitmap_reserve_dt_hwirqs(&msi_data->bitmap);
  74. if (rc < 0) {
  75. msi_bitmap_free(&msi_data->bitmap);
  76. return rc;
  77. }
  78. return 0;
  79. }
  80. static int fsl_msi_check_device(struct pci_dev *pdev, int nvec, int type)
  81. {
  82. if (type == PCI_CAP_ID_MSIX)
  83. pr_debug("fslmsi: MSI-X untested, trying anyway.\n");
  84. return 0;
  85. }
  86. static void fsl_teardown_msi_irqs(struct pci_dev *pdev)
  87. {
  88. struct msi_desc *entry;
  89. struct fsl_msi *msi_data;
  90. list_for_each_entry(entry, &pdev->msi_list, list) {
  91. if (entry->irq == NO_IRQ)
  92. continue;
  93. msi_data = get_irq_data(entry->irq);
  94. set_irq_msi(entry->irq, NULL);
  95. msi_bitmap_free_hwirqs(&msi_data->bitmap,
  96. virq_to_hw(entry->irq), 1);
  97. irq_dispose_mapping(entry->irq);
  98. }
  99. return;
  100. }
  101. static void fsl_compose_msi_msg(struct pci_dev *pdev, int hwirq,
  102. struct msi_msg *msg,
  103. struct fsl_msi *fsl_msi_data)
  104. {
  105. struct fsl_msi *msi_data = fsl_msi_data;
  106. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  107. u32 base = 0;
  108. pci_bus_read_config_dword(hose->bus,
  109. PCI_DEVFN(0, 0), PCI_BASE_ADDRESS_0, &base);
  110. msg->address_lo = msi_data->msi_addr_lo + base;
  111. msg->address_hi = msi_data->msi_addr_hi;
  112. msg->data = hwirq;
  113. pr_debug("%s: allocated srs: %d, ibs: %d\n",
  114. __func__, hwirq / IRQS_PER_MSI_REG, hwirq % IRQS_PER_MSI_REG);
  115. }
  116. static int fsl_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
  117. {
  118. int rc, hwirq = -ENOMEM;
  119. unsigned int virq;
  120. struct msi_desc *entry;
  121. struct msi_msg msg;
  122. struct fsl_msi *msi_data;
  123. list_for_each_entry(entry, &pdev->msi_list, list) {
  124. list_for_each_entry(msi_data, &msi_head, list) {
  125. hwirq = msi_bitmap_alloc_hwirqs(&msi_data->bitmap, 1);
  126. if (hwirq >= 0)
  127. break;
  128. }
  129. if (hwirq < 0) {
  130. rc = hwirq;
  131. pr_debug("%s: fail allocating msi interrupt\n",
  132. __func__);
  133. goto out_free;
  134. }
  135. virq = irq_create_mapping(msi_data->irqhost, hwirq);
  136. if (virq == NO_IRQ) {
  137. pr_debug("%s: fail mapping hwirq 0x%x\n",
  138. __func__, hwirq);
  139. msi_bitmap_free_hwirqs(&msi_data->bitmap, hwirq, 1);
  140. rc = -ENOSPC;
  141. goto out_free;
  142. }
  143. set_irq_data(virq, msi_data);
  144. set_irq_msi(virq, entry);
  145. fsl_compose_msi_msg(pdev, hwirq, &msg, msi_data);
  146. write_msi_msg(virq, &msg);
  147. }
  148. return 0;
  149. out_free:
  150. /* free by the caller of this function */
  151. return rc;
  152. }
  153. static void fsl_msi_cascade(unsigned int irq, struct irq_desc *desc)
  154. {
  155. unsigned int cascade_irq;
  156. struct fsl_msi *msi_data;
  157. int msir_index = -1;
  158. u32 msir_value = 0;
  159. u32 intr_index;
  160. u32 have_shift = 0;
  161. struct fsl_msi_cascade_data *cascade_data;
  162. cascade_data = (struct fsl_msi_cascade_data *)get_irq_data(irq);
  163. msi_data = cascade_data->msi_data;
  164. raw_spin_lock(&desc->lock);
  165. if ((msi_data->feature & FSL_PIC_IP_MASK) == FSL_PIC_IP_IPIC) {
  166. if (desc->chip->mask_ack)
  167. desc->chip->mask_ack(irq);
  168. else {
  169. desc->chip->mask(irq);
  170. desc->chip->ack(irq);
  171. }
  172. }
  173. if (unlikely(desc->status & IRQ_INPROGRESS))
  174. goto unlock;
  175. msir_index = cascade_data->index;
  176. if (msir_index >= NR_MSI_REG)
  177. cascade_irq = NO_IRQ;
  178. desc->status |= IRQ_INPROGRESS;
  179. switch (msi_data->feature & FSL_PIC_IP_MASK) {
  180. case FSL_PIC_IP_MPIC:
  181. msir_value = fsl_msi_read(msi_data->msi_regs,
  182. msir_index * 0x10);
  183. break;
  184. case FSL_PIC_IP_IPIC:
  185. msir_value = fsl_msi_read(msi_data->msi_regs, msir_index * 0x4);
  186. break;
  187. }
  188. while (msir_value) {
  189. intr_index = ffs(msir_value) - 1;
  190. cascade_irq = irq_linear_revmap(msi_data->irqhost,
  191. msir_index * IRQS_PER_MSI_REG +
  192. intr_index + have_shift);
  193. if (cascade_irq != NO_IRQ)
  194. generic_handle_irq(cascade_irq);
  195. have_shift += intr_index + 1;
  196. msir_value = msir_value >> (intr_index + 1);
  197. }
  198. desc->status &= ~IRQ_INPROGRESS;
  199. switch (msi_data->feature & FSL_PIC_IP_MASK) {
  200. case FSL_PIC_IP_MPIC:
  201. desc->chip->eoi(irq);
  202. break;
  203. case FSL_PIC_IP_IPIC:
  204. if (!(desc->status & IRQ_DISABLED) && desc->chip->unmask)
  205. desc->chip->unmask(irq);
  206. break;
  207. }
  208. unlock:
  209. raw_spin_unlock(&desc->lock);
  210. }
  211. static int fsl_of_msi_remove(struct platform_device *ofdev)
  212. {
  213. struct fsl_msi *msi = ofdev->dev.platform_data;
  214. int virq, i;
  215. struct fsl_msi_cascade_data *cascade_data;
  216. if (msi->list.prev != NULL)
  217. list_del(&msi->list);
  218. for (i = 0; i < NR_MSI_REG; i++) {
  219. virq = msi->msi_virqs[i];
  220. if (virq != NO_IRQ) {
  221. cascade_data = get_irq_data(virq);
  222. kfree(cascade_data);
  223. irq_dispose_mapping(virq);
  224. }
  225. }
  226. if (msi->bitmap.bitmap)
  227. msi_bitmap_free(&msi->bitmap);
  228. iounmap(msi->msi_regs);
  229. kfree(msi);
  230. return 0;
  231. }
  232. static int __devinit fsl_of_msi_probe(struct platform_device *dev,
  233. const struct of_device_id *match)
  234. {
  235. struct fsl_msi *msi;
  236. struct resource res;
  237. int err, i, count;
  238. int rc;
  239. int virt_msir;
  240. const u32 *p;
  241. struct fsl_msi_feature *features = match->data;
  242. struct fsl_msi_cascade_data *cascade_data = NULL;
  243. int len;
  244. u32 offset;
  245. printk(KERN_DEBUG "Setting up Freescale MSI support\n");
  246. msi = kzalloc(sizeof(struct fsl_msi), GFP_KERNEL);
  247. if (!msi) {
  248. dev_err(&dev->dev, "No memory for MSI structure\n");
  249. return -ENOMEM;
  250. }
  251. dev->dev.platform_data = msi;
  252. msi->irqhost = irq_alloc_host(dev->dev.of_node, IRQ_HOST_MAP_LINEAR,
  253. NR_MSI_IRQS, &fsl_msi_host_ops, 0);
  254. if (msi->irqhost == NULL) {
  255. dev_err(&dev->dev, "No memory for MSI irqhost\n");
  256. err = -ENOMEM;
  257. goto error_out;
  258. }
  259. /* Get the MSI reg base */
  260. err = of_address_to_resource(dev->dev.of_node, 0, &res);
  261. if (err) {
  262. dev_err(&dev->dev, "%s resource error!\n",
  263. dev->dev.of_node->full_name);
  264. goto error_out;
  265. }
  266. msi->msi_regs = ioremap(res.start, res.end - res.start + 1);
  267. if (!msi->msi_regs) {
  268. dev_err(&dev->dev, "ioremap problem failed\n");
  269. goto error_out;
  270. }
  271. msi->feature = features->fsl_pic_ip;
  272. msi->irqhost->host_data = msi;
  273. msi->msi_addr_hi = 0x0;
  274. msi->msi_addr_lo = features->msiir_offset + (res.start & 0xfffff);
  275. rc = fsl_msi_init_allocator(msi);
  276. if (rc) {
  277. dev_err(&dev->dev, "Error allocating MSI bitmap\n");
  278. goto error_out;
  279. }
  280. p = of_get_property(dev->dev.of_node, "interrupts", &count);
  281. if (!p) {
  282. dev_err(&dev->dev, "no interrupts property found on %s\n",
  283. dev->dev.of_node->full_name);
  284. err = -ENODEV;
  285. goto error_out;
  286. }
  287. if (count % 8 != 0) {
  288. dev_err(&dev->dev, "Malformed interrupts property on %s\n",
  289. dev->dev.of_node->full_name);
  290. err = -EINVAL;
  291. goto error_out;
  292. }
  293. offset = 0;
  294. p = of_get_property(dev->dev.of_node, "msi-available-ranges", &len);
  295. if (p)
  296. offset = *p / IRQS_PER_MSI_REG;
  297. count /= sizeof(u32);
  298. for (i = 0; i < min(count / 2, NR_MSI_REG); i++) {
  299. virt_msir = irq_of_parse_and_map(dev->dev.of_node, i);
  300. if (virt_msir != NO_IRQ) {
  301. cascade_data = kzalloc(
  302. sizeof(struct fsl_msi_cascade_data),
  303. GFP_KERNEL);
  304. if (!cascade_data) {
  305. dev_err(&dev->dev,
  306. "No memory for MSI cascade data\n");
  307. err = -ENOMEM;
  308. goto error_out;
  309. }
  310. msi->msi_virqs[i] = virt_msir;
  311. cascade_data->index = i + offset;
  312. cascade_data->msi_data = msi;
  313. set_irq_data(virt_msir, (void *)cascade_data);
  314. set_irq_chained_handler(virt_msir, fsl_msi_cascade);
  315. }
  316. }
  317. list_add_tail(&msi->list, &msi_head);
  318. /* The multiple setting ppc_md.setup_msi_irqs will not harm things */
  319. if (!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. } else if (ppc_md.setup_msi_irqs != fsl_setup_msi_irqs) {
  324. dev_err(&dev->dev, "Different MSI driver already installed!\n");
  325. err = -ENODEV;
  326. goto error_out;
  327. }
  328. return 0;
  329. error_out:
  330. fsl_of_msi_remove(dev);
  331. return err;
  332. }
  333. static const struct fsl_msi_feature mpic_msi_feature = {
  334. .fsl_pic_ip = FSL_PIC_IP_MPIC,
  335. .msiir_offset = 0x140,
  336. };
  337. static const struct fsl_msi_feature ipic_msi_feature = {
  338. .fsl_pic_ip = FSL_PIC_IP_IPIC,
  339. .msiir_offset = 0x38,
  340. };
  341. static const struct of_device_id fsl_of_msi_ids[] = {
  342. {
  343. .compatible = "fsl,mpic-msi",
  344. .data = (void *)&mpic_msi_feature,
  345. },
  346. {
  347. .compatible = "fsl,ipic-msi",
  348. .data = (void *)&ipic_msi_feature,
  349. },
  350. {}
  351. };
  352. static struct of_platform_driver fsl_of_msi_driver = {
  353. .driver = {
  354. .name = "fsl-msi",
  355. .owner = THIS_MODULE,
  356. .of_match_table = fsl_of_msi_ids,
  357. },
  358. .probe = fsl_of_msi_probe,
  359. .remove = fsl_of_msi_remove,
  360. };
  361. static __init int fsl_of_msi_init(void)
  362. {
  363. return of_register_platform_driver(&fsl_of_msi_driver);
  364. }
  365. subsys_initcall(fsl_of_msi_init);