8 #include "helper_regs.h"
11 #define HPTES_PER_GROUP 8
13 #define HPTE_V_SSIZE_SHIFT 62
14 #define HPTE_V_AVPN_SHIFT 7
15 #define HPTE_V_AVPN 0x3fffffffffffff80ULL
16 #define HPTE_V_AVPN_VAL(x) (((x) & HPTE_V_AVPN) >> HPTE_V_AVPN_SHIFT)
17 #define HPTE_V_COMPARE(x, y) (!(((x) ^ (y)) & 0xffffffffffffff80UL))
18 #define HPTE_V_BOLTED 0x0000000000000010ULL
19 #define HPTE_V_LOCK 0x0000000000000008ULL
20 #define HPTE_V_LARGE 0x0000000000000004ULL
21 #define HPTE_V_SECONDARY 0x0000000000000002ULL
22 #define HPTE_V_VALID 0x0000000000000001ULL
24 #define HPTE_R_PP0 0x8000000000000000ULL
25 #define HPTE_R_TS 0x4000000000000000ULL
26 #define HPTE_R_KEY_HI 0x3000000000000000ULL
27 #define HPTE_R_RPN_SHIFT 12
28 #define HPTE_R_RPN 0x3ffffffffffff000ULL
29 #define HPTE_R_FLAGS 0x00000000000003ffULL
30 #define HPTE_R_PP 0x0000000000000003ULL
31 #define HPTE_R_N 0x0000000000000004ULL
32 #define HPTE_R_G 0x0000000000000008ULL
33 #define HPTE_R_M 0x0000000000000010ULL
34 #define HPTE_R_I 0x0000000000000020ULL
35 #define HPTE_R_W 0x0000000000000040ULL
36 #define HPTE_R_WIMG 0x0000000000000078ULL
37 #define HPTE_R_C 0x0000000000000080ULL
38 #define HPTE_R_R 0x0000000000000100ULL
39 #define HPTE_R_KEY_LO 0x0000000000000e00ULL
41 #define HPTE_V_1TB_SEG 0x4000000000000000ULL
42 #define HPTE_V_VRMA_MASK 0x4001ffffff000000ULL
44 #define HPTE_V_HVLOCK 0x40ULL
46 static inline int lock_hpte(void *hpte
, target_ulong bits
)
52 /* We're protected by qemu's global lock here */
56 stq_p(hpte
, pteh
| HPTE_V_HVLOCK
);
60 static target_ulong
compute_tlbie_rb(target_ulong v
, target_ulong r
,
61 target_ulong pte_index
)
63 target_ulong rb
, va_low
;
65 rb
= (v
& ~0x7fULL
) << 16; /* AVA field */
66 va_low
= pte_index
>> 3;
67 if (v
& HPTE_V_SECONDARY
) {
70 /* xor vsid from AVA */
71 if (!(v
& HPTE_V_1TB_SEG
)) {
77 if (v
& HPTE_V_LARGE
) {
78 rb
|= 1; /* L field */
79 #if 0 /* Disable that P7 specific bit for now */
81 /* non-16MB large page, must be 64k */
82 /* (masks depend on page size) */
83 rb
|= 0x1000; /* page encoding in LP field */
84 rb
|= (va_low
& 0x7f) << 16; /* 7b of VA in AVA/LP field */
85 rb
|= (va_low
& 0xfe); /* AVAL field */
90 rb
|= (va_low
& 0x7ff) << 12; /* remaining 11b of AVA */
92 rb
|= (v
>> 54) & 0x300; /* B field */
96 static target_ulong
h_enter(CPUState
*env
, sPAPREnvironment
*spapr
,
97 target_ulong opcode
, target_ulong
*args
)
99 target_ulong flags
= args
[0];
100 target_ulong pte_index
= args
[1];
101 target_ulong pteh
= args
[2];
102 target_ulong ptel
= args
[3];
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) {
115 /* lowest AVA bit must be 0 for 16M pages */
124 /* FIXME: bounds check the pa? */
127 if ((ptel
& HPTE_R_WIMG
) != HPTE_R_M
) {
132 if ((pte_index
* HASH_PTE_SIZE_64
) & ~env
->htab_mask
) {
135 if (likely((flags
& H_EXACT
) == 0)) {
137 hpte
= env
->external_htab
+ (pte_index
* HASH_PTE_SIZE_64
);
142 if (((ldq_p(hpte
) & HPTE_V_VALID
) == 0) &&
143 lock_hpte(hpte
, HPTE_V_HVLOCK
| HPTE_V_VALID
)) {
146 hpte
+= HASH_PTE_SIZE_64
;
150 hpte
= env
->external_htab
+ (pte_index
* HASH_PTE_SIZE_64
);
151 if (!lock_hpte(hpte
, HPTE_V_HVLOCK
| HPTE_V_VALID
)) {
155 stq_p(hpte
+ (HASH_PTE_SIZE_64
/2), ptel
);
156 /* eieio(); FIXME: need some sort of barrier for smp? */
159 assert(!(ldq_p(hpte
) & HPTE_V_HVLOCK
));
160 args
[0] = pte_index
+ i
;
164 static target_ulong
h_remove(CPUState
*env
, sPAPREnvironment
*spapr
,
165 target_ulong opcode
, target_ulong
*args
)
167 target_ulong flags
= args
[0];
168 target_ulong pte_index
= args
[1];
169 target_ulong avpn
= args
[2];
171 target_ulong v
, r
, rb
;
173 if ((pte_index
* HASH_PTE_SIZE_64
) & ~env
->htab_mask
) {
177 hpte
= env
->external_htab
+ (pte_index
* HASH_PTE_SIZE_64
);
178 while (!lock_hpte(hpte
, HPTE_V_HVLOCK
)) {
179 /* We have no real concurrency in qemu soft-emulation, so we
180 * will never actually have a contested lock */
185 r
= ldq_p(hpte
+ (HASH_PTE_SIZE_64
/2));
187 if ((v
& HPTE_V_VALID
) == 0 ||
188 ((flags
& H_AVPN
) && (v
& ~0x7fULL
) != avpn
) ||
189 ((flags
& H_ANDCOND
) && (v
& avpn
) != 0)) {
190 stq_p(hpte
, v
& ~HPTE_V_HVLOCK
);
191 assert(!(ldq_p(hpte
) & HPTE_V_HVLOCK
));
194 args
[0] = v
& ~HPTE_V_HVLOCK
;
197 rb
= compute_tlbie_rb(v
, r
, pte_index
);
198 ppc_tlb_invalidate_one(env
, rb
);
199 assert(!(ldq_p(hpte
) & HPTE_V_HVLOCK
));
203 static target_ulong
h_protect(CPUState
*env
, sPAPREnvironment
*spapr
,
204 target_ulong opcode
, target_ulong
*args
)
206 target_ulong flags
= args
[0];
207 target_ulong pte_index
= args
[1];
208 target_ulong avpn
= args
[2];
210 target_ulong v
, r
, rb
;
212 if ((pte_index
* HASH_PTE_SIZE_64
) & ~env
->htab_mask
) {
216 hpte
= env
->external_htab
+ (pte_index
* HASH_PTE_SIZE_64
);
217 while (!lock_hpte(hpte
, HPTE_V_HVLOCK
)) {
218 /* We have no real concurrency in qemu soft-emulation, so we
219 * will never actually have a contested lock */
224 r
= ldq_p(hpte
+ (HASH_PTE_SIZE_64
/2));
226 if ((v
& HPTE_V_VALID
) == 0 ||
227 ((flags
& H_AVPN
) && (v
& ~0x7fULL
) != avpn
)) {
228 stq_p(hpte
, v
& ~HPTE_V_HVLOCK
);
229 assert(!(ldq_p(hpte
) & HPTE_V_HVLOCK
));
233 r
&= ~(HPTE_R_PP0
| HPTE_R_PP
| HPTE_R_N
|
234 HPTE_R_KEY_HI
| HPTE_R_KEY_LO
);
235 r
|= (flags
<< 55) & HPTE_R_PP0
;
236 r
|= (flags
<< 48) & HPTE_R_KEY_HI
;
237 r
|= flags
& (HPTE_R_PP
| HPTE_R_N
| HPTE_R_KEY_LO
);
238 rb
= compute_tlbie_rb(v
, r
, pte_index
);
239 stq_p(hpte
, v
& ~HPTE_V_VALID
);
240 ppc_tlb_invalidate_one(env
, rb
);
241 stq_p(hpte
+ (HASH_PTE_SIZE_64
/2), r
);
242 /* Don't need a memory barrier, due to qemu's global lock */
243 stq_p(hpte
, v
& ~HPTE_V_HVLOCK
);
244 assert(!(ldq_p(hpte
) & HPTE_V_HVLOCK
));
248 static target_ulong
h_set_dabr(CPUState
*env
, sPAPREnvironment
*spapr
,
249 target_ulong opcode
, target_ulong
*args
)
251 /* FIXME: actually implement this */
255 #define FLAGS_REGISTER_VPA 0x0000200000000000ULL
256 #define FLAGS_REGISTER_DTL 0x0000400000000000ULL
257 #define FLAGS_REGISTER_SLBSHADOW 0x0000600000000000ULL
258 #define FLAGS_DEREGISTER_VPA 0x0000a00000000000ULL
259 #define FLAGS_DEREGISTER_DTL 0x0000c00000000000ULL
260 #define FLAGS_DEREGISTER_SLBSHADOW 0x0000e00000000000ULL
262 #define VPA_MIN_SIZE 640
263 #define VPA_SIZE_OFFSET 0x4
264 #define VPA_SHARED_PROC_OFFSET 0x9
265 #define VPA_SHARED_PROC_VAL 0x2
267 static target_ulong
register_vpa(CPUState
*env
, target_ulong vpa
)
273 hcall_dprintf("Can't cope with registering a VPA at logical 0\n");
277 if (vpa
% env
->dcache_line_size
) {
280 /* FIXME: bounds check the address */
282 size
= lduw_phys(vpa
+ 0x4);
284 if (size
< VPA_MIN_SIZE
) {
288 /* VPA is not allowed to cross a page boundary */
289 if ((vpa
/ 4096) != ((vpa
+ size
- 1) / 4096)) {
295 tmp
= ldub_phys(env
->vpa
+ VPA_SHARED_PROC_OFFSET
);
296 tmp
|= VPA_SHARED_PROC_VAL
;
297 stb_phys(env
->vpa
+ VPA_SHARED_PROC_OFFSET
, tmp
);
302 static target_ulong
deregister_vpa(CPUState
*env
, target_ulong vpa
)
304 if (env
->slb_shadow
) {
308 if (env
->dispatch_trace_log
) {
316 static target_ulong
register_slb_shadow(CPUState
*env
, target_ulong addr
)
321 hcall_dprintf("Can't cope with SLB shadow at logical 0\n");
325 size
= ldl_phys(addr
+ 0x4);
330 if ((addr
/ 4096) != ((addr
+ size
- 1) / 4096)) {
338 env
->slb_shadow
= addr
;
343 static target_ulong
deregister_slb_shadow(CPUState
*env
, target_ulong addr
)
349 static target_ulong
register_dtl(CPUState
*env
, target_ulong addr
)
354 hcall_dprintf("Can't cope with DTL at logical 0\n");
358 size
= ldl_phys(addr
+ 0x4);
368 env
->dispatch_trace_log
= addr
;
369 env
->dtl_size
= size
;
374 static target_ulong
deregister_dtl(CPUState
*emv
, target_ulong addr
)
376 env
->dispatch_trace_log
= 0;
382 static target_ulong
h_register_vpa(CPUState
*env
, sPAPREnvironment
*spapr
,
383 target_ulong opcode
, target_ulong
*args
)
385 target_ulong flags
= args
[0];
386 target_ulong procno
= args
[1];
387 target_ulong vpa
= args
[2];
388 target_ulong ret
= H_PARAMETER
;
391 for (tenv
= first_cpu
; tenv
; tenv
= tenv
->next_cpu
) {
392 if (tenv
->cpu_index
== procno
) {
402 case FLAGS_REGISTER_VPA
:
403 ret
= register_vpa(tenv
, vpa
);
406 case FLAGS_DEREGISTER_VPA
:
407 ret
= deregister_vpa(tenv
, vpa
);
410 case FLAGS_REGISTER_SLBSHADOW
:
411 ret
= register_slb_shadow(tenv
, vpa
);
414 case FLAGS_DEREGISTER_SLBSHADOW
:
415 ret
= deregister_slb_shadow(tenv
, vpa
);
418 case FLAGS_REGISTER_DTL
:
419 ret
= register_dtl(tenv
, vpa
);
422 case FLAGS_DEREGISTER_DTL
:
423 ret
= deregister_dtl(tenv
, vpa
);
430 static target_ulong
h_cede(CPUState
*env
, sPAPREnvironment
*spapr
,
431 target_ulong opcode
, target_ulong
*args
)
433 env
->msr
|= (1ULL << MSR_EE
);
434 hreg_compute_hflags(env
);
435 if (!cpu_has_work(env
)) {
441 static target_ulong
h_rtas(CPUState
*env
, sPAPREnvironment
*spapr
,
442 target_ulong opcode
, target_ulong
*args
)
444 target_ulong rtas_r3
= args
[0];
445 uint32_t token
= ldl_phys(rtas_r3
);
446 uint32_t nargs
= ldl_phys(rtas_r3
+ 4);
447 uint32_t nret
= ldl_phys(rtas_r3
+ 8);
449 return spapr_rtas_call(spapr
, token
, nargs
, rtas_r3
+ 12,
450 nret
, rtas_r3
+ 12 + 4*nargs
);
453 static spapr_hcall_fn papr_hypercall_table
[(MAX_HCALL_OPCODE
/ 4) + 1];
454 static spapr_hcall_fn kvmppc_hypercall_table
[KVMPPC_HCALL_MAX
- KVMPPC_HCALL_BASE
+ 1];
456 void spapr_register_hypercall(target_ulong opcode
, spapr_hcall_fn fn
)
458 spapr_hcall_fn
*slot
;
460 if (opcode
<= MAX_HCALL_OPCODE
) {
461 assert((opcode
& 0x3) == 0);
463 slot
= &papr_hypercall_table
[opcode
/ 4];
465 assert((opcode
>= KVMPPC_HCALL_BASE
) && (opcode
<= KVMPPC_HCALL_MAX
));
468 slot
= &kvmppc_hypercall_table
[opcode
- KVMPPC_HCALL_BASE
];
471 assert(!(*slot
) || (fn
== *slot
));
475 target_ulong
spapr_hypercall(CPUState
*env
, target_ulong opcode
,
479 hcall_dprintf("Hypercall made with MSR[PR]=1\n");
483 if ((opcode
<= MAX_HCALL_OPCODE
)
484 && ((opcode
& 0x3) == 0)) {
485 spapr_hcall_fn fn
= papr_hypercall_table
[opcode
/ 4];
488 return fn(env
, spapr
, opcode
, args
);
490 } else if ((opcode
>= KVMPPC_HCALL_BASE
) &&
491 (opcode
<= KVMPPC_HCALL_MAX
)) {
492 spapr_hcall_fn fn
= kvmppc_hypercall_table
[opcode
- KVMPPC_HCALL_BASE
];
495 return fn(env
, spapr
, opcode
, args
);
499 hcall_dprintf("Unimplemented hcall 0x" TARGET_FMT_lx
"\n", opcode
);
503 static void hypercall_init(void)
506 spapr_register_hypercall(H_ENTER
, h_enter
);
507 spapr_register_hypercall(H_REMOVE
, h_remove
);
508 spapr_register_hypercall(H_PROTECT
, h_protect
);
511 spapr_register_hypercall(H_SET_DABR
, h_set_dabr
);
514 spapr_register_hypercall(H_REGISTER_VPA
, h_register_vpa
);
515 spapr_register_hypercall(H_CEDE
, h_cede
);
517 /* qemu/KVM-PPC specific hcalls */
518 spapr_register_hypercall(KVMPPC_H_RTAS
, h_rtas
);
520 device_init(hypercall_init
);