Michael Clark | 4b50b8d | 2018-03-03 01:31:12 +1300 | [diff] [blame] | 1 | /* |
| 2 | * QEMU RISCV Hart Array |
| 3 | * |
| 4 | * Copyright (c) 2017 SiFive, Inc. |
| 5 | * |
Bin Meng | 91c9858 | 2019-09-06 09:20:03 -0700 | [diff] [blame] | 6 | * Holds the state of a homogeneous array of RISC-V harts |
Michael Clark | 4b50b8d | 2018-03-03 01:31:12 +1300 | [diff] [blame] | 7 | * |
| 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. |
| 11 | * |
| 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. |
| 16 | * |
| 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/>. |
| 19 | */ |
| 20 | |
| 21 | #include "qemu/osdep.h" |
| 22 | #include "qapi/error.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 23 | #include "qemu/module.h" |
Markus Armbruster | 71e8a91 | 2019-08-12 07:23:38 +0200 | [diff] [blame] | 24 | #include "sysemu/reset.h" |
Michael Clark | 4b50b8d | 2018-03-03 01:31:12 +1300 | [diff] [blame] | 25 | #include "hw/sysbus.h" |
| 26 | #include "target/riscv/cpu.h" |
Markus Armbruster | a27bd6c | 2019-08-12 07:23:51 +0200 | [diff] [blame] | 27 | #include "hw/qdev-properties.h" |
Michael Clark | 4b50b8d | 2018-03-03 01:31:12 +1300 | [diff] [blame] | 28 | #include "hw/riscv/riscv_hart.h" |
| 29 | |
| 30 | static Property riscv_harts_props[] = { |
| 31 | DEFINE_PROP_UINT32("num-harts", RISCVHartArrayState, num_harts, 1), |
Bin Meng | e8c5678 | 2019-09-06 09:20:04 -0700 | [diff] [blame] | 32 | DEFINE_PROP_UINT32("hartid-base", RISCVHartArrayState, hartid_base, 0), |
Michael Clark | 4b50b8d | 2018-03-03 01:31:12 +1300 | [diff] [blame] | 33 | DEFINE_PROP_STRING("cpu-type", RISCVHartArrayState, cpu_type), |
Bin Meng | 4100d5e | 2020-09-01 09:38:57 +0800 | [diff] [blame] | 34 | DEFINE_PROP_UINT64("resetvec", RISCVHartArrayState, resetvec, |
| 35 | DEFAULT_RSTVEC), |
Michael Clark | 4b50b8d | 2018-03-03 01:31:12 +1300 | [diff] [blame] | 36 | DEFINE_PROP_END_OF_LIST(), |
| 37 | }; |
| 38 | |
| 39 | static void riscv_harts_cpu_reset(void *opaque) |
| 40 | { |
| 41 | RISCVCPU *cpu = opaque; |
| 42 | cpu_reset(CPU(cpu)); |
| 43 | } |
| 44 | |
Markus Armbruster | 3e9a88c | 2020-06-30 11:03:43 +0200 | [diff] [blame] | 45 | static bool riscv_hart_realize(RISCVHartArrayState *s, int idx, |
Bin Meng | 91c9858 | 2019-09-06 09:20:03 -0700 | [diff] [blame] | 46 | char *cpu_type, Error **errp) |
| 47 | { |
Markus Armbruster | 9fc7fc4 | 2020-06-10 07:32:25 +0200 | [diff] [blame] | 48 | object_initialize_child(OBJECT(s), "harts[*]", &s->harts[idx], cpu_type); |
Bin Meng | 4100d5e | 2020-09-01 09:38:57 +0800 | [diff] [blame] | 49 | qdev_prop_set_uint64(DEVICE(&s->harts[idx]), "resetvec", s->resetvec); |
Bin Meng | e8c5678 | 2019-09-06 09:20:04 -0700 | [diff] [blame] | 50 | s->harts[idx].env.mhartid = s->hartid_base + idx; |
Bin Meng | 91c9858 | 2019-09-06 09:20:03 -0700 | [diff] [blame] | 51 | qemu_register_reset(riscv_harts_cpu_reset, &s->harts[idx]); |
Markus Armbruster | 3e9a88c | 2020-06-30 11:03:43 +0200 | [diff] [blame] | 52 | return qdev_realize(DEVICE(&s->harts[idx]), NULL, errp); |
Bin Meng | 91c9858 | 2019-09-06 09:20:03 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Michael Clark | 4b50b8d | 2018-03-03 01:31:12 +1300 | [diff] [blame] | 55 | static void riscv_harts_realize(DeviceState *dev, Error **errp) |
| 56 | { |
| 57 | RISCVHartArrayState *s = RISCV_HART_ARRAY(dev); |
Michael Clark | 4b50b8d | 2018-03-03 01:31:12 +1300 | [diff] [blame] | 58 | int n; |
| 59 | |
| 60 | s->harts = g_new0(RISCVCPU, s->num_harts); |
| 61 | |
| 62 | for (n = 0; n < s->num_harts; n++) { |
Markus Armbruster | 3e9a88c | 2020-06-30 11:03:43 +0200 | [diff] [blame] | 63 | if (!riscv_hart_realize(s, n, s->cpu_type, errp)) { |
| 64 | return; |
| 65 | } |
Michael Clark | 4b50b8d | 2018-03-03 01:31:12 +1300 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | static void riscv_harts_class_init(ObjectClass *klass, void *data) |
| 70 | { |
| 71 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 72 | |
Marc-André Lureau | 4f67d30 | 2020-01-10 19:30:32 +0400 | [diff] [blame] | 73 | device_class_set_props(dc, riscv_harts_props); |
Michael Clark | 4b50b8d | 2018-03-03 01:31:12 +1300 | [diff] [blame] | 74 | dc->realize = riscv_harts_realize; |
| 75 | } |
| 76 | |
Michael Clark | 4b50b8d | 2018-03-03 01:31:12 +1300 | [diff] [blame] | 77 | static const TypeInfo riscv_harts_info = { |
| 78 | .name = TYPE_RISCV_HART_ARRAY, |
| 79 | .parent = TYPE_SYS_BUS_DEVICE, |
| 80 | .instance_size = sizeof(RISCVHartArrayState), |
Michael Clark | 4b50b8d | 2018-03-03 01:31:12 +1300 | [diff] [blame] | 81 | .class_init = riscv_harts_class_init, |
| 82 | }; |
| 83 | |
| 84 | static void riscv_harts_register_types(void) |
| 85 | { |
| 86 | type_register_static(&riscv_harts_info); |
| 87 | } |
| 88 | |
| 89 | type_init(riscv_harts_register_types) |