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 #define HPTE_V_HVLOCK 0x40ULL
44 static inline int lock_hpte(void *hpte
, target_ulong bits
)
50 /* We're protected by qemu's global lock here */
54 stq_p(hpte
, pteh
| HPTE_V_HVLOCK
);
58 static target_ulong
compute_tlbie_rb(target_ulong v
, target_ulong r
,
59 target_ulong pte_index
)
61 target_ulong rb
, va_low
;
63 rb
= (v
& ~0x7fULL
) << 16; /* AVA field */
64 va_low
= pte_index
>> 3;
65 if (v
& HPTE_V_SECONDARY
) {
68 /* xor vsid from AVA */
69 if (!(v
& HPTE_V_1TB_SEG
)) {
75 if (v
& HPTE_V_LARGE
) {
76 rb
|= 1; /* L field */
77 #if 0 /* Disable that P7 specific bit for now */
79 /* non-16MB large page, must be 64k */
80 /* (masks depend on page size) */
81 rb
|= 0x1000; /* page encoding in LP field */
82 rb
|= (va_low
& 0x7f) << 16; /* 7b of VA in AVA/LP field */
83 rb
|= (va_low
& 0xfe); /* AVAL field */
88 rb
|= (va_low
& 0x7ff) << 12; /* remaining 11b of AVA */
90 rb
|= (v
>> 54) & 0x300; /* B field */
94 static target_ulong
h_enter(CPUPPCState
*env
, sPAPREnvironment
*spapr
,
95 target_ulong opcode
, target_ulong
*args
)
97 target_ulong flags
= args
[0];
98 target_ulong pte_index
= args
[1];
99 target_ulong pteh
= args
[2];
100 target_ulong ptel
= args
[3];
101 target_ulong page_shift
= 12;
106 /* only handle 4k and 16M pages for now */
107 if (pteh
& HPTE_V_LARGE
) {
108 #if 0 /* We don't support 64k pages yet */
109 if ((ptel
& 0xf000) == 0x1000) {
113 if ((ptel
& 0xff000) == 0) {
116 /* lowest AVA bit must be 0 for 16M pages */
125 raddr
= (ptel
& HPTE_R_RPN
) & ~((1ULL << page_shift
) - 1);
127 if (raddr
< spapr
->ram_limit
) {
128 /* Regular RAM - should have WIMG=0010 */
129 if ((ptel
& HPTE_R_WIMG
) != HPTE_R_M
) {
133 /* Looks like an IO address */
134 /* FIXME: What WIMG combinations could be sensible for IO?
135 * For now we allow WIMG=010x, but are there others? */
136 /* FIXME: Should we check against registered IO addresses? */
137 if ((ptel
& (HPTE_R_W
| HPTE_R_I
| HPTE_R_M
)) != HPTE_R_I
) {
144 if ((pte_index
* HASH_PTE_SIZE_64
) & ~env
->htab_mask
) {
147 if (likely((flags
& H_EXACT
) == 0)) {
149 hpte
= env
->external_htab
+ (pte_index
* HASH_PTE_SIZE_64
);
154 if (((ldq_p(hpte
) & HPTE_V_VALID
) == 0) &&
155 lock_hpte(hpte
, HPTE_V_HVLOCK
| HPTE_V_VALID
)) {
158 hpte
+= HASH_PTE_SIZE_64
;
162 hpte
= env
->external_htab
+ (pte_index
* HASH_PTE_SIZE_64
);
163 if (!lock_hpte(hpte
, HPTE_V_HVLOCK
| HPTE_V_VALID
)) {
167 stq_p(hpte
+ (HASH_PTE_SIZE_64
/2), ptel
);
168 /* eieio(); FIXME: need some sort of barrier for smp? */
171 assert(!(ldq_p(hpte
) & HPTE_V_HVLOCK
));
172 args
[0] = pte_index
+ i
;
178 REMOVE_NOT_FOUND
= 1,
183 static target_ulong
remove_hpte(CPUPPCState
*env
, target_ulong ptex
,
186 target_ulong
*vp
, target_ulong
*rp
)
189 target_ulong v
, r
, rb
;
191 if ((ptex
* HASH_PTE_SIZE_64
) & ~env
->htab_mask
) {
195 hpte
= env
->external_htab
+ (ptex
* HASH_PTE_SIZE_64
);
196 while (!lock_hpte(hpte
, HPTE_V_HVLOCK
)) {
197 /* We have no real concurrency in qemu soft-emulation, so we
198 * will never actually have a contested lock */
203 r
= ldq_p(hpte
+ (HASH_PTE_SIZE_64
/2));
205 if ((v
& HPTE_V_VALID
) == 0 ||
206 ((flags
& H_AVPN
) && (v
& ~0x7fULL
) != avpn
) ||
207 ((flags
& H_ANDCOND
) && (v
& avpn
) != 0)) {
208 stq_p(hpte
, v
& ~HPTE_V_HVLOCK
);
209 assert(!(ldq_p(hpte
) & HPTE_V_HVLOCK
));
210 return REMOVE_NOT_FOUND
;
212 *vp
= v
& ~HPTE_V_HVLOCK
;
215 rb
= compute_tlbie_rb(v
, r
, ptex
);
216 ppc_tlb_invalidate_one(env
, rb
);
217 assert(!(ldq_p(hpte
) & HPTE_V_HVLOCK
));
218 return REMOVE_SUCCESS
;
221 static target_ulong
h_remove(CPUPPCState
*env
, sPAPREnvironment
*spapr
,
222 target_ulong opcode
, target_ulong
*args
)
224 target_ulong flags
= args
[0];
225 target_ulong pte_index
= args
[1];
226 target_ulong avpn
= args
[2];
229 ret
= remove_hpte(env
, pte_index
, avpn
, flags
,
236 case REMOVE_NOT_FOUND
:
249 #define H_BULK_REMOVE_TYPE 0xc000000000000000ULL
250 #define H_BULK_REMOVE_REQUEST 0x4000000000000000ULL
251 #define H_BULK_REMOVE_RESPONSE 0x8000000000000000ULL
252 #define H_BULK_REMOVE_END 0xc000000000000000ULL
253 #define H_BULK_REMOVE_CODE 0x3000000000000000ULL
254 #define H_BULK_REMOVE_SUCCESS 0x0000000000000000ULL
255 #define H_BULK_REMOVE_NOT_FOUND 0x1000000000000000ULL
256 #define H_BULK_REMOVE_PARM 0x2000000000000000ULL
257 #define H_BULK_REMOVE_HW 0x3000000000000000ULL
258 #define H_BULK_REMOVE_RC 0x0c00000000000000ULL
259 #define H_BULK_REMOVE_FLAGS 0x0300000000000000ULL
260 #define H_BULK_REMOVE_ABSOLUTE 0x0000000000000000ULL
261 #define H_BULK_REMOVE_ANDCOND 0x0100000000000000ULL
262 #define H_BULK_REMOVE_AVPN 0x0200000000000000ULL
263 #define H_BULK_REMOVE_PTEX 0x00ffffffffffffffULL
265 #define H_BULK_REMOVE_MAX_BATCH 4
267 static target_ulong
h_bulk_remove(CPUPPCState
*env
, sPAPREnvironment
*spapr
,
268 target_ulong opcode
, target_ulong
*args
)
272 for (i
= 0; i
< H_BULK_REMOVE_MAX_BATCH
; i
++) {
273 target_ulong
*tsh
= &args
[i
*2];
274 target_ulong tsl
= args
[i
*2 + 1];
275 target_ulong v
, r
, ret
;
277 if ((*tsh
& H_BULK_REMOVE_TYPE
) == H_BULK_REMOVE_END
) {
279 } else if ((*tsh
& H_BULK_REMOVE_TYPE
) != H_BULK_REMOVE_REQUEST
) {
283 *tsh
&= H_BULK_REMOVE_PTEX
| H_BULK_REMOVE_FLAGS
;
284 *tsh
|= H_BULK_REMOVE_RESPONSE
;
286 if ((*tsh
& H_BULK_REMOVE_ANDCOND
) && (*tsh
& H_BULK_REMOVE_AVPN
)) {
287 *tsh
|= H_BULK_REMOVE_PARM
;
291 ret
= remove_hpte(env
, *tsh
& H_BULK_REMOVE_PTEX
, tsl
,
292 (*tsh
& H_BULK_REMOVE_FLAGS
) >> 26,
299 *tsh
|= (r
& (HPTE_R_C
| HPTE_R_R
)) << 43;
313 static target_ulong
h_protect(CPUPPCState
*env
, sPAPREnvironment
*spapr
,
314 target_ulong opcode
, target_ulong
*args
)
316 target_ulong flags
= args
[0];
317 target_ulong pte_index
= args
[1];
318 target_ulong avpn
= args
[2];
320 target_ulong v
, r
, rb
;
322 if ((pte_index
* HASH_PTE_SIZE_64
) & ~env
->htab_mask
) {
326 hpte
= env
->external_htab
+ (pte_index
* HASH_PTE_SIZE_64
);
327 while (!lock_hpte(hpte
, HPTE_V_HVLOCK
)) {
328 /* We have no real concurrency in qemu soft-emulation, so we
329 * will never actually have a contested lock */
334 r
= ldq_p(hpte
+ (HASH_PTE_SIZE_64
/2));
336 if ((v
& HPTE_V_VALID
) == 0 ||
337 ((flags
& H_AVPN
) && (v
& ~0x7fULL
) != avpn
)) {
338 stq_p(hpte
, v
& ~HPTE_V_HVLOCK
);
339 assert(!(ldq_p(hpte
) & HPTE_V_HVLOCK
));
343 r
&= ~(HPTE_R_PP0
| HPTE_R_PP
| HPTE_R_N
|
344 HPTE_R_KEY_HI
| HPTE_R_KEY_LO
);
345 r
|= (flags
<< 55) & HPTE_R_PP0
;
346 r
|= (flags
<< 48) & HPTE_R_KEY_HI
;
347 r
|= flags
& (HPTE_R_PP
| HPTE_R_N
| HPTE_R_KEY_LO
);
348 rb
= compute_tlbie_rb(v
, r
, pte_index
);
349 stq_p(hpte
, v
& ~HPTE_V_VALID
);
350 ppc_tlb_invalidate_one(env
, rb
);
351 stq_p(hpte
+ (HASH_PTE_SIZE_64
/2), r
);
352 /* Don't need a memory barrier, due to qemu's global lock */
353 stq_p(hpte
, v
& ~HPTE_V_HVLOCK
);
354 assert(!(ldq_p(hpte
) & HPTE_V_HVLOCK
));
358 static target_ulong
h_set_dabr(CPUPPCState
*env
, sPAPREnvironment
*spapr
,
359 target_ulong opcode
, target_ulong
*args
)
361 /* FIXME: actually implement this */
365 #define FLAGS_REGISTER_VPA 0x0000200000000000ULL
366 #define FLAGS_REGISTER_DTL 0x0000400000000000ULL
367 #define FLAGS_REGISTER_SLBSHADOW 0x0000600000000000ULL
368 #define FLAGS_DEREGISTER_VPA 0x0000a00000000000ULL
369 #define FLAGS_DEREGISTER_DTL 0x0000c00000000000ULL
370 #define FLAGS_DEREGISTER_SLBSHADOW 0x0000e00000000000ULL
372 #define VPA_MIN_SIZE 640
373 #define VPA_SIZE_OFFSET 0x4
374 #define VPA_SHARED_PROC_OFFSET 0x9
375 #define VPA_SHARED_PROC_VAL 0x2
377 static target_ulong
register_vpa(CPUPPCState
*env
, target_ulong vpa
)
383 hcall_dprintf("Can't cope with registering a VPA at logical 0\n");
387 if (vpa
% env
->dcache_line_size
) {
390 /* FIXME: bounds check the address */
392 size
= lduw_be_phys(vpa
+ 0x4);
394 if (size
< VPA_MIN_SIZE
) {
398 /* VPA is not allowed to cross a page boundary */
399 if ((vpa
/ 4096) != ((vpa
+ size
- 1) / 4096)) {
405 tmp
= ldub_phys(env
->vpa
+ VPA_SHARED_PROC_OFFSET
);
406 tmp
|= VPA_SHARED_PROC_VAL
;
407 stb_phys(env
->vpa
+ VPA_SHARED_PROC_OFFSET
, tmp
);
412 static target_ulong
deregister_vpa(CPUPPCState
*env
, target_ulong vpa
)
414 if (env
->slb_shadow
) {
418 if (env
->dispatch_trace_log
) {
426 static target_ulong
register_slb_shadow(CPUPPCState
*env
, target_ulong addr
)
431 hcall_dprintf("Can't cope with SLB shadow at logical 0\n");
435 size
= ldl_be_phys(addr
+ 0x4);
440 if ((addr
/ 4096) != ((addr
+ size
- 1) / 4096)) {
448 env
->slb_shadow
= addr
;
453 static target_ulong
deregister_slb_shadow(CPUPPCState
*env
, target_ulong addr
)
459 static target_ulong
register_dtl(CPUPPCState
*env
, target_ulong addr
)
464 hcall_dprintf("Can't cope with DTL at logical 0\n");
468 size
= ldl_be_phys(addr
+ 0x4);
478 env
->dispatch_trace_log
= addr
;
479 env
->dtl_size
= size
;
484 static target_ulong
deregister_dtl(CPUPPCState
*env
, target_ulong addr
)
486 env
->dispatch_trace_log
= 0;
492 static target_ulong
h_register_vpa(CPUPPCState
*env
, sPAPREnvironment
*spapr
,
493 target_ulong opcode
, target_ulong
*args
)
495 target_ulong flags
= args
[0];
496 target_ulong procno
= args
[1];
497 target_ulong vpa
= args
[2];
498 target_ulong ret
= H_PARAMETER
;
501 for (tenv
= first_cpu
; tenv
; tenv
= tenv
->next_cpu
) {
502 if (tenv
->cpu_index
== procno
) {
512 case FLAGS_REGISTER_VPA
:
513 ret
= register_vpa(tenv
, vpa
);
516 case FLAGS_DEREGISTER_VPA
:
517 ret
= deregister_vpa(tenv
, vpa
);
520 case FLAGS_REGISTER_SLBSHADOW
:
521 ret
= register_slb_shadow(tenv
, vpa
);
524 case FLAGS_DEREGISTER_SLBSHADOW
:
525 ret
= deregister_slb_shadow(tenv
, vpa
);
528 case FLAGS_REGISTER_DTL
:
529 ret
= register_dtl(tenv
, vpa
);
532 case FLAGS_DEREGISTER_DTL
:
533 ret
= deregister_dtl(tenv
, vpa
);
540 static target_ulong
h_cede(CPUPPCState
*env
, sPAPREnvironment
*spapr
,
541 target_ulong opcode
, target_ulong
*args
)
543 env
->msr
|= (1ULL << MSR_EE
);
544 hreg_compute_hflags(env
);
545 if (!cpu_has_work(env
)) {
551 static target_ulong
h_rtas(CPUPPCState
*env
, sPAPREnvironment
*spapr
,
552 target_ulong opcode
, target_ulong
*args
)
554 target_ulong rtas_r3
= args
[0];
555 uint32_t token
= ldl_be_phys(rtas_r3
);
556 uint32_t nargs
= ldl_be_phys(rtas_r3
+ 4);
557 uint32_t nret
= ldl_be_phys(rtas_r3
+ 8);
559 return spapr_rtas_call(spapr
, token
, nargs
, rtas_r3
+ 12,
560 nret
, rtas_r3
+ 12 + 4*nargs
);
563 static target_ulong
h_logical_load(CPUPPCState
*env
, sPAPREnvironment
*spapr
,
564 target_ulong opcode
, target_ulong
*args
)
566 target_ulong size
= args
[0];
567 target_ulong addr
= args
[1];
571 args
[0] = ldub_phys(addr
);
574 args
[0] = lduw_phys(addr
);
577 args
[0] = ldl_phys(addr
);
580 args
[0] = ldq_phys(addr
);
586 static target_ulong
h_logical_store(CPUPPCState
*env
, sPAPREnvironment
*spapr
,
587 target_ulong opcode
, target_ulong
*args
)
589 target_ulong size
= args
[0];
590 target_ulong addr
= args
[1];
591 target_ulong val
= args
[2];
610 static target_ulong
h_logical_memop(CPUPPCState
*env
, sPAPREnvironment
*spapr
,
611 target_ulong opcode
, target_ulong
*args
)
613 target_ulong dst
= args
[0]; /* Destination address */
614 target_ulong src
= args
[1]; /* Source address */
615 target_ulong esize
= args
[2]; /* Element size (0=1,1=2,2=4,3=8) */
616 target_ulong count
= args
[3]; /* Element count */
617 target_ulong op
= args
[4]; /* 0 = copy, 1 = invert */
619 unsigned int mask
= (1 << esize
) - 1;
620 int step
= 1 << esize
;
622 if (count
> 0x80000000) {
626 if ((dst
& mask
) || (src
& mask
) || (op
> 1)) {
630 if (dst
>= src
&& dst
< (src
+ (count
<< esize
))) {
631 dst
= dst
+ ((count
- 1) << esize
);
632 src
= src
+ ((count
- 1) << esize
);
639 tmp
= ldub_phys(src
);
642 tmp
= lduw_phys(src
);
677 static target_ulong
h_logical_icbi(CPUPPCState
*env
, sPAPREnvironment
*spapr
,
678 target_ulong opcode
, target_ulong
*args
)
680 /* Nothing to do on emulation, KVM will trap this in the kernel */
684 static target_ulong
h_logical_dcbf(CPUPPCState
*env
, sPAPREnvironment
*spapr
,
685 target_ulong opcode
, target_ulong
*args
)
687 /* Nothing to do on emulation, KVM will trap this in the kernel */
691 static spapr_hcall_fn papr_hypercall_table
[(MAX_HCALL_OPCODE
/ 4) + 1];
692 static spapr_hcall_fn kvmppc_hypercall_table
[KVMPPC_HCALL_MAX
- KVMPPC_HCALL_BASE
+ 1];
694 void spapr_register_hypercall(target_ulong opcode
, spapr_hcall_fn fn
)
696 spapr_hcall_fn
*slot
;
698 if (opcode
<= MAX_HCALL_OPCODE
) {
699 assert((opcode
& 0x3) == 0);
701 slot
= &papr_hypercall_table
[opcode
/ 4];
703 assert((opcode
>= KVMPPC_HCALL_BASE
) && (opcode
<= KVMPPC_HCALL_MAX
));
706 slot
= &kvmppc_hypercall_table
[opcode
- KVMPPC_HCALL_BASE
];
709 assert(!(*slot
) || (fn
== *slot
));
713 target_ulong
spapr_hypercall(CPUPPCState
*env
, target_ulong opcode
,
717 hcall_dprintf("Hypercall made with MSR[PR]=1\n");
721 if ((opcode
<= MAX_HCALL_OPCODE
)
722 && ((opcode
& 0x3) == 0)) {
723 spapr_hcall_fn fn
= papr_hypercall_table
[opcode
/ 4];
726 return fn(env
, spapr
, opcode
, args
);
728 } else if ((opcode
>= KVMPPC_HCALL_BASE
) &&
729 (opcode
<= KVMPPC_HCALL_MAX
)) {
730 spapr_hcall_fn fn
= kvmppc_hypercall_table
[opcode
- KVMPPC_HCALL_BASE
];
733 return fn(env
, spapr
, opcode
, args
);
737 hcall_dprintf("Unimplemented hcall 0x" TARGET_FMT_lx
"\n", opcode
);
741 static void hypercall_register_types(void)
744 spapr_register_hypercall(H_ENTER
, h_enter
);
745 spapr_register_hypercall(H_REMOVE
, h_remove
);
746 spapr_register_hypercall(H_PROTECT
, h_protect
);
749 spapr_register_hypercall(H_BULK_REMOVE
, h_bulk_remove
);
752 spapr_register_hypercall(H_SET_DABR
, h_set_dabr
);
755 spapr_register_hypercall(H_REGISTER_VPA
, h_register_vpa
);
756 spapr_register_hypercall(H_CEDE
, h_cede
);
758 /* "debugger" hcalls (also used by SLOF). Note: We do -not- differenciate
759 * here between the "CI" and the "CACHE" variants, they will use whatever
760 * mapping attributes qemu is using. When using KVM, the kernel will
761 * enforce the attributes more strongly
763 spapr_register_hypercall(H_LOGICAL_CI_LOAD
, h_logical_load
);
764 spapr_register_hypercall(H_LOGICAL_CI_STORE
, h_logical_store
);
765 spapr_register_hypercall(H_LOGICAL_CACHE_LOAD
, h_logical_load
);
766 spapr_register_hypercall(H_LOGICAL_CACHE_STORE
, h_logical_store
);
767 spapr_register_hypercall(H_LOGICAL_ICBI
, h_logical_icbi
);
768 spapr_register_hypercall(H_LOGICAL_DCBF
, h_logical_dcbf
);
769 spapr_register_hypercall(KVMPPC_H_LOGICAL_MEMOP
, h_logical_memop
);
771 /* qemu/KVM-PPC specific hcalls */
772 spapr_register_hypercall(KVMPPC_H_RTAS
, h_rtas
);
775 type_init(hypercall_register_types
)