]> git.proxmox.com Git - mirror_qemu.git/blame - hw/ppc/spapr_hcall.c
shutdown: Add source information to SHUTDOWN and RESET
[mirror_qemu.git] / hw / ppc / spapr_hcall.c
CommitLineData
0d75590d 1#include "qemu/osdep.h"
da34e65c 2#include "qapi/error.h"
b3946626 3#include "sysemu/hw_accel.h"
9c17d615 4#include "sysemu/sysemu.h"
03dd024f 5#include "qemu/log.h"
9fdf0c29 6#include "cpu.h"
63c91552 7#include "exec/exec-all.h"
ed120055 8#include "helper_regs.h"
0d09e41a 9#include "hw/ppc/spapr.h"
d5aea6f3 10#include "mmu-hash64.h"
3794d548
AK
11#include "cpu-models.h"
12#include "trace.h"
13#include "kvm_ppc.h"
facdb8b6 14#include "hw/ppc/spapr_ovec.h"
b4db5413
SJS
15#include "qemu/error-report.h"
16#include "mmu-book3s-v3.h"
f43e3525 17
a46622fd 18struct SPRSyncState {
a46622fd
AK
19 int spr;
20 target_ulong value;
21 target_ulong mask;
22};
23
14e6fe12 24static void do_spr_sync(CPUState *cs, run_on_cpu_data arg)
a46622fd 25{
14e6fe12 26 struct SPRSyncState *s = arg.host_ptr;
e0eeb4a2 27 PowerPCCPU *cpu = POWERPC_CPU(cs);
a46622fd
AK
28 CPUPPCState *env = &cpu->env;
29
e0eeb4a2 30 cpu_synchronize_state(cs);
a46622fd
AK
31 env->spr[s->spr] &= ~s->mask;
32 env->spr[s->spr] |= s->value;
33}
34
35static void set_spr(CPUState *cs, int spr, target_ulong value,
36 target_ulong mask)
37{
38 struct SPRSyncState s = {
a46622fd
AK
39 .spr = spr,
40 .value = value,
41 .mask = mask
42 };
14e6fe12 43 run_on_cpu(cs, do_spr_sync, RUN_ON_CPU_HOST_PTR(&s));
a46622fd
AK
44}
45
af08a58f
TH
46static bool has_spr(PowerPCCPU *cpu, int spr)
47{
48 /* We can test whether the SPR is defined by checking for a valid name */
49 return cpu->env.spr_cb[spr].name != NULL;
50}
51
c6404ade 52static inline bool valid_ptex(PowerPCCPU *cpu, target_ulong ptex)
f3c75d42
AK
53{
54 /*
36778660 55 * hash value/pteg group index is normalized by HPT mask
f3c75d42 56 */
36778660 57 if (((ptex & ~7ULL) / HPTES_PER_GROUP) & ~ppc_hash64_hpt_mask(cpu)) {
f3c75d42
AK
58 return false;
59 }
60 return true;
61}
62
ecbc25fa
DG
63static bool is_ram_address(sPAPRMachineState *spapr, hwaddr addr)
64{
65 MachineState *machine = MACHINE(spapr);
66 MemoryHotplugState *hpms = &spapr->hotplug_memory;
67
68 if (addr < machine->ram_size) {
69 return true;
70 }
71 if ((addr >= hpms->base)
72 && ((addr - hpms->base) < memory_region_size(&hpms->mr))) {
73 return true;
74 }
75
76 return false;
77}
78
28e02042 79static target_ulong h_enter(PowerPCCPU *cpu, sPAPRMachineState *spapr,
f43e3525
DG
80 target_ulong opcode, target_ulong *args)
81{
82 target_ulong flags = args[0];
c6404ade 83 target_ulong ptex = args[1];
f43e3525
DG
84 target_ulong pteh = args[2];
85 target_ulong ptel = args[3];
1f0252e6 86 unsigned apshift;
f73a2575 87 target_ulong raddr;
c6404ade 88 target_ulong slot;
7222b94a 89 const ppc_hash_pte64_t *hptes;
f43e3525 90
1f0252e6 91 apshift = ppc_hash64_hpte_page_shift_noslb(cpu, pteh, ptel);
1114e712
DG
92 if (!apshift) {
93 /* Bad page size encoding */
94 return H_PARAMETER;
f43e3525
DG
95 }
96
1114e712 97 raddr = (ptel & HPTE64_R_RPN) & ~((1ULL << apshift) - 1);
f43e3525 98
ecbc25fa 99 if (is_ram_address(spapr, raddr)) {
f73a2575 100 /* Regular RAM - should have WIMG=0010 */
d5aea6f3 101 if ((ptel & HPTE64_R_WIMG) != HPTE64_R_M) {
f73a2575
DG
102 return H_PARAMETER;
103 }
104 } else {
c1175907 105 target_ulong wimg_flags;
f73a2575
DG
106 /* Looks like an IO address */
107 /* FIXME: What WIMG combinations could be sensible for IO?
108 * For now we allow WIMG=010x, but are there others? */
109 /* FIXME: Should we check against registered IO addresses? */
c1175907
AK
110 wimg_flags = (ptel & (HPTE64_R_W | HPTE64_R_I | HPTE64_R_M));
111
112 if (wimg_flags != HPTE64_R_I &&
113 wimg_flags != (HPTE64_R_I | HPTE64_R_M)) {
f73a2575
DG
114 return H_PARAMETER;
115 }
f43e3525 116 }
f73a2575 117
f43e3525
DG
118 pteh &= ~0x60ULL;
119
c6404ade 120 if (!valid_ptex(cpu, ptex)) {
f43e3525
DG
121 return H_PARAMETER;
122 }
7c43bca0 123
c6404ade
DG
124 slot = ptex & 7ULL;
125 ptex = ptex & ~7ULL;
126
f43e3525 127 if (likely((flags & H_EXACT) == 0)) {
7222b94a 128 hptes = ppc_hash64_map_hptes(cpu, ptex, HPTES_PER_GROUP);
c6404ade 129 for (slot = 0; slot < 8; slot++) {
7222b94a 130 if (!(ppc_hash64_hpte0(cpu, hptes, slot) & HPTE64_V_VALID)) {
f43e3525
DG
131 break;
132 }
7aaf4957 133 }
7222b94a 134 ppc_hash64_unmap_hptes(cpu, hptes, ptex, HPTES_PER_GROUP);
c6404ade 135 if (slot == 8) {
7aaf4957
AK
136 return H_PTEG_FULL;
137 }
f43e3525 138 } else {
7222b94a
DG
139 hptes = ppc_hash64_map_hptes(cpu, ptex + slot, 1);
140 if (ppc_hash64_hpte0(cpu, hptes, 0) & HPTE64_V_VALID) {
141 ppc_hash64_unmap_hptes(cpu, hptes, ptex + slot, 1);
f43e3525
DG
142 return H_PTEG_FULL;
143 }
7222b94a 144 ppc_hash64_unmap_hptes(cpu, hptes, ptex, 1);
f43e3525 145 }
7c43bca0 146
c6404ade 147 ppc_hash64_store_hpte(cpu, ptex + slot, pteh | HPTE64_V_HPTE_DIRTY, ptel);
f43e3525 148
c6404ade 149 args[0] = ptex + slot;
f43e3525
DG
150 return H_SUCCESS;
151}
152
a3801402 153typedef enum {
a3d0abae
DG
154 REMOVE_SUCCESS = 0,
155 REMOVE_NOT_FOUND = 1,
156 REMOVE_PARM = 2,
157 REMOVE_HW = 3,
a3801402 158} RemoveResult;
a3d0abae 159
7ef23068 160static RemoveResult remove_hpte(PowerPCCPU *cpu, target_ulong ptex,
a3d0abae
DG
161 target_ulong avpn,
162 target_ulong flags,
163 target_ulong *vp, target_ulong *rp)
f43e3525 164{
7222b94a 165 const ppc_hash_pte64_t *hptes;
61a36c9b 166 target_ulong v, r;
f43e3525 167
c6404ade 168 if (!valid_ptex(cpu, ptex)) {
a3d0abae 169 return REMOVE_PARM;
f43e3525
DG
170 }
171
7222b94a
DG
172 hptes = ppc_hash64_map_hptes(cpu, ptex, 1);
173 v = ppc_hash64_hpte0(cpu, hptes, 0);
174 r = ppc_hash64_hpte1(cpu, hptes, 0);
175 ppc_hash64_unmap_hptes(cpu, hptes, ptex, 1);
f43e3525 176
d5aea6f3 177 if ((v & HPTE64_V_VALID) == 0 ||
f43e3525
DG
178 ((flags & H_AVPN) && (v & ~0x7fULL) != avpn) ||
179 ((flags & H_ANDCOND) && (v & avpn) != 0)) {
a3d0abae 180 return REMOVE_NOT_FOUND;
f43e3525 181 }
35f9304d 182 *vp = v;
a3d0abae 183 *rp = r;
7ef23068 184 ppc_hash64_store_hpte(cpu, ptex, HPTE64_V_HPTE_DIRTY, 0);
61a36c9b 185 ppc_hash64_tlb_flush_hpte(cpu, ptex, v, r);
a3d0abae
DG
186 return REMOVE_SUCCESS;
187}
188
28e02042 189static target_ulong h_remove(PowerPCCPU *cpu, sPAPRMachineState *spapr,
a3d0abae
DG
190 target_ulong opcode, target_ulong *args)
191{
cd0c6f47 192 CPUPPCState *env = &cpu->env;
a3d0abae 193 target_ulong flags = args[0];
c6404ade 194 target_ulong ptex = args[1];
a3d0abae 195 target_ulong avpn = args[2];
a3801402 196 RemoveResult ret;
a3d0abae 197
c6404ade 198 ret = remove_hpte(cpu, ptex, avpn, flags,
a3d0abae
DG
199 &args[0], &args[1]);
200
201 switch (ret) {
202 case REMOVE_SUCCESS:
e3cffe6f 203 check_tlb_flush(env, true);
a3d0abae
DG
204 return H_SUCCESS;
205
206 case REMOVE_NOT_FOUND:
207 return H_NOT_FOUND;
208
209 case REMOVE_PARM:
210 return H_PARAMETER;
211
212 case REMOVE_HW:
213 return H_HARDWARE;
214 }
215
9a39970d 216 g_assert_not_reached();
a3d0abae
DG
217}
218
219#define H_BULK_REMOVE_TYPE 0xc000000000000000ULL
220#define H_BULK_REMOVE_REQUEST 0x4000000000000000ULL
221#define H_BULK_REMOVE_RESPONSE 0x8000000000000000ULL
222#define H_BULK_REMOVE_END 0xc000000000000000ULL
223#define H_BULK_REMOVE_CODE 0x3000000000000000ULL
224#define H_BULK_REMOVE_SUCCESS 0x0000000000000000ULL
225#define H_BULK_REMOVE_NOT_FOUND 0x1000000000000000ULL
226#define H_BULK_REMOVE_PARM 0x2000000000000000ULL
227#define H_BULK_REMOVE_HW 0x3000000000000000ULL
228#define H_BULK_REMOVE_RC 0x0c00000000000000ULL
229#define H_BULK_REMOVE_FLAGS 0x0300000000000000ULL
230#define H_BULK_REMOVE_ABSOLUTE 0x0000000000000000ULL
231#define H_BULK_REMOVE_ANDCOND 0x0100000000000000ULL
232#define H_BULK_REMOVE_AVPN 0x0200000000000000ULL
233#define H_BULK_REMOVE_PTEX 0x00ffffffffffffffULL
234
235#define H_BULK_REMOVE_MAX_BATCH 4
236
28e02042 237static target_ulong h_bulk_remove(PowerPCCPU *cpu, sPAPRMachineState *spapr,
a3d0abae
DG
238 target_ulong opcode, target_ulong *args)
239{
cd0c6f47 240 CPUPPCState *env = &cpu->env;
a3d0abae 241 int i;
cd0c6f47 242 target_ulong rc = H_SUCCESS;
a3d0abae
DG
243
244 for (i = 0; i < H_BULK_REMOVE_MAX_BATCH; i++) {
245 target_ulong *tsh = &args[i*2];
246 target_ulong tsl = args[i*2 + 1];
247 target_ulong v, r, ret;
248
249 if ((*tsh & H_BULK_REMOVE_TYPE) == H_BULK_REMOVE_END) {
250 break;
251 } else if ((*tsh & H_BULK_REMOVE_TYPE) != H_BULK_REMOVE_REQUEST) {
252 return H_PARAMETER;
253 }
254
255 *tsh &= H_BULK_REMOVE_PTEX | H_BULK_REMOVE_FLAGS;
256 *tsh |= H_BULK_REMOVE_RESPONSE;
257
258 if ((*tsh & H_BULK_REMOVE_ANDCOND) && (*tsh & H_BULK_REMOVE_AVPN)) {
259 *tsh |= H_BULK_REMOVE_PARM;
260 return H_PARAMETER;
261 }
262
7ef23068 263 ret = remove_hpte(cpu, *tsh & H_BULK_REMOVE_PTEX, tsl,
a3d0abae
DG
264 (*tsh & H_BULK_REMOVE_FLAGS) >> 26,
265 &v, &r);
266
267 *tsh |= ret << 60;
268
269 switch (ret) {
270 case REMOVE_SUCCESS:
d5aea6f3 271 *tsh |= (r & (HPTE64_R_C | HPTE64_R_R)) << 43;
a3d0abae
DG
272 break;
273
274 case REMOVE_PARM:
cd0c6f47
BH
275 rc = H_PARAMETER;
276 goto exit;
a3d0abae
DG
277
278 case REMOVE_HW:
cd0c6f47
BH
279 rc = H_HARDWARE;
280 goto exit;
a3d0abae
DG
281 }
282 }
cd0c6f47 283 exit:
e3cffe6f 284 check_tlb_flush(env, true);
a3d0abae 285
cd0c6f47 286 return rc;
f43e3525
DG
287}
288
28e02042 289static target_ulong h_protect(PowerPCCPU *cpu, sPAPRMachineState *spapr,
f43e3525
DG
290 target_ulong opcode, target_ulong *args)
291{
b13ce26d 292 CPUPPCState *env = &cpu->env;
f43e3525 293 target_ulong flags = args[0];
c6404ade 294 target_ulong ptex = args[1];
f43e3525 295 target_ulong avpn = args[2];
7222b94a 296 const ppc_hash_pte64_t *hptes;
61a36c9b 297 target_ulong v, r;
f43e3525 298
c6404ade 299 if (!valid_ptex(cpu, ptex)) {
f43e3525
DG
300 return H_PARAMETER;
301 }
302
7222b94a
DG
303 hptes = ppc_hash64_map_hptes(cpu, ptex, 1);
304 v = ppc_hash64_hpte0(cpu, hptes, 0);
305 r = ppc_hash64_hpte1(cpu, hptes, 0);
306 ppc_hash64_unmap_hptes(cpu, hptes, ptex, 1);
f43e3525 307
d5aea6f3 308 if ((v & HPTE64_V_VALID) == 0 ||
f43e3525 309 ((flags & H_AVPN) && (v & ~0x7fULL) != avpn)) {
f43e3525
DG
310 return H_NOT_FOUND;
311 }
312
d5aea6f3
DG
313 r &= ~(HPTE64_R_PP0 | HPTE64_R_PP | HPTE64_R_N |
314 HPTE64_R_KEY_HI | HPTE64_R_KEY_LO);
315 r |= (flags << 55) & HPTE64_R_PP0;
316 r |= (flags << 48) & HPTE64_R_KEY_HI;
317 r |= flags & (HPTE64_R_PP | HPTE64_R_N | HPTE64_R_KEY_LO);
c6404ade 318 ppc_hash64_store_hpte(cpu, ptex,
3f94170b 319 (v & ~HPTE64_V_VALID) | HPTE64_V_HPTE_DIRTY, 0);
c6404ade 320 ppc_hash64_tlb_flush_hpte(cpu, ptex, v, r);
d76ab5e1
ND
321 /* Flush the tlb */
322 check_tlb_flush(env, true);
f43e3525 323 /* Don't need a memory barrier, due to qemu's global lock */
c6404ade 324 ppc_hash64_store_hpte(cpu, ptex, v | HPTE64_V_HPTE_DIRTY, r);
f43e3525
DG
325 return H_SUCCESS;
326}
327
28e02042 328static target_ulong h_read(PowerPCCPU *cpu, sPAPRMachineState *spapr,
6bbd5dde
EC
329 target_ulong opcode, target_ulong *args)
330{
6bbd5dde 331 target_ulong flags = args[0];
c6404ade 332 target_ulong ptex = args[1];
6bbd5dde
EC
333 uint8_t *hpte;
334 int i, ridx, n_entries = 1;
335
c6404ade 336 if (!valid_ptex(cpu, ptex)) {
6bbd5dde
EC
337 return H_PARAMETER;
338 }
339
340 if (flags & H_READ_4) {
341 /* Clear the two low order bits */
c6404ade 342 ptex &= ~(3ULL);
6bbd5dde
EC
343 n_entries = 4;
344 }
345
e57ca75c 346 hpte = spapr->htab + (ptex * HASH_PTE_SIZE_64);
6bbd5dde
EC
347
348 for (i = 0, ridx = 0; i < n_entries; i++) {
349 args[ridx++] = ldq_p(hpte);
350 args[ridx++] = ldq_p(hpte + (HASH_PTE_SIZE_64/2));
351 hpte += HASH_PTE_SIZE_64;
352 }
353
354 return H_SUCCESS;
355}
356
423576f7
TH
357static target_ulong h_set_sprg0(PowerPCCPU *cpu, sPAPRMachineState *spapr,
358 target_ulong opcode, target_ulong *args)
359{
360 cpu_synchronize_state(CPU(cpu));
361 cpu->env.spr[SPR_SPRG0] = args[0];
362
363 return H_SUCCESS;
364}
365
28e02042 366static target_ulong h_set_dabr(PowerPCCPU *cpu, sPAPRMachineState *spapr,
821303f5
DG
367 target_ulong opcode, target_ulong *args)
368{
af08a58f
TH
369 if (!has_spr(cpu, SPR_DABR)) {
370 return H_HARDWARE; /* DABR register not available */
371 }
372 cpu_synchronize_state(CPU(cpu));
373
374 if (has_spr(cpu, SPR_DABRX)) {
375 cpu->env.spr[SPR_DABRX] = 0x3; /* Use Problem and Privileged state */
376 } else if (!(args[0] & 0x4)) { /* Breakpoint Translation set? */
377 return H_RESERVED_DABR;
378 }
379
380 cpu->env.spr[SPR_DABR] = args[0];
381 return H_SUCCESS;
821303f5
DG
382}
383
e49ff266
TH
384static target_ulong h_set_xdabr(PowerPCCPU *cpu, sPAPRMachineState *spapr,
385 target_ulong opcode, target_ulong *args)
386{
387 target_ulong dabrx = args[1];
388
389 if (!has_spr(cpu, SPR_DABR) || !has_spr(cpu, SPR_DABRX)) {
390 return H_HARDWARE;
391 }
392
393 if ((dabrx & ~0xfULL) != 0 || (dabrx & H_DABRX_HYPERVISOR) != 0
394 || (dabrx & (H_DABRX_KERNEL | H_DABRX_USER)) == 0) {
395 return H_PARAMETER;
396 }
397
398 cpu_synchronize_state(CPU(cpu));
399 cpu->env.spr[SPR_DABRX] = dabrx;
400 cpu->env.spr[SPR_DABR] = args[0];
401
402 return H_SUCCESS;
403}
404
3240dd9a
TH
405static target_ulong h_page_init(PowerPCCPU *cpu, sPAPRMachineState *spapr,
406 target_ulong opcode, target_ulong *args)
407{
408 target_ulong flags = args[0];
409 hwaddr dst = args[1];
410 hwaddr src = args[2];
411 hwaddr len = TARGET_PAGE_SIZE;
412 uint8_t *pdst, *psrc;
413 target_long ret = H_SUCCESS;
414
415 if (flags & ~(H_ICACHE_SYNCHRONIZE | H_ICACHE_INVALIDATE
416 | H_COPY_PAGE | H_ZERO_PAGE)) {
417 qemu_log_mask(LOG_UNIMP, "h_page_init: Bad flags (" TARGET_FMT_lx "\n",
418 flags);
419 return H_PARAMETER;
420 }
421
422 /* Map-in destination */
423 if (!is_ram_address(spapr, dst) || (dst & ~TARGET_PAGE_MASK) != 0) {
424 return H_PARAMETER;
425 }
426 pdst = cpu_physical_memory_map(dst, &len, 1);
427 if (!pdst || len != TARGET_PAGE_SIZE) {
428 return H_PARAMETER;
429 }
430
431 if (flags & H_COPY_PAGE) {
432 /* Map-in source, copy to destination, and unmap source again */
433 if (!is_ram_address(spapr, src) || (src & ~TARGET_PAGE_MASK) != 0) {
434 ret = H_PARAMETER;
435 goto unmap_out;
436 }
437 psrc = cpu_physical_memory_map(src, &len, 0);
438 if (!psrc || len != TARGET_PAGE_SIZE) {
439 ret = H_PARAMETER;
440 goto unmap_out;
441 }
442 memcpy(pdst, psrc, len);
443 cpu_physical_memory_unmap(psrc, len, 0, len);
444 } else if (flags & H_ZERO_PAGE) {
445 memset(pdst, 0, len); /* Just clear the destination page */
446 }
447
448 if (kvm_enabled() && (flags & H_ICACHE_SYNCHRONIZE) != 0) {
449 kvmppc_dcbst_range(cpu, pdst, len);
450 }
451 if (flags & (H_ICACHE_SYNCHRONIZE | H_ICACHE_INVALIDATE)) {
452 if (kvm_enabled()) {
453 kvmppc_icbi_range(cpu, pdst, len);
454 } else {
455 tb_flush(CPU(cpu));
456 }
457 }
458
459unmap_out:
460 cpu_physical_memory_unmap(pdst, TARGET_PAGE_SIZE, 1, len);
461 return ret;
462}
463
ed120055
DG
464#define FLAGS_REGISTER_VPA 0x0000200000000000ULL
465#define FLAGS_REGISTER_DTL 0x0000400000000000ULL
466#define FLAGS_REGISTER_SLBSHADOW 0x0000600000000000ULL
467#define FLAGS_DEREGISTER_VPA 0x0000a00000000000ULL
468#define FLAGS_DEREGISTER_DTL 0x0000c00000000000ULL
469#define FLAGS_DEREGISTER_SLBSHADOW 0x0000e00000000000ULL
470
471#define VPA_MIN_SIZE 640
472#define VPA_SIZE_OFFSET 0x4
473#define VPA_SHARED_PROC_OFFSET 0x9
474#define VPA_SHARED_PROC_VAL 0x2
475
e2684c0b 476static target_ulong register_vpa(CPUPPCState *env, target_ulong vpa)
ed120055 477{
33276f1b 478 CPUState *cs = CPU(ppc_env_get_cpu(env));
ed120055
DG
479 uint16_t size;
480 uint8_t tmp;
481
482 if (vpa == 0) {
483 hcall_dprintf("Can't cope with registering a VPA at logical 0\n");
484 return H_HARDWARE;
485 }
486
487 if (vpa % env->dcache_line_size) {
488 return H_PARAMETER;
489 }
490 /* FIXME: bounds check the address */
491
41701aa4 492 size = lduw_be_phys(cs->as, vpa + 0x4);
ed120055
DG
493
494 if (size < VPA_MIN_SIZE) {
495 return H_PARAMETER;
496 }
497
498 /* VPA is not allowed to cross a page boundary */
499 if ((vpa / 4096) != ((vpa + size - 1) / 4096)) {
500 return H_PARAMETER;
501 }
502
1bfb37d1 503 env->vpa_addr = vpa;
ed120055 504
2c17449b 505 tmp = ldub_phys(cs->as, env->vpa_addr + VPA_SHARED_PROC_OFFSET);
ed120055 506 tmp |= VPA_SHARED_PROC_VAL;
db3be60d 507 stb_phys(cs->as, env->vpa_addr + VPA_SHARED_PROC_OFFSET, tmp);
ed120055
DG
508
509 return H_SUCCESS;
510}
511
e2684c0b 512static target_ulong deregister_vpa(CPUPPCState *env, target_ulong vpa)
ed120055 513{
1bfb37d1 514 if (env->slb_shadow_addr) {
ed120055
DG
515 return H_RESOURCE;
516 }
517
1bfb37d1 518 if (env->dtl_addr) {
ed120055
DG
519 return H_RESOURCE;
520 }
521
1bfb37d1 522 env->vpa_addr = 0;
ed120055
DG
523 return H_SUCCESS;
524}
525
e2684c0b 526static target_ulong register_slb_shadow(CPUPPCState *env, target_ulong addr)
ed120055 527{
33276f1b 528 CPUState *cs = CPU(ppc_env_get_cpu(env));
ed120055
DG
529 uint32_t size;
530
531 if (addr == 0) {
532 hcall_dprintf("Can't cope with SLB shadow at logical 0\n");
533 return H_HARDWARE;
534 }
535
fdfba1a2 536 size = ldl_be_phys(cs->as, addr + 0x4);
ed120055
DG
537 if (size < 0x8) {
538 return H_PARAMETER;
539 }
540
541 if ((addr / 4096) != ((addr + size - 1) / 4096)) {
542 return H_PARAMETER;
543 }
544
1bfb37d1 545 if (!env->vpa_addr) {
ed120055
DG
546 return H_RESOURCE;
547 }
548
1bfb37d1
DG
549 env->slb_shadow_addr = addr;
550 env->slb_shadow_size = size;
ed120055
DG
551
552 return H_SUCCESS;
553}
554
e2684c0b 555static target_ulong deregister_slb_shadow(CPUPPCState *env, target_ulong addr)
ed120055 556{
1bfb37d1
DG
557 env->slb_shadow_addr = 0;
558 env->slb_shadow_size = 0;
ed120055
DG
559 return H_SUCCESS;
560}
561
e2684c0b 562static target_ulong register_dtl(CPUPPCState *env, target_ulong addr)
ed120055 563{
33276f1b 564 CPUState *cs = CPU(ppc_env_get_cpu(env));
ed120055
DG
565 uint32_t size;
566
567 if (addr == 0) {
568 hcall_dprintf("Can't cope with DTL at logical 0\n");
569 return H_HARDWARE;
570 }
571
fdfba1a2 572 size = ldl_be_phys(cs->as, addr + 0x4);
ed120055
DG
573
574 if (size < 48) {
575 return H_PARAMETER;
576 }
577
1bfb37d1 578 if (!env->vpa_addr) {
ed120055
DG
579 return H_RESOURCE;
580 }
581
1bfb37d1 582 env->dtl_addr = addr;
ed120055
DG
583 env->dtl_size = size;
584
585 return H_SUCCESS;
586}
587
73f7821b 588static target_ulong deregister_dtl(CPUPPCState *env, target_ulong addr)
ed120055 589{
1bfb37d1 590 env->dtl_addr = 0;
ed120055
DG
591 env->dtl_size = 0;
592
593 return H_SUCCESS;
594}
595
28e02042 596static target_ulong h_register_vpa(PowerPCCPU *cpu, sPAPRMachineState *spapr,
ed120055
DG
597 target_ulong opcode, target_ulong *args)
598{
599 target_ulong flags = args[0];
600 target_ulong procno = args[1];
601 target_ulong vpa = args[2];
602 target_ulong ret = H_PARAMETER;
e2684c0b 603 CPUPPCState *tenv;
0f20ba62 604 PowerPCCPU *tcpu;
ed120055 605
0f20ba62 606 tcpu = ppc_get_vcpu_by_dt_id(procno);
5353d03d 607 if (!tcpu) {
ed120055
DG
608 return H_PARAMETER;
609 }
0f20ba62 610 tenv = &tcpu->env;
ed120055
DG
611
612 switch (flags) {
613 case FLAGS_REGISTER_VPA:
614 ret = register_vpa(tenv, vpa);
615 break;
616
617 case FLAGS_DEREGISTER_VPA:
618 ret = deregister_vpa(tenv, vpa);
619 break;
620
621 case FLAGS_REGISTER_SLBSHADOW:
622 ret = register_slb_shadow(tenv, vpa);
623 break;
624
625 case FLAGS_DEREGISTER_SLBSHADOW:
626 ret = deregister_slb_shadow(tenv, vpa);
627 break;
628
629 case FLAGS_REGISTER_DTL:
630 ret = register_dtl(tenv, vpa);
631 break;
632
633 case FLAGS_DEREGISTER_DTL:
634 ret = deregister_dtl(tenv, vpa);
635 break;
636 }
637
638 return ret;
639}
640
28e02042 641static target_ulong h_cede(PowerPCCPU *cpu, sPAPRMachineState *spapr,
ed120055
DG
642 target_ulong opcode, target_ulong *args)
643{
b13ce26d 644 CPUPPCState *env = &cpu->env;
fcd7d003 645 CPUState *cs = CPU(cpu);
b13ce26d 646
ed120055
DG
647 env->msr |= (1ULL << MSR_EE);
648 hreg_compute_hflags(env);
fcd7d003 649 if (!cpu_has_work(cs)) {
259186a7 650 cs->halted = 1;
27103424 651 cs->exception_index = EXCP_HLT;
fcd7d003 652 cs->exit_request = 1;
ed120055
DG
653 }
654 return H_SUCCESS;
655}
656
28e02042 657static target_ulong h_rtas(PowerPCCPU *cpu, sPAPRMachineState *spapr,
39ac8455
DG
658 target_ulong opcode, target_ulong *args)
659{
660 target_ulong rtas_r3 = args[0];
4fe822e0
AK
661 uint32_t token = rtas_ld(rtas_r3, 0);
662 uint32_t nargs = rtas_ld(rtas_r3, 1);
663 uint32_t nret = rtas_ld(rtas_r3, 2);
39ac8455 664
210b580b 665 return spapr_rtas_call(cpu, spapr, token, nargs, rtas_r3 + 12,
39ac8455
DG
666 nret, rtas_r3 + 12 + 4*nargs);
667}
668
28e02042 669static target_ulong h_logical_load(PowerPCCPU *cpu, sPAPRMachineState *spapr,
827200a2
DG
670 target_ulong opcode, target_ulong *args)
671{
fdfba1a2 672 CPUState *cs = CPU(cpu);
827200a2
DG
673 target_ulong size = args[0];
674 target_ulong addr = args[1];
675
676 switch (size) {
677 case 1:
2c17449b 678 args[0] = ldub_phys(cs->as, addr);
827200a2
DG
679 return H_SUCCESS;
680 case 2:
41701aa4 681 args[0] = lduw_phys(cs->as, addr);
827200a2
DG
682 return H_SUCCESS;
683 case 4:
fdfba1a2 684 args[0] = ldl_phys(cs->as, addr);
827200a2
DG
685 return H_SUCCESS;
686 case 8:
2c17449b 687 args[0] = ldq_phys(cs->as, addr);
827200a2
DG
688 return H_SUCCESS;
689 }
690 return H_PARAMETER;
691}
692
28e02042 693static target_ulong h_logical_store(PowerPCCPU *cpu, sPAPRMachineState *spapr,
827200a2
DG
694 target_ulong opcode, target_ulong *args)
695{
f606604f
EI
696 CPUState *cs = CPU(cpu);
697
827200a2
DG
698 target_ulong size = args[0];
699 target_ulong addr = args[1];
700 target_ulong val = args[2];
701
702 switch (size) {
703 case 1:
db3be60d 704 stb_phys(cs->as, addr, val);
827200a2
DG
705 return H_SUCCESS;
706 case 2:
5ce5944d 707 stw_phys(cs->as, addr, val);
827200a2
DG
708 return H_SUCCESS;
709 case 4:
ab1da857 710 stl_phys(cs->as, addr, val);
827200a2
DG
711 return H_SUCCESS;
712 case 8:
f606604f 713 stq_phys(cs->as, addr, val);
827200a2
DG
714 return H_SUCCESS;
715 }
716 return H_PARAMETER;
717}
718
28e02042 719static target_ulong h_logical_memop(PowerPCCPU *cpu, sPAPRMachineState *spapr,
c73e3771
BH
720 target_ulong opcode, target_ulong *args)
721{
fdfba1a2
EI
722 CPUState *cs = CPU(cpu);
723
c73e3771
BH
724 target_ulong dst = args[0]; /* Destination address */
725 target_ulong src = args[1]; /* Source address */
726 target_ulong esize = args[2]; /* Element size (0=1,1=2,2=4,3=8) */
727 target_ulong count = args[3]; /* Element count */
728 target_ulong op = args[4]; /* 0 = copy, 1 = invert */
729 uint64_t tmp;
730 unsigned int mask = (1 << esize) - 1;
731 int step = 1 << esize;
732
733 if (count > 0x80000000) {
734 return H_PARAMETER;
735 }
736
737 if ((dst & mask) || (src & mask) || (op > 1)) {
738 return H_PARAMETER;
739 }
740
741 if (dst >= src && dst < (src + (count << esize))) {
742 dst = dst + ((count - 1) << esize);
743 src = src + ((count - 1) << esize);
744 step = -step;
745 }
746
747 while (count--) {
748 switch (esize) {
749 case 0:
2c17449b 750 tmp = ldub_phys(cs->as, src);
c73e3771
BH
751 break;
752 case 1:
41701aa4 753 tmp = lduw_phys(cs->as, src);
c73e3771
BH
754 break;
755 case 2:
fdfba1a2 756 tmp = ldl_phys(cs->as, src);
c73e3771
BH
757 break;
758 case 3:
2c17449b 759 tmp = ldq_phys(cs->as, src);
c73e3771
BH
760 break;
761 default:
762 return H_PARAMETER;
763 }
764 if (op == 1) {
765 tmp = ~tmp;
766 }
767 switch (esize) {
768 case 0:
db3be60d 769 stb_phys(cs->as, dst, tmp);
c73e3771
BH
770 break;
771 case 1:
5ce5944d 772 stw_phys(cs->as, dst, tmp);
c73e3771
BH
773 break;
774 case 2:
ab1da857 775 stl_phys(cs->as, dst, tmp);
c73e3771
BH
776 break;
777 case 3:
f606604f 778 stq_phys(cs->as, dst, tmp);
c73e3771
BH
779 break;
780 }
781 dst = dst + step;
782 src = src + step;
783 }
784
785 return H_SUCCESS;
786}
787
28e02042 788static target_ulong h_logical_icbi(PowerPCCPU *cpu, sPAPRMachineState *spapr,
827200a2
DG
789 target_ulong opcode, target_ulong *args)
790{
791 /* Nothing to do on emulation, KVM will trap this in the kernel */
792 return H_SUCCESS;
793}
794
28e02042 795static target_ulong h_logical_dcbf(PowerPCCPU *cpu, sPAPRMachineState *spapr,
827200a2
DG
796 target_ulong opcode, target_ulong *args)
797{
798 /* Nothing to do on emulation, KVM will trap this in the kernel */
799 return H_SUCCESS;
800}
801
7d0cd464
PM
802static target_ulong h_set_mode_resource_le(PowerPCCPU *cpu,
803 target_ulong mflags,
804 target_ulong value1,
805 target_ulong value2)
42561bf2
AB
806{
807 CPUState *cs;
42561bf2 808
c4015bbd
AK
809 if (value1) {
810 return H_P3;
811 }
812 if (value2) {
813 return H_P4;
814 }
815
816 switch (mflags) {
817 case H_SET_MODE_ENDIAN_BIG:
818 CPU_FOREACH(cs) {
819 set_spr(cs, SPR_LPCR, 0, LPCR_ILE);
42561bf2 820 }
eefaccc0 821 spapr_pci_switch_vga(true);
c4015bbd
AK
822 return H_SUCCESS;
823
824 case H_SET_MODE_ENDIAN_LITTLE:
825 CPU_FOREACH(cs) {
826 set_spr(cs, SPR_LPCR, LPCR_ILE, LPCR_ILE);
42561bf2 827 }
eefaccc0 828 spapr_pci_switch_vga(false);
c4015bbd
AK
829 return H_SUCCESS;
830 }
42561bf2 831
c4015bbd
AK
832 return H_UNSUPPORTED_FLAG;
833}
42561bf2 834
7d0cd464
PM
835static target_ulong h_set_mode_resource_addr_trans_mode(PowerPCCPU *cpu,
836 target_ulong mflags,
837 target_ulong value1,
838 target_ulong value2)
d5ac4f54
AK
839{
840 CPUState *cs;
841 PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
d5ac4f54
AK
842
843 if (!(pcc->insns_flags2 & PPC2_ISA207S)) {
844 return H_P2;
845 }
846 if (value1) {
847 return H_P3;
848 }
849 if (value2) {
850 return H_P4;
851 }
852
5c94b2a5 853 if (mflags == AIL_RESERVED) {
d5ac4f54
AK
854 return H_UNSUPPORTED_FLAG;
855 }
856
857 CPU_FOREACH(cs) {
d5ac4f54 858 set_spr(cs, SPR_LPCR, mflags << LPCR_AIL_SHIFT, LPCR_AIL);
d5ac4f54
AK
859 }
860
861 return H_SUCCESS;
862}
863
28e02042 864static target_ulong h_set_mode(PowerPCCPU *cpu, sPAPRMachineState *spapr,
c4015bbd
AK
865 target_ulong opcode, target_ulong *args)
866{
867 target_ulong resource = args[1];
868 target_ulong ret = H_P2;
869
870 switch (resource) {
871 case H_SET_MODE_RESOURCE_LE:
7d0cd464 872 ret = h_set_mode_resource_le(cpu, args[0], args[2], args[3]);
c4015bbd 873 break;
d5ac4f54 874 case H_SET_MODE_RESOURCE_ADDR_TRANS_MODE:
7d0cd464
PM
875 ret = h_set_mode_resource_addr_trans_mode(cpu, args[0],
876 args[2], args[3]);
d5ac4f54 877 break;
42561bf2
AB
878 }
879
42561bf2
AB
880 return ret;
881}
882
d77a98b0
SJS
883static target_ulong h_clean_slb(PowerPCCPU *cpu, sPAPRMachineState *spapr,
884 target_ulong opcode, target_ulong *args)
885{
886 qemu_log_mask(LOG_UNIMP, "Unimplemented SPAPR hcall 0x"TARGET_FMT_lx"%s\n",
887 opcode, " (H_CLEAN_SLB)");
888 return H_FUNCTION;
889}
890
891static target_ulong h_invalidate_pid(PowerPCCPU *cpu, sPAPRMachineState *spapr,
892 target_ulong opcode, target_ulong *args)
893{
894 qemu_log_mask(LOG_UNIMP, "Unimplemented SPAPR hcall 0x"TARGET_FMT_lx"%s\n",
895 opcode, " (H_INVALIDATE_PID)");
896 return H_FUNCTION;
897}
898
b4db5413
SJS
899static void spapr_check_setup_free_hpt(sPAPRMachineState *spapr,
900 uint64_t patbe_old, uint64_t patbe_new)
901{
902 /*
903 * We have 4 Options:
904 * HASH->HASH || RADIX->RADIX || NOTHING->RADIX : Do Nothing
905 * HASH->RADIX : Free HPT
906 * RADIX->HASH : Allocate HPT
907 * NOTHING->HASH : Allocate HPT
908 * Note: NOTHING implies the case where we said the guest could choose
909 * later and so assumed radix and now it's called H_REG_PROC_TBL
910 */
911
912 if ((patbe_old & PATBE1_GR) == (patbe_new & PATBE1_GR)) {
913 /* We assume RADIX, so this catches all the "Do Nothing" cases */
914 } else if (!(patbe_old & PATBE1_GR)) {
915 /* HASH->RADIX : Free HPT */
916 g_free(spapr->htab);
917 spapr->htab = NULL;
918 spapr->htab_shift = 0;
919 close_htab_fd(spapr);
920 } else if (!(patbe_new & PATBE1_GR)) {
921 /* RADIX->HASH || NOTHING->HASH : Allocate HPT */
922 spapr_setup_hpt_and_vrma(spapr);
923 }
924 return;
925}
926
927#define FLAGS_MASK 0x01FULL
928#define FLAG_MODIFY 0x10
929#define FLAG_REGISTER 0x08
930#define FLAG_RADIX 0x04
931#define FLAG_HASH_PROC_TBL 0x02
932#define FLAG_GTSE 0x01
933
d77a98b0
SJS
934static target_ulong h_register_process_table(PowerPCCPU *cpu,
935 sPAPRMachineState *spapr,
936 target_ulong opcode,
937 target_ulong *args)
938{
6de83307 939 CPUState *cs;
b4db5413
SJS
940 target_ulong flags = args[0];
941 target_ulong proc_tbl = args[1];
942 target_ulong page_size = args[2];
943 target_ulong table_size = args[3];
944 uint64_t cproc;
945
946 if (flags & ~FLAGS_MASK) { /* Check no reserved bits are set */
947 return H_PARAMETER;
948 }
949 if (flags & FLAG_MODIFY) {
950 if (flags & FLAG_REGISTER) {
951 if (flags & FLAG_RADIX) { /* Register new RADIX process table */
952 if (proc_tbl & 0xfff || proc_tbl >> 60) {
953 return H_P2;
954 } else if (page_size) {
955 return H_P3;
956 } else if (table_size > 24) {
957 return H_P4;
958 }
959 cproc = PATBE1_GR | proc_tbl | table_size;
960 } else { /* Register new HPT process table */
961 if (flags & FLAG_HASH_PROC_TBL) { /* Hash with Segment Tables */
962 /* TODO - Not Supported */
963 /* Technically caused by flag bits => H_PARAMETER */
964 return H_PARAMETER;
965 } else { /* Hash with SLB */
966 if (proc_tbl >> 38) {
967 return H_P2;
968 } else if (page_size & ~0x7) {
969 return H_P3;
970 } else if (table_size > 24) {
971 return H_P4;
972 }
973 }
974 cproc = (proc_tbl << 25) | page_size << 5 | table_size;
975 }
976
977 } else { /* Deregister current process table */
978 /* Set to benign value: (current GR) | 0. This allows
979 * deregistration in KVM to succeed even if the radix bit in flags
980 * doesn't match the radix bit in the old PATB. */
981 cproc = spapr->patb_entry & PATBE1_GR;
982 }
983 } else { /* Maintain current registration */
984 if (!(flags & FLAG_RADIX) != !(spapr->patb_entry & PATBE1_GR)) {
985 /* Technically caused by flag bits => H_PARAMETER */
986 return H_PARAMETER; /* Existing Process Table Mismatch */
987 }
988 cproc = spapr->patb_entry;
989 }
990
991 /* Check if we need to setup OR free the hpt */
992 spapr_check_setup_free_hpt(spapr, spapr->patb_entry, cproc);
993
994 spapr->patb_entry = cproc; /* Save new process table */
6de83307
SJS
995
996 /* Update the UPRT and GTSE bits in the LPCR for all cpus */
997 CPU_FOREACH(cs) {
998 set_spr(cs, SPR_LPCR, LPCR_UPRT | LPCR_GTSE,
999 ((flags & (FLAG_RADIX | FLAG_HASH_PROC_TBL)) ? LPCR_UPRT : 0) |
1000 ((flags & FLAG_GTSE) ? LPCR_GTSE : 0));
b4db5413
SJS
1001 }
1002
1003 if (kvm_enabled()) {
1004 return kvmppc_configure_v3_mmu(cpu, flags & FLAG_RADIX,
1005 flags & FLAG_GTSE, cproc);
1006 }
1007 return H_SUCCESS;
d77a98b0
SJS
1008}
1009
1c7ad77e
NP
1010#define H_SIGNAL_SYS_RESET_ALL -1
1011#define H_SIGNAL_SYS_RESET_ALLBUTSELF -2
1012
1013static target_ulong h_signal_sys_reset(PowerPCCPU *cpu,
1014 sPAPRMachineState *spapr,
1015 target_ulong opcode, target_ulong *args)
1016{
1017 target_long target = args[0];
1018 CPUState *cs;
1019
1020 if (target < 0) {
1021 /* Broadcast */
1022 if (target < H_SIGNAL_SYS_RESET_ALLBUTSELF) {
1023 return H_PARAMETER;
1024 }
1025
1026 CPU_FOREACH(cs) {
1027 PowerPCCPU *c = POWERPC_CPU(cs);
1028
1029 if (target == H_SIGNAL_SYS_RESET_ALLBUTSELF) {
1030 if (c == cpu) {
1031 continue;
1032 }
1033 }
1034 run_on_cpu(cs, spapr_do_system_reset_on_cpu, RUN_ON_CPU_NULL);
1035 }
1036 return H_SUCCESS;
1037
1038 } else {
1039 /* Unicast */
1040 CPU_FOREACH(cs) {
1041 if (cpu->cpu_dt_id == target) {
1042 run_on_cpu(cs, spapr_do_system_reset_on_cpu, RUN_ON_CPU_NULL);
1043 return H_SUCCESS;
1044 }
1045 }
1046 return H_PARAMETER;
1047 }
1048}
1049
152ef803 1050static target_ulong h_client_architecture_support(PowerPCCPU *cpu,
28e02042 1051 sPAPRMachineState *spapr,
2a6593cb
AK
1052 target_ulong opcode,
1053 target_ulong *args)
1054{
27ac3e06 1055 target_ulong list = ppc64_phys_to_real(args[0]);
facdb8b6 1056 target_ulong ov_table;
152ef803
DG
1057 bool explicit_match = false; /* Matched the CPU's real PVR */
1058 uint32_t max_compat = cpu->max_compat;
1059 uint32_t best_compat = 0;
1060 int i;
e957f6a9 1061 sPAPROptionVector *ov1_guest, *ov5_guest, *ov5_cas_old, *ov5_updates;
9fb4541f 1062 bool guest_radix;
3794d548 1063
152ef803
DG
1064 /*
1065 * We scan the supplied table of PVRs looking for two things
1066 * 1. Is our real CPU PVR in the list?
1067 * 2. What's the "best" listed logical PVR
1068 */
1069 for (i = 0; i < 512; ++i) {
3794d548
AK
1070 uint32_t pvr, pvr_mask;
1071
27ac3e06 1072 pvr_mask = ldl_be_phys(&address_space_memory, list);
152ef803
DG
1073 pvr = ldl_be_phys(&address_space_memory, list + 4);
1074 list += 8;
1075
3794d548 1076 if (~pvr_mask & pvr) {
152ef803 1077 break; /* Terminator record */
3794d548 1078 }
152ef803
DG
1079
1080 if ((cpu->env.spr[SPR_PVR] & pvr_mask) == (pvr & pvr_mask)) {
1081 explicit_match = true;
1082 } else {
1083 if (ppc_check_compat(cpu, pvr, best_compat, max_compat)) {
1084 best_compat = pvr;
1085 }
1086 }
1087 }
1088
1089 if ((best_compat == 0) && (!explicit_match || max_compat)) {
1090 /* We couldn't find a suitable compatibility mode, and either
1091 * the guest doesn't support "raw" mode for this CPU, or raw
1092 * mode is disabled because a maximum compat mode is set */
1093 return H_HARDWARE;
3794d548
AK
1094 }
1095
3794d548 1096 /* Parsing finished */
152ef803 1097 trace_spapr_cas_pvr(cpu->compat_pvr, explicit_match, best_compat);
3794d548
AK
1098
1099 /* Update CPUs */
152ef803 1100 if (cpu->compat_pvr != best_compat) {
f6f242c7 1101 Error *local_err = NULL;
3794d548 1102
f6f242c7
DG
1103 ppc_set_compat_all(best_compat, &local_err);
1104 if (local_err) {
1105 error_report_err(local_err);
1106 return H_HARDWARE;
3794d548
AK
1107 }
1108 }
1109
03d196b7
BR
1110 /* For the future use: here @ov_table points to the first option vector */
1111 ov_table = list;
1112
e957f6a9 1113 ov1_guest = spapr_ovec_parse_vector(ov_table, 1);
facdb8b6 1114 ov5_guest = spapr_ovec_parse_vector(ov_table, 5);
9fb4541f
SB
1115 if (spapr_ovec_test(ov5_guest, OV5_MMU_BOTH)) {
1116 error_report("guest requested hash and radix MMU, which is invalid.");
1117 exit(EXIT_FAILURE);
1118 }
1119 /* The radix/hash bit in byte 24 requires special handling: */
1120 guest_radix = spapr_ovec_test(ov5_guest, OV5_MMU_RADIX_300);
1121 spapr_ovec_clear(ov5_guest, OV5_MMU_RADIX_300);
2a6593cb 1122
facdb8b6
MR
1123 /* NOTE: there are actually a number of ov5 bits where input from the
1124 * guest is always zero, and the platform/QEMU enables them independently
1125 * of guest input. To model these properly we'd want some sort of mask,
1126 * but since they only currently apply to memory migration as defined
1127 * by LoPAPR 1.1, 14.5.4.8, which QEMU doesn't implement, we don't need
6787d27b 1128 * to worry about this for now.
facdb8b6 1129 */
6787d27b
MR
1130 ov5_cas_old = spapr_ovec_clone(spapr->ov5_cas);
1131 /* full range of negotiated ov5 capabilities */
facdb8b6
MR
1132 spapr_ovec_intersect(spapr->ov5_cas, spapr->ov5, ov5_guest);
1133 spapr_ovec_cleanup(ov5_guest);
6787d27b
MR
1134 /* capabilities that have been added since CAS-generated guest reset.
1135 * if capabilities have since been removed, generate another reset
1136 */
1137 ov5_updates = spapr_ovec_new();
1138 spapr->cas_reboot = spapr_ovec_diff(ov5_updates,
1139 ov5_cas_old, spapr->ov5_cas);
9fb4541f
SB
1140 /* Now that processing is finished, set the radix/hash bit for the
1141 * guest if it requested a valid mode; otherwise terminate the boot. */
1142 if (guest_radix) {
1143 if (kvm_enabled() && !kvmppc_has_cap_mmu_radix()) {
1144 error_report("Guest requested unavailable MMU mode (radix).");
1145 exit(EXIT_FAILURE);
1146 }
1147 spapr_ovec_set(spapr->ov5_cas, OV5_MMU_RADIX_300);
1148 } else {
1149 if (kvm_enabled() && kvmppc_has_cap_mmu_radix()
1150 && !kvmppc_has_cap_mmu_hash_v3()) {
1151 error_report("Guest requested unavailable MMU mode (hash).");
1152 exit(EXIT_FAILURE);
1153 }
1154 }
e957f6a9
SB
1155 spapr->cas_legacy_guest_workaround = !spapr_ovec_test(ov1_guest,
1156 OV1_PPC_3_00);
6787d27b
MR
1157 if (!spapr->cas_reboot) {
1158 spapr->cas_reboot =
5b120785 1159 (spapr_h_cas_compose_response(spapr, args[1], args[2],
6787d27b
MR
1160 ov5_updates) != 0);
1161 }
1162 spapr_ovec_cleanup(ov5_updates);
03d196b7 1163
6787d27b 1164 if (spapr->cas_reboot) {
cf83f140 1165 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
9fb4541f
SB
1166 } else {
1167 /* If ppc_spapr_reset() did not set up a HPT but one is necessary
1168 * (because the guest isn't going to use radix) then set it up here. */
1169 if ((spapr->patb_entry & PATBE1_GR) && !guest_radix) {
1170 /* legacy hash or new hash: */
1171 spapr_setup_hpt_and_vrma(spapr);
1172 }
2a6593cb
AK
1173 }
1174
1175 return H_SUCCESS;
1176}
1177
7d7ba3fe
DG
1178static spapr_hcall_fn papr_hypercall_table[(MAX_HCALL_OPCODE / 4) + 1];
1179static spapr_hcall_fn kvmppc_hypercall_table[KVMPPC_HCALL_MAX - KVMPPC_HCALL_BASE + 1];
9fdf0c29
DG
1180
1181void spapr_register_hypercall(target_ulong opcode, spapr_hcall_fn fn)
1182{
39ac8455
DG
1183 spapr_hcall_fn *slot;
1184
1185 if (opcode <= MAX_HCALL_OPCODE) {
1186 assert((opcode & 0x3) == 0);
9fdf0c29 1187
39ac8455
DG
1188 slot = &papr_hypercall_table[opcode / 4];
1189 } else {
1190 assert((opcode >= KVMPPC_HCALL_BASE) && (opcode <= KVMPPC_HCALL_MAX));
9fdf0c29 1191
39ac8455
DG
1192 slot = &kvmppc_hypercall_table[opcode - KVMPPC_HCALL_BASE];
1193 }
9fdf0c29 1194
c89d5299 1195 assert(!(*slot));
39ac8455 1196 *slot = fn;
9fdf0c29
DG
1197}
1198
aa100fa4 1199target_ulong spapr_hypercall(PowerPCCPU *cpu, target_ulong opcode,
9fdf0c29
DG
1200 target_ulong *args)
1201{
28e02042
DG
1202 sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
1203
9fdf0c29
DG
1204 if ((opcode <= MAX_HCALL_OPCODE)
1205 && ((opcode & 0x3) == 0)) {
39ac8455
DG
1206 spapr_hcall_fn fn = papr_hypercall_table[opcode / 4];
1207
1208 if (fn) {
b13ce26d 1209 return fn(cpu, spapr, opcode, args);
39ac8455
DG
1210 }
1211 } else if ((opcode >= KVMPPC_HCALL_BASE) &&
1212 (opcode <= KVMPPC_HCALL_MAX)) {
1213 spapr_hcall_fn fn = kvmppc_hypercall_table[opcode - KVMPPC_HCALL_BASE];
9fdf0c29
DG
1214
1215 if (fn) {
b13ce26d 1216 return fn(cpu, spapr, opcode, args);
9fdf0c29
DG
1217 }
1218 }
1219
aaf87c66
TH
1220 qemu_log_mask(LOG_UNIMP, "Unimplemented SPAPR hcall 0x" TARGET_FMT_lx "\n",
1221 opcode);
9fdf0c29
DG
1222 return H_FUNCTION;
1223}
f43e3525 1224
83f7d43a 1225static void hypercall_register_types(void)
f43e3525
DG
1226{
1227 /* hcall-pft */
1228 spapr_register_hypercall(H_ENTER, h_enter);
1229 spapr_register_hypercall(H_REMOVE, h_remove);
1230 spapr_register_hypercall(H_PROTECT, h_protect);
6bbd5dde 1231 spapr_register_hypercall(H_READ, h_read);
39ac8455 1232
a3d0abae
DG
1233 /* hcall-bulk */
1234 spapr_register_hypercall(H_BULK_REMOVE, h_bulk_remove);
1235
ed120055
DG
1236 /* hcall-splpar */
1237 spapr_register_hypercall(H_REGISTER_VPA, h_register_vpa);
1238 spapr_register_hypercall(H_CEDE, h_cede);
1c7ad77e 1239 spapr_register_hypercall(H_SIGNAL_SYS_RESET, h_signal_sys_reset);
ed120055 1240
423576f7
TH
1241 /* processor register resource access h-calls */
1242 spapr_register_hypercall(H_SET_SPRG0, h_set_sprg0);
af08a58f 1243 spapr_register_hypercall(H_SET_DABR, h_set_dabr);
e49ff266 1244 spapr_register_hypercall(H_SET_XDABR, h_set_xdabr);
3240dd9a 1245 spapr_register_hypercall(H_PAGE_INIT, h_page_init);
423576f7
TH
1246 spapr_register_hypercall(H_SET_MODE, h_set_mode);
1247
d77a98b0
SJS
1248 /* In Memory Table MMU h-calls */
1249 spapr_register_hypercall(H_CLEAN_SLB, h_clean_slb);
1250 spapr_register_hypercall(H_INVALIDATE_PID, h_invalidate_pid);
1251 spapr_register_hypercall(H_REGISTER_PROC_TBL, h_register_process_table);
1252
827200a2
DG
1253 /* "debugger" hcalls (also used by SLOF). Note: We do -not- differenciate
1254 * here between the "CI" and the "CACHE" variants, they will use whatever
1255 * mapping attributes qemu is using. When using KVM, the kernel will
1256 * enforce the attributes more strongly
1257 */
1258 spapr_register_hypercall(H_LOGICAL_CI_LOAD, h_logical_load);
1259 spapr_register_hypercall(H_LOGICAL_CI_STORE, h_logical_store);
1260 spapr_register_hypercall(H_LOGICAL_CACHE_LOAD, h_logical_load);
1261 spapr_register_hypercall(H_LOGICAL_CACHE_STORE, h_logical_store);
1262 spapr_register_hypercall(H_LOGICAL_ICBI, h_logical_icbi);
1263 spapr_register_hypercall(H_LOGICAL_DCBF, h_logical_dcbf);
c73e3771 1264 spapr_register_hypercall(KVMPPC_H_LOGICAL_MEMOP, h_logical_memop);
827200a2 1265
39ac8455
DG
1266 /* qemu/KVM-PPC specific hcalls */
1267 spapr_register_hypercall(KVMPPC_H_RTAS, h_rtas);
42561bf2 1268
2a6593cb
AK
1269 /* ibm,client-architecture-support support */
1270 spapr_register_hypercall(KVMPPC_H_CAS, h_client_architecture_support);
f43e3525 1271}
83f7d43a
AF
1272
1273type_init(hypercall_register_types)