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