Madhukar Pappireddy | 457af4c | 2022-12-22 16:13:37 -0600 | [diff] [blame^] | 1 | /* |
| 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 | |
| 9 | #include "ffa_secure_partitions.h" |
| 10 | #include "partition_services.h" |
| 11 | #include "sp_helpers.h" |
| 12 | |
| 13 | #define SP_SLEEP_TIME 400U |
| 14 | |
| 15 | static void configure_trusted_wdog_interrupt(ffa_vm_id_t source, |
| 16 | ffa_vm_id_t dest, bool enable) |
| 17 | { |
| 18 | struct ffa_value res; |
| 19 | |
| 20 | res = sp_virtual_interrupt_cmd_send(source, dest, IRQ_TWDOG_INTID, |
| 21 | enable, 0); |
| 22 | |
| 23 | EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32); |
| 24 | EXPECT_EQ(sp_resp(res), SP_SUCCESS); |
| 25 | } |
| 26 | |
| 27 | static void enable_trusted_wdog_interrupt(ffa_vm_id_t source, ffa_vm_id_t dest) |
| 28 | { |
| 29 | configure_trusted_wdog_interrupt(source, dest, true); |
| 30 | } |
| 31 | |
| 32 | static void disable_trusted_wdog_interrupt(ffa_vm_id_t source, ffa_vm_id_t dest) |
| 33 | { |
| 34 | configure_trusted_wdog_interrupt(source, dest, false); |
| 35 | } |
| 36 | |
| 37 | static void enable_trigger_trusted_wdog_timer(ffa_vm_id_t own_id, |
| 38 | ffa_vm_id_t receiver_id, |
| 39 | uint32_t timer_ms) |
| 40 | { |
| 41 | struct ffa_value res; |
| 42 | |
| 43 | /* Enable trusted watchdog interrupt as vIRQ in the secure side. */ |
| 44 | enable_trusted_wdog_interrupt(own_id, receiver_id); |
| 45 | |
| 46 | res = sp_twdog_map_cmd_send(own_id, receiver_id); |
| 47 | |
| 48 | EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32); |
| 49 | EXPECT_EQ(sp_resp(res), SP_SUCCESS); |
| 50 | |
| 51 | /* |
| 52 | * Send a message to the SP through direct messaging requesting it to |
| 53 | * start the trusted watchdog timer. |
| 54 | */ |
| 55 | res = sp_twdog_cmd_send(own_id, receiver_id, timer_ms); |
| 56 | |
| 57 | EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32); |
| 58 | EXPECT_EQ(sp_resp(res), SP_SUCCESS); |
| 59 | } |
| 60 | |
| 61 | static void check_and_disable_trusted_wdog_timer(ffa_vm_id_t own_id, |
| 62 | ffa_vm_id_t receiver_id) |
| 63 | { |
| 64 | struct ffa_value res; |
| 65 | |
| 66 | /* Check for the last serviced secure virtual interrupt. */ |
| 67 | res = sp_get_last_interrupt_cmd_send(own_id, receiver_id); |
| 68 | |
| 69 | EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32); |
| 70 | EXPECT_EQ(sp_resp(res), SP_SUCCESS); |
| 71 | |
| 72 | /* Make sure Trusted Watchdog timer interrupt was serviced. */ |
| 73 | EXPECT_EQ(sp_resp_value(res), IRQ_TWDOG_INTID); |
| 74 | |
| 75 | /* Disable Trusted Watchdog interrupt. */ |
| 76 | disable_trusted_wdog_interrupt(own_id, receiver_id); |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * Test secure interrupt handling while the Secure Partition is in RUNNING |
| 81 | * state. |
| 82 | */ |
| 83 | TEST(secure_interrupts, sp_running) |
| 84 | { |
| 85 | struct ffa_value res; |
| 86 | ffa_vm_id_t own_id = hf_vm_get_id(); |
| 87 | struct ffa_partition_info *service2_info = service2(); |
| 88 | const ffa_vm_id_t receiver_id = service2_info->vm_id; |
| 89 | |
| 90 | enable_trigger_trusted_wdog_timer(own_id, receiver_id, 400); |
| 91 | |
| 92 | /* Send request to the SP to sleep. */ |
| 93 | res = sp_sleep_cmd_send(own_id, receiver_id, SP_SLEEP_TIME); |
| 94 | |
| 95 | /* |
| 96 | * Secure interrupt should trigger during this time, SP will handle the |
| 97 | * trusted watchdog timer interrupt. |
| 98 | */ |
| 99 | EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32); |
| 100 | EXPECT_EQ(sp_resp(res), SP_SUCCESS); |
| 101 | |
| 102 | /* Make sure elapsed time not less than sleep time. */ |
| 103 | EXPECT_GE(sp_resp_value(res), SP_SLEEP_TIME); |
| 104 | |
| 105 | check_and_disable_trusted_wdog_timer(own_id, receiver_id); |
| 106 | } |