msi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 rtas_msi_check_device(struct pci_dev *pdev, int nvec, int type)
  267. {
  268. int quota, rc;
  269. if (type == PCI_CAP_ID_MSIX)
  270. rc = check_req_msix(pdev, nvec);
  271. else
  272. rc = check_req_msi(pdev, nvec);
  273. if (rc)
  274. return rc;
  275. quota = msi_quota_for_device(pdev, nvec);
  276. if (quota && quota < nvec)
  277. return quota;
  278. return 0;
  279. }
  280. static int check_msix_entries(struct pci_dev *pdev)
  281. {
  282. struct msi_desc *entry;
  283. int expected;
  284. /* There's no way for us to express to firmware that we want
  285. * a discontiguous, or non-zero based, range of MSI-X entries.
  286. * So we must reject such requests. */
  287. expected = 0;
  288. list_for_each_entry(entry, &pdev->msi_list, list) {
  289. if (entry->msi_attrib.entry_nr != expected) {
  290. pr_debug("rtas_msi: bad MSI-X entries.\n");
  291. return -EINVAL;
  292. }
  293. expected++;
  294. }
  295. return 0;
  296. }
  297. static void rtas_hack_32bit_msi_gen2(struct pci_dev *pdev)
  298. {
  299. u32 addr_hi, addr_lo;
  300. /*
  301. * We should only get in here for IODA1 configs. This is based on the
  302. * fact that we using RTAS for MSIs, we don't have the 32 bit MSI RTAS
  303. * support, and we are in a PCIe Gen2 slot.
  304. */
  305. dev_info(&pdev->dev,
  306. "rtas_msi: No 32 bit MSI firmware support, forcing 32 bit MSI\n");
  307. pci_read_config_dword(pdev, pdev->msi_cap + PCI_MSI_ADDRESS_HI, &addr_hi);
  308. addr_lo = 0xffff0000 | ((addr_hi >> (48 - 32)) << 4);
  309. pci_write_config_dword(pdev, pdev->msi_cap + PCI_MSI_ADDRESS_LO, addr_lo);
  310. pci_write_config_dword(pdev, pdev->msi_cap + PCI_MSI_ADDRESS_HI, 0);
  311. }
  312. static int rtas_setup_msi_irqs(struct pci_dev *pdev, int nvec_in, int type)
  313. {
  314. struct pci_dn *pdn;
  315. int hwirq, virq, i, rc;
  316. struct msi_desc *entry;
  317. struct msi_msg msg;
  318. int nvec = nvec_in;
  319. int use_32bit_msi_hack = 0;
  320. pdn = pci_get_pdn(pdev);
  321. if (!pdn)
  322. return -ENODEV;
  323. if (type == PCI_CAP_ID_MSIX && check_msix_entries(pdev))
  324. return -EINVAL;
  325. /*
  326. * Firmware currently refuse any non power of two allocation
  327. * so we round up if the quota will allow it.
  328. */
  329. if (type == PCI_CAP_ID_MSIX) {
  330. int m = roundup_pow_of_two(nvec);
  331. int quota = msi_quota_for_device(pdev, m);
  332. if (quota >= m)
  333. nvec = m;
  334. }
  335. /*
  336. * Try the new more explicit firmware interface, if that fails fall
  337. * back to the old interface. The old interface is known to never
  338. * return MSI-Xs.
  339. */
  340. again:
  341. if (type == PCI_CAP_ID_MSI) {
  342. if (pdn->force_32bit_msi) {
  343. rc = rtas_change_msi(pdn, RTAS_CHANGE_32MSI_FN, nvec);
  344. if (rc < 0) {
  345. /*
  346. * We only want to run the 32 bit MSI hack below if
  347. * the max bus speed is Gen2 speed
  348. */
  349. if (pdev->bus->max_bus_speed != PCIE_SPEED_5_0GT)
  350. return rc;
  351. use_32bit_msi_hack = 1;
  352. }
  353. } else
  354. rc = -1;
  355. if (rc < 0)
  356. rc = rtas_change_msi(pdn, RTAS_CHANGE_MSI_FN, nvec);
  357. if (rc < 0) {
  358. pr_debug("rtas_msi: trying the old firmware call.\n");
  359. rc = rtas_change_msi(pdn, RTAS_CHANGE_FN, nvec);
  360. }
  361. if (use_32bit_msi_hack && rc > 0)
  362. rtas_hack_32bit_msi_gen2(pdev);
  363. } else
  364. rc = rtas_change_msi(pdn, RTAS_CHANGE_MSIX_FN, nvec);
  365. if (rc != nvec) {
  366. if (nvec != nvec_in) {
  367. nvec = nvec_in;
  368. goto again;
  369. }
  370. pr_debug("rtas_msi: rtas_change_msi() failed\n");
  371. return rc;
  372. }
  373. i = 0;
  374. list_for_each_entry(entry, &pdev->msi_list, list) {
  375. hwirq = rtas_query_irq_number(pdn, i++);
  376. if (hwirq < 0) {
  377. pr_debug("rtas_msi: error (%d) getting hwirq\n", rc);
  378. return hwirq;
  379. }
  380. virq = irq_create_mapping(NULL, hwirq);
  381. if (virq == NO_IRQ) {
  382. pr_debug("rtas_msi: Failed mapping hwirq %d\n", hwirq);
  383. return -ENOSPC;
  384. }
  385. dev_dbg(&pdev->dev, "rtas_msi: allocated virq %d\n", virq);
  386. irq_set_msi_desc(virq, entry);
  387. /* Read config space back so we can restore after reset */
  388. read_msi_msg(virq, &msg);
  389. entry->msg = msg;
  390. }
  391. return 0;
  392. }
  393. static void rtas_msi_pci_irq_fixup(struct pci_dev *pdev)
  394. {
  395. /* No LSI -> leave MSIs (if any) configured */
  396. if (pdev->irq == NO_IRQ) {
  397. dev_dbg(&pdev->dev, "rtas_msi: no LSI, nothing to do.\n");
  398. return;
  399. }
  400. /* No MSI -> MSIs can't have been assigned by fw, leave LSI */
  401. if (check_req_msi(pdev, 1) && check_req_msix(pdev, 1)) {
  402. dev_dbg(&pdev->dev, "rtas_msi: no req#msi/x, nothing to do.\n");
  403. return;
  404. }
  405. dev_dbg(&pdev->dev, "rtas_msi: disabling existing MSI.\n");
  406. rtas_disable_msi(pdev);
  407. }
  408. static int rtas_msi_init(void)
  409. {
  410. query_token = rtas_token("ibm,query-interrupt-source-number");
  411. change_token = rtas_token("ibm,change-msi");
  412. if ((query_token == RTAS_UNKNOWN_SERVICE) ||
  413. (change_token == RTAS_UNKNOWN_SERVICE)) {
  414. pr_debug("rtas_msi: no RTAS tokens, no MSI support.\n");
  415. return -1;
  416. }
  417. pr_debug("rtas_msi: Registering RTAS MSI callbacks.\n");
  418. WARN_ON(ppc_md.setup_msi_irqs);
  419. ppc_md.setup_msi_irqs = rtas_setup_msi_irqs;
  420. ppc_md.teardown_msi_irqs = rtas_teardown_msi_irqs;
  421. ppc_md.msi_check_device = rtas_msi_check_device;
  422. WARN_ON(ppc_md.pci_irq_fixup);
  423. ppc_md.pci_irq_fixup = rtas_msi_pci_irq_fixup;
  424. return 0;
  425. }
  426. machine_arch_initcall(pseries, rtas_msi_init);