]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/powerpc/platforms/powernv/npu-dma.c
powerpc/powernv/npu: Use the correct IOMMU page size
[karo-tx-linux.git] / arch / powerpc / platforms / powernv / npu-dma.c
1 /*
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.
4  *
5  * Copyright Alistair Popple, IBM Corporation 2015.
6  *
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.
10  */
11
12 #include <linux/export.h>
13 #include <linux/pci.h>
14 #include <linux/memblock.h>
15
16 #include <asm/iommu.h>
17 #include <asm/pnv-pci.h>
18 #include <asm/msi_bitmap.h>
19 #include <asm/opal.h>
20
21 #include "powernv.h"
22 #include "pci.h"
23
24 /*
25  * Other types of TCE cache invalidation are not functional in the
26  * hardware.
27  */
28 static struct pci_dev *get_pci_dev(struct device_node *dn)
29 {
30         return PCI_DN(dn)->pcidev;
31 }
32
33 /* Given a NPU device get the associated PCI device. */
34 struct pci_dev *pnv_pci_get_gpu_dev(struct pci_dev *npdev)
35 {
36         struct device_node *dn;
37         struct pci_dev *gpdev;
38
39         /* Get assoicated PCI device */
40         dn = of_parse_phandle(npdev->dev.of_node, "ibm,gpu", 0);
41         if (!dn)
42                 return NULL;
43
44         gpdev = get_pci_dev(dn);
45         of_node_put(dn);
46
47         return gpdev;
48 }
49 EXPORT_SYMBOL(pnv_pci_get_gpu_dev);
50
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)
53 {
54         struct device_node *dn;
55         struct pci_dev *npdev;
56
57         /* Get assoicated PCI device */
58         dn = of_parse_phandle(gpdev->dev.of_node, "ibm,npu", index);
59         if (!dn)
60                 return NULL;
61
62         npdev = get_pci_dev(dn);
63         of_node_put(dn);
64
65         return npdev;
66 }
67 EXPORT_SYMBOL(pnv_pci_get_npu_dev);
68
69 #define NPU_DMA_OP_UNSUPPORTED()                                        \
70         dev_err_once(dev, "%s operation unsupported for NVLink devices\n", \
71                 __func__)
72
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)
76 {
77         NPU_DMA_OP_UNSUPPORTED();
78         return NULL;
79 }
80
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)
84 {
85         NPU_DMA_OP_UNSUPPORTED();
86 }
87
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)
92 {
93         NPU_DMA_OP_UNSUPPORTED();
94         return 0;
95 }
96
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)
100 {
101         NPU_DMA_OP_UNSUPPORTED();
102         return 0;
103 }
104
105 static int dma_npu_dma_supported(struct device *dev, u64 mask)
106 {
107         NPU_DMA_OP_UNSUPPORTED();
108         return 0;
109 }
110
111 static u64 dma_npu_get_required_mask(struct device *dev)
112 {
113         NPU_DMA_OP_UNSUPPORTED();
114         return 0;
115 }
116
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,
124 };
125
126 /*
127  * Returns the PE assoicated with the PCI device of the given
128  * NPU. Returns the linked pci device if pci_dev != NULL.
129  */
130 static struct pnv_ioda_pe *get_gpu_pci_dev_and_pe(struct pnv_ioda_pe *npe,
131                                                   struct pci_dev **gpdev)
132 {
133         struct pnv_phb *phb;
134         struct pci_controller *hose;
135         struct pci_dev *pdev;
136         struct pnv_ioda_pe *pe;
137         struct pci_dn *pdn;
138
139         if (npe->flags & PNV_IODA_PE_PEER) {
140                 pe = npe->peers[0];
141                 pdev = pe->pdev;
142         } else {
143                 pdev = pnv_pci_get_gpu_dev(npe->pdev);
144                 if (!pdev)
145                         return NULL;
146
147                 pdn = pci_get_pdn(pdev);
148                 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
149                         return NULL;
150
151                 hose = pci_bus_to_host(pdev->bus);
152                 phb = hose->private_data;
153                 pe = &phb->ioda.pe_array[pdn->pe_number];
154         }
155
156         if (gpdev)
157                 *gpdev = pdev;
158
159         return pe;
160 }
161
162 void pnv_npu_init_dma_pe(struct pnv_ioda_pe *npe)
163 {
164         struct pnv_ioda_pe *gpe;
165         struct pci_dev *gpdev;
166         int i, avail = -1;
167
168         if (!npe->pdev || !(npe->flags & PNV_IODA_PE_DEV))
169                 return;
170
171         gpe = get_gpu_pci_dev_and_pe(npe, &gpdev);
172         if (!gpe)
173                 return;
174
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)
178                         return;
179
180                 if (!gpe->peers[i])
181                         avail = i;
182         }
183
184         if (WARN_ON(avail < 0))
185                 return;
186
187         gpe->peers[avail] = npe;
188         gpe->flags |= PNV_IODA_PE_PEER;
189
190         /*
191          * We assume that the NPU devices only have a single peer PE
192          * (the GPU PCIe device PE).
193          */
194         npe->peers[0] = gpe;
195         npe->flags |= PNV_IODA_PE_PEER;
196 }
197
198 /*
199  * For the NPU we want to point the TCE table at the same table as the
200  * real PCI device.
201  */
202 static void pnv_npu_disable_bypass(struct pnv_ioda_pe *npe)
203 {
204         struct pnv_phb *phb = npe->phb;
205         struct pci_dev *gpdev;
206         struct pnv_ioda_pe *gpe;
207         struct iommu_table *tbl;
208         int64_t rc;
209
210         /*
211          * Find the assoicated PCI devices and get the dma window
212          * information from there.
213          */
214         if (!npe->pdev || !(npe->flags & PNV_IODA_PE_DEV))
215                 return;
216
217         gpe = get_gpu_pci_dev_and_pe(npe, &gpdev);
218         if (!gpe)
219                 return;
220
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),
224                                         tbl->it_size << 3,
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);
229
230         /*
231          * We don't initialise npu_pe->tce32_table as we always use
232          * dma_npu_ops which are nops.
233          */
234         set_dma_ops(&npe->pdev->dev, &dma_npu_ops);
235 }
236
237 /*
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.
242  */
243 int pnv_npu_dma_set_bypass(struct pnv_ioda_pe *npe, bool enable)
244 {
245         struct pnv_phb *phb = npe->phb;
246         int64_t rc = 0;
247
248         if (phb->type != PNV_PHB_NPU || !npe->pdev)
249                 return -EINVAL;
250
251         if (enable) {
252                 /* Enable the bypass window */
253                 phys_addr_t top = memblock_end_of_DRAM();
254
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",
258                          npe->pe_number);
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);
262         } else {
263                 /*
264                  * Disable the bypass window by replacing it with the
265                  * TCE32 window.
266                  */
267                 pnv_npu_disable_bypass(npe);
268         }
269
270         return rc;
271 }
272
273 int pnv_npu_dma_set_mask(struct pci_dev *npdev, u64 dma_mask)
274 {
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;
280         uint64_t top;
281         bool bypass = false;
282
283         if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
284                 return -ENXIO;
285
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);
289         if (!gpe)
290                 return -ENODEV;
291
292         if (gpe->tce_bypass_enabled) {
293                 top = gpe->tce_bypass_base + memblock_end_of_DRAM() - 1;
294                 bypass = (dma_mask >= top);
295         }
296
297         if (bypass)
298                 dev_info(&npdev->dev, "Using 64-bit DMA iommu bypass\n");
299         else
300                 dev_info(&npdev->dev, "Using 32-bit DMA via iommu\n");
301
302         pnv_npu_dma_set_bypass(npe, bypass);
303         *npdev->dev.dma_mask = dma_mask;
304
305         return 0;
306 }