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