blob: adbb5fd776da9ca64d3a3221ad435723a6e48f89 [file] [log] [blame]
Stefan Reinauer38cd29e2009-08-11 21:28:25 +00001/*
2 * This file is part of the coreboot project.
3 *
Stefan Reinauer9839cbd2010-04-21 20:06:10 +00004 * Copyright (C) 2007 Advanced Micro Devices, Inc.
5 * Copyright (C) 2009-2010 coresystems GmbH
Stefan Reinauer38cd29e2009-08-11 21:28:25 +00006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
Stefan Reinauer6c641ee2009-09-23 21:52:45 +000018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
Stefan Reinauer38cd29e2009-08-11 21:28:25 +000019 */
20
21#include <device/pci.h>
22#include <string.h>
23
Stefan Reinauer38cd29e2009-08-11 21:28:25 +000024#include <arch/io.h>
Stefan Reinauer42dc7212009-10-24 00:47:07 +000025#include <arch/registers.h>
Stefan Reinauer38cd29e2009-08-11 21:28:25 +000026#include <console/console.h>
Libra Lic1436932009-12-23 19:16:47 +000027#include <arch/interrupt.h>
28
Stefan Reinauer074356e2009-10-25 19:50:47 +000029#define REALMODE_BASE ((void *)0x600)
Stefan Reinauer38cd29e2009-08-11 21:28:25 +000030
31struct realmode_idt {
32 u16 offset, cs;
33};
34
Stefan Reinauer38cd29e2009-08-11 21:28:25 +000035void x86_exception(struct eregs *info);
36
Stefan Reinauer841af5e2010-05-11 15:39:20 +000037/* From x86_asm.S */
Stefan Reinauer38cd29e2009-08-11 21:28:25 +000038extern unsigned char __idt_handler, __idt_handler_size;
39extern unsigned char __realmode_code, __realmode_code_size;
Stefan Reinauer841af5e2010-05-11 15:39:20 +000040extern unsigned char __realmode_call, __realmode_interrupt;
Stefan Reinauer38cd29e2009-08-11 21:28:25 +000041
Stefan Reinauer841af5e2010-05-11 15:39:20 +000042void (*realmode_call)(u32 addr, u32 eax, u32 ebx, u32 ecx, u32 edx,
43 u32 esi, u32 edi) __attribute__((regparm(0))) = (void *)&__realmode_call;
44
45void (*realmode_interrupt)(u32 intno, u32 eax, u32 ebx, u32 ecx, u32 edx,
46 u32 esi, u32 edi) __attribute__((regparm(0))) = (void *)&__realmode_interrupt;
47
48#define FAKE_MEMORY_SIZE (1024*1024) // only 1MB
49#define INITIAL_EBDA_SEGMENT 0xF600
50#define INITIAL_EBDA_SIZE 0x400
51
52static void setup_bda(void)
53{
54 /* clear BIOS DATA AREA */
55 memset((void *)0x400, 0, 0x200);
56
57 write16(0x413, FAKE_MEMORY_SIZE / 1024);
58 write16(0x40e, INITIAL_EBDA_SEGMENT);
59
60 /* Set up EBDA */
61 memset((void *)(INITIAL_EBDA_SEGMENT << 4), 0, INITIAL_EBDA_SIZE);
62 write16((INITIAL_EBDA_SEGMENT << 4) + 0x0, INITIAL_EBDA_SIZE / 1024);
63}
64
65static void setup_rombios(void)
66{
67 const char date[] = "06/11/99";
68 memcpy((void *)0xffff5, &date, 8);
69
70 const char ident[] = "PCI_ISA";
71 memcpy((void *)0xfffd9, &ident, 7);
72
73 /* system model: IBM-AT */
74 write8(0xffffe, 0xfc);
75}
Stefan Reinauer38cd29e2009-08-11 21:28:25 +000076
77int (*intXX_handler[256])(struct eregs *regs) = { NULL };
78
79static int intXX_exception_handler(struct eregs *regs)
80{
Stefan Reinauer14e22772010-04-27 06:56:47 +000081 printk(BIOS_INFO, "Oops, exception %d while executing option rom\n",
Stefan Reinauer38cd29e2009-08-11 21:28:25 +000082 regs->vector);
Stefan Reinauer841af5e2010-05-11 15:39:20 +000083#if 0
84 // Odd: The i945GM VGA oprom chokes on a pushl %eax and will
85 // die with an exception #6 if we run the coreboot exception
86 // handler. Just continue, as it executes fine.
Stefan Reinauer14e22772010-04-27 06:56:47 +000087 x86_exception(regs); // Call coreboot exception handler
Stefan Reinauer841af5e2010-05-11 15:39:20 +000088#endif
Stefan Reinauer38cd29e2009-08-11 21:28:25 +000089
90 return 0; // Never returns?
91}
92
93static int intXX_unknown_handler(struct eregs *regs)
94{
Myles Watsonf53eaa32010-09-09 14:42:58 +000095 printk(BIOS_INFO, "Unsupported software interrupt #0x%x eax 0x%x\n",
96 regs->vector, regs->eax);
Stefan Reinauer38cd29e2009-08-11 21:28:25 +000097
98 return -1;
99}
100
Libra Lic1436932009-12-23 19:16:47 +0000101/* setup interrupt handlers for mainboard */
102void mainboard_interrupt_handlers(int intXX, void *intXX_func)
103{
104 intXX_handler[intXX] = intXX_func;
105}
106
Myles Watsonf53eaa32010-09-09 14:42:58 +0000107static int int10_handler(struct eregs *regs)
108{
109 int res=-1;
110 static u8 cursor_row=0, cursor_col=0;
111 switch((regs->eax & 0xff00)>>8) {
112 case 0x01: // Set cursor shape
113 res = 0;
114 break;
115 case 0x02: // Set cursor position
116 if (cursor_row != ((regs->edx >> 8) & 0xff) ||
117 cursor_col >= (regs->edx & 0xff)) {
118 printk(BIOS_INFO, "\n");
119 }
120 cursor_row = (regs->edx >> 8) & 0xff;
121 cursor_col = regs->edx & 0xff;
122 res = 0;
123 break;
124 case 0x03: // Get cursor position
125 regs->eax &= 0x00ff;
126 regs->ecx = 0x0607;
127 regs->edx = (cursor_row << 8) | cursor_col;
128 res = 0;
129 break;
130 case 0x06: // Scroll up
131 printk(BIOS_INFO, "\n");
132 res = 0;
133 break;
134 case 0x08: // Get Character and Mode at Cursor Position
135 regs->eax = 0x0f00 | 'A'; // White on black 'A'
136 res = 0;
137 break;
138 case 0x09: // Write Character and attribute
139 case 0x10: // Write Character
140 printk(BIOS_INFO, "%c", regs->eax & 0xff);
141 res = 0;
142 break;
143 case 0x0f: // Get video mode
144 regs->eax = 0x5002; //80x25
145 regs->ebx &= 0x00ff;
146 res = 0;
147 break;
148 default:
149 printk(BIOS_WARNING, "Unknown INT10 function %04x!\n",
150 regs->eax & 0xffff);
151 break;
152 }
153 return res;
154}
155
156static int int16_handler(struct eregs *regs)
157{
158 int res=-1;
159 switch((regs->eax & 0xff00)>>8) {
160 case 0x00: // Check for Keystroke
161 regs->eax = 0x6120; // Space Bar, Space
162 res = 0;
163 break;
164 case 0x01: // Check for Keystroke
165 regs->eflags |= 1<<6; // Zero Flag set (no key available)
166 res = 0;
167 break;
168 default:
169 printk(BIOS_WARNING, "Unknown INT16 function %04x!\n",
170 regs->eax & 0xffff);
171 break;
172 }
173 return res;
174}
175
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000176int int12_handler(struct eregs *regs);
177int int15_handler(struct eregs *regs);
178int int1a_handler(struct eregs *regs);
179
180static void setup_interrupt_handlers(void)
181{
182 int i;
183
Stefan Reinauer14e22772010-04-27 06:56:47 +0000184 /* The first 16 intXX functions are not BIOS services,
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000185 * but the CPU-generated exceptions ("hardware interrupts")
186 */
187 for (i = 0; i < 0x10; i++)
188 intXX_handler[i] = &intXX_exception_handler;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000189
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000190 /* Mark all other intXX calls as unknown first */
191 for (i = 0x10; i < 0x100; i++)
Libra Lic1436932009-12-23 19:16:47 +0000192 {
193 /* If the mainboard_interrupt_handler isn't called first.
194 */
195 if(!intXX_handler[i])
196 {
197 /* Now set the default functions that are actually
198 * needed to initialize the option roms. This is very
199 * slick, as it allows us to implement mainboard specific
200 * interrupt handlers, such as the int15
201 */
202 switch (i) {
Myles Watsonf53eaa32010-09-09 14:42:58 +0000203 case 0x10:
204 intXX_handler[0x10] = &int10_handler;
205 break;
Libra Lic1436932009-12-23 19:16:47 +0000206 case 0x12:
207 intXX_handler[0x12] = &int12_handler;
208 break;
209 case 0x15:
210 intXX_handler[0x15] = &int15_handler;
211 break;
Myles Watsonf53eaa32010-09-09 14:42:58 +0000212 case 0x16:
213 intXX_handler[0x16] = &int16_handler;
214 break;
Libra Lic1436932009-12-23 19:16:47 +0000215 case 0x1a:
216 intXX_handler[0x1a] = &int1a_handler;
217 break;
218 default:
219 intXX_handler[i] = &intXX_unknown_handler;
220 break;
221 }
222 }
223 }
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000224}
225
226static void write_idt_stub(void *target, u8 intnum)
227{
228 unsigned char *codeptr;
229 codeptr = (unsigned char *) target;
230 memcpy(codeptr, &__idt_handler, (size_t)&__idt_handler_size);
231 codeptr[3] = intnum; /* modify int# in the code stub. */
232}
233
234static void setup_realmode_idt(void)
235{
236 struct realmode_idt *idts = (struct realmode_idt *) 0;
237 int i;
238
239 /* Copy IDT stub code for each interrupt. This might seem wasteful
240 * but it is really simple
241 */
242 for (i = 0; i < 256; i++) {
243 idts[i].cs = 0;
244 idts[i].offset = 0x1000 + (i * (u32)&__idt_handler_size);
245 write_idt_stub((void *)((u32 )idts[i].offset), i);
246 }
247
248 /* Many option ROMs use the hard coded interrupt entry points in the
Stefan Reinauer14e22772010-04-27 06:56:47 +0000249 * system bios. So install them at the known locations.
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000250 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000251
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000252 /* int42 is the relocated int10 */
Stefan Reinauer714e2a12010-04-24 23:15:23 +0000253 write_idt_stub((void *)0xff065, 0x42);
Stefan Reinauer841af5e2010-05-11 15:39:20 +0000254 /* BIOS Int 11 Handler F000:F84D */
255 write_idt_stub((void *)0xff84d, 0x11);
256 /* BIOS Int 12 Handler F000:F841 */
257 write_idt_stub((void *)0xff841, 0x12);
258 /* BIOS Int 13 Handler F000:EC59 */
259 write_idt_stub((void *)0xfec59, 0x13);
260 /* BIOS Int 14 Handler F000:E739 */
261 write_idt_stub((void *)0xfe739, 0x14);
262 /* BIOS Int 15 Handler F000:F859 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000263 write_idt_stub((void *)0xff859, 0x15);
Stefan Reinauer841af5e2010-05-11 15:39:20 +0000264 /* BIOS Int 16 Handler F000:E82E */
265 write_idt_stub((void *)0xfe82e, 0x16);
266 /* BIOS Int 17 Handler F000:EFD2 */
267 write_idt_stub((void *)0xfefd2, 0x17);
268 /* ROM BIOS Int 1A Handler F000:FE6E */
269 write_idt_stub((void *)0xffe6e, 0x1a);
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000270}
271
272void run_bios(struct device *dev, unsigned long addr)
273{
Stefan Reinauer841af5e2010-05-11 15:39:20 +0000274 u32 num_dev = (dev->bus->secondary << 8) | dev->path.pci.devfn;
275
276 /* Set up BIOS Data Area */
277 setup_bda();
278
279 /* Set up some legacy information in the F segment */
280 setup_rombios();
Libra Lic1436932009-12-23 19:16:47 +0000281
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000282 /* Set up C interrupt handlers */
283 setup_interrupt_handlers();
284
Stefan Reinauer841af5e2010-05-11 15:39:20 +0000285 /* Set up real-mode IDT */
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000286 setup_realmode_idt();
287
288 memcpy(REALMODE_BASE, &__realmode_code, (size_t)&__realmode_code_size);
289 printk(BIOS_SPEW, "Real mode stub @%p: %d bytes\n", REALMODE_BASE,
290 (u32)&__realmode_code_size);
291
292 printk(BIOS_DEBUG, "Calling Option ROM...\n");
Stefan Reinauer841af5e2010-05-11 15:39:20 +0000293 /* TODO ES:DI Pointer to System BIOS PnP Installation Check Structure */
294 /* Option ROM entry point is at OPROM start + 3 */
295 realmode_call(addr + 0x0003, num_dev, 0xffff, 0x0000, 0xffff, 0x0, 0x0);
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000296 printk(BIOS_DEBUG, "... Option ROM returned.\n");
297}
298
Stefan Reinauer9839cbd2010-04-21 20:06:10 +0000299#if defined(CONFIG_GEODE_VSA) && CONFIG_GEODE_VSA
300#include <cpu/amd/lxdef.h>
301#include <cpu/amd/vr.h>
302#include <cbfs.h>
303
304#define VSA2_BUFFER 0x60000
305#define VSA2_ENTRY_POINT 0x60020
306
307// TODO move to a header file.
308void do_vsmbios(void);
309
310/* VSA virtual register helper */
311static u32 VSA_vrRead(u16 classIndex)
312{
313 u32 eax, ebx, ecx, edx;
314 asm volatile (
315 "movw $0x0AC1C, %%dx\n"
316 "orl $0x0FC530000, %%eax\n"
317 "outl %%eax, %%dx\n"
318 "addb $2, %%dl\n"
319 "inw %%dx, %%ax\n"
Stefan Reinauer14e22772010-04-27 06:56:47 +0000320 : "=a" (eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
Stefan Reinauer9839cbd2010-04-21 20:06:10 +0000321 : "a"(classIndex)
322 );
323
324 return eax;
325}
326
327void do_vsmbios(void)
328{
329 printk(BIOS_DEBUG, "Preparing for VSA...\n");
330
Stefan Reinauer9839cbd2010-04-21 20:06:10 +0000331 /* Set up C interrupt handlers */
332 setup_interrupt_handlers();
333
334 /* Setting up realmode IDT */
335 setup_realmode_idt();
336
337 memcpy(REALMODE_BASE, &__realmode_code, (size_t)&__realmode_code_size);
338 printk(BIOS_SPEW, "VSA: Real mode stub @%p: %d bytes\n", REALMODE_BASE,
339 (u32)&__realmode_code_size);
340
341 if ((unsigned int)cbfs_load_stage("vsa") != VSA2_ENTRY_POINT) {
342 printk(BIOS_ERR, "Failed to load VSA.\n");
343 return;
344 }
345
346 unsigned char *buf = (unsigned char *)VSA2_BUFFER;
347 printk(BIOS_DEBUG, "VSA: Buffer @%p *[0k]=%02x\n", buf, buf[0]);
348 printk(BIOS_DEBUG, "VSA: Signature *[0x20-0x23] is %02x:%02x:%02x:%02x\n",
349 buf[0x20], buf[0x21], buf[0x22], buf[0x23]);
350
351 /* Check for code to emit POST code at start of VSA. */
352 if ((buf[0x20] != 0xb0) || (buf[0x21] != 0x10) ||
353 (buf[0x22] != 0xe6) || (buf[0x23] != 0x80)) {
354 printk(BIOS_WARNING, "VSA: Signature incorrect. Install failed.\n");
355 return;
356 }
357
358 printk(BIOS_DEBUG, "Calling VSA module...\n");
Stefan Reinauer841af5e2010-05-11 15:39:20 +0000359
Stefan Reinauer9839cbd2010-04-21 20:06:10 +0000360 /* ECX gets SMM, EDX gets SYSMEM */
Stefan Reinauer841af5e2010-05-11 15:39:20 +0000361 realmode_call(VSA2_ENTRY_POINT, 0x0, 0x0, MSR_GLIU0_SMM,
362 MSR_GLIU0_SYSMEM, 0x0, 0x0);
363
Stefan Reinauer9839cbd2010-04-21 20:06:10 +0000364 printk(BIOS_DEBUG, "... VSA module returned.\n");
365
366 /* Restart timer 1 */
367 outb(0x56, 0x43);
368 outb(0x12, 0x41);
369
370 /* Check that VSA is running OK */
371 if (VSA_vrRead(SIGNATURE) == VSA2_SIGNATURE)
372 printk(BIOS_DEBUG, "VSM: VSA2 VR signature verified.\n");
373 else
374 printk(BIOS_ERR, "VSM: VSA2 VR signature not valid. Install failed.\n");
375}
376#endif
377
Stefan Reinauer14e22772010-04-27 06:56:47 +0000378/* interrupt_handler() is called from assembler code only,
Stefan Reinauer9839cbd2010-04-21 20:06:10 +0000379 * so there is no use in putting the prototype into a header file.
380 */
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000381int __attribute__((regparm(0))) interrupt_handler(u32 intnumber,
382 u32 gsfs, u32 dses,
383 u32 edi, u32 esi,
384 u32 ebp, u32 esp,
385 u32 ebx, u32 edx,
386 u32 ecx, u32 eax,
Uwe Hermann312673c2009-10-27 21:49:33 +0000387 u32 cs_ip, u16 stackflags);
388
389int __attribute__((regparm(0))) interrupt_handler(u32 intnumber,
390 u32 gsfs, u32 dses,
391 u32 edi, u32 esi,
392 u32 ebp, u32 esp,
393 u32 ebx, u32 edx,
394 u32 ecx, u32 eax,
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000395 u32 cs_ip, u16 stackflags)
396{
397 u32 ip;
398 u32 cs;
399 u32 flags;
400 int ret = -1;
401 struct eregs reg_info;
402
403 ip = cs_ip & 0xffff;
404 cs = cs_ip >> 16;
405 flags = stackflags;
406
Myles Watson6c9bc012010-09-07 22:30:15 +0000407#if CONFIG_REALMODE_DEBUG
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000408 printk(BIOS_DEBUG, "oprom: INT# 0x%x\n", intnumber);
409 printk(BIOS_DEBUG, "oprom: eax: %08x ebx: %08x ecx: %08x edx: %08x\n",
410 eax, ebx, ecx, edx);
411 printk(BIOS_DEBUG, "oprom: ebp: %08x esp: %08x edi: %08x esi: %08x\n",
412 ebp, esp, edi, esi);
413 printk(BIOS_DEBUG, "oprom: ip: %04x cs: %04x flags: %08x\n",
414 ip, cs, flags);
Myles Watson6c9bc012010-09-07 22:30:15 +0000415#endif
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000416
417 // Fetch arguments from the stack and put them into
418 // a structure that we want to pass on to our sub interrupt
419 // handlers.
420 reg_info = (struct eregs) {
421 .eax=eax,
422 .ecx=ecx,
423 .edx=edx,
424 .ebx=ebx,
425 .esp=esp,
426 .ebp=ebp,
427 .esi=esi,
428 .edi=edi,
429 .vector=intnumber,
430 .error_code=0, // ??
431 .eip=ip,
432 .cs=cs,
433 .eflags=flags // ??
434 };
435
436 // Call the interrupt handler for this int#
437 ret = intXX_handler[intnumber](&reg_info);
438
439 // Put registers back on the stack. The assembler code
440 // will later pop them.
441 // What happens here is that we force (volatile!) changing
442 // the values of the parameters of this function. We do this
Stefan Reinauer14e22772010-04-27 06:56:47 +0000443 // because we know that they stay alive on the stack after
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000444 // we leave this function. Don't say this is bollocks.
445 *(volatile u32 *)&eax = reg_info.eax;
446 *(volatile u32 *)&ecx = reg_info.ecx;
447 *(volatile u32 *)&edx = reg_info.edx;
448 *(volatile u32 *)&ebx = reg_info.ebx;
449 *(volatile u32 *)&esi = reg_info.esi;
450 *(volatile u32 *)&edi = reg_info.edi;
451 flags = reg_info.eflags;
452
453 /* Pass errors back to our caller via the CARRY flag */
454 if (ret) {
Stefan Reinauerf75b19a2010-04-22 18:15:32 +0000455 printk(BIOS_DEBUG,"int%02x call returned error.\n", intnumber);
Stefan Reinauer38cd29e2009-08-11 21:28:25 +0000456 flags |= 1; // error: set carry
457 }else{
458 flags &= ~1; // no error: clear carry
459 }
460 *(volatile u16 *)&stackflags = flags;
461
462 return ret;
463}
464