iPXE
acpipwr.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2016 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 <byteswap.h>
00029 #include <ipxe/io.h>
00030 #include <ipxe/acpi.h>
00031 #include <ipxe/acpipwr.h>
00032 
00033 /** @file
00034  *
00035  * ACPI power off
00036  *
00037  */
00038 
00039 /** Colour for debug messages */
00040 #define colour FADT_SIGNATURE
00041 
00042 /** _S5_ signature */
00043 #define S5_SIGNATURE ACPI_SIGNATURE ( '_', 'S', '5', '_' )
00044 
00045 /**
00046  * Power off the computer using ACPI
00047  *
00048  * @ret rc              Return status code
00049  */
00050 int acpi_poweroff ( void ) {
00051         struct acpi_fadt fadtab;
00052         userptr_t fadt;
00053         unsigned int pm1a_cnt_blk;
00054         unsigned int pm1b_cnt_blk;
00055         unsigned int pm1a_cnt;
00056         unsigned int pm1b_cnt;
00057         unsigned int slp_typa;
00058         unsigned int slp_typb;
00059         int s5;
00060         int rc;
00061 
00062         /* Locate FADT */
00063         fadt = acpi_find ( FADT_SIGNATURE, 0 );
00064         if ( ! fadt ) {
00065                 DBGC ( colour, "ACPI could not find FADT\n" );
00066                 return -ENOENT;
00067         }
00068 
00069         /* Read FADT */
00070         copy_from_user ( &fadtab, fadt, 0, sizeof ( fadtab ) );
00071         pm1a_cnt_blk = le32_to_cpu ( fadtab.pm1a_cnt_blk );
00072         pm1b_cnt_blk = le32_to_cpu ( fadtab.pm1b_cnt_blk );
00073         pm1a_cnt = ( pm1a_cnt_blk + ACPI_PM1_CNT );
00074         pm1b_cnt = ( pm1b_cnt_blk + ACPI_PM1_CNT );
00075 
00076         /* Extract \_S5 from DSDT or any SSDT */
00077         s5 = acpi_sx ( S5_SIGNATURE );
00078         if ( s5 < 0 ) {
00079                 rc = s5;
00080                 DBGC ( colour, "ACPI could not extract \\_S5: %s\n",
00081                        strerror ( rc ) );
00082                 return rc;
00083         }
00084 
00085         /* Power off system */
00086         if ( pm1a_cnt_blk ) {
00087                 slp_typa = ( ( s5 >> 0 ) & 0xff );
00088                 DBGC ( colour, "ACPI PM1a sleep type %#x => %04x\n",
00089                        slp_typa, pm1a_cnt );
00090                 outw ( ( ACPI_PM1_CNT_SLP_TYP ( slp_typa ) |
00091                          ACPI_PM1_CNT_SLP_EN ), pm1a_cnt );
00092         }
00093         if ( pm1b_cnt_blk ) {
00094                 slp_typb = ( ( s5 >> 8 ) & 0xff );
00095                 DBGC ( colour, "ACPI PM1b sleep type %#x => %04x\n",
00096                        slp_typb, pm1b_cnt );
00097                 outw ( ( ACPI_PM1_CNT_SLP_TYP ( slp_typb ) |
00098                          ACPI_PM1_CNT_SLP_EN ), pm1b_cnt );
00099         }
00100 
00101         /* On some systems, execution will continue briefly.  Delay to
00102          * avoid potentially confusing log messages.
00103          */
00104         mdelay ( 1000 );
00105 
00106         DBGC ( colour, "ACPI power off failed\n" );
00107         return -EPROTO;
00108 }