]> Git Repo - qemu.git/blame - hw/ppc/spapr_hcall.c
ppc: Do some batching of TCG tlb flushes
[qemu.git] / hw / ppc / spapr_hcall.c
CommitLineData
0d75590d 1#include "qemu/osdep.h"
da34e65c 2#include "qapi/error.h"
9c17d615 3#include "sysemu/sysemu.h"
03dd024f 4#include "qemu/log.h"
9fdf0c29 5#include "cpu.h"
63c91552 6#include "exec/exec-all.h"
ed120055 7#include "helper_regs.h"
0d09e41a 8#include "hw/ppc/spapr.h"
d5aea6f3 9#include "mmu-hash64.h"
3794d548
AK
10#include "cpu-models.h"
11#include "trace.h"
77ac58dd 12#include "sysemu/kvm.h"
3794d548 13#include "kvm_ppc.h"
f43e3525 14
a46622fd
AK
15struct SPRSyncState {
16 CPUState *cs;
17 int spr;
18 target_ulong value;
19 target_ulong mask;
20};
21
22static void do_spr_sync(void *arg)
23{
24 struct SPRSyncState *s = arg;
25 PowerPCCPU *cpu = POWERPC_CPU(s->cs);
26 CPUPPCState *env = &cpu->env;
27
28 cpu_synchronize_state(s->cs);
29 env->spr[s->spr] &= ~s->mask;
30 env->spr[s->spr] |= s->value;
31}
32
33static void set_spr(CPUState *cs, int spr, target_ulong value,
34 target_ulong mask)
35{
36 struct SPRSyncState s = {
37 .cs = cs,
38 .spr = spr,
39 .value = value,
40 .mask = mask
41 };
42 run_on_cpu(cs, do_spr_sync, &s);
43}
44
af08a58f
TH
45static bool has_spr(PowerPCCPU *cpu, int spr)
46{
47 /* We can test whether the SPR is defined by checking for a valid name */
48 return cpu->env.spr_cb[spr].name != NULL;
49}
50
f3c75d42
AK
51static inline bool valid_pte_index(CPUPPCState *env, target_ulong pte_index)
52{
53 /*
54 * hash value/pteg group index is normalized by htab_mask
55 */
56 if (((pte_index & ~7ULL) / HPTES_PER_GROUP) & ~env->htab_mask) {
57 return false;
58 }
59 return true;
60}
61
ecbc25fa
DG
62static bool is_ram_address(sPAPRMachineState *spapr, hwaddr addr)
63{
64 MachineState *machine = MACHINE(spapr);
65 MemoryHotplugState *hpms = &spapr->hotplug_memory;
66
67 if (addr < machine->ram_size) {
68 return true;
69 }
70 if ((addr >= hpms->base)
71 && ((addr - hpms->base) < memory_region_size(&hpms->mr))) {
72 return true;
73 }
74
75 return false;
76}
77
28e02042 78static target_ulong h_enter(PowerPCCPU *cpu, sPAPRMachineState *spapr,
f43e3525
DG
79 target_ulong opcode, target_ulong *args)
80{
b13ce26d 81 CPUPPCState *env = &cpu->env;
f43e3525
DG
82 target_ulong flags = args[0];
83 target_ulong pte_index = args[1];
84 target_ulong pteh = args[2];
85 target_ulong ptel = args[3];
1114e712 86 unsigned apshift, spshift;
f73a2575 87 target_ulong raddr;
7c43bca0 88 target_ulong index;
7c43bca0 89 uint64_t token;
f43e3525 90
1114e712
DG
91 apshift = ppc_hash64_hpte_page_shift_noslb(cpu, pteh, ptel, &spshift);
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 {
105 /* Looks like an IO address */
106 /* FIXME: What WIMG combinations could be sensible for IO?
107 * For now we allow WIMG=010x, but are there others? */
108 /* FIXME: Should we check against registered IO addresses? */
d5aea6f3 109 if ((ptel & (HPTE64_R_W | HPTE64_R_I | HPTE64_R_M)) != HPTE64_R_I) {
f73a2575
DG
110 return H_PARAMETER;
111 }
f43e3525 112 }
f73a2575 113
f43e3525
DG
114 pteh &= ~0x60ULL;
115
f3c75d42 116 if (!valid_pte_index(env, pte_index)) {
f43e3525
DG
117 return H_PARAMETER;
118 }
7c43bca0
AK
119
120 index = 0;
f43e3525
DG
121 if (likely((flags & H_EXACT) == 0)) {
122 pte_index &= ~7ULL;
7c43bca0 123 token = ppc_hash64_start_access(cpu, pte_index);
7aaf4957 124 for (; index < 8; index++) {
7ef23068 125 if (!(ppc_hash64_load_hpte0(cpu, token, index) & HPTE64_V_VALID)) {
f43e3525
DG
126 break;
127 }
7aaf4957 128 }
c18ad9a5 129 ppc_hash64_stop_access(cpu, token);
7aaf4957
AK
130 if (index == 8) {
131 return H_PTEG_FULL;
132 }
f43e3525 133 } else {
7c43bca0 134 token = ppc_hash64_start_access(cpu, pte_index);
7ef23068 135 if (ppc_hash64_load_hpte0(cpu, token, 0) & HPTE64_V_VALID) {
c18ad9a5 136 ppc_hash64_stop_access(cpu, token);
f43e3525
DG
137 return H_PTEG_FULL;
138 }
c18ad9a5 139 ppc_hash64_stop_access(cpu, token);
f43e3525 140 }
7c43bca0 141
7ef23068 142 ppc_hash64_store_hpte(cpu, pte_index + index,
3f94170b 143 pteh | HPTE64_V_HPTE_DIRTY, ptel);
f43e3525 144
7c43bca0 145 args[0] = pte_index + index;
f43e3525
DG
146 return H_SUCCESS;
147}
148
a3801402 149typedef enum {
a3d0abae
DG
150 REMOVE_SUCCESS = 0,
151 REMOVE_NOT_FOUND = 1,
152 REMOVE_PARM = 2,
153 REMOVE_HW = 3,
a3801402 154} RemoveResult;
a3d0abae 155
7ef23068 156static RemoveResult remove_hpte(PowerPCCPU *cpu, target_ulong ptex,
a3d0abae
DG
157 target_ulong avpn,
158 target_ulong flags,
159 target_ulong *vp, target_ulong *rp)
f43e3525 160{
7ef23068 161 CPUPPCState *env = &cpu->env;
7c43bca0 162 uint64_t token;
61a36c9b 163 target_ulong v, r;
f43e3525 164
f3c75d42 165 if (!valid_pte_index(env, ptex)) {
a3d0abae 166 return REMOVE_PARM;
f43e3525
DG
167 }
168
7ef23068
DG
169 token = ppc_hash64_start_access(cpu, ptex);
170 v = ppc_hash64_load_hpte0(cpu, token, 0);
171 r = ppc_hash64_load_hpte1(cpu, token, 0);
c18ad9a5 172 ppc_hash64_stop_access(cpu, token);
f43e3525 173
d5aea6f3 174 if ((v & HPTE64_V_VALID) == 0 ||
f43e3525
DG
175 ((flags & H_AVPN) && (v & ~0x7fULL) != avpn) ||
176 ((flags & H_ANDCOND) && (v & avpn) != 0)) {
a3d0abae 177 return REMOVE_NOT_FOUND;
f43e3525 178 }
35f9304d 179 *vp = v;
a3d0abae 180 *rp = r;
7ef23068 181 ppc_hash64_store_hpte(cpu, ptex, HPTE64_V_HPTE_DIRTY, 0);
61a36c9b 182 ppc_hash64_tlb_flush_hpte(cpu, ptex, v, r);
a3d0abae
DG
183 return REMOVE_SUCCESS;
184}
185
28e02042 186static target_ulong h_remove(PowerPCCPU *cpu, sPAPRMachineState *spapr,
a3d0abae
DG
187 target_ulong opcode, target_ulong *args)
188{
cd0c6f47 189 CPUPPCState *env = &cpu->env;
a3d0abae
DG
190 target_ulong flags = args[0];
191 target_ulong pte_index = args[1];
192 target_ulong avpn = args[2];
a3801402 193 RemoveResult ret;
a3d0abae 194
7ef23068 195 ret = remove_hpte(cpu, pte_index, avpn, flags,
a3d0abae
DG
196 &args[0], &args[1]);
197
198 switch (ret) {
199 case REMOVE_SUCCESS:
cd0c6f47 200 check_tlb_flush(env);
a3d0abae
DG
201 return H_SUCCESS;
202
203 case REMOVE_NOT_FOUND:
204 return H_NOT_FOUND;
205
206 case REMOVE_PARM:
207 return H_PARAMETER;
208
209 case REMOVE_HW:
210 return H_HARDWARE;
211 }
212
9a39970d 213 g_assert_not_reached();
a3d0abae
DG
214}
215
216#define H_BULK_REMOVE_TYPE 0xc000000000000000ULL
217#define H_BULK_REMOVE_REQUEST 0x4000000000000000ULL
218#define H_BULK_REMOVE_RESPONSE 0x8000000000000000ULL
219#define H_BULK_REMOVE_END 0xc000000000000000ULL
220#define H_BULK_REMOVE_CODE 0x3000000000000000ULL
221#define H_BULK_REMOVE_SUCCESS 0x0000000000000000ULL
222#define H_BULK_REMOVE_NOT_FOUND 0x1000000000000000ULL
223#define H_BULK_REMOVE_PARM 0x2000000000000000ULL
224#define H_BULK_REMOVE_HW 0x3000000000000000ULL
225#define H_BULK_REMOVE_RC 0x0c00000000000000ULL
226#define H_BULK_REMOVE_FLAGS 0x0300000000000000ULL
227#define H_BULK_REMOVE_ABSOLUTE 0x0000000000000000ULL
228#define H_BULK_REMOVE_ANDCOND 0x0100000000000000ULL
229#define H_BULK_REMOVE_AVPN 0x0200000000000000ULL
230#define H_BULK_REMOVE_PTEX 0x00ffffffffffffffULL
231
232#define H_BULK_REMOVE_MAX_BATCH 4
233
28e02042 234static target_ulong h_bulk_remove(PowerPCCPU *cpu, sPAPRMachineState *spapr,
a3d0abae
DG
235 target_ulong opcode, target_ulong *args)
236{
cd0c6f47 237 CPUPPCState *env = &cpu->env;
a3d0abae 238 int i;
cd0c6f47 239 target_ulong rc = H_SUCCESS;
a3d0abae
DG
240
241 for (i = 0; i < H_BULK_REMOVE_MAX_BATCH; i++) {
242 target_ulong *tsh = &args[i*2];
243 target_ulong tsl = args[i*2 + 1];
244 target_ulong v, r, ret;
245
246 if ((*tsh & H_BULK_REMOVE_TYPE) == H_BULK_REMOVE_END) {
247 break;
248 } else if ((*tsh & H_BULK_REMOVE_TYPE) != H_BULK_REMOVE_REQUEST) {
249 return H_PARAMETER;
250 }
251
252 *tsh &= H_BULK_REMOVE_PTEX | H_BULK_REMOVE_FLAGS;
253 *tsh |= H_BULK_REMOVE_RESPONSE;
254
255 if ((*tsh & H_BULK_REMOVE_ANDCOND) && (*tsh & H_BULK_REMOVE_AVPN)) {
256 *tsh |= H_BULK_REMOVE_PARM;
257 return H_PARAMETER;
258 }
259
7ef23068 260 ret = remove_hpte(cpu, *tsh & H_BULK_REMOVE_PTEX, tsl,
a3d0abae
DG
261 (*tsh & H_BULK_REMOVE_FLAGS) >> 26,
262 &v, &r);
263
264 *tsh |= ret << 60;
265
266 switch (ret) {
267 case REMOVE_SUCCESS:
d5aea6f3 268 *tsh |= (r & (HPTE64_R_C | HPTE64_R_R)) << 43;
a3d0abae
DG
269 break;
270
271 case REMOVE_PARM:
cd0c6f47
BH
272 rc = H_PARAMETER;
273 goto exit;
a3d0abae
DG
274
275 case REMOVE_HW:
cd0c6f47
BH
276 rc = H_HARDWARE;
277 goto exit;
a3d0abae
DG
278 }
279 }
cd0c6f47
BH
280 exit:
281 check_tlb_flush(env);
a3d0abae 282
cd0c6f47 283 return rc;
f43e3525
DG
284}
285
28e02042 286static target_ulong h_protect(PowerPCCPU *cpu, sPAPRMachineState *spapr,
f43e3525
DG
287 target_ulong opcode, target_ulong *args)
288{
b13ce26d 289 CPUPPCState *env = &cpu->env;
f43e3525
DG
290 target_ulong flags = args[0];
291 target_ulong pte_index = args[1];
292 target_ulong avpn = args[2];
7c43bca0 293 uint64_t token;
61a36c9b 294 target_ulong v, r;
f43e3525 295
f3c75d42 296 if (!valid_pte_index(env, pte_index)) {
f43e3525
DG
297 return H_PARAMETER;
298 }
299
7c43bca0 300 token = ppc_hash64_start_access(cpu, pte_index);
7ef23068
DG
301 v = ppc_hash64_load_hpte0(cpu, token, 0);
302 r = ppc_hash64_load_hpte1(cpu, token, 0);
c18ad9a5 303 ppc_hash64_stop_access(cpu, token);
f43e3525 304
d5aea6f3 305 if ((v & HPTE64_V_VALID) == 0 ||
f43e3525 306 ((flags & H_AVPN) && (v & ~0x7fULL) != avpn)) {
f43e3525
DG
307 return H_NOT_FOUND;
308 }
309
d5aea6f3
DG
310 r &= ~(HPTE64_R_PP0 | HPTE64_R_PP | HPTE64_R_N |
311 HPTE64_R_KEY_HI | HPTE64_R_KEY_LO);
312 r |= (flags << 55) & HPTE64_R_PP0;
313 r |= (flags << 48) & HPTE64_R_KEY_HI;
314 r |= flags & (HPTE64_R_PP | HPTE64_R_N | HPTE64_R_KEY_LO);
7ef23068 315 ppc_hash64_store_hpte(cpu, pte_index,
3f94170b 316 (v & ~HPTE64_V_VALID) | HPTE64_V_HPTE_DIRTY, 0);
61a36c9b 317 ppc_hash64_tlb_flush_hpte(cpu, pte_index, v, r);
f43e3525 318 /* Don't need a memory barrier, due to qemu's global lock */
7ef23068 319 ppc_hash64_store_hpte(cpu, pte_index, v | HPTE64_V_HPTE_DIRTY, r);
f43e3525
DG
320 return H_SUCCESS;
321}
322
28e02042 323static target_ulong h_read(PowerPCCPU *cpu, sPAPRMachineState *spapr,
6bbd5dde
EC
324 target_ulong opcode, target_ulong *args)
325{
326 CPUPPCState *env = &cpu->env;
327 target_ulong flags = args[0];
328 target_ulong pte_index = args[1];
329 uint8_t *hpte;
330 int i, ridx, n_entries = 1;
331
f3c75d42 332 if (!valid_pte_index(env, pte_index)) {
6bbd5dde
EC
333 return H_PARAMETER;
334 }
335
336 if (flags & H_READ_4) {
337 /* Clear the two low order bits */
338 pte_index &= ~(3ULL);
339 n_entries = 4;
340 }
341
342 hpte = env->external_htab + (pte_index * HASH_PTE_SIZE_64);
343
344 for (i = 0, ridx = 0; i < n_entries; i++) {
345 args[ridx++] = ldq_p(hpte);
346 args[ridx++] = ldq_p(hpte + (HASH_PTE_SIZE_64/2));
347 hpte += HASH_PTE_SIZE_64;
348 }
349
350 return H_SUCCESS;
351}
352
423576f7
TH
353static target_ulong h_set_sprg0(PowerPCCPU *cpu, sPAPRMachineState *spapr,
354 target_ulong opcode, target_ulong *args)
355{
356 cpu_synchronize_state(CPU(cpu));
357 cpu->env.spr[SPR_SPRG0] = args[0];
358
359 return H_SUCCESS;
360}
361
28e02042 362static target_ulong h_set_dabr(PowerPCCPU *cpu, sPAPRMachineState *spapr,
821303f5
DG
363 target_ulong opcode, target_ulong *args)
364{
af08a58f
TH
365 if (!has_spr(cpu, SPR_DABR)) {
366 return H_HARDWARE; /* DABR register not available */
367 }
368 cpu_synchronize_state(CPU(cpu));
369
370 if (has_spr(cpu, SPR_DABRX)) {
371 cpu->env.spr[SPR_DABRX] = 0x3; /* Use Problem and Privileged state */
372 } else if (!(args[0] & 0x4)) { /* Breakpoint Translation set? */
373 return H_RESERVED_DABR;
374 }
375
376 cpu->env.spr[SPR_DABR] = args[0];
377 return H_SUCCESS;
821303f5
DG
378}
379
e49ff266
TH
380static target_ulong h_set_xdabr(PowerPCCPU *cpu, sPAPRMachineState *spapr,
381 target_ulong opcode, target_ulong *args)
382{
383 target_ulong dabrx = args[1];
384
385 if (!has_spr(cpu, SPR_DABR) || !has_spr(cpu, SPR_DABRX)) {
386 return H_HARDWARE;
387 }
388
389 if ((dabrx & ~0xfULL) != 0 || (dabrx & H_DABRX_HYPERVISOR) != 0
390 || (dabrx & (H_DABRX_KERNEL | H_DABRX_USER)) == 0) {
391 return H_PARAMETER;
392 }
393
394 cpu_synchronize_state(CPU(cpu));
395 cpu->env.spr[SPR_DABRX] = dabrx;
396 cpu->env.spr[SPR_DABR] = args[0];
397
398 return H_SUCCESS;
399}
400
3240dd9a
TH
401static target_ulong h_page_init(PowerPCCPU *cpu, sPAPRMachineState *spapr,
402 target_ulong opcode, target_ulong *args)
403{
404 target_ulong flags = args[0];
405 hwaddr dst = args[1];
406 hwaddr src = args[2];
407 hwaddr len = TARGET_PAGE_SIZE;
408 uint8_t *pdst, *psrc;
409 target_long ret = H_SUCCESS;
410
411 if (flags & ~(H_ICACHE_SYNCHRONIZE | H_ICACHE_INVALIDATE
412 | H_COPY_PAGE | H_ZERO_PAGE)) {
413 qemu_log_mask(LOG_UNIMP, "h_page_init: Bad flags (" TARGET_FMT_lx "\n",
414 flags);
415 return H_PARAMETER;
416 }
417
418 /* Map-in destination */
419 if (!is_ram_address(spapr, dst) || (dst & ~TARGET_PAGE_MASK) != 0) {
420 return H_PARAMETER;
421 }
422 pdst = cpu_physical_memory_map(dst, &len, 1);
423 if (!pdst || len != TARGET_PAGE_SIZE) {
424 return H_PARAMETER;
425 }
426
427 if (flags & H_COPY_PAGE) {
428 /* Map-in source, copy to destination, and unmap source again */
429 if (!is_ram_address(spapr, src) || (src & ~TARGET_PAGE_MASK) != 0) {
430 ret = H_PARAMETER;
431 goto unmap_out;
432 }
433 psrc = cpu_physical_memory_map(src, &len, 0);
434 if (!psrc || len != TARGET_PAGE_SIZE) {
435 ret = H_PARAMETER;
436 goto unmap_out;
437 }
438 memcpy(pdst, psrc, len);
439 cpu_physical_memory_unmap(psrc, len, 0, len);
440 } else if (flags & H_ZERO_PAGE) {
441 memset(pdst, 0, len); /* Just clear the destination page */
442 }
443
444 if (kvm_enabled() && (flags & H_ICACHE_SYNCHRONIZE) != 0) {
445 kvmppc_dcbst_range(cpu, pdst, len);
446 }
447 if (flags & (H_ICACHE_SYNCHRONIZE | H_ICACHE_INVALIDATE)) {
448 if (kvm_enabled()) {
449 kvmppc_icbi_range(cpu, pdst, len);
450 } else {
451 tb_flush(CPU(cpu));
452 }
453 }
454
455unmap_out:
456 cpu_physical_memory_unmap(pdst, TARGET_PAGE_SIZE, 1, len);
457 return ret;
458}
459
ed120055
DG
460#define FLAGS_REGISTER_VPA 0x0000200000000000ULL
461#define FLAGS_REGISTER_DTL 0x0000400000000000ULL
462#define FLAGS_REGISTER_SLBSHADOW 0x0000600000000000ULL
463#define FLAGS_DEREGISTER_VPA 0x0000a00000000000ULL
464#define FLAGS_DEREGISTER_DTL 0x0000c00000000000ULL
465#define FLAGS_DEREGISTER_SLBSHADOW 0x0000e00000000000ULL
466
467#define VPA_MIN_SIZE 640
468#define VPA_SIZE_OFFSET 0x4
469#define VPA_SHARED_PROC_OFFSET 0x9
470#define VPA_SHARED_PROC_VAL 0x2
471
e2684c0b 472static target_ulong register_vpa(CPUPPCState *env, target_ulong vpa)
ed120055 473{
33276f1b 474 CPUState *cs = CPU(ppc_env_get_cpu(env));
ed120055
DG
475 uint16_t size;
476 uint8_t tmp;
477
478 if (vpa == 0) {
479 hcall_dprintf("Can't cope with registering a VPA at logical 0\n");
480 return H_HARDWARE;
481 }
482
483 if (vpa % env->dcache_line_size) {
484 return H_PARAMETER;
485 }
486 /* FIXME: bounds check the address */
487
41701aa4 488 size = lduw_be_phys(cs->as, vpa + 0x4);
ed120055
DG
489
490 if (size < VPA_MIN_SIZE) {
491 return H_PARAMETER;
492 }
493
494 /* VPA is not allowed to cross a page boundary */
495 if ((vpa / 4096) != ((vpa + size - 1) / 4096)) {
496 return H_PARAMETER;
497 }
498
1bfb37d1 499 env->vpa_addr = vpa;
ed120055 500
2c17449b 501 tmp = ldub_phys(cs->as, env->vpa_addr + VPA_SHARED_PROC_OFFSET);
ed120055 502 tmp |= VPA_SHARED_PROC_VAL;
db3be60d 503 stb_phys(cs->as, env->vpa_addr + VPA_SHARED_PROC_OFFSET, tmp);
ed120055
DG
504
505 return H_SUCCESS;
506}
507
e2684c0b 508static target_ulong deregister_vpa(CPUPPCState *env, target_ulong vpa)
ed120055 509{
1bfb37d1 510 if (env->slb_shadow_addr) {
ed120055
DG
511 return H_RESOURCE;
512 }
513
1bfb37d1 514 if (env->dtl_addr) {
ed120055
DG
515 return H_RESOURCE;
516 }
517
1bfb37d1 518 env->vpa_addr = 0;
ed120055
DG
519 return H_SUCCESS;
520}
521
e2684c0b 522static target_ulong register_slb_shadow(CPUPPCState *env, target_ulong addr)
ed120055 523{
33276f1b 524 CPUState *cs = CPU(ppc_env_get_cpu(env));
ed120055
DG
525 uint32_t size;
526
527 if (addr == 0) {
528 hcall_dprintf("Can't cope with SLB shadow at logical 0\n");
529 return H_HARDWARE;
530 }
531
fdfba1a2 532 size = ldl_be_phys(cs->as, addr + 0x4);
ed120055
DG
533 if (size < 0x8) {
534 return H_PARAMETER;
535 }
536
537 if ((addr / 4096) != ((addr + size - 1) / 4096)) {
538 return H_PARAMETER;
539 }
540
1bfb37d1 541 if (!env->vpa_addr) {
ed120055
DG
542 return H_RESOURCE;
543 }
544
1bfb37d1
DG
545 env->slb_shadow_addr = addr;
546 env->slb_shadow_size = size;
ed120055
DG
547
548 return H_SUCCESS;
549}
550
e2684c0b 551static target_ulong deregister_slb_shadow(CPUPPCState *env, target_ulong addr)
ed120055 552{
1bfb37d1
DG
553 env->slb_shadow_addr = 0;
554 env->slb_shadow_size = 0;
ed120055
DG
555 return H_SUCCESS;
556}
557
e2684c0b 558static target_ulong register_dtl(CPUPPCState *env, target_ulong addr)
ed120055 559{
33276f1b 560 CPUState *cs = CPU(ppc_env_get_cpu(env));
ed120055
DG
561 uint32_t size;
562
563 if (addr == 0) {
564 hcall_dprintf("Can't cope with DTL at logical 0\n");
565 return H_HARDWARE;
566 }
567
fdfba1a2 568 size = ldl_be_phys(cs->as, addr + 0x4);
ed120055
DG
569
570 if (size < 48) {
571 return H_PARAMETER;
572 }
573
1bfb37d1 574 if (!env->vpa_addr) {
ed120055
DG
575 return H_RESOURCE;
576 }
577
1bfb37d1 578 env->dtl_addr = addr;
ed120055
DG
579 env->dtl_size = size;
580
581 return H_SUCCESS;
582}
583
73f7821b 584static target_ulong deregister_dtl(CPUPPCState *env, target_ulong addr)
ed120055 585{
1bfb37d1 586 env->dtl_addr = 0;
ed120055
DG
587 env->dtl_size = 0;
588
589 return H_SUCCESS;
590}
591
28e02042 592static target_ulong h_register_vpa(PowerPCCPU *cpu, sPAPRMachineState *spapr,
ed120055
DG
593 target_ulong opcode, target_ulong *args)
594{
595 target_ulong flags = args[0];
596 target_ulong procno = args[1];
597 target_ulong vpa = args[2];
598 target_ulong ret = H_PARAMETER;
e2684c0b 599 CPUPPCState *tenv;
0f20ba62 600 PowerPCCPU *tcpu;
ed120055 601
0f20ba62 602 tcpu = ppc_get_vcpu_by_dt_id(procno);
5353d03d 603 if (!tcpu) {
ed120055
DG
604 return H_PARAMETER;
605 }
0f20ba62 606 tenv = &tcpu->env;
ed120055
DG
607
608 switch (flags) {
609 case FLAGS_REGISTER_VPA:
610 ret = register_vpa(tenv, vpa);
611 break;
612
613 case FLAGS_DEREGISTER_VPA:
614 ret = deregister_vpa(tenv, vpa);
615 break;
616
617 case FLAGS_REGISTER_SLBSHADOW:
618 ret = register_slb_shadow(tenv, vpa);
619 break;
620
621 case FLAGS_DEREGISTER_SLBSHADOW:
622 ret = deregister_slb_shadow(tenv, vpa);
623 break;
624
625 case FLAGS_REGISTER_DTL:
626 ret = register_dtl(tenv, vpa);
627 break;
628
629 case FLAGS_DEREGISTER_DTL:
630 ret = deregister_dtl(tenv, vpa);
631 break;
632 }
633
634 return ret;
635}
636
28e02042 637static target_ulong h_cede(PowerPCCPU *cpu, sPAPRMachineState *spapr,
ed120055
DG
638 target_ulong opcode, target_ulong *args)
639{
b13ce26d 640 CPUPPCState *env = &cpu->env;
fcd7d003 641 CPUState *cs = CPU(cpu);
b13ce26d 642
ed120055
DG
643 env->msr |= (1ULL << MSR_EE);
644 hreg_compute_hflags(env);
fcd7d003 645 if (!cpu_has_work(cs)) {
259186a7 646 cs->halted = 1;
27103424 647 cs->exception_index = EXCP_HLT;
fcd7d003 648 cs->exit_request = 1;
ed120055
DG
649 }
650 return H_SUCCESS;
651}
652
28e02042 653static target_ulong h_rtas(PowerPCCPU *cpu, sPAPRMachineState *spapr,
39ac8455
DG
654 target_ulong opcode, target_ulong *args)
655{
656 target_ulong rtas_r3 = args[0];
4fe822e0
AK
657 uint32_t token = rtas_ld(rtas_r3, 0);
658 uint32_t nargs = rtas_ld(rtas_r3, 1);
659 uint32_t nret = rtas_ld(rtas_r3, 2);
39ac8455 660
210b580b 661 return spapr_rtas_call(cpu, spapr, token, nargs, rtas_r3 + 12,
39ac8455
DG
662 nret, rtas_r3 + 12 + 4*nargs);
663}
664
28e02042 665static target_ulong h_logical_load(PowerPCCPU *cpu, sPAPRMachineState *spapr,
827200a2
DG
666 target_ulong opcode, target_ulong *args)
667{
fdfba1a2 668 CPUState *cs = CPU(cpu);
827200a2
DG
669 target_ulong size = args[0];
670 target_ulong addr = args[1];
671
672 switch (size) {
673 case 1:
2c17449b 674 args[0] = ldub_phys(cs->as, addr);
827200a2
DG
675 return H_SUCCESS;
676 case 2:
41701aa4 677 args[0] = lduw_phys(cs->as, addr);
827200a2
DG
678 return H_SUCCESS;
679 case 4:
fdfba1a2 680 args[0] = ldl_phys(cs->as, addr);
827200a2
DG
681 return H_SUCCESS;
682 case 8:
2c17449b 683 args[0] = ldq_phys(cs->as, addr);
827200a2
DG
684 return H_SUCCESS;
685 }
686 return H_PARAMETER;
687}
688
28e02042 689static target_ulong h_logical_store(PowerPCCPU *cpu, sPAPRMachineState *spapr,
827200a2
DG
690 target_ulong opcode, target_ulong *args)
691{
f606604f
EI
692 CPUState *cs = CPU(cpu);
693
827200a2
DG
694 target_ulong size = args[0];
695 target_ulong addr = args[1];
696 target_ulong val = args[2];
697
698 switch (size) {
699 case 1:
db3be60d 700 stb_phys(cs->as, addr, val);
827200a2
DG
701 return H_SUCCESS;
702 case 2:
5ce5944d 703 stw_phys(cs->as, addr, val);
827200a2
DG
704 return H_SUCCESS;
705 case 4:
ab1da857 706 stl_phys(cs->as, addr, val);
827200a2
DG
707 return H_SUCCESS;
708 case 8:
f606604f 709 stq_phys(cs->as, addr, val);
827200a2
DG
710 return H_SUCCESS;
711 }
712 return H_PARAMETER;
713}
714
28e02042 715static target_ulong h_logical_memop(PowerPCCPU *cpu, sPAPRMachineState *spapr,
c73e3771
BH
716 target_ulong opcode, target_ulong *args)
717{
fdfba1a2
EI
718 CPUState *cs = CPU(cpu);
719
c73e3771
BH
720 target_ulong dst = args[0]; /* Destination address */
721 target_ulong src = args[1]; /* Source address */
722 target_ulong esize = args[2]; /* Element size (0=1,1=2,2=4,3=8) */
723 target_ulong count = args[3]; /* Element count */
724 target_ulong op = args[4]; /* 0 = copy, 1 = invert */
725 uint64_t tmp;
726 unsigned int mask = (1 << esize) - 1;
727 int step = 1 << esize;
728
729 if (count > 0x80000000) {
730 return H_PARAMETER;
731 }
732
733 if ((dst & mask) || (src & mask) || (op > 1)) {
734 return H_PARAMETER;
735 }
736
737 if (dst >= src && dst < (src + (count << esize))) {
738 dst = dst + ((count - 1) << esize);
739 src = src + ((count - 1) << esize);
740 step = -step;
741 }
742
743 while (count--) {
744 switch (esize) {
745 case 0:
2c17449b 746 tmp = ldub_phys(cs->as, src);
c73e3771
BH
747 break;
748 case 1:
41701aa4 749 tmp = lduw_phys(cs->as, src);
c73e3771
BH
750 break;
751 case 2:
fdfba1a2 752 tmp = ldl_phys(cs->as, src);
c73e3771
BH
753 break;
754 case 3:
2c17449b 755 tmp = ldq_phys(cs->as, src);
c73e3771
BH
756 break;
757 default:
758 return H_PARAMETER;
759 }
760 if (op == 1) {
761 tmp = ~tmp;
762 }
763 switch (esize) {
764 case 0:
db3be60d 765 stb_phys(cs->as, dst, tmp);
c73e3771
BH
766 break;
767 case 1:
5ce5944d 768 stw_phys(cs->as, dst, tmp);
c73e3771
BH
769 break;
770 case 2:
ab1da857 771 stl_phys(cs->as, dst, tmp);
c73e3771
BH
772 break;
773 case 3:
f606604f 774 stq_phys(cs->as, dst, tmp);
c73e3771
BH
775 break;
776 }
777 dst = dst + step;
778 src = src + step;
779 }
780
781 return H_SUCCESS;
782}
783
28e02042 784static target_ulong h_logical_icbi(PowerPCCPU *cpu, sPAPRMachineState *spapr,
827200a2
DG
785 target_ulong opcode, target_ulong *args)
786{
787 /* Nothing to do on emulation, KVM will trap this in the kernel */
788 return H_SUCCESS;
789}
790
28e02042 791static target_ulong h_logical_dcbf(PowerPCCPU *cpu, sPAPRMachineState *spapr,
827200a2
DG
792 target_ulong opcode, target_ulong *args)
793{
794 /* Nothing to do on emulation, KVM will trap this in the kernel */
795 return H_SUCCESS;
796}
797
7d0cd464
PM
798static target_ulong h_set_mode_resource_le(PowerPCCPU *cpu,
799 target_ulong mflags,
800 target_ulong value1,
801 target_ulong value2)
42561bf2
AB
802{
803 CPUState *cs;
42561bf2 804
c4015bbd
AK
805 if (value1) {
806 return H_P3;
807 }
808 if (value2) {
809 return H_P4;
810 }
811
812 switch (mflags) {
813 case H_SET_MODE_ENDIAN_BIG:
814 CPU_FOREACH(cs) {
815 set_spr(cs, SPR_LPCR, 0, LPCR_ILE);
42561bf2 816 }
eefaccc0 817 spapr_pci_switch_vga(true);
c4015bbd
AK
818 return H_SUCCESS;
819
820 case H_SET_MODE_ENDIAN_LITTLE:
821 CPU_FOREACH(cs) {
822 set_spr(cs, SPR_LPCR, LPCR_ILE, LPCR_ILE);
42561bf2 823 }
eefaccc0 824 spapr_pci_switch_vga(false);
c4015bbd
AK
825 return H_SUCCESS;
826 }
42561bf2 827
c4015bbd
AK
828 return H_UNSUPPORTED_FLAG;
829}
42561bf2 830
7d0cd464
PM
831static target_ulong h_set_mode_resource_addr_trans_mode(PowerPCCPU *cpu,
832 target_ulong mflags,
833 target_ulong value1,
834 target_ulong value2)
d5ac4f54
AK
835{
836 CPUState *cs;
837 PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
d5ac4f54
AK
838
839 if (!(pcc->insns_flags2 & PPC2_ISA207S)) {
840 return H_P2;
841 }
842 if (value1) {
843 return H_P3;
844 }
845 if (value2) {
846 return H_P4;
847 }
848
5c94b2a5 849 if (mflags == AIL_RESERVED) {
d5ac4f54
AK
850 return H_UNSUPPORTED_FLAG;
851 }
852
853 CPU_FOREACH(cs) {
d5ac4f54 854 set_spr(cs, SPR_LPCR, mflags << LPCR_AIL_SHIFT, LPCR_AIL);
d5ac4f54
AK
855 }
856
857 return H_SUCCESS;
858}
859
28e02042 860static target_ulong h_set_mode(PowerPCCPU *cpu, sPAPRMachineState *spapr,
c4015bbd
AK
861 target_ulong opcode, target_ulong *args)
862{
863 target_ulong resource = args[1];
864 target_ulong ret = H_P2;
865
866 switch (resource) {
867 case H_SET_MODE_RESOURCE_LE:
7d0cd464 868 ret = h_set_mode_resource_le(cpu, args[0], args[2], args[3]);
c4015bbd 869 break;
d5ac4f54 870 case H_SET_MODE_RESOURCE_ADDR_TRANS_MODE:
7d0cd464
PM
871 ret = h_set_mode_resource_addr_trans_mode(cpu, args[0],
872 args[2], args[3]);
d5ac4f54 873 break;
42561bf2
AB
874 }
875
42561bf2
AB
876 return ret;
877}
878
03d196b7
BR
879/*
880 * Return the offset to the requested option vector @vector in the
881 * option vector table @table.
882 */
883static target_ulong cas_get_option_vector(int vector, target_ulong table)
884{
885 int i;
886 char nr_vectors, nr_entries;
887
888 if (!table) {
889 return 0;
890 }
891
892 nr_vectors = (ldl_phys(&address_space_memory, table) >> 24) + 1;
893 if (!vector || vector > nr_vectors) {
894 return 0;
895 }
896 table++; /* skip nr option vectors */
897
898 for (i = 0; i < vector - 1; i++) {
899 nr_entries = ldl_phys(&address_space_memory, table) >> 24;
900 table += nr_entries + 2;
901 }
902 return table;
903}
904
3794d548
AK
905typedef struct {
906 PowerPCCPU *cpu;
907 uint32_t cpu_version;
f9ab1e87 908 Error *err;
3794d548
AK
909} SetCompatState;
910
911static void do_set_compat(void *arg)
912{
913 SetCompatState *s = arg;
914
915 cpu_synchronize_state(CPU(s->cpu));
f9ab1e87 916 ppc_set_compat(s->cpu, s->cpu_version, &s->err);
3794d548
AK
917}
918
919#define get_compat_level(cpuver) ( \
920 ((cpuver) == CPU_POWERPC_LOGICAL_2_05) ? 2050 : \
921 ((cpuver) == CPU_POWERPC_LOGICAL_2_06) ? 2060 : \
922 ((cpuver) == CPU_POWERPC_LOGICAL_2_06_PLUS) ? 2061 : \
923 ((cpuver) == CPU_POWERPC_LOGICAL_2_07) ? 2070 : 0)
924
03d196b7
BR
925#define OV5_DRCONF_MEMORY 0x20
926
2a6593cb 927static target_ulong h_client_architecture_support(PowerPCCPU *cpu_,
28e02042 928 sPAPRMachineState *spapr,
2a6593cb
AK
929 target_ulong opcode,
930 target_ulong *args)
931{
27ac3e06
DG
932 target_ulong list = ppc64_phys_to_real(args[0]);
933 target_ulong ov_table, ov5;
3794d548
AK
934 PowerPCCPUClass *pcc_ = POWERPC_CPU_GET_CLASS(cpu_);
935 CPUState *cs;
03d196b7 936 bool cpu_match = false, cpu_update = true, memory_update = false;
3794d548
AK
937 unsigned old_cpu_version = cpu_->cpu_version;
938 unsigned compat_lvl = 0, cpu_version = 0;
939 unsigned max_lvl = get_compat_level(cpu_->max_compat);
940 int counter;
03d196b7 941 char ov5_byte2;
3794d548
AK
942
943 /* Parse PVR list */
944 for (counter = 0; counter < 512; ++counter) {
945 uint32_t pvr, pvr_mask;
946
27ac3e06 947 pvr_mask = ldl_be_phys(&address_space_memory, list);
3794d548 948 list += 4;
27ac3e06 949 pvr = ldl_be_phys(&address_space_memory, list);
3794d548
AK
950 list += 4;
951
952 trace_spapr_cas_pvr_try(pvr);
953 if (!max_lvl &&
954 ((cpu_->env.spr[SPR_PVR] & pvr_mask) == (pvr & pvr_mask))) {
955 cpu_match = true;
956 cpu_version = 0;
957 } else if (pvr == cpu_->cpu_version) {
958 cpu_match = true;
959 cpu_version = cpu_->cpu_version;
960 } else if (!cpu_match) {
961 /* If it is a logical PVR, try to determine the highest level */
962 unsigned lvl = get_compat_level(pvr);
963 if (lvl) {
964 bool is205 = (pcc_->pcr_mask & PCR_COMPAT_2_05) &&
965 (lvl == get_compat_level(CPU_POWERPC_LOGICAL_2_05));
966 bool is206 = (pcc_->pcr_mask & PCR_COMPAT_2_06) &&
967 ((lvl == get_compat_level(CPU_POWERPC_LOGICAL_2_06)) ||
968 (lvl == get_compat_level(CPU_POWERPC_LOGICAL_2_06_PLUS)));
969
970 if (is205 || is206) {
971 if (!max_lvl) {
972 /* User did not set the level, choose the highest */
973 if (compat_lvl <= lvl) {
974 compat_lvl = lvl;
975 cpu_version = pvr;
976 }
977 } else if (max_lvl >= lvl) {
978 /* User chose the level, don't set higher than this */
979 compat_lvl = lvl;
980 cpu_version = pvr;
981 }
982 }
983 }
984 }
985 /* Terminator record */
986 if (~pvr_mask & pvr) {
987 break;
988 }
989 }
990
3794d548
AK
991 /* Parsing finished */
992 trace_spapr_cas_pvr(cpu_->cpu_version, cpu_match,
993 cpu_version, pcc_->pcr_mask);
994
995 /* Update CPUs */
996 if (old_cpu_version != cpu_version) {
997 CPU_FOREACH(cs) {
998 SetCompatState s = {
999 .cpu = POWERPC_CPU(cs),
1000 .cpu_version = cpu_version,
f9ab1e87 1001 .err = NULL,
3794d548
AK
1002 };
1003
1004 run_on_cpu(cs, do_set_compat, &s);
1005
f9ab1e87
DG
1006 if (s.err) {
1007 error_report_err(s.err);
3794d548
AK
1008 return H_HARDWARE;
1009 }
1010 }
1011 }
1012
1013 if (!cpu_version) {
03d196b7 1014 cpu_update = false;
3794d548 1015 }
2a6593cb 1016
03d196b7
BR
1017 /* For the future use: here @ov_table points to the first option vector */
1018 ov_table = list;
1019
27ac3e06
DG
1020 ov5 = cas_get_option_vector(5, ov_table);
1021 if (!ov5) {
2a6593cb
AK
1022 return H_SUCCESS;
1023 }
1024
03d196b7 1025 /* @list now points to OV 5 */
27ac3e06 1026 ov5_byte2 = ldub_phys(&address_space_memory, ov5 + 2);
03d196b7
BR
1027 if (ov5_byte2 & OV5_DRCONF_MEMORY) {
1028 memory_update = true;
1029 }
1030
1031 if (spapr_h_cas_compose_response(spapr, args[1], args[2],
1032 cpu_update, memory_update)) {
2a6593cb
AK
1033 qemu_system_reset_request();
1034 }
1035
1036 return H_SUCCESS;
1037}
1038
7d7ba3fe
DG
1039static spapr_hcall_fn papr_hypercall_table[(MAX_HCALL_OPCODE / 4) + 1];
1040static spapr_hcall_fn kvmppc_hypercall_table[KVMPPC_HCALL_MAX - KVMPPC_HCALL_BASE + 1];
9fdf0c29
DG
1041
1042void spapr_register_hypercall(target_ulong opcode, spapr_hcall_fn fn)
1043{
39ac8455
DG
1044 spapr_hcall_fn *slot;
1045
1046 if (opcode <= MAX_HCALL_OPCODE) {
1047 assert((opcode & 0x3) == 0);
9fdf0c29 1048
39ac8455
DG
1049 slot = &papr_hypercall_table[opcode / 4];
1050 } else {
1051 assert((opcode >= KVMPPC_HCALL_BASE) && (opcode <= KVMPPC_HCALL_MAX));
9fdf0c29 1052
39ac8455
DG
1053 slot = &kvmppc_hypercall_table[opcode - KVMPPC_HCALL_BASE];
1054 }
9fdf0c29 1055
c89d5299 1056 assert(!(*slot));
39ac8455 1057 *slot = fn;
9fdf0c29
DG
1058}
1059
aa100fa4 1060target_ulong spapr_hypercall(PowerPCCPU *cpu, target_ulong opcode,
9fdf0c29
DG
1061 target_ulong *args)
1062{
28e02042
DG
1063 sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
1064
9fdf0c29
DG
1065 if ((opcode <= MAX_HCALL_OPCODE)
1066 && ((opcode & 0x3) == 0)) {
39ac8455
DG
1067 spapr_hcall_fn fn = papr_hypercall_table[opcode / 4];
1068
1069 if (fn) {
b13ce26d 1070 return fn(cpu, spapr, opcode, args);
39ac8455
DG
1071 }
1072 } else if ((opcode >= KVMPPC_HCALL_BASE) &&
1073 (opcode <= KVMPPC_HCALL_MAX)) {
1074 spapr_hcall_fn fn = kvmppc_hypercall_table[opcode - KVMPPC_HCALL_BASE];
9fdf0c29
DG
1075
1076 if (fn) {
b13ce26d 1077 return fn(cpu, spapr, opcode, args);
9fdf0c29
DG
1078 }
1079 }
1080
aaf87c66
TH
1081 qemu_log_mask(LOG_UNIMP, "Unimplemented SPAPR hcall 0x" TARGET_FMT_lx "\n",
1082 opcode);
9fdf0c29
DG
1083 return H_FUNCTION;
1084}
f43e3525 1085
83f7d43a 1086static void hypercall_register_types(void)
f43e3525
DG
1087{
1088 /* hcall-pft */
1089 spapr_register_hypercall(H_ENTER, h_enter);
1090 spapr_register_hypercall(H_REMOVE, h_remove);
1091 spapr_register_hypercall(H_PROTECT, h_protect);
6bbd5dde 1092 spapr_register_hypercall(H_READ, h_read);
39ac8455 1093
a3d0abae
DG
1094 /* hcall-bulk */
1095 spapr_register_hypercall(H_BULK_REMOVE, h_bulk_remove);
1096
ed120055
DG
1097 /* hcall-splpar */
1098 spapr_register_hypercall(H_REGISTER_VPA, h_register_vpa);
1099 spapr_register_hypercall(H_CEDE, h_cede);
1100
423576f7
TH
1101 /* processor register resource access h-calls */
1102 spapr_register_hypercall(H_SET_SPRG0, h_set_sprg0);
af08a58f 1103 spapr_register_hypercall(H_SET_DABR, h_set_dabr);
e49ff266 1104 spapr_register_hypercall(H_SET_XDABR, h_set_xdabr);
3240dd9a 1105 spapr_register_hypercall(H_PAGE_INIT, h_page_init);
423576f7
TH
1106 spapr_register_hypercall(H_SET_MODE, h_set_mode);
1107
827200a2
DG
1108 /* "debugger" hcalls (also used by SLOF). Note: We do -not- differenciate
1109 * here between the "CI" and the "CACHE" variants, they will use whatever
1110 * mapping attributes qemu is using. When using KVM, the kernel will
1111 * enforce the attributes more strongly
1112 */
1113 spapr_register_hypercall(H_LOGICAL_CI_LOAD, h_logical_load);
1114 spapr_register_hypercall(H_LOGICAL_CI_STORE, h_logical_store);
1115 spapr_register_hypercall(H_LOGICAL_CACHE_LOAD, h_logical_load);
1116 spapr_register_hypercall(H_LOGICAL_CACHE_STORE, h_logical_store);
1117 spapr_register_hypercall(H_LOGICAL_ICBI, h_logical_icbi);
1118 spapr_register_hypercall(H_LOGICAL_DCBF, h_logical_dcbf);
c73e3771 1119 spapr_register_hypercall(KVMPPC_H_LOGICAL_MEMOP, h_logical_memop);
827200a2 1120
39ac8455
DG
1121 /* qemu/KVM-PPC specific hcalls */
1122 spapr_register_hypercall(KVMPPC_H_RTAS, h_rtas);
42561bf2 1123
2a6593cb
AK
1124 /* ibm,client-architecture-support support */
1125 spapr_register_hypercall(KVMPPC_H_CAS, h_client_architecture_support);
f43e3525 1126}
83f7d43a
AF
1127
1128type_init(hypercall_register_types)
This page took 0.657962 seconds and 4 git commands to generate.