1 #include "sysemu/sysemu.h"
3 #include "helper_regs.h"
4 #include "hw/ppc/spapr.h"
5 #include "mmu-hash64.h"
14 static void do_spr_sync(void *arg
)
16 struct SPRSyncState
*s
= arg
;
17 PowerPCCPU
*cpu
= POWERPC_CPU(s
->cs
);
18 CPUPPCState
*env
= &cpu
->env
;
20 cpu_synchronize_state(s
->cs
);
21 env
->spr
[s
->spr
] &= ~s
->mask
;
22 env
->spr
[s
->spr
] |= s
->value
;
25 static void set_spr(CPUState
*cs
, int spr
, target_ulong value
,
28 struct SPRSyncState s
= {
34 run_on_cpu(cs
, do_spr_sync
, &s
);
37 static target_ulong
compute_tlbie_rb(target_ulong v
, target_ulong r
,
38 target_ulong pte_index
)
40 target_ulong rb
, va_low
;
42 rb
= (v
& ~0x7fULL
) << 16; /* AVA field */
43 va_low
= pte_index
>> 3;
44 if (v
& HPTE64_V_SECONDARY
) {
47 /* xor vsid from AVA */
48 if (!(v
& HPTE64_V_1TB_SEG
)) {
54 if (v
& HPTE64_V_LARGE
) {
55 rb
|= 1; /* L field */
56 #if 0 /* Disable that P7 specific bit for now */
58 /* non-16MB large page, must be 64k */
59 /* (masks depend on page size) */
60 rb
|= 0x1000; /* page encoding in LP field */
61 rb
|= (va_low
& 0x7f) << 16; /* 7b of VA in AVA/LP field */
62 rb
|= (va_low
& 0xfe); /* AVAL field */
67 rb
|= (va_low
& 0x7ff) << 12; /* remaining 11b of AVA */
69 rb
|= (v
>> 54) & 0x300; /* B field */
73 static inline bool valid_pte_index(CPUPPCState
*env
, target_ulong pte_index
)
76 * hash value/pteg group index is normalized by htab_mask
78 if (((pte_index
& ~7ULL) / HPTES_PER_GROUP
) & ~env
->htab_mask
) {
84 static target_ulong
h_enter(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
85 target_ulong opcode
, target_ulong
*args
)
87 CPUPPCState
*env
= &cpu
->env
;
88 target_ulong flags
= args
[0];
89 target_ulong pte_index
= args
[1];
90 target_ulong pteh
= args
[2];
91 target_ulong ptel
= args
[3];
92 target_ulong page_shift
= 12;
97 /* only handle 4k and 16M pages for now */
98 if (pteh
& HPTE64_V_LARGE
) {
99 #if 0 /* We don't support 64k pages yet */
100 if ((ptel
& 0xf000) == 0x1000) {
104 if ((ptel
& 0xff000) == 0) {
107 /* lowest AVA bit must be 0 for 16M pages */
116 raddr
= (ptel
& HPTE64_R_RPN
) & ~((1ULL << page_shift
) - 1);
118 if (raddr
< spapr
->ram_limit
) {
119 /* Regular RAM - should have WIMG=0010 */
120 if ((ptel
& HPTE64_R_WIMG
) != HPTE64_R_M
) {
124 /* Looks like an IO address */
125 /* FIXME: What WIMG combinations could be sensible for IO?
126 * For now we allow WIMG=010x, but are there others? */
127 /* FIXME: Should we check against registered IO addresses? */
128 if ((ptel
& (HPTE64_R_W
| HPTE64_R_I
| HPTE64_R_M
)) != HPTE64_R_I
) {
135 if (!valid_pte_index(env
, pte_index
)) {
140 if (likely((flags
& H_EXACT
) == 0)) {
142 token
= ppc_hash64_start_access(cpu
, pte_index
);
143 for (; index
< 8; index
++) {
144 if ((ppc_hash64_load_hpte0(env
, token
, index
) & HPTE64_V_VALID
) == 0) {
148 ppc_hash64_stop_access(token
);
153 token
= ppc_hash64_start_access(cpu
, pte_index
);
154 if (ppc_hash64_load_hpte0(env
, token
, 0) & HPTE64_V_VALID
) {
155 ppc_hash64_stop_access(token
);
158 ppc_hash64_stop_access(token
);
161 ppc_hash64_store_hpte(env
, pte_index
+ index
,
162 pteh
| HPTE64_V_HPTE_DIRTY
, ptel
);
164 args
[0] = pte_index
+ index
;
170 REMOVE_NOT_FOUND
= 1,
175 static RemoveResult
remove_hpte(CPUPPCState
*env
, target_ulong ptex
,
178 target_ulong
*vp
, target_ulong
*rp
)
181 target_ulong v
, r
, rb
;
183 if (!valid_pte_index(env
, ptex
)) {
187 token
= ppc_hash64_start_access(ppc_env_get_cpu(env
), ptex
);
188 v
= ppc_hash64_load_hpte0(env
, token
, 0);
189 r
= ppc_hash64_load_hpte1(env
, token
, 0);
190 ppc_hash64_stop_access(token
);
192 if ((v
& HPTE64_V_VALID
) == 0 ||
193 ((flags
& H_AVPN
) && (v
& ~0x7fULL
) != avpn
) ||
194 ((flags
& H_ANDCOND
) && (v
& avpn
) != 0)) {
195 return REMOVE_NOT_FOUND
;
199 ppc_hash64_store_hpte(env
, ptex
, HPTE64_V_HPTE_DIRTY
, 0);
200 rb
= compute_tlbie_rb(v
, r
, ptex
);
201 ppc_tlb_invalidate_one(env
, rb
);
202 return REMOVE_SUCCESS
;
205 static target_ulong
h_remove(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
206 target_ulong opcode
, target_ulong
*args
)
208 CPUPPCState
*env
= &cpu
->env
;
209 target_ulong flags
= args
[0];
210 target_ulong pte_index
= args
[1];
211 target_ulong avpn
= args
[2];
214 ret
= remove_hpte(env
, pte_index
, avpn
, flags
,
221 case REMOVE_NOT_FOUND
:
231 g_assert_not_reached();
234 #define H_BULK_REMOVE_TYPE 0xc000000000000000ULL
235 #define H_BULK_REMOVE_REQUEST 0x4000000000000000ULL
236 #define H_BULK_REMOVE_RESPONSE 0x8000000000000000ULL
237 #define H_BULK_REMOVE_END 0xc000000000000000ULL
238 #define H_BULK_REMOVE_CODE 0x3000000000000000ULL
239 #define H_BULK_REMOVE_SUCCESS 0x0000000000000000ULL
240 #define H_BULK_REMOVE_NOT_FOUND 0x1000000000000000ULL
241 #define H_BULK_REMOVE_PARM 0x2000000000000000ULL
242 #define H_BULK_REMOVE_HW 0x3000000000000000ULL
243 #define H_BULK_REMOVE_RC 0x0c00000000000000ULL
244 #define H_BULK_REMOVE_FLAGS 0x0300000000000000ULL
245 #define H_BULK_REMOVE_ABSOLUTE 0x0000000000000000ULL
246 #define H_BULK_REMOVE_ANDCOND 0x0100000000000000ULL
247 #define H_BULK_REMOVE_AVPN 0x0200000000000000ULL
248 #define H_BULK_REMOVE_PTEX 0x00ffffffffffffffULL
250 #define H_BULK_REMOVE_MAX_BATCH 4
252 static target_ulong
h_bulk_remove(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
253 target_ulong opcode
, target_ulong
*args
)
255 CPUPPCState
*env
= &cpu
->env
;
258 for (i
= 0; i
< H_BULK_REMOVE_MAX_BATCH
; i
++) {
259 target_ulong
*tsh
= &args
[i
*2];
260 target_ulong tsl
= args
[i
*2 + 1];
261 target_ulong v
, r
, ret
;
263 if ((*tsh
& H_BULK_REMOVE_TYPE
) == H_BULK_REMOVE_END
) {
265 } else if ((*tsh
& H_BULK_REMOVE_TYPE
) != H_BULK_REMOVE_REQUEST
) {
269 *tsh
&= H_BULK_REMOVE_PTEX
| H_BULK_REMOVE_FLAGS
;
270 *tsh
|= H_BULK_REMOVE_RESPONSE
;
272 if ((*tsh
& H_BULK_REMOVE_ANDCOND
) && (*tsh
& H_BULK_REMOVE_AVPN
)) {
273 *tsh
|= H_BULK_REMOVE_PARM
;
277 ret
= remove_hpte(env
, *tsh
& H_BULK_REMOVE_PTEX
, tsl
,
278 (*tsh
& H_BULK_REMOVE_FLAGS
) >> 26,
285 *tsh
|= (r
& (HPTE64_R_C
| HPTE64_R_R
)) << 43;
299 static target_ulong
h_protect(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
300 target_ulong opcode
, target_ulong
*args
)
302 CPUPPCState
*env
= &cpu
->env
;
303 target_ulong flags
= args
[0];
304 target_ulong pte_index
= args
[1];
305 target_ulong avpn
= args
[2];
307 target_ulong v
, r
, rb
;
309 if (!valid_pte_index(env
, pte_index
)) {
313 token
= ppc_hash64_start_access(cpu
, pte_index
);
314 v
= ppc_hash64_load_hpte0(env
, token
, 0);
315 r
= ppc_hash64_load_hpte1(env
, token
, 0);
316 ppc_hash64_stop_access(token
);
318 if ((v
& HPTE64_V_VALID
) == 0 ||
319 ((flags
& H_AVPN
) && (v
& ~0x7fULL
) != avpn
)) {
323 r
&= ~(HPTE64_R_PP0
| HPTE64_R_PP
| HPTE64_R_N
|
324 HPTE64_R_KEY_HI
| HPTE64_R_KEY_LO
);
325 r
|= (flags
<< 55) & HPTE64_R_PP0
;
326 r
|= (flags
<< 48) & HPTE64_R_KEY_HI
;
327 r
|= flags
& (HPTE64_R_PP
| HPTE64_R_N
| HPTE64_R_KEY_LO
);
328 rb
= compute_tlbie_rb(v
, r
, pte_index
);
329 ppc_hash64_store_hpte(env
, pte_index
,
330 (v
& ~HPTE64_V_VALID
) | HPTE64_V_HPTE_DIRTY
, 0);
331 ppc_tlb_invalidate_one(env
, rb
);
332 /* Don't need a memory barrier, due to qemu's global lock */
333 ppc_hash64_store_hpte(env
, pte_index
, v
| HPTE64_V_HPTE_DIRTY
, r
);
337 static target_ulong
h_read(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
338 target_ulong opcode
, target_ulong
*args
)
340 CPUPPCState
*env
= &cpu
->env
;
341 target_ulong flags
= args
[0];
342 target_ulong pte_index
= args
[1];
344 int i
, ridx
, n_entries
= 1;
346 if (!valid_pte_index(env
, pte_index
)) {
350 if (flags
& H_READ_4
) {
351 /* Clear the two low order bits */
352 pte_index
&= ~(3ULL);
356 hpte
= env
->external_htab
+ (pte_index
* HASH_PTE_SIZE_64
);
358 for (i
= 0, ridx
= 0; i
< n_entries
; i
++) {
359 args
[ridx
++] = ldq_p(hpte
);
360 args
[ridx
++] = ldq_p(hpte
+ (HASH_PTE_SIZE_64
/2));
361 hpte
+= HASH_PTE_SIZE_64
;
367 static target_ulong
h_set_dabr(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
368 target_ulong opcode
, target_ulong
*args
)
370 /* FIXME: actually implement this */
374 #define FLAGS_REGISTER_VPA 0x0000200000000000ULL
375 #define FLAGS_REGISTER_DTL 0x0000400000000000ULL
376 #define FLAGS_REGISTER_SLBSHADOW 0x0000600000000000ULL
377 #define FLAGS_DEREGISTER_VPA 0x0000a00000000000ULL
378 #define FLAGS_DEREGISTER_DTL 0x0000c00000000000ULL
379 #define FLAGS_DEREGISTER_SLBSHADOW 0x0000e00000000000ULL
381 #define VPA_MIN_SIZE 640
382 #define VPA_SIZE_OFFSET 0x4
383 #define VPA_SHARED_PROC_OFFSET 0x9
384 #define VPA_SHARED_PROC_VAL 0x2
386 static target_ulong
register_vpa(CPUPPCState
*env
, target_ulong vpa
)
388 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
393 hcall_dprintf("Can't cope with registering a VPA at logical 0\n");
397 if (vpa
% env
->dcache_line_size
) {
400 /* FIXME: bounds check the address */
402 size
= lduw_be_phys(cs
->as
, vpa
+ 0x4);
404 if (size
< VPA_MIN_SIZE
) {
408 /* VPA is not allowed to cross a page boundary */
409 if ((vpa
/ 4096) != ((vpa
+ size
- 1) / 4096)) {
415 tmp
= ldub_phys(cs
->as
, env
->vpa_addr
+ VPA_SHARED_PROC_OFFSET
);
416 tmp
|= VPA_SHARED_PROC_VAL
;
417 stb_phys(cs
->as
, env
->vpa_addr
+ VPA_SHARED_PROC_OFFSET
, tmp
);
422 static target_ulong
deregister_vpa(CPUPPCState
*env
, target_ulong vpa
)
424 if (env
->slb_shadow_addr
) {
436 static target_ulong
register_slb_shadow(CPUPPCState
*env
, target_ulong addr
)
438 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
442 hcall_dprintf("Can't cope with SLB shadow at logical 0\n");
446 size
= ldl_be_phys(cs
->as
, addr
+ 0x4);
451 if ((addr
/ 4096) != ((addr
+ size
- 1) / 4096)) {
455 if (!env
->vpa_addr
) {
459 env
->slb_shadow_addr
= addr
;
460 env
->slb_shadow_size
= size
;
465 static target_ulong
deregister_slb_shadow(CPUPPCState
*env
, target_ulong addr
)
467 env
->slb_shadow_addr
= 0;
468 env
->slb_shadow_size
= 0;
472 static target_ulong
register_dtl(CPUPPCState
*env
, target_ulong addr
)
474 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
478 hcall_dprintf("Can't cope with DTL at logical 0\n");
482 size
= ldl_be_phys(cs
->as
, addr
+ 0x4);
488 if (!env
->vpa_addr
) {
492 env
->dtl_addr
= addr
;
493 env
->dtl_size
= size
;
498 static target_ulong
deregister_dtl(CPUPPCState
*env
, target_ulong addr
)
506 static target_ulong
h_register_vpa(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
507 target_ulong opcode
, target_ulong
*args
)
509 target_ulong flags
= args
[0];
510 target_ulong procno
= args
[1];
511 target_ulong vpa
= args
[2];
512 target_ulong ret
= H_PARAMETER
;
516 tcpu
= ppc_get_vcpu_by_dt_id(procno
);
523 case FLAGS_REGISTER_VPA
:
524 ret
= register_vpa(tenv
, vpa
);
527 case FLAGS_DEREGISTER_VPA
:
528 ret
= deregister_vpa(tenv
, vpa
);
531 case FLAGS_REGISTER_SLBSHADOW
:
532 ret
= register_slb_shadow(tenv
, vpa
);
535 case FLAGS_DEREGISTER_SLBSHADOW
:
536 ret
= deregister_slb_shadow(tenv
, vpa
);
539 case FLAGS_REGISTER_DTL
:
540 ret
= register_dtl(tenv
, vpa
);
543 case FLAGS_DEREGISTER_DTL
:
544 ret
= deregister_dtl(tenv
, vpa
);
551 static target_ulong
h_cede(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
552 target_ulong opcode
, target_ulong
*args
)
554 CPUPPCState
*env
= &cpu
->env
;
555 CPUState
*cs
= CPU(cpu
);
557 env
->msr
|= (1ULL << MSR_EE
);
558 hreg_compute_hflags(env
);
559 if (!cpu_has_work(cs
)) {
561 cs
->exception_index
= EXCP_HLT
;
562 cs
->exit_request
= 1;
567 static target_ulong
h_rtas(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
568 target_ulong opcode
, target_ulong
*args
)
570 target_ulong rtas_r3
= args
[0];
571 uint32_t token
= rtas_ld(rtas_r3
, 0);
572 uint32_t nargs
= rtas_ld(rtas_r3
, 1);
573 uint32_t nret
= rtas_ld(rtas_r3
, 2);
575 return spapr_rtas_call(cpu
, spapr
, token
, nargs
, rtas_r3
+ 12,
576 nret
, rtas_r3
+ 12 + 4*nargs
);
579 static target_ulong
h_logical_load(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
580 target_ulong opcode
, target_ulong
*args
)
582 CPUState
*cs
= CPU(cpu
);
583 target_ulong size
= args
[0];
584 target_ulong addr
= args
[1];
588 args
[0] = ldub_phys(cs
->as
, addr
);
591 args
[0] = lduw_phys(cs
->as
, addr
);
594 args
[0] = ldl_phys(cs
->as
, addr
);
597 args
[0] = ldq_phys(cs
->as
, addr
);
603 static target_ulong
h_logical_store(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
604 target_ulong opcode
, target_ulong
*args
)
606 CPUState
*cs
= CPU(cpu
);
608 target_ulong size
= args
[0];
609 target_ulong addr
= args
[1];
610 target_ulong val
= args
[2];
614 stb_phys(cs
->as
, addr
, val
);
617 stw_phys(cs
->as
, addr
, val
);
620 stl_phys(cs
->as
, addr
, val
);
623 stq_phys(cs
->as
, addr
, val
);
629 static target_ulong
h_logical_memop(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
630 target_ulong opcode
, target_ulong
*args
)
632 CPUState
*cs
= CPU(cpu
);
634 target_ulong dst
= args
[0]; /* Destination address */
635 target_ulong src
= args
[1]; /* Source address */
636 target_ulong esize
= args
[2]; /* Element size (0=1,1=2,2=4,3=8) */
637 target_ulong count
= args
[3]; /* Element count */
638 target_ulong op
= args
[4]; /* 0 = copy, 1 = invert */
640 unsigned int mask
= (1 << esize
) - 1;
641 int step
= 1 << esize
;
643 if (count
> 0x80000000) {
647 if ((dst
& mask
) || (src
& mask
) || (op
> 1)) {
651 if (dst
>= src
&& dst
< (src
+ (count
<< esize
))) {
652 dst
= dst
+ ((count
- 1) << esize
);
653 src
= src
+ ((count
- 1) << esize
);
660 tmp
= ldub_phys(cs
->as
, src
);
663 tmp
= lduw_phys(cs
->as
, src
);
666 tmp
= ldl_phys(cs
->as
, src
);
669 tmp
= ldq_phys(cs
->as
, src
);
679 stb_phys(cs
->as
, dst
, tmp
);
682 stw_phys(cs
->as
, dst
, tmp
);
685 stl_phys(cs
->as
, dst
, tmp
);
688 stq_phys(cs
->as
, dst
, tmp
);
698 static target_ulong
h_logical_icbi(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
699 target_ulong opcode
, target_ulong
*args
)
701 /* Nothing to do on emulation, KVM will trap this in the kernel */
705 static target_ulong
h_logical_dcbf(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
706 target_ulong opcode
, target_ulong
*args
)
708 /* Nothing to do on emulation, KVM will trap this in the kernel */
712 static target_ulong
h_set_mode(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
713 target_ulong opcode
, target_ulong
*args
)
716 target_ulong mflags
= args
[0];
717 target_ulong resource
= args
[1];
718 target_ulong value1
= args
[2];
719 target_ulong value2
= args
[3];
720 target_ulong ret
= H_P2
;
722 if (resource
== H_SET_MODE_RESOURCE_LE
) {
732 case H_SET_MODE_ENDIAN_BIG
:
734 set_spr(cs
, SPR_LPCR
, 0, LPCR_ILE
);
739 case H_SET_MODE_ENDIAN_LITTLE
:
741 set_spr(cs
, SPR_LPCR
, LPCR_ILE
, LPCR_ILE
);
747 ret
= H_UNSUPPORTED_FLAG
;
755 static spapr_hcall_fn papr_hypercall_table
[(MAX_HCALL_OPCODE
/ 4) + 1];
756 static spapr_hcall_fn kvmppc_hypercall_table
[KVMPPC_HCALL_MAX
- KVMPPC_HCALL_BASE
+ 1];
758 void spapr_register_hypercall(target_ulong opcode
, spapr_hcall_fn fn
)
760 spapr_hcall_fn
*slot
;
762 if (opcode
<= MAX_HCALL_OPCODE
) {
763 assert((opcode
& 0x3) == 0);
765 slot
= &papr_hypercall_table
[opcode
/ 4];
767 assert((opcode
>= KVMPPC_HCALL_BASE
) && (opcode
<= KVMPPC_HCALL_MAX
));
769 slot
= &kvmppc_hypercall_table
[opcode
- KVMPPC_HCALL_BASE
];
776 target_ulong
spapr_hypercall(PowerPCCPU
*cpu
, target_ulong opcode
,
779 if ((opcode
<= MAX_HCALL_OPCODE
)
780 && ((opcode
& 0x3) == 0)) {
781 spapr_hcall_fn fn
= papr_hypercall_table
[opcode
/ 4];
784 return fn(cpu
, spapr
, opcode
, args
);
786 } else if ((opcode
>= KVMPPC_HCALL_BASE
) &&
787 (opcode
<= KVMPPC_HCALL_MAX
)) {
788 spapr_hcall_fn fn
= kvmppc_hypercall_table
[opcode
- KVMPPC_HCALL_BASE
];
791 return fn(cpu
, spapr
, opcode
, args
);
795 hcall_dprintf("Unimplemented hcall 0x" TARGET_FMT_lx
"\n", opcode
);
799 static void hypercall_register_types(void)
802 spapr_register_hypercall(H_ENTER
, h_enter
);
803 spapr_register_hypercall(H_REMOVE
, h_remove
);
804 spapr_register_hypercall(H_PROTECT
, h_protect
);
805 spapr_register_hypercall(H_READ
, h_read
);
808 spapr_register_hypercall(H_BULK_REMOVE
, h_bulk_remove
);
811 spapr_register_hypercall(H_SET_DABR
, h_set_dabr
);
814 spapr_register_hypercall(H_REGISTER_VPA
, h_register_vpa
);
815 spapr_register_hypercall(H_CEDE
, h_cede
);
817 /* "debugger" hcalls (also used by SLOF). Note: We do -not- differenciate
818 * here between the "CI" and the "CACHE" variants, they will use whatever
819 * mapping attributes qemu is using. When using KVM, the kernel will
820 * enforce the attributes more strongly
822 spapr_register_hypercall(H_LOGICAL_CI_LOAD
, h_logical_load
);
823 spapr_register_hypercall(H_LOGICAL_CI_STORE
, h_logical_store
);
824 spapr_register_hypercall(H_LOGICAL_CACHE_LOAD
, h_logical_load
);
825 spapr_register_hypercall(H_LOGICAL_CACHE_STORE
, h_logical_store
);
826 spapr_register_hypercall(H_LOGICAL_ICBI
, h_logical_icbi
);
827 spapr_register_hypercall(H_LOGICAL_DCBF
, h_logical_dcbf
);
828 spapr_register_hypercall(KVMPPC_H_LOGICAL_MEMOP
, h_logical_memop
);
830 /* qemu/KVM-PPC specific hcalls */
831 spapr_register_hypercall(KVMPPC_H_RTAS
, h_rtas
);
833 spapr_register_hypercall(H_SET_MODE
, h_set_mode
);
836 type_init(hypercall_register_types
)