xref: /openbmc/qemu/hw/intc/arm_gicv2m.c (revision cb5ed407a1ddadf788fd373fed41c87c9e81e5b0)
1770c58f8SChristoffer Dall /*
2770c58f8SChristoffer Dall  *  GICv2m extension for MSI/MSI-x support with a GICv2-based system
3770c58f8SChristoffer Dall  *
4770c58f8SChristoffer Dall  * Copyright (C) 2015 Linaro, All rights reserved.
5770c58f8SChristoffer Dall  *
6770c58f8SChristoffer Dall  * Author: Christoffer Dall <christoffer.dall@linaro.org>
7770c58f8SChristoffer Dall  *
8770c58f8SChristoffer Dall  * This library is free software; you can redistribute it and/or
9770c58f8SChristoffer Dall  * modify it under the terms of the GNU Lesser General Public
10770c58f8SChristoffer Dall  * License as published by the Free Software Foundation; either
11*50f57e09SChetan Pant  * version 2.1 of the License, or (at your option) any later version.
12770c58f8SChristoffer Dall  *
13770c58f8SChristoffer Dall  * This library is distributed in the hope that it will be useful,
14770c58f8SChristoffer Dall  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15770c58f8SChristoffer Dall  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16770c58f8SChristoffer Dall  * Lesser General Public License for more details.
17770c58f8SChristoffer Dall  *
18770c58f8SChristoffer Dall  * You should have received a copy of the GNU Lesser General Public
19770c58f8SChristoffer Dall  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20770c58f8SChristoffer Dall  */
21770c58f8SChristoffer Dall 
22770c58f8SChristoffer Dall /* This file implements an emulated GICv2m widget as described in the ARM
23770c58f8SChristoffer Dall  * Server Base System Architecture (SBSA) specification Version 2.2
24770c58f8SChristoffer Dall  * (ARM-DEN-0029 v2.2) pages 35-39 without any optional implementation defined
25770c58f8SChristoffer Dall  * identification registers and with a single non-secure MSI register frame.
26770c58f8SChristoffer Dall  */
27770c58f8SChristoffer Dall 
288ef94f0bSPeter Maydell #include "qemu/osdep.h"
29da34e65cSMarkus Armbruster #include "qapi/error.h"
30770c58f8SChristoffer Dall #include "hw/sysbus.h"
3164552b6bSMarkus Armbruster #include "hw/irq.h"
32770c58f8SChristoffer Dall #include "hw/pci/msi.h"
33a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
3477ac58ddSPaolo Bonzini #include "sysemu/kvm.h"
3503dd024fSPaolo Bonzini #include "qemu/log.h"
360b8fa32fSMarkus Armbruster #include "qemu/module.h"
37db1015e9SEduardo Habkost #include "qom/object.h"
38770c58f8SChristoffer Dall 
39770c58f8SChristoffer Dall #define TYPE_ARM_GICV2M "arm-gicv2m"
408063396bSEduardo Habkost OBJECT_DECLARE_SIMPLE_TYPE(ARMGICv2mState, ARM_GICV2M)
41770c58f8SChristoffer Dall 
42770c58f8SChristoffer Dall #define GICV2M_NUM_SPI_MAX 128
43770c58f8SChristoffer Dall 
44770c58f8SChristoffer Dall #define V2M_MSI_TYPER           0x008
45770c58f8SChristoffer Dall #define V2M_MSI_SETSPI_NS       0x040
46770c58f8SChristoffer Dall #define V2M_MSI_IIDR            0xFCC
47770c58f8SChristoffer Dall #define V2M_IIDR0               0xFD0
48770c58f8SChristoffer Dall #define V2M_IIDR11              0xFFC
49770c58f8SChristoffer Dall 
50770c58f8SChristoffer Dall #define PRODUCT_ID_QEMU         0x51 /* ASCII code Q */
51770c58f8SChristoffer Dall 
52db1015e9SEduardo Habkost struct ARMGICv2mState {
53770c58f8SChristoffer Dall     SysBusDevice parent_obj;
54770c58f8SChristoffer Dall 
55770c58f8SChristoffer Dall     MemoryRegion iomem;
56770c58f8SChristoffer Dall     qemu_irq spi[GICV2M_NUM_SPI_MAX];
57770c58f8SChristoffer Dall 
58770c58f8SChristoffer Dall     uint32_t base_spi;
59770c58f8SChristoffer Dall     uint32_t num_spi;
60db1015e9SEduardo Habkost };
61770c58f8SChristoffer Dall 
gicv2m_set_irq(void * opaque,int irq)62770c58f8SChristoffer Dall static void gicv2m_set_irq(void *opaque, int irq)
63770c58f8SChristoffer Dall {
64770c58f8SChristoffer Dall     ARMGICv2mState *s = (ARMGICv2mState *)opaque;
65770c58f8SChristoffer Dall 
66770c58f8SChristoffer Dall     qemu_irq_pulse(s->spi[irq]);
67770c58f8SChristoffer Dall }
68770c58f8SChristoffer Dall 
gicv2m_read(void * opaque,hwaddr offset,unsigned size)69770c58f8SChristoffer Dall static uint64_t gicv2m_read(void *opaque, hwaddr offset,
70770c58f8SChristoffer Dall                             unsigned size)
71770c58f8SChristoffer Dall {
72770c58f8SChristoffer Dall     ARMGICv2mState *s = (ARMGICv2mState *)opaque;
73770c58f8SChristoffer Dall     uint32_t val;
74770c58f8SChristoffer Dall 
75770c58f8SChristoffer Dall     if (size != 4) {
76770c58f8SChristoffer Dall         qemu_log_mask(LOG_GUEST_ERROR, "gicv2m_read: bad size %u\n", size);
77770c58f8SChristoffer Dall         return 0;
78770c58f8SChristoffer Dall     }
79770c58f8SChristoffer Dall 
80770c58f8SChristoffer Dall     switch (offset) {
81770c58f8SChristoffer Dall     case V2M_MSI_TYPER:
82770c58f8SChristoffer Dall         val = (s->base_spi + 32) << 16;
83770c58f8SChristoffer Dall         val |= s->num_spi;
84770c58f8SChristoffer Dall         return val;
85770c58f8SChristoffer Dall     case V2M_MSI_IIDR:
86770c58f8SChristoffer Dall         /* We don't have any valid implementor so we leave that field as zero
87770c58f8SChristoffer Dall          * and we return 0 in the arch revision as per the spec.
88770c58f8SChristoffer Dall          */
89770c58f8SChristoffer Dall         return (PRODUCT_ID_QEMU << 20);
90770c58f8SChristoffer Dall     case V2M_IIDR0 ... V2M_IIDR11:
91770c58f8SChristoffer Dall         /* We do not implement any optional identification registers and the
92770c58f8SChristoffer Dall          * mandatory MSI_PIDR2 register reads as 0x0, so we capture all
93770c58f8SChristoffer Dall          * implementation defined registers here.
94770c58f8SChristoffer Dall          */
95770c58f8SChristoffer Dall         return 0;
96770c58f8SChristoffer Dall     default:
97770c58f8SChristoffer Dall         qemu_log_mask(LOG_GUEST_ERROR,
98770c58f8SChristoffer Dall                       "gicv2m_read: Bad offset %x\n", (int)offset);
99770c58f8SChristoffer Dall         return 0;
100770c58f8SChristoffer Dall     }
101770c58f8SChristoffer Dall }
102770c58f8SChristoffer Dall 
gicv2m_write(void * opaque,hwaddr offset,uint64_t value,unsigned size)103770c58f8SChristoffer Dall static void gicv2m_write(void *opaque, hwaddr offset,
104770c58f8SChristoffer Dall                         uint64_t value, unsigned size)
105770c58f8SChristoffer Dall {
106770c58f8SChristoffer Dall     ARMGICv2mState *s = (ARMGICv2mState *)opaque;
107770c58f8SChristoffer Dall 
108770c58f8SChristoffer Dall     if (size != 2 && size != 4) {
109770c58f8SChristoffer Dall         qemu_log_mask(LOG_GUEST_ERROR, "gicv2m_write: bad size %u\n", size);
110770c58f8SChristoffer Dall         return;
111770c58f8SChristoffer Dall     }
112770c58f8SChristoffer Dall 
113770c58f8SChristoffer Dall     switch (offset) {
114770c58f8SChristoffer Dall     case V2M_MSI_SETSPI_NS: {
115770c58f8SChristoffer Dall         int spi;
116770c58f8SChristoffer Dall 
117770c58f8SChristoffer Dall         spi = (value & 0x3ff) - (s->base_spi + 32);
118770c58f8SChristoffer Dall         if (spi >= 0 && spi < s->num_spi) {
119770c58f8SChristoffer Dall             gicv2m_set_irq(s, spi);
120770c58f8SChristoffer Dall         }
121770c58f8SChristoffer Dall         return;
122770c58f8SChristoffer Dall     }
123770c58f8SChristoffer Dall     default:
124770c58f8SChristoffer Dall         qemu_log_mask(LOG_GUEST_ERROR,
125770c58f8SChristoffer Dall                       "gicv2m_write: Bad offset %x\n", (int)offset);
126770c58f8SChristoffer Dall     }
127770c58f8SChristoffer Dall }
128770c58f8SChristoffer Dall 
129770c58f8SChristoffer Dall static const MemoryRegionOps gicv2m_ops = {
130770c58f8SChristoffer Dall     .read = gicv2m_read,
131770c58f8SChristoffer Dall     .write = gicv2m_write,
132770c58f8SChristoffer Dall     .endianness = DEVICE_LITTLE_ENDIAN,
133770c58f8SChristoffer Dall };
134770c58f8SChristoffer Dall 
gicv2m_realize(DeviceState * dev,Error ** errp)135770c58f8SChristoffer Dall static void gicv2m_realize(DeviceState *dev, Error **errp)
136770c58f8SChristoffer Dall {
137770c58f8SChristoffer Dall     ARMGICv2mState *s = ARM_GICV2M(dev);
138770c58f8SChristoffer Dall     int i;
139770c58f8SChristoffer Dall 
140770c58f8SChristoffer Dall     if (s->num_spi > GICV2M_NUM_SPI_MAX) {
141770c58f8SChristoffer Dall         error_setg(errp,
142770c58f8SChristoffer Dall                    "requested %u SPIs exceeds GICv2m frame maximum %d",
143770c58f8SChristoffer Dall                    s->num_spi, GICV2M_NUM_SPI_MAX);
144770c58f8SChristoffer Dall         return;
145770c58f8SChristoffer Dall     }
146770c58f8SChristoffer Dall 
147770c58f8SChristoffer Dall     if (s->base_spi + 32 > 1020 - s->num_spi) {
148770c58f8SChristoffer Dall         error_setg(errp,
149770c58f8SChristoffer Dall                    "requested base SPI %u+%u exceeds max. number 1020",
150770c58f8SChristoffer Dall                    s->base_spi + 32, s->num_spi);
151770c58f8SChristoffer Dall         return;
152770c58f8SChristoffer Dall     }
153770c58f8SChristoffer Dall 
154770c58f8SChristoffer Dall     for (i = 0; i < s->num_spi; i++) {
155770c58f8SChristoffer Dall         sysbus_init_irq(SYS_BUS_DEVICE(dev), &s->spi[i]);
156770c58f8SChristoffer Dall     }
157770c58f8SChristoffer Dall 
158226419d6SMichael S. Tsirkin     msi_nonbroken = true;
1599718e4aeSEric Auger     kvm_gsi_direct_mapping = true;
1609718e4aeSEric Auger     kvm_msi_via_irqfd_allowed = kvm_irqfds_enabled();
161770c58f8SChristoffer Dall }
162770c58f8SChristoffer Dall 
gicv2m_init(Object * obj)163770c58f8SChristoffer Dall static void gicv2m_init(Object *obj)
164770c58f8SChristoffer Dall {
165770c58f8SChristoffer Dall     SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
166770c58f8SChristoffer Dall     ARMGICv2mState *s = ARM_GICV2M(obj);
167770c58f8SChristoffer Dall 
168770c58f8SChristoffer Dall     memory_region_init_io(&s->iomem, OBJECT(s), &gicv2m_ops, s,
169770c58f8SChristoffer Dall                           "gicv2m", 0x1000);
170770c58f8SChristoffer Dall     sysbus_init_mmio(sbd, &s->iomem);
171770c58f8SChristoffer Dall }
172770c58f8SChristoffer Dall 
173770c58f8SChristoffer Dall static Property gicv2m_properties[] = {
174770c58f8SChristoffer Dall     DEFINE_PROP_UINT32("base-spi", ARMGICv2mState, base_spi, 0),
175770c58f8SChristoffer Dall     DEFINE_PROP_UINT32("num-spi", ARMGICv2mState, num_spi, 64),
176770c58f8SChristoffer Dall     DEFINE_PROP_END_OF_LIST(),
177770c58f8SChristoffer Dall };
178770c58f8SChristoffer Dall 
gicv2m_class_init(ObjectClass * klass,void * data)179770c58f8SChristoffer Dall static void gicv2m_class_init(ObjectClass *klass, void *data)
180770c58f8SChristoffer Dall {
181770c58f8SChristoffer Dall     DeviceClass *dc = DEVICE_CLASS(klass);
182770c58f8SChristoffer Dall 
1834f67d30bSMarc-André Lureau     device_class_set_props(dc, gicv2m_properties);
184770c58f8SChristoffer Dall     dc->realize = gicv2m_realize;
185770c58f8SChristoffer Dall }
186770c58f8SChristoffer Dall 
187770c58f8SChristoffer Dall static const TypeInfo gicv2m_info = {
188770c58f8SChristoffer Dall     .name          = TYPE_ARM_GICV2M,
189770c58f8SChristoffer Dall     .parent        = TYPE_SYS_BUS_DEVICE,
190770c58f8SChristoffer Dall     .instance_size = sizeof(ARMGICv2mState),
191770c58f8SChristoffer Dall     .instance_init = gicv2m_init,
192770c58f8SChristoffer Dall     .class_init    = gicv2m_class_init,
193770c58f8SChristoffer Dall };
194770c58f8SChristoffer Dall 
gicv2m_register_types(void)195770c58f8SChristoffer Dall static void gicv2m_register_types(void)
196770c58f8SChristoffer Dall {
197770c58f8SChristoffer Dall     type_register_static(&gicv2m_info);
198770c58f8SChristoffer Dall }
199770c58f8SChristoffer Dall 
200770c58f8SChristoffer Dall type_init(gicv2m_register_types)
201