msi.c 12 KB

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