msi.c 11 KB

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