npu-dma.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * This file implements the DMA operations for NVLink devices. The NPU
  3. * devices all point to the same iommu table as the parent PCI device.
  4. *
  5. * Copyright Alistair Popple, IBM Corporation 2015.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of version 2 of the GNU General Public
  9. * License as published by the Free Software Foundation.
  10. */
  11. #include <linux/export.h>
  12. #include <linux/pci.h>
  13. #include <linux/memblock.h>
  14. #include <linux/iommu.h>
  15. #include <asm/iommu.h>
  16. #include <asm/pnv-pci.h>
  17. #include <asm/msi_bitmap.h>
  18. #include <asm/opal.h>
  19. #include "powernv.h"
  20. #include "pci.h"
  21. /*
  22. * Other types of TCE cache invalidation are not functional in the
  23. * hardware.
  24. */
  25. static struct pci_dev *get_pci_dev(struct device_node *dn)
  26. {
  27. return PCI_DN(dn)->pcidev;
  28. }
  29. /* Given a NPU device get the associated PCI device. */
  30. struct pci_dev *pnv_pci_get_gpu_dev(struct pci_dev *npdev)
  31. {
  32. struct device_node *dn;
  33. struct pci_dev *gpdev;
  34. /* Get assoicated PCI device */
  35. dn = of_parse_phandle(npdev->dev.of_node, "ibm,gpu", 0);
  36. if (!dn)
  37. return NULL;
  38. gpdev = get_pci_dev(dn);
  39. of_node_put(dn);
  40. return gpdev;
  41. }
  42. EXPORT_SYMBOL(pnv_pci_get_gpu_dev);
  43. /* Given the real PCI device get a linked NPU device. */
  44. struct pci_dev *pnv_pci_get_npu_dev(struct pci_dev *gpdev, int index)
  45. {
  46. struct device_node *dn;
  47. struct pci_dev *npdev;
  48. /* Get assoicated PCI device */
  49. dn = of_parse_phandle(gpdev->dev.of_node, "ibm,npu", index);
  50. if (!dn)
  51. return NULL;
  52. npdev = get_pci_dev(dn);
  53. of_node_put(dn);
  54. return npdev;
  55. }
  56. EXPORT_SYMBOL(pnv_pci_get_npu_dev);
  57. #define NPU_DMA_OP_UNSUPPORTED() \
  58. dev_err_once(dev, "%s operation unsupported for NVLink devices\n", \
  59. __func__)
  60. static void *dma_npu_alloc(struct device *dev, size_t size,
  61. dma_addr_t *dma_handle, gfp_t flag,
  62. unsigned long attrs)
  63. {
  64. NPU_DMA_OP_UNSUPPORTED();
  65. return NULL;
  66. }
  67. static void dma_npu_free(struct device *dev, size_t size,
  68. void *vaddr, dma_addr_t dma_handle,
  69. unsigned long attrs)
  70. {
  71. NPU_DMA_OP_UNSUPPORTED();
  72. }
  73. static dma_addr_t dma_npu_map_page(struct device *dev, struct page *page,
  74. unsigned long offset, size_t size,
  75. enum dma_data_direction direction,
  76. unsigned long attrs)
  77. {
  78. NPU_DMA_OP_UNSUPPORTED();
  79. return 0;
  80. }
  81. static int dma_npu_map_sg(struct device *dev, struct scatterlist *sglist,
  82. int nelems, enum dma_data_direction direction,
  83. unsigned long attrs)
  84. {
  85. NPU_DMA_OP_UNSUPPORTED();
  86. return 0;
  87. }
  88. static int dma_npu_dma_supported(struct device *dev, u64 mask)
  89. {
  90. NPU_DMA_OP_UNSUPPORTED();
  91. return 0;
  92. }
  93. static u64 dma_npu_get_required_mask(struct device *dev)
  94. {
  95. NPU_DMA_OP_UNSUPPORTED();
  96. return 0;
  97. }
  98. static struct dma_map_ops dma_npu_ops = {
  99. .map_page = dma_npu_map_page,
  100. .map_sg = dma_npu_map_sg,
  101. .alloc = dma_npu_alloc,
  102. .free = dma_npu_free,
  103. .dma_supported = dma_npu_dma_supported,
  104. .get_required_mask = dma_npu_get_required_mask,
  105. };
  106. /*
  107. * Returns the PE assoicated with the PCI device of the given
  108. * NPU. Returns the linked pci device if pci_dev != NULL.
  109. */
  110. static struct pnv_ioda_pe *get_gpu_pci_dev_and_pe(struct pnv_ioda_pe *npe,
  111. struct pci_dev **gpdev)
  112. {
  113. struct pnv_phb *phb;
  114. struct pci_controller *hose;
  115. struct pci_dev *pdev;
  116. struct pnv_ioda_pe *pe;
  117. struct pci_dn *pdn;
  118. pdev = pnv_pci_get_gpu_dev(npe->pdev);
  119. if (!pdev)
  120. return NULL;
  121. pdn = pci_get_pdn(pdev);
  122. if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
  123. return NULL;
  124. hose = pci_bus_to_host(pdev->bus);
  125. phb = hose->private_data;
  126. pe = &phb->ioda.pe_array[pdn->pe_number];
  127. if (gpdev)
  128. *gpdev = pdev;
  129. return pe;
  130. }
  131. long pnv_npu_set_window(struct pnv_ioda_pe *npe, int num,
  132. struct iommu_table *tbl)
  133. {
  134. struct pnv_phb *phb = npe->phb;
  135. int64_t rc;
  136. const unsigned long size = tbl->it_indirect_levels ?
  137. tbl->it_level_size : tbl->it_size;
  138. const __u64 start_addr = tbl->it_offset << tbl->it_page_shift;
  139. const __u64 win_size = tbl->it_size << tbl->it_page_shift;
  140. pe_info(npe, "Setting up window %llx..%llx pg=%lx\n",
  141. start_addr, start_addr + win_size - 1,
  142. IOMMU_PAGE_SIZE(tbl));
  143. rc = opal_pci_map_pe_dma_window(phb->opal_id,
  144. npe->pe_number,
  145. npe->pe_number,
  146. tbl->it_indirect_levels + 1,
  147. __pa(tbl->it_base),
  148. size << 3,
  149. IOMMU_PAGE_SIZE(tbl));
  150. if (rc) {
  151. pe_err(npe, "Failed to configure TCE table, err %lld\n", rc);
  152. return rc;
  153. }
  154. pnv_pci_phb3_tce_invalidate_entire(phb, false);
  155. /* Add the table to the list so its TCE cache will get invalidated */
  156. pnv_pci_link_table_and_group(phb->hose->node, num,
  157. tbl, &npe->table_group);
  158. return 0;
  159. }
  160. long pnv_npu_unset_window(struct pnv_ioda_pe *npe, int num)
  161. {
  162. struct pnv_phb *phb = npe->phb;
  163. int64_t rc;
  164. pe_info(npe, "Removing DMA window\n");
  165. rc = opal_pci_map_pe_dma_window(phb->opal_id, npe->pe_number,
  166. npe->pe_number,
  167. 0/* levels */, 0/* table address */,
  168. 0/* table size */, 0/* page size */);
  169. if (rc) {
  170. pe_err(npe, "Unmapping failed, ret = %lld\n", rc);
  171. return rc;
  172. }
  173. pnv_pci_phb3_tce_invalidate_entire(phb, false);
  174. pnv_pci_unlink_table_and_group(npe->table_group.tables[num],
  175. &npe->table_group);
  176. return 0;
  177. }
  178. /*
  179. * Enables 32 bit DMA on NPU.
  180. */
  181. static void pnv_npu_dma_set_32(struct pnv_ioda_pe *npe)
  182. {
  183. struct pci_dev *gpdev;
  184. struct pnv_ioda_pe *gpe;
  185. int64_t rc;
  186. /*
  187. * Find the assoicated PCI devices and get the dma window
  188. * information from there.
  189. */
  190. if (!npe->pdev || !(npe->flags & PNV_IODA_PE_DEV))
  191. return;
  192. gpe = get_gpu_pci_dev_and_pe(npe, &gpdev);
  193. if (!gpe)
  194. return;
  195. rc = pnv_npu_set_window(npe, 0, gpe->table_group.tables[0]);
  196. /*
  197. * We don't initialise npu_pe->tce32_table as we always use
  198. * dma_npu_ops which are nops.
  199. */
  200. set_dma_ops(&npe->pdev->dev, &dma_npu_ops);
  201. }
  202. /*
  203. * Enables bypass mode on the NPU. The NPU only supports one
  204. * window per link, so bypass needs to be explicitly enabled or
  205. * disabled. Unlike for a PHB3 bypass and non-bypass modes can't be
  206. * active at the same time.
  207. */
  208. static int pnv_npu_dma_set_bypass(struct pnv_ioda_pe *npe)
  209. {
  210. struct pnv_phb *phb = npe->phb;
  211. int64_t rc = 0;
  212. phys_addr_t top = memblock_end_of_DRAM();
  213. if (phb->type != PNV_PHB_NPU || !npe->pdev)
  214. return -EINVAL;
  215. rc = pnv_npu_unset_window(npe, 0);
  216. if (rc != OPAL_SUCCESS)
  217. return rc;
  218. /* Enable the bypass window */
  219. top = roundup_pow_of_two(top);
  220. dev_info(&npe->pdev->dev, "Enabling bypass for PE %d\n",
  221. npe->pe_number);
  222. rc = opal_pci_map_pe_dma_window_real(phb->opal_id,
  223. npe->pe_number, npe->pe_number,
  224. 0 /* bypass base */, top);
  225. if (rc == OPAL_SUCCESS)
  226. pnv_pci_phb3_tce_invalidate_entire(phb, false);
  227. return rc;
  228. }
  229. void pnv_npu_try_dma_set_bypass(struct pci_dev *gpdev, bool bypass)
  230. {
  231. int i;
  232. struct pnv_phb *phb;
  233. struct pci_dn *pdn;
  234. struct pnv_ioda_pe *npe;
  235. struct pci_dev *npdev;
  236. for (i = 0; ; ++i) {
  237. npdev = pnv_pci_get_npu_dev(gpdev, i);
  238. if (!npdev)
  239. break;
  240. pdn = pci_get_pdn(npdev);
  241. if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
  242. return;
  243. phb = pci_bus_to_host(npdev->bus)->private_data;
  244. /* We only do bypass if it's enabled on the linked device */
  245. npe = &phb->ioda.pe_array[pdn->pe_number];
  246. if (bypass) {
  247. dev_info(&npdev->dev,
  248. "Using 64-bit DMA iommu bypass\n");
  249. pnv_npu_dma_set_bypass(npe);
  250. } else {
  251. dev_info(&npdev->dev, "Using 32-bit DMA via iommu\n");
  252. pnv_npu_dma_set_32(npe);
  253. }
  254. }
  255. }
  256. /* Switch ownership from platform code to external user (e.g. VFIO) */
  257. void pnv_npu_take_ownership(struct pnv_ioda_pe *npe)
  258. {
  259. struct pnv_phb *phb = npe->phb;
  260. int64_t rc;
  261. /*
  262. * Note: NPU has just a single TVE in the hardware which means that
  263. * while used by the kernel, it can have either 32bit window or
  264. * DMA bypass but never both. So we deconfigure 32bit window only
  265. * if it was enabled at the moment of ownership change.
  266. */
  267. if (npe->table_group.tables[0]) {
  268. pnv_npu_unset_window(npe, 0);
  269. return;
  270. }
  271. /* Disable bypass */
  272. rc = opal_pci_map_pe_dma_window_real(phb->opal_id,
  273. npe->pe_number, npe->pe_number,
  274. 0 /* bypass base */, 0);
  275. if (rc) {
  276. pe_err(npe, "Failed to disable bypass, err %lld\n", rc);
  277. return;
  278. }
  279. pnv_pci_phb3_tce_invalidate_entire(npe->phb, false);
  280. }
  281. struct pnv_ioda_pe *pnv_pci_npu_setup_iommu(struct pnv_ioda_pe *npe)
  282. {
  283. struct pnv_phb *phb = npe->phb;
  284. struct pci_bus *pbus = phb->hose->bus;
  285. struct pci_dev *npdev, *gpdev = NULL, *gptmp;
  286. struct pnv_ioda_pe *gpe = get_gpu_pci_dev_and_pe(npe, &gpdev);
  287. if (!gpe || !gpdev)
  288. return NULL;
  289. list_for_each_entry(npdev, &pbus->devices, bus_list) {
  290. gptmp = pnv_pci_get_gpu_dev(npdev);
  291. if (gptmp != gpdev)
  292. continue;
  293. pe_info(gpe, "Attached NPU %s\n", dev_name(&npdev->dev));
  294. iommu_group_add_device(gpe->table_group.group, &npdev->dev);
  295. }
  296. return gpe;
  297. }