xref: /qemu/hw/ppc/spapr_iommu.c (revision 523e7b8ab818ec3fab14cfd7a20d51cb3aa33a9d)
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 >> SPAPR_TCE_PAGE_SHIFT) < tcet->nb_table) {
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 const VMStateDescription vmstate_spapr_tce_table = {
88     .name = "spapr_iommu",
89     .version_id = 2,
90     .minimum_version_id = 2,
91     .fields      = (VMStateField []) {
92         /* Sanity check */
93         VMSTATE_UINT32_EQUAL(liobn, sPAPRTCETable),
94         VMSTATE_UINT32_EQUAL(nb_table, sPAPRTCETable),
95 
96         /* IOMMU state */
97         VMSTATE_BOOL(bypass, sPAPRTCETable),
98         VMSTATE_VARRAY_UINT32(table, sPAPRTCETable, nb_table, 0, vmstate_info_uint64, uint64_t),
99 
100         VMSTATE_END_OF_LIST()
101     },
102 };
103 
104 static MemoryRegionIOMMUOps spapr_iommu_ops = {
105     .translate = spapr_tce_translate_iommu,
106 };
107 
108 static int spapr_tce_table_realize(DeviceState *dev)
109 {
110     sPAPRTCETable *tcet = SPAPR_TCE_TABLE(dev);
111 
112     if (kvm_enabled()) {
113         tcet->table = kvmppc_create_spapr_tce(tcet->liobn,
114                                               tcet->nb_table <<
115                                               SPAPR_TCE_PAGE_SHIFT,
116                                               &tcet->fd);
117     }
118 
119     if (!tcet->table) {
120         size_t table_size = tcet->nb_table * sizeof(uint64_t);
121         tcet->table = g_malloc0(table_size);
122     }
123 
124     trace_spapr_iommu_new_table(tcet->liobn, tcet, tcet->table, tcet->fd);
125 
126     memory_region_init_iommu(&tcet->iommu, OBJECT(dev), &spapr_iommu_ops,
127                              "iommu-spapr", ram_size);
128 
129     QLIST_INSERT_HEAD(&spapr_tce_tables, tcet, list);
130 
131     vmstate_register(DEVICE(tcet), tcet->liobn, &vmstate_spapr_tce_table,
132                      tcet);
133 
134     return 0;
135 }
136 
137 sPAPRTCETable *spapr_tce_new_table(DeviceState *owner, uint32_t liobn,
138                                    uint32_t nb_table)
139 {
140     sPAPRTCETable *tcet;
141 
142     if (spapr_tce_find_by_liobn(liobn)) {
143         fprintf(stderr, "Attempted to create TCE table with duplicate"
144                 " LIOBN 0x%x\n", liobn);
145         return NULL;
146     }
147 
148     if (!nb_table) {
149         return NULL;
150     }
151 
152     tcet = SPAPR_TCE_TABLE(object_new(TYPE_SPAPR_TCE_TABLE));
153     tcet->liobn = liobn;
154     tcet->nb_table = nb_table;
155 
156     object_property_add_child(OBJECT(owner), "tce-table", OBJECT(tcet), NULL);
157 
158     object_property_set_bool(OBJECT(tcet), true, "realized", NULL);
159 
160     return tcet;
161 }
162 
163 static void spapr_tce_table_finalize(Object *obj)
164 {
165     sPAPRTCETable *tcet = SPAPR_TCE_TABLE(obj);
166 
167     QLIST_REMOVE(tcet, list);
168 
169     if (!kvm_enabled() ||
170         (kvmppc_remove_spapr_tce(tcet->table, tcet->fd,
171                                  tcet->nb_table) != 0)) {
172         g_free(tcet->table);
173     }
174 }
175 
176 MemoryRegion *spapr_tce_get_iommu(sPAPRTCETable *tcet)
177 {
178     return &tcet->iommu;
179 }
180 
181 void spapr_tce_set_bypass(sPAPRTCETable *tcet, bool bypass)
182 {
183     tcet->bypass = bypass;
184 }
185 
186 static void spapr_tce_reset(DeviceState *dev)
187 {
188     sPAPRTCETable *tcet = SPAPR_TCE_TABLE(dev);
189     size_t table_size = tcet->nb_table * sizeof(uint64_t);
190 
191     tcet->bypass = false;
192     memset(tcet->table, 0, table_size);
193 }
194 
195 static target_ulong put_tce_emu(sPAPRTCETable *tcet, target_ulong ioba,
196                                 target_ulong tce)
197 {
198     IOMMUTLBEntry entry;
199 
200     if ((ioba >> SPAPR_TCE_PAGE_SHIFT) >= tcet->nb_table) {
201         hcall_dprintf("spapr_vio_put_tce on out-of-bounds IOBA 0x"
202                       TARGET_FMT_lx "\n", ioba);
203         return H_PARAMETER;
204     }
205 
206     tcet->table[ioba >> SPAPR_TCE_PAGE_SHIFT] = tce;
207 
208     entry.target_as = &address_space_memory,
209     entry.iova = ioba & ~SPAPR_TCE_PAGE_MASK;
210     entry.translated_addr = tce & ~SPAPR_TCE_PAGE_MASK;
211     entry.addr_mask = SPAPR_TCE_PAGE_MASK;
212     entry.perm = tce;
213     memory_region_notify_iommu(&tcet->iommu, entry);
214 
215     return H_SUCCESS;
216 }
217 
218 static target_ulong h_put_tce_indirect(PowerPCCPU *cpu,
219                                        sPAPREnvironment *spapr,
220                                        target_ulong opcode, target_ulong *args)
221 {
222     int i;
223     target_ulong liobn = args[0];
224     target_ulong ioba = args[1];
225     target_ulong ioba1 = ioba;
226     target_ulong tce_list = args[2];
227     target_ulong npages = args[3];
228     target_ulong ret = H_PARAMETER;
229     sPAPRTCETable *tcet = spapr_tce_find_by_liobn(liobn);
230     CPUState *cs = CPU(cpu);
231 
232     if (!tcet) {
233         return H_PARAMETER;
234     }
235 
236     if (npages > 512) {
237         return H_PARAMETER;
238     }
239 
240     ioba &= ~SPAPR_TCE_PAGE_MASK;
241     tce_list &= ~SPAPR_TCE_PAGE_MASK;
242 
243     for (i = 0; i < npages; ++i, ioba += SPAPR_TCE_PAGE_SIZE) {
244         target_ulong tce = ldq_phys(cs->as, tce_list +
245                                     i * sizeof(target_ulong));
246         ret = put_tce_emu(tcet, ioba, tce);
247         if (ret) {
248             break;
249         }
250     }
251 
252     /* Trace last successful or the first problematic entry */
253     i = i ? (i - 1) : 0;
254     trace_spapr_iommu_indirect(liobn, ioba1, tce_list, i,
255                                ldq_phys(cs->as,
256                                tce_list + i * sizeof(target_ulong)),
257                                ret);
258 
259     return ret;
260 }
261 
262 static target_ulong h_stuff_tce(PowerPCCPU *cpu, sPAPREnvironment *spapr,
263                               target_ulong opcode, target_ulong *args)
264 {
265     int i;
266     target_ulong liobn = args[0];
267     target_ulong ioba = args[1];
268     target_ulong tce_value = args[2];
269     target_ulong npages = args[3];
270     target_ulong ret = H_PARAMETER;
271     sPAPRTCETable *tcet = spapr_tce_find_by_liobn(liobn);
272 
273     if (!tcet) {
274         return H_PARAMETER;
275     }
276 
277     if (npages > tcet->nb_table) {
278         return H_PARAMETER;
279     }
280 
281     ioba &= ~SPAPR_TCE_PAGE_MASK;
282 
283     for (i = 0; i < npages; ++i, ioba += SPAPR_TCE_PAGE_SIZE) {
284         ret = put_tce_emu(tcet, ioba, tce_value);
285         if (ret) {
286             break;
287         }
288     }
289     trace_spapr_iommu_stuff(liobn, ioba, tce_value, npages, ret);
290 
291     return ret;
292 }
293 
294 static target_ulong h_put_tce(PowerPCCPU *cpu, sPAPREnvironment *spapr,
295                               target_ulong opcode, target_ulong *args)
296 {
297     target_ulong liobn = args[0];
298     target_ulong ioba = args[1];
299     target_ulong tce = args[2];
300     target_ulong ret = H_PARAMETER;
301     sPAPRTCETable *tcet = spapr_tce_find_by_liobn(liobn);
302 
303     ioba &= ~(SPAPR_TCE_PAGE_SIZE - 1);
304 
305     if (tcet) {
306         ret = put_tce_emu(tcet, ioba, tce);
307     }
308     trace_spapr_iommu_put(liobn, ioba, tce, ret);
309 
310     return ret;
311 }
312 
313 static target_ulong get_tce_emu(sPAPRTCETable *tcet, target_ulong ioba,
314                                 target_ulong *tce)
315 {
316     if ((ioba >> SPAPR_TCE_PAGE_SHIFT) >= tcet->nb_table) {
317         hcall_dprintf("spapr_iommu_get_tce on out-of-bounds IOBA 0x"
318                       TARGET_FMT_lx "\n", ioba);
319         return H_PARAMETER;
320     }
321 
322     *tce = tcet->table[ioba >> SPAPR_TCE_PAGE_SHIFT];
323 
324     return H_SUCCESS;
325 }
326 
327 static target_ulong h_get_tce(PowerPCCPU *cpu, sPAPREnvironment *spapr,
328                               target_ulong opcode, target_ulong *args)
329 {
330     target_ulong liobn = args[0];
331     target_ulong ioba = args[1];
332     target_ulong tce = 0;
333     target_ulong ret = H_PARAMETER;
334     sPAPRTCETable *tcet = spapr_tce_find_by_liobn(liobn);
335 
336     ioba &= ~(SPAPR_TCE_PAGE_SIZE - 1);
337 
338     if (tcet) {
339         ret = get_tce_emu(tcet, ioba, &tce);
340         if (!ret) {
341             args[0] = tce;
342         }
343     }
344     trace_spapr_iommu_get(liobn, ioba, ret, tce);
345 
346     return ret;
347 }
348 
349 int spapr_dma_dt(void *fdt, int node_off, const char *propname,
350                  uint32_t liobn, uint64_t window, uint32_t size)
351 {
352     uint32_t dma_prop[5];
353     int ret;
354 
355     dma_prop[0] = cpu_to_be32(liobn);
356     dma_prop[1] = cpu_to_be32(window >> 32);
357     dma_prop[2] = cpu_to_be32(window & 0xFFFFFFFF);
358     dma_prop[3] = 0; /* window size is 32 bits */
359     dma_prop[4] = cpu_to_be32(size);
360 
361     ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-address-cells", 2);
362     if (ret < 0) {
363         return ret;
364     }
365 
366     ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-size-cells", 2);
367     if (ret < 0) {
368         return ret;
369     }
370 
371     ret = fdt_setprop(fdt, node_off, propname, dma_prop, sizeof(dma_prop));
372     if (ret < 0) {
373         return ret;
374     }
375 
376     return 0;
377 }
378 
379 int spapr_tcet_dma_dt(void *fdt, int node_off, const char *propname,
380                       sPAPRTCETable *tcet)
381 {
382     if (!tcet) {
383         return 0;
384     }
385 
386     return spapr_dma_dt(fdt, node_off, propname,
387                         tcet->liobn, 0, tcet->nb_table << SPAPR_TCE_PAGE_SHIFT);
388 }
389 
390 static void spapr_tce_table_class_init(ObjectClass *klass, void *data)
391 {
392     DeviceClass *dc = DEVICE_CLASS(klass);
393     dc->init = spapr_tce_table_realize;
394     dc->reset = spapr_tce_reset;
395 
396     QLIST_INIT(&spapr_tce_tables);
397 
398     /* hcall-tce */
399     spapr_register_hypercall(H_PUT_TCE, h_put_tce);
400     spapr_register_hypercall(H_GET_TCE, h_get_tce);
401     spapr_register_hypercall(H_PUT_TCE_INDIRECT, h_put_tce_indirect);
402     spapr_register_hypercall(H_STUFF_TCE, h_stuff_tce);
403 }
404 
405 static TypeInfo spapr_tce_table_info = {
406     .name = TYPE_SPAPR_TCE_TABLE,
407     .parent = TYPE_DEVICE,
408     .instance_size = sizeof(sPAPRTCETable),
409     .class_init = spapr_tce_table_class_init,
410     .instance_finalize = spapr_tce_table_finalize,
411 };
412 
413 static void register_types(void)
414 {
415     type_register_static(&spapr_tce_table_info);
416 }
417 
418 type_init(register_types);
419