]> git.proxmox.com Git - mirror_qemu.git/blame - hw/intc/riscv_aclint.c
accel/tcg: Replace CPUState.env_ptr with cpu_env()
[mirror_qemu.git] / hw / intc / riscv_aclint.c
CommitLineData
1c77c410 1/*
b8fb878a
AP
2 * RISC-V ACLINT (Advanced Core Local Interruptor)
3 * URL: https://github.com/riscv/riscv-aclint
1c77c410
MC
4 *
5 * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu
6 * Copyright (c) 2017 SiFive, Inc.
b8fb878a 7 * Copyright (c) 2021 Western Digital Corporation or its affiliates.
1c77c410
MC
8 *
9 * This provides real-time clock, timer and interprocessor interrupts.
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms and conditions of the GNU General Public License,
13 * version 2 or later, as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * more details.
19 *
20 * You should have received a copy of the GNU General Public License along with
21 * this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24#include "qemu/osdep.h"
3e80f690 25#include "qapi/error.h"
1c77c410 26#include "qemu/error-report.h"
b8fb878a 27#include "qemu/log.h"
0b8fa32f 28#include "qemu/module.h"
1c77c410
MC
29#include "hw/sysbus.h"
30#include "target/riscv/cpu.h"
a27bd6c7 31#include "hw/qdev-properties.h"
cc63a182 32#include "hw/intc/riscv_aclint.h"
1c77c410 33#include "qemu/timer.h"
a714b8aa 34#include "hw/irq.h"
7cbcc538 35#include "migration/vmstate.h"
a714b8aa 36
b8fb878a
AP
37typedef struct riscv_aclint_mtimer_callback {
38 RISCVAclintMTimerState *s;
a714b8aa 39 int num;
b8fb878a 40} riscv_aclint_mtimer_callback;
1c77c410 41
e2f01f3c 42static uint64_t cpu_riscv_read_rtc_raw(uint32_t timebase_freq)
1c77c410 43{
2a8756ed 44 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
a47ef6e9 45 timebase_freq, NANOSECONDS_PER_SECOND);
1c77c410
MC
46}
47
e2f01f3c
FC
48static uint64_t cpu_riscv_read_rtc(void *opaque)
49{
50 RISCVAclintMTimerState *mtimer = opaque;
51 return cpu_riscv_read_rtc_raw(mtimer->timebase_freq) + mtimer->time_delta;
52}
53
1c77c410
MC
54/*
55 * Called when timecmp is written to update the QEMU timer or immediately
56 * trigger timer interrupt if mtimecmp <= current timer value.
57 */
b8fb878a
AP
58static void riscv_aclint_mtimer_write_timecmp(RISCVAclintMTimerState *mtimer,
59 RISCVCPU *cpu,
60 int hartid,
e2f01f3c 61 uint64_t value)
1c77c410 62{
e2f01f3c 63 uint32_t timebase_freq = mtimer->timebase_freq;
1c77c410
MC
64 uint64_t next;
65 uint64_t diff;
66
9382a9ea 67 uint64_t rtc = cpu_riscv_read_rtc(mtimer);
1c77c410 68
7cbcc538
AP
69 /* Compute the relative hartid w.r.t the socket */
70 hartid = hartid - mtimer->hartid_base;
71
72 mtimer->timecmp[hartid] = value;
9382a9ea 73 if (mtimer->timecmp[hartid] <= rtc) {
b8fb878a
AP
74 /*
75 * If we're setting an MTIMECMP value in the "past",
76 * immediately raise the timer interrupt
77 */
7cbcc538 78 qemu_irq_raise(mtimer->timer_irqs[hartid]);
1c77c410
MC
79 return;
80 }
81
82 /* otherwise, set up the future timer interrupt */
7cbcc538 83 qemu_irq_lower(mtimer->timer_irqs[hartid]);
9382a9ea 84 diff = mtimer->timecmp[hartid] - rtc;
1c77c410 85 /* back to ns (note args switched in muldiv64) */
4dc06bb8
DH
86 uint64_t ns_diff = muldiv64(diff, NANOSECONDS_PER_SECOND, timebase_freq);
87
88 /*
89 * check if ns_diff overflowed and check if the addition would potentially
90 * overflow
91 */
92 if ((NANOSECONDS_PER_SECOND > timebase_freq && ns_diff < diff) ||
93 ns_diff > INT64_MAX) {
94 next = INT64_MAX;
95 } else {
96 /*
97 * as it is very unlikely qemu_clock_get_ns will return a value
98 * greater than INT64_MAX, no additional check is needed for an
99 * unsigned integer overflow.
100 */
101 next = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + ns_diff;
102 /*
103 * if ns_diff is INT64_MAX next may still be outside the range
104 * of a signed integer.
105 */
106 next = MIN(next, INT64_MAX);
107 }
108
7cbcc538 109 timer_mod(mtimer->timers[hartid], next);
1c77c410
MC
110}
111
112/*
113 * Callback used when the timer set using timer_mod expires.
114 * Should raise the timer interrupt line
115 */
b8fb878a 116static void riscv_aclint_mtimer_cb(void *opaque)
1c77c410 117{
b8fb878a 118 riscv_aclint_mtimer_callback *state = opaque;
a714b8aa
AF
119
120 qemu_irq_raise(state->s->timer_irqs[state->num]);
1c77c410
MC
121}
122
b8fb878a
AP
123/* CPU read MTIMER register */
124static uint64_t riscv_aclint_mtimer_read(void *opaque, hwaddr addr,
125 unsigned size)
1c77c410 126{
b8fb878a
AP
127 RISCVAclintMTimerState *mtimer = opaque;
128
129 if (addr >= mtimer->timecmp_base &&
130 addr < (mtimer->timecmp_base + (mtimer->num_harts << 3))) {
131 size_t hartid = mtimer->hartid_base +
132 ((addr - mtimer->timecmp_base) >> 3);
64452a09 133 CPUState *cpu = cpu_by_arch_id(hartid);
b77af26e 134 CPURISCVState *env = cpu ? cpu_env(cpu) : NULL;
1c77c410 135 if (!env) {
b8fb878a
AP
136 qemu_log_mask(LOG_GUEST_ERROR,
137 "aclint-mtimer: invalid hartid: %zu", hartid);
1c77c410 138 } else if ((addr & 0x7) == 0) {
d42df0ea 139 /* timecmp_lo for RV32/RV64 or timecmp for RV64 */
7cbcc538 140 uint64_t timecmp = mtimer->timecmp[hartid];
d42df0ea 141 return (size == 4) ? (timecmp & 0xFFFFFFFF) : timecmp;
1c77c410
MC
142 } else if ((addr & 0x7) == 4) {
143 /* timecmp_hi */
7cbcc538 144 uint64_t timecmp = mtimer->timecmp[hartid];
1c77c410
MC
145 return (timecmp >> 32) & 0xFFFFFFFF;
146 } else {
b8fb878a
AP
147 qemu_log_mask(LOG_UNIMP,
148 "aclint-mtimer: invalid read: %08x", (uint32_t)addr);
1c77c410
MC
149 return 0;
150 }
b8fb878a 151 } else if (addr == mtimer->time_base) {
d42df0ea 152 /* time_lo for RV32/RV64 or timecmp for RV64 */
e2f01f3c 153 uint64_t rtc = cpu_riscv_read_rtc(mtimer);
d42df0ea 154 return (size == 4) ? (rtc & 0xFFFFFFFF) : rtc;
b8fb878a 155 } else if (addr == mtimer->time_base + 4) {
1c77c410 156 /* time_hi */
e2f01f3c 157 return (cpu_riscv_read_rtc(mtimer) >> 32) & 0xFFFFFFFF;
1c77c410
MC
158 }
159
b8fb878a
AP
160 qemu_log_mask(LOG_UNIMP,
161 "aclint-mtimer: invalid read: %08x", (uint32_t)addr);
1c77c410
MC
162 return 0;
163}
164
b8fb878a
AP
165/* CPU write MTIMER register */
166static void riscv_aclint_mtimer_write(void *opaque, hwaddr addr,
167 uint64_t value, unsigned size)
1c77c410 168{
b8fb878a 169 RISCVAclintMTimerState *mtimer = opaque;
e2f01f3c 170 int i;
1c77c410 171
b8fb878a
AP
172 if (addr >= mtimer->timecmp_base &&
173 addr < (mtimer->timecmp_base + (mtimer->num_harts << 3))) {
174 size_t hartid = mtimer->hartid_base +
175 ((addr - mtimer->timecmp_base) >> 3);
64452a09 176 CPUState *cpu = cpu_by_arch_id(hartid);
b77af26e 177 CPURISCVState *env = cpu ? cpu_env(cpu) : NULL;
1c77c410 178 if (!env) {
b8fb878a
AP
179 qemu_log_mask(LOG_GUEST_ERROR,
180 "aclint-mtimer: invalid hartid: %zu", hartid);
1c77c410 181 } else if ((addr & 0x7) == 0) {
d42df0ea
FC
182 if (size == 4) {
183 /* timecmp_lo for RV32/RV64 */
7cbcc538 184 uint64_t timecmp_hi = mtimer->timecmp[hartid] >> 32;
d42df0ea 185 riscv_aclint_mtimer_write_timecmp(mtimer, RISCV_CPU(cpu), hartid,
e2f01f3c 186 timecmp_hi << 32 | (value & 0xFFFFFFFF));
d42df0ea
FC
187 } else {
188 /* timecmp for RV64 */
189 riscv_aclint_mtimer_write_timecmp(mtimer, RISCV_CPU(cpu), hartid,
e2f01f3c 190 value);
d42df0ea 191 }
1c77c410 192 } else if ((addr & 0x7) == 4) {
d42df0ea
FC
193 if (size == 4) {
194 /* timecmp_hi for RV32/RV64 */
7cbcc538 195 uint64_t timecmp_lo = mtimer->timecmp[hartid];
d42df0ea 196 riscv_aclint_mtimer_write_timecmp(mtimer, RISCV_CPU(cpu), hartid,
e2f01f3c 197 value << 32 | (timecmp_lo & 0xFFFFFFFF));
d42df0ea
FC
198 } else {
199 qemu_log_mask(LOG_GUEST_ERROR,
200 "aclint-mtimer: invalid timecmp_hi write: %08x",
201 (uint32_t)addr);
202 }
1c77c410 203 } else {
b8fb878a
AP
204 qemu_log_mask(LOG_UNIMP,
205 "aclint-mtimer: invalid timecmp write: %08x",
206 (uint32_t)addr);
1c77c410
MC
207 }
208 return;
e2f01f3c
FC
209 } else if (addr == mtimer->time_base || addr == mtimer->time_base + 4) {
210 uint64_t rtc_r = cpu_riscv_read_rtc_raw(mtimer->timebase_freq);
e0922b73 211 uint64_t rtc = cpu_riscv_read_rtc(mtimer);
e2f01f3c
FC
212
213 if (addr == mtimer->time_base) {
214 if (size == 4) {
215 /* time_lo for RV32/RV64 */
e0922b73 216 mtimer->time_delta = ((rtc & ~0xFFFFFFFFULL) | value) - rtc_r;
e2f01f3c
FC
217 } else {
218 /* time for RV64 */
219 mtimer->time_delta = value - rtc_r;
220 }
221 } else {
222 if (size == 4) {
223 /* time_hi for RV32/RV64 */
e0922b73 224 mtimer->time_delta = (value << 32 | (rtc & 0xFFFFFFFF)) - rtc_r;
e2f01f3c
FC
225 } else {
226 qemu_log_mask(LOG_GUEST_ERROR,
227 "aclint-mtimer: invalid time_hi write: %08x",
228 (uint32_t)addr);
229 return;
230 }
231 }
232
233 /* Check if timer interrupt is triggered for each hart. */
234 for (i = 0; i < mtimer->num_harts; i++) {
64452a09 235 CPUState *cpu = cpu_by_arch_id(mtimer->hartid_base + i);
b77af26e 236 CPURISCVState *env = cpu ? cpu_env(cpu) : NULL;
e2f01f3c
FC
237 if (!env) {
238 continue;
239 }
240 riscv_aclint_mtimer_write_timecmp(mtimer, RISCV_CPU(cpu),
77046729 241 mtimer->hartid_base + i,
7cbcc538 242 mtimer->timecmp[i]);
e2f01f3c 243 }
1c77c410
MC
244 return;
245 }
246
b8fb878a
AP
247 qemu_log_mask(LOG_UNIMP,
248 "aclint-mtimer: invalid write: %08x", (uint32_t)addr);
1c77c410
MC
249}
250
b8fb878a
AP
251static const MemoryRegionOps riscv_aclint_mtimer_ops = {
252 .read = riscv_aclint_mtimer_read,
253 .write = riscv_aclint_mtimer_write,
1c77c410
MC
254 .endianness = DEVICE_LITTLE_ENDIAN,
255 .valid = {
256 .min_access_size = 4,
70b78d4e 257 .max_access_size = 8
231a90c0
FC
258 },
259 .impl = {
260 .min_access_size = 4,
261 .max_access_size = 8,
1c77c410
MC
262 }
263};
264
b8fb878a
AP
265static Property riscv_aclint_mtimer_properties[] = {
266 DEFINE_PROP_UINT32("hartid-base", RISCVAclintMTimerState,
267 hartid_base, 0),
268 DEFINE_PROP_UINT32("num-harts", RISCVAclintMTimerState, num_harts, 1),
269 DEFINE_PROP_UINT32("timecmp-base", RISCVAclintMTimerState,
270 timecmp_base, RISCV_ACLINT_DEFAULT_MTIMECMP),
271 DEFINE_PROP_UINT32("time-base", RISCVAclintMTimerState,
272 time_base, RISCV_ACLINT_DEFAULT_MTIME),
273 DEFINE_PROP_UINT32("aperture-size", RISCVAclintMTimerState,
274 aperture_size, RISCV_ACLINT_DEFAULT_MTIMER_SIZE),
275 DEFINE_PROP_UINT32("timebase-freq", RISCVAclintMTimerState,
276 timebase_freq, 0),
1c77c410
MC
277 DEFINE_PROP_END_OF_LIST(),
278};
279
b8fb878a 280static void riscv_aclint_mtimer_realize(DeviceState *dev, Error **errp)
1c77c410 281{
b8fb878a
AP
282 RISCVAclintMTimerState *s = RISCV_ACLINT_MTIMER(dev);
283 int i;
284
285 memory_region_init_io(&s->mmio, OBJECT(dev), &riscv_aclint_mtimer_ops,
286 s, TYPE_RISCV_ACLINT_MTIMER, s->aperture_size);
1c77c410 287 sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->mmio);
a714b8aa 288
b21e2380 289 s->timer_irqs = g_new(qemu_irq, s->num_harts);
a714b8aa
AF
290 qdev_init_gpio_out(dev, s->timer_irqs, s->num_harts);
291
7cbcc538
AP
292 s->timers = g_new0(QEMUTimer *, s->num_harts);
293 s->timecmp = g_new0(uint64_t, s->num_harts);
b8fb878a
AP
294 /* Claim timer interrupt bits */
295 for (i = 0; i < s->num_harts; i++) {
64452a09 296 RISCVCPU *cpu = RISCV_CPU(cpu_by_arch_id(s->hartid_base + i));
b8fb878a
AP
297 if (riscv_cpu_claim_interrupts(cpu, MIP_MTIP) < 0) {
298 error_report("MTIP already claimed");
299 exit(1);
300 }
301 }
1c77c410
MC
302}
303
8124f819
JS
304static void riscv_aclint_mtimer_reset_enter(Object *obj, ResetType type)
305{
306 /*
307 * According to RISC-V ACLINT spec:
308 * - On MTIMER device reset, the MTIME register is cleared to zero.
309 * - On MTIMER device reset, the MTIMECMP registers are in unknown state.
310 */
311 RISCVAclintMTimerState *mtimer = RISCV_ACLINT_MTIMER(obj);
312
313 /*
314 * Clear mtime register by writing to 0 it.
315 * Pending mtime interrupts will also be cleared at the same time.
316 */
317 riscv_aclint_mtimer_write(mtimer, mtimer->time_base, 0, 8);
318}
319
7cbcc538
AP
320static const VMStateDescription vmstate_riscv_mtimer = {
321 .name = "riscv_mtimer",
322 .version_id = 1,
323 .minimum_version_id = 1,
324 .fields = (VMStateField[]) {
325 VMSTATE_VARRAY_UINT32(timecmp, RISCVAclintMTimerState,
326 num_harts, 0,
327 vmstate_info_uint64, uint64_t),
328 VMSTATE_END_OF_LIST()
329 }
330};
331
b8fb878a 332static void riscv_aclint_mtimer_class_init(ObjectClass *klass, void *data)
1c77c410
MC
333{
334 DeviceClass *dc = DEVICE_CLASS(klass);
b8fb878a
AP
335 dc->realize = riscv_aclint_mtimer_realize;
336 device_class_set_props(dc, riscv_aclint_mtimer_properties);
8124f819
JS
337 ResettableClass *rc = RESETTABLE_CLASS(klass);
338 rc->phases.enter = riscv_aclint_mtimer_reset_enter;
7cbcc538 339 dc->vmsd = &vmstate_riscv_mtimer;
1c77c410
MC
340}
341
b8fb878a
AP
342static const TypeInfo riscv_aclint_mtimer_info = {
343 .name = TYPE_RISCV_ACLINT_MTIMER,
1c77c410 344 .parent = TYPE_SYS_BUS_DEVICE,
b8fb878a
AP
345 .instance_size = sizeof(RISCVAclintMTimerState),
346 .class_init = riscv_aclint_mtimer_class_init,
1c77c410
MC
347};
348
1c77c410 349/*
b8fb878a 350 * Create ACLINT MTIMER device.
1c77c410 351 */
b8fb878a
AP
352DeviceState *riscv_aclint_mtimer_create(hwaddr addr, hwaddr size,
353 uint32_t hartid_base, uint32_t num_harts,
a47ef6e9
BM
354 uint32_t timecmp_base, uint32_t time_base, uint32_t timebase_freq,
355 bool provide_rdtime)
1c77c410
MC
356{
357 int i;
b8fb878a 358 DeviceState *dev = qdev_new(TYPE_RISCV_ACLINT_MTIMER);
7cbcc538 359 RISCVAclintMTimerState *s = RISCV_ACLINT_MTIMER(dev);
b8fb878a
AP
360
361 assert(num_harts <= RISCV_ACLINT_MAX_HARTS);
362 assert(!(addr & 0x7));
363 assert(!(timecmp_base & 0x7));
364 assert(!(time_base & 0x7));
a714b8aa 365
a714b8aa
AF
366 qdev_prop_set_uint32(dev, "hartid-base", hartid_base);
367 qdev_prop_set_uint32(dev, "num-harts", num_harts);
a714b8aa
AF
368 qdev_prop_set_uint32(dev, "timecmp-base", timecmp_base);
369 qdev_prop_set_uint32(dev, "time-base", time_base);
370 qdev_prop_set_uint32(dev, "aperture-size", size);
371 qdev_prop_set_uint32(dev, "timebase-freq", timebase_freq);
372 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
373 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, addr);
374
1c77c410 375 for (i = 0; i < num_harts; i++) {
64452a09 376 CPUState *cpu = cpu_by_arch_id(hartid_base + i);
a714b8aa 377 RISCVCPU *rvcpu = RISCV_CPU(cpu);
b77af26e 378 CPURISCVState *env = cpu ? cpu_env(cpu) : NULL;
b8fb878a 379 riscv_aclint_mtimer_callback *cb =
b21e2380 380 g_new0(riscv_aclint_mtimer_callback, 1);
a714b8aa 381
1c77c410 382 if (!env) {
a714b8aa 383 g_free(cb);
1c77c410
MC
384 continue;
385 }
5f3616cc 386 if (provide_rdtime) {
e2f01f3c 387 riscv_cpu_set_rdtime_fn(env, cpu_riscv_read_rtc, dev);
5f3616cc 388 }
a714b8aa 389
7cbcc538 390 cb->s = s;
a714b8aa 391 cb->num = i;
7cbcc538 392 s->timers[i] = timer_new_ns(QEMU_CLOCK_VIRTUAL,
b8fb878a 393 &riscv_aclint_mtimer_cb, cb);
7cbcc538 394 s->timecmp[i] = 0;
a714b8aa
AF
395
396 qdev_connect_gpio_out(dev, i,
397 qdev_get_gpio_in(DEVICE(rvcpu), IRQ_M_TIMER));
1c77c410
MC
398 }
399
1c77c410
MC
400 return dev;
401}
b8fb878a
AP
402
403/* CPU read [M|S]SWI register */
404static uint64_t riscv_aclint_swi_read(void *opaque, hwaddr addr,
405 unsigned size)
406{
407 RISCVAclintSwiState *swi = opaque;
408
409 if (addr < (swi->num_harts << 2)) {
410 size_t hartid = swi->hartid_base + (addr >> 2);
64452a09 411 CPUState *cpu = cpu_by_arch_id(hartid);
b77af26e 412 CPURISCVState *env = cpu ? cpu_env(cpu) : NULL;
b8fb878a
AP
413 if (!env) {
414 qemu_log_mask(LOG_GUEST_ERROR,
415 "aclint-swi: invalid hartid: %zu", hartid);
416 } else if ((addr & 0x3) == 0) {
417 return (swi->sswi) ? 0 : ((env->mip & MIP_MSIP) > 0);
418 }
419 }
420
421 qemu_log_mask(LOG_UNIMP,
422 "aclint-swi: invalid read: %08x", (uint32_t)addr);
423 return 0;
424}
425
426/* CPU write [M|S]SWI register */
427static void riscv_aclint_swi_write(void *opaque, hwaddr addr, uint64_t value,
428 unsigned size)
429{
430 RISCVAclintSwiState *swi = opaque;
431
432 if (addr < (swi->num_harts << 2)) {
433 size_t hartid = swi->hartid_base + (addr >> 2);
64452a09 434 CPUState *cpu = cpu_by_arch_id(hartid);
b77af26e 435 CPURISCVState *env = cpu ? cpu_env(cpu) : NULL;
b8fb878a
AP
436 if (!env) {
437 qemu_log_mask(LOG_GUEST_ERROR,
438 "aclint-swi: invalid hartid: %zu", hartid);
439 } else if ((addr & 0x3) == 0) {
440 if (value & 0x1) {
441 qemu_irq_raise(swi->soft_irqs[hartid - swi->hartid_base]);
442 } else {
443 if (!swi->sswi) {
444 qemu_irq_lower(swi->soft_irqs[hartid - swi->hartid_base]);
445 }
446 }
447 return;
448 }
449 }
450
451 qemu_log_mask(LOG_UNIMP,
452 "aclint-swi: invalid write: %08x", (uint32_t)addr);
453}
454
455static const MemoryRegionOps riscv_aclint_swi_ops = {
456 .read = riscv_aclint_swi_read,
457 .write = riscv_aclint_swi_write,
458 .endianness = DEVICE_LITTLE_ENDIAN,
459 .valid = {
460 .min_access_size = 4,
461 .max_access_size = 4
462 }
463};
464
465static Property riscv_aclint_swi_properties[] = {
466 DEFINE_PROP_UINT32("hartid-base", RISCVAclintSwiState, hartid_base, 0),
467 DEFINE_PROP_UINT32("num-harts", RISCVAclintSwiState, num_harts, 1),
468 DEFINE_PROP_UINT32("sswi", RISCVAclintSwiState, sswi, false),
469 DEFINE_PROP_END_OF_LIST(),
470};
471
472static void riscv_aclint_swi_realize(DeviceState *dev, Error **errp)
473{
474 RISCVAclintSwiState *swi = RISCV_ACLINT_SWI(dev);
475 int i;
476
477 memory_region_init_io(&swi->mmio, OBJECT(dev), &riscv_aclint_swi_ops, swi,
478 TYPE_RISCV_ACLINT_SWI, RISCV_ACLINT_SWI_SIZE);
479 sysbus_init_mmio(SYS_BUS_DEVICE(dev), &swi->mmio);
480
b21e2380 481 swi->soft_irqs = g_new(qemu_irq, swi->num_harts);
b8fb878a
AP
482 qdev_init_gpio_out(dev, swi->soft_irqs, swi->num_harts);
483
484 /* Claim software interrupt bits */
485 for (i = 0; i < swi->num_harts; i++) {
486 RISCVCPU *cpu = RISCV_CPU(qemu_get_cpu(swi->hartid_base + i));
9323e79f 487 /* We don't claim mip.SSIP because it is writable by software */
b8fb878a
AP
488 if (riscv_cpu_claim_interrupts(cpu, swi->sswi ? 0 : MIP_MSIP) < 0) {
489 error_report("MSIP already claimed");
490 exit(1);
491 }
492 }
493}
494
8124f819
JS
495static void riscv_aclint_swi_reset_enter(Object *obj, ResetType type)
496{
497 /*
498 * According to RISC-V ACLINT spec:
499 * - On MSWI device reset, each MSIP register is cleared to zero.
500 *
501 * p.s. SSWI device reset does nothing since SETSIP register always reads 0.
502 */
503 RISCVAclintSwiState *swi = RISCV_ACLINT_SWI(obj);
504 int i;
505
506 if (!swi->sswi) {
507 for (i = 0; i < swi->num_harts; i++) {
508 /* Clear MSIP registers by lowering software interrupts. */
509 qemu_irq_lower(swi->soft_irqs[i]);
510 }
511 }
512}
513
b8fb878a
AP
514static void riscv_aclint_swi_class_init(ObjectClass *klass, void *data)
515{
516 DeviceClass *dc = DEVICE_CLASS(klass);
517 dc->realize = riscv_aclint_swi_realize;
518 device_class_set_props(dc, riscv_aclint_swi_properties);
8124f819
JS
519 ResettableClass *rc = RESETTABLE_CLASS(klass);
520 rc->phases.enter = riscv_aclint_swi_reset_enter;
b8fb878a
AP
521}
522
523static const TypeInfo riscv_aclint_swi_info = {
524 .name = TYPE_RISCV_ACLINT_SWI,
525 .parent = TYPE_SYS_BUS_DEVICE,
526 .instance_size = sizeof(RISCVAclintSwiState),
527 .class_init = riscv_aclint_swi_class_init,
528};
529
530/*
531 * Create ACLINT [M|S]SWI device.
532 */
533DeviceState *riscv_aclint_swi_create(hwaddr addr, uint32_t hartid_base,
534 uint32_t num_harts, bool sswi)
535{
536 int i;
537 DeviceState *dev = qdev_new(TYPE_RISCV_ACLINT_SWI);
538
539 assert(num_harts <= RISCV_ACLINT_MAX_HARTS);
540 assert(!(addr & 0x3));
541
542 qdev_prop_set_uint32(dev, "hartid-base", hartid_base);
543 qdev_prop_set_uint32(dev, "num-harts", num_harts);
544 qdev_prop_set_uint32(dev, "sswi", sswi ? true : false);
545 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
546 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, addr);
547
548 for (i = 0; i < num_harts; i++) {
64452a09 549 CPUState *cpu = cpu_by_arch_id(hartid_base + i);
b8fb878a
AP
550 RISCVCPU *rvcpu = RISCV_CPU(cpu);
551
552 qdev_connect_gpio_out(dev, i,
553 qdev_get_gpio_in(DEVICE(rvcpu),
554 (sswi) ? IRQ_S_SOFT : IRQ_M_SOFT));
555 }
556
557 return dev;
558}
559
560static void riscv_aclint_register_types(void)
561{
562 type_register_static(&riscv_aclint_mtimer_info);
563 type_register_static(&riscv_aclint_swi_info);
564}
565
566type_init(riscv_aclint_register_types)