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 counter;
18 static struct efi_boot_services *boottime;
21 * Notification function, increments a counter.
23 * @event notified event
24 * @context pointer to the counter
26 static void EFIAPI notify(struct efi_event *event, void *context)
30 ++*(unsigned int *)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
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,
50 TPL_CALLBACK, notify, (void *)&counter,
52 if (ret != EFI_SUCCESS) {
53 efi_st_error("could not create event\n");
56 ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_WAIT,
57 TPL_CALLBACK, notify, NULL, &event_wait);
58 if (ret != EFI_SUCCESS) {
59 efi_st_error("could not create event\n");
66 * Tear down unit test.
68 * Close the events created in setup.
70 static int teardown(void)
75 ret = boottime->close_event(event_notify);
77 if (ret != EFI_SUCCESS) {
78 efi_st_error("could not close event\n");
83 ret = boottime->close_event(event_wait);
85 if (ret != EFI_SUCCESS) {
86 efi_st_error("could not close event\n");
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 * Run a 100 ms single shot timer and check that it is called once
100 * while waiting for 100 ms periodic timer for two periods.
102 static int execute(void)
107 /* Set 10 ms timer */
109 ret = boottime->set_timer(event_notify, EFI_TIMER_PERIODIC, 100000);
110 if (ret != EFI_SUCCESS) {
111 efi_st_error("Could not set timer\n");
114 /* Set 100 ms timer */
115 ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000000);
116 if (ret != EFI_SUCCESS) {
117 efi_st_error("Could not set timer\n");
122 ret = boottime->wait_for_event(1, &event_wait, &index);
123 if (ret != EFI_SUCCESS) {
124 efi_st_error("Could not wait for event\n");
127 ret = boottime->check_event(event_wait);
128 if (ret != EFI_NOT_READY) {
129 efi_st_error("Signaled state was not cleared.\n");
130 efi_st_printf("ret = %u\n", (unsigned int)ret);
134 efi_st_error("WaitForEvent returned wrong index\n");
137 efi_st_printf("Counter periodic: %u\n", counter);
138 if (counter < 8 || counter > 12) {
139 efi_st_error("Incorrect timing of events\n");
142 ret = boottime->set_timer(event_notify, EFI_TIMER_STOP, 0);
144 efi_st_error("Could not cancel timer\n");
147 /* Set 10 ms timer */
149 ret = boottime->set_timer(event_notify, EFI_TIMER_RELATIVE, 100000);
151 efi_st_error("Could not set timer\n");
154 /* Set 100 ms timer */
155 ret = boottime->set_timer(event_wait, EFI_TIMER_PERIODIC, 1000000);
157 efi_st_error("Could not set timer\n");
160 ret = boottime->wait_for_event(1, &event_wait, &index);
161 if (ret != EFI_SUCCESS) {
162 efi_st_error("Could not wait for event\n");
165 efi_st_printf("Counter single shot: %u\n", counter);
167 efi_st_error("Single shot timer failed\n");
170 ret = boottime->wait_for_event(1, &event_wait, &index);
171 if (ret != EFI_SUCCESS) {
172 efi_st_error("Could not wait for event\n");
175 efi_st_printf("Stopped counter: %u\n", counter);
177 efi_st_error("Stopped timer fired\n");
180 ret = boottime->set_timer(event_wait, EFI_TIMER_STOP, 0);
182 efi_st_error("Could not cancel timer\n");
189 EFI_UNIT_TEST(events) = {
190 .name = "event services",
191 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
194 .teardown = teardown,