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.
10 #include "qemu/osdep.h"
12 #include "libqos/pci-pc.h"
13 #include "qemu/timer.h"
14 #include "qemu-common.h"
16 /* Tests only initialization so far. TODO: Replace with functional tests */
23 static QPCIBus
*pcibus
;
24 static QPCIDevice
*dev
;
25 static void *dev_base
;
27 static void save_fn(QPCIDevice
*dev
, int devfn
, void *data
)
29 QPCIDevice
**pdev
= (QPCIDevice
**) data
;
34 static QPCIDevice
*get_device(void)
38 pcibus
= qpci_init_pc(NULL
);
39 qpci_device_foreach(pcibus
, 0x10ec, 0x8139, save_fn
, &dev
);
40 g_assert(dev
!= NULL
);
45 #define PORT(name, len, val) \
46 static unsigned __attribute__((unused)) in_##name(void) \
48 unsigned res = qpci_io_read##len(dev, dev_base+(val)); \
49 g_test_message("*%s -> %x\n", #name, res); \
52 static void out_##name(unsigned v) \
54 g_test_message("%x -> *%s\n", v, #name); \
55 qpci_io_write##len(dev, dev_base+(val), v); \
59 PORT(IntrMask
, w
, 0x3c)
60 PORT(IntrStatus
, w
, 0x3E)
61 PORT(TimerInt
, l
, 0x54)
63 #define fatal(...) do { g_test_message(__VA_ARGS__); g_assert(0); } while (0)
65 static void test_timer(void)
67 const unsigned from
= 0.95 * CLK
;
68 const unsigned to
= 1.6 * CLK
;
69 unsigned prev
, curr
, next
;
78 /* Test 1. test counter continue and continue */
79 out_TimerInt(0); /* disable timer */
80 out_IntrStatus(0x4000);
81 out_Timer(12345); /* reset timer to 0 */
83 if (curr
> 0.1 * CLK
) {
84 fatal("time too big %u\n", curr
);
87 clock_step(1 * NANOSECONDS_PER_SECOND
);
91 /* test skip is in a specific range */
92 diff
= (curr
-prev
) & 0xffffffffu
;
93 if (diff
< from
|| diff
> to
) {
94 fatal("Invalid diff %u (%u-%u)\n", diff
, from
, to
);
96 if (curr
< prev
&& ++cnt
== 3) {
101 /* Test 2. Check we didn't get an interrupt with TimerInt == 0 */
102 if (in_IntrStatus() & 0x4000) {
103 fatal("got an interrupt\n");
106 /* Test 3. Setting TimerInt to 1 and Timer to 0 get interrupt */
110 if ((in_IntrStatus() & 0x4000) == 0) {
111 fatal("we should have an interrupt here!\n");
114 /* Test 3. Check acknowledge */
115 out_IntrStatus(0x4000);
116 if (in_IntrStatus() & 0x4000) {
117 fatal("got an interrupt\n");
120 /* Test. Status set after Timer reset */
123 out_IntrStatus(0x4000);
125 out_TimerInt(curr
+ 0.5 * CLK
);
126 clock_step(1 * NANOSECONDS_PER_SECOND
);
128 if ((in_IntrStatus() & 0x4000) == 0) {
129 fatal("we should have an interrupt here!\n");
132 /* Test. Status set after TimerInt reset */
135 out_IntrStatus(0x4000);
137 out_TimerInt(curr
+ 0.5 * CLK
);
138 clock_step(1 * NANOSECONDS_PER_SECOND
);
140 if ((in_IntrStatus() & 0x4000) == 0) {
141 fatal("we should have an interrupt here!\n");
144 /* Test 4. Increment TimerInt we should see an interrupt */
146 next
= curr
+ 5.0 * CLK
;
149 clock_step(1 * NANOSECONDS_PER_SECOND
);
152 diff
= (curr
-prev
) & 0xffffffffu
;
153 if (diff
< from
|| diff
> to
) {
154 fatal("Invalid diff %u (%u-%u)\n", diff
, from
, to
);
156 if (cnt
< 3 && curr
> next
) {
157 if ((in_IntrStatus() & 0x4000) == 0) {
158 fatal("we should have an interrupt here!\n");
160 out_IntrStatus(0x4000);
161 next
= curr
+ 5.0 * CLK
;
166 /* Test 5. Second time we pass from 0 should see an interrupt */
167 } else if (cnt
>= 3 && curr
< prev
) {
168 /* here we should have an interrupt */
169 if ((in_IntrStatus() & 0x4000) == 0) {
170 fatal("we should have an interrupt here!\n");
172 out_IntrStatus(0x4000);
179 g_test_message("Everythink is ok!\n");
183 static void test_init(void)
189 dev_base
= qpci_iomap(dev
, 0, &barsize
);
191 g_assert(dev_base
!= NULL
);
193 qpci_device_enable(dev
);
198 int main(int argc
, char **argv
)
202 g_test_init(&argc
, &argv
, NULL
);
203 qtest_add_func("/rtl8139/nop", nop
);
204 qtest_add_func("/rtl8139/timer", test_init
);
206 qtest_start("-device rtl8139");