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/sysemu.h" 29 #include "sysemu/char.h" 30 #include "hw/qdev.h" 31 #include "sysemu/device_tree.h" 32 33 #include "hw/ppc/spapr.h" 34 #include "hw/ppc/spapr_vio.h" 35 #include "qapi-event.h" 36 37 #include <libfdt.h> 38 39 static void rtas_display_character(PowerPCCPU *cpu, sPAPREnvironment *spapr, 40 uint32_t token, uint32_t nargs, 41 target_ulong args, 42 uint32_t nret, target_ulong rets) 43 { 44 uint8_t c = rtas_ld(args, 0); 45 VIOsPAPRDevice *sdev = vty_lookup(spapr, 0); 46 47 if (!sdev) { 48 rtas_st(rets, 0, RTAS_OUT_HW_ERROR); 49 } else { 50 vty_putchars(sdev, &c, sizeof(c)); 51 rtas_st(rets, 0, RTAS_OUT_SUCCESS); 52 } 53 } 54 55 static void rtas_get_time_of_day(PowerPCCPU *cpu, sPAPREnvironment *spapr, 56 uint32_t token, uint32_t nargs, 57 target_ulong args, 58 uint32_t nret, target_ulong rets) 59 { 60 struct tm tm; 61 62 if (nret != 8) { 63 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 64 return; 65 } 66 67 qemu_get_timedate(&tm, spapr->rtc_offset); 68 69 rtas_st(rets, 0, RTAS_OUT_SUCCESS); 70 rtas_st(rets, 1, tm.tm_year + 1900); 71 rtas_st(rets, 2, tm.tm_mon + 1); 72 rtas_st(rets, 3, tm.tm_mday); 73 rtas_st(rets, 4, tm.tm_hour); 74 rtas_st(rets, 5, tm.tm_min); 75 rtas_st(rets, 6, tm.tm_sec); 76 rtas_st(rets, 7, 0); /* we don't do nanoseconds */ 77 } 78 79 static void rtas_set_time_of_day(PowerPCCPU *cpu, sPAPREnvironment *spapr, 80 uint32_t token, uint32_t nargs, 81 target_ulong args, 82 uint32_t nret, target_ulong rets) 83 { 84 struct tm tm; 85 86 tm.tm_year = rtas_ld(args, 0) - 1900; 87 tm.tm_mon = rtas_ld(args, 1) - 1; 88 tm.tm_mday = rtas_ld(args, 2); 89 tm.tm_hour = rtas_ld(args, 3); 90 tm.tm_min = rtas_ld(args, 4); 91 tm.tm_sec = rtas_ld(args, 5); 92 93 /* Just generate a monitor event for the change */ 94 qapi_event_send_rtc_change(qemu_timedate_diff(&tm), &error_abort); 95 spapr->rtc_offset = qemu_timedate_diff(&tm); 96 97 rtas_st(rets, 0, RTAS_OUT_SUCCESS); 98 } 99 100 static void rtas_power_off(PowerPCCPU *cpu, sPAPREnvironment *spapr, 101 uint32_t token, uint32_t nargs, target_ulong args, 102 uint32_t nret, target_ulong rets) 103 { 104 if (nargs != 2 || nret != 1) { 105 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 106 return; 107 } 108 qemu_system_shutdown_request(); 109 rtas_st(rets, 0, RTAS_OUT_SUCCESS); 110 } 111 112 static void rtas_system_reboot(PowerPCCPU *cpu, sPAPREnvironment *spapr, 113 uint32_t token, uint32_t nargs, 114 target_ulong args, 115 uint32_t nret, target_ulong rets) 116 { 117 if (nargs != 0 || nret != 1) { 118 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 119 return; 120 } 121 qemu_system_reset_request(); 122 rtas_st(rets, 0, RTAS_OUT_SUCCESS); 123 } 124 125 static void rtas_query_cpu_stopped_state(PowerPCCPU *cpu_, 126 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; 132 PowerPCCPU *cpu; 133 134 if (nargs != 1 || nret != 2) { 135 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 136 return; 137 } 138 139 id = rtas_ld(args, 0); 140 cpu = ppc_get_vcpu_by_dt_id(id); 141 if (cpu != NULL) { 142 if (CPU(cpu)->halted) { 143 rtas_st(rets, 1, 0); 144 } else { 145 rtas_st(rets, 1, 2); 146 } 147 148 rtas_st(rets, 0, RTAS_OUT_SUCCESS); 149 return; 150 } 151 152 /* Didn't find a matching cpu */ 153 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 154 } 155 156 static void rtas_start_cpu(PowerPCCPU *cpu_, sPAPREnvironment *spapr, 157 uint32_t token, uint32_t nargs, 158 target_ulong args, 159 uint32_t nret, target_ulong rets) 160 { 161 target_ulong id, start, r3; 162 PowerPCCPU *cpu; 163 164 if (nargs != 3 || nret != 1) { 165 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 166 return; 167 } 168 169 id = rtas_ld(args, 0); 170 start = rtas_ld(args, 1); 171 r3 = rtas_ld(args, 2); 172 173 cpu = ppc_get_vcpu_by_dt_id(id); 174 if (cpu != NULL) { 175 CPUState *cs = CPU(cpu); 176 CPUPPCState *env = &cpu->env; 177 178 if (!cs->halted) { 179 rtas_st(rets, 0, RTAS_OUT_HW_ERROR); 180 return; 181 } 182 183 /* This will make sure qemu state is up to date with kvm, and 184 * mark it dirty so our changes get flushed back before the 185 * new cpu enters */ 186 kvm_cpu_synchronize_state(cs); 187 188 env->msr = (1ULL << MSR_SF) | (1ULL << MSR_ME); 189 env->nip = start; 190 env->gpr[3] = r3; 191 cs->halted = 0; 192 193 qemu_cpu_kick(cs); 194 195 rtas_st(rets, 0, RTAS_OUT_SUCCESS); 196 return; 197 } 198 199 /* Didn't find a matching cpu */ 200 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 201 } 202 203 static void rtas_stop_self(PowerPCCPU *cpu, sPAPREnvironment *spapr, 204 uint32_t token, uint32_t nargs, 205 target_ulong args, 206 uint32_t nret, target_ulong rets) 207 { 208 CPUState *cs = CPU(cpu); 209 CPUPPCState *env = &cpu->env; 210 211 cs->halted = 1; 212 cpu_exit(cs); 213 /* 214 * While stopping a CPU, the guest calls H_CPPR which 215 * effectively disables interrupts on XICS level. 216 * However decrementer interrupts in TCG can still 217 * wake the CPU up so here we disable interrupts in MSR 218 * as well. 219 * As rtas_start_cpu() resets the whole MSR anyway, there is 220 * no need to bother with specific bits, we just clear it. 221 */ 222 env->msr = 0; 223 } 224 225 static void rtas_ibm_get_system_parameter(PowerPCCPU *cpu, 226 sPAPREnvironment *spapr, 227 uint32_t token, uint32_t nargs, 228 target_ulong args, 229 uint32_t nret, target_ulong rets) 230 { 231 target_ulong parameter = rtas_ld(args, 0); 232 target_ulong buffer = rtas_ld(args, 1); 233 target_ulong length = rtas_ld(args, 2); 234 target_ulong ret = RTAS_OUT_SUCCESS; 235 236 switch (parameter) { 237 case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS: { 238 char *param_val = g_strdup_printf("MaxEntCap=%d,MaxPlatProcs=%d", 239 max_cpus, smp_cpus); 240 rtas_st_buffer(buffer, length, (uint8_t *)param_val, strlen(param_val)); 241 g_free(param_val); 242 break; 243 } 244 case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE: { 245 uint8_t param_val = DIAGNOSTICS_RUN_MODE_DISABLED; 246 247 rtas_st_buffer(buffer, length, ¶m_val, sizeof(param_val)); 248 break; 249 } 250 case RTAS_SYSPARM_UUID: 251 rtas_st_buffer(buffer, length, qemu_uuid, (qemu_uuid_set ? 16 : 0)); 252 break; 253 default: 254 ret = RTAS_OUT_NOT_SUPPORTED; 255 } 256 257 rtas_st(rets, 0, ret); 258 } 259 260 static void rtas_ibm_set_system_parameter(PowerPCCPU *cpu, 261 sPAPREnvironment *spapr, 262 uint32_t token, uint32_t nargs, 263 target_ulong args, 264 uint32_t nret, target_ulong rets) 265 { 266 target_ulong parameter = rtas_ld(args, 0); 267 target_ulong ret = RTAS_OUT_NOT_SUPPORTED; 268 269 switch (parameter) { 270 case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS: 271 case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE: 272 case RTAS_SYSPARM_UUID: 273 ret = RTAS_OUT_NOT_AUTHORIZED; 274 break; 275 } 276 277 rtas_st(rets, 0, ret); 278 } 279 280 static struct rtas_call { 281 const char *name; 282 spapr_rtas_fn fn; 283 } rtas_table[RTAS_TOKEN_MAX - RTAS_TOKEN_BASE]; 284 285 target_ulong spapr_rtas_call(PowerPCCPU *cpu, sPAPREnvironment *spapr, 286 uint32_t token, uint32_t nargs, target_ulong args, 287 uint32_t nret, target_ulong rets) 288 { 289 if ((token >= RTAS_TOKEN_BASE) && (token < RTAS_TOKEN_MAX)) { 290 struct rtas_call *call = rtas_table + (token - RTAS_TOKEN_BASE); 291 292 if (call->fn) { 293 call->fn(cpu, spapr, token, nargs, args, nret, rets); 294 return H_SUCCESS; 295 } 296 } 297 298 /* HACK: Some Linux early debug code uses RTAS display-character, 299 * but assumes the token value is 0xa (which it is on some real 300 * machines) without looking it up in the device tree. This 301 * special case makes this work */ 302 if (token == 0xa) { 303 rtas_display_character(cpu, spapr, 0xa, nargs, args, nret, rets); 304 return H_SUCCESS; 305 } 306 307 hcall_dprintf("Unknown RTAS token 0x%x\n", token); 308 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 309 return H_PARAMETER; 310 } 311 312 void spapr_rtas_register(int token, const char *name, spapr_rtas_fn fn) 313 { 314 if (!((token >= RTAS_TOKEN_BASE) && (token < RTAS_TOKEN_MAX))) { 315 fprintf(stderr, "RTAS invalid token 0x%x\n", token); 316 exit(1); 317 } 318 319 token -= RTAS_TOKEN_BASE; 320 if (rtas_table[token].name) { 321 fprintf(stderr, "RTAS call \"%s\" is registered already as 0x%x\n", 322 rtas_table[token].name, token); 323 exit(1); 324 } 325 326 rtas_table[token].name = name; 327 rtas_table[token].fn = fn; 328 } 329 330 int spapr_rtas_device_tree_setup(void *fdt, hwaddr rtas_addr, 331 hwaddr rtas_size) 332 { 333 int ret; 334 int i; 335 336 ret = fdt_add_mem_rsv(fdt, rtas_addr, rtas_size); 337 if (ret < 0) { 338 fprintf(stderr, "Couldn't add RTAS reserve entry: %s\n", 339 fdt_strerror(ret)); 340 return ret; 341 } 342 343 ret = qemu_fdt_setprop_cell(fdt, "/rtas", "linux,rtas-base", 344 rtas_addr); 345 if (ret < 0) { 346 fprintf(stderr, "Couldn't add linux,rtas-base property: %s\n", 347 fdt_strerror(ret)); 348 return ret; 349 } 350 351 ret = qemu_fdt_setprop_cell(fdt, "/rtas", "linux,rtas-entry", 352 rtas_addr); 353 if (ret < 0) { 354 fprintf(stderr, "Couldn't add linux,rtas-entry property: %s\n", 355 fdt_strerror(ret)); 356 return ret; 357 } 358 359 ret = qemu_fdt_setprop_cell(fdt, "/rtas", "rtas-size", 360 rtas_size); 361 if (ret < 0) { 362 fprintf(stderr, "Couldn't add rtas-size property: %s\n", 363 fdt_strerror(ret)); 364 return ret; 365 } 366 367 for (i = 0; i < RTAS_TOKEN_MAX - RTAS_TOKEN_BASE; i++) { 368 struct rtas_call *call = &rtas_table[i]; 369 370 if (!call->name) { 371 continue; 372 } 373 374 ret = qemu_fdt_setprop_cell(fdt, "/rtas", call->name, 375 i + RTAS_TOKEN_BASE); 376 if (ret < 0) { 377 fprintf(stderr, "Couldn't add rtas token for %s: %s\n", 378 call->name, fdt_strerror(ret)); 379 return ret; 380 } 381 382 } 383 return 0; 384 } 385 386 static void core_rtas_register_types(void) 387 { 388 spapr_rtas_register(RTAS_DISPLAY_CHARACTER, "display-character", 389 rtas_display_character); 390 spapr_rtas_register(RTAS_GET_TIME_OF_DAY, "get-time-of-day", 391 rtas_get_time_of_day); 392 spapr_rtas_register(RTAS_SET_TIME_OF_DAY, "set-time-of-day", 393 rtas_set_time_of_day); 394 spapr_rtas_register(RTAS_POWER_OFF, "power-off", rtas_power_off); 395 spapr_rtas_register(RTAS_SYSTEM_REBOOT, "system-reboot", 396 rtas_system_reboot); 397 spapr_rtas_register(RTAS_QUERY_CPU_STOPPED_STATE, "query-cpu-stopped-state", 398 rtas_query_cpu_stopped_state); 399 spapr_rtas_register(RTAS_START_CPU, "start-cpu", rtas_start_cpu); 400 spapr_rtas_register(RTAS_STOP_SELF, "stop-self", rtas_stop_self); 401 spapr_rtas_register(RTAS_IBM_GET_SYSTEM_PARAMETER, 402 "ibm,get-system-parameter", 403 rtas_ibm_get_system_parameter); 404 spapr_rtas_register(RTAS_IBM_SET_SYSTEM_PARAMETER, 405 "ibm,set-system-parameter", 406 rtas_ibm_set_system_parameter); 407 } 408 409 type_init(core_rtas_register_types) 410