1 #include <console/console.h>
2 #include <device/pci.h>
3 #include <device/pci_ids.h>
4 #include <device/pci_ops.h>
8 #include <cpu/amd/lxdef.h>
9 #include <cpu/amd/vr.h>
12 #define VSA2_BUFFER 0x60000
13 #define VSA2_ENTRY_POINT 0x60020
15 /* vsmsetup.c derived from vgabios.c. Derived from: */
17 /*------------------------------------------------------------ -*- C -*-
18 * 2 Kernel Monte a.k.a. Linux loading Linux on x86
20 * Erik Arjan Hendriks <hendriks@lanl.gov>
22 * This version is a derivative of the original two kernel monte
23 * which is (C) 2000 Scyld.
25 * Copyright (C) 2000 Scyld Computing Corporation
27 * This program is free software; you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation; either version 2 of the License, or
30 * (at your option) any later version.
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with this program; if not, write to the Free Software
39 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
41 * Portions related to the alpha architecture are:
43 * Copyright(C) 2001 University of California. LA-CC Number 01-67.
44 * This software has been authored by an employee or employees of the
45 * University of California, operator of the Los Alamos National
46 * Laboratory under Contract No. W-7405-ENG-36 with the U.S.
47 * Department of Energy. The U.S. Government has rights to use,
48 * reproduce, and distribute this software. If the software is
49 * modified to produce derivative works, such modified software should
50 * be clearly marked, so as not to confuse it with the version
51 * available from LANL.
53 * This software may be used and distributed according to the terms
54 * of the GNU General Public License, incorporated herein by
55 * reference to http://www.gnu.org/licenses/gpl.html.
57 * This software is provided by the author(s) "as is" and any express
58 * or implied warranties, including, but not limited to, the implied
59 * warranties of merchantability and fitness for a particular purpose
60 * are disclaimed. In no event shall the author(s) be liable for any
61 * direct, indirect, incidental, special, exemplary, or consequential
62 * damages (including, but not limited to, procurement of substitute
63 * goods or services; loss of use, data, or profits; or business
64 * interruption) however caused and on any theory of liability,
65 * whether in contract, strict liability, or tort (including
66 * negligence or otherwise) arising in any way out of the use of this
67 * software, even if advised of the possibility of such damage.
69 * $Id: vsmsetup.c,v 1.8 2006/09/08 12:47:57 andrei Exp $
71 * Copyright (C) 2007 Advanced Micro Devices, Inc.
73 *--------------------------------------------------------------------*/
75 /* Modified to be a self sufficient plug in so that it can be used
76 without reliance on other parts of core coreboot
77 (C) 2005 Nick.Barker9@btinternet.com
79 Used initially for epia-m where there are problems getting the bios
80 emulator to successfully run this bios.
83 /* Declare a temporary global descriptor table - necessary because the
84 Core part of the bios no longer sets up any 16 bit segments */
86 /* pointer to original gdt */
90 /* compute the table limit */
91 "__mygdt_limit = __mygdt_end - __mygdt - 1 \n"
93 " .word __mygdt_limit \n"
96 /* selgdt 0, unused */
97 " .word 0x0000, 0x0000 \n"
98 " .byte 0x00, 0x00, 0x00, 0x00 \n"
99 /* selgdt 8, unused */
100 " .word 0x0000, 0x0000 \n"
101 " .byte 0x00, 0x00, 0x00, 0x00 \n"
102 /* selgdt 0x10, flat code segment */
103 " .word 0xffff, 0x0000 \n"
104 " .byte 0x00, 0x9b, 0xcf, 0x00 \n"
105 /* selgdt 0x18, flat data segment */
106 " .word 0xffff, 0x0000 \n"
107 " .byte 0x00, 0x93, 0xcf, 0x00 \n"
108 /* selgdt 0x20, unused */
109 " .word 0x0000, 0x0000 \n"
110 " .byte 0x00, 0x00, 0x00, 0x00 \n"
111 /* selgdt 0x28 16-bit 64k code at 0x00000000 */
112 " .word 0xffff, 0x0000 \n"
113 " .byte 0, 0x9a, 0, 0 \n"
114 /* selgdt 0x30 16-bit 64k data at 0x00000000 */
115 " .word 0xffff, 0x0000 \n"
116 " .byte 0, 0x92, 0, 0 \n"
120 /* Declare a pointer to where our idt is going to be i.e. at mem zero */
130 /* The address arguments to this function are PHYSICAL ADDRESSES */
131 static void real_mode_switch_call_vsm(unsigned long smm, unsigned long sysm)
133 uint16_t entryHi = (VSA2_ENTRY_POINT & 0xffff0000) >> 4;
134 uint16_t entryLo = (VSA2_ENTRY_POINT & 0xffff);
136 __asm__ __volatile__(
137 // paranoia -- does ecx get saved? not sure. This is
138 // the easiest safe thing to do.
141 " mov %%esp, __stack \n"
143 "__stack: .long 0 \n"
145 /* get devfn into %%ecx */
146 " movl %%esp, %%ebp \n"
148 /* I'm not happy about that pushal followed by esp-relative
149 * references. Just do hard-codes for now
151 " movl 8(%%ebp), %%ecx \n"
152 " movl 12(%%ebp), %%edx \n"
157 " lgdt %%cs:__mygdtaddr \n"
158 /* This configures CS properly for real mode. */
159 " ljmp $0x28, $__rms_16bit\n"
162 /* 16 bit code from here on... */
163 /* Load the segment registers w/ properly configured segment
164 * descriptors. They will retain these configurations (limits,
165 * writability, etc.) once protected mode is turned off. */
166 " mov $0x30, %%ax \n"
172 /* Turn off protection (bit 0 in CR0) */
173 " movl %%cr0, %%eax \n"
174 " andl $0xFFFFFFFE, %%eax \n"
175 " movl %%eax, %%cr0 \n"
176 /* Now really going into real mode */
177 " ljmp $0, $__rms_real\n"
180 /* put the stack at the end of page zero.
181 * that way we can easily share it between real and protected,
182 * since the 16-bit ESP at segment 0 will work for any case.
188 " movl $0x1000, %%eax \n"
189 " movl %%eax, %%esp \n"
190 /* Load our 16 it idt */
194 /* Dump zeros in the other segregs */
196 /* FixMe: Big real mode for gs, fs? */
199 " mov $0x40, %%ax \n"
201 //" mov %%cx, %%ax \n"
204 /* call the VSA2 entry point address */
206 /* if we got here, just about done.
207 * Need to get back to protected mode */
208 " movl %%cr0, %%eax \n"
209 " orl $0x0000001, %%eax\n" /* PE = 1 */
210 " movl %%eax, %%cr0 \n"
211 /* Now that we are in protected mode jump to a 32 bit code segment. */
212 " data32 ljmp $0x10, $vsmrestart\n"
215 " movw $0x18, %%ax \n"
221 /* restore proper gdt and idt */
222 " lgdt %%cs:gdtarg \n"
226 " mov __stack, %%esp \n"
228 "g" (smm), "g"(sysm), "g"(entryHi), "g"(entryLo)
232 __asm__(".text\n" "real_mode_switch_end:\n");
233 extern char real_mode_switch_end[];
235 // andrei: some VSA virtual register helpers: raw read and MSR read
237 uint32_t VSA_vrRead(uint16_t classIndex)
239 unsigned eax, ebx, ecx, edx;
241 "movw $0x0AC1C, %%dx \n"
242 "orl $0x0FC530000, %%eax \n"
243 "outl %%eax, %%dx \n"
246 : "=a" (eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
253 uint32_t VSA_msrRead(uint32_t msrAddr)
255 unsigned eax, ebx, ecx, edx;
257 "movw $0x0AC1C, %%dx \n"
258 "movl $0x0FC530007, %%eax \n"
259 "outl %%eax, %%dx \n"
262 : "=a" (eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
269 void do_vsmbios(void)
272 unsigned long busdevfn;
274 unsigned int size = SMM_SIZE * 1024;
277 printk(BIOS_ERR, "do_vsmbios\n");
278 /* clear vsm bios data area */
279 for (i = 0x400; i < 0x500; i++) {
280 *(volatile unsigned char *)i = 0;
283 /* declare rom address here - keep any config data out of the way
284 * of core LXB stuff */
286 /* this is the base of rom on the LX at present. At some point, this has to be
287 * much better parameterized
290 if ((unsigned int)cbfs_load_stage("vsa") != VSA2_ENTRY_POINT) {
291 printk(BIOS_ERR, "do_vsmbios: Failed to load VSA.\n");
293 buf = (unsigned char *)VSA2_BUFFER;
294 printk(BIOS_DEBUG, "buf %p *buf %d buf[256k] %d\n",
295 buf, buf[0], buf[SMM_SIZE * 1024]);
296 printk(BIOS_DEBUG, "buf[0x20] signature is %x:%x:%x:%x\n",
297 buf[0x20], buf[0x21], buf[0x22], buf[0x23]);
298 /* check for post code at start of vsainit.bin. If you don't see it,
300 if ((buf[0x20] != 0xb0) || (buf[0x21] != 0x10) ||
301 (buf[0x22] != 0xe6) || (buf[0x23] != 0x80)) {
302 printk(BIOS_ERR, "do_vsmbios: no vsainit.bin signature, skipping!\n");
306 /* ecx gets smm, edx gets sysm */
307 printk(BIOS_ERR, "Call real_mode_switch_call_vsm\n");
308 real_mode_switch_call_vsm(MSR_GLIU0_SMM, MSR_GLIU0_SYSMEM);
310 /* restart timer 1 */
314 // check that VSA is running OK
315 if (VSA_vrRead(SIGNATURE) == VSA2_SIGNATURE)
316 printk(BIOS_DEBUG, "do_vsmbios: VSA2 VR signature verified\n");
318 printk(BIOS_ERR, "do_vsmbios: VSA2 VR signature not valid, install failed!\n");
321 // we had hoped to avoid this.
322 // this is a stub IDT only. It's main purpose is to ignore calls
324 // no longer. Dammit. We have to respond to these.
326 unsigned short offset, cs;
329 // from a handy writeup that andrey found.
332 // There are some assumptions we can make here.
333 // First, the Top Of Stack (TOS) is located on the top of page zero.
334 // we can share this stack between real and protected mode.
335 // that simplifies a lot of things ...
336 // we'll just push all the registers on the stack as longwords,
337 // and pop to protected mode.
338 // second, since this only ever runs as part of coreboot,
339 // we know all the segment register values -- so we don't save any.
340 // keep the handler that calls things small. It can do a call to
341 // more complex code in coreboot itself. This helps a lot as we don't
342 // have to do address fixup in this little stub, and calls are absolute
343 // so the handler is relocatable.
346 __asm__ __volatile__(
351 " ljmp $0, $callbiosint16\n"
357 void debughandler(void)
359 __asm__ __volatile__(
368 "end_debughandle: \n"
373 // Calling conventions. The first C function is called with this stuff
374 // on the stack. They look like value parameters, but note that if you
375 // modify them they will go back to the INTx function modified.
376 // the C function will call the biosint function with these as
377 // REFERENCE parameters. In this way, we can easily get
378 // returns back to the INTx caller (i.e. vgabios)
379 void callbiosint(void)
381 __asm__ __volatile__(
388 // clean up the int #. To save space we put it in the lower
389 // byte. But the top 24 bits are junk.
390 " andl $0xff, %eax\n"
391 // this push does two things:
392 // - put the INT # on the stack as a parameter
393 // - provides us with a temp for the %cr0 mods.
398 " orl $0x00000001, %eax\n" /* PE = 1 */
400 /* Now that we are in protected mode jump to a 32 bit code segment. */
401 " data32 ljmp $0x10, $biosprotect\n"
412 // back to real mode ...
413 " ljmp $0x28, $__rms_16bit2\n"
416 /* 16 bit code from here on... */
417 /* Load the segment registers w/ properly configured segment
418 * descriptors. They will retain these configurations (limits,
419 * writability, etc.) once protected mode is turned off. */
426 /* Turn off protection (bit 0 in CR0) */
427 " movl %cr0, %eax \n"
428 " andl $0xFFFFFFFE, %eax \n"
429 " movl %eax, %cr0 \n"
430 /* Now really going into real mode */
431 " ljmp $0, $__rms_real2 \n"
434 * FixME: where is esp? */
435 /* no need for a fix here. The esp is shared from 32-bit and 16-bit mode.
436 * you have to hack on the ss, but the esp remains the same across
441 /* debugging for RGM */
443 " outb %al, $0x80 \n"
444 /* Load our 16 bit idt */
448 /* Dump zeros in the other segregs */
454 /* pop the INT # that you pushed earlier */
471 int pcibios(unsigned long *pedi, unsigned long *pesi, unsigned long *pebp,
472 unsigned long *pesp, unsigned long *pebx, unsigned long *pedx,
473 unsigned long *pecx, unsigned long *peax, unsigned long *pflags);
475 int handleint21(unsigned long *pedi, unsigned long *pesi, unsigned long *pebp,
476 unsigned long *pesp, unsigned long *pebx, unsigned long *pedx,
477 unsigned long *pecx, unsigned long *peax,
478 unsigned long *pflags);
480 int biosint(unsigned long intnumber,
481 unsigned long gsfs, unsigned long dses,
482 unsigned long edi, unsigned long esi,
483 unsigned long ebp, unsigned long esp,
484 unsigned long ebx, unsigned long edx,
485 unsigned long ecx, unsigned long eax,
486 unsigned long cs_ip, unsigned short stackflags)
497 printk(BIOS_DEBUG, "biosint: INT# 0x%lx\n", intnumber);
498 printk(BIOS_DEBUG, "biosint: eax 0x%lx ebx 0x%lx ecx 0x%lx edx 0x%lx\n",
500 printk(BIOS_DEBUG, "biosint: ebp 0x%lx esp 0x%lx edi 0x%lx esi 0x%lx\n",
502 printk(BIOS_DEBUG, "biosint: ip 0x%x cs 0x%x flags 0x%x\n",
503 (u32)ip, (u32)cs, (u32)flags);
504 printk(BIOS_DEBUG, "biosint: gs 0x%x fs 0x%x ds 0x%x es 0x%x\n",
505 (u16)(gsfs >> 16), (u16)(gsfs & 0xffff), (u16)(dses >> 16), (u16)(dses & 0xffff));
507 // cases in a good compiler are just as good as your own tables.
510 // These are not BIOS service, but the CPU-generated exceptions
511 printk(BIOS_INFO, "biosint: Oops, exception 0x%x\n", (u32)intnumber);
513 printk(BIOS_DEBUG, "Stack contents: ");
514 while (esp < 0x1000) {
515 printk(BIOS_DEBUG, "0x%04x ", *(unsigned short *)esp);
518 printk(BIOS_DEBUG, "\n");
520 printk(BIOS_DEBUG, "biosint: Bailing out ... not now\n");
526 ret = pcibios(&edi, &esi, &ebp, &esp,
527 &ebx, &edx, &ecx, &eax, &flags);
535 ret = handleint21(&edi, &esi, &ebp, &esp,
536 &ebx, &edx, &ecx, &eax, &flags);
539 printk(BIOS_INFO, "BIOSINT: Unsupported int #0x%x\n", (u32)intnumber);
543 flags |= 1; // carry flags
550 void setup_realmode_idt(void)
552 extern unsigned char idthandle, end_idthandle;
553 extern unsigned char debughandle, end_debughandle;
556 struct realidt *idts = (struct realidt *)0;
557 int codesize = &end_idthandle - &idthandle;
558 unsigned char *intbyte, *codeptr;
560 // for each int, we create a customized little handler
561 // that just pushes %ax, puts the int # in %al,
562 // then calls the common interrupt handler.
563 // this necessitated because intel didn't know much about
564 // architecture when they did the 8086 (it shows)
565 // (hmm do they know anymore even now :-)
566 // obviously you can see I don't really care about memory
567 // efficiency. If I did I would probe back through the stack
568 // and get it that way. But that's really disgusting.
569 for (i = 0; i < 256; i++) {
571 codeptr = (unsigned char *)4096 + i * codesize;
572 idts[i].offset = (unsigned)codeptr;
573 memcpy(codeptr, &idthandle, codesize);
574 intbyte = codeptr + 3;
578 // fixed entry points
580 // VGA BIOSes tend to hardcode f000:f065 as the previous handler of
582 // calling convention here is the same as INTs, we can reuse
583 // the int entry code.
584 codeptr = (unsigned char *)0xff065;
585 memcpy(codeptr, &idthandle, codesize);
586 intbyte = codeptr + 3;
587 *intbyte = 0x42; /* int42 is the relocated int10 */
589 /* debug handler - useful to set a programmable delay between instructions if the
590 TF bit is set upon call to real mode */
592 idts[1].offset = 16384;
593 memcpy((void *)16384, &debughandle, &end_debughandle - &debughandle);
599 READCONFBYTE = 0xb108,
600 READCONFWORD = 0xb109,
601 READCONFDWORD = 0xb10a,
602 WRITECONFBYTE = 0xb10b,
603 WRITECONFWORD = 0xb10c,
604 WRITECONFDWORD = 0xb10d
607 // errors go in AH. Just set these up so that word assigns
610 PCIBIOS_NODEV = 0x8600,
611 PCIBIOS_BADREG = 0x8700
615 pcibios(unsigned long *pedi, unsigned long *pesi, unsigned long *pebp,
616 unsigned long *pesp, unsigned long *pebx, unsigned long *pedx,
617 unsigned long *pecx, unsigned long *peax, unsigned long *pflags)
619 unsigned short func = (unsigned short)*peax;
621 unsigned short devid, vendorid, devfn;
622 short devindex; /* Use short to get rid of gabage in upper half of 32-bit register */
638 while ((dev = dev_find_device(vendorid, devid, dev))) {
644 unsigned short busdevfn;
646 // busnum is an unsigned char;
647 // devfn is an int, so we mask it off.
648 busdevfn = (dev->bus->secondary << 8)
649 | (dev->path.pci.devfn & 0xff);
650 printk(BIOS_DEBUG, "0x%x: return 0x%x\n", func,
655 *peax = PCIBIOS_NODEV;
672 devfn = *pebx & 0xff;
675 dev = dev_find_slot(bus, devfn);
677 printk(BIOS_DEBUG, "0x%x: BAD DEVICE bus %d devfn 0x%x\n",
679 // idiots. the pcibios guys assumed you'd never pass a bad bus/devfn!
680 *peax = PCIBIOS_BADREG;
685 byte = pci_read_config8(dev, reg);
689 word = pci_read_config16(dev, reg);
693 dword = pci_read_config32(dev, reg);
698 pci_write_config8(dev, reg, byte);
702 pci_write_config16(dev, reg, word);
706 pci_write_config32(dev, reg, dword);
711 retval = PCIBIOS_BADREG;
712 printk(BIOS_DEBUG, "0x%x: bus %d devfn 0x%x reg 0x%x val 0x%lx\n",
713 func, bus, devfn, reg, *pecx);
719 printk(BIOS_ERR, "UNSUPPORTED PCIBIOS FUNCTION 0x%x\n", func);
726 int handleint21(unsigned long *edi, unsigned long *esi, unsigned long *ebp,
727 unsigned long *esp, unsigned long *ebx, unsigned long *edx,
728 unsigned long *ecx, unsigned long *eax, unsigned long *flags)
731 printk(BIOS_DEBUG, "handleint21, eax 0x%x\n", (u32)*eax);
732 switch (*eax & 0xffff) {
737 *ebx = 0x545; // MCLK = 133, 32M frame buffer, 256 M main memory
746 *ecx = (*ecx & 0xffffff00) | 2; // panel type = 2 = 1024 * 768
751 *ebx = (*ebx & 0xffff0000) | 2;
752 *ecx = (*ecx & 0xffff0000) | 0x401; // PAL + crt only
753 *edx = (*edx & 0xffff0000) | 0; // TV Layout - default