kvm: move cpu synchronization code
[qemu/ar7.git] / hw / ppc / spapr_rtas.c
blobbb19944686d298520a83fcc37b4bec5da5a39f28
1 /*
2 * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
4 * Hypercall based emulated RTAS
6 * Copyright (c) 2010-2011 David Gibson, IBM Corporation.
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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.
27 #include "qemu/osdep.h"
28 #include "cpu.h"
29 #include "qemu/log.h"
30 #include "qemu/error-report.h"
31 #include "sysemu/sysemu.h"
32 #include "sysemu/char.h"
33 #include "hw/qdev.h"
34 #include "sysemu/device_tree.h"
35 #include "sysemu/cpus.h"
36 #include "sysemu/kvm.h"
38 #include "hw/ppc/spapr.h"
39 #include "hw/ppc/spapr_vio.h"
40 #include "hw/ppc/spapr_rtas.h"
41 #include "hw/ppc/ppc.h"
42 #include "qapi-event.h"
43 #include "hw/boards.h"
45 #include <libfdt.h>
46 #include "hw/ppc/spapr_drc.h"
47 #include "qemu/cutils.h"
48 #include "trace.h"
49 #include "hw/ppc/fdt.h"
51 static sPAPRConfigureConnectorState *spapr_ccs_find(sPAPRMachineState *spapr,
52 uint32_t drc_index)
54 sPAPRConfigureConnectorState *ccs = NULL;
56 QTAILQ_FOREACH(ccs, &spapr->ccs_list, next) {
57 if (ccs->drc_index == drc_index) {
58 break;
62 return ccs;
65 static void spapr_ccs_add(sPAPRMachineState *spapr,
66 sPAPRConfigureConnectorState *ccs)
68 g_assert(!spapr_ccs_find(spapr, ccs->drc_index));
69 QTAILQ_INSERT_HEAD(&spapr->ccs_list, ccs, next);
72 static void spapr_ccs_remove(sPAPRMachineState *spapr,
73 sPAPRConfigureConnectorState *ccs)
75 QTAILQ_REMOVE(&spapr->ccs_list, ccs, next);
76 g_free(ccs);
79 void spapr_ccs_reset_hook(void *opaque)
81 sPAPRMachineState *spapr = opaque;
82 sPAPRConfigureConnectorState *ccs, *ccs_tmp;
84 QTAILQ_FOREACH_SAFE(ccs, &spapr->ccs_list, next, ccs_tmp) {
85 spapr_ccs_remove(spapr, ccs);
89 static void rtas_display_character(PowerPCCPU *cpu, sPAPRMachineState *spapr,
90 uint32_t token, uint32_t nargs,
91 target_ulong args,
92 uint32_t nret, target_ulong rets)
94 uint8_t c = rtas_ld(args, 0);
95 VIOsPAPRDevice *sdev = vty_lookup(spapr, 0);
97 if (!sdev) {
98 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
99 } else {
100 vty_putchars(sdev, &c, sizeof(c));
101 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
105 static void rtas_power_off(PowerPCCPU *cpu, sPAPRMachineState *spapr,
106 uint32_t token, uint32_t nargs, target_ulong args,
107 uint32_t nret, target_ulong rets)
109 if (nargs != 2 || nret != 1) {
110 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
111 return;
113 qemu_system_shutdown_request();
114 cpu_stop_current();
115 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
118 static void rtas_system_reboot(PowerPCCPU *cpu, sPAPRMachineState *spapr,
119 uint32_t token, uint32_t nargs,
120 target_ulong args,
121 uint32_t nret, target_ulong rets)
123 if (nargs != 0 || nret != 1) {
124 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
125 return;
127 qemu_system_reset_request();
128 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
131 static void rtas_query_cpu_stopped_state(PowerPCCPU *cpu_,
132 sPAPRMachineState *spapr,
133 uint32_t token, uint32_t nargs,
134 target_ulong args,
135 uint32_t nret, target_ulong rets)
137 target_ulong id;
138 PowerPCCPU *cpu;
140 if (nargs != 1 || nret != 2) {
141 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
142 return;
145 id = rtas_ld(args, 0);
146 cpu = ppc_get_vcpu_by_dt_id(id);
147 if (cpu != NULL) {
148 if (CPU(cpu)->halted) {
149 rtas_st(rets, 1, 0);
150 } else {
151 rtas_st(rets, 1, 2);
154 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
155 return;
158 /* Didn't find a matching cpu */
159 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
163 * Set the timebase offset of the CPU to that of first CPU.
164 * This helps hotplugged CPU to have the correct timebase offset.
166 static void spapr_cpu_update_tb_offset(PowerPCCPU *cpu)
168 PowerPCCPU *fcpu = POWERPC_CPU(first_cpu);
170 cpu->env.tb_env->tb_offset = fcpu->env.tb_env->tb_offset;
173 static void spapr_cpu_set_endianness(PowerPCCPU *cpu)
175 PowerPCCPU *fcpu = POWERPC_CPU(first_cpu);
176 PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(fcpu);
178 if (!pcc->interrupts_big_endian(fcpu)) {
179 cpu->env.spr[SPR_LPCR] |= LPCR_ILE;
183 static void rtas_start_cpu(PowerPCCPU *cpu_, sPAPRMachineState *spapr,
184 uint32_t token, uint32_t nargs,
185 target_ulong args,
186 uint32_t nret, target_ulong rets)
188 target_ulong id, start, r3;
189 PowerPCCPU *cpu;
191 if (nargs != 3 || nret != 1) {
192 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
193 return;
196 id = rtas_ld(args, 0);
197 start = rtas_ld(args, 1);
198 r3 = rtas_ld(args, 2);
200 cpu = ppc_get_vcpu_by_dt_id(id);
201 if (cpu != NULL) {
202 CPUState *cs = CPU(cpu);
203 CPUPPCState *env = &cpu->env;
205 if (!cs->halted) {
206 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
207 return;
210 /* This will make sure qemu state is up to date with kvm, and
211 * mark it dirty so our changes get flushed back before the
212 * new cpu enters */
213 kvm_cpu_synchronize_state(cs);
215 env->msr = (1ULL << MSR_SF) | (1ULL << MSR_ME);
216 env->nip = start;
217 env->gpr[3] = r3;
218 cs->halted = 0;
219 spapr_cpu_set_endianness(cpu);
220 spapr_cpu_update_tb_offset(cpu);
222 qemu_cpu_kick(cs);
224 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
225 return;
228 /* Didn't find a matching cpu */
229 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
232 static void rtas_stop_self(PowerPCCPU *cpu, sPAPRMachineState *spapr,
233 uint32_t token, uint32_t nargs,
234 target_ulong args,
235 uint32_t nret, target_ulong rets)
237 CPUState *cs = CPU(cpu);
238 CPUPPCState *env = &cpu->env;
240 cs->halted = 1;
241 qemu_cpu_kick(cs);
243 * While stopping a CPU, the guest calls H_CPPR which
244 * effectively disables interrupts on XICS level.
245 * However decrementer interrupts in TCG can still
246 * wake the CPU up so here we disable interrupts in MSR
247 * as well.
248 * As rtas_start_cpu() resets the whole MSR anyway, there is
249 * no need to bother with specific bits, we just clear it.
251 env->msr = 0;
254 static inline int sysparm_st(target_ulong addr, target_ulong len,
255 const void *val, uint16_t vallen)
257 hwaddr phys = ppc64_phys_to_real(addr);
259 if (len < 2) {
260 return RTAS_OUT_SYSPARM_PARAM_ERROR;
262 stw_be_phys(&address_space_memory, phys, vallen);
263 cpu_physical_memory_write(phys + 2, val, MIN(len - 2, vallen));
264 return RTAS_OUT_SUCCESS;
267 static void rtas_ibm_get_system_parameter(PowerPCCPU *cpu,
268 sPAPRMachineState *spapr,
269 uint32_t token, uint32_t nargs,
270 target_ulong args,
271 uint32_t nret, target_ulong rets)
273 target_ulong parameter = rtas_ld(args, 0);
274 target_ulong buffer = rtas_ld(args, 1);
275 target_ulong length = rtas_ld(args, 2);
276 target_ulong ret;
278 switch (parameter) {
279 case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS: {
280 char *param_val = g_strdup_printf("MaxEntCap=%d,"
281 "DesMem=%llu,"
282 "DesProcs=%d,"
283 "MaxPlatProcs=%d",
284 max_cpus,
285 current_machine->ram_size / M_BYTE,
286 smp_cpus,
287 max_cpus);
288 ret = sysparm_st(buffer, length, param_val, strlen(param_val) + 1);
289 g_free(param_val);
290 break;
292 case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE: {
293 uint8_t param_val = DIAGNOSTICS_RUN_MODE_DISABLED;
295 ret = sysparm_st(buffer, length, &param_val, sizeof(param_val));
296 break;
298 case RTAS_SYSPARM_UUID:
299 ret = sysparm_st(buffer, length, (unsigned char *)&qemu_uuid,
300 (qemu_uuid_set ? 16 : 0));
301 break;
302 default:
303 ret = RTAS_OUT_NOT_SUPPORTED;
306 rtas_st(rets, 0, ret);
309 static void rtas_ibm_set_system_parameter(PowerPCCPU *cpu,
310 sPAPRMachineState *spapr,
311 uint32_t token, uint32_t nargs,
312 target_ulong args,
313 uint32_t nret, target_ulong rets)
315 target_ulong parameter = rtas_ld(args, 0);
316 target_ulong ret = RTAS_OUT_NOT_SUPPORTED;
318 switch (parameter) {
319 case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS:
320 case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE:
321 case RTAS_SYSPARM_UUID:
322 ret = RTAS_OUT_NOT_AUTHORIZED;
323 break;
326 rtas_st(rets, 0, ret);
329 static void rtas_ibm_os_term(PowerPCCPU *cpu,
330 sPAPRMachineState *spapr,
331 uint32_t token, uint32_t nargs,
332 target_ulong args,
333 uint32_t nret, target_ulong rets)
335 target_ulong ret = 0;
337 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE, &error_abort);
339 rtas_st(rets, 0, ret);
342 static void rtas_set_power_level(PowerPCCPU *cpu, sPAPRMachineState *spapr,
343 uint32_t token, uint32_t nargs,
344 target_ulong args, uint32_t nret,
345 target_ulong rets)
347 int32_t power_domain;
349 if (nargs != 2 || nret != 2) {
350 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
351 return;
354 /* we currently only use a single, "live insert" powerdomain for
355 * hotplugged/dlpar'd resources, so the power is always live/full (100)
357 power_domain = rtas_ld(args, 0);
358 if (power_domain != -1) {
359 rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
360 return;
363 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
364 rtas_st(rets, 1, 100);
367 static void rtas_get_power_level(PowerPCCPU *cpu, sPAPRMachineState *spapr,
368 uint32_t token, uint32_t nargs,
369 target_ulong args, uint32_t nret,
370 target_ulong rets)
372 int32_t power_domain;
374 if (nargs != 1 || nret != 2) {
375 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
376 return;
379 /* we currently only use a single, "live insert" powerdomain for
380 * hotplugged/dlpar'd resources, so the power is always live/full (100)
382 power_domain = rtas_ld(args, 0);
383 if (power_domain != -1) {
384 rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
385 return;
388 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
389 rtas_st(rets, 1, 100);
392 static bool sensor_type_is_dr(uint32_t sensor_type)
394 switch (sensor_type) {
395 case RTAS_SENSOR_TYPE_ISOLATION_STATE:
396 case RTAS_SENSOR_TYPE_DR:
397 case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
398 return true;
401 return false;
404 static void rtas_set_indicator(PowerPCCPU *cpu, sPAPRMachineState *spapr,
405 uint32_t token, uint32_t nargs,
406 target_ulong args, uint32_t nret,
407 target_ulong rets)
409 uint32_t sensor_type;
410 uint32_t sensor_index;
411 uint32_t sensor_state;
412 uint32_t ret = RTAS_OUT_SUCCESS;
413 sPAPRDRConnector *drc;
414 sPAPRDRConnectorClass *drck;
416 if (nargs != 3 || nret != 1) {
417 ret = RTAS_OUT_PARAM_ERROR;
418 goto out;
421 sensor_type = rtas_ld(args, 0);
422 sensor_index = rtas_ld(args, 1);
423 sensor_state = rtas_ld(args, 2);
425 if (!sensor_type_is_dr(sensor_type)) {
426 goto out_unimplemented;
429 /* if this is a DR sensor we can assume sensor_index == drc_index */
430 drc = spapr_dr_connector_by_index(sensor_index);
431 if (!drc) {
432 trace_spapr_rtas_set_indicator_invalid(sensor_index);
433 ret = RTAS_OUT_PARAM_ERROR;
434 goto out;
436 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
438 switch (sensor_type) {
439 case RTAS_SENSOR_TYPE_ISOLATION_STATE:
440 /* if the guest is configuring a device attached to this
441 * DRC, we should reset the configuration state at this
442 * point since it may no longer be reliable (guest released
443 * device and needs to start over, or unplug occurred so
444 * the FDT is no longer valid)
446 if (sensor_state == SPAPR_DR_ISOLATION_STATE_ISOLATED) {
447 sPAPRConfigureConnectorState *ccs = spapr_ccs_find(spapr,
448 sensor_index);
449 if (ccs) {
450 spapr_ccs_remove(spapr, ccs);
453 ret = drck->set_isolation_state(drc, sensor_state);
454 break;
455 case RTAS_SENSOR_TYPE_DR:
456 ret = drck->set_indicator_state(drc, sensor_state);
457 break;
458 case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
459 ret = drck->set_allocation_state(drc, sensor_state);
460 break;
461 default:
462 goto out_unimplemented;
465 out:
466 rtas_st(rets, 0, ret);
467 return;
469 out_unimplemented:
470 /* currently only DR-related sensors are implemented */
471 trace_spapr_rtas_set_indicator_not_supported(sensor_index, sensor_type);
472 rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
475 static void rtas_get_sensor_state(PowerPCCPU *cpu, sPAPRMachineState *spapr,
476 uint32_t token, uint32_t nargs,
477 target_ulong args, uint32_t nret,
478 target_ulong rets)
480 uint32_t sensor_type;
481 uint32_t sensor_index;
482 uint32_t sensor_state = 0;
483 sPAPRDRConnector *drc;
484 sPAPRDRConnectorClass *drck;
485 uint32_t ret = RTAS_OUT_SUCCESS;
487 if (nargs != 2 || nret != 2) {
488 ret = RTAS_OUT_PARAM_ERROR;
489 goto out;
492 sensor_type = rtas_ld(args, 0);
493 sensor_index = rtas_ld(args, 1);
495 if (sensor_type != RTAS_SENSOR_TYPE_ENTITY_SENSE) {
496 /* currently only DR-related sensors are implemented */
497 trace_spapr_rtas_get_sensor_state_not_supported(sensor_index,
498 sensor_type);
499 ret = RTAS_OUT_NOT_SUPPORTED;
500 goto out;
503 drc = spapr_dr_connector_by_index(sensor_index);
504 if (!drc) {
505 trace_spapr_rtas_get_sensor_state_invalid(sensor_index);
506 ret = RTAS_OUT_PARAM_ERROR;
507 goto out;
509 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
510 ret = drck->entity_sense(drc, &sensor_state);
512 out:
513 rtas_st(rets, 0, ret);
514 rtas_st(rets, 1, sensor_state);
517 /* configure-connector work area offsets, int32_t units for field
518 * indexes, bytes for field offset/len values.
520 * as documented by PAPR+ v2.7, 13.5.3.5
522 #define CC_IDX_NODE_NAME_OFFSET 2
523 #define CC_IDX_PROP_NAME_OFFSET 2
524 #define CC_IDX_PROP_LEN 3
525 #define CC_IDX_PROP_DATA_OFFSET 4
526 #define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4)
527 #define CC_WA_LEN 4096
529 static void configure_connector_st(target_ulong addr, target_ulong offset,
530 const void *buf, size_t len)
532 cpu_physical_memory_write(ppc64_phys_to_real(addr + offset),
533 buf, MIN(len, CC_WA_LEN - offset));
536 static void rtas_ibm_configure_connector(PowerPCCPU *cpu,
537 sPAPRMachineState *spapr,
538 uint32_t token, uint32_t nargs,
539 target_ulong args, uint32_t nret,
540 target_ulong rets)
542 uint64_t wa_addr;
543 uint64_t wa_offset;
544 uint32_t drc_index;
545 sPAPRDRConnector *drc;
546 sPAPRDRConnectorClass *drck;
547 sPAPRConfigureConnectorState *ccs;
548 sPAPRDRCCResponse resp = SPAPR_DR_CC_RESPONSE_CONTINUE;
549 int rc;
550 const void *fdt;
552 if (nargs != 2 || nret != 1) {
553 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
554 return;
557 wa_addr = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 0);
559 drc_index = rtas_ld(wa_addr, 0);
560 drc = spapr_dr_connector_by_index(drc_index);
561 if (!drc) {
562 trace_spapr_rtas_ibm_configure_connector_invalid(drc_index);
563 rc = RTAS_OUT_PARAM_ERROR;
564 goto out;
567 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
568 fdt = drck->get_fdt(drc, NULL);
569 if (!fdt) {
570 trace_spapr_rtas_ibm_configure_connector_missing_fdt(drc_index);
571 rc = SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE;
572 goto out;
575 ccs = spapr_ccs_find(spapr, drc_index);
576 if (!ccs) {
577 ccs = g_new0(sPAPRConfigureConnectorState, 1);
578 (void)drck->get_fdt(drc, &ccs->fdt_offset);
579 ccs->drc_index = drc_index;
580 spapr_ccs_add(spapr, ccs);
583 do {
584 uint32_t tag;
585 const char *name;
586 const struct fdt_property *prop;
587 int fdt_offset_next, prop_len;
589 tag = fdt_next_tag(fdt, ccs->fdt_offset, &fdt_offset_next);
591 switch (tag) {
592 case FDT_BEGIN_NODE:
593 ccs->fdt_depth++;
594 name = fdt_get_name(fdt, ccs->fdt_offset, NULL);
596 /* provide the name of the next OF node */
597 wa_offset = CC_VAL_DATA_OFFSET;
598 rtas_st(wa_addr, CC_IDX_NODE_NAME_OFFSET, wa_offset);
599 configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
600 resp = SPAPR_DR_CC_RESPONSE_NEXT_CHILD;
601 break;
602 case FDT_END_NODE:
603 ccs->fdt_depth--;
604 if (ccs->fdt_depth == 0) {
605 /* done sending the device tree, don't need to track
606 * the state anymore
608 drck->set_configured(drc);
609 spapr_ccs_remove(spapr, ccs);
610 ccs = NULL;
611 resp = SPAPR_DR_CC_RESPONSE_SUCCESS;
612 } else {
613 resp = SPAPR_DR_CC_RESPONSE_PREV_PARENT;
615 break;
616 case FDT_PROP:
617 prop = fdt_get_property_by_offset(fdt, ccs->fdt_offset,
618 &prop_len);
619 name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
621 /* provide the name of the next OF property */
622 wa_offset = CC_VAL_DATA_OFFSET;
623 rtas_st(wa_addr, CC_IDX_PROP_NAME_OFFSET, wa_offset);
624 configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
626 /* provide the length and value of the OF property. data gets
627 * placed immediately after NULL terminator of the OF property's
628 * name string
630 wa_offset += strlen(name) + 1,
631 rtas_st(wa_addr, CC_IDX_PROP_LEN, prop_len);
632 rtas_st(wa_addr, CC_IDX_PROP_DATA_OFFSET, wa_offset);
633 configure_connector_st(wa_addr, wa_offset, prop->data, prop_len);
634 resp = SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY;
635 break;
636 case FDT_END:
637 resp = SPAPR_DR_CC_RESPONSE_ERROR;
638 default:
639 /* keep seeking for an actionable tag */
640 break;
642 if (ccs) {
643 ccs->fdt_offset = fdt_offset_next;
645 } while (resp == SPAPR_DR_CC_RESPONSE_CONTINUE);
647 rc = resp;
648 out:
649 rtas_st(rets, 0, rc);
652 static struct rtas_call {
653 const char *name;
654 spapr_rtas_fn fn;
655 } rtas_table[RTAS_TOKEN_MAX - RTAS_TOKEN_BASE];
657 target_ulong spapr_rtas_call(PowerPCCPU *cpu, sPAPRMachineState *spapr,
658 uint32_t token, uint32_t nargs, target_ulong args,
659 uint32_t nret, target_ulong rets)
661 if ((token >= RTAS_TOKEN_BASE) && (token < RTAS_TOKEN_MAX)) {
662 struct rtas_call *call = rtas_table + (token - RTAS_TOKEN_BASE);
664 if (call->fn) {
665 call->fn(cpu, spapr, token, nargs, args, nret, rets);
666 return H_SUCCESS;
670 /* HACK: Some Linux early debug code uses RTAS display-character,
671 * but assumes the token value is 0xa (which it is on some real
672 * machines) without looking it up in the device tree. This
673 * special case makes this work */
674 if (token == 0xa) {
675 rtas_display_character(cpu, spapr, 0xa, nargs, args, nret, rets);
676 return H_SUCCESS;
679 hcall_dprintf("Unknown RTAS token 0x%x\n", token);
680 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
681 return H_PARAMETER;
684 uint64_t qtest_rtas_call(char *cmd, uint32_t nargs, uint64_t args,
685 uint32_t nret, uint64_t rets)
687 int token;
689 for (token = 0; token < RTAS_TOKEN_MAX - RTAS_TOKEN_BASE; token++) {
690 if (strcmp(cmd, rtas_table[token].name) == 0) {
691 sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
692 PowerPCCPU *cpu = POWERPC_CPU(first_cpu);
694 rtas_table[token].fn(cpu, spapr, token + RTAS_TOKEN_BASE,
695 nargs, args, nret, rets);
696 return H_SUCCESS;
699 return H_PARAMETER;
702 void spapr_rtas_register(int token, const char *name, spapr_rtas_fn fn)
704 assert((token >= RTAS_TOKEN_BASE) && (token < RTAS_TOKEN_MAX));
706 token -= RTAS_TOKEN_BASE;
708 assert(!rtas_table[token].name);
710 rtas_table[token].name = name;
711 rtas_table[token].fn = fn;
714 void spapr_dt_rtas_tokens(void *fdt, int rtas)
716 int i;
718 for (i = 0; i < RTAS_TOKEN_MAX - RTAS_TOKEN_BASE; i++) {
719 struct rtas_call *call = &rtas_table[i];
721 if (!call->name) {
722 continue;
725 _FDT(fdt_setprop_cell(fdt, rtas, call->name, i + RTAS_TOKEN_BASE));
729 void spapr_load_rtas(sPAPRMachineState *spapr, void *fdt, hwaddr addr)
731 int rtas_node;
732 int ret;
734 /* Copy RTAS blob into guest RAM */
735 cpu_physical_memory_write(addr, spapr->rtas_blob, spapr->rtas_size);
737 ret = fdt_add_mem_rsv(fdt, addr, spapr->rtas_size);
738 if (ret < 0) {
739 error_report("Couldn't add RTAS reserve entry: %s",
740 fdt_strerror(ret));
741 exit(1);
744 /* Update the device tree with the blob's location */
745 rtas_node = fdt_path_offset(fdt, "/rtas");
746 assert(rtas_node >= 0);
748 ret = fdt_setprop_cell(fdt, rtas_node, "linux,rtas-base", addr);
749 if (ret < 0) {
750 error_report("Couldn't add linux,rtas-base property: %s",
751 fdt_strerror(ret));
752 exit(1);
755 ret = fdt_setprop_cell(fdt, rtas_node, "linux,rtas-entry", addr);
756 if (ret < 0) {
757 error_report("Couldn't add linux,rtas-entry property: %s",
758 fdt_strerror(ret));
759 exit(1);
762 ret = fdt_setprop_cell(fdt, rtas_node, "rtas-size", spapr->rtas_size);
763 if (ret < 0) {
764 error_report("Couldn't add rtas-size property: %s",
765 fdt_strerror(ret));
766 exit(1);
770 static void core_rtas_register_types(void)
772 spapr_rtas_register(RTAS_DISPLAY_CHARACTER, "display-character",
773 rtas_display_character);
774 spapr_rtas_register(RTAS_POWER_OFF, "power-off", rtas_power_off);
775 spapr_rtas_register(RTAS_SYSTEM_REBOOT, "system-reboot",
776 rtas_system_reboot);
777 spapr_rtas_register(RTAS_QUERY_CPU_STOPPED_STATE, "query-cpu-stopped-state",
778 rtas_query_cpu_stopped_state);
779 spapr_rtas_register(RTAS_START_CPU, "start-cpu", rtas_start_cpu);
780 spapr_rtas_register(RTAS_STOP_SELF, "stop-self", rtas_stop_self);
781 spapr_rtas_register(RTAS_IBM_GET_SYSTEM_PARAMETER,
782 "ibm,get-system-parameter",
783 rtas_ibm_get_system_parameter);
784 spapr_rtas_register(RTAS_IBM_SET_SYSTEM_PARAMETER,
785 "ibm,set-system-parameter",
786 rtas_ibm_set_system_parameter);
787 spapr_rtas_register(RTAS_IBM_OS_TERM, "ibm,os-term",
788 rtas_ibm_os_term);
789 spapr_rtas_register(RTAS_SET_POWER_LEVEL, "set-power-level",
790 rtas_set_power_level);
791 spapr_rtas_register(RTAS_GET_POWER_LEVEL, "get-power-level",
792 rtas_get_power_level);
793 spapr_rtas_register(RTAS_SET_INDICATOR, "set-indicator",
794 rtas_set_indicator);
795 spapr_rtas_register(RTAS_GET_SENSOR_STATE, "get-sensor-state",
796 rtas_get_sensor_state);
797 spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR, "ibm,configure-connector",
798 rtas_ibm_configure_connector);
801 type_init(core_rtas_register_types)