1
2
3
4
5
6
7
8
9
10#include "qemu/osdep.h"
11#include <glib.h>
12#include "libqtest.h"
13#include "libqos/pci-pc.h"
14#include "qemu/timer.h"
15#include "qemu-common.h"
16
17
18static void nop(void)
19{
20}
21
22#define CLK 33333333
23
24static QPCIBus *pcibus;
25static QPCIDevice *dev;
26static void *dev_base;
27
28static void save_fn(QPCIDevice *dev, int devfn, void *data)
29{
30 QPCIDevice **pdev = (QPCIDevice **) data;
31
32 *pdev = dev;
33}
34
35static QPCIDevice *get_device(void)
36{
37 QPCIDevice *dev;
38
39 pcibus = qpci_init_pc();
40 qpci_device_foreach(pcibus, 0x10ec, 0x8139, save_fn, &dev);
41 g_assert(dev != NULL);
42
43 return dev;
44}
45
46#define PORT(name, len, val) \
47static unsigned __attribute__((unused)) in_##name(void) \
48{ \
49 unsigned res = qpci_io_read##len(dev, dev_base+(val)); \
50 g_test_message("*%s -> %x\n", #name, res); \
51 return res; \
52} \
53static void out_##name(unsigned v) \
54{ \
55 g_test_message("%x -> *%s\n", v, #name); \
56 qpci_io_write##len(dev, dev_base+(val), v); \
57}
58
59PORT(Timer, l, 0x48)
60PORT(IntrMask, w, 0x3c)
61PORT(IntrStatus, w, 0x3E)
62PORT(TimerInt, l, 0x54)
63
64#define fatal(...) do { g_test_message(__VA_ARGS__); g_assert(0); } while (0)
65
66static void test_timer(void)
67{
68 const unsigned from = 0.95 * CLK;
69 const unsigned to = 1.6 * CLK;
70 unsigned prev, curr, next;
71 unsigned cnt, diff;
72
73 out_IntrMask(0);
74
75 in_IntrStatus();
76 in_Timer();
77 in_Timer();
78
79
80 out_TimerInt(0);
81 out_IntrStatus(0x4000);
82 out_Timer(12345);
83 curr = in_Timer();
84 if (curr > 0.1 * CLK) {
85 fatal("time too big %u\n", curr);
86 }
87 for (cnt = 0; ; ) {
88 clock_step(1 * NANOSECONDS_PER_SECOND);
89 prev = curr;
90 curr = in_Timer();
91
92
93 diff = (curr-prev) & 0xffffffffu;
94 if (diff < from || diff > to) {
95 fatal("Invalid diff %u (%u-%u)\n", diff, from, to);
96 }
97 if (curr < prev && ++cnt == 3) {
98 break;
99 }
100 }
101
102
103 if (in_IntrStatus() & 0x4000) {
104 fatal("got an interrupt\n");
105 }
106
107
108 out_TimerInt(1);
109 out_Timer(0);
110 clock_step(40);
111 if ((in_IntrStatus() & 0x4000) == 0) {
112 fatal("we should have an interrupt here!\n");
113 }
114
115
116 out_IntrStatus(0x4000);
117 if (in_IntrStatus() & 0x4000) {
118 fatal("got an interrupt\n");
119 }
120
121
122 out_Timer(0);
123 out_TimerInt(0);
124 out_IntrStatus(0x4000);
125 curr = in_Timer();
126 out_TimerInt(curr + 0.5 * CLK);
127 clock_step(1 * NANOSECONDS_PER_SECOND);
128 out_Timer(0);
129 if ((in_IntrStatus() & 0x4000) == 0) {
130 fatal("we should have an interrupt here!\n");
131 }
132
133
134 out_Timer(0);
135 out_TimerInt(0);
136 out_IntrStatus(0x4000);
137 curr = in_Timer();
138 out_TimerInt(curr + 0.5 * CLK);
139 clock_step(1 * NANOSECONDS_PER_SECOND);
140 out_TimerInt(0);
141 if ((in_IntrStatus() & 0x4000) == 0) {
142 fatal("we should have an interrupt here!\n");
143 }
144
145
146 curr = in_Timer();
147 next = curr + 5.0 * CLK;
148 out_TimerInt(next);
149 for (cnt = 0; ; ) {
150 clock_step(1 * NANOSECONDS_PER_SECOND);
151 prev = curr;
152 curr = in_Timer();
153 diff = (curr-prev) & 0xffffffffu;
154 if (diff < from || diff > to) {
155 fatal("Invalid diff %u (%u-%u)\n", diff, from, to);
156 }
157 if (cnt < 3 && curr > next) {
158 if ((in_IntrStatus() & 0x4000) == 0) {
159 fatal("we should have an interrupt here!\n");
160 }
161 out_IntrStatus(0x4000);
162 next = curr + 5.0 * CLK;
163 out_TimerInt(next);
164 if (++cnt == 3) {
165 out_TimerInt(1);
166 }
167
168 } else if (cnt >= 3 && curr < prev) {
169
170 if ((in_IntrStatus() & 0x4000) == 0) {
171 fatal("we should have an interrupt here!\n");
172 }
173 out_IntrStatus(0x4000);
174 if (++cnt == 5) {
175 break;
176 }
177 }
178 }
179
180 g_test_message("Everythink is ok!\n");
181}
182
183
184static void test_init(void)
185{
186 uint64_t barsize;
187
188 dev = get_device();
189
190 dev_base = qpci_iomap(dev, 0, &barsize);
191
192 g_assert(dev_base != NULL);
193
194 qpci_device_enable(dev);
195
196 test_timer();
197}
198
199int main(int argc, char **argv)
200{
201 int ret;
202
203 g_test_init(&argc, &argv, NULL);
204 qtest_add_func("/rtl8139/nop", nop);
205 qtest_add_func("/rtl8139/timer", test_init);
206
207 qtest_start("-device rtl8139");
208 ret = g_test_run();
209
210 qtest_end();
211
212 return ret;
213}
214