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);
31 static struct pci_dev *get_pci_dev(struct device_node *dn)
33 struct pci_dn *pdn = PCI_DN(dn);
35 return pci_get_domain_bus_and_slot(pci_domain_nr(pdn->phb->bus),
36 pdn->busno, pdn->devfn);
39 /* Given a NPU device get the associated PCI device. */
40 struct pci_dev *pnv_pci_get_gpu_dev(struct pci_dev *npdev)
42 struct device_node *dn;
43 struct pci_dev *gpdev;
48 if (WARN_ON(!npdev->dev.of_node))
51 /* Get assoicated PCI device */
52 dn = of_parse_phandle(npdev->dev.of_node, "ibm,gpu", 0);
56 gpdev = get_pci_dev(dn);
61 EXPORT_SYMBOL(pnv_pci_get_gpu_dev);
63 /* Given the real PCI device get a linked NPU device. */
64 struct pci_dev *pnv_pci_get_npu_dev(struct pci_dev *gpdev, int index)
66 struct device_node *dn;
67 struct pci_dev *npdev;
72 /* Not all PCI devices have device-tree nodes */
73 if (!gpdev->dev.of_node)
76 /* Get assoicated PCI device */
77 dn = of_parse_phandle(gpdev->dev.of_node, "ibm,npu", index);
81 npdev = get_pci_dev(dn);
86 EXPORT_SYMBOL(pnv_pci_get_npu_dev);
89 * Returns the PE assoicated with the PCI device of the given
90 * NPU. Returns the linked pci device if pci_dev != NULL.
92 static struct pnv_ioda_pe *get_gpu_pci_dev_and_pe(struct pnv_ioda_pe *npe,
93 struct pci_dev **gpdev)
96 struct pci_controller *hose;
98 struct pnv_ioda_pe *pe;
101 pdev = pnv_pci_get_gpu_dev(npe->pdev);
105 pdn = pci_get_pdn(pdev);
106 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
109 hose = pci_bus_to_host(pdev->bus);
110 phb = hose->private_data;
111 pe = &phb->ioda.pe_array[pdn->pe_number];
119 static long pnv_npu_unset_window(struct iommu_table_group *table_group,
122 static long pnv_npu_set_window(struct iommu_table_group *table_group, int num,
123 struct iommu_table *tbl)
125 struct pnv_ioda_pe *npe = container_of(table_group, struct pnv_ioda_pe,
127 struct pnv_phb *phb = npe->phb;
129 const unsigned long size = tbl->it_indirect_levels ?
130 tbl->it_level_size : tbl->it_size;
131 const __u64 start_addr = tbl->it_offset << tbl->it_page_shift;
132 const __u64 win_size = tbl->it_size << tbl->it_page_shift;
133 int num2 = (num == 0) ? 1 : 0;
135 /* NPU has just one TVE so if there is another table, remove it first */
136 if (npe->table_group.tables[num2])
137 pnv_npu_unset_window(&npe->table_group, num2);
139 pe_info(npe, "Setting up window %llx..%llx pg=%lx\n",
140 start_addr, start_addr + win_size - 1,
141 IOMMU_PAGE_SIZE(tbl));
143 rc = opal_pci_map_pe_dma_window(phb->opal_id,
146 tbl->it_indirect_levels + 1,
149 IOMMU_PAGE_SIZE(tbl));
151 pe_err(npe, "Failed to configure TCE table, err %lld\n", rc);
154 pnv_pci_ioda2_tce_invalidate_entire(phb, false);
156 /* Add the table to the list so its TCE cache will get invalidated */
157 pnv_pci_link_table_and_group(phb->hose->node, num,
158 tbl, &npe->table_group);
163 static long pnv_npu_unset_window(struct iommu_table_group *table_group, int num)
165 struct pnv_ioda_pe *npe = container_of(table_group, struct pnv_ioda_pe,
167 struct pnv_phb *phb = npe->phb;
170 if (!npe->table_group.tables[num])
173 pe_info(npe, "Removing DMA window\n");
175 rc = opal_pci_map_pe_dma_window(phb->opal_id, npe->pe_number,
177 0/* levels */, 0/* table address */,
178 0/* table size */, 0/* page size */);
180 pe_err(npe, "Unmapping failed, ret = %lld\n", rc);
183 pnv_pci_ioda2_tce_invalidate_entire(phb, false);
185 pnv_pci_unlink_table_and_group(npe->table_group.tables[num],
192 * Enables 32 bit DMA on NPU.
194 static void pnv_npu_dma_set_32(struct pnv_ioda_pe *npe)
196 struct pci_dev *gpdev;
197 struct pnv_ioda_pe *gpe;
201 * Find the assoicated PCI devices and get the dma window
202 * information from there.
204 if (!npe->pdev || !(npe->flags & PNV_IODA_PE_DEV))
207 gpe = get_gpu_pci_dev_and_pe(npe, &gpdev);
211 rc = pnv_npu_set_window(&npe->table_group, 0,
212 gpe->table_group.tables[0]);
215 * NVLink devices use the same TCE table configuration as
216 * their parent device so drivers shouldn't be doing DMA
217 * operations directly on these devices.
219 set_dma_ops(&npe->pdev->dev, NULL);
223 * Enables bypass mode on the NPU. The NPU only supports one
224 * window per link, so bypass needs to be explicitly enabled or
225 * disabled. Unlike for a PHB3 bypass and non-bypass modes can't be
226 * active at the same time.
228 static int pnv_npu_dma_set_bypass(struct pnv_ioda_pe *npe)
230 struct pnv_phb *phb = npe->phb;
232 phys_addr_t top = memblock_end_of_DRAM();
234 if (phb->type != PNV_PHB_NPU_NVLINK || !npe->pdev)
237 rc = pnv_npu_unset_window(&npe->table_group, 0);
238 if (rc != OPAL_SUCCESS)
241 /* Enable the bypass window */
243 top = roundup_pow_of_two(top);
244 dev_info(&npe->pdev->dev, "Enabling bypass for PE %x\n",
246 rc = opal_pci_map_pe_dma_window_real(phb->opal_id,
247 npe->pe_number, npe->pe_number,
248 0 /* bypass base */, top);
250 if (rc == OPAL_SUCCESS)
251 pnv_pci_ioda2_tce_invalidate_entire(phb, false);
256 void pnv_npu_try_dma_set_bypass(struct pci_dev *gpdev, bool bypass)
261 struct pnv_ioda_pe *npe;
262 struct pci_dev *npdev;
265 npdev = pnv_pci_get_npu_dev(gpdev, i);
270 pdn = pci_get_pdn(npdev);
271 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
274 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];
280 dev_info(&npdev->dev,
281 "Using 64-bit DMA iommu bypass\n");
282 pnv_npu_dma_set_bypass(npe);
284 dev_info(&npdev->dev, "Using 32-bit DMA via iommu\n");
285 pnv_npu_dma_set_32(npe);
290 #ifdef CONFIG_IOMMU_API
291 /* Switch ownership from platform code to external user (e.g. VFIO) */
292 static void pnv_npu_take_ownership(struct iommu_table_group *table_group)
294 struct pnv_ioda_pe *npe = container_of(table_group, struct pnv_ioda_pe,
296 struct pnv_phb *phb = npe->phb;
298 struct pci_dev *gpdev = NULL;
301 * Note: NPU has just a single TVE in the hardware which means that
302 * while used by the kernel, it can have either 32bit window or
303 * DMA bypass but never both. So we deconfigure 32bit window only
304 * if it was enabled at the moment of ownership change.
306 if (npe->table_group.tables[0]) {
307 pnv_npu_unset_window(&npe->table_group, 0);
312 rc = opal_pci_map_pe_dma_window_real(phb->opal_id,
313 npe->pe_number, npe->pe_number,
314 0 /* bypass base */, 0);
316 pe_err(npe, "Failed to disable bypass, err %lld\n", rc);
319 pnv_pci_ioda2_tce_invalidate_entire(npe->phb, false);
321 get_gpu_pci_dev_and_pe(npe, &gpdev);
323 pnv_npu2_unmap_lpar_dev(gpdev);
326 static void pnv_npu_release_ownership(struct iommu_table_group *table_group)
328 struct pnv_ioda_pe *npe = container_of(table_group, struct pnv_ioda_pe,
330 struct pci_dev *gpdev = NULL;
332 get_gpu_pci_dev_and_pe(npe, &gpdev);
334 pnv_npu2_map_lpar_dev(gpdev, 0, MSR_DR | MSR_PR | MSR_HV);
337 static struct iommu_table_group_ops pnv_pci_npu_ops = {
338 .set_window = pnv_npu_set_window,
339 .unset_window = pnv_npu_unset_window,
340 .take_ownership = pnv_npu_take_ownership,
341 .release_ownership = pnv_npu_release_ownership,
343 #endif /* !CONFIG_IOMMU_API */
348 /* Maximum possible number of ATSD MMIO registers per NPU */
349 #define NV_NMMU_ATSD_REGS 8
350 #define NV_NPU_MAX_PE_NUM 16
353 * A compound NPU IOMMU group which might consist of 1 GPU + 2xNPUs (POWER8) or
354 * up to 3 x (GPU + 2xNPUs) (POWER9).
357 struct iommu_table_group table_group;
359 struct pnv_ioda_pe *pe[NV_NPU_MAX_PE_NUM];
362 /* An NPU descriptor, valid for POWER9 only */
365 __be64 *mmio_atsd_regs[NV_NMMU_ATSD_REGS];
366 unsigned int mmio_atsd_count;
368 /* Bitmask for MMIO register usage */
369 unsigned long mmio_atsd_usage;
371 /* Do we need to explicitly flush the nest mmu? */
374 struct npu_comp npucomp;
377 #ifdef CONFIG_IOMMU_API
378 static long pnv_npu_peers_create_table_userspace(
379 struct iommu_table_group *table_group,
380 int num, __u32 page_shift, __u64 window_size, __u32 levels,
381 struct iommu_table **ptbl)
383 struct npu_comp *npucomp = container_of(table_group, struct npu_comp,
386 if (!npucomp->pe_num || !npucomp->pe[0] ||
387 !npucomp->pe[0]->table_group.ops ||
388 !npucomp->pe[0]->table_group.ops->create_table)
391 return npucomp->pe[0]->table_group.ops->create_table(
392 &npucomp->pe[0]->table_group, num, page_shift,
393 window_size, levels, ptbl);
396 static long pnv_npu_peers_set_window(struct iommu_table_group *table_group,
397 int num, struct iommu_table *tbl)
401 struct npu_comp *npucomp = container_of(table_group, struct npu_comp,
404 for (i = 0; i < npucomp->pe_num; ++i) {
405 struct pnv_ioda_pe *pe = npucomp->pe[i];
407 if (!pe->table_group.ops->set_window)
410 ret = pe->table_group.ops->set_window(&pe->table_group,
417 for (j = 0; j < i; ++j) {
418 struct pnv_ioda_pe *pe = npucomp->pe[j];
420 if (!pe->table_group.ops->unset_window)
423 ret = pe->table_group.ops->unset_window(
424 &pe->table_group, num);
429 table_group->tables[num] = iommu_tce_table_get(tbl);
435 static long pnv_npu_peers_unset_window(struct iommu_table_group *table_group,
440 struct npu_comp *npucomp = container_of(table_group, struct npu_comp,
443 for (i = 0; i < npucomp->pe_num; ++i) {
444 struct pnv_ioda_pe *pe = npucomp->pe[i];
446 WARN_ON(npucomp->table_group.tables[num] !=
447 table_group->tables[num]);
448 if (!npucomp->table_group.tables[num])
451 if (!pe->table_group.ops->unset_window)
454 ret = pe->table_group.ops->unset_window(&pe->table_group, num);
460 for (j = 0; j < i; ++j) {
461 struct pnv_ioda_pe *pe = npucomp->pe[j];
463 if (!npucomp->table_group.tables[num])
466 if (!pe->table_group.ops->set_window)
469 ret = pe->table_group.ops->set_window(&pe->table_group,
470 num, table_group->tables[num]);
474 } else if (table_group->tables[num]) {
475 iommu_tce_table_put(table_group->tables[num]);
476 table_group->tables[num] = NULL;
482 static void pnv_npu_peers_take_ownership(struct iommu_table_group *table_group)
485 struct npu_comp *npucomp = container_of(table_group, struct npu_comp,
488 for (i = 0; i < npucomp->pe_num; ++i) {
489 struct pnv_ioda_pe *pe = npucomp->pe[i];
491 if (!pe->table_group.ops->take_ownership)
493 pe->table_group.ops->take_ownership(&pe->table_group);
497 static void pnv_npu_peers_release_ownership(
498 struct iommu_table_group *table_group)
501 struct npu_comp *npucomp = container_of(table_group, struct npu_comp,
504 for (i = 0; i < npucomp->pe_num; ++i) {
505 struct pnv_ioda_pe *pe = npucomp->pe[i];
507 if (!pe->table_group.ops->release_ownership)
509 pe->table_group.ops->release_ownership(&pe->table_group);
513 static struct iommu_table_group_ops pnv_npu_peers_ops = {
514 .get_table_size = pnv_pci_ioda2_get_table_size,
515 .create_table = pnv_npu_peers_create_table_userspace,
516 .set_window = pnv_npu_peers_set_window,
517 .unset_window = pnv_npu_peers_unset_window,
518 .take_ownership = pnv_npu_peers_take_ownership,
519 .release_ownership = pnv_npu_peers_release_ownership,
522 static void pnv_comp_attach_table_group(struct npu_comp *npucomp,
523 struct pnv_ioda_pe *pe)
525 if (WARN_ON(npucomp->pe_num == NV_NPU_MAX_PE_NUM))
528 npucomp->pe[npucomp->pe_num] = pe;
532 struct iommu_table_group *pnv_try_setup_npu_table_group(struct pnv_ioda_pe *pe)
534 struct iommu_table_group *table_group;
535 struct npu_comp *npucomp;
536 struct pci_dev *gpdev = NULL;
537 struct pci_controller *hose;
538 struct pci_dev *npdev = NULL;
540 list_for_each_entry(gpdev, &pe->pbus->devices, bus_list) {
541 npdev = pnv_pci_get_npu_dev(gpdev, 0);
547 /* It is not an NPU attached device, skip */
550 hose = pci_bus_to_host(npdev->bus);
553 table_group = &hose->npu->npucomp.table_group;
555 if (!table_group->group) {
556 table_group->ops = &pnv_npu_peers_ops;
557 iommu_register_group(table_group,
562 /* Create a group for 1 GPU and attached NPUs for POWER8 */
563 pe->npucomp = kzalloc(sizeof(*pe->npucomp), GFP_KERNEL);
564 table_group = &pe->npucomp->table_group;
565 table_group->ops = &pnv_npu_peers_ops;
566 iommu_register_group(table_group, hose->global_number,
570 /* Steal capabilities from a GPU PE */
571 table_group->max_dynamic_windows_supported =
572 pe->table_group.max_dynamic_windows_supported;
573 table_group->tce32_start = pe->table_group.tce32_start;
574 table_group->tce32_size = pe->table_group.tce32_size;
575 table_group->max_levels = pe->table_group.max_levels;
576 if (!table_group->pgsizes)
577 table_group->pgsizes = pe->table_group.pgsizes;
579 npucomp = container_of(table_group, struct npu_comp, table_group);
580 pnv_comp_attach_table_group(npucomp, pe);
585 struct iommu_table_group *pnv_npu_compound_attach(struct pnv_ioda_pe *pe)
587 struct iommu_table_group *table_group;
588 struct npu_comp *npucomp;
589 struct pci_dev *gpdev = NULL;
590 struct pci_dev *npdev;
591 struct pnv_ioda_pe *gpe = get_gpu_pci_dev_and_pe(pe, &gpdev);
593 WARN_ON(!(pe->flags & PNV_IODA_PE_DEV));
598 * IODA2 bridges get this set up from pci_controller_ops::setup_bridge
599 * but NPU bridges do not have this hook defined so we do it here.
600 * We do not setup other table group parameters as they won't be used
601 * anyway - NVLink bridges are subordinate PEs.
603 pe->table_group.ops = &pnv_pci_npu_ops;
605 table_group = iommu_group_get_iommudata(
606 iommu_group_get(&gpdev->dev));
609 * On P9 NPU PHB and PCI PHB support different page sizes,
610 * keep only matching. We expect here that NVLink bridge PE pgsizes is
611 * initialized by the caller.
613 table_group->pgsizes &= pe->table_group.pgsizes;
614 npucomp = container_of(table_group, struct npu_comp, table_group);
615 pnv_comp_attach_table_group(npucomp, pe);
617 list_for_each_entry(npdev, &pe->phb->hose->bus->devices, bus_list) {
618 struct pci_dev *gpdevtmp = pnv_pci_get_gpu_dev(npdev);
620 if (gpdevtmp != gpdev)
623 iommu_add_device(table_group, &npdev->dev);
628 #endif /* CONFIG_IOMMU_API */
630 /* Maximum number of nvlinks per npu */
631 #define NV_MAX_LINKS 6
633 /* Maximum index of npu2 hosts in the system. Always < NV_MAX_NPUS */
634 static int max_npu2_index;
637 struct mm_struct *mm;
638 struct pci_dev *npdev[NV_MAX_NPUS][NV_MAX_LINKS];
639 struct mmu_notifier mn;
643 /* Callback to stop translation requests on a given GPU */
644 void (*release_cb)(struct npu_context *context, void *priv);
647 * Private pointer passed to the above callback for usage by
653 struct mmio_atsd_reg {
659 * Find a free MMIO ATSD register and mark it in use. Return -ENOSPC
660 * if none are available.
662 static int get_mmio_atsd_reg(struct npu *npu)
666 for (i = 0; i < npu->mmio_atsd_count; i++) {
667 if (!test_bit(i, &npu->mmio_atsd_usage))
668 if (!test_and_set_bit_lock(i, &npu->mmio_atsd_usage))
675 static void put_mmio_atsd_reg(struct npu *npu, int reg)
677 clear_bit_unlock(reg, &npu->mmio_atsd_usage);
680 /* MMIO ATSD register offsets */
681 #define XTS_ATSD_LAUNCH 0
682 #define XTS_ATSD_AVA 1
683 #define XTS_ATSD_STAT 2
685 static unsigned long get_atsd_launch_val(unsigned long pid, unsigned long psize)
687 unsigned long launch = 0;
689 if (psize == MMU_PAGE_COUNT) {
690 /* IS set to invalidate entire matching PID */
691 launch |= PPC_BIT(12);
693 /* AP set to invalidate region of psize */
694 launch |= (u64)mmu_get_ap(psize) << PPC_BITLSHIFT(17);
697 /* PRS set to process-scoped */
698 launch |= PPC_BIT(13);
701 launch |= pid << PPC_BITLSHIFT(38);
703 /* Leave "No flush" (bit 39) 0 so every ATSD performs a flush */
708 static void mmio_atsd_regs_write(struct mmio_atsd_reg
709 mmio_atsd_reg[NV_MAX_NPUS], unsigned long offset,
715 for (i = 0; i <= max_npu2_index; i++) {
716 reg = mmio_atsd_reg[i].reg;
720 npu = mmio_atsd_reg[i].npu;
721 __raw_writeq_be(val, npu->mmio_atsd_regs[reg] + offset);
725 static void mmio_invalidate_pid(struct mmio_atsd_reg mmio_atsd_reg[NV_MAX_NPUS],
728 unsigned long launch = get_atsd_launch_val(pid, MMU_PAGE_COUNT);
730 /* Invalidating the entire process doesn't use a va */
731 mmio_atsd_regs_write(mmio_atsd_reg, XTS_ATSD_LAUNCH, launch);
734 static void mmio_invalidate_range(struct mmio_atsd_reg
735 mmio_atsd_reg[NV_MAX_NPUS], unsigned long pid,
736 unsigned long start, unsigned long psize)
738 unsigned long launch = get_atsd_launch_val(pid, psize);
740 /* Write all VAs first */
741 mmio_atsd_regs_write(mmio_atsd_reg, XTS_ATSD_AVA, start);
743 /* Issue one barrier for all address writes */
747 mmio_atsd_regs_write(mmio_atsd_reg, XTS_ATSD_LAUNCH, launch);
750 #define mn_to_npu_context(x) container_of(x, struct npu_context, mn)
752 static void mmio_invalidate_wait(
753 struct mmio_atsd_reg mmio_atsd_reg[NV_MAX_NPUS])
758 /* Wait for all invalidations to complete */
759 for (i = 0; i <= max_npu2_index; i++) {
760 if (mmio_atsd_reg[i].reg < 0)
763 /* Wait for completion */
764 npu = mmio_atsd_reg[i].npu;
765 reg = mmio_atsd_reg[i].reg;
766 while (__raw_readq(npu->mmio_atsd_regs[reg] + XTS_ATSD_STAT))
772 * Acquires all the address translation shootdown (ATSD) registers required to
773 * launch an ATSD on all links this npu_context is active on.
775 static void acquire_atsd_reg(struct npu_context *npu_context,
776 struct mmio_atsd_reg mmio_atsd_reg[NV_MAX_NPUS])
780 struct pci_dev *npdev;
782 for (i = 0; i <= max_npu2_index; i++) {
783 mmio_atsd_reg[i].reg = -1;
784 for (j = 0; j < NV_MAX_LINKS; j++) {
786 * There are no ordering requirements with respect to
787 * the setup of struct npu_context, but to ensure
788 * consistent behaviour we need to ensure npdev[][] is
791 npdev = READ_ONCE(npu_context->npdev[i][j]);
795 npu = pci_bus_to_host(npdev->bus)->npu;
799 mmio_atsd_reg[i].npu = npu;
800 mmio_atsd_reg[i].reg = get_mmio_atsd_reg(npu);
801 while (mmio_atsd_reg[i].reg < 0) {
802 mmio_atsd_reg[i].reg = get_mmio_atsd_reg(npu);
811 * Release previously acquired ATSD registers. To avoid deadlocks the registers
812 * must be released in the same order they were acquired above in
815 static void release_atsd_reg(struct mmio_atsd_reg mmio_atsd_reg[NV_MAX_NPUS])
819 for (i = 0; i <= max_npu2_index; i++) {
821 * We can't rely on npu_context->npdev[][] being the same here
822 * as when acquire_atsd_reg() was called, hence we use the
823 * values stored in mmio_atsd_reg during the acquire phase
824 * rather than re-reading npdev[][].
826 if (mmio_atsd_reg[i].reg < 0)
829 put_mmio_atsd_reg(mmio_atsd_reg[i].npu, mmio_atsd_reg[i].reg);
834 * Invalidate a virtual address range
836 static void mmio_invalidate(struct npu_context *npu_context,
837 unsigned long start, unsigned long size)
839 struct mmio_atsd_reg mmio_atsd_reg[NV_MAX_NPUS];
840 unsigned long pid = npu_context->mm->context.id;
841 unsigned long atsd_start = 0;
842 unsigned long end = start + size - 1;
843 int atsd_psize = MMU_PAGE_COUNT;
846 * Convert the input range into one of the supported sizes. If the range
847 * doesn't fit, use the next larger supported size. Invalidation latency
848 * is high, so over-invalidation is preferred to issuing multiple
851 * A 4K page size isn't supported by NPU/GPU ATS, so that case is
854 if (size == SZ_64K) {
856 atsd_psize = MMU_PAGE_64K;
857 } else if (ALIGN_DOWN(start, SZ_2M) == ALIGN_DOWN(end, SZ_2M)) {
858 atsd_start = ALIGN_DOWN(start, SZ_2M);
859 atsd_psize = MMU_PAGE_2M;
860 } else if (ALIGN_DOWN(start, SZ_1G) == ALIGN_DOWN(end, SZ_1G)) {
861 atsd_start = ALIGN_DOWN(start, SZ_1G);
862 atsd_psize = MMU_PAGE_1G;
865 if (npu_context->nmmu_flush)
867 * Unfortunately the nest mmu does not support flushing specific
868 * addresses so we have to flush the whole mm once before
869 * shooting down the GPU translation.
871 flush_all_mm(npu_context->mm);
874 * Loop over all the NPUs this process is active on and launch
877 acquire_atsd_reg(npu_context, mmio_atsd_reg);
879 if (atsd_psize == MMU_PAGE_COUNT)
880 mmio_invalidate_pid(mmio_atsd_reg, pid);
882 mmio_invalidate_range(mmio_atsd_reg, pid, atsd_start,
885 mmio_invalidate_wait(mmio_atsd_reg);
888 * The GPU requires two flush ATSDs to ensure all entries have been
889 * flushed. We use PID 0 as it will never be used for a process on the
892 mmio_invalidate_pid(mmio_atsd_reg, 0);
893 mmio_invalidate_wait(mmio_atsd_reg);
894 mmio_invalidate_pid(mmio_atsd_reg, 0);
895 mmio_invalidate_wait(mmio_atsd_reg);
897 release_atsd_reg(mmio_atsd_reg);
900 static void pnv_npu2_mn_release(struct mmu_notifier *mn,
901 struct mm_struct *mm)
903 struct npu_context *npu_context = mn_to_npu_context(mn);
905 /* Call into device driver to stop requests to the NMMU */
906 if (npu_context->release_cb)
907 npu_context->release_cb(npu_context, npu_context->priv);
910 * There should be no more translation requests for this PID, but we
911 * need to ensure any entries for it are removed from the TLB.
913 mmio_invalidate(npu_context, 0, ~0UL);
916 static void pnv_npu2_mn_change_pte(struct mmu_notifier *mn,
917 struct mm_struct *mm,
918 unsigned long address,
921 struct npu_context *npu_context = mn_to_npu_context(mn);
922 mmio_invalidate(npu_context, address, PAGE_SIZE);
925 static void pnv_npu2_mn_invalidate_range(struct mmu_notifier *mn,
926 struct mm_struct *mm,
927 unsigned long start, unsigned long end)
929 struct npu_context *npu_context = mn_to_npu_context(mn);
930 mmio_invalidate(npu_context, start, end - start);
933 static const struct mmu_notifier_ops nv_nmmu_notifier_ops = {
934 .release = pnv_npu2_mn_release,
935 .change_pte = pnv_npu2_mn_change_pte,
936 .invalidate_range = pnv_npu2_mn_invalidate_range,
940 * Call into OPAL to setup the nmmu context for the current task in
941 * the NPU. This must be called to setup the context tables before the
942 * GPU issues ATRs. pdev should be a pointed to PCIe GPU device.
944 * A release callback should be registered to allow a device driver to
945 * be notified that it should not launch any new translation requests
946 * as the final TLB invalidate is about to occur.
948 * Returns an error if there no contexts are currently available or a
949 * npu_context which should be passed to pnv_npu2_handle_fault().
951 * mmap_sem must be held in write mode and must not be called from interrupt
954 struct npu_context *pnv_npu2_init_context(struct pci_dev *gpdev,
956 void (*cb)(struct npu_context *, void *),
961 struct device_node *nvlink_dn;
962 struct mm_struct *mm = current->mm;
964 struct npu_context *npu_context;
965 struct pci_controller *hose;
968 * At present we don't support GPUs connected to multiple NPUs and I'm
969 * not sure the hardware does either.
971 struct pci_dev *npdev = pnv_pci_get_npu_dev(gpdev, 0);
974 /* No nvlink associated with this GPU device */
975 return ERR_PTR(-ENODEV);
977 /* We only support DR/PR/HV in pnv_npu2_map_lpar_dev() */
978 if (flags & ~(MSR_DR | MSR_PR | MSR_HV))
979 return ERR_PTR(-EINVAL);
981 nvlink_dn = of_parse_phandle(npdev->dev.of_node, "ibm,nvlink", 0);
982 if (WARN_ON(of_property_read_u32(nvlink_dn, "ibm,npu-link-index",
984 return ERR_PTR(-ENODEV);
986 if (!mm || mm->context.id == 0) {
988 * Kernel thread contexts are not supported and context id 0 is
989 * reserved on the GPU.
991 return ERR_PTR(-EINVAL);
994 hose = pci_bus_to_host(npdev->bus);
997 return ERR_PTR(-ENODEV);
1000 * We store the npu pci device so we can more easily get at the
1003 spin_lock(&npu_context_lock);
1004 npu_context = mm->context.npu_context;
1006 if (npu_context->release_cb != cb ||
1007 npu_context->priv != priv) {
1008 spin_unlock(&npu_context_lock);
1009 return ERR_PTR(-EINVAL);
1012 WARN_ON(!kref_get_unless_zero(&npu_context->kref));
1014 spin_unlock(&npu_context_lock);
1018 * We can set up these fields without holding the
1019 * npu_context_lock as the npu_context hasn't been returned to
1020 * the caller meaning it can't be destroyed. Parallel allocation
1021 * is protected against by mmap_sem.
1024 npu_context = kzalloc(sizeof(struct npu_context), GFP_KERNEL);
1026 kref_init(&npu_context->kref);
1027 npu_context->mm = mm;
1028 npu_context->mn.ops = &nv_nmmu_notifier_ops;
1029 rc = __mmu_notifier_register(&npu_context->mn, mm);
1037 mm->context.npu_context = npu_context;
1040 npu_context->release_cb = cb;
1041 npu_context->priv = priv;
1044 * npdev is a pci_dev pointer setup by the PCI code. We assign it to
1045 * npdev[][] to indicate to the mmu notifiers that an invalidation
1046 * should also be sent over this nvlink. The notifiers don't use any
1047 * other fields in npu_context, so we just need to ensure that when they
1048 * deference npu_context->npdev[][] it is either a valid pointer or
1051 WRITE_ONCE(npu_context->npdev[npu->index][nvlink_index], npdev);
1053 if (!npu->nmmu_flush) {
1055 * If we're not explicitly flushing ourselves we need to mark
1056 * the thread for global flushes
1058 npu_context->nmmu_flush = false;
1059 mm_context_add_copro(mm);
1061 npu_context->nmmu_flush = true;
1065 EXPORT_SYMBOL(pnv_npu2_init_context);
1067 static void pnv_npu2_release_context(struct kref *kref)
1069 struct npu_context *npu_context =
1070 container_of(kref, struct npu_context, kref);
1072 if (!npu_context->nmmu_flush)
1073 mm_context_remove_copro(npu_context->mm);
1075 npu_context->mm->context.npu_context = NULL;
1079 * Destroy a context on the given GPU. May free the npu_context if it is no
1080 * longer active on any GPUs. Must not be called from interrupt context.
1082 void pnv_npu2_destroy_context(struct npu_context *npu_context,
1083 struct pci_dev *gpdev)
1087 struct pci_dev *npdev = pnv_pci_get_npu_dev(gpdev, 0);
1088 struct device_node *nvlink_dn;
1090 struct pci_controller *hose;
1092 if (WARN_ON(!npdev))
1095 hose = pci_bus_to_host(npdev->bus);
1099 nvlink_dn = of_parse_phandle(npdev->dev.of_node, "ibm,nvlink", 0);
1100 if (WARN_ON(of_property_read_u32(nvlink_dn, "ibm,npu-link-index",
1103 WRITE_ONCE(npu_context->npdev[npu->index][nvlink_index], NULL);
1104 spin_lock(&npu_context_lock);
1105 removed = kref_put(&npu_context->kref, pnv_npu2_release_context);
1106 spin_unlock(&npu_context_lock);
1109 * We need to do this outside of pnv_npu2_release_context so that it is
1110 * outside the spinlock as mmu_notifier_destroy uses SRCU.
1113 mmu_notifier_unregister(&npu_context->mn,
1120 EXPORT_SYMBOL(pnv_npu2_destroy_context);
1123 * Assumes mmap_sem is held for the contexts associated mm.
1125 int pnv_npu2_handle_fault(struct npu_context *context, uintptr_t *ea,
1126 unsigned long *flags, unsigned long *status, int count)
1128 u64 rc = 0, result = 0;
1130 struct page *page[1];
1131 const char __user *u;
1134 /* mmap_sem should be held so the struct_mm must be present */
1135 struct mm_struct *mm = context->mm;
1137 WARN_ON(!rwsem_is_locked(&mm->mmap_sem));
1139 for (i = 0; i < count; i++) {
1140 is_write = flags[i] & NPU2_WRITE;
1141 rc = get_user_pages_remote(NULL, mm, ea[i], 1,
1142 is_write ? FOLL_WRITE : 0,
1151 /* Make sure partition scoped tree gets a pte */
1152 u = page_address(page[0]);
1153 if (__get_user(c, u))
1162 EXPORT_SYMBOL(pnv_npu2_handle_fault);
1164 int pnv_npu2_init(struct pci_controller *hose)
1168 static int npu_index;
1172 npu = kzalloc(sizeof(*npu), GFP_KERNEL);
1176 npu->nmmu_flush = of_property_read_bool(hose->dn, "ibm,nmmu-flush");
1178 for (i = 0; i < ARRAY_SIZE(npu->mmio_atsd_regs) &&
1179 !of_property_read_u64_index(hose->dn, "ibm,mmio-atsd",
1180 i, &mmio_atsd); i++)
1181 npu->mmio_atsd_regs[i] = ioremap(mmio_atsd, 32);
1183 pr_info("NPU%d: Found %d MMIO ATSD registers", hose->global_number, i);
1184 npu->mmio_atsd_count = i;
1185 npu->mmio_atsd_usage = 0;
1187 if (WARN_ON(npu_index >= NV_MAX_NPUS)) {
1191 max_npu2_index = npu_index;
1192 npu->index = npu_index;
1198 for (i = 0; i < npu->mmio_atsd_count; ++i)
1199 iounmap(npu->mmio_atsd_regs[i]);
1206 int pnv_npu2_map_lpar_dev(struct pci_dev *gpdev, unsigned int lparid,
1210 struct pci_dev *npdev = pnv_pci_get_npu_dev(gpdev, 0);
1211 struct pci_controller *hose;
1212 struct pnv_phb *nphb;
1217 hose = pci_bus_to_host(npdev->bus);
1218 nphb = hose->private_data;
1220 dev_dbg(&gpdev->dev, "Map LPAR opalid=%llu lparid=%u\n",
1221 nphb->opal_id, lparid);
1223 * Currently we only support radix and non-zero LPCR only makes sense
1224 * for hash tables so skiboot expects the LPCR parameter to be a zero.
1226 ret = opal_npu_map_lpar(nphb->opal_id,
1227 PCI_DEVID(gpdev->bus->number, gpdev->devfn), lparid,
1230 dev_err(&gpdev->dev, "Error %d mapping device to LPAR\n", ret);
1234 dev_dbg(&gpdev->dev, "init context opalid=%llu msr=%lx\n",
1235 nphb->opal_id, msr);
1236 ret = opal_npu_init_context(nphb->opal_id, 0/*__unused*/, msr,
1237 PCI_DEVID(gpdev->bus->number, gpdev->devfn));
1239 dev_err(&gpdev->dev, "Failed to init context: %d\n", ret);
1245 EXPORT_SYMBOL_GPL(pnv_npu2_map_lpar_dev);
1247 void pnv_npu2_map_lpar(struct pnv_ioda_pe *gpe, unsigned long msr)
1249 struct pci_dev *gpdev;
1251 list_for_each_entry(gpdev, &gpe->pbus->devices, bus_list)
1252 pnv_npu2_map_lpar_dev(gpdev, 0, msr);
1255 int pnv_npu2_unmap_lpar_dev(struct pci_dev *gpdev)
1258 struct pci_dev *npdev = pnv_pci_get_npu_dev(gpdev, 0);
1259 struct pci_controller *hose;
1260 struct pnv_phb *nphb;
1265 hose = pci_bus_to_host(npdev->bus);
1266 nphb = hose->private_data;
1268 dev_dbg(&gpdev->dev, "destroy context opalid=%llu\n",
1270 ret = opal_npu_destroy_context(nphb->opal_id, 0/*__unused*/,
1271 PCI_DEVID(gpdev->bus->number, gpdev->devfn));
1273 dev_err(&gpdev->dev, "Failed to destroy context: %d\n", ret);
1277 /* Set LPID to 0 anyway, just to be safe */
1278 dev_dbg(&gpdev->dev, "Map LPAR opalid=%llu lparid=0\n", nphb->opal_id);
1279 ret = opal_npu_map_lpar(nphb->opal_id,
1280 PCI_DEVID(gpdev->bus->number, gpdev->devfn), 0 /*LPID*/,
1283 dev_err(&gpdev->dev, "Error %d mapping device to LPAR\n", ret);
1287 EXPORT_SYMBOL_GPL(pnv_npu2_unmap_lpar_dev);