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