Initialize
[sdk/emulator/qemu.git] / 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 "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(void)
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 $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 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 #define BDA_JUMP (((struct bios_data_area_s *)0)->jump)
64         // resume execution by jump via 40h:0067h
65         asm volatile(
66             "movw %w1, %%ds\n"
67             "ljmpw *%0\n"
68             : : "m"(BDA_JUMP), "r"(SEG_BDA)
69             );
70         break;
71
72     case 0x0b:
73         // resume execution via IRET via 40h:0067h
74         asm volatile(
75             "movw %w1, %%ds\n"
76             "lssw %0, %%sp\n"
77             "iretw\n"
78             : : "m"(BDA_JUMP), "r"(SEG_BDA)
79             );
80         break;
81
82     case 0x0c:
83         // resume execution via RETF via 40h:0067h
84         asm volatile(
85             "movw %w1, %%ds\n"
86             "lssw %0, %%sp\n"
87             "lretw\n"
88             : : "m"(BDA_JUMP), "r"(SEG_BDA)
89             );
90         break;
91     }
92
93     panic("Unimplemented shutdown status: %02x\n", status);
94 }
95
96 void VISIBLE32FLAT
97 s3_resume(void)
98 {
99     ASSERT32FLAT();
100     if (!CONFIG_S3_RESUME)
101         panic("S3 resume support not compiled in.\n");
102
103     dprintf(1, "In 32bit resume\n");
104
105     smm_init();
106
107     s3_resume_vga_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.code = FLATPTR_TO_SEGOFF((void*)s3_resume_vector);
119     } else {
120         dprintf(1, "No resume vector set!\n");
121         // Jump to the post vector to restart with a normal boot.
122         br.code = SEGOFF(SEG_BIOS, (u32)reset_vector - BUILD_BIOS_ADDR);
123     }
124     call16big(&br);
125 }