1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
7 * This unit test uses timer events to check the implementation
8 * of the following boottime services:
9 * CreateEvent, CloseEvent, WaitForEvent, CheckEvent, SetTimer.
12 #include <efi_selftest.h>
14 static struct efi_event *efi_st_event_notify;
15 static struct efi_event *event_wait;
16 static unsigned int timer_ticks;
17 static struct efi_boot_services *boottime;
20 * Notification function, increments the notification count if parameter
21 * context is provided.
23 * @event notified event
24 * @context pointer to the notification count
26 static void EFIAPI notify(struct efi_event *event, void *context)
28 unsigned int *count = context;
37 * Create two timer events.
38 * One with EVT_NOTIFY_SIGNAL, the other with EVT_NOTIFY_WAIT.
40 * @handle: handle of the loaded image
41 * @systable: system table
42 * Return: EFI_ST_SUCCESS for success
44 static int setup(const efi_handle_t handle,
45 const struct efi_system_table *systable)
49 boottime = systable->boottime;
51 ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL,
52 TPL_CALLBACK, notify, (void *)&timer_ticks,
53 &efi_st_event_notify);
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_CALLBACK, 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)
78 if (efi_st_event_notify) {
79 ret = boottime->close_event(efi_st_event_notify);
80 efi_st_event_notify = NULL;
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 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 * Run a 100 ms single shot timer and check that it is called once
104 * while waiting for 100 ms periodic timer for two periods.
106 * Return: EFI_ST_SUCCESS for success
108 static int execute(void)
113 /* Set 10 ms timer */
115 ret = boottime->set_timer(efi_st_event_notify, EFI_TIMER_PERIODIC,
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 if (timer_ticks < 8 || timer_ticks > 12) {
146 efi_st_printf("Notification count periodic: %u\n", timer_ticks);
147 efi_st_error("Incorrect timing of events\n");
148 return EFI_ST_FAILURE;
150 ret = boottime->set_timer(efi_st_event_notify, EFI_TIMER_STOP, 0);
151 if (ret != EFI_SUCCESS) {
152 efi_st_error("Could not cancel timer\n");
153 return EFI_ST_FAILURE;
155 /* Set 10 ms timer */
157 ret = boottime->set_timer(efi_st_event_notify, EFI_TIMER_RELATIVE,
159 if (ret != EFI_SUCCESS) {
160 efi_st_error("Could not set timer\n");
161 return EFI_ST_FAILURE;
163 /* Set 100 ms timer */
164 ret = boottime->set_timer(event_wait, EFI_TIMER_PERIODIC, 1000000);
165 if (ret != EFI_SUCCESS) {
166 efi_st_error("Could not set timer\n");
167 return EFI_ST_FAILURE;
169 ret = boottime->wait_for_event(1, &event_wait, &index);
170 if (ret != EFI_SUCCESS) {
171 efi_st_error("Could not wait for event\n");
172 return EFI_ST_FAILURE;
174 if (timer_ticks != 1) {
175 efi_st_printf("Notification count single shot: %u\n",
177 efi_st_error("Single shot timer failed\n");
178 return EFI_ST_FAILURE;
180 ret = boottime->wait_for_event(1, &event_wait, &index);
181 if (ret != EFI_SUCCESS) {
182 efi_st_error("Could not wait for event\n");
183 return EFI_ST_FAILURE;
185 if (timer_ticks != 1) {
186 efi_st_printf("Notification count stopped timer: %u\n",
188 efi_st_error("Stopped timer fired\n");
189 return EFI_ST_FAILURE;
191 ret = boottime->set_timer(event_wait, EFI_TIMER_STOP, 0);
192 if (ret != EFI_SUCCESS) {
193 efi_st_error("Could not cancel timer\n");
194 return EFI_ST_FAILURE;
197 return EFI_ST_SUCCESS;
200 EFI_UNIT_TEST(events) = {
201 .name = "event services",
202 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
205 .teardown = teardown,