David Gibson | 39ac845 | 2011-04-01 15:15:23 +1100 | [diff] [blame] | 1 | /* |
| 2 | * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator |
| 3 | * |
| 4 | * Hypercall based emulated RTAS |
| 5 | * |
| 6 | * Copyright (c) 2010-2011 David Gibson, IBM Corporation. |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 21 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | * THE SOFTWARE. |
| 25 | * |
| 26 | */ |
| 27 | #include "cpu.h" |
| 28 | #include "sysemu.h" |
| 29 | #include "qemu-char.h" |
| 30 | #include "hw/qdev.h" |
| 31 | #include "device_tree.h" |
| 32 | |
| 33 | #include "hw/spapr.h" |
| 34 | #include "hw/spapr_vio.h" |
| 35 | |
| 36 | #include <libfdt.h> |
| 37 | |
| 38 | #define TOKEN_BASE 0x2000 |
| 39 | #define TOKEN_MAX 0x100 |
| 40 | |
David Gibson | 821303f | 2011-04-01 15:15:24 +1100 | [diff] [blame] | 41 | static void rtas_display_character(sPAPREnvironment *spapr, |
| 42 | uint32_t token, uint32_t nargs, |
| 43 | target_ulong args, |
| 44 | uint32_t nret, target_ulong rets) |
| 45 | { |
| 46 | uint8_t c = rtas_ld(args, 0); |
| 47 | VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, 0); |
| 48 | |
| 49 | if (!sdev) { |
| 50 | rtas_st(rets, 0, -1); |
| 51 | } else { |
| 52 | vty_putchars(sdev, &c, sizeof(c)); |
| 53 | rtas_st(rets, 0, 0); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | static void rtas_get_time_of_day(sPAPREnvironment *spapr, |
| 58 | uint32_t token, uint32_t nargs, |
| 59 | target_ulong args, |
| 60 | uint32_t nret, target_ulong rets) |
| 61 | { |
| 62 | struct tm tm; |
| 63 | |
| 64 | if (nret != 8) { |
| 65 | rtas_st(rets, 0, -3); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | qemu_get_timedate(&tm, 0); |
| 70 | |
| 71 | rtas_st(rets, 0, 0); /* Success */ |
| 72 | rtas_st(rets, 1, tm.tm_year + 1900); |
| 73 | rtas_st(rets, 2, tm.tm_mon + 1); |
| 74 | rtas_st(rets, 3, tm.tm_mday); |
| 75 | rtas_st(rets, 4, tm.tm_hour); |
| 76 | rtas_st(rets, 5, tm.tm_min); |
| 77 | rtas_st(rets, 6, tm.tm_sec); |
| 78 | rtas_st(rets, 7, 0); /* we don't do nanoseconds */ |
| 79 | } |
| 80 | |
| 81 | static void rtas_power_off(sPAPREnvironment *spapr, |
| 82 | uint32_t token, uint32_t nargs, target_ulong args, |
| 83 | uint32_t nret, target_ulong rets) |
| 84 | { |
| 85 | if (nargs != 2 || nret != 1) { |
| 86 | rtas_st(rets, 0, -3); |
| 87 | return; |
| 88 | } |
| 89 | qemu_system_shutdown_request(); |
| 90 | rtas_st(rets, 0, 0); |
| 91 | } |
| 92 | |
David Gibson | a9f8ad8 | 2011-04-01 15:15:34 +1100 | [diff] [blame] | 93 | static void rtas_query_cpu_stopped_state(sPAPREnvironment *spapr, |
| 94 | uint32_t token, uint32_t nargs, |
| 95 | target_ulong args, |
| 96 | uint32_t nret, target_ulong rets) |
| 97 | { |
| 98 | target_ulong id; |
| 99 | CPUState *env; |
| 100 | |
| 101 | if (nargs != 1 || nret != 2) { |
| 102 | rtas_st(rets, 0, -3); |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | id = rtas_ld(args, 0); |
| 107 | for (env = first_cpu; env; env = env->next_cpu) { |
| 108 | if (env->cpu_index != id) { |
| 109 | continue; |
| 110 | } |
| 111 | |
| 112 | if (env->halted) { |
| 113 | rtas_st(rets, 1, 0); |
| 114 | } else { |
| 115 | rtas_st(rets, 1, 2); |
| 116 | } |
| 117 | |
| 118 | rtas_st(rets, 0, 0); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | /* Didn't find a matching cpu */ |
| 123 | rtas_st(rets, 0, -3); |
| 124 | } |
| 125 | |
| 126 | static void rtas_start_cpu(sPAPREnvironment *spapr, |
| 127 | uint32_t token, uint32_t nargs, |
| 128 | target_ulong args, |
| 129 | uint32_t nret, target_ulong rets) |
| 130 | { |
| 131 | target_ulong id, start, r3; |
| 132 | CPUState *env; |
| 133 | |
| 134 | if (nargs != 3 || nret != 1) { |
| 135 | rtas_st(rets, 0, -3); |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | id = rtas_ld(args, 0); |
| 140 | start = rtas_ld(args, 1); |
| 141 | r3 = rtas_ld(args, 2); |
| 142 | |
| 143 | for (env = first_cpu; env; env = env->next_cpu) { |
| 144 | if (env->cpu_index != id) { |
| 145 | continue; |
| 146 | } |
| 147 | |
| 148 | if (!env->halted) { |
| 149 | rtas_st(rets, 0, -1); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | env->msr = (1ULL << MSR_SF) | (1ULL << MSR_ME); |
| 154 | env->nip = start; |
| 155 | env->gpr[3] = r3; |
| 156 | env->halted = 0; |
| 157 | |
| 158 | qemu_cpu_kick(env); |
| 159 | |
| 160 | rtas_st(rets, 0, 0); |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | /* Didn't find a matching cpu */ |
| 165 | rtas_st(rets, 0, -3); |
| 166 | } |
| 167 | |
David Gibson | 39ac845 | 2011-04-01 15:15:23 +1100 | [diff] [blame] | 168 | static struct rtas_call { |
| 169 | const char *name; |
| 170 | spapr_rtas_fn fn; |
| 171 | } rtas_table[TOKEN_MAX]; |
| 172 | |
| 173 | struct rtas_call *rtas_next = rtas_table; |
| 174 | |
| 175 | target_ulong spapr_rtas_call(sPAPREnvironment *spapr, |
| 176 | uint32_t token, uint32_t nargs, target_ulong args, |
| 177 | uint32_t nret, target_ulong rets) |
| 178 | { |
| 179 | if ((token >= TOKEN_BASE) |
| 180 | && ((token - TOKEN_BASE) < TOKEN_MAX)) { |
| 181 | struct rtas_call *call = rtas_table + (token - TOKEN_BASE); |
| 182 | |
| 183 | if (call->fn) { |
| 184 | call->fn(spapr, token, nargs, args, nret, rets); |
| 185 | return H_SUCCESS; |
| 186 | } |
| 187 | } |
| 188 | |
David Gibson | 821303f | 2011-04-01 15:15:24 +1100 | [diff] [blame] | 189 | /* HACK: Some Linux early debug code uses RTAS display-character, |
| 190 | * but assumes the token value is 0xa (which it is on some real |
| 191 | * machines) without looking it up in the device tree. This |
| 192 | * special case makes this work */ |
| 193 | if (token == 0xa) { |
| 194 | rtas_display_character(spapr, 0xa, nargs, args, nret, rets); |
| 195 | return H_SUCCESS; |
| 196 | } |
| 197 | |
David Gibson | 39ac845 | 2011-04-01 15:15:23 +1100 | [diff] [blame] | 198 | hcall_dprintf("Unknown RTAS token 0x%x\n", token); |
| 199 | rtas_st(rets, 0, -3); |
| 200 | return H_PARAMETER; |
| 201 | } |
| 202 | |
| 203 | void spapr_rtas_register(const char *name, spapr_rtas_fn fn) |
| 204 | { |
| 205 | assert(rtas_next < (rtas_table + TOKEN_MAX)); |
| 206 | |
| 207 | rtas_next->name = name; |
| 208 | rtas_next->fn = fn; |
| 209 | |
| 210 | rtas_next++; |
| 211 | } |
| 212 | |
| 213 | int spapr_rtas_device_tree_setup(void *fdt, target_phys_addr_t rtas_addr, |
| 214 | target_phys_addr_t rtas_size) |
| 215 | { |
| 216 | int ret; |
| 217 | int i; |
| 218 | |
| 219 | ret = fdt_add_mem_rsv(fdt, rtas_addr, rtas_size); |
| 220 | if (ret < 0) { |
| 221 | fprintf(stderr, "Couldn't add RTAS reserve entry: %s\n", |
| 222 | fdt_strerror(ret)); |
| 223 | return ret; |
| 224 | } |
| 225 | |
| 226 | ret = qemu_devtree_setprop_cell(fdt, "/rtas", "linux,rtas-base", |
| 227 | rtas_addr); |
| 228 | if (ret < 0) { |
| 229 | fprintf(stderr, "Couldn't add linux,rtas-base property: %s\n", |
| 230 | fdt_strerror(ret)); |
| 231 | return ret; |
| 232 | } |
| 233 | |
| 234 | ret = qemu_devtree_setprop_cell(fdt, "/rtas", "linux,rtas-entry", |
| 235 | rtas_addr); |
| 236 | if (ret < 0) { |
| 237 | fprintf(stderr, "Couldn't add linux,rtas-entry property: %s\n", |
| 238 | fdt_strerror(ret)); |
| 239 | return ret; |
| 240 | } |
| 241 | |
| 242 | ret = qemu_devtree_setprop_cell(fdt, "/rtas", "rtas-size", |
| 243 | rtas_size); |
| 244 | if (ret < 0) { |
| 245 | fprintf(stderr, "Couldn't add rtas-size property: %s\n", |
| 246 | fdt_strerror(ret)); |
| 247 | return ret; |
| 248 | } |
| 249 | |
| 250 | for (i = 0; i < TOKEN_MAX; i++) { |
| 251 | struct rtas_call *call = &rtas_table[i]; |
| 252 | |
| 253 | if (!call->fn) { |
| 254 | continue; |
| 255 | } |
| 256 | |
| 257 | ret = qemu_devtree_setprop_cell(fdt, "/rtas", call->name, |
| 258 | i + TOKEN_BASE); |
| 259 | if (ret < 0) { |
| 260 | fprintf(stderr, "Couldn't add rtas token for %s: %s\n", |
| 261 | call->name, fdt_strerror(ret)); |
| 262 | return ret; |
| 263 | } |
| 264 | |
| 265 | } |
| 266 | return 0; |
| 267 | } |
David Gibson | 821303f | 2011-04-01 15:15:24 +1100 | [diff] [blame] | 268 | |
| 269 | static void register_core_rtas(void) |
| 270 | { |
| 271 | spapr_rtas_register("display-character", rtas_display_character); |
| 272 | spapr_rtas_register("get-time-of-day", rtas_get_time_of_day); |
| 273 | spapr_rtas_register("power-off", rtas_power_off); |
David Gibson | a9f8ad8 | 2011-04-01 15:15:34 +1100 | [diff] [blame] | 274 | spapr_rtas_register("query-cpu-stopped-state", |
| 275 | rtas_query_cpu_stopped_state); |
| 276 | spapr_rtas_register("start-cpu", rtas_start_cpu); |
David Gibson | 821303f | 2011-04-01 15:15:24 +1100 | [diff] [blame] | 277 | } |
| 278 | device_init(register_core_rtas); |