blob: 5393e393523a1331ff032f69b417d9da744ca9a0 [file] [log] [blame]
Heinrich Schuchardtbd126692017-09-15 10:06:15 +02001/*
2 * efi_selftest_events
3 *
4 * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 *
8 * This unit test uses timer events to check the implementation
9 * of the following boottime services:
10 * CreateEvent, CloseEvent, WaitForEvent, CheckEvent, SetTimer.
11 */
12
13#include <efi_selftest.h>
14
15static struct efi_event *event_notify;
16static struct efi_event *event_wait;
Heinrich Schuchardte67e7242017-10-04 15:31:26 +020017static unsigned int timer_ticks;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +020018static struct efi_boot_services *boottime;
19
20/*
Heinrich Schuchardte67e7242017-10-04 15:31:26 +020021 * Notification function, increments the notfication count if parameter
22 * context is provided.
Heinrich Schuchardtbd126692017-09-15 10:06:15 +020023 *
24 * @event notified event
Heinrich Schuchardte67e7242017-10-04 15:31:26 +020025 * @context pointer to the notification count
Heinrich Schuchardtbd126692017-09-15 10:06:15 +020026 */
27static void EFIAPI notify(struct efi_event *event, void *context)
28{
Heinrich Schuchardte67e7242017-10-04 15:31:26 +020029 unsigned int *count = context;
30
31 if (count)
32 ++*count;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +020033}
34
35/*
36 * Setup unit test.
37 *
38 * Create two timer events.
39 * One with EVT_NOTIFY_SIGNAL, the other with EVT_NOTIFY_WAIT.
40 *
41 * @handle: handle of the loaded image
42 * @systable: system table
Heinrich Schuchardte67e7242017-10-04 15:31:26 +020043 * @return: EFI_ST_SUCCESS for success
Heinrich Schuchardtbd126692017-09-15 10:06:15 +020044 */
45static int setup(const efi_handle_t handle,
46 const struct efi_system_table *systable)
47{
48 efi_status_t ret;
49
50 boottime = systable->boottime;
51
52 ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL,
Heinrich Schuchardte67e7242017-10-04 15:31:26 +020053 TPL_CALLBACK, notify, (void *)&timer_ticks,
Heinrich Schuchardtbd126692017-09-15 10:06:15 +020054 &event_notify);
55 if (ret != EFI_SUCCESS) {
56 efi_st_error("could not create event\n");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +020057 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +020058 }
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");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +020063 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +020064 }
Heinrich Schuchardte67e7242017-10-04 15:31:26 +020065 return EFI_ST_SUCCESS;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +020066}
67
68/*
69 * Tear down unit test.
70 *
71 * Close the events created in setup.
Heinrich Schuchardte67e7242017-10-04 15:31:26 +020072 *
73 * @return: EFI_ST_SUCCESS for success
Heinrich Schuchardtbd126692017-09-15 10:06:15 +020074 */
75static int teardown(void)
76{
77 efi_status_t ret;
78
79 if (event_notify) {
80 ret = boottime->close_event(event_notify);
81 event_notify = NULL;
82 if (ret != EFI_SUCCESS) {
83 efi_st_error("could not close event\n");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +020084 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +020085 }
86 }
87 if (event_wait) {
88 ret = boottime->close_event(event_wait);
89 event_wait = NULL;
90 if (ret != EFI_SUCCESS) {
91 efi_st_error("could not close event\n");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +020092 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +020093 }
94 }
Heinrich Schuchardte67e7242017-10-04 15:31:26 +020095 return EFI_ST_SUCCESS;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +020096}
97
98/*
99 * Execute unit test.
100 *
101 * Run a 10 ms periodic timer and check that it is called 10 times
102 * while waiting for 100 ms single shot timer.
103 *
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.
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200106 *
107 * @return: EFI_ST_SUCCESS for success
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200108 */
109static int execute(void)
110{
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100111 efi_uintn_t index;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200112 efi_status_t ret;
113
114 /* Set 10 ms timer */
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200115 timer_ticks = 0;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200116 ret = boottime->set_timer(event_notify, EFI_TIMER_PERIODIC, 100000);
117 if (ret != EFI_SUCCESS) {
118 efi_st_error("Could not set timer\n");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200119 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200120 }
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");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200125 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200126 }
127
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200128 /* Set some arbitrary non-zero value to make change detectable. */
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200129 index = 5;
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");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200133 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200134 }
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);
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200139 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200140 }
141 if (index != 0) {
142 efi_st_error("WaitForEvent returned wrong index\n");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200143 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200144 }
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200145 if (timer_ticks < 8 || timer_ticks > 12) {
Heinrich Schuchardtad50dcf2017-12-22 19:21:02 +0100146 efi_st_printf("Notification count periodic: %u\n", timer_ticks);
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200147 efi_st_error("Incorrect timing of events\n");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200148 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200149 }
150 ret = boottime->set_timer(event_notify, EFI_TIMER_STOP, 0);
151 if (index != 0) {
152 efi_st_error("Could not cancel timer\n");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200153 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200154 }
155 /* Set 10 ms timer */
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200156 timer_ticks = 0;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200157 ret = boottime->set_timer(event_notify, EFI_TIMER_RELATIVE, 100000);
158 if (index != 0) {
159 efi_st_error("Could not set timer\n");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200160 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200161 }
162 /* Set 100 ms timer */
163 ret = boottime->set_timer(event_wait, EFI_TIMER_PERIODIC, 1000000);
164 if (index != 0) {
165 efi_st_error("Could not set timer\n");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200166 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200167 }
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");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200171 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200172 }
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200173 if (timer_ticks != 1) {
Heinrich Schuchardtad50dcf2017-12-22 19:21:02 +0100174 efi_st_printf("Notification count single shot: %u\n",
175 timer_ticks);
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200176 efi_st_error("Single shot timer failed\n");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200177 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200178 }
179 ret = boottime->wait_for_event(1, &event_wait, &index);
180 if (ret != EFI_SUCCESS) {
181 efi_st_error("Could not wait for event\n");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200182 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200183 }
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200184 if (timer_ticks != 1) {
Heinrich Schuchardtad50dcf2017-12-22 19:21:02 +0100185 efi_st_printf("Notification count stopped timer: %u\n",
186 timer_ticks);
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200187 efi_st_error("Stopped timer fired\n");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200188 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200189 }
190 ret = boottime->set_timer(event_wait, EFI_TIMER_STOP, 0);
Heinrich Schuchardtabe99462017-10-13 01:00:05 +0200191 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200192 efi_st_error("Could not cancel timer\n");
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200193 return EFI_ST_FAILURE;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200194 }
195
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200196 return EFI_ST_SUCCESS;
Heinrich Schuchardtbd126692017-09-15 10:06:15 +0200197}
198
199EFI_UNIT_TEST(events) = {
200 .name = "event services",
201 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
202 .setup = setup,
203 .execute = execute,
204 .teardown = teardown,
205};