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>
15 #include <linux/iommu.h>
17 #include <asm/iommu.h>
18 #include <asm/pnv-pci.h>
19 #include <asm/msi_bitmap.h>
26 * Other types of TCE cache invalidation are not functional in the
29 static struct pci_dev
*get_pci_dev(struct device_node
*dn
)
31 return PCI_DN(dn
)->pcidev
;
34 /* Given a NPU device get the associated PCI device. */
35 struct pci_dev
*pnv_pci_get_gpu_dev(struct pci_dev
*npdev
)
37 struct device_node
*dn
;
38 struct pci_dev
*gpdev
;
43 if (WARN_ON(!npdev
->dev
.of_node
))
46 /* Get assoicated PCI device */
47 dn
= of_parse_phandle(npdev
->dev
.of_node
, "ibm,gpu", 0);
51 gpdev
= get_pci_dev(dn
);
56 EXPORT_SYMBOL(pnv_pci_get_gpu_dev
);
58 /* Given the real PCI device get a linked NPU device. */
59 struct pci_dev
*pnv_pci_get_npu_dev(struct pci_dev
*gpdev
, int index
)
61 struct device_node
*dn
;
62 struct pci_dev
*npdev
;
67 if (WARN_ON(!gpdev
->dev
.of_node
))
70 /* Get assoicated PCI device */
71 dn
= of_parse_phandle(gpdev
->dev
.of_node
, "ibm,npu", index
);
75 npdev
= get_pci_dev(dn
);
80 EXPORT_SYMBOL(pnv_pci_get_npu_dev
);
82 #define NPU_DMA_OP_UNSUPPORTED() \
83 dev_err_once(dev, "%s operation unsupported for NVLink devices\n", \
86 static void *dma_npu_alloc(struct device
*dev
, size_t size
,
87 dma_addr_t
*dma_handle
, gfp_t flag
,
90 NPU_DMA_OP_UNSUPPORTED();
94 static void dma_npu_free(struct device
*dev
, size_t size
,
95 void *vaddr
, dma_addr_t dma_handle
,
98 NPU_DMA_OP_UNSUPPORTED();
101 static dma_addr_t
dma_npu_map_page(struct device
*dev
, struct page
*page
,
102 unsigned long offset
, size_t size
,
103 enum dma_data_direction direction
,
106 NPU_DMA_OP_UNSUPPORTED();
110 static int dma_npu_map_sg(struct device
*dev
, struct scatterlist
*sglist
,
111 int nelems
, enum dma_data_direction direction
,
114 NPU_DMA_OP_UNSUPPORTED();
118 static int dma_npu_dma_supported(struct device
*dev
, u64 mask
)
120 NPU_DMA_OP_UNSUPPORTED();
124 static u64
dma_npu_get_required_mask(struct device
*dev
)
126 NPU_DMA_OP_UNSUPPORTED();
130 static struct dma_map_ops dma_npu_ops
= {
131 .map_page
= dma_npu_map_page
,
132 .map_sg
= dma_npu_map_sg
,
133 .alloc
= dma_npu_alloc
,
134 .free
= dma_npu_free
,
135 .dma_supported
= dma_npu_dma_supported
,
136 .get_required_mask
= dma_npu_get_required_mask
,
140 * Returns the PE assoicated with the PCI device of the given
141 * NPU. Returns the linked pci device if pci_dev != NULL.
143 static struct pnv_ioda_pe
*get_gpu_pci_dev_and_pe(struct pnv_ioda_pe
*npe
,
144 struct pci_dev
**gpdev
)
147 struct pci_controller
*hose
;
148 struct pci_dev
*pdev
;
149 struct pnv_ioda_pe
*pe
;
152 pdev
= pnv_pci_get_gpu_dev(npe
->pdev
);
156 pdn
= pci_get_pdn(pdev
);
157 if (WARN_ON(!pdn
|| pdn
->pe_number
== IODA_INVALID_PE
))
160 hose
= pci_bus_to_host(pdev
->bus
);
161 phb
= hose
->private_data
;
162 pe
= &phb
->ioda
.pe_array
[pdn
->pe_number
];
170 long pnv_npu_set_window(struct pnv_ioda_pe
*npe
, int num
,
171 struct iommu_table
*tbl
)
173 struct pnv_phb
*phb
= npe
->phb
;
175 const unsigned long size
= tbl
->it_indirect_levels
?
176 tbl
->it_level_size
: tbl
->it_size
;
177 const __u64 start_addr
= tbl
->it_offset
<< tbl
->it_page_shift
;
178 const __u64 win_size
= tbl
->it_size
<< tbl
->it_page_shift
;
180 pe_info(npe
, "Setting up window %llx..%llx pg=%lx\n",
181 start_addr
, start_addr
+ win_size
- 1,
182 IOMMU_PAGE_SIZE(tbl
));
184 rc
= opal_pci_map_pe_dma_window(phb
->opal_id
,
187 tbl
->it_indirect_levels
+ 1,
190 IOMMU_PAGE_SIZE(tbl
));
192 pe_err(npe
, "Failed to configure TCE table, err %lld\n", rc
);
195 pnv_pci_ioda2_tce_invalidate_entire(phb
, false);
197 /* Add the table to the list so its TCE cache will get invalidated */
198 pnv_pci_link_table_and_group(phb
->hose
->node
, num
,
199 tbl
, &npe
->table_group
);
204 long pnv_npu_unset_window(struct pnv_ioda_pe
*npe
, int num
)
206 struct pnv_phb
*phb
= npe
->phb
;
209 pe_info(npe
, "Removing DMA window\n");
211 rc
= opal_pci_map_pe_dma_window(phb
->opal_id
, npe
->pe_number
,
213 0/* levels */, 0/* table address */,
214 0/* table size */, 0/* page size */);
216 pe_err(npe
, "Unmapping failed, ret = %lld\n", rc
);
219 pnv_pci_ioda2_tce_invalidate_entire(phb
, false);
221 pnv_pci_unlink_table_and_group(npe
->table_group
.tables
[num
],
228 * Enables 32 bit DMA on NPU.
230 static void pnv_npu_dma_set_32(struct pnv_ioda_pe
*npe
)
232 struct pci_dev
*gpdev
;
233 struct pnv_ioda_pe
*gpe
;
237 * Find the assoicated PCI devices and get the dma window
238 * information from there.
240 if (!npe
->pdev
|| !(npe
->flags
& PNV_IODA_PE_DEV
))
243 gpe
= get_gpu_pci_dev_and_pe(npe
, &gpdev
);
247 rc
= pnv_npu_set_window(npe
, 0, gpe
->table_group
.tables
[0]);
250 * We don't initialise npu_pe->tce32_table as we always use
251 * dma_npu_ops which are nops.
253 set_dma_ops(&npe
->pdev
->dev
, &dma_npu_ops
);
257 * Enables bypass mode on the NPU. The NPU only supports one
258 * window per link, so bypass needs to be explicitly enabled or
259 * disabled. Unlike for a PHB3 bypass and non-bypass modes can't be
260 * active at the same time.
262 static int pnv_npu_dma_set_bypass(struct pnv_ioda_pe
*npe
)
264 struct pnv_phb
*phb
= npe
->phb
;
266 phys_addr_t top
= memblock_end_of_DRAM();
268 if (phb
->type
!= PNV_PHB_NPU
|| !npe
->pdev
)
271 rc
= pnv_npu_unset_window(npe
, 0);
272 if (rc
!= OPAL_SUCCESS
)
275 /* Enable the bypass window */
277 top
= roundup_pow_of_two(top
);
278 dev_info(&npe
->pdev
->dev
, "Enabling bypass for PE %x\n",
280 rc
= opal_pci_map_pe_dma_window_real(phb
->opal_id
,
281 npe
->pe_number
, npe
->pe_number
,
282 0 /* bypass base */, top
);
284 if (rc
== OPAL_SUCCESS
)
285 pnv_pci_ioda2_tce_invalidate_entire(phb
, false);
290 void pnv_npu_try_dma_set_bypass(struct pci_dev
*gpdev
, bool bypass
)
295 struct pnv_ioda_pe
*npe
;
296 struct pci_dev
*npdev
;
299 npdev
= pnv_pci_get_npu_dev(gpdev
, i
);
304 pdn
= pci_get_pdn(npdev
);
305 if (WARN_ON(!pdn
|| pdn
->pe_number
== IODA_INVALID_PE
))
308 phb
= pci_bus_to_host(npdev
->bus
)->private_data
;
310 /* We only do bypass if it's enabled on the linked device */
311 npe
= &phb
->ioda
.pe_array
[pdn
->pe_number
];
314 dev_info(&npdev
->dev
,
315 "Using 64-bit DMA iommu bypass\n");
316 pnv_npu_dma_set_bypass(npe
);
318 dev_info(&npdev
->dev
, "Using 32-bit DMA via iommu\n");
319 pnv_npu_dma_set_32(npe
);
324 /* Switch ownership from platform code to external user (e.g. VFIO) */
325 void pnv_npu_take_ownership(struct pnv_ioda_pe
*npe
)
327 struct pnv_phb
*phb
= npe
->phb
;
331 * Note: NPU has just a single TVE in the hardware which means that
332 * while used by the kernel, it can have either 32bit window or
333 * DMA bypass but never both. So we deconfigure 32bit window only
334 * if it was enabled at the moment of ownership change.
336 if (npe
->table_group
.tables
[0]) {
337 pnv_npu_unset_window(npe
, 0);
342 rc
= opal_pci_map_pe_dma_window_real(phb
->opal_id
,
343 npe
->pe_number
, npe
->pe_number
,
344 0 /* bypass base */, 0);
346 pe_err(npe
, "Failed to disable bypass, err %lld\n", rc
);
349 pnv_pci_ioda2_tce_invalidate_entire(npe
->phb
, false);
352 struct pnv_ioda_pe
*pnv_pci_npu_setup_iommu(struct pnv_ioda_pe
*npe
)
354 struct pnv_phb
*phb
= npe
->phb
;
355 struct pci_bus
*pbus
= phb
->hose
->bus
;
356 struct pci_dev
*npdev
, *gpdev
= NULL
, *gptmp
;
357 struct pnv_ioda_pe
*gpe
= get_gpu_pci_dev_and_pe(npe
, &gpdev
);
362 list_for_each_entry(npdev
, &pbus
->devices
, bus_list
) {
363 gptmp
= pnv_pci_get_gpu_dev(npdev
);
368 pe_info(gpe
, "Attached NPU %s\n", dev_name(&npdev
->dev
));
369 iommu_group_add_device(gpe
->table_group
.group
, &npdev
->dev
);