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