Prepare the BIOS data areas before device init.
[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 #include <cbfs.h>
29 #include <delay.h>
30 #include <pc80/i8259.h>
31 #include "x86.h"
32 #include "vbe.h"
33 #include "../../src/lib/jpeg.h"
34
35 void (*realmode_call)(u32 addr, u32 eax, u32 ebx, u32 ecx, u32 edx,
36                 u32 esi, u32 edi) __attribute__((regparm(0))) =
37                                                 (void *)&__realmode_call;
38
39 void (*realmode_interrupt)(u32 intno, u32 eax, u32 ebx, u32 ecx, u32 edx,
40                 u32 esi, u32 edi) __attribute__((regparm(0))) =
41                                                 (void *)&__realmode_interrupt;
42
43 static void setup_rombios(void)
44 {
45         const char date[] = "06/11/99";
46         memcpy((void *)0xffff5, &date, 8);
47
48         const char ident[] = "PCI_ISA";
49         memcpy((void *)0xfffd9, &ident, 7);
50
51         /* system model: IBM-AT */
52         write8(0xffffe, 0xfc);
53 }
54
55 int (*intXX_handler[256])(struct eregs *regs) = { NULL };
56
57 static int intXX_exception_handler(struct eregs *regs)
58 {
59         printk(BIOS_INFO, "Oops, exception %d while executing option rom\n",
60                         regs->vector);
61         x86_exception(regs);    // Call coreboot exception handler
62
63         return 0;               // Never really returns
64 }
65
66 static int intXX_unknown_handler(struct eregs *regs)
67 {
68         printk(BIOS_INFO, "Unsupported software interrupt #0x%x eax 0x%x\n",
69                         regs->vector, regs->eax);
70
71         return -1;
72 }
73
74 /* setup interrupt handlers for mainboard */
75 void mainboard_interrupt_handlers(int intXX, void *intXX_func)
76 {
77         intXX_handler[intXX] = intXX_func;
78 }
79
80 static void setup_interrupt_handlers(void)
81 {
82         int i;
83
84         /* The first 16 intXX functions are not BIOS services,
85          * but the CPU-generated exceptions ("hardware interrupts")
86          */
87         for (i = 0; i < 0x10; i++)
88                 intXX_handler[i] = &intXX_exception_handler;
89
90         /* Mark all other intXX calls as unknown first */
91         for (i = 0x10; i < 0x100; i++)
92         {
93                 /* If the mainboard_interrupt_handler isn't called first.
94                  */
95                 if(!intXX_handler[i])
96                 {
97                         /* Now set the default functions that are actually
98                          * needed to initialize the option roms. This is
99                          * very slick, as it allows us to implement mainboard
100                          * specific interrupt handlers, such as the int15.
101                          */
102                         switch (i) {
103                         case 0x10:
104                                 intXX_handler[0x10] = &int10_handler;
105                                 break;
106                         case 0x12:
107                                 intXX_handler[0x12] = &int12_handler;
108                                 break;
109                         case 0x16:
110                                 intXX_handler[0x16] = &int16_handler;
111                                 break;
112                         case 0x1a:
113                                 intXX_handler[0x1a] = &int1a_handler;
114                                 break;
115                         default:
116                                 intXX_handler[i] = &intXX_unknown_handler;
117                                 break;
118                         }
119                 }
120         }
121 }
122
123 static void write_idt_stub(void *target, u8 intnum)
124 {
125         unsigned char *codeptr;
126         codeptr = (unsigned char *) target;
127         memcpy(codeptr, &__idt_handler, (size_t)&__idt_handler_size);
128         codeptr[3] = intnum; /* modify int# in the code stub. */
129 }
130
131 static void setup_realmode_idt(void)
132 {
133         struct realmode_idt *idts = (struct realmode_idt *) 0;
134         int i;
135
136         /* Copy IDT stub code for each interrupt. This might seem wasteful
137          * but it is really simple
138          */
139          for (i = 0; i < 256; i++) {
140                 idts[i].cs = 0;
141                 idts[i].offset = 0x1000 + (i * (u32)&__idt_handler_size);
142                 write_idt_stub((void *)((u32 )idts[i].offset), i);
143         }
144
145         /* Many option ROMs use the hard coded interrupt entry points in the
146          * system bios. So install them at the known locations.
147          */
148
149         /* int42 is the relocated int10 */
150         write_idt_stub((void *)0xff065, 0x42);
151         /* BIOS Int 11 Handler F000:F84D */
152         write_idt_stub((void *)0xff84d, 0x11);
153         /* BIOS Int 12 Handler F000:F841 */
154         write_idt_stub((void *)0xff841, 0x12);
155         /* BIOS Int 13 Handler F000:EC59 */
156         write_idt_stub((void *)0xfec59, 0x13);
157         /* BIOS Int 14 Handler F000:E739 */
158         write_idt_stub((void *)0xfe739, 0x14);
159         /* BIOS Int 15 Handler F000:F859 */
160         write_idt_stub((void *)0xff859, 0x15);
161         /* BIOS Int 16 Handler F000:E82E */
162         write_idt_stub((void *)0xfe82e, 0x16);
163         /* BIOS Int 17 Handler F000:EFD2 */
164         write_idt_stub((void *)0xfefd2, 0x17);
165         /* ROM BIOS Int 1A Handler F000:FE6E */
166         write_idt_stub((void *)0xffe6e, 0x1a);
167 }
168
169 #if CONFIG_FRAMEBUFFER_SET_VESA_MODE
170 static u8 vbe_get_mode_info(vbe_mode_info_t * mode_info)
171 {
172         printk(BIOS_DEBUG, "Getting information about VESA mode %04x\n",
173                 mode_info->video_mode);
174         char *buffer = (char *)&__buffer;
175         u16 buffer_seg = (((unsigned long)buffer) >> 4) & 0xff00;
176         u16 buffer_adr = ((unsigned long)buffer) & 0xffff;
177         realmode_interrupt(0x10, VESA_GET_MODE_INFO, 0x0000,
178                         mode_info->video_mode, 0x0000, buffer_seg, buffer_adr);
179         memcpy(mode_info->mode_info_block, buffer, sizeof(vbe_mode_info_t));
180         return 0;
181 }
182
183 static u8 vbe_set_mode(vbe_mode_info_t * mode_info)
184 {
185         printk(BIOS_DEBUG, "Setting VESA mode %04x\n", mode_info->video_mode);
186         // request linear framebuffer mode
187         mode_info->video_mode |= (1 << 14);
188         // request clearing of framebuffer
189         mode_info->video_mode &= ~(1 << 15);
190         realmode_interrupt(0x10, VESA_SET_MODE, mode_info->video_mode,
191                         0x0000, 0x0000, 0x0000, 0x0000);
192         return 0;
193 }
194
195 vbe_mode_info_t mode_info;
196
197 /* These two functions could probably even be generic between
198  * yabel and x86 native. TBD later.
199  */
200 void vbe_set_graphics(void)
201 {
202         mode_info.video_mode = (1 << 14) | CONFIG_FRAMEBUFFER_VESA_MODE;
203         vbe_get_mode_info(&mode_info);
204         unsigned char *framebuffer =
205                 (unsigned char *)mode_info.vesa.phys_base_ptr;
206         printk(BIOS_DEBUG, "framebuffer: %p\n", framebuffer);
207         vbe_set_mode(&mode_info);
208 #if CONFIG_BOOTSPLASH
209         struct jpeg_decdata *decdata;
210         decdata = malloc(sizeof(*decdata));
211         unsigned char *jpeg = cbfs_find_file("bootsplash.jpg",
212                                                 CBFS_TYPE_BOOTSPLASH);
213         if (!jpeg) {
214                 return;
215         }
216         int ret = 0;
217         ret = jpeg_decode(jpeg, framebuffer, 1024, 768, 16, decdata);
218 #endif
219 }
220
221 void vbe_textmode_console(void)
222 {
223         delay(2);
224         realmode_interrupt(0x10, 0x0003, 0x0000, 0x0000,
225                                 0x0000, 0x0000, 0x0000);
226 }
227
228 void fill_lb_framebuffer(struct lb_framebuffer *framebuffer)
229 {
230         framebuffer->physical_address = mode_info.vesa.phys_base_ptr;
231
232         framebuffer->x_resolution = le16_to_cpu(mode_info.vesa.x_resolution);
233         framebuffer->y_resolution = le16_to_cpu(mode_info.vesa.y_resolution);
234         framebuffer->bytes_per_line =
235                                 le16_to_cpu(mode_info.vesa.bytes_per_scanline);
236         framebuffer->bits_per_pixel = mode_info.vesa.bits_per_pixel;
237
238         framebuffer->red_mask_pos = mode_info.vesa.red_mask_pos;
239         framebuffer->red_mask_size = mode_info.vesa.red_mask_size;
240
241         framebuffer->green_mask_pos = mode_info.vesa.green_mask_pos;
242         framebuffer->green_mask_size = mode_info.vesa.green_mask_size;
243
244         framebuffer->blue_mask_pos = mode_info.vesa.blue_mask_pos;
245         framebuffer->blue_mask_size = mode_info.vesa.blue_mask_size;
246
247         framebuffer->reserved_mask_pos = mode_info.vesa.reserved_mask_pos;
248         framebuffer->reserved_mask_size = mode_info.vesa.reserved_mask_size;
249 }
250 #endif
251
252 void run_bios(struct device *dev, unsigned long addr)
253 {
254         u32 num_dev = (dev->bus->secondary << 8) | dev->path.pci.devfn;
255
256         /* Setting up required hardware.
257          * Removing this will cause random illegal instruction exceptions
258          * in some option roms.
259          */
260         setup_i8259();
261
262         /* Set up some legacy information in the F segment */
263         setup_rombios();
264
265         /* Set up C interrupt handlers */
266         setup_interrupt_handlers();
267
268         /* Set up real-mode IDT */
269         setup_realmode_idt();
270
271         memcpy(REALMODE_BASE, &__realmode_code, (size_t)&__realmode_code_size);
272         printk(BIOS_SPEW, "Real mode stub @%p: %d bytes\n", REALMODE_BASE,
273                         (u32)&__realmode_code_size);
274
275         printk(BIOS_DEBUG, "Calling Option ROM...\n");
276         /* TODO ES:DI Pointer to System BIOS PnP Installation Check Structure */
277         /* Option ROM entry point is at OPROM start + 3 */
278         realmode_call(addr + 0x0003, num_dev, 0xffff, 0x0000, 0xffff, 0x0, 0x0);
279         printk(BIOS_DEBUG, "... Option ROM returned.\n");
280
281 #if CONFIG_FRAMEBUFFER_SET_VESA_MODE
282         vbe_set_graphics();
283 #endif
284 }
285
286 #if CONFIG_GEODE_VSA
287 #include <cpu/amd/lxdef.h>
288 #include <cpu/amd/vr.h>
289 #include <cbfs.h>
290
291 #define VSA2_BUFFER             0x60000
292 #define VSA2_ENTRY_POINT        0x60020
293
294 // TODO move to a header file.
295 void do_vsmbios(void);
296
297 /* VSA virtual register helper */
298 static u32 VSA_vrRead(u16 classIndex)
299 {
300         u32 eax, ebx, ecx, edx;
301         asm volatile (
302                 "movw   $0x0AC1C, %%dx\n"
303                 "orl    $0x0FC530000, %%eax\n"
304                 "outl   %%eax, %%dx\n"
305                 "addb   $2, %%dl\n"
306                 "inw    %%dx, %%ax\n"
307                 : "=a" (eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
308                 : "a"(classIndex)
309         );
310
311         return eax;
312 }
313
314 void do_vsmbios(void)
315 {
316         printk(BIOS_DEBUG, "Preparing for VSA...\n");
317
318         /* Set up C interrupt handlers */
319         setup_interrupt_handlers();
320
321         /* Setting up realmode IDT */
322         setup_realmode_idt();
323
324         memcpy(REALMODE_BASE, &__realmode_code, (size_t)&__realmode_code_size);
325         printk(BIOS_SPEW, "VSA: Real mode stub @%p: %d bytes\n", REALMODE_BASE,
326                         (u32)&__realmode_code_size);
327
328         if ((unsigned int)cbfs_load_stage("vsa") != VSA2_ENTRY_POINT) {
329                 printk(BIOS_ERR, "Failed to load VSA.\n");
330                 return;
331         }
332
333         unsigned char *buf = (unsigned char *)VSA2_BUFFER;
334         printk(BIOS_DEBUG, "VSA: Buffer @%p *[0k]=%02x\n", buf, buf[0]);
335         printk(BIOS_DEBUG, "VSA: Signature *[0x20-0x23] is %02x:%02x:%02x:%02x\n",
336                      buf[0x20], buf[0x21], buf[0x22], buf[0x23]);
337
338         /* Check for code to emit POST code at start of VSA. */
339         if ((buf[0x20] != 0xb0) || (buf[0x21] != 0x10) ||
340             (buf[0x22] != 0xe6) || (buf[0x23] != 0x80)) {
341                 printk(BIOS_WARNING, "VSA: Signature incorrect. Install failed.\n");
342                 return;
343         }
344
345         printk(BIOS_DEBUG, "Calling VSA module...\n");
346
347         /* ECX gets SMM, EDX gets SYSMEM */
348         realmode_call(VSA2_ENTRY_POINT, 0x0, 0x0, MSR_GLIU0_SMM,
349                         MSR_GLIU0_SYSMEM, 0x0, 0x0);
350
351         printk(BIOS_DEBUG, "... VSA module returned.\n");
352
353         /* Restart timer 1 */
354         outb(0x56, 0x43);
355         outb(0x12, 0x41);
356
357         /* Check that VSA is running OK */
358         if (VSA_vrRead(SIGNATURE) == VSA2_SIGNATURE)
359                 printk(BIOS_DEBUG, "VSM: VSA2 VR signature verified.\n");
360         else
361                 printk(BIOS_ERR, "VSM: VSA2 VR signature not valid. Install failed.\n");
362 }
363 #endif
364
365 /* interrupt_handler() is called from assembler code only,
366  * so there is no use in putting the prototype into a header file.
367  */
368 int __attribute__((regparm(0))) interrupt_handler(u32 intnumber,
369             u32 gsfs, u32 dses,
370             u32 edi, u32 esi,
371             u32 ebp, u32 esp,
372             u32 ebx, u32 edx,
373             u32 ecx, u32 eax,
374             u32 cs_ip, u16 stackflags);
375
376 int __attribute__((regparm(0))) interrupt_handler(u32 intnumber,
377             u32 gsfs, u32 dses,
378             u32 edi, u32 esi,
379             u32 ebp, u32 esp,
380             u32 ebx, u32 edx,
381             u32 ecx, u32 eax,
382             u32 cs_ip, u16 stackflags)
383 {
384         u32 ip;
385         u32 cs;
386         u32 flags;
387         int ret = -1;
388         struct eregs reg_info;
389
390         ip = cs_ip & 0xffff;
391         cs = cs_ip >> 16;
392         flags = stackflags;
393
394 #if CONFIG_REALMODE_DEBUG
395         printk(BIOS_DEBUG, "oprom: INT# 0x%x\n", intnumber);
396         printk(BIOS_DEBUG, "oprom: eax: %08x ebx: %08x ecx: %08x edx: %08x\n",
397                       eax, ebx, ecx, edx);
398         printk(BIOS_DEBUG, "oprom: ebp: %08x esp: %08x edi: %08x esi: %08x\n",
399                      ebp, esp, edi, esi);
400         printk(BIOS_DEBUG, "oprom:  ip: %04x      cs: %04x   flags: %08x\n",
401                      ip, cs, flags);
402 #endif
403
404         // Fetch arguments from the stack and put them into
405         // a structure that we want to pass on to our sub interrupt
406         // handlers.
407         reg_info = (struct eregs) {
408                 .eax=eax,
409                 .ecx=ecx,
410                 .edx=edx,
411                 .ebx=ebx,
412                 .esp=esp,
413                 .ebp=ebp,
414                 .esi=esi,
415                 .edi=edi,
416                 .vector=intnumber,
417                 .error_code=0, // ??
418                 .eip=ip,
419                 .cs=cs,
420                 .eflags=flags // ??
421         };
422
423         // Call the interrupt handler for this int#
424         ret = intXX_handler[intnumber](&reg_info);
425
426         // Put registers back on the stack. The assembler code
427         // will later pop them.
428         // What happens here is that we force (volatile!) changing
429         // the values of the parameters of this function. We do this
430         // because we know that they stay alive on the stack after
431         // we leave this function. Don't say this is bollocks.
432         *(volatile u32 *)&eax = reg_info.eax;
433         *(volatile u32 *)&ecx = reg_info.ecx;
434         *(volatile u32 *)&edx = reg_info.edx;
435         *(volatile u32 *)&ebx = reg_info.ebx;
436         *(volatile u32 *)&esi = reg_info.esi;
437         *(volatile u32 *)&edi = reg_info.edi;
438         flags = reg_info.eflags;
439
440         /* Pass errors back to our caller via the CARRY flag */
441         if (ret) {
442                 printk(BIOS_DEBUG,"int%02x call returned error.\n", intnumber);
443                 flags |= 1;  // error: set carry
444         }else{
445                 flags &= ~1; // no error: clear carry
446         }
447         *(volatile u16 *)&stackflags = flags;
448
449         return ret;
450 }
451