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