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
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
31 #include <ipxe/timer.h>
32 #include <ipxe/efi/efi.h>
33 #include <ipxe/efi/Protocol/Cpu.h>
37 * iPXE timer API for EFI
41 /** Scale factor to apply to CPU timer 0
43 * The timer is scaled down in order to ensure that reasonable values
44 * for "number of ticks" don't exceed the size of an unsigned long.
46 #define EFI_TIMER0_SHIFT 12
48 /** Calibration time */
49 #define EFI_CALIBRATE_DELAY_MS 1
52 static EFI_CPU_ARCH_PROTOCOL *cpu_arch;
53 EFI_REQUIRE_PROTOCOL ( EFI_CPU_ARCH_PROTOCOL, &cpu_arch );
56 * Delay for a fixed number of microseconds
58 * @v usecs Number of microseconds for which to delay
60 static void efi_udelay ( unsigned long usecs ) {
61 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
65 if ( ( efirc = bs->Stall ( usecs ) ) != 0 ) {
67 DBG ( "EFI could not delay for %ldus: %s\n",
68 usecs, strerror ( rc ) );
69 /* Probably screwed */
74 * Get current system time in ticks
76 * @ret ticks Current time, in ticks
78 static unsigned long efi_currticks ( void ) {
83 /* Read CPU timer 0 (TSC) */
84 if ( ( efirc = cpu_arch->GetTimerValue ( cpu_arch, 0, &time,
87 DBG ( "EFI could not read CPU timer: %s\n", strerror ( rc ) );
88 /* Probably screwed */
92 return ( time >> EFI_TIMER0_SHIFT );
96 * Get number of ticks per second
98 * @ret ticks_per_sec Number of ticks per second
100 static unsigned long efi_ticks_per_sec ( void ) {
101 static unsigned long ticks_per_sec = 0;
103 /* Calibrate timer, if necessary. EFI does nominally provide
104 * the timer speed via the (optional) TimerPeriod parameter to
105 * the GetTimerValue() call, but it gets the speed slightly
106 * wrong. By up to three orders of magnitude. Not helpful.
108 if ( ! ticks_per_sec ) {
110 unsigned long elapsed;
112 DBG ( "Calibrating EFI timer with a %d ms delay\n",
113 EFI_CALIBRATE_DELAY_MS );
115 mdelay ( EFI_CALIBRATE_DELAY_MS );
116 elapsed = ( currticks() - start );
117 ticks_per_sec = ( elapsed * ( 1000 / EFI_CALIBRATE_DELAY_MS ));
118 DBG ( "EFI CPU timer calibrated at %ld ticks in %d ms (%ld "
119 "ticks/sec)\n", elapsed, EFI_CALIBRATE_DELAY_MS,
123 return ticks_per_sec;
126 PROVIDE_TIMER ( efi, udelay, efi_udelay );
127 PROVIDE_TIMER ( efi, currticks, efi_currticks );
128 PROVIDE_TIMER ( efi, ticks_per_sec, efi_ticks_per_sec );