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