2 * Copyright (C) 2010 Piotr JaroszyĆski <p.jaroszynski@gmail.com>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 FILE_LICENCE(GPL2_OR_LATER);
22 #include <ipxe/timer.h>
24 #include <linux_api.h>
28 * iPXE timer API for linux
33 * Delay for a fixed number of microseconds
35 * @v usecs Number of microseconds for which to delay
37 static void linux_udelay(unsigned long usecs)
43 * Get number of ticks per second
45 * @ret ticks_per_sec Number of ticks per second
47 static unsigned long linux_ticks_per_sec(void)
53 * Get current system time in ticks
55 * linux doesn't provide an easy access to jiffies so implement it by measuring
56 * the time since the first call to this function.
58 * Since this function is used to seed the (non-cryptographic) random
59 * number generator, we round the start time down to the nearest whole
60 * second. This minimises the chances of generating identical RNG
61 * sequences (and hence identical TCP port numbers, etc) on
62 * consecutive invocations of iPXE.
64 * @ret ticks Current time, in ticks
66 static unsigned long linux_currticks(void)
68 static struct timeval start;
69 static int initialized = 0;
72 linux_gettimeofday(&start, NULL);
77 linux_gettimeofday(&now, NULL);
79 unsigned long ticks = (now.tv_sec - start.tv_sec) * linux_ticks_per_sec();
80 ticks += now.tv_usec / (long)(1000000 / linux_ticks_per_sec());
85 PROVIDE_TIMER(linux, udelay, linux_udelay);
86 PROVIDE_TIMER(linux, currticks, linux_currticks);
87 PROVIDE_TIMER(linux, ticks_per_sec, linux_ticks_per_sec);