Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / arch / i386 / interface / pcbios / apm.c
1 /*
2  * Copyright (C) 2013 Marin Hannache <ipxe@mareo.fr>.
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
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 /**
23  * @file
24  *
25  * Advanced Power Management
26  *
27  */
28
29 #include <errno.h>
30 #include <realmode.h>
31 #include <ipxe/reboot.h>
32
33 /**
34  * Power off the computer using APM
35  *
36  * @ret rc              Return status code
37  */
38 static int apm_poweroff ( void ) {
39         uint16_t apm_version;
40         uint16_t apm_signature;
41         uint16_t apm_flags;
42         uint16_t carry;
43
44         /* APM check */
45         __asm__ __volatile__ ( REAL_CODE ( "int $0x15\n\t"
46                                            "adc %%edx,0\n\t" )
47                                : "=a" ( apm_version ), "=b" ( apm_signature ),
48                                  "=c" ( apm_flags ), "=d" ( carry )
49                                : "a" ( 0x5300 ), "b" ( 0x0000 ),
50                                  "d" ( 0x0000 ) );
51         if ( carry ) {
52                 DBG ( "APM not present\n" );
53                 return -ENOTSUP;
54         }
55         if ( apm_signature != 0x504d ) { /* signature 'PM' */
56                 DBG ( "APM not present\n" );
57                 return -ENOTSUP;
58         }
59         if ( apm_version < 0x0101 ) { /* Need version 1.1+ */
60                 DBG ( "APM 1.1+ not supported\n" );
61                 return -ENOTSUP;
62         }
63         if ( ( apm_flags & 0x8 ) == 0x8 ) {
64                 DBG ( "APM power management disabled\n" );
65                 return -EPERM;
66         }
67         DBG2 ( "APM check completed\n" );
68
69         /* APM initialisation */
70         __asm__ __volatile__ ( REAL_CODE ( "int $0x15\n\t"
71                                            "adc %%edx,0\n\t" )
72                                : "=d" ( carry )
73                                : "a" ( 0x5301 ), "b" ( 0x0000 ),
74                                  "d" ( 0x0000 ) );
75         if ( carry ) {
76                 DBG ( "APM initialisation failed\n" );
77                 return -EIO;
78         }
79         DBG2 ( "APM initialisation completed\n" );
80
81         /* Set APM driver version */
82         __asm__ __volatile__ ( REAL_CODE ( "int $0x15\n\t"
83                                            "adc %%edx,0\n\t" )
84                                : "=d" ( carry )
85                                : "a" ( 0x530e ), "b" ( 0x0000 ),
86                                  "c" ( 0x0101 ), "d" ( 0x0000 ) );
87         if ( carry ) {
88                 DBG ( "APM setting driver version failed\n" );
89                 return -EIO;
90         }
91         DBG2 ( "APM driver version set\n" );
92
93         /* Setting power state to off */
94         __asm__ __volatile__ ( REAL_CODE ( "int $0x15\n\t"
95                                            "adc %%edx,0\n\t" )
96                                : "=d" ( carry )
97                                : "a" ( 0x5307 ), "b" ( 0x0001 ),
98                                  "c" ( 0x0003 ), "d" ( 0x0000) );
99         if ( carry ) {
100                 DBG ( "APM setting power state failed\n" );
101                 return -ENOTTY;
102         }
103
104         /* Should never happen */
105         return -ECANCELED;
106 }
107
108 PROVIDE_REBOOT ( pcbios, poweroff, apm_poweroff );