xref: /OK3568_Linux_fs/kernel/arch/x86/boot/main.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /* -*- linux-c -*- ------------------------------------------------------- *
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  *   Copyright (C) 1991, 1992 Linus Torvalds
5*4882a593Smuzhiyun  *   Copyright 2007 rPath, Inc. - All Rights Reserved
6*4882a593Smuzhiyun  *   Copyright 2009 Intel Corporation; author H. Peter Anvin
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * ----------------------------------------------------------------------- */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun /*
11*4882a593Smuzhiyun  * Main module for the real-mode kernel code
12*4882a593Smuzhiyun  */
13*4882a593Smuzhiyun #include <linux/build_bug.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #include "boot.h"
16*4882a593Smuzhiyun #include "string.h"
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun struct boot_params boot_params __attribute__((aligned(16)));
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun char *HEAP = _end;
21*4882a593Smuzhiyun char *heap_end = _end;		/* Default end of heap = no heap */
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun /*
24*4882a593Smuzhiyun  * Copy the header into the boot parameter block.  Since this
25*4882a593Smuzhiyun  * screws up the old-style command line protocol, adjust by
26*4882a593Smuzhiyun  * filling in the new-style command line pointer instead.
27*4882a593Smuzhiyun  */
28*4882a593Smuzhiyun 
copy_boot_params(void)29*4882a593Smuzhiyun static void copy_boot_params(void)
30*4882a593Smuzhiyun {
31*4882a593Smuzhiyun 	struct old_cmdline {
32*4882a593Smuzhiyun 		u16 cl_magic;
33*4882a593Smuzhiyun 		u16 cl_offset;
34*4882a593Smuzhiyun 	};
35*4882a593Smuzhiyun 	const struct old_cmdline * const oldcmd =
36*4882a593Smuzhiyun 		(const struct old_cmdline *)OLD_CL_ADDRESS;
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun 	BUILD_BUG_ON(sizeof(boot_params) != 4096);
39*4882a593Smuzhiyun 	memcpy(&boot_params.hdr, &hdr, sizeof(hdr));
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun 	if (!boot_params.hdr.cmd_line_ptr &&
42*4882a593Smuzhiyun 	    oldcmd->cl_magic == OLD_CL_MAGIC) {
43*4882a593Smuzhiyun 		/* Old-style command line protocol. */
44*4882a593Smuzhiyun 		u16 cmdline_seg;
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 		/* Figure out if the command line falls in the region
47*4882a593Smuzhiyun 		   of memory that an old kernel would have copied up
48*4882a593Smuzhiyun 		   to 0x90000... */
49*4882a593Smuzhiyun 		if (oldcmd->cl_offset < boot_params.hdr.setup_move_size)
50*4882a593Smuzhiyun 			cmdline_seg = ds();
51*4882a593Smuzhiyun 		else
52*4882a593Smuzhiyun 			cmdline_seg = 0x9000;
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 		boot_params.hdr.cmd_line_ptr =
55*4882a593Smuzhiyun 			(cmdline_seg << 4) + oldcmd->cl_offset;
56*4882a593Smuzhiyun 	}
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun /*
60*4882a593Smuzhiyun  * Query the keyboard lock status as given by the BIOS, and
61*4882a593Smuzhiyun  * set the keyboard repeat rate to maximum.  Unclear why the latter
62*4882a593Smuzhiyun  * is done here; this might be possible to kill off as stale code.
63*4882a593Smuzhiyun  */
keyboard_init(void)64*4882a593Smuzhiyun static void keyboard_init(void)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun 	struct biosregs ireg, oreg;
67*4882a593Smuzhiyun 	initregs(&ireg);
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 	ireg.ah = 0x02;		/* Get keyboard status */
70*4882a593Smuzhiyun 	intcall(0x16, &ireg, &oreg);
71*4882a593Smuzhiyun 	boot_params.kbd_status = oreg.al;
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	ireg.ax = 0x0305;	/* Set keyboard repeat rate */
74*4882a593Smuzhiyun 	intcall(0x16, &ireg, NULL);
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun /*
78*4882a593Smuzhiyun  * Get Intel SpeedStep (IST) information.
79*4882a593Smuzhiyun  */
query_ist(void)80*4882a593Smuzhiyun static void query_ist(void)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun 	struct biosregs ireg, oreg;
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	/* Some older BIOSes apparently crash on this call, so filter
85*4882a593Smuzhiyun 	   it from machines too old to have SpeedStep at all. */
86*4882a593Smuzhiyun 	if (cpu.level < 6)
87*4882a593Smuzhiyun 		return;
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	initregs(&ireg);
90*4882a593Smuzhiyun 	ireg.ax  = 0xe980;	 /* IST Support */
91*4882a593Smuzhiyun 	ireg.edx = 0x47534943;	 /* Request value */
92*4882a593Smuzhiyun 	intcall(0x15, &ireg, &oreg);
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	boot_params.ist_info.signature  = oreg.eax;
95*4882a593Smuzhiyun 	boot_params.ist_info.command    = oreg.ebx;
96*4882a593Smuzhiyun 	boot_params.ist_info.event      = oreg.ecx;
97*4882a593Smuzhiyun 	boot_params.ist_info.perf_level = oreg.edx;
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun /*
101*4882a593Smuzhiyun  * Tell the BIOS what CPU mode we intend to run in.
102*4882a593Smuzhiyun  */
set_bios_mode(void)103*4882a593Smuzhiyun static void set_bios_mode(void)
104*4882a593Smuzhiyun {
105*4882a593Smuzhiyun #ifdef CONFIG_X86_64
106*4882a593Smuzhiyun 	struct biosregs ireg;
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	initregs(&ireg);
109*4882a593Smuzhiyun 	ireg.ax = 0xec00;
110*4882a593Smuzhiyun 	ireg.bx = 2;
111*4882a593Smuzhiyun 	intcall(0x15, &ireg, NULL);
112*4882a593Smuzhiyun #endif
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun 
init_heap(void)115*4882a593Smuzhiyun static void init_heap(void)
116*4882a593Smuzhiyun {
117*4882a593Smuzhiyun 	char *stack_end;
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	if (boot_params.hdr.loadflags & CAN_USE_HEAP) {
120*4882a593Smuzhiyun 		asm("leal %P1(%%esp),%0"
121*4882a593Smuzhiyun 		    : "=r" (stack_end) : "i" (-STACK_SIZE));
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 		heap_end = (char *)
124*4882a593Smuzhiyun 			((size_t)boot_params.hdr.heap_end_ptr + 0x200);
125*4882a593Smuzhiyun 		if (heap_end > stack_end)
126*4882a593Smuzhiyun 			heap_end = stack_end;
127*4882a593Smuzhiyun 	} else {
128*4882a593Smuzhiyun 		/* Boot protocol 2.00 only, no heap available */
129*4882a593Smuzhiyun 		puts("WARNING: Ancient bootloader, some functionality "
130*4882a593Smuzhiyun 		     "may be limited!\n");
131*4882a593Smuzhiyun 	}
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun 
main(void)134*4882a593Smuzhiyun void main(void)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun 	/* First, copy the boot header into the "zeropage" */
137*4882a593Smuzhiyun 	copy_boot_params();
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	/* Initialize the early-boot console */
140*4882a593Smuzhiyun 	console_init();
141*4882a593Smuzhiyun 	if (cmdline_find_option_bool("debug"))
142*4882a593Smuzhiyun 		puts("early console in setup code\n");
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	/* End of heap check */
145*4882a593Smuzhiyun 	init_heap();
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	/* Make sure we have all the proper CPU support */
148*4882a593Smuzhiyun 	if (validate_cpu()) {
149*4882a593Smuzhiyun 		puts("Unable to boot - please use a kernel appropriate "
150*4882a593Smuzhiyun 		     "for your CPU.\n");
151*4882a593Smuzhiyun 		die();
152*4882a593Smuzhiyun 	}
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	/* Tell the BIOS what CPU mode we intend to run in. */
155*4882a593Smuzhiyun 	set_bios_mode();
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	/* Detect memory layout */
158*4882a593Smuzhiyun 	detect_memory();
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	/* Set keyboard repeat rate (why?) and query the lock flags */
161*4882a593Smuzhiyun 	keyboard_init();
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	/* Query Intel SpeedStep (IST) information */
164*4882a593Smuzhiyun 	query_ist();
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	/* Query APM information */
167*4882a593Smuzhiyun #if defined(CONFIG_APM) || defined(CONFIG_APM_MODULE)
168*4882a593Smuzhiyun 	query_apm_bios();
169*4882a593Smuzhiyun #endif
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 	/* Query EDD information */
172*4882a593Smuzhiyun #if defined(CONFIG_EDD) || defined(CONFIG_EDD_MODULE)
173*4882a593Smuzhiyun 	query_edd();
174*4882a593Smuzhiyun #endif
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	/* Set the video mode */
177*4882a593Smuzhiyun 	set_video();
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	/* Do the last things and invoke protected mode */
180*4882a593Smuzhiyun 	go_to_protected_mode();
181*4882a593Smuzhiyun }
182