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  * Extract \_Sx value from DSDT/SSDT
47  *
48  * @v zsdt DSDT or SSDT
49  * @v len Length of DSDT/SSDT
50  * @v offset Offset of signature within DSDT/SSDT
51  * @v data Data buffer
52  * @ret rc Return status code
53  *
54  * In theory, extracting the \_Sx value from the DSDT/SSDT requires a
55  * full ACPI parser plus some heuristics to work around the various
56  * broken encodings encountered in real ACPI implementations.
57  *
58  * In practice, we can get the same result by scanning through the
59  * DSDT/SSDT for the signature (e.g. "_S5_"), extracting the first
60  * four bytes, removing any bytes with bit 3 set, and treating
61  * whatever is left as a little-endian value. This is one of the
62  * uglier hacks I have ever implemented, but it's still prettier than
63  * the ACPI specification itself.
64  */
65 static int acpi_extract_sx ( userptr_t zsdt, size_t len, size_t offset,
66  void *data ) {
67  unsigned int *sx = data;
68  uint8_t bytes[4];
69  uint8_t *byte;
70 
71  /* Skip signature and package header */
72  offset += ( 4 /* signature */ + 3 /* package header */ );
73 
74  /* Sanity check */
75  if ( ( offset + sizeof ( bytes ) /* value */ ) > len ) {
76  return -EINVAL;
77  }
78 
79  /* Read first four bytes of value */
80  copy_from_user ( bytes, zsdt, offset, sizeof ( bytes ) );
81  DBGC ( colour, "ACPI found \\_Sx containing %02x:%02x:%02x:%02x\n",
82  bytes[0], bytes[1], bytes[2], bytes[3] );
83 
84  /* Extract \Sx value. There are three potential encodings
85  * that we might encounter:
86  *
87  * - SLP_TYPa, SLP_TYPb, rsvd, rsvd
88  *
89  * - <byteprefix>, SLP_TYPa, <byteprefix>, SLP_TYPb, ...
90  *
91  * - <dwordprefix>, SLP_TYPa, SLP_TYPb, 0, 0
92  *
93  * Since <byteprefix> and <dwordprefix> both have bit 3 set,
94  * and valid SLP_TYPx must have bit 3 clear (since SLP_TYPx is
95  * a 3-bit field), we can just skip any bytes with bit 3 set.
96  */
97  byte = bytes;
98  if ( *byte & 0x08 )
99  byte++;
100  *sx = *(byte++);
101  if ( *byte & 0x08 )
102  byte++;
103  *sx |= ( *byte << 8 );
104 
105  return 0;
106 }
107 
108 /**
109  * Power off the computer using ACPI
110  *
111  * @ret rc Return status code
112  */
113 int acpi_poweroff ( void ) {
114  struct acpi_fadt fadtab;
115  userptr_t fadt;
116  unsigned int pm1a_cnt_blk;
117  unsigned int pm1b_cnt_blk;
118  unsigned int pm1a_cnt;
119  unsigned int pm1b_cnt;
120  unsigned int slp_typa;
121  unsigned int slp_typb;
122  unsigned int s5;
123  int rc;
124 
125  /* Locate FADT */
126  fadt = acpi_table ( FADT_SIGNATURE, 0 );
127  if ( ! fadt ) {
128  DBGC ( colour, "ACPI could not find FADT\n" );
129  return -ENOENT;
130  }
131 
132  /* Read FADT */
133  copy_from_user ( &fadtab, fadt, 0, sizeof ( fadtab ) );
136  pm1a_cnt = ( pm1a_cnt_blk + ACPI_PM1_CNT );
137  pm1b_cnt = ( pm1b_cnt_blk + ACPI_PM1_CNT );
138 
139  /* Extract \_S5 from DSDT or any SSDT */
140  if ( ( rc = acpi_extract ( S5_SIGNATURE, &s5,
141  acpi_extract_sx ) ) != 0 ) {
142  DBGC ( colour, "ACPI could not extract \\_S5: %s\n",
143  strerror ( rc ) );
144  return rc;
145  }
146 
147  /* Power off system */
148  if ( pm1a_cnt_blk ) {
149  slp_typa = ( ( s5 >> 0 ) & 0xff );
150  DBGC ( colour, "ACPI PM1a sleep type %#x => %04x\n",
151  slp_typa, pm1a_cnt );
152  outw ( ( ACPI_PM1_CNT_SLP_TYP ( slp_typa ) |
153  ACPI_PM1_CNT_SLP_EN ), pm1a_cnt );
154  }
155  if ( pm1b_cnt_blk ) {
156  slp_typb = ( ( s5 >> 8 ) & 0xff );
157  DBGC ( colour, "ACPI PM1b sleep type %#x => %04x\n",
158  slp_typb, pm1b_cnt );
159  outw ( ( ACPI_PM1_CNT_SLP_TYP ( slp_typb ) |
160  ACPI_PM1_CNT_SLP_EN ), pm1b_cnt );
161  }
162 
163  /* On some systems, execution will continue briefly. Delay to
164  * avoid potentially confusing log messages.
165  */
166  mdelay ( 1000 );
167 
168  DBGC ( colour, "ACPI power off failed\n" );
169  return -EPROTO;
170 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
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:113
#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:264
static int acpi_extract_sx(userptr_t zsdt, size_t len, size_t offset, void *data)
Extract _Sx value from DSDT/SSDT.
Definition: acpipwr.c:65
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
userptr_t acpi_table(uint32_t signature, unsigned int index)
Locate ACPI table.
Definition: acpi.c:98
ACPI power off.
#define ENOENT
No such file or directory.
Definition: errno.h:514
#define colour
Colour for debug messages.
Definition: acpipwr.c:40
int acpi_extract(uint32_t signature, void *data, int(*extract)(userptr_t zsdt, size_t len, size_t offset, void *data))
Extract value from DSDT/SSDT.
Definition: acpi.c:240
Fixed ACPI Description Table (FADT)
Definition: acpi.h:244
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
#define FADT_SIGNATURE
Fixed ACPI Description Table (FADT) signature.
Definition: acpi.h:241
uint32_t pm1a_cnt_blk
PM1a Control Register Block.
Definition: acpi.h:254
#define ACPI_PM1_CNT_SLP_EN
Sleep enable.
Definition: acpi.h:266
#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.
unsigned char uint8_t
Definition: stdint.h:10
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
unsigned char byte
Definition: smc9000.h:38
void mdelay(unsigned long msecs)
Delay for a fixed number of milliseconds.
Definition: timer.c:78
uint32_t len
Length.
Definition: ena.h:14
#define ACPI_PM1_CNT_SLP_TYP(x)
Sleep type.
Definition: acpi.h:265
uint32_t pm1b_cnt_blk
PM1b Control Register Block.
Definition: acpi.h:256
#define S5_SIGNATURE
S5 signature
Definition: acpipwr.c:43
uint8_t data[48]
Additional event data.
Definition: ena.h:22
uint8_t bytes[64]
Definition: ib_mad.h:16
unsigned long userptr_t
A pointer to a user buffer.
Definition: uaccess.h:33