4 * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
6 * SPDX-License-Identifier: GPL-2.0+
8 * This unit test uses timer events to check the handling of
9 * task priority levels.
12 #include <efi_selftest.h>
14 static struct efi_event *event_notify;
15 static struct efi_event *event_wait;
16 static unsigned int notification_count;
17 static struct efi_boot_services *boottime;
20 * Notification function, increments the notification count.
22 * @event notified event
23 * @context pointer to the notification count
25 static void EFIAPI notify(struct efi_event *event, void *context)
27 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,
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_HIGH_LEVEL, notify, NULL, &event_wait);
59 if (ret != EFI_SUCCESS) {
60 efi_st_error("could not create event\n");
61 return EFI_ST_FAILURE;
63 return EFI_ST_SUCCESS;
67 * Tear down unit test.
69 * Close the events created in setup.
71 * @return: EFI_ST_SUCCESS for success
73 static int teardown(void)
78 ret = boottime->close_event(event_notify);
80 if (ret != EFI_SUCCESS) {
81 efi_st_error("could not close event\n");
82 return EFI_ST_FAILURE;
86 ret = boottime->close_event(event_wait);
88 if (ret != EFI_SUCCESS) {
89 efi_st_error("could not close event\n");
90 return EFI_ST_FAILURE;
93 boottime->restore_tpl(TPL_APPLICATION);
94 return EFI_ST_SUCCESS;
100 * Run a 10 ms periodic timer and check that it is called 10 times
101 * while waiting for 100 ms single shot timer.
103 * Raise the TPL level to the level of the 10 ms timer and observe
104 * that the notification function is not called again.
106 * Lower the TPL level and check that the queued notification
107 * function is called.
109 * @return: EFI_ST_SUCCESS for success
111 static int execute(void)
117 /* Set 10 ms timer */
118 notification_count = 0;
119 ret = boottime->set_timer(event_notify, EFI_TIMER_PERIODIC, 100000);
120 if (ret != EFI_SUCCESS) {
121 efi_st_error("Could not set timer\n");
122 return EFI_ST_FAILURE;
124 /* Set 100 ms timer */
125 ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000000);
126 if (ret != EFI_SUCCESS) {
127 efi_st_error("Could not set timer\n");
128 return EFI_ST_FAILURE;
131 ret = boottime->wait_for_event(1, &event_wait, &index);
132 if (ret != EFI_SUCCESS) {
133 efi_st_error("Could not wait for event\n");
134 return EFI_ST_FAILURE;
136 ret = boottime->check_event(event_wait);
137 if (ret != EFI_NOT_READY) {
138 efi_st_error("Signaled state was not cleared.\n");
139 efi_st_printf("ret = %u\n", (unsigned int)ret);
140 return EFI_ST_FAILURE;
143 efi_st_error("WaitForEvent returned wrong index\n");
144 return EFI_ST_FAILURE;
146 efi_st_printf("Notification count with TPL level TPL_APPLICATION: %u\n",
148 if (notification_count < 8 || notification_count > 12) {
149 efi_st_error("Incorrect timing of events\n");
150 return EFI_ST_FAILURE;
152 ret = boottime->set_timer(event_notify, EFI_TIMER_STOP, 0);
154 efi_st_error("Could not cancel timer\n");
155 return EFI_ST_FAILURE;
157 /* Raise TPL level */
158 old_tpl = boottime->raise_tpl(TPL_CALLBACK);
159 if (old_tpl != TPL_APPLICATION) {
160 efi_st_error("Initial TPL level was not TPL_APPLICATION");
161 return EFI_ST_FAILURE;
163 /* Set 10 ms timer */
164 notification_count = 0;
165 ret = boottime->set_timer(event_notify, EFI_TIMER_PERIODIC, 100000);
167 efi_st_error("Could not set timer\n");
168 return EFI_ST_FAILURE;
170 /* Set 100 ms timer */
171 ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000000);
172 if (ret != EFI_SUCCESS) {
173 efi_st_error("Could not set timer\n");
174 return EFI_ST_FAILURE;
177 ret = boottime->check_event(event_wait);
178 } while (ret == EFI_NOT_READY);
179 if (ret != EFI_SUCCESS) {
180 efi_st_error("Could not check event\n");
181 return EFI_ST_FAILURE;
183 efi_st_printf("Notification count with TPL level TPL_CALLBACK: %u\n",
185 if (notification_count != 0) {
186 efi_st_error("Suppressed timer fired\n");
187 return EFI_ST_FAILURE;
190 ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000);
191 if (ret != EFI_SUCCESS) {
192 efi_st_error("Could not set timer\n");
193 return EFI_ST_FAILURE;
195 /* Restore the old TPL level */
196 boottime->restore_tpl(TPL_APPLICATION);
197 ret = boottime->wait_for_event(1, &event_wait, &index);
198 if (ret != EFI_SUCCESS) {
199 efi_st_error("Could not wait for event\n");
200 return EFI_ST_FAILURE;
202 efi_st_printf("Notification count with TPL level TPL_APPLICATION: %u\n",
204 if (notification_count < 1) {
205 efi_st_error("Queued timer event did not fire\n");
206 return EFI_ST_FAILURE;
208 ret = boottime->set_timer(event_wait, EFI_TIMER_STOP, 0);
210 efi_st_error("Could not cancel timer\n");
211 return EFI_ST_FAILURE;
214 return EFI_ST_SUCCESS;
217 EFI_UNIT_TEST(tpl) = {
218 .name = "task priority levels",
219 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
222 .teardown = teardown,