15129044dSC.J. Collier/*-
25129044dSC.J. Collier *   BSD LICENSE
35129044dSC.J. Collier *
45129044dSC.J. Collier *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
55129044dSC.J. Collier *   All rights reserved.
65129044dSC.J. Collier *
75129044dSC.J. Collier *   Redistribution and use in source and binary forms, with or without
85129044dSC.J. Collier *   modification, are permitted provided that the following conditions
95129044dSC.J. Collier *   are met:
105129044dSC.J. Collier *
115129044dSC.J. Collier *     * Redistributions of source code must retain the above copyright
125129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer.
135129044dSC.J. Collier *     * Redistributions in binary form must reproduce the above copyright
145129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer in
155129044dSC.J. Collier *       the documentation and/or other materials provided with the
165129044dSC.J. Collier *       distribution.
175129044dSC.J. Collier *     * Neither the name of Intel Corporation nor the names of its
185129044dSC.J. Collier *       contributors may be used to endorse or promote products derived
195129044dSC.J. Collier *       from this software without specific prior written permission.
205129044dSC.J. Collier *
215129044dSC.J. Collier *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
225129044dSC.J. Collier *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
235129044dSC.J. Collier *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
245129044dSC.J. Collier *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
255129044dSC.J. Collier *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
265129044dSC.J. Collier *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
275129044dSC.J. Collier *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
285129044dSC.J. Collier *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
295129044dSC.J. Collier *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
305129044dSC.J. Collier *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
315129044dSC.J. Collier *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
325129044dSC.J. Collier */
335129044dSC.J. Collier
345129044dSC.J. Collier#include <stdio.h>
355129044dSC.J. Collier#include <stdint.h>
365129044dSC.J. Collier
375129044dSC.J. Collier#include <rte_common.h>
385129044dSC.J. Collier#include <rte_cycles.h>
395129044dSC.J. Collier
405129044dSC.J. Collier#include "test.h"
415129044dSC.J. Collier
425129044dSC.J. Collier#define N 10000
435129044dSC.J. Collier
445129044dSC.J. Collier/*
455129044dSC.J. Collier * Cycles test
465129044dSC.J. Collier * ===========
475129044dSC.J. Collier *
485129044dSC.J. Collier * - Loop N times and check that the timer always increments and
495129044dSC.J. Collier *   never decrements during this loop.
505129044dSC.J. Collier *
515129044dSC.J. Collier * - Wait one second using rte_usleep() and check that the increment
525129044dSC.J. Collier *   of cycles is correct with regard to the frequency of the timer.
535129044dSC.J. Collier */
545129044dSC.J. Collier
555129044dSC.J. Collierstatic int
565129044dSC.J. Colliertest_cycles(void)
575129044dSC.J. Collier{
585129044dSC.J. Collier	unsigned i;
595129044dSC.J. Collier	uint64_t start_cycles, cycles, prev_cycles;
605129044dSC.J. Collier	uint64_t hz = rte_get_timer_hz();
615129044dSC.J. Collier	uint64_t max_inc = (hz / 100); /* 10 ms max between 2 reads */
625129044dSC.J. Collier
635129044dSC.J. Collier	/* check that the timer is always incrementing */
645129044dSC.J. Collier	start_cycles = rte_get_timer_cycles();
655129044dSC.J. Collier	prev_cycles = start_cycles;
665129044dSC.J. Collier	for (i=0; i<N; i++) {
675129044dSC.J. Collier		cycles = rte_get_timer_cycles();
685129044dSC.J. Collier		if ((uint64_t)(cycles - prev_cycles) > max_inc) {
695129044dSC.J. Collier			printf("increment too high or going backwards\n");
705129044dSC.J. Collier			return -1;
715129044dSC.J. Collier		}
725129044dSC.J. Collier		prev_cycles = cycles;
735129044dSC.J. Collier	}
745129044dSC.J. Collier
755129044dSC.J. Collier	/* check that waiting 1 second is precise */
765129044dSC.J. Collier	prev_cycles = rte_get_timer_cycles();
775129044dSC.J. Collier	rte_delay_us(1000000);
785129044dSC.J. Collier	cycles = rte_get_timer_cycles();
795129044dSC.J. Collier
805129044dSC.J. Collier	if ((uint64_t)(cycles - prev_cycles) > (hz + max_inc)) {
815129044dSC.J. Collier		printf("delay_us is not accurate: too long\n");
825129044dSC.J. Collier		return -1;
835129044dSC.J. Collier	}
845129044dSC.J. Collier	if ((uint64_t)(cycles - prev_cycles) < (hz - max_inc)) {
855129044dSC.J. Collier		printf("delay_us is not accurate: too short\n");
865129044dSC.J. Collier		return -1;
875129044dSC.J. Collier	}
885129044dSC.J. Collier
895129044dSC.J. Collier	return 0;
905129044dSC.J. Collier}
915129044dSC.J. Collier
929ecc306dSRicardo SalvetiREGISTER_TEST_COMMAND(cycles_autotest, test_cycles);
933d9b7210SChristian Ehrhardt
943d9b7210SChristian Ehrhardt/*
953d9b7210SChristian Ehrhardt * rte_delay_us_callback test
963d9b7210SChristian Ehrhardt *
973d9b7210SChristian Ehrhardt * - check if callback is correctly registered/unregistered
983d9b7210SChristian Ehrhardt *
993d9b7210SChristian Ehrhardt */
1003d9b7210SChristian Ehrhardt
1013d9b7210SChristian Ehrhardtstatic unsigned int pattern;
1023d9b7210SChristian Ehrhardtstatic void my_rte_delay_us(unsigned int us)
1033d9b7210SChristian Ehrhardt{
1043d9b7210SChristian Ehrhardt	pattern += us;
1053d9b7210SChristian Ehrhardt}
1063d9b7210SChristian Ehrhardt
1073d9b7210SChristian Ehrhardtstatic int
1083d9b7210SChristian Ehrhardttest_user_delay_us(void)
1093d9b7210SChristian Ehrhardt{
1103d9b7210SChristian Ehrhardt	pattern = 0;
1113d9b7210SChristian Ehrhardt
1123d9b7210SChristian Ehrhardt	rte_delay_us(2);
1133d9b7210SChristian Ehrhardt	if (pattern != 0)
1143d9b7210SChristian Ehrhardt		return -1;
1153d9b7210SChristian Ehrhardt
1163d9b7210SChristian Ehrhardt	/* register custom delay function */
1173d9b7210SChristian Ehrhardt	rte_delay_us_callback_register(my_rte_delay_us);
1183d9b7210SChristian Ehrhardt
1193d9b7210SChristian Ehrhardt	rte_delay_us(2);
1203d9b7210SChristian Ehrhardt	if (pattern != 2)
1213d9b7210SChristian Ehrhardt		return -1;
1223d9b7210SChristian Ehrhardt
1233d9b7210SChristian Ehrhardt	rte_delay_us(3);
1243d9b7210SChristian Ehrhardt	if (pattern != 5)
1253d9b7210SChristian Ehrhardt		return -1;
1263d9b7210SChristian Ehrhardt
1273d9b7210SChristian Ehrhardt	/* restore original delay function */
1283d9b7210SChristian Ehrhardt	rte_delay_us_callback_register(rte_delay_us_block);
1293d9b7210SChristian Ehrhardt
1303d9b7210SChristian Ehrhardt	rte_delay_us(3);
1313d9b7210SChristian Ehrhardt	if (pattern != 5)
1323d9b7210SChristian Ehrhardt		return -1;
1333d9b7210SChristian Ehrhardt
1343d9b7210SChristian Ehrhardt	return 0;
1353d9b7210SChristian Ehrhardt}
1363d9b7210SChristian Ehrhardt
1373d9b7210SChristian EhrhardtREGISTER_TEST_COMMAND(user_delay_us, test_user_delay_us);
138