blob: 743a6b9154fcf8ef018d52ca14d2b0716cb28973 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heinrich Schuchardtbd126692017-09-15 10:06:15 +02002/*
3 * efi_selftest_events
4 *
5 * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
6 *
Heinrich Schuchardtbd126692017-09-15 10:06:15 +02007 * This unit test uses timer events to check the implementation
8 * of the following boottime services:
9 * CreateEvent, CloseEvent, WaitForEvent, CheckEvent, SetTimer.
10 */
11
12#include <efi_selftest.h>
13
Heinrich Schuchardt564e55c2022-10-06 07:28:19 +020014static struct efi_event *efi_st_event_notify;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +020015static struct efi_event *event_wait;
Heinrich Schuchardte67e7242017-10-04 15:31:26 +020016static unsigned int timer_ticks;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +020017static struct efi_boot_services *boottime;
18
19/*
Heinrich Schuchardtd8b22162018-09-27 20:44:40 +020020 * Notification function, increments the notification count if parameter
Heinrich Schuchardte67e7242017-10-04 15:31:26 +020021 * context is provided.
Heinrich Schuchardtbd126692017-09-15 10:06:15 +020022 *
23 * @event notified event
Heinrich Schuchardte67e7242017-10-04 15:31:26 +020024 * @context pointer to the notification count
Heinrich Schuchardtbd126692017-09-15 10:06:15 +020025 */
26static void EFIAPI notify(struct efi_event *event, void *context)
27{
Heinrich Schuchardte67e7242017-10-04 15:31:26 +020028 unsigned int *count = context;
29
30 if (count)
31 ++*count;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +020032}
33
34/*
35 * Setup unit test.
36 *
37 * Create two timer events.
38 * One with EVT_NOTIFY_SIGNAL, the other with EVT_NOTIFY_WAIT.
39 *
40 * @handle: handle of the loaded image
41 * @systable: system table
Heinrich Schuchardt3dd719d2022-01-20 19:48:20 +010042 * Return: EFI_ST_SUCCESS for success
Heinrich Schuchardtbd126692017-09-15 10:06:15 +020043 */
44static int setup(const efi_handle_t handle,
45 const struct efi_system_table *systable)
46{
47 efi_status_t ret;
48
49 boottime = systable->boottime;
50
51 ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL,
Heinrich Schuchardte67e7242017-10-04 15:31:26 +020052 TPL_CALLBACK, notify, (void *)&timer_ticks,
Heinrich Schuchardt564e55c2022-10-06 07:28:19 +020053 &efi_st_event_notify);
Heinrich Schuchardtbd126692017-09-15 10:06:15 +020054 if (ret != EFI_SUCCESS) {
55 efi_st_error("could not create event\n");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +020056 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +020057 }
58 ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_WAIT,
59 TPL_CALLBACK, notify, NULL, &event_wait);
60 if (ret != EFI_SUCCESS) {
61 efi_st_error("could not create event\n");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +020062 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +020063 }
Heinrich Schuchardte67e7242017-10-04 15:31:26 +020064 return EFI_ST_SUCCESS;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +020065}
66
67/*
68 * Tear down unit test.
69 *
70 * Close the events created in setup.
Heinrich Schuchardte67e7242017-10-04 15:31:26 +020071 *
Heinrich Schuchardt3dd719d2022-01-20 19:48:20 +010072 * Return: EFI_ST_SUCCESS for success
Heinrich Schuchardtbd126692017-09-15 10:06:15 +020073 */
74static int teardown(void)
75{
76 efi_status_t ret;
77
Heinrich Schuchardt564e55c2022-10-06 07:28:19 +020078 if (efi_st_event_notify) {
79 ret = boottime->close_event(efi_st_event_notify);
80 efi_st_event_notify = NULL;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +020081 if (ret != EFI_SUCCESS) {
82 efi_st_error("could not close event\n");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +020083 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +020084 }
85 }
86 if (event_wait) {
87 ret = boottime->close_event(event_wait);
88 event_wait = NULL;
89 if (ret != EFI_SUCCESS) {
90 efi_st_error("could not close event\n");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +020091 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +020092 }
93 }
Heinrich Schuchardte67e7242017-10-04 15:31:26 +020094 return EFI_ST_SUCCESS;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +020095}
96
97/*
98 * Execute unit test.
99 *
100 * Run a 10 ms periodic timer and check that it is called 10 times
101 * while waiting for 100 ms single shot timer.
102 *
103 * Run a 100 ms single shot timer and check that it is called once
104 * while waiting for 100 ms periodic timer for two periods.
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200105 *
Heinrich Schuchardt3dd719d2022-01-20 19:48:20 +0100106 * Return: EFI_ST_SUCCESS for success
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200107 */
108static int execute(void)
109{
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100110 efi_uintn_t index;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200111 efi_status_t ret;
112
113 /* Set 10 ms timer */
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200114 timer_ticks = 0;
Heinrich Schuchardt564e55c2022-10-06 07:28:19 +0200115 ret = boottime->set_timer(efi_st_event_notify, EFI_TIMER_PERIODIC,
116 100000);
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200117 if (ret != EFI_SUCCESS) {
118 efi_st_error("Could not set timer\n");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200119 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200120 }
121 /* Set 100 ms timer */
122 ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000000);
123 if (ret != EFI_SUCCESS) {
124 efi_st_error("Could not set timer\n");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200125 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200126 }
127
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200128 /* Set some arbitrary non-zero value to make change detectable. */
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200129 index = 5;
130 ret = boottime->wait_for_event(1, &event_wait, &index);
131 if (ret != EFI_SUCCESS) {
132 efi_st_error("Could not wait for event\n");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200133 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200134 }
135 ret = boottime->check_event(event_wait);
136 if (ret != EFI_NOT_READY) {
137 efi_st_error("Signaled state was not cleared.\n");
138 efi_st_printf("ret = %u\n", (unsigned int)ret);
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200139 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200140 }
141 if (index != 0) {
142 efi_st_error("WaitForEvent returned wrong index\n");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200143 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200144 }
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200145 if (timer_ticks < 8 || timer_ticks > 12) {
Heinrich Schuchardtad50dcf2017-12-22 19:21:02 +0100146 efi_st_printf("Notification count periodic: %u\n", timer_ticks);
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200147 efi_st_error("Incorrect timing of events\n");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200148 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200149 }
Heinrich Schuchardt564e55c2022-10-06 07:28:19 +0200150 ret = boottime->set_timer(efi_st_event_notify, EFI_TIMER_STOP, 0);
Heinrich Schuchardt1309a152019-01-06 16:38:57 +0100151 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200152 efi_st_error("Could not cancel timer\n");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200153 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200154 }
155 /* Set 10 ms timer */
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200156 timer_ticks = 0;
Heinrich Schuchardt564e55c2022-10-06 07:28:19 +0200157 ret = boottime->set_timer(efi_st_event_notify, EFI_TIMER_RELATIVE,
158 100000);
Heinrich Schuchardt1309a152019-01-06 16:38:57 +0100159 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200160 efi_st_error("Could not set timer\n");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200161 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200162 }
163 /* Set 100 ms timer */
164 ret = boottime->set_timer(event_wait, EFI_TIMER_PERIODIC, 1000000);
Heinrich Schuchardt1309a152019-01-06 16:38:57 +0100165 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200166 efi_st_error("Could not set timer\n");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200167 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200168 }
169 ret = boottime->wait_for_event(1, &event_wait, &index);
170 if (ret != EFI_SUCCESS) {
171 efi_st_error("Could not wait for event\n");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200172 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200173 }
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200174 if (timer_ticks != 1) {
Heinrich Schuchardtad50dcf2017-12-22 19:21:02 +0100175 efi_st_printf("Notification count single shot: %u\n",
176 timer_ticks);
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200177 efi_st_error("Single shot timer failed\n");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200178 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200179 }
180 ret = boottime->wait_for_event(1, &event_wait, &index);
181 if (ret != EFI_SUCCESS) {
182 efi_st_error("Could not wait for event\n");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200183 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200184 }
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200185 if (timer_ticks != 1) {
Heinrich Schuchardtad50dcf2017-12-22 19:21:02 +0100186 efi_st_printf("Notification count stopped timer: %u\n",
187 timer_ticks);
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200188 efi_st_error("Stopped timer fired\n");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200189 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200190 }
191 ret = boottime->set_timer(event_wait, EFI_TIMER_STOP, 0);
Heinrich Schuchardtabe99462017-10-13 01:00:05 +0200192 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200193 efi_st_error("Could not cancel timer\n");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200194 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200195 }
196
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200197 return EFI_ST_SUCCESS;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200198}
199
200EFI_UNIT_TEST(events) = {
201 .name = "event services",
202 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
203 .setup = setup,
204 .execute = execute,
205 .teardown = teardown,
206};