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/export.h>
13 #include <linux/pci.h>
14 #include <linux/memblock.h>
16 #include <asm/iommu.h>
17 #include <asm/pnv-pci.h>
18 #include <asm/msi_bitmap.h>
25 * Other types of TCE cache invalidation are not functional in the
28 static struct pci_dev *get_pci_dev(struct device_node *dn)
30 return PCI_DN(dn)->pcidev;
33 /* Given a NPU device get the associated PCI device. */
34 struct pci_dev *pnv_pci_get_gpu_dev(struct pci_dev *npdev)
36 struct device_node *dn;
37 struct pci_dev *gpdev;
39 /* Get assoicated PCI device */
40 dn = of_parse_phandle(npdev->dev.of_node, "ibm,gpu", 0);
44 gpdev = get_pci_dev(dn);
49 EXPORT_SYMBOL(pnv_pci_get_gpu_dev);
51 /* Given the real PCI device get a linked NPU device. */
52 struct pci_dev *pnv_pci_get_npu_dev(struct pci_dev *gpdev, int index)
54 struct device_node *dn;
55 struct pci_dev *npdev;
57 /* Get assoicated PCI device */
58 dn = of_parse_phandle(gpdev->dev.of_node, "ibm,npu", index);
62 npdev = get_pci_dev(dn);
67 EXPORT_SYMBOL(pnv_pci_get_npu_dev);
69 #define NPU_DMA_OP_UNSUPPORTED() \
70 dev_err_once(dev, "%s operation unsupported for NVLink devices\n", \
73 static void *dma_npu_alloc(struct device *dev, size_t size,
74 dma_addr_t *dma_handle, gfp_t flag,
75 struct dma_attrs *attrs)
77 NPU_DMA_OP_UNSUPPORTED();
81 static void dma_npu_free(struct device *dev, size_t size,
82 void *vaddr, dma_addr_t dma_handle,
83 struct dma_attrs *attrs)
85 NPU_DMA_OP_UNSUPPORTED();
88 static dma_addr_t dma_npu_map_page(struct device *dev, struct page *page,
89 unsigned long offset, size_t size,
90 enum dma_data_direction direction,
91 struct dma_attrs *attrs)
93 NPU_DMA_OP_UNSUPPORTED();
97 static int dma_npu_map_sg(struct device *dev, struct scatterlist *sglist,
98 int nelems, enum dma_data_direction direction,
99 struct dma_attrs *attrs)
101 NPU_DMA_OP_UNSUPPORTED();
105 static int dma_npu_dma_supported(struct device *dev, u64 mask)
107 NPU_DMA_OP_UNSUPPORTED();
111 static u64 dma_npu_get_required_mask(struct device *dev)
113 NPU_DMA_OP_UNSUPPORTED();
117 struct dma_map_ops dma_npu_ops = {
118 .map_page = dma_npu_map_page,
119 .map_sg = dma_npu_map_sg,
120 .alloc = dma_npu_alloc,
121 .free = dma_npu_free,
122 .dma_supported = dma_npu_dma_supported,
123 .get_required_mask = dma_npu_get_required_mask,
127 * Returns the PE assoicated with the PCI device of the given
128 * NPU. Returns the linked pci device if pci_dev != NULL.
130 static struct pnv_ioda_pe *get_gpu_pci_dev_and_pe(struct pnv_ioda_pe *npe,
131 struct pci_dev **gpdev)
134 struct pci_controller *hose;
135 struct pci_dev *pdev;
136 struct pnv_ioda_pe *pe;
139 if (npe->flags & PNV_IODA_PE_PEER) {
143 pdev = pnv_pci_get_gpu_dev(npe->pdev);
147 pdn = pci_get_pdn(pdev);
148 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
151 hose = pci_bus_to_host(pdev->bus);
152 phb = hose->private_data;
153 pe = &phb->ioda.pe_array[pdn->pe_number];
162 void pnv_npu_init_dma_pe(struct pnv_ioda_pe *npe)
164 struct pnv_ioda_pe *gpe;
165 struct pci_dev *gpdev;
168 if (!npe->pdev || !(npe->flags & PNV_IODA_PE_DEV))
171 gpe = get_gpu_pci_dev_and_pe(npe, &gpdev);
175 for (i = 0; i < PNV_IODA_MAX_PEER_PES; i++) {
176 /* Nothing to do if the PE is already connected. */
177 if (gpe->peers[i] == npe)
184 if (WARN_ON(avail < 0))
187 gpe->peers[avail] = npe;
188 gpe->flags |= PNV_IODA_PE_PEER;
191 * We assume that the NPU devices only have a single peer PE
192 * (the GPU PCIe device PE).
195 npe->flags |= PNV_IODA_PE_PEER;
199 * For the NPU we want to point the TCE table at the same table as the
202 static void pnv_npu_disable_bypass(struct pnv_ioda_pe *npe)
204 struct pnv_phb *phb = npe->phb;
205 struct pci_dev *gpdev;
206 struct pnv_ioda_pe *gpe;
207 struct iommu_table *tbl;
211 * Find the assoicated PCI devices and get the dma window
212 * information from there.
214 if (!npe->pdev || !(npe->flags & PNV_IODA_PE_DEV))
217 gpe = get_gpu_pci_dev_and_pe(npe, &gpdev);
221 tbl = gpe->table_group.tables[0];
222 rc = opal_pci_map_pe_dma_window(phb->opal_id, npe->pe_number,
223 npe->pe_number, 1, __pa(tbl->it_base),
225 IOMMU_PAGE_SIZE(tbl));
226 if (rc != OPAL_SUCCESS)
227 pr_warn("%s: Error %lld setting DMA window on PHB#%d-PE#%d\n",
228 __func__, rc, phb->hose->global_number, npe->pe_number);
231 * We don't initialise npu_pe->tce32_table as we always use
232 * dma_npu_ops which are nops.
234 set_dma_ops(&npe->pdev->dev, &dma_npu_ops);
238 * Enable/disable bypass mode on the NPU. The NPU only supports one
239 * window per link, so bypass needs to be explicitly enabled or
240 * disabled. Unlike for a PHB3 bypass and non-bypass modes can't be
241 * active at the same time.
243 int pnv_npu_dma_set_bypass(struct pnv_ioda_pe *npe, bool enable)
245 struct pnv_phb *phb = npe->phb;
248 if (phb->type != PNV_PHB_NPU || !npe->pdev)
252 /* Enable the bypass window */
253 phys_addr_t top = memblock_end_of_DRAM();
255 npe->tce_bypass_base = 0;
256 top = roundup_pow_of_two(top);
257 dev_info(&npe->pdev->dev, "Enabling bypass for PE %d\n",
259 rc = opal_pci_map_pe_dma_window_real(phb->opal_id,
260 npe->pe_number, npe->pe_number,
261 npe->tce_bypass_base, top);
264 * Disable the bypass window by replacing it with the
267 pnv_npu_disable_bypass(npe);
273 int pnv_npu_dma_set_mask(struct pci_dev *npdev, u64 dma_mask)
275 struct pci_controller *hose = pci_bus_to_host(npdev->bus);
276 struct pnv_phb *phb = hose->private_data;
277 struct pci_dn *pdn = pci_get_pdn(npdev);
278 struct pnv_ioda_pe *npe, *gpe;
279 struct pci_dev *gpdev;
283 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
286 /* We only do bypass if it's enabled on the linked device */
287 npe = &phb->ioda.pe_array[pdn->pe_number];
288 gpe = get_gpu_pci_dev_and_pe(npe, &gpdev);
292 if (gpe->tce_bypass_enabled) {
293 top = gpe->tce_bypass_base + memblock_end_of_DRAM() - 1;
294 bypass = (dma_mask >= top);
298 dev_info(&npdev->dev, "Using 64-bit DMA iommu bypass\n");
300 dev_info(&npdev->dev, "Using 32-bit DMA via iommu\n");
302 pnv_npu_dma_set_bypass(npe, bypass);
303 *npdev->dev.dma_mask = dma_mask;