msi.c 12 KB

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