Add CONFIG_S3_RESUME to control support for S3 resume.
[seabios.git] / src / resume.c
1 // Code for handling calls to "post" that are resume related.
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 //
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
6
7 #include "util.h" // dprintf
8 #include "ioport.h" // outb
9 #include "pic.h" // eoi_pic2
10 #include "biosvar.h" // struct bios_data_area_s
11 #include "bregs.h" // struct bregs
12 #include "acpi.h" // find_resume_vector
13
14 // Reset DMA controller
15 void
16 init_dma()
17 {
18     // first reset the DMA controllers
19     outb(0, PORT_DMA1_MASTER_CLEAR);
20     outb(0, PORT_DMA2_MASTER_CLEAR);
21
22     // then initialize the DMA controllers
23     outb(0xc0, PORT_DMA2_MODE_REG);
24     outb(0x00, PORT_DMA2_MASK_REG);
25 }
26
27 // Handler for post calls that look like a resume.
28 void VISIBLE16
29 handle_resume(u8 status)
30 {
31     init_dma();
32
33     debug_serial_setup();
34     dprintf(1, "In resume (status=%d)\n", status);
35
36     struct bios_data_area_s *bda = MAKE_FARPTR(SEG_BDA, 0);
37     switch (status) {
38     case 0xfe:
39         if (CONFIG_S3_RESUME) {
40             // S3 resume request.  Jump to 32bit mode to handle the resume.
41             asm volatile(
42                 "movw %%ax, %%ss\n"
43                 "movl %0, %%esp\n"
44                 "pushl $_code32_s3_resume\n"
45                 "jmp transition32\n"
46                 : : "i"(BUILD_S3RESUME_STACK_ADDR), "a"(0)
47                 );
48             break;
49         }
50         // NO BREAK
51     case 0x00:
52     case 0x09:
53     case 0x0d ... 0xfd:
54     case 0xff:
55         // Normal post - now that status has been cleared a reset will
56         // run regular boot code..
57         reset_vector();
58         break;
59
60     case 0x05:
61         // flush keyboard (issue EOI) and jump via 40h:0067h
62         eoi_pic2();
63         // NO BREAK
64     case 0x0a:
65         // resume execution by jump via 40h:0067h
66         asm volatile(
67             "movw %%ax, %%ds\n"
68             "ljmpw *%0\n"
69             : : "m"(bda->jump_ip), "a"(SEG_BDA)
70             );
71         break;
72
73     case 0x0b:
74         // resume execution via IRET via 40h:0067h
75         asm volatile(
76             "movw %%ax, %%ds\n"
77             "movw %0, %%sp\n"
78             "movw %1, %%ss\n"
79             "iretw\n"
80             : : "m"(bda->jump_ip), "m"(bda->jump_cs), "a"(SEG_BDA)
81             );
82         break;
83
84     case 0x0c:
85         // resume execution via RETF via 40h:0067h
86         asm volatile(
87             "movw %%ax, %%ds\n"
88             "movw %0, %%sp\n"
89             "movw %1, %%ss\n"
90             "lretw\n"
91             : : "m"(bda->jump_ip), "m"(bda->jump_cs), "a"(SEG_BDA)
92             );
93         break;
94     }
95
96     BX_PANIC("Unimplemented shutdown status: %02x\n", status);
97 }
98
99 void VISIBLE32
100 s3_resume()
101 {
102     if (!CONFIG_S3_RESUME)
103         BX_PANIC("S3 resume support not compiled in.\n");
104
105     dprintf(1, "In 32bit resume\n");
106
107     smm_init();
108
109     make_bios_readonly();
110
111     u32 s3_resume_vector = find_resume_vector();
112
113     // Invoke the resume vector.
114     struct bregs br;
115     memset(&br, 0, sizeof(br));
116     if (s3_resume_vector) {
117         dprintf(1, "Jump to resume vector (%x)\n", s3_resume_vector);
118         br.ip = FARPTR_TO_OFFSET(s3_resume_vector);
119         br.cs = FARPTR_TO_SEG(s3_resume_vector);
120     } else {
121         dprintf(1, "No resume vector set!\n");
122         // Jump to the post vector to restart with a normal boot.
123         br.ip = (u32)reset_vector - BUILD_BIOS_ADDR;
124         br.cs = SEG_BIOS;
125     }
126     call16big(&br);
127 }
128
129 // Ughh - some older gcc compilers have a bug which causes VISIBLE32
130 // functions to not be exported as global variables.
131 asm(".global s3_resume");