iPXE
acpi_timer.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2018 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <unistd.h>
00027 #include <errno.h>
00028 #include <assert.h>
00029 #include <byteswap.h>
00030 #include <ipxe/io.h>
00031 #include <ipxe/acpi.h>
00032 
00033 /** @file
00034  *
00035  * ACPI power management timer
00036  *
00037  */
00038 
00039 /** ACPI timer frequency (fixed 3.579545MHz) */
00040 #define ACPI_TIMER_HZ 3579545
00041 
00042 /** ACPI timer mask
00043  *
00044  * Timers may be implemented as either 24-bit or 32-bit counters.  We
00045  * simplify the code by pessimistically assuming that the timer has
00046  * only 24 bits.
00047  */
00048 #define ACPI_TIMER_MASK 0x00ffffffUL
00049 
00050 /** Power management timer register address */
00051 static unsigned int pm_tmr;
00052 
00053 struct timer acpi_timer __timer ( TIMER_PREFERRED );
00054 
00055 /**
00056  * Get current system time in ticks
00057  *
00058  * @ret ticks           Current time, in ticks
00059  */
00060 static unsigned long acpi_currticks ( void ) {
00061         static unsigned long offset;
00062         static uint32_t prev;
00063         uint32_t now;
00064 
00065         /* Read timer and account for wraparound */
00066         now = ( inl ( pm_tmr ) & ACPI_TIMER_MASK );
00067         if ( now < prev ) {
00068                 offset += ( ( ACPI_TIMER_MASK + 1 ) /
00069                             ( ACPI_TIMER_HZ / TICKS_PER_SEC ) );
00070         }
00071         prev = now;
00072 
00073         /* Convert to timer ticks */
00074         return ( offset + ( now / ( ACPI_TIMER_HZ / TICKS_PER_SEC ) ) );
00075 }
00076 
00077 /**
00078  * Delay for a fixed number of microseconds
00079  *
00080  * @v usecs             Number of microseconds for which to delay
00081  */
00082 static void acpi_udelay ( unsigned long usecs ) {
00083         uint32_t start;
00084         uint32_t elapsed;
00085         uint32_t threshold;
00086 
00087         /* Delay until a suitable number of ticks have elapsed.  We do
00088          * not need to allow for multiple wraparound, since the
00089          * wraparound period for a 24-bit timer at 3.579545MHz is
00090          * around 4700000us.
00091          */
00092         start = inl ( pm_tmr );
00093         threshold = ( ( usecs * ACPI_TIMER_HZ ) / 1000000 );
00094         do {
00095                 elapsed = ( ( inl ( pm_tmr ) - start ) & ACPI_TIMER_MASK );
00096         } while ( elapsed < threshold );
00097 }
00098 
00099 /**
00100  * Probe ACPI power management timer
00101  *
00102  * @ret rc              Return status code
00103  */
00104 static int acpi_timer_probe ( void ) {
00105         struct acpi_fadt fadtab;
00106         userptr_t fadt;
00107         unsigned int pm_tmr_blk;
00108 
00109         /* Locate FADT */
00110         fadt = acpi_find ( FADT_SIGNATURE, 0 );
00111         if ( ! fadt ) {
00112                 DBGC ( &acpi_timer, "ACPI could not find FADT\n" );
00113                 return -ENOENT;
00114         }
00115 
00116         /* Read FADT */
00117         copy_from_user ( &fadtab, fadt, 0, sizeof ( fadtab ) );
00118         pm_tmr_blk = le32_to_cpu ( fadtab.pm_tmr_blk );
00119         if ( ! pm_tmr_blk ) {
00120                 DBGC ( &acpi_timer, "ACPI has no timer\n" );
00121                 return -ENOENT;
00122         }
00123 
00124         /* Record power management timer register address */
00125         pm_tmr = ( pm_tmr_blk + ACPI_PM_TMR );
00126 
00127         return 0;
00128 }
00129 
00130 /** ACPI timer */
00131 struct timer acpi_timer __timer ( TIMER_PREFERRED ) = {
00132         .name = "acpi",
00133         .probe = acpi_timer_probe,
00134         .currticks = acpi_currticks,
00135         .udelay = acpi_udelay,
00136 };