1 // SPDX-License-Identifier: GPL-2.0-only
3 * This file implements the DMA operations for NVLink devices. The NPU
4 * devices all point to the same iommu table as the parent PCI device.
6 * Copyright Alistair Popple, IBM Corporation 2015.
9 #include <linux/mmu_notifier.h>
10 #include <linux/mmu_context.h>
12 #include <linux/pci.h>
13 #include <linux/memblock.h>
14 #include <linux/sizes.h>
16 #include <asm/debugfs.h>
17 #include <asm/powernv.h>
18 #include <asm/ppc-pci.h>
23 static struct pci_dev *get_pci_dev(struct device_node *dn)
25 struct pci_dn *pdn = PCI_DN(dn);
28 pdev = pci_get_domain_bus_and_slot(pci_domain_nr(pdn->phb->bus),
29 pdn->busno, pdn->devfn);
32 * pci_get_domain_bus_and_slot() increased the reference count of
33 * the PCI device, but callers don't need that actually as the PE
34 * already holds a reference to the device. Since callers aren't
35 * aware of the reference count change, call pci_dev_put() now to
44 /* Given a NPU device get the associated PCI device. */
45 struct pci_dev *pnv_pci_get_gpu_dev(struct pci_dev *npdev)
47 struct device_node *dn;
48 struct pci_dev *gpdev;
53 if (WARN_ON(!npdev->dev.of_node))
56 /* Get assoicated PCI device */
57 dn = of_parse_phandle(npdev->dev.of_node, "ibm,gpu", 0);
61 gpdev = get_pci_dev(dn);
66 EXPORT_SYMBOL(pnv_pci_get_gpu_dev);
68 /* Given the real PCI device get a linked NPU device. */
69 struct pci_dev *pnv_pci_get_npu_dev(struct pci_dev *gpdev, int index)
71 struct device_node *dn;
72 struct pci_dev *npdev;
77 /* Not all PCI devices have device-tree nodes */
78 if (!gpdev->dev.of_node)
81 /* Get assoicated PCI device */
82 dn = of_parse_phandle(gpdev->dev.of_node, "ibm,npu", index);
86 npdev = get_pci_dev(dn);
91 EXPORT_SYMBOL(pnv_pci_get_npu_dev);
93 #ifdef CONFIG_IOMMU_API
95 * Returns the PE assoicated with the PCI device of the given
96 * NPU. Returns the linked pci device if pci_dev != NULL.
98 static struct pnv_ioda_pe *get_gpu_pci_dev_and_pe(struct pnv_ioda_pe *npe,
99 struct pci_dev **gpdev)
102 struct pci_controller *hose;
103 struct pci_dev *pdev;
104 struct pnv_ioda_pe *pe;
107 pdev = pnv_pci_get_gpu_dev(npe->pdev);
111 pdn = pci_get_pdn(pdev);
112 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
115 hose = pci_bus_to_host(pdev->bus);
116 phb = hose->private_data;
117 pe = &phb->ioda.pe_array[pdn->pe_number];
125 static long pnv_npu_unset_window(struct iommu_table_group *table_group,
128 static long pnv_npu_set_window(struct iommu_table_group *table_group, int num,
129 struct iommu_table *tbl)
131 struct pnv_ioda_pe *npe = container_of(table_group, struct pnv_ioda_pe,
133 struct pnv_phb *phb = npe->phb;
135 const unsigned long size = tbl->it_indirect_levels ?
136 tbl->it_level_size : tbl->it_size;
137 const __u64 start_addr = tbl->it_offset << tbl->it_page_shift;
138 const __u64 win_size = tbl->it_size << tbl->it_page_shift;
139 int num2 = (num == 0) ? 1 : 0;
141 /* NPU has just one TVE so if there is another table, remove it first */
142 if (npe->table_group.tables[num2])
143 pnv_npu_unset_window(&npe->table_group, num2);
145 pe_info(npe, "Setting up window %llx..%llx pg=%lx\n",
146 start_addr, start_addr + win_size - 1,
147 IOMMU_PAGE_SIZE(tbl));
149 rc = opal_pci_map_pe_dma_window(phb->opal_id,
152 tbl->it_indirect_levels + 1,
155 IOMMU_PAGE_SIZE(tbl));
157 pe_err(npe, "Failed to configure TCE table, err %lld\n", rc);
160 pnv_pci_ioda2_tce_invalidate_entire(phb, false);
162 /* Add the table to the list so its TCE cache will get invalidated */
163 pnv_pci_link_table_and_group(phb->hose->node, num,
164 tbl, &npe->table_group);
169 static long pnv_npu_unset_window(struct iommu_table_group *table_group, int num)
171 struct pnv_ioda_pe *npe = container_of(table_group, struct pnv_ioda_pe,
173 struct pnv_phb *phb = npe->phb;
176 if (!npe->table_group.tables[num])
179 pe_info(npe, "Removing DMA window\n");
181 rc = opal_pci_map_pe_dma_window(phb->opal_id, npe->pe_number,
183 0/* levels */, 0/* table address */,
184 0/* table size */, 0/* page size */);
186 pe_err(npe, "Unmapping failed, ret = %lld\n", rc);
189 pnv_pci_ioda2_tce_invalidate_entire(phb, false);
191 pnv_pci_unlink_table_and_group(npe->table_group.tables[num],
197 /* Switch ownership from platform code to external user (e.g. VFIO) */
198 static void pnv_npu_take_ownership(struct iommu_table_group *table_group)
200 struct pnv_ioda_pe *npe = container_of(table_group, struct pnv_ioda_pe,
202 struct pnv_phb *phb = npe->phb;
204 struct pci_dev *gpdev = NULL;
207 * Note: NPU has just a single TVE in the hardware which means that
208 * while used by the kernel, it can have either 32bit window or
209 * DMA bypass but never both. So we deconfigure 32bit window only
210 * if it was enabled at the moment of ownership change.
212 if (npe->table_group.tables[0]) {
213 pnv_npu_unset_window(&npe->table_group, 0);
218 rc = opal_pci_map_pe_dma_window_real(phb->opal_id,
219 npe->pe_number, npe->pe_number,
220 0 /* bypass base */, 0);
222 pe_err(npe, "Failed to disable bypass, err %lld\n", rc);
225 pnv_pci_ioda2_tce_invalidate_entire(npe->phb, false);
227 get_gpu_pci_dev_and_pe(npe, &gpdev);
229 pnv_npu2_unmap_lpar_dev(gpdev);
232 static void pnv_npu_release_ownership(struct iommu_table_group *table_group)
234 struct pnv_ioda_pe *npe = container_of(table_group, struct pnv_ioda_pe,
236 struct pci_dev *gpdev = NULL;
238 get_gpu_pci_dev_and_pe(npe, &gpdev);
240 pnv_npu2_map_lpar_dev(gpdev, 0, MSR_DR | MSR_PR | MSR_HV);
243 static struct iommu_table_group_ops pnv_pci_npu_ops = {
244 .set_window = pnv_npu_set_window,
245 .unset_window = pnv_npu_unset_window,
246 .take_ownership = pnv_npu_take_ownership,
247 .release_ownership = pnv_npu_release_ownership,
249 #endif /* !CONFIG_IOMMU_API */
254 /* Maximum possible number of ATSD MMIO registers per NPU */
255 #define NV_NMMU_ATSD_REGS 8
256 #define NV_NPU_MAX_PE_NUM 16
259 * A compound NPU IOMMU group which might consist of 1 GPU + 2xNPUs (POWER8) or
260 * up to 3 x (GPU + 2xNPUs) (POWER9).
263 struct iommu_table_group table_group;
265 struct pnv_ioda_pe *pe[NV_NPU_MAX_PE_NUM];
268 /* An NPU descriptor, valid for POWER9 only */
271 struct npu_comp npucomp;
274 #ifdef CONFIG_IOMMU_API
275 static long pnv_npu_peers_create_table_userspace(
276 struct iommu_table_group *table_group,
277 int num, __u32 page_shift, __u64 window_size, __u32 levels,
278 struct iommu_table **ptbl)
280 struct npu_comp *npucomp = container_of(table_group, struct npu_comp,
283 if (!npucomp->pe_num || !npucomp->pe[0] ||
284 !npucomp->pe[0]->table_group.ops ||
285 !npucomp->pe[0]->table_group.ops->create_table)
288 return npucomp->pe[0]->table_group.ops->create_table(
289 &npucomp->pe[0]->table_group, num, page_shift,
290 window_size, levels, ptbl);
293 static long pnv_npu_peers_set_window(struct iommu_table_group *table_group,
294 int num, struct iommu_table *tbl)
298 struct npu_comp *npucomp = container_of(table_group, struct npu_comp,
301 for (i = 0; i < npucomp->pe_num; ++i) {
302 struct pnv_ioda_pe *pe = npucomp->pe[i];
304 if (!pe->table_group.ops->set_window)
307 ret = pe->table_group.ops->set_window(&pe->table_group,
314 for (j = 0; j < i; ++j) {
315 struct pnv_ioda_pe *pe = npucomp->pe[j];
317 if (!pe->table_group.ops->unset_window)
320 ret = pe->table_group.ops->unset_window(
321 &pe->table_group, num);
326 table_group->tables[num] = iommu_tce_table_get(tbl);
332 static long pnv_npu_peers_unset_window(struct iommu_table_group *table_group,
337 struct npu_comp *npucomp = container_of(table_group, struct npu_comp,
340 for (i = 0; i < npucomp->pe_num; ++i) {
341 struct pnv_ioda_pe *pe = npucomp->pe[i];
343 WARN_ON(npucomp->table_group.tables[num] !=
344 table_group->tables[num]);
345 if (!npucomp->table_group.tables[num])
348 if (!pe->table_group.ops->unset_window)
351 ret = pe->table_group.ops->unset_window(&pe->table_group, num);
357 for (j = 0; j < i; ++j) {
358 struct pnv_ioda_pe *pe = npucomp->pe[j];
360 if (!npucomp->table_group.tables[num])
363 if (!pe->table_group.ops->set_window)
366 ret = pe->table_group.ops->set_window(&pe->table_group,
367 num, table_group->tables[num]);
371 } else if (table_group->tables[num]) {
372 iommu_tce_table_put(table_group->tables[num]);
373 table_group->tables[num] = NULL;
379 static void pnv_npu_peers_take_ownership(struct iommu_table_group *table_group)
382 struct npu_comp *npucomp = container_of(table_group, struct npu_comp,
385 for (i = 0; i < npucomp->pe_num; ++i) {
386 struct pnv_ioda_pe *pe = npucomp->pe[i];
388 if (!pe->table_group.ops ||
389 !pe->table_group.ops->take_ownership)
391 pe->table_group.ops->take_ownership(&pe->table_group);
395 static void pnv_npu_peers_release_ownership(
396 struct iommu_table_group *table_group)
399 struct npu_comp *npucomp = container_of(table_group, struct npu_comp,
402 for (i = 0; i < npucomp->pe_num; ++i) {
403 struct pnv_ioda_pe *pe = npucomp->pe[i];
405 if (!pe->table_group.ops ||
406 !pe->table_group.ops->release_ownership)
408 pe->table_group.ops->release_ownership(&pe->table_group);
412 static struct iommu_table_group_ops pnv_npu_peers_ops = {
413 .get_table_size = pnv_pci_ioda2_get_table_size,
414 .create_table = pnv_npu_peers_create_table_userspace,
415 .set_window = pnv_npu_peers_set_window,
416 .unset_window = pnv_npu_peers_unset_window,
417 .take_ownership = pnv_npu_peers_take_ownership,
418 .release_ownership = pnv_npu_peers_release_ownership,
421 static void pnv_comp_attach_table_group(struct npu_comp *npucomp,
422 struct pnv_ioda_pe *pe)
424 if (WARN_ON(npucomp->pe_num == NV_NPU_MAX_PE_NUM))
427 npucomp->pe[npucomp->pe_num] = pe;
431 static struct iommu_table_group *
432 pnv_try_setup_npu_table_group(struct pnv_ioda_pe *pe)
434 struct iommu_table_group *compound_group;
435 struct npu_comp *npucomp;
436 struct pci_dev *gpdev = NULL;
437 struct pci_controller *hose;
438 struct pci_dev *npdev = NULL;
440 list_for_each_entry(gpdev, &pe->pbus->devices, bus_list) {
441 npdev = pnv_pci_get_npu_dev(gpdev, 0);
447 /* It is not an NPU attached device, skip */
450 hose = pci_bus_to_host(npdev->bus);
453 /* P9 case: compound group is per-NPU (all gpus, all links) */
454 npucomp = &hose->npu->npucomp;
456 /* P8 case: Compound group is per-GPU (1 gpu, 2 links) */
457 npucomp = pe->npucomp = kzalloc(sizeof(*npucomp), GFP_KERNEL);
460 compound_group = &npucomp->table_group;
461 if (!compound_group->group) {
462 compound_group->ops = &pnv_npu_peers_ops;
463 iommu_register_group(compound_group, hose->global_number,
466 /* Steal capabilities from a GPU PE */
467 compound_group->max_dynamic_windows_supported =
468 pe->table_group.max_dynamic_windows_supported;
469 compound_group->tce32_start = pe->table_group.tce32_start;
470 compound_group->tce32_size = pe->table_group.tce32_size;
471 compound_group->max_levels = pe->table_group.max_levels;
472 if (!compound_group->pgsizes)
473 compound_group->pgsizes = pe->table_group.pgsizes;
477 * The gpu would have been added to the iommu group that's created
478 * for the PE. Pull it out now.
480 iommu_del_device(&gpdev->dev);
483 * I'm not sure this is strictly required, but it's probably a good idea
484 * since the table_group for the PE is going to be attached to the
485 * compound table group. If we leave the PE's iommu group active then
486 * we might have the same table_group being modifiable via two sepeate
489 iommu_group_put(pe->table_group.group);
491 /* now put the GPU into the compound group */
492 pnv_comp_attach_table_group(npucomp, pe);
493 iommu_add_device(compound_group, &gpdev->dev);
495 return compound_group;
498 static struct iommu_table_group *pnv_npu_compound_attach(struct pnv_ioda_pe *pe)
500 struct iommu_table_group *table_group;
501 struct npu_comp *npucomp;
502 struct pci_dev *gpdev = NULL;
503 struct pci_dev *npdev;
504 struct pnv_ioda_pe *gpe = get_gpu_pci_dev_and_pe(pe, &gpdev);
506 WARN_ON(!(pe->flags & PNV_IODA_PE_DEV));
511 * IODA2 bridges get this set up from pci_controller_ops::setup_bridge
512 * but NPU bridges do not have this hook defined so we do it here.
513 * We do not setup other table group parameters as they won't be used
514 * anyway - NVLink bridges are subordinate PEs.
516 pe->table_group.ops = &pnv_pci_npu_ops;
518 table_group = iommu_group_get_iommudata(
519 iommu_group_get(&gpdev->dev));
522 * On P9 NPU PHB and PCI PHB support different page sizes,
523 * keep only matching. We expect here that NVLink bridge PE pgsizes is
524 * initialized by the caller.
526 table_group->pgsizes &= pe->table_group.pgsizes;
527 npucomp = container_of(table_group, struct npu_comp, table_group);
528 pnv_comp_attach_table_group(npucomp, pe);
530 list_for_each_entry(npdev, &pe->phb->hose->bus->devices, bus_list) {
531 struct pci_dev *gpdevtmp = pnv_pci_get_gpu_dev(npdev);
533 if (gpdevtmp != gpdev)
536 iommu_add_device(table_group, &npdev->dev);
542 void pnv_pci_npu_setup_iommu_groups(void)
544 struct pci_controller *hose;
546 struct pnv_ioda_pe *pe;
549 * For non-nvlink devices the IOMMU group is registered when the PE is
550 * configured and devices are added to the group when the per-device
551 * DMA setup is run. That's done in hose->ops.dma_dev_setup() which is
552 * only initialise for "normal" IODA PHBs.
554 * For NVLink devices we need to ensure the NVLinks and the GPU end up
555 * in the same IOMMU group, so that's handled here.
557 list_for_each_entry(hose, &hose_list, list_node) {
558 phb = hose->private_data;
560 if (phb->type == PNV_PHB_IODA2)
561 list_for_each_entry(pe, &phb->ioda.pe_list, list)
562 pnv_try_setup_npu_table_group(pe);
566 * Now we have all PHBs discovered, time to add NPU devices to
567 * the corresponding IOMMU groups.
569 list_for_each_entry(hose, &hose_list, list_node) {
570 unsigned long pgsizes;
572 phb = hose->private_data;
574 if (phb->type != PNV_PHB_NPU_NVLINK)
577 pgsizes = pnv_ioda_parse_tce_sizes(phb);
578 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
580 * IODA2 bridges get this set up from
581 * pci_controller_ops::setup_bridge but NPU bridges
582 * do not have this hook defined so we do it here.
584 pe->table_group.pgsizes = pgsizes;
585 pnv_npu_compound_attach(pe);
589 #endif /* CONFIG_IOMMU_API */
591 int pnv_npu2_init(struct pci_controller *hose)
593 static int npu_index;
597 npu = kzalloc(sizeof(*npu), GFP_KERNEL);
602 if (WARN_ON(npu_index >= NV_MAX_NPUS)) {
606 npu->index = npu_index;
616 int pnv_npu2_map_lpar_dev(struct pci_dev *gpdev, unsigned int lparid,
620 struct pci_dev *npdev = pnv_pci_get_npu_dev(gpdev, 0);
621 struct pci_controller *hose;
622 struct pnv_phb *nphb;
627 hose = pci_bus_to_host(npdev->bus);
628 if (hose->npu == NULL) {
629 dev_info_once(&npdev->dev, "Nvlink1 does not support contexts");
633 nphb = hose->private_data;
635 dev_dbg(&gpdev->dev, "Map LPAR opalid=%llu lparid=%u\n",
636 nphb->opal_id, lparid);
638 * Currently we only support radix and non-zero LPCR only makes sense
639 * for hash tables so skiboot expects the LPCR parameter to be a zero.
641 ret = opal_npu_map_lpar(nphb->opal_id, pci_dev_id(gpdev), lparid,
644 dev_err(&gpdev->dev, "Error %d mapping device to LPAR\n", ret);
648 dev_dbg(&gpdev->dev, "init context opalid=%llu msr=%lx\n",
650 ret = opal_npu_init_context(nphb->opal_id, 0/*__unused*/, msr,
653 dev_err(&gpdev->dev, "Failed to init context: %d\n", ret);
659 EXPORT_SYMBOL_GPL(pnv_npu2_map_lpar_dev);
661 void pnv_npu2_map_lpar(struct pnv_ioda_pe *gpe, unsigned long msr)
663 struct pci_dev *gpdev;
665 list_for_each_entry(gpdev, &gpe->pbus->devices, bus_list)
666 pnv_npu2_map_lpar_dev(gpdev, 0, msr);
669 int pnv_npu2_unmap_lpar_dev(struct pci_dev *gpdev)
672 struct pci_dev *npdev = pnv_pci_get_npu_dev(gpdev, 0);
673 struct pci_controller *hose;
674 struct pnv_phb *nphb;
679 hose = pci_bus_to_host(npdev->bus);
680 if (hose->npu == NULL) {
681 dev_info_once(&npdev->dev, "Nvlink1 does not support contexts");
685 nphb = hose->private_data;
687 dev_dbg(&gpdev->dev, "destroy context opalid=%llu\n",
689 ret = opal_npu_destroy_context(nphb->opal_id, 0/*__unused*/,
692 dev_err(&gpdev->dev, "Failed to destroy context: %d\n", ret);
696 /* Set LPID to 0 anyway, just to be safe */
697 dev_dbg(&gpdev->dev, "Map LPAR opalid=%llu lparid=0\n", nphb->opal_id);
698 ret = opal_npu_map_lpar(nphb->opal_id, pci_dev_id(gpdev), 0 /*LPID*/,
701 dev_err(&gpdev->dev, "Error %d mapping device to LPAR\n", ret);
705 EXPORT_SYMBOL_GPL(pnv_npu2_unmap_lpar_dev);