Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 1 | // Basic support for apmbios callbacks. |
| 2 | // |
| 3 | // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net> |
| 4 | // Copyright (C) 2005 Struan Bartlett |
| 5 | // Copyright (C) 2004 Fabrice Bellard |
| 6 | // |
Kevin O'Connor | b1b7c2a | 2009-01-15 20:52:58 -0500 | [diff] [blame] | 7 | // This file may be distributed under the terms of the GNU LGPLv3 license. |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 8 | |
Kevin O'Connor | 15157a3 | 2008-12-13 11:10:37 -0500 | [diff] [blame] | 9 | #include "biosvar.h" // GET_GLOBAL |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 10 | #include "bregs.h" // struct bregs |
| 11 | #include "config.h" // CONFIG_* |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 12 | #include "fw/paravirt.h" // runningOnQEMU |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 13 | #include "output.h" // dprintf |
Kevin O'Connor | 3df600b | 2013-09-14 19:28:55 -0400 | [diff] [blame] | 14 | #include "stacks.h" // yield_toirq |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 15 | #include "util.h" // apm_shutdown |
Kevin O'Connor | 4ade523 | 2013-09-18 21:41:48 -0400 | [diff] [blame] | 16 | #include "x86.h" // outb |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 17 | |
| 18 | static void |
| 19 | out_str(const char *str_cs) |
| 20 | { |
Kevin O'Connor | 897fb11 | 2013-02-07 23:32:48 -0500 | [diff] [blame] | 21 | if (!runningOnQEMU()) { |
Kevin O'Connor | ac8df8c | 2008-05-24 23:46:33 -0400 | [diff] [blame] | 22 | dprintf(1, "APM request '%s'\n", str_cs); |
Kevin O'Connor | f64f0db | 2008-05-18 02:42:58 -0400 | [diff] [blame] | 23 | return; |
| 24 | } |
| 25 | |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 26 | u8 *s = (u8*)str_cs; |
| 27 | for (;;) { |
Kevin O'Connor | 15157a3 | 2008-12-13 11:10:37 -0500 | [diff] [blame] | 28 | u8 c = GET_GLOBAL(*s); |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 29 | if (!c) |
| 30 | break; |
Kevin O'Connor | 2ad3744 | 2008-05-06 19:49:01 -0400 | [diff] [blame] | 31 | outb(c, PORT_BIOS_APM); |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 32 | s++; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | // APM installation check |
| 37 | static void |
| 38 | handle_155300(struct bregs *regs) |
| 39 | { |
| 40 | regs->ah = 1; // APM major version |
| 41 | regs->al = 2; // APM minor version |
| 42 | regs->bh = 'P'; |
| 43 | regs->bl = 'M'; |
| 44 | // bit 0 : 16 bit interface supported |
| 45 | // bit 1 : 32 bit interface supported |
| 46 | regs->cx = 0x03; |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame] | 47 | set_success(regs); |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | // APM real mode interface connect |
| 51 | static void |
| 52 | handle_155301(struct bregs *regs) |
| 53 | { |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame] | 54 | set_success(regs); |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 55 | } |
| 56 | |
Kevin O'Connor | 44d6530 | 2008-03-08 11:34:46 -0500 | [diff] [blame] | 57 | // Assembler entry points defined in romlayout.S |
Kevin O'Connor | 47c8e31 | 2011-07-10 22:57:32 -0400 | [diff] [blame] | 58 | extern void entry_apm16(void); |
| 59 | extern void entry_apm32(void); |
Kevin O'Connor | 44d6530 | 2008-03-08 11:34:46 -0500 | [diff] [blame] | 60 | |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 61 | // APM 16 bit protected mode interface connect |
| 62 | static void |
| 63 | handle_155302(struct bregs *regs) |
| 64 | { |
Kevin O'Connor | 47c8e31 | 2011-07-10 22:57:32 -0400 | [diff] [blame] | 65 | regs->bx = (u32)entry_apm16; |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 66 | regs->ax = SEG_BIOS; // 16 bit code segment base |
| 67 | regs->si = 0xfff0; // 16 bit code segment size |
| 68 | regs->cx = SEG_BIOS; // data segment address |
| 69 | regs->di = 0xfff0; // data segment length |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame] | 70 | set_success(regs); |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | // APM 32 bit protected mode interface connect |
| 74 | static void |
| 75 | handle_155303(struct bregs *regs) |
| 76 | { |
Kevin O'Connor | 44d6530 | 2008-03-08 11:34:46 -0500 | [diff] [blame] | 77 | regs->ax = SEG_BIOS; // 32 bit code segment base |
Kevin O'Connor | 47c8e31 | 2011-07-10 22:57:32 -0400 | [diff] [blame] | 78 | regs->ebx = (u32)entry_apm32; |
Kevin O'Connor | 44d6530 | 2008-03-08 11:34:46 -0500 | [diff] [blame] | 79 | regs->cx = SEG_BIOS; // 16 bit code segment base |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 80 | // 32 bit code segment size (low 16 bits) |
| 81 | // 16 bit code segment size (high 16 bits) |
| 82 | regs->esi = 0xfff0fff0; |
Kevin O'Connor | 44d6530 | 2008-03-08 11:34:46 -0500 | [diff] [blame] | 83 | regs->dx = SEG_BIOS; // data segment address |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 84 | regs->di = 0xfff0; // data segment length |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame] | 85 | set_success(regs); |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | // APM interface disconnect |
| 89 | static void |
| 90 | handle_155304(struct bregs *regs) |
| 91 | { |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame] | 92 | set_success(regs); |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // APM cpu idle |
| 96 | static void |
| 97 | handle_155305(struct bregs *regs) |
| 98 | { |
Kevin O'Connor | 94c749c | 2012-05-28 11:44:02 -0400 | [diff] [blame] | 99 | yield_toirq(); |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame] | 100 | set_success(regs); |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 101 | } |
| 102 | |
Kevin O'Connor | 0fae9e1 | 2008-06-07 14:51:14 -0400 | [diff] [blame] | 103 | // APM cpu busy |
| 104 | static void |
| 105 | handle_155306(struct bregs *regs) |
| 106 | { |
| 107 | set_success(regs); |
| 108 | } |
| 109 | |
Kevin O'Connor | 244caf8 | 2010-09-15 21:48:16 -0400 | [diff] [blame] | 110 | void |
| 111 | apm_shutdown(void) |
| 112 | { |
Gerd Hoffmann | 5b63109 | 2013-07-25 09:47:18 +0200 | [diff] [blame] | 113 | u16 pm1a_cnt = GET_GLOBAL(acpi_pm1a_cnt); |
| 114 | |
Kevin O'Connor | 244caf8 | 2010-09-15 21:48:16 -0400 | [diff] [blame] | 115 | irq_disable(); |
Gerd Hoffmann | 5b63109 | 2013-07-25 09:47:18 +0200 | [diff] [blame] | 116 | if (pm1a_cnt) |
| 117 | outw(0x2000, pm1a_cnt); |
Kevin O'Connor | 244caf8 | 2010-09-15 21:48:16 -0400 | [diff] [blame] | 118 | out_str("Shutdown"); |
| 119 | for (;;) |
| 120 | hlt(); |
| 121 | } |
| 122 | |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 123 | // APM Set Power State |
| 124 | static void |
| 125 | handle_155307(struct bregs *regs) |
| 126 | { |
| 127 | if (regs->bx != 1) { |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame] | 128 | set_success(regs); |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 129 | return; |
| 130 | } |
| 131 | switch (regs->cx) { |
| 132 | case 1: |
| 133 | out_str("Standby"); |
| 134 | break; |
| 135 | case 2: |
| 136 | out_str("Suspend"); |
| 137 | break; |
| 138 | case 3: |
Kevin O'Connor | 244caf8 | 2010-09-15 21:48:16 -0400 | [diff] [blame] | 139 | apm_shutdown(); |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 140 | break; |
| 141 | } |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame] | 142 | set_success(regs); |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static void |
| 146 | handle_155308(struct bregs *regs) |
| 147 | { |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame] | 148 | set_success(regs); |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | // Get Power Status |
| 152 | static void |
| 153 | handle_15530a(struct bregs *regs) |
| 154 | { |
| 155 | regs->bh = 0x01; // on line |
| 156 | regs->bl = 0xff; // unknown battery status |
| 157 | regs->ch = 0x80; // no system battery |
| 158 | regs->cl = 0xff; // unknown remaining time |
| 159 | regs->dx = 0xffff; // unknown remaining time |
| 160 | regs->si = 0x00; // zero battery |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame] | 161 | set_success(regs); |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 162 | } |
| 163 | |
Kevin O'Connor | 0fae9e1 | 2008-06-07 14:51:14 -0400 | [diff] [blame] | 164 | #define RET_ENOEVENT 0x80 |
| 165 | |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 166 | // Get PM Event |
| 167 | static void |
| 168 | handle_15530b(struct bregs *regs) |
| 169 | { |
Kevin O'Connor | dfefeb5 | 2009-12-13 13:04:17 -0500 | [diff] [blame] | 170 | set_code_invalid_silent(regs, RET_ENOEVENT); |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | // APM Driver Version |
| 174 | static void |
| 175 | handle_15530e(struct bregs *regs) |
| 176 | { |
| 177 | regs->ah = 1; |
| 178 | regs->al = 2; |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame] | 179 | set_success(regs); |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | // APM Engage / Disengage |
| 183 | static void |
| 184 | handle_15530f(struct bregs *regs) |
| 185 | { |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame] | 186 | set_success(regs); |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | // APM Get Capabilities |
| 190 | static void |
| 191 | handle_155310(struct bregs *regs) |
| 192 | { |
| 193 | regs->bl = 0; |
| 194 | regs->cx = 0; |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame] | 195 | set_success(regs); |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static void |
| 199 | handle_1553XX(struct bregs *regs) |
| 200 | { |
Kevin O'Connor | dfefeb5 | 2009-12-13 13:04:17 -0500 | [diff] [blame] | 201 | set_unimplemented(regs); |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 202 | } |
| 203 | |
Kevin O'Connor | c003148 | 2010-01-01 13:03:17 -0500 | [diff] [blame] | 204 | void |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 205 | handle_1553(struct bregs *regs) |
| 206 | { |
Kevin O'Connor | 7ba6b30 | 2008-05-26 15:19:33 -0400 | [diff] [blame] | 207 | if (! CONFIG_APMBIOS) { |
Kevin O'Connor | dfefeb5 | 2009-12-13 13:04:17 -0500 | [diff] [blame] | 208 | set_code_invalid(regs, RET_EUNSUPPORTED); |
Kevin O'Connor | 7ba6b30 | 2008-05-26 15:19:33 -0400 | [diff] [blame] | 209 | return; |
| 210 | } |
| 211 | |
Kevin O'Connor | c38e480 | 2008-03-15 11:07:50 -0400 | [diff] [blame] | 212 | //debug_stub(regs); |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 213 | switch (regs->al) { |
| 214 | case 0x00: handle_155300(regs); break; |
| 215 | case 0x01: handle_155301(regs); break; |
| 216 | case 0x02: handle_155302(regs); break; |
| 217 | case 0x03: handle_155303(regs); break; |
| 218 | case 0x04: handle_155304(regs); break; |
| 219 | case 0x05: handle_155305(regs); break; |
Kevin O'Connor | 0fae9e1 | 2008-06-07 14:51:14 -0400 | [diff] [blame] | 220 | case 0x06: handle_155306(regs); break; |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 221 | case 0x07: handle_155307(regs); break; |
| 222 | case 0x08: handle_155308(regs); break; |
| 223 | case 0x0a: handle_15530a(regs); break; |
| 224 | case 0x0b: handle_15530b(regs); break; |
| 225 | case 0x0e: handle_15530e(regs); break; |
| 226 | case 0x0f: handle_15530f(regs); break; |
| 227 | case 0x10: handle_155310(regs); break; |
| 228 | default: handle_1553XX(regs); break; |
| 229 | } |
| 230 | } |
Kevin O'Connor | c003148 | 2010-01-01 13:03:17 -0500 | [diff] [blame] | 231 | |
Kevin O'Connor | d3e4367 | 2012-05-28 11:37:53 -0400 | [diff] [blame] | 232 | void VISIBLE16 VISIBLE32SEG |
| 233 | handle_apm(struct bregs *regs) |
Kevin O'Connor | c003148 | 2010-01-01 13:03:17 -0500 | [diff] [blame] | 234 | { |
| 235 | debug_enter(regs, DEBUG_HDL_apm); |
| 236 | handle_1553(regs); |
| 237 | } |