Skip to content

Commit ef12ea6

Browse files
committed
Merge tag 'loongarch-kvm-6.7' of git://git.kernel.org/pub/scm/linux/kernel/git/chenhuacai/linux-loongson into HEAD
LoongArch KVM changes for v6.7 Add LoongArch's KVM support. Loongson 3A5000/3A6000 supports hardware assisted virtualization. With cpu virtualization, there are separate hw-supported user mode and kernel mode in guest mode. With memory virtualization, there are two-level hw mmu table for guest mode and host mode. Also there is separate hw cpu timer with consant frequency in guest mode, so that vm can migrate between hosts with different freq. Currently, we are able to boot LoongArch Linux Guests. Few key aspects of KVM LoongArch added by this series are: 1. Enable kvm hardware function when kvm module is loaded. 2. Implement VM and vcpu related ioctl interface such as vcpu create, vcpu run etc. GET_ONE_REG/SET_ONE_REG ioctl commands are use to get general registers one by one. 3. Hardware access about MMU, timer and csr are emulated in kernel. 4. Hardwares such as mmio and iocsr device are emulated in user space such as IPI, irqchips, pci devices etc.
2 parents ffc2532 + 2c10cda commit ef12ea6

File tree

27 files changed

+4902
-14
lines changed

27 files changed

+4902
-14
lines changed

Documentation/virt/kvm/api.rst

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,13 @@ Reads the general purpose registers from the vcpu.
416416
__u64 pc;
417417
};
418418

419+
/* LoongArch */
420+
struct kvm_regs {
421+
/* out (KVM_GET_REGS) / in (KVM_SET_REGS) */
422+
unsigned long gpr[32];
423+
unsigned long pc;
424+
};
425+
419426

420427
4.12 KVM_SET_REGS
421428
-----------------
@@ -506,7 +513,7 @@ translation mode.
506513
------------------
507514

508515
:Capability: basic
509-
:Architectures: x86, ppc, mips, riscv
516+
:Architectures: x86, ppc, mips, riscv, loongarch
510517
:Type: vcpu ioctl
511518
:Parameters: struct kvm_interrupt (in)
512519
:Returns: 0 on success, negative on failure.
@@ -592,6 +599,14 @@ b) KVM_INTERRUPT_UNSET
592599

593600
This is an asynchronous vcpu ioctl and can be invoked from any thread.
594601

602+
LOONGARCH:
603+
^^^^^^^^^^
604+
605+
Queues an external interrupt to be injected into the virtual CPU. A negative
606+
interrupt number dequeues the interrupt.
607+
608+
This is an asynchronous vcpu ioctl and can be invoked from any thread.
609+
595610

596611
4.17 KVM_DEBUG_GUEST
597612
--------------------
@@ -737,7 +752,7 @@ signal mask.
737752
----------------
738753

739754
:Capability: basic
740-
:Architectures: x86
755+
:Architectures: x86, loongarch
741756
:Type: vcpu ioctl
742757
:Parameters: struct kvm_fpu (out)
743758
:Returns: 0 on success, -1 on error
@@ -746,7 +761,7 @@ Reads the floating point state from the vcpu.
746761

747762
::
748763

749-
/* for KVM_GET_FPU and KVM_SET_FPU */
764+
/* x86: for KVM_GET_FPU and KVM_SET_FPU */
750765
struct kvm_fpu {
751766
__u8 fpr[8][16];
752767
__u16 fcw;
@@ -761,12 +776,21 @@ Reads the floating point state from the vcpu.
761776
__u32 pad2;
762777
};
763778

779+
/* LoongArch: for KVM_GET_FPU and KVM_SET_FPU */
780+
struct kvm_fpu {
781+
__u32 fcsr;
782+
__u64 fcc;
783+
struct kvm_fpureg {
784+
__u64 val64[4];
785+
}fpr[32];
786+
};
787+
764788

765789
4.23 KVM_SET_FPU
766790
----------------
767791

768792
:Capability: basic
769-
:Architectures: x86
793+
:Architectures: x86, loongarch
770794
:Type: vcpu ioctl
771795
:Parameters: struct kvm_fpu (in)
772796
:Returns: 0 on success, -1 on error
@@ -775,7 +799,7 @@ Writes the floating point state to the vcpu.
775799

776800
::
777801

