blob: 06cb08913d86c1408ebc3a20d34ea50f4ffbaae5 [file] [log] [blame]
Madhukar Pappireddy457af4c2022-12-22 16:13:37 -06001/*
2 * Copyright 2023 The Hafnium Authors.
3 *
4 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
7 */
8
Madhukar Pappireddy66421182022-12-22 16:50:09 -06009#include "hf/arch/irq.h"
Madhukar Pappireddyace2ff72023-05-03 15:45:06 -050010#include "hf/arch/vm/delay.h"
Madhukar Pappireddy66421182022-12-22 16:50:09 -060011#include "hf/arch/vm/interrupts_gicv3.h"
Madhukar Pappireddy6c23d432023-07-24 16:39:41 -050012#include "hf/arch/vm/power_mgmt.h"
Madhukar Pappireddy66421182022-12-22 16:50:09 -060013#include "hf/arch/vm/timer.h"
Madhukar Pappireddy4d16db62022-12-22 16:38:10 -060014
Madhukar Pappireddy457af4c2022-12-22 16:13:37 -060015#include "ffa_secure_partitions.h"
Madhukar Pappireddy66421182022-12-22 16:50:09 -060016#include "gicv3.h"
Madhukar Pappireddy457af4c2022-12-22 16:13:37 -060017#include "partition_services.h"
Madhukar Pappireddy9a1b9c02024-01-04 17:14:16 -060018#include "sp805.h"
Madhukar Pappireddy457af4c2022-12-22 16:13:37 -060019#include "sp_helpers.h"
20
21#define SP_SLEEP_TIME 400U
Madhukar Pappireddy4d16db62022-12-22 16:38:10 -060022#define NS_SLEEP_TIME 200U
23
Madhukar Pappireddy6c23d432023-07-24 16:39:41 -050024#define LAST_SECONDARY_VCPU_ID (MAX_CPUS - 1)
25#define MID_SECONDARY_VCPU_ID (MAX_CPUS / 2)
26
27alignas(4096) static uint8_t secondary_ec_stack[MAX_CPUS - 1][PAGE_SIZE];
28
29struct secondary_cpu_entry_args {
30 ffa_id_t receiver_id;
31 ffa_vcpu_count_t vcpu_count;
32 ffa_vcpu_index_t vcpu_id;
33 struct spinlock lock;
34 ffa_vcpu_index_t target_vcpu_id;
35};
36
J-Alves19e20cf2023-08-02 12:48:55 +010037static void configure_trusted_wdog_interrupt(ffa_id_t source, ffa_id_t dest,
38 bool enable)
Madhukar Pappireddy457af4c2022-12-22 16:13:37 -060039{
40 struct ffa_value res;
41
42 res = sp_virtual_interrupt_cmd_send(source, dest, IRQ_TWDOG_INTID,
43 enable, 0);
44
45 EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32);
46 EXPECT_EQ(sp_resp(res), SP_SUCCESS);
47}
48
J-Alves19e20cf2023-08-02 12:48:55 +010049static void enable_trusted_wdog_interrupt(ffa_id_t source, ffa_id_t dest)
Madhukar Pappireddy457af4c2022-12-22 16:13:37 -060050{
51 configure_trusted_wdog_interrupt(source, dest, true);
52}
53
J-Alves19e20cf2023-08-02 12:48:55 +010054static void disable_trusted_wdog_interrupt(ffa_id_t source, ffa_id_t dest)
Madhukar Pappireddy457af4c2022-12-22 16:13:37 -060055{
56 configure_trusted_wdog_interrupt(source, dest, false);
57}
58
J-Alves19e20cf2023-08-02 12:48:55 +010059static void enable_trigger_trusted_wdog_timer(ffa_id_t own_id,
60 ffa_id_t receiver_id,
Madhukar Pappireddy457af4c2022-12-22 16:13:37 -060061 uint32_t timer_ms)
62{
63 struct ffa_value res;
64
65 /* Enable trusted watchdog interrupt as vIRQ in the secure side. */
66 enable_trusted_wdog_interrupt(own_id, receiver_id);
67
68 res = sp_twdog_map_cmd_send(own_id, receiver_id);
69
70 EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32);
71 EXPECT_EQ(sp_resp(res), SP_SUCCESS);
72
73 /*
74 * Send a message to the SP through direct messaging requesting it to
75 * start the trusted watchdog timer.
76 */
77 res = sp_twdog_cmd_send(own_id, receiver_id, timer_ms);
78
79 EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32);
80 EXPECT_EQ(sp_resp(res), SP_SUCCESS);
81}
82
J-Alves19e20cf2023-08-02 12:48:55 +010083static void check_and_disable_trusted_wdog_timer(ffa_id_t own_id,
84 ffa_id_t receiver_id)
Madhukar Pappireddy457af4c2022-12-22 16:13:37 -060085{
86 struct ffa_value res;
87
88 /* Check for the last serviced secure virtual interrupt. */
89 res = sp_get_last_interrupt_cmd_send(own_id, receiver_id);
90
91 EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32);
92 EXPECT_EQ(sp_resp(res), SP_SUCCESS);
93
94 /* Make sure Trusted Watchdog timer interrupt was serviced. */
95 EXPECT_EQ(sp_resp_value(res), IRQ_TWDOG_INTID);
96
97 /* Disable Trusted Watchdog interrupt. */
98 disable_trusted_wdog_interrupt(own_id, receiver_id);
99}
100
101/*
102 * Test secure interrupt handling while the Secure Partition is in RUNNING
103 * state.
104 */
105TEST(secure_interrupts, sp_running)
106{
107 struct ffa_value res;
J-Alves19e20cf2023-08-02 12:48:55 +0100108 ffa_id_t own_id = hf_vm_get_id();
J-Alves5d6926a2022-12-08 14:44:28 +0000109 struct mailbox_buffers mb = set_up_mailbox();
110 struct ffa_partition_info *service2_info = service2(mb.recv);
J-Alves19e20cf2023-08-02 12:48:55 +0100111 const ffa_id_t receiver_id = service2_info->vm_id;
Madhukar Pappireddy457af4c2022-12-22 16:13:37 -0600112
113 enable_trigger_trusted_wdog_timer(own_id, receiver_id, 400);
114
115 /* Send request to the SP to sleep. */
Madhukar Pappireddy3aafcb62023-12-06 11:49:04 -0600116 res = sp_sleep_cmd_send(own_id, receiver_id, SP_SLEEP_TIME, 0);
Madhukar Pappireddy457af4c2022-12-22 16:13:37 -0600117
118 /*
119 * Secure interrupt should trigger during this time, SP will handle the
120 * trusted watchdog timer interrupt.
121 */
122 EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32);
123 EXPECT_EQ(sp_resp(res), SP_SUCCESS);
124
125 /* Make sure elapsed time not less than sleep time. */
126 EXPECT_GE(sp_resp_value(res), SP_SLEEP_TIME);
127
128 check_and_disable_trusted_wdog_timer(own_id, receiver_id);
129}
Madhukar Pappireddy4d16db62022-12-22 16:38:10 -0600130
131/*
Madhukar Pappireddyed7dee82023-12-06 11:55:15 -0600132 * Test secure interrupt handling while the Secure Partition runs with
133 * interrupts potentially masked. This test helps to validate the functionality
134 * of the SPMC to intercept a direct response message if there are pending
135 * virtual secure interrupts and reschedule the partition to handle the pending
136 * interrupt.
137 */
138TEST(secure_interrupts, sp_direct_response_intercepted)
139{
140 struct ffa_value res;
141 ffa_id_t own_id = hf_vm_get_id();
142 struct mailbox_buffers mb = set_up_mailbox();
143 struct ffa_partition_info *service2_info = service2(mb.recv);
144 const ffa_id_t receiver_id = service2_info->vm_id;
145
146 enable_trigger_trusted_wdog_timer(own_id, receiver_id, 400);
147
148 /* Send request to the SP to sleep uninterrupted. */
149 res = sp_sleep_cmd_send(own_id, receiver_id, SP_SLEEP_TIME, 1);
150
151 /*
152 * Secure interrupt should trigger during this time, SP will handle the
153 * trusted watchdog timer interrupt.
154 */
155 EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32);
156 EXPECT_EQ(sp_resp(res), SP_SUCCESS);
157
158 /* Make sure elapsed time not less than sleep time. */
159 EXPECT_GE(sp_resp_value(res), SP_SLEEP_TIME);
160
161 check_and_disable_trusted_wdog_timer(own_id, receiver_id);
162}
163
164/*
Madhukar Pappireddy4d16db62022-12-22 16:38:10 -0600165 * Test secure interrupt handling while the Secure Partition is in WAITING
166 * state.
167 */
168TEST(secure_interrupts, sp_waiting)
169{
J-Alves19e20cf2023-08-02 12:48:55 +0100170 ffa_id_t own_id = hf_vm_get_id();
J-Alves5d6926a2022-12-08 14:44:28 +0000171 struct mailbox_buffers mb = set_up_mailbox();
172 struct ffa_partition_info *service2_info = service2(mb.recv);
J-Alves19e20cf2023-08-02 12:48:55 +0100173 const ffa_id_t receiver_id = service2_info->vm_id;
Madhukar Pappireddy4d16db62022-12-22 16:38:10 -0600174 uint64_t time1;
175 volatile uint64_t time_lapsed;
176 uint64_t timer_freq = read_msr(cntfrq_el0);
177
178 enable_trigger_trusted_wdog_timer(own_id, receiver_id, 100);
179 time1 = syscounter_read();
180
181 /*
182 * Sleep for NS_SLEEP_TIME ms. This ensures secure wdog timer triggers
183 * during this time.
184 */
185 waitms(NS_SLEEP_TIME);
186
187 /* Lapsed time should be at least equal to sleep time. */
188 time_lapsed = ((syscounter_read() - time1) * 1000) / timer_freq;
189
190 EXPECT_GE(time_lapsed, NS_SLEEP_TIME);
191
192 check_and_disable_trusted_wdog_timer(own_id, receiver_id);
193}
Madhukar Pappireddy8cc6deb2022-12-22 16:46:04 -0600194
195/*
196 * Test secure interrupt handling while the Secure Partition is in BLOCKED
197 * state.
198 */
199TEST(secure_interrupts, sp_blocked)
200{
201 struct ffa_value res;
J-Alves19e20cf2023-08-02 12:48:55 +0100202 ffa_id_t own_id = hf_vm_get_id();
J-Alves5d6926a2022-12-08 14:44:28 +0000203 struct mailbox_buffers mb = set_up_mailbox();
204 struct ffa_partition_info *service1_info = service1(mb.recv);
205 struct ffa_partition_info *service2_info = service2(mb.recv);
J-Alves19e20cf2023-08-02 12:48:55 +0100206 const ffa_id_t receiver_id = service2_info->vm_id;
207 const ffa_id_t companion_id = service1_info->vm_id;
Madhukar Pappireddy8cc6deb2022-12-22 16:46:04 -0600208
209 enable_trigger_trusted_wdog_timer(own_id, receiver_id, 400);
210
211 /*
212 * Send command to receiver SP to send command to companion SP to sleep
213 * there by putting receiver SP in BLOCKED state.
214 */
215 res = sp_fwd_sleep_cmd_send(own_id, receiver_id, companion_id,
216 SP_SLEEP_TIME, false);
217
218 /*
219 * Secure interrupt should trigger during this time, receiver SP will
220 * handle the trusted watchdog timer and sends direct response message.
221 */
222 EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32);
223 EXPECT_EQ(sp_resp(res), SP_SUCCESS);
224
225 check_and_disable_trusted_wdog_timer(own_id, receiver_id);
226}
Madhukar Pappireddy66421182022-12-22 16:50:09 -0600227
228TEST(secure_interrupts, sp_preempted)
229{
230 struct ffa_value res;
J-Alves19e20cf2023-08-02 12:48:55 +0100231 ffa_id_t own_id = hf_vm_get_id();
J-Alves5d6926a2022-12-08 14:44:28 +0000232 struct mailbox_buffers mb = set_up_mailbox();
233 struct ffa_partition_info *service2_info = service2(mb.recv);
J-Alves19e20cf2023-08-02 12:48:55 +0100234 const ffa_id_t receiver_id = service2_info->vm_id;
Madhukar Pappireddy66421182022-12-22 16:50:09 -0600235
236 gicv3_system_setup();
237 interrupt_enable(PHYSICAL_TIMER_IRQ, true);
238 interrupt_set_priority(PHYSICAL_TIMER_IRQ, 0x80);
239 interrupt_set_edge_triggered(PHYSICAL_TIMER_IRQ, true);
240 interrupt_set_priority_mask(0xff);
241 arch_irq_enable();
242
243 /* Set physical timer for 20 ms and enable. */
244 write_msr(CNTP_TVAL_EL0, ns_to_ticks(20000000));
245 write_msr(CNTP_CTL_EL0, CNTx_CTL_ENABLE_MASK);
246
247 enable_trigger_trusted_wdog_timer(own_id, receiver_id, 200);
248
249 /* Send request to receiver SP to sleep. */
Madhukar Pappireddy3aafcb62023-12-06 11:49:04 -0600250 res = sp_sleep_cmd_send(own_id, receiver_id, 50, 0);
Madhukar Pappireddy66421182022-12-22 16:50:09 -0600251
252 /* SP is pre-empted by the non-secure timer interrupt. */
253 EXPECT_EQ(res.func, FFA_INTERRUPT_32);
254
255 /* VM id/vCPU index are passed through arg1. */
256 EXPECT_EQ(res.arg1, ffa_vm_vcpu(receiver_id, 0));
257
258 /* Waiting for interrupt to be serviced in normal world. */
259 while (last_interrupt_id == 0) {
260 EXPECT_EQ(io_read32_array(GICD_ISPENDR, 0), 0);
261 EXPECT_EQ(io_read32(GICR_ISPENDR0), 0);
262 EXPECT_EQ(io_read32_array(GICD_ISACTIVER, 0), 0);
263 EXPECT_EQ(io_read32(GICR_ISACTIVER0), 0);
264 }
265
266 /* Check that we got the interrupt. */
267 EXPECT_EQ(last_interrupt_id, PHYSICAL_TIMER_IRQ);
268
269 /* Check timer status. */
270 EXPECT_EQ(read_msr(CNTP_CTL_EL0),
271 CNTx_CTL_ISTS_MASK | CNTx_CTL_ENABLE_MASK);
272
273 /*
274 * NS Interrupt has been serviced and receiver SP is now in PREEMPTED
275 * state. Wait for trusted watchdog timer to be fired. SPMC queues
276 * the secure virtual interrupt.
277 */
278 waitms(NS_SLEEP_TIME);
279
280 /*
281 * Resume the SP to complete the busy loop, handle the secure virtual
282 * interrupt and return with success.
283 */
284 res = ffa_run(ffa_vm_id(res), ffa_vcpu_index(res));
285 EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32);
286 EXPECT_EQ(res.arg3, SP_SUCCESS);
287
288 check_and_disable_trusted_wdog_timer(own_id, receiver_id);
289}
Madhukar Pappireddy7adccf72023-01-31 15:16:47 -0600290
291/*
292 * Test Secure Partition runs to completion if it specifies action in response
293 * to Other-S Interrupt as queued.
294 */
295TEST(secure_interrupts, sp_other_s_interrupt_queued)
296{
297 struct ffa_value res;
J-Alves19e20cf2023-08-02 12:48:55 +0100298 ffa_id_t own_id = hf_vm_get_id();
Madhukar Pappireddy7adccf72023-01-31 15:16:47 -0600299 struct mailbox_buffers mb = set_up_mailbox();
300 struct ffa_partition_info *service2_info = service2(mb.recv);
301 struct ffa_partition_info *service3_info = service3(mb.recv);
302
303 /*
304 * Service2 SP is the target of trusted watchdog timer interrupt.
305 * Service3 SP specified action to Other-S Interrupt as queued.
306 */
J-Alves19e20cf2023-08-02 12:48:55 +0100307 const ffa_id_t target_id = service2_info->vm_id;
308 const ffa_id_t receiver_id = service3_info->vm_id;
Madhukar Pappireddy7adccf72023-01-31 15:16:47 -0600309
310 enable_trigger_trusted_wdog_timer(own_id, target_id, 400);
311
312 /*
313 * Send command to receiver SP(Service3) to sleep for SP_SLEEP_TIME
314 * ms. Secure interrupt should trigger while SP is busy in running the
315 * sleep command. SPMC queues the virtual interrupt and resumes the
316 * SP.
317 */
Madhukar Pappireddy3aafcb62023-12-06 11:49:04 -0600318 res = sp_sleep_cmd_send(own_id, receiver_id, SP_SLEEP_TIME, 0);
Madhukar Pappireddy7adccf72023-01-31 15:16:47 -0600319
320 /* Service3 SP finishes and sends direct response back. */
321 EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32);
322 EXPECT_EQ(sp_resp(res), SP_SUCCESS);
323
324 /*
325 * Allocate cycles to target SP for it to handle the virtual secure
326 * interrupt.
327 */
Madhukar Pappireddy3aafcb62023-12-06 11:49:04 -0600328 res = sp_sleep_cmd_send(own_id, target_id, 10, 0);
Madhukar Pappireddy7adccf72023-01-31 15:16:47 -0600329
330 /*
331 * Secure interrupt should trigger during this time, SP will handle the
332 * trusted watchdog timer interrupt.
333 */
334 EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32);
335 EXPECT_EQ(sp_resp(res), SP_SUCCESS);
336
337 /*
338 * Check if the trusted watchdog timer interrupt has been handled.
339 */
340 check_and_disable_trusted_wdog_timer(own_id, target_id);
341}
Madhukar Pappireddyef062762023-05-23 17:46:57 -0500342
343/*
344 * Test that an SP can attempt to yield CPU cycles while handling secure
345 * interrupt by invoking FFA_YIELD.
346 */
347TEST(secure_interrupts, sp_yield_sec_interrupt_handling)
348{
349 struct ffa_value res;
J-Alves19e20cf2023-08-02 12:48:55 +0100350 ffa_id_t own_id = hf_vm_get_id();
Madhukar Pappireddyef062762023-05-23 17:46:57 -0500351 struct mailbox_buffers mb = set_up_mailbox();
352 struct ffa_partition_info *service2_info = service2(mb.recv);
J-Alves19e20cf2023-08-02 12:48:55 +0100353 const ffa_id_t receiver_id = service2_info->vm_id;
Madhukar Pappireddyef062762023-05-23 17:46:57 -0500354 uint64_t time1;
355 volatile uint64_t time_lapsed;
356 uint64_t timer_freq = read_msr(cntfrq_el0);
357
358 /*
359 * Send command to SP asking it attempt to yield cycles while handling
360 * secure interrupt.
361 */
362 res = sp_yield_secure_interrupt_handling_cmd_send(own_id, receiver_id,
363 true);
364
365 EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32);
366 EXPECT_EQ(sp_resp(res), SP_SUCCESS);
367
368 enable_trigger_trusted_wdog_timer(own_id, receiver_id, 75);
369 time1 = syscounter_read();
370
371 /*
372 * Sleep for 100ms. This ensures secure wdog timer triggers
373 * during this time. SP starts handling secure interrupt but attempts
374 * to yields cycles. However, SPMC just resumes the SP to complete
375 * interrupt handling.
376 */
377 waitms(100);
378
379 /* Lapsed time should be at least equal to sleep time. */
380 time_lapsed = ((syscounter_read() - time1) * 1000) / timer_freq;
381
382 EXPECT_GE(time_lapsed, 100);
383
384 res = sp_yield_secure_interrupt_handling_cmd_send(own_id, receiver_id,
385 false);
386
387 EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32);
388 EXPECT_EQ(sp_resp(res), SP_SUCCESS);
389 check_and_disable_trusted_wdog_timer(own_id, receiver_id);
390}
Madhukar Pappireddy6c23d432023-07-24 16:39:41 -0500391
392static void cpu_entry_sp_sleep_loop(uintptr_t arg)
393{
394 ffa_id_t own_id = hf_vm_get_id();
395 struct ffa_value res;
396 struct secondary_cpu_entry_args *args =
397 // NOLINTNEXTLINE(performance-no-int-to-ptr)
398 (struct secondary_cpu_entry_args *)arg;
399 bool is_receiver_up_sp = args->vcpu_count == 1;
400
401 /*
402 * Execution context(s) of secondary Secure Partitions need CPU cycles
403 * to be allocated through FFA_RUN interface to reach message loop.
404 */
405 if (is_receiver_up_sp) {
406 res = ffa_run(args->receiver_id, (ffa_vcpu_index_t)0);
407 } else {
408 res = ffa_run(args->receiver_id, args->vcpu_id);
409 }
410
411 EXPECT_EQ(ffa_func_id(res), FFA_MSG_WAIT_32);
412
413 /* Prepare for the trusted watchdog interrupt routed to target vCPU. */
414 if (args->vcpu_id == args->target_vcpu_id) {
415 res = sp_route_interrupt_to_target_vcpu_cmd_send(
416 own_id, args->receiver_id, args->target_vcpu_id,
417 IRQ_TWDOG_INTID);
418
419 EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32);
420 EXPECT_EQ(sp_resp(res), SP_SUCCESS);
421
422 /*
423 * Make sure that twdog timer triggers shortly before the
424 * sleep duration ends.
425 */
426 enable_trigger_trusted_wdog_timer(own_id, args->receiver_id,
427 SP_SLEEP_TIME - 50);
428 }
429
430 /* Send request to the SP to sleep. */
Madhukar Pappireddy3aafcb62023-12-06 11:49:04 -0600431 res = sp_sleep_cmd_send(own_id, args->receiver_id, SP_SLEEP_TIME, 0);
Madhukar Pappireddy6c23d432023-07-24 16:39:41 -0500432 EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32);
433 EXPECT_EQ(sp_resp(res), SP_SUCCESS);
434
435 /* Make sure elapsed time not less than sleep time. */
436 EXPECT_GE(sp_resp_value(res), SP_SLEEP_TIME);
437
438 /* Check for the last serviced secure virtual interrupt. */
439 res = sp_get_last_interrupt_cmd_send(own_id, args->receiver_id);
440
441 EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32);
442 EXPECT_EQ(sp_resp(res), SP_SUCCESS);
443
444 /*
445 * Expect the target execution context of Service2 SP to handle the
446 * trusted watchdog interrupt succesfully.
447 */
448 if (args->vcpu_id == args->target_vcpu_id) {
449 EXPECT_EQ(sp_resp_value(res), IRQ_TWDOG_INTID);
450 } else {
451 /*
452 * Make sure Trusted Watchdog timer interrupt was not serviced
453 * by this execution context.
454 */
455 EXPECT_NE(sp_resp_value(res), IRQ_TWDOG_INTID);
456 }
457
458 /* Clear last serviced secure virtual interrupt. */
459 res = sp_clear_last_interrupt_cmd_send(own_id, args->receiver_id);
460
461 EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32);
462 EXPECT_EQ(sp_resp(res), SP_SUCCESS);
463
464 /* Releases the lock passed in. */
465 sl_unlock(&args->lock);
466 arch_cpu_stop();
467}
468
469static void sp_route_interrupt_to_secondary_vcpu_base(
470 struct secondary_cpu_entry_args args)
471{
472 /* Start secondary EC while holding lock. */
473 sl_lock(&args.lock);
474
475 for (ffa_vcpu_index_t i = 1; i < MAX_CPUS; i++) {
476 uintptr_t cpu_id;
477 ffa_vcpu_index_t hftest_cpu_index = MAX_CPUS - i;
478
479 cpu_id = hftest_get_cpu_id(hftest_cpu_index);
480 args.vcpu_id = i;
481 HFTEST_LOG("Booting CPU %u - %x", i, cpu_id);
482
483 EXPECT_EQ(hftest_cpu_start(cpu_id, secondary_ec_stack[i - 1],
484 sizeof(secondary_ec_stack[0]),
485 cpu_entry_sp_sleep_loop,
486 (uintptr_t)&args),
487 true);
488
489 /* Wait for CPU to release the lock. */
490 sl_lock(&args.lock);
491
492 HFTEST_LOG("Done with CPU %u", i);
493 }
494}
495
496/*
497 * Test a Secure Partition can request the SPMC to reconfigure an interrupt to
498 * be routed to a secondary vCPU.
499 */
500TEST(secure_interrupts, sp_route_interrupt_to_secondary_vcpu)
501{
502 struct secondary_cpu_entry_args args = {.lock = SPINLOCK_INIT};
503 struct mailbox_buffers mb = set_up_mailbox();
504 struct ffa_partition_info *service2_info = service2(mb.recv);
505 const ffa_id_t receiver_id = service2_info->vm_id;
506
507 args.receiver_id = receiver_id;
508 args.vcpu_count = service2_info->vcpu_count;
509
510 /*
511 * Reconfigure the twdog interrupt to be routed to last secondary
512 * execution context of SP.
513 */
514 args.target_vcpu_id = LAST_SECONDARY_VCPU_ID;
515 sp_route_interrupt_to_secondary_vcpu_base(args);
516
517 /*
518 * Reconfigure the twdog interrupt to be routed to mid secondary
519 * execution context of SP.
520 */
521 args.target_vcpu_id = MID_SECONDARY_VCPU_ID;
522 sp_route_interrupt_to_secondary_vcpu_base(args);
523}