msi.c 12 KB

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