printk_foo -> printk(BIOS_FOO, ...)
[coreboot.git] / src / cpu / amd / model_lx / vsmsetup.c
1 #include <console/console.h>
2 #include <device/pci.h>
3 #include <device/pci_ids.h>
4 #include <device/pci_ops.h>
5 #undef __KERNEL__
6 #include <arch/io.h>
7 #include <string.h>
8 #include <cpu/amd/lxdef.h>
9 #include <cpu/amd/vr.h>
10 #include <cbfs.h>
11
12 #define VSA2_BUFFER                     0x60000
13 #define VSA2_ENTRY_POINT        0x60020
14
15 /* vsmsetup.c derived from vgabios.c. Derived from: */
16
17 /*------------------------------------------------------------ -*- C -*-
18  *  2 Kernel Monte a.k.a. Linux loading Linux on x86
19  *
20  *  Erik Arjan Hendriks <hendriks@lanl.gov>
21  *
22  *  This version is a derivative of the original two kernel monte
23  *  which is (C) 2000 Scyld.
24  *
25  *  Copyright (C) 2000 Scyld Computing Corporation
26  *
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.
31  *
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.
36  *
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.
40  *
41  * Portions related to the alpha architecture are:
42  *
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.
52  *
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.
56  *
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.
68  *
69  *  $Id: vsmsetup.c,v 1.8 2006/09/08 12:47:57 andrei Exp $
70  *
71  * Copyright (C) 2007 Advanced Micro Devices, Inc.
72  *
73  *--------------------------------------------------------------------*/
74
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
78
79   Used initially for epia-m where there are problems getting the bios
80   emulator to successfully run this bios.
81 */
82
83 /* Declare a temporary global descriptor table - necessary because the
84    Core part of the bios no longer sets up any 16 bit segments */
85 __asm__(
86        /* pointer to original gdt */
87        "gdtarg:                 \n"
88        "        .word   gdt_limit       \n"
89        "        .long   gdt             \n"
90        /* compute the table limit */
91        "__mygdt_limit = __mygdt_end - __mygdt - 1       \n"
92        "__mygdtaddr:                    \n"
93        "        .word   __mygdt_limit   \n"
94        "        .long   __mygdt         \n"
95        "__mygdt:                        \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"
117        "__mygdt_end:                            \n"
118 );
119
120 /* Declare a pointer to where our idt is going to be i.e. at mem zero */
121 __asm__(
122         "__myidt:               \n"
123         /* 16-bit limit */
124         "       .word 1023      \n"
125         /* 24-bit base */
126         "       .long 0         \n" 
127         "       .word 0         \n"
128 );
129
130 /* The address arguments to this function are PHYSICAL ADDRESSES */
131 static void real_mode_switch_call_vsm(unsigned long smm, unsigned long sysm)
132 {
133         uint16_t entryHi = (VSA2_ENTRY_POINT & 0xffff0000) >> 4;
134         uint16_t entryLo = (VSA2_ENTRY_POINT & 0xffff);
135
136         __asm__ __volatile__(
137                 // paranoia -- does ecx get saved? not sure. This is
138                 // the easiest safe thing to do.
139                 "       pushal                  \n"
140                 /* save the stack */
141                 "       mov     %%esp, __stack  \n"
142                 "       jmp     1f              \n"
143                 "__stack: .long 0               \n" 
144                 "1:\n"
145                 /* get devfn into %%ecx */
146                 "       movl    %%esp, %%ebp    \n"
147 #if 0
148                 /* I'm not happy about that pushal followed by esp-relative
149                  * references. Just do hard-codes for now
150                  */
151                 "       movl    8(%%ebp), %%ecx \n"
152                 "       movl    12(%%ebp), %%edx        \n"
153 #endif
154                 "       movl    %0, %%ecx       \n"
155                 "       movl    %1, %%edx       \n"
156                 /* load 'our' gdt */
157                 "       lgdt    %%cs:__mygdtaddr        \n"
158                 /*  This configures CS properly for real mode. */
159                 "       ljmp    $0x28, $__rms_16bit\n"
160                 "__rms_16bit:                   \n"
161                 "       .code16                 \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"
167                 "       mov     %%ax, %%ds              \n"
168                 "       mov     %%ax, %%es              \n"
169                 "       mov     %%ax, %%fs              \n"
170                 "       mov     %%ax, %%gs              \n"
171                 "       mov     %%ax, %%ss              \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"
178                 "__rms_real:                    \n"
179
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. 
183                  */
184
185                 /* Setup a stack */
186                 "       mov     $0x0, %%ax      \n"
187                 "       mov     %%ax, %%ss      \n"
188                 "       movl    $0x1000, %%eax  \n"
189                 "       movl    %%eax, %%esp    \n"
190                 /* Load our 16 it idt */
191                 "       xor     %%ax, %%ax      \n"
192                 "       mov     %%ax, %%ds      \n"
193                 "       lidt    __myidt         \n"
194                 /* Dump zeros in the other segregs */
195                 "       mov     %%ax, %%es      \n"
196                 /* FixMe: Big real mode for gs, fs? */
197                 "       mov     %%ax, %%fs      \n"
198                 "       mov     %%ax, %%gs      \n"
199                 "       mov     $0x40, %%ax     \n"
200                 "       mov     %%ax, %%ds      \n"
201                 //"     mov     %%cx, %%ax      \n"
202                 "       movl    %0, %%ecx       \n"
203                 "       movl    %1, %%edx       \n"
204                 /* call the VSA2 entry point address */
205                 "       lcall   %2, %3\n"
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"
213                 "vsmrestart:\n"
214                 "       .code32\n"
215                 "       movw    $0x18, %%ax     \n"
216                 "       mov     %%ax, %%ds      \n"
217                 "       mov     %%ax, %%es      \n"
218                 "       mov     %%ax, %%fs      \n"
219                 "       mov     %%ax, %%gs      \n"
220                 "       mov     %%ax, %%ss      \n"
221                 /* restore proper gdt and idt */
222                 "       lgdt    %%cs:gdtarg     \n"
223                 "       lidt    idtarg          \n"
224                 ".globl vsm_exit                \n"
225                 "vsm_exit:                      \n"
226                 "       mov     __stack, %%esp  \n"
227                 "       popal                   \n"::
228                 "g" (smm), "g"(sysm), "g"(entryHi), "g"(entryLo)
229         );
230 }
231
232 __asm__(".text\n" "real_mode_switch_end:\n");
233 extern char real_mode_switch_end[];
234
235 // andrei: some VSA virtual register helpers: raw read and MSR read
236
237 uint32_t VSA_vrRead(uint16_t classIndex)
238 {
239         unsigned eax, ebx, ecx, edx;
240         asm volatile (
241                 "movw   $0x0AC1C, %%dx          \n"
242                 "orl    $0x0FC530000, %%eax     \n"
243                 "outl   %%eax, %%dx             \n"
244                 "addb   $2, %%dl                \n"
245                 "inw    %%dx, %%ax              \n"
246                 : "=a" (eax), "=b"(ebx), "=c"(ecx), "=d"(edx) 
247                 : "a"(classIndex)
248         );
249
250         return eax;
251 }
252
253 uint32_t VSA_msrRead(uint32_t msrAddr)
254 {
255         unsigned eax, ebx, ecx, edx;
256         asm volatile (
257                 "movw   $0x0AC1C, %%dx                  \n"
258                 "movl   $0x0FC530007, %%eax             \n"
259                 "outl   %%eax, %%dx                             \n"
260                 "addb   $2, %%dl                                \n"
261                 "inw    %%dx, %%ax                              \n"
262                 : "=a" (eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
263                 : "c"(msrAddr)
264         );
265
266         return eax;
267 }
268
269 void do_vsmbios(void)
270 {
271         device_t dev;
272         unsigned long busdevfn;
273         unsigned char *buf;
274         unsigned int size = SMM_SIZE * 1024;
275         int i;
276
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;
281         }
282
283         /* declare rom address here - keep any config data out of the way
284          * of core LXB stuff */
285
286         /* this is the base of rom on the LX at present. At some point, this has to be
287          * much better parameterized
288          */
289
290         if ((unsigned int)cbfs_load_stage("vsa") != VSA2_ENTRY_POINT) {
291                 printk(BIOS_ERR, "do_vsmbios: Failed to load VSA.\n");
292         }
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,
299            don't bother. */
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");
303                 return;
304         }
305
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);
309
310         /* restart timer 1 */
311         outb(0x56, 0x43);
312         outb(0x12, 0x41);
313
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");
317         else
318                 printk(BIOS_ERR, "do_vsmbios: VSA2 VR signature not valid, install failed!\n");
319 }
320
321 // we had hoped to avoid this.
322 // this is a stub IDT only. It's main purpose is to ignore calls
323 // to the BIOS.
324 // no longer. Dammit. We have to respond to these.
325 struct realidt {
326         unsigned short offset, cs;
327 };
328
329 // from a handy writeup that andrey found.
330
331 // handler.
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.
344 void handler(void)
345 {
346         __asm__ __volatile__(
347                 "       .code16         \n"
348                 "idthandle:             \n"
349                 "       pushal          \n"
350                 "       movb    $0, %al \n"
351                 "       ljmp    $0, $callbiosint16\n"
352                 "end_idthandle:         \n"
353                 "       .code32         \n"
354         );
355 }
356
357 void debughandler(void)
358 {
359         __asm__ __volatile__(
360                 "       .code16         \n"
361                 "debughandle:           \n"
362                 "       pushw   %cx     \n"
363                 "       movw    $250, %cx \n"
364                 "dbh1:                  \n"
365                 "       loop    dbh1    \n"
366                 "       popw    %cx     \n"
367                 "       iret            \n"
368                 "end_debughandle:       \n" 
369                 ".code32                \n"
370         );
371 }
372
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)
380 {
381         __asm__ __volatile__(
382                 "       .code16         \n"
383                 "callbiosint16:         \n"
384                 "       push    %ds     \n"
385                 "       push    %es     \n"
386                 "       push    %fs     \n"
387                 "       push    %gs     \n"
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.
394                 "       pushl   %eax    \n" 
395                 "       movb    $0xbb, %al\n" 
396                 "       outb    %al, $0x80\n" 
397                 "       movl    %cr0, %eax\n" 
398                 "       orl     $0x00000001, %eax\n"    /* PE = 1 */
399                 "       movl    %eax, %cr0\n"
400                 /* Now that we are in protected mode jump to a 32 bit code segment. */
401                 "       data32  ljmp    $0x10, $biosprotect\n"
402                 "biosprotect:           \n"
403                 "       .code32         \n"
404                 "       movw    $0x18, %ax\n"
405                 "       mov     %ax, %ds\n"
406                 "       mov     %ax, %es\n"
407                 "       mov     %ax, %fs\n"
408                 "       mov     %ax, %gs\n"
409                 "       mov     %ax, %ss\n"
410                 "       lidt    idtarg  \n"
411                 "       call    biosint \n"
412                 // back to real mode ...
413                 "       ljmp    $0x28, $__rms_16bit2\n"
414                 "__rms_16bit2:          \n"
415                 "       .code16         \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. */
420                 "       mov     $0x30, %ax\n"
421                 "       mov     %ax, %ds\n"
422                 "       mov     %ax, %es\n"
423                 "       mov     %ax, %fs\n"
424                 "       mov     %ax, %gs\n"
425                 "       mov     %ax, %ss\n"
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"
432                 "__rms_real2:                   \n"
433                 /* Setup a stack
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
437                  * modes.
438                  */
439                 "       mov     $0x0, %ax       \n"
440                 "       mov     %ax, %ss        \n"
441                 /* debugging for RGM */
442                 "       mov     $0x11, %al      \n"
443                 "       outb    %al, $0x80      \n"
444                 /* Load our 16 bit idt */
445                 "       xor     %ax, %ax        \n"
446                 "       mov     %ax, %ds        \n"
447                 "       lidt    __myidt         \n"
448                 /* Dump zeros in the other segregs */
449                 "       mov     %ax, %es        \n"
450                 "       mov     %ax, %fs        \n"
451                 "       mov     %ax, %gs        \n"
452                 "       mov     $0x40, %ax      \n"
453                 "       mov     %ax, %ds        \n"
454                 /* pop the INT # that you pushed earlier */
455                 "       popl    %eax            \n"
456                 "       pop     %gs             \n"
457                 "       pop     %fs             \n"
458                 "       pop     %es             \n"
459                 "       pop     %ds             \n"
460                 "       popal                   \n"
461                 "       iret                    \n"
462                 "       .code32                 \n"
463         );
464 }
465
466 enum {
467         PCIBIOS = 0x1a,
468         MEMSIZE = 0x12
469 };
470
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);
474
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);
479
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)
487 {
488         unsigned long ip;
489         unsigned long cs;
490         unsigned long flags;
491         int ret = -1;
492
493         ip = cs_ip & 0xffff;
494         cs = cs_ip >> 16;
495         flags = stackflags;
496
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",
499                      eax, ebx, ecx, edx);
500         printk(BIOS_DEBUG, "biosint: ebp 0x%lx esp 0x%lx edi 0x%lx esi 0x%lx\n",
501                      ebp, esp, edi, esi);
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));
506
507         // cases in a good compiler are just as good as your own tables.
508         switch (intnumber) {
509         case 0 ... 15:
510                 // These are not BIOS service, but the CPU-generated exceptions
511                 printk(BIOS_INFO, "biosint: Oops, exception 0x%x\n", (u32)intnumber);
512                 if (esp < 0x1000) {
513                         printk(BIOS_DEBUG, "Stack contents: ");
514                         while (esp < 0x1000) {
515                                 printk(BIOS_DEBUG, "0x%04x ", *(unsigned short *)esp);
516                                 esp += 2;
517                         }
518                         printk(BIOS_DEBUG, "\n");
519                 }
520                 printk(BIOS_DEBUG, "biosint: Bailing out ... not now\n");
521                 // "longjmp"
522                 //vga_exit();
523                 break;
524
525         case PCIBIOS:
526                 ret = pcibios(&edi, &esi, &ebp, &esp,
527                               &ebx, &edx, &ecx, &eax, &flags);
528                 break;
529         case MEMSIZE:
530                 // who cares.
531                 eax = 128 * 1024;
532                 ret = 0;
533                 break;
534         case 0x15:
535                 ret = handleint21(&edi, &esi, &ebp, &esp,
536                                   &ebx, &edx, &ecx, &eax, &flags);
537                 break;
538         default:
539                 printk(BIOS_INFO, "BIOSINT: Unsupported int #0x%x\n", (u32)intnumber);
540                 break;
541         }
542         if (ret)
543                 flags |= 1;     // carry flags
544         else
545                 flags &= ~1;
546         stackflags = flags;
547         return ret;
548 }
549
550 void setup_realmode_idt(void)
551 {
552         extern unsigned char idthandle, end_idthandle;
553         extern unsigned char debughandle, end_debughandle;
554
555         int i;
556         struct realidt *idts = (struct realidt *)0;
557         int codesize = &end_idthandle - &idthandle;
558         unsigned char *intbyte, *codeptr;
559
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++) {
570                 idts[i].cs = 0;
571                 codeptr = (unsigned char *)4096 + i * codesize;
572                 idts[i].offset = (unsigned)codeptr;
573                 memcpy(codeptr, &idthandle, codesize);
574                 intbyte = codeptr + 3;
575                 *intbyte = i;
576         }
577
578         // fixed entry points
579
580         // VGA BIOSes tend to hardcode f000:f065 as the previous handler of
581         // int10.
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 */
588
589         /* debug handler - useful to set a programmable delay between instructions if the
590            TF bit is set upon call to real mode */
591         idts[1].cs = 0;
592         idts[1].offset = 16384;
593         memcpy((void *)16384, &debughandle, &end_debughandle - &debughandle);
594 }
595
596 enum {
597         CHECK = 0xb001,
598         FINDDEV = 0xb102,
599         READCONFBYTE = 0xb108,
600         READCONFWORD = 0xb109,
601         READCONFDWORD = 0xb10a,
602         WRITECONFBYTE = 0xb10b,
603         WRITECONFWORD = 0xb10c,
604         WRITECONFDWORD = 0xb10d
605 };
606
607 // errors go in AH. Just set these up so that word assigns
608 // will work. KISS.
609 enum {
610         PCIBIOS_NODEV = 0x8600,
611         PCIBIOS_BADREG = 0x8700
612 };
613
614 int
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)
618 {
619         unsigned short func = (unsigned short)*peax;
620         int retval = 0;
621         unsigned short devid, vendorid, devfn;
622         short devindex;         /* Use short to get rid of gabage in upper half of 32-bit register */
623         unsigned char bus;
624         device_t dev;
625
626         switch (func) {
627         case CHECK:
628                 *pedx = 0x4350;
629                 *pecx = 0x2049;
630                 retval = 0;
631                 break;
632         case FINDDEV:
633                 {
634                         devid = *pecx;
635                         vendorid = *pedx;
636                         devindex = *pesi;
637                         dev = 0;
638                         while ((dev = dev_find_device(vendorid, devid, dev))) {
639                                 if (devindex <= 0)
640                                         break;
641                                 devindex--;
642                         }
643                         if (dev) {
644                                 unsigned short busdevfn;
645                                 *peax = 0;
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,
651                                              busdevfn);
652                                 *pebx = busdevfn;
653                                 retval = 0;
654                         } else {
655                                 *peax = PCIBIOS_NODEV;
656                                 retval = -1;
657                         }
658                 }
659                 break;
660         case READCONFDWORD:
661         case READCONFWORD:
662         case READCONFBYTE:
663         case WRITECONFDWORD:
664         case WRITECONFWORD:
665         case WRITECONFBYTE:
666                 {
667                         unsigned long dword;
668                         unsigned short word;
669                         unsigned char byte;
670                         unsigned char reg;
671
672                         devfn = *pebx & 0xff;
673                         bus = *pebx >> 8;
674                         reg = *pedi;
675                         dev = dev_find_slot(bus, devfn);
676                         if (!dev) {
677                                 printk(BIOS_DEBUG, "0x%x: BAD DEVICE bus %d devfn 0x%x\n",
678                                      func, bus, devfn);
679                                 // idiots. the pcibios guys assumed you'd never pass a bad bus/devfn!
680                                 *peax = PCIBIOS_BADREG;
681                                 retval = -1;
682                         }
683                         switch (func) {
684                         case READCONFBYTE:
685                                 byte = pci_read_config8(dev, reg);
686                                 *pecx = byte;
687                                 break;
688                         case READCONFWORD:
689                                 word = pci_read_config16(dev, reg);
690                                 *pecx = word;
691                                 break;
692                         case READCONFDWORD:
693                                 dword = pci_read_config32(dev, reg);
694                                 *pecx = dword;
695                                 break;
696                         case WRITECONFBYTE:
697                                 byte = *pecx;
698                                 pci_write_config8(dev, reg, byte);
699                                 break;
700                         case WRITECONFWORD:
701                                 word = *pecx;
702                                 pci_write_config16(dev, reg, word);
703                                 break;
704                         case WRITECONFDWORD:
705                                 dword = *pecx;
706                                 pci_write_config32(dev, reg, dword);
707                                 break;
708                         }
709
710                         if (retval)
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);
714                         *peax = 0;
715                         retval = 0;
716                 }
717                 break;
718         default:
719                 printk(BIOS_ERR, "UNSUPPORTED PCIBIOS FUNCTION 0x%x\n", func);
720                 break;
721         }
722
723         return retval;
724 }
725
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)
729 {
730         int res = -1;
731         printk(BIOS_DEBUG, "handleint21, eax 0x%x\n", (u32)*eax);
732         switch (*eax & 0xffff) {
733         case 0x5f19:
734                 break;
735         case 0x5f18:
736                 *eax = 0x5f;
737                 *ebx = 0x545;   // MCLK = 133, 32M frame buffer, 256 M main memory
738                 *ecx = 0x060;
739                 res = 0;
740                 break;
741         case 0x5f00:
742                 *eax = 0x8600;
743                 break;
744         case 0x5f01:
745                 *eax = 0x5f;
746                 *ecx = (*ecx & 0xffffff00) | 2; // panel type =  2 = 1024 * 768
747                 res = 0;
748                 break;
749         case 0x5f02:
750                 *eax = 0x5f;
751                 *ebx = (*ebx & 0xffff0000) | 2;
752                 *ecx = (*ecx & 0xffff0000) | 0x401;     // PAL + crt only
753                 *edx = (*edx & 0xffff0000) | 0; // TV Layout - default
754                 res = 0;
755                 break;
756         case 0x5f0f:
757                 *eax = 0x860f;
758                 break;
759         case 0xBEA7:
760                 *eax = 66;
761                 break;
762         case 0xBEA4:
763                 *eax = 500;
764                 break;
765         }
766         return res;
767 }