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_IRQCHIP 0x08000000ULL
14 #define RISCV_IRQCHIP_SIZE SZ_128M
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_RING_ENDIAN VIRTIO_ENDIAN_LE
56
57 #define ARCH_HAS_PCI_EXP 1
58
59 struct kvm;
60
61 struct kvm_arch {
62 /*
63 * We may have to align the guest memory for virtio, so keep the
64 * original pointers here for munmap.
65 */
66 void *ram_alloc_start;
67 u64 ram_alloc_size;
68
69 /*
70 * Guest addresses for memory layout.
71 */
72 u64 memory_guest_start;
73 u64 kern_guest_start;
74 u64 initrd_guest_start;
75 u64 initrd_size;
76 u64 dtb_guest_start;
77 };
78
riscv_addr_in_ioport_region(u64 phys_addr)79 static inline bool riscv_addr_in_ioport_region(u64 phys_addr)
80 {
81 u64 limit = KVM_IOPORT_AREA + RISCV_IOPORT_SIZE;
82 return phys_addr >= KVM_IOPORT_AREA && phys_addr < limit;
83 }
84
85 enum irq_type;
86
87 enum irqchip_type {
88 IRQCHIP_UNKNOWN = 0,
89 IRQCHIP_PLIC,
90 IRQCHIP_AIA
91 };
92
93 extern enum irqchip_type riscv_irqchip;
94 extern bool riscv_irqchip_inkernel;
95 extern void (*riscv_irqchip_trigger)(struct kvm *kvm, int irq,
96 int level, bool edge);
97 extern void (*riscv_irqchip_generate_fdt_node)(void *fdt, struct kvm *kvm);
98 extern u32 riscv_irqchip_phandle;
99 extern u32 riscv_irqchip_msi_phandle;
100 extern bool riscv_irqchip_line_sensing;
101 extern bool riscv_irqchip_irqfd_ready;
102
103 void aia__create(struct kvm *kvm);
104 void plic__create(struct kvm *kvm);
105
106 void pci__generate_fdt_nodes(void *fdt);
107
108 int riscv__add_irqfd(struct kvm *kvm, unsigned int gsi, int trigger_fd,
109 int resample_fd);
110
111 void riscv__del_irqfd(struct kvm *kvm, unsigned int gsi, int trigger_fd);
112
113 #define irq__add_irqfd riscv__add_irqfd
114 #define irq__del_irqfd riscv__del_irqfd
115
116 int riscv__setup_irqfd_lines(struct kvm *kvm);
117
118 void riscv__generate_irq_prop(void *fdt, u8 irq, enum irq_type irq_type);
119
120 void riscv__irqchip_create(struct kvm *kvm);
121
122 #endif /* KVM__KVM_ARCH_H */
123