msi.c 11 KB

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