778-
/* for KVM_GET_FPU and KVM_SET_FPU */
802+
/* x86: for KVM_GET_FPU and KVM_SET_FPU */
779803
struct kvm_fpu {
780804
__u8 fpr[8][16];
781805
__u16 fcw;
@@ -790,6 +814,15 @@ Writes the floating point state to the vcpu.
790814
__u32 pad2;
791815
};
792816

817+
/* LoongArch: for KVM_GET_FPU and KVM_SET_FPU */
818+
struct kvm_fpu {
819+
__u32 fcsr;
820+
__u64 fcc;
821+
struct kvm_fpureg {
822+
__u64 val64[4];
823+
}fpr[32];
824+
};
825+
793826

794827
4.24 KVM_CREATE_IRQCHIP
795828
-----------------------
@@ -1387,7 +1420,7 @@ documentation when it pops into existence).
13871420
-------------------
13881421

13891422
:Capability: KVM_CAP_ENABLE_CAP
1390-
:Architectures: mips, ppc, s390, x86
1423+
:Architectures: mips, ppc, s390, x86, loongarch
13911424
:Type: vcpu ioctl
13921425
:Parameters: struct kvm_enable_cap (in)
13931426
:Returns: 0 on success; -1 on error
@@ -1442,7 +1475,7 @@ for vm-wide capabilities.
14421475
---------------------
14431476

14441477
:Capability: KVM_CAP_MP_STATE
1445-
:Architectures: x86, s390, arm64, riscv
1478+
:Architectures: x86, s390, arm64, riscv, loongarch
14461479
:Type: vcpu ioctl
14471480
:Parameters: struct kvm_mp_state (out)
14481481
:Returns: 0 on success; -1 on error
@@ -1460,7 +1493,7 @@ Possible values are:
14601493

14611494
========================== ===============================================
14621495
KVM_MP_STATE_RUNNABLE the vcpu is currently running
1463-
[x86,arm64,riscv]
1496+
[x86,arm64,riscv,loongarch]
14641497
KVM_MP_STATE_UNINITIALIZED the vcpu is an application processor (AP)
14651498
which has not yet received an INIT signal [x86]
14661499
KVM_MP_STATE_INIT_RECEIVED the vcpu has received an INIT signal, and is
@@ -1516,11 +1549,14 @@ For riscv:
15161549
The only states that are valid are KVM_MP_STATE_STOPPED and
15171550
KVM_MP_STATE_RUNNABLE which reflect if the vcpu is paused or not.
15181551

1552+
On LoongArch, only the KVM_MP_STATE_RUNNABLE state is used to reflect
1553+
whether the vcpu is runnable.
1554+
15191555
4.39 KVM_SET_MP_STATE
15201556
---------------------
15211557

15221558
:Capability: KVM_CAP_MP_STATE
1523-
:Architectures: x86, s390, arm64, riscv
1559+
:Architectures: x86, s390, arm64, riscv, loongarch
15241560
:Type: vcpu ioctl
15251561
:Parameters: struct kvm_mp_state (in)
15261562
:Returns: 0 on success; -1 on error
@@ -1538,6 +1574,9 @@ For arm64/riscv:
15381574
The only states that are valid are KVM_MP_STATE_STOPPED and
15391575
KVM_MP_STATE_RUNNABLE which reflect if the vcpu should be paused or not.
15401576

1577+
On LoongArch, only the KVM_MP_STATE_RUNNABLE state is used to reflect
1578+
whether the vcpu is runnable.
1579+
15411580
4.40 KVM_SET_IDENTITY_MAP_ADDR
15421581
------------------------------
15431582

@@ -2841,6 +2880,19 @@ Following are the RISC-V D-extension registers:
28412880
0x8020 0000 0600 0020 fcsr Floating point control and status register
28422881
======================= ========= =============================================
28432882

2883+
LoongArch registers are mapped using the lower 32 bits. The upper 16 bits of
2884+
that is the register group type.
2885+
2886+
LoongArch csr registers are used to control guest cpu or get status of guest
2887+
cpu, and they have the following id bit patterns::
2888+
2889+
0x9030 0000 0001 00 <reg:5> <sel:3> (64-bit)
2890+
2891+
LoongArch KVM control registers are used to implement some new defined functions
2892+
such as set vcpu counter or reset vcpu, and they have the following id bit patterns::
2893+
2894+
0x9030 0000 0002 <reg:16>
2895+
28442896

28452897
4.69 KVM_GET_ONE_REG
28462898
--------------------

