]> git.proxmox.com Git - mirror_qemu.git/blame - tests/rtl8139-test.c
Merge remote-tracking branch 'remotes/berrange/tags/pull-io-next-2016-02-16-1' into...
[mirror_qemu.git] / tests / rtl8139-test.c
CommitLineData
74769fe7
AF
1/*
2 * QTest testcase for Realtek 8139 NIC
3 *
4 * Copyright (c) 2013-2014 SUSE LINUX Products GmbH
5 *
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
8 */
9
681c28a3 10#include "qemu/osdep.h"
74769fe7 11#include <glib.h>
74769fe7 12#include "libqtest.h"
069bb583 13#include "libqos/pci-pc.h"
e0cf11f3 14#include "qemu/timer.h"
069bb583 15#include "qemu-common.h"
74769fe7
AF
16
17/* Tests only initialization so far. TODO: Replace with functional tests */
18static void nop(void)
19{
20}
21
37b9ab92 22#define CLK 33333333
069bb583
FZ
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 /* Test 1. test counter continue and continue */
80 out_TimerInt(0); /* disable timer */
81 out_IntrStatus(0x4000);
82 out_Timer(12345); /* reset timer to 0 */
83 curr = in_Timer();
84 if (curr > 0.1 * CLK) {
85 fatal("time too big %u\n", curr);
86 }
87 for (cnt = 0; ; ) {
13566fe3 88 clock_step(1 * NANOSECONDS_PER_SECOND);
069bb583
FZ
89 prev = curr;
90 curr = in_Timer();
91
92 /* test skip is in a specific range */
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 /* Test 2. Check we didn't get an interrupt with TimerInt == 0 */
103 if (in_IntrStatus() & 0x4000) {
104 fatal("got an interrupt\n");
105 }
106
107 /* Test 3. Setting TimerInt to 1 and Timer to 0 get interrupt */
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 /* Test 3. Check acknowledge */
116 out_IntrStatus(0x4000);
117 if (in_IntrStatus() & 0x4000) {
118 fatal("got an interrupt\n");
119 }
120
121 /* Test. Status set after Timer reset */
122 out_Timer(0);
123 out_TimerInt(0);
124 out_IntrStatus(0x4000);
125 curr = in_Timer();
126 out_TimerInt(curr + 0.5 * CLK);
13566fe3 127 clock_step(1 * NANOSECONDS_PER_SECOND);
069bb583
FZ
128 out_Timer(0);
129 if ((in_IntrStatus() & 0x4000) == 0) {
130 fatal("we should have an interrupt here!\n");
131 }
132
133 /* Test. Status set after TimerInt reset */
134 out_Timer(0);
135 out_TimerInt(0);
136 out_IntrStatus(0x4000);
137 curr = in_Timer();
138 out_TimerInt(curr + 0.5 * CLK);
13566fe3 139 clock_step(1 * NANOSECONDS_PER_SECOND);
069bb583
FZ
140 out_TimerInt(0);
141 if ((in_IntrStatus() & 0x4000) == 0) {
142 fatal("we should have an interrupt here!\n");
143 }
144
145 /* Test 4. Increment TimerInt we should see an interrupt */
146 curr = in_Timer();
147 next = curr + 5.0 * CLK;
148 out_TimerInt(next);
149 for (cnt = 0; ; ) {
13566fe3 150 clock_step(1 * NANOSECONDS_PER_SECOND);
069bb583
FZ
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 /* Test 5. Second time we pass from 0 should see an interrupt */
168 } else if (cnt >= 3 && curr < prev) {
169 /* here we should have an interrupt */
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
74769fe7
AF
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);
069bb583 205 qtest_add_func("/rtl8139/timer", test_init);
74769fe7
AF
206
207 qtest_start("-device rtl8139");
208 ret = g_test_run();
209
210 qtest_end();
211
212 return ret;
213}