xref: /qemu/include/hw/riscv/virt.h (revision 49f95221)
1 /*
2  * QEMU RISC-V VirtIO machine interface
3  *
4  * Copyright (c) 2017 SiFive, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2 or later, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef HW_RISCV_VIRT_H
20 #define HW_RISCV_VIRT_H
21 
22 #include "hw/riscv/riscv_hart.h"
23 #include "hw/sysbus.h"
24 #include "hw/block/flash.h"
25 #include "qom/object.h"
26 
27 #define VIRT_CPUS_MAX_BITS             9
28 #define VIRT_CPUS_MAX                  (1 << VIRT_CPUS_MAX_BITS)
29 #define VIRT_SOCKETS_MAX_BITS          2
30 #define VIRT_SOCKETS_MAX               (1 << VIRT_SOCKETS_MAX_BITS)
31 
32 #define TYPE_RISCV_VIRT_MACHINE MACHINE_TYPE_NAME("virt")
33 typedef struct RISCVVirtState RISCVVirtState;
34 DECLARE_INSTANCE_CHECKER(RISCVVirtState, RISCV_VIRT_MACHINE,
35                          TYPE_RISCV_VIRT_MACHINE)
36 
37 typedef enum RISCVVirtAIAType {
38     VIRT_AIA_TYPE_NONE = 0,
39     VIRT_AIA_TYPE_APLIC,
40     VIRT_AIA_TYPE_APLIC_IMSIC,
41 } RISCVVirtAIAType;
42 
43 struct RISCVVirtState {
44     /*< private >*/
45     MachineState parent;
46 
47     /*< public >*/
48     RISCVHartArrayState soc[VIRT_SOCKETS_MAX];
49     DeviceState *irqchip[VIRT_SOCKETS_MAX];
50     PFlashCFI01 *flash[2];
51     FWCfgState *fw_cfg;
52 
53     int fdt_size;
54     bool have_aclint;
55     RISCVVirtAIAType aia_type;
56     int aia_guests;
57 };
58 
59 enum {
60     VIRT_DEBUG,
61     VIRT_MROM,
62     VIRT_TEST,
63     VIRT_RTC,
64     VIRT_CLINT,
65     VIRT_ACLINT_SSWI,
66     VIRT_PLIC,
67     VIRT_APLIC_M,
68     VIRT_APLIC_S,
69     VIRT_UART0,
70     VIRT_VIRTIO,
71     VIRT_FW_CFG,
72     VIRT_IMSIC_M,
73     VIRT_IMSIC_S,
74     VIRT_FLASH,
75     VIRT_DRAM,
76     VIRT_PCIE_MMIO,
77     VIRT_PCIE_PIO,
78     VIRT_PCIE_ECAM
79 };
80 
81 enum {
82     UART0_IRQ = 10,
83     RTC_IRQ = 11,
84     VIRTIO_IRQ = 1, /* 1 to 8 */
85     VIRTIO_COUNT = 8,
86     PCIE_IRQ = 0x20, /* 32 to 35 */
87     VIRTIO_NDEV = 0x35 /* Arbitrary maximum number of interrupts */
88 };
89 
90 #define VIRT_IRQCHIP_IPI_MSI 1
91 #define VIRT_IRQCHIP_NUM_MSIS 255
92 #define VIRT_IRQCHIP_NUM_SOURCES VIRTIO_NDEV
93 #define VIRT_IRQCHIP_NUM_PRIO_BITS 3
94 #define VIRT_IRQCHIP_MAX_GUESTS_BITS 3
95 #define VIRT_IRQCHIP_MAX_GUESTS ((1U << VIRT_IRQCHIP_MAX_GUESTS_BITS) - 1U)
96 
97 #define VIRT_PLIC_PRIORITY_BASE 0x04
98 #define VIRT_PLIC_PENDING_BASE 0x1000
99 #define VIRT_PLIC_ENABLE_BASE 0x2000
100 #define VIRT_PLIC_ENABLE_STRIDE 0x80
101 #define VIRT_PLIC_CONTEXT_BASE 0x200000
102 #define VIRT_PLIC_CONTEXT_STRIDE 0x1000
103 #define VIRT_PLIC_SIZE(__num_context) \
104     (VIRT_PLIC_CONTEXT_BASE + (__num_context) * VIRT_PLIC_CONTEXT_STRIDE)
105 
106 #define FDT_PCI_ADDR_CELLS    3
107 #define FDT_PCI_INT_CELLS     1
108 #define FDT_PLIC_INT_CELLS    1
109 #define FDT_APLIC_INT_CELLS   2
110 #define FDT_IMSIC_INT_CELLS   0
111 #define FDT_MAX_INT_CELLS     2
112 #define FDT_MAX_INT_MAP_WIDTH (FDT_PCI_ADDR_CELLS + FDT_PCI_INT_CELLS + \
113                                  1 + FDT_MAX_INT_CELLS)
114 #define FDT_PLIC_INT_MAP_WIDTH  (FDT_PCI_ADDR_CELLS + FDT_PCI_INT_CELLS + \
115                                  1 + FDT_PLIC_INT_CELLS)
116 #define FDT_APLIC_INT_MAP_WIDTH (FDT_PCI_ADDR_CELLS + FDT_PCI_INT_CELLS + \
117                                  1 + FDT_APLIC_INT_CELLS)
118 
119 #endif
120