2 * QEMU sPAPR IOMMU (TCE) code
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.
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.
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/>.
20 #include "sysemu/kvm.h"
23 #include "sysemu/dma.h"
24 #include "exec/address-spaces.h"
27 #include "hw/ppc/spapr.h"
38 #define IOMMU_PAGE_SIZE(shift) (1ULL << (shift))
39 #define IOMMU_PAGE_MASK(shift) (~(IOMMU_PAGE_SIZE(shift) - 1))
41 static QLIST_HEAD(spapr_tce_tables, sPAPRTCETable) spapr_tce_tables;
43 static sPAPRTCETable *spapr_tce_find_by_liobn(uint32_t liobn)
47 if (liobn & 0xFFFFFFFF00000000ULL) {
48 hcall_dprintf("Request for out-of-bounds LIOBN 0x" TARGET_FMT_lx "\n",
53 QLIST_FOREACH(tcet, &spapr_tce_tables, list) {
54 if (tcet->liobn == liobn) {
62 static IOMMUTLBEntry spapr_tce_translate_iommu(MemoryRegion *iommu, hwaddr addr)
64 sPAPRTCETable *tcet = container_of(iommu, sPAPRTCETable, iommu);
67 .target_as = &address_space_memory,
70 .addr_mask = ~(hwaddr)0,
76 } else if ((addr >> tcet->page_shift) < tcet->nb_table) {
77 /* Check if we are in bound */
78 hwaddr page_mask = IOMMU_PAGE_MASK(tcet->page_shift);
80 tce = tcet->table[addr >> tcet->page_shift];
81 ret.iova = addr & page_mask;
82 ret.translated_addr = tce & page_mask;
83 ret.addr_mask = ~page_mask;
84 ret.perm = tce & IOMMU_RW;
86 trace_spapr_iommu_xlate(tcet->liobn, addr, ret.iova, ret.perm,
92 static const VMStateDescription vmstate_spapr_tce_table = {
93 .name = "spapr_iommu",
95 .minimum_version_id = 2,
96 .fields = (VMStateField []) {
98 VMSTATE_UINT32_EQUAL(liobn, sPAPRTCETable),
99 VMSTATE_UINT32_EQUAL(nb_table, sPAPRTCETable),
102 VMSTATE_BOOL(bypass, sPAPRTCETable),
103 VMSTATE_VARRAY_UINT32(table, sPAPRTCETable, nb_table, 0, vmstate_info_uint64, uint64_t),
105 VMSTATE_END_OF_LIST()
109 static MemoryRegionIOMMUOps spapr_iommu_ops = {
110 .translate = spapr_tce_translate_iommu,
113 static int spapr_tce_table_realize(DeviceState *dev)
115 sPAPRTCETable *tcet = SPAPR_TCE_TABLE(dev);
118 tcet->table = kvmppc_create_spapr_tce(tcet->liobn,
126 size_t table_size = tcet->nb_table * sizeof(uint64_t);
127 tcet->table = g_malloc0(table_size);
130 trace_spapr_iommu_new_table(tcet->liobn, tcet, tcet->table, tcet->fd);
132 memory_region_init_iommu(&tcet->iommu, OBJECT(dev), &spapr_iommu_ops,
133 "iommu-spapr", ram_size);
135 QLIST_INSERT_HEAD(&spapr_tce_tables, tcet, list);
137 vmstate_register(DEVICE(tcet), tcet->liobn, &vmstate_spapr_tce_table,
143 sPAPRTCETable *spapr_tce_new_table(DeviceState *owner, uint32_t liobn,
151 if (spapr_tce_find_by_liobn(liobn)) {
152 fprintf(stderr, "Attempted to create TCE table with duplicate"
153 " LIOBN 0x%x\n", liobn);
161 tcet = SPAPR_TCE_TABLE(object_new(TYPE_SPAPR_TCE_TABLE));
163 tcet->bus_offset = bus_offset;
164 tcet->page_shift = page_shift;
165 tcet->nb_table = nb_table;
166 tcet->vfio_accel = vfio_accel;
168 object_property_add_child(OBJECT(owner), "tce-table", OBJECT(tcet), NULL);
170 object_property_set_bool(OBJECT(tcet), true, "realized", NULL);
175 static void spapr_tce_table_finalize(Object *obj)
177 sPAPRTCETable *tcet = SPAPR_TCE_TABLE(obj);
179 QLIST_REMOVE(tcet, list);
181 if (!kvm_enabled() ||
182 (kvmppc_remove_spapr_tce(tcet->table, tcet->fd,
183 tcet->nb_table) != 0)) {
188 MemoryRegion *spapr_tce_get_iommu(sPAPRTCETable *tcet)
193 void spapr_tce_set_bypass(sPAPRTCETable *tcet, bool bypass)
195 tcet->bypass = bypass;
198 static void spapr_tce_reset(DeviceState *dev)
200 sPAPRTCETable *tcet = SPAPR_TCE_TABLE(dev);
201 size_t table_size = tcet->nb_table * sizeof(uint64_t);
203 tcet->bypass = false;
204 memset(tcet->table, 0, table_size);
207 static target_ulong put_tce_emu(sPAPRTCETable *tcet, target_ulong ioba,
211 hwaddr page_mask = IOMMU_PAGE_MASK(tcet->page_shift);
212 unsigned long index = (ioba - tcet->bus_offset) >> tcet->page_shift;
214 if (index >= tcet->nb_table) {
215 hcall_dprintf("spapr_vio_put_tce on out-of-bounds IOBA 0x"
216 TARGET_FMT_lx "\n", ioba);
220 tcet->table[index] = tce;
222 entry.target_as = &address_space_memory,
223 entry.iova = ioba & page_mask;
224 entry.translated_addr = tce & page_mask;
225 entry.addr_mask = ~page_mask;
226 entry.perm = tce & IOMMU_RW;
227 memory_region_notify_iommu(&tcet->iommu, entry);
232 static target_ulong h_put_tce_indirect(PowerPCCPU *cpu,
233 sPAPREnvironment *spapr,
234 target_ulong opcode, target_ulong *args)
237 target_ulong liobn = args[0];
238 target_ulong ioba = args[1];
239 target_ulong ioba1 = ioba;
240 target_ulong tce_list = args[2];
241 target_ulong npages = args[3];
242 target_ulong ret = H_PARAMETER;
243 sPAPRTCETable *tcet = spapr_tce_find_by_liobn(liobn);
244 CPUState *cs = CPU(cpu);
245 hwaddr page_mask, page_size;
251 if ((npages > 512) || (tce_list & SPAPR_TCE_PAGE_MASK)) {
255 page_mask = IOMMU_PAGE_MASK(tcet->page_shift);
256 page_size = IOMMU_PAGE_SIZE(tcet->page_shift);
259 for (i = 0; i < npages; ++i, ioba += page_size) {
260 target_ulong off = (tce_list & ~SPAPR_TCE_RW) +
261 i * sizeof(target_ulong);
262 target_ulong tce = ldq_phys(cs->as, off);
264 ret = put_tce_emu(tcet, ioba, tce);
270 /* Trace last successful or the first problematic entry */
272 trace_spapr_iommu_indirect(liobn, ioba1, tce_list, i,
274 tce_list + i * sizeof(target_ulong)),
280 static target_ulong h_stuff_tce(PowerPCCPU *cpu, sPAPREnvironment *spapr,
281 target_ulong opcode, target_ulong *args)
284 target_ulong liobn = args[0];
285 target_ulong ioba = args[1];
286 target_ulong tce_value = args[2];
287 target_ulong npages = args[3];
288 target_ulong ret = H_PARAMETER;
289 sPAPRTCETable *tcet = spapr_tce_find_by_liobn(liobn);
290 hwaddr page_mask, page_size;
296 if (npages > tcet->nb_table) {
300 page_mask = IOMMU_PAGE_MASK(tcet->page_shift);
301 page_size = IOMMU_PAGE_SIZE(tcet->page_shift);
304 for (i = 0; i < npages; ++i, ioba += page_size) {
305 ret = put_tce_emu(tcet, ioba, tce_value);
310 trace_spapr_iommu_stuff(liobn, ioba, tce_value, npages, ret);
315 static target_ulong h_put_tce(PowerPCCPU *cpu, sPAPREnvironment *spapr,
316 target_ulong opcode, target_ulong *args)
318 target_ulong liobn = args[0];
319 target_ulong ioba = args[1];
320 target_ulong tce = args[2];
321 target_ulong ret = H_PARAMETER;
322 sPAPRTCETable *tcet = spapr_tce_find_by_liobn(liobn);
325 hwaddr page_mask = IOMMU_PAGE_MASK(tcet->page_shift);
329 ret = put_tce_emu(tcet, ioba, tce);
331 trace_spapr_iommu_put(liobn, ioba, tce, ret);
336 static target_ulong get_tce_emu(sPAPRTCETable *tcet, target_ulong ioba,
339 unsigned long index = (ioba - tcet->bus_offset) >> tcet->page_shift;
341 if (index >= tcet->nb_table) {
342 hcall_dprintf("spapr_iommu_get_tce on out-of-bounds IOBA 0x"
343 TARGET_FMT_lx "\n", ioba);
347 *tce = tcet->table[index];
352 static target_ulong h_get_tce(PowerPCCPU *cpu, sPAPREnvironment *spapr,
353 target_ulong opcode, target_ulong *args)
355 target_ulong liobn = args[0];
356 target_ulong ioba = args[1];
357 target_ulong tce = 0;
358 target_ulong ret = H_PARAMETER;
359 sPAPRTCETable *tcet = spapr_tce_find_by_liobn(liobn);
362 hwaddr page_mask = IOMMU_PAGE_MASK(tcet->page_shift);
366 ret = get_tce_emu(tcet, ioba, &tce);
371 trace_spapr_iommu_get(liobn, ioba, ret, tce);
376 int spapr_dma_dt(void *fdt, int node_off, const char *propname,
377 uint32_t liobn, uint64_t window, uint32_t size)
379 uint32_t dma_prop[5];
382 dma_prop[0] = cpu_to_be32(liobn);
383 dma_prop[1] = cpu_to_be32(window >> 32);
384 dma_prop[2] = cpu_to_be32(window & 0xFFFFFFFF);
385 dma_prop[3] = 0; /* window size is 32 bits */
386 dma_prop[4] = cpu_to_be32(size);
388 ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-address-cells", 2);
393 ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-size-cells", 2);
398 ret = fdt_setprop(fdt, node_off, propname, dma_prop, sizeof(dma_prop));
406 int spapr_tcet_dma_dt(void *fdt, int node_off, const char *propname,
413 return spapr_dma_dt(fdt, node_off, propname,
414 tcet->liobn, 0, tcet->nb_table << tcet->page_shift);
417 static void spapr_tce_table_class_init(ObjectClass *klass, void *data)
419 DeviceClass *dc = DEVICE_CLASS(klass);
420 dc->init = spapr_tce_table_realize;
421 dc->reset = spapr_tce_reset;
423 QLIST_INIT(&spapr_tce_tables);
426 spapr_register_hypercall(H_PUT_TCE, h_put_tce);
427 spapr_register_hypercall(H_GET_TCE, h_get_tce);
428 spapr_register_hypercall(H_PUT_TCE_INDIRECT, h_put_tce_indirect);
429 spapr_register_hypercall(H_STUFF_TCE, h_stuff_tce);
432 static TypeInfo spapr_tce_table_info = {
433 .name = TYPE_SPAPR_TCE_TABLE,
434 .parent = TYPE_DEVICE,
435 .instance_size = sizeof(sPAPRTCETable),
436 .class_init = spapr_tce_table_class_init,
437 .instance_finalize = spapr_tce_table_finalize,
440 static void register_types(void)
442 type_register_static(&spapr_tce_table_info);
445 type_init(register_types);