]> Git Repo - J-u-boot.git/blame - lib/efi_selftest/efi_selftest_tpl.c
Merge patch series "U-boot: arm: Refine the booting on Total Compute"
[J-u-boot.git] / lib / efi_selftest / efi_selftest_tpl.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
1835f6ea 2/*
d8b2216c 3 * efi_selftest_tpl
1835f6ea
HS
4 *
5 * Copyright (c) 2017 Heinrich Schuchardt <[email protected]>
6 *
1835f6ea
HS
7 * This unit test uses timer events to check the handling of
8 * task priority levels.
9 */
10
11#include <efi_selftest.h>
12
564e55c7
HS
13static struct efi_event *efi_st_event_notify;
14static struct efi_event *efi_st_event_wait;
e67e7249 15static unsigned int notification_count;
1835f6ea
HS
16static struct efi_boot_services *boottime;
17
18/*
e67e7249 19 * Notification function, increments the notification count.
1835f6ea
HS
20 *
21 * @event notified event
e67e7249 22 * @context pointer to the notification count
1835f6ea
HS
23 */
24static void EFIAPI notify(struct efi_event *event, void *context)
25{
e67e7249
HS
26 unsigned int *count = context;
27
7f8ec5b6
HS
28 if (count)
29 ++*count;
1835f6ea
HS
30}
31
32/*
33 * Setup unit test.
34 *
35 * Create two timer events.
36 * One with EVT_NOTIFY_SIGNAL, the other with EVT_NOTIFY_WAIT.
37 *
38 * @handle: handle of the loaded image
39 * @systable: system table
3dd719d4 40 * Return: EFI_ST_SUCCESS for success
1835f6ea
HS
41 */
42static int setup(const efi_handle_t handle,
43 const struct efi_system_table *systable)
44{
45 efi_status_t ret;
46
47 boottime = systable->boottime;
48
49 ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL,
e67e7249
HS
50 TPL_CALLBACK, notify,
51 (void *)&notification_count,
564e55c7 52 &efi_st_event_notify);
1835f6ea
HS
53 if (ret != EFI_SUCCESS) {
54 efi_st_error("could not create event\n");
e67e7249 55 return EFI_ST_FAILURE;
1835f6ea
HS
56 }
57 ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_WAIT,
564e55c7
HS
58 TPL_NOTIFY, notify, NULL,
59 &efi_st_event_wait);
1835f6ea
HS
60 if (ret != EFI_SUCCESS) {
61 efi_st_error("could not create event\n");
e67e7249 62 return EFI_ST_FAILURE;
1835f6ea 63 }
e67e7249 64 return EFI_ST_SUCCESS;
1835f6ea
HS
65}
66
67/*
68 * Tear down unit test.
69 *
70 * Close the events created in setup.
e67e7249 71 *
3dd719d4 72 * Return: EFI_ST_SUCCESS for success
1835f6ea
HS
73 */
74static int teardown(void)
75{
76 efi_status_t ret;
77
564e55c7
HS
78 if (efi_st_event_notify) {
79 ret = boottime->close_event(efi_st_event_notify);
80 efi_st_event_notify = NULL;
1835f6ea
HS
81 if (ret != EFI_SUCCESS) {
82 efi_st_error("could not close event\n");
e67e7249 83 return EFI_ST_FAILURE;
1835f6ea
HS
84 }
85 }
564e55c7
HS
86 if (efi_st_event_wait) {
87 ret = boottime->close_event(efi_st_event_wait);
88 efi_st_event_wait = NULL;
1835f6ea
HS
89 if (ret != EFI_SUCCESS) {
90 efi_st_error("could not close event\n");
e67e7249 91 return EFI_ST_FAILURE;
1835f6ea
HS
92 }
93 }
94 boottime->restore_tpl(TPL_APPLICATION);
e67e7249 95 return EFI_ST_SUCCESS;
1835f6ea
HS
96}
97
98/*
99 * Execute unit test.
100 *
101 * Run a 10 ms periodic timer and check that it is called 10 times
102 * while waiting for 100 ms single shot timer.
103 *
104 * Raise the TPL level to the level of the 10 ms timer and observe
105 * that the notification function is not called again.
106 *
107 * Lower the TPL level and check that the queued notification
108 * function is called.
e67e7249 109 *
3dd719d4 110 * Return: EFI_ST_SUCCESS for success
1835f6ea
HS
111 */
112static int execute(void)
113{
f5a2a938 114 efi_uintn_t index;
1835f6ea 115 efi_status_t ret;
152cade3 116 efi_uintn_t old_tpl;
1835f6ea
HS
117
118 /* Set 10 ms timer */
e67e7249 119 notification_count = 0;
564e55c7
HS
120 ret = boottime->set_timer(efi_st_event_notify, EFI_TIMER_PERIODIC,
121 100000);
1835f6ea
HS
122 if (ret != EFI_SUCCESS) {
123 efi_st_error("Could not set timer\n");
e67e7249 124 return EFI_ST_FAILURE;
1835f6ea
HS
125 }
126 /* Set 100 ms timer */
564e55c7
HS
127 ret = boottime->set_timer(efi_st_event_wait, EFI_TIMER_RELATIVE,
128 1000000);
1835f6ea
HS
129 if (ret != EFI_SUCCESS) {
130 efi_st_error("Could not set timer\n");
e67e7249 131 return EFI_ST_FAILURE;
1835f6ea
HS
132 }
133 index = 5;
564e55c7 134 ret = boottime->wait_for_event(1, &efi_st_event_wait, &index);
1835f6ea
HS
135 if (ret != EFI_SUCCESS) {
136 efi_st_error("Could not wait for event\n");
e67e7249 137 return EFI_ST_FAILURE;
1835f6ea 138 }
564e55c7 139 ret = boottime->check_event(efi_st_event_wait);
1835f6ea
HS
140 if (ret != EFI_NOT_READY) {
141 efi_st_error("Signaled state was not cleared.\n");
142 efi_st_printf("ret = %u\n", (unsigned int)ret);
e67e7249 143 return EFI_ST_FAILURE;
1835f6ea
HS
144 }
145 if (index != 0) {
146 efi_st_error("WaitForEvent returned wrong index\n");
e67e7249 147 return EFI_ST_FAILURE;
1835f6ea 148 }
e67e7249 149 if (notification_count < 8 || notification_count > 12) {
6a380e5b
HS
150 efi_st_printf(
151 "Notification count with TPL level TPL_APPLICATION: %u\n",
152 notification_count);
1835f6ea 153 efi_st_error("Incorrect timing of events\n");
e67e7249 154 return EFI_ST_FAILURE;
1835f6ea 155 }
564e55c7 156 ret = boottime->set_timer(efi_st_event_notify, EFI_TIMER_STOP, 0);
44e7c62a 157 if (ret != EFI_SUCCESS) {
1835f6ea 158 efi_st_error("Could not cancel timer\n");
e67e7249 159 return EFI_ST_FAILURE;
1835f6ea
HS
160 }
161 /* Raise TPL level */
162 old_tpl = boottime->raise_tpl(TPL_CALLBACK);
163 if (old_tpl != TPL_APPLICATION) {
164 efi_st_error("Initial TPL level was not TPL_APPLICATION");
e67e7249 165 return EFI_ST_FAILURE;
1835f6ea
HS
166 }
167 /* Set 10 ms timer */
e67e7249 168 notification_count = 0;
564e55c7
HS
169 ret = boottime->set_timer(efi_st_event_notify, EFI_TIMER_PERIODIC,
170 100000);
44e7c62a 171 if (ret != EFI_SUCCESS) {
1835f6ea 172 efi_st_error("Could not set timer\n");
e67e7249 173 return EFI_ST_FAILURE;
1835f6ea
HS
174 }
175 /* Set 100 ms timer */
564e55c7
HS
176 ret = boottime->set_timer(efi_st_event_wait, EFI_TIMER_RELATIVE,
177 1000000);
1835f6ea
HS
178 if (ret != EFI_SUCCESS) {
179 efi_st_error("Could not set timer\n");
e67e7249 180 return EFI_ST_FAILURE;
1835f6ea
HS
181 }
182 do {
564e55c7 183 ret = boottime->check_event(efi_st_event_wait);
1835f6ea
HS
184 } while (ret == EFI_NOT_READY);
185 if (ret != EFI_SUCCESS) {
186 efi_st_error("Could not check event\n");
e67e7249 187 return EFI_ST_FAILURE;
1835f6ea 188 }
e67e7249 189 if (notification_count != 0) {
6a380e5b
HS
190 efi_st_printf(
191 "Notification count with TPL level TPL_CALLBACK: %u\n",
192 notification_count);
1835f6ea 193 efi_st_error("Suppressed timer fired\n");
e67e7249 194 return EFI_ST_FAILURE;
1835f6ea
HS
195 }
196 /* Set 1 ms timer */
564e55c7 197 ret = boottime->set_timer(efi_st_event_wait, EFI_TIMER_RELATIVE, 1000);
1835f6ea
HS
198 if (ret != EFI_SUCCESS) {
199 efi_st_error("Could not set timer\n");
e67e7249 200 return EFI_ST_FAILURE;
1835f6ea
HS
201 }
202 /* Restore the old TPL level */
203 boottime->restore_tpl(TPL_APPLICATION);
564e55c7 204 ret = boottime->wait_for_event(1, &efi_st_event_wait, &index);
1835f6ea
HS
205 if (ret != EFI_SUCCESS) {
206 efi_st_error("Could not wait for event\n");
e67e7249 207 return EFI_ST_FAILURE;
1835f6ea 208 }
e67e7249 209 if (notification_count < 1) {
6a380e5b
HS
210 efi_st_printf(
211 "Notification count with TPL level TPL_APPLICATION: %u\n",
212 notification_count);
1835f6ea 213 efi_st_error("Queued timer event did not fire\n");
e67e7249 214 return EFI_ST_FAILURE;
1835f6ea 215 }
564e55c7 216 ret = boottime->set_timer(efi_st_event_wait, EFI_TIMER_STOP, 0);
abe99463 217 if (ret != EFI_SUCCESS) {
1835f6ea 218 efi_st_error("Could not cancel timer\n");
e67e7249 219 return EFI_ST_FAILURE;
1835f6ea
HS
220 }
221
e67e7249 222 return EFI_ST_SUCCESS;
1835f6ea
HS
223}
224
225EFI_UNIT_TEST(tpl) = {
226 .name = "task priority levels",
227 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
228 .setup = setup,
229 .execute = execute,
230 .teardown = teardown,
231};
This page took 0.315339 seconds and 4 git commands to generate.