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