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 */
acpi_poweroff(void)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 ) );
71 pm1a_cnt_blk = le32_to_cpu ( fadtab.pm1a_cnt_blk );
72 pm1b_cnt_blk = le32_to_cpu ( fadtab.pm1b_cnt_blk );
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 }
109