Fix apparent bug in 16bit resume code.
[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     switch (status) {
37     case 0xfe:
38         if (CONFIG_S3_RESUME) {
39             // S3 resume request.  Jump to 32bit mode to handle the resume.
40             asm volatile(
41                 "movw %w1, %%ss\n"
42                 "movl %0, %%esp\n"
43                 "pushl $_code32_s3_resume\n"
44                 "jmp transition32\n"
45                 : : "i"(BUILD_S3RESUME_STACK_ADDR), "r"(0)
46                 );
47             break;
48         }
49         // NO BREAK
50     case 0x00:
51     case 0x09:
52     case 0x0d ... 0xfd:
53     case 0xff:
54         // Normal post - now that status has been cleared a reset will
55         // run regular boot code..
56         reset_vector();
57         break;
58
59     case 0x05:
60         // flush keyboard (issue EOI) and jump via 40h:0067h
61         eoi_pic2();
62         // NO BREAK
63     case 0x0a:
64 #define BDA_JUMP_IP (((struct bios_data_area_s *)0)->jump_ip)
65         // resume execution by jump via 40h:0067h
66         asm volatile(
67             "movw %w1, %%ds\n"
68             "ljmpw *%0\n"
69             : : "m"(BDA_JUMP_IP), "r"(SEG_BDA)
70             );
71         break;
72
73     case 0x0b:
74         // resume execution via IRET via 40h:0067h
75         asm volatile(
76             "movw %w1, %%ds\n"
77             "lssw %0, %%sp\n"
78             "iretw\n"
79             : : "m"(BDA_JUMP_IP), "r"(SEG_BDA)
80             );
81         break;
82
83     case 0x0c:
84         // resume execution via RETF via 40h:0067h
85         asm volatile(
86             "movw %w1, %%ds\n"
87             "lssw %0, %%sp\n"
88             "lretw\n"
89             : : "m"(BDA_JUMP_IP), "r"(SEG_BDA)
90             );
91         break;
92     }
93
94     BX_PANIC("Unimplemented shutdown status: %02x\n", status);
95 }
96
97 void VISIBLE32
98 s3_resume()
99 {
100     if (!CONFIG_S3_RESUME)
101         BX_PANIC("S3 resume support not compiled in.\n");
102
103     dprintf(1, "In 32bit resume\n");
104
105     smm_init();
106
107     make_bios_readonly();
108
109     u32 s3_resume_vector = find_resume_vector();
110
111     // Invoke the resume vector.
112     struct bregs br;
113     memset(&br, 0, sizeof(br));
114     if (s3_resume_vector) {
115         dprintf(1, "Jump to resume vector (%x)\n", s3_resume_vector);
116         br.ip = FARPTR_TO_OFFSET(s3_resume_vector);
117         br.cs = FARPTR_TO_SEG(s3_resume_vector);
118     } else {
119         dprintf(1, "No resume vector set!\n");
120         // Jump to the post vector to restart with a normal boot.
121         br.ip = (u32)reset_vector - BUILD_BIOS_ADDR;
122         br.cs = SEG_BIOS;
123     }
124     call16big(&br);
125 }
126
127 // Ughh - some older gcc compilers have a bug which causes VISIBLE32
128 // functions to not be exported as global variables.
129 asm(".global s3_resume");