]> git.proxmox.com Git - mirror_qemu.git/blame - hw/ppc/spapr_hcall.c
Merge remote-tracking branch 'remotes/philmd-gitlab/tags/mips-20210104' into staging
[mirror_qemu.git] / hw / ppc / spapr_hcall.c
CommitLineData
0d75590d 1#include "qemu/osdep.h"
0c21e073 2#include "qemu/cutils.h"
da34e65c 3#include "qapi/error.h"
b3946626 4#include "sysemu/hw_accel.h"
54d31236 5#include "sysemu/runstate.h"
03dd024f 6#include "qemu/log.h"
db725815 7#include "qemu/main-loop.h"
0b8fa32f 8#include "qemu/module.h"
0b0b8310 9#include "qemu/error-report.h"
9fdf0c29 10#include "cpu.h"
63c91552 11#include "exec/exec-all.h"
ed120055 12#include "helper_regs.h"
0d09e41a 13#include "hw/ppc/spapr.h"
7388efaf 14#include "hw/ppc/spapr_cpu_core.h"
d5aea6f3 15#include "mmu-hash64.h"
3794d548
AK
16#include "cpu-models.h"
17#include "trace.h"
18#include "kvm_ppc.h"
0c21e073 19#include "hw/ppc/fdt.h"
facdb8b6 20#include "hw/ppc/spapr_ovec.h"
b4db5413 21#include "mmu-book3s-v3.h"
2cc0e2e8 22#include "hw/mem/memory-device.h"
f43e3525 23
af08a58f
TH
24static bool has_spr(PowerPCCPU *cpu, int spr)
25{
26 /* We can test whether the SPR is defined by checking for a valid name */
27 return cpu->env.spr_cb[spr].name != NULL;
28}
29
c6404ade 30static inline bool valid_ptex(PowerPCCPU *cpu, target_ulong ptex)
f3c75d42
AK
31{
32 /*
36778660 33 * hash value/pteg group index is normalized by HPT mask
f3c75d42 34 */
36778660 35 if (((ptex & ~7ULL) / HPTES_PER_GROUP) & ~ppc_hash64_hpt_mask(cpu)) {
f3c75d42
AK
36 return false;
37 }
38 return true;
39}
40
ce2918cb 41static bool is_ram_address(SpaprMachineState *spapr, hwaddr addr)
ecbc25fa
DG
42{
43 MachineState *machine = MACHINE(spapr);
e017da37 44 DeviceMemoryState *dms = machine->device_memory;
ecbc25fa
DG
45
46 if (addr < machine->ram_size) {
47 return true;
48 }
e017da37
DH
49 if ((addr >= dms->base)
50 && ((addr - dms->base) < memory_region_size(&dms->mr))) {
ecbc25fa
DG
51 return true;
52 }
53
54 return false;
55}
56
ce2918cb 57static target_ulong h_enter(PowerPCCPU *cpu, SpaprMachineState *spapr,
f43e3525
DG
58 target_ulong opcode, target_ulong *args)
59{
60 target_ulong flags = args[0];
c6404ade 61 target_ulong ptex = args[1];
f43e3525
DG
62 target_ulong pteh = args[2];
63 target_ulong ptel = args[3];
1f0252e6 64 unsigned apshift;
f73a2575 65 target_ulong raddr;
c6404ade 66 target_ulong slot;
7222b94a 67 const ppc_hash_pte64_t *hptes;
f43e3525 68
1f0252e6 69 apshift = ppc_hash64_hpte_page_shift_noslb(cpu, pteh, ptel);
1114e712
DG
70 if (!apshift) {
71 /* Bad page size encoding */
72 return H_PARAMETER;
f43e3525
DG
73 }
74
1114e712 75 raddr = (ptel & HPTE64_R_RPN) & ~((1ULL << apshift) - 1);
f43e3525 76
ecbc25fa 77 if (is_ram_address(spapr, raddr)) {
f73a2575 78 /* Regular RAM - should have WIMG=0010 */
d5aea6f3 79 if ((ptel & HPTE64_R_WIMG) != HPTE64_R_M) {
f73a2575
DG
80 return H_PARAMETER;
81 }
82 } else {
c1175907 83 target_ulong wimg_flags;
f73a2575
DG
84 /* Looks like an IO address */
85 /* FIXME: What WIMG combinations could be sensible for IO?
86 * For now we allow WIMG=010x, but are there others? */
87 /* FIXME: Should we check against registered IO addresses? */
c1175907
AK
88 wimg_flags = (ptel & (HPTE64_R_W | HPTE64_R_I | HPTE64_R_M));
89
90 if (wimg_flags != HPTE64_R_I &&
91 wimg_flags != (HPTE64_R_I | HPTE64_R_M)) {
f73a2575
DG
92 return H_PARAMETER;
93 }
f43e3525 94 }
f73a2575 95
f43e3525
DG
96 pteh &= ~0x60ULL;
97
c6404ade 98 if (!valid_ptex(cpu, ptex)) {
f43e3525
DG
99 return H_PARAMETER;
100 }
7c43bca0 101
c6404ade
DG
102 slot = ptex & 7ULL;
103 ptex = ptex & ~7ULL;
104
f43e3525 105 if (likely((flags & H_EXACT) == 0)) {
7222b94a 106 hptes = ppc_hash64_map_hptes(cpu, ptex, HPTES_PER_GROUP);
c6404ade 107 for (slot = 0; slot < 8; slot++) {
7222b94a 108 if (!(ppc_hash64_hpte0(cpu, hptes, slot) & HPTE64_V_VALID)) {
f43e3525
DG
109 break;
110 }
7aaf4957 111 }
7222b94a 112 ppc_hash64_unmap_hptes(cpu, hptes, ptex, HPTES_PER_GROUP);
c6404ade 113 if (slot == 8) {
7aaf4957
AK
114 return H_PTEG_FULL;
115 }
f43e3525 116 } else {
7222b94a
DG
117 hptes = ppc_hash64_map_hptes(cpu, ptex + slot, 1);
118 if (ppc_hash64_hpte0(cpu, hptes, 0) & HPTE64_V_VALID) {
119 ppc_hash64_unmap_hptes(cpu, hptes, ptex + slot, 1);
f43e3525
DG
120 return H_PTEG_FULL;
121 }
7222b94a 122 ppc_hash64_unmap_hptes(cpu, hptes, ptex, 1);
f43e3525 123 }
7c43bca0 124
a2dd4e83 125 spapr_store_hpte(cpu, ptex + slot, pteh | HPTE64_V_HPTE_DIRTY, ptel);
f43e3525 126
c6404ade 127 args[0] = ptex + slot;
f43e3525
DG
128 return H_SUCCESS;
129}
130
a3801402 131typedef enum {
a3d0abae
DG
132 REMOVE_SUCCESS = 0,
133 REMOVE_NOT_FOUND = 1,
134 REMOVE_PARM = 2,
135 REMOVE_HW = 3,
a3801402 136} RemoveResult;
a3d0abae 137
a2dd4e83
BH
138static RemoveResult remove_hpte(PowerPCCPU *cpu
139 , target_ulong ptex,
a3d0abae
DG
140 target_ulong avpn,
141 target_ulong flags,
142 target_ulong *vp, target_ulong *rp)
f43e3525 143{
7222b94a 144 const ppc_hash_pte64_t *hptes;
61a36c9b 145 target_ulong v, r;
f43e3525 146
c6404ade 147 if (!valid_ptex(cpu, ptex)) {
a3d0abae 148 return REMOVE_PARM;
f43e3525
DG
149 }
150
7222b94a
DG
151 hptes = ppc_hash64_map_hptes(cpu, ptex, 1);
152 v = ppc_hash64_hpte0(cpu, hptes, 0);
153 r = ppc_hash64_hpte1(cpu, hptes, 0);
154 ppc_hash64_unmap_hptes(cpu, hptes, ptex, 1);
f43e3525 155
d5aea6f3 156 if ((v & HPTE64_V_VALID) == 0 ||
f43e3525
DG
157 ((flags & H_AVPN) && (v & ~0x7fULL) != avpn) ||
158 ((flags & H_ANDCOND) && (v & avpn) != 0)) {
a3d0abae 159 return REMOVE_NOT_FOUND;
f43e3525 160 }
35f9304d 161 *vp = v;
a3d0abae 162 *rp = r;
a2dd4e83 163 spapr_store_hpte(cpu, ptex, HPTE64_V_HPTE_DIRTY, 0);
61a36c9b 164 ppc_hash64_tlb_flush_hpte(cpu, ptex, v, r);
a3d0abae
DG
165 return REMOVE_SUCCESS;
166}
167
ce2918cb 168static target_ulong h_remove(PowerPCCPU *cpu, SpaprMachineState *spapr,
a3d0abae
DG
169 target_ulong opcode, target_ulong *args)
170{
cd0c6f47 171 CPUPPCState *env = &cpu->env;
a3d0abae 172 target_ulong flags = args[0];
c6404ade 173 target_ulong ptex = args[1];
a3d0abae 174 target_ulong avpn = args[2];
a3801402 175 RemoveResult ret;
a3d0abae 176
c6404ade 177 ret = remove_hpte(cpu, ptex, avpn, flags,
a3d0abae
DG
178 &args[0], &args[1]);
179
180 switch (ret) {
181 case REMOVE_SUCCESS:
e3cffe6f 182 check_tlb_flush(env, true);
a3d0abae
DG
183 return H_SUCCESS;
184
185 case REMOVE_NOT_FOUND:
186 return H_NOT_FOUND;
187
188 case REMOVE_PARM:
189 return H_PARAMETER;
190
191 case REMOVE_HW:
192 return H_HARDWARE;
193 }
194
9a39970d 195 g_assert_not_reached();
a3d0abae
DG
196}
197
198#define H_BULK_REMOVE_TYPE 0xc000000000000000ULL
199#define H_BULK_REMOVE_REQUEST 0x4000000000000000ULL
200#define H_BULK_REMOVE_RESPONSE 0x8000000000000000ULL
201#define H_BULK_REMOVE_END 0xc000000000000000ULL
202#define H_BULK_REMOVE_CODE 0x3000000000000000ULL
203#define H_BULK_REMOVE_SUCCESS 0x0000000000000000ULL
204#define H_BULK_REMOVE_NOT_FOUND 0x1000000000000000ULL
205#define H_BULK_REMOVE_PARM 0x2000000000000000ULL
206#define H_BULK_REMOVE_HW 0x3000000000000000ULL
207#define H_BULK_REMOVE_RC 0x0c00000000000000ULL
208#define H_BULK_REMOVE_FLAGS 0x0300000000000000ULL
209#define H_BULK_REMOVE_ABSOLUTE 0x0000000000000000ULL
210#define H_BULK_REMOVE_ANDCOND 0x0100000000000000ULL
211#define H_BULK_REMOVE_AVPN 0x0200000000000000ULL
212#define H_BULK_REMOVE_PTEX 0x00ffffffffffffffULL
213
214#define H_BULK_REMOVE_MAX_BATCH 4
215
ce2918cb 216static target_ulong h_bulk_remove(PowerPCCPU *cpu, SpaprMachineState *spapr,
a3d0abae
DG
217 target_ulong opcode, target_ulong *args)
218{
cd0c6f47 219 CPUPPCState *env = &cpu->env;
a3d0abae 220 int i;
cd0c6f47 221 target_ulong rc = H_SUCCESS;
a3d0abae
DG
222
223 for (i = 0; i < H_BULK_REMOVE_MAX_BATCH; i++) {
224 target_ulong *tsh = &args[i*2];
225 target_ulong tsl = args[i*2 + 1];
226 target_ulong v, r, ret;
227
228 if ((*tsh & H_BULK_REMOVE_TYPE) == H_BULK_REMOVE_END) {
229 break;
230 } else if ((*tsh & H_BULK_REMOVE_TYPE) != H_BULK_REMOVE_REQUEST) {
231 return H_PARAMETER;
232 }
233
234 *tsh &= H_BULK_REMOVE_PTEX | H_BULK_REMOVE_FLAGS;
235 *tsh |= H_BULK_REMOVE_RESPONSE;
236
237 if ((*tsh & H_BULK_REMOVE_ANDCOND) && (*tsh & H_BULK_REMOVE_AVPN)) {
238 *tsh |= H_BULK_REMOVE_PARM;
239 return H_PARAMETER;
240 }
241
7ef23068 242 ret = remove_hpte(cpu, *tsh & H_BULK_REMOVE_PTEX, tsl,
a3d0abae
DG
243 (*tsh & H_BULK_REMOVE_FLAGS) >> 26,
244 &v, &r);
245
246 *tsh |= ret << 60;
247
248 switch (ret) {
249 case REMOVE_SUCCESS:
d5aea6f3 250 *tsh |= (r & (HPTE64_R_C | HPTE64_R_R)) << 43;
a3d0abae
DG
251 break;
252
253 case REMOVE_PARM:
cd0c6f47
BH
254 rc = H_PARAMETER;
255 goto exit;
a3d0abae
DG
256
257 case REMOVE_HW:
cd0c6f47
BH
258 rc = H_HARDWARE;
259 goto exit;
a3d0abae
DG
260 }
261 }
cd0c6f47 262 exit:
e3cffe6f 263 check_tlb_flush(env, true);
a3d0abae 264
cd0c6f47 265 return rc;
f43e3525
DG
266}
267
ce2918cb 268static target_ulong h_protect(PowerPCCPU *cpu, SpaprMachineState *spapr,
f43e3525
DG
269 target_ulong opcode, target_ulong *args)
270{
b13ce26d 271 CPUPPCState *env = &cpu->env;
f43e3525 272 target_ulong flags = args[0];
c6404ade 273 target_ulong ptex = args[1];
f43e3525 274 target_ulong avpn = args[2];
7222b94a 275 const ppc_hash_pte64_t *hptes;
61a36c9b 276 target_ulong v, r;
f43e3525 277
c6404ade 278 if (!valid_ptex(cpu, ptex)) {
f43e3525
DG
279 return H_PARAMETER;
280 }
281
7222b94a
DG
282 hptes = ppc_hash64_map_hptes(cpu, ptex, 1);
283 v = ppc_hash64_hpte0(cpu, hptes, 0);
284 r = ppc_hash64_hpte1(cpu, hptes, 0);
285 ppc_hash64_unmap_hptes(cpu, hptes, ptex, 1);
f43e3525 286
d5aea6f3 287 if ((v & HPTE64_V_VALID) == 0 ||
f43e3525 288 ((flags & H_AVPN) && (v & ~0x7fULL) != avpn)) {
f43e3525
DG
289 return H_NOT_FOUND;
290 }
291
d5aea6f3
DG
292 r &= ~(HPTE64_R_PP0 | HPTE64_R_PP | HPTE64_R_N |
293 HPTE64_R_KEY_HI | HPTE64_R_KEY_LO);
294 r |= (flags << 55) & HPTE64_R_PP0;
295 r |= (flags << 48) & HPTE64_R_KEY_HI;
296 r |= flags & (HPTE64_R_PP | HPTE64_R_N | HPTE64_R_KEY_LO);
a2dd4e83
BH
297 spapr_store_hpte(cpu, ptex,
298 (v & ~HPTE64_V_VALID) | HPTE64_V_HPTE_DIRTY, 0);
c6404ade 299 ppc_hash64_tlb_flush_hpte(cpu, ptex, v, r);
d76ab5e1
ND
300 /* Flush the tlb */
301 check_tlb_flush(env, true);
f43e3525 302 /* Don't need a memory barrier, due to qemu's global lock */
a2dd4e83 303 spapr_store_hpte(cpu, ptex, v | HPTE64_V_HPTE_DIRTY, r);
f43e3525
DG
304 return H_SUCCESS;
305}
306
ce2918cb 307static target_ulong h_read(PowerPCCPU *cpu, SpaprMachineState *spapr,
6bbd5dde
EC
308 target_ulong opcode, target_ulong *args)
309{
6bbd5dde 310 target_ulong flags = args[0];
c6404ade 311 target_ulong ptex = args[1];
6bbd5dde 312 int i, ridx, n_entries = 1;
993aaf0c 313 const ppc_hash_pte64_t *hptes;
6bbd5dde 314
c6404ade 315 if (!valid_ptex(cpu, ptex)) {
6bbd5dde
EC
316 return H_PARAMETER;
317 }
318
319 if (flags & H_READ_4) {
320 /* Clear the two low order bits */
c6404ade 321 ptex &= ~(3ULL);
6bbd5dde
EC
322 n_entries = 4;
323 }
324
993aaf0c 325 hptes = ppc_hash64_map_hptes(cpu, ptex, n_entries);
6bbd5dde 326 for (i = 0, ridx = 0; i < n_entries; i++) {
993aaf0c
BH
327 args[ridx++] = ppc_hash64_hpte0(cpu, hptes, i);
328 args[ridx++] = ppc_hash64_hpte1(cpu, hptes, i);
6bbd5dde 329 }
993aaf0c 330 ppc_hash64_unmap_hptes(cpu, hptes, ptex, n_entries);
6bbd5dde
EC
331
332 return H_SUCCESS;
333}
334
ce2918cb 335struct SpaprPendingHpt {
0b0b8310
DG
336 /* These fields are read-only after initialization */
337 int shift;
338 QemuThread thread;
339
340 /* These fields are protected by the BQL */
341 bool complete;
342
343 /* These fields are private to the preparation thread if
344 * !complete, otherwise protected by the BQL */
345 int ret;
346 void *hpt;
347};
348
ce2918cb 349static void free_pending_hpt(SpaprPendingHpt *pending)
0b0b8310
DG
350{
351 if (pending->hpt) {
352 qemu_vfree(pending->hpt);
353 }
354
355 g_free(pending);
356}
357
358static void *hpt_prepare_thread(void *opaque)
359{
ce2918cb 360 SpaprPendingHpt *pending = opaque;
0b0b8310
DG
361 size_t size = 1ULL << pending->shift;
362
f29b959d 363 pending->hpt = qemu_try_memalign(size, size);
0b0b8310
DG
364 if (pending->hpt) {
365 memset(pending->hpt, 0, size);
366 pending->ret = H_SUCCESS;
367 } else {
368 pending->ret = H_NO_MEM;
369 }
370
371 qemu_mutex_lock_iothread();
372
373 if (SPAPR_MACHINE(qdev_get_machine())->pending_hpt == pending) {
374 /* Ready to go */
375 pending->complete = true;
376 } else {
377 /* We've been cancelled, clean ourselves up */
378 free_pending_hpt(pending);
379 }
380
381 qemu_mutex_unlock_iothread();
382 return NULL;
383}
384
385/* Must be called with BQL held */
ce2918cb 386static void cancel_hpt_prepare(SpaprMachineState *spapr)
0b0b8310 387{
ce2918cb 388 SpaprPendingHpt *pending = spapr->pending_hpt;
0b0b8310
DG
389
390 /* Let the thread know it's cancelled */
391 spapr->pending_hpt = NULL;
392
393 if (!pending) {
394 /* Nothing to do */
395 return;
396 }
397
398 if (!pending->complete) {
399 /* thread will clean itself up */
400 return;
401 }
402
403 free_pending_hpt(pending);
404}
405
b55d295e
DG
406/* Convert a return code from the KVM ioctl()s implementing resize HPT
407 * into a PAPR hypercall return code */
408static target_ulong resize_hpt_convert_rc(int ret)
409{
410 if (ret >= 100000) {
411 return H_LONG_BUSY_ORDER_100_SEC;
412 } else if (ret >= 10000) {
413 return H_LONG_BUSY_ORDER_10_SEC;
414 } else if (ret >= 1000) {
415 return H_LONG_BUSY_ORDER_1_SEC;
416 } else if (ret >= 100) {
417 return H_LONG_BUSY_ORDER_100_MSEC;
418 } else if (ret >= 10) {
419 return H_LONG_BUSY_ORDER_10_MSEC;
420 } else if (ret > 0) {
421 return H_LONG_BUSY_ORDER_1_MSEC;
422 }
423
424 switch (ret) {
425 case 0:
426 return H_SUCCESS;
427 case -EPERM:
428 return H_AUTHORITY;
429 case -EINVAL:
430 return H_PARAMETER;
431 case -ENXIO:
432 return H_CLOSED;
433 case -ENOSPC:
434 return H_PTEG_FULL;
435 case -EBUSY:
436 return H_BUSY;
437 case -ENOMEM:
438 return H_NO_MEM;
439 default:
440 return H_HARDWARE;
441 }
442}
443
30f4b05b 444static target_ulong h_resize_hpt_prepare(PowerPCCPU *cpu,
ce2918cb 445 SpaprMachineState *spapr,
30f4b05b
DG
446 target_ulong opcode,
447 target_ulong *args)
448{
449 target_ulong flags = args[0];
0b0b8310 450 int shift = args[1];
ce2918cb 451 SpaprPendingHpt *pending = spapr->pending_hpt;
db50f280 452 uint64_t current_ram_size;
b55d295e 453 int rc;
30f4b05b
DG
454
455 if (spapr->resize_hpt == SPAPR_RESIZE_HPT_DISABLED) {
456 return H_AUTHORITY;
457 }
458
0b0b8310
DG
459 if (!spapr->htab_shift) {
460 /* Radix guest, no HPT */
461 return H_NOT_AVAILABLE;
462 }
463
30f4b05b 464 trace_spapr_h_resize_hpt_prepare(flags, shift);
0b0b8310
DG
465
466 if (flags != 0) {
467 return H_PARAMETER;
468 }
469
470 if (shift && ((shift < 18) || (shift > 46))) {
471 return H_PARAMETER;
472 }
473
db50f280 474 current_ram_size = MACHINE(spapr)->ram_size + get_plugged_memory_size();
0b0b8310
DG
475
476 /* We only allow the guest to allocate an HPT one order above what
477 * we'd normally give them (to stop a small guest claiming a huge
478 * chunk of resources in the HPT */
479 if (shift > (spapr_hpt_shift_for_ramsize(current_ram_size) + 1)) {
480 return H_RESOURCE;
481 }
482
b55d295e
DG
483 rc = kvmppc_resize_hpt_prepare(cpu, flags, shift);
484 if (rc != -ENOSYS) {
485 return resize_hpt_convert_rc(rc);
486 }
487
0b0b8310
DG
488 if (pending) {
489 /* something already in progress */
490 if (pending->shift == shift) {
491 /* and it's suitable */
492 if (pending->complete) {
493 return pending->ret;
494 } else {
495 return H_LONG_BUSY_ORDER_100_MSEC;
496 }
497 }
498
499 /* not suitable, cancel and replace */
500 cancel_hpt_prepare(spapr);
501 }
502
503 if (!shift) {
504 /* nothing to do */
505 return H_SUCCESS;
506 }
507
508 /* start new prepare */
509
ce2918cb 510 pending = g_new0(SpaprPendingHpt, 1);
0b0b8310
DG
511 pending->shift = shift;
512 pending->ret = H_HARDWARE;
513
514 qemu_thread_create(&pending->thread, "sPAPR HPT prepare",
515 hpt_prepare_thread, pending, QEMU_THREAD_DETACHED);
516
517 spapr->pending_hpt = pending;
518
519 /* In theory we could estimate the time more accurately based on
520 * the new size, but there's not much point */
521 return H_LONG_BUSY_ORDER_100_MSEC;
522}
523
524static uint64_t new_hpte_load0(void *htab, uint64_t pteg, int slot)
525{
526 uint8_t *addr = htab;
527
528 addr += pteg * HASH_PTEG_SIZE_64;
529 addr += slot * HASH_PTE_SIZE_64;
530 return ldq_p(addr);
531}
532
533static void new_hpte_store(void *htab, uint64_t pteg, int slot,
534 uint64_t pte0, uint64_t pte1)
535{
536 uint8_t *addr = htab;
537
538 addr += pteg * HASH_PTEG_SIZE_64;
539 addr += slot * HASH_PTE_SIZE_64;
540
541 stq_p(addr, pte0);
542 stq_p(addr + HASH_PTE_SIZE_64 / 2, pte1);
543}
544
545static int rehash_hpte(PowerPCCPU *cpu,
546 const ppc_hash_pte64_t *hptes,
547 void *old_hpt, uint64_t oldsize,
548 void *new_hpt, uint64_t newsize,
549 uint64_t pteg, int slot)
550{
551 uint64_t old_hash_mask = (oldsize >> 7) - 1;
552 uint64_t new_hash_mask = (newsize >> 7) - 1;
553 target_ulong pte0 = ppc_hash64_hpte0(cpu, hptes, slot);
554 target_ulong pte1;
555 uint64_t avpn;
556 unsigned base_pg_shift;
557 uint64_t hash, new_pteg, replace_pte0;
558
559 if (!(pte0 & HPTE64_V_VALID) || !(pte0 & HPTE64_V_BOLTED)) {
560 return H_SUCCESS;
561 }
562
563 pte1 = ppc_hash64_hpte1(cpu, hptes, slot);
564
565 base_pg_shift = ppc_hash64_hpte_page_shift_noslb(cpu, pte0, pte1);
566 assert(base_pg_shift); /* H_ENTER shouldn't allow a bad encoding */
567 avpn = HPTE64_V_AVPN_VAL(pte0) & ~(((1ULL << base_pg_shift) - 1) >> 23);
568
569 if (pte0 & HPTE64_V_SECONDARY) {
570 pteg = ~pteg;
571 }
572
573 if ((pte0 & HPTE64_V_SSIZE) == HPTE64_V_SSIZE_256M) {
574 uint64_t offset, vsid;
575
576 /* We only have 28 - 23 bits of offset in avpn */
577 offset = (avpn & 0x1f) << 23;
578 vsid = avpn >> 5;
579 /* We can find more bits from the pteg value */
580 if (base_pg_shift < 23) {
581 offset |= ((vsid ^ pteg) & old_hash_mask) << base_pg_shift;
582 }
583
584 hash = vsid ^ (offset >> base_pg_shift);
585 } else if ((pte0 & HPTE64_V_SSIZE) == HPTE64_V_SSIZE_1T) {
586 uint64_t offset, vsid;
587
588 /* We only have 40 - 23 bits of seg_off in avpn */
589 offset = (avpn & 0x1ffff) << 23;
590 vsid = avpn >> 17;
591 if (base_pg_shift < 23) {
592 offset |= ((vsid ^ (vsid << 25) ^ pteg) & old_hash_mask)
593 << base_pg_shift;
594 }
595
596 hash = vsid ^ (vsid << 25) ^ (offset >> base_pg_shift);
597 } else {
598 error_report("rehash_pte: Bad segment size in HPTE");
599 return H_HARDWARE;
600 }
601
602 new_pteg = hash & new_hash_mask;
603 if (pte0 & HPTE64_V_SECONDARY) {
604 assert(~pteg == (hash & old_hash_mask));
605 new_pteg = ~new_pteg;
606 } else {
607 assert(pteg == (hash & old_hash_mask));
608 }
609 assert((oldsize != newsize) || (pteg == new_pteg));
610 replace_pte0 = new_hpte_load0(new_hpt, new_pteg, slot);
611 /*
612 * Strictly speaking, we don't need all these tests, since we only
613 * ever rehash bolted HPTEs. We might in future handle non-bolted
614 * HPTEs, though so make the logic correct for those cases as
615 * well.
616 */
617 if (replace_pte0 & HPTE64_V_VALID) {
618 assert(newsize < oldsize);
619 if (replace_pte0 & HPTE64_V_BOLTED) {
620 if (pte0 & HPTE64_V_BOLTED) {
621 /* Bolted collision, nothing we can do */
622 return H_PTEG_FULL;
623 } else {
624 /* Discard this hpte */
625 return H_SUCCESS;
626 }
627 }
628 }
629
630 new_hpte_store(new_hpt, new_pteg, slot, pte0, pte1);
631 return H_SUCCESS;
632}
633
634static int rehash_hpt(PowerPCCPU *cpu,
635 void *old_hpt, uint64_t oldsize,
636 void *new_hpt, uint64_t newsize)
637{
638 uint64_t n_ptegs = oldsize >> 7;
639 uint64_t pteg;
640 int slot;
641 int rc;
642
643 for (pteg = 0; pteg < n_ptegs; pteg++) {
644 hwaddr ptex = pteg * HPTES_PER_GROUP;
645 const ppc_hash_pte64_t *hptes
646 = ppc_hash64_map_hptes(cpu, ptex, HPTES_PER_GROUP);
647
648 if (!hptes) {
649 return H_HARDWARE;
650 }
651
652 for (slot = 0; slot < HPTES_PER_GROUP; slot++) {
653 rc = rehash_hpte(cpu, hptes, old_hpt, oldsize, new_hpt, newsize,
654 pteg, slot);
655 if (rc != H_SUCCESS) {
656 ppc_hash64_unmap_hptes(cpu, hptes, ptex, HPTES_PER_GROUP);
657 return rc;
658 }
659 }
660 ppc_hash64_unmap_hptes(cpu, hptes, ptex, HPTES_PER_GROUP);
661 }
662
663 return H_SUCCESS;
30f4b05b
DG
664}
665
1ec26c75
GK
666static void do_push_sregs_to_kvm_pr(CPUState *cs, run_on_cpu_data data)
667{
668 int ret;
669
670 cpu_synchronize_state(cs);
671
672 ret = kvmppc_put_books_sregs(POWERPC_CPU(cs));
673 if (ret < 0) {
674 error_report("failed to push sregs to KVM: %s", strerror(-ret));
675 exit(1);
676 }
677}
678
ce2918cb 679static void push_sregs_to_kvm_pr(SpaprMachineState *spapr)
1ec26c75
GK
680{
681 CPUState *cs;
682
683 /*
684 * This is a hack for the benefit of KVM PR - it abuses the SDR1
685 * slot in kvm_sregs to communicate the userspace address of the
686 * HPT
687 */
688 if (!kvm_enabled() || !spapr->htab) {
689 return;
690 }
691
692 CPU_FOREACH(cs) {
693 run_on_cpu(cs, do_push_sregs_to_kvm_pr, RUN_ON_CPU_NULL);
694 }
695}
696
30f4b05b 697static target_ulong h_resize_hpt_commit(PowerPCCPU *cpu,
ce2918cb 698 SpaprMachineState *spapr,
30f4b05b
DG
699 target_ulong opcode,
700 target_ulong *args)
701{
702 target_ulong flags = args[0];
703 target_ulong shift = args[1];
ce2918cb 704 SpaprPendingHpt *pending = spapr->pending_hpt;
0b0b8310
DG
705 int rc;
706 size_t newsize;
30f4b05b
DG
707
708 if (spapr->resize_hpt == SPAPR_RESIZE_HPT_DISABLED) {
709 return H_AUTHORITY;
710 }
711
94789567
DHB
712 if (!spapr->htab_shift) {
713 /* Radix guest, no HPT */
714 return H_NOT_AVAILABLE;
715 }
716
30f4b05b 717 trace_spapr_h_resize_hpt_commit(flags, shift);
0b0b8310 718
b55d295e
DG
719 rc = kvmppc_resize_hpt_commit(cpu, flags, shift);
720 if (rc != -ENOSYS) {
94789567
DHB
721 rc = resize_hpt_convert_rc(rc);
722 if (rc == H_SUCCESS) {
723 /* Need to set the new htab_shift in the machine state */
724 spapr->htab_shift = shift;
725 }
726 return rc;
b55d295e
DG
727 }
728
0b0b8310
DG
729 if (flags != 0) {
730 return H_PARAMETER;
731 }
732
733 if (!pending || (pending->shift != shift)) {
734 /* no matching prepare */
735 return H_CLOSED;
736 }
737
738 if (!pending->complete) {
739 /* prepare has not completed */
740 return H_BUSY;
741 }
742
743 /* Shouldn't have got past PREPARE without an HPT */
744 g_assert(spapr->htab_shift);
745
746 newsize = 1ULL << pending->shift;
747 rc = rehash_hpt(cpu, spapr->htab, HTAB_SIZE(spapr),
748 pending->hpt, newsize);
749 if (rc == H_SUCCESS) {
750 qemu_vfree(spapr->htab);
751 spapr->htab = pending->hpt;
752 spapr->htab_shift = pending->shift;
753
1ec26c75 754 push_sregs_to_kvm_pr(spapr);
b55d295e 755
0b0b8310
DG
756 pending->hpt = NULL; /* so it's not free()d */
757 }
758
759 /* Clean up */
760 spapr->pending_hpt = NULL;
761 free_pending_hpt(pending);
762
763 return rc;
30f4b05b
DG
764}
765
ce2918cb 766static target_ulong h_set_sprg0(PowerPCCPU *cpu, SpaprMachineState *spapr,
423576f7
TH
767 target_ulong opcode, target_ulong *args)
768{
769 cpu_synchronize_state(CPU(cpu));
770 cpu->env.spr[SPR_SPRG0] = args[0];
771
772 return H_SUCCESS;
773}
774
ce2918cb 775static target_ulong h_set_dabr(PowerPCCPU *cpu, SpaprMachineState *spapr,
821303f5
DG
776 target_ulong opcode, target_ulong *args)
777{
af08a58f
TH
778 if (!has_spr(cpu, SPR_DABR)) {
779 return H_HARDWARE; /* DABR register not available */
780 }
781 cpu_synchronize_state(CPU(cpu));
782
783 if (has_spr(cpu, SPR_DABRX)) {
784 cpu->env.spr[SPR_DABRX] = 0x3; /* Use Problem and Privileged state */
785 } else if (!(args[0] & 0x4)) { /* Breakpoint Translation set? */
786 return H_RESERVED_DABR;
787 }
788
789 cpu->env.spr[SPR_DABR] = args[0];
790 return H_SUCCESS;
821303f5
DG
791}
792
ce2918cb 793static target_ulong h_set_xdabr(PowerPCCPU *cpu, SpaprMachineState *spapr,
e49ff266
TH
794 target_ulong opcode, target_ulong *args)
795{
796 target_ulong dabrx = args[1];
797
798 if (!has_spr(cpu, SPR_DABR) || !has_spr(cpu, SPR_DABRX)) {
799 return H_HARDWARE;
800 }
801
802 if ((dabrx & ~0xfULL) != 0 || (dabrx & H_DABRX_HYPERVISOR) != 0
803 || (dabrx & (H_DABRX_KERNEL | H_DABRX_USER)) == 0) {
804 return H_PARAMETER;
805 }
806
807 cpu_synchronize_state(CPU(cpu));
808 cpu->env.spr[SPR_DABRX] = dabrx;
809 cpu->env.spr[SPR_DABR] = args[0];
810
811 return H_SUCCESS;
812}
813
ce2918cb 814static target_ulong h_page_init(PowerPCCPU *cpu, SpaprMachineState *spapr,
3240dd9a
TH
815 target_ulong opcode, target_ulong *args)
816{
817 target_ulong flags = args[0];
818 hwaddr dst = args[1];
819 hwaddr src = args[2];
820 hwaddr len = TARGET_PAGE_SIZE;
821 uint8_t *pdst, *psrc;
822 target_long ret = H_SUCCESS;
823
824 if (flags & ~(H_ICACHE_SYNCHRONIZE | H_ICACHE_INVALIDATE
825 | H_COPY_PAGE | H_ZERO_PAGE)) {
826 qemu_log_mask(LOG_UNIMP, "h_page_init: Bad flags (" TARGET_FMT_lx "\n",
827 flags);
828 return H_PARAMETER;
829 }
830
831 /* Map-in destination */
832 if (!is_ram_address(spapr, dst) || (dst & ~TARGET_PAGE_MASK) != 0) {
833 return H_PARAMETER;
834 }
85eb7c18 835 pdst = cpu_physical_memory_map(dst, &len, true);
3240dd9a
TH
836 if (!pdst || len != TARGET_PAGE_SIZE) {
837 return H_PARAMETER;
838 }
839
840 if (flags & H_COPY_PAGE) {
841 /* Map-in source, copy to destination, and unmap source again */
842 if (!is_ram_address(spapr, src) || (src & ~TARGET_PAGE_MASK) != 0) {
843 ret = H_PARAMETER;
844 goto unmap_out;
845 }
85eb7c18 846 psrc = cpu_physical_memory_map(src, &len, false);
3240dd9a
TH
847 if (!psrc || len != TARGET_PAGE_SIZE) {
848 ret = H_PARAMETER;
849 goto unmap_out;
850 }
851 memcpy(pdst, psrc, len);
852 cpu_physical_memory_unmap(psrc, len, 0, len);
853 } else if (flags & H_ZERO_PAGE) {
854 memset(pdst, 0, len); /* Just clear the destination page */
855 }
856
857 if (kvm_enabled() && (flags & H_ICACHE_SYNCHRONIZE) != 0) {
858 kvmppc_dcbst_range(cpu, pdst, len);
859 }
860 if (flags & (H_ICACHE_SYNCHRONIZE | H_ICACHE_INVALIDATE)) {
861 if (kvm_enabled()) {
862 kvmppc_icbi_range(cpu, pdst, len);
863 } else {
864 tb_flush(CPU(cpu));
865 }
866 }
867
868unmap_out:
869 cpu_physical_memory_unmap(pdst, TARGET_PAGE_SIZE, 1, len);
870 return ret;
871}
872
ed120055
DG
873#define FLAGS_REGISTER_VPA 0x0000200000000000ULL
874#define FLAGS_REGISTER_DTL 0x0000400000000000ULL
875#define FLAGS_REGISTER_SLBSHADOW 0x0000600000000000ULL
876#define FLAGS_DEREGISTER_VPA 0x0000a00000000000ULL
877#define FLAGS_DEREGISTER_DTL 0x0000c00000000000ULL
878#define FLAGS_DEREGISTER_SLBSHADOW 0x0000e00000000000ULL
879
7388efaf 880static target_ulong register_vpa(PowerPCCPU *cpu, target_ulong vpa)
ed120055 881{
7388efaf
DG
882 CPUState *cs = CPU(cpu);
883 CPUPPCState *env = &cpu->env;
ce2918cb 884 SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
ed120055
DG
885 uint16_t size;
886 uint8_t tmp;
887
888 if (vpa == 0) {
889 hcall_dprintf("Can't cope with registering a VPA at logical 0\n");
890 return H_HARDWARE;
891 }
892
893 if (vpa % env->dcache_line_size) {
894 return H_PARAMETER;
895 }
896 /* FIXME: bounds check the address */
897
41701aa4 898 size = lduw_be_phys(cs->as, vpa + 0x4);
ed120055
DG
899
900 if (size < VPA_MIN_SIZE) {
901 return H_PARAMETER;
902 }
903
904 /* VPA is not allowed to cross a page boundary */
905 if ((vpa / 4096) != ((vpa + size - 1) / 4096)) {
906 return H_PARAMETER;
907 }
908
7388efaf 909 spapr_cpu->vpa_addr = vpa;
ed120055 910
7388efaf 911 tmp = ldub_phys(cs->as, spapr_cpu->vpa_addr + VPA_SHARED_PROC_OFFSET);
ed120055 912 tmp |= VPA_SHARED_PROC_VAL;
7388efaf 913 stb_phys(cs->as, spapr_cpu->vpa_addr + VPA_SHARED_PROC_OFFSET, tmp);
ed120055
DG
914
915 return H_SUCCESS;
916}
917
7388efaf 918static target_ulong deregister_vpa(PowerPCCPU *cpu, target_ulong vpa)
ed120055 919{
ce2918cb 920 SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
7388efaf
DG
921
922 if (spapr_cpu->slb_shadow_addr) {
ed120055
DG
923 return H_RESOURCE;
924 }
925
7388efaf 926 if (spapr_cpu->dtl_addr) {
ed120055
DG
927 return H_RESOURCE;
928 }
929
7388efaf 930 spapr_cpu->vpa_addr = 0;
ed120055
DG
931 return H_SUCCESS;
932}
933
7388efaf 934static target_ulong register_slb_shadow(PowerPCCPU *cpu, target_ulong addr)
ed120055 935{
ce2918cb 936 SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
ed120055
DG
937 uint32_t size;
938
939 if (addr == 0) {
940 hcall_dprintf("Can't cope with SLB shadow at logical 0\n");
941 return H_HARDWARE;
942 }
943
7388efaf 944 size = ldl_be_phys(CPU(cpu)->as, addr + 0x4);
ed120055
DG
945 if (size < 0x8) {
946 return H_PARAMETER;
947 }
948
949 if ((addr / 4096) != ((addr + size - 1) / 4096)) {
950 return H_PARAMETER;
951 }
952
7388efaf 953 if (!spapr_cpu->vpa_addr) {
ed120055
DG
954 return H_RESOURCE;
955 }
956
7388efaf
DG
957 spapr_cpu->slb_shadow_addr = addr;
958 spapr_cpu->slb_shadow_size = size;
ed120055
DG
959
960 return H_SUCCESS;
961}
962
7388efaf 963static target_ulong deregister_slb_shadow(PowerPCCPU *cpu, target_ulong addr)
ed120055 964{
ce2918cb 965 SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
7388efaf
DG
966
967 spapr_cpu->slb_shadow_addr = 0;
968 spapr_cpu->slb_shadow_size = 0;
ed120055
DG
969 return H_SUCCESS;
970}
971
7388efaf 972static target_ulong register_dtl(PowerPCCPU *cpu, target_ulong addr)
ed120055 973{
ce2918cb 974 SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
ed120055
DG
975 uint32_t size;
976
977 if (addr == 0) {
978 hcall_dprintf("Can't cope with DTL at logical 0\n");
979 return H_HARDWARE;
980 }
981
7388efaf 982 size = ldl_be_phys(CPU(cpu)->as, addr + 0x4);
ed120055
DG
983
984 if (size < 48) {
985 return H_PARAMETER;
986 }
987
7388efaf 988 if (!spapr_cpu->vpa_addr) {
ed120055
DG
989 return H_RESOURCE;
990 }
991
7388efaf
DG
992 spapr_cpu->dtl_addr = addr;
993 spapr_cpu->dtl_size = size;
ed120055
DG
994
995 return H_SUCCESS;
996}
997
7388efaf 998static target_ulong deregister_dtl(PowerPCCPU *cpu, target_ulong addr)
ed120055 999{
ce2918cb 1000 SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
7388efaf
DG
1001
1002 spapr_cpu->dtl_addr = 0;
1003 spapr_cpu->dtl_size = 0;
ed120055
DG
1004
1005 return H_SUCCESS;
1006}
1007
ce2918cb 1008static target_ulong h_register_vpa(PowerPCCPU *cpu, SpaprMachineState *spapr,
ed120055
DG
1009 target_ulong opcode, target_ulong *args)
1010{
1011 target_ulong flags = args[0];
1012 target_ulong procno = args[1];
1013 target_ulong vpa = args[2];
1014 target_ulong ret = H_PARAMETER;
0f20ba62 1015 PowerPCCPU *tcpu;
ed120055 1016
2e886fb3 1017 tcpu = spapr_find_cpu(procno);
5353d03d 1018 if (!tcpu) {
ed120055
DG
1019 return H_PARAMETER;
1020 }
1021
1022 switch (flags) {
1023 case FLAGS_REGISTER_VPA:
7388efaf 1024 ret = register_vpa(tcpu, vpa);
ed120055
DG
1025 break;
1026
1027 case FLAGS_DEREGISTER_VPA:
7388efaf 1028 ret = deregister_vpa(tcpu, vpa);
ed120055
DG
1029 break;
1030
1031 case FLAGS_REGISTER_SLBSHADOW:
7388efaf 1032 ret = register_slb_shadow(tcpu, vpa);
ed120055
DG
1033 break;
1034
1035 case FLAGS_DEREGISTER_SLBSHADOW:
7388efaf 1036 ret = deregister_slb_shadow(tcpu, vpa);
ed120055
DG
1037 break;
1038
1039 case FLAGS_REGISTER_DTL:
7388efaf 1040 ret = register_dtl(tcpu, vpa);
ed120055
DG
1041 break;
1042
1043 case FLAGS_DEREGISTER_DTL:
7388efaf 1044 ret = deregister_dtl(tcpu, vpa);
ed120055
DG
1045 break;
1046 }
1047
1048 return ret;
1049}
1050
ce2918cb 1051static target_ulong h_cede(PowerPCCPU *cpu, SpaprMachineState *spapr,
ed120055
DG
1052 target_ulong opcode, target_ulong *args)
1053{
b13ce26d 1054 CPUPPCState *env = &cpu->env;
fcd7d003 1055 CPUState *cs = CPU(cpu);
3a6e6224 1056 SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
b13ce26d 1057
ed120055
DG
1058 env->msr |= (1ULL << MSR_EE);
1059 hreg_compute_hflags(env);
3a6e6224
NP
1060
1061 if (spapr_cpu->prod) {
1062 spapr_cpu->prod = false;
1063 return H_SUCCESS;
1064 }
1065
fcd7d003 1066 if (!cpu_has_work(cs)) {
259186a7 1067 cs->halted = 1;
27103424 1068 cs->exception_index = EXCP_HLT;
fcd7d003 1069 cs->exit_request = 1;
ed120055 1070 }
3a6e6224
NP
1071
1072 return H_SUCCESS;
1073}
1074
10741314
NP
1075/*
1076 * Confer to self, aka join. Cede could use the same pattern as well, if
1077 * EXCP_HLT can be changed to ECXP_HALTED.
1078 */
1079static target_ulong h_confer_self(PowerPCCPU *cpu)
1080{
1081 CPUState *cs = CPU(cpu);
1082 SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
1083
1084 if (spapr_cpu->prod) {
1085 spapr_cpu->prod = false;
1086 return H_SUCCESS;
1087 }
1088 cs->halted = 1;
1089 cs->exception_index = EXCP_HALTED;
1090 cs->exit_request = 1;
1091
1092 return H_SUCCESS;
1093}
1094
1095static target_ulong h_join(PowerPCCPU *cpu, SpaprMachineState *spapr,
1096 target_ulong opcode, target_ulong *args)
1097{
1098 CPUPPCState *env = &cpu->env;
1099 CPUState *cs;
1100 bool last_unjoined = true;
1101
1102 if (env->msr & (1ULL << MSR_EE)) {
1103 return H_BAD_MODE;
1104 }
1105
1106 /*
1107 * Must not join the last CPU running. Interestingly, no such restriction
1108 * for H_CONFER-to-self, but that is probably not intended to be used
1109 * when H_JOIN is available.
1110 */
1111 CPU_FOREACH(cs) {
1112 PowerPCCPU *c = POWERPC_CPU(cs);
1113 CPUPPCState *e = &c->env;
1114 if (c == cpu) {
1115 continue;
1116 }
1117
1118 /* Don't have a way to indicate joined, so use halted && MSR[EE]=0 */
1119 if (!cs->halted || (e->msr & (1ULL << MSR_EE))) {
1120 last_unjoined = false;
1121 break;
1122 }
1123 }
1124 if (last_unjoined) {
1125 return H_CONTINUE;
1126 }
1127
1128 return h_confer_self(cpu);
1129}
1130
e8ce0e40
NP
1131static target_ulong h_confer(PowerPCCPU *cpu, SpaprMachineState *spapr,
1132 target_ulong opcode, target_ulong *args)
1133{
1134 target_long target = args[0];
1135 uint32_t dispatch = args[1];
1136 CPUState *cs = CPU(cpu);
1137 SpaprCpuState *spapr_cpu;
1138
1139 /*
1140 * -1 means confer to all other CPUs without dispatch counter check,
1141 * otherwise it's a targeted confer.
1142 */
1143 if (target != -1) {
1144 PowerPCCPU *target_cpu = spapr_find_cpu(target);
1145 uint32_t target_dispatch;
1146
1147 if (!target_cpu) {
1148 return H_PARAMETER;
1149 }
1150
e8ce0e40
NP
1151 /*
1152 * target == self is a special case, we wait until prodded, without
1153 * dispatch counter check.
1154 */
1155 if (cpu == target_cpu) {
10741314 1156 return h_confer_self(cpu);
e8ce0e40
NP
1157 }
1158
10741314 1159 spapr_cpu = spapr_cpu_state(target_cpu);
e8ce0e40
NP
1160 if (!spapr_cpu->vpa_addr || ((dispatch & 1) == 0)) {
1161 return H_SUCCESS;
1162 }
1163
1164 target_dispatch = ldl_be_phys(cs->as,
1165 spapr_cpu->vpa_addr + VPA_DISPATCH_COUNTER);
1166 if (target_dispatch != dispatch) {
1167 return H_SUCCESS;
1168 }
1169
1170 /*
1171 * The targeted confer does not do anything special beyond yielding
1172 * the current vCPU, but even this should be better than nothing.
1173 * At least for single-threaded tcg, it gives the target a chance to
1174 * run before we run again. Multi-threaded tcg does not really do
1175 * anything with EXCP_YIELD yet.
1176 */
1177 }
1178
1179 cs->exception_index = EXCP_YIELD;
1180 cs->exit_request = 1;
1181 cpu_loop_exit(cs);
1182
1183 return H_SUCCESS;
1184}
1185
3a6e6224
NP
1186static target_ulong h_prod(PowerPCCPU *cpu, SpaprMachineState *spapr,
1187 target_ulong opcode, target_ulong *args)
1188{
1189 target_long target = args[0];
1190 PowerPCCPU *tcpu;
1191 CPUState *cs;
1192 SpaprCpuState *spapr_cpu;
1193
1194 tcpu = spapr_find_cpu(target);
1195 cs = CPU(tcpu);
1196 if (!cs) {
1197 return H_PARAMETER;
1198 }
1199
1200 spapr_cpu = spapr_cpu_state(tcpu);
1201 spapr_cpu->prod = true;
1202 cs->halted = 0;
1203 qemu_cpu_kick(cs);
1204
ed120055
DG
1205 return H_SUCCESS;
1206}
1207
ce2918cb 1208static target_ulong h_rtas(PowerPCCPU *cpu, SpaprMachineState *spapr,
39ac8455
DG
1209 target_ulong opcode, target_ulong *args)
1210{
1211 target_ulong rtas_r3 = args[0];
4fe822e0
AK
1212 uint32_t token = rtas_ld(rtas_r3, 0);
1213 uint32_t nargs = rtas_ld(rtas_r3, 1);
1214 uint32_t nret = rtas_ld(rtas_r3, 2);
39ac8455 1215
210b580b 1216 return spapr_rtas_call(cpu, spapr, token, nargs, rtas_r3 + 12,
39ac8455
DG
1217 nret, rtas_r3 + 12 + 4*nargs);
1218}
1219
ce2918cb 1220static target_ulong h_logical_load(PowerPCCPU *cpu, SpaprMachineState *spapr,
827200a2
DG
1221 target_ulong opcode, target_ulong *args)
1222{
fdfba1a2 1223 CPUState *cs = CPU(cpu);
827200a2
DG
1224 target_ulong size = args[0];
1225 target_ulong addr = args[1];
1226
1227 switch (size) {
1228 case 1:
2c17449b 1229 args[0] = ldub_phys(cs->as, addr);
827200a2
DG
1230 return H_SUCCESS;
1231 case 2:
41701aa4 1232 args[0] = lduw_phys(cs->as, addr);
827200a2
DG
1233 return H_SUCCESS;
1234 case 4:
fdfba1a2 1235 args[0] = ldl_phys(cs->as, addr);
827200a2
DG
1236 return H_SUCCESS;
1237 case 8:
2c17449b 1238 args[0] = ldq_phys(cs->as, addr);
827200a2
DG
1239 return H_SUCCESS;
1240 }
1241 return H_PARAMETER;
1242}
1243
ce2918cb 1244static target_ulong h_logical_store(PowerPCCPU *cpu, SpaprMachineState *spapr,
827200a2
DG
1245 target_ulong opcode, target_ulong *args)
1246{
f606604f
EI
1247 CPUState *cs = CPU(cpu);
1248
827200a2
DG
1249 target_ulong size = args[0];
1250 target_ulong addr = args[1];
1251 target_ulong val = args[2];
1252
1253 switch (size) {
1254 case 1:
db3be60d 1255 stb_phys(cs->as, addr, val);
827200a2
DG
1256 return H_SUCCESS;
1257 case 2:
5ce5944d 1258 stw_phys(cs->as, addr, val);
827200a2
DG
1259 return H_SUCCESS;
1260 case 4:
ab1da857 1261 stl_phys(cs->as, addr, val);
827200a2
DG
1262 return H_SUCCESS;
1263 case 8:
f606604f 1264 stq_phys(cs->as, addr, val);
827200a2
DG
1265 return H_SUCCESS;
1266 }
1267 return H_PARAMETER;
1268}
1269
ce2918cb 1270static target_ulong h_logical_memop(PowerPCCPU *cpu, SpaprMachineState *spapr,
c73e3771
BH
1271 target_ulong opcode, target_ulong *args)
1272{
fdfba1a2
EI
1273 CPUState *cs = CPU(cpu);
1274
c73e3771
BH
1275 target_ulong dst = args[0]; /* Destination address */
1276 target_ulong src = args[1]; /* Source address */
1277 target_ulong esize = args[2]; /* Element size (0=1,1=2,2=4,3=8) */
1278 target_ulong count = args[3]; /* Element count */
1279 target_ulong op = args[4]; /* 0 = copy, 1 = invert */
1280 uint64_t tmp;
1281 unsigned int mask = (1 << esize) - 1;
1282 int step = 1 << esize;
1283
1284 if (count > 0x80000000) {
1285 return H_PARAMETER;
1286 }
1287
1288 if ((dst & mask) || (src & mask) || (op > 1)) {
1289 return H_PARAMETER;
1290 }
1291
1292 if (dst >= src && dst < (src + (count << esize))) {
1293 dst = dst + ((count - 1) << esize);
1294 src = src + ((count - 1) << esize);
1295 step = -step;
1296 }
1297
1298 while (count--) {
1299 switch (esize) {
1300 case 0:
2c17449b 1301 tmp = ldub_phys(cs->as, src);
c73e3771
BH
1302 break;
1303 case 1:
41701aa4 1304 tmp = lduw_phys(cs->as, src);
c73e3771
BH
1305 break;
1306 case 2:
fdfba1a2 1307 tmp = ldl_phys(cs->as, src);
c73e3771
BH
1308 break;
1309 case 3:
2c17449b 1310 tmp = ldq_phys(cs->as, src);
c73e3771
BH
1311 break;
1312 default:
1313 return H_PARAMETER;
1314 }
1315 if (op == 1) {
1316 tmp = ~tmp;
1317 }
1318 switch (esize) {
1319 case 0:
db3be60d 1320 stb_phys(cs->as, dst, tmp);
c73e3771
BH
1321 break;
1322 case 1:
5ce5944d 1323 stw_phys(cs->as, dst, tmp);
c73e3771
BH
1324 break;
1325 case 2:
ab1da857 1326 stl_phys(cs->as, dst, tmp);
c73e3771
BH
1327 break;
1328 case 3:
f606604f 1329 stq_phys(cs->as, dst, tmp);
c73e3771
BH
1330 break;
1331 }
1332 dst = dst + step;
1333 src = src + step;
1334 }
1335
1336 return H_SUCCESS;
1337}
1338
ce2918cb 1339static target_ulong h_logical_icbi(PowerPCCPU *cpu, SpaprMachineState *spapr,
827200a2
DG
1340 target_ulong opcode, target_ulong *args)
1341{
1342 /* Nothing to do on emulation, KVM will trap this in the kernel */
1343 return H_SUCCESS;
1344}
1345
ce2918cb 1346static target_ulong h_logical_dcbf(PowerPCCPU *cpu, SpaprMachineState *spapr,
827200a2
DG
1347 target_ulong opcode, target_ulong *args)
1348{
1349 /* Nothing to do on emulation, KVM will trap this in the kernel */
1350 return H_SUCCESS;
1351}
1352
7d0cd464 1353static target_ulong h_set_mode_resource_le(PowerPCCPU *cpu,
c4c81d7d 1354 SpaprMachineState *spapr,
7d0cd464
PM
1355 target_ulong mflags,
1356 target_ulong value1,
1357 target_ulong value2)
42561bf2 1358{
c4015bbd
AK
1359 if (value1) {
1360 return H_P3;
1361 }
1362 if (value2) {
1363 return H_P4;
1364 }
1365
1366 switch (mflags) {
1367 case H_SET_MODE_ENDIAN_BIG:
00fd075e 1368 spapr_set_all_lpcrs(0, LPCR_ILE);
c4c81d7d 1369 spapr_pci_switch_vga(spapr, true);
c4015bbd
AK
1370 return H_SUCCESS;
1371
1372 case H_SET_MODE_ENDIAN_LITTLE:
00fd075e 1373 spapr_set_all_lpcrs(LPCR_ILE, LPCR_ILE);
c4c81d7d 1374 spapr_pci_switch_vga(spapr, false);
c4015bbd
AK
1375 return H_SUCCESS;
1376 }
42561bf2 1377
c4015bbd
AK
1378 return H_UNSUPPORTED_FLAG;
1379}
42561bf2 1380
7d0cd464
PM
1381static target_ulong h_set_mode_resource_addr_trans_mode(PowerPCCPU *cpu,
1382 target_ulong mflags,
1383 target_ulong value1,
1384 target_ulong value2)
d5ac4f54 1385{
d5ac4f54 1386 PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
d5ac4f54
AK
1387
1388 if (!(pcc->insns_flags2 & PPC2_ISA207S)) {
1389 return H_P2;
1390 }
1391 if (value1) {
1392 return H_P3;
1393 }
1394 if (value2) {
1395 return H_P4;
1396 }
1397
5c94b2a5 1398 if (mflags == AIL_RESERVED) {
d5ac4f54
AK
1399 return H_UNSUPPORTED_FLAG;
1400 }
1401
00fd075e 1402 spapr_set_all_lpcrs(mflags << LPCR_AIL_SHIFT, LPCR_AIL);
d5ac4f54
AK
1403
1404 return H_SUCCESS;
1405}
1406
ce2918cb 1407static target_ulong h_set_mode(PowerPCCPU *cpu, SpaprMachineState *spapr,
c4015bbd
AK
1408 target_ulong opcode, target_ulong *args)
1409{
1410 target_ulong resource = args[1];
1411 target_ulong ret = H_P2;
1412
1413 switch (resource) {
1414 case H_SET_MODE_RESOURCE_LE:
c4c81d7d 1415 ret = h_set_mode_resource_le(cpu, spapr, args[0], args[2], args[3]);
c4015bbd 1416 break;
d5ac4f54 1417 case H_SET_MODE_RESOURCE_ADDR_TRANS_MODE:
7d0cd464
PM
1418 ret = h_set_mode_resource_addr_trans_mode(cpu, args[0],
1419 args[2], args[3]);
d5ac4f54 1420 break;
42561bf2
AB
1421 }
1422
42561bf2
AB
1423 return ret;
1424}
1425
ce2918cb 1426static target_ulong h_clean_slb(PowerPCCPU *cpu, SpaprMachineState *spapr,
d77a98b0
SJS
1427 target_ulong opcode, target_ulong *args)
1428{
1429 qemu_log_mask(LOG_UNIMP, "Unimplemented SPAPR hcall 0x"TARGET_FMT_lx"%s\n",
1430 opcode, " (H_CLEAN_SLB)");
1431 return H_FUNCTION;
1432}
1433
ce2918cb 1434static target_ulong h_invalidate_pid(PowerPCCPU *cpu, SpaprMachineState *spapr,
d77a98b0
SJS
1435 target_ulong opcode, target_ulong *args)
1436{
1437 qemu_log_mask(LOG_UNIMP, "Unimplemented SPAPR hcall 0x"TARGET_FMT_lx"%s\n",
1438 opcode, " (H_INVALIDATE_PID)");
1439 return H_FUNCTION;
1440}
1441
ce2918cb 1442static void spapr_check_setup_free_hpt(SpaprMachineState *spapr,
b4db5413
SJS
1443 uint64_t patbe_old, uint64_t patbe_new)
1444{
1445 /*
1446 * We have 4 Options:
1447 * HASH->HASH || RADIX->RADIX || NOTHING->RADIX : Do Nothing
1448 * HASH->RADIX : Free HPT
1449 * RADIX->HASH : Allocate HPT
1450 * NOTHING->HASH : Allocate HPT
1451 * Note: NOTHING implies the case where we said the guest could choose
1452 * later and so assumed radix and now it's called H_REG_PROC_TBL
1453 */
1454
79825f4d 1455 if ((patbe_old & PATE1_GR) == (patbe_new & PATE1_GR)) {
b4db5413 1456 /* We assume RADIX, so this catches all the "Do Nothing" cases */
79825f4d 1457 } else if (!(patbe_old & PATE1_GR)) {
b4db5413 1458 /* HASH->RADIX : Free HPT */
06ec79e8 1459 spapr_free_hpt(spapr);
79825f4d 1460 } else if (!(patbe_new & PATE1_GR)) {
b4db5413 1461 /* RADIX->HASH || NOTHING->HASH : Allocate HPT */
8897ea5a 1462 spapr_setup_hpt(spapr);
b4db5413
SJS
1463 }
1464 return;
1465}
1466
1467#define FLAGS_MASK 0x01FULL
1468#define FLAG_MODIFY 0x10
1469#define FLAG_REGISTER 0x08
1470#define FLAG_RADIX 0x04
1471#define FLAG_HASH_PROC_TBL 0x02
1472#define FLAG_GTSE 0x01
1473
d77a98b0 1474static target_ulong h_register_process_table(PowerPCCPU *cpu,
ce2918cb 1475 SpaprMachineState *spapr,
d77a98b0
SJS
1476 target_ulong opcode,
1477 target_ulong *args)
1478{
b4db5413
SJS
1479 target_ulong flags = args[0];
1480 target_ulong proc_tbl = args[1];
1481 target_ulong page_size = args[2];
1482 target_ulong table_size = args[3];
176dccee 1483 target_ulong update_lpcr = 0;
b4db5413
SJS
1484 uint64_t cproc;
1485
1486 if (flags & ~FLAGS_MASK) { /* Check no reserved bits are set */
1487 return H_PARAMETER;
1488 }
1489 if (flags & FLAG_MODIFY) {
1490 if (flags & FLAG_REGISTER) {
1491 if (flags & FLAG_RADIX) { /* Register new RADIX process table */
1492 if (proc_tbl & 0xfff || proc_tbl >> 60) {
1493 return H_P2;
1494 } else if (page_size) {
1495 return H_P3;
1496 } else if (table_size > 24) {
1497 return H_P4;
1498 }
79825f4d 1499 cproc = PATE1_GR | proc_tbl | table_size;
b4db5413
SJS
1500 } else { /* Register new HPT process table */
1501 if (flags & FLAG_HASH_PROC_TBL) { /* Hash with Segment Tables */
1502 /* TODO - Not Supported */
1503 /* Technically caused by flag bits => H_PARAMETER */
1504 return H_PARAMETER;
1505 } else { /* Hash with SLB */
1506 if (proc_tbl >> 38) {
1507 return H_P2;
1508 } else if (page_size & ~0x7) {
1509 return H_P3;
1510 } else if (table_size > 24) {
1511 return H_P4;
1512 }
1513 }
1514 cproc = (proc_tbl << 25) | page_size << 5 | table_size;
1515 }
1516
1517 } else { /* Deregister current process table */
79825f4d
BH
1518 /*
1519 * Set to benign value: (current GR) | 0. This allows
1520 * deregistration in KVM to succeed even if the radix bit
1521 * in flags doesn't match the radix bit in the old PATE.
1522 */
1523 cproc = spapr->patb_entry & PATE1_GR;
b4db5413
SJS
1524 }
1525 } else { /* Maintain current registration */
79825f4d 1526 if (!(flags & FLAG_RADIX) != !(spapr->patb_entry & PATE1_GR)) {
b4db5413
SJS
1527 /* Technically caused by flag bits => H_PARAMETER */
1528 return H_PARAMETER; /* Existing Process Table Mismatch */
1529 }
1530 cproc = spapr->patb_entry;
1531 }
1532
1533 /* Check if we need to setup OR free the hpt */
1534 spapr_check_setup_free_hpt(spapr, spapr->patb_entry, cproc);
1535
1536 spapr->patb_entry = cproc; /* Save new process table */
6de83307 1537
00fd075e 1538 /* Update the UPRT, HR and GTSE bits in the LPCR for all cpus */
176dccee
SJS
1539 if (flags & FLAG_RADIX) /* Radix must use process tables, also set HR */
1540 update_lpcr |= (LPCR_UPRT | LPCR_HR);
1541 else if (flags & FLAG_HASH_PROC_TBL) /* Hash with process tables */
1542 update_lpcr |= LPCR_UPRT;
1543 if (flags & FLAG_GTSE) /* Guest translation shootdown enable */
49e9fdd7
DG
1544 update_lpcr |= LPCR_GTSE;
1545
176dccee 1546 spapr_set_all_lpcrs(update_lpcr, LPCR_UPRT | LPCR_HR | LPCR_GTSE);
b4db5413
SJS
1547
1548 if (kvm_enabled()) {
1549 return kvmppc_configure_v3_mmu(cpu, flags & FLAG_RADIX,
1550 flags & FLAG_GTSE, cproc);
1551 }
1552 return H_SUCCESS;
d77a98b0
SJS
1553}
1554
1c7ad77e
NP
1555#define H_SIGNAL_SYS_RESET_ALL -1
1556#define H_SIGNAL_SYS_RESET_ALLBUTSELF -2
1557
1558static target_ulong h_signal_sys_reset(PowerPCCPU *cpu,
ce2918cb 1559 SpaprMachineState *spapr,
1c7ad77e
NP
1560 target_ulong opcode, target_ulong *args)
1561{
1562 target_long target = args[0];
1563 CPUState *cs;
1564
1565 if (target < 0) {
1566 /* Broadcast */
1567 if (target < H_SIGNAL_SYS_RESET_ALLBUTSELF) {
1568 return H_PARAMETER;
1569 }
1570
1571 CPU_FOREACH(cs) {
1572 PowerPCCPU *c = POWERPC_CPU(cs);
1573
1574 if (target == H_SIGNAL_SYS_RESET_ALLBUTSELF) {
1575 if (c == cpu) {
1576 continue;
1577 }
1578 }
1579 run_on_cpu(cs, spapr_do_system_reset_on_cpu, RUN_ON_CPU_NULL);
1580 }
1581 return H_SUCCESS;
1582
1583 } else {
1584 /* Unicast */
2e886fb3 1585 cs = CPU(spapr_find_cpu(target));
f57467e3
SB
1586 if (cs) {
1587 run_on_cpu(cs, spapr_do_system_reset_on_cpu, RUN_ON_CPU_NULL);
1588 return H_SUCCESS;
1c7ad77e
NP
1589 }
1590 return H_PARAMETER;
1591 }
1592}
1593
121afbe4
GK
1594/* Returns either a logical PVR or zero if none was found */
1595static uint32_t cas_check_pvr(PowerPCCPU *cpu, uint32_t max_compat,
1596 target_ulong *addr, bool *raw_mode_supported)
2a6593cb 1597{
152ef803 1598 bool explicit_match = false; /* Matched the CPU's real PVR */
152ef803
DG
1599 uint32_t best_compat = 0;
1600 int i;
3794d548 1601
152ef803
DG
1602 /*
1603 * We scan the supplied table of PVRs looking for two things
1604 * 1. Is our real CPU PVR in the list?
1605 * 2. What's the "best" listed logical PVR
1606 */
1607 for (i = 0; i < 512; ++i) {
3794d548
AK
1608 uint32_t pvr, pvr_mask;
1609
80c33d34
DG
1610 pvr_mask = ldl_be_phys(&address_space_memory, *addr);
1611 pvr = ldl_be_phys(&address_space_memory, *addr + 4);
1612 *addr += 8;
152ef803 1613
3794d548 1614 if (~pvr_mask & pvr) {
152ef803 1615 break; /* Terminator record */
3794d548 1616 }
152ef803
DG
1617
1618 if ((cpu->env.spr[SPR_PVR] & pvr_mask) == (pvr & pvr_mask)) {
1619 explicit_match = true;
1620 } else {
1621 if (ppc_check_compat(cpu, pvr, best_compat, max_compat)) {
1622 best_compat = pvr;
1623 }
1624 }
1625 }
1626
cc7b35b1
GK
1627 *raw_mode_supported = explicit_match;
1628
3794d548 1629 /* Parsing finished */
152ef803 1630 trace_spapr_cas_pvr(cpu->compat_pvr, explicit_match, best_compat);
3794d548 1631
80c33d34
DG
1632 return best_compat;
1633}
3794d548 1634
ad334d89 1635static void spapr_handle_transient_dev_before_cas(SpaprMachineState *spapr)
0c21e073 1636{
4b63db12 1637 Object *drc_container;
0c21e073
DG
1638 ObjectProperty *prop;
1639 ObjectPropertyIterator iter;
1640
1641 drc_container = container_get(object_get_root(), "/dr-connector");
1642 object_property_iter_init(&iter, drc_container);
1643 while ((prop = object_property_iter_next(&iter))) {
4b63db12
GK
1644 SpaprDrc *drc;
1645
0c21e073
DG
1646 if (!strstart(prop->type, "link<", NULL)) {
1647 continue;
1648 }
4b63db12 1649 drc = SPAPR_DR_CONNECTOR(object_property_get_link(drc_container,
552d7f49
MA
1650 prop->name,
1651 &error_abort));
4b63db12
GK
1652
1653 if (spapr_drc_transient(drc)) {
ad334d89 1654 spapr_drc_reset(drc);
0c21e073
DG
1655 }
1656 }
ad334d89
GK
1657
1658 spapr_clear_pending_hotplug_events(spapr);
0c21e073
DG
1659}
1660
91067db1
AK
1661target_ulong do_client_architecture_support(PowerPCCPU *cpu,
1662 SpaprMachineState *spapr,
1663 target_ulong vec,
1664 target_ulong fdt_bufsize)
80c33d34 1665{
91067db1 1666 target_ulong ov_table; /* Working address in data buffer */
80c33d34 1667 uint32_t cas_pvr;
86962462 1668 SpaprOptionVector *ov1_guest, *ov5_guest;
80c33d34 1669 bool guest_radix;
cc7b35b1 1670 bool raw_mode_supported = false;
e7f78db9 1671 bool guest_xive;
12b3868e 1672 CPUState *cs;
087820e3 1673 void *fdt;
121afbe4 1674 uint32_t max_compat = spapr->max_compat_pvr;
12b3868e
GK
1675
1676 /* CAS is supposed to be called early when only the boot vCPU is active. */
1677 CPU_FOREACH(cs) {
1678 if (cs == CPU(cpu)) {
1679 continue;
1680 }
1681 if (!cs->halted) {
1682 warn_report("guest has multiple active vCPUs at CAS, which is not allowed");
1683 return H_MULTI_THREADS_ACTIVE;
1684 }
1685 }
80c33d34 1686
121afbe4
GK
1687 cas_pvr = cas_check_pvr(cpu, max_compat, &vec, &raw_mode_supported);
1688 if (!cas_pvr && (!raw_mode_supported || max_compat)) {
1689 /*
1690 * We couldn't find a suitable compatibility mode, and either
1691 * the guest doesn't support "raw" mode for this CPU, or "raw"
1692 * mode is disabled because a maximum compat mode is set.
1693 */
1694 error_report("Couldn't negotiate a suitable PVR during CAS");
80c33d34
DG
1695 return H_HARDWARE;
1696 }
1697
1698 /* Update CPUs */
1699 if (cpu->compat_pvr != cas_pvr) {
7e92da81
GK
1700 Error *local_err = NULL;
1701
1702 if (ppc_set_compat_all(cas_pvr, &local_err) < 0) {
cc7b35b1
GK
1703 /* We fail to set compat mode (likely because running with KVM PR),
1704 * but maybe we can fallback to raw mode if the guest supports it.
1705 */
1706 if (!raw_mode_supported) {
1707 error_report_err(local_err);
1708 return H_HARDWARE;
1709 }
2c9dfdac 1710 error_free(local_err);
3794d548
AK
1711 }
1712 }
1713
03d196b7 1714 /* For the future use: here @ov_table points to the first option vector */
91067db1 1715 ov_table = vec;
03d196b7 1716
e957f6a9 1717 ov1_guest = spapr_ovec_parse_vector(ov_table, 1);
cbd0d7f3
GK
1718 if (!ov1_guest) {
1719 warn_report("guest didn't provide option vector 1");
1720 return H_PARAMETER;
1721 }
facdb8b6 1722 ov5_guest = spapr_ovec_parse_vector(ov_table, 5);
cbd0d7f3 1723 if (!ov5_guest) {
ce05fa0f 1724 spapr_ovec_cleanup(ov1_guest);
cbd0d7f3
GK
1725 warn_report("guest didn't provide option vector 5");
1726 return H_PARAMETER;
1727 }
9fb4541f
SB
1728 if (spapr_ovec_test(ov5_guest, OV5_MMU_BOTH)) {
1729 error_report("guest requested hash and radix MMU, which is invalid.");
1730 exit(EXIT_FAILURE);
1731 }
e7f78db9
GK
1732 if (spapr_ovec_test(ov5_guest, OV5_XIVE_BOTH)) {
1733 error_report("guest requested an invalid interrupt mode");
1734 exit(EXIT_FAILURE);
1735 }
1736
9fb4541f 1737 guest_radix = spapr_ovec_test(ov5_guest, OV5_MMU_RADIX_300);
2a6593cb 1738
e7f78db9
GK
1739 guest_xive = spapr_ovec_test(ov5_guest, OV5_XIVE_EXPLOIT);
1740
2772cf6b
DG
1741 /*
1742 * HPT resizing is a bit of a special case, because when enabled
1743 * we assume an HPT guest will support it until it says it
1744 * doesn't, instead of assuming it won't support it until it says
1745 * it does. Strictly speaking that approach could break for
1746 * guests which don't make a CAS call, but those are so old we
1747 * don't care about them. Without that assumption we'd have to
1748 * make at least a temporary allocation of an HPT sized for max
1749 * memory, which could be impossibly difficult under KVM HV if
1750 * maxram is large.
1751 */
1752 if (!guest_radix && !spapr_ovec_test(ov5_guest, OV5_HPT_RESIZE)) {
1753 int maxshift = spapr_hpt_shift_for_ramsize(MACHINE(spapr)->maxram_size);
1754
1755 if (spapr->resize_hpt == SPAPR_RESIZE_HPT_REQUIRED) {
1756 error_report(
1757 "h_client_architecture_support: Guest doesn't support HPT resizing, but resize-hpt=required");
1758 exit(1);
1759 }
1760
1761 if (spapr->htab_shift < maxshift) {
1762 /* Guest doesn't know about HPT resizing, so we
1763 * pre-emptively resize for the maximum permitted RAM. At
1764 * the point this is called, nothing should have been
1765 * entered into the existing HPT */
1766 spapr_reallocate_hpt(spapr, maxshift, &error_fatal);
1ec26c75 1767 push_sregs_to_kvm_pr(spapr);
2772cf6b
DG
1768 }
1769 }
1770
facdb8b6
MR
1771 /* NOTE: there are actually a number of ov5 bits where input from the
1772 * guest is always zero, and the platform/QEMU enables them independently
1773 * of guest input. To model these properly we'd want some sort of mask,
1774 * but since they only currently apply to memory migration as defined
1775 * by LoPAPR 1.1, 14.5.4.8, which QEMU doesn't implement, we don't need
6787d27b 1776 * to worry about this for now.
facdb8b6 1777 */
30bf9ed1 1778
6787d27b 1779 /* full range of negotiated ov5 capabilities */
facdb8b6
MR
1780 spapr_ovec_intersect(spapr->ov5_cas, spapr->ov5, ov5_guest);
1781 spapr_ovec_cleanup(ov5_guest);
b4b83312 1782
9fb4541f
SB
1783 if (guest_radix) {
1784 if (kvm_enabled() && !kvmppc_has_cap_mmu_radix()) {
1785 error_report("Guest requested unavailable MMU mode (radix).");
1786 exit(EXIT_FAILURE);
1787 }
9fb4541f
SB
1788 } else {
1789 if (kvm_enabled() && kvmppc_has_cap_mmu_radix()
1790 && !kvmppc_has_cap_mmu_hash_v3()) {
1791 error_report("Guest requested unavailable MMU mode (hash).");
1792 exit(EXIT_FAILURE);
1793 }
1794 }
daa36379 1795 spapr->cas_pre_isa3_guest = !spapr_ovec_test(ov1_guest, OV1_PPC_3_00);
00005f22 1796 spapr_ovec_cleanup(ov1_guest);
13db0cd9 1797
e7f78db9 1798 /*
8deb8019
DG
1799 * Ensure the guest asks for an interrupt mode we support;
1800 * otherwise terminate the boot.
e7f78db9
GK
1801 */
1802 if (guest_xive) {
ca62823b 1803 if (!spapr->irq->xive) {
75de5941
GK
1804 error_report(
1805"Guest requested unavailable interrupt mode (XIVE), try the ic-mode=xive or ic-mode=dual machine property");
e7f78db9
GK
1806 exit(EXIT_FAILURE);
1807 }
1808 } else {
ca62823b 1809 if (!spapr->irq->xics) {
75de5941
GK
1810 error_report(
1811"Guest requested unavailable interrupt mode (XICS), either don't set the ic-mode machine property or try ic-mode=xics or ic-mode=dual");
e7f78db9
GK
1812 exit(EXIT_FAILURE);
1813 }
1814 }
1815
8deb8019
DG
1816 spapr_irq_update_active_intc(spapr);
1817
ad334d89 1818 spapr_handle_transient_dev_before_cas(spapr);
0c21e073 1819
087820e3
GK
1820 /*
1821 * If spapr_machine_reset() did not set up a HPT but one is necessary
1822 * (because the guest isn't going to use radix) then set it up here.
1823 */
1824 if ((spapr->patb_entry & PATE1_GR) && !guest_radix) {
1825 /* legacy hash or new hash: */
1826 spapr_setup_hpt(spapr);
13db0cd9
CLG
1827 }
1828
087820e3
GK
1829 fdt = spapr_build_fdt(spapr, false, fdt_bufsize);
1830
1831 g_free(spapr->fdt_blob);
1832 spapr->fdt_size = fdt_totalsize(fdt);
1833 spapr->fdt_initial_size = spapr->fdt_size;
1834 spapr->fdt_blob = fdt;
2a6593cb
AK
1835
1836 return H_SUCCESS;
1837}
1838
91067db1
AK
1839static target_ulong h_client_architecture_support(PowerPCCPU *cpu,
1840 SpaprMachineState *spapr,
1841 target_ulong opcode,
1842 target_ulong *args)
1843{
1844 target_ulong vec = ppc64_phys_to_real(args[0]);
1845 target_ulong fdt_buf = args[1];
1846 target_ulong fdt_bufsize = args[2];
1847 target_ulong ret;
1848 SpaprDeviceTreeUpdateHeader hdr = { .version_id = 1 };
1849
1850 if (fdt_bufsize < sizeof(hdr)) {
1851 error_report("SLOF provided insufficient CAS buffer "
1852 TARGET_FMT_lu " (min: %zu)", fdt_bufsize, sizeof(hdr));
1853 exit(EXIT_FAILURE);
1854 }
1855
1856 fdt_bufsize -= sizeof(hdr);
1857
1858 ret = do_client_architecture_support(cpu, spapr, vec, fdt_bufsize);
1859 if (ret == H_SUCCESS) {
1860 _FDT((fdt_pack(spapr->fdt_blob)));
1861 spapr->fdt_size = fdt_totalsize(spapr->fdt_blob);
1862 spapr->fdt_initial_size = spapr->fdt_size;
1863
1864 cpu_physical_memory_write(fdt_buf, &hdr, sizeof(hdr));
1865 cpu_physical_memory_write(fdt_buf + sizeof(hdr), spapr->fdt_blob,
1866 spapr->fdt_size);
1867 trace_spapr_cas_continue(spapr->fdt_size + sizeof(hdr));
1868 }
1869
1870 return ret;
1871}
1872
c59704b2 1873static target_ulong h_get_cpu_characteristics(PowerPCCPU *cpu,
ce2918cb 1874 SpaprMachineState *spapr,
c59704b2
SJS
1875 target_ulong opcode,
1876 target_ulong *args)
1877{
1878 uint64_t characteristics = H_CPU_CHAR_HON_BRANCH_HINTS &
1879 ~H_CPU_CHAR_THR_RECONF_TRIG;
1880 uint64_t behaviour = H_CPU_BEHAV_FAVOUR_SECURITY;
1881 uint8_t safe_cache = spapr_get_cap(spapr, SPAPR_CAP_CFPC);
1882 uint8_t safe_bounds_check = spapr_get_cap(spapr, SPAPR_CAP_SBBC);
1883 uint8_t safe_indirect_branch = spapr_get_cap(spapr, SPAPR_CAP_IBS);
8ff43ee4
SJS
1884 uint8_t count_cache_flush_assist = spapr_get_cap(spapr,
1885 SPAPR_CAP_CCF_ASSIST);
c59704b2
SJS
1886
1887 switch (safe_cache) {
1888 case SPAPR_CAP_WORKAROUND:
1889 characteristics |= H_CPU_CHAR_L1D_FLUSH_ORI30;
1890 characteristics |= H_CPU_CHAR_L1D_FLUSH_TRIG2;
1891 characteristics |= H_CPU_CHAR_L1D_THREAD_PRIV;
1892 behaviour |= H_CPU_BEHAV_L1D_FLUSH_PR;
1893 break;
1894 case SPAPR_CAP_FIXED:
1895 break;
1896 default: /* broken */
1897 assert(safe_cache == SPAPR_CAP_BROKEN);
1898 behaviour |= H_CPU_BEHAV_L1D_FLUSH_PR;
1899 break;
1900 }
1901
1902 switch (safe_bounds_check) {
1903 case SPAPR_CAP_WORKAROUND:
1904 characteristics |= H_CPU_CHAR_SPEC_BAR_ORI31;
1905 behaviour |= H_CPU_BEHAV_BNDS_CHK_SPEC_BAR;
1906 break;
1907 case SPAPR_CAP_FIXED:
1908 break;
1909 default: /* broken */
1910 assert(safe_bounds_check == SPAPR_CAP_BROKEN);
1911 behaviour |= H_CPU_BEHAV_BNDS_CHK_SPEC_BAR;
1912 break;
1913 }
1914
1915 switch (safe_indirect_branch) {
399b2896
SJS
1916 case SPAPR_CAP_FIXED_NA:
1917 break;
c76c0d30
SJS
1918 case SPAPR_CAP_FIXED_CCD:
1919 characteristics |= H_CPU_CHAR_CACHE_COUNT_DIS;
1920 break;
1921 case SPAPR_CAP_FIXED_IBS:
c59704b2 1922 characteristics |= H_CPU_CHAR_BCCTRL_SERIALISED;
fa86f592 1923 break;
399b2896
SJS
1924 case SPAPR_CAP_WORKAROUND:
1925 behaviour |= H_CPU_BEHAV_FLUSH_COUNT_CACHE;
8ff43ee4
SJS
1926 if (count_cache_flush_assist) {
1927 characteristics |= H_CPU_CHAR_BCCTR_FLUSH_ASSIST;
1928 }
399b2896 1929 break;
c59704b2
SJS
1930 default: /* broken */
1931 assert(safe_indirect_branch == SPAPR_CAP_BROKEN);
1932 break;
1933 }
1934
1935 args[0] = characteristics;
1936 args[1] = behaviour;
fea35ca4
AK
1937 return H_SUCCESS;
1938}
1939
ce2918cb 1940static target_ulong h_update_dt(PowerPCCPU *cpu, SpaprMachineState *spapr,
fea35ca4
AK
1941 target_ulong opcode, target_ulong *args)
1942{
1943 target_ulong dt = ppc64_phys_to_real(args[0]);
1944 struct fdt_header hdr = { 0 };
1945 unsigned cb;
ce2918cb 1946 SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
fea35ca4
AK
1947 void *fdt;
1948
1949 cpu_physical_memory_read(dt, &hdr, sizeof(hdr));
1950 cb = fdt32_to_cpu(hdr.totalsize);
1951
1952 if (!smc->update_dt_enabled) {
1953 return H_SUCCESS;
1954 }
1955
1956 /* Check that the fdt did not grow out of proportion */
1957 if (cb > spapr->fdt_initial_size * 2) {
1958 trace_spapr_update_dt_failed_size(spapr->fdt_initial_size, cb,
1959 fdt32_to_cpu(hdr.magic));
1960 return H_PARAMETER;
1961 }
1962
1963 fdt = g_malloc0(cb);
1964 cpu_physical_memory_read(dt, fdt, cb);
1965
1966 /* Check the fdt consistency */
1967 if (fdt_check_full(fdt, cb)) {
1968 trace_spapr_update_dt_failed_check(spapr->fdt_initial_size, cb,
1969 fdt32_to_cpu(hdr.magic));
1970 return H_PARAMETER;
1971 }
1972
1973 g_free(spapr->fdt_blob);
1974 spapr->fdt_size = cb;
1975 spapr->fdt_blob = fdt;
1976 trace_spapr_update_dt(cb);
c59704b2
SJS
1977
1978 return H_SUCCESS;
1979}
1980
7d7ba3fe
DG
1981static spapr_hcall_fn papr_hypercall_table[(MAX_HCALL_OPCODE / 4) + 1];
1982static spapr_hcall_fn kvmppc_hypercall_table[KVMPPC_HCALL_MAX - KVMPPC_HCALL_BASE + 1];
0fb6bd07 1983static spapr_hcall_fn svm_hypercall_table[(SVM_HCALL_MAX - SVM_HCALL_BASE) / 4 + 1];
9fdf0c29
DG
1984
1985void spapr_register_hypercall(target_ulong opcode, spapr_hcall_fn fn)
1986{
39ac8455
DG
1987 spapr_hcall_fn *slot;
1988
1989 if (opcode <= MAX_HCALL_OPCODE) {
1990 assert((opcode & 0x3) == 0);
9fdf0c29 1991
39ac8455 1992 slot = &papr_hypercall_table[opcode / 4];
0fb6bd07
MR
1993 } else if (opcode >= SVM_HCALL_BASE && opcode <= SVM_HCALL_MAX) {
1994 /* we only have SVM-related hcall numbers assigned in multiples of 4 */
1995 assert((opcode & 0x3) == 0);
1996
1997 slot = &svm_hypercall_table[(opcode - SVM_HCALL_BASE) / 4];
39ac8455
DG
1998 } else {
1999 assert((opcode >= KVMPPC_HCALL_BASE) && (opcode <= KVMPPC_HCALL_MAX));
9fdf0c29 2000
39ac8455
DG
2001 slot = &kvmppc_hypercall_table[opcode - KVMPPC_HCALL_BASE];
2002 }
9fdf0c29 2003
c89d5299 2004 assert(!(*slot));
39ac8455 2005 *slot = fn;
9fdf0c29
DG
2006}
2007
aa100fa4 2008target_ulong spapr_hypercall(PowerPCCPU *cpu, target_ulong opcode,
9fdf0c29
DG
2009 target_ulong *args)
2010{
ce2918cb 2011 SpaprMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
28e02042 2012
9fdf0c29
DG
2013 if ((opcode <= MAX_HCALL_OPCODE)
2014 && ((opcode & 0x3) == 0)) {
39ac8455
DG
2015 spapr_hcall_fn fn = papr_hypercall_table[opcode / 4];
2016
0fb6bd07
MR
2017 if (fn) {
2018 return fn(cpu, spapr, opcode, args);
2019 }
2020 } else if ((opcode >= SVM_HCALL_BASE) &&
2021 (opcode <= SVM_HCALL_MAX)) {
2022 spapr_hcall_fn fn = svm_hypercall_table[(opcode - SVM_HCALL_BASE) / 4];
2023
39ac8455 2024 if (fn) {
b13ce26d 2025 return fn(cpu, spapr, opcode, args);
39ac8455
DG
2026 }
2027 } else if ((opcode >= KVMPPC_HCALL_BASE) &&
2028 (opcode <= KVMPPC_HCALL_MAX)) {
2029 spapr_hcall_fn fn = kvmppc_hypercall_table[opcode - KVMPPC_HCALL_BASE];
9fdf0c29
DG
2030
2031 if (fn) {
b13ce26d 2032 return fn(cpu, spapr, opcode, args);
9fdf0c29
DG
2033 }
2034 }
2035
aaf87c66
TH
2036 qemu_log_mask(LOG_UNIMP, "Unimplemented SPAPR hcall 0x" TARGET_FMT_lx "\n",
2037 opcode);
9fdf0c29
DG
2038 return H_FUNCTION;
2039}
f43e3525 2040
83f7d43a 2041static void hypercall_register_types(void)
f43e3525
DG
2042{
2043 /* hcall-pft */
2044 spapr_register_hypercall(H_ENTER, h_enter);
2045 spapr_register_hypercall(H_REMOVE, h_remove);
2046 spapr_register_hypercall(H_PROTECT, h_protect);
6bbd5dde 2047 spapr_register_hypercall(H_READ, h_read);
39ac8455 2048
a3d0abae
DG
2049 /* hcall-bulk */
2050 spapr_register_hypercall(H_BULK_REMOVE, h_bulk_remove);
2051
30f4b05b
DG
2052 /* hcall-hpt-resize */
2053 spapr_register_hypercall(H_RESIZE_HPT_PREPARE, h_resize_hpt_prepare);
2054 spapr_register_hypercall(H_RESIZE_HPT_COMMIT, h_resize_hpt_commit);
2055
ed120055
DG
2056 /* hcall-splpar */
2057 spapr_register_hypercall(H_REGISTER_VPA, h_register_vpa);
2058 spapr_register_hypercall(H_CEDE, h_cede);
e8ce0e40 2059 spapr_register_hypercall(H_CONFER, h_confer);
3a6e6224
NP
2060 spapr_register_hypercall(H_PROD, h_prod);
2061
10741314
NP
2062 /* hcall-join */
2063 spapr_register_hypercall(H_JOIN, h_join);
2064
1c7ad77e 2065 spapr_register_hypercall(H_SIGNAL_SYS_RESET, h_signal_sys_reset);
ed120055 2066
423576f7
TH
2067 /* processor register resource access h-calls */
2068 spapr_register_hypercall(H_SET_SPRG0, h_set_sprg0);
af08a58f 2069 spapr_register_hypercall(H_SET_DABR, h_set_dabr);
e49ff266 2070 spapr_register_hypercall(H_SET_XDABR, h_set_xdabr);
3240dd9a 2071 spapr_register_hypercall(H_PAGE_INIT, h_page_init);
423576f7
TH
2072 spapr_register_hypercall(H_SET_MODE, h_set_mode);
2073
d77a98b0
SJS
2074 /* In Memory Table MMU h-calls */
2075 spapr_register_hypercall(H_CLEAN_SLB, h_clean_slb);
2076 spapr_register_hypercall(H_INVALIDATE_PID, h_invalidate_pid);
2077 spapr_register_hypercall(H_REGISTER_PROC_TBL, h_register_process_table);
2078
c59704b2
SJS
2079 /* hcall-get-cpu-characteristics */
2080 spapr_register_hypercall(H_GET_CPU_CHARACTERISTICS,
2081 h_get_cpu_characteristics);
2082
827200a2
DG
2083 /* "debugger" hcalls (also used by SLOF). Note: We do -not- differenciate
2084 * here between the "CI" and the "CACHE" variants, they will use whatever
2085 * mapping attributes qemu is using. When using KVM, the kernel will
2086 * enforce the attributes more strongly
2087 */
2088 spapr_register_hypercall(H_LOGICAL_CI_LOAD, h_logical_load);
2089 spapr_register_hypercall(H_LOGICAL_CI_STORE, h_logical_store);
2090 spapr_register_hypercall(H_LOGICAL_CACHE_LOAD, h_logical_load);
2091 spapr_register_hypercall(H_LOGICAL_CACHE_STORE, h_logical_store);
2092 spapr_register_hypercall(H_LOGICAL_ICBI, h_logical_icbi);
2093 spapr_register_hypercall(H_LOGICAL_DCBF, h_logical_dcbf);
c73e3771 2094 spapr_register_hypercall(KVMPPC_H_LOGICAL_MEMOP, h_logical_memop);
827200a2 2095
39ac8455
DG
2096 /* qemu/KVM-PPC specific hcalls */
2097 spapr_register_hypercall(KVMPPC_H_RTAS, h_rtas);
42561bf2 2098
2a6593cb
AK
2099 /* ibm,client-architecture-support support */
2100 spapr_register_hypercall(KVMPPC_H_CAS, h_client_architecture_support);
c24ba3d0 2101
fea35ca4 2102 spapr_register_hypercall(KVMPPC_H_UPDATE_DT, h_update_dt);
f43e3525 2103}
83f7d43a
AF
2104
2105type_init(hypercall_register_types)