spapr: make IOMMU translation go through IOMMUTLBEntry
[qemu/ar7.git] / hw / ppc / spapr_iommu.c
blobcf5ccb17ed9bb2617515bfc75d4212aa12070e2c
1 /*
2 * QEMU sPAPR IOMMU (TCE) code
4 * Copyright (c) 2010 David Gibson, IBM Corporation <dwg@au1.ibm.com>
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/>.
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"
26 #include "hw/ppc/spapr.h"
28 #include <libfdt.h>
30 /* #define DEBUG_TCE */
32 enum sPAPRTCEAccess {
33 SPAPR_TCE_FAULT = 0,
34 SPAPR_TCE_RO = 1,
35 SPAPR_TCE_WO = 2,
36 SPAPR_TCE_RW = 3,
39 struct sPAPRTCETable {
40 DMAContext dma;
41 uint32_t liobn;
42 uint32_t window_size;
43 sPAPRTCE *table;
44 bool bypass;
45 int fd;
46 QLIST_ENTRY(sPAPRTCETable) list;
50 QLIST_HEAD(spapr_tce_tables, sPAPRTCETable) spapr_tce_tables;
52 static sPAPRTCETable *spapr_tce_find_by_liobn(uint32_t liobn)
54 sPAPRTCETable *tcet;
56 if (liobn & 0xFFFFFFFF00000000ULL) {
57 hcall_dprintf("Request for out-of-bounds LIOBN 0x" TARGET_FMT_lx "\n",
58 liobn);
59 return NULL;
62 QLIST_FOREACH(tcet, &spapr_tce_tables, list) {
63 if (tcet->liobn == liobn) {
64 return tcet;
68 return NULL;
71 static IOMMUTLBEntry spapr_tce_translate_iommu(sPAPRTCETable *tcet, hwaddr addr)
73 uint64_t tce;
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
80 if (tcet->bypass) {
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,
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
95 return (IOMMUTLBEntry) { .perm = IOMMU_NONE };
98 tce = tcet->table[addr >> SPAPR_TCE_PAGE_SHIFT].tce;
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
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,
114 static int spapr_tce_translate(DMAContext *dma,
115 dma_addr_t addr,
116 hwaddr *paddr,
117 hwaddr *len,
118 DMADirection dir)
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))) {
124 return -EPERM;
127 /* Translate */
128 *paddr = entry.translated_addr | (addr & entry.addr_mask);
129 *len = (addr | entry.addr_mask) - addr + 1;
130 return 0;
133 sPAPRTCETable *spapr_tce_new_table(uint32_t liobn, size_t window_size)
135 sPAPRTCETable *tcet;
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;
143 if (!window_size) {
144 return NULL;
147 tcet = g_malloc0(sizeof(*tcet));
148 dma_context_init(&tcet->dma, &address_space_memory, spapr_tce_translate, NULL, NULL);
150 tcet->liobn = liobn;
151 tcet->window_size = window_size;
153 if (kvm_enabled()) {
154 tcet->table = kvmppc_create_spapr_tce(liobn,
155 window_size,
156 &tcet->fd);
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);
165 #ifdef DEBUG_TCE
166 fprintf(stderr, "spapr_iommu: New TCE table @ %p, liobn=0x%x, "
167 "table @ %p, fd=%d\n", tcet, liobn, tcet->table, tcet->fd);
168 #endif
170 QLIST_INSERT_HEAD(&spapr_tce_tables, tcet, list);
172 return tcet;
175 void spapr_tce_free(sPAPRTCETable *tcet)
177 QLIST_REMOVE(tcet, list);
179 if (!kvm_enabled() ||
180 (kvmppc_remove_spapr_tce(tcet->table, tcet->fd,
181 tcet->window_size) != 0)) {
182 g_free(tcet->table);
185 g_free(tcet);
188 DMAContext *spapr_tce_get_dma(sPAPRTCETable *tcet)
190 return &tcet->dma;
193 void spapr_tce_set_bypass(sPAPRTCETable *tcet, bool bypass)
195 tcet->bypass = bypass;
198 void spapr_tce_reset(sPAPRTCETable *tcet)
200 size_t table_size = (tcet->window_size >> SPAPR_TCE_PAGE_SHIFT)
201 * sizeof(sPAPRTCE);
203 tcet->bypass = false;
204 memset(tcet->table, 0, table_size);
207 static target_ulong put_tce_emu(sPAPRTCETable *tcet, target_ulong ioba,
208 target_ulong tce)
210 sPAPRTCE *tcep;
212 if (ioba >= tcet->window_size) {
213 hcall_dprintf("spapr_vio_put_tce on out-of-bounds IOBA 0x"
214 TARGET_FMT_lx "\n", ioba);
215 return H_PARAMETER;
218 tcep = tcet->table + (ioba >> SPAPR_TCE_PAGE_SHIFT);
219 tcep->tce = tce;
221 return H_SUCCESS;
224 static target_ulong h_put_tce(PowerPCCPU *cpu, sPAPREnvironment *spapr,
225 target_ulong opcode, target_ulong *args)
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);
232 ioba &= ~(SPAPR_TCE_PAGE_SIZE - 1);
234 if (tcet) {
235 return put_tce_emu(tcet, ioba, tce);
237 #ifdef DEBUG_TCE
238 fprintf(stderr, "%s on liobn=" TARGET_FMT_lx /*%s*/
239 " ioba 0x" TARGET_FMT_lx " TCE 0x" TARGET_FMT_lx "\n",
240 __func__, liobn, /*dev->qdev.id, */ioba, tce);
241 #endif
243 return H_PARAMETER;
246 void spapr_iommu_init(void)
248 QLIST_INIT(&spapr_tce_tables);
250 /* hcall-tce */
251 spapr_register_hypercall(H_PUT_TCE, h_put_tce);
254 int spapr_dma_dt(void *fdt, int node_off, const char *propname,
255 uint32_t liobn, uint64_t window, uint32_t size)
257 uint32_t dma_prop[5];
258 int ret;
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);
266 ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-address-cells", 2);
267 if (ret < 0) {
268 return ret;
271 ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-size-cells", 2);
272 if (ret < 0) {
273 return ret;
276 ret = fdt_setprop(fdt, node_off, propname, dma_prop, sizeof(dma_prop));
277 if (ret < 0) {
278 return ret;
281 return 0;
284 int spapr_tcet_dma_dt(void *fdt, int node_off, const char *propname,
285 sPAPRTCETable *tcet)
287 if (!tcet) {
288 return 0;
291 return spapr_dma_dt(fdt, node_off, propname,
292 tcet->liobn, 0, tcet->window_size);