2 * This file is part of the coreboot project.
4 * Copyright (C) 2009 coresystems GmbH
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include <device/pci.h>
24 #include <arch/registers.h>
25 #include <console/console.h>
26 #define printk(x...) do_printk(x)
28 #include <arch/interrupt.h>
30 #define REALMODE_BASE ((void *)0x600)
36 void x86_exception(struct eregs *info);
37 void run_bios(struct device *dev, unsigned long addr);
39 extern unsigned char __idt_handler, __idt_handler_size;
40 extern unsigned char __realmode_code, __realmode_code_size;
41 extern unsigned char __run_optionrom, __run_interrupt;
43 void (*run_optionrom)(u32 devfn) __attribute__((regparm(0))) = (void *)&__run_optionrom;
44 void (*vga_enable_console)(void) __attribute__((regparm(0))) = (void *)&__run_interrupt;
46 int (*intXX_handler[256])(struct eregs *regs) = { NULL };
48 static int intXX_exception_handler(struct eregs *regs)
50 printk(BIOS_INFO, "Oops, exception %d while executing option rom\n",
52 x86_exception(regs); // Call coreboot exception handler
54 return 0; // Never returns?
57 static int intXX_unknown_handler(struct eregs *regs)
59 printk(BIOS_INFO, "Unsupported software interrupt #0x%x\n",
65 /* setup interrupt handlers for mainboard */
66 void mainboard_interrupt_handlers(int intXX, void *intXX_func)
68 intXX_handler[intXX] = intXX_func;
71 int int12_handler(struct eregs *regs);
72 int int15_handler(struct eregs *regs);
73 int int1a_handler(struct eregs *regs);
75 static void setup_interrupt_handlers(void)
79 /* The first 16 intXX functions are not BIOS services,
80 * but the CPU-generated exceptions ("hardware interrupts")
82 for (i = 0; i < 0x10; i++)
83 intXX_handler[i] = &intXX_exception_handler;
85 /* Mark all other intXX calls as unknown first */
86 for (i = 0x10; i < 0x100; i++)
88 /* If the mainboard_interrupt_handler isn't called first.
92 /* Now set the default functions that are actually
93 * needed to initialize the option roms. This is very
94 * slick, as it allows us to implement mainboard specific
95 * interrupt handlers, such as the int15
99 intXX_handler[0x12] = &int12_handler;
102 intXX_handler[0x15] = &int15_handler;
105 intXX_handler[0x1a] = &int1a_handler;
108 intXX_handler[i] = &intXX_unknown_handler;
115 static void write_idt_stub(void *target, u8 intnum)
117 unsigned char *codeptr;
118 codeptr = (unsigned char *) target;
119 memcpy(codeptr, &__idt_handler, (size_t)&__idt_handler_size);
120 codeptr[3] = intnum; /* modify int# in the code stub. */
123 static void setup_realmode_idt(void)
125 struct realmode_idt *idts = (struct realmode_idt *) 0;
128 /* Copy IDT stub code for each interrupt. This might seem wasteful
129 * but it is really simple
131 for (i = 0; i < 256; i++) {
133 idts[i].offset = 0x1000 + (i * (u32)&__idt_handler_size);
134 write_idt_stub((void *)((u32 )idts[i].offset), i);
137 /* Many option ROMs use the hard coded interrupt entry points in the
138 * system bios. So install them at the known locations.
139 * Only need int10 so far.
142 /* int42 is the relocated int10 */
143 write_idt_stub((void *)0xff065, 0x42);
146 void run_bios(struct device *dev, unsigned long addr)
148 /* clear vga bios data area */
149 memset((void *)0x400, 0, 0x200);
151 /* Set up C interrupt handlers */
152 setup_interrupt_handlers();
154 /* Setting up realmode IDT */
155 setup_realmode_idt();
157 memcpy(REALMODE_BASE, &__realmode_code, (size_t)&__realmode_code_size);
158 printk(BIOS_SPEW, "Real mode stub @%p: %d bytes\n", REALMODE_BASE,
159 (u32)&__realmode_code_size);
161 printk(BIOS_DEBUG, "Calling Option ROM...\n");
162 run_optionrom((dev->bus->secondary << 8) | dev->path.pci.devfn);
163 printk(BIOS_DEBUG, "... Option ROM returned.\n");
166 int __attribute__((regparm(0))) interrupt_handler(u32 intnumber,
172 u32 cs_ip, u16 stackflags);
174 int __attribute__((regparm(0))) interrupt_handler(u32 intnumber,
180 u32 cs_ip, u16 stackflags)
186 struct eregs reg_info;
192 printk(BIOS_DEBUG, "oprom: INT# 0x%x\n", intnumber);
193 printk(BIOS_DEBUG, "oprom: eax: %08x ebx: %08x ecx: %08x edx: %08x\n",
195 printk(BIOS_DEBUG, "oprom: ebp: %08x esp: %08x edi: %08x esi: %08x\n",
197 printk(BIOS_DEBUG, "oprom: ip: %04x cs: %04x flags: %08x\n",
200 // Fetch arguments from the stack and put them into
201 // a structure that we want to pass on to our sub interrupt
203 reg_info = (struct eregs) {
219 // Call the interrupt handler for this int#
220 ret = intXX_handler[intnumber](®_info);
222 // Put registers back on the stack. The assembler code
223 // will later pop them.
224 // What happens here is that we force (volatile!) changing
225 // the values of the parameters of this function. We do this
226 // because we know that they stay alive on the stack after
227 // we leave this function. Don't say this is bollocks.
228 *(volatile u32 *)&eax = reg_info.eax;
229 *(volatile u32 *)&ecx = reg_info.ecx;
230 *(volatile u32 *)&edx = reg_info.edx;
231 *(volatile u32 *)&ebx = reg_info.ebx;
232 *(volatile u32 *)&esi = reg_info.esi;
233 *(volatile u32 *)&edi = reg_info.edi;
234 flags = reg_info.eflags;
236 /* Pass errors back to our caller via the CARRY flag */
238 printk(BIOS_DEBUG,"error!\n");
239 flags |= 1; // error: set carry
241 flags &= ~1; // no error: clear carry
243 *(volatile u16 *)&stackflags = flags;