npu-dma.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  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/slab.h>
  12. #include <linux/mmu_notifier.h>
  13. #include <linux/mmu_context.h>
  14. #include <linux/of.h>
  15. #include <linux/export.h>
  16. #include <linux/pci.h>
  17. #include <linux/memblock.h>
  18. #include <linux/iommu.h>
  19. #include <asm/tlb.h>
  20. #include <asm/powernv.h>
  21. #include <asm/reg.h>
  22. #include <asm/opal.h>
  23. #include <asm/io.h>
  24. #include <asm/iommu.h>
  25. #include <asm/pnv-pci.h>
  26. #include <asm/msi_bitmap.h>
  27. #include <asm/opal.h>
  28. #include "powernv.h"
  29. #include "pci.h"
  30. #define npu_to_phb(x) container_of(x, struct pnv_phb, npu)
  31. /*
  32. * spinlock to protect initialisation of an npu_context for a particular
  33. * mm_struct.
  34. */
  35. static DEFINE_SPINLOCK(npu_context_lock);
  36. /*
  37. * When an address shootdown range exceeds this threshold we invalidate the
  38. * entire TLB on the GPU for the given PID rather than each specific address in
  39. * the range.
  40. */
  41. #define ATSD_THRESHOLD (2*1024*1024)
  42. /*
  43. * Other types of TCE cache invalidation are not functional in the
  44. * hardware.
  45. */
  46. static struct pci_dev *get_pci_dev(struct device_node *dn)
  47. {
  48. struct pci_dn *pdn = PCI_DN(dn);
  49. return pci_get_domain_bus_and_slot(pci_domain_nr(pdn->phb->bus),
  50. pdn->busno, pdn->devfn);
  51. }
  52. /* Given a NPU device get the associated PCI device. */
  53. struct pci_dev *pnv_pci_get_gpu_dev(struct pci_dev *npdev)
  54. {
  55. struct device_node *dn;
  56. struct pci_dev *gpdev;
  57. if (WARN_ON(!npdev))
  58. return NULL;
  59. if (WARN_ON(!npdev->dev.of_node))
  60. return NULL;
  61. /* Get assoicated PCI device */
  62. dn = of_parse_phandle(npdev->dev.of_node, "ibm,gpu", 0);
  63. if (!dn)
  64. return NULL;
  65. gpdev = get_pci_dev(dn);
  66. of_node_put(dn);
  67. return gpdev;
  68. }
  69. EXPORT_SYMBOL(pnv_pci_get_gpu_dev);
  70. /* Given the real PCI device get a linked NPU device. */
  71. struct pci_dev *pnv_pci_get_npu_dev(struct pci_dev *gpdev, int index)
  72. {
  73. struct device_node *dn;
  74. struct pci_dev *npdev;
  75. if (WARN_ON(!gpdev))
  76. return NULL;
  77. /* Not all PCI devices have device-tree nodes */
  78. if (!gpdev->dev.of_node)
  79. return NULL;
  80. /* Get assoicated PCI device */
  81. dn = of_parse_phandle(gpdev->dev.of_node, "ibm,npu", index);
  82. if (!dn)
  83. return NULL;
  84. npdev = get_pci_dev(dn);
  85. of_node_put(dn);
  86. return npdev;
  87. }
  88. EXPORT_SYMBOL(pnv_pci_get_npu_dev);
  89. #define NPU_DMA_OP_UNSUPPORTED() \
  90. dev_err_once(dev, "%s operation unsupported for NVLink devices\n", \
  91. __func__)
  92. static void *dma_npu_alloc(struct device *dev, size_t size,
  93. dma_addr_t *dma_handle, gfp_t flag,
  94. unsigned long attrs)
  95. {
  96. NPU_DMA_OP_UNSUPPORTED();
  97. return NULL;
  98. }
  99. static void dma_npu_free(struct device *dev, size_t size,
  100. void *vaddr, dma_addr_t dma_handle,
  101. unsigned long attrs)
  102. {
  103. NPU_DMA_OP_UNSUPPORTED();
  104. }
  105. static dma_addr_t dma_npu_map_page(struct device *dev, struct page *page,
  106. unsigned long offset, size_t size,
  107. enum dma_data_direction direction,
  108. unsigned long attrs)
  109. {
  110. NPU_DMA_OP_UNSUPPORTED();
  111. return 0;
  112. }
  113. static int dma_npu_map_sg(struct device *dev, struct scatterlist *sglist,
  114. int nelems, enum dma_data_direction direction,
  115. unsigned long attrs)
  116. {
  117. NPU_DMA_OP_UNSUPPORTED();
  118. return 0;
  119. }
  120. static int dma_npu_dma_supported(struct device *dev, u64 mask)
  121. {
  122. NPU_DMA_OP_UNSUPPORTED();
  123. return 0;
  124. }
  125. static u64 dma_npu_get_required_mask(struct device *dev)
  126. {
  127. NPU_DMA_OP_UNSUPPORTED();
  128. return 0;
  129. }
  130. static const struct dma_map_ops dma_npu_ops = {
  131. .map_page = dma_npu_map_page,
  132. .map_sg = dma_npu_map_sg,
  133. .alloc = dma_npu_alloc,
  134. .free = dma_npu_free,
  135. .dma_supported = dma_npu_dma_supported,
  136. .get_required_mask = dma_npu_get_required_mask,
  137. };
  138. /*
  139. * Returns the PE assoicated with the PCI device of the given
  140. * NPU. Returns the linked pci device if pci_dev != NULL.
  141. */
  142. static struct pnv_ioda_pe *get_gpu_pci_dev_and_pe(struct pnv_ioda_pe *npe,
  143. struct pci_dev **gpdev)
  144. {
  145. struct pnv_phb *phb;
  146. struct pci_controller *hose;
  147. struct pci_dev *pdev;
  148. struct pnv_ioda_pe *pe;
  149. struct pci_dn *pdn;
  150. pdev = pnv_pci_get_gpu_dev(npe->pdev);
  151. if (!pdev)
  152. return NULL;
  153. pdn = pci_get_pdn(pdev);
  154. if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
  155. return NULL;
  156. hose = pci_bus_to_host(pdev->bus);
  157. phb = hose->private_data;
  158. pe = &phb->ioda.pe_array[pdn->pe_number];
  159. if (gpdev)
  160. *gpdev = pdev;
  161. return pe;
  162. }
  163. long pnv_npu_set_window(struct pnv_ioda_pe *npe, int num,
  164. struct iommu_table *tbl)
  165. {
  166. struct pnv_phb *phb = npe->phb;
  167. int64_t rc;
  168. const unsigned long size = tbl->it_indirect_levels ?
  169. tbl->it_level_size : tbl->it_size;
  170. const __u64 start_addr = tbl->it_offset << tbl->it_page_shift;
  171. const __u64 win_size = tbl->it_size << tbl->it_page_shift;
  172. pe_info(npe, "Setting up window %llx..%llx pg=%lx\n",
  173. start_addr, start_addr + win_size - 1,
  174. IOMMU_PAGE_SIZE(tbl));
  175. rc = opal_pci_map_pe_dma_window(phb->opal_id,
  176. npe->pe_number,
  177. npe->pe_number,
  178. tbl->it_indirect_levels + 1,
  179. __pa(tbl->it_base),
  180. size << 3,
  181. IOMMU_PAGE_SIZE(tbl));
  182. if (rc) {
  183. pe_err(npe, "Failed to configure TCE table, err %lld\n", rc);
  184. return rc;
  185. }
  186. pnv_pci_ioda2_tce_invalidate_entire(phb, false);
  187. /* Add the table to the list so its TCE cache will get invalidated */
  188. pnv_pci_link_table_and_group(phb->hose->node, num,
  189. tbl, &npe->table_group);
  190. return 0;
  191. }
  192. long pnv_npu_unset_window(struct pnv_ioda_pe *npe, int num)
  193. {
  194. struct pnv_phb *phb = npe->phb;
  195. int64_t rc;
  196. pe_info(npe, "Removing DMA window\n");
  197. rc = opal_pci_map_pe_dma_window(phb->opal_id, npe->pe_number,
  198. npe->pe_number,
  199. 0/* levels */, 0/* table address */,
  200. 0/* table size */, 0/* page size */);
  201. if (rc) {
  202. pe_err(npe, "Unmapping failed, ret = %lld\n", rc);
  203. return rc;
  204. }
  205. pnv_pci_ioda2_tce_invalidate_entire(phb, false);
  206. pnv_pci_unlink_table_and_group(npe->table_group.tables[num],
  207. &npe->table_group);
  208. return 0;
  209. }
  210. /*
  211. * Enables 32 bit DMA on NPU.
  212. */
  213. static void pnv_npu_dma_set_32(struct pnv_ioda_pe *npe)
  214. {
  215. struct pci_dev *gpdev;
  216. struct pnv_ioda_pe *gpe;
  217. int64_t rc;
  218. /*
  219. * Find the assoicated PCI devices and get the dma window
  220. * information from there.
  221. */
  222. if (!npe->pdev || !(npe->flags & PNV_IODA_PE_DEV))
  223. return;
  224. gpe = get_gpu_pci_dev_and_pe(npe, &gpdev);
  225. if (!gpe)
  226. return;
  227. rc = pnv_npu_set_window(npe, 0, gpe->table_group.tables[0]);
  228. /*
  229. * We don't initialise npu_pe->tce32_table as we always use
  230. * dma_npu_ops which are nops.
  231. */
  232. set_dma_ops(&npe->pdev->dev, &dma_npu_ops);
  233. }
  234. /*
  235. * Enables bypass mode on the NPU. The NPU only supports one
  236. * window per link, so bypass needs to be explicitly enabled or
  237. * disabled. Unlike for a PHB3 bypass and non-bypass modes can't be
  238. * active at the same time.
  239. */
  240. static int pnv_npu_dma_set_bypass(struct pnv_ioda_pe *npe)
  241. {
  242. struct pnv_phb *phb = npe->phb;
  243. int64_t rc = 0;
  244. phys_addr_t top = memblock_end_of_DRAM();
  245. if (phb->type != PNV_PHB_NPU_NVLINK || !npe->pdev)
  246. return -EINVAL;
  247. rc = pnv_npu_unset_window(npe, 0);
  248. if (rc != OPAL_SUCCESS)
  249. return rc;
  250. /* Enable the bypass window */
  251. top = roundup_pow_of_two(top);
  252. dev_info(&npe->pdev->dev, "Enabling bypass for PE %x\n",
  253. npe->pe_number);
  254. rc = opal_pci_map_pe_dma_window_real(phb->opal_id,
  255. npe->pe_number, npe->pe_number,
  256. 0 /* bypass base */, top);
  257. if (rc == OPAL_SUCCESS)
  258. pnv_pci_ioda2_tce_invalidate_entire(phb, false);
  259. return rc;
  260. }
  261. void pnv_npu_try_dma_set_bypass(struct pci_dev *gpdev, bool bypass)
  262. {
  263. int i;
  264. struct pnv_phb *phb;
  265. struct pci_dn *pdn;
  266. struct pnv_ioda_pe *npe;
  267. struct pci_dev *npdev;
  268. for (i = 0; ; ++i) {
  269. npdev = pnv_pci_get_npu_dev(gpdev, i);
  270. if (!npdev)
  271. break;
  272. pdn = pci_get_pdn(npdev);
  273. if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
  274. return;
  275. phb = pci_bus_to_host(npdev->bus)->private_data;
  276. /* We only do bypass if it's enabled on the linked device */
  277. npe = &phb->ioda.pe_array[pdn->pe_number];
  278. if (bypass) {
  279. dev_info(&npdev->dev,
  280. "Using 64-bit DMA iommu bypass\n");
  281. pnv_npu_dma_set_bypass(npe);
  282. } else {
  283. dev_info(&npdev->dev, "Using 32-bit DMA via iommu\n");
  284. pnv_npu_dma_set_32(npe);
  285. }
  286. }
  287. }
  288. /* Switch ownership from platform code to external user (e.g. VFIO) */
  289. void pnv_npu_take_ownership(struct pnv_ioda_pe *npe)
  290. {
  291. struct pnv_phb *phb = npe->phb;
  292. int64_t rc;
  293. /*
  294. * Note: NPU has just a single TVE in the hardware which means that
  295. * while used by the kernel, it can have either 32bit window or
  296. * DMA bypass but never both. So we deconfigure 32bit window only
  297. * if it was enabled at the moment of ownership change.
  298. */
  299. if (npe->table_group.tables[0]) {
  300. pnv_npu_unset_window(npe, 0);
  301. return;
  302. }
  303. /* Disable bypass */
  304. rc = opal_pci_map_pe_dma_window_real(phb->opal_id,
  305. npe->pe_number, npe->pe_number,
  306. 0 /* bypass base */, 0);
  307. if (rc) {
  308. pe_err(npe, "Failed to disable bypass, err %lld\n", rc);
  309. return;
  310. }
  311. pnv_pci_ioda2_tce_invalidate_entire(npe->phb, false);
  312. }
  313. struct pnv_ioda_pe *pnv_pci_npu_setup_iommu(struct pnv_ioda_pe *npe)
  314. {
  315. struct pnv_phb *phb = npe->phb;
  316. struct pci_bus *pbus = phb->hose->bus;
  317. struct pci_dev *npdev, *gpdev = NULL, *gptmp;
  318. struct pnv_ioda_pe *gpe = get_gpu_pci_dev_and_pe(npe, &gpdev);
  319. if (!gpe || !gpdev)
  320. return NULL;
  321. list_for_each_entry(npdev, &pbus->devices, bus_list) {
  322. gptmp = pnv_pci_get_gpu_dev(npdev);
  323. if (gptmp != gpdev)
  324. continue;
  325. pe_info(gpe, "Attached NPU %s\n", dev_name(&npdev->dev));
  326. iommu_group_add_device(gpe->table_group.group, &npdev->dev);
  327. }
  328. return gpe;
  329. }
  330. /* Maximum number of nvlinks per npu */
  331. #define NV_MAX_LINKS 6
  332. /* Maximum index of npu2 hosts in the system. Always < NV_MAX_NPUS */
  333. static int max_npu2_index;
  334. struct npu_context {
  335. struct mm_struct *mm;
  336. struct pci_dev *npdev[NV_MAX_NPUS][NV_MAX_LINKS];
  337. struct mmu_notifier mn;
  338. struct kref kref;
  339. bool nmmu_flush;
  340. /* Callback to stop translation requests on a given GPU */
  341. void (*release_cb)(struct npu_context *context, void *priv);
  342. /*
  343. * Private pointer passed to the above callback for usage by
  344. * device drivers.
  345. */
  346. void *priv;
  347. };
  348. struct mmio_atsd_reg {
  349. struct npu *npu;
  350. int reg;
  351. };
  352. /*
  353. * Find a free MMIO ATSD register and mark it in use. Return -ENOSPC
  354. * if none are available.
  355. */
  356. static int get_mmio_atsd_reg(struct npu *npu)
  357. {
  358. int i;
  359. for (i = 0; i < npu->mmio_atsd_count; i++) {
  360. if (!test_and_set_bit_lock(i, &npu->mmio_atsd_usage))
  361. return i;
  362. }
  363. return -ENOSPC;
  364. }
  365. static void put_mmio_atsd_reg(struct npu *npu, int reg)
  366. {
  367. clear_bit_unlock(reg, &npu->mmio_atsd_usage);
  368. }
  369. /* MMIO ATSD register offsets */
  370. #define XTS_ATSD_AVA 1
  371. #define XTS_ATSD_STAT 2
  372. static void mmio_launch_invalidate(struct mmio_atsd_reg *mmio_atsd_reg,
  373. unsigned long launch, unsigned long va)
  374. {
  375. struct npu *npu = mmio_atsd_reg->npu;
  376. int reg = mmio_atsd_reg->reg;
  377. __raw_writeq(cpu_to_be64(va),
  378. npu->mmio_atsd_regs[reg] + XTS_ATSD_AVA);
  379. eieio();
  380. __raw_writeq(cpu_to_be64(launch), npu->mmio_atsd_regs[reg]);
  381. }
  382. static void mmio_invalidate_pid(struct mmio_atsd_reg mmio_atsd_reg[NV_MAX_NPUS],
  383. unsigned long pid, bool flush)
  384. {
  385. int i;
  386. unsigned long launch;
  387. for (i = 0; i <= max_npu2_index; i++) {
  388. if (mmio_atsd_reg[i].reg < 0)
  389. continue;
  390. /* IS set to invalidate matching PID */
  391. launch = PPC_BIT(12);
  392. /* PRS set to process-scoped */
  393. launch |= PPC_BIT(13);
  394. /* AP */
  395. launch |= (u64)
  396. mmu_get_ap(mmu_virtual_psize) << PPC_BITLSHIFT(17);
  397. /* PID */
  398. launch |= pid << PPC_BITLSHIFT(38);
  399. /* No flush */
  400. launch |= !flush << PPC_BITLSHIFT(39);
  401. /* Invalidating the entire process doesn't use a va */
  402. mmio_launch_invalidate(&mmio_atsd_reg[i], launch, 0);
  403. }
  404. }
  405. static void mmio_invalidate_va(struct mmio_atsd_reg mmio_atsd_reg[NV_MAX_NPUS],
  406. unsigned long va, unsigned long pid, bool flush)
  407. {
  408. int i;
  409. unsigned long launch;
  410. for (i = 0; i <= max_npu2_index; i++) {
  411. if (mmio_atsd_reg[i].reg < 0)
  412. continue;
  413. /* IS set to invalidate target VA */
  414. launch = 0;
  415. /* PRS set to process scoped */
  416. launch |= PPC_BIT(13);
  417. /* AP */
  418. launch |= (u64)
  419. mmu_get_ap(mmu_virtual_psize) << PPC_BITLSHIFT(17);
  420. /* PID */
  421. launch |= pid << PPC_BITLSHIFT(38);
  422. /* No flush */
  423. launch |= !flush << PPC_BITLSHIFT(39);
  424. mmio_launch_invalidate(&mmio_atsd_reg[i], launch, va);
  425. }
  426. }
  427. #define mn_to_npu_context(x) container_of(x, struct npu_context, mn)
  428. static void mmio_invalidate_wait(
  429. struct mmio_atsd_reg mmio_atsd_reg[NV_MAX_NPUS])
  430. {
  431. struct npu *npu;
  432. int i, reg;
  433. /* Wait for all invalidations to complete */
  434. for (i = 0; i <= max_npu2_index; i++) {
  435. if (mmio_atsd_reg[i].reg < 0)
  436. continue;
  437. /* Wait for completion */
  438. npu = mmio_atsd_reg[i].npu;
  439. reg = mmio_atsd_reg[i].reg;
  440. while (__raw_readq(npu->mmio_atsd_regs[reg] + XTS_ATSD_STAT))
  441. cpu_relax();
  442. }
  443. }
  444. /*
  445. * Acquires all the address translation shootdown (ATSD) registers required to
  446. * launch an ATSD on all links this npu_context is active on.
  447. */
  448. static void acquire_atsd_reg(struct npu_context *npu_context,
  449. struct mmio_atsd_reg mmio_atsd_reg[NV_MAX_NPUS])
  450. {
  451. int i, j;
  452. struct npu *npu;
  453. struct pci_dev *npdev;
  454. struct pnv_phb *nphb;
  455. for (i = 0; i <= max_npu2_index; i++) {
  456. mmio_atsd_reg[i].reg = -1;
  457. for (j = 0; j < NV_MAX_LINKS; j++) {
  458. /*
  459. * There are no ordering requirements with respect to
  460. * the setup of struct npu_context, but to ensure
  461. * consistent behaviour we need to ensure npdev[][] is
  462. * only read once.
  463. */
  464. npdev = READ_ONCE(npu_context->npdev[i][j]);
  465. if (!npdev)
  466. continue;
  467. nphb = pci_bus_to_host(npdev->bus)->private_data;
  468. npu = &nphb->npu;
  469. mmio_atsd_reg[i].npu = npu;
  470. mmio_atsd_reg[i].reg = get_mmio_atsd_reg(npu);
  471. while (mmio_atsd_reg[i].reg < 0) {
  472. mmio_atsd_reg[i].reg = get_mmio_atsd_reg(npu);
  473. cpu_relax();
  474. }
  475. break;
  476. }
  477. }
  478. }
  479. /*
  480. * Release previously acquired ATSD registers. To avoid deadlocks the registers
  481. * must be released in the same order they were acquired above in
  482. * acquire_atsd_reg.
  483. */
  484. static void release_atsd_reg(struct mmio_atsd_reg mmio_atsd_reg[NV_MAX_NPUS])
  485. {
  486. int i;
  487. for (i = 0; i <= max_npu2_index; i++) {
  488. /*
  489. * We can't rely on npu_context->npdev[][] being the same here
  490. * as when acquire_atsd_reg() was called, hence we use the
  491. * values stored in mmio_atsd_reg during the acquire phase
  492. * rather than re-reading npdev[][].
  493. */
  494. if (mmio_atsd_reg[i].reg < 0)
  495. continue;
  496. put_mmio_atsd_reg(mmio_atsd_reg[i].npu, mmio_atsd_reg[i].reg);
  497. }
  498. }
  499. /*
  500. * Invalidate either a single address or an entire PID depending on
  501. * the value of va.
  502. */
  503. static void mmio_invalidate(struct npu_context *npu_context, int va,
  504. unsigned long address, bool flush)
  505. {
  506. struct mmio_atsd_reg mmio_atsd_reg[NV_MAX_NPUS];
  507. unsigned long pid = npu_context->mm->context.id;
  508. if (npu_context->nmmu_flush)
  509. /*
  510. * Unfortunately the nest mmu does not support flushing specific
  511. * addresses so we have to flush the whole mm once before
  512. * shooting down the GPU translation.
  513. */
  514. flush_all_mm(npu_context->mm);
  515. /*
  516. * Loop over all the NPUs this process is active on and launch
  517. * an invalidate.
  518. */
  519. acquire_atsd_reg(npu_context, mmio_atsd_reg);
  520. if (va)
  521. mmio_invalidate_va(mmio_atsd_reg, address, pid, flush);
  522. else
  523. mmio_invalidate_pid(mmio_atsd_reg, pid, flush);
  524. mmio_invalidate_wait(mmio_atsd_reg);
  525. if (flush) {
  526. /*
  527. * The GPU requires two flush ATSDs to ensure all entries have
  528. * been flushed. We use PID 0 as it will never be used for a
  529. * process on the GPU.
  530. */
  531. mmio_invalidate_pid(mmio_atsd_reg, 0, true);
  532. mmio_invalidate_wait(mmio_atsd_reg);
  533. mmio_invalidate_pid(mmio_atsd_reg, 0, true);
  534. mmio_invalidate_wait(mmio_atsd_reg);
  535. }
  536. release_atsd_reg(mmio_atsd_reg);
  537. }
  538. static void pnv_npu2_mn_release(struct mmu_notifier *mn,
  539. struct mm_struct *mm)
  540. {
  541. struct npu_context *npu_context = mn_to_npu_context(mn);
  542. /* Call into device driver to stop requests to the NMMU */
  543. if (npu_context->release_cb)
  544. npu_context->release_cb(npu_context, npu_context->priv);
  545. /*
  546. * There should be no more translation requests for this PID, but we
  547. * need to ensure any entries for it are removed from the TLB.
  548. */
  549. mmio_invalidate(npu_context, 0, 0, true);
  550. }
  551. static void pnv_npu2_mn_change_pte(struct mmu_notifier *mn,
  552. struct mm_struct *mm,
  553. unsigned long address,
  554. pte_t pte)
  555. {
  556. struct npu_context *npu_context = mn_to_npu_context(mn);
  557. mmio_invalidate(npu_context, 1, address, true);
  558. }
  559. static void pnv_npu2_mn_invalidate_range(struct mmu_notifier *mn,
  560. struct mm_struct *mm,
  561. unsigned long start, unsigned long end)
  562. {
  563. struct npu_context *npu_context = mn_to_npu_context(mn);
  564. unsigned long address;
  565. if (end - start > ATSD_THRESHOLD) {
  566. /*
  567. * Just invalidate the entire PID if the address range is too
  568. * large.
  569. */
  570. mmio_invalidate(npu_context, 0, 0, true);
  571. } else {
  572. for (address = start; address < end; address += PAGE_SIZE)
  573. mmio_invalidate(npu_context, 1, address, false);
  574. /* Do the flush only on the final addess == end */
  575. mmio_invalidate(npu_context, 1, address, true);
  576. }
  577. }
  578. static const struct mmu_notifier_ops nv_nmmu_notifier_ops = {
  579. .release = pnv_npu2_mn_release,
  580. .change_pte = pnv_npu2_mn_change_pte,
  581. .invalidate_range = pnv_npu2_mn_invalidate_range,
  582. };
  583. /*
  584. * Call into OPAL to setup the nmmu context for the current task in
  585. * the NPU. This must be called to setup the context tables before the
  586. * GPU issues ATRs. pdev should be a pointed to PCIe GPU device.
  587. *
  588. * A release callback should be registered to allow a device driver to
  589. * be notified that it should not launch any new translation requests
  590. * as the final TLB invalidate is about to occur.
  591. *
  592. * Returns an error if there no contexts are currently available or a
  593. * npu_context which should be passed to pnv_npu2_handle_fault().
  594. *
  595. * mmap_sem must be held in write mode and must not be called from interrupt
  596. * context.
  597. */
  598. struct npu_context *pnv_npu2_init_context(struct pci_dev *gpdev,
  599. unsigned long flags,
  600. void (*cb)(struct npu_context *, void *),
  601. void *priv)
  602. {
  603. int rc;
  604. u32 nvlink_index;
  605. struct device_node *nvlink_dn;
  606. struct mm_struct *mm = current->mm;
  607. struct pnv_phb *nphb;
  608. struct npu *npu;
  609. struct npu_context *npu_context;
  610. /*
  611. * At present we don't support GPUs connected to multiple NPUs and I'm
  612. * not sure the hardware does either.
  613. */
  614. struct pci_dev *npdev = pnv_pci_get_npu_dev(gpdev, 0);
  615. if (!firmware_has_feature(FW_FEATURE_OPAL))
  616. return ERR_PTR(-ENODEV);
  617. if (!npdev)
  618. /* No nvlink associated with this GPU device */
  619. return ERR_PTR(-ENODEV);
  620. nvlink_dn = of_parse_phandle(npdev->dev.of_node, "ibm,nvlink", 0);
  621. if (WARN_ON(of_property_read_u32(nvlink_dn, "ibm,npu-link-index",
  622. &nvlink_index)))
  623. return ERR_PTR(-ENODEV);
  624. if (!mm || mm->context.id == 0) {
  625. /*
  626. * Kernel thread contexts are not supported and context id 0 is
  627. * reserved on the GPU.
  628. */
  629. return ERR_PTR(-EINVAL);
  630. }
  631. nphb = pci_bus_to_host(npdev->bus)->private_data;
  632. npu = &nphb->npu;
  633. /*
  634. * Setup the NPU context table for a particular GPU. These need to be
  635. * per-GPU as we need the tables to filter ATSDs when there are no
  636. * active contexts on a particular GPU. It is safe for these to be
  637. * called concurrently with destroy as the OPAL call takes appropriate
  638. * locks and refcounts on init/destroy.
  639. */
  640. rc = opal_npu_init_context(nphb->opal_id, mm->context.id, flags,
  641. PCI_DEVID(gpdev->bus->number, gpdev->devfn));
  642. if (rc < 0)
  643. return ERR_PTR(-ENOSPC);
  644. /*
  645. * We store the npu pci device so we can more easily get at the
  646. * associated npus.
  647. */
  648. spin_lock(&npu_context_lock);
  649. npu_context = mm->context.npu_context;
  650. if (npu_context) {
  651. if (npu_context->release_cb != cb ||
  652. npu_context->priv != priv) {
  653. spin_unlock(&npu_context_lock);
  654. opal_npu_destroy_context(nphb->opal_id, mm->context.id,
  655. PCI_DEVID(gpdev->bus->number,
  656. gpdev->devfn));
  657. return ERR_PTR(-EINVAL);
  658. }
  659. WARN_ON(!kref_get_unless_zero(&npu_context->kref));
  660. }
  661. spin_unlock(&npu_context_lock);
  662. if (!npu_context) {
  663. /*
  664. * We can set up these fields without holding the
  665. * npu_context_lock as the npu_context hasn't been returned to
  666. * the caller meaning it can't be destroyed. Parallel allocation
  667. * is protected against by mmap_sem.
  668. */
  669. rc = -ENOMEM;
  670. npu_context = kzalloc(sizeof(struct npu_context), GFP_KERNEL);
  671. if (npu_context) {
  672. kref_init(&npu_context->kref);
  673. npu_context->mm = mm;
  674. npu_context->mn.ops = &nv_nmmu_notifier_ops;
  675. rc = __mmu_notifier_register(&npu_context->mn, mm);
  676. }
  677. if (rc) {
  678. kfree(npu_context);
  679. opal_npu_destroy_context(nphb->opal_id, mm->context.id,
  680. PCI_DEVID(gpdev->bus->number,
  681. gpdev->devfn));
  682. return ERR_PTR(rc);
  683. }
  684. mm->context.npu_context = npu_context;
  685. }
  686. npu_context->release_cb = cb;
  687. npu_context->priv = priv;
  688. /*
  689. * npdev is a pci_dev pointer setup by the PCI code. We assign it to
  690. * npdev[][] to indicate to the mmu notifiers that an invalidation
  691. * should also be sent over this nvlink. The notifiers don't use any
  692. * other fields in npu_context, so we just need to ensure that when they
  693. * deference npu_context->npdev[][] it is either a valid pointer or
  694. * NULL.
  695. */
  696. WRITE_ONCE(npu_context->npdev[npu->index][nvlink_index], npdev);
  697. if (!nphb->npu.nmmu_flush) {
  698. /*
  699. * If we're not explicitly flushing ourselves we need to mark
  700. * the thread for global flushes
  701. */
  702. npu_context->nmmu_flush = false;
  703. mm_context_add_copro(mm);
  704. } else
  705. npu_context->nmmu_flush = true;
  706. return npu_context;
  707. }
  708. EXPORT_SYMBOL(pnv_npu2_init_context);
  709. static void pnv_npu2_release_context(struct kref *kref)
  710. {
  711. struct npu_context *npu_context =
  712. container_of(kref, struct npu_context, kref);
  713. if (!npu_context->nmmu_flush)
  714. mm_context_remove_copro(npu_context->mm);
  715. npu_context->mm->context.npu_context = NULL;
  716. }
  717. /*
  718. * Destroy a context on the given GPU. May free the npu_context if it is no
  719. * longer active on any GPUs. Must not be called from interrupt context.
  720. */
  721. void pnv_npu2_destroy_context(struct npu_context *npu_context,
  722. struct pci_dev *gpdev)
  723. {
  724. int removed;
  725. struct pnv_phb *nphb;
  726. struct npu *npu;
  727. struct pci_dev *npdev = pnv_pci_get_npu_dev(gpdev, 0);
  728. struct device_node *nvlink_dn;
  729. u32 nvlink_index;
  730. if (WARN_ON(!npdev))
  731. return;
  732. if (!firmware_has_feature(FW_FEATURE_OPAL))
  733. return;
  734. nphb = pci_bus_to_host(npdev->bus)->private_data;
  735. npu = &nphb->npu;
  736. nvlink_dn = of_parse_phandle(npdev->dev.of_node, "ibm,nvlink", 0);
  737. if (WARN_ON(of_property_read_u32(nvlink_dn, "ibm,npu-link-index",
  738. &nvlink_index)))
  739. return;
  740. WRITE_ONCE(npu_context->npdev[npu->index][nvlink_index], NULL);
  741. opal_npu_destroy_context(nphb->opal_id, npu_context->mm->context.id,
  742. PCI_DEVID(gpdev->bus->number, gpdev->devfn));
  743. spin_lock(&npu_context_lock);
  744. removed = kref_put(&npu_context->kref, pnv_npu2_release_context);
  745. spin_unlock(&npu_context_lock);
  746. /*
  747. * We need to do this outside of pnv_npu2_release_context so that it is
  748. * outside the spinlock as mmu_notifier_destroy uses SRCU.
  749. */
  750. if (removed) {
  751. mmu_notifier_unregister(&npu_context->mn,
  752. npu_context->mm);
  753. kfree(npu_context);
  754. }
  755. }
  756. EXPORT_SYMBOL(pnv_npu2_destroy_context);
  757. /*
  758. * Assumes mmap_sem is held for the contexts associated mm.
  759. */
  760. int pnv_npu2_handle_fault(struct npu_context *context, uintptr_t *ea,
  761. unsigned long *flags, unsigned long *status, int count)
  762. {
  763. u64 rc = 0, result = 0;
  764. int i, is_write;
  765. struct page *page[1];
  766. /* mmap_sem should be held so the struct_mm must be present */
  767. struct mm_struct *mm = context->mm;
  768. if (!firmware_has_feature(FW_FEATURE_OPAL))
  769. return -ENODEV;
  770. WARN_ON(!rwsem_is_locked(&mm->mmap_sem));
  771. for (i = 0; i < count; i++) {
  772. is_write = flags[i] & NPU2_WRITE;
  773. rc = get_user_pages_remote(NULL, mm, ea[i], 1,
  774. is_write ? FOLL_WRITE : 0,
  775. page, NULL, NULL);
  776. /*
  777. * To support virtualised environments we will have to do an
  778. * access to the page to ensure it gets faulted into the
  779. * hypervisor. For the moment virtualisation is not supported in
  780. * other areas so leave the access out.
  781. */
  782. if (rc != 1) {
  783. status[i] = rc;
  784. result = -EFAULT;
  785. continue;
  786. }
  787. status[i] = 0;
  788. put_page(page[0]);
  789. }
  790. return result;
  791. }
  792. EXPORT_SYMBOL(pnv_npu2_handle_fault);
  793. int pnv_npu2_init(struct pnv_phb *phb)
  794. {
  795. unsigned int i;
  796. u64 mmio_atsd;
  797. struct device_node *dn;
  798. struct pci_dev *gpdev;
  799. static int npu_index;
  800. uint64_t rc = 0;
  801. phb->npu.nmmu_flush =
  802. of_property_read_bool(phb->hose->dn, "ibm,nmmu-flush");
  803. for_each_child_of_node(phb->hose->dn, dn) {
  804. gpdev = pnv_pci_get_gpu_dev(get_pci_dev(dn));
  805. if (gpdev) {
  806. rc = opal_npu_map_lpar(phb->opal_id,
  807. PCI_DEVID(gpdev->bus->number, gpdev->devfn),
  808. 0, 0);
  809. if (rc)
  810. dev_err(&gpdev->dev,
  811. "Error %lld mapping device to LPAR\n",
  812. rc);
  813. }
  814. }
  815. for (i = 0; !of_property_read_u64_index(phb->hose->dn, "ibm,mmio-atsd",
  816. i, &mmio_atsd); i++)
  817. phb->npu.mmio_atsd_regs[i] = ioremap(mmio_atsd, 32);
  818. pr_info("NPU%lld: Found %d MMIO ATSD registers", phb->opal_id, i);
  819. phb->npu.mmio_atsd_count = i;
  820. phb->npu.mmio_atsd_usage = 0;
  821. npu_index++;
  822. if (WARN_ON(npu_index >= NV_MAX_NPUS))
  823. return -ENOSPC;
  824. max_npu2_index = npu_index;
  825. phb->npu.index = npu_index;
  826. return 0;
  827. }