msi.c 12 KB

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