blob: f887f454e7cc8d351ade16669ca49d407616dfa9 [file] [log] [blame]
Kevin O'Connor18e38b22008-12-10 20:40:13 -05001// Code for handling calls to "post" that are resume related.
2//
Kevin O'Connor14458432009-05-23 18:21:18 -04003// Copyright (C) 2008,2009 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connor18e38b22008-12-10 20:40:13 -05004//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05005// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connor18e38b22008-12-10 20:40:13 -05006
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
Kevin O'Connor9967ab72008-12-18 21:57:33 -050011#include "bregs.h" // struct bregs
12#include "acpi.h" // find_resume_vector
Kevin O'Connor87b533b2011-07-10 22:35:07 -040013#include "ps2port.h" // i8042_reboot
14#include "pci.h" // pci_reboot
15#include "cmos.h" // inb_cmos
16
17// Indicator if POST phase has been run.
18int HaveRunPost VAR16VISIBLE;
Kevin O'Connor18e38b22008-12-10 20:40:13 -050019
20// Reset DMA controller
21void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -050022init_dma(void)
Kevin O'Connor18e38b22008-12-10 20:40:13 -050023{
24 // first reset the DMA controllers
25 outb(0, PORT_DMA1_MASTER_CLEAR);
26 outb(0, PORT_DMA2_MASTER_CLEAR);
27
28 // then initialize the DMA controllers
29 outb(0xc0, PORT_DMA2_MODE_REG);
30 outb(0x00, PORT_DMA2_MASK_REG);
31}
32
33// Handler for post calls that look like a resume.
34void VISIBLE16
Kevin O'Connor87b533b2011-07-10 22:35:07 -040035handle_resume(void)
Kevin O'Connor18e38b22008-12-10 20:40:13 -050036{
Kevin O'Connor18e38b22008-12-10 20:40:13 -050037 debug_serial_setup();
Kevin O'Connor87b533b2011-07-10 22:35:07 -040038 int status = inb_cmos(CMOS_RESET_CODE);
39 outb_cmos(0, CMOS_RESET_CODE);
Kevin O'Connor18e38b22008-12-10 20:40:13 -050040 dprintf(1, "In resume (status=%d)\n", status);
41
Kevin O'Connor87b533b2011-07-10 22:35:07 -040042 init_dma();
43
Kevin O'Connor18e38b22008-12-10 20:40:13 -050044 switch (status) {
Kevin O'Connor87b533b2011-07-10 22:35:07 -040045 case 0x01 ... 0x04:
46 case 0x06 ... 0x09:
47 panic("Unimplemented shutdown status: %02x\n", status);
Kevin O'Connor18e38b22008-12-10 20:40:13 -050048
49 case 0x05:
50 // flush keyboard (issue EOI) and jump via 40h:0067h
51 eoi_pic2();
52 // NO BREAK
53 case 0x0a:
Kevin O'Connor9f985422009-09-09 11:34:39 -040054#define BDA_JUMP (((struct bios_data_area_s *)0)->jump)
Kevin O'Connor18e38b22008-12-10 20:40:13 -050055 // resume execution by jump via 40h:0067h
Kevin O'Connor18e38b22008-12-10 20:40:13 -050056 asm volatile(
Kevin O'Connor590e7152009-01-19 12:53:54 -050057 "movw %w1, %%ds\n"
Kevin O'Connor18e38b22008-12-10 20:40:13 -050058 "ljmpw *%0\n"
Kevin O'Connor9f985422009-09-09 11:34:39 -040059 : : "m"(BDA_JUMP), "r"(SEG_BDA)
Kevin O'Connor18e38b22008-12-10 20:40:13 -050060 );
61 break;
62
63 case 0x0b:
64 // resume execution via IRET via 40h:0067h
65 asm volatile(
Kevin O'Connor590e7152009-01-19 12:53:54 -050066 "movw %w1, %%ds\n"
67 "lssw %0, %%sp\n"
Kevin O'Connor18e38b22008-12-10 20:40:13 -050068 "iretw\n"
Kevin O'Connor9f985422009-09-09 11:34:39 -040069 : : "m"(BDA_JUMP), "r"(SEG_BDA)
Kevin O'Connor18e38b22008-12-10 20:40:13 -050070 );
71 break;
72
73 case 0x0c:
74 // resume execution via RETF via 40h:0067h
75 asm volatile(
Kevin O'Connor590e7152009-01-19 12:53:54 -050076 "movw %w1, %%ds\n"
77 "lssw %0, %%sp\n"
Kevin O'Connor18e38b22008-12-10 20:40:13 -050078 "lretw\n"
Kevin O'Connor9f985422009-09-09 11:34:39 -040079 : : "m"(BDA_JUMP), "r"(SEG_BDA)
Kevin O'Connor18e38b22008-12-10 20:40:13 -050080 );
81 break;
Kevin O'Connor87b533b2011-07-10 22:35:07 -040082
83 default:
84 break;
Kevin O'Connor18e38b22008-12-10 20:40:13 -050085 }
86
Kevin O'Connor87b533b2011-07-10 22:35:07 -040087 // Not a 16bit resume - do remaining checks in 32bit mode
88 asm volatile(
89 "movw %w1, %%ss\n"
90 "movl %0, %%esp\n"
91 "movl $_cfunc32flat_handle_resume32, %%edx\n"
92 "jmp transition32\n"
93 : : "i"(BUILD_S3RESUME_STACK_ADDR), "r"(0), "a"(status)
94 );
Kevin O'Connor18e38b22008-12-10 20:40:13 -050095}
Kevin O'Connor9967ab72008-12-18 21:57:33 -050096
Kevin O'Connor87b533b2011-07-10 22:35:07 -040097// Handle an S3 resume event
98static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -050099s3_resume(void)
Kevin O'Connor9967ab72008-12-18 21:57:33 -0500100{
Kevin O'Connorb24c5742009-01-17 21:52:52 -0500101 if (!CONFIG_S3_RESUME)
Kevin O'Connor87b533b2011-07-10 22:35:07 -0400102 return;
Kevin O'Connorb24c5742009-01-17 21:52:52 -0500103
Kevin O'Connor87b533b2011-07-10 22:35:07 -0400104 u32 s3_resume_vector = find_resume_vector();
105 if (!s3_resume_vector) {
106 dprintf(1, "No resume vector set!\n");
107 return;
108 }
Kevin O'Connor9967ab72008-12-18 21:57:33 -0500109
Gleb Natapovecae9bf2012-01-24 14:33:42 +0200110 pic_setup();
Kevin O'Connor9967ab72008-12-18 21:57:33 -0500111 smm_init();
112
Kevin O'Connord282af72009-07-04 04:10:32 -0400113 s3_resume_vga_init();
114
Kevin O'Connor9967ab72008-12-18 21:57:33 -0500115 make_bios_readonly();
116
Kevin O'Connor9967ab72008-12-18 21:57:33 -0500117 // Invoke the resume vector.
118 struct bregs br;
119 memset(&br, 0, sizeof(br));
Kevin O'Connor87b533b2011-07-10 22:35:07 -0400120 dprintf(1, "Jump to resume vector (%x)\n", s3_resume_vector);
121 br.code = FLATPTR_TO_SEGOFF((void*)s3_resume_vector);
Kevin O'Connor9967ab72008-12-18 21:57:33 -0500122 call16big(&br);
123}
Kevin O'Connor87b533b2011-07-10 22:35:07 -0400124
125// Attempt to invoke a hard-reboot.
126static void
127tryReboot(void)
128{
129 dprintf(1, "Attempting a hard reboot\n");
130
131 // Setup for reset on qemu.
132 if (! CONFIG_COREBOOT) {
133 qemu_prep_reset();
134 if (HaveRunPost)
135 apm_shutdown();
136 }
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
150void VISIBLE32FLAT
151handle_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}