6 #include "helper_regs.h"
9 #define HPTES_PER_GROUP 8
11 #define HPTE_V_SSIZE_SHIFT 62
12 #define HPTE_V_AVPN_SHIFT 7
13 #define HPTE_V_AVPN 0x3fffffffffffff80ULL
14 #define HPTE_V_AVPN_VAL(x) (((x) & HPTE_V_AVPN) >> HPTE_V_AVPN_SHIFT)
15 #define HPTE_V_COMPARE(x, y) (!(((x) ^ (y)) & 0xffffffffffffff80UL))
16 #define HPTE_V_BOLTED 0x0000000000000010ULL
17 #define HPTE_V_LOCK 0x0000000000000008ULL
18 #define HPTE_V_LARGE 0x0000000000000004ULL
19 #define HPTE_V_SECONDARY 0x0000000000000002ULL
20 #define HPTE_V_VALID 0x0000000000000001ULL
22 #define HPTE_R_PP0 0x8000000000000000ULL
23 #define HPTE_R_TS 0x4000000000000000ULL
24 #define HPTE_R_KEY_HI 0x3000000000000000ULL
25 #define HPTE_R_RPN_SHIFT 12
26 #define HPTE_R_RPN 0x3ffffffffffff000ULL
27 #define HPTE_R_FLAGS 0x00000000000003ffULL
28 #define HPTE_R_PP 0x0000000000000003ULL
29 #define HPTE_R_N 0x0000000000000004ULL
30 #define HPTE_R_G 0x0000000000000008ULL
31 #define HPTE_R_M 0x0000000000000010ULL
32 #define HPTE_R_I 0x0000000000000020ULL
33 #define HPTE_R_W 0x0000000000000040ULL
34 #define HPTE_R_WIMG 0x0000000000000078ULL
35 #define HPTE_R_C 0x0000000000000080ULL
36 #define HPTE_R_R 0x0000000000000100ULL
37 #define HPTE_R_KEY_LO 0x0000000000000e00ULL
39 #define HPTE_V_1TB_SEG 0x4000000000000000ULL
40 #define HPTE_V_VRMA_MASK 0x4001ffffff000000ULL
42 static target_ulong compute_tlbie_rb(target_ulong v, target_ulong r,
43 target_ulong pte_index)
45 target_ulong rb, va_low;
47 rb = (v & ~0x7fULL) << 16; /* AVA field */
48 va_low = pte_index >> 3;
49 if (v & HPTE_V_SECONDARY) {
52 /* xor vsid from AVA */
53 if (!(v & HPTE_V_1TB_SEG)) {
59 if (v & HPTE_V_LARGE) {
60 rb |= 1; /* L field */
61 #if 0 /* Disable that P7 specific bit for now */
63 /* non-16MB large page, must be 64k */
64 /* (masks depend on page size) */
65 rb |= 0x1000; /* page encoding in LP field */
66 rb |= (va_low & 0x7f) << 16; /* 7b of VA in AVA/LP field */
67 rb |= (va_low & 0xfe); /* AVAL field */
72 rb |= (va_low & 0x7ff) << 12; /* remaining 11b of AVA */
74 rb |= (v >> 54) & 0x300; /* B field */
78 static target_ulong h_enter(CPUPPCState *env, sPAPREnvironment *spapr,
79 target_ulong opcode, target_ulong *args)
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];
85 target_ulong page_shift = 12;
90 /* only handle 4k and 16M pages for now */
91 if (pteh & HPTE_V_LARGE) {
92 #if 0 /* We don't support 64k pages yet */
93 if ((ptel & 0xf000) == 0x1000) {
97 if ((ptel & 0xff000) == 0) {
100 /* lowest AVA bit must be 0 for 16M pages */
109 raddr = (ptel & HPTE_R_RPN) & ~((1ULL << page_shift) - 1);
111 if (raddr < spapr->ram_limit) {
112 /* Regular RAM - should have WIMG=0010 */
113 if ((ptel & HPTE_R_WIMG) != HPTE_R_M) {
117 /* Looks like an IO address */
118 /* FIXME: What WIMG combinations could be sensible for IO?
119 * For now we allow WIMG=010x, but are there others? */
120 /* FIXME: Should we check against registered IO addresses? */
121 if ((ptel & (HPTE_R_W | HPTE_R_I | HPTE_R_M)) != HPTE_R_I) {
128 if ((pte_index * HASH_PTE_SIZE_64) & ~env->htab_mask) {
131 if (likely((flags & H_EXACT) == 0)) {
133 hpte = env->external_htab + (pte_index * HASH_PTE_SIZE_64);
138 if ((ldq_p(hpte) & HPTE_V_VALID) == 0) {
141 hpte += HASH_PTE_SIZE_64;
145 hpte = env->external_htab + (pte_index * HASH_PTE_SIZE_64);
146 if (ldq_p(hpte) & HPTE_V_VALID) {
150 stq_p(hpte + (HASH_PTE_SIZE_64/2), ptel);
151 /* eieio(); FIXME: need some sort of barrier for smp? */
154 args[0] = pte_index + i;
160 REMOVE_NOT_FOUND = 1,
165 static target_ulong remove_hpte(CPUPPCState *env, target_ulong ptex,
168 target_ulong *vp, target_ulong *rp)
171 target_ulong v, r, rb;
173 if ((ptex * HASH_PTE_SIZE_64) & ~env->htab_mask) {
177 hpte = env->external_htab + (ptex * HASH_PTE_SIZE_64);
180 r = ldq_p(hpte + (HASH_PTE_SIZE_64/2));
182 if ((v & HPTE_V_VALID) == 0 ||
183 ((flags & H_AVPN) && (v & ~0x7fULL) != avpn) ||
184 ((flags & H_ANDCOND) && (v & avpn) != 0)) {
185 return REMOVE_NOT_FOUND;
190 rb = compute_tlbie_rb(v, r, ptex);
191 ppc_tlb_invalidate_one(env, rb);
192 return REMOVE_SUCCESS;
195 static target_ulong h_remove(CPUPPCState *env, sPAPREnvironment *spapr,
196 target_ulong opcode, target_ulong *args)
198 target_ulong flags = args[0];
199 target_ulong pte_index = args[1];
200 target_ulong avpn = args[2];
203 ret = remove_hpte(env, pte_index, avpn, flags,
210 case REMOVE_NOT_FOUND:
223 #define H_BULK_REMOVE_TYPE 0xc000000000000000ULL
224 #define H_BULK_REMOVE_REQUEST 0x4000000000000000ULL
225 #define H_BULK_REMOVE_RESPONSE 0x8000000000000000ULL
226 #define H_BULK_REMOVE_END 0xc000000000000000ULL
227 #define H_BULK_REMOVE_CODE 0x3000000000000000ULL
228 #define H_BULK_REMOVE_SUCCESS 0x0000000000000000ULL
229 #define H_BULK_REMOVE_NOT_FOUND 0x1000000000000000ULL
230 #define H_BULK_REMOVE_PARM 0x2000000000000000ULL
231 #define H_BULK_REMOVE_HW 0x3000000000000000ULL
232 #define H_BULK_REMOVE_RC 0x0c00000000000000ULL
233 #define H_BULK_REMOVE_FLAGS 0x0300000000000000ULL
234 #define H_BULK_REMOVE_ABSOLUTE 0x0000000000000000ULL
235 #define H_BULK_REMOVE_ANDCOND 0x0100000000000000ULL
236 #define H_BULK_REMOVE_AVPN 0x0200000000000000ULL
237 #define H_BULK_REMOVE_PTEX 0x00ffffffffffffffULL
239 #define H_BULK_REMOVE_MAX_BATCH 4
241 static target_ulong h_bulk_remove(CPUPPCState *env, sPAPREnvironment *spapr,
242 target_ulong opcode, target_ulong *args)
246 for (i = 0; i < H_BULK_REMOVE_MAX_BATCH; i++) {
247 target_ulong *tsh = &args[i*2];
248 target_ulong tsl = args[i*2 + 1];
249 target_ulong v, r, ret;
251 if ((*tsh & H_BULK_REMOVE_TYPE) == H_BULK_REMOVE_END) {
253 } else if ((*tsh & H_BULK_REMOVE_TYPE) != H_BULK_REMOVE_REQUEST) {
257 *tsh &= H_BULK_REMOVE_PTEX | H_BULK_REMOVE_FLAGS;
258 *tsh |= H_BULK_REMOVE_RESPONSE;
260 if ((*tsh & H_BULK_REMOVE_ANDCOND) && (*tsh & H_BULK_REMOVE_AVPN)) {
261 *tsh |= H_BULK_REMOVE_PARM;
265 ret = remove_hpte(env, *tsh & H_BULK_REMOVE_PTEX, tsl,
266 (*tsh & H_BULK_REMOVE_FLAGS) >> 26,
273 *tsh |= (r & (HPTE_R_C | HPTE_R_R)) << 43;
287 static target_ulong h_protect(CPUPPCState *env, sPAPREnvironment *spapr,
288 target_ulong opcode, target_ulong *args)
290 target_ulong flags = args[0];
291 target_ulong pte_index = args[1];
292 target_ulong avpn = args[2];
294 target_ulong v, r, rb;
296 if ((pte_index * HASH_PTE_SIZE_64) & ~env->htab_mask) {
300 hpte = env->external_htab + (pte_index * HASH_PTE_SIZE_64);
303 r = ldq_p(hpte + (HASH_PTE_SIZE_64/2));
305 if ((v & HPTE_V_VALID) == 0 ||
306 ((flags & H_AVPN) && (v & ~0x7fULL) != avpn)) {
310 r &= ~(HPTE_R_PP0 | HPTE_R_PP | HPTE_R_N |
311 HPTE_R_KEY_HI | HPTE_R_KEY_LO);
312 r |= (flags << 55) & HPTE_R_PP0;
313 r |= (flags << 48) & HPTE_R_KEY_HI;
314 r |= flags & (HPTE_R_PP | HPTE_R_N | HPTE_R_KEY_LO);
315 rb = compute_tlbie_rb(v, r, pte_index);
316 stq_p(hpte, v & ~HPTE_V_VALID);
317 ppc_tlb_invalidate_one(env, rb);
318 stq_p(hpte + (HASH_PTE_SIZE_64/2), r);
319 /* Don't need a memory barrier, due to qemu's global lock */
324 static target_ulong h_set_dabr(CPUPPCState *env, sPAPREnvironment *spapr,
325 target_ulong opcode, target_ulong *args)
327 /* FIXME: actually implement this */
331 #define FLAGS_REGISTER_VPA 0x0000200000000000ULL
332 #define FLAGS_REGISTER_DTL 0x0000400000000000ULL
333 #define FLAGS_REGISTER_SLBSHADOW 0x0000600000000000ULL
334 #define FLAGS_DEREGISTER_VPA 0x0000a00000000000ULL
335 #define FLAGS_DEREGISTER_DTL 0x0000c00000000000ULL
336 #define FLAGS_DEREGISTER_SLBSHADOW 0x0000e00000000000ULL
338 #define VPA_MIN_SIZE 640
339 #define VPA_SIZE_OFFSET 0x4
340 #define VPA_SHARED_PROC_OFFSET 0x9
341 #define VPA_SHARED_PROC_VAL 0x2
343 static target_ulong register_vpa(CPUPPCState *env, target_ulong vpa)
349 hcall_dprintf("Can't cope with registering a VPA at logical 0\n");
353 if (vpa % env->dcache_line_size) {
356 /* FIXME: bounds check the address */
358 size = lduw_be_phys(vpa + 0x4);
360 if (size < VPA_MIN_SIZE) {
364 /* VPA is not allowed to cross a page boundary */
365 if ((vpa / 4096) != ((vpa + size - 1) / 4096)) {
371 tmp = ldub_phys(env->vpa + VPA_SHARED_PROC_OFFSET);
372 tmp |= VPA_SHARED_PROC_VAL;
373 stb_phys(env->vpa + VPA_SHARED_PROC_OFFSET, tmp);
378 static target_ulong deregister_vpa(CPUPPCState *env, target_ulong vpa)
380 if (env->slb_shadow) {
384 if (env->dispatch_trace_log) {
392 static target_ulong register_slb_shadow(CPUPPCState *env, target_ulong addr)
397 hcall_dprintf("Can't cope with SLB shadow at logical 0\n");
401 size = ldl_be_phys(addr + 0x4);
406 if ((addr / 4096) != ((addr + size - 1) / 4096)) {
414 env->slb_shadow = addr;
419 static target_ulong deregister_slb_shadow(CPUPPCState *env, target_ulong addr)
425 static target_ulong register_dtl(CPUPPCState *env, target_ulong addr)
430 hcall_dprintf("Can't cope with DTL at logical 0\n");
434 size = ldl_be_phys(addr + 0x4);
444 env->dispatch_trace_log = addr;
445 env->dtl_size = size;
450 static target_ulong deregister_dtl(CPUPPCState *env, target_ulong addr)
452 env->dispatch_trace_log = 0;
458 static target_ulong h_register_vpa(CPUPPCState *env, sPAPREnvironment *spapr,
459 target_ulong opcode, target_ulong *args)
461 target_ulong flags = args[0];
462 target_ulong procno = args[1];
463 target_ulong vpa = args[2];
464 target_ulong ret = H_PARAMETER;
467 for (tenv = first_cpu; tenv; tenv = tenv->next_cpu) {
468 if (tenv->cpu_index == procno) {
478 case FLAGS_REGISTER_VPA:
479 ret = register_vpa(tenv, vpa);
482 case FLAGS_DEREGISTER_VPA:
483 ret = deregister_vpa(tenv, vpa);
486 case FLAGS_REGISTER_SLBSHADOW:
487 ret = register_slb_shadow(tenv, vpa);
490 case FLAGS_DEREGISTER_SLBSHADOW:
491 ret = deregister_slb_shadow(tenv, vpa);
494 case FLAGS_REGISTER_DTL:
495 ret = register_dtl(tenv, vpa);
498 case FLAGS_DEREGISTER_DTL:
499 ret = deregister_dtl(tenv, vpa);
506 static target_ulong h_cede(CPUPPCState *env, sPAPREnvironment *spapr,
507 target_ulong opcode, target_ulong *args)
509 env->msr |= (1ULL << MSR_EE);
510 hreg_compute_hflags(env);
511 if (!cpu_has_work(env)) {
513 env->exception_index = EXCP_HLT;
514 env->exit_request = 1;
519 static target_ulong h_rtas(CPUPPCState *env, sPAPREnvironment *spapr,
520 target_ulong opcode, target_ulong *args)
522 target_ulong rtas_r3 = args[0];
523 uint32_t token = ldl_be_phys(rtas_r3);
524 uint32_t nargs = ldl_be_phys(rtas_r3 + 4);
525 uint32_t nret = ldl_be_phys(rtas_r3 + 8);
527 return spapr_rtas_call(spapr, token, nargs, rtas_r3 + 12,
528 nret, rtas_r3 + 12 + 4*nargs);
531 static target_ulong h_logical_load(CPUPPCState *env, sPAPREnvironment *spapr,
532 target_ulong opcode, target_ulong *args)
534 target_ulong size = args[0];
535 target_ulong addr = args[1];
539 args[0] = ldub_phys(addr);
542 args[0] = lduw_phys(addr);
545 args[0] = ldl_phys(addr);
548 args[0] = ldq_phys(addr);
554 static target_ulong h_logical_store(CPUPPCState *env, sPAPREnvironment *spapr,
555 target_ulong opcode, target_ulong *args)
557 target_ulong size = args[0];
558 target_ulong addr = args[1];
559 target_ulong val = args[2];
578 static target_ulong h_logical_memop(CPUPPCState *env, sPAPREnvironment *spapr,
579 target_ulong opcode, target_ulong *args)
581 target_ulong dst = args[0]; /* Destination address */
582 target_ulong src = args[1]; /* Source address */
583 target_ulong esize = args[2]; /* Element size (0=1,1=2,2=4,3=8) */
584 target_ulong count = args[3]; /* Element count */
585 target_ulong op = args[4]; /* 0 = copy, 1 = invert */
587 unsigned int mask = (1 << esize) - 1;
588 int step = 1 << esize;
590 if (count > 0x80000000) {
594 if ((dst & mask) || (src & mask) || (op > 1)) {
598 if (dst >= src && dst < (src + (count << esize))) {
599 dst = dst + ((count - 1) << esize);
600 src = src + ((count - 1) << esize);
607 tmp = ldub_phys(src);
610 tmp = lduw_phys(src);
645 static target_ulong h_logical_icbi(CPUPPCState *env, sPAPREnvironment *spapr,
646 target_ulong opcode, target_ulong *args)
648 /* Nothing to do on emulation, KVM will trap this in the kernel */
652 static target_ulong h_logical_dcbf(CPUPPCState *env, sPAPREnvironment *spapr,
653 target_ulong opcode, target_ulong *args)
655 /* Nothing to do on emulation, KVM will trap this in the kernel */
659 static spapr_hcall_fn papr_hypercall_table[(MAX_HCALL_OPCODE / 4) + 1];
660 static spapr_hcall_fn kvmppc_hypercall_table[KVMPPC_HCALL_MAX - KVMPPC_HCALL_BASE + 1];
662 void spapr_register_hypercall(target_ulong opcode, spapr_hcall_fn fn)
664 spapr_hcall_fn *slot;
666 if (opcode <= MAX_HCALL_OPCODE) {
667 assert((opcode & 0x3) == 0);
669 slot = &papr_hypercall_table[opcode / 4];
671 assert((opcode >= KVMPPC_HCALL_BASE) && (opcode <= KVMPPC_HCALL_MAX));
674 slot = &kvmppc_hypercall_table[opcode - KVMPPC_HCALL_BASE];
677 assert(!(*slot) || (fn == *slot));
681 target_ulong spapr_hypercall(CPUPPCState *env, target_ulong opcode,
684 if ((opcode <= MAX_HCALL_OPCODE)
685 && ((opcode & 0x3) == 0)) {
686 spapr_hcall_fn fn = papr_hypercall_table[opcode / 4];
689 return fn(env, spapr, opcode, args);
691 } else if ((opcode >= KVMPPC_HCALL_BASE) &&
692 (opcode <= KVMPPC_HCALL_MAX)) {
693 spapr_hcall_fn fn = kvmppc_hypercall_table[opcode - KVMPPC_HCALL_BASE];
696 return fn(env, spapr, opcode, args);
700 hcall_dprintf("Unimplemented hcall 0x" TARGET_FMT_lx "\n", opcode);
704 static void hypercall_register_types(void)
707 spapr_register_hypercall(H_ENTER, h_enter);
708 spapr_register_hypercall(H_REMOVE, h_remove);
709 spapr_register_hypercall(H_PROTECT, h_protect);
712 spapr_register_hypercall(H_BULK_REMOVE, h_bulk_remove);
715 spapr_register_hypercall(H_SET_DABR, h_set_dabr);
718 spapr_register_hypercall(H_REGISTER_VPA, h_register_vpa);
719 spapr_register_hypercall(H_CEDE, h_cede);
721 /* "debugger" hcalls (also used by SLOF). Note: We do -not- differenciate
722 * here between the "CI" and the "CACHE" variants, they will use whatever
723 * mapping attributes qemu is using. When using KVM, the kernel will
724 * enforce the attributes more strongly
726 spapr_register_hypercall(H_LOGICAL_CI_LOAD, h_logical_load);
727 spapr_register_hypercall(H_LOGICAL_CI_STORE, h_logical_store);
728 spapr_register_hypercall(H_LOGICAL_CACHE_LOAD, h_logical_load);
729 spapr_register_hypercall(H_LOGICAL_CACHE_STORE, h_logical_store);
730 spapr_register_hypercall(H_LOGICAL_ICBI, h_logical_icbi);
731 spapr_register_hypercall(H_LOGICAL_DCBF, h_logical_dcbf);
732 spapr_register_hypercall(KVMPPC_H_LOGICAL_MEMOP, h_logical_memop);
734 /* qemu/KVM-PPC specific hcalls */
735 spapr_register_hypercall(KVMPPC_H_RTAS, h_rtas);
738 type_init(hypercall_register_types)