xref: /qemu/hw/ppc/spapr_vof.c (revision fc8c745d50150a63f6c5ba2cd0b83b430963b7e8)
1 /*
2  * SPAPR machine hooks to Virtual Open Firmware,
3  *
4  * SPDX-License-Identifier: GPL-2.0-or-later
5  */
6 #include "qemu/osdep.h"
7 #include "qemu-common.h"
8 #include "qapi/error.h"
9 #include "hw/ppc/spapr.h"
10 #include "hw/ppc/spapr_vio.h"
11 #include "hw/ppc/fdt.h"
12 #include "hw/ppc/vof.h"
13 #include "sysemu/sysemu.h"
14 #include "qom/qom-qobject.h"
15 #include "trace.h"
16 
17 target_ulong spapr_h_vof_client(PowerPCCPU *cpu, SpaprMachineState *spapr,
18                                 target_ulong opcode, target_ulong *_args)
19 {
20     int ret = vof_client_call(MACHINE(spapr), spapr->vof, spapr->fdt_blob,
21                               ppc64_phys_to_real(_args[0]));
22 
23     if (ret) {
24         return H_PARAMETER;
25     }
26     return H_SUCCESS;
27 }
28 
29 void spapr_vof_client_dt_finalize(SpaprMachineState *spapr, void *fdt)
30 {
31     char *stdout_path = spapr_vio_stdout_path(spapr->vio_bus);
32     int chosen;
33 
34     vof_build_dt(fdt, spapr->vof);
35 
36     _FDT(chosen = fdt_path_offset(fdt, "/chosen"));
37     _FDT(fdt_setprop_string(fdt, chosen, "bootargs",
38                             spapr->vof->bootargs ? : ""));
39 
40     /*
41      * SLOF-less setup requires an open instance of stdout for early
42      * kernel printk. By now all phandles are settled so we can open
43      * the default serial console.
44      */
45     if (stdout_path) {
46         _FDT(vof_client_open_store(fdt, spapr->vof, "/chosen", "stdout",
47                                    stdout_path));
48     }
49 }
50 
51 void spapr_vof_reset(SpaprMachineState *spapr, void *fdt,
52                      target_ulong *stack_ptr, Error **errp)
53 {
54     Vof *vof = spapr->vof;
55 
56     vof_init(vof, spapr->rma_size, errp);
57 
58     *stack_ptr = vof_claim(vof, 0, VOF_STACK_SIZE, VOF_STACK_SIZE);
59     if (*stack_ptr == -1) {
60         error_setg(errp, "Memory allocation for stack failed");
61         return;
62     }
63     /* Stack grows downwards plus reserve space for the minimum stack frame */
64     *stack_ptr += VOF_STACK_SIZE - 0x20;
65 
66     if (spapr->kernel_size &&
67         vof_claim(vof, spapr->kernel_addr, spapr->kernel_size, 0) == -1) {
68         error_setg(errp, "Memory for kernel is in use");
69         return;
70     }
71 
72     if (spapr->initrd_size &&
73         vof_claim(vof, spapr->initrd_base, spapr->initrd_size, 0) == -1) {
74         error_setg(errp, "Memory for initramdisk is in use");
75         return;
76     }
77 
78     spapr_vof_client_dt_finalize(spapr, fdt);
79 
80     /*
81      * At this point the expected allocation map is:
82      *
83      * 0..c38 - the initial firmware
84      * 8000..10000 - stack
85      * 400000.. - kernel
86      * 3ea0000.. - initramdisk
87      *
88      * We skip writing FDT as nothing expects it; OF client interface is
89      * going to be used for reading the device tree.
90      */
91 }
92 
93 void spapr_vof_quiesce(MachineState *ms)
94 {
95     SpaprMachineState *spapr = SPAPR_MACHINE(ms);
96 
97     spapr->fdt_size = fdt_totalsize(spapr->fdt_blob);
98     spapr->fdt_initial_size = spapr->fdt_size;
99 }
100 
101 bool spapr_vof_setprop(MachineState *ms, const char *path, const char *propname,
102                        void *val, int vallen)
103 {
104     SpaprMachineState *spapr = SPAPR_MACHINE(ms);
105 
106     /*
107      * We only allow changing properties which we know how to update in QEMU
108      * OR
109      * the ones which we know that they need to survive during "quiesce".
110      */
111 
112     if (strcmp(path, "/rtas") == 0) {
113         if (strcmp(propname, "linux,rtas-base") == 0 ||
114             strcmp(propname, "linux,rtas-entry") == 0) {
115             /* These need to survive quiesce so let them store in the FDT */
116             return true;
117         }
118     }
119 
120     if (strcmp(path, "/chosen") == 0) {
121         if (strcmp(propname, "bootargs") == 0) {
122             Vof *vof = spapr->vof;
123 
124             g_free(vof->bootargs);
125             vof->bootargs = g_strndup(val, vallen);
126             return true;
127         }
128         if (strcmp(propname, "linux,initrd-start") == 0) {
129             if (vallen == sizeof(uint32_t)) {
130                 spapr->initrd_base = ldl_be_p(val);
131                 return true;
132             }
133             if (vallen == sizeof(uint64_t)) {
134                 spapr->initrd_base = ldq_be_p(val);
135                 return true;
136             }
137             return false;
138         }
139         if (strcmp(propname, "linux,initrd-end") == 0) {
140             if (vallen == sizeof(uint32_t)) {
141                 spapr->initrd_size = ldl_be_p(val) - spapr->initrd_base;
142                 return true;
143             }
144             if (vallen == sizeof(uint64_t)) {
145                 spapr->initrd_size = ldq_be_p(val) - spapr->initrd_base;
146                 return true;
147             }
148             return false;
149         }
150     }
151 
152     return true;
153 }
154