msi.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. /*
  60. * If the RTAS call succeeded, check the number of irqs is actually
  61. * what we asked for. If not, return an error.
  62. */
  63. if (rc == 0 && rtas_ret[0] != num_irqs)
  64. rc = -ENOSPC;
  65. pr_debug("rtas_msi: ibm,change_msi(func=%d,num=%d), got %d rc = %d\n",
  66. func, num_irqs, rtas_ret[0], rc);
  67. return rc;
  68. }
  69. static void rtas_disable_msi(struct pci_dev *pdev)
  70. {
  71. struct pci_dn *pdn;
  72. pdn = get_pdn(pdev);
  73. if (!pdn)
  74. return;
  75. if (rtas_change_msi(pdn, RTAS_CHANGE_FN, 0))
  76. pr_debug("rtas_msi: Setting MSIs to 0 failed!\n");
  77. }
  78. static int rtas_query_irq_number(struct pci_dn *pdn, int offset)
  79. {
  80. u32 addr, rtas_ret[2];
  81. unsigned long buid;
  82. int rc;
  83. addr = rtas_config_addr(pdn->busno, pdn->devfn, 0);
  84. buid = pdn->phb->buid;
  85. do {
  86. rc = rtas_call(query_token, 4, 3, rtas_ret, addr,
  87. BUID_HI(buid), BUID_LO(buid), offset);
  88. } while (rtas_busy_delay(rc));
  89. if (rc) {
  90. pr_debug("rtas_msi: error (%d) querying source number\n", rc);
  91. return rc;
  92. }
  93. return rtas_ret[0];
  94. }
  95. static void rtas_teardown_msi_irqs(struct pci_dev *pdev)
  96. {
  97. struct msi_desc *entry;
  98. list_for_each_entry(entry, &pdev->msi_list, list) {
  99. if (entry->irq == NO_IRQ)
  100. continue;
  101. set_irq_msi(entry->irq, NULL);
  102. irq_dispose_mapping(entry->irq);
  103. }
  104. rtas_disable_msi(pdev);
  105. }
  106. static int check_req_msi(struct pci_dev *pdev, int nvec)
  107. {
  108. struct device_node *dn;
  109. struct pci_dn *pdn;
  110. const u32 *req_msi;
  111. pdn = get_pdn(pdev);
  112. if (!pdn)
  113. return -ENODEV;
  114. dn = pdn->node;
  115. req_msi = of_get_property(dn, "ibm,req#msi", NULL);
  116. if (!req_msi) {
  117. pr_debug("rtas_msi: No ibm,req#msi on %s\n", dn->full_name);
  118. return -ENOENT;
  119. }
  120. if (*req_msi < nvec) {
  121. pr_debug("rtas_msi: ibm,req#msi requests < %d MSIs\n", nvec);
  122. return -ENOSPC;
  123. }
  124. return 0;
  125. }
  126. static int rtas_msi_check_device(struct pci_dev *pdev, int nvec, int type)
  127. {
  128. if (type == PCI_CAP_ID_MSIX)
  129. pr_debug("rtas_msi: MSI-X untested, trying anyway.\n");
  130. return check_req_msi(pdev, nvec);
  131. }
  132. static int rtas_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
  133. {
  134. struct pci_dn *pdn;
  135. int hwirq, virq, i, rc;
  136. struct msi_desc *entry;
  137. pdn = get_pdn(pdev);
  138. if (!pdn)
  139. return -ENODEV;
  140. /*
  141. * Try the new more explicit firmware interface, if that fails fall
  142. * back to the old interface. The old interface is known to never
  143. * return MSI-Xs.
  144. */
  145. if (type == PCI_CAP_ID_MSI) {
  146. rc = rtas_change_msi(pdn, RTAS_CHANGE_MSI_FN, nvec);
  147. if (rc) {
  148. pr_debug("rtas_msi: trying the old firmware call.\n");
  149. rc = rtas_change_msi(pdn, RTAS_CHANGE_FN, nvec);
  150. }
  151. } else
  152. rc = rtas_change_msi(pdn, RTAS_CHANGE_MSIX_FN, nvec);
  153. if (rc) {
  154. pr_debug("rtas_msi: rtas_change_msi() failed\n");
  155. return rc;
  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. pr_debug("rtas_msi: error (%d) getting hwirq\n", rc);
  162. return hwirq;
  163. }
  164. virq = irq_create_mapping(NULL, hwirq);
  165. if (virq == NO_IRQ) {
  166. pr_debug("rtas_msi: Failed mapping hwirq %d\n", hwirq);
  167. return -ENOSPC;
  168. }
  169. dev_dbg(&pdev->dev, "rtas_msi: allocated virq %d\n", virq);
  170. set_irq_msi(virq, entry);
  171. unmask_msi_irq(virq);
  172. }
  173. return 0;
  174. }
  175. static void rtas_msi_pci_irq_fixup(struct pci_dev *pdev)
  176. {
  177. /* No LSI -> leave MSIs (if any) configured */
  178. if (pdev->irq == NO_IRQ) {
  179. dev_dbg(&pdev->dev, "rtas_msi: no LSI, nothing to do.\n");
  180. return;
  181. }
  182. /* No MSI -> MSIs can't have been assigned by fw, leave LSI */
  183. if (check_req_msi(pdev, 1)) {
  184. dev_dbg(&pdev->dev, "rtas_msi: no req#msi, nothing to do.\n");
  185. return;
  186. }
  187. dev_dbg(&pdev->dev, "rtas_msi: disabling existing MSI.\n");
  188. rtas_disable_msi(pdev);
  189. }
  190. static int rtas_msi_init(void)
  191. {
  192. query_token = rtas_token("ibm,query-interrupt-source-number");
  193. change_token = rtas_token("ibm,change-msi");
  194. if ((query_token == RTAS_UNKNOWN_SERVICE) ||
  195. (change_token == RTAS_UNKNOWN_SERVICE)) {
  196. pr_debug("rtas_msi: no RTAS tokens, no MSI support.\n");
  197. return -1;
  198. }
  199. pr_debug("rtas_msi: Registering RTAS MSI callbacks.\n");
  200. WARN_ON(ppc_md.setup_msi_irqs);
  201. ppc_md.setup_msi_irqs = rtas_setup_msi_irqs;
  202. ppc_md.teardown_msi_irqs = rtas_teardown_msi_irqs;
  203. ppc_md.msi_check_device = rtas_msi_check_device;
  204. WARN_ON(ppc_md.pci_irq_fixup);
  205. ppc_md.pci_irq_fixup = rtas_msi_pci_irq_fixup;
  206. return 0;
  207. }
  208. arch_initcall(rtas_msi_init);