xref: /qemu/hw/ppc/spapr_iommu.c (revision e4c35b78bce645aaa299d3b7b62494c880c6c74d)
1 /*
2  * QEMU sPAPR IOMMU (TCE) code
3  *
4  * Copyright (c) 2010 David Gibson, IBM Corporation <dwg@au1.ibm.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19 #include "hw/hw.h"
20 #include "sysemu/kvm.h"
21 #include "hw/qdev.h"
22 #include "kvm_ppc.h"
23 #include "sysemu/dma.h"
24 #include "exec/address-spaces.h"
25 #include "trace.h"
26 
27 #include "hw/ppc/spapr.h"
28 
29 #include <libfdt.h>
30 
31 enum sPAPRTCEAccess {
32     SPAPR_TCE_FAULT = 0,
33     SPAPR_TCE_RO = 1,
34     SPAPR_TCE_WO = 2,
35     SPAPR_TCE_RW = 3,
36 };
37 
38 static QLIST_HEAD(spapr_tce_tables, sPAPRTCETable) spapr_tce_tables;
39 
40 static sPAPRTCETable *spapr_tce_find_by_liobn(uint32_t liobn)
41 {
42     sPAPRTCETable *tcet;
43 
44     if (liobn & 0xFFFFFFFF00000000ULL) {
45         hcall_dprintf("Request for out-of-bounds LIOBN 0x" TARGET_FMT_lx "\n",
46                       liobn);
47         return NULL;
48     }
49 
50     QLIST_FOREACH(tcet, &spapr_tce_tables, list) {
51         if (tcet->liobn == liobn) {
52             return tcet;
53         }
54     }
55 
56     return NULL;
57 }
58 
59 static IOMMUTLBEntry spapr_tce_translate_iommu(MemoryRegion *iommu, hwaddr addr)
60 {
61     sPAPRTCETable *tcet = container_of(iommu, sPAPRTCETable, iommu);
62     uint64_t tce;
63     IOMMUTLBEntry ret = {
64         .target_as = &address_space_memory,
65         .iova = 0,
66         .translated_addr = 0,
67         .addr_mask = ~(hwaddr)0,
68         .perm = IOMMU_NONE,
69     };
70 
71     if (tcet->bypass) {
72         ret.perm = IOMMU_RW;
73     } else if (addr < tcet->window_size) {
74         /* Check if we are in bound */
75         tce = tcet->table[addr >> SPAPR_TCE_PAGE_SHIFT];
76         ret.iova = addr & ~SPAPR_TCE_PAGE_MASK;
77         ret.translated_addr = tce & ~SPAPR_TCE_PAGE_MASK;
78         ret.addr_mask = SPAPR_TCE_PAGE_MASK;
79         ret.perm = tce;
80     }
81     trace_spapr_iommu_xlate(tcet->liobn, addr, ret.iova, ret.perm,
82                             ret.addr_mask);
83 
84     return ret;
85 }
86 
87 static int spapr_tce_table_pre_load(void *opaque)
88 {
89     sPAPRTCETable *tcet = SPAPR_TCE_TABLE(opaque);
90 
91     tcet->nb_table = tcet->window_size >> SPAPR_TCE_PAGE_SHIFT;
92 
93     return 0;
94 }
95 
96 static const VMStateDescription vmstate_spapr_tce_table = {
97     .name = "spapr_iommu",
98     .version_id = 1,
99     .minimum_version_id = 1,
100     .pre_load = spapr_tce_table_pre_load,
101     .fields = (VMStateField[]) {
102         /* Sanity check */
103         VMSTATE_UINT32_EQUAL(liobn, sPAPRTCETable),
104         VMSTATE_UINT32_EQUAL(window_size, sPAPRTCETable),
105 
106         /* IOMMU state */
107         VMSTATE_BOOL(bypass, sPAPRTCETable),
108         VMSTATE_VARRAY_UINT32(table, sPAPRTCETable, nb_table, 0, vmstate_info_uint64, uint64_t),
109 
110         VMSTATE_END_OF_LIST()
111     },
112 };
113 
114 static MemoryRegionIOMMUOps spapr_iommu_ops = {
115     .translate = spapr_tce_translate_iommu,
116 };
117 
118 static int spapr_tce_table_realize(DeviceState *dev)
119 {
120     sPAPRTCETable *tcet = SPAPR_TCE_TABLE(dev);
121 
122     if (kvm_enabled()) {
123         tcet->table = kvmppc_create_spapr_tce(tcet->liobn,
124                                               tcet->window_size,
125                                               &tcet->fd);
126     }
127 
128     if (!tcet->table) {
129         size_t table_size = (tcet->window_size >> SPAPR_TCE_PAGE_SHIFT)
130             * sizeof(uint64_t);
131         tcet->table = g_malloc0(table_size);
132     }
133     tcet->nb_table = tcet->window_size >> SPAPR_TCE_PAGE_SHIFT;
134 
135     trace_spapr_iommu_new_table(tcet->liobn, tcet, tcet->table, tcet->fd);
136 
137     memory_region_init_iommu(&tcet->iommu, OBJECT(dev), &spapr_iommu_ops,
138                              "iommu-spapr", ram_size);
139 
140     QLIST_INSERT_HEAD(&spapr_tce_tables, tcet, list);
141 
142     vmstate_register(DEVICE(tcet), tcet->liobn, &vmstate_spapr_tce_table,
143                      tcet);
144 
145     return 0;
146 }
147 
148 sPAPRTCETable *spapr_tce_new_table(DeviceState *owner, uint32_t liobn, size_t window_size)
149 {
150     sPAPRTCETable *tcet;
151 
152     if (spapr_tce_find_by_liobn(liobn)) {
153         fprintf(stderr, "Attempted to create TCE table with duplicate"
154                 " LIOBN 0x%x\n", liobn);
155         return NULL;
156     }
157 
158     if (!window_size) {
159         return NULL;
160     }
161 
162     tcet = SPAPR_TCE_TABLE(object_new(TYPE_SPAPR_TCE_TABLE));
163     tcet->liobn = liobn;
164     tcet->window_size = window_size;
165 
166     object_property_add_child(OBJECT(owner), "tce-table", OBJECT(tcet), NULL);
167 
168     object_property_set_bool(OBJECT(tcet), true, "realized", NULL);
169 
170     return tcet;
171 }
172 
173 static void spapr_tce_table_finalize(Object *obj)
174 {
175     sPAPRTCETable *tcet = SPAPR_TCE_TABLE(obj);
176 
177     QLIST_REMOVE(tcet, list);
178 
179     if (!kvm_enabled() ||
180         (kvmppc_remove_spapr_tce(tcet->table, tcet->fd,
181                                  tcet->window_size) != 0)) {
182         g_free(tcet->table);
183     }
184 }
185 
186 MemoryRegion *spapr_tce_get_iommu(sPAPRTCETable *tcet)
187 {
188     return &tcet->iommu;
189 }
190 
191 void spapr_tce_set_bypass(sPAPRTCETable *tcet, bool bypass)
192 {
193     tcet->bypass = bypass;
194 }
195 
196 static void spapr_tce_reset(DeviceState *dev)
197 {
198     sPAPRTCETable *tcet = SPAPR_TCE_TABLE(dev);
199     size_t table_size = (tcet->window_size >> SPAPR_TCE_PAGE_SHIFT)
200         * sizeof(uint64_t);
201 
202     tcet->bypass = false;
203     memset(tcet->table, 0, table_size);
204 }
205 
206 static target_ulong put_tce_emu(sPAPRTCETable *tcet, target_ulong ioba,
207                                 target_ulong tce)
208 {
209     IOMMUTLBEntry entry;
210 
211     if (ioba >= tcet->window_size) {
212         hcall_dprintf("spapr_vio_put_tce on out-of-bounds IOBA 0x"
213                       TARGET_FMT_lx "\n", ioba);
214         return H_PARAMETER;
215     }
216 
217     tcet->table[ioba >> SPAPR_TCE_PAGE_SHIFT] = tce;
218 
219     entry.target_as = &address_space_memory,
220     entry.iova = ioba & ~SPAPR_TCE_PAGE_MASK;
221     entry.translated_addr = tce & ~SPAPR_TCE_PAGE_MASK;
222     entry.addr_mask = SPAPR_TCE_PAGE_MASK;
223     entry.perm = tce;
224     memory_region_notify_iommu(&tcet->iommu, entry);
225 
226     return H_SUCCESS;
227 }
228 
229 static target_ulong h_put_tce_indirect(PowerPCCPU *cpu,
230                                        sPAPREnvironment *spapr,
231                                        target_ulong opcode, target_ulong *args)
232 {
233     int i;
234     target_ulong liobn = args[0];
235     target_ulong ioba = args[1];
236     target_ulong ioba1 = ioba;
237     target_ulong tce_list = args[2];
238     target_ulong npages = args[3];
239     target_ulong ret = H_PARAMETER;
240     sPAPRTCETable *tcet = spapr_tce_find_by_liobn(liobn);
241     CPUState *cs = CPU(cpu);
242 
243     if (!tcet) {
244         return H_PARAMETER;
245     }
246 
247     if (npages > 512) {
248         return H_PARAMETER;
249     }
250 
251     ioba &= ~SPAPR_TCE_PAGE_MASK;
252     tce_list &= ~SPAPR_TCE_PAGE_MASK;
253 
254     for (i = 0; i < npages; ++i, ioba += SPAPR_TCE_PAGE_SIZE) {
255         target_ulong tce = ldq_phys(cs->as, tce_list +
256                                     i * sizeof(target_ulong));
257         ret = put_tce_emu(tcet, ioba, tce);
258         if (ret) {
259             break;
260         }
261     }
262 
263     /* Trace last successful or the first problematic entry */
264     i = i ? (i - 1) : 0;
265     trace_spapr_iommu_indirect(liobn, ioba1, tce_list, i,
266                                ldq_phys(cs->as,
267                                tce_list + i * sizeof(target_ulong)),
268                                ret);
269 
270     return ret;
271 }
272 
273 static target_ulong h_stuff_tce(PowerPCCPU *cpu, sPAPREnvironment *spapr,
274                               target_ulong opcode, target_ulong *args)
275 {
276     int i;
277     target_ulong liobn = args[0];
278     target_ulong ioba = args[1];
279     target_ulong tce_value = args[2];
280     target_ulong npages = args[3];
281     target_ulong ret = H_PARAMETER;
282     sPAPRTCETable *tcet = spapr_tce_find_by_liobn(liobn);
283 
284     if (!tcet) {
285         return H_PARAMETER;
286     }
287 
288     if (npages > tcet->nb_table) {
289         return H_PARAMETER;
290     }
291 
292     ioba &= ~SPAPR_TCE_PAGE_MASK;
293 
294     for (i = 0; i < npages; ++i, ioba += SPAPR_TCE_PAGE_SIZE) {
295         ret = put_tce_emu(tcet, ioba, tce_value);
296         if (ret) {
297             break;
298         }
299     }
300     trace_spapr_iommu_stuff(liobn, ioba, tce_value, npages, ret);
301 
302     return ret;
303 }
304 
305 static target_ulong h_put_tce(PowerPCCPU *cpu, sPAPREnvironment *spapr,
306                               target_ulong opcode, target_ulong *args)
307 {
308     target_ulong liobn = args[0];
309     target_ulong ioba = args[1];
310     target_ulong tce = args[2];
311     target_ulong ret = H_PARAMETER;
312     sPAPRTCETable *tcet = spapr_tce_find_by_liobn(liobn);
313 
314     ioba &= ~(SPAPR_TCE_PAGE_SIZE - 1);
315 
316     if (tcet) {
317         ret = put_tce_emu(tcet, ioba, tce);
318     }
319     trace_spapr_iommu_put(liobn, ioba, tce, ret);
320 
321     return ret;
322 }
323 
324 static target_ulong get_tce_emu(sPAPRTCETable *tcet, target_ulong ioba,
325                                 target_ulong *tce)
326 {
327     if (ioba >= tcet->window_size) {
328         hcall_dprintf("spapr_iommu_get_tce on out-of-bounds IOBA 0x"
329                       TARGET_FMT_lx "\n", ioba);
330         return H_PARAMETER;
331     }
332 
333     *tce = tcet->table[ioba >> SPAPR_TCE_PAGE_SHIFT];
334 
335     return H_SUCCESS;
336 }
337 
338 static target_ulong h_get_tce(PowerPCCPU *cpu, sPAPREnvironment *spapr,
339                               target_ulong opcode, target_ulong *args)
340 {
341     target_ulong liobn = args[0];
342     target_ulong ioba = args[1];
343     target_ulong tce = 0;
344     target_ulong ret = H_PARAMETER;
345     sPAPRTCETable *tcet = spapr_tce_find_by_liobn(liobn);
346 
347     ioba &= ~(SPAPR_TCE_PAGE_SIZE - 1);
348 
349     if (tcet) {
350         ret = get_tce_emu(tcet, ioba, &tce);
351         if (!ret) {
352             args[0] = tce;
353         }
354     }
355     trace_spapr_iommu_get(liobn, ioba, ret, tce);
356 
357     return ret;
358 }
359 
360 int spapr_dma_dt(void *fdt, int node_off, const char *propname,
361                  uint32_t liobn, uint64_t window, uint32_t size)
362 {
363     uint32_t dma_prop[5];
364     int ret;
365 
366     dma_prop[0] = cpu_to_be32(liobn);
367     dma_prop[1] = cpu_to_be32(window >> 32);
368     dma_prop[2] = cpu_to_be32(window & 0xFFFFFFFF);
369     dma_prop[3] = 0; /* window size is 32 bits */
370     dma_prop[4] = cpu_to_be32(size);
371 
372     ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-address-cells", 2);
373     if (ret < 0) {
374         return ret;
375     }
376 
377     ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-size-cells", 2);
378     if (ret < 0) {
379         return ret;
380     }
381 
382     ret = fdt_setprop(fdt, node_off, propname, dma_prop, sizeof(dma_prop));
383     if (ret < 0) {
384         return ret;
385     }
386 
387     return 0;
388 }
389 
390 int spapr_tcet_dma_dt(void *fdt, int node_off, const char *propname,
391                       sPAPRTCETable *tcet)
392 {
393     if (!tcet) {
394         return 0;
395     }
396 
397     return spapr_dma_dt(fdt, node_off, propname,
398                         tcet->liobn, 0, tcet->window_size);
399 }
400 
401 static void spapr_tce_table_class_init(ObjectClass *klass, void *data)
402 {
403     DeviceClass *dc = DEVICE_CLASS(klass);
404     dc->init = spapr_tce_table_realize;
405     dc->reset = spapr_tce_reset;
406 
407     QLIST_INIT(&spapr_tce_tables);
408 
409     /* hcall-tce */
410     spapr_register_hypercall(H_PUT_TCE, h_put_tce);
411     spapr_register_hypercall(H_GET_TCE, h_get_tce);
412     spapr_register_hypercall(H_PUT_TCE_INDIRECT, h_put_tce_indirect);
413     spapr_register_hypercall(H_STUFF_TCE, h_stuff_tce);
414 }
415 
416 static TypeInfo spapr_tce_table_info = {
417     .name = TYPE_SPAPR_TCE_TABLE,
418     .parent = TYPE_DEVICE,
419     .instance_size = sizeof(sPAPRTCETable),
420     .class_init = spapr_tce_table_class_init,
421     .instance_finalize = spapr_tce_table_finalize,
422 };
423 
424 static void register_types(void)
425 {
426     type_register_static(&spapr_tce_table_info);
427 }
428 
429 type_init(register_types);
430