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 implementation
9 * of the following boottime services:
10 * CreateEvent, CloseEvent, WaitForEvent, CheckEvent, SetTimer.
13 #include <efi_selftest.h>
15 static struct efi_event *event_notify;
16 static struct efi_event *event_wait;
17 static unsigned int timer_ticks;
18 static struct efi_boot_services *boottime;
21 * Notification function, increments the notfication count if parameter
22 * context is provided.
24 * @event notified event
25 * @context pointer to the notification count
27 static void EFIAPI notify(struct efi_event *event, void *context)
29 unsigned int *count = context;
38 * Create two timer events.
39 * One with EVT_NOTIFY_SIGNAL, the other with EVT_NOTIFY_WAIT.
41 * @handle: handle of the loaded image
42 * @systable: system table
43 * @return: EFI_ST_SUCCESS for success
45 static int setup(const efi_handle_t handle,
46 const struct efi_system_table *systable)
50 boottime = systable->boottime;
52 ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL,
53 TPL_CALLBACK, notify, (void *)&timer_ticks,
55 if (ret != EFI_SUCCESS) {
56 efi_st_error("could not create event\n");
57 return EFI_ST_FAILURE;
59 ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_WAIT,
60 TPL_CALLBACK, notify, NULL, &event_wait);
61 if (ret != EFI_SUCCESS) {
62 efi_st_error("could not create event\n");
63 return EFI_ST_FAILURE;
65 return EFI_ST_SUCCESS;
69 * Tear down unit test.
71 * Close the events created in setup.
73 * @return: EFI_ST_SUCCESS for success
75 static int teardown(void)
80 ret = boottime->close_event(event_notify);
82 if (ret != EFI_SUCCESS) {
83 efi_st_error("could not close event\n");
84 return EFI_ST_FAILURE;
88 ret = boottime->close_event(event_wait);
90 if (ret != EFI_SUCCESS) {
91 efi_st_error("could not close event\n");
92 return EFI_ST_FAILURE;
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 * Run a 100 ms single shot timer and check that it is called once
105 * while waiting for 100 ms periodic timer for two periods.
107 * @return: EFI_ST_SUCCESS for success
109 static int execute(void)
114 /* Set 10 ms timer */
116 ret = boottime->set_timer(event_notify, EFI_TIMER_PERIODIC, 100000);
117 if (ret != EFI_SUCCESS) {
118 efi_st_error("Could not set timer\n");
119 return EFI_ST_FAILURE;
121 /* Set 100 ms timer */
122 ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000000);
123 if (ret != EFI_SUCCESS) {
124 efi_st_error("Could not set timer\n");
125 return EFI_ST_FAILURE;
128 /* Set some arbitrary non-zero value to make change detectable. */
130 ret = boottime->wait_for_event(1, &event_wait, &index);
131 if (ret != EFI_SUCCESS) {
132 efi_st_error("Could not wait for event\n");
133 return EFI_ST_FAILURE;
135 ret = boottime->check_event(event_wait);
136 if (ret != EFI_NOT_READY) {
137 efi_st_error("Signaled state was not cleared.\n");
138 efi_st_printf("ret = %u\n", (unsigned int)ret);
139 return EFI_ST_FAILURE;
142 efi_st_error("WaitForEvent returned wrong index\n");
143 return EFI_ST_FAILURE;
145 efi_st_printf("Notification count periodic: %u\n", timer_ticks);
146 if (timer_ticks < 8 || timer_ticks > 12) {
147 efi_st_error("Incorrect timing of events\n");
148 return EFI_ST_FAILURE;
150 ret = boottime->set_timer(event_notify, EFI_TIMER_STOP, 0);
152 efi_st_error("Could not cancel timer\n");
153 return EFI_ST_FAILURE;
155 /* Set 10 ms timer */
157 ret = boottime->set_timer(event_notify, EFI_TIMER_RELATIVE, 100000);
159 efi_st_error("Could not set timer\n");
160 return EFI_ST_FAILURE;
162 /* Set 100 ms timer */
163 ret = boottime->set_timer(event_wait, EFI_TIMER_PERIODIC, 1000000);
165 efi_st_error("Could not set timer\n");
166 return EFI_ST_FAILURE;
168 ret = boottime->wait_for_event(1, &event_wait, &index);
169 if (ret != EFI_SUCCESS) {
170 efi_st_error("Could not wait for event\n");
171 return EFI_ST_FAILURE;
173 efi_st_printf("Notification count single shot: %u\n", timer_ticks);
174 if (timer_ticks != 1) {
175 efi_st_error("Single shot timer failed\n");
176 return EFI_ST_FAILURE;
178 ret = boottime->wait_for_event(1, &event_wait, &index);
179 if (ret != EFI_SUCCESS) {
180 efi_st_error("Could not wait for event\n");
181 return EFI_ST_FAILURE;
183 efi_st_printf("Notification count stopped timer: %u\n", timer_ticks);
184 if (timer_ticks != 1) {
185 efi_st_error("Stopped timer fired\n");
186 return EFI_ST_FAILURE;
188 ret = boottime->set_timer(event_wait, EFI_TIMER_STOP, 0);
189 if (ret != EFI_SUCCESS) {
190 efi_st_error("Could not cancel timer\n");
191 return EFI_ST_FAILURE;
194 return EFI_ST_SUCCESS;
197 EFI_UNIT_TEST(events) = {
198 .name = "event services",
199 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
202 .teardown = teardown,