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
)) {
110 if (likely((flags
& H_EXACT
) == 0)) {
112 token
= ppc_hash64_start_access(cpu
, pte_index
);
115 ppc_hash64_stop_access(token
);
118 if ((ppc_hash64_load_hpte0(env
, token
, index
) & HPTE64_V_VALID
) == 0) {
122 ppc_hash64_stop_access(token
);
124 token
= ppc_hash64_start_access(cpu
, pte_index
);
125 if (ppc_hash64_load_hpte0(env
, token
, 0) & HPTE64_V_VALID
) {
126 ppc_hash64_stop_access(token
);
129 ppc_hash64_stop_access(token
);
132 ppc_hash64_store_hpte(env
, pte_index
+ index
,
133 pteh
| HPTE64_V_HPTE_DIRTY
, ptel
);
135 args
[0] = pte_index
+ index
;
141 REMOVE_NOT_FOUND
= 1,
146 static RemoveResult
remove_hpte(CPUPPCState
*env
, target_ulong ptex
,
149 target_ulong
*vp
, target_ulong
*rp
)
152 target_ulong v
, r
, rb
;
154 if (!valid_pte_index(env
, ptex
)) {
158 token
= ppc_hash64_start_access(ppc_env_get_cpu(env
), ptex
);
159 v
= ppc_hash64_load_hpte0(env
, token
, 0);
160 r
= ppc_hash64_load_hpte1(env
, token
, 0);
161 ppc_hash64_stop_access(token
);
163 if ((v
& HPTE64_V_VALID
) == 0 ||
164 ((flags
& H_AVPN
) && (v
& ~0x7fULL
) != avpn
) ||
165 ((flags
& H_ANDCOND
) && (v
& avpn
) != 0)) {
166 return REMOVE_NOT_FOUND
;
170 ppc_hash64_store_hpte(env
, ptex
, HPTE64_V_HPTE_DIRTY
, 0);
171 rb
= compute_tlbie_rb(v
, r
, ptex
);
172 ppc_tlb_invalidate_one(env
, rb
);
173 return REMOVE_SUCCESS
;
176 static target_ulong
h_remove(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
177 target_ulong opcode
, target_ulong
*args
)
179 CPUPPCState
*env
= &cpu
->env
;
180 target_ulong flags
= args
[0];
181 target_ulong pte_index
= args
[1];
182 target_ulong avpn
= args
[2];
185 ret
= remove_hpte(env
, pte_index
, avpn
, flags
,
192 case REMOVE_NOT_FOUND
:
202 g_assert_not_reached();
205 #define H_BULK_REMOVE_TYPE 0xc000000000000000ULL
206 #define H_BULK_REMOVE_REQUEST 0x4000000000000000ULL
207 #define H_BULK_REMOVE_RESPONSE 0x8000000000000000ULL
208 #define H_BULK_REMOVE_END 0xc000000000000000ULL
209 #define H_BULK_REMOVE_CODE 0x3000000000000000ULL
210 #define H_BULK_REMOVE_SUCCESS 0x0000000000000000ULL
211 #define H_BULK_REMOVE_NOT_FOUND 0x1000000000000000ULL
212 #define H_BULK_REMOVE_PARM 0x2000000000000000ULL
213 #define H_BULK_REMOVE_HW 0x3000000000000000ULL
214 #define H_BULK_REMOVE_RC 0x0c00000000000000ULL
215 #define H_BULK_REMOVE_FLAGS 0x0300000000000000ULL
216 #define H_BULK_REMOVE_ABSOLUTE 0x0000000000000000ULL
217 #define H_BULK_REMOVE_ANDCOND 0x0100000000000000ULL
218 #define H_BULK_REMOVE_AVPN 0x0200000000000000ULL
219 #define H_BULK_REMOVE_PTEX 0x00ffffffffffffffULL
221 #define H_BULK_REMOVE_MAX_BATCH 4
223 static target_ulong
h_bulk_remove(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
224 target_ulong opcode
, target_ulong
*args
)
226 CPUPPCState
*env
= &cpu
->env
;
229 for (i
= 0; i
< H_BULK_REMOVE_MAX_BATCH
; i
++) {
230 target_ulong
*tsh
= &args
[i
*2];
231 target_ulong tsl
= args
[i
*2 + 1];
232 target_ulong v
, r
, ret
;
234 if ((*tsh
& H_BULK_REMOVE_TYPE
) == H_BULK_REMOVE_END
) {
236 } else if ((*tsh
& H_BULK_REMOVE_TYPE
) != H_BULK_REMOVE_REQUEST
) {
240 *tsh
&= H_BULK_REMOVE_PTEX
| H_BULK_REMOVE_FLAGS
;
241 *tsh
|= H_BULK_REMOVE_RESPONSE
;
243 if ((*tsh
& H_BULK_REMOVE_ANDCOND
) && (*tsh
& H_BULK_REMOVE_AVPN
)) {
244 *tsh
|= H_BULK_REMOVE_PARM
;
248 ret
= remove_hpte(env
, *tsh
& H_BULK_REMOVE_PTEX
, tsl
,
249 (*tsh
& H_BULK_REMOVE_FLAGS
) >> 26,
256 *tsh
|= (r
& (HPTE64_R_C
| HPTE64_R_R
)) << 43;
270 static target_ulong
h_protect(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
271 target_ulong opcode
, target_ulong
*args
)
273 CPUPPCState
*env
= &cpu
->env
;
274 target_ulong flags
= args
[0];
275 target_ulong pte_index
= args
[1];
276 target_ulong avpn
= args
[2];
278 target_ulong v
, r
, rb
;
280 if (!valid_pte_index(env
, pte_index
)) {
284 token
= ppc_hash64_start_access(cpu
, pte_index
);
285 v
= ppc_hash64_load_hpte0(env
, token
, 0);
286 r
= ppc_hash64_load_hpte1(env
, token
, 0);
287 ppc_hash64_stop_access(token
);
289 if ((v
& HPTE64_V_VALID
) == 0 ||
290 ((flags
& H_AVPN
) && (v
& ~0x7fULL
) != avpn
)) {
294 r
&= ~(HPTE64_R_PP0
| HPTE64_R_PP
| HPTE64_R_N
|
295 HPTE64_R_KEY_HI
| HPTE64_R_KEY_LO
);
296 r
|= (flags
<< 55) & HPTE64_R_PP0
;
297 r
|= (flags
<< 48) & HPTE64_R_KEY_HI
;
298 r
|= flags
& (HPTE64_R_PP
| HPTE64_R_N
| HPTE64_R_KEY_LO
);
299 rb
= compute_tlbie_rb(v
, r
, pte_index
);
300 ppc_hash64_store_hpte(env
, pte_index
,
301 (v
& ~HPTE64_V_VALID
) | HPTE64_V_HPTE_DIRTY
, 0);
302 ppc_tlb_invalidate_one(env
, rb
);
303 /* Don't need a memory barrier, due to qemu's global lock */
304 ppc_hash64_store_hpte(env
, pte_index
, v
| HPTE64_V_HPTE_DIRTY
, r
);
308 static target_ulong
h_read(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
309 target_ulong opcode
, target_ulong
*args
)
311 CPUPPCState
*env
= &cpu
->env
;
312 target_ulong flags
= args
[0];
313 target_ulong pte_index
= args
[1];
315 int i
, ridx
, n_entries
= 1;
317 if (!valid_pte_index(env
, pte_index
)) {
321 if (flags
& H_READ_4
) {
322 /* Clear the two low order bits */
323 pte_index
&= ~(3ULL);
327 hpte
= env
->external_htab
+ (pte_index
* HASH_PTE_SIZE_64
);
329 for (i
= 0, ridx
= 0; i
< n_entries
; i
++) {
330 args
[ridx
++] = ldq_p(hpte
);
331 args
[ridx
++] = ldq_p(hpte
+ (HASH_PTE_SIZE_64
/2));
332 hpte
+= HASH_PTE_SIZE_64
;
338 static target_ulong
h_set_dabr(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
339 target_ulong opcode
, target_ulong
*args
)
341 /* FIXME: actually implement this */
345 #define FLAGS_REGISTER_VPA 0x0000200000000000ULL
346 #define FLAGS_REGISTER_DTL 0x0000400000000000ULL
347 #define FLAGS_REGISTER_SLBSHADOW 0x0000600000000000ULL
348 #define FLAGS_DEREGISTER_VPA 0x0000a00000000000ULL
349 #define FLAGS_DEREGISTER_DTL 0x0000c00000000000ULL
350 #define FLAGS_DEREGISTER_SLBSHADOW 0x0000e00000000000ULL
352 #define VPA_MIN_SIZE 640
353 #define VPA_SIZE_OFFSET 0x4
354 #define VPA_SHARED_PROC_OFFSET 0x9
355 #define VPA_SHARED_PROC_VAL 0x2
357 static target_ulong
register_vpa(CPUPPCState
*env
, target_ulong vpa
)
359 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
364 hcall_dprintf("Can't cope with registering a VPA at logical 0\n");
368 if (vpa
% env
->dcache_line_size
) {
371 /* FIXME: bounds check the address */
373 size
= lduw_be_phys(cs
->as
, vpa
+ 0x4);
375 if (size
< VPA_MIN_SIZE
) {
379 /* VPA is not allowed to cross a page boundary */
380 if ((vpa
/ 4096) != ((vpa
+ size
- 1) / 4096)) {
386 tmp
= ldub_phys(cs
->as
, env
->vpa_addr
+ VPA_SHARED_PROC_OFFSET
);
387 tmp
|= VPA_SHARED_PROC_VAL
;
388 stb_phys(cs
->as
, env
->vpa_addr
+ VPA_SHARED_PROC_OFFSET
, tmp
);
393 static target_ulong
deregister_vpa(CPUPPCState
*env
, target_ulong vpa
)
395 if (env
->slb_shadow_addr
) {
407 static target_ulong
register_slb_shadow(CPUPPCState
*env
, target_ulong addr
)
409 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
413 hcall_dprintf("Can't cope with SLB shadow at logical 0\n");
417 size
= ldl_be_phys(cs
->as
, addr
+ 0x4);
422 if ((addr
/ 4096) != ((addr
+ size
- 1) / 4096)) {
426 if (!env
->vpa_addr
) {
430 env
->slb_shadow_addr
= addr
;
431 env
->slb_shadow_size
= size
;
436 static target_ulong
deregister_slb_shadow(CPUPPCState
*env
, target_ulong addr
)
438 env
->slb_shadow_addr
= 0;
439 env
->slb_shadow_size
= 0;
443 static target_ulong
register_dtl(CPUPPCState
*env
, target_ulong addr
)
445 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
449 hcall_dprintf("Can't cope with DTL at logical 0\n");
453 size
= ldl_be_phys(cs
->as
, addr
+ 0x4);
459 if (!env
->vpa_addr
) {
463 env
->dtl_addr
= addr
;
464 env
->dtl_size
= size
;
469 static target_ulong
deregister_dtl(CPUPPCState
*env
, target_ulong addr
)
477 static target_ulong
h_register_vpa(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
478 target_ulong opcode
, target_ulong
*args
)
480 target_ulong flags
= args
[0];
481 target_ulong procno
= args
[1];
482 target_ulong vpa
= args
[2];
483 target_ulong ret
= H_PARAMETER
;
487 tcpu
= ppc_get_vcpu_by_dt_id(procno
);
494 case FLAGS_REGISTER_VPA
:
495 ret
= register_vpa(tenv
, vpa
);
498 case FLAGS_DEREGISTER_VPA
:
499 ret
= deregister_vpa(tenv
, vpa
);
502 case FLAGS_REGISTER_SLBSHADOW
:
503 ret
= register_slb_shadow(tenv
, vpa
);
506 case FLAGS_DEREGISTER_SLBSHADOW
:
507 ret
= deregister_slb_shadow(tenv
, vpa
);
510 case FLAGS_REGISTER_DTL
:
511 ret
= register_dtl(tenv
, vpa
);
514 case FLAGS_DEREGISTER_DTL
:
515 ret
= deregister_dtl(tenv
, vpa
);
522 static target_ulong
h_cede(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
523 target_ulong opcode
, target_ulong
*args
)
525 CPUPPCState
*env
= &cpu
->env
;
526 CPUState
*cs
= CPU(cpu
);
528 env
->msr
|= (1ULL << MSR_EE
);
529 hreg_compute_hflags(env
);
530 if (!cpu_has_work(cs
)) {
532 env
->exception_index
= EXCP_HLT
;
533 cs
->exit_request
= 1;
538 static target_ulong
h_rtas(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
539 target_ulong opcode
, target_ulong
*args
)
541 target_ulong rtas_r3
= args
[0];
542 uint32_t token
= rtas_ld(rtas_r3
, 0);
543 uint32_t nargs
= rtas_ld(rtas_r3
, 1);
544 uint32_t nret
= rtas_ld(rtas_r3
, 2);
546 return spapr_rtas_call(cpu
, spapr
, token
, nargs
, rtas_r3
+ 12,
547 nret
, rtas_r3
+ 12 + 4*nargs
);
550 static target_ulong
h_logical_load(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
551 target_ulong opcode
, target_ulong
*args
)
553 CPUState
*cs
= CPU(cpu
);
554 target_ulong size
= args
[0];
555 target_ulong addr
= args
[1];
559 args
[0] = ldub_phys(cs
->as
, addr
);
562 args
[0] = lduw_phys(cs
->as
, addr
);
565 args
[0] = ldl_phys(cs
->as
, addr
);
568 args
[0] = ldq_phys(cs
->as
, addr
);
574 static target_ulong
h_logical_store(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
575 target_ulong opcode
, target_ulong
*args
)
577 CPUState
*cs
= CPU(cpu
);
579 target_ulong size
= args
[0];
580 target_ulong addr
= args
[1];
581 target_ulong val
= args
[2];
585 stb_phys(cs
->as
, addr
, val
);
588 stw_phys(cs
->as
, addr
, val
);
591 stl_phys(cs
->as
, addr
, val
);
594 stq_phys(cs
->as
, addr
, val
);
600 static target_ulong
h_logical_memop(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
601 target_ulong opcode
, target_ulong
*args
)
603 CPUState
*cs
= CPU(cpu
);
605 target_ulong dst
= args
[0]; /* Destination address */
606 target_ulong src
= args
[1]; /* Source address */
607 target_ulong esize
= args
[2]; /* Element size (0=1,1=2,2=4,3=8) */
608 target_ulong count
= args
[3]; /* Element count */
609 target_ulong op
= args
[4]; /* 0 = copy, 1 = invert */
611 unsigned int mask
= (1 << esize
) - 1;
612 int step
= 1 << esize
;
614 if (count
> 0x80000000) {
618 if ((dst
& mask
) || (src
& mask
) || (op
> 1)) {
622 if (dst
>= src
&& dst
< (src
+ (count
<< esize
))) {
623 dst
= dst
+ ((count
- 1) << esize
);
624 src
= src
+ ((count
- 1) << esize
);
631 tmp
= ldub_phys(cs
->as
, src
);
634 tmp
= lduw_phys(cs
->as
, src
);
637 tmp
= ldl_phys(cs
->as
, src
);
640 tmp
= ldq_phys(cs
->as
, src
);
650 stb_phys(cs
->as
, dst
, tmp
);
653 stw_phys(cs
->as
, dst
, tmp
);
656 stl_phys(cs
->as
, dst
, tmp
);
659 stq_phys(cs
->as
, dst
, tmp
);
669 static target_ulong
h_logical_icbi(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
670 target_ulong opcode
, target_ulong
*args
)
672 /* Nothing to do on emulation, KVM will trap this in the kernel */
676 static target_ulong
h_logical_dcbf(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
677 target_ulong opcode
, target_ulong
*args
)
679 /* Nothing to do on emulation, KVM will trap this in the kernel */
683 static target_ulong
h_set_mode(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
684 target_ulong opcode
, target_ulong
*args
)
687 target_ulong mflags
= args
[0];
688 target_ulong resource
= args
[1];
689 target_ulong value1
= args
[2];
690 target_ulong value2
= args
[3];
691 target_ulong ret
= H_P2
;
693 if (resource
== H_SET_MODE_ENDIAN
) {
704 case H_SET_MODE_ENDIAN_BIG
:
706 PowerPCCPU
*cp
= POWERPC_CPU(cs
);
707 CPUPPCState
*env
= &cp
->env
;
708 env
->spr
[SPR_LPCR
] &= ~LPCR_ILE
;
713 case H_SET_MODE_ENDIAN_LITTLE
:
715 PowerPCCPU
*cp
= POWERPC_CPU(cs
);
716 CPUPPCState
*env
= &cp
->env
;
717 env
->spr
[SPR_LPCR
] |= LPCR_ILE
;
723 ret
= H_UNSUPPORTED_FLAG
;
731 static spapr_hcall_fn papr_hypercall_table
[(MAX_HCALL_OPCODE
/ 4) + 1];
732 static spapr_hcall_fn kvmppc_hypercall_table
[KVMPPC_HCALL_MAX
- KVMPPC_HCALL_BASE
+ 1];
734 void spapr_register_hypercall(target_ulong opcode
, spapr_hcall_fn fn
)
736 spapr_hcall_fn
*slot
;
738 if (opcode
<= MAX_HCALL_OPCODE
) {
739 assert((opcode
& 0x3) == 0);
741 slot
= &papr_hypercall_table
[opcode
/ 4];
743 assert((opcode
>= KVMPPC_HCALL_BASE
) && (opcode
<= KVMPPC_HCALL_MAX
));
745 slot
= &kvmppc_hypercall_table
[opcode
- KVMPPC_HCALL_BASE
];
752 target_ulong
spapr_hypercall(PowerPCCPU
*cpu
, target_ulong opcode
,
755 if ((opcode
<= MAX_HCALL_OPCODE
)
756 && ((opcode
& 0x3) == 0)) {
757 spapr_hcall_fn fn
= papr_hypercall_table
[opcode
/ 4];
760 return fn(cpu
, spapr
, opcode
, args
);
762 } else if ((opcode
>= KVMPPC_HCALL_BASE
) &&
763 (opcode
<= KVMPPC_HCALL_MAX
)) {
764 spapr_hcall_fn fn
= kvmppc_hypercall_table
[opcode
- KVMPPC_HCALL_BASE
];
767 return fn(cpu
, spapr
, opcode
, args
);
771 hcall_dprintf("Unimplemented hcall 0x" TARGET_FMT_lx
"\n", opcode
);
775 static void hypercall_register_types(void)
778 spapr_register_hypercall(H_ENTER
, h_enter
);
779 spapr_register_hypercall(H_REMOVE
, h_remove
);
780 spapr_register_hypercall(H_PROTECT
, h_protect
);
781 spapr_register_hypercall(H_READ
, h_read
);
784 spapr_register_hypercall(H_BULK_REMOVE
, h_bulk_remove
);
787 spapr_register_hypercall(H_SET_DABR
, h_set_dabr
);
790 spapr_register_hypercall(H_REGISTER_VPA
, h_register_vpa
);
791 spapr_register_hypercall(H_CEDE
, h_cede
);
793 /* "debugger" hcalls (also used by SLOF). Note: We do -not- differenciate
794 * here between the "CI" and the "CACHE" variants, they will use whatever
795 * mapping attributes qemu is using. When using KVM, the kernel will
796 * enforce the attributes more strongly
798 spapr_register_hypercall(H_LOGICAL_CI_LOAD
, h_logical_load
);
799 spapr_register_hypercall(H_LOGICAL_CI_STORE
, h_logical_store
);
800 spapr_register_hypercall(H_LOGICAL_CACHE_LOAD
, h_logical_load
);
801 spapr_register_hypercall(H_LOGICAL_CACHE_STORE
, h_logical_store
);
802 spapr_register_hypercall(H_LOGICAL_ICBI
, h_logical_icbi
);
803 spapr_register_hypercall(H_LOGICAL_DCBF
, h_logical_dcbf
);
804 spapr_register_hypercall(KVMPPC_H_LOGICAL_MEMOP
, h_logical_memop
);
806 /* qemu/KVM-PPC specific hcalls */
807 spapr_register_hypercall(KVMPPC_H_RTAS
, h_rtas
);
809 spapr_register_hypercall(H_SET_MODE
, h_set_mode
);
812 type_init(hypercall_register_types
)