1 // SPDX-License-Identifier: GPL-2.0+
7 * This unit test uses timer events to check the handling of
8 * task priority levels.
11 #include <efi_selftest.h>
13 static struct efi_event *efi_st_event_notify;
14 static struct efi_event *efi_st_event_wait;
15 static unsigned int notification_count;
16 static struct efi_boot_services *boottime;
19 * Notification function, increments the notification count.
21 * @event notified event
22 * @context pointer to the notification count
24 static void EFIAPI notify(struct efi_event *event, void *context)
26 unsigned int *count = context;
35 * Create two timer events.
36 * One with EVT_NOTIFY_SIGNAL, the other with EVT_NOTIFY_WAIT.
38 * @handle: handle of the loaded image
39 * @systable: system table
40 * Return: EFI_ST_SUCCESS for success
42 static int setup(const efi_handle_t handle,
43 const struct efi_system_table *systable)
47 boottime = systable->boottime;
49 ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL,
51 (void *)¬ification_count,
52 &efi_st_event_notify);
53 if (ret != EFI_SUCCESS) {
54 efi_st_error("could not create event\n");
55 return EFI_ST_FAILURE;
57 ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_WAIT,
58 TPL_NOTIFY, notify, NULL,
60 if (ret != EFI_SUCCESS) {
61 efi_st_error("could not create event\n");
62 return EFI_ST_FAILURE;
64 return EFI_ST_SUCCESS;
68 * Tear down unit test.
70 * Close the events created in setup.
72 * Return: EFI_ST_SUCCESS for success
74 static int teardown(void)
78 if (efi_st_event_notify) {
79 ret = boottime->close_event(efi_st_event_notify);
80 efi_st_event_notify = NULL;
81 if (ret != EFI_SUCCESS) {
82 efi_st_error("could not close event\n");
83 return EFI_ST_FAILURE;
86 if (efi_st_event_wait) {
87 ret = boottime->close_event(efi_st_event_wait);
88 efi_st_event_wait = NULL;
89 if (ret != EFI_SUCCESS) {
90 efi_st_error("could not close event\n");
91 return EFI_ST_FAILURE;
94 boottime->restore_tpl(TPL_APPLICATION);
95 return EFI_ST_SUCCESS;
101 * Run a 10 ms periodic timer and check that it is called 10 times
102 * while waiting for 100 ms single shot timer.
104 * Raise the TPL level to the level of the 10 ms timer and observe
105 * that the notification function is not called again.
107 * Lower the TPL level and check that the queued notification
108 * function is called.
110 * Return: EFI_ST_SUCCESS for success
112 static int execute(void)
118 /* Set 10 ms timer */
119 notification_count = 0;
120 ret = boottime->set_timer(efi_st_event_notify, EFI_TIMER_PERIODIC,
122 if (ret != EFI_SUCCESS) {
123 efi_st_error("Could not set timer\n");
124 return EFI_ST_FAILURE;
126 /* Set 100 ms timer */
127 ret = boottime->set_timer(efi_st_event_wait, EFI_TIMER_RELATIVE,
129 if (ret != EFI_SUCCESS) {
130 efi_st_error("Could not set timer\n");
131 return EFI_ST_FAILURE;
134 ret = boottime->wait_for_event(1, &efi_st_event_wait, &index);
135 if (ret != EFI_SUCCESS) {
136 efi_st_error("Could not wait for event\n");
137 return EFI_ST_FAILURE;
139 ret = boottime->check_event(efi_st_event_wait);
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);
143 return EFI_ST_FAILURE;
146 efi_st_error("WaitForEvent returned wrong index\n");
147 return EFI_ST_FAILURE;
149 if (notification_count < 8 || notification_count > 12) {
151 "Notification count with TPL level TPL_APPLICATION: %u\n",
153 efi_st_error("Incorrect timing of events\n");
154 return EFI_ST_FAILURE;
156 ret = boottime->set_timer(efi_st_event_notify, EFI_TIMER_STOP, 0);
157 if (ret != EFI_SUCCESS) {
158 efi_st_error("Could not cancel timer\n");
159 return EFI_ST_FAILURE;
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");
165 return EFI_ST_FAILURE;
167 /* Set 10 ms timer */
168 notification_count = 0;
169 ret = boottime->set_timer(efi_st_event_notify, EFI_TIMER_PERIODIC,
171 if (ret != EFI_SUCCESS) {
172 efi_st_error("Could not set timer\n");
173 return EFI_ST_FAILURE;
175 /* Set 100 ms timer */
176 ret = boottime->set_timer(efi_st_event_wait, EFI_TIMER_RELATIVE,
178 if (ret != EFI_SUCCESS) {
179 efi_st_error("Could not set timer\n");
180 return EFI_ST_FAILURE;
183 ret = boottime->check_event(efi_st_event_wait);
184 } while (ret == EFI_NOT_READY);
185 if (ret != EFI_SUCCESS) {
186 efi_st_error("Could not check event\n");
187 return EFI_ST_FAILURE;
189 if (notification_count != 0) {
191 "Notification count with TPL level TPL_CALLBACK: %u\n",
193 efi_st_error("Suppressed timer fired\n");
194 return EFI_ST_FAILURE;
197 ret = boottime->set_timer(efi_st_event_wait, EFI_TIMER_RELATIVE, 1000);
198 if (ret != EFI_SUCCESS) {
199 efi_st_error("Could not set timer\n");
200 return EFI_ST_FAILURE;
202 /* Restore the old TPL level */
203 boottime->restore_tpl(TPL_APPLICATION);
204 ret = boottime->wait_for_event(1, &efi_st_event_wait, &index);
205 if (ret != EFI_SUCCESS) {
206 efi_st_error("Could not wait for event\n");
207 return EFI_ST_FAILURE;
209 if (notification_count < 1) {
211 "Notification count with TPL level TPL_APPLICATION: %u\n",
213 efi_st_error("Queued timer event did not fire\n");
214 return EFI_ST_FAILURE;
216 ret = boottime->set_timer(efi_st_event_wait, EFI_TIMER_STOP, 0);
217 if (ret != EFI_SUCCESS) {
218 efi_st_error("Could not cancel timer\n");
219 return EFI_ST_FAILURE;
222 return EFI_ST_SUCCESS;
225 EFI_UNIT_TEST(tpl) = {
226 .name = "task priority levels",
227 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
230 .teardown = teardown,