Move the prototype for run_vsa.
[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 #define REALMODE_BASE ((void *)0x600)
30
31 struct realmode_idt {
32         u16 offset, cs;
33 };
34
35 void x86_exception(struct eregs *info);
36
37 extern unsigned char __idt_handler, __idt_handler_size;
38 extern unsigned char __realmode_code, __realmode_code_size;
39 extern unsigned char __run_optionrom, __run_interrupt;
40 extern unsigned char __run_vsa;
41
42 void (*run_optionrom)(u32 devfn) __attribute__((regparm(0))) = (void *)&__run_optionrom;
43 void (*vga_enable_console)(void) __attribute__((regparm(0))) = (void *)&__run_interrupt;
44
45 int (*intXX_handler[256])(struct eregs *regs) = { NULL };
46
47 static int intXX_exception_handler(struct eregs *regs)
48 {
49         printk(BIOS_INFO, "Oops, exception %d while executing option rom\n", 
50                         regs->vector);
51         x86_exception(regs);    // Call coreboot exception handler 
52
53         return 0;               // Never returns?
54 }
55
56 static int intXX_unknown_handler(struct eregs *regs)
57 {
58         printk(BIOS_INFO, "Unsupported software interrupt #0x%x\n", 
59                         regs->vector);
60
61         return -1;
62 }
63
64 /* setup interrupt handlers for mainboard */
65 void mainboard_interrupt_handlers(int intXX, void *intXX_func)
66 {
67         intXX_handler[intXX] = intXX_func;
68 }
69
70 int int12_handler(struct eregs *regs);
71 int int15_handler(struct eregs *regs);
72 int int1a_handler(struct eregs *regs);
73
74 static void setup_interrupt_handlers(void)
75 {
76         int i;
77
78         /* The first 16 intXX functions are not BIOS services, 
79          * but the CPU-generated exceptions ("hardware interrupts")
80          */
81         for (i = 0; i < 0x10; i++)
82                 intXX_handler[i] = &intXX_exception_handler;
83         
84         /* Mark all other intXX calls as unknown first */
85         for (i = 0x10; i < 0x100; i++)
86         {
87                 /* If the mainboard_interrupt_handler isn't called first.
88                  */
89                 if(!intXX_handler[i])
90                 {
91                         /* Now set the default functions that are actually
92                          * needed to initialize the option roms. This is very
93                          * slick, as it allows us to implement mainboard specific
94                          * interrupt handlers, such as the int15
95                          */
96                         switch (i) {
97                         case 0x12:
98                                 intXX_handler[0x12] = &int12_handler;
99                                 break;
100                         case 0x15:
101                                 intXX_handler[0x15] = &int15_handler;
102                                 break;
103                         case 0x1a:
104                                 intXX_handler[0x1a] = &int1a_handler;
105                                 break;
106                         default:
107                                 intXX_handler[i] = &intXX_unknown_handler;
108                                 break;
109                         }
110                 }
111         }
112 }
113
114 static void write_idt_stub(void *target, u8 intnum)
115 {
116         unsigned char *codeptr;
117         codeptr = (unsigned char *) target;
118         memcpy(codeptr, &__idt_handler, (size_t)&__idt_handler_size);
119         codeptr[3] = intnum; /* modify int# in the code stub. */
120 }
121
122 static void setup_realmode_idt(void)
123 {
124         struct realmode_idt *idts = (struct realmode_idt *) 0;
125         int i;
126
127         /* Copy IDT stub code for each interrupt. This might seem wasteful
128          * but it is really simple
129          */
130          for (i = 0; i < 256; i++) {
131                 idts[i].cs = 0;
132                 idts[i].offset = 0x1000 + (i * (u32)&__idt_handler_size);
133                 write_idt_stub((void *)((u32 )idts[i].offset), i);
134         }
135
136         /* Many option ROMs use the hard coded interrupt entry points in the
137          * system bios. So install them at the known locations. 
138          * Only need int10 so far.
139          */
140         
141         /* int42 is the relocated int10 */
142         write_idt_stub((void *)0xff065, 0x42); 
143 }
144
145 void run_bios(struct device *dev, unsigned long addr)
146 {
147         /* clear vga bios data area */
148         memset((void *)0x400, 0, 0x200);
149
150         /* Set up C interrupt handlers */
151         setup_interrupt_handlers();
152
153         /* Setting up realmode IDT */
154         setup_realmode_idt();
155
156         memcpy(REALMODE_BASE, &__realmode_code, (size_t)&__realmode_code_size);
157         printk(BIOS_SPEW, "Real mode stub @%p: %d bytes\n", REALMODE_BASE,
158                         (u32)&__realmode_code_size);
159
160         printk(BIOS_DEBUG, "Calling Option ROM...\n");
161         run_optionrom((dev->bus->secondary << 8) | dev->path.pci.devfn);
162         printk(BIOS_DEBUG, "... Option ROM returned.\n");
163 }
164
165 #if defined(CONFIG_GEODE_VSA) && CONFIG_GEODE_VSA
166 #include <cpu/amd/lxdef.h>
167 #include <cpu/amd/vr.h>
168 #include <cbfs.h>
169
170 #define VSA2_BUFFER             0x60000
171 #define VSA2_ENTRY_POINT        0x60020
172
173 void (*run_vsa)(u32 smm, u32 sysmem) __attribute__((regparm(0))) = (void *)&__run_vsa;
174
175 // TODO move to a header file.
176 void do_vsmbios(void);
177
178 /* VSA virtual register helper */
179 static u32 VSA_vrRead(u16 classIndex)
180 {
181         u32 eax, ebx, ecx, edx;
182         asm volatile (
183                 "movw   $0x0AC1C, %%dx\n"
184                 "orl    $0x0FC530000, %%eax\n"
185                 "outl   %%eax, %%dx\n"
186                 "addb   $2, %%dl\n"
187                 "inw    %%dx, %%ax\n"
188                 : "=a" (eax), "=b"(ebx), "=c"(ecx), "=d"(edx) 
189                 : "a"(classIndex)
190         );
191
192         return eax;
193 }
194
195 void do_vsmbios(void)
196 {
197         printk(BIOS_DEBUG, "Preparing for VSA...\n");
198
199         /* clear bios data area */
200         memset((void *)0x400, 0, 0x200);
201
202         /* Set up C interrupt handlers */
203         setup_interrupt_handlers();
204
205         /* Setting up realmode IDT */
206         setup_realmode_idt();
207
208         memcpy(REALMODE_BASE, &__realmode_code, (size_t)&__realmode_code_size);
209         printk(BIOS_SPEW, "VSA: Real mode stub @%p: %d bytes\n", REALMODE_BASE,
210                         (u32)&__realmode_code_size);
211
212         if ((unsigned int)cbfs_load_stage("vsa") != VSA2_ENTRY_POINT) {
213                 printk(BIOS_ERR, "Failed to load VSA.\n");
214                 return;
215         }
216
217         unsigned char *buf = (unsigned char *)VSA2_BUFFER;
218         printk(BIOS_DEBUG, "VSA: Buffer @%p *[0k]=%02x\n", buf, buf[0]);
219         printk(BIOS_DEBUG, "VSA: Signature *[0x20-0x23] is %02x:%02x:%02x:%02x\n",
220                      buf[0x20], buf[0x21], buf[0x22], buf[0x23]);
221
222         /* Check for code to emit POST code at start of VSA. */
223         if ((buf[0x20] != 0xb0) || (buf[0x21] != 0x10) ||
224             (buf[0x22] != 0xe6) || (buf[0x23] != 0x80)) {
225                 printk(BIOS_WARNING, "VSA: Signature incorrect. Install failed.\n");
226                 return;
227         }
228
229         printk(BIOS_DEBUG, "Calling VSA module...\n");
230         /* ECX gets SMM, EDX gets SYSMEM */
231         run_vsa(MSR_GLIU0_SMM, MSR_GLIU0_SYSMEM);
232         printk(BIOS_DEBUG, "... VSA module returned.\n");
233
234         /* Restart timer 1 */
235         outb(0x56, 0x43);
236         outb(0x12, 0x41);
237
238         /* Check that VSA is running OK */
239         if (VSA_vrRead(SIGNATURE) == VSA2_SIGNATURE)
240                 printk(BIOS_DEBUG, "VSM: VSA2 VR signature verified.\n");
241         else
242                 printk(BIOS_ERR, "VSM: VSA2 VR signature not valid. Install failed.\n");
243 }
244 #endif
245
246 /* interrupt_handler() is called from assembler code only, 
247  * so there is no use in putting the prototype into a header file.
248  */
249 int __attribute__((regparm(0))) interrupt_handler(u32 intnumber,
250             u32 gsfs, u32 dses,
251             u32 edi, u32 esi,
252             u32 ebp, u32 esp,
253             u32 ebx, u32 edx,
254             u32 ecx, u32 eax,
255             u32 cs_ip, u16 stackflags);
256
257 int __attribute__((regparm(0))) interrupt_handler(u32 intnumber,
258             u32 gsfs, u32 dses,
259             u32 edi, u32 esi,
260             u32 ebp, u32 esp,
261             u32 ebx, u32 edx,
262             u32 ecx, u32 eax,
263             u32 cs_ip, u16 stackflags)
264 {
265         u32 ip;
266         u32 cs;
267         u32 flags;
268         int ret = -1;
269         struct eregs reg_info;
270
271         ip = cs_ip & 0xffff;
272         cs = cs_ip >> 16;
273         flags = stackflags;
274
275         printk(BIOS_DEBUG, "oprom: INT# 0x%x\n", intnumber);
276         printk(BIOS_DEBUG, "oprom: eax: %08x ebx: %08x ecx: %08x edx: %08x\n",
277                       eax, ebx, ecx, edx);
278         printk(BIOS_DEBUG, "oprom: ebp: %08x esp: %08x edi: %08x esi: %08x\n",
279                      ebp, esp, edi, esi);
280         printk(BIOS_DEBUG, "oprom:  ip: %04x      cs: %04x   flags: %08x\n",
281                      ip, cs, flags);
282
283         // Fetch arguments from the stack and put them into
284         // a structure that we want to pass on to our sub interrupt
285         // handlers.
286         reg_info = (struct eregs) {
287                 .eax=eax,
288                 .ecx=ecx,
289                 .edx=edx,
290                 .ebx=ebx,
291                 .esp=esp,
292                 .ebp=ebp,
293                 .esi=esi,
294                 .edi=edi,
295                 .vector=intnumber,
296                 .error_code=0, // ??
297                 .eip=ip,
298                 .cs=cs,
299                 .eflags=flags // ??
300         };
301
302         // Call the interrupt handler for this int#
303         ret = intXX_handler[intnumber](&reg_info);
304
305         // Put registers back on the stack. The assembler code
306         // will later pop them.
307         // What happens here is that we force (volatile!) changing
308         // the values of the parameters of this function. We do this
309         // because we know that they stay alive on the stack after 
310         // we leave this function. Don't say this is bollocks.
311         *(volatile u32 *)&eax = reg_info.eax;
312         *(volatile u32 *)&ecx = reg_info.ecx;
313         *(volatile u32 *)&edx = reg_info.edx;
314         *(volatile u32 *)&ebx = reg_info.ebx;
315         *(volatile u32 *)&esi = reg_info.esi;
316         *(volatile u32 *)&edi = reg_info.edi;
317         flags = reg_info.eflags;
318
319         /* Pass errors back to our caller via the CARRY flag */
320         if (ret) {
321                 printk(BIOS_DEBUG,"error!\n");
322                 flags |= 1;  // error: set carry
323         }else{
324                 flags &= ~1; // no error: clear carry
325         }
326         *(volatile u16 *)&stackflags = flags;
327
328         return ret;
329 }
330