1 /*
2  * Copyright (c) 2019, Google, Inc. All rights reserved
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files
6  * (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify, merge,
8  * publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 #include <assert.h>
24 #include <err.h>
25 #include <kernel/timer.h>
26 #include <lk/init.h>
27 #include <platform.h>
28 #include <trace.h>
29 
30 #include <lib/trusty/event.h>
31 #include <lib/trusty/uirq.h>
32 #include <lib/trusty/uuid.h>
33 
34 #define LOCAL_TRACE 0
35 
36 #define MS_TO_NS(ms) ((ms)*1000ULL * 1000ULL)
37 
38 struct timer_uirq {
39     struct uirq uirq;
40     struct timer tm;
41     struct handle* handle;
42     lk_time_ns_t delay;
43 };
44 
45 static const struct uuid _test_app_uuid[] = {
46         /* UIRQ unittest app UUID : {e20af937-a4d0-4b95-b852-95ef21333cd1} */
47         {0xe20af937,
48          0xa4d0,
49          0x4b95,
50          {0xb8, 0x52, 0x95, 0xef, 0x21, 0x33, 0x3c, 0xd1}},
51 };
52 
53 static struct timer_uirq _tm_uirqs[] = {
54         {
55                 .uirq = UIRQ_INITIALIZER("test-uirq-10ms",
56                                          &_test_app_uuid[0],
57                                          1,
58                                          0),
59                 .tm = TIMER_INITIAL_VALUE(.tm),
60                 .delay = MS_TO_NS(10),
61         },
62         {
63                 .uirq = UIRQ_INITIALIZER("test-uirq-no-access",
64                                          &zero_uuid,
65                                          1,
66                                          0),
67                 .tm = TIMER_INITIAL_VALUE(.tm),
68                 .delay = MS_TO_NS(50),
69         },
70 };
71 
72 static lk_time_ns_t ts_start;
73 static lk_time_ns_t ts_notified;
74 static lk_time_ns_t ts_handled;
75 static lk_time_ns_t ttl_elapsed1;
76 static lk_time_ns_t ttl_elapsed2;
77 static unsigned int ttl_count;
78 
test_tm_uirq_callback(struct timer * t,lk_time_ns_t now,void * arg)79 static enum handler_return test_tm_uirq_callback(struct timer* t,
80                                                  lk_time_ns_t now,
81                                                  void* arg) {
82     struct timer_uirq* u = arg;
83     ts_start = current_time_ns();
84     event_source_signal(u->handle);
85     ts_notified = current_time_ns();
86     return INT_RESCHEDULE;
87 };
88 
tm_uirq_mask(const void * arg)89 static void tm_uirq_mask(const void* arg) {}
90 
tm_uirq_unmask(const void * arg)91 static void tm_uirq_unmask(const void* arg) {
92     ts_handled = current_time_ns();
93     if (ts_start) {
94         ttl_elapsed1 += ts_notified - ts_start;
95         ttl_elapsed2 += ts_handled - ts_start;
96         ttl_count++;
97         ts_start = 0;
98     }
99     struct timer_uirq* u = (struct timer_uirq*)arg;
100     timer_set_oneshot_ns(&u->tm, u->delay, test_tm_uirq_callback, u);
101 }
102 
tm_uirq_open(const void * arg)103 static void tm_uirq_open(const void* arg) {
104     ttl_count = 0;
105     ts_start = 0;
106     ts_notified = 0;
107     ts_handled = 0;
108     ttl_elapsed1 = 0;
109     ttl_elapsed2 = 0;
110 
111     struct timer_uirq* u = (struct timer_uirq*)arg;
112     timer_set_oneshot_ns(&u->tm, u->delay, test_tm_uirq_callback, u);
113 }
114 
tm_uirq_close(const void * arg)115 static void tm_uirq_close(const void* arg) {
116     struct timer_uirq* u = (struct timer_uirq*)arg;
117     timer_cancel_sync(&u->tm);
118 
119     if (ttl_count) {
120         LTRACEF("cnt=%u: %lld %lld\n", ttl_count, ttl_elapsed1, ttl_elapsed2);
121     }
122 }
123 
124 static const struct event_source_ops _tm_evt_ops = {
125         .open = tm_uirq_open,
126         .mask = tm_uirq_mask,
127         .unmask = tm_uirq_unmask,
128         .close = tm_uirq_close,
129 };
130 
uirq_test_init(uint level)131 static void uirq_test_init(uint level) {
132     int rc;
133 
134     /* register uirq interrupts */
135     for (uint i = 0; i < countof(_tm_uirqs); i++) {
136         struct timer_uirq* u = &_tm_uirqs[i];
137         rc = uirq_register_sw_irq(&u->uirq, &_tm_evt_ops, u, &u->handle);
138         if (rc < 0) {
139             TRACEF("Failed (%d) to initialize test uirq %s\n", rc,
140                    u->uirq.name);
141         }
142     }
143 }
144 
145 LK_INIT_HOOK(uirq_test, uirq_test_init, LK_INIT_LEVEL_APPS - 2);
146