]> git.proxmox.com Git - mirror_qemu.git/blame - hw/ppc/spapr_iommu.c
pseries: savevm support for PAPR VIO logical tty
[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"
ad0ebb91 25
0d09e41a 26#include "hw/ppc/spapr.h"
ad0ebb91
DG
27
28#include <libfdt.h>
29
30/* #define DEBUG_TCE */
31
32enum sPAPRTCEAccess {
33 SPAPR_TCE_FAULT = 0,
34 SPAPR_TCE_RO = 1,
35 SPAPR_TCE_WO = 2,
36 SPAPR_TCE_RW = 3,
37};
38
ad0ebb91 39struct sPAPRTCETable {
ad0ebb91
DG
40 uint32_t liobn;
41 uint32_t window_size;
42 sPAPRTCE *table;
53724ee5 43 bool bypass;
ad0ebb91 44 int fd;
a84bb436 45 MemoryRegion iommu;
ad0ebb91
DG
46 QLIST_ENTRY(sPAPRTCETable) list;
47};
48
49
50QLIST_HEAD(spapr_tce_tables, sPAPRTCETable) spapr_tce_tables;
51
52static sPAPRTCETable *spapr_tce_find_by_liobn(uint32_t liobn)
53{
54 sPAPRTCETable *tcet;
55
d4261662
DG
56 if (liobn & 0xFFFFFFFF00000000ULL) {
57 hcall_dprintf("Request for out-of-bounds LIOBN 0x" TARGET_FMT_lx "\n",
58 liobn);
59 return NULL;
60 }
61
ad0ebb91
DG
62 QLIST_FOREACH(tcet, &spapr_tce_tables, list) {
63 if (tcet->liobn == liobn) {
64 return tcet;
65 }
66 }
67
68 return NULL;
69}
70
a84bb436 71static IOMMUTLBEntry spapr_tce_translate_iommu(MemoryRegion *iommu, hwaddr addr)
ad0ebb91 72{
a84bb436 73 sPAPRTCETable *tcet = container_of(iommu, sPAPRTCETable, iommu);
ad0ebb91
DG
74 uint64_t tce;
75
76#ifdef DEBUG_TCE
77 fprintf(stderr, "spapr_tce_translate liobn=0x%" PRIx32 " addr=0x"
78 DMA_ADDR_FMT "\n", tcet->liobn, addr);
79#endif
80
53724ee5 81 if (tcet->bypass) {
a71bfbfe
PB
82 return (IOMMUTLBEntry) {
83 .target_as = &address_space_memory,
84 .iova = 0,
85 .translated_addr = 0,
86 .addr_mask = ~(hwaddr)0,
87 .perm = IOMMU_RW,
88 };
53724ee5
DG
89 }
90
ad0ebb91
DG
91 /* Check if we are in bound */
92 if (addr >= tcet->window_size) {
93#ifdef DEBUG_TCE
94 fprintf(stderr, "spapr_tce_translate out of bounds\n");
95#endif
a71bfbfe 96 return (IOMMUTLBEntry) { .perm = IOMMU_NONE };
ad0ebb91
DG
97 }
98
99 tce = tcet->table[addr >> SPAPR_TCE_PAGE_SHIFT].tce;
100
a71bfbfe
PB
101#ifdef DEBUG_TCE
102 fprintf(stderr, " -> *paddr=0x%llx, *len=0x%llx\n",
103 (tce & ~SPAPR_TCE_PAGE_MASK), SPAPR_TCE_PAGE_MASK + 1);
104#endif
105
106 return (IOMMUTLBEntry) {
107 .target_as = &address_space_memory,
108 .iova = addr & ~SPAPR_TCE_PAGE_MASK,
109 .translated_addr = tce & ~SPAPR_TCE_PAGE_MASK,
110 .addr_mask = SPAPR_TCE_PAGE_MASK,
111 .perm = tce,
112 };
113}
114
a84bb436
PB
115static MemoryRegionIOMMUOps spapr_iommu_ops = {
116 .translate = spapr_tce_translate_iommu,
117};
ad0ebb91 118
84af6d9f 119sPAPRTCETable *spapr_tce_new_table(DeviceState *owner, uint32_t liobn, size_t window_size)
ad0ebb91
DG
120{
121 sPAPRTCETable *tcet;
122
8b1853e7
DG
123 if (spapr_tce_find_by_liobn(liobn)) {
124 fprintf(stderr, "Attempted to create TCE table with duplicate"
125 " LIOBN 0x%x\n", liobn);
126 return NULL;
127 }
128
ad0ebb91
DG
129 if (!window_size) {
130 return NULL;
131 }
132
133 tcet = g_malloc0(sizeof(*tcet));
ad0ebb91
DG
134 tcet->liobn = liobn;
135 tcet->window_size = window_size;
136
137 if (kvm_enabled()) {
138 tcet->table = kvmppc_create_spapr_tce(liobn,
139 window_size,
140 &tcet->fd);
141 }
142
143 if (!tcet->table) {
144 size_t table_size = (window_size >> SPAPR_TCE_PAGE_SHIFT)
145 * sizeof(sPAPRTCE);
146 tcet->table = g_malloc0(table_size);
147 }
148
149#ifdef DEBUG_TCE
2b7dc949
PB
150 fprintf(stderr, "spapr_iommu: New TCE table @ %p, liobn=0x%x, "
151 "table @ %p, fd=%d\n", tcet, liobn, tcet->table, tcet->fd);
ad0ebb91
DG
152#endif
153
84af6d9f 154 memory_region_init_iommu(&tcet->iommu, OBJECT(owner), &spapr_iommu_ops,
a84bb436 155 "iommu-spapr", UINT64_MAX);
a84bb436 156
ad0ebb91
DG
157 QLIST_INSERT_HEAD(&spapr_tce_tables, tcet, list);
158
2b7dc949 159 return tcet;
ad0ebb91
DG
160}
161
2b7dc949 162void spapr_tce_free(sPAPRTCETable *tcet)
ad0ebb91 163{
2b7dc949 164 QLIST_REMOVE(tcet, list);
ad0ebb91 165
2b7dc949
PB
166 if (!kvm_enabled() ||
167 (kvmppc_remove_spapr_tce(tcet->table, tcet->fd,
168 tcet->window_size) != 0)) {
169 g_free(tcet->table);
ad0ebb91 170 }
2b7dc949
PB
171
172 g_free(tcet);
ad0ebb91
DG
173}
174
a84bb436
PB
175MemoryRegion *spapr_tce_get_iommu(sPAPRTCETable *tcet)
176{
177 return &tcet->iommu;
178}
179
2b7dc949
PB
180void spapr_tce_set_bypass(sPAPRTCETable *tcet, bool bypass)
181{
53724ee5
DG
182 tcet->bypass = bypass;
183}
184
2b7dc949 185void spapr_tce_reset(sPAPRTCETable *tcet)
eddeed26 186{
53724ee5
DG
187 size_t table_size = (tcet->window_size >> SPAPR_TCE_PAGE_SHIFT)
188 * sizeof(sPAPRTCE);
eddeed26 189
53724ee5
DG
190 tcet->bypass = false;
191 memset(tcet->table, 0, table_size);
eddeed26
DG
192}
193
edded454
DG
194static target_ulong put_tce_emu(sPAPRTCETable *tcet, target_ulong ioba,
195 target_ulong tce)
196{
197 sPAPRTCE *tcep;
a84bb436 198 IOMMUTLBEntry entry;
edded454
DG
199
200 if (ioba >= tcet->window_size) {
b55519a0 201 hcall_dprintf("spapr_vio_put_tce on out-of-bounds IOBA 0x"
edded454
DG
202 TARGET_FMT_lx "\n", ioba);
203 return H_PARAMETER;
204 }
205
206 tcep = tcet->table + (ioba >> SPAPR_TCE_PAGE_SHIFT);
207 tcep->tce = tce;
208
a84bb436
PB
209 entry.target_as = &address_space_memory,
210 entry.iova = ioba & ~SPAPR_TCE_PAGE_MASK;
211 entry.translated_addr = tce & ~SPAPR_TCE_PAGE_MASK;
212 entry.addr_mask = SPAPR_TCE_PAGE_MASK;
213 entry.perm = tce;
214 memory_region_notify_iommu(&tcet->iommu, entry);
215
edded454
DG
216 return H_SUCCESS;
217}
ad0ebb91 218
b13ce26d 219static target_ulong h_put_tce(PowerPCCPU *cpu, sPAPREnvironment *spapr,
ad0ebb91
DG
220 target_ulong opcode, target_ulong *args)
221{
222 target_ulong liobn = args[0];
223 target_ulong ioba = args[1];
224 target_ulong tce = args[2];
225 sPAPRTCETable *tcet = spapr_tce_find_by_liobn(liobn);
ad0ebb91 226
ad0ebb91
DG
227 ioba &= ~(SPAPR_TCE_PAGE_SIZE - 1);
228
edded454
DG
229 if (tcet) {
230 return put_tce_emu(tcet, ioba, tce);
231 }
ad0ebb91 232#ifdef DEBUG_TCE
edded454 233 fprintf(stderr, "%s on liobn=" TARGET_FMT_lx /*%s*/
ad0ebb91 234 " ioba 0x" TARGET_FMT_lx " TCE 0x" TARGET_FMT_lx "\n",
edded454 235 __func__, liobn, /*dev->qdev.id, */ioba, tce);
ad0ebb91
DG
236#endif
237
edded454 238 return H_PARAMETER;
ad0ebb91
DG
239}
240
241void spapr_iommu_init(void)
242{
243 QLIST_INIT(&spapr_tce_tables);
244
245 /* hcall-tce */
246 spapr_register_hypercall(H_PUT_TCE, h_put_tce);
247}
248
249int spapr_dma_dt(void *fdt, int node_off, const char *propname,
5c4cbcf2 250 uint32_t liobn, uint64_t window, uint32_t size)
ad0ebb91 251{
5c4cbcf2
AK
252 uint32_t dma_prop[5];
253 int ret;
254
255 dma_prop[0] = cpu_to_be32(liobn);
256 dma_prop[1] = cpu_to_be32(window >> 32);
257 dma_prop[2] = cpu_to_be32(window & 0xFFFFFFFF);
258 dma_prop[3] = 0; /* window size is 32 bits */
259 dma_prop[4] = cpu_to_be32(size);
260
261 ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-address-cells", 2);
262 if (ret < 0) {
263 return ret;
264 }
ad0ebb91 265
5c4cbcf2
AK
266 ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-size-cells", 2);
267 if (ret < 0) {
268 return ret;
269 }
ad0ebb91 270
5c4cbcf2
AK
271 ret = fdt_setprop(fdt, node_off, propname, dma_prop, sizeof(dma_prop));
272 if (ret < 0) {
273 return ret;
ad0ebb91
DG
274 }
275
276 return 0;
277}
5c4cbcf2
AK
278
279int spapr_tcet_dma_dt(void *fdt, int node_off, const char *propname,
2b7dc949 280 sPAPRTCETable *tcet)
5c4cbcf2 281{
2b7dc949 282 if (!tcet) {
5c4cbcf2
AK
283 return 0;
284 }
285
2b7dc949
PB
286 return spapr_dma_dt(fdt, node_off, propname,
287 tcet->liobn, 0, tcet->window_size);
5c4cbcf2 288}