Refactor option rom initialization code in coreboot.
[coreboot.git] / src / devices / oprom / x86.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2007 Advanced Micro Devices, Inc.
5  * Copyright (C) 2009-2010 coresystems GmbH
6  *
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
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20
21 #include <device/pci.h>
22 #include <string.h>
23
24 #include <arch/io.h>
25 #include <arch/registers.h>
26 #include <console/console.h>
27 #include <arch/interrupt.h>
28
29 #include "x86.h"
30
31 void (*realmode_call)(u32 addr, u32 eax, u32 ebx, u32 ecx, u32 edx,
32                 u32 esi, u32 edi) __attribute__((regparm(0))) = (void *)&__realmode_call;
33
34 void (*realmode_interrupt)(u32 intno, u32 eax, u32 ebx, u32 ecx, u32 edx, 
35                 u32 esi, u32 edi) __attribute__((regparm(0))) = (void *)&__realmode_interrupt;
36
37 static void setup_bda(void)
38 {
39         /* clear BIOS DATA AREA */
40         memset((void *)0x400, 0, 0x200);
41
42         write16(0x413, FAKE_MEMORY_SIZE / 1024);
43         write16(0x40e, INITIAL_EBDA_SEGMENT);
44
45         /* Set up EBDA */
46         memset((void *)(INITIAL_EBDA_SEGMENT << 4), 0, INITIAL_EBDA_SIZE);
47         write16((INITIAL_EBDA_SEGMENT << 4) + 0x0, INITIAL_EBDA_SIZE / 1024);
48 }
49
50 static void setup_rombios(void)
51 {
52         const char date[] = "06/11/99";
53         memcpy((void *)0xffff5, &date, 8);
54
55         const char ident[] = "PCI_ISA";
56         memcpy((void *)0xfffd9, &ident, 7);
57
58         /* system model: IBM-AT */
59         write8(0xffffe, 0xfc);
60 }
61
62 int (*intXX_handler[256])(struct eregs *regs) = { NULL };
63
64 static int intXX_exception_handler(struct eregs *regs)
65 {
66         printk(BIOS_INFO, "Oops, exception %d while executing option rom\n",
67                         regs->vector);
68 #if 0
69         // Odd: The i945GM VGA oprom chokes on a pushl %eax and will
70         // die with an exception #6 if we run the coreboot exception 
71         // handler. Just continue, as it executes fine.
72         x86_exception(regs);    // Call coreboot exception handler
73 #endif
74
75         return 0;               // Never returns?
76 }
77
78 static int intXX_unknown_handler(struct eregs *regs)
79 {
80         printk(BIOS_INFO, "Unsupported software interrupt #0x%x eax 0x%x\n",
81                         regs->vector, regs->eax);
82
83         return -1;
84 }
85
86 /* setup interrupt handlers for mainboard */
87 void mainboard_interrupt_handlers(int intXX, void *intXX_func)
88 {
89         intXX_handler[intXX] = intXX_func;
90 }
91
92 static void setup_interrupt_handlers(void)
93 {
94         int i;
95
96         /* The first 16 intXX functions are not BIOS services,
97          * but the CPU-generated exceptions ("hardware interrupts")
98          */
99         for (i = 0; i < 0x10; i++)
100                 intXX_handler[i] = &intXX_exception_handler;
101
102         /* Mark all other intXX calls as unknown first */
103         for (i = 0x10; i < 0x100; i++)
104         {
105                 /* If the mainboard_interrupt_handler isn't called first.
106                  */
107                 if(!intXX_handler[i])
108                 {
109                         /* Now set the default functions that are actually
110                          * needed to initialize the option roms. This is very
111                          * slick, as it allows us to implement mainboard specific
112                          * interrupt handlers, such as the int15
113                          */
114                         switch (i) {
115                         case 0x10:
116                                 intXX_handler[0x10] = &int10_handler;
117                                 break;
118                         case 0x12:
119                                 intXX_handler[0x12] = &int12_handler;
120                                 break;
121                         case 0x16:
122                                 intXX_handler[0x16] = &int16_handler;
123                                 break;
124                         case 0x1a:
125                                 intXX_handler[0x1a] = &int1a_handler;
126                                 break;
127                         default:
128                                 intXX_handler[i] = &intXX_unknown_handler;
129                                 break;
130                         }
131                 }
132         }
133 }
134
135 static void write_idt_stub(void *target, u8 intnum)
136 {
137         unsigned char *codeptr;
138         codeptr = (unsigned char *) target;
139         memcpy(codeptr, &__idt_handler, (size_t)&__idt_handler_size);
140         codeptr[3] = intnum; /* modify int# in the code stub. */
141 }
142
143 static void setup_realmode_idt(void)
144 {
145         struct realmode_idt *idts = (struct realmode_idt *) 0;
146         int i;
147
148         /* Copy IDT stub code for each interrupt. This might seem wasteful
149          * but it is really simple
150          */
151          for (i = 0; i < 256; i++) {
152                 idts[i].cs = 0;
153                 idts[i].offset = 0x1000 + (i * (u32)&__idt_handler_size);
154                 write_idt_stub((void *)((u32 )idts[i].offset), i);
155         }
156
157         /* Many option ROMs use the hard coded interrupt entry points in the
158          * system bios. So install them at the known locations.
159          */
160
161         /* int42 is the relocated int10 */
162         write_idt_stub((void *)0xff065, 0x42);
163         /* BIOS Int 11 Handler F000:F84D */
164         write_idt_stub((void *)0xff84d, 0x11);
165         /* BIOS Int 12 Handler F000:F841 */
166         write_idt_stub((void *)0xff841, 0x12);
167         /* BIOS Int 13 Handler F000:EC59 */
168         write_idt_stub((void *)0xfec59, 0x13);
169         /* BIOS Int 14 Handler F000:E739 */
170         write_idt_stub((void *)0xfe739, 0x14);
171         /* BIOS Int 15 Handler F000:F859 */
172         write_idt_stub((void *)0xff859, 0x15);
173         /* BIOS Int 16 Handler F000:E82E */
174         write_idt_stub((void *)0xfe82e, 0x16);
175         /* BIOS Int 17 Handler F000:EFD2 */
176         write_idt_stub((void *)0xfefd2, 0x17);
177         /* ROM BIOS Int 1A Handler F000:FE6E */
178         write_idt_stub((void *)0xffe6e, 0x1a);
179 }
180
181 void run_bios(struct device *dev, unsigned long addr)
182 {
183         u32 num_dev = (dev->bus->secondary << 8) | dev->path.pci.devfn;
184
185         /* Set up BIOS Data Area */
186         setup_bda();
187
188         /* Set up some legacy information in the F segment */
189         setup_rombios();
190
191         /* Set up C interrupt handlers */
192         setup_interrupt_handlers();
193
194         /* Set up real-mode IDT */
195         setup_realmode_idt();
196
197         memcpy(REALMODE_BASE, &__realmode_code, (size_t)&__realmode_code_size);
198         printk(BIOS_SPEW, "Real mode stub @%p: %d bytes\n", REALMODE_BASE,
199                         (u32)&__realmode_code_size);
200
201         printk(BIOS_DEBUG, "Calling Option ROM...\n");
202         /* TODO ES:DI Pointer to System BIOS PnP Installation Check Structure */
203         /* Option ROM entry point is at OPROM start + 3 */
204         realmode_call(addr + 0x0003, num_dev, 0xffff, 0x0000, 0xffff, 0x0, 0x0);
205         printk(BIOS_DEBUG, "... Option ROM returned.\n");
206 }
207
208 #if CONFIG_GEODE_VSA
209 #include <cpu/amd/lxdef.h>
210 #include <cpu/amd/vr.h>
211 #include <cbfs.h>
212
213 #define VSA2_BUFFER             0x60000
214 #define VSA2_ENTRY_POINT        0x60020
215
216 // TODO move to a header file.
217 void do_vsmbios(void);
218
219 /* VSA virtual register helper */
220 static u32 VSA_vrRead(u16 classIndex)
221 {
222         u32 eax, ebx, ecx, edx;
223         asm volatile (
224                 "movw   $0x0AC1C, %%dx\n"
225                 "orl    $0x0FC530000, %%eax\n"
226                 "outl   %%eax, %%dx\n"
227                 "addb   $2, %%dl\n"
228                 "inw    %%dx, %%ax\n"
229                 : "=a" (eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
230                 : "a"(classIndex)
231         );
232
233         return eax;
234 }
235
236 void do_vsmbios(void)
237 {
238         printk(BIOS_DEBUG, "Preparing for VSA...\n");
239
240         /* Set up C interrupt handlers */
241         setup_interrupt_handlers();
242
243         /* Setting up realmode IDT */
244         setup_realmode_idt();
245
246         memcpy(REALMODE_BASE, &__realmode_code, (size_t)&__realmode_code_size);
247         printk(BIOS_SPEW, "VSA: Real mode stub @%p: %d bytes\n", REALMODE_BASE,
248                         (u32)&__realmode_code_size);
249
250         if ((unsigned int)cbfs_load_stage("vsa") != VSA2_ENTRY_POINT) {
251                 printk(BIOS_ERR, "Failed to load VSA.\n");
252                 return;
253         }
254
255         unsigned char *buf = (unsigned char *)VSA2_BUFFER;
256         printk(BIOS_DEBUG, "VSA: Buffer @%p *[0k]=%02x\n", buf, buf[0]);
257         printk(BIOS_DEBUG, "VSA: Signature *[0x20-0x23] is %02x:%02x:%02x:%02x\n",
258                      buf[0x20], buf[0x21], buf[0x22], buf[0x23]);
259
260         /* Check for code to emit POST code at start of VSA. */
261         if ((buf[0x20] != 0xb0) || (buf[0x21] != 0x10) ||
262             (buf[0x22] != 0xe6) || (buf[0x23] != 0x80)) {
263                 printk(BIOS_WARNING, "VSA: Signature incorrect. Install failed.\n");
264                 return;
265         }
266
267         printk(BIOS_DEBUG, "Calling VSA module...\n");
268
269         /* ECX gets SMM, EDX gets SYSMEM */
270         realmode_call(VSA2_ENTRY_POINT, 0x0, 0x0, MSR_GLIU0_SMM, 
271                         MSR_GLIU0_SYSMEM, 0x0, 0x0);
272
273         printk(BIOS_DEBUG, "... VSA module returned.\n");
274
275         /* Restart timer 1 */
276         outb(0x56, 0x43);
277         outb(0x12, 0x41);
278
279         /* Check that VSA is running OK */
280         if (VSA_vrRead(SIGNATURE) == VSA2_SIGNATURE)
281                 printk(BIOS_DEBUG, "VSM: VSA2 VR signature verified.\n");
282         else
283                 printk(BIOS_ERR, "VSM: VSA2 VR signature not valid. Install failed.\n");
284 }
285 #endif
286
287 /* interrupt_handler() is called from assembler code only,
288  * so there is no use in putting the prototype into a header file.
289  */
290 int __attribute__((regparm(0))) interrupt_handler(u32 intnumber,
291             u32 gsfs, u32 dses,
292             u32 edi, u32 esi,
293             u32 ebp, u32 esp,
294             u32 ebx, u32 edx,
295             u32 ecx, u32 eax,
296             u32 cs_ip, u16 stackflags);
297
298 int __attribute__((regparm(0))) interrupt_handler(u32 intnumber,
299             u32 gsfs, u32 dses,
300             u32 edi, u32 esi,
301             u32 ebp, u32 esp,
302             u32 ebx, u32 edx,
303             u32 ecx, u32 eax,
304             u32 cs_ip, u16 stackflags)
305 {
306         u32 ip;
307         u32 cs;
308         u32 flags;
309         int ret = -1;
310         struct eregs reg_info;
311
312         ip = cs_ip & 0xffff;
313         cs = cs_ip >> 16;
314         flags = stackflags;
315
316 #if CONFIG_REALMODE_DEBUG
317         printk(BIOS_DEBUG, "oprom: INT# 0x%x\n", intnumber);
318         printk(BIOS_DEBUG, "oprom: eax: %08x ebx: %08x ecx: %08x edx: %08x\n",
319                       eax, ebx, ecx, edx);
320         printk(BIOS_DEBUG, "oprom: ebp: %08x esp: %08x edi: %08x esi: %08x\n",
321                      ebp, esp, edi, esi);
322         printk(BIOS_DEBUG, "oprom:  ip: %04x      cs: %04x   flags: %08x\n",
323                      ip, cs, flags);
324 #endif
325
326         // Fetch arguments from the stack and put them into
327         // a structure that we want to pass on to our sub interrupt
328         // handlers.
329         reg_info = (struct eregs) {
330                 .eax=eax,
331                 .ecx=ecx,
332                 .edx=edx,
333                 .ebx=ebx,
334                 .esp=esp,
335                 .ebp=ebp,
336                 .esi=esi,
337                 .edi=edi,
338                 .vector=intnumber,
339                 .error_code=0, // ??
340                 .eip=ip,
341                 .cs=cs,
342                 .eflags=flags // ??
343         };
344
345         // Call the interrupt handler for this int#
346         ret = intXX_handler[intnumber](&reg_info);
347
348         // Put registers back on the stack. The assembler code
349         // will later pop them.
350         // What happens here is that we force (volatile!) changing
351         // the values of the parameters of this function. We do this
352         // because we know that they stay alive on the stack after
353         // we leave this function. Don't say this is bollocks.
354         *(volatile u32 *)&eax = reg_info.eax;
355         *(volatile u32 *)&ecx = reg_info.ecx;
356         *(volatile u32 *)&edx = reg_info.edx;
357         *(volatile u32 *)&ebx = reg_info.ebx;
358         *(volatile u32 *)&esi = reg_info.esi;
359         *(volatile u32 *)&edi = reg_info.edi;
360         flags = reg_info.eflags;
361
362         /* Pass errors back to our caller via the CARRY flag */
363         if (ret) {
364                 printk(BIOS_DEBUG,"int%02x call returned error.\n", intnumber);
365                 flags |= 1;  // error: set carry
366         }else{
367                 flags &= ~1; // no error: clear carry
368         }
369         *(volatile u16 *)&stackflags = flags;
370
371         return ret;
372 }
373