xref: /kvmtool/riscv/include/kvm/kvm-arch.h (revision 2f030d283c0e7a417f50cf0d70d076870815f3e6)
1 #ifndef KVM__KVM_ARCH_H
2 #define KVM__KVM_ARCH_H
3 
4 #include <stdbool.h>
5 #include <linux/const.h>
6 #include <linux/sizes.h>
7 #include <linux/types.h>
8 
9 #define MAX_PAGE_SIZE		SZ_4K
10 
11 #define RISCV_IOPORT		0x00000000ULL
12 #define RISCV_IOPORT_SIZE	SZ_64K
13 #define RISCV_PLIC		0x0c000000ULL
14 #define RISCV_PLIC_SIZE		SZ_64M
15 #define RISCV_MMIO		0x10000000ULL
16 #define RISCV_MMIO_SIZE		SZ_512M
17 #define RISCV_PCI		0x30000000ULL
18 /*
19  * KVMTOOL emulates legacy PCI config space with 24bits device address
20  * so 16M is sufficient but we reserve 256M to keep it future ready for
21  * PCIe config space with 28bits device address.
22  */
23 #define RISCV_PCI_CFG_SIZE	SZ_256M
24 #define RISCV_PCI_MMIO_SIZE	SZ_1G
25 #define RISCV_PCI_SIZE		(RISCV_PCI_CFG_SIZE + RISCV_PCI_MMIO_SIZE)
26 
27 #define RISCV_RAM		0x80000000ULL
28 
29 #define RISCV_LOMAP_MAX_MEMORY	((1ULL << 32) - RISCV_RAM)
30 #define RISCV_HIMAP_MAX_MEMORY	((1ULL << 40) - RISCV_RAM)
31 
32 #if __riscv_xlen == 64
33 #define RISCV_MAX_MEMORY(kvm)	RISCV_HIMAP_MAX_MEMORY
34 #elif __riscv_xlen == 32
35 #define RISCV_MAX_MEMORY(kvm)	RISCV_LOMAP_MAX_MEMORY
36 #endif
37 
38 #define RISCV_UART_MMIO_BASE	RISCV_MMIO
39 #define RISCV_UART_MMIO_SIZE	0x10000
40 
41 #define RISCV_RTC_MMIO_BASE	(RISCV_UART_MMIO_BASE + RISCV_UART_MMIO_SIZE)
42 #define RISCV_RTC_MMIO_SIZE	0x10000
43 
44 #define KVM_IOPORT_AREA		RISCV_IOPORT
45 #define KVM_PCI_CFG_AREA	RISCV_PCI
46 #define KVM_PCI_MMIO_AREA	(KVM_PCI_CFG_AREA + RISCV_PCI_CFG_SIZE)
47 #define KVM_VIRTIO_MMIO_AREA	(RISCV_RTC_MMIO_BASE + RISCV_RTC_MMIO_SIZE)
48 
49 #define KVM_IOEVENTFD_HAS_PIO	0
50 
51 #define KVM_IRQ_OFFSET		1
52 
53 #define KVM_VM_TYPE		0
54 
55 #define VIRTIO_DEFAULT_TRANS(kvm) \
56 	((kvm)->cfg.virtio_legacy ? VIRTIO_MMIO_LEGACY : VIRTIO_MMIO)
57 
58 #define VIRTIO_RING_ENDIAN	VIRTIO_ENDIAN_LE
59 
60 #define ARCH_HAS_PCI_EXP	1
61 
62 struct kvm;
63 
64 struct kvm_arch {
65 	/*
66 	 * We may have to align the guest memory for virtio, so keep the
67 	 * original pointers here for munmap.
68 	 */
69 	void	*ram_alloc_start;
70 	u64	ram_alloc_size;
71 
72 	/*
73 	 * Guest addresses for memory layout.
74 	 */
75 	u64	memory_guest_start;
76 	u64	kern_guest_start;
77 	u64	initrd_guest_start;
78 	u64	initrd_size;
79 	u64	dtb_guest_start;
80 };
81 
82 static inline bool riscv_addr_in_ioport_region(u64 phys_addr)
83 {
84 	u64 limit = KVM_IOPORT_AREA + RISCV_IOPORT_SIZE;
85 	return phys_addr >= KVM_IOPORT_AREA && phys_addr < limit;
86 }
87 
88 enum irq_type;
89 
90 void plic__generate_irq_prop(void *fdt, u8 irq, enum irq_type irq_type);
91 
92 void plic__irq_trig(struct kvm *kvm, int irq, int level, bool edge);
93 
94 void pci__generate_fdt_nodes(void *fdt);
95 
96 #endif /* KVM__KVM_ARCH_H */
97