Merge remote-tracking branch 'remotes/dgilbert-gitlab/tags/pull-migration-20210726a...
[qemu/armbru.git] / hw / intc / ibex_plic.c
blobedf76e4f61d3311480e5ba931173d0a103ffd32a
1 /*
2 * QEMU RISC-V lowRISC Ibex PLIC
4 * Copyright (c) 2020 Western Digital
6 * Documentation avaliable: https://docs.opentitan.org/hw/ip/rv_plic/doc/
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2 or later, as published by the Free Software Foundation.
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include "qemu/log.h"
23 #include "hw/qdev-properties.h"
24 #include "hw/core/cpu.h"
25 #include "hw/boards.h"
26 #include "hw/pci/msi.h"
27 #include "target/riscv/cpu_bits.h"
28 #include "target/riscv/cpu.h"
29 #include "hw/intc/ibex_plic.h"
31 static bool addr_between(uint32_t addr, uint32_t base, uint32_t num)
33 uint32_t end = base + (num * 0x04);
35 if (addr >= base && addr < end) {
36 return true;
39 return false;
42 static void ibex_plic_irqs_set_pending(IbexPlicState *s, int irq, bool level)
44 int pending_num = irq / 32;
46 if (!level) {
48 * If the level is low make sure we clear the hidden_pending.
50 s->hidden_pending[pending_num] &= ~(1 << (irq % 32));
53 if (s->claimed[pending_num] & 1 << (irq % 32)) {
55 * The interrupt has been claimed, but not completed.
56 * The pending bit can't be set.
57 * Save the pending level for after the interrupt is completed.
59 s->hidden_pending[pending_num] |= level << (irq % 32);
60 } else {
61 s->pending[pending_num] |= level << (irq % 32);
65 static bool ibex_plic_irqs_pending(IbexPlicState *s, uint32_t context)
67 int i;
68 uint32_t max_irq = 0;
69 uint32_t max_prio = s->threshold;
71 for (i = 0; i < s->pending_num; i++) {
72 uint32_t irq_num = ctz64(s->pending[i]) + (i * 32);
74 if (!(s->pending[i] & s->enable[i])) {
75 /* No pending and enabled IRQ */
76 continue;
79 if (s->priority[irq_num] > max_prio) {
80 max_irq = irq_num;
81 max_prio = s->priority[irq_num];
85 if (max_irq) {
86 s->claim = max_irq;
87 return true;
90 return false;
93 static void ibex_plic_update(IbexPlicState *s)
95 CPUState *cpu;
96 int level, i;
98 for (i = 0; i < s->num_cpus; i++) {
99 cpu = qemu_get_cpu(i);
101 if (!cpu) {
102 continue;
105 level = ibex_plic_irqs_pending(s, 0);
107 riscv_cpu_update_mip(RISCV_CPU(cpu), MIP_MEIP, BOOL_TO_MASK(level));
111 static void ibex_plic_reset(DeviceState *dev)
113 IbexPlicState *s = IBEX_PLIC(dev);
115 s->threshold = 0x00000000;
116 s->claim = 0x00000000;
119 static uint64_t ibex_plic_read(void *opaque, hwaddr addr,
120 unsigned int size)
122 IbexPlicState *s = opaque;
123 int offset;
124 uint32_t ret = 0;
126 if (addr_between(addr, s->pending_base, s->pending_num)) {
127 offset = (addr - s->pending_base) / 4;
128 ret = s->pending[offset];
129 } else if (addr_between(addr, s->source_base, s->source_num)) {
130 qemu_log_mask(LOG_UNIMP,
131 "%s: Interrupt source mode not supported\n", __func__);
132 } else if (addr_between(addr, s->priority_base, s->priority_num)) {
133 offset = (addr - s->priority_base) / 4;
134 ret = s->priority[offset];
135 } else if (addr_between(addr, s->enable_base, s->enable_num)) {
136 offset = (addr - s->enable_base) / 4;
137 ret = s->enable[offset];
138 } else if (addr_between(addr, s->threshold_base, 1)) {
139 ret = s->threshold;
140 } else if (addr_between(addr, s->claim_base, 1)) {
141 int pending_num = s->claim / 32;
142 s->pending[pending_num] &= ~(1 << (s->claim % 32));
144 /* Set the interrupt as claimed, but not completed */
145 s->claimed[pending_num] |= 1 << (s->claim % 32);
147 /* Return the current claimed interrupt */
148 ret = s->claim;
150 /* Clear the claimed interrupt */
151 s->claim = 0x00000000;
153 /* Update the interrupt status after the claim */
154 ibex_plic_update(s);
157 return ret;
160 static void ibex_plic_write(void *opaque, hwaddr addr,
161 uint64_t value, unsigned int size)
163 IbexPlicState *s = opaque;
165 if (addr_between(addr, s->pending_base, s->pending_num)) {
166 qemu_log_mask(LOG_GUEST_ERROR,
167 "%s: Pending registers are read only\n", __func__);
168 } else if (addr_between(addr, s->source_base, s->source_num)) {
169 qemu_log_mask(LOG_UNIMP,
170 "%s: Interrupt source mode not supported\n", __func__);
171 } else if (addr_between(addr, s->priority_base, s->priority_num)) {
172 uint32_t irq = ((addr - s->priority_base) >> 2) + 1;
173 s->priority[irq] = value & 7;
174 ibex_plic_update(s);
175 } else if (addr_between(addr, s->enable_base, s->enable_num)) {
176 uint32_t enable_reg = (addr - s->enable_base) / 4;
178 s->enable[enable_reg] = value;
179 } else if (addr_between(addr, s->threshold_base, 1)) {
180 s->threshold = value & 3;
181 } else if (addr_between(addr, s->claim_base, 1)) {
182 if (s->claim == value) {
183 /* Interrupt was completed */
184 s->claim = 0;
186 if (s->claimed[value / 32] & 1 << (value % 32)) {
187 int pending_num = value / 32;
189 /* This value was already claimed, clear it. */
190 s->claimed[pending_num] &= ~(1 << (value % 32));
192 if (s->hidden_pending[pending_num] & (1 << (value % 32))) {
194 * If the bit in hidden_pending is set then that means we
195 * received an interrupt between claiming and completing
196 * the interrupt that hasn't since been de-asserted.
197 * On hardware this would trigger an interrupt, so let's
198 * trigger one here as well.
200 s->pending[pending_num] |= 1 << (value % 32);
205 ibex_plic_update(s);
208 static const MemoryRegionOps ibex_plic_ops = {
209 .read = ibex_plic_read,
210 .write = ibex_plic_write,
211 .endianness = DEVICE_NATIVE_ENDIAN,
212 .valid = {
213 .min_access_size = 4,
214 .max_access_size = 4
218 static void ibex_plic_irq_request(void *opaque, int irq, int level)
220 IbexPlicState *s = opaque;
222 ibex_plic_irqs_set_pending(s, irq, level > 0);
223 ibex_plic_update(s);
226 static Property ibex_plic_properties[] = {
227 DEFINE_PROP_UINT32("num-cpus", IbexPlicState, num_cpus, 1),
228 DEFINE_PROP_UINT32("num-sources", IbexPlicState, num_sources, 176),
230 DEFINE_PROP_UINT32("pending-base", IbexPlicState, pending_base, 0),
231 DEFINE_PROP_UINT32("pending-num", IbexPlicState, pending_num, 6),
233 DEFINE_PROP_UINT32("source-base", IbexPlicState, source_base, 0x18),
234 DEFINE_PROP_UINT32("source-num", IbexPlicState, source_num, 6),
236 DEFINE_PROP_UINT32("priority-base", IbexPlicState, priority_base, 0x30),
237 DEFINE_PROP_UINT32("priority-num", IbexPlicState, priority_num, 177),
239 DEFINE_PROP_UINT32("enable-base", IbexPlicState, enable_base, 0x300),
240 DEFINE_PROP_UINT32("enable-num", IbexPlicState, enable_num, 6),
242 DEFINE_PROP_UINT32("threshold-base", IbexPlicState, threshold_base, 0x318),
244 DEFINE_PROP_UINT32("claim-base", IbexPlicState, claim_base, 0x31c),
245 DEFINE_PROP_END_OF_LIST(),
248 static void ibex_plic_init(Object *obj)
250 IbexPlicState *s = IBEX_PLIC(obj);
252 memory_region_init_io(&s->mmio, obj, &ibex_plic_ops, s,
253 TYPE_IBEX_PLIC, 0x400);
254 sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
257 static void ibex_plic_realize(DeviceState *dev, Error **errp)
259 IbexPlicState *s = IBEX_PLIC(dev);
260 int i;
262 s->pending = g_new0(uint32_t, s->pending_num);
263 s->hidden_pending = g_new0(uint32_t, s->pending_num);
264 s->claimed = g_new0(uint32_t, s->pending_num);
265 s->source = g_new0(uint32_t, s->source_num);
266 s->priority = g_new0(uint32_t, s->priority_num);
267 s->enable = g_new0(uint32_t, s->enable_num);
269 qdev_init_gpio_in(dev, ibex_plic_irq_request, s->num_sources);
272 * We can't allow the supervisor to control SEIP as this would allow the
273 * supervisor to clear a pending external interrupt which will result in
274 * a lost interrupt in the case a PLIC is attached. The SEIP bit must be
275 * hardware controlled when a PLIC is attached.
277 MachineState *ms = MACHINE(qdev_get_machine());
278 unsigned int smp_cpus = ms->smp.cpus;
279 for (i = 0; i < smp_cpus; i++) {
280 RISCVCPU *cpu = RISCV_CPU(qemu_get_cpu(i));
281 if (riscv_cpu_claim_interrupts(cpu, MIP_SEIP) < 0) {
282 error_report("SEIP already claimed");
283 exit(1);
287 msi_nonbroken = true;
290 static void ibex_plic_class_init(ObjectClass *klass, void *data)
292 DeviceClass *dc = DEVICE_CLASS(klass);
294 dc->reset = ibex_plic_reset;
295 device_class_set_props(dc, ibex_plic_properties);
296 dc->realize = ibex_plic_realize;
299 static const TypeInfo ibex_plic_info = {
300 .name = TYPE_IBEX_PLIC,
301 .parent = TYPE_SYS_BUS_DEVICE,
302 .instance_size = sizeof(IbexPlicState),
303 .instance_init = ibex_plic_init,
304 .class_init = ibex_plic_class_init,
307 static void ibex_plic_register_types(void)
309 type_register_static(&ibex_plic_info);
312 type_init(ibex_plic_register_types)