qualcomm/qbox
Loading...
Searching...
No Matches
cortex-a710.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#pragma once
10
11#include <string>
12
13#include <cci_configuration>
14
15#include <libqemu-cxx/target/aarch64.h>
16
17#include <tlm-extensions/exclusive-access.h>
18#include <module_factory_registery.h>
19
20#include <arm.h>
21#include <ports/qemu-initiator-signal-socket.h>
22#include <ports/qemu-target-signal-socket.h>
23#include <qemu-instance.h>
24
25class cpu_arm_cortexA710 : public QemuCpuArm
26{
27protected:
28 int get_psci_conduit_val() const
29 {
30 if (p_psci_conduit.get_value() == "disabled") {
31 return 0;
32 } else if (p_psci_conduit.get_value() == "smc") {
33 return 1;
34 } else if (p_psci_conduit.get_value() == "hvc") {
35 return 2;
36 } else {
37 /* TODO: report warning */
38 return 0;
39 }
40 }
41
42 void add_exclusive_ext(TlmPayload& pl)
43 {
45 ext->add_hop(m_cpu.get_index());
46 pl.set_extension(ext);
47 }
48
49 static uint64_t extract_data_from_payload(const TlmPayload& pl)
50 {
51 uint8_t* ptr = pl.get_data_ptr() + pl.get_data_length() - 1;
52 uint64_t ret = 0;
53
54 /* QEMU never accesses more than 64 bits at the same time */
55 assert(pl.get_data_length() <= 8);
56
57 while (ptr >= pl.get_data_ptr()) {
58 ret = (ret << 8) | *(ptr--);
59 }
60
61 return ret;
62 }
63
64public:
65 cci::cci_param<unsigned int> p_mp_affinity;
66 cci::cci_param<bool> p_has_el2;
67 cci::cci_param<bool> p_has_el3;
68 cci::cci_param<bool> p_start_powered_off;
69 cci::cci_param<bool> p_en_pauth;
70 cci::cci_param<std::string> p_psci_conduit;
71 cci::cci_param<uint64_t> p_rvbar;
72 cci::cci_param<uint64_t> p_cntfrq_hz;
73
78
79 QemuInitiatorSignalSocket irq_timer_phys_out;
80 QemuInitiatorSignalSocket irq_timer_virt_out;
81 QemuInitiatorSignalSocket irq_timer_hyp_out;
82 QemuInitiatorSignalSocket irq_timer_sec_out;
83 QemuInitiatorSignalSocket irq_maintenance_out;
84 QemuInitiatorSignalSocket irq_pmu_out;
85 cpu_arm_cortexA710(const sc_core::sc_module_name& name, sc_core::sc_object* o)
86 : cpu_arm_cortexA710(name, *(dynamic_cast<QemuInstance*>(o)))
87 {
88 }
89 cpu_arm_cortexA710(const sc_core::sc_module_name& name, QemuInstance& inst)
90 : QemuCpuArm(name, inst, "cortex-a710-arm")
91 , p_mp_affinity("mp_affinity", 0, "Multi-processor affinity value")
92 , p_has_el2("has_el2", true, "ARM virtualization extensions")
93 , p_has_el3("has_el3", true, "ARM secure-mode extensions")
94 , p_en_pauth("enable_pauth", true, "ARM pointer authentication")
95 , p_start_powered_off("start_powered_off", false,
96 "Start and reset the CPU "
97 "in powered-off state")
98 , p_psci_conduit("psci_conduit", "disabled",
99 "Set the QEMU PSCI conduit: "
100 "disabled->no conduit, "
101 "hvc->through hvc call, "
102 "smc->through smc call")
103 , p_rvbar("rvbar", 0ull, "Reset vector base address register value")
104 , p_cntfrq_hz("cntfrq_hz", 0ull, "CPU Generic Timer CNTFRQ in Hz")
105
106 , irq_in("irq_in")
107 , fiq_in("fiq_in")
108 , virq_in("virq_in")
109 , vfiq_in("vfiq_in")
110 , irq_timer_phys_out("irq_timer_phys_out")
111 , irq_timer_virt_out("irq_timer_virt_out")
112 , irq_timer_hyp_out("irq_timer_hyp_out")
113 , irq_timer_sec_out("irq_timer_sec_out")
114 , irq_maintenance_out("gicv3_maintenance_interrupt")
115 , irq_pmu_out("pmu_interrupt")
116 {
117 m_external_ev |= irq_in->default_event();
118 m_external_ev |= fiq_in->default_event();
119 m_external_ev |= virq_in->default_event();
120 m_external_ev |= vfiq_in->default_event();
121 }
122
123 void before_end_of_elaboration() override
124 {
125 QemuCpuArm::before_end_of_elaboration();
126
127 qemu::CpuAarch64 cpu(m_cpu);
128 cpu.set_aarch64_mode(true);
129
130 if (!p_mp_affinity.is_default_value()) {
131 cpu.set_prop_int("mp-affinity", p_mp_affinity);
132 }
133 cpu.set_prop_bool("has_el2", p_has_el2);
134 cpu.set_prop_bool("has_el3", p_has_el3);
135 init_secure_memory(p_has_el3.get_value());
136
137 if (!p_en_pauth.is_default_value()) {
138 cpu.set_prop_bool("pauth", p_en_pauth);
139 }
140 cpu.set_prop_bool("start-powered-off", p_start_powered_off);
141 cpu.set_prop_int("psci-conduit", get_psci_conduit_val());
142
143 cpu.set_prop_int("rvbar", p_rvbar);
144 if (!p_cntfrq_hz.is_default_value()) {
145 cpu.set_prop_int("cntfrq", p_cntfrq_hz);
146 }
147 }
148
149 void end_of_elaboration() override
150 {
151 QemuCpuArm::end_of_elaboration();
152
153 irq_in.init(m_dev, 0);
154 fiq_in.init(m_dev, 1);
155 virq_in.init(m_dev, 2);
156 vfiq_in.init(m_dev, 3);
157
158 irq_timer_phys_out.init(m_dev, 0);
159 irq_timer_virt_out.init(m_dev, 1);
160 irq_timer_hyp_out.init(m_dev, 2);
161 irq_timer_sec_out.init(m_dev, 3);
162 irq_maintenance_out.init_named(m_dev, "gicv3-maintenance-interrupt", 0);
163 irq_pmu_out.init_named(m_dev, "pmu-interrupt", 0);
164 }
165
166 void initiator_customize_tlm_payload(TlmPayload& payload) override
167 {
168 uint64_t addr;
169 qemu::CpuAarch64 arm_cpu(m_cpu);
170
171 QemuCpu::initiator_customize_tlm_payload(payload);
172
173 addr = payload.get_address();
174
175 if (!arm_cpu.is_in_exclusive_context()) {
176 return;
177 }
178
179 if (addr != arm_cpu.get_exclusive_addr()) {
180 return;
181 }
182
183 /*
184 * We are in the load/store pair of the cmpxchg of the exclusive store
185 * implementation. Add the exclusive access extension to lock the
186 * memory and check for exclusive store success in
187 * initiator_tidy_tlm_payload.
188 */
189 add_exclusive_ext(payload);
190 }
191
192 void initiator_tidy_tlm_payload(TlmPayload& payload) override
193 {
194 using namespace tlm;
195
197 qemu::CpuAarch64 arm_cpu(m_cpu);
198
199 QemuCpu::initiator_tidy_tlm_payload(payload);
200
201 payload.get_extension(ext);
202 bool exit_tb = false;
203
204 if (ext == nullptr) {
205 return;
206 }
207
208 if (payload.get_command() == TLM_WRITE_COMMAND) {
209 auto sta = ext->get_exclusive_store_status();
210
211 /* We just executed an exclusive store. Check its status */
212 if (sta == ExclusiveAccessTlmExtension::EXCLUSIVE_STORE_FAILURE) {
213 /*
214 * To actually make the exclusive store fails, we need to trick
215 * QEMU into thinking that the value at the store address has
216 * changed compared to when it did the corresponding ldrex (due
217 * to the way exclusive loads/stores are implemented in QEMU).
218 * For this, we simply smash the exclusive_val field of the ARM
219 * CPU state in case it currently matches with the value in
220 * memory.
221 */
222 uint64_t exclusive_val = arm_cpu.get_exclusive_val();
223 uint64_t mem_val = extract_data_from_payload(payload);
224 uint64_t mask = (payload.get_data_length() == 8) ? -1 : (1 << (8 * payload.get_data_length())) - 1;
225
226 if ((exclusive_val & mask) == mem_val) {
227 arm_cpu.set_exclusive_val(~exclusive_val);
228
229 /*
230 * Exit the execution loop to force QEMU to re-do the
231 * store. This is necessary because we modify exclusive_val
232 * in the CPU env. This field is also mapped on a TCG
233 * global. Even though the qemu_st_ixx TCG opcs are marked
234 * TCG_OPF_CALL_CLOBBER, TCG does not reload the global
235 * after the store as I thought it would do. To force this,
236 * we exit the TB here so that the new exclusive_val value
237 * will be taken into account on the next execution.
238 */
239 exit_tb = true;
240 }
241
242 payload.set_response_status(TLM_OK_RESPONSE);
243 }
244 }
245
246 payload.clear_extension(ext);
247 delete ext;
248
249 if (exit_tb) {
250 /*
251 * FIXME: exiting the CPU loop from here is a bit violent. The
252 * caller won't have a chance to destruct its stack objects. The
253 * object model should be reworked to allow exiting the loop
254 * cleanly.
255 */
256 m_cpu.exit_loop_from_io();
257 }
258 }
259};
260
261extern "C" void module_register();
Exclusive load/store TLM extension.
Definition exclusive-access.h:36
A QEMU output GPIO exposed as a InitiatorSignalSocket<bool>.
Definition qemu-initiator-signal-socket.h:40
This class encapsulates a libqemu-cxx qemu::LibQemu instance. It handles QEMU parameters and instance...
Definition qemu-instance.h:89
A QEMU input GPIO exposed as a TargetSignalSocket<bool>.
Definition qemu-target-signal-socket.h:29
Definition aarch64.h:40