qualcomm/qbox
Loading...
Searching...
No Matches
arm_gicv3.h
1/*
2 * This file is part of libqbox
3 * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All Rights Reserved.
4 * Author: GreenSocs 2020
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8
9#ifndef _LIBQBOX_COMPONENTS_IRQ_CTRL_ARM_GICV3_H
10#define _LIBQBOX_COMPONENTS_IRQ_CTRL_ARM_GICV3_H
11
12#include <sstream>
13
14#include <systemc>
15#include <cci_configuration>
16
17#include <global_peripheral_initiator.h>
18#include <libqemu-cxx/libqemu-cxx.h>
19
20#include <device.h>
21#include <ports/target.h>
22#include <ports/qemu-initiator-signal-socket.h>
23#include <ports/qemu-target-signal-socket.h>
24#include <qemu-instance.h>
25#include <module_factory_registery.h>
26
27class arm_gicv3 : public QemuDevice
28{
29public:
30 static const uint32_t NUM_PPI = 32;
31
32protected:
33 cci::cci_param<unsigned int> p_num_cpu;
34 cci::cci_param<unsigned int> p_first_cpu_index;
35 cci::cci_param<unsigned int> p_num_spi;
36 cci::cci_param<unsigned int> p_revision;
37 cci::cci_param<std::vector<unsigned int> > p_redist_region;
38 cci::cci_param<bool> p_has_security_extensions;
39 cci::cci_param<bool> p_has_lpi; // Locality specific Peripheral Interrupts
40public:
41 QemuTargetSocket<> dist_iface;
42 sc_core::sc_vector<QemuTargetSocket<> > redist_iface;
43
44 /* Shared peripheral interrupts: sized with the p_num_spi parameter */
45 sc_core::sc_vector<QemuTargetSignalSocket> spi_in;
46
47 /*
48 * Private peripheral interrupts: 32 per CPUs, the outer vector is sized with the
49 * number of CPUs.
50 */
51 sc_core::sc_vector<sc_core::sc_vector<QemuTargetSignalSocket> > ppi_in;
52
53 /* Output interrupt lines. Vector are sized with the number of CPUs */
54 sc_core::sc_vector<QemuInitiatorSignalSocket> irq_out;
55 sc_core::sc_vector<QemuInitiatorSignalSocket> fiq_out;
56 sc_core::sc_vector<QemuInitiatorSignalSocket> virq_out;
57 sc_core::sc_vector<QemuInitiatorSignalSocket> vfiq_out;
58 arm_gicv3(const sc_core::sc_module_name& name, sc_core::sc_object* o)
59 : arm_gicv3(name, *(dynamic_cast<QemuInstance*>(o)))
60 {
61 }
62
63 static const char* get_gicv3_type(QemuInstance& inst)
64 {
65 if (inst.is_kvm_enabled()) {
66 return "kvm-arm-gicv3";
67 } else if (inst.is_whpx_enabled()) {
68 return "whpx-arm-gicv3";
69 } else {
70 return "arm-gicv3";
71 }
72 }
73
74 arm_gicv3(const sc_core::sc_module_name& name, QemuInstance& inst, unsigned num_cpus = 0)
75 : QemuDevice(name, inst, get_gicv3_type(inst))
76 , p_num_cpu("num_cpus", num_cpus, "Number of CPU interfaces")
77 , p_first_cpu_index("first_cpu_index", 0, "index of the first cpu attached to the GIC (default 0)")
78 , p_num_spi("num_spi", 0, "Number of shared peripheral interrupts")
79 , p_revision("revision", 3, "Revision of the GIC (3 -> v3, 4 -> v4)")
80 // , p_redist_region("redist_region", std::vector<unsigned int>({}),
81 // "Redistributor regions configuration")
82 , p_redist_region("redist_region",
83 std::vector<unsigned int>(gs::cci_get_vector<unsigned int>(
84 cci::cci_get_broker(), std::string(sc_module::name()) + ".redist_region")),
85 "Redistributor regions configuration")
86 , p_has_security_extensions("has_security_extensions", false, "Enable security extensions")
87 , p_has_lpi("has_lpi", false, "Enable LPI")
88 , dist_iface("dist_iface", inst)
89 , redist_iface("redist_iface", p_redist_region.get_value().size(),
90 [&inst](const char* n, int i) { return new QemuTargetSocket<>(n, inst); })
91 , spi_in("spi_in", p_num_spi)
92 , ppi_in("ppi_in_cpu", p_num_cpu,
93 [](const char* n, size_t i) { return new sc_core::sc_vector<QemuTargetSignalSocket>(n, NUM_PPI); })
94 , irq_out("irq_out", p_num_cpu)
95 , fiq_out("fiq_out", p_num_cpu)
96 , virq_out("virq_out", p_num_cpu)
97 , vfiq_out("vfiq_out", p_num_cpu)
98 {
99 }
100
101 void before_end_of_elaboration()
102 {
103 QemuDevice::before_end_of_elaboration();
104 int i;
105
106 m_dev.set_prop_int("num-cpu", p_num_cpu);
107 m_dev.set_prop_int("first-cpu-index", p_first_cpu_index);
108 m_dev.set_prop_int("num-irq", p_num_spi + NUM_PPI);
109 m_dev.set_prop_int("revision", p_revision);
110
111 bool irqchip_in_kernel = m_inst.is_kvm_enabled() || m_inst.is_whpx_enabled();
112 bool has_security_extensions = irqchip_in_kernel ? false : p_has_security_extensions.get_value();
113 bool has_lpi = irqchip_in_kernel ? false : p_has_lpi.get_value();
114 m_dev.set_prop_bool("has-security-extensions", has_security_extensions);
115 m_dev.set_prop_bool("has-lpi", has_lpi);
116 if (has_lpi) {
117 m_dev.set_prop_link("sysmem", *(this->get_qemu_inst().get().get_system_memory()));
118 }
119 m_dev.set_prop_uint_array("redist-region-count", p_redist_region.get_value());
120 }
121
122 void end_of_elaboration()
123 {
124 QemuDevice::set_sysbus_as_parent_bus();
125 QemuDevice::end_of_elaboration();
126
127 qemu::SysBusDevice sbd(m_dev);
128 int cpu, i;
129
130 dist_iface.init(sbd, 0);
131
132 for (i = 0; i < p_redist_region.get_value().size(); i++) {
133 redist_iface[i].init(sbd, 1 + i);
134 }
135
136 /* SPIs in */
137 for (i = 0; i < p_num_spi; i++) {
138 spi_in[i].init(m_dev, i);
139 }
140
141 /* PPIs in */
142 for (cpu = 0; cpu < p_num_cpu; cpu++) {
143 for (i = 0; i < NUM_PPI; i++) {
144 int ppi_idx = p_num_spi + cpu * NUM_PPI + i;
145 ppi_in[cpu][i].init(m_dev, ppi_idx);
146 }
147 }
148
149 /* Output lines */
150 for (cpu = 0; cpu < p_num_cpu; cpu++) {
151 irq_out[cpu].init_sbd(sbd, p_num_cpu * 0 + cpu);
152 fiq_out[cpu].init_sbd(sbd, p_num_cpu * 1 + cpu);
153 virq_out[cpu].init_sbd(sbd, p_num_cpu * 2 + cpu);
154 vfiq_out[cpu].init_sbd(sbd, p_num_cpu * 3 + cpu);
155 }
156
157 if (m_inst.is_kvm_enabled() || m_inst.is_whpx_enabled()) {
158 uint64_t val;
160 sc_core::sc_object* init_obj = gs::find_sc_obj(nullptr, "platform.global_peripheral_initiator_arm_0");
161 auto init = dynamic_cast<global_peripheral_initiator*>(init_obj);
162 assert(init && "Failed to find global peripheral initiator");
163
164 m_inst.get().lock_iothread();
165 uint64_t addr;
166 addr = gs::cci_get<uint64_t>(cci::cci_get_broker(), std::string(dist_iface.name()) + ".address");
167 init->m_initiator.qemu_io_read(addr, &val, sizeof(val), attrs);
168 for (i = 0; i < p_redist_region.get_value().size(); i++) {
169 addr = gs::cci_get<uint64_t>(cci::cci_get_broker(), std::string(redist_iface[i].name()) + ".address");
170 init->m_initiator.qemu_io_read(addr, &val, sizeof(val), attrs);
171 }
172 m_inst.get().unlock_iothread();
173 }
174 }
175};
176
177extern "C" void module_register();
178#endif
QemuDevice(const sc_core::sc_module_name &name, QemuInstance &inst, const char *qom_type)
Construct a QEMU device.
Definition device.h:86
This class encapsulates a libqemu-cxx qemu::LibQemu instance. It handles QEMU parameters and instance...
Definition qemu-instance.h:89
Definition target.h:194
Definition global_peripheral_initiator.h:18
Definition libqemu-cxx.h:699
sc_core::sc_object * find_sc_obj(sc_core::sc_object *m, std::string name, bool test=false)
Helper function to find a sc_object by fully qualified name throws SC_ERROR if nothing found.
Definition cciutils.cc:18
Definition libqemu-cxx.h:352