Merge remote-tracking branch 'remotes/kraxel/tags/pull-vga-20160509-1' into staging
[qemu/kevin.git] / tests / rtl8139-test.c
blob54e5aa7d0ec204afcd75dc4239b48e03566d1cb8
1 /*
2 * QTest testcase for Realtek 8139 NIC
4 * Copyright (c) 2013-2014 SUSE LINUX Products GmbH
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 */
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"
17 /* Tests only initialization so far. TODO: Replace with functional tests */
18 static void nop(void)
22 #define CLK 33333333
24 static QPCIBus *pcibus;
25 static QPCIDevice *dev;
26 static void *dev_base;
28 static void save_fn(QPCIDevice *dev, int devfn, void *data)
30 QPCIDevice **pdev = (QPCIDevice **) data;
32 *pdev = dev;
35 static QPCIDevice *get_device(void)
37 QPCIDevice *dev;
39 pcibus = qpci_init_pc();
40 qpci_device_foreach(pcibus, 0x10ec, 0x8139, save_fn, &dev);
41 g_assert(dev != NULL);
43 return dev;
46 #define PORT(name, len, val) \
47 static 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 } \
53 static 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); \
59 PORT(Timer, l, 0x48)
60 PORT(IntrMask, w, 0x3c)
61 PORT(IntrStatus, w, 0x3E)
62 PORT(TimerInt, l, 0x54)
64 #define fatal(...) do { g_test_message(__VA_ARGS__); g_assert(0); } while (0)
66 static void test_timer(void)
68 const unsigned from = 0.95 * CLK;
69 const unsigned to = 1.6 * CLK;
70 unsigned prev, curr, next;
71 unsigned cnt, diff;
73 out_IntrMask(0);
75 in_IntrStatus();
76 in_Timer();
77 in_Timer();
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);
87 for (cnt = 0; ; ) {
88 clock_step(1 * NANOSECONDS_PER_SECOND);
89 prev = curr;
90 curr = in_Timer();
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);
97 if (curr < prev && ++cnt == 3) {
98 break;
102 /* Test 2. Check we didn't get an interrupt with TimerInt == 0 */
103 if (in_IntrStatus() & 0x4000) {
104 fatal("got an interrupt\n");
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");
115 /* Test 3. Check acknowledge */
116 out_IntrStatus(0x4000);
117 if (in_IntrStatus() & 0x4000) {
118 fatal("got an interrupt\n");
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);
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");
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);
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");
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; ; ) {
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);
157 if (cnt < 3 && curr > next) {
158 if ((in_IntrStatus() & 0x4000) == 0) {
159 fatal("we should have an interrupt here!\n");
161 out_IntrStatus(0x4000);
162 next = curr + 5.0 * CLK;
163 out_TimerInt(next);
164 if (++cnt == 3) {
165 out_TimerInt(1);
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");
173 out_IntrStatus(0x4000);
174 if (++cnt == 5) {
175 break;
180 g_test_message("Everythink is ok!\n");
184 static void test_init(void)
186 uint64_t barsize;
188 dev = get_device();
190 dev_base = qpci_iomap(dev, 0, &barsize);
192 g_assert(dev_base != NULL);
194 qpci_device_enable(dev);
196 test_timer();
199 int main(int argc, char **argv)
201 int ret;
203 g_test_init(&argc, &argv, NULL);
204 qtest_add_func("/rtl8139/nop", nop);
205 qtest_add_func("/rtl8139/timer", test_init);
207 qtest_start("-device rtl8139");
208 ret = g_test_run();
210 qtest_end();
212 return ret;