mips: Add Loongson-3 machine support
v12 cover mips: Add Loongson-3 machine support
Loongson-3 CPU family include Loongson-3A R1/R2/R3/R4 and Loongson-3B R1/R2. Loongson-3A R1 is the oldest and its ISA is the smallest, while Loongson-3A R4 is the newest and its ISA is almost the superset of all others. To reduce complexity, in QEMU we just define two CPU types:
1, "Loongson-3A1000" CPU which is corresponding to Loongson-3A R1. It is
suitable for TCG because Loongson-3A R1 has fewest ASE.
2, "Loongson-3A4000" CPU which is corresponding to Loongson-3A R4. It is
suitable for KVM because Loongson-3A R4 has the VZ ASE.
Loongson-3 lacks English documents. I've tried to translated them with translate.google.com, and the machine translated documents (together with their original Chinese versions) are available here.
Loongson-3A R1 (Loongson-3A1000)
User Manual Part 1:
http://ftp.godson.ac.cn/lemote/3A1000_p1.pdf
http://ftp.godson.ac.cn/lemote/Loongson3A1000_processor_user_manual_P1.pdf (Chinese Version) User Manual Part 2:
http://ftp.godson.ac.cn/lemote/3A1000_p2.pdf
http://ftp.godson.ac.cn/lemote/Loongson3A1000_processor_user_manual_P2.pdf (Chinese Version)
Loongson-3A R2 (Loongson-3A2000)
User Manual Part 1:
http://ftp.godson.ac.cn/lemote/3A2000_p1.pdf
http://ftp.godson.ac.cn/lemote/Loongson3A2000_user1.pdf (Chinese Version) User Manual Part 2:
http://ftp.godson.ac.cn/lemote/3A2000_p2.pdf
http://ftp.godson.ac.cn/lemote/Loongson3A2000_user2.pdf (Chinese Version)
Loongson-3A R3 (Loongson-3A3000)
User Manual Part 1:
http://ftp.godson.ac.cn/lemote/3A3000_p1.pdf
http://ftp.godson.ac.cn/lemote/Loongson3A3000_3B3000usermanual1.pdf (Chinese Version) User Manual Part 2:
http://ftp.godson.ac.cn/lemote/3A3000_p2.pdf
http://ftp.godson.ac.cn/lemote/Loongson3A3000_3B3000usermanual2.pdf (Chinese Version)
Loongson-3A R4 (Loongson-3A4000)
User Manual Part 1:
http://ftp.godson.ac.cn/lemote/3A4000_p1.pdf
http://ftp.godson.ac.cn/lemote/3A4000user.pdf (Chinese Version) User Manual Part 2:
I'm sorry that it is unavailable now.
And human-translated documents (W.I.P) are available here now:
https://github.com/loongson-community/docs/tree/master/English-translation-of-Loongson-manual
Both KVM and TCG are available now!
We now already have a full functional Linux kernel (based on Linux-5.4.x LTS, the kvm host side and guest side have both been upstream for Linux- 5.9, but Linux-5.9 has not been released yet) here:
https://github.com/chenhuacai/linux
Of course the upstream kernel is also usable (though it is "unstable" now):
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
How to use QEMU/Loongson-3?
1, Download kernel source from the above URL;
2, Build a kernel with arch/mips/configs/loongson3_{def,hpc}config;
3, Boot a Loongson-3A4000 host with this kernel (for KVM mode);
4, Build QEMU-master with this patchset;
5, modprobe kvm (only necessary for KVM mode);
6, Use QEMU with TCG:
qemu-system-mips64el -M loongson3-virt,accel=tcg -cpu Loongson-3A1000 -kernel <path_to_kernel> -append ...
Use QEMU with KVM:
qemu-system-mips64el -M loongson3-virt,accel=kvm -cpu Loongson-3A4000 -kernel <path_to_kernel> -append ...
The "-cpu" parameter is optional here and QEMU will use the correct type for TCG/KVM automatically.
V1 -> V2:
1, Add a cover letter;
2, Improve CPU definitions;
3, Remove LS7A-related things (Use GPEX instead); 4, Add a description of how to run QEMU/Loongson-3.
V2 -> V3:
1, Fix all possible checkpatch.pl errors and warnings.
V3 -> V4:
1, Sync code with upstream;
2, Remove merged patches;
3, Fix build failure without CONFIG_KVM;
4, Add Reviewed-by: Aleksandar Markovic <[email protected]>.
V4 -> V5:
1, Improve coding style;
2, Remove merged patches;
3, Rename machine name from "loongson3" to "loongson3-virt"; 4, Rework the "loongson3-virt" machine to drop any ISA things; 5, Rework "hw/mips: Implement the kvm_type() hook in MachineClass"; 6, Add Jiaxun Yang as a reviewer of Loongson-3.
V5 -> V6:
1, Fix license preamble;
2, Improve commit messages;
3, Add hw/intc/loongson_liointc.c to MAINTAINERS; 4, Fix all possible checkpatch.pl errors and warnings.
V7 and V8 have only one patch (machine definition) with some minor improvements.
V8 -> V9:
1, Update KVM type definition from kernel; 2, Fix PageMask with variable page size for TCG; 3, Add TCG support (add Loongson-EXT instructions).
V9 -> V10:
1, Split fw_cfg to a separate patch;
2, Split boot parameters definition to a local header; 3, Update MIPS machine documentation;
4, Many other improvements suggested by Philippe Mathieu-Daudé.
V10 -> V11:
1, Fix some typos;
2, Add Reviewed-by: Philippe Mathieu-Daudé <[email protected]>.
V11 -> V12:
1, Split boot parameter helpers to loongson3_bootp.c; 2, Support both BE and LE host (Loongson guests are always LE).
Huacai Chen and Jiaxun Yang (8):
linux-headers: Update MIPS KVM type defintition target/mips: Fix PageMask with variable page size target/mips: Add loongson-ext lswc2 group of instructions (Part 1) target/mips: Add loongson-ext lswc2 group of instructions (Part 2) target/mips: Add loongson-ext lsdc2 group of instructions hw/mips: Implement fw_cfg_arch_key_name()
hw/mips: Add Loongson-3 machine support
docs/system: Update MIPS machine documentation
--- default-configs/mips64el-softmmu.mak | 1 + docs/system/cpu-models-mips.rst.inc | 10 +- docs/system/target-mips.rst | 10 + hw/mips/Kconfig | 12 + hw/mips/fw_cfg.c | 35 ++ hw/mips/fw_cfg.h | 19 ++ hw/mips/loongson3_bootp.c | 162 +++++++++ hw/mips/loongson3_bootp.h | 225 +++++++++++++ hw/mips/loongson3_virt.c | 615 +++++++++++++++++++++++++++++++++++ hw/mips/meson.build | 3 +- linux-headers/linux/kvm.h | 5 +- target/mips/cp0_helper.c | 36 +- target/mips/cpu.h | 1 + target/mips/translate.c | 443 +++++++++++++++++++++++++ 14 files changed, 1565 insertions(+), 12 deletions(-) create mode 100644 hw/mips/fw_cfg.c create mode 100644 hw/mips/fw_cfg.h create mode 100644 hw/mips/loongson3_bootp.c create mode 100644 hw/mips/loongson3_bootp.h create mode 100644 hw/mips/loongson3_virt.c
--
2.7.0
PATCH v12 1/8 linux-headers: Update MIPS KVM type defintition
Update MIPS KVM type defintition from Linux 5.9-rc6.
--- linux-headers/linux/kvm.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
linux-headers/linux/kvm.h +3 -2
@@ -789,9 +789,10 @@ struct kvm_ppc_resize_hpt {#define KVM_VM_PPC_HV 1#define KVM_VM_PPC_PR 2 -/* on MIPS, 0 forces trap & emulate, 1 forces VZ ASE */ -#define KVM_VM_MIPS_TE 0 +/* on MIPS, 0 indicates auto, 1 forces VZ ASE, 2 forces trap & emulate */ +#define KVM_VM_MIPS_AUTO 0#define KVM_VM_MIPS_VZ 1 +#define KVM_VM_MIPS_TE 2#define KVM_S390_SIE_PAGE_OFFSET 1
Signature · 1 line
2.7.0
PATCH v12 2/8 target/mips: Fix PageMask with variable page size
In reply to Huacai ChenFrom: Jiaxun Yang <[email protected]>
Our current code assumed the target page size is always 4k when handling PageMask and VPN2, however, variable page size was just added to mips target and that's no longer true.
--- target/mips/cp0_helper.c | 36 +++++++++++++++++++++++++++++------- target/mips/cpu.h | 1 + 2 files changed, 30 insertions(+), 7 deletions(-)
target/mips/cp0_helper.c +29 -7
@@ -867,13 +867,35 @@ void helper_mtc0_memorymapid(CPUMIPSState *env, target_ulong arg1)void update_pagemask (CPUMIPSState *env,target_ulong arg1,int32_t *pagemask) { -uint64_t mask = arg1 >> (TARGET_PAGE_BITS +1 ); -if (!(env->insn_flags &ISA_MIPS32R6 ) || (arg1 == ~0 ) || - (mask ==0x0000 || mask ==0x0003 || mask ==0x000F || - mask ==0x003F || mask ==0x00FF || mask ==0x03FF || - mask ==0x0FFF || mask ==0x3FFF || mask ==0xFFFF )) { - env->CP0_PageMask = arg1 & (0x1FFFFFFF & (TARGET_PAGE_MASK <<1 )); +unsigned long mask; +int maskbits; + +if (env->insn_flags &ISA_MIPS32R6 ) { +return ; + } +/* Don't care MASKX as we don't support 1KB page */ + mask =extract32 ((uint32_t )arg1,CP0PM_MASK ,16 ); + maskbits =find_first_zero_bit (&mask,32 ); + +/* Ensure no more set bit after first zero */ +if (mask >> maskbits) { + goto invalid; + } +/* We don't support VTLB entry smaller than target page */ +if ((maskbits +12 ) <TARGET_PAGE_BITS ) { + goto invalid; } + env->CP0_PageMask = mask <<CP0PM_MASK ; + +return ; + +invalid: +/* +* When invalid, ensure the value is bigger than or equal to +* the minimal but smaller than or equal to the maxium. +*/ + maskbits =MIN (16 ,MAX (maskbits,TARGET_PAGE_BITS -12 )); + env->CP0_PageMask = ((1 << (16 +1 )) -1 ) <<CP0PM_MASK ; }void helper_mtc0_pagemask (CPUMIPSState *env,target_ulong arg1)
@@ -1104,7 +1126,7 @@ void helper_mthc0_saar(CPUMIPSState *env, target_ulong arg1)void helper_mtc0_entryhi (CPUMIPSState *env,target_ulong arg1) {target_ulong old, val, mask; - mask = (TARGET_PAGE_MASK <<1 ) | env->CP0_EntryHi_ASID_mask; + mask = ~((1 <<14 ) -1 ) | env->CP0_EntryHi_ASID_mask;if (((env->CP0_Config4 >>CP0C4_IE ) &0x3 ) >=2 ) { mask |=1 << CP0EnHi_EHINV; }
target/mips/cpu.h +1 -0
@@ -618,6 +618,7 @@ struct CPUMIPSState {* CP0 Register 5 */ int32_t CP0_PageMask; +#define CP0PM_MASK 13int32_t CP0_PageGrain_rw_bitmask;int32_t CP0_PageGrain;#define CP0PG_RIE 31
Signature · 1 line
2.7.0
PATCH v12 3/8 target/mips: Add loongson-ext lswc2 group of instructions (Part 1)
In reply to Huacai ChenFrom: Jiaxun Yang <[email protected]>
LWC2 & SWC2 have been rewritten by Loongson EXT vendor ASE as "load/store quad word" and "shifted load/store" groups of instructions.
This patch add implementation of these instructions:
gslq: load 16 bytes to GPR
gssq: store 16 bytes from GPR
gslqc1: load 16 bytes to FPR
gssqc1: store 16 bytes from FPR
Details of Loongson-EXT is here:
https://github.com/FlyGoat/loongson-insn/blob/master/loongson-ext.md
--- target/mips/translate.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+)
target/mips/translate.c +87 -0
@@ -460,6 +460,17 @@ enum { R6_OPC_SCD = 0x27 | OPC_SPECIAL3, }; +/* Loongson EXT load/store quad word opcodes */ +#define MASK_LOONGSON_GSLSQ (op) (MASK_OP_MAJOR(op) | (op & 0x8020)) +enum { +OPC_GSLQ =0x0020 |OPC_LWC2 , +OPC_GSLQC1 =0x8020 |OPC_LWC2 , +OPC_GSSHFL =OPC_LWC2 , +OPC_GSSQ =0x0020 |OPC_SWC2 , +OPC_GSSQC1 =0x8020 |OPC_SWC2 , +OPC_GSSHFS =OPC_SWC2 , +}; +/* BSHFL opcodes */ #define MASK_BSHFL (op) (MASK_SPECIAL3(op) | (op & (0x1F << 6)))
@@ -5910,6 +5921,80 @@ no_rd:tcg_temp_free_i64 (t1); } +static void gen_loongson_lswc2 (DisasContext *ctx,int rt, +int rs,int rd) +{ +TCGv t0, t1, t2; +TCGv_i32 fp0; +#if defined(TARGET_MIPS64 ) +int lsq_rt1 = ctx->opcode &0x1f ; +int lsq_offset = ((int )((ctx->opcode >>6 ) &0x1ff ) <<23 ) >>19 ; +#endif +int shf_offset = (int8_t )(ctx->opcode >>6 ); + + t0 =tcg_temp_new (); + +switch (MASK_LOONGSON_GSLSQ (ctx->opcode)) { +#if defined(TARGET_MIPS64 ) +case OPC_GSLQ : + t1 =tcg_temp_new (); +gen_base_offset_addr (ctx, t0, rs, lsq_offset); +tcg_gen_qemu_ld_tl (t1, t0, ctx->mem_idx,MO_TEQ | + ctx->default_tcg_memop_mask); +gen_base_offset_addr (ctx, t0, rs, lsq_offset +8 ); +tcg_gen_qemu_ld_tl (t0, t0, ctx->mem_idx,MO_TEQ | + ctx->default_tcg_memop_mask); +gen_store_gpr (t1, rt); +gen_store_gpr (t0, lsq_rt1); +tcg_temp_free (t1); +break ; +case OPC_GSLQC1 : +check_cp1_enabled (ctx); + t1 =tcg_temp_new (); +gen_base_offset_addr (ctx, t0, rs, lsq_offset); +tcg_gen_qemu_ld_tl (t1, t0, ctx->mem_idx,MO_TEQ | + ctx->default_tcg_memop_mask); +gen_base_offset_addr (ctx, t0, rs, lsq_offset +8 ); +tcg_gen_qemu_ld_tl (t0, t0, ctx->mem_idx,MO_TEQ | + ctx->default_tcg_memop_mask); +gen_store_fpr64 (ctx, t1, rt); +gen_store_fpr64 (ctx, t0, lsq_rt1); +tcg_temp_free (t1); +break ; +case OPC_GSSQ : + t1 =tcg_temp_new (); +gen_base_offset_addr (ctx, t0, rs, lsq_offset); +gen_load_gpr (t1, rt); +tcg_gen_qemu_st_tl (t1, t0, ctx->mem_idx,MO_TEQ | + ctx->default_tcg_memop_mask); +gen_base_offset_addr (ctx, t0, rs, lsq_offset +8 ); +gen_load_gpr (t1, lsq_rt1); +tcg_gen_qemu_st_tl (t1, t0, ctx->mem_idx,MO_TEQ | + ctx->default_tcg_memop_mask); +tcg_temp_free (t1); +break ; +case OPC_GSSQC1 : +check_cp1_enabled (ctx); + t1 =tcg_temp_new (); +gen_base_offset_addr (ctx, t0, rs, lsq_offset); +gen_load_fpr64 (ctx, t1, rt); +tcg_gen_qemu_st_tl (t1, t0, ctx->mem_idx,MO_TEQ | + ctx->default_tcg_memop_mask); +gen_base_offset_addr (ctx, t0, rs, lsq_offset +8 ); +gen_load_fpr64 (ctx, t1, lsq_rt1); +tcg_gen_qemu_st_tl (t1, t0, ctx->mem_idx,MO_TEQ | + ctx->default_tcg_memop_mask); +tcg_temp_free (t1); +break ; +#endif +default : +MIPS_INVAL ("loongson_gslsq" ); +generate_exception_end (ctx,EXCP_RI ); +break ; + } +tcg_temp_free (t0); +} +/* Traps */ static void gen_trap (DisasContext *ctx,uint32_t opc,int rs,int rt,int16_t imm)
@@ -30774,6 +30859,8 @@ static void decode_opc(CPUMIPSState *env, DisasContext *ctx)/* OPC_BC, OPC_BALC */ gen_compute_compact_branch (ctx, op,0 ,0 ,sextract32 (ctx->opcode <<2 ,0 ,28 )); + }else if (ctx->insn_flags &ASE_LEXT ) { +gen_loongson_lswc2 (ctx, rt, rs, rd); }else {/* OPC_LWC2, OPC_SWC2 */ /* COP2: Not implemented. */
Signature · 1 line
2.7.0
PATCH v12 4/8 target/mips: Add loongson-ext lswc2 group of instructions (Part 2)
In reply to Huacai ChenFrom: Jiaxun Yang <[email protected]>
LWC2 & SWC2 have been rewritten by Loongson EXT vendor ASE as "load/store quad word" and "shifted load/store" groups of instructions.
This patch add implementation of these instructions:
gslwlc1: similar to lwl but RT is FPR instead of GPR
gslwrc1: similar to lwr but RT is FPR instead of GPR
gsldlc1: similar to ldl but RT is FPR instead of GPR
gsldrc1: similar to ldr but RT is FPR instead of GPR
gsswlc1: similar to swl but RT is FPR instead of GPR
gsswrc1: similar to swr but RT is FPR instead of GPR
gssdlc1: similar to sdl but RT is FPR instead of GPR
gssdrc1: similar to sdr but RT is FPR instead of GPR
Details of Loongson-EXT is here:
https://github.com/FlyGoat/loongson-insn/blob/master/loongson-ext.md
--- target/mips/translate.c | 177 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 177 insertions(+)
target/mips/translate.c +177 -0
@@ -471,6 +471,19 @@ enum { OPC_GSSHFS = OPC_SWC2, }; +/* Loongson EXT shifted load/store opcodes */ +#define MASK_LOONGSON_GSSHFLS (op) (MASK_OP_MAJOR(op) | (op & 0xc03f)) +enum { +OPC_GSLWLC1 =0x4 |OPC_GSSHFL , +OPC_GSLWRC1 =0x5 |OPC_GSSHFL , +OPC_GSLDLC1 =0x6 |OPC_GSSHFL , +OPC_GSLDRC1 =0x7 |OPC_GSSHFL , +OPC_GSSWLC1 =0x4 |OPC_GSSHFS , +OPC_GSSWRC1 =0x5 |OPC_GSSHFS , +OPC_GSSDLC1 =0x6 |OPC_GSSHFS , +OPC_GSSDRC1 =0x7 |OPC_GSSHFS , +}; +/* BSHFL opcodes */ #define MASK_BSHFL (op) (MASK_SPECIAL3(op) | (op & (0x1F << 6)))
@@ -5987,6 +6000,170 @@ static void gen_loongson_lswc2(DisasContext *ctx, int rt,tcg_temp_free (t1);break ;#endif +case OPC_GSSHFL : +switch (MASK_LOONGSON_GSSHFLS (ctx->opcode)) { +case OPC_GSLWLC1 : +check_cp1_enabled (ctx); +gen_base_offset_addr (ctx, t0, rs, shf_offset); + t1 =tcg_temp_new (); +tcg_gen_qemu_ld_tl (t1, t0, ctx->mem_idx,MO_UB ); +tcg_gen_andi_tl (t1, t0,3 ); +#ifndef TARGET_WORDS_BIGENDIAN +tcg_gen_xori_tl (t1, t1,3 ); +#endif +tcg_gen_shli_tl (t1, t1,3 ); +tcg_gen_andi_tl (t0, t0, ~3 ); +tcg_gen_qemu_ld_tl (t0, t0, ctx->mem_idx,MO_TEUL ); +tcg_gen_shl_tl (t0, t0, t1); + t2 =tcg_const_tl (-1 ); +tcg_gen_shl_tl (t2, t2, t1); + fp0 =tcg_temp_new_i32 (); +gen_load_fpr32 (ctx, fp0, rt); +tcg_gen_ext_i32_tl (t1, fp0); +tcg_gen_andc_tl (t1, t1, t2); +tcg_temp_free (t2); +tcg_gen_or_tl (t0, t0, t1); +tcg_temp_free (t1); +#if defined(TARGET_MIPS64 ) +tcg_gen_extrl_i64_i32 (fp0, t0); +#else +tcg_gen_ext32s_tl (fp0, t0); +#endif +gen_store_fpr32 (ctx, fp0, rt); +tcg_temp_free_i32 (fp0); +break ; +case OPC_GSLWRC1 : +check_cp1_enabled (ctx); +gen_base_offset_addr (ctx, t0, rs, shf_offset); + t1 =tcg_temp_new (); +tcg_gen_qemu_ld_tl (t1, t0, ctx->mem_idx,MO_UB ); +tcg_gen_andi_tl (t1, t0,3 ); +#ifdef TARGET_WORDS_BIGENDIAN +tcg_gen_xori_tl (t1, t1,3 ); +#endif +tcg_gen_shli_tl (t1, t1,3 ); +tcg_gen_andi_tl (t0, t0, ~3 ); +tcg_gen_qemu_ld_tl (t0, t0, ctx->mem_idx,MO_TEUL ); +tcg_gen_shr_tl (t0, t0, t1); +tcg_gen_xori_tl (t1, t1,31 ); + t2 =tcg_const_tl (0xfffffffeull ); +tcg_gen_shl_tl (t2, t2, t1); + fp0 =tcg_temp_new_i32 (); +gen_load_fpr32 (ctx, fp0, rt); +tcg_gen_ext_i32_tl (t1, fp0); +tcg_gen_and_tl (t1, t1, t2); +tcg_temp_free (t2); +tcg_gen_or_tl (t0, t0, t1); +tcg_temp_free (t1); +#if defined(TARGET_MIPS64 ) +tcg_gen_extrl_i64_i32 (fp0, t0); +#else +tcg_gen_ext32s_tl (fp0, t0); +#endif +gen_store_fpr32 (ctx, fp0, rt); +tcg_temp_free_i32 (fp0); +break ; +#if defined(TARGET_MIPS64 ) +case OPC_GSLDLC1 : +check_cp1_enabled (ctx); +gen_base_offset_addr (ctx, t0, rs, shf_offset); + t1 =tcg_temp_new (); +tcg_gen_qemu_ld_tl (t1, t0, ctx->mem_idx,MO_UB ); +tcg_gen_andi_tl (t1, t0,7 ); +#ifndef TARGET_WORDS_BIGENDIAN +tcg_gen_xori_tl (t1, t1,7 ); +#endif +tcg_gen_shli_tl (t1, t1,3 ); +tcg_gen_andi_tl (t0, t0, ~7 ); +tcg_gen_qemu_ld_tl (t0, t0, ctx->mem_idx,MO_TEQ ); +tcg_gen_shl_tl (t0, t0, t1); + t2 =tcg_const_tl (-1 ); +tcg_gen_shl_tl (t2, t2, t1); +gen_load_fpr64 (ctx, t1, rt); +tcg_gen_andc_tl (t1, t1, t2); +tcg_temp_free (t2); +tcg_gen_or_tl (t0, t0, t1); +tcg_temp_free (t1); +gen_store_fpr64 (ctx, t0, rt); +break ; +case OPC_GSLDRC1 : +check_cp1_enabled (ctx); +gen_base_offset_addr (ctx, t0, rs, shf_offset); + t1 =tcg_temp_new (); +tcg_gen_qemu_ld_tl (t1, t0, ctx->mem_idx,MO_UB ); +tcg_gen_andi_tl (t1, t0,7 ); +#ifdef TARGET_WORDS_BIGENDIAN +tcg_gen_xori_tl (t1, t1,7 ); +#endif +tcg_gen_shli_tl (t1, t1,3 ); +tcg_gen_andi_tl (t0, t0, ~7 ); +tcg_gen_qemu_ld_tl (t0, t0, ctx->mem_idx,MO_TEQ ); +tcg_gen_shr_tl (t0, t0, t1); +tcg_gen_xori_tl (t1, t1,63 ); + t2 =tcg_const_tl (0xfffffffffffffffeull ); +tcg_gen_shl_tl (t2, t2, t1); +gen_load_fpr64 (ctx, t1, rt); +tcg_gen_and_tl (t1, t1, t2); +tcg_temp_free (t2); +tcg_gen_or_tl (t0, t0, t1); +tcg_temp_free (t1); +gen_store_fpr64 (ctx, t0, rt); +break ; +#endif +default : +MIPS_INVAL ("loongson_gsshfl" ); +generate_exception_end (ctx,EXCP_RI ); +break ; + } +break ; +case OPC_GSSHFS : +switch (MASK_LOONGSON_GSSHFLS (ctx->opcode)) { +case OPC_GSSWLC1 : +check_cp1_enabled (ctx); + t1 =tcg_temp_new (); +gen_base_offset_addr (ctx, t0, rs, shf_offset); + fp0 =tcg_temp_new_i32 (); +gen_load_fpr32 (ctx, fp0, rt); +tcg_gen_ext_i32_tl (t1, fp0); +gen_helper_0e2i (swl, t1, t0, ctx->mem_idx); +tcg_temp_free_i32 (fp0); +tcg_temp_free (t1); +break ; +case OPC_GSSWRC1 : +check_cp1_enabled (ctx); + t1 =tcg_temp_new (); +gen_base_offset_addr (ctx, t0, rs, shf_offset); + fp0 =tcg_temp_new_i32 (); +gen_load_fpr32 (ctx, fp0, rt); +tcg_gen_ext_i32_tl (t1, fp0); +gen_helper_0e2i (swr, t1, t0, ctx->mem_idx); +tcg_temp_free_i32 (fp0); +tcg_temp_free (t1); +break ; +#if defined(TARGET_MIPS64 ) +case OPC_GSSDLC1 : +check_cp1_enabled (ctx); + t1 =tcg_temp_new (); +gen_base_offset_addr (ctx, t0, rs, shf_offset); +gen_load_fpr64 (ctx, t1, rt); +gen_helper_0e2i (sdl, t1, t0, ctx->mem_idx); +tcg_temp_free (t1); +break ; +case OPC_GSSDRC1 : +check_cp1_enabled (ctx); + t1 =tcg_temp_new (); +gen_base_offset_addr (ctx, t0, rs, shf_offset); +gen_load_fpr64 (ctx, t1, rt); +gen_helper_0e2i (sdr, t1, t0, ctx->mem_idx); +tcg_temp_free (t1); +break ; +#endif +default : +MIPS_INVAL ("loongson_gsshfs" ); +generate_exception_end (ctx,EXCP_RI ); +break ; + } +break ;default :MIPS_INVAL ("loongson_gslsq" );generate_exception_end (ctx,EXCP_RI );
Signature · 1 line
2.7.0
PATCH v12 5/8 target/mips: Add loongson-ext lsdc2 group of instructions
In reply to Huacai ChenFrom: Jiaxun Yang <[email protected]>
LDC2/SDC2 opcodes have been rewritten as "load & store with offset" group of instructions by loongson-ext ASE.
This patch add implementation of these instructions:
gslbx: load 1 bytes to GPR
gslhx: load 2 bytes to GPR
gslwx: load 4 bytes to GPR
gsldx: load 8 bytes to GPR
gslwxc1: load 4 bytes to FPR
gsldxc1: load 8 bytes to FPR
gssbx: store 1 bytes from GPR
gsshx: store 2 bytes from GPR
gsswx: store 4 bytes from GPR
gssdx: store 8 bytes from GPR
gsswxc1: store 4 bytes from FPR
gssdxc1: store 8 bytes from FPR
Details of Loongson-EXT is here:
https://github.com/FlyGoat/loongson-insn/blob/master/loongson-ext.md
--- target/mips/translate.c | 179 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+)
target/mips/translate.c +179 -0
@@ -484,6 +484,24 @@ enum { OPC_GSSDRC1 = 0x7 | OPC_GSSHFS, }; +/* Loongson EXT LDC2/SDC2 opcodes */ +#define MASK_LOONGSON_LSDC2 (op) (MASK_OP_MAJOR(op) | (op & 0x7)) + +enum { +OPC_GSLBX =0x0 |OPC_LDC2 , +OPC_GSLHX =0x1 |OPC_LDC2 , +OPC_GSLWX =0x2 |OPC_LDC2 , +OPC_GSLDX =0x3 |OPC_LDC2 , +OPC_GSLWXC1 =0x6 |OPC_LDC2 , +OPC_GSLDXC1 =0x7 |OPC_LDC2 , +OPC_GSSBX =0x0 |OPC_SDC2 , +OPC_GSSHX =0x1 |OPC_SDC2 , +OPC_GSSWX =0x2 |OPC_SDC2 , +OPC_GSSDX =0x3 |OPC_SDC2 , +OPC_GSSWXC1 =0x6 |OPC_SDC2 , +OPC_GSSDXC1 =0x7 |OPC_SDC2 , +}; +/* BSHFL opcodes */ #define MASK_BSHFL (op) (MASK_SPECIAL3(op) | (op & (0x1F << 6)))
@@ -6172,6 +6190,165 @@ static void gen_loongson_lswc2(DisasContext *ctx, int rt,tcg_temp_free (t0); } +/* Loongson EXT LDC2/SDC2 */ +static void gen_loongson_lsdc2 (DisasContext *ctx,int rt, +int rs,int rd) +{ +int offset = (int8_t )(ctx->opcode >>3 ); +uint32_t opc =MASK_LOONGSON_LSDC2 (ctx->opcode); +TCGv t0, t1; +TCGv_i32 fp0; + +/* Pre-conditions */ +switch (opc) { +case OPC_GSLBX : +case OPC_GSLHX : +case OPC_GSLWX : +case OPC_GSLDX : +/* prefetch, implement as NOP */ +if (rt ==0 ) { +return ; + } +break ; +case OPC_GSSBX : +case OPC_GSSHX : +case OPC_GSSWX : +case OPC_GSSDX : +break ; +case OPC_GSLWXC1 : +#if defined(TARGET_MIPS64 ) +case OPC_GSLDXC1 : +#endif +check_cp1_enabled (ctx); +/* prefetch, implement as NOP */ +if (rt ==0 ) { +return ; + } +break ; +case OPC_GSSWXC1 : +#if defined(TARGET_MIPS64 ) +case OPC_GSSDXC1 : +#endif +check_cp1_enabled (ctx); +break ; +default : +MIPS_INVAL ("loongson_lsdc2" ); +generate_exception_end (ctx,EXCP_RI ); +return ; +break ; + } + + t0 =tcg_temp_new (); + +gen_base_offset_addr (ctx, t0, rs, offset); +gen_op_addr_add (ctx, t0, cpu_gpr[rd], t0); + +switch (opc) { +case OPC_GSLBX : +tcg_gen_qemu_ld_tl (t0, t0, ctx->mem_idx,MO_SB ); +gen_store_gpr (t0, rt); +break ; +case OPC_GSLHX : +tcg_gen_qemu_ld_tl (t0, t0, ctx->mem_idx,MO_TESW | + ctx->default_tcg_memop_mask); +gen_store_gpr (t0, rt); +break ; +case OPC_GSLWX : +gen_base_offset_addr (ctx, t0, rs, offset); +if (rd) { +gen_op_addr_add (ctx, t0, cpu_gpr[rd], t0); + } +tcg_gen_qemu_ld_tl (t0, t0, ctx->mem_idx,MO_TESL | + ctx->default_tcg_memop_mask); +gen_store_gpr (t0, rt); +break ; +#if defined(TARGET_MIPS64 ) +case OPC_GSLDX : +gen_base_offset_addr (ctx, t0, rs, offset); +if (rd) { +gen_op_addr_add (ctx, t0, cpu_gpr[rd], t0); + } +tcg_gen_qemu_ld_tl (t0, t0, ctx->mem_idx,MO_TEQ | + ctx->default_tcg_memop_mask); +gen_store_gpr (t0, rt); +break ; +#endif +case OPC_GSLWXC1 : +check_cp1_enabled (ctx); +gen_base_offset_addr (ctx, t0, rs, offset); +if (rd) { +gen_op_addr_add (ctx, t0, cpu_gpr[rd], t0); + } + fp0 =tcg_temp_new_i32 (); +tcg_gen_qemu_ld_i32 (fp0, t0, ctx->mem_idx,MO_TESL | + ctx->default_tcg_memop_mask); +gen_store_fpr32 (ctx, fp0, rt); +tcg_temp_free_i32 (fp0); +break ; +#if defined(TARGET_MIPS64 ) +case OPC_GSLDXC1 : +check_cp1_enabled (ctx); +gen_base_offset_addr (ctx, t0, rs, offset); +if (rd) { +gen_op_addr_add (ctx, t0, cpu_gpr[rd], t0); + } +tcg_gen_qemu_ld_tl (t0, t0, ctx->mem_idx,MO_TEQ | + ctx->default_tcg_memop_mask); +gen_store_fpr64 (ctx, t0, rt); +break ; +#endif +case OPC_GSSBX : + t1 =tcg_temp_new (); +gen_load_gpr (t1, rt); +tcg_gen_qemu_st_tl (t1, t0, ctx->mem_idx,MO_SB ); +tcg_temp_free (t1); +break ; +case OPC_GSSHX : + t1 =tcg_temp_new (); +gen_load_gpr (t1, rt); +tcg_gen_qemu_st_tl (t1, t0, ctx->mem_idx,MO_TEUW | + ctx->default_tcg_memop_mask); +tcg_temp_free (t1); +break ; +case OPC_GSSWX : + t1 =tcg_temp_new (); +gen_load_gpr (t1, rt); +tcg_gen_qemu_st_tl (t1, t0, ctx->mem_idx,MO_TEUL | + ctx->default_tcg_memop_mask); +tcg_temp_free (t1); +break ; +#if defined(TARGET_MIPS64 ) +case OPC_GSSDX : + t1 =tcg_temp_new (); +gen_load_gpr (t1, rt); +tcg_gen_qemu_st_tl (t1, t0, ctx->mem_idx,MO_TEQ | + ctx->default_tcg_memop_mask); +tcg_temp_free (t1); +break ; +#endif +case OPC_GSSWXC1 : + fp0 =tcg_temp_new_i32 (); +gen_load_fpr32 (ctx, fp0, rt); +tcg_gen_qemu_st_i32 (fp0, t0, ctx->mem_idx,MO_TEUL | + ctx->default_tcg_memop_mask); +tcg_temp_free_i32 (fp0); +break ; +#if defined(TARGET_MIPS64 ) +case OPC_GSSDXC1 : + t1 =tcg_temp_new (); +gen_load_fpr64 (ctx, t1, rt); +tcg_gen_qemu_st_i64 (t1, t0, ctx->mem_idx,MO_TEQ | + ctx->default_tcg_memop_mask); +tcg_temp_free (t1); +break ; +#endif +default : +break ; + } + +tcg_temp_free (t0); +} +/* Traps */ static void gen_trap (DisasContext *ctx,uint32_t opc,int rs,int rt,int16_t imm)
@@ -31055,6 +31232,8 @@ static void decode_opc(CPUMIPSState *env, DisasContext *ctx)/* OPC_JIC, OPC_JIALC */ gen_compute_compact_branch (ctx, op,0 , rt, imm); } + }else if (ctx->insn_flags &ASE_LEXT ) { +gen_loongson_lsdc2 (ctx, rt, rs, rd); }else {/* OPC_LWC2, OPC_SWC2 */ /* COP2: Not implemented. */
Signature · 1 line
2.7.0
PATCH v12 6/8 hw/mips: Implement fw_cfg_arch_key_name()
In reply to Huacai ChenImplement fw_cfg_arch_key_name(), which returns the name of a mips-specific key.
--- hw/mips/fw_cfg.c | 35 +++++++++++++++++++++++++++++++++++ hw/mips/fw_cfg.h | 19 +++++++++++++++++++ hw/mips/meson.build | 2 +- 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 hw/mips/fw_cfg.c create mode 100644 hw/mips/fw_cfg.h
hw/mips/fw_cfg.c new file +35 -0
@@ -0,0 +1,35 @@ +/* +* QEMU fw_cfg helpers (MIPS specific) +* +* Copyright (c) 2020 Lemote, Inc. +* +* Author: +* Huacai Chen ([email protected]) +* +* SPDX-License-Identifier: GPL-2.0-or-later +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ + +#include "qemu/osdep.h" +#include "hw/mips/fw_cfg.h" +#include "hw/nvram/fw_cfg.h" + +const char *fw_cfg_arch_key_name (uint16_t key) +{ +static const struct { +uint16_t key; +const char *name; + } fw_cfg_arch_wellknown_keys[] = { + {FW_CFG_MACHINE_VERSION ,"machine_version" }, + {FW_CFG_CPU_FREQ ,"cpu_frequency" }, + }; + +for (size_t i =0 ; i <ARRAY_SIZE (fw_cfg_arch_wellknown_keys); i++) { +if (fw_cfg_arch_wellknown_keys[i].key == key) { +return fw_cfg_arch_wellknown_keys[i].name; + } + } +return NULL ; +}
hw/mips/fw_cfg.h new file +19 -0
@@ -0,0 +1,19 @@ +/* +* QEMU fw_cfg helpers (MIPS specific) +* +* Copyright (c) 2020 Huacai Chen +* +* SPDX-License-Identifier: MIT +*/ + +#ifndef HW_MIPS_FW_CFG_H +#define HW_MIPS_FW_CFG_H + +#include "hw/boards.h" +#include "hw/nvram/fw_cfg.h" + +/* Data for BIOS to identify machine */ +#define FW_CFG_MACHINE_VERSION (FW_CFG_ARCH_LOCAL + 0) +#define FW_CFG_CPU_FREQ (FW_CFG_ARCH_LOCAL + 1) + +#endif
hw/mips/meson.build +1 -1
@@ -1,5 +1,5 @@ mips_ss = ss.source_set() -mips_ss.add(files('addr.c', 'mips_int.c')) +mips_ss.add(files('addr.c', 'mips_int.c', 'fw_cfg.c')) mips_ss.add(when: 'CONFIG_FULOONG', if_true: files('fuloong2e.c')) mips_ss.add(when: 'CONFIG_JAZZ', if_true: files('jazz.c')) mips_ss.add(when: 'CONFIG_MALTA', if_true: files('gt64xxx_pci.c', 'malta.c'))
Signature · 1 line
2.7.0
PATCH v12 7/8 hw/mips: Add Loongson-3 machine support
In reply to Huacai ChenAdd Loongson-3 based machine support, it use liointc as the interrupt controler and use GPEX as the pci controller. Currently it can work with both TCG and KVM.
As the machine model is not based on any exiting physical hardware, the name of the machine is "loongson3-virt". It may be superseded in future by a real machine model. If this happens, then a regular deprecation procedure shall occur for "loongson3-virt" machine.
We now already have a full functional Linux kernel (based on Linux-5.4.x LTS, the kvm host side and guest side have both been upstream for Linux- 5.9, but Linux-5.9 has not been released yet) here:
https://github.com/chenhuacai/linux
Of course the upstream kernel is also usable (though it is "unstable" now):
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
How to use QEMU/Loongson-3?
1, Download kernel source from the above URL;
2, Build a kernel with arch/mips/configs/loongson3_defconfig;
3, Boot a Loongson-3A4000 host with this kernel (for KVM mode);
4, Build QEMU-master with this patchset;
5, modprobe kvm (only necessary for KVM mode);
6, Use QEMU with TCG:
qemu-system-mips64el -M loongson3-virt,accel=tcg -cpu Loongson-3A1000 -kernel <path_to_kernel> -append ...
Use QEMU with KVM:
qemu-system-mips64el -M loongson3-virt,accel=kvm -cpu Loongson-3A4000 -kernel <path_to_kernel> -append ...
The "-cpu" parameter is optional here and QEMU will use the correct type for TCG/KVM automatically.
--- default-configs/mips64el-softmmu.mak | 1 + hw/mips/Kconfig | 12 + hw/mips/loongson3_bootp.c | 162 +++++++++ hw/mips/loongson3_bootp.h | 225 +++++++++++++ hw/mips/loongson3_virt.c | 615 +++++++++++++++++++++++++++++++++++ hw/mips/meson.build | 1 + 6 files changed, 1016 insertions(+) create mode 100644 hw/mips/loongson3_bootp.c create mode 100644 hw/mips/loongson3_bootp.h create mode 100644 hw/mips/loongson3_virt.c
default-configs/mips64el-softmmu.mak +1 -0
@@ -3,6 +3,7 @@ include mips-softmmu-common.mak CONFIG_IDE_VIA=y CONFIG_FULOONG=y +CONFIG_LOONGSON3V=y CONFIG_ATI_VGA=y CONFIG_RTL8139_PCI=y CONFIG_JAZZ=y
hw/mips/Kconfig +12 -0
@@ -45,6 +45,18 @@ config FULOONG bool select PCI_BONITO +config LOONGSON3V + bool + select PCKBD + select SERIAL + select GOLDFISH_RTC + select LOONGSON_LIOINTC + select PCI_DEVICES + select PCI_EXPRESS_GENERIC_BRIDGE + select VIRTIO_VGA + select QXL if SPICE + select MSI_NONBROKEN + config MIPS_CPS bool select PTIMER
hw/mips/loongson3_bootp.c new file +162 -0
162 lines not on this page · raw message
hw/mips/loongson3_bootp.h new file +225 -0
225 lines not on this page · raw message
hw/mips/loongson3_virt.c new file +615 -0
615 lines not on this page · raw message
hw/mips/meson.build +1 -0
@@ -1,6 +1,7 @@ mips_ss = ss.source_set() mips_ss.add(files('addr.c', 'mips_int.c', 'fw_cfg.c')) mips_ss.add(when: 'CONFIG_FULOONG', if_true: files('fuloong2e.c')) +mips_ss.add(when: 'CONFIG_LOONGSON3V', if_true: files('loongson3_bootp.c', 'loongson3_virt.c')) mips_ss.add(when: 'CONFIG_JAZZ', if_true: files('jazz.c')) mips_ss.add(when: 'CONFIG_MALTA', if_true: files('gt64xxx_pci.c', 'malta.c')) mips_ss.add(when: 'CONFIG_MIPSSIM', if_true: files('mipssim.c'))
Signature · 1 line
2.7.0
PATCH v12 8/8 docs/system: Update MIPS machine documentation
In reply to Huacai ChenAdd Loongson-3A CPU models and Loongson-3 based machine description.
--- docs/system/cpu-models-mips.rst.inc | 10 ++++++++-- docs/system/target-mips.rst | 10 ++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-)
docs/system/cpu-models-mips.rst.inc +8 -2
@@ -48,11 +48,17 @@ across all desired hosts. ``I6400`` MIPS64 Processor (Release 6, 2014) +``Loongson-2E`` + MIPS64 Processor (Loongson 2, 2006) + ``Loongson-2F`` MIPS64 Processor (Loongson 2, 2008) -``Loongson-2E`` - MIPS64 Processor (Loongson 2, 2006) +``Loongson-3A1000`` + MIPS64 Processor (Loongson 3, 2010) + +``Loongson-3A4000`` + MIPS64 Processor (Loongson 3, 2018) ``mips64dspr2`` MIPS64 Processor (Release 2, 2006)
docs/system/target-mips.rst +10 -0
@@ -84,6 +84,16 @@ The Fuloong 2E emulation supports: - RTL8139D as a network card chipset +The Loongson-3 virtual platform emulation supports: + +- Loongson 3A CPU + +- LIOINTC as interrupt controller + +- GPEX and virtio as variable devices + +- Both KVM and TCG supported + The mipssim pseudo board emulation provides an environment similar to what the proprietary MIPS emulator uses for running Linux. It supports:
Signature · 1 line
2.7.0