msi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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, return the number of irqs allocated.
  61. * If not, make sure we return a negative error code.
  62. */
  63. if (rc == 0)
  64. rc = rtas_ret[0];
  65. else if (rc > 0)
  66. rc = -rc;
  67. pr_debug("rtas_msi: ibm,change_msi(func=%d,num=%d), got %d rc = %d\n",
  68. func, num_irqs, rtas_ret[0], rc);
  69. return rc;
  70. }
  71. static void rtas_disable_msi(struct pci_dev *pdev)
  72. {
  73. struct pci_dn *pdn;
  74. pdn = get_pdn(pdev);
  75. if (!pdn)
  76. return;
  77. if (rtas_change_msi(pdn, RTAS_CHANGE_FN, 0) != 0)
  78. pr_debug("rtas_msi: Setting MSIs to 0 failed!\n");
  79. }
  80. static int rtas_query_irq_number(struct pci_dn *pdn, int offset)
  81. {
  82. u32 addr, rtas_ret[2];
  83. unsigned long buid;
  84. int rc;
  85. addr = rtas_config_addr(pdn->busno, pdn->devfn, 0);
  86. buid = pdn->phb->buid;
  87. do {
  88. rc = rtas_call(query_token, 4, 3, rtas_ret, addr,
  89. BUID_HI(buid), BUID_LO(buid), offset);
  90. } while (rtas_busy_delay(rc));
  91. if (rc) {
  92. pr_debug("rtas_msi: error (%d) querying source number\n", rc);
  93. return rc;
  94. }
  95. return rtas_ret[0];
  96. }
  97. static void rtas_teardown_msi_irqs(struct pci_dev *pdev)
  98. {
  99. struct msi_desc *entry;
  100. list_for_each_entry(entry, &pdev->msi_list, list) {
  101. if (entry->irq == NO_IRQ)
  102. continue;
  103. set_irq_msi(entry->irq, NULL);
  104. irq_dispose_mapping(entry->irq);
  105. }
  106. rtas_disable_msi(pdev);
  107. }
  108. static int check_req(struct pci_dev *pdev, int nvec, char *prop_name)
  109. {
  110. struct device_node *dn;
  111. struct pci_dn *pdn;
  112. const u32 *req_msi;
  113. pdn = get_pdn(pdev);
  114. if (!pdn)
  115. return -ENODEV;
  116. dn = pdn->node;
  117. req_msi = of_get_property(dn, prop_name, NULL);
  118. if (!req_msi) {
  119. pr_debug("rtas_msi: No %s on %s\n", prop_name, dn->full_name);
  120. return -ENOENT;
  121. }
  122. if (*req_msi < nvec) {
  123. pr_debug("rtas_msi: %s requests < %d MSIs\n", prop_name, nvec);
  124. if (*req_msi == 0) /* Be paranoid */
  125. return -ENOSPC;
  126. return *req_msi;
  127. }
  128. return 0;
  129. }
  130. static int check_req_msi(struct pci_dev *pdev, int nvec)
  131. {
  132. return check_req(pdev, nvec, "ibm,req#msi");
  133. }
  134. static int check_req_msix(struct pci_dev *pdev, int nvec)
  135. {
  136. return check_req(pdev, nvec, "ibm,req#msi-x");
  137. }
  138. /* Quota calculation */
  139. static struct device_node *find_pe_total_msi(struct pci_dev *dev, int *total)
  140. {
  141. struct device_node *dn;
  142. const u32 *p;
  143. dn = of_node_get(pci_device_to_OF_node(dev));
  144. while (dn) {
  145. p = of_get_property(dn, "ibm,pe-total-#msi", NULL);
  146. if (p) {
  147. pr_debug("rtas_msi: found prop on dn %s\n",
  148. dn->full_name);
  149. *total = *p;
  150. return dn;
  151. }
  152. dn = of_get_next_parent(dn);
  153. }
  154. return NULL;
  155. }
  156. static struct device_node *find_pe_dn(struct pci_dev *dev, int *total)
  157. {
  158. struct device_node *dn;
  159. /* Found our PE and assume 8 at that point. */
  160. dn = pci_device_to_OF_node(dev);
  161. if (!dn)
  162. return NULL;
  163. dn = find_device_pe(dn);
  164. if (!dn)
  165. return NULL;
  166. /* We actually want the parent */
  167. dn = of_get_parent(dn);
  168. if (!dn)
  169. return NULL;
  170. /* Hardcode of 8 for old firmwares */
  171. *total = 8;
  172. pr_debug("rtas_msi: using PE dn %s\n", dn->full_name);
  173. return dn;
  174. }
  175. struct msi_counts {
  176. struct device_node *requestor;
  177. int num_devices;
  178. int request;
  179. int quota;
  180. int spare;
  181. int over_quota;
  182. };
  183. static void *count_non_bridge_devices(struct device_node *dn, void *data)
  184. {
  185. struct msi_counts *counts = data;
  186. const u32 *p;
  187. u32 class;
  188. pr_debug("rtas_msi: counting %s\n", dn->full_name);
  189. p = of_get_property(dn, "class-code", NULL);
  190. class = p ? *p : 0;
  191. if ((class >> 8) != PCI_CLASS_BRIDGE_PCI)
  192. counts->num_devices++;
  193. return NULL;
  194. }
  195. static void *count_spare_msis(struct device_node *dn, void *data)
  196. {
  197. struct msi_counts *counts = data;
  198. const u32 *p;
  199. int req;
  200. if (dn == counts->requestor)
  201. req = counts->request;
  202. else {
  203. /* We don't know if a driver will try to use MSI or MSI-X,
  204. * so we just have to punt and use the larger of the two. */
  205. req = 0;
  206. p = of_get_property(dn, "ibm,req#msi", NULL);
  207. if (p)
  208. req = *p;
  209. p = of_get_property(dn, "ibm,req#msi-x", NULL);
  210. if (p)
  211. req = max(req, (int)*p);
  212. }
  213. if (req < counts->quota)
  214. counts->spare += counts->quota - req;
  215. else if (req > counts->quota)
  216. counts->over_quota++;
  217. return NULL;
  218. }
  219. static int msi_quota_for_device(struct pci_dev *dev, int request)
  220. {
  221. struct device_node *pe_dn;
  222. struct msi_counts counts;
  223. int total;
  224. pr_debug("rtas_msi: calc quota for %s, request %d\n", pci_name(dev),
  225. request);
  226. pe_dn = find_pe_total_msi(dev, &total);
  227. if (!pe_dn)
  228. pe_dn = find_pe_dn(dev, &total);
  229. if (!pe_dn) {
  230. pr_err("rtas_msi: couldn't find PE for %s\n", pci_name(dev));
  231. goto out;
  232. }
  233. pr_debug("rtas_msi: found PE %s\n", pe_dn->full_name);
  234. memset(&counts, 0, sizeof(struct msi_counts));
  235. /* Work out how many devices we have below this PE */
  236. traverse_pci_devices(pe_dn, count_non_bridge_devices, &counts);
  237. if (counts.num_devices == 0) {
  238. pr_err("rtas_msi: found 0 devices under PE for %s\n",
  239. pci_name(dev));
  240. goto out;
  241. }
  242. counts.quota = total / counts.num_devices;
  243. if (request <= counts.quota)
  244. goto out;
  245. /* else, we have some more calculating to do */
  246. counts.requestor = pci_device_to_OF_node(dev);
  247. counts.request = request;
  248. traverse_pci_devices(pe_dn, count_spare_msis, &counts);
  249. /* If the quota isn't an integer multiple of the total, we can
  250. * use the remainder as spare MSIs for anyone that wants them. */
  251. counts.spare += total % counts.num_devices;
  252. /* Divide any spare by the number of over-quota requestors */
  253. if (counts.over_quota)
  254. counts.quota += counts.spare / counts.over_quota;
  255. /* And finally clamp the request to the possibly adjusted quota */
  256. request = min(counts.quota, request);
  257. pr_debug("rtas_msi: request clamped to quota %d\n", request);
  258. out:
  259. of_node_put(pe_dn);
  260. return request;
  261. }
  262. static int rtas_msi_check_device(struct pci_dev *pdev, int nvec, int type)
  263. {
  264. int quota, rc;
  265. if (type == PCI_CAP_ID_MSIX)
  266. rc = check_req_msix(pdev, nvec);
  267. else
  268. rc = check_req_msi(pdev, nvec);
  269. if (rc)
  270. return rc;
  271. quota = msi_quota_for_device(pdev, nvec);
  272. if (quota && quota < nvec)
  273. return quota;
  274. return 0;
  275. }
  276. static int check_msix_entries(struct pci_dev *pdev)
  277. {
  278. struct msi_desc *entry;
  279. int expected;
  280. /* There's no way for us to express to firmware that we want
  281. * a discontiguous, or non-zero based, range of MSI-X entries.
  282. * So we must reject such requests. */
  283. expected = 0;
  284. list_for_each_entry(entry, &pdev->msi_list, list) {
  285. if (entry->msi_attrib.entry_nr != expected) {
  286. pr_debug("rtas_msi: bad MSI-X entries.\n");
  287. return -EINVAL;
  288. }
  289. expected++;
  290. }
  291. return 0;
  292. }
  293. static int rtas_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
  294. {
  295. struct pci_dn *pdn;
  296. int hwirq, virq, i, rc;
  297. struct msi_desc *entry;
  298. struct msi_msg msg;
  299. pdn = get_pdn(pdev);
  300. if (!pdn)
  301. return -ENODEV;
  302. if (type == PCI_CAP_ID_MSIX && check_msix_entries(pdev))
  303. return -EINVAL;
  304. /*
  305. * Try the new more explicit firmware interface, if that fails fall
  306. * back to the old interface. The old interface is known to never
  307. * return MSI-Xs.
  308. */
  309. if (type == PCI_CAP_ID_MSI) {
  310. rc = rtas_change_msi(pdn, RTAS_CHANGE_MSI_FN, nvec);
  311. if (rc < 0) {
  312. pr_debug("rtas_msi: trying the old firmware call.\n");
  313. rc = rtas_change_msi(pdn, RTAS_CHANGE_FN, nvec);
  314. }
  315. } else
  316. rc = rtas_change_msi(pdn, RTAS_CHANGE_MSIX_FN, nvec);
  317. if (rc != nvec) {
  318. pr_debug("rtas_msi: rtas_change_msi() failed\n");
  319. return rc;
  320. }
  321. i = 0;
  322. list_for_each_entry(entry, &pdev->msi_list, list) {
  323. hwirq = rtas_query_irq_number(pdn, i++);
  324. if (hwirq < 0) {
  325. pr_debug("rtas_msi: error (%d) getting hwirq\n", rc);
  326. return hwirq;
  327. }
  328. virq = irq_create_mapping(NULL, hwirq);
  329. if (virq == NO_IRQ) {
  330. pr_debug("rtas_msi: Failed mapping hwirq %d\n", hwirq);
  331. return -ENOSPC;
  332. }
  333. dev_dbg(&pdev->dev, "rtas_msi: allocated virq %d\n", virq);
  334. set_irq_msi(virq, entry);
  335. /* Read config space back so we can restore after reset */
  336. read_msi_msg(virq, &msg);
  337. entry->msg = msg;
  338. unmask_msi_irq(virq);
  339. }
  340. return 0;
  341. }
  342. static void rtas_msi_pci_irq_fixup(struct pci_dev *pdev)
  343. {
  344. /* No LSI -> leave MSIs (if any) configured */
  345. if (pdev->irq == NO_IRQ) {
  346. dev_dbg(&pdev->dev, "rtas_msi: no LSI, nothing to do.\n");
  347. return;
  348. }
  349. /* No MSI -> MSIs can't have been assigned by fw, leave LSI */
  350. if (check_req_msi(pdev, 1) && check_req_msix(pdev, 1)) {
  351. dev_dbg(&pdev->dev, "rtas_msi: no req#msi/x, nothing to do.\n");
  352. return;
  353. }
  354. dev_dbg(&pdev->dev, "rtas_msi: disabling existing MSI.\n");
  355. rtas_disable_msi(pdev);
  356. }
  357. static int rtas_msi_init(void)
  358. {
  359. query_token = rtas_token("ibm,query-interrupt-source-number");
  360. change_token = rtas_token("ibm,change-msi");
  361. if ((query_token == RTAS_UNKNOWN_SERVICE) ||
  362. (change_token == RTAS_UNKNOWN_SERVICE)) {
  363. pr_debug("rtas_msi: no RTAS tokens, no MSI support.\n");
  364. return -1;
  365. }
  366. pr_debug("rtas_msi: Registering RTAS MSI callbacks.\n");
  367. WARN_ON(ppc_md.setup_msi_irqs);
  368. ppc_md.setup_msi_irqs = rtas_setup_msi_irqs;
  369. ppc_md.teardown_msi_irqs = rtas_teardown_msi_irqs;
  370. ppc_md.msi_check_device = rtas_msi_check_device;
  371. WARN_ON(ppc_md.pci_irq_fixup);
  372. ppc_md.pci_irq_fixup = rtas_msi_pci_irq_fixup;
  373. return 0;
  374. }
  375. arch_initcall(rtas_msi_init);