msi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. static struct pci_dn *get_pdn(struct pci_dev *pdev)
  25. {
  26. struct device_node *dn;
  27. struct pci_dn *pdn;
  28. dn = pci_device_to_OF_node(pdev);
  29. if (!dn) {
  30. dev_dbg(&pdev->dev, "rtas_msi: No OF device node\n");
  31. return NULL;
  32. }
  33. pdn = PCI_DN(dn);
  34. if (!pdn) {
  35. dev_dbg(&pdev->dev, "rtas_msi: No PCI DN\n");
  36. return NULL;
  37. }
  38. return pdn;
  39. }
  40. /* RTAS Helpers */
  41. static int rtas_change_msi(struct pci_dn *pdn, u32 func, u32 num_irqs)
  42. {
  43. u32 addr, seq_num, rtas_ret[3];
  44. unsigned long buid;
  45. int rc;
  46. addr = rtas_config_addr(pdn->busno, pdn->devfn, 0);
  47. buid = pdn->phb->buid;
  48. seq_num = 1;
  49. do {
  50. if (func == RTAS_CHANGE_MSI_FN || func == RTAS_CHANGE_MSIX_FN ||
  51. func == RTAS_CHANGE_32MSI_FN)
  52. rc = rtas_call(change_token, 6, 4, rtas_ret, addr,
  53. BUID_HI(buid), BUID_LO(buid),
  54. func, num_irqs, seq_num);
  55. else
  56. rc = rtas_call(change_token, 6, 3, rtas_ret, addr,
  57. BUID_HI(buid), BUID_LO(buid),
  58. func, num_irqs, seq_num);
  59. seq_num = rtas_ret[1];
  60. } while (rtas_busy_delay(rc));
  61. /*
  62. * If the RTAS call succeeded, return the number of irqs allocated.
  63. * If not, make sure we return a negative error code.
  64. */
  65. if (rc == 0)
  66. rc = rtas_ret[0];
  67. else if (rc > 0)
  68. rc = -rc;
  69. pr_debug("rtas_msi: ibm,change_msi(func=%d,num=%d), got %d rc = %d\n",
  70. func, num_irqs, rtas_ret[0], rc);
  71. return rc;
  72. }
  73. static void rtas_disable_msi(struct pci_dev *pdev)
  74. {
  75. struct pci_dn *pdn;
  76. pdn = get_pdn(pdev);
  77. if (!pdn)
  78. return;
  79. /*
  80. * disabling MSI with the explicit interface also disables MSI-X
  81. */
  82. if (rtas_change_msi(pdn, RTAS_CHANGE_MSI_FN, 0) != 0) {
  83. /*
  84. * may have failed because explicit interface is not
  85. * present
  86. */
  87. if (rtas_change_msi(pdn, RTAS_CHANGE_FN, 0) != 0) {
  88. pr_debug("rtas_msi: Setting MSIs to 0 failed!\n");
  89. }
  90. }
  91. }
  92. static int rtas_query_irq_number(struct pci_dn *pdn, int offset)
  93. {
  94. u32 addr, rtas_ret[2];
  95. unsigned long buid;
  96. int rc;
  97. addr = rtas_config_addr(pdn->busno, pdn->devfn, 0);
  98. buid = pdn->phb->buid;
  99. do {
  100. rc = rtas_call(query_token, 4, 3, rtas_ret, addr,
  101. BUID_HI(buid), BUID_LO(buid), offset);
  102. } while (rtas_busy_delay(rc));
  103. if (rc) {
  104. pr_debug("rtas_msi: error (%d) querying source number\n", rc);
  105. return rc;
  106. }
  107. return rtas_ret[0];
  108. }
  109. static void rtas_teardown_msi_irqs(struct pci_dev *pdev)
  110. {
  111. struct msi_desc *entry;
  112. list_for_each_entry(entry, &pdev->msi_list, list) {
  113. if (entry->irq == NO_IRQ)
  114. continue;
  115. irq_set_msi_desc(entry->irq, NULL);
  116. irq_dispose_mapping(entry->irq);
  117. }
  118. rtas_disable_msi(pdev);
  119. }
  120. static int check_req(struct pci_dev *pdev, int nvec, char *prop_name)
  121. {
  122. struct device_node *dn;
  123. struct pci_dn *pdn;
  124. const u32 *req_msi;
  125. pdn = get_pdn(pdev);
  126. if (!pdn)
  127. return -ENODEV;
  128. dn = pdn->node;
  129. req_msi = of_get_property(dn, prop_name, NULL);
  130. if (!req_msi) {
  131. pr_debug("rtas_msi: No %s on %s\n", prop_name, dn->full_name);
  132. return -ENOENT;
  133. }
  134. if (*req_msi < nvec) {
  135. pr_debug("rtas_msi: %s requests < %d MSIs\n", prop_name, nvec);
  136. if (*req_msi == 0) /* Be paranoid */
  137. return -ENOSPC;
  138. return *req_msi;
  139. }
  140. return 0;
  141. }
  142. static int check_req_msi(struct pci_dev *pdev, int nvec)
  143. {
  144. return check_req(pdev, nvec, "ibm,req#msi");
  145. }
  146. static int check_req_msix(struct pci_dev *pdev, int nvec)
  147. {
  148. return check_req(pdev, nvec, "ibm,req#msi-x");
  149. }
  150. /* Quota calculation */
  151. static struct device_node *find_pe_total_msi(struct pci_dev *dev, int *total)
  152. {
  153. struct device_node *dn;
  154. const u32 *p;
  155. dn = of_node_get(pci_device_to_OF_node(dev));
  156. while (dn) {
  157. p = of_get_property(dn, "ibm,pe-total-#msi", NULL);
  158. if (p) {
  159. pr_debug("rtas_msi: found prop on dn %s\n",
  160. dn->full_name);
  161. *total = *p;
  162. return dn;
  163. }
  164. dn = of_get_next_parent(dn);
  165. }
  166. return NULL;
  167. }
  168. static struct device_node *find_pe_dn(struct pci_dev *dev, int *total)
  169. {
  170. struct device_node *dn;
  171. struct eeh_dev *edev;
  172. /* Found our PE and assume 8 at that point. */
  173. dn = pci_device_to_OF_node(dev);
  174. if (!dn)
  175. return NULL;
  176. /* Get the top level device in the PE */
  177. edev = of_node_to_eeh_dev(dn);
  178. if (edev->pe)
  179. edev = list_first_entry(&edev->pe->edevs, struct eeh_dev, list);
  180. dn = eeh_dev_to_of_node(edev);
  181. if (!dn)
  182. return NULL;
  183. /* We actually want the parent */
  184. dn = of_get_parent(dn);
  185. if (!dn)
  186. return NULL;
  187. /* Hardcode of 8 for old firmwares */
  188. *total = 8;
  189. pr_debug("rtas_msi: using PE dn %s\n", dn->full_name);
  190. return dn;
  191. }
  192. struct msi_counts {
  193. struct device_node *requestor;
  194. int num_devices;
  195. int request;
  196. int quota;
  197. int spare;
  198. int over_quota;
  199. };
  200. static void *count_non_bridge_devices(struct device_node *dn, void *data)
  201. {
  202. struct msi_counts *counts = data;
  203. const u32 *p;
  204. u32 class;
  205. pr_debug("rtas_msi: counting %s\n", dn->full_name);
  206. p = of_get_property(dn, "class-code", NULL);
  207. class = p ? *p : 0;
  208. if ((class >> 8) != PCI_CLASS_BRIDGE_PCI)
  209. counts->num_devices++;
  210. return NULL;
  211. }
  212. static void *count_spare_msis(struct device_node *dn, void *data)
  213. {
  214. struct msi_counts *counts = data;
  215. const u32 *p;
  216. int req;
  217. if (dn == counts->requestor)
  218. req = counts->request;
  219. else {
  220. /* We don't know if a driver will try to use MSI or MSI-X,
  221. * so we just have to punt and use the larger of the two. */
  222. req = 0;
  223. p = of_get_property(dn, "ibm,req#msi", NULL);
  224. if (p)
  225. req = *p;
  226. p = of_get_property(dn, "ibm,req#msi-x", NULL);
  227. if (p)
  228. req = max(req, (int)*p);
  229. }
  230. if (req < counts->quota)
  231. counts->spare += counts->quota - req;
  232. else if (req > counts->quota)
  233. counts->over_quota++;
  234. return NULL;
  235. }
  236. static int msi_quota_for_device(struct pci_dev *dev, int request)
  237. {
  238. struct device_node *pe_dn;
  239. struct msi_counts counts;
  240. int total;
  241. pr_debug("rtas_msi: calc quota for %s, request %d\n", pci_name(dev),
  242. request);
  243. pe_dn = find_pe_total_msi(dev, &total);
  244. if (!pe_dn)
  245. pe_dn = find_pe_dn(dev, &total);
  246. if (!pe_dn) {
  247. pr_err("rtas_msi: couldn't find PE for %s\n", pci_name(dev));
  248. goto out;
  249. }
  250. pr_debug("rtas_msi: found PE %s\n", pe_dn->full_name);
  251. memset(&counts, 0, sizeof(struct msi_counts));
  252. /* Work out how many devices we have below this PE */
  253. traverse_pci_devices(pe_dn, count_non_bridge_devices, &counts);
  254. if (counts.num_devices == 0) {
  255. pr_err("rtas_msi: found 0 devices under PE for %s\n",
  256. pci_name(dev));
  257. goto out;
  258. }
  259. counts.quota = total / counts.num_devices;
  260. if (request <= counts.quota)
  261. goto out;
  262. /* else, we have some more calculating to do */
  263. counts.requestor = pci_device_to_OF_node(dev);
  264. counts.request = request;
  265. traverse_pci_devices(pe_dn, count_spare_msis, &counts);
  266. /* If the quota isn't an integer multiple of the total, we can
  267. * use the remainder as spare MSIs for anyone that wants them. */
  268. counts.spare += total % counts.num_devices;
  269. /* Divide any spare by the number of over-quota requestors */
  270. if (counts.over_quota)
  271. counts.quota += counts.spare / counts.over_quota;
  272. /* And finally clamp the request to the possibly adjusted quota */
  273. request = min(counts.quota, request);
  274. pr_debug("rtas_msi: request clamped to quota %d\n", request);
  275. out:
  276. of_node_put(pe_dn);
  277. return request;
  278. }
  279. static int rtas_msi_check_device(struct pci_dev *pdev, int nvec, int type)
  280. {
  281. int quota, rc;
  282. if (type == PCI_CAP_ID_MSIX)
  283. rc = check_req_msix(pdev, nvec);
  284. else
  285. rc = check_req_msi(pdev, nvec);
  286. if (rc)
  287. return rc;
  288. quota = msi_quota_for_device(pdev, nvec);
  289. if (quota && quota < nvec)
  290. return quota;
  291. return 0;
  292. }
  293. static int check_msix_entries(struct pci_dev *pdev)
  294. {
  295. struct msi_desc *entry;
  296. int expected;
  297. /* There's no way for us to express to firmware that we want
  298. * a discontiguous, or non-zero based, range of MSI-X entries.
  299. * So we must reject such requests. */
  300. expected = 0;
  301. list_for_each_entry(entry, &pdev->msi_list, list) {
  302. if (entry->msi_attrib.entry_nr != expected) {
  303. pr_debug("rtas_msi: bad MSI-X entries.\n");
  304. return -EINVAL;
  305. }
  306. expected++;
  307. }
  308. return 0;
  309. }
  310. static int rtas_setup_msi_irqs(struct pci_dev *pdev, int nvec_in, int type)
  311. {
  312. struct pci_dn *pdn;
  313. int hwirq, virq, i, rc;
  314. struct msi_desc *entry;
  315. struct msi_msg msg;
  316. int nvec = nvec_in;
  317. pdn = 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. else
  342. rc = rtas_change_msi(pdn, RTAS_CHANGE_MSI_FN, nvec);
  343. if (rc < 0 && !pdn->force_32bit_msi) {
  344. pr_debug("rtas_msi: trying the old firmware call.\n");
  345. rc = rtas_change_msi(pdn, RTAS_CHANGE_FN, nvec);
  346. }
  347. } else
  348. rc = rtas_change_msi(pdn, RTAS_CHANGE_MSIX_FN, nvec);
  349. if (rc != nvec) {
  350. if (nvec != nvec_in) {
  351. nvec = nvec_in;
  352. goto again;
  353. }
  354. pr_debug("rtas_msi: rtas_change_msi() failed\n");
  355. return rc;
  356. }
  357. i = 0;
  358. list_for_each_entry(entry, &pdev->msi_list, list) {
  359. hwirq = rtas_query_irq_number(pdn, i++);
  360. if (hwirq < 0) {
  361. pr_debug("rtas_msi: error (%d) getting hwirq\n", rc);
  362. return hwirq;
  363. }
  364. virq = irq_create_mapping(NULL, hwirq);
  365. if (virq == NO_IRQ) {
  366. pr_debug("rtas_msi: Failed mapping hwirq %d\n", hwirq);
  367. return -ENOSPC;
  368. }
  369. dev_dbg(&pdev->dev, "rtas_msi: allocated virq %d\n", virq);
  370. irq_set_msi_desc(virq, entry);
  371. /* Read config space back so we can restore after reset */
  372. read_msi_msg(virq, &msg);
  373. entry->msg = msg;
  374. }
  375. return 0;
  376. }
  377. static void rtas_msi_pci_irq_fixup(struct pci_dev *pdev)
  378. {
  379. /* No LSI -> leave MSIs (if any) configured */
  380. if (pdev->irq == NO_IRQ) {
  381. dev_dbg(&pdev->dev, "rtas_msi: no LSI, nothing to do.\n");
  382. return;
  383. }
  384. /* No MSI -> MSIs can't have been assigned by fw, leave LSI */
  385. if (check_req_msi(pdev, 1) && check_req_msix(pdev, 1)) {
  386. dev_dbg(&pdev->dev, "rtas_msi: no req#msi/x, nothing to do.\n");
  387. return;
  388. }
  389. dev_dbg(&pdev->dev, "rtas_msi: disabling existing MSI.\n");
  390. rtas_disable_msi(pdev);
  391. }
  392. static int rtas_msi_init(void)
  393. {
  394. query_token = rtas_token("ibm,query-interrupt-source-number");
  395. change_token = rtas_token("ibm,change-msi");
  396. if ((query_token == RTAS_UNKNOWN_SERVICE) ||
  397. (change_token == RTAS_UNKNOWN_SERVICE)) {
  398. pr_debug("rtas_msi: no RTAS tokens, no MSI support.\n");
  399. return -1;
  400. }
  401. pr_debug("rtas_msi: Registering RTAS MSI callbacks.\n");
  402. WARN_ON(ppc_md.setup_msi_irqs);
  403. ppc_md.setup_msi_irqs = rtas_setup_msi_irqs;
  404. ppc_md.teardown_msi_irqs = rtas_teardown_msi_irqs;
  405. ppc_md.msi_check_device = rtas_msi_check_device;
  406. WARN_ON(ppc_md.pci_irq_fixup);
  407. ppc_md.pci_irq_fixup = rtas_msi_pci_irq_fixup;
  408. return 0;
  409. }
  410. arch_initcall(rtas_msi_init);
  411. static void quirk_radeon(struct pci_dev *dev)
  412. {
  413. struct pci_dn *pdn = get_pdn(dev);
  414. if (pdn)
  415. pdn->force_32bit_msi = 1;
  416. }
  417. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATI, 0x68f2, quirk_radeon);
  418. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATI, 0xaa68, quirk_radeon);