blob: cf6083d444f4710ff79b8f41b2cdc65113c83eb0 [file] [log] [blame]
H. Peter Anvin62607312007-07-11 12:18:54 -07001/* -*- linux-c -*- ------------------------------------------------------- *
2 *
3 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Copyright 2007 rPath, Inc. - All Rights Reserved
H. Peter Anvindf7699c2009-04-01 18:13:46 -07005 * Copyright 2009 Intel Corporation; author H. Peter Anvin
H. Peter Anvin62607312007-07-11 12:18:54 -07006 *
7 * This file is part of the Linux kernel, and is made available under
8 * the terms of the GNU General Public License version 2.
9 *
10 * ----------------------------------------------------------------------- */
11
12/*
H. Peter Anvin62607312007-07-11 12:18:54 -070013 * Main module for the real-mode kernel code
14 */
15
16#include "boot.h"
17
18struct boot_params boot_params __attribute__((aligned(16)));
19
20char *HEAP = _end;
21char *heap_end = _end; /* Default end of heap = no heap */
22
23/*
24 * Copy the header into the boot parameter block. Since this
25 * screws up the old-style command line protocol, adjust by
26 * filling in the new-style command line pointer instead.
27 */
H. Peter Anvin62607312007-07-11 12:18:54 -070028
29static void copy_boot_params(void)
30{
31 struct old_cmdline {
32 u16 cl_magic;
33 u16 cl_offset;
34 };
35 const struct old_cmdline * const oldcmd =
36 (const struct old_cmdline *)OLD_CL_ADDRESS;
37
38 BUILD_BUG_ON(sizeof boot_params != 4096);
39 memcpy(&boot_params.hdr, &hdr, sizeof hdr);
40
41 if (!boot_params.hdr.cmd_line_ptr &&
42 oldcmd->cl_magic == OLD_CL_MAGIC) {
43 /* Old-style command line protocol. */
44 u16 cmdline_seg;
45
46 /* Figure out if the command line falls in the region
47 of memory that an old kernel would have copied up
48 to 0x90000... */
49 if (oldcmd->cl_offset < boot_params.hdr.setup_move_size)
50 cmdline_seg = ds();
51 else
52 cmdline_seg = 0x9000;
53
54 boot_params.hdr.cmd_line_ptr =
55 (cmdline_seg << 4) + oldcmd->cl_offset;
56 }
57}
58
59/*
Joshua Covb2d0b7a2012-04-13 21:08:26 +020060 * Query the keyboard lock status as given by the BIOS, and
61 * set the keyboard repeat rate to maximum. Unclear why the latter
H. Peter Anvin62607312007-07-11 12:18:54 -070062 * is done here; this might be possible to kill off as stale code.
63 */
Joshua Covb2d0b7a2012-04-13 21:08:26 +020064static void keyboard_init(void)
H. Peter Anvin62607312007-07-11 12:18:54 -070065{
Joshua Covb2d0b7a2012-04-13 21:08:26 +020066 struct biosregs ireg, oreg;
H. Peter Anvindf7699c2009-04-01 18:13:46 -070067 initregs(&ireg);
Joshua Covb2d0b7a2012-04-13 21:08:26 +020068
69 ireg.ah = 0x02; /* Get keyboard status */
70 intcall(0x16, &ireg, &oreg);
71 boot_params.kbd_status = oreg.al;
72
73 ireg.ax = 0x0305; /* Set keyboard repeat rate */
H. Peter Anvindf7699c2009-04-01 18:13:46 -070074 intcall(0x16, &ireg, NULL);
H. Peter Anvin62607312007-07-11 12:18:54 -070075}
76
77/*
H. Peter Anvin238b7062007-07-18 17:19:30 -070078 * Get Intel SpeedStep (IST) information.
H. Peter Anvin62607312007-07-11 12:18:54 -070079 */
H. Peter Anvin238b7062007-07-18 17:19:30 -070080static void query_ist(void)
H. Peter Anvin62607312007-07-11 12:18:54 -070081{
H. Peter Anvindf7699c2009-04-01 18:13:46 -070082 struct biosregs ireg, oreg;
83
H. Peter Anvinc2dcfde2008-08-13 13:14:22 -070084 /* Some older BIOSes apparently crash on this call, so filter
85 it from machines too old to have SpeedStep at all. */
Joerg Roedel7b277182008-08-13 10:07:05 +020086 if (cpu.level < 6)
87 return;
88
H. Peter Anvindf7699c2009-04-01 18:13:46 -070089 initregs(&ireg);
90 ireg.ax = 0xe980; /* IST Support */
91 ireg.edx = 0x47534943; /* Request value */
92 intcall(0x15, &ireg, &oreg);
93
94 boot_params.ist_info.signature = oreg.eax;
95 boot_params.ist_info.command = oreg.ebx;
96 boot_params.ist_info.event = oreg.ecx;
97 boot_params.ist_info.perf_level = oreg.edx;
H. Peter Anvin62607312007-07-11 12:18:54 -070098}
99
100/*
101 * Tell the BIOS what CPU mode we intend to run in.
102 */
103static void set_bios_mode(void)
104{
105#ifdef CONFIG_X86_64
H. Peter Anvindf7699c2009-04-01 18:13:46 -0700106 struct biosregs ireg;
H. Peter Anvin62607312007-07-11 12:18:54 -0700107
H. Peter Anvindf7699c2009-04-01 18:13:46 -0700108 initregs(&ireg);
109 ireg.ax = 0xec00;
110 ireg.bx = 2;
111 intcall(0x15, &ireg, NULL);
H. Peter Anvin62607312007-07-11 12:18:54 -0700112#endif
113}
114
H. Peter Anvinacd644b2008-01-30 13:33:04 +0100115static void init_heap(void)
116{
117 char *stack_end;
118
119 if (boot_params.hdr.loadflags & CAN_USE_HEAP) {
120 asm("leal %P1(%%esp),%0"
121 : "=r" (stack_end) : "i" (-STACK_SIZE));
122
123 heap_end = (char *)
124 ((size_t)boot_params.hdr.heap_end_ptr + 0x200);
125 if (heap_end > stack_end)
126 heap_end = stack_end;
127 } else {
128 /* Boot protocol 2.00 only, no heap available */
129 puts("WARNING: Ancient bootloader, some functionality "
130 "may be limited!\n");
131 }
132}
133
H. Peter Anvin62607312007-07-11 12:18:54 -0700134void main(void)
135{
136 /* First, copy the boot header into the "zeropage" */
137 copy_boot_params();
138
Pekka Enbergfa97bdf2010-07-11 11:06:57 +0300139 /* Initialize the early-boot console */
140 console_init();
Yinghai Lu8fee13a2010-08-02 16:21:22 -0700141 if (cmdline_find_option_bool("debug"))
142 puts("early console in setup code\n");
Pekka Enbergfa97bdf2010-07-11 11:06:57 +0300143
H. Peter Anvin62607312007-07-11 12:18:54 -0700144 /* End of heap check */
H. Peter Anvinacd644b2008-01-30 13:33:04 +0100145 init_heap();
H. Peter Anvin62607312007-07-11 12:18:54 -0700146
147 /* Make sure we have all the proper CPU support */
148 if (validate_cpu()) {
149 puts("Unable to boot - please use a kernel appropriate "
150 "for your CPU.\n");
151 die();
152 }
153
154 /* Tell the BIOS what CPU mode we intend to run in. */
155 set_bios_mode();
156
157 /* Detect memory layout */
158 detect_memory();
159
Joshua Covb2d0b7a2012-04-13 21:08:26 +0200160 /* Set keyboard repeat rate (why?) and query the lock flags */
161 keyboard_init();
H. Peter Anvin62607312007-07-11 12:18:54 -0700162
H. Peter Anvin62607312007-07-11 12:18:54 -0700163 /* Query MCA information */
164 query_mca();
165
H. Peter Anvin238b7062007-07-18 17:19:30 -0700166 /* Query Intel SpeedStep (IST) information */
167 query_ist();
H. Peter Anvin62607312007-07-11 12:18:54 -0700168
169 /* Query APM information */
170#if defined(CONFIG_APM) || defined(CONFIG_APM_MODULE)
171 query_apm_bios();
172#endif
173
174 /* Query EDD information */
175#if defined(CONFIG_EDD) || defined(CONFIG_EDD_MODULE)
176 query_edd();
177#endif
H. Peter Anvin1a8514e2008-01-30 13:33:03 +0100178
179 /* Set the video mode */
180 set_video();
181
H. Peter Anvin62607312007-07-11 12:18:54 -0700182 /* Do the last things and invoke protected mode */
183 go_to_protected_mode();
184}