]> git.proxmox.com Git - mirror_qemu.git/blame - hw/ppc/spapr_iommu.c
spapr: Enable dynamic change of the supported hypercalls list
[mirror_qemu.git] / hw / ppc / spapr_iommu.c
CommitLineData
ad0ebb91
DG
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 */
83c9f4ca 19#include "hw/hw.h"
9c17d615 20#include "sysemu/kvm.h"
83c9f4ca 21#include "hw/qdev.h"
ad0ebb91 22#include "kvm_ppc.h"
9c17d615 23#include "sysemu/dma.h"
022c62cb 24#include "exec/address-spaces.h"
7e472264 25#include "trace.h"
ad0ebb91 26
0d09e41a 27#include "hw/ppc/spapr.h"
ad0ebb91
DG
28
29#include <libfdt.h>
30
ad0ebb91
DG
31enum sPAPRTCEAccess {
32 SPAPR_TCE_FAULT = 0,
33 SPAPR_TCE_RO = 1,
34 SPAPR_TCE_WO = 2,
35 SPAPR_TCE_RW = 3,
36};
37
6a0a70b0 38static QLIST_HEAD(spapr_tce_tables, sPAPRTCETable) spapr_tce_tables;
ad0ebb91
DG
39
40static sPAPRTCETable *spapr_tce_find_by_liobn(uint32_t liobn)
41{
42 sPAPRTCETable *tcet;
43
d4261662
DG
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
ad0ebb91
DG
50 QLIST_FOREACH(tcet, &spapr_tce_tables, list) {
51 if (tcet->liobn == liobn) {
52 return tcet;
53 }
54 }
55
56 return NULL;
57}
58
a84bb436 59static IOMMUTLBEntry spapr_tce_translate_iommu(MemoryRegion *iommu, hwaddr addr)
ad0ebb91 60{
a84bb436 61 sPAPRTCETable *tcet = container_of(iommu, sPAPRTCETable, iommu);
ad0ebb91 62 uint64_t tce;
7e472264
AK
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 };
ad0ebb91 70
53724ee5 71 if (tcet->bypass) {
7e472264
AK
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;
ad0ebb91 80 }
7e472264
AK
81 trace_spapr_iommu_xlate(tcet->liobn, addr, ret.iova, ret.perm,
82 ret.addr_mask);
ad0ebb91 83
7e472264 84 return ret;
a71bfbfe
PB
85}
86
a83000f5
AL
87static 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
96static const VMStateDescription vmstate_spapr_tce_table = {
97 .name = "spapr_iommu",
98 .version_id = 1,
99 .minimum_version_id = 1,
a83000f5 100 .pre_load = spapr_tce_table_pre_load,
3aff6c2f 101 .fields = (VMStateField[]) {
a83000f5
AL
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
a84bb436
PB
114static MemoryRegionIOMMUOps spapr_iommu_ops = {
115 .translate = spapr_tce_translate_iommu,
116};
ad0ebb91 117
a83000f5 118static int spapr_tce_table_realize(DeviceState *dev)
ad0ebb91 119{
a83000f5 120 sPAPRTCETable *tcet = SPAPR_TCE_TABLE(dev);
ad0ebb91
DG
121
122 if (kvm_enabled()) {
a83000f5
AL
123 tcet->table = kvmppc_create_spapr_tce(tcet->liobn,
124 tcet->window_size,
ad0ebb91
DG
125 &tcet->fd);
126 }
127
128 if (!tcet->table) {
a83000f5
AL
129 size_t table_size = (tcet->window_size >> SPAPR_TCE_PAGE_SHIFT)
130 * sizeof(uint64_t);
ad0ebb91
DG
131 tcet->table = g_malloc0(table_size);
132 }
a83000f5 133 tcet->nb_table = tcet->window_size >> SPAPR_TCE_PAGE_SHIFT;
ad0ebb91 134
7e472264 135 trace_spapr_iommu_new_table(tcet->liobn, tcet, tcet->table, tcet->fd);
ad0ebb91 136
a83000f5 137 memory_region_init_iommu(&tcet->iommu, OBJECT(dev), &spapr_iommu_ops,
a84bb436 138 "iommu-spapr", UINT64_MAX);
a84bb436 139
ad0ebb91
DG
140 QLIST_INSERT_HEAD(&spapr_tce_tables, tcet, list);
141
00d4f525
AK
142 vmstate_register(DEVICE(tcet), tcet->liobn, &vmstate_spapr_tce_table,
143 tcet);
144
a83000f5
AL
145 return 0;
146}
147
148sPAPRTCETable *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 qdev_init_nofail(DEVICE(tcet));
169
2b7dc949 170 return tcet;
ad0ebb91
DG
171}
172
a83000f5 173static void spapr_tce_table_finalize(Object *obj)
ad0ebb91 174{
a83000f5
AL
175 sPAPRTCETable *tcet = SPAPR_TCE_TABLE(obj);
176
2b7dc949 177 QLIST_REMOVE(tcet, list);
ad0ebb91 178
2b7dc949
PB
179 if (!kvm_enabled() ||
180 (kvmppc_remove_spapr_tce(tcet->table, tcet->fd,
181 tcet->window_size) != 0)) {
182 g_free(tcet->table);
ad0ebb91
DG
183 }
184}
185
a84bb436
PB
186MemoryRegion *spapr_tce_get_iommu(sPAPRTCETable *tcet)
187{
188 return &tcet->iommu;
189}
190
2b7dc949
PB
191void spapr_tce_set_bypass(sPAPRTCETable *tcet, bool bypass)
192{
53724ee5
DG
193 tcet->bypass = bypass;
194}
195
a83000f5 196static void spapr_tce_reset(DeviceState *dev)
eddeed26 197{
a83000f5 198 sPAPRTCETable *tcet = SPAPR_TCE_TABLE(dev);
53724ee5 199 size_t table_size = (tcet->window_size >> SPAPR_TCE_PAGE_SHIFT)
a83000f5 200 * sizeof(uint64_t);
eddeed26 201
53724ee5
DG
202 tcet->bypass = false;
203 memset(tcet->table, 0, table_size);
eddeed26
DG
204}
205
edded454
DG
206static target_ulong put_tce_emu(sPAPRTCETable *tcet, target_ulong ioba,
207 target_ulong tce)
208{
a84bb436 209 IOMMUTLBEntry entry;
edded454
DG
210
211 if (ioba >= tcet->window_size) {
b55519a0 212 hcall_dprintf("spapr_vio_put_tce on out-of-bounds IOBA 0x"
edded454
DG
213 TARGET_FMT_lx "\n", ioba);
214 return H_PARAMETER;
215 }
216
a83000f5 217 tcet->table[ioba >> SPAPR_TCE_PAGE_SHIFT] = tce;
edded454 218
a84bb436
PB
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
edded454
DG
226 return H_SUCCESS;
227}
ad0ebb91 228
b13ce26d 229static target_ulong h_put_tce(PowerPCCPU *cpu, sPAPREnvironment *spapr,
ad0ebb91
DG
230 target_ulong opcode, target_ulong *args)
231{
232 target_ulong liobn = args[0];
233 target_ulong ioba = args[1];
234 target_ulong tce = args[2];
7e472264 235 target_ulong ret = H_PARAMETER;
ad0ebb91 236 sPAPRTCETable *tcet = spapr_tce_find_by_liobn(liobn);
ad0ebb91 237
ad0ebb91
DG
238 ioba &= ~(SPAPR_TCE_PAGE_SIZE - 1);
239
edded454 240 if (tcet) {
7e472264 241 ret = put_tce_emu(tcet, ioba, tce);
edded454 242 }
7e472264 243 trace_spapr_iommu_put(liobn, ioba, tce, ret);
ad0ebb91 244
7e472264 245 return ret;
ad0ebb91
DG
246}
247
a0fcac9c
LD
248static target_ulong get_tce_emu(sPAPRTCETable *tcet, target_ulong ioba,
249 target_ulong *tce)
250{
251 if (ioba >= tcet->window_size) {
252 hcall_dprintf("spapr_iommu_get_tce on out-of-bounds IOBA 0x"
253 TARGET_FMT_lx "\n", ioba);
254 return H_PARAMETER;
255 }
256
257 *tce = tcet->table[ioba >> SPAPR_TCE_PAGE_SHIFT];
258
259 return H_SUCCESS;
260}
261
262static target_ulong h_get_tce(PowerPCCPU *cpu, sPAPREnvironment *spapr,
263 target_ulong opcode, target_ulong *args)
264{
265 target_ulong liobn = args[0];
266 target_ulong ioba = args[1];
267 target_ulong tce = 0;
268 target_ulong ret = H_PARAMETER;
269 sPAPRTCETable *tcet = spapr_tce_find_by_liobn(liobn);
270
271 ioba &= ~(SPAPR_TCE_PAGE_SIZE - 1);
272
273 if (tcet) {
274 ret = get_tce_emu(tcet, ioba, &tce);
275 if (!ret) {
276 args[0] = tce;
277 }
278 }
279 trace_spapr_iommu_get(liobn, ioba, ret, tce);
280
281 return ret;
282}
283
ad0ebb91 284int spapr_dma_dt(void *fdt, int node_off, const char *propname,
5c4cbcf2 285 uint32_t liobn, uint64_t window, uint32_t size)
ad0ebb91 286{
5c4cbcf2
AK
287 uint32_t dma_prop[5];
288 int ret;
289
290 dma_prop[0] = cpu_to_be32(liobn);
291 dma_prop[1] = cpu_to_be32(window >> 32);
292 dma_prop[2] = cpu_to_be32(window & 0xFFFFFFFF);
293 dma_prop[3] = 0; /* window size is 32 bits */
294 dma_prop[4] = cpu_to_be32(size);
295
296 ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-address-cells", 2);
297 if (ret < 0) {
298 return ret;
299 }
ad0ebb91 300
5c4cbcf2
AK
301 ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-size-cells", 2);
302 if (ret < 0) {
303 return ret;
304 }
ad0ebb91 305
5c4cbcf2
AK
306 ret = fdt_setprop(fdt, node_off, propname, dma_prop, sizeof(dma_prop));
307 if (ret < 0) {
308 return ret;
ad0ebb91
DG
309 }
310
311 return 0;
312}
5c4cbcf2
AK
313
314int spapr_tcet_dma_dt(void *fdt, int node_off, const char *propname,
2b7dc949 315 sPAPRTCETable *tcet)
5c4cbcf2 316{
2b7dc949 317 if (!tcet) {
5c4cbcf2
AK
318 return 0;
319 }
320
2b7dc949
PB
321 return spapr_dma_dt(fdt, node_off, propname,
322 tcet->liobn, 0, tcet->window_size);
5c4cbcf2 323}
a83000f5
AL
324
325static void spapr_tce_table_class_init(ObjectClass *klass, void *data)
326{
327 DeviceClass *dc = DEVICE_CLASS(klass);
a83000f5
AL
328 dc->init = spapr_tce_table_realize;
329 dc->reset = spapr_tce_reset;
330
331 QLIST_INIT(&spapr_tce_tables);
332
333 /* hcall-tce */
334 spapr_register_hypercall(H_PUT_TCE, h_put_tce);
a0fcac9c 335 spapr_register_hypercall(H_GET_TCE, h_get_tce);
a83000f5
AL
336}
337
338static TypeInfo spapr_tce_table_info = {
339 .name = TYPE_SPAPR_TCE_TABLE,
340 .parent = TYPE_DEVICE,
341 .instance_size = sizeof(sPAPRTCETable),
342 .class_init = spapr_tce_table_class_init,
343 .instance_finalize = spapr_tce_table_finalize,
344};
345
346static void register_types(void)
347{
348 type_register_static(&spapr_tce_table_info);
349}
350
351type_init(register_types);