pci-cxl.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * Copyright 2014-2016 IBM Corp.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/msi.h>
  11. #include <asm/pci-bridge.h>
  12. #include <asm/pnv-pci.h>
  13. #include <asm/opal.h>
  14. #include <misc/cxl.h>
  15. #include "pci.h"
  16. struct device_node *pnv_pci_get_phb_node(struct pci_dev *dev)
  17. {
  18. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  19. return of_node_get(hose->dn);
  20. }
  21. EXPORT_SYMBOL(pnv_pci_get_phb_node);
  22. int pnv_phb_to_cxl_mode(struct pci_dev *dev, uint64_t mode)
  23. {
  24. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  25. struct pnv_phb *phb = hose->private_data;
  26. struct pnv_ioda_pe *pe;
  27. int rc;
  28. pe = pnv_ioda_get_pe(dev);
  29. if (!pe)
  30. return -ENODEV;
  31. pe_info(pe, "Switching PHB to CXL\n");
  32. rc = opal_pci_set_phb_cxl_mode(phb->opal_id, mode, pe->pe_number);
  33. if (rc == OPAL_UNSUPPORTED)
  34. dev_err(&dev->dev, "Required cxl mode not supported by firmware - update skiboot\n");
  35. else if (rc)
  36. dev_err(&dev->dev, "opal_pci_set_phb_cxl_mode failed: %i\n", rc);
  37. return rc;
  38. }
  39. EXPORT_SYMBOL(pnv_phb_to_cxl_mode);
  40. /* Find PHB for cxl dev and allocate MSI hwirqs?
  41. * Returns the absolute hardware IRQ number
  42. */
  43. int pnv_cxl_alloc_hwirqs(struct pci_dev *dev, int num)
  44. {
  45. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  46. struct pnv_phb *phb = hose->private_data;
  47. int hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, num);
  48. if (hwirq < 0) {
  49. dev_warn(&dev->dev, "Failed to find a free MSI\n");
  50. return -ENOSPC;
  51. }
  52. return phb->msi_base + hwirq;
  53. }
  54. EXPORT_SYMBOL(pnv_cxl_alloc_hwirqs);
  55. void pnv_cxl_release_hwirqs(struct pci_dev *dev, int hwirq, int num)
  56. {
  57. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  58. struct pnv_phb *phb = hose->private_data;
  59. msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq - phb->msi_base, num);
  60. }
  61. EXPORT_SYMBOL(pnv_cxl_release_hwirqs);
  62. void pnv_cxl_release_hwirq_ranges(struct cxl_irq_ranges *irqs,
  63. struct pci_dev *dev)
  64. {
  65. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  66. struct pnv_phb *phb = hose->private_data;
  67. int i, hwirq;
  68. for (i = 1; i < CXL_IRQ_RANGES; i++) {
  69. if (!irqs->range[i])
  70. continue;
  71. pr_devel("cxl release irq range 0x%x: offset: 0x%lx limit: %ld\n",
  72. i, irqs->offset[i],
  73. irqs->range[i]);
  74. hwirq = irqs->offset[i] - phb->msi_base;
  75. msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq,
  76. irqs->range[i]);
  77. }
  78. }
  79. EXPORT_SYMBOL(pnv_cxl_release_hwirq_ranges);
  80. int pnv_cxl_alloc_hwirq_ranges(struct cxl_irq_ranges *irqs,
  81. struct pci_dev *dev, int num)
  82. {
  83. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  84. struct pnv_phb *phb = hose->private_data;
  85. int i, hwirq, try;
  86. memset(irqs, 0, sizeof(struct cxl_irq_ranges));
  87. /* 0 is reserved for the multiplexed PSL DSI interrupt */
  88. for (i = 1; i < CXL_IRQ_RANGES && num; i++) {
  89. try = num;
  90. while (try) {
  91. hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, try);
  92. if (hwirq >= 0)
  93. break;
  94. try /= 2;
  95. }
  96. if (!try)
  97. goto fail;
  98. irqs->offset[i] = phb->msi_base + hwirq;
  99. irqs->range[i] = try;
  100. pr_devel("cxl alloc irq range 0x%x: offset: 0x%lx limit: %li\n",
  101. i, irqs->offset[i], irqs->range[i]);
  102. num -= try;
  103. }
  104. if (num)
  105. goto fail;
  106. return 0;
  107. fail:
  108. pnv_cxl_release_hwirq_ranges(irqs, dev);
  109. return -ENOSPC;
  110. }
  111. EXPORT_SYMBOL(pnv_cxl_alloc_hwirq_ranges);
  112. int pnv_cxl_get_irq_count(struct pci_dev *dev)
  113. {
  114. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  115. struct pnv_phb *phb = hose->private_data;
  116. return phb->msi_bmp.irq_count;
  117. }
  118. EXPORT_SYMBOL(pnv_cxl_get_irq_count);
  119. int pnv_cxl_ioda_msi_setup(struct pci_dev *dev, unsigned int hwirq,
  120. unsigned int virq)
  121. {
  122. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  123. struct pnv_phb *phb = hose->private_data;
  124. unsigned int xive_num = hwirq - phb->msi_base;
  125. struct pnv_ioda_pe *pe;
  126. int rc;
  127. if (!(pe = pnv_ioda_get_pe(dev)))
  128. return -ENODEV;
  129. /* Assign XIVE to PE */
  130. rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
  131. if (rc) {
  132. pe_warn(pe, "%s: OPAL error %d setting msi_base 0x%x "
  133. "hwirq 0x%x XIVE 0x%x PE\n",
  134. pci_name(dev), rc, phb->msi_base, hwirq, xive_num);
  135. return -EIO;
  136. }
  137. pnv_set_msi_irq_chip(phb, virq);
  138. return 0;
  139. }
  140. EXPORT_SYMBOL(pnv_cxl_ioda_msi_setup);
  141. #if IS_MODULE(CONFIG_CXL)
  142. static inline int get_cxl_module(void)
  143. {
  144. struct module *cxl_module;
  145. mutex_lock(&module_mutex);
  146. cxl_module = find_module("cxl");
  147. if (cxl_module)
  148. __module_get(cxl_module);
  149. mutex_unlock(&module_mutex);
  150. if (!cxl_module)
  151. return -ENODEV;
  152. return 0;
  153. }
  154. #else
  155. static inline int get_cxl_module(void) { return 0; }
  156. #endif
  157. /*
  158. * Sets flags and switches the controller ops to enable the cxl kernel api.
  159. * Originally the cxl kernel API operated on a virtual PHB, but certain cards
  160. * such as the Mellanox CX4 use a peer model instead and for these cards the
  161. * cxl kernel api will operate on the real PHB.
  162. */
  163. int pnv_cxl_enable_phb_kernel_api(struct pci_controller *hose, bool enable)
  164. {
  165. struct pnv_phb *phb = hose->private_data;
  166. int rc;
  167. if (!enable) {
  168. /*
  169. * Once cxl mode is enabled on the PHB, there is currently no
  170. * known safe method to disable it again, and trying risks a
  171. * checkstop. If we can find a way to safely disable cxl mode
  172. * in the future we can revisit this, but for now the only sane
  173. * thing to do is to refuse to disable cxl mode:
  174. */
  175. return -EPERM;
  176. }
  177. /*
  178. * Hold a reference to the cxl module since several PHB operations now
  179. * depend on it, and it would be insane to allow it to be removed so
  180. * long as we are in this mode (and since we can't safely disable this
  181. * mode once enabled...).
  182. */
  183. rc = get_cxl_module();
  184. if (rc)
  185. return rc;
  186. phb->flags |= PNV_PHB_FLAG_CXL;
  187. hose->controller_ops = pnv_cxl_cx4_ioda_controller_ops;
  188. return 0;
  189. }
  190. EXPORT_SYMBOL_GPL(pnv_cxl_enable_phb_kernel_api);
  191. bool pnv_pci_on_cxl_phb(struct pci_dev *dev)
  192. {
  193. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  194. struct pnv_phb *phb = hose->private_data;
  195. return !!(phb->flags & PNV_PHB_FLAG_CXL);
  196. }
  197. EXPORT_SYMBOL_GPL(pnv_pci_on_cxl_phb);
  198. struct cxl_afu *pnv_cxl_phb_to_afu(struct pci_controller *hose)
  199. {
  200. struct pnv_phb *phb = hose->private_data;
  201. return (struct cxl_afu *)phb->cxl_afu;
  202. }
  203. EXPORT_SYMBOL_GPL(pnv_cxl_phb_to_afu);
  204. void pnv_cxl_phb_set_peer_afu(struct pci_dev *dev, struct cxl_afu *afu)
  205. {
  206. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  207. struct pnv_phb *phb = hose->private_data;
  208. phb->cxl_afu = afu;
  209. }
  210. EXPORT_SYMBOL_GPL(pnv_cxl_phb_set_peer_afu);
  211. /*
  212. * In the peer cxl model, the XSL/PSL is physical function 0, and will be used
  213. * by other functions on the device for memory access and interrupts. When the
  214. * other functions are enabled we explicitly take a reference on the cxl
  215. * function since they will use it, and allocate a default context associated
  216. * with that function just like the vPHB model of the cxl kernel API.
  217. */
  218. bool pnv_cxl_enable_device_hook(struct pci_dev *dev)
  219. {
  220. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  221. struct pnv_phb *phb = hose->private_data;
  222. struct cxl_afu *afu = phb->cxl_afu;
  223. if (!pnv_pci_enable_device_hook(dev))
  224. return false;
  225. /* No special handling for the cxl function, which is always PF 0 */
  226. if (PCI_FUNC(dev->devfn) == 0)
  227. return true;
  228. if (!afu) {
  229. dev_WARN(&dev->dev, "Attempted to enable function > 0 on CXL PHB without a peer AFU\n");
  230. return false;
  231. }
  232. dev_info(&dev->dev, "Enabling function on CXL enabled PHB with peer AFU\n");
  233. /* Make sure the peer AFU can't go away while this device is active */
  234. cxl_afu_get(afu);
  235. return cxl_pci_associate_default_context(dev, afu);
  236. }
  237. void pnv_cxl_disable_device(struct pci_dev *dev)
  238. {
  239. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  240. struct pnv_phb *phb = hose->private_data;
  241. struct cxl_afu *afu = phb->cxl_afu;
  242. /* No special handling for cxl function: */
  243. if (PCI_FUNC(dev->devfn) == 0)
  244. return;
  245. cxl_pci_disable_device(dev);
  246. cxl_afu_put(afu);
  247. }
  248. /*
  249. * This is a special version of pnv_setup_msi_irqs for cards in cxl mode. This
  250. * function handles setting up the IVTE entries for the XSL to use.
  251. *
  252. * We are currently not filling out the MSIX table, since the only currently
  253. * supported adapter (CX4) uses a custom MSIX table format in cxl mode and it
  254. * is up to their driver to fill that out. In the future we may fill out the
  255. * MSIX table (and change the IVTE entries to be an index to the MSIX table)
  256. * for adapters implementing the Full MSI-X mode described in the CAIA.
  257. */
  258. int pnv_cxl_cx4_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
  259. {
  260. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  261. struct pnv_phb *phb = hose->private_data;
  262. struct msi_desc *entry;
  263. struct cxl_context *ctx = NULL;
  264. unsigned int virq;
  265. int hwirq;
  266. int afu_irq = 0;
  267. int rc;
  268. if (WARN_ON(!phb) || !phb->msi_bmp.bitmap)
  269. return -ENODEV;
  270. if (pdev->no_64bit_msi && !phb->msi32_support)
  271. return -ENODEV;
  272. rc = cxl_cx4_setup_msi_irqs(pdev, nvec, type);
  273. if (rc)
  274. return rc;
  275. for_each_pci_msi_entry(entry, pdev) {
  276. if (!entry->msi_attrib.is_64 && !phb->msi32_support) {
  277. pr_warn("%s: Supports only 64-bit MSIs\n",
  278. pci_name(pdev));
  279. return -ENXIO;
  280. }
  281. hwirq = cxl_next_msi_hwirq(pdev, &ctx, &afu_irq);
  282. if (WARN_ON(hwirq <= 0))
  283. return (hwirq ? hwirq : -ENOMEM);
  284. virq = irq_create_mapping(NULL, hwirq);
  285. if (!virq) {
  286. pr_warn("%s: Failed to map cxl mode MSI to linux irq\n",
  287. pci_name(pdev));
  288. return -ENOMEM;
  289. }
  290. rc = pnv_cxl_ioda_msi_setup(pdev, hwirq, virq);
  291. if (rc) {
  292. pr_warn("%s: Failed to setup cxl mode MSI\n", pci_name(pdev));
  293. irq_dispose_mapping(virq);
  294. return rc;
  295. }
  296. irq_set_msi_desc(virq, entry);
  297. }
  298. return 0;
  299. }
  300. void pnv_cxl_cx4_teardown_msi_irqs(struct pci_dev *pdev)
  301. {
  302. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  303. struct pnv_phb *phb = hose->private_data;
  304. struct msi_desc *entry;
  305. irq_hw_number_t hwirq;
  306. if (WARN_ON(!phb))
  307. return;
  308. for_each_pci_msi_entry(entry, pdev) {
  309. if (!entry->irq)
  310. continue;
  311. hwirq = virq_to_hw(entry->irq);
  312. irq_set_msi_desc(entry->irq, NULL);
  313. irq_dispose_mapping(entry->irq);
  314. }
  315. cxl_cx4_teardown_msi_irqs(pdev);
  316. }