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