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
;
36 * Create two timer events.
37 * One with EVT_NOTIFY_SIGNAL, the other with EVT_NOTIFY_WAIT.
39 * @handle: handle of the loaded image
40 * @systable: system table
41 * @return: EFI_ST_SUCCESS for success
43 static int setup(const efi_handle_t handle
,
44 const struct efi_system_table
*systable
)
48 boottime
= systable
->boottime
;
50 ret
= boottime
->create_event(EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
52 (void *)¬ification_count
,
54 if (ret
!= EFI_SUCCESS
) {
55 efi_st_error("could not create event\n");
56 return EFI_ST_FAILURE
;
58 ret
= boottime
->create_event(EVT_TIMER
| EVT_NOTIFY_WAIT
,
59 TPL_HIGH_LEVEL
, notify
, NULL
, &event_wait
);
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)
79 ret
= boottime
->close_event(event_notify
);
81 if (ret
!= EFI_SUCCESS
) {
82 efi_st_error("could not close event\n");
83 return EFI_ST_FAILURE
;
87 ret
= boottime
->close_event(event_wait
);
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(event_notify
, EFI_TIMER_PERIODIC
, 100000);
121 if (ret
!= EFI_SUCCESS
) {
122 efi_st_error("Could not set timer\n");
123 return EFI_ST_FAILURE
;
125 /* Set 100 ms timer */
126 ret
= boottime
->set_timer(event_wait
, EFI_TIMER_RELATIVE
, 1000000);
127 if (ret
!= EFI_SUCCESS
) {
128 efi_st_error("Could not set timer\n");
129 return EFI_ST_FAILURE
;
132 ret
= boottime
->wait_for_event(1, &event_wait
, &index
);
133 if (ret
!= EFI_SUCCESS
) {
134 efi_st_error("Could not wait for event\n");
135 return EFI_ST_FAILURE
;
137 ret
= boottime
->check_event(event_wait
);
138 if (ret
!= EFI_NOT_READY
) {
139 efi_st_error("Signaled state was not cleared.\n");
140 efi_st_printf("ret = %u\n", (unsigned int)ret
);
141 return EFI_ST_FAILURE
;
144 efi_st_error("WaitForEvent returned wrong index\n");
145 return EFI_ST_FAILURE
;
147 efi_st_printf("Notification count with TPL level TPL_APPLICATION: %u\n",
149 if (notification_count
< 8 || notification_count
> 12) {
150 efi_st_error("Incorrect timing of events\n");
151 return EFI_ST_FAILURE
;
153 ret
= boottime
->set_timer(event_notify
, EFI_TIMER_STOP
, 0);
155 efi_st_error("Could not cancel timer\n");
156 return EFI_ST_FAILURE
;
158 /* Raise TPL level */
159 old_tpl
= boottime
->raise_tpl(TPL_CALLBACK
);
160 if (old_tpl
!= TPL_APPLICATION
) {
161 efi_st_error("Initial TPL level was not TPL_APPLICATION");
162 return EFI_ST_FAILURE
;
164 /* Set 10 ms timer */
165 notification_count
= 0;
166 ret
= boottime
->set_timer(event_notify
, EFI_TIMER_PERIODIC
, 100000);
168 efi_st_error("Could not set timer\n");
169 return EFI_ST_FAILURE
;
171 /* Set 100 ms timer */
172 ret
= boottime
->set_timer(event_wait
, EFI_TIMER_RELATIVE
, 1000000);
173 if (ret
!= EFI_SUCCESS
) {
174 efi_st_error("Could not set timer\n");
175 return EFI_ST_FAILURE
;
178 ret
= boottime
->check_event(event_wait
);
179 } while (ret
== EFI_NOT_READY
);
180 if (ret
!= EFI_SUCCESS
) {
181 efi_st_error("Could not check event\n");
182 return EFI_ST_FAILURE
;
184 efi_st_printf("Notification count with TPL level TPL_CALLBACK: %u\n",
186 if (notification_count
!= 0) {
187 efi_st_error("Suppressed timer fired\n");
188 return EFI_ST_FAILURE
;
191 ret
= boottime
->set_timer(event_wait
, EFI_TIMER_RELATIVE
, 1000);
192 if (ret
!= EFI_SUCCESS
) {
193 efi_st_error("Could not set timer\n");
194 return EFI_ST_FAILURE
;
196 /* Restore the old TPL level */
197 boottime
->restore_tpl(TPL_APPLICATION
);
198 ret
= boottime
->wait_for_event(1, &event_wait
, &index
);
199 if (ret
!= EFI_SUCCESS
) {
200 efi_st_error("Could not wait for event\n");
201 return EFI_ST_FAILURE
;
203 efi_st_printf("Notification count with TPL level TPL_APPLICATION: %u\n",
205 if (notification_count
< 1) {
206 efi_st_error("Queued timer event did not fire\n");
207 return EFI_ST_FAILURE
;
209 ret
= boottime
->set_timer(event_wait
, EFI_TIMER_STOP
, 0);
210 if (ret
!= EFI_SUCCESS
) {
211 efi_st_error("Could not cancel timer\n");
212 return EFI_ST_FAILURE
;
215 return EFI_ST_SUCCESS
;
218 EFI_UNIT_TEST(tpl
) = {
219 .name
= "task priority levels",
220 .phase
= EFI_EXECUTE_BEFORE_BOOTTIME_EXIT
,
223 .teardown
= teardown
,