2 * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
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 Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
27 #include <ipxe/timer.h>
28 #include <ipxe/efi/efi.h>
29 #include <ipxe/efi/Protocol/Cpu.h>
33 * iPXE timer API for EFI
37 /** Scale factor to apply to CPU timer 0
39 * The timer is scaled down in order to ensure that reasonable values
40 * for "number of ticks" don't exceed the size of an unsigned long.
42 #define EFI_TIMER0_SHIFT 12
44 /** Calibration time */
45 #define EFI_CALIBRATE_DELAY_MS 1
48 static EFI_CPU_ARCH_PROTOCOL *cpu_arch;
49 EFI_REQUIRE_PROTOCOL ( EFI_CPU_ARCH_PROTOCOL, &cpu_arch );
52 * Delay for a fixed number of microseconds
54 * @v usecs Number of microseconds for which to delay
56 static void efi_udelay ( unsigned long usecs ) {
57 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
61 if ( ( efirc = bs->Stall ( usecs ) ) != 0 ) {
63 DBG ( "EFI could not delay for %ldus: %s\n",
64 usecs, strerror ( rc ) );
65 /* Probably screwed */
70 * Get current system time in ticks
72 * @ret ticks Current time, in ticks
74 static unsigned long efi_currticks ( void ) {
79 /* Read CPU timer 0 (TSC) */
80 if ( ( efirc = cpu_arch->GetTimerValue ( cpu_arch, 0, &time,
83 DBG ( "EFI could not read CPU timer: %s\n", strerror ( rc ) );
84 /* Probably screwed */
88 return ( time >> EFI_TIMER0_SHIFT );
92 * Get number of ticks per second
94 * @ret ticks_per_sec Number of ticks per second
96 static unsigned long efi_ticks_per_sec ( void ) {
97 static unsigned long ticks_per_sec = 0;
99 /* Calibrate timer, if necessary. EFI does nominally provide
100 * the timer speed via the (optional) TimerPeriod parameter to
101 * the GetTimerValue() call, but it gets the speed slightly
102 * wrong. By up to three orders of magnitude. Not helpful.
104 if ( ! ticks_per_sec ) {
106 unsigned long elapsed;
108 DBG ( "Calibrating EFI timer with a %d ms delay\n",
109 EFI_CALIBRATE_DELAY_MS );
111 mdelay ( EFI_CALIBRATE_DELAY_MS );
112 elapsed = ( currticks() - start );
113 ticks_per_sec = ( elapsed * ( 1000 / EFI_CALIBRATE_DELAY_MS ));
114 DBG ( "EFI CPU timer calibrated at %ld ticks in %d ms (%ld "
115 "ticks/sec)\n", elapsed, EFI_CALIBRATE_DELAY_MS,
119 return ticks_per_sec;
122 PROVIDE_TIMER ( efi, udelay, efi_udelay );
123 PROVIDE_TIMER ( efi, currticks, efi_currticks );
124 PROVIDE_TIMER ( efi, ticks_per_sec, efi_ticks_per_sec );