Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / seabios / src / resume.c
1 // Code for handling calls to "post" that are resume related.
2 //
3 // Copyright (C) 2008,2009  Kevin O'Connor <kevin@koconnor.net>
4 //
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
6
7 #include "bregs.h" // struct bregs
8 #include "config.h" // CONFIG_*
9 #include "farptr.h" // FLATPTR_TO_SEGOFF
10 #include "hw/pci.h" // pci_reboot
11 #include "hw/pic.h" // pic_eoi2
12 #include "hw/ps2port.h" // i8042_reboot
13 #include "hw/rtc.h" // rtc_read
14 #include "output.h" // dprintf
15 #include "stacks.h" // farcall16big
16 #include "std/bda.h" // struct bios_data_area_s
17 #include "string.h" // memset
18 #include "util.h" // dma_setup
19
20 // Handler for post calls that look like a resume.
21 void VISIBLE16
22 handle_resume(void)
23 {
24     ASSERT16();
25     int status = rtc_read(CMOS_RESET_CODE);
26     rtc_write(CMOS_RESET_CODE, 0);
27     dprintf(1, "In resume (status=%d)\n", status);
28
29     dma_setup();
30
31     switch (status) {
32     case 0x01 ... 0x04:
33     case 0x06 ... 0x09:
34         panic("Unimplemented shutdown status: %02x\n", status);
35
36     case 0x05:
37         // flush keyboard (issue EOI) and jump via 40h:0067h
38         pic_eoi2();
39         // NO BREAK
40     case 0x0a:
41 #define BDA_JUMP (((struct bios_data_area_s *)0)->jump)
42         // resume execution by jump via 40h:0067h
43         asm volatile(
44             "movw %w1, %%ds\n"
45             "ljmpw *%0\n"
46             : : "m"(BDA_JUMP), "r"(SEG_BDA)
47             );
48         break;
49
50     case 0x0b:
51         // resume execution via IRET via 40h:0067h
52         asm volatile(
53             "movw %w1, %%ds\n"
54             "lssw %0, %%sp\n"
55             "iretw\n"
56             : : "m"(BDA_JUMP), "r"(SEG_BDA)
57             );
58         break;
59
60     case 0x0c:
61         // resume execution via RETF via 40h:0067h
62         asm volatile(
63             "movw %w1, %%ds\n"
64             "lssw %0, %%sp\n"
65             "lretw\n"
66             : : "m"(BDA_JUMP), "r"(SEG_BDA)
67             );
68         break;
69
70     default:
71         break;
72     }
73
74     // Not a 16bit resume - do remaining checks in 32bit mode
75     asm volatile(
76         "movw %w1, %%ss\n"
77         "movl %0, %%esp\n"
78         "movl $_cfunc32flat_handle_resume32, %%edx\n"
79         "jmp transition32\n"
80         : : "i"(BUILD_S3RESUME_STACK_ADDR), "r"(0), "a"(status)
81         );
82 }
83
84 // Handle an S3 resume event
85 static void
86 s3_resume(void)
87 {
88     if (!CONFIG_S3_RESUME)
89         return;
90
91     u32 s3_resume_vector = find_resume_vector();
92     if (!s3_resume_vector) {
93         dprintf(1, "No resume vector set!\n");
94         return;
95     }
96
97     pic_setup();
98     smm_setup();
99
100     pci_resume();
101
102     s3_resume_vga();
103
104     make_bios_readonly();
105
106     // Invoke the resume vector.
107     struct bregs br;
108     memset(&br, 0, sizeof(br));
109     dprintf(1, "Jump to resume vector (%x)\n", s3_resume_vector);
110     br.code = FLATPTR_TO_SEGOFF((void*)s3_resume_vector);
111     farcall16big(&br);
112 }
113
114 u8 HaveAttemptedReboot VARLOW;
115
116 // Attempt to invoke a hard-reboot.
117 static void
118 tryReboot(void)
119 {
120     if (HaveAttemptedReboot) {
121         // Hard reboot has failed - try to shutdown machine.
122         dprintf(1, "Unable to hard-reboot machine - attempting shutdown.\n");
123         apm_shutdown();
124     }
125     HaveAttemptedReboot = 1;
126
127     dprintf(1, "Attempting a hard reboot\n");
128
129     // Setup for reset on qemu.
130     qemu_prep_reset();
131
132     // Reboot using ACPI RESET_REG
133     acpi_reboot();
134
135     // Try keyboard controller reboot.
136     i8042_reboot();
137
138     // Try PCI 0xcf9 reboot
139     pci_reboot();
140
141     // Try triple fault
142     asm volatile("int3");
143
144     panic("Could not reboot");
145 }
146
147 void VISIBLE32FLAT
148 handle_resume32(int status)
149 {
150     ASSERT32FLAT();
151     dprintf(1, "In 32bit resume\n");
152
153     if (status == 0xfe)
154         s3_resume();
155
156     // Must be a soft reboot - invoke a hard reboot.
157     tryReboot();
158 }