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 counter;
17 static struct efi_boot_services *boottime;
20 * Notification function, increments a counter.
22 * @event notified event
23 * @context pointer to the counter
25 static void EFIAPI notify(struct efi_event *event, void *context)
29 ++*(unsigned int *)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
41 static int setup(const efi_handle_t handle,
42 const struct efi_system_table *systable)
46 boottime = systable->boottime;
48 ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL,
49 TPL_CALLBACK, notify, (void *)&counter,
51 if (ret != EFI_SUCCESS) {
52 efi_st_error("could not create event\n");
55 ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_WAIT,
56 TPL_HIGH_LEVEL, notify, NULL, &event_wait);
57 if (ret != EFI_SUCCESS) {
58 efi_st_error("could not create event\n");
65 * Tear down unit test.
67 * Close the events created in setup.
69 static int teardown(void)
74 ret = boottime->close_event(event_notify);
76 if (ret != EFI_SUCCESS) {
77 efi_st_error("could not close event\n");
82 ret = boottime->close_event(event_wait);
84 if (ret != EFI_SUCCESS) {
85 efi_st_error("could not close event\n");
89 boottime->restore_tpl(TPL_APPLICATION);
96 * Run a 10 ms periodic timer and check that it is called 10 times
97 * while waiting for 100 ms single shot timer.
99 * Raise the TPL level to the level of the 10 ms timer and observe
100 * that the notification function is not called again.
102 * Lower the TPL level and check that the queued notification
103 * function is called.
105 static int execute(void)
111 /* Set 10 ms timer */
113 ret = boottime->set_timer(event_notify, EFI_TIMER_PERIODIC, 100000);
114 if (ret != EFI_SUCCESS) {
115 efi_st_error("Could not set timer\n");
118 /* Set 100 ms timer */
119 ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000000);
120 if (ret != EFI_SUCCESS) {
121 efi_st_error("Could not set timer\n");
125 ret = boottime->wait_for_event(1, &event_wait, &index);
126 if (ret != EFI_SUCCESS) {
127 efi_st_error("Could not wait for event\n");
130 ret = boottime->check_event(event_wait);
131 if (ret != EFI_NOT_READY) {
132 efi_st_error("Signaled state was not cleared.\n");
133 efi_st_printf("ret = %u\n", (unsigned int)ret);
137 efi_st_error("WaitForEvent returned wrong index\n");
140 efi_st_printf("Counter with TPL level TPL_APPLICATION: %u\n", counter);
141 if (counter < 8 || counter > 12) {
142 efi_st_error("Incorrect timing of events\n");
145 ret = boottime->set_timer(event_notify, EFI_TIMER_STOP, 0);
147 efi_st_error("Could not cancel timer\n");
150 /* Raise TPL level */
151 old_tpl = boottime->raise_tpl(TPL_CALLBACK);
152 if (old_tpl != TPL_APPLICATION) {
153 efi_st_error("Initial TPL level was not TPL_APPLICATION");
156 /* Set 10 ms timer */
158 ret = boottime->set_timer(event_notify, EFI_TIMER_PERIODIC, 100000);
160 efi_st_error("Could not set timer\n");
163 /* Set 100 ms timer */
164 ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000000);
165 if (ret != EFI_SUCCESS) {
166 efi_st_error("Could not set timer\n");
170 ret = boottime->check_event(event_wait);
171 } while (ret == EFI_NOT_READY);
172 if (ret != EFI_SUCCESS) {
173 efi_st_error("Could not check event\n");
176 efi_st_printf("Counter with TPL level TPL_CALLBACK: %u\n", counter);
178 efi_st_error("Suppressed timer fired\n");
182 ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000);
183 if (ret != EFI_SUCCESS) {
184 efi_st_error("Could not set timer\n");
187 /* Restore the old TPL level */
188 boottime->restore_tpl(TPL_APPLICATION);
189 ret = boottime->wait_for_event(1, &event_wait, &index);
190 if (ret != EFI_SUCCESS) {
191 efi_st_error("Could not wait for event\n");
194 efi_st_printf("Counter with TPL level TPL_APPLICATION: %u\n", counter);
196 efi_st_error("Queued timer event did not fire\n");
199 ret = boottime->set_timer(event_wait, EFI_TIMER_STOP, 0);
201 efi_st_error("Could not cancel timer\n");
208 EFI_UNIT_TEST(tpl) = {
209 .name = "task priority levels",
210 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
213 .teardown = teardown,