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/slab.h>
13 #include <linux/mmu_notifier.h>
14 #include <linux/mmu_context.h>
16 #include <linux/export.h>
17 #include <linux/pci.h>
18 #include <linux/memblock.h>
19 #include <linux/iommu.h>
20 #include <linux/sizes.h>
22 #include <asm/debugfs.h>
24 #include <asm/powernv.h>
28 #include <asm/iommu.h>
29 #include <asm/pnv-pci.h>
30 #include <asm/msi_bitmap.h>
36 #define npu_to_phb(x) container_of(x, struct pnv_phb, npu)
39 * spinlock to protect initialisation of an npu_context for a particular
42 static DEFINE_SPINLOCK(npu_context_lock);
45 * Other types of TCE cache invalidation are not functional in the
48 static struct pci_dev *get_pci_dev(struct device_node *dn)
50 struct pci_dn *pdn = PCI_DN(dn);
52 return pci_get_domain_bus_and_slot(pci_domain_nr(pdn->phb->bus),
53 pdn->busno, pdn->devfn);
56 /* Given a NPU device get the associated PCI device. */
57 struct pci_dev *pnv_pci_get_gpu_dev(struct pci_dev *npdev)
59 struct device_node *dn;
60 struct pci_dev *gpdev;
65 if (WARN_ON(!npdev->dev.of_node))
68 /* Get assoicated PCI device */
69 dn = of_parse_phandle(npdev->dev.of_node, "ibm,gpu", 0);
73 gpdev = get_pci_dev(dn);
78 EXPORT_SYMBOL(pnv_pci_get_gpu_dev);
80 /* Given the real PCI device get a linked NPU device. */
81 struct pci_dev *pnv_pci_get_npu_dev(struct pci_dev *gpdev, int index)
83 struct device_node *dn;
84 struct pci_dev *npdev;
89 /* Not all PCI devices have device-tree nodes */
90 if (!gpdev->dev.of_node)
93 /* Get assoicated PCI device */
94 dn = of_parse_phandle(gpdev->dev.of_node, "ibm,npu", index);
98 npdev = get_pci_dev(dn);
103 EXPORT_SYMBOL(pnv_pci_get_npu_dev);
106 * Returns the PE assoicated with the PCI device of the given
107 * NPU. Returns the linked pci device if pci_dev != NULL.
109 static struct pnv_ioda_pe *get_gpu_pci_dev_and_pe(struct pnv_ioda_pe *npe,
110 struct pci_dev **gpdev)
113 struct pci_controller *hose;
114 struct pci_dev *pdev;
115 struct pnv_ioda_pe *pe;
118 pdev = pnv_pci_get_gpu_dev(npe->pdev);
122 pdn = pci_get_pdn(pdev);
123 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
126 hose = pci_bus_to_host(pdev->bus);
127 phb = hose->private_data;
128 pe = &phb->ioda.pe_array[pdn->pe_number];
136 long pnv_npu_set_window(struct pnv_ioda_pe *npe, int num,
137 struct iommu_table *tbl)
139 struct pnv_phb *phb = npe->phb;
141 const unsigned long size = tbl->it_indirect_levels ?
142 tbl->it_level_size : tbl->it_size;
143 const __u64 start_addr = tbl->it_offset << tbl->it_page_shift;
144 const __u64 win_size = tbl->it_size << tbl->it_page_shift;
146 pe_info(npe, "Setting up window %llx..%llx pg=%lx\n",
147 start_addr, start_addr + win_size - 1,
148 IOMMU_PAGE_SIZE(tbl));
150 rc = opal_pci_map_pe_dma_window(phb->opal_id,
153 tbl->it_indirect_levels + 1,
156 IOMMU_PAGE_SIZE(tbl));
158 pe_err(npe, "Failed to configure TCE table, err %lld\n", rc);
161 pnv_pci_ioda2_tce_invalidate_entire(phb, false);
163 /* Add the table to the list so its TCE cache will get invalidated */
164 pnv_pci_link_table_and_group(phb->hose->node, num,
165 tbl, &npe->table_group);
170 long pnv_npu_unset_window(struct pnv_ioda_pe *npe, int num)
172 struct pnv_phb *phb = npe->phb;
175 pe_info(npe, "Removing DMA window\n");
177 rc = opal_pci_map_pe_dma_window(phb->opal_id, npe->pe_number,
179 0/* levels */, 0/* table address */,
180 0/* table size */, 0/* page size */);
182 pe_err(npe, "Unmapping failed, ret = %lld\n", rc);
185 pnv_pci_ioda2_tce_invalidate_entire(phb, false);
187 pnv_pci_unlink_table_and_group(npe->table_group.tables[num],
194 * Enables 32 bit DMA on NPU.
196 static void pnv_npu_dma_set_32(struct pnv_ioda_pe *npe)
198 struct pci_dev *gpdev;
199 struct pnv_ioda_pe *gpe;
203 * Find the assoicated PCI devices and get the dma window
204 * information from there.
206 if (!npe->pdev || !(npe->flags & PNV_IODA_PE_DEV))
209 gpe = get_gpu_pci_dev_and_pe(npe, &gpdev);
213 rc = pnv_npu_set_window(npe, 0, gpe->table_group.tables[0]);
216 * NVLink devices use the same TCE table configuration as
217 * their parent device so drivers shouldn't be doing DMA
218 * operations directly on these devices.
220 set_dma_ops(&npe->pdev->dev, NULL);
224 * Enables bypass mode on the NPU. The NPU only supports one
225 * window per link, so bypass needs to be explicitly enabled or
226 * disabled. Unlike for a PHB3 bypass and non-bypass modes can't be
227 * active at the same time.
229 static int pnv_npu_dma_set_bypass(struct pnv_ioda_pe *npe)
231 struct pnv_phb *phb = npe->phb;
233 phys_addr_t top = memblock_end_of_DRAM();
235 if (phb->type != PNV_PHB_NPU_NVLINK || !npe->pdev)
238 rc = pnv_npu_unset_window(npe, 0);
239 if (rc != OPAL_SUCCESS)
242 /* Enable the bypass window */
244 top = roundup_pow_of_two(top);
245 dev_info(&npe->pdev->dev, "Enabling bypass for PE %x\n",
247 rc = opal_pci_map_pe_dma_window_real(phb->opal_id,
248 npe->pe_number, npe->pe_number,
249 0 /* bypass base */, top);
251 if (rc == OPAL_SUCCESS)
252 pnv_pci_ioda2_tce_invalidate_entire(phb, false);
257 void pnv_npu_try_dma_set_bypass(struct pci_dev *gpdev, bool bypass)
262 struct pnv_ioda_pe *npe;
263 struct pci_dev *npdev;
266 npdev = pnv_pci_get_npu_dev(gpdev, i);
271 pdn = pci_get_pdn(npdev);
272 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
275 phb = pci_bus_to_host(npdev->bus)->private_data;
277 /* We only do bypass if it's enabled on the linked device */
278 npe = &phb->ioda.pe_array[pdn->pe_number];
281 dev_info(&npdev->dev,
282 "Using 64-bit DMA iommu bypass\n");
283 pnv_npu_dma_set_bypass(npe);
285 dev_info(&npdev->dev, "Using 32-bit DMA via iommu\n");
286 pnv_npu_dma_set_32(npe);
291 /* Switch ownership from platform code to external user (e.g. VFIO) */
292 void pnv_npu_take_ownership(struct pnv_ioda_pe *npe)
294 struct pnv_phb *phb = npe->phb;
298 * Note: NPU has just a single TVE in the hardware which means that
299 * while used by the kernel, it can have either 32bit window or
300 * DMA bypass but never both. So we deconfigure 32bit window only
301 * if it was enabled at the moment of ownership change.
303 if (npe->table_group.tables[0]) {
304 pnv_npu_unset_window(npe, 0);
309 rc = opal_pci_map_pe_dma_window_real(phb->opal_id,
310 npe->pe_number, npe->pe_number,
311 0 /* bypass base */, 0);
313 pe_err(npe, "Failed to disable bypass, err %lld\n", rc);
316 pnv_pci_ioda2_tce_invalidate_entire(npe->phb, false);
319 struct pnv_ioda_pe *pnv_pci_npu_setup_iommu(struct pnv_ioda_pe *npe)
321 struct pnv_phb *phb = npe->phb;
322 struct pci_bus *pbus = phb->hose->bus;
323 struct pci_dev *npdev, *gpdev = NULL, *gptmp;
324 struct pnv_ioda_pe *gpe = get_gpu_pci_dev_and_pe(npe, &gpdev);
329 list_for_each_entry(npdev, &pbus->devices, bus_list) {
330 gptmp = pnv_pci_get_gpu_dev(npdev);
335 pe_info(gpe, "Attached NPU %s\n", dev_name(&npdev->dev));
336 iommu_group_add_device(gpe->table_group.group, &npdev->dev);
342 /* Maximum number of nvlinks per npu */
343 #define NV_MAX_LINKS 6
345 /* Maximum index of npu2 hosts in the system. Always < NV_MAX_NPUS */
346 static int max_npu2_index;
349 struct mm_struct *mm;
350 struct pci_dev *npdev[NV_MAX_NPUS][NV_MAX_LINKS];
351 struct mmu_notifier mn;
355 /* Callback to stop translation requests on a given GPU */
356 void (*release_cb)(struct npu_context *context, void *priv);
359 * Private pointer passed to the above callback for usage by
365 struct mmio_atsd_reg {
371 * Find a free MMIO ATSD register and mark it in use. Return -ENOSPC
372 * if none are available.
374 static int get_mmio_atsd_reg(struct npu *npu)
378 for (i = 0; i < npu->mmio_atsd_count; i++) {
379 if (!test_bit(i, &npu->mmio_atsd_usage))
380 if (!test_and_set_bit_lock(i, &npu->mmio_atsd_usage))
387 static void put_mmio_atsd_reg(struct npu *npu, int reg)
389 clear_bit_unlock(reg, &npu->mmio_atsd_usage);
392 /* MMIO ATSD register offsets */
393 #define XTS_ATSD_LAUNCH 0
394 #define XTS_ATSD_AVA 1
395 #define XTS_ATSD_STAT 2
397 static unsigned long get_atsd_launch_val(unsigned long pid, unsigned long psize)
399 unsigned long launch = 0;
401 if (psize == MMU_PAGE_COUNT) {
402 /* IS set to invalidate entire matching PID */
403 launch |= PPC_BIT(12);
405 /* AP set to invalidate region of psize */
406 launch |= (u64)mmu_get_ap(psize) << PPC_BITLSHIFT(17);
409 /* PRS set to process-scoped */
410 launch |= PPC_BIT(13);
413 launch |= pid << PPC_BITLSHIFT(38);
415 /* Leave "No flush" (bit 39) 0 so every ATSD performs a flush */
420 static void mmio_atsd_regs_write(struct mmio_atsd_reg
421 mmio_atsd_reg[NV_MAX_NPUS], unsigned long offset,
427 for (i = 0; i <= max_npu2_index; i++) {
428 reg = mmio_atsd_reg[i].reg;
432 npu = mmio_atsd_reg[i].npu;
433 __raw_writeq_be(val, npu->mmio_atsd_regs[reg] + offset);
437 static void mmio_invalidate_pid(struct mmio_atsd_reg mmio_atsd_reg[NV_MAX_NPUS],
440 unsigned long launch = get_atsd_launch_val(pid, MMU_PAGE_COUNT);
442 /* Invalidating the entire process doesn't use a va */
443 mmio_atsd_regs_write(mmio_atsd_reg, XTS_ATSD_LAUNCH, launch);
446 static void mmio_invalidate_range(struct mmio_atsd_reg
447 mmio_atsd_reg[NV_MAX_NPUS], unsigned long pid,
448 unsigned long start, unsigned long psize)
450 unsigned long launch = get_atsd_launch_val(pid, psize);
452 /* Write all VAs first */
453 mmio_atsd_regs_write(mmio_atsd_reg, XTS_ATSD_AVA, start);
455 /* Issue one barrier for all address writes */
459 mmio_atsd_regs_write(mmio_atsd_reg, XTS_ATSD_LAUNCH, launch);
462 #define mn_to_npu_context(x) container_of(x, struct npu_context, mn)
464 static void mmio_invalidate_wait(
465 struct mmio_atsd_reg mmio_atsd_reg[NV_MAX_NPUS])
470 /* Wait for all invalidations to complete */
471 for (i = 0; i <= max_npu2_index; i++) {
472 if (mmio_atsd_reg[i].reg < 0)
475 /* Wait for completion */
476 npu = mmio_atsd_reg[i].npu;
477 reg = mmio_atsd_reg[i].reg;
478 while (__raw_readq(npu->mmio_atsd_regs[reg] + XTS_ATSD_STAT))
484 * Acquires all the address translation shootdown (ATSD) registers required to
485 * launch an ATSD on all links this npu_context is active on.
487 static void acquire_atsd_reg(struct npu_context *npu_context,
488 struct mmio_atsd_reg mmio_atsd_reg[NV_MAX_NPUS])
492 struct pci_dev *npdev;
493 struct pnv_phb *nphb;
495 for (i = 0; i <= max_npu2_index; i++) {
496 mmio_atsd_reg[i].reg = -1;
497 for (j = 0; j < NV_MAX_LINKS; j++) {
499 * There are no ordering requirements with respect to
500 * the setup of struct npu_context, but to ensure
501 * consistent behaviour we need to ensure npdev[][] is
504 npdev = READ_ONCE(npu_context->npdev[i][j]);
508 nphb = pci_bus_to_host(npdev->bus)->private_data;
510 mmio_atsd_reg[i].npu = npu;
511 mmio_atsd_reg[i].reg = get_mmio_atsd_reg(npu);
512 while (mmio_atsd_reg[i].reg < 0) {
513 mmio_atsd_reg[i].reg = get_mmio_atsd_reg(npu);
522 * Release previously acquired ATSD registers. To avoid deadlocks the registers
523 * must be released in the same order they were acquired above in
526 static void release_atsd_reg(struct mmio_atsd_reg mmio_atsd_reg[NV_MAX_NPUS])
530 for (i = 0; i <= max_npu2_index; i++) {
532 * We can't rely on npu_context->npdev[][] being the same here
533 * as when acquire_atsd_reg() was called, hence we use the
534 * values stored in mmio_atsd_reg during the acquire phase
535 * rather than re-reading npdev[][].
537 if (mmio_atsd_reg[i].reg < 0)
540 put_mmio_atsd_reg(mmio_atsd_reg[i].npu, mmio_atsd_reg[i].reg);
545 * Invalidate a virtual address range
547 static void mmio_invalidate(struct npu_context *npu_context,
548 unsigned long start, unsigned long size)
550 struct mmio_atsd_reg mmio_atsd_reg[NV_MAX_NPUS];
551 unsigned long pid = npu_context->mm->context.id;
552 unsigned long atsd_start = 0;
553 unsigned long end = start + size - 1;
554 int atsd_psize = MMU_PAGE_COUNT;
557 * Convert the input range into one of the supported sizes. If the range
558 * doesn't fit, use the next larger supported size. Invalidation latency
559 * is high, so over-invalidation is preferred to issuing multiple
562 * A 4K page size isn't supported by NPU/GPU ATS, so that case is
565 if (size == SZ_64K) {
567 atsd_psize = MMU_PAGE_64K;
568 } else if (ALIGN_DOWN(start, SZ_2M) == ALIGN_DOWN(end, SZ_2M)) {
569 atsd_start = ALIGN_DOWN(start, SZ_2M);
570 atsd_psize = MMU_PAGE_2M;
571 } else if (ALIGN_DOWN(start, SZ_1G) == ALIGN_DOWN(end, SZ_1G)) {
572 atsd_start = ALIGN_DOWN(start, SZ_1G);
573 atsd_psize = MMU_PAGE_1G;
576 if (npu_context->nmmu_flush)
578 * Unfortunately the nest mmu does not support flushing specific
579 * addresses so we have to flush the whole mm once before
580 * shooting down the GPU translation.
582 flush_all_mm(npu_context->mm);
585 * Loop over all the NPUs this process is active on and launch
588 acquire_atsd_reg(npu_context, mmio_atsd_reg);
590 if (atsd_psize == MMU_PAGE_COUNT)
591 mmio_invalidate_pid(mmio_atsd_reg, pid);
593 mmio_invalidate_range(mmio_atsd_reg, pid, atsd_start,
596 mmio_invalidate_wait(mmio_atsd_reg);
599 * The GPU requires two flush ATSDs to ensure all entries have been
600 * flushed. We use PID 0 as it will never be used for a process on the
603 mmio_invalidate_pid(mmio_atsd_reg, 0);
604 mmio_invalidate_wait(mmio_atsd_reg);
605 mmio_invalidate_pid(mmio_atsd_reg, 0);
606 mmio_invalidate_wait(mmio_atsd_reg);
608 release_atsd_reg(mmio_atsd_reg);
611 static void pnv_npu2_mn_release(struct mmu_notifier *mn,
612 struct mm_struct *mm)
614 struct npu_context *npu_context = mn_to_npu_context(mn);
616 /* Call into device driver to stop requests to the NMMU */
617 if (npu_context->release_cb)
618 npu_context->release_cb(npu_context, npu_context->priv);
621 * There should be no more translation requests for this PID, but we
622 * need to ensure any entries for it are removed from the TLB.
624 mmio_invalidate(npu_context, 0, ~0UL);
627 static void pnv_npu2_mn_change_pte(struct mmu_notifier *mn,
628 struct mm_struct *mm,
629 unsigned long address,
632 struct npu_context *npu_context = mn_to_npu_context(mn);
633 mmio_invalidate(npu_context, address, PAGE_SIZE);
636 static void pnv_npu2_mn_invalidate_range(struct mmu_notifier *mn,
637 struct mm_struct *mm,
638 unsigned long start, unsigned long end)
640 struct npu_context *npu_context = mn_to_npu_context(mn);
641 mmio_invalidate(npu_context, start, end - start);
644 static const struct mmu_notifier_ops nv_nmmu_notifier_ops = {
645 .release = pnv_npu2_mn_release,
646 .change_pte = pnv_npu2_mn_change_pte,
647 .invalidate_range = pnv_npu2_mn_invalidate_range,
651 * Call into OPAL to setup the nmmu context for the current task in
652 * the NPU. This must be called to setup the context tables before the
653 * GPU issues ATRs. pdev should be a pointed to PCIe GPU device.
655 * A release callback should be registered to allow a device driver to
656 * be notified that it should not launch any new translation requests
657 * as the final TLB invalidate is about to occur.
659 * Returns an error if there no contexts are currently available or a
660 * npu_context which should be passed to pnv_npu2_handle_fault().
662 * mmap_sem must be held in write mode and must not be called from interrupt
665 struct npu_context *pnv_npu2_init_context(struct pci_dev *gpdev,
667 void (*cb)(struct npu_context *, void *),
672 struct device_node *nvlink_dn;
673 struct mm_struct *mm = current->mm;
674 struct pnv_phb *nphb;
676 struct npu_context *npu_context;
679 * At present we don't support GPUs connected to multiple NPUs and I'm
680 * not sure the hardware does either.
682 struct pci_dev *npdev = pnv_pci_get_npu_dev(gpdev, 0);
684 if (!firmware_has_feature(FW_FEATURE_OPAL))
685 return ERR_PTR(-ENODEV);
688 /* No nvlink associated with this GPU device */
689 return ERR_PTR(-ENODEV);
691 nvlink_dn = of_parse_phandle(npdev->dev.of_node, "ibm,nvlink", 0);
692 if (WARN_ON(of_property_read_u32(nvlink_dn, "ibm,npu-link-index",
694 return ERR_PTR(-ENODEV);
696 if (!mm || mm->context.id == 0) {
698 * Kernel thread contexts are not supported and context id 0 is
699 * reserved on the GPU.
701 return ERR_PTR(-EINVAL);
704 nphb = pci_bus_to_host(npdev->bus)->private_data;
708 * Setup the NPU context table for a particular GPU. These need to be
709 * per-GPU as we need the tables to filter ATSDs when there are no
710 * active contexts on a particular GPU. It is safe for these to be
711 * called concurrently with destroy as the OPAL call takes appropriate
712 * locks and refcounts on init/destroy.
714 rc = opal_npu_init_context(nphb->opal_id, mm->context.id, flags,
715 PCI_DEVID(gpdev->bus->number, gpdev->devfn));
717 return ERR_PTR(-ENOSPC);
720 * We store the npu pci device so we can more easily get at the
723 spin_lock(&npu_context_lock);
724 npu_context = mm->context.npu_context;
726 if (npu_context->release_cb != cb ||
727 npu_context->priv != priv) {
728 spin_unlock(&npu_context_lock);
729 opal_npu_destroy_context(nphb->opal_id, mm->context.id,
730 PCI_DEVID(gpdev->bus->number,
732 return ERR_PTR(-EINVAL);
735 WARN_ON(!kref_get_unless_zero(&npu_context->kref));
737 spin_unlock(&npu_context_lock);
741 * We can set up these fields without holding the
742 * npu_context_lock as the npu_context hasn't been returned to
743 * the caller meaning it can't be destroyed. Parallel allocation
744 * is protected against by mmap_sem.
747 npu_context = kzalloc(sizeof(struct npu_context), GFP_KERNEL);
749 kref_init(&npu_context->kref);
750 npu_context->mm = mm;
751 npu_context->mn.ops = &nv_nmmu_notifier_ops;
752 rc = __mmu_notifier_register(&npu_context->mn, mm);
757 opal_npu_destroy_context(nphb->opal_id, mm->context.id,
758 PCI_DEVID(gpdev->bus->number,
763 mm->context.npu_context = npu_context;
766 npu_context->release_cb = cb;
767 npu_context->priv = priv;
770 * npdev is a pci_dev pointer setup by the PCI code. We assign it to
771 * npdev[][] to indicate to the mmu notifiers that an invalidation
772 * should also be sent over this nvlink. The notifiers don't use any
773 * other fields in npu_context, so we just need to ensure that when they
774 * deference npu_context->npdev[][] it is either a valid pointer or
777 WRITE_ONCE(npu_context->npdev[npu->index][nvlink_index], npdev);
779 if (!nphb->npu.nmmu_flush) {
781 * If we're not explicitly flushing ourselves we need to mark
782 * the thread for global flushes
784 npu_context->nmmu_flush = false;
785 mm_context_add_copro(mm);
787 npu_context->nmmu_flush = true;
791 EXPORT_SYMBOL(pnv_npu2_init_context);
793 static void pnv_npu2_release_context(struct kref *kref)
795 struct npu_context *npu_context =
796 container_of(kref, struct npu_context, kref);
798 if (!npu_context->nmmu_flush)
799 mm_context_remove_copro(npu_context->mm);
801 npu_context->mm->context.npu_context = NULL;
805 * Destroy a context on the given GPU. May free the npu_context if it is no
806 * longer active on any GPUs. Must not be called from interrupt context.
808 void pnv_npu2_destroy_context(struct npu_context *npu_context,
809 struct pci_dev *gpdev)
812 struct pnv_phb *nphb;
814 struct pci_dev *npdev = pnv_pci_get_npu_dev(gpdev, 0);
815 struct device_node *nvlink_dn;
821 if (!firmware_has_feature(FW_FEATURE_OPAL))
824 nphb = pci_bus_to_host(npdev->bus)->private_data;
826 nvlink_dn = of_parse_phandle(npdev->dev.of_node, "ibm,nvlink", 0);
827 if (WARN_ON(of_property_read_u32(nvlink_dn, "ibm,npu-link-index",
830 WRITE_ONCE(npu_context->npdev[npu->index][nvlink_index], NULL);
831 opal_npu_destroy_context(nphb->opal_id, npu_context->mm->context.id,
832 PCI_DEVID(gpdev->bus->number, gpdev->devfn));
833 spin_lock(&npu_context_lock);
834 removed = kref_put(&npu_context->kref, pnv_npu2_release_context);
835 spin_unlock(&npu_context_lock);
838 * We need to do this outside of pnv_npu2_release_context so that it is
839 * outside the spinlock as mmu_notifier_destroy uses SRCU.
842 mmu_notifier_unregister(&npu_context->mn,
849 EXPORT_SYMBOL(pnv_npu2_destroy_context);
852 * Assumes mmap_sem is held for the contexts associated mm.
854 int pnv_npu2_handle_fault(struct npu_context *context, uintptr_t *ea,
855 unsigned long *flags, unsigned long *status, int count)
857 u64 rc = 0, result = 0;
859 struct page *page[1];
861 /* mmap_sem should be held so the struct_mm must be present */
862 struct mm_struct *mm = context->mm;
864 if (!firmware_has_feature(FW_FEATURE_OPAL))
867 WARN_ON(!rwsem_is_locked(&mm->mmap_sem));
869 for (i = 0; i < count; i++) {
870 is_write = flags[i] & NPU2_WRITE;
871 rc = get_user_pages_remote(NULL, mm, ea[i], 1,
872 is_write ? FOLL_WRITE : 0,
876 * To support virtualised environments we will have to do an
877 * access to the page to ensure it gets faulted into the
878 * hypervisor. For the moment virtualisation is not supported in
879 * other areas so leave the access out.
893 EXPORT_SYMBOL(pnv_npu2_handle_fault);
895 int pnv_npu2_init(struct pnv_phb *phb)
899 struct device_node *dn;
900 struct pci_dev *gpdev;
901 static int npu_index;
904 phb->npu.nmmu_flush =
905 of_property_read_bool(phb->hose->dn, "ibm,nmmu-flush");
906 for_each_child_of_node(phb->hose->dn, dn) {
907 gpdev = pnv_pci_get_gpu_dev(get_pci_dev(dn));
909 rc = opal_npu_map_lpar(phb->opal_id,
910 PCI_DEVID(gpdev->bus->number, gpdev->devfn),
914 "Error %lld mapping device to LPAR\n",
919 for (i = 0; !of_property_read_u64_index(phb->hose->dn, "ibm,mmio-atsd",
921 phb->npu.mmio_atsd_regs[i] = ioremap(mmio_atsd, 32);
923 pr_info("NPU%lld: Found %d MMIO ATSD registers", phb->opal_id, i);
924 phb->npu.mmio_atsd_count = i;
925 phb->npu.mmio_atsd_usage = 0;
927 if (WARN_ON(npu_index >= NV_MAX_NPUS))
929 max_npu2_index = npu_index;
930 phb->npu.index = npu_index;