fsl_msi.c 9.0 KB

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