MAINTAINERS

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11522,6 +11522,18 @@ F: include/kvm/arm_*
1152211522
F: tools/testing/selftests/kvm/*/aarch64/
1152311523
F: tools/testing/selftests/kvm/aarch64/
1152411524

11525+
KERNEL VIRTUAL MACHINE FOR LOONGARCH (KVM/LoongArch)
11526+
M: Tianrui Zhao <[email protected]>
11527+
M: Bibo Mao <[email protected]>
11528+
M: Huacai Chen <[email protected]>
11529+
11530+
11531+
S: Maintained
11532+
T: git git://git.kernel.org/pub/scm/virt/kvm/kvm.git
11533+
F: arch/loongarch/include/asm/kvm*
11534+
F: arch/loongarch/include/uapi/asm/kvm*
11535+
F: arch/loongarch/kvm/
11536+
1152511537
KERNEL VIRTUAL MACHINE FOR MIPS (KVM/mips)
1152611538
M: Huacai Chen <[email protected]>
1152711539

arch/loongarch/Kbuild

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ obj-y += mm/
33
obj-y += net/
44
obj-y += vdso/
55

6+
obj-$(CONFIG_KVM) += kvm/
7+
68
# for cleaning
79
subdir- += boot

arch/loongarch/Kconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ config LOONGARCH
129129
select HAVE_KPROBES
130130
select HAVE_KPROBES_ON_FTRACE
131131
select HAVE_KRETPROBES
132+
select HAVE_KVM
132133
select HAVE_MOD_ARCH_SPECIFIC
133134
select HAVE_NMI
134135
select HAVE_PCI
@@ -263,6 +264,9 @@ config AS_HAS_LASX_EXTENSION
263264
config AS_HAS_LBT_EXTENSION
264265
def_bool $(as-instr,movscr2gr \$a0$(comma)\$scr0)
265266

267+
config AS_HAS_LVZ_EXTENSION
268+
def_bool $(as-instr,hvcl 0)
269+
266270
menu "Kernel type and options"
267271

268272
source "kernel/Kconfig.hz"
@@ -676,3 +680,5 @@ source "kernel/power/Kconfig"
676680
source "drivers/acpi/Kconfig"
677681

678682
endmenu
683+
684+
source "arch/loongarch/kvm/Kconfig"

arch/loongarch/configs/loongson3_defconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ CONFIG_EFI_ZBOOT=y
6666
CONFIG_EFI_GENERIC_STUB_INITRD_CMDLINE_LOADER=y
6767
CONFIG_EFI_CAPSULE_LOADER=m
6868
CONFIG_EFI_TEST=m
69+
CONFIG_VIRTUALIZATION=y
70+
CONFIG_KVM=m
6971
CONFIG_JUMP_LABEL=y
7072
CONFIG_MODULES=y
7173
CONFIG_MODULE_FORCE_LOAD=y

arch/loongarch/include/asm/inst.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ enum reg2_op {
6565
revbd_op = 0x0f,
6666
revh2w_op = 0x10,
6767
revhd_op = 0x11,
68+
iocsrrdb_op = 0x19200,
69+
iocsrrdh_op = 0x19201,
70+
iocsrrdw_op = 0x19202,
71+
iocsrrdd_op = 0x19203,
72+
iocsrwrb_op = 0x19204,
73+
iocsrwrh_op = 0x19205,
74+
iocsrwrw_op = 0x19206,
75+
iocsrwrd_op = 0x19207,
6876
};
6977

7078
enum reg2i5_op {
@@ -318,6 +326,13 @@ struct reg2bstrd_format {
318326
unsigned int opcode : 10;
319327
};
320328

329+
struct reg2csr_format {
330+
unsigned int rd : 5;
331+
unsigned int rj : 5;
332+
unsigned int csr : 14;
333+
unsigned int opcode : 8;
334+
};
335+
321336
struct reg3_format {
322337
unsigned int rd : 5;
323338
unsigned int rj : 5;
@@ -346,6 +361,7 @@ union loongarch_instruction {
346361
struct reg2i14_format reg2i14_format;
347362
struct reg2i16_format reg2i16_format;
348363
struct reg2bstrd_format reg2bstrd_format;
364+
struct reg2csr_format reg2csr_format;
349365
struct reg3_format reg3_format;
350366
struct reg3sa2_format reg3sa2_format;
351367
};

0 commit comments

Comments
 (0)