msi.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright 2006 Jake Moilanen <moilanen@austin.ibm.com>, IBM Corp.
  3. * Copyright 2006-2007 Michael Ellerman, IBM Corp.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; version 2 of the
  8. * License.
  9. *
  10. */
  11. #include <linux/device.h>
  12. #include <linux/irq.h>
  13. #include <linux/msi.h>
  14. #include <asm/rtas.h>
  15. #include <asm/hw_irq.h>
  16. #include <asm/ppc-pci.h>
  17. static int query_token, change_token;
  18. #define RTAS_QUERY_FN 0
  19. #define RTAS_CHANGE_FN 1
  20. #define RTAS_RESET_FN 2
  21. #define RTAS_CHANGE_MSI_FN 3
  22. #define RTAS_CHANGE_MSIX_FN 4
  23. static struct pci_dn *get_pdn(struct pci_dev *pdev)
  24. {
  25. struct device_node *dn;
  26. struct pci_dn *pdn;
  27. dn = pci_device_to_OF_node(pdev);
  28. if (!dn) {
  29. dev_dbg(&pdev->dev, "rtas_msi: No OF device node\n");
  30. return NULL;
  31. }
  32. pdn = PCI_DN(dn);
  33. if (!pdn) {
  34. dev_dbg(&pdev->dev, "rtas_msi: No PCI DN\n");
  35. return NULL;
  36. }
  37. return pdn;
  38. }
  39. /* RTAS Helpers */
  40. static int rtas_change_msi(struct pci_dn *pdn, u32 func, u32 num_irqs)
  41. {
  42. u32 addr, seq_num, rtas_ret[3];
  43. unsigned long buid;
  44. int rc;
  45. addr = rtas_config_addr(pdn->busno, pdn->devfn, 0);
  46. buid = pdn->phb->buid;
  47. seq_num = 1;
  48. do {
  49. if (func == RTAS_CHANGE_MSI_FN || func == RTAS_CHANGE_MSIX_FN)
  50. rc = rtas_call(change_token, 6, 4, rtas_ret, addr,
  51. BUID_HI(buid), BUID_LO(buid),
  52. func, num_irqs, seq_num);
  53. else
  54. rc = rtas_call(change_token, 6, 3, rtas_ret, addr,
  55. BUID_HI(buid), BUID_LO(buid),
  56. func, num_irqs, seq_num);
  57. seq_num = rtas_ret[1];
  58. } while (rtas_busy_delay(rc));
  59. if (rc == 0) /* Success */
  60. rc = rtas_ret[0];
  61. pr_debug("rtas_msi: ibm,change_msi(func=%d,num=%d) = (%d)\n",
  62. func, num_irqs, rc);
  63. return rc;
  64. }
  65. static void rtas_disable_msi(struct pci_dev *pdev)
  66. {
  67. struct pci_dn *pdn;
  68. pdn = get_pdn(pdev);
  69. if (!pdn)
  70. return;
  71. if (rtas_change_msi(pdn, RTAS_CHANGE_FN, 0) != 0)
  72. pr_debug("rtas_msi: Setting MSIs to 0 failed!\n");
  73. }
  74. static int rtas_query_irq_number(struct pci_dn *pdn, int offset)
  75. {
  76. u32 addr, rtas_ret[2];
  77. unsigned long buid;
  78. int rc;
  79. addr = rtas_config_addr(pdn->busno, pdn->devfn, 0);
  80. buid = pdn->phb->buid;
  81. do {
  82. rc = rtas_call(query_token, 4, 3, rtas_ret, addr,
  83. BUID_HI(buid), BUID_LO(buid), offset);
  84. } while (rtas_busy_delay(rc));
  85. if (rc) {
  86. pr_debug("rtas_msi: error (%d) querying source number\n", rc);
  87. return rc;
  88. }
  89. return rtas_ret[0];
  90. }
  91. static void rtas_teardown_msi_irqs(struct pci_dev *pdev)
  92. {
  93. struct msi_desc *entry;
  94. list_for_each_entry(entry, &pdev->msi_list, list) {
  95. if (entry->irq == NO_IRQ)
  96. continue;
  97. set_irq_msi(entry->irq, NULL);
  98. irq_dispose_mapping(entry->irq);
  99. }
  100. rtas_disable_msi(pdev);
  101. }
  102. static int check_req_msi(struct pci_dev *pdev, int nvec)
  103. {
  104. struct device_node *dn;
  105. struct pci_dn *pdn;
  106. const u32 *req_msi;
  107. pdn = get_pdn(pdev);
  108. if (!pdn)
  109. return -ENODEV;
  110. dn = pdn->node;
  111. req_msi = of_get_property(dn, "ibm,req#msi", NULL);
  112. if (!req_msi) {
  113. pr_debug("rtas_msi: No ibm,req#msi on %s\n", dn->full_name);
  114. return -ENOENT;
  115. }
  116. if (*req_msi < nvec) {
  117. pr_debug("rtas_msi: ibm,req#msi requests < %d MSIs\n", nvec);
  118. return -ENOSPC;
  119. }
  120. return 0;
  121. }
  122. static int rtas_msi_check_device(struct pci_dev *pdev, int nvec, int type)
  123. {
  124. if (type == PCI_CAP_ID_MSIX)
  125. pr_debug("rtas_msi: MSI-X untested, trying anyway.\n");
  126. return check_req_msi(pdev, nvec);
  127. }
  128. static int rtas_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
  129. {
  130. struct pci_dn *pdn;
  131. int hwirq, virq, i, rc;
  132. struct msi_desc *entry;
  133. pdn = get_pdn(pdev);
  134. if (!pdn)
  135. return -ENODEV;
  136. /*
  137. * Try the new more explicit firmware interface, if that fails fall
  138. * back to the old interface. The old interface is known to never
  139. * return MSI-Xs.
  140. */
  141. if (type == PCI_CAP_ID_MSI) {
  142. rc = rtas_change_msi(pdn, RTAS_CHANGE_MSI_FN, nvec);
  143. if (rc != nvec) {
  144. pr_debug("rtas_msi: trying the old firmware call.\n");
  145. rc = rtas_change_msi(pdn, RTAS_CHANGE_FN, nvec);
  146. }
  147. } else
  148. rc = rtas_change_msi(pdn, RTAS_CHANGE_MSIX_FN, nvec);
  149. if (rc != nvec) {
  150. pr_debug("rtas_msi: rtas_change_msi() failed\n");
  151. /*
  152. * In case of an error it's not clear whether the device is
  153. * left with MSI enabled or not, so we explicitly disable.
  154. */
  155. goto out_free;
  156. }
  157. i = 0;
  158. list_for_each_entry(entry, &pdev->msi_list, list) {
  159. hwirq = rtas_query_irq_number(pdn, i);
  160. if (hwirq < 0) {
  161. rc = hwirq;
  162. pr_debug("rtas_msi: error (%d) getting hwirq\n", rc);
  163. goto out_free;
  164. }
  165. virq = irq_create_mapping(NULL, hwirq);
  166. if (virq == NO_IRQ) {
  167. pr_debug("rtas_msi: Failed mapping hwirq %d\n", hwirq);
  168. rc = -ENOSPC;
  169. goto out_free;
  170. }
  171. dev_dbg(&pdev->dev, "rtas_msi: allocated virq %d\n", virq);
  172. set_irq_msi(virq, entry);
  173. unmask_msi_irq(virq);
  174. }
  175. return 0;
  176. out_free:
  177. rtas_teardown_msi_irqs(pdev);
  178. return rc;
  179. }
  180. static void rtas_msi_pci_irq_fixup(struct pci_dev *pdev)
  181. {
  182. /* No LSI -> leave MSIs (if any) configured */
  183. if (pdev->irq == NO_IRQ) {
  184. dev_dbg(&pdev->dev, "rtas_msi: no LSI, nothing to do.\n");
  185. return;
  186. }
  187. /* No MSI -> MSIs can't have been assigned by fw, leave LSI */
  188. if (check_req_msi(pdev, 1)) {
  189. dev_dbg(&pdev->dev, "rtas_msi: no req#msi, nothing to do.\n");
  190. return;
  191. }
  192. dev_dbg(&pdev->dev, "rtas_msi: disabling existing MSI.\n");
  193. rtas_disable_msi(pdev);
  194. }
  195. static int rtas_msi_init(void)
  196. {
  197. query_token = rtas_token("ibm,query-interrupt-source-number");
  198. change_token = rtas_token("ibm,change-msi");
  199. if ((query_token == RTAS_UNKNOWN_SERVICE) ||
  200. (change_token == RTAS_UNKNOWN_SERVICE)) {
  201. pr_debug("rtas_msi: no RTAS tokens, no MSI support.\n");
  202. return -1;
  203. }
  204. pr_debug("rtas_msi: Registering RTAS MSI callbacks.\n");
  205. WARN_ON(ppc_md.setup_msi_irqs);
  206. ppc_md.setup_msi_irqs = rtas_setup_msi_irqs;
  207. ppc_md.teardown_msi_irqs = rtas_teardown_msi_irqs;
  208. ppc_md.msi_check_device = rtas_msi_check_device;
  209. WARN_ON(ppc_md.pci_irq_fixup);
  210. ppc_md.pci_irq_fixup = rtas_msi_pci_irq_fixup;
  211. return 0;
  212. }
  213. arch_initcall(rtas_msi_init);