xref: /openbmc/qemu/tests/qtest/rtl8139-test.c (revision 9b47188186a7192fa9835ffd9789b5daf218b639)
11e8a1faeSThomas Huth /*
21e8a1faeSThomas Huth  * QTest testcase for Realtek 8139 NIC
31e8a1faeSThomas Huth  *
41e8a1faeSThomas Huth  * Copyright (c) 2013-2014 SUSE LINUX Products GmbH
51e8a1faeSThomas Huth  *
61e8a1faeSThomas Huth  * This work is licensed under the terms of the GNU GPL, version 2 or later.
71e8a1faeSThomas Huth  * See the COPYING file in the top-level directory.
81e8a1faeSThomas Huth  */
91e8a1faeSThomas Huth 
101e8a1faeSThomas Huth #include "qemu/osdep.h"
111e8a1faeSThomas Huth #include "libqtest-single.h"
121e8a1faeSThomas Huth #include "libqos/pci-pc.h"
131e8a1faeSThomas Huth #include "qemu/timer.h"
141e8a1faeSThomas Huth 
155ebecf20SThomas Huth static int verbosity_level;
165ebecf20SThomas Huth 
171e8a1faeSThomas Huth /* Tests only initialization so far. TODO: Replace with functional tests */
nop(void)181e8a1faeSThomas Huth static void nop(void)
191e8a1faeSThomas Huth {
201e8a1faeSThomas Huth }
211e8a1faeSThomas Huth 
221e8a1faeSThomas Huth #define CLK 33333333
231e8a1faeSThomas Huth 
241e8a1faeSThomas Huth static QPCIBus *pcibus;
25a186fedbSPhilippe Mathieu-Daudé static QPCIDevice *pcidev;
261e8a1faeSThomas Huth static QPCIBar dev_bar;
271e8a1faeSThomas Huth 
save_fn(QPCIDevice * dev,int devfn,void * data)281e8a1faeSThomas Huth static void save_fn(QPCIDevice *dev, int devfn, void *data)
291e8a1faeSThomas Huth {
301e8a1faeSThomas Huth     QPCIDevice **pdev = (QPCIDevice **) data;
311e8a1faeSThomas Huth 
321e8a1faeSThomas Huth     *pdev = dev;
331e8a1faeSThomas Huth }
341e8a1faeSThomas Huth 
get_device(void)351e8a1faeSThomas Huth static QPCIDevice *get_device(void)
361e8a1faeSThomas Huth {
371e8a1faeSThomas Huth     QPCIDevice *dev;
381e8a1faeSThomas Huth 
391e8a1faeSThomas Huth     pcibus = qpci_new_pc(global_qtest, NULL);
401e8a1faeSThomas Huth     qpci_device_foreach(pcibus, 0x10ec, 0x8139, save_fn, &dev);
411e8a1faeSThomas Huth     g_assert(dev != NULL);
421e8a1faeSThomas Huth 
431e8a1faeSThomas Huth     return dev;
441e8a1faeSThomas Huth }
451e8a1faeSThomas Huth 
461e8a1faeSThomas Huth #define PORT(name, len, val) \
471e8a1faeSThomas Huth static unsigned __attribute__((unused)) in_##name(void) \
481e8a1faeSThomas Huth { \
49a186fedbSPhilippe Mathieu-Daudé     unsigned res = qpci_io_read##len(pcidev, dev_bar, (val));     \
505ebecf20SThomas Huth     if (verbosity_level >= 2) { \
511e8a1faeSThomas Huth         g_test_message("*%s -> %x", #name, res); \
525ebecf20SThomas Huth     } \
531e8a1faeSThomas Huth     return res; \
541e8a1faeSThomas Huth } \
551e8a1faeSThomas Huth static void out_##name(unsigned v) \
561e8a1faeSThomas Huth { \
575ebecf20SThomas Huth     if (verbosity_level >= 2) { \
581e8a1faeSThomas Huth         g_test_message("%x -> *%s", v, #name); \
595ebecf20SThomas Huth     } \
60a186fedbSPhilippe Mathieu-Daudé     qpci_io_write##len(pcidev, dev_bar, (val), v);        \
611e8a1faeSThomas Huth }
621e8a1faeSThomas Huth 
631e8a1faeSThomas Huth PORT(Timer, l, 0x48)
641e8a1faeSThomas Huth PORT(IntrMask, w, 0x3c)
651e8a1faeSThomas Huth PORT(IntrStatus, w, 0x3E)
661e8a1faeSThomas Huth PORT(TimerInt, l, 0x54)
671e8a1faeSThomas Huth 
68*74dcb253SPierrick Bouvier #define fatal(...) do { g_test_message(__VA_ARGS__); g_assert_not_reached(); } while (0)
691e8a1faeSThomas Huth 
test_timer(void)701e8a1faeSThomas Huth static void test_timer(void)
711e8a1faeSThomas Huth {
721e8a1faeSThomas Huth     const unsigned from = 0.95 * CLK;
731e8a1faeSThomas Huth     const unsigned to = 1.6 * CLK;
741e8a1faeSThomas Huth     unsigned prev, curr, next;
751e8a1faeSThomas Huth     unsigned cnt, diff;
761e8a1faeSThomas Huth 
771e8a1faeSThomas Huth     out_IntrMask(0);
781e8a1faeSThomas Huth 
791e8a1faeSThomas Huth     in_IntrStatus();
801e8a1faeSThomas Huth     in_Timer();
811e8a1faeSThomas Huth     in_Timer();
821e8a1faeSThomas Huth 
831e8a1faeSThomas Huth     /* Test 1. test counter continue and continue */
841e8a1faeSThomas Huth     out_TimerInt(0); /* disable timer */
851e8a1faeSThomas Huth     out_IntrStatus(0x4000);
861e8a1faeSThomas Huth     out_Timer(12345); /* reset timer to 0 */
871e8a1faeSThomas Huth     curr = in_Timer();
881e8a1faeSThomas Huth     if (curr > 0.1 * CLK) {
891e8a1faeSThomas Huth         fatal("time too big %u\n", curr);
901e8a1faeSThomas Huth     }
911e8a1faeSThomas Huth     for (cnt = 0; ; ) {
921e8a1faeSThomas Huth         clock_step(1 * NANOSECONDS_PER_SECOND);
931e8a1faeSThomas Huth         prev = curr;
941e8a1faeSThomas Huth         curr = in_Timer();
951e8a1faeSThomas Huth 
961e8a1faeSThomas Huth         /* test skip is in a specific range */
971e8a1faeSThomas Huth         diff = (curr-prev) & 0xffffffffu;
981e8a1faeSThomas Huth         if (diff < from || diff > to) {
991e8a1faeSThomas Huth             fatal("Invalid diff %u (%u-%u)\n", diff, from, to);
1001e8a1faeSThomas Huth         }
1011e8a1faeSThomas Huth         if (curr < prev && ++cnt == 3) {
1021e8a1faeSThomas Huth             break;
1031e8a1faeSThomas Huth         }
1041e8a1faeSThomas Huth     }
1051e8a1faeSThomas Huth 
1061e8a1faeSThomas Huth     /* Test 2. Check we didn't get an interrupt with TimerInt == 0 */
1071e8a1faeSThomas Huth     if (in_IntrStatus() & 0x4000) {
1081e8a1faeSThomas Huth         fatal("got an interrupt\n");
1091e8a1faeSThomas Huth     }
1101e8a1faeSThomas Huth 
1111e8a1faeSThomas Huth     /* Test 3. Setting TimerInt to 1 and Timer to 0 get interrupt */
1121e8a1faeSThomas Huth     out_TimerInt(1);
1131e8a1faeSThomas Huth     out_Timer(0);
1141e8a1faeSThomas Huth     clock_step(40);
1151e8a1faeSThomas Huth     if ((in_IntrStatus() & 0x4000) == 0) {
1161e8a1faeSThomas Huth         fatal("we should have an interrupt here!\n");
1171e8a1faeSThomas Huth     }
1181e8a1faeSThomas Huth 
1191e8a1faeSThomas Huth     /* Test 3. Check acknowledge */
1201e8a1faeSThomas Huth     out_IntrStatus(0x4000);
1211e8a1faeSThomas Huth     if (in_IntrStatus() & 0x4000) {
1221e8a1faeSThomas Huth         fatal("got an interrupt\n");
1231e8a1faeSThomas Huth     }
1241e8a1faeSThomas Huth 
1251e8a1faeSThomas Huth     /* Test. Status set after Timer reset */
1261e8a1faeSThomas Huth     out_Timer(0);
1271e8a1faeSThomas Huth     out_TimerInt(0);
1281e8a1faeSThomas Huth     out_IntrStatus(0x4000);
1291e8a1faeSThomas Huth     curr = in_Timer();
1301e8a1faeSThomas Huth     out_TimerInt(curr + 0.5 * CLK);
1311e8a1faeSThomas Huth     clock_step(1 * NANOSECONDS_PER_SECOND);
1321e8a1faeSThomas Huth     out_Timer(0);
1331e8a1faeSThomas Huth     if ((in_IntrStatus() & 0x4000) == 0) {
1341e8a1faeSThomas Huth         fatal("we should have an interrupt here!\n");
1351e8a1faeSThomas Huth     }
1361e8a1faeSThomas Huth 
1371e8a1faeSThomas Huth     /* Test. Status set after TimerInt reset */
1381e8a1faeSThomas Huth     out_Timer(0);
1391e8a1faeSThomas Huth     out_TimerInt(0);
1401e8a1faeSThomas Huth     out_IntrStatus(0x4000);
1411e8a1faeSThomas Huth     curr = in_Timer();
1421e8a1faeSThomas Huth     out_TimerInt(curr + 0.5 * CLK);
1431e8a1faeSThomas Huth     clock_step(1 * NANOSECONDS_PER_SECOND);
1441e8a1faeSThomas Huth     out_TimerInt(0);
1451e8a1faeSThomas Huth     if ((in_IntrStatus() & 0x4000) == 0) {
1461e8a1faeSThomas Huth         fatal("we should have an interrupt here!\n");
1471e8a1faeSThomas Huth     }
1481e8a1faeSThomas Huth 
1491e8a1faeSThomas Huth     /* Test 4. Increment TimerInt we should see an interrupt */
1501e8a1faeSThomas Huth     curr = in_Timer();
1511e8a1faeSThomas Huth     next = curr + 5.0 * CLK;
1521e8a1faeSThomas Huth     out_TimerInt(next);
1531e8a1faeSThomas Huth     for (cnt = 0; ; ) {
1541e8a1faeSThomas Huth         clock_step(1 * NANOSECONDS_PER_SECOND);
1551e8a1faeSThomas Huth         prev = curr;
1561e8a1faeSThomas Huth         curr = in_Timer();
1571e8a1faeSThomas Huth         diff = (curr-prev) & 0xffffffffu;
1581e8a1faeSThomas Huth         if (diff < from || diff > to) {
1591e8a1faeSThomas Huth             fatal("Invalid diff %u (%u-%u)\n", diff, from, to);
1601e8a1faeSThomas Huth         }
1611e8a1faeSThomas Huth         if (cnt < 3 && curr > next) {
1621e8a1faeSThomas Huth             if ((in_IntrStatus() & 0x4000) == 0) {
1631e8a1faeSThomas Huth                 fatal("we should have an interrupt here!\n");
1641e8a1faeSThomas Huth             }
1651e8a1faeSThomas Huth             out_IntrStatus(0x4000);
1661e8a1faeSThomas Huth             next = curr + 5.0 * CLK;
1671e8a1faeSThomas Huth             out_TimerInt(next);
1681e8a1faeSThomas Huth             if (++cnt == 3) {
1691e8a1faeSThomas Huth                 out_TimerInt(1);
1701e8a1faeSThomas Huth             }
1711e8a1faeSThomas Huth         /* Test 5. Second time we pass from 0 should see an interrupt */
1721e8a1faeSThomas Huth         } else if (cnt >= 3 && curr < prev) {
1731e8a1faeSThomas Huth             /* here we should have an interrupt */
1741e8a1faeSThomas Huth             if ((in_IntrStatus() & 0x4000) == 0) {
1751e8a1faeSThomas Huth                 fatal("we should have an interrupt here!\n");
1761e8a1faeSThomas Huth             }
1771e8a1faeSThomas Huth             out_IntrStatus(0x4000);
1781e8a1faeSThomas Huth             if (++cnt == 5) {
1791e8a1faeSThomas Huth                 break;
1801e8a1faeSThomas Huth             }
1811e8a1faeSThomas Huth         }
1821e8a1faeSThomas Huth     }
1831e8a1faeSThomas Huth 
1841e8a1faeSThomas Huth     g_test_message("Everythink is ok!");
1851e8a1faeSThomas Huth }
1861e8a1faeSThomas Huth 
1871e8a1faeSThomas Huth 
test_init(void)1881e8a1faeSThomas Huth static void test_init(void)
1891e8a1faeSThomas Huth {
1901e8a1faeSThomas Huth     uint64_t barsize;
1911e8a1faeSThomas Huth 
192a186fedbSPhilippe Mathieu-Daudé     pcidev = get_device();
1931e8a1faeSThomas Huth 
194a186fedbSPhilippe Mathieu-Daudé     dev_bar = qpci_iomap(pcidev, 0, &barsize);
1951e8a1faeSThomas Huth 
196a186fedbSPhilippe Mathieu-Daudé     qpci_device_enable(pcidev);
1971e8a1faeSThomas Huth 
1981e8a1faeSThomas Huth     test_timer();
1991e8a1faeSThomas Huth }
2001e8a1faeSThomas Huth 
main(int argc,char ** argv)2011e8a1faeSThomas Huth int main(int argc, char **argv)
2021e8a1faeSThomas Huth {
2031e8a1faeSThomas Huth     int ret;
2045ebecf20SThomas Huth     char *v_env = getenv("V");
2055ebecf20SThomas Huth 
2065ebecf20SThomas Huth     if (v_env) {
2075ebecf20SThomas Huth         verbosity_level = atoi(v_env);
2085ebecf20SThomas Huth     }
2091e8a1faeSThomas Huth 
210ae4b01b3SRichard W.M. Jones     g_test_init(&argc, &argv, NULL);
211ae4b01b3SRichard W.M. Jones 
212f5af1dadSThomas Huth     if (!qtest_has_device("rtl8139")) {
213f5af1dadSThomas Huth         return 0;
214f5af1dadSThomas Huth     }
215f5af1dadSThomas Huth 
2161e8a1faeSThomas Huth     qtest_start("-device rtl8139");
2171e8a1faeSThomas Huth 
2181e8a1faeSThomas Huth     qtest_add_func("/rtl8139/nop", nop);
2191e8a1faeSThomas Huth     qtest_add_func("/rtl8139/timer", test_init);
2201e8a1faeSThomas Huth 
2211e8a1faeSThomas Huth     ret = g_test_run();
2221e8a1faeSThomas Huth 
2231e8a1faeSThomas Huth     qtest_end();
2241e8a1faeSThomas Huth 
2251e8a1faeSThomas Huth     return ret;
2261e8a1faeSThomas Huth }
227