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.
5 * Copyright Alistair Popple, IBM Corporation 2015.
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.
12 #include <linux/mmu_notifier.h>
13 #include <linux/mmu_context.h>
15 #include <linux/pci.h>
16 #include <linux/memblock.h>
17 #include <linux/sizes.h>
19 #include <asm/debugfs.h>
20 #include <asm/powernv.h>
26 * spinlock to protect initialisation of an npu_context for a particular
29 static DEFINE_SPINLOCK(npu_context_lock);
32 * Other types of TCE cache invalidation are not functional in the
35 static struct pci_dev *get_pci_dev(struct device_node *dn)
37 struct pci_dn *pdn = PCI_DN(dn);
39 return pci_get_domain_bus_and_slot(pci_domain_nr(pdn->phb->bus),
40 pdn->busno, pdn->devfn);
43 /* Given a NPU device get the associated PCI device. */
44 struct pci_dev *pnv_pci_get_gpu_dev(struct pci_dev *npdev)
46 struct device_node *dn;
47 struct pci_dev *gpdev;
52 if (WARN_ON(!npdev->dev.of_node))
55 /* Get assoicated PCI device */
56 dn = of_parse_phandle(npdev->dev.of_node, "ibm,gpu", 0);
60 gpdev = get_pci_dev(dn);
65 EXPORT_SYMBOL(pnv_pci_get_gpu_dev);
67 /* Given the real PCI device get a linked NPU device. */
68 struct pci_dev *pnv_pci_get_npu_dev(struct pci_dev *gpdev, int index)
70 struct device_node *dn;
71 struct pci_dev *npdev;
76 /* Not all PCI devices have device-tree nodes */
77 if (!gpdev->dev.of_node)
80 /* Get assoicated PCI device */
81 dn = of_parse_phandle(gpdev->dev.of_node, "ibm,npu", index);
85 npdev = get_pci_dev(dn);
90 EXPORT_SYMBOL(pnv_pci_get_npu_dev);
93 * Returns the PE assoicated with the PCI device of the given
94 * NPU. Returns the linked pci device if pci_dev != NULL.
96 static struct pnv_ioda_pe *get_gpu_pci_dev_and_pe(struct pnv_ioda_pe *npe,
97 struct pci_dev **gpdev)
100 struct pci_controller *hose;
101 struct pci_dev *pdev;
102 struct pnv_ioda_pe *pe;
105 pdev = pnv_pci_get_gpu_dev(npe->pdev);
109 pdn = pci_get_pdn(pdev);
110 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
113 hose = pci_bus_to_host(pdev->bus);
114 phb = hose->private_data;
115 pe = &phb->ioda.pe_array[pdn->pe_number];
123 static long pnv_npu_unset_window(struct iommu_table_group *table_group,
126 static long pnv_npu_set_window(struct iommu_table_group *table_group, int num,
127 struct iommu_table *tbl)
129 struct pnv_ioda_pe *npe = container_of(table_group, struct pnv_ioda_pe,
131 struct pnv_phb *phb = npe->phb;
133 const unsigned long size = tbl->it_indirect_levels ?
134 tbl->it_level_size : tbl->it_size;
135 const __u64 start_addr = tbl->it_offset << tbl->it_page_shift;
136 const __u64 win_size = tbl->it_size << tbl->it_page_shift;
137 int num2 = (num == 0) ? 1 : 0;
139 /* NPU has just one TVE so if there is another table, remove it first */
140 if (npe->table_group.tables[num2])
141 pnv_npu_unset_window(&npe->table_group, num2);
143 pe_info(npe, "Setting up window %llx..%llx pg=%lx\n",
144 start_addr, start_addr + win_size - 1,
145 IOMMU_PAGE_SIZE(tbl));
147 rc = opal_pci_map_pe_dma_window(phb->opal_id,
150 tbl->it_indirect_levels + 1,
153 IOMMU_PAGE_SIZE(tbl));
155 pe_err(npe, "Failed to configure TCE table, err %lld\n", rc);
158 pnv_pci_ioda2_tce_invalidate_entire(phb, false);
160 /* Add the table to the list so its TCE cache will get invalidated */
161 pnv_pci_link_table_and_group(phb->hose->node, num,
162 tbl, &npe->table_group);
167 static long pnv_npu_unset_window(struct iommu_table_group *table_group, int num)
169 struct pnv_ioda_pe *npe = container_of(table_group, struct pnv_ioda_pe,
171 struct pnv_phb *phb = npe->phb;
174 if (!npe->table_group.tables[num])
177 pe_info(npe, "Removing DMA window\n");
179 rc = opal_pci_map_pe_dma_window(phb->opal_id, npe->pe_number,
181 0/* levels */, 0/* table address */,
182 0/* table size */, 0/* page size */);
184 pe_err(npe, "Unmapping failed, ret = %lld\n", rc);
187 pnv_pci_ioda2_tce_invalidate_entire(phb, false);
189 pnv_pci_unlink_table_and_group(npe->table_group.tables[num],
196 * Enables 32 bit DMA on NPU.
198 static void pnv_npu_dma_set_32(struct pnv_ioda_pe *npe)
200 struct pci_dev *gpdev;
201 struct pnv_ioda_pe *gpe;
205 * Find the assoicated PCI devices and get the dma window
206 * information from there.
208 if (!npe->pdev || !(npe->flags & PNV_IODA_PE_DEV))
211 gpe = get_gpu_pci_dev_and_pe(npe, &gpdev);
215 rc = pnv_npu_set_window(&npe->table_group, 0,
216 gpe->table_group.tables[0]);
219 * NVLink devices use the same TCE table configuration as
220 * their parent device so drivers shouldn't be doing DMA
221 * operations directly on these devices.
223 set_dma_ops(&npe->pdev->dev, NULL);
227 * Enables bypass mode on the NPU. The NPU only supports one
228 * window per link, so bypass needs to be explicitly enabled or
229 * disabled. Unlike for a PHB3 bypass and non-bypass modes can't be
230 * active at the same time.
232 static int pnv_npu_dma_set_bypass(struct pnv_ioda_pe *npe)
234 struct pnv_phb *phb = npe->phb;
236 phys_addr_t top = memblock_end_of_DRAM();
238 if (phb->type != PNV_PHB_NPU_NVLINK || !npe->pdev)
241 rc = pnv_npu_unset_window(&npe->table_group, 0);
242 if (rc != OPAL_SUCCESS)
245 /* Enable the bypass window */
247 top = roundup_pow_of_two(top);
248 dev_info(&npe->pdev->dev, "Enabling bypass for PE %x\n",
250 rc = opal_pci_map_pe_dma_window_real(phb->opal_id,
251 npe->pe_number, npe->pe_number,
252 0 /* bypass base */, top);
254 if (rc == OPAL_SUCCESS)
255 pnv_pci_ioda2_tce_invalidate_entire(phb, false);
260 void pnv_npu_try_dma_set_bypass(struct pci_dev *gpdev, bool bypass)
265 struct pnv_ioda_pe *npe;
266 struct pci_dev *npdev;
269 npdev = pnv_pci_get_npu_dev(gpdev, i);
274 pdn = pci_get_pdn(npdev);
275 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
278 phb = pci_bus_to_host(npdev->bus)->private_data;
280 /* We only do bypass if it's enabled on the linked device */
281 npe = &phb->ioda.pe_array[pdn->pe_number];
284 dev_info(&npdev->dev,
285 "Using 64-bit DMA iommu bypass\n");
286 pnv_npu_dma_set_bypass(npe);
288 dev_info(&npdev->dev, "Using 32-bit DMA via iommu\n");
289 pnv_npu_dma_set_32(npe);
294 #ifdef CONFIG_IOMMU_API
295 /* Switch ownership from platform code to external user (e.g. VFIO) */
296 static void pnv_npu_take_ownership(struct iommu_table_group *table_group)
298 struct pnv_ioda_pe *npe = container_of(table_group, struct pnv_ioda_pe,
300 struct pnv_phb *phb = npe->phb;
302 struct pci_dev *gpdev = NULL;
305 * Note: NPU has just a single TVE in the hardware which means that
306 * while used by the kernel, it can have either 32bit window or
307 * DMA bypass but never both. So we deconfigure 32bit window only
308 * if it was enabled at the moment of ownership change.
310 if (npe->table_group.tables[0]) {
311 pnv_npu_unset_window(&npe->table_group, 0);
316 rc = opal_pci_map_pe_dma_window_real(phb->opal_id,
317 npe->pe_number, npe->pe_number,
318 0 /* bypass base */, 0);
320 pe_err(npe, "Failed to disable bypass, err %lld\n", rc);
323 pnv_pci_ioda2_tce_invalidate_entire(npe->phb, false);
325 get_gpu_pci_dev_and_pe(npe, &gpdev);
327 pnv_npu2_unmap_lpar_dev(gpdev);
330 static void pnv_npu_release_ownership(struct iommu_table_group *table_group)
332 struct pnv_ioda_pe *npe = container_of(table_group, struct pnv_ioda_pe,
334 struct pci_dev *gpdev = NULL;
336 get_gpu_pci_dev_and_pe(npe, &gpdev);
338 pnv_npu2_map_lpar_dev(gpdev, 0, MSR_DR | MSR_PR | MSR_HV);
341 static struct iommu_table_group_ops pnv_pci_npu_ops = {
342 .set_window = pnv_npu_set_window,
343 .unset_window = pnv_npu_unset_window,
344 .take_ownership = pnv_npu_take_ownership,
345 .release_ownership = pnv_npu_release_ownership,
347 #endif /* !CONFIG_IOMMU_API */
352 /* Maximum possible number of ATSD MMIO registers per NPU */
353 #define NV_NMMU_ATSD_REGS 8
354 #define NV_NPU_MAX_PE_NUM 16
357 * A compound NPU IOMMU group which might consist of 1 GPU + 2xNPUs (POWER8) or
358 * up to 3 x (GPU + 2xNPUs) (POWER9).
361 struct iommu_table_group table_group;
363 struct pnv_ioda_pe *pe[NV_NPU_MAX_PE_NUM];
366 /* An NPU descriptor, valid for POWER9 only */
369 __be64 *mmio_atsd_regs[NV_NMMU_ATSD_REGS];
370 unsigned int mmio_atsd_count;
372 /* Bitmask for MMIO register usage */
373 unsigned long mmio_atsd_usage;
375 /* Do we need to explicitly flush the nest mmu? */
378 struct npu_comp npucomp;
381 #ifdef CONFIG_IOMMU_API
382 static long pnv_npu_peers_create_table_userspace(
383 struct iommu_table_group *table_group,
384 int num, __u32 page_shift, __u64 window_size, __u32 levels,
385 struct iommu_table **ptbl)
387 struct npu_comp *npucomp = container_of(table_group, struct npu_comp,
390 if (!npucomp->pe_num || !npucomp->pe[0] ||
391 !npucomp->pe[0]->table_group.ops ||
392 !npucomp->pe[0]->table_group.ops->create_table)
395 return npucomp->pe[0]->table_group.ops->create_table(
396 &npucomp->pe[0]->table_group, num, page_shift,
397 window_size, levels, ptbl);
400 static long pnv_npu_peers_set_window(struct iommu_table_group *table_group,
401 int num, struct iommu_table *tbl)
405 struct npu_comp *npucomp = container_of(table_group, struct npu_comp,
408 for (i = 0; i < npucomp->pe_num; ++i) {
409 struct pnv_ioda_pe *pe = npucomp->pe[i];
411 if (!pe->table_group.ops->set_window)
414 ret = pe->table_group.ops->set_window(&pe->table_group,
421 for (j = 0; j < i; ++j) {
422 struct pnv_ioda_pe *pe = npucomp->pe[j];
424 if (!pe->table_group.ops->unset_window)
427 ret = pe->table_group.ops->unset_window(
428 &pe->table_group, num);
433 table_group->tables[num] = iommu_tce_table_get(tbl);
439 static long pnv_npu_peers_unset_window(struct iommu_table_group *table_group,
444 struct npu_comp *npucomp = container_of(table_group, struct npu_comp,
447 for (i = 0; i < npucomp->pe_num; ++i) {
448 struct pnv_ioda_pe *pe = npucomp->pe[i];
450 WARN_ON(npucomp->table_group.tables[num] !=
451 table_group->tables[num]);
452 if (!npucomp->table_group.tables[num])
455 if (!pe->table_group.ops->unset_window)
458 ret = pe->table_group.ops->unset_window(&pe->table_group, num);
464 for (j = 0; j < i; ++j) {
465 struct pnv_ioda_pe *pe = npucomp->pe[j];
467 if (!npucomp->table_group.tables[num])
470 if (!pe->table_group.ops->set_window)
473 ret = pe->table_group.ops->set_window(&pe->table_group,
474 num, table_group->tables[num]);
478 } else if (table_group->tables[num]) {
479 iommu_tce_table_put(table_group->tables[num]);
480 table_group->tables[num] = NULL;
486 static void pnv_npu_peers_take_ownership(struct iommu_table_group *table_group)
489 struct npu_comp *npucomp = container_of(table_group, struct npu_comp,
492 for (i = 0; i < npucomp->pe_num; ++i) {
493 struct pnv_ioda_pe *pe = npucomp->pe[i];
495 if (!pe->table_group.ops->take_ownership)
497 pe->table_group.ops->take_ownership(&pe->table_group);
501 static void pnv_npu_peers_release_ownership(
502 struct iommu_table_group *table_group)
505 struct npu_comp *npucomp = container_of(table_group, struct npu_comp,
508 for (i = 0; i < npucomp->pe_num; ++i) {
509 struct pnv_ioda_pe *pe = npucomp->pe[i];
511 if (!pe->table_group.ops->release_ownership)
513 pe->table_group.ops->release_ownership(&pe->table_group);
517 static struct iommu_table_group_ops pnv_npu_peers_ops = {
518 .get_table_size = pnv_pci_ioda2_get_table_size,
519 .create_table = pnv_npu_peers_create_table_userspace,
520 .set_window = pnv_npu_peers_set_window,
521 .unset_window = pnv_npu_peers_unset_window,
522 .take_ownership = pnv_npu_peers_take_ownership,
523 .release_ownership = pnv_npu_peers_release_ownership,
526 static void pnv_comp_attach_table_group(struct npu_comp *npucomp,
527 struct pnv_ioda_pe *pe)
529 if (WARN_ON(npucomp->pe_num == NV_NPU_MAX_PE_NUM))
532 npucomp->pe[npucomp->pe_num] = pe;
536 struct iommu_table_group *pnv_try_setup_npu_table_group(struct pnv_ioda_pe *pe)
538 struct iommu_table_group *table_group;
539 struct npu_comp *npucomp;
540 struct pci_dev *gpdev = NULL;
541 struct pci_controller *hose;
542 struct pci_dev *npdev = NULL;
544 list_for_each_entry(gpdev, &pe->pbus->devices, bus_list) {
545 npdev = pnv_pci_get_npu_dev(gpdev, 0);
551 /* It is not an NPU attached device, skip */
554 hose = pci_bus_to_host(npdev->bus);
557 table_group = &hose->npu->npucomp.table_group;
559 if (!table_group->group) {
560 table_group->ops = &pnv_npu_peers_ops;
561 iommu_register_group(table_group,
566 /* Create a group for 1 GPU and attached NPUs for POWER8 */
567 pe->npucomp = kzalloc(sizeof(*pe->npucomp), GFP_KERNEL);
568 table_group = &pe->npucomp->table_group;
569 table_group->ops = &pnv_npu_peers_ops;
570 iommu_register_group(table_group, hose->global_number,
574 /* Steal capabilities from a GPU PE */
575 table_group->max_dynamic_windows_supported =
576 pe->table_group.max_dynamic_windows_supported;
577 table_group->tce32_start = pe->table_group.tce32_start;
578 table_group->tce32_size = pe->table_group.tce32_size;
579 table_group->max_levels = pe->table_group.max_levels;
580 if (!table_group->pgsizes)
581 table_group->pgsizes = pe->table_group.pgsizes;
583 npucomp = container_of(table_group, struct npu_comp, table_group);
584 pnv_comp_attach_table_group(npucomp, pe);
589 struct iommu_table_group *pnv_npu_compound_attach(struct pnv_ioda_pe *pe)
591 struct iommu_table_group *table_group;
592 struct npu_comp *npucomp;
593 struct pci_dev *gpdev = NULL;
594 struct pci_dev *npdev;
595 struct pnv_ioda_pe *gpe = get_gpu_pci_dev_and_pe(pe, &gpdev);
597 WARN_ON(!(pe->flags & PNV_IODA_PE_DEV));
602 * IODA2 bridges get this set up from pci_controller_ops::setup_bridge
603 * but NPU bridges do not have this hook defined so we do it here.
604 * We do not setup other table group parameters as they won't be used
605 * anyway - NVLink bridges are subordinate PEs.
607 pe->table_group.ops = &pnv_pci_npu_ops;
609 table_group = iommu_group_get_iommudata(
610 iommu_group_get(&gpdev->dev));
613 * On P9 NPU PHB and PCI PHB support different page sizes,
614 * keep only matching. We expect here that NVLink bridge PE pgsizes is
615 * initialized by the caller.
617 table_group->pgsizes &= pe->table_group.pgsizes;
618 npucomp = container_of(table_group, struct npu_comp, table_group);
619 pnv_comp_attach_table_group(npucomp, pe);
621 list_for_each_entry(npdev, &pe->phb->hose->bus->devices, bus_list) {
622 struct pci_dev *gpdevtmp = pnv_pci_get_gpu_dev(npdev);
624 if (gpdevtmp != gpdev)
627 iommu_add_device(table_group, &npdev->dev);
632 #endif /* CONFIG_IOMMU_API */
634 /* Maximum number of nvlinks per npu */
635 #define NV_MAX_LINKS 6
637 /* Maximum index of npu2 hosts in the system. Always < NV_MAX_NPUS */
638 static int max_npu2_index;
641 struct mm_struct *mm;
642 struct pci_dev *npdev[NV_MAX_NPUS][NV_MAX_LINKS];
643 struct mmu_notifier mn;
647 /* Callback to stop translation requests on a given GPU */
648 void (*release_cb)(struct npu_context *context, void *priv);
651 * Private pointer passed to the above callback for usage by
657 struct mmio_atsd_reg {
663 * Find a free MMIO ATSD register and mark it in use. Return -ENOSPC
664 * if none are available.
666 static int get_mmio_atsd_reg(struct npu *npu)
670 for (i = 0; i < npu->mmio_atsd_count; i++) {
671 if (!test_bit(i, &npu->mmio_atsd_usage))
672 if (!test_and_set_bit_lock(i, &npu->mmio_atsd_usage))
679 static void put_mmio_atsd_reg(struct npu *npu, int reg)
681 clear_bit_unlock(reg, &npu->mmio_atsd_usage);
684 /* MMIO ATSD register offsets */
685 #define XTS_ATSD_LAUNCH 0
686 #define XTS_ATSD_AVA 1
687 #define XTS_ATSD_STAT 2
689 static unsigned long get_atsd_launch_val(unsigned long pid, unsigned long psize)
691 unsigned long launch = 0;
693 if (psize == MMU_PAGE_COUNT) {
694 /* IS set to invalidate entire matching PID */
695 launch |= PPC_BIT(12);
697 /* AP set to invalidate region of psize */
698 launch |= (u64)mmu_get_ap(psize) << PPC_BITLSHIFT(17);
701 /* PRS set to process-scoped */
702 launch |= PPC_BIT(13);
705 launch |= pid << PPC_BITLSHIFT(38);
707 /* Leave "No flush" (bit 39) 0 so every ATSD performs a flush */
712 static void mmio_atsd_regs_write(struct mmio_atsd_reg
713 mmio_atsd_reg[NV_MAX_NPUS], unsigned long offset,
719 for (i = 0; i <= max_npu2_index; i++) {
720 reg = mmio_atsd_reg[i].reg;
724 npu = mmio_atsd_reg[i].npu;
725 __raw_writeq_be(val, npu->mmio_atsd_regs[reg] + offset);
729 static void mmio_invalidate_pid(struct mmio_atsd_reg mmio_atsd_reg[NV_MAX_NPUS],
732 unsigned long launch = get_atsd_launch_val(pid, MMU_PAGE_COUNT);
734 /* Invalidating the entire process doesn't use a va */
735 mmio_atsd_regs_write(mmio_atsd_reg, XTS_ATSD_LAUNCH, launch);
738 static void mmio_invalidate_range(struct mmio_atsd_reg
739 mmio_atsd_reg[NV_MAX_NPUS], unsigned long pid,
740 unsigned long start, unsigned long psize)
742 unsigned long launch = get_atsd_launch_val(pid, psize);
744 /* Write all VAs first */
745 mmio_atsd_regs_write(mmio_atsd_reg, XTS_ATSD_AVA, start);
747 /* Issue one barrier for all address writes */
751 mmio_atsd_regs_write(mmio_atsd_reg, XTS_ATSD_LAUNCH, launch);
754 #define mn_to_npu_context(x) container_of(x, struct npu_context, mn)
756 static void mmio_invalidate_wait(
757 struct mmio_atsd_reg mmio_atsd_reg[NV_MAX_NPUS])
762 /* Wait for all invalidations to complete */
763 for (i = 0; i <= max_npu2_index; i++) {
764 if (mmio_atsd_reg[i].reg < 0)
767 /* Wait for completion */
768 npu = mmio_atsd_reg[i].npu;
769 reg = mmio_atsd_reg[i].reg;
770 while (__raw_readq(npu->mmio_atsd_regs[reg] + XTS_ATSD_STAT))
776 * Acquires all the address translation shootdown (ATSD) registers required to
777 * launch an ATSD on all links this npu_context is active on.
779 static void acquire_atsd_reg(struct npu_context *npu_context,
780 struct mmio_atsd_reg mmio_atsd_reg[NV_MAX_NPUS])
784 struct pci_dev *npdev;
786 for (i = 0; i <= max_npu2_index; i++) {
787 mmio_atsd_reg[i].reg = -1;
788 for (j = 0; j < NV_MAX_LINKS; j++) {
790 * There are no ordering requirements with respect to
791 * the setup of struct npu_context, but to ensure
792 * consistent behaviour we need to ensure npdev[][] is
795 npdev = READ_ONCE(npu_context->npdev[i][j]);
799 npu = pci_bus_to_host(npdev->bus)->npu;
803 mmio_atsd_reg[i].npu = npu;
804 mmio_atsd_reg[i].reg = get_mmio_atsd_reg(npu);
805 while (mmio_atsd_reg[i].reg < 0) {
806 mmio_atsd_reg[i].reg = get_mmio_atsd_reg(npu);
815 * Release previously acquired ATSD registers. To avoid deadlocks the registers
816 * must be released in the same order they were acquired above in
819 static void release_atsd_reg(struct mmio_atsd_reg mmio_atsd_reg[NV_MAX_NPUS])
823 for (i = 0; i <= max_npu2_index; i++) {
825 * We can't rely on npu_context->npdev[][] being the same here
826 * as when acquire_atsd_reg() was called, hence we use the
827 * values stored in mmio_atsd_reg during the acquire phase
828 * rather than re-reading npdev[][].
830 if (mmio_atsd_reg[i].reg < 0)
833 put_mmio_atsd_reg(mmio_atsd_reg[i].npu, mmio_atsd_reg[i].reg);
838 * Invalidate a virtual address range
840 static void mmio_invalidate(struct npu_context *npu_context,
841 unsigned long start, unsigned long size)
843 struct mmio_atsd_reg mmio_atsd_reg[NV_MAX_NPUS];
844 unsigned long pid = npu_context->mm->context.id;
845 unsigned long atsd_start = 0;
846 unsigned long end = start + size - 1;
847 int atsd_psize = MMU_PAGE_COUNT;
850 * Convert the input range into one of the supported sizes. If the range
851 * doesn't fit, use the next larger supported size. Invalidation latency
852 * is high, so over-invalidation is preferred to issuing multiple
855 * A 4K page size isn't supported by NPU/GPU ATS, so that case is
858 if (size == SZ_64K) {
860 atsd_psize = MMU_PAGE_64K;
861 } else if (ALIGN_DOWN(start, SZ_2M) == ALIGN_DOWN(end, SZ_2M)) {
862 atsd_start = ALIGN_DOWN(start, SZ_2M);
863 atsd_psize = MMU_PAGE_2M;
864 } else if (ALIGN_DOWN(start, SZ_1G) == ALIGN_DOWN(end, SZ_1G)) {
865 atsd_start = ALIGN_DOWN(start, SZ_1G);
866 atsd_psize = MMU_PAGE_1G;
869 if (npu_context->nmmu_flush)
871 * Unfortunately the nest mmu does not support flushing specific
872 * addresses so we have to flush the whole mm once before
873 * shooting down the GPU translation.
875 flush_all_mm(npu_context->mm);
878 * Loop over all the NPUs this process is active on and launch
881 acquire_atsd_reg(npu_context, mmio_atsd_reg);
883 if (atsd_psize == MMU_PAGE_COUNT)
884 mmio_invalidate_pid(mmio_atsd_reg, pid);
886 mmio_invalidate_range(mmio_atsd_reg, pid, atsd_start,
889 mmio_invalidate_wait(mmio_atsd_reg);
892 * The GPU requires two flush ATSDs to ensure all entries have been
893 * flushed. We use PID 0 as it will never be used for a process on the
896 mmio_invalidate_pid(mmio_atsd_reg, 0);
897 mmio_invalidate_wait(mmio_atsd_reg);
898 mmio_invalidate_pid(mmio_atsd_reg, 0);
899 mmio_invalidate_wait(mmio_atsd_reg);
901 release_atsd_reg(mmio_atsd_reg);
904 static void pnv_npu2_mn_release(struct mmu_notifier *mn,
905 struct mm_struct *mm)
907 struct npu_context *npu_context = mn_to_npu_context(mn);
909 /* Call into device driver to stop requests to the NMMU */
910 if (npu_context->release_cb)
911 npu_context->release_cb(npu_context, npu_context->priv);
914 * There should be no more translation requests for this PID, but we
915 * need to ensure any entries for it are removed from the TLB.
917 mmio_invalidate(npu_context, 0, ~0UL);
920 static void pnv_npu2_mn_change_pte(struct mmu_notifier *mn,
921 struct mm_struct *mm,
922 unsigned long address,
925 struct npu_context *npu_context = mn_to_npu_context(mn);
926 mmio_invalidate(npu_context, address, PAGE_SIZE);
929 static void pnv_npu2_mn_invalidate_range(struct mmu_notifier *mn,
930 struct mm_struct *mm,
931 unsigned long start, unsigned long end)
933 struct npu_context *npu_context = mn_to_npu_context(mn);
934 mmio_invalidate(npu_context, start, end - start);
937 static const struct mmu_notifier_ops nv_nmmu_notifier_ops = {
938 .release = pnv_npu2_mn_release,
939 .change_pte = pnv_npu2_mn_change_pte,
940 .invalidate_range = pnv_npu2_mn_invalidate_range,
944 * Call into OPAL to setup the nmmu context for the current task in
945 * the NPU. This must be called to setup the context tables before the
946 * GPU issues ATRs. pdev should be a pointed to PCIe GPU device.
948 * A release callback should be registered to allow a device driver to
949 * be notified that it should not launch any new translation requests
950 * as the final TLB invalidate is about to occur.
952 * Returns an error if there no contexts are currently available or a
953 * npu_context which should be passed to pnv_npu2_handle_fault().
955 * mmap_sem must be held in write mode and must not be called from interrupt
958 struct npu_context *pnv_npu2_init_context(struct pci_dev *gpdev,
960 void (*cb)(struct npu_context *, void *),
965 struct device_node *nvlink_dn;
966 struct mm_struct *mm = current->mm;
968 struct npu_context *npu_context;
969 struct pci_controller *hose;
972 * At present we don't support GPUs connected to multiple NPUs and I'm
973 * not sure the hardware does either.
975 struct pci_dev *npdev = pnv_pci_get_npu_dev(gpdev, 0);
978 /* No nvlink associated with this GPU device */
979 return ERR_PTR(-ENODEV);
981 /* We only support DR/PR/HV in pnv_npu2_map_lpar_dev() */
982 if (flags & ~(MSR_DR | MSR_PR | MSR_HV))
983 return ERR_PTR(-EINVAL);
985 nvlink_dn = of_parse_phandle(npdev->dev.of_node, "ibm,nvlink", 0);
986 if (WARN_ON(of_property_read_u32(nvlink_dn, "ibm,npu-link-index",
988 return ERR_PTR(-ENODEV);
990 if (!mm || mm->context.id == 0) {
992 * Kernel thread contexts are not supported and context id 0 is
993 * reserved on the GPU.
995 return ERR_PTR(-EINVAL);
998 hose = pci_bus_to_host(npdev->bus);
1001 return ERR_PTR(-ENODEV);
1004 * We store the npu pci device so we can more easily get at the
1007 spin_lock(&npu_context_lock);
1008 npu_context = mm->context.npu_context;
1010 if (npu_context->release_cb != cb ||
1011 npu_context->priv != priv) {
1012 spin_unlock(&npu_context_lock);
1013 return ERR_PTR(-EINVAL);
1016 WARN_ON(!kref_get_unless_zero(&npu_context->kref));
1018 spin_unlock(&npu_context_lock);
1022 * We can set up these fields without holding the
1023 * npu_context_lock as the npu_context hasn't been returned to
1024 * the caller meaning it can't be destroyed. Parallel allocation
1025 * is protected against by mmap_sem.
1028 npu_context = kzalloc(sizeof(struct npu_context), GFP_KERNEL);
1030 kref_init(&npu_context->kref);
1031 npu_context->mm = mm;
1032 npu_context->mn.ops = &nv_nmmu_notifier_ops;
1033 rc = __mmu_notifier_register(&npu_context->mn, mm);
1041 mm->context.npu_context = npu_context;
1044 npu_context->release_cb = cb;
1045 npu_context->priv = priv;
1048 * npdev is a pci_dev pointer setup by the PCI code. We assign it to
1049 * npdev[][] to indicate to the mmu notifiers that an invalidation
1050 * should also be sent over this nvlink. The notifiers don't use any
1051 * other fields in npu_context, so we just need to ensure that when they
1052 * deference npu_context->npdev[][] it is either a valid pointer or
1055 WRITE_ONCE(npu_context->npdev[npu->index][nvlink_index], npdev);
1057 if (!npu->nmmu_flush) {
1059 * If we're not explicitly flushing ourselves we need to mark
1060 * the thread for global flushes
1062 npu_context->nmmu_flush = false;
1063 mm_context_add_copro(mm);
1065 npu_context->nmmu_flush = true;
1069 EXPORT_SYMBOL(pnv_npu2_init_context);
1071 static void pnv_npu2_release_context(struct kref *kref)
1073 struct npu_context *npu_context =
1074 container_of(kref, struct npu_context, kref);
1076 if (!npu_context->nmmu_flush)
1077 mm_context_remove_copro(npu_context->mm);
1079 npu_context->mm->context.npu_context = NULL;
1083 * Destroy a context on the given GPU. May free the npu_context if it is no
1084 * longer active on any GPUs. Must not be called from interrupt context.
1086 void pnv_npu2_destroy_context(struct npu_context *npu_context,
1087 struct pci_dev *gpdev)
1091 struct pci_dev *npdev = pnv_pci_get_npu_dev(gpdev, 0);
1092 struct device_node *nvlink_dn;
1094 struct pci_controller *hose;
1096 if (WARN_ON(!npdev))
1099 hose = pci_bus_to_host(npdev->bus);
1103 nvlink_dn = of_parse_phandle(npdev->dev.of_node, "ibm,nvlink", 0);
1104 if (WARN_ON(of_property_read_u32(nvlink_dn, "ibm,npu-link-index",
1107 WRITE_ONCE(npu_context->npdev[npu->index][nvlink_index], NULL);
1108 spin_lock(&npu_context_lock);
1109 removed = kref_put(&npu_context->kref, pnv_npu2_release_context);
1110 spin_unlock(&npu_context_lock);
1113 * We need to do this outside of pnv_npu2_release_context so that it is
1114 * outside the spinlock as mmu_notifier_destroy uses SRCU.
1117 mmu_notifier_unregister(&npu_context->mn,
1124 EXPORT_SYMBOL(pnv_npu2_destroy_context);
1127 * Assumes mmap_sem is held for the contexts associated mm.
1129 int pnv_npu2_handle_fault(struct npu_context *context, uintptr_t *ea,
1130 unsigned long *flags, unsigned long *status, int count)
1132 u64 rc = 0, result = 0;
1134 struct page *page[1];
1135 const char __user *u;
1138 /* mmap_sem should be held so the struct_mm must be present */
1139 struct mm_struct *mm = context->mm;
1141 WARN_ON(!rwsem_is_locked(&mm->mmap_sem));
1143 for (i = 0; i < count; i++) {
1144 is_write = flags[i] & NPU2_WRITE;
1145 rc = get_user_pages_remote(NULL, mm, ea[i], 1,
1146 is_write ? FOLL_WRITE : 0,
1155 /* Make sure partition scoped tree gets a pte */
1156 u = page_address(page[0]);
1157 if (__get_user(c, u))
1166 EXPORT_SYMBOL(pnv_npu2_handle_fault);
1168 int pnv_npu2_init(struct pci_controller *hose)
1172 static int npu_index;
1176 npu = kzalloc(sizeof(*npu), GFP_KERNEL);
1180 npu->nmmu_flush = of_property_read_bool(hose->dn, "ibm,nmmu-flush");
1182 for (i = 0; i < ARRAY_SIZE(npu->mmio_atsd_regs) &&
1183 !of_property_read_u64_index(hose->dn, "ibm,mmio-atsd",
1184 i, &mmio_atsd); i++)
1185 npu->mmio_atsd_regs[i] = ioremap(mmio_atsd, 32);
1187 pr_info("NPU%d: Found %d MMIO ATSD registers", hose->global_number, i);
1188 npu->mmio_atsd_count = i;
1189 npu->mmio_atsd_usage = 0;
1191 if (WARN_ON(npu_index >= NV_MAX_NPUS)) {
1195 max_npu2_index = npu_index;
1196 npu->index = npu_index;
1202 for (i = 0; i < npu->mmio_atsd_count; ++i)
1203 iounmap(npu->mmio_atsd_regs[i]);
1210 int pnv_npu2_map_lpar_dev(struct pci_dev *gpdev, unsigned int lparid,
1214 struct pci_dev *npdev = pnv_pci_get_npu_dev(gpdev, 0);
1215 struct pci_controller *hose;
1216 struct pnv_phb *nphb;
1221 hose = pci_bus_to_host(npdev->bus);
1222 nphb = hose->private_data;
1224 dev_dbg(&gpdev->dev, "Map LPAR opalid=%llu lparid=%u\n",
1225 nphb->opal_id, lparid);
1227 * Currently we only support radix and non-zero LPCR only makes sense
1228 * for hash tables so skiboot expects the LPCR parameter to be a zero.
1230 ret = opal_npu_map_lpar(nphb->opal_id,
1231 PCI_DEVID(gpdev->bus->number, gpdev->devfn), lparid,
1234 dev_err(&gpdev->dev, "Error %d mapping device to LPAR\n", ret);
1238 dev_dbg(&gpdev->dev, "init context opalid=%llu msr=%lx\n",
1239 nphb->opal_id, msr);
1240 ret = opal_npu_init_context(nphb->opal_id, 0/*__unused*/, msr,
1241 PCI_DEVID(gpdev->bus->number, gpdev->devfn));
1243 dev_err(&gpdev->dev, "Failed to init context: %d\n", ret);
1249 EXPORT_SYMBOL_GPL(pnv_npu2_map_lpar_dev);
1251 void pnv_npu2_map_lpar(struct pnv_ioda_pe *gpe, unsigned long msr)
1253 struct pci_dev *gpdev;
1255 list_for_each_entry(gpdev, &gpe->pbus->devices, bus_list)
1256 pnv_npu2_map_lpar_dev(gpdev, 0, msr);
1259 int pnv_npu2_unmap_lpar_dev(struct pci_dev *gpdev)
1262 struct pci_dev *npdev = pnv_pci_get_npu_dev(gpdev, 0);
1263 struct pci_controller *hose;
1264 struct pnv_phb *nphb;
1269 hose = pci_bus_to_host(npdev->bus);
1270 nphb = hose->private_data;
1272 dev_dbg(&gpdev->dev, "destroy context opalid=%llu\n",
1274 ret = opal_npu_destroy_context(nphb->opal_id, 0/*__unused*/,
1275 PCI_DEVID(gpdev->bus->number, gpdev->devfn));
1277 dev_err(&gpdev->dev, "Failed to destroy context: %d\n", ret);
1281 /* Set LPID to 0 anyway, just to be safe */
1282 dev_dbg(&gpdev->dev, "Map LPAR opalid=%llu lparid=0\n", nphb->opal_id);
1283 ret = opal_npu_map_lpar(nphb->opal_id,
1284 PCI_DEVID(gpdev->bus->number, gpdev->devfn), 0 /*LPID*/,
1287 dev_err(&gpdev->dev, "Error %d mapping device to LPAR\n", ret);
1291 EXPORT_SYMBOL_GPL(pnv_npu2_unmap_lpar_dev);