1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28#include "qemu/osdep.h"
29#include "qapi/error.h"
30#include "hw/sysbus.h"
31#include "hw/irq.h"
32#include "hw/pci/msi.h"
33#include "hw/qdev-properties.h"
34#include "sysemu/kvm.h"
35#include "qemu/log.h"
36#include "qemu/module.h"
37#include "qom/object.h"
38
39#define TYPE_ARM_GICV2M "arm-gicv2m"
40OBJECT_DECLARE_SIMPLE_TYPE(ARMGICv2mState, ARM_GICV2M)
41
42#define GICV2M_NUM_SPI_MAX 128
43
44#define V2M_MSI_TYPER 0x008
45#define V2M_MSI_SETSPI_NS 0x040
46#define V2M_MSI_IIDR 0xFCC
47#define V2M_IIDR0 0xFD0
48#define V2M_IIDR11 0xFFC
49
50#define PRODUCT_ID_QEMU 0x51
51
52struct ARMGICv2mState {
53 SysBusDevice parent_obj;
54
55 MemoryRegion iomem;
56 qemu_irq spi[GICV2M_NUM_SPI_MAX];
57
58 uint32_t base_spi;
59 uint32_t num_spi;
60};
61
62static void gicv2m_set_irq(void *opaque, int irq)
63{
64 ARMGICv2mState *s = (ARMGICv2mState *)opaque;
65
66 qemu_irq_pulse(s->spi[irq]);
67}
68
69static uint64_t gicv2m_read(void *opaque, hwaddr offset,
70 unsigned size)
71{
72 ARMGICv2mState *s = (ARMGICv2mState *)opaque;
73 uint32_t val;
74
75 if (size != 4) {
76 qemu_log_mask(LOG_GUEST_ERROR, "gicv2m_read: bad size %u\n", size);
77 return 0;
78 }
79
80 switch (offset) {
81 case V2M_MSI_TYPER:
82 val = (s->base_spi + 32) << 16;
83 val |= s->num_spi;
84 return val;
85 case V2M_MSI_IIDR:
86
87
88
89 return (PRODUCT_ID_QEMU << 20);
90 case V2M_IIDR0 ... V2M_IIDR11:
91
92
93
94
95 return 0;
96 default:
97 qemu_log_mask(LOG_GUEST_ERROR,
98 "gicv2m_read: Bad offset %x\n", (int)offset);
99 return 0;
100 }
101}
102
103static void gicv2m_write(void *opaque, hwaddr offset,
104 uint64_t value, unsigned size)
105{
106 ARMGICv2mState *s = (ARMGICv2mState *)opaque;
107
108 if (size != 2 && size != 4) {
109 qemu_log_mask(LOG_GUEST_ERROR, "gicv2m_write: bad size %u\n", size);
110 return;
111 }
112
113 switch (offset) {
114 case V2M_MSI_SETSPI_NS: {
115 int spi;
116
117 spi = (value & 0x3ff) - (s->base_spi + 32);
118 if (spi >= 0 && spi < s->num_spi) {
119 gicv2m_set_irq(s, spi);
120 }
121 return;
122 }
123 default:
124 qemu_log_mask(LOG_GUEST_ERROR,
125 "gicv2m_write: Bad offset %x\n", (int)offset);
126 }
127}
128
129static const MemoryRegionOps gicv2m_ops = {
130 .read = gicv2m_read,
131 .write = gicv2m_write,
132 .endianness = DEVICE_LITTLE_ENDIAN,
133};
134
135static void gicv2m_realize(DeviceState *dev, Error **errp)
136{
137 ARMGICv2mState *s = ARM_GICV2M(dev);
138 int i;
139
140 if (s->num_spi > GICV2M_NUM_SPI_MAX) {
141 error_setg(errp,
142 "requested %u SPIs exceeds GICv2m frame maximum %d",
143 s->num_spi, GICV2M_NUM_SPI_MAX);
144 return;
145 }
146
147 if (s->base_spi + 32 > 1020 - s->num_spi) {
148 error_setg(errp,
149 "requested base SPI %u+%u exceeds max. number 1020",
150 s->base_spi + 32, s->num_spi);
151 return;
152 }
153
154 for (i = 0; i < s->num_spi; i++) {
155 sysbus_init_irq(SYS_BUS_DEVICE(dev), &s->spi[i]);
156 }
157
158 msi_nonbroken = true;
159 kvm_gsi_direct_mapping = true;
160 kvm_msi_via_irqfd_allowed = kvm_irqfds_enabled();
161}
162
163static void gicv2m_init(Object *obj)
164{
165 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
166 ARMGICv2mState *s = ARM_GICV2M(obj);
167
168 memory_region_init_io(&s->iomem, OBJECT(s), &gicv2m_ops, s,
169 "gicv2m", 0x1000);
170 sysbus_init_mmio(sbd, &s->iomem);
171}
172
173static Property gicv2m_properties[] = {
174 DEFINE_PROP_UINT32("base-spi", ARMGICv2mState, base_spi, 0),
175 DEFINE_PROP_UINT32("num-spi", ARMGICv2mState, num_spi, 64),
176 DEFINE_PROP_END_OF_LIST(),
177};
178
179static void gicv2m_class_init(ObjectClass *klass, void *data)
180{
181 DeviceClass *dc = DEVICE_CLASS(klass);
182
183 device_class_set_props(dc, gicv2m_properties);
184 dc->realize = gicv2m_realize;
185}
186
187static const TypeInfo gicv2m_info = {
188 .name = TYPE_ARM_GICV2M,
189 .parent = TYPE_SYS_BUS_DEVICE,
190 .instance_size = sizeof(ARMGICv2mState),
191 .instance_init = gicv2m_init,
192 .class_init = gicv2m_class_init,
193};
194
195static void gicv2m_register_types(void)
196{
197 type_register_static(&gicv2m_info);
198}
199
200type_init(gicv2m_register_types)
201