refactor vesa mode setting code and bootsplash code
[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 "x86.h"
31 #include "vbe.h"
32 #include "../../src/lib/jpeg.h"
33
34 void (*realmode_call)(u32 addr, u32 eax, u32 ebx, u32 ecx, u32 edx,
35                 u32 esi, u32 edi) __attribute__((regparm(0))) =
36                                                 (void *)&__realmode_call;
37
38 void (*realmode_interrupt)(u32 intno, u32 eax, u32 ebx, u32 ecx, u32 edx, 
39                 u32 esi, u32 edi) __attribute__((regparm(0))) =
40                                                 (void *)&__realmode_interrupt;
41
42 static void setup_bda(void)
43 {
44         /* clear BIOS DATA AREA */
45         memset((void *)0x400, 0, 0x200);
46
47         write16(0x413, FAKE_MEMORY_SIZE / 1024);
48         write16(0x40e, INITIAL_EBDA_SEGMENT);
49
50         /* Set up EBDA */
51         memset((void *)(INITIAL_EBDA_SEGMENT << 4), 0, INITIAL_EBDA_SIZE);
52         write16((INITIAL_EBDA_SEGMENT << 4) + 0x0, INITIAL_EBDA_SIZE / 1024);
53 }
54
55 static void setup_rombios(void)
56 {
57         const char date[] = "06/11/99";
58         memcpy((void *)0xffff5, &date, 8);
59
60         const char ident[] = "PCI_ISA";
61         memcpy((void *)0xfffd9, &ident, 7);
62
63         /* system model: IBM-AT */
64         write8(0xffffe, 0xfc);
65 }
66
67 int (*intXX_handler[256])(struct eregs *regs) = { NULL };
68
69 static int intXX_exception_handler(struct eregs *regs)
70 {
71         printk(BIOS_INFO, "Oops, exception %d while executing option rom\n",
72                         regs->vector);
73 #if 0
74         // Odd: The i945GM VGA oprom chokes on a pushl %eax and will
75         // die with an exception #6 if we run the coreboot exception 
76         // handler. Just continue, as it executes fine.
77         x86_exception(regs);    // Call coreboot exception handler
78 #endif
79
80         return 0;               // Never returns?
81 }
82
83 static int intXX_unknown_handler(struct eregs *regs)
84 {
85         printk(BIOS_INFO, "Unsupported software interrupt #0x%x eax 0x%x\n",
86                         regs->vector, regs->eax);
87
88         return -1;
89 }
90
91 /* setup interrupt handlers for mainboard */
92 void mainboard_interrupt_handlers(int intXX, void *intXX_func)
93 {
94         intXX_handler[intXX] = intXX_func;
95 }
96
97 static void setup_interrupt_handlers(void)
98 {
99         int i;
100
101         /* The first 16 intXX functions are not BIOS services,
102          * but the CPU-generated exceptions ("hardware interrupts")
103          */
104         for (i = 0; i < 0x10; i++)
105                 intXX_handler[i] = &intXX_exception_handler;
106
107         /* Mark all other intXX calls as unknown first */
108         for (i = 0x10; i < 0x100; i++)
109         {
110                 /* If the mainboard_interrupt_handler isn't called first.
111                  */
112                 if(!intXX_handler[i])
113                 {
114                         /* Now set the default functions that are actually
115                          * needed to initialize the option roms. This is
116                          * very slick, as it allows us to implement mainboard
117                          * specific interrupt handlers, such as the int15.
118                          */
119                         switch (i) {
120                         case 0x10:
121                                 intXX_handler[0x10] = &int10_handler;
122                                 break;
123                         case 0x12:
124                                 intXX_handler[0x12] = &int12_handler;
125                                 break;
126                         case 0x16:
127                                 intXX_handler[0x16] = &int16_handler;
128                                 break;
129                         case 0x1a:
130                                 intXX_handler[0x1a] = &int1a_handler;
131                                 break;
132                         default:
133                                 intXX_handler[i] = &intXX_unknown_handler;
134                                 break;
135                         }
136                 }
137         }
138 }
139
140 static void write_idt_stub(void *target, u8 intnum)
141 {
142         unsigned char *codeptr;
143         codeptr = (unsigned char *) target;
144         memcpy(codeptr, &__idt_handler, (size_t)&__idt_handler_size);
145         codeptr[3] = intnum; /* modify int# in the code stub. */
146 }
147
148 static void setup_realmode_idt(void)
149 {
150         struct realmode_idt *idts = (struct realmode_idt *) 0;
151         int i;
152
153         /* Copy IDT stub code for each interrupt. This might seem wasteful
154          * but it is really simple
155          */
156          for (i = 0; i < 256; i++) {
157                 idts[i].cs = 0;
158                 idts[i].offset = 0x1000 + (i * (u32)&__idt_handler_size);
159                 write_idt_stub((void *)((u32 )idts[i].offset), i);
160         }
161
162         /* Many option ROMs use the hard coded interrupt entry points in the
163          * system bios. So install them at the known locations.
164          */
165
166         /* int42 is the relocated int10 */
167         write_idt_stub((void *)0xff065, 0x42);
168         /* BIOS Int 11 Handler F000:F84D */
169         write_idt_stub((void *)0xff84d, 0x11);
170         /* BIOS Int 12 Handler F000:F841 */
171         write_idt_stub((void *)0xff841, 0x12);
172         /* BIOS Int 13 Handler F000:EC59 */
173         write_idt_stub((void *)0xfec59, 0x13);
174         /* BIOS Int 14 Handler F000:E739 */
175         write_idt_stub((void *)0xfe739, 0x14);
176         /* BIOS Int 15 Handler F000:F859 */
177         write_idt_stub((void *)0xff859, 0x15);
178         /* BIOS Int 16 Handler F000:E82E */
179         write_idt_stub((void *)0xfe82e, 0x16);
180         /* BIOS Int 17 Handler F000:EFD2 */
181         write_idt_stub((void *)0xfefd2, 0x17);
182         /* ROM BIOS Int 1A Handler F000:FE6E */
183         write_idt_stub((void *)0xffe6e, 0x1a);
184 }
185
186 #if CONFIG_FRAMEBUFFER_SET_VESA_MODE
187 static u8 vbe_get_mode_info(vbe_mode_info_t * mode_info)
188 {
189         char *buffer = (char *)&__buffer;
190         u16 buffer_seg = (((unsigned long)buffer) >> 4) & 0xff00;
191         u16 buffer_adr = ((unsigned long)buffer) & 0xffff;
192         realmode_interrupt(0x10, VESA_GET_MODE_INFO, 0x0000,
193                         mode_info->video_mode, 0x0000, buffer_seg, buffer_adr);
194         memcpy(mode_info, buffer, sizeof(vbe_mode_info_t));
195         return 0;
196 }
197
198 static u8 vbe_set_mode(vbe_mode_info_t * mode_info)
199 {
200         // request linear framebuffer mode
201         mode_info->video_mode |= (1 << 14);
202         // request clearing of framebuffer
203         mode_info->video_mode &= ~(1 << 15);
204         realmode_interrupt(0x10, VESA_SET_MODE, mode_info->video_mode,
205                         0x0000, 0x0000, 0x0000, 0x0000);
206         return 0;
207 }
208
209 vbe_mode_info_t mode_info;
210
211 /* These two functions could probably even be generic between
212  * yabel and x86 native. TBD later.
213  */
214 void vbe_set_graphics(void)
215 {
216         mode_info.video_mode = (1 << 14) | CONFIG_FRAMEBUFFER_VESA_MODE;
217         vbe_get_mode_info(&mode_info);
218         unsigned char *framebuffer =
219                 (unsigned char *) le32_to_cpu(mode_info.vesa.phys_base_ptr);
220         printk(BIOS_DEBUG, "framebuffer: %p\n", framebuffer);
221         printk(BIOS_DEBUG, "framebuffer: %x\n", mode_info.vesa.phys_base_ptr);
222         vbe_set_mode(&mode_info);
223 #if CONFIG_BOOTSPLASH
224         struct jpeg_decdata *decdata;
225         decdata = malloc(sizeof(*decdata));
226         unsigned char *jpeg = cbfs_find_file("bootsplash.jpg",
227                                                 CBFS_TYPE_BOOTSPLASH);
228         if (!jpeg) {
229                 return;
230         }
231         int ret = 0;
232         ret = jpeg_decode(jpeg, framebuffer, 1024, 768, 16, decdata);
233 #endif
234 }
235
236 void vbe_textmode_console(void)
237 {
238         delay(2);
239         realmode_interrupt(0x10, 0x0003, 0x0000, 0x0000,
240                                 0x0000, 0x0000, 0x0000);
241 }
242
243 void fill_lb_framebuffer(struct lb_framebuffer *framebuffer)
244 {
245         framebuffer->physical_address =
246                                 le32_to_cpu(mode_info.vesa.phys_base_ptr);
247
248         framebuffer->x_resolution = le16_to_cpu(mode_info.vesa.x_resolution);
249         framebuffer->y_resolution = le16_to_cpu(mode_info.vesa.y_resolution);
250         framebuffer->bytes_per_line =
251                                 le16_to_cpu(mode_info.vesa.bytes_per_scanline);
252         framebuffer->bits_per_pixel = mode_info.vesa.bits_per_pixel;
253
254         framebuffer->red_mask_pos = mode_info.vesa.red_mask_pos;
255         framebuffer->red_mask_size = mode_info.vesa.red_mask_size;
256
257         framebuffer->green_mask_pos = mode_info.vesa.green_mask_pos;
258         framebuffer->green_mask_size = mode_info.vesa.green_mask_size;
259
260         framebuffer->blue_mask_pos = mode_info.vesa.blue_mask_pos;
261         framebuffer->blue_mask_size = mode_info.vesa.blue_mask_size;
262
263         framebuffer->reserved_mask_pos = mode_info.vesa.reserved_mask_pos;
264         framebuffer->reserved_mask_size = mode_info.vesa.reserved_mask_size;
265 }
266 #endif
267
268 void run_bios(struct device *dev, unsigned long addr)
269 {
270         u32 num_dev = (dev->bus->secondary << 8) | dev->path.pci.devfn;
271
272         /* Set up BIOS Data Area */
273         setup_bda();
274
275         /* Set up some legacy information in the F segment */
276         setup_rombios();
277
278         /* Set up C interrupt handlers */
279         setup_interrupt_handlers();
280
281         /* Set up real-mode IDT */
282         setup_realmode_idt();
283
284         memcpy(REALMODE_BASE, &__realmode_code, (size_t)&__realmode_code_size);
285         printk(BIOS_SPEW, "Real mode stub @%p: %d bytes\n", REALMODE_BASE,
286                         (u32)&__realmode_code_size);
287
288         printk(BIOS_DEBUG, "Calling Option ROM...\n");
289         /* TODO ES:DI Pointer to System BIOS PnP Installation Check Structure */
290         /* Option ROM entry point is at OPROM start + 3 */
291         realmode_call(addr + 0x0003, num_dev, 0xffff, 0x0000, 0xffff, 0x0, 0x0);
292         printk(BIOS_DEBUG, "... Option ROM returned.\n");
293
294 #if CONFIG_FRAMEBUFFER_SET_VESA_MODE
295         vbe_set_graphics();
296 #endif
297 }
298
299 #if 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.
308 void do_vsmbios(void);
309
310 /* VSA virtual register helper */
311 static 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"
320                 : "=a" (eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
321                 : "a"(classIndex)
322         );
323
324         return eax;
325 }
326
327 void do_vsmbios(void)
328 {
329         printk(BIOS_DEBUG, "Preparing for VSA...\n");
330
331         /* 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");
359
360         /* ECX gets SMM, EDX gets SYSMEM */
361         realmode_call(VSA2_ENTRY_POINT, 0x0, 0x0, MSR_GLIU0_SMM, 
362                         MSR_GLIU0_SYSMEM, 0x0, 0x0);
363
364         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
378 /* interrupt_handler() is called from assembler code only,
379  * so there is no use in putting the prototype into a header file.
380  */
381 int __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,
387             u32 cs_ip, u16 stackflags);
388
389 int __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,
395             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
407 #if CONFIG_REALMODE_DEBUG
408         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);
415 #endif
416
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
443         // because we know that they stay alive on the stack after
444         // 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) {
455                 printk(BIOS_DEBUG,"int%02x call returned error.\n", intnumber);
456                 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