msi.c 12 KB

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