blob: 224e49b97da559697287a865282c1e89f3fe40b2 [file] [log] [blame]
Angel Ponsc74dae92020-04-02 23:48:16 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauer38cd29e2009-08-11 21:28:25 +00002
Kyösti Mälkki13f66502019-03-03 08:01:05 +02003#include <device/mmio.h>
Edward O'Callaghan91810dd2014-07-30 13:53:04 +10004#include <arch/interrupt.h>
Stefan Reinauer42dc7212009-10-24 00:47:07 +00005#include <arch/registers.h>
Edward O'Callaghan91810dd2014-07-30 13:53:04 +10006#include <boot/coreboot_tables.h>
Stefan Reinauer38cd29e2009-08-11 21:28:25 +00007#include <console/console.h>
Stefan Reinauerc1efb902011-10-12 14:30:59 -07008#include <delay.h>
Edward O'Callaghan91810dd2014-07-30 13:53:04 +10009#include <device/pci.h>
10#include <device/pci_ids.h>
Edward O'Callaghan91810dd2014-07-30 13:53:04 +100011#include <pc80/i8259.h>
zbao741a0dd2015-06-25 16:58:53 -040012#include <pc80/i8254.h>
Edward O'Callaghan91810dd2014-07-30 13:53:04 +100013#include <string.h>
14#include <vbe.h>
Patrick Rudolph92106b12020-02-19 12:54:06 +010015#include <framebuffer_info.h>
Edward O'Callaghan91810dd2014-07-30 13:53:04 +100016
Patrick Georgi199b09c2012-11-22 12:46:12 +010017/* we use x86emu's register file representation */
18#include <x86emu/regs.h>
Edward O'Callaghan91810dd2014-07-30 13:53:04 +100019
20#include "x86.h"
Patrick Georgi199b09c2012-11-22 12:46:12 +010021
Subrata Banika2602152019-07-12 17:41:43 +053022typedef struct {
23 char signature[4];
24 u16 version;
25 u8 *oem_string_ptr;
26 u32 capabilities;
27 u32 video_mode_ptr;
28 u16 total_memory;
29 char reserved[236];
30} __packed vbe_info_block;
31
Aaron Durbina146d582013-02-08 16:56:51 -060032/* The following symbols cannot be used directly. They need to be fixed up
33 * to point to the correct address location after the code has been copied
34 * to REALMODE_BASE. Absolute symbols are not used because those symbols are
35 * relocated when a relocatable ramstage is enabled.
36 */
37extern unsigned char __realmode_call, __realmode_interrupt;
38extern unsigned char __realmode_buffer;
39
40#define PTR_TO_REAL_MODE(sym)\
41 (void *)(REALMODE_BASE + ((char *)&(sym) - (char *)&__realmode_code))
42
Patrick Georgi199b09c2012-11-22 12:46:12 +010043/* to have a common register file for interrupt handlers */
44X86EMU_sysEnv _X86EMU_env;
Stefan Reinauer38cd29e2009-08-11 21:28:25 +000045
Subrata Banikc76bfac2019-07-12 16:43:17 +053046unsigned int (*realmode_call)(u32 addr, u32 eax, u32 ebx, u32 ecx, u32 edx,
Aaron Durbina146d582013-02-08 16:56:51 -060047 u32 esi, u32 edi) asmlinkage;
Stefan Reinauer841af5e2010-05-11 15:39:20 +000048
Subrata Banikc76bfac2019-07-12 16:43:17 +053049unsigned int (*realmode_interrupt)(u32 intno, u32 eax, u32 ebx, u32 ecx,
50 u32 edx, u32 esi, u32 edi) asmlinkage;
Aaron Durbina146d582013-02-08 16:56:51 -060051
52static void setup_realmode_code(void)
53{
54 memcpy(REALMODE_BASE, &__realmode_code, __realmode_code_size);
55
56 /* Ensure the global pointers are relocated properly. */
57 realmode_call = PTR_TO_REAL_MODE(__realmode_call);
58 realmode_interrupt = PTR_TO_REAL_MODE(__realmode_interrupt);
59
60 printk(BIOS_SPEW, "Real mode stub @%p: %d bytes\n", REALMODE_BASE,
61 __realmode_code_size);
62}
Stefan Reinauer841af5e2010-05-11 15:39:20 +000063
Stefan Reinauer841af5e2010-05-11 15:39:20 +000064static void setup_rombios(void)
65{
66 const char date[] = "06/11/99";
67 memcpy((void *)0xffff5, &date, 8);
68
69 const char ident[] = "PCI_ISA";
70 memcpy((void *)0xfffd9, &ident, 7);
71
72 /* system model: IBM-AT */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080073 write8((void *)0xffffe, 0xfc);
Stefan Reinauer841af5e2010-05-11 15:39:20 +000074}
Stefan Reinauer38cd29e2009-08-11 21:28:25 +000075
Patrick Georgi199b09c2012-11-22 12:46:12 +010076static int (*intXX_handler[256])(void) = { NULL };
Stefan Reinauer38cd29e2009-08-11 21:28:25 +000077
Patrick Georgi199b09c2012-11-22 12:46:12 +010078static int intXX_exception_handler(void)
Stefan Reinauer38cd29e2009-08-11 21:28:25 +000079{
Patrick Georgi199b09c2012-11-22 12:46:12 +010080 /* compatibility shim */
81 struct eregs reg_info = {
82 .eax=X86_EAX,
83 .ecx=X86_ECX,
84 .edx=X86_EDX,
85 .ebx=X86_EBX,
86 .esp=X86_ESP,
87 .ebp=X86_EBP,
88 .esi=X86_ESI,
89 .edi=X86_EDI,
90 .vector=M.x86.intno,
91 .error_code=0, // FIXME: fill in
92 .eip=X86_EIP,
93 .cs=X86_CS,
94 .eflags=X86_EFLAGS
95 };
96 struct eregs *regs = &reg_info;
97
Stefan Reinauer14e22772010-04-27 06:56:47 +000098 printk(BIOS_INFO, "Oops, exception %d while executing option rom\n",
Stefan Reinauer38cd29e2009-08-11 21:28:25 +000099 regs->vector);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000100 x86_exception(regs); // Call coreboot exception handler
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000101
Stefan Reinauerb6b88712011-10-12 14:35:54 -0700102 return 0; // Never really returns
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000103}
104
Patrick Georgi199b09c2012-11-22 12:46:12 +0100105static int intXX_unknown_handler(void)
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000106{
Myles Watsonf53eaa32010-09-09 14:42:58 +0000107 printk(BIOS_INFO, "Unsupported software interrupt #0x%x eax 0x%x\n",
Patrick Georgi199b09c2012-11-22 12:46:12 +0100108 M.x86.intno, X86_EAX);
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000109
110 return -1;
111}
112
Libra Lic1436932009-12-23 19:16:47 +0000113/* setup interrupt handlers for mainboard */
Aaron Durbin4d7a4c52013-04-23 10:25:34 -0500114void mainboard_interrupt_handlers(int intXX, int (*intXX_func)(void))
Libra Lic1436932009-12-23 19:16:47 +0000115{
116 intXX_handler[intXX] = intXX_func;
117}
118
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000119static void setup_interrupt_handlers(void)
120{
121 int i;
122
Stefan Reinauer14e22772010-04-27 06:56:47 +0000123 /* The first 16 intXX functions are not BIOS services,
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000124 * but the CPU-generated exceptions ("hardware interrupts")
125 */
126 for (i = 0; i < 0x10; i++)
127 intXX_handler[i] = &intXX_exception_handler;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000128
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000129 /* Mark all other intXX calls as unknown first */
130 for (i = 0x10; i < 0x100; i++)
Libra Lic1436932009-12-23 19:16:47 +0000131 {
132 /* If the mainboard_interrupt_handler isn't called first.
133 */
Elyes HAOUAS5ba154a2020-08-04 13:27:52 +0200134 if (!intXX_handler[i])
Libra Lic1436932009-12-23 19:16:47 +0000135 {
136 /* Now set the default functions that are actually
Stefan Reinauerc1efb902011-10-12 14:30:59 -0700137 * needed to initialize the option roms. This is
138 * very slick, as it allows us to implement mainboard
139 * specific interrupt handlers, such as the int15.
Libra Lic1436932009-12-23 19:16:47 +0000140 */
141 switch (i) {
Myles Watsonf53eaa32010-09-09 14:42:58 +0000142 case 0x10:
143 intXX_handler[0x10] = &int10_handler;
144 break;
Libra Lic1436932009-12-23 19:16:47 +0000145 case 0x12:
146 intXX_handler[0x12] = &int12_handler;
147 break;
Myles Watsonf53eaa32010-09-09 14:42:58 +0000148 case 0x16:
149 intXX_handler[0x16] = &int16_handler;
150 break;
Libra Lic1436932009-12-23 19:16:47 +0000151 case 0x1a:
152 intXX_handler[0x1a] = &int1a_handler;
153 break;
154 default:
155 intXX_handler[i] = &intXX_unknown_handler;
156 break;
157 }
158 }
159 }
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000160}
161
162static void write_idt_stub(void *target, u8 intnum)
163{
164 unsigned char *codeptr;
165 codeptr = (unsigned char *) target;
Aaron Durbina146d582013-02-08 16:56:51 -0600166 memcpy(codeptr, &__idt_handler, __idt_handler_size);
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000167 codeptr[3] = intnum; /* modify int# in the code stub. */
168}
169
170static void setup_realmode_idt(void)
171{
172 struct realmode_idt *idts = (struct realmode_idt *) 0;
173 int i;
174
175 /* Copy IDT stub code for each interrupt. This might seem wasteful
176 * but it is really simple
177 */
178 for (i = 0; i < 256; i++) {
179 idts[i].cs = 0;
Aaron Durbina146d582013-02-08 16:56:51 -0600180 idts[i].offset = 0x1000 + (i * __idt_handler_size);
Stefan Reinauer83fa1692015-06-18 22:01:07 -0700181 write_idt_stub((void *)((uintptr_t)idts[i].offset), i);
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000182 }
183
184 /* Many option ROMs use the hard coded interrupt entry points in the
Stefan Reinauer14e22772010-04-27 06:56:47 +0000185 * system bios. So install them at the known locations.
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000186 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000187
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000188 /* int42 is the relocated int10 */
Stefan Reinauer714e2a12010-04-24 23:15:23 +0000189 write_idt_stub((void *)0xff065, 0x42);
Stefan Reinauer841af5e2010-05-11 15:39:20 +0000190 /* BIOS Int 11 Handler F000:F84D */
191 write_idt_stub((void *)0xff84d, 0x11);
192 /* BIOS Int 12 Handler F000:F841 */
193 write_idt_stub((void *)0xff841, 0x12);
194 /* BIOS Int 13 Handler F000:EC59 */
195 write_idt_stub((void *)0xfec59, 0x13);
196 /* BIOS Int 14 Handler F000:E739 */
197 write_idt_stub((void *)0xfe739, 0x14);
198 /* BIOS Int 15 Handler F000:F859 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000199 write_idt_stub((void *)0xff859, 0x15);
Stefan Reinauer841af5e2010-05-11 15:39:20 +0000200 /* BIOS Int 16 Handler F000:E82E */
201 write_idt_stub((void *)0xfe82e, 0x16);
202 /* BIOS Int 17 Handler F000:EFD2 */
203 write_idt_stub((void *)0xfefd2, 0x17);
204 /* ROM BIOS Int 1A Handler F000:FE6E */
205 write_idt_stub((void *)0xffe6e, 0x1a);
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000206}
207
Julius Wernercd49cce2019-03-05 16:53:33 -0800208#if CONFIG(FRAMEBUFFER_SET_VESA_MODE)
Johanna Schanderdb7a3ae2019-07-24 10:14:26 +0200209static vbe_mode_info_t mode_info;
Gabe Blackfb8632a2012-09-30 04:47:48 -0700210static int mode_info_valid;
211
Johanna Schanderdb7a3ae2019-07-24 10:14:26 +0200212const vbe_mode_info_t *vbe_mode_info(void)
213{
214 if (!mode_info_valid || !mode_info.vesa.phys_base_ptr)
215 return NULL;
216 return &mode_info;
217}
218
Subrata Banika2602152019-07-12 17:41:43 +0530219static int vbe_check_for_failure(int ah);
220
Martin Rothaeffa862021-01-20 12:07:31 -0700221static u8 vbe_get_ctrl_info(vbe_info_block *info)
Subrata Banika2602152019-07-12 17:41:43 +0530222{
223 char *buffer = PTR_TO_REAL_MODE(__realmode_buffer);
224 u16 buffer_seg = (((unsigned long)buffer) >> 4) & 0xff00;
225 u16 buffer_adr = ((unsigned long)buffer) & 0xffff;
226 X86_EAX = realmode_interrupt(0x10, VESA_GET_INFO, 0x0000, 0x0000,
227 0x0000, buffer_seg, buffer_adr);
228 /* If the VBE function completed successfully, 0x0 is returned in AH */
Martin Rothaeffa862021-01-20 12:07:31 -0700229 if (X86_AH) {
230 printk(BIOS_WARNING, "Warning: Error from VGA BIOS in %s\n", __func__);
231 return 1;
232 }
Subrata Banika2602152019-07-12 17:41:43 +0530233 memcpy(info, buffer, sizeof(vbe_info_block));
Martin Rothaeffa862021-01-20 12:07:31 -0700234 return 0;
Subrata Banika2602152019-07-12 17:41:43 +0530235}
236
237static void vbe_oprom_list_supported_mode(uint16_t *video_mode_ptr)
238{
239 uint16_t mode;
240 printk(BIOS_DEBUG, "Supported Video Mode list for OpRom:\n");
241 do {
242 mode = *video_mode_ptr++;
243 if (mode != 0xffff)
244 printk(BIOS_DEBUG, "%x\n", mode);
245 } while (mode != 0xffff);
246}
247
Martin Rothaeffa862021-01-20 12:07:31 -0700248static u8 vbe_oprom_supported_mode_list(void)
Subrata Banika2602152019-07-12 17:41:43 +0530249{
250 uint16_t segment, offset;
251 vbe_info_block info;
252
Martin Rothaeffa862021-01-20 12:07:31 -0700253 if (vbe_get_ctrl_info(&info))
254 return 1;
Subrata Banika2602152019-07-12 17:41:43 +0530255
256 offset = info.video_mode_ptr;
257 segment = info.video_mode_ptr >> 16;
258
259 vbe_oprom_list_supported_mode((uint16_t *)((segment << 4) + offset));
Martin Rothaeffa862021-01-20 12:07:31 -0700260 return 0;
Subrata Banika2602152019-07-12 17:41:43 +0530261}
Subrata Banikc76bfac2019-07-12 16:43:17 +0530262/*
263 * EAX register is used to indicate the completion status upon return from
264 * VBE function in real mode.
265 *
266 * If the VBE function completed successfully then 0x0 is returned in the AH
267 * register. Otherwise the AH register is set with the nature of the failure:
268 *
269 * AH == 0x00: Function call successful
270 * AH == 0x01: Function call failed
271 * AH == 0x02: Function is not supported in the current HW configuration
272 * AH == 0x03: Function call invalid in current video mode
273 *
274 * Return 0 on success else -1 for failure
275 */
276static int vbe_check_for_failure(int ah)
277{
278 int status;
279
280 switch (ah) {
281 case 0x0:
282 status = 0;
283 break;
284 case 1:
285 printk(BIOS_DEBUG, "VBE: Function call failed!\n");
286 status = -1;
287 break;
288 case 2:
289 printk(BIOS_DEBUG, "VBE: Function is not supported!\n");
290 status = -1;
291 break;
292 case 3:
293 default:
294 printk(BIOS_DEBUG, "VBE: Unsupported video mode %x!\n",
295 CONFIG_FRAMEBUFFER_VESA_MODE);
Martin Rothaeffa862021-01-20 12:07:31 -0700296 if (vbe_oprom_supported_mode_list())
297 printk(BIOS_WARNING, "VBE Warning: Could not get VBE mode list.\n");
Subrata Banikc76bfac2019-07-12 16:43:17 +0530298 status = -1;
299 break;
300 }
301
302 return status;
303}
Gabe Blackfb8632a2012-09-30 04:47:48 -0700304static u8 vbe_get_mode_info(vbe_mode_info_t * mi)
Stefan Reinauerc1efb902011-10-12 14:30:59 -0700305{
Stefan Reinauer01c3de92012-08-29 09:28:52 -0700306 printk(BIOS_DEBUG, "VBE: Getting information about VESA mode %04x\n",
Gabe Blackfb8632a2012-09-30 04:47:48 -0700307 mi->video_mode);
Aaron Durbina146d582013-02-08 16:56:51 -0600308 char *buffer = PTR_TO_REAL_MODE(__realmode_buffer);
Stefan Reinauerc1efb902011-10-12 14:30:59 -0700309 u16 buffer_seg = (((unsigned long)buffer) >> 4) & 0xff00;
310 u16 buffer_adr = ((unsigned long)buffer) & 0xffff;
Subrata Banikc76bfac2019-07-12 16:43:17 +0530311 X86_EAX = realmode_interrupt(0x10, VESA_GET_MODE_INFO, 0x0000,
Gabe Blackfb8632a2012-09-30 04:47:48 -0700312 mi->video_mode, 0x0000, buffer_seg, buffer_adr);
Martin Rothaeffa862021-01-20 12:07:31 -0700313 if (vbe_check_for_failure(X86_AH)) {
314 printk(BIOS_WARNING, "VBE Warning: Error from VGA BIOS in %s\n", __func__);
315 return 1;
316 }
Zhuo-Hao Lee29445dc2014-12-24 11:13:34 +0800317 memcpy(mi->mode_info_block, buffer, sizeof(mi->mode_info_block));
Gabe Blackfb8632a2012-09-30 04:47:48 -0700318 mode_info_valid = 1;
Stefan Reinauerc1efb902011-10-12 14:30:59 -0700319 return 0;
320}
321
Gabe Blackfb8632a2012-09-30 04:47:48 -0700322static u8 vbe_set_mode(vbe_mode_info_t * mi)
Stefan Reinauerc1efb902011-10-12 14:30:59 -0700323{
Gabe Blackfb8632a2012-09-30 04:47:48 -0700324 printk(BIOS_DEBUG, "VBE: Setting VESA mode %04x\n", mi->video_mode);
Stefan Reinauerc1efb902011-10-12 14:30:59 -0700325 // request linear framebuffer mode
Gabe Blackfb8632a2012-09-30 04:47:48 -0700326 mi->video_mode |= (1 << 14);
Stefan Reinauerc1efb902011-10-12 14:30:59 -0700327 // request clearing of framebuffer
Gabe Blackfb8632a2012-09-30 04:47:48 -0700328 mi->video_mode &= ~(1 << 15);
Subrata Banikc76bfac2019-07-12 16:43:17 +0530329 X86_EAX = realmode_interrupt(0x10, VESA_SET_MODE, mi->video_mode,
Stefan Reinauerc1efb902011-10-12 14:30:59 -0700330 0x0000, 0x0000, 0x0000, 0x0000);
Martin Rothaeffa862021-01-20 12:07:31 -0700331 if (vbe_check_for_failure(X86_AH)) {
332 printk(BIOS_WARNING, "VBE Warning: Error from VGA BIOS in %s\n", __func__);
333 return 1;
334 }
Stefan Reinauerc1efb902011-10-12 14:30:59 -0700335 return 0;
336}
337
Stefan Reinauerc1efb902011-10-12 14:30:59 -0700338/* These two functions could probably even be generic between
339 * yabel and x86 native. TBD later.
340 */
341void vbe_set_graphics(void)
342{
343 mode_info.video_mode = (1 << 14) | CONFIG_FRAMEBUFFER_VESA_MODE;
Martin Rothaeffa862021-01-20 12:07:31 -0700344 if (vbe_get_mode_info(&mode_info)) {
345 printk(BIOS_WARNING, "VBE Warning: Could not get VBE graphics mode info.\n");
346 return;
347 }
Stefan Reinauerc1efb902011-10-12 14:30:59 -0700348 unsigned char *framebuffer =
Stefan Reinauerb6b88712011-10-12 14:35:54 -0700349 (unsigned char *)mode_info.vesa.phys_base_ptr;
Stefan Reinauer01c3de92012-08-29 09:28:52 -0700350 printk(BIOS_DEBUG, "VBE: resolution: %dx%d@%d\n",
351 le16_to_cpu(mode_info.vesa.x_resolution),
352 le16_to_cpu(mode_info.vesa.y_resolution),
353 mode_info.vesa.bits_per_pixel);
Johanna Schanderdb7a3ae2019-07-24 10:14:26 +0200354
Stefan Reinauer01c3de92012-08-29 09:28:52 -0700355 printk(BIOS_DEBUG, "VBE: framebuffer: %p\n", framebuffer);
356 if (!framebuffer) {
357 printk(BIOS_DEBUG, "VBE: Mode does not support linear "
358 "framebuffer\n");
359 return;
360 }
361
Martin Rothaeffa862021-01-20 12:07:31 -0700362 if (vbe_set_mode(&mode_info)) {
363 printk(BIOS_WARNING, "VBE Warning: Could not set VBE graphics mode.\n");
364 return;
365 }
Patrick Rudolph92106b12020-02-19 12:54:06 +0100366 const struct lb_framebuffer fb = {
367 .physical_address = mode_info.vesa.phys_base_ptr,
368 .x_resolution = le16_to_cpu(mode_info.vesa.x_resolution),
369 .y_resolution = le16_to_cpu(mode_info.vesa.y_resolution),
370 .bytes_per_line = le16_to_cpu(mode_info.vesa.bytes_per_scanline),
371 .bits_per_pixel = mode_info.vesa.bits_per_pixel,
372 .red_mask_pos = mode_info.vesa.red_mask_pos,
373 .red_mask_size = mode_info.vesa.red_mask_size,
374 .green_mask_pos = mode_info.vesa.green_mask_pos,
375 .green_mask_size = mode_info.vesa.green_mask_size,
376 .blue_mask_pos = mode_info.vesa.blue_mask_pos,
377 .blue_mask_size = mode_info.vesa.blue_mask_size,
378 .reserved_mask_pos = mode_info.vesa.reserved_mask_pos,
379 .reserved_mask_size = mode_info.vesa.reserved_mask_size,
380 .orientation = LB_FB_ORIENTATION_NORMAL,
381 };
382
383 fb_add_framebuffer_info_ex(&fb);
Stefan Reinauerc1efb902011-10-12 14:30:59 -0700384}
385
386void vbe_textmode_console(void)
387{
Martin Rothaeffa862021-01-20 12:07:31 -0700388 u8 retval = 1;
389 if (mode_info.vesa.phys_base_ptr) {
390 delay(2);
391 X86_EAX = realmode_interrupt(0x10, 0x0003, 0x0000, 0x0000,
392 0x0000, 0x0000, 0x0000);
393 if (!vbe_check_for_failure(X86_AH))
394 retval = 0;
395 }
396
397 if (retval)
398 printk(BIOS_WARNING, "VBE Warning: Could not set VBE text mode.\n");
Stefan Reinauerc1efb902011-10-12 14:30:59 -0700399}
400
Stefan Reinauerc1efb902011-10-12 14:30:59 -0700401#endif
402
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000403void run_bios(struct device *dev, unsigned long addr)
404{
Stefan Reinauer841af5e2010-05-11 15:39:20 +0000405 u32 num_dev = (dev->bus->secondary << 8) | dev->path.pci.devfn;
406
Stefan Reinauerb6b88712011-10-12 14:35:54 -0700407 /* Setting up required hardware.
408 * Removing this will cause random illegal instruction exceptions
409 * in some option roms.
410 */
411 setup_i8259();
zbao741a0dd2015-06-25 16:58:53 -0400412 setup_i8254();
Stefan Reinauerb6b88712011-10-12 14:35:54 -0700413
Stefan Reinauer841af5e2010-05-11 15:39:20 +0000414 /* Set up some legacy information in the F segment */
415 setup_rombios();
Libra Lic1436932009-12-23 19:16:47 +0000416
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000417 /* Set up C interrupt handlers */
418 setup_interrupt_handlers();
419
Stefan Reinauer841af5e2010-05-11 15:39:20 +0000420 /* Set up real-mode IDT */
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000421 setup_realmode_idt();
422
Aaron Durbina146d582013-02-08 16:56:51 -0600423 /* Make sure the code is placed. */
424 setup_realmode_code();
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000425
426 printk(BIOS_DEBUG, "Calling Option ROM...\n");
Stefan Reinauer841af5e2010-05-11 15:39:20 +0000427 /* TODO ES:DI Pointer to System BIOS PnP Installation Check Structure */
428 /* Option ROM entry point is at OPROM start + 3 */
429 realmode_call(addr + 0x0003, num_dev, 0xffff, 0x0000, 0xffff, 0x0, 0x0);
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000430 printk(BIOS_DEBUG, "... Option ROM returned.\n");
Stefan Reinauerc1efb902011-10-12 14:30:59 -0700431
Julius Wernercd49cce2019-03-05 16:53:33 -0800432#if CONFIG(FRAMEBUFFER_SET_VESA_MODE)
Stefan Reinauer22ae2b92013-02-08 08:48:20 -0800433 if ((dev->class >> 8)== PCI_CLASS_DISPLAY_VGA)
434 vbe_set_graphics();
Stefan Reinauerc1efb902011-10-12 14:30:59 -0700435#endif
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000436}
437
Stefan Reinauer14e22772010-04-27 06:56:47 +0000438/* interrupt_handler() is called from assembler code only,
Stefan Reinauer9839cbd2010-04-21 20:06:10 +0000439 * so there is no use in putting the prototype into a header file.
440 */
Stefan Reinauer399486e2012-12-06 13:54:29 -0800441int asmlinkage interrupt_handler(u32 intnumber,
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000442 u32 gsfs, u32 dses,
443 u32 edi, u32 esi,
444 u32 ebp, u32 esp,
445 u32 ebx, u32 edx,
446 u32 ecx, u32 eax,
Uwe Hermann312673c2009-10-27 21:49:33 +0000447 u32 cs_ip, u16 stackflags);
448
Stefan Reinauer399486e2012-12-06 13:54:29 -0800449int asmlinkage interrupt_handler(u32 intnumber,
Uwe Hermann312673c2009-10-27 21:49:33 +0000450 u32 gsfs, u32 dses,
451 u32 edi, u32 esi,
452 u32 ebp, u32 esp,
453 u32 ebx, u32 edx,
454 u32 ecx, u32 eax,
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000455 u32 cs_ip, u16 stackflags)
456{
457 u32 ip;
458 u32 cs;
459 u32 flags;
Patrick Georgi503af722012-11-22 10:48:18 +0100460 int ret = 0;
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000461
462 ip = cs_ip & 0xffff;
463 cs = cs_ip >> 16;
464 flags = stackflags;
465
Julius Wernercd49cce2019-03-05 16:53:33 -0800466#if CONFIG(REALMODE_DEBUG)
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000467 printk(BIOS_DEBUG, "oprom: INT# 0x%x\n", intnumber);
468 printk(BIOS_DEBUG, "oprom: eax: %08x ebx: %08x ecx: %08x edx: %08x\n",
469 eax, ebx, ecx, edx);
470 printk(BIOS_DEBUG, "oprom: ebp: %08x esp: %08x edi: %08x esi: %08x\n",
471 ebp, esp, edi, esi);
472 printk(BIOS_DEBUG, "oprom: ip: %04x cs: %04x flags: %08x\n",
473 ip, cs, flags);
Myles Watson6c9bc012010-09-07 22:30:15 +0000474#endif
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000475
Patrick Georgi199b09c2012-11-22 12:46:12 +0100476 // Fetch arguments from the stack and put them to a place
477 // suitable for the interrupt handlers
478 X86_EAX = eax;
479 X86_ECX = ecx;
480 X86_EDX = edx;
481 X86_EBX = ebx;
482 X86_ESP = esp;
483 X86_EBP = ebp;
484 X86_ESI = esi;
485 X86_EDI = edi;
486 M.x86.intno = intnumber;
487 /* TODO: error_code must be stored somewhere */
488 X86_EIP = ip;
489 X86_CS = cs;
490 X86_EFLAGS = flags;
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000491
492 // Call the interrupt handler for this int#
Patrick Georgi199b09c2012-11-22 12:46:12 +0100493 ret = intXX_handler[intnumber]();
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000494
495 // Put registers back on the stack. The assembler code
496 // will later pop them.
497 // What happens here is that we force (volatile!) changing
498 // the values of the parameters of this function. We do this
Stefan Reinauer14e22772010-04-27 06:56:47 +0000499 // because we know that they stay alive on the stack after
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000500 // we leave this function. Don't say this is bollocks.
Patrick Georgi199b09c2012-11-22 12:46:12 +0100501 *(volatile u32 *)&eax = X86_EAX;
502 *(volatile u32 *)&ecx = X86_ECX;
503 *(volatile u32 *)&edx = X86_EDX;
504 *(volatile u32 *)&ebx = X86_EBX;
505 *(volatile u32 *)&esi = X86_ESI;
506 *(volatile u32 *)&edi = X86_EDI;
507 flags = X86_EFLAGS;
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000508
Patrick Georgi503af722012-11-22 10:48:18 +0100509 /* Pass success or error back to our caller via the CARRY flag */
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000510 if (ret) {
Patrick Georgi503af722012-11-22 10:48:18 +0100511 flags &= ~1; // no error: clear carry
512 }else{
Stefan Reinauerf75b19a2010-04-22 18:15:32 +0000513 printk(BIOS_DEBUG,"int%02x call returned error.\n", intnumber);
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000514 flags |= 1; // error: set carry
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000515 }
516 *(volatile u16 *)&stackflags = flags;
517
Patrick Georgi503af722012-11-22 10:48:18 +0100518 /* The assembler code doesn't actually care for the return value,
519 * but keep it around so its expectations are met */
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000520 return ret;
521}