iPXE
acpipwr.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301, USA.
18  *
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.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <unistd.h>
27 #include <errno.h>
28 #include <byteswap.h>
29 #include <ipxe/io.h>
30 #include <ipxe/acpi.h>
31 #include <ipxe/acpipwr.h>
32 
33 /** @file
34  *
35  * ACPI power off
36  *
37  */
38 
39 /** Colour for debug messages */
40 #define colour FADT_SIGNATURE
41 
42 /** _S5_ signature */
43 #define S5_SIGNATURE ACPI_SIGNATURE ( '_', 'S', '5', '_' )
44 
45 /**
46  * Power off the computer using ACPI
47  *
48  * @ret rc Return status code
49  */
50 int acpi_poweroff ( void ) {
51  struct acpi_fadt fadtab;
52  userptr_t fadt;
53  unsigned int pm1a_cnt_blk;
54  unsigned int pm1b_cnt_blk;
55  unsigned int pm1a_cnt;
56  unsigned int pm1b_cnt;
57  unsigned int slp_typa;
58  unsigned int slp_typb;
59  int s5;
60  int rc;
61 
62  /* Locate FADT */
63  fadt = acpi_find ( FADT_SIGNATURE, 0 );
64  if ( ! fadt ) {
65  DBGC ( colour, "ACPI could not find FADT\n" );
66  return -ENOENT;
67  }
68 
69  /* Read FADT */
70  copy_from_user ( &fadtab, fadt, 0, sizeof ( fadtab ) );
73  pm1a_cnt = ( pm1a_cnt_blk + ACPI_PM1_CNT );
74  pm1b_cnt = ( pm1b_cnt_blk + ACPI_PM1_CNT );
75 
76  /* Extract \_S5 from DSDT or any SSDT */
77  s5 = acpi_sx ( S5_SIGNATURE );
78  if ( s5 < 0 ) {
79  rc = s5;
80  DBGC ( colour, "ACPI could not extract \\_S5: %s\n",
81  strerror ( rc ) );
82  return rc;
83  }
84 
85  /* Power off system */
86  if ( pm1a_cnt_blk ) {
87  slp_typa = ( ( s5 >> 0 ) & 0xff );
88  DBGC ( colour, "ACPI PM1a sleep type %#x => %04x\n",
89  slp_typa, pm1a_cnt );
90  outw ( ( ACPI_PM1_CNT_SLP_TYP ( slp_typa ) |
91  ACPI_PM1_CNT_SLP_EN ), pm1a_cnt );
92  }
93  if ( pm1b_cnt_blk ) {
94  slp_typb = ( ( s5 >> 8 ) & 0xff );
95  DBGC ( colour, "ACPI PM1b sleep type %#x => %04x\n",
96  slp_typb, pm1b_cnt );
97  outw ( ( ACPI_PM1_CNT_SLP_TYP ( slp_typb ) |
98  ACPI_PM1_CNT_SLP_EN ), pm1b_cnt );
99  }
100 
101  /* On some systems, execution will continue briefly. Delay to
102  * avoid potentially confusing log messages.
103  */
104  mdelay ( 1000 );
105 
106  DBGC ( colour, "ACPI power off failed\n" );
107  return -EPROTO;
108 }
iPXE I/O API
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int acpi_poweroff(void)
Power off the computer using ACPI.
Definition: acpipwr.c:50
#define le32_to_cpu(value)
Definition: byteswap.h:113
#define outw(data, io_addr)
Definition: io.h:319
Error codes.
#define ACPI_PM1_CNT
ACPI PM1 Control Register (within PM1a_CNT_BLK or PM1A_CNT_BLK)
Definition: acpi.h:129
static __always_inline void copy_from_user(void *dest, userptr_t src, off_t src_off, size_t len)
Copy data from user buffer.
Definition: uaccess.h:337
#define DBGC(...)
Definition: compiler.h:505
ACPI power off.
#define ENOENT
No such file or directory.
Definition: errno.h:514
#define colour
Colour for debug messages.
Definition: acpipwr.c:40
Fixed ACPI Description Table (FADT)
Definition: acpi.h:109
#define FADT_SIGNATURE
Fixed ACPI Description Table (FADT) signature.
Definition: acpi.h:106
uint32_t pm1a_cnt_blk
PM1a Control Register Block.
Definition: acpi.h:119
#define ACPI_PM1_CNT_SLP_EN
Sleep enable.
Definition: acpi.h:131
#define EPROTO
Protocol error.
Definition: errno.h:624
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
ACPI data structures.
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
void mdelay(unsigned long msecs)
Delay for a fixed number of milliseconds.
Definition: timer.c:78
#define ACPI_PM1_CNT_SLP_TYP(x)
Sleep type.
Definition: acpi.h:130
uint32_t pm1b_cnt_blk
PM1b Control Register Block.
Definition: acpi.h:121
#define S5_SIGNATURE
S5 signature
Definition: acpipwr.c:43
userptr_t acpi_find(uint32_t signature, unsigned int index)
Locate ACPI table.
Definition: acpi.c:89
int acpi_sx(uint32_t signature)
Extract _Sx value from DSDT/SSDT.
Definition: acpi.c:257
unsigned long userptr_t
A pointer to a user buffer.
Definition: uaccess.h:33