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 */ 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" 25 26 #include "hw/ppc/spapr.h" 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 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; 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 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 62 QLIST_FOREACH(tcet, &spapr_tce_tables, list) { 63 if (tcet->liobn == liobn) { 64 return tcet; 65 } 66 } 67 68 return NULL; 69 } 70 71 static int spapr_tce_translate(DMAContext *dma, 72 dma_addr_t addr, 73 hwaddr *paddr, 74 hwaddr *len, 75 DMADirection dir) 76 { 77 sPAPRTCETable *tcet = DO_UPCAST(sPAPRTCETable, dma, dma); 78 enum sPAPRTCEAccess access = (dir == DMA_DIRECTION_FROM_DEVICE) 79 ? SPAPR_TCE_WO : SPAPR_TCE_RO; 80 uint64_t tce; 81 82 #ifdef DEBUG_TCE 83 fprintf(stderr, "spapr_tce_translate liobn=0x%" PRIx32 " addr=0x" 84 DMA_ADDR_FMT "\n", tcet->liobn, addr); 85 #endif 86 87 if (tcet->bypass) { 88 *paddr = addr; 89 *len = (hwaddr)-1; 90 return 0; 91 } 92 93 /* Check if we are in bound */ 94 if (addr >= tcet->window_size) { 95 #ifdef DEBUG_TCE 96 fprintf(stderr, "spapr_tce_translate out of bounds\n"); 97 #endif 98 return -EFAULT; 99 } 100 101 tce = tcet->table[addr >> SPAPR_TCE_PAGE_SHIFT].tce; 102 103 /* Check TCE */ 104 if (!(tce & access)) { 105 return -EPERM; 106 } 107 108 /* How much til end of page ? */ 109 *len = ((~addr) & SPAPR_TCE_PAGE_MASK) + 1; 110 111 /* Translate */ 112 *paddr = (tce & ~SPAPR_TCE_PAGE_MASK) | 113 (addr & SPAPR_TCE_PAGE_MASK); 114 115 #ifdef DEBUG_TCE 116 fprintf(stderr, " -> *paddr=0x" TARGET_FMT_plx ", *len=0x" 117 TARGET_FMT_plx "\n", *paddr, *len); 118 #endif 119 120 return 0; 121 } 122 123 sPAPRTCETable *spapr_tce_new_table(uint32_t liobn, size_t window_size) 124 { 125 sPAPRTCETable *tcet; 126 127 if (spapr_tce_find_by_liobn(liobn)) { 128 fprintf(stderr, "Attempted to create TCE table with duplicate" 129 " LIOBN 0x%x\n", liobn); 130 return NULL; 131 } 132 133 if (!window_size) { 134 return NULL; 135 } 136 137 tcet = g_malloc0(sizeof(*tcet)); 138 dma_context_init(&tcet->dma, &address_space_memory, spapr_tce_translate, NULL, NULL); 139 140 tcet->liobn = liobn; 141 tcet->window_size = window_size; 142 143 if (kvm_enabled()) { 144 tcet->table = kvmppc_create_spapr_tce(liobn, 145 window_size, 146 &tcet->fd); 147 } 148 149 if (!tcet->table) { 150 size_t table_size = (window_size >> SPAPR_TCE_PAGE_SHIFT) 151 * sizeof(sPAPRTCE); 152 tcet->table = g_malloc0(table_size); 153 } 154 155 #ifdef DEBUG_TCE 156 fprintf(stderr, "spapr_iommu: New TCE table @ %p, liobn=0x%x, " 157 "table @ %p, fd=%d\n", tcet, liobn, tcet->table, tcet->fd); 158 #endif 159 160 QLIST_INSERT_HEAD(&spapr_tce_tables, tcet, list); 161 162 return tcet; 163 } 164 165 void spapr_tce_free(sPAPRTCETable *tcet) 166 { 167 QLIST_REMOVE(tcet, list); 168 169 if (!kvm_enabled() || 170 (kvmppc_remove_spapr_tce(tcet->table, tcet->fd, 171 tcet->window_size) != 0)) { 172 g_free(tcet->table); 173 } 174 175 g_free(tcet); 176 } 177 178 DMAContext *spapr_tce_get_dma(sPAPRTCETable *tcet) 179 { 180 return &tcet->dma; 181 } 182 183 void spapr_tce_set_bypass(sPAPRTCETable *tcet, bool bypass) 184 { 185 tcet->bypass = bypass; 186 } 187 188 void spapr_tce_reset(sPAPRTCETable *tcet) 189 { 190 size_t table_size = (tcet->window_size >> SPAPR_TCE_PAGE_SHIFT) 191 * sizeof(sPAPRTCE); 192 193 tcet->bypass = false; 194 memset(tcet->table, 0, table_size); 195 } 196 197 static target_ulong put_tce_emu(sPAPRTCETable *tcet, target_ulong ioba, 198 target_ulong tce) 199 { 200 sPAPRTCE *tcep; 201 202 if (ioba >= tcet->window_size) { 203 hcall_dprintf("spapr_vio_put_tce on out-of-bounds IOBA 0x" 204 TARGET_FMT_lx "\n", ioba); 205 return H_PARAMETER; 206 } 207 208 tcep = tcet->table + (ioba >> SPAPR_TCE_PAGE_SHIFT); 209 tcep->tce = tce; 210 211 return H_SUCCESS; 212 } 213 214 static target_ulong h_put_tce(PowerPCCPU *cpu, sPAPREnvironment *spapr, 215 target_ulong opcode, target_ulong *args) 216 { 217 target_ulong liobn = args[0]; 218 target_ulong ioba = args[1]; 219 target_ulong tce = args[2]; 220 sPAPRTCETable *tcet = spapr_tce_find_by_liobn(liobn); 221 222 ioba &= ~(SPAPR_TCE_PAGE_SIZE - 1); 223 224 if (tcet) { 225 return put_tce_emu(tcet, ioba, tce); 226 } 227 #ifdef DEBUG_TCE 228 fprintf(stderr, "%s on liobn=" TARGET_FMT_lx /*%s*/ 229 " ioba 0x" TARGET_FMT_lx " TCE 0x" TARGET_FMT_lx "\n", 230 __func__, liobn, /*dev->qdev.id, */ioba, tce); 231 #endif 232 233 return H_PARAMETER; 234 } 235 236 void spapr_iommu_init(void) 237 { 238 QLIST_INIT(&spapr_tce_tables); 239 240 /* hcall-tce */ 241 spapr_register_hypercall(H_PUT_TCE, h_put_tce); 242 } 243 244 int spapr_dma_dt(void *fdt, int node_off, const char *propname, 245 uint32_t liobn, uint64_t window, uint32_t size) 246 { 247 uint32_t dma_prop[5]; 248 int ret; 249 250 dma_prop[0] = cpu_to_be32(liobn); 251 dma_prop[1] = cpu_to_be32(window >> 32); 252 dma_prop[2] = cpu_to_be32(window & 0xFFFFFFFF); 253 dma_prop[3] = 0; /* window size is 32 bits */ 254 dma_prop[4] = cpu_to_be32(size); 255 256 ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-address-cells", 2); 257 if (ret < 0) { 258 return ret; 259 } 260 261 ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-size-cells", 2); 262 if (ret < 0) { 263 return ret; 264 } 265 266 ret = fdt_setprop(fdt, node_off, propname, dma_prop, sizeof(dma_prop)); 267 if (ret < 0) { 268 return ret; 269 } 270 271 return 0; 272 } 273 274 int spapr_tcet_dma_dt(void *fdt, int node_off, const char *propname, 275 sPAPRTCETable *tcet) 276 { 277 if (!tcet) { 278 return 0; 279 } 280 281 return spapr_dma_dt(fdt, node_off, propname, 282 tcet->liobn, 0, tcet->window_size); 283 } 284