]>
Commit | Line | Data |
---|---|---|
770c58f8 CD |
1 | /* |
2 | * GICv2m extension for MSI/MSI-x support with a GICv2-based system | |
3 | * | |
4 | * Copyright (C) 2015 Linaro, All rights reserved. | |
5 | * | |
6 | * Author: Christoffer Dall <christoffer.dall@linaro.org> | |
7 | * | |
8 | * This library is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License as published by the Free Software Foundation; either | |
11 | * version 2 of the License, or (at your option) any later version. | |
12 | * | |
13 | * This library is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * Lesser General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU Lesser General Public | |
19 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. | |
20 | */ | |
21 | ||
22 | /* This file implements an emulated GICv2m widget as described in the ARM | |
23 | * Server Base System Architecture (SBSA) specification Version 2.2 | |
24 | * (ARM-DEN-0029 v2.2) pages 35-39 without any optional implementation defined | |
25 | * identification registers and with a single non-secure MSI register frame. | |
26 | */ | |
27 | ||
8ef94f0b | 28 | #include "qemu/osdep.h" |
da34e65c | 29 | #include "qapi/error.h" |
770c58f8 | 30 | #include "hw/sysbus.h" |
64552b6b | 31 | #include "hw/irq.h" |
770c58f8 | 32 | #include "hw/pci/msi.h" |
a27bd6c7 | 33 | #include "hw/qdev-properties.h" |
77ac58dd | 34 | #include "sysemu/kvm.h" |
03dd024f | 35 | #include "qemu/log.h" |
0b8fa32f | 36 | #include "qemu/module.h" |
db1015e9 | 37 | #include "qom/object.h" |
770c58f8 CD |
38 | |
39 | #define TYPE_ARM_GICV2M "arm-gicv2m" | |
db1015e9 | 40 | typedef struct ARMGICv2mState ARMGICv2mState; |
770c58f8 CD |
41 | #define ARM_GICV2M(obj) OBJECT_CHECK(ARMGICv2mState, (obj), TYPE_ARM_GICV2M) |
42 | ||
43 | #define GICV2M_NUM_SPI_MAX 128 | |
44 | ||
45 | #define V2M_MSI_TYPER 0x008 | |
46 | #define V2M_MSI_SETSPI_NS 0x040 | |
47 | #define V2M_MSI_IIDR 0xFCC | |
48 | #define V2M_IIDR0 0xFD0 | |
49 | #define V2M_IIDR11 0xFFC | |
50 | ||
51 | #define PRODUCT_ID_QEMU 0x51 /* ASCII code Q */ | |
52 | ||
db1015e9 | 53 | struct ARMGICv2mState { |
770c58f8 CD |
54 | SysBusDevice parent_obj; |
55 | ||
56 | MemoryRegion iomem; | |
57 | qemu_irq spi[GICV2M_NUM_SPI_MAX]; | |
58 | ||
59 | uint32_t base_spi; | |
60 | uint32_t num_spi; | |
db1015e9 | 61 | }; |
770c58f8 CD |
62 | |
63 | static void gicv2m_set_irq(void *opaque, int irq) | |
64 | { | |
65 | ARMGICv2mState *s = (ARMGICv2mState *)opaque; | |
66 | ||
67 | qemu_irq_pulse(s->spi[irq]); | |
68 | } | |
69 | ||
70 | static uint64_t gicv2m_read(void *opaque, hwaddr offset, | |
71 | unsigned size) | |
72 | { | |
73 | ARMGICv2mState *s = (ARMGICv2mState *)opaque; | |
74 | uint32_t val; | |
75 | ||
76 | if (size != 4) { | |
77 | qemu_log_mask(LOG_GUEST_ERROR, "gicv2m_read: bad size %u\n", size); | |
78 | return 0; | |
79 | } | |
80 | ||
81 | switch (offset) { | |
82 | case V2M_MSI_TYPER: | |
83 | val = (s->base_spi + 32) << 16; | |
84 | val |= s->num_spi; | |
85 | return val; | |
86 | case V2M_MSI_IIDR: | |
87 | /* We don't have any valid implementor so we leave that field as zero | |
88 | * and we return 0 in the arch revision as per the spec. | |
89 | */ | |
90 | return (PRODUCT_ID_QEMU << 20); | |
91 | case V2M_IIDR0 ... V2M_IIDR11: | |
92 | /* We do not implement any optional identification registers and the | |
93 | * mandatory MSI_PIDR2 register reads as 0x0, so we capture all | |
94 | * implementation defined registers here. | |
95 | */ | |
96 | return 0; | |
97 | default: | |
98 | qemu_log_mask(LOG_GUEST_ERROR, | |
99 | "gicv2m_read: Bad offset %x\n", (int)offset); | |
100 | return 0; | |
101 | } | |
102 | } | |
103 | ||
104 | static void gicv2m_write(void *opaque, hwaddr offset, | |
105 | uint64_t value, unsigned size) | |
106 | { | |
107 | ARMGICv2mState *s = (ARMGICv2mState *)opaque; | |
108 | ||
109 | if (size != 2 && size != 4) { | |
110 | qemu_log_mask(LOG_GUEST_ERROR, "gicv2m_write: bad size %u\n", size); | |
111 | return; | |
112 | } | |
113 | ||
114 | switch (offset) { | |
115 | case V2M_MSI_SETSPI_NS: { | |
116 | int spi; | |
117 | ||
118 | spi = (value & 0x3ff) - (s->base_spi + 32); | |
119 | if (spi >= 0 && spi < s->num_spi) { | |
120 | gicv2m_set_irq(s, spi); | |
121 | } | |
122 | return; | |
123 | } | |
124 | default: | |
125 | qemu_log_mask(LOG_GUEST_ERROR, | |
126 | "gicv2m_write: Bad offset %x\n", (int)offset); | |
127 | } | |
128 | } | |
129 | ||
130 | static const MemoryRegionOps gicv2m_ops = { | |
131 | .read = gicv2m_read, | |
132 | .write = gicv2m_write, | |
133 | .endianness = DEVICE_LITTLE_ENDIAN, | |
134 | }; | |
135 | ||
136 | static void gicv2m_realize(DeviceState *dev, Error **errp) | |
137 | { | |
138 | ARMGICv2mState *s = ARM_GICV2M(dev); | |
139 | int i; | |
140 | ||
141 | if (s->num_spi > GICV2M_NUM_SPI_MAX) { | |
142 | error_setg(errp, | |
143 | "requested %u SPIs exceeds GICv2m frame maximum %d", | |
144 | s->num_spi, GICV2M_NUM_SPI_MAX); | |
145 | return; | |
146 | } | |
147 | ||
148 | if (s->base_spi + 32 > 1020 - s->num_spi) { | |
149 | error_setg(errp, | |
150 | "requested base SPI %u+%u exceeds max. number 1020", | |
151 | s->base_spi + 32, s->num_spi); | |
152 | return; | |
153 | } | |
154 | ||
155 | for (i = 0; i < s->num_spi; i++) { | |
156 | sysbus_init_irq(SYS_BUS_DEVICE(dev), &s->spi[i]); | |
157 | } | |
158 | ||
226419d6 | 159 | msi_nonbroken = true; |
9718e4ae EA |
160 | kvm_gsi_direct_mapping = true; |
161 | kvm_msi_via_irqfd_allowed = kvm_irqfds_enabled(); | |
770c58f8 CD |
162 | } |
163 | ||
164 | static void gicv2m_init(Object *obj) | |
165 | { | |
166 | SysBusDevice *sbd = SYS_BUS_DEVICE(obj); | |
167 | ARMGICv2mState *s = ARM_GICV2M(obj); | |
168 | ||
169 | memory_region_init_io(&s->iomem, OBJECT(s), &gicv2m_ops, s, | |
170 | "gicv2m", 0x1000); | |
171 | sysbus_init_mmio(sbd, &s->iomem); | |
172 | } | |
173 | ||
174 | static Property gicv2m_properties[] = { | |
175 | DEFINE_PROP_UINT32("base-spi", ARMGICv2mState, base_spi, 0), | |
176 | DEFINE_PROP_UINT32("num-spi", ARMGICv2mState, num_spi, 64), | |
177 | DEFINE_PROP_END_OF_LIST(), | |
178 | }; | |
179 | ||
180 | static void gicv2m_class_init(ObjectClass *klass, void *data) | |
181 | { | |
182 | DeviceClass *dc = DEVICE_CLASS(klass); | |
183 | ||
4f67d30b | 184 | device_class_set_props(dc, gicv2m_properties); |
770c58f8 CD |
185 | dc->realize = gicv2m_realize; |
186 | } | |
187 | ||
188 | static const TypeInfo gicv2m_info = { | |
189 | .name = TYPE_ARM_GICV2M, | |
190 | .parent = TYPE_SYS_BUS_DEVICE, | |
191 | .instance_size = sizeof(ARMGICv2mState), | |
192 | .instance_init = gicv2m_init, | |
193 | .class_init = gicv2m_class_init, | |
194 | }; | |
195 | ||
196 | static void gicv2m_register_types(void) | |
197 | { | |
198 | type_register_static(&gicv2m_info); | |
199 | } | |
200 | ||
201 | type_init(gicv2m_register_types) |