1 #include "sysemu/sysemu.h"
3 #include "helper_regs.h"
4 #include "hw/ppc/spapr.h"
5 #include "mmu-hash64.h"
7 static target_ulong compute_tlbie_rb(target_ulong v, target_ulong r,
8 target_ulong pte_index)
10 target_ulong rb, va_low;
12 rb = (v & ~0x7fULL) << 16; /* AVA field */
13 va_low = pte_index >> 3;
14 if (v & HPTE64_V_SECONDARY) {
17 /* xor vsid from AVA */
18 if (!(v & HPTE64_V_1TB_SEG)) {
24 if (v & HPTE64_V_LARGE) {
25 rb |= 1; /* L field */
26 #if 0 /* Disable that P7 specific bit for now */
28 /* non-16MB large page, must be 64k */
29 /* (masks depend on page size) */
30 rb |= 0x1000; /* page encoding in LP field */
31 rb |= (va_low & 0x7f) << 16; /* 7b of VA in AVA/LP field */
32 rb |= (va_low & 0xfe); /* AVAL field */
37 rb |= (va_low & 0x7ff) << 12; /* remaining 11b of AVA */
39 rb |= (v >> 54) & 0x300; /* B field */
43 static inline bool valid_pte_index(CPUPPCState *env, target_ulong pte_index)
46 * hash value/pteg group index is normalized by htab_mask
48 if (((pte_index & ~7ULL) / HPTES_PER_GROUP) & ~env->htab_mask) {
54 static target_ulong h_enter(PowerPCCPU *cpu, sPAPREnvironment *spapr,
55 target_ulong opcode, target_ulong *args)
57 CPUPPCState *env = &cpu->env;
58 target_ulong flags = args[0];
59 target_ulong pte_index = args[1];
60 target_ulong pteh = args[2];
61 target_ulong ptel = args[3];
62 target_ulong page_shift = 12;
67 /* only handle 4k and 16M pages for now */
68 if (pteh & HPTE64_V_LARGE) {
69 #if 0 /* We don't support 64k pages yet */
70 if ((ptel & 0xf000) == 0x1000) {
74 if ((ptel & 0xff000) == 0) {
77 /* lowest AVA bit must be 0 for 16M pages */
86 raddr = (ptel & HPTE64_R_RPN) & ~((1ULL << page_shift) - 1);
88 if (raddr < spapr->ram_limit) {
89 /* Regular RAM - should have WIMG=0010 */
90 if ((ptel & HPTE64_R_WIMG) != HPTE64_R_M) {
94 /* Looks like an IO address */
95 /* FIXME: What WIMG combinations could be sensible for IO?
96 * For now we allow WIMG=010x, but are there others? */
97 /* FIXME: Should we check against registered IO addresses? */
98 if ((ptel & (HPTE64_R_W | HPTE64_R_I | HPTE64_R_M)) != HPTE64_R_I) {
105 if (!valid_pte_index(env, pte_index)) {
108 if (likely((flags & H_EXACT) == 0)) {
110 hpte = pte_index * HASH_PTE_SIZE_64;
115 if ((ppc_hash64_load_hpte0(env, hpte) & HPTE64_V_VALID) == 0) {
118 hpte += HASH_PTE_SIZE_64;
122 hpte = pte_index * HASH_PTE_SIZE_64;
123 if (ppc_hash64_load_hpte0(env, hpte) & HPTE64_V_VALID) {
127 ppc_hash64_store_hpte1(env, hpte, ptel);
128 /* eieio(); FIXME: need some sort of barrier for smp? */
129 ppc_hash64_store_hpte0(env, hpte, pteh | HPTE64_V_HPTE_DIRTY);
131 args[0] = pte_index + i;
137 REMOVE_NOT_FOUND = 1,
142 static RemoveResult remove_hpte(CPUPPCState *env, target_ulong ptex,
145 target_ulong *vp, target_ulong *rp)
148 target_ulong v, r, rb;
150 if (!valid_pte_index(env, ptex)) {
154 hpte = ptex * HASH_PTE_SIZE_64;
156 v = ppc_hash64_load_hpte0(env, hpte);
157 r = ppc_hash64_load_hpte1(env, hpte);
159 if ((v & HPTE64_V_VALID) == 0 ||
160 ((flags & H_AVPN) && (v & ~0x7fULL) != avpn) ||
161 ((flags & H_ANDCOND) && (v & avpn) != 0)) {
162 return REMOVE_NOT_FOUND;
166 ppc_hash64_store_hpte0(env, hpte, HPTE64_V_HPTE_DIRTY);
167 rb = compute_tlbie_rb(v, r, ptex);
168 ppc_tlb_invalidate_one(env, rb);
169 return REMOVE_SUCCESS;
172 static target_ulong h_remove(PowerPCCPU *cpu, sPAPREnvironment *spapr,
173 target_ulong opcode, target_ulong *args)
175 CPUPPCState *env = &cpu->env;
176 target_ulong flags = args[0];
177 target_ulong pte_index = args[1];
178 target_ulong avpn = args[2];
181 ret = remove_hpte(env, pte_index, avpn, flags,
188 case REMOVE_NOT_FOUND:
198 g_assert_not_reached();
201 #define H_BULK_REMOVE_TYPE 0xc000000000000000ULL
202 #define H_BULK_REMOVE_REQUEST 0x4000000000000000ULL
203 #define H_BULK_REMOVE_RESPONSE 0x8000000000000000ULL
204 #define H_BULK_REMOVE_END 0xc000000000000000ULL
205 #define H_BULK_REMOVE_CODE 0x3000000000000000ULL
206 #define H_BULK_REMOVE_SUCCESS 0x0000000000000000ULL
207 #define H_BULK_REMOVE_NOT_FOUND 0x1000000000000000ULL
208 #define H_BULK_REMOVE_PARM 0x2000000000000000ULL
209 #define H_BULK_REMOVE_HW 0x3000000000000000ULL
210 #define H_BULK_REMOVE_RC 0x0c00000000000000ULL
211 #define H_BULK_REMOVE_FLAGS 0x0300000000000000ULL
212 #define H_BULK_REMOVE_ABSOLUTE 0x0000000000000000ULL
213 #define H_BULK_REMOVE_ANDCOND 0x0100000000000000ULL
214 #define H_BULK_REMOVE_AVPN 0x0200000000000000ULL
215 #define H_BULK_REMOVE_PTEX 0x00ffffffffffffffULL
217 #define H_BULK_REMOVE_MAX_BATCH 4
219 static target_ulong h_bulk_remove(PowerPCCPU *cpu, sPAPREnvironment *spapr,
220 target_ulong opcode, target_ulong *args)
222 CPUPPCState *env = &cpu->env;
225 for (i = 0; i < H_BULK_REMOVE_MAX_BATCH; i++) {
226 target_ulong *tsh = &args[i*2];
227 target_ulong tsl = args[i*2 + 1];
228 target_ulong v, r, ret;
230 if ((*tsh & H_BULK_REMOVE_TYPE) == H_BULK_REMOVE_END) {
232 } else if ((*tsh & H_BULK_REMOVE_TYPE) != H_BULK_REMOVE_REQUEST) {
236 *tsh &= H_BULK_REMOVE_PTEX | H_BULK_REMOVE_FLAGS;
237 *tsh |= H_BULK_REMOVE_RESPONSE;
239 if ((*tsh & H_BULK_REMOVE_ANDCOND) && (*tsh & H_BULK_REMOVE_AVPN)) {
240 *tsh |= H_BULK_REMOVE_PARM;
244 ret = remove_hpte(env, *tsh & H_BULK_REMOVE_PTEX, tsl,
245 (*tsh & H_BULK_REMOVE_FLAGS) >> 26,
252 *tsh |= (r & (HPTE64_R_C | HPTE64_R_R)) << 43;
266 static target_ulong h_protect(PowerPCCPU *cpu, sPAPREnvironment *spapr,
267 target_ulong opcode, target_ulong *args)
269 CPUPPCState *env = &cpu->env;
270 target_ulong flags = args[0];
271 target_ulong pte_index = args[1];
272 target_ulong avpn = args[2];
274 target_ulong v, r, rb;
276 if (!valid_pte_index(env, pte_index)) {
280 hpte = pte_index * HASH_PTE_SIZE_64;
282 v = ppc_hash64_load_hpte0(env, hpte);
283 r = ppc_hash64_load_hpte1(env, hpte);
285 if ((v & HPTE64_V_VALID) == 0 ||
286 ((flags & H_AVPN) && (v & ~0x7fULL) != avpn)) {
290 r &= ~(HPTE64_R_PP0 | HPTE64_R_PP | HPTE64_R_N |
291 HPTE64_R_KEY_HI | HPTE64_R_KEY_LO);
292 r |= (flags << 55) & HPTE64_R_PP0;
293 r |= (flags << 48) & HPTE64_R_KEY_HI;
294 r |= flags & (HPTE64_R_PP | HPTE64_R_N | HPTE64_R_KEY_LO);
295 rb = compute_tlbie_rb(v, r, pte_index);
296 ppc_hash64_store_hpte0(env, hpte, (v & ~HPTE64_V_VALID) | HPTE64_V_HPTE_DIRTY);
297 ppc_tlb_invalidate_one(env, rb);
298 ppc_hash64_store_hpte1(env, hpte, r);
299 /* Don't need a memory barrier, due to qemu's global lock */
300 ppc_hash64_store_hpte0(env, hpte, v | HPTE64_V_HPTE_DIRTY);
304 static target_ulong h_read(PowerPCCPU *cpu, sPAPREnvironment *spapr,
305 target_ulong opcode, target_ulong *args)
307 CPUPPCState *env = &cpu->env;
308 target_ulong flags = args[0];
309 target_ulong pte_index = args[1];
311 int i, ridx, n_entries = 1;
313 if (!valid_pte_index(env, pte_index)) {
317 if (flags & H_READ_4) {
318 /* Clear the two low order bits */
319 pte_index &= ~(3ULL);
323 hpte = env->external_htab + (pte_index * HASH_PTE_SIZE_64);
325 for (i = 0, ridx = 0; i < n_entries; i++) {
326 args[ridx++] = ldq_p(hpte);
327 args[ridx++] = ldq_p(hpte + (HASH_PTE_SIZE_64/2));
328 hpte += HASH_PTE_SIZE_64;
334 static target_ulong h_set_dabr(PowerPCCPU *cpu, sPAPREnvironment *spapr,
335 target_ulong opcode, target_ulong *args)
337 /* FIXME: actually implement this */
341 #define FLAGS_REGISTER_VPA 0x0000200000000000ULL
342 #define FLAGS_REGISTER_DTL 0x0000400000000000ULL
343 #define FLAGS_REGISTER_SLBSHADOW 0x0000600000000000ULL
344 #define FLAGS_DEREGISTER_VPA 0x0000a00000000000ULL
345 #define FLAGS_DEREGISTER_DTL 0x0000c00000000000ULL
346 #define FLAGS_DEREGISTER_SLBSHADOW 0x0000e00000000000ULL
348 #define VPA_MIN_SIZE 640
349 #define VPA_SIZE_OFFSET 0x4
350 #define VPA_SHARED_PROC_OFFSET 0x9
351 #define VPA_SHARED_PROC_VAL 0x2
353 static target_ulong register_vpa(CPUPPCState *env, target_ulong vpa)
355 CPUState *cs = ENV_GET_CPU(env);
360 hcall_dprintf("Can't cope with registering a VPA at logical 0\n");
364 if (vpa % env->dcache_line_size) {
367 /* FIXME: bounds check the address */
369 size = lduw_be_phys(cs->as, vpa + 0x4);
371 if (size < VPA_MIN_SIZE) {
375 /* VPA is not allowed to cross a page boundary */
376 if ((vpa / 4096) != ((vpa + size - 1) / 4096)) {
382 tmp = ldub_phys(cs->as, env->vpa_addr + VPA_SHARED_PROC_OFFSET);
383 tmp |= VPA_SHARED_PROC_VAL;
384 stb_phys(cs->as, env->vpa_addr + VPA_SHARED_PROC_OFFSET, tmp);
389 static target_ulong deregister_vpa(CPUPPCState *env, target_ulong vpa)
391 if (env->slb_shadow_addr) {
403 static target_ulong register_slb_shadow(CPUPPCState *env, target_ulong addr)
405 CPUState *cs = ENV_GET_CPU(env);
409 hcall_dprintf("Can't cope with SLB shadow at logical 0\n");
413 size = ldl_be_phys(cs->as, addr + 0x4);
418 if ((addr / 4096) != ((addr + size - 1) / 4096)) {
422 if (!env->vpa_addr) {
426 env->slb_shadow_addr = addr;
427 env->slb_shadow_size = size;
432 static target_ulong deregister_slb_shadow(CPUPPCState *env, target_ulong addr)
434 env->slb_shadow_addr = 0;
435 env->slb_shadow_size = 0;
439 static target_ulong register_dtl(CPUPPCState *env, target_ulong addr)
441 CPUState *cs = ENV_GET_CPU(env);
445 hcall_dprintf("Can't cope with DTL at logical 0\n");
449 size = ldl_be_phys(cs->as, addr + 0x4);
455 if (!env->vpa_addr) {
459 env->dtl_addr = addr;
460 env->dtl_size = size;
465 static target_ulong deregister_dtl(CPUPPCState *env, target_ulong addr)
473 static target_ulong h_register_vpa(PowerPCCPU *cpu, sPAPREnvironment *spapr,
474 target_ulong opcode, target_ulong *args)
476 target_ulong flags = args[0];
477 target_ulong procno = args[1];
478 target_ulong vpa = args[2];
479 target_ulong ret = H_PARAMETER;
483 tcpu = qemu_get_cpu(procno);
487 tenv = tcpu->env_ptr;
490 case FLAGS_REGISTER_VPA:
491 ret = register_vpa(tenv, vpa);
494 case FLAGS_DEREGISTER_VPA:
495 ret = deregister_vpa(tenv, vpa);
498 case FLAGS_REGISTER_SLBSHADOW:
499 ret = register_slb_shadow(tenv, vpa);
502 case FLAGS_DEREGISTER_SLBSHADOW:
503 ret = deregister_slb_shadow(tenv, vpa);
506 case FLAGS_REGISTER_DTL:
507 ret = register_dtl(tenv, vpa);
510 case FLAGS_DEREGISTER_DTL:
511 ret = deregister_dtl(tenv, vpa);
518 static target_ulong h_cede(PowerPCCPU *cpu, sPAPREnvironment *spapr,
519 target_ulong opcode, target_ulong *args)
521 CPUPPCState *env = &cpu->env;
522 CPUState *cs = CPU(cpu);
524 env->msr |= (1ULL << MSR_EE);
525 hreg_compute_hflags(env);
526 if (!cpu_has_work(cs)) {
528 env->exception_index = EXCP_HLT;
529 cs->exit_request = 1;
534 static target_ulong h_rtas(PowerPCCPU *cpu, sPAPREnvironment *spapr,
535 target_ulong opcode, target_ulong *args)
537 target_ulong rtas_r3 = args[0];
538 uint32_t token = rtas_ld(rtas_r3, 0);
539 uint32_t nargs = rtas_ld(rtas_r3, 1);
540 uint32_t nret = rtas_ld(rtas_r3, 2);
542 return spapr_rtas_call(cpu, spapr, token, nargs, rtas_r3 + 12,
543 nret, rtas_r3 + 12 + 4*nargs);
546 static target_ulong h_logical_load(PowerPCCPU *cpu, sPAPREnvironment *spapr,
547 target_ulong opcode, target_ulong *args)
549 CPUState *cs = CPU(cpu);
550 target_ulong size = args[0];
551 target_ulong addr = args[1];
555 args[0] = ldub_phys(cs->as, addr);
558 args[0] = lduw_phys(cs->as, addr);
561 args[0] = ldl_phys(cs->as, addr);
564 args[0] = ldq_phys(cs->as, addr);
570 static target_ulong h_logical_store(PowerPCCPU *cpu, sPAPREnvironment *spapr,
571 target_ulong opcode, target_ulong *args)
573 CPUState *cs = CPU(cpu);
575 target_ulong size = args[0];
576 target_ulong addr = args[1];
577 target_ulong val = args[2];
581 stb_phys(cs->as, addr, val);
584 stw_phys(cs->as, addr, val);
587 stl_phys(cs->as, addr, val);
590 stq_phys(cs->as, addr, val);
596 static target_ulong h_logical_memop(PowerPCCPU *cpu, sPAPREnvironment *spapr,
597 target_ulong opcode, target_ulong *args)
599 CPUState *cs = CPU(cpu);
601 target_ulong dst = args[0]; /* Destination address */
602 target_ulong src = args[1]; /* Source address */
603 target_ulong esize = args[2]; /* Element size (0=1,1=2,2=4,3=8) */
604 target_ulong count = args[3]; /* Element count */
605 target_ulong op = args[4]; /* 0 = copy, 1 = invert */
607 unsigned int mask = (1 << esize) - 1;
608 int step = 1 << esize;
610 if (count > 0x80000000) {
614 if ((dst & mask) || (src & mask) || (op > 1)) {
618 if (dst >= src && dst < (src + (count << esize))) {
619 dst = dst + ((count - 1) << esize);
620 src = src + ((count - 1) << esize);
627 tmp = ldub_phys(cs->as, src);
630 tmp = lduw_phys(cs->as, src);
633 tmp = ldl_phys(cs->as, src);
636 tmp = ldq_phys(cs->as, src);
646 stb_phys(cs->as, dst, tmp);
649 stw_phys(cs->as, dst, tmp);
652 stl_phys(cs->as, dst, tmp);
655 stq_phys(cs->as, dst, tmp);
665 static target_ulong h_logical_icbi(PowerPCCPU *cpu, sPAPREnvironment *spapr,
666 target_ulong opcode, target_ulong *args)
668 /* Nothing to do on emulation, KVM will trap this in the kernel */
672 static target_ulong h_logical_dcbf(PowerPCCPU *cpu, sPAPREnvironment *spapr,
673 target_ulong opcode, target_ulong *args)
675 /* Nothing to do on emulation, KVM will trap this in the kernel */
679 static target_ulong h_set_mode(PowerPCCPU *cpu, sPAPREnvironment *spapr,
680 target_ulong opcode, target_ulong *args)
683 target_ulong mflags = args[0];
684 target_ulong resource = args[1];
685 target_ulong value1 = args[2];
686 target_ulong value2 = args[3];
687 target_ulong ret = H_P2;
689 if (resource == H_SET_MODE_ENDIAN) {
700 case H_SET_MODE_ENDIAN_BIG:
702 PowerPCCPU *cp = POWERPC_CPU(cs);
703 CPUPPCState *env = &cp->env;
704 env->spr[SPR_LPCR] &= ~LPCR_ILE;
709 case H_SET_MODE_ENDIAN_LITTLE:
711 PowerPCCPU *cp = POWERPC_CPU(cs);
712 CPUPPCState *env = &cp->env;
713 env->spr[SPR_LPCR] |= LPCR_ILE;
719 ret = H_UNSUPPORTED_FLAG;
727 static spapr_hcall_fn papr_hypercall_table[(MAX_HCALL_OPCODE / 4) + 1];
728 static spapr_hcall_fn kvmppc_hypercall_table[KVMPPC_HCALL_MAX - KVMPPC_HCALL_BASE + 1];
730 void spapr_register_hypercall(target_ulong opcode, spapr_hcall_fn fn)
732 spapr_hcall_fn *slot;
734 if (opcode <= MAX_HCALL_OPCODE) {
735 assert((opcode & 0x3) == 0);
737 slot = &papr_hypercall_table[opcode / 4];
739 assert((opcode >= KVMPPC_HCALL_BASE) && (opcode <= KVMPPC_HCALL_MAX));
741 slot = &kvmppc_hypercall_table[opcode - KVMPPC_HCALL_BASE];
748 target_ulong spapr_hypercall(PowerPCCPU *cpu, target_ulong opcode,
751 if ((opcode <= MAX_HCALL_OPCODE)
752 && ((opcode & 0x3) == 0)) {
753 spapr_hcall_fn fn = papr_hypercall_table[opcode / 4];
756 return fn(cpu, spapr, opcode, args);
758 } else if ((opcode >= KVMPPC_HCALL_BASE) &&
759 (opcode <= KVMPPC_HCALL_MAX)) {
760 spapr_hcall_fn fn = kvmppc_hypercall_table[opcode - KVMPPC_HCALL_BASE];
763 return fn(cpu, spapr, opcode, args);
767 hcall_dprintf("Unimplemented hcall 0x" TARGET_FMT_lx "\n", opcode);
771 static void hypercall_register_types(void)
774 spapr_register_hypercall(H_ENTER, h_enter);
775 spapr_register_hypercall(H_REMOVE, h_remove);
776 spapr_register_hypercall(H_PROTECT, h_protect);
777 spapr_register_hypercall(H_READ, h_read);
780 spapr_register_hypercall(H_BULK_REMOVE, h_bulk_remove);
783 spapr_register_hypercall(H_SET_DABR, h_set_dabr);
786 spapr_register_hypercall(H_REGISTER_VPA, h_register_vpa);
787 spapr_register_hypercall(H_CEDE, h_cede);
789 /* "debugger" hcalls (also used by SLOF). Note: We do -not- differenciate
790 * here between the "CI" and the "CACHE" variants, they will use whatever
791 * mapping attributes qemu is using. When using KVM, the kernel will
792 * enforce the attributes more strongly
794 spapr_register_hypercall(H_LOGICAL_CI_LOAD, h_logical_load);
795 spapr_register_hypercall(H_LOGICAL_CI_STORE, h_logical_store);
796 spapr_register_hypercall(H_LOGICAL_CACHE_LOAD, h_logical_load);
797 spapr_register_hypercall(H_LOGICAL_CACHE_STORE, h_logical_store);
798 spapr_register_hypercall(H_LOGICAL_ICBI, h_logical_icbi);
799 spapr_register_hypercall(H_LOGICAL_DCBF, h_logical_dcbf);
800 spapr_register_hypercall(KVMPPC_H_LOGICAL_MEMOP, h_logical_memop);
802 /* qemu/KVM-PPC specific hcalls */
803 spapr_register_hypercall(KVMPPC_H_RTAS, h_rtas);
805 spapr_register_hypercall(H_SET_MODE, h_set_mode);
808 type_init(hypercall_register_types)