Add VIA CX700 support, plus VIA vt8454c reference board support.
[coreboot.git] / src / northbridge / via / cx700 / vgabios.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * (C) 2007-2009 coresystems GmbH
5  * (See further copyright notices below)
6  *
7  * NOTE: This file is supposed to go away once the generic vm86 handler
8  * in util/x86emu is able to handle intXX hooks like yabel does.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; version 2 of the License.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
22  */
23
24 #include <console/console.h>
25 #include <device/pci.h>
26 #include <device/pci_ids.h>
27 #include <device/pci_ops.h>
28 #undef __KERNEL__
29 #include <arch/io.h>
30 #include <string.h>
31
32 void write_protect_vgabios(void);
33
34 /* vgabios.c. Derived from: */
35
36 /*------------------------------------------------------------ -*- C -*-
37  *  2 Kernel Monte a.k.a. Linux loading Linux on x86
38  *
39  *  Erik Arjan Hendriks <hendriks@lanl.gov>
40  *
41  *  This version is a derivative of the original two kernel monte
42  *  which is (C) 2000 Scyld.
43  *
44  *  Copyright (C) 2000 Scyld Computing Corporation
45  *
46  * Portions related to the alpha architecture are:
47  *
48  *  Copyright(C) 2001 University of California.  LA-CC Number 01-67.
49  *  This software has been authored by an employee or employees of the
50  *  University of California, operator of the Los Alamos National
51  *  Laboratory under Contract No.  W-7405-ENG-36 with the U.S.
52  *  Department of Energy.  The U.S. Government has rights to use,
53  *  reproduce, and distribute this software. If the software is
54  *  modified to produce derivative works, such modified software should
55  *  be clearly marked, so as not to confuse it with the version
56  *  available from LANL.
57  *
58  *  This software may be used and distributed according to the terms
59  *  of the GNU General Public License, incorporated herein by
60  *  reference to http://www.gnu.org/licenses/gpl.html.
61  *
62  *  This software is provided by the author(s) "as is" and any express
63  *  or implied warranties, including, but not limited to, the implied
64  *  warranties of merchantability and fitness for a particular purpose
65  *  are disclaimed.  In no event shall the author(s) be liable for any
66  *  direct, indirect, incidental, special, exemplary, or consequential
67  *  damages (including, but not limited to, procurement of substitute
68  *  goods or services; loss of use, data, or profits; or business
69  *  interruption) however caused and on any theory of liability,
70  *  whether in contract, strict liability, or tort (including
71  *  negligence or otherwise) arising in any way out of the use of this
72  *  software, even if advised of the possibility of such damage.
73  *
74  *--------------------------------------------------------------------*/
75
76 /* Modified to be a self sufficient plug in so that it can be used 
77    without reliance on other parts of core Linuxbios 
78    (C) 2005 Nick.Barker9@btinternet.com
79
80   Used initially for epia-m where there are problems getting the bios
81   emulator to successfully run this bios.
82 */
83
84 /* Declare a temporary global descriptor table - necessary because the
85    Core part of the bios no longer sets up any 16 bit segments */
86 __asm__(
87                /* pointer to original gdt */
88                "gdtarg:                 \n"
89                "        .word   gdt_limit       \n"
90                "        .long   gdt             \n"
91                /* compute the table limit */
92                "__mygdt_limit = __mygdt_end - __mygdt - 1       \n"
93                "__mygdtaddr:                    \n"
94                "        .word   __mygdt_limit   \n"
95                "        .long   __mygdt         \n"
96                "__mygdt:                        \n"
97                /* selgdt 0, unused */
98                "        .word   0x0000, 0x0000  \n"
99                "        .byte   0x00, 0x00, 0x00, 0x00  \n"
100                /* selgdt 8, unused */
101                "        .word   0x0000, 0x0000          \n"
102                "        .byte   0x00, 0x00, 0x00, 0x00  \n"
103                /* selgdt 0x10, flat code segment */
104                "        .word   0xffff, 0x0000          \n"
105                "        .byte   0x00, 0x9b, 0xcf, 0x00  \n"
106                /* selgdt 0x18, flat data segment */
107                "        .word   0xffff, 0x0000          \n"
108                "        .byte   0x00, 0x93, 0xcf, 0x00  \n"
109                /* selgdt 0x20, unused */
110                "        .word   0x0000, 0x0000          \n"
111                "        .byte   0x00, 0x00, 0x00, 0x00  \n"
112                /* selgdt 0x28 16-bit 64k code at 0x00000000 */
113                "        .word   0xffff, 0x0000          \n"
114                "        .byte   0, 0x9a, 0, 0           \n"
115                /* selgdt 0x30 16-bit 64k data at 0x00000000 */
116                "        .word   0xffff, 0x0000          \n"
117                "        .byte   0, 0x92, 0, 0           \n"
118                "__mygdt_end:                            \n");
119
120 /* Declare a pointer to where our idt is going to be i.e. at mem zero */
121 __asm__("__myidt:               \n"
122         /* 16-bit limit */
123         "       .word 1023      \n"
124         /* 24-bit base */
125         "       .long 0         \n" "   .word 0         \n");
126
127 /* The address arguments to this function are PHYSICAL ADDRESSES */
128 static void real_mode_switch_call_vga(unsigned long devfn)
129 {
130         __asm__ __volatile__(
131                                     // paranoia -- does ecx get saved? not sure. This is 
132                                     // the easiest safe thing to do.
133                                     "   pushal                  \n"
134                                     /* save the stack */
135                                     "   mov     %esp, __stack   \n"
136                                     "   jmp     1f              \n"
137                                     "__stack: .long 0           \n" "1:\n"
138                                     /* get devfn into %ecx */
139                                     "   movl    %esp, %ebp      \n"
140                                     "   movl    8(%ebp), %ecx   \n"
141                                     /* load 'our' gdt */
142                                     "   lgdt    %cs:__mygdtaddr \n"
143                                     /*  This configures CS properly for real mode. */
144                                     "   ljmp    $0x28, $__rms_16bit\n"
145                                     "__rms_16bit:                       \n"
146                                     "   .code16                 \n"
147                                     /* 16 bit code from here on... */
148                                     /* Load the segment registers w/ properly configured segment
149                                      * descriptors.  They will retain these configurations (limits,
150                                      * writability, etc.) once protected mode is turned off. */
151                                     "   mov     $0x30, %ax      \n"
152                                     "   mov     %ax, %ds        \n"
153                                     "   mov     %ax, %es        \n"
154                                     "   mov     %ax, %fs        \n"
155                                     "   mov     %ax, %gs        \n"
156                                     "   mov     %ax, %ss        \n"
157                                     /* Turn off protection (bit 0 in CR0) */
158                                     "   movl    %cr0, %eax      \n"
159                                     "   andl    $0xFFFFFFFE, %eax \n"
160                                     "   movl    %eax, %cr0      \n"
161                                     /* Now really going into real mode */
162                                     "   ljmp    $0,  $__rms_real\n"
163                                     "__rms_real:                        \n"
164                                     /* put the stack at the end of page zero. 
165                                      * that way we can easily share it between real and protected, 
166                                      * since the 16-bit ESP at segment 0 will work for any case. 
167                                      */
168                                     /* Setup a stack */
169                                     "   mov     $0x0, %ax       \n"
170                                     "   mov     %ax, %ss        \n"
171                                     "   movl    $0x1000, %eax   \n"
172                                     "   movl    %eax, %esp      \n"
173                                     /* Load our 16 it idt */
174                                     "   xor     %ax, %ax        \n"
175                                     "   mov     %ax, %ds        \n"
176                                     "   lidt    __myidt         \n"
177                                     /* Dump zeros in the other segregs */
178                                     "   mov     %ax, %es        \n"
179                                     "   mov     %ax, %fs        \n"
180                                     "   mov     %ax, %gs        \n"
181                                     "   mov     $0x40, %ax      \n"
182                                     "   mov     %ax, %ds        \n"
183                                     "   mov     %cx, %ax        \n"
184                                     /* run VGA BIOS at 0xc000:0003 */
185                                     "   lcall   $0xc000, $0x0003\n"
186                                     /* if we got here, just about done. 
187                                      * Need to get back to protected mode */
188                                     "   movl    %cr0, %eax      \n" "   orl     $0x0000001, %eax\n"     /* PE = 1 */
189                                     "   movl    %eax, %cr0      \n"
190                                     /* Now that we are in protected mode jump to a 32 bit code segment. */
191                                     "   data32  ljmp    $0x10, $vgarestart\n"
192                                     "vgarestart:\n"
193                                     "   .code32\n"
194                                     "   movw    $0x18, %ax      \n"
195                                     "   mov     %ax, %ds        \n"
196                                     "   mov     %ax, %es        \n"
197                                     "   mov     %ax, %fs        \n"
198                                     "   mov     %ax, %gs        \n"
199                                     "   mov     %ax, %ss        \n"
200                                     /* restore proper gdt and idt */
201                                     "   lgdt    %cs:gdtarg      \n"
202                                     "   lidt    idtarg          \n"
203                                     ".globl vga_exit            \n"
204                                     "vga_exit:                  \n"
205                                     "   mov     __stack, %esp   \n"
206                                     "   popal                   \n");
207 }
208
209 __asm__(".text\n" "real_mode_switch_end:\n");
210 extern char real_mode_switch_end[];
211
212 /* call vga bios int 10 function 0x4f14 to enable main console 
213    epia-m does not always autosence the main console so forcing it on is good !! */
214 void vga_enable_console(void)
215 {
216         __asm__ __volatile__(
217                                     /* paranoia -- does ecx get saved? not sure. This is 
218                                      * the easiest safe thing to do. */
219                                     "   pushal                  \n"
220                                     /* save the stack */
221                                     "   mov     %esp, __stack   \n"
222                                     /* load 'our' gdt */
223                                     "   lgdt    %cs:__mygdtaddr \n"
224                                     /*  This configures CS properly for real mode. */
225                                     "   ljmp    $0x28, $__vga_ec_16bit\n"
226                                     "__vga_ec_16bit:            \n"
227                                     "   .code16                 \n"
228                                     /* 16 bit code from here on... */
229                                     /* Load the segment registers w/ properly configured segment
230                                      * descriptors.  They will retain these configurations (limits,
231                                      * writability, etc.) once protected mode is turned off. */
232                                     "   mov     $0x30, %ax      \n"
233                                     "   mov     %ax, %ds        \n"
234                                     "   mov     %ax, %es        \n"
235                                     "   mov     %ax, %fs        \n"
236                                     "   mov     %ax, %gs        \n"
237                                     "   mov     %ax, %ss        \n"
238                                     /* Turn off protection (bit 0 in CR0) */
239                                     "   movl    %cr0, %eax      \n"
240                                     "   andl    $0xFFFFFFFE, %eax\n"
241                                     "   movl    %eax, %cr0      \n"
242                                     /* Now really going into real mode */
243                                     "   ljmp    $0, $__vga_ec_real \n"
244                                     "__vga_ec_real:                  \n"
245                                     /* put the stack at the end of page zero. 
246                                      * that way we can easily share it between real and protected, 
247                                      * since the 16-bit ESP at segment 0 will work for any case. 
248                                      */
249                                     /* Setup a stack */
250                                     "   mov     $0x0, %ax       \n"
251                                     "   mov     %ax, %ss        \n"
252                                     "   movl    $0x1000, %eax   \n"
253                                     "   movl    %eax, %esp      \n"
254                                     /* debugging for RGM */
255                                     "   mov     $0x11, %al      \n"
256                                     "   outb    %al, $0x80      \n"
257                                     /* Load our 16 it idt */
258                                     "   xor     %ax, %ax        \n"
259                                     "   mov     %ax, %ds        \n"
260                                     "   lidt    __myidt         \n"
261                                     /* Dump zeros in the other segregs */
262                                     "   mov     %ax, %ds        \n"
263                                     "   mov     %ax, %es        \n"
264                                     "   mov     %ax, %fs        \n"
265                                     "   mov     %ax, %gs        \n"
266                                     /* ask bios to enable main console */
267                                     /* set up for int 10 call - values found from X server
268                                      * bios call routines */
269                                     "   movw    $0x4f14,%ax     \n"
270                                     "   movw    $0x8003,%bx     \n"
271                                     "   movw    $1, %cx         \n"
272                                     "   movw    $0, %dx         \n"
273                                     "   movw    $0, %di         \n"
274                                     "   int     $0x10           \n"
275                                     "   movb    $0x55, %al      \n"
276                                     "   outb    %al, $0x80      \n"
277                                     /* if we got here, just about done. 
278                                      * Need to get back to protected mode */
279                                     "   movl    %cr0, %eax      \n" "   orl     $0x0000001, %eax\n"     /* PE = 1 */
280                                     "   movl    %eax, %cr0      \n"
281                                     /* Now that we are in protected mode jump to a 32 bit code segment. */
282                                     "   data32  ljmp    $0x10, $vga_ec_restart\n"
283                                     "vga_ec_restart:\n"
284                                     "   .code32\n"
285                                     "   movw    $0x18, %ax      \n"
286                                     "   mov     %ax, %ds        \n"
287                                     "   mov     %ax, %es        \n"
288                                     "   mov     %ax, %fs        \n"
289                                     "   mov     %ax, %gs        \n"
290                                     "   mov     %ax, %ss        \n"
291                                     /* restore proper gdt and idt */
292                                     "   lgdt    %cs:gdtarg      \n"
293                                     "   lidt    idtarg          \n"
294                                     "   .globl  vga__ec_exit    \n"
295                                     "vga_ec_exit:\n"
296                                     "   mov     __stack, %esp   \n"
297                                     "   popal\n");
298 }
299
300 void do_vgabios(void)
301 {
302         device_t dev;
303         unsigned long busdevfn;
304         unsigned int rom = 0;
305         unsigned char *buf;
306         unsigned int size = 64 * 1024;
307         int i;
308
309         /* clear vga bios data area */
310         for (i = 0x400; i < 0x500; i++) {
311                 *(unsigned char *)i = 0;
312         }
313
314         dev = dev_find_class(PCI_CLASS_DISPLAY_VGA << 8, 0);
315
316         if (!dev) {
317                 printk_debug("NO VGA FOUND\n");
318                 return;
319         }
320         printk_debug("found VGA: vid=%x, did=%x\n", dev->vendor, dev->device);
321
322         /* declare rom address here - keep any config data out of the way
323          * of core LXB stuff */
324
325 #warning ROM address hardcoded to 512K
326         rom = 0xfff80000;
327         pci_write_config32(dev, PCI_ROM_ADDRESS, rom | 1);
328         printk_debug("rom base, size: %x\n", rom);
329
330         buf = (unsigned char *)rom;
331         if ((buf[0] == 0x55) && (buf[1] == 0xaa)) {
332                 memcpy((void *)0xc0000, buf, size);
333
334                 write_protect_vgabios();        // in northbridge
335
336                 // check signature again
337                 buf = (unsigned char *)0xc0000;
338                 if (buf[0] == 0x55 && buf[1] == 0xAA) {
339                         busdevfn =
340                             (dev->bus->secondary << 8) | dev->path.pci.devfn;
341                         printk_debug("bus/devfn = %#x\n", busdevfn);
342
343                         real_mode_switch_call_vga(busdevfn);
344                 } else
345                         printk_debug("Failed to copy VGA BIOS to 0xc0000\n");
346         } else
347                 printk_debug("BAD SIGNATURE 0x%x 0x%x\n", buf[0], buf[1]);
348
349         pci_write_config32(dev, PCI_ROM_ADDRESS, 0);
350 }
351
352 // we had hoped to avoid this. 
353 // this is a stub IDT only. It's main purpose is to ignore calls 
354 // to the BIOS. 
355 // no longer. Dammit. We have to respond to these.
356 struct realidt {
357         unsigned short offset, cs;
358 };
359
360 // from a handy writeup that andrey found.
361
362 // handler. 
363 // There are some assumptions we can make here. 
364 // First, the Top Of Stack (TOS) is located on the top of page zero. 
365 // we can share this stack between real and protected mode. 
366 // that simplifies a lot of things ...
367 // we'll just push all the registers on the stack as longwords, 
368 // and pop to protected mode. 
369 // second, since this only ever runs as part of linuxbios, 
370 // we know all the segment register values -- so we don't save any.
371 // keep the handler that calls things small. It can do a call to 
372 // more complex code in linuxbios itself. This helps a lot as we don't
373 // have to do address fixup in this little stub, and calls are absolute
374 // so the handler is relocatable.
375 void handler(void)
376 {
377         __asm__ __volatile__("  .code16         \n"
378                              "idthandle:                \n"
379                              "  pushal          \n"
380                              "  movb    $0, %al \n"
381                              "  ljmp    $0, $callbiosint16\n"
382                              "end_idthandle:            \n"
383                              "  .code32         \n");
384 }
385
386 void debughandler(void)
387 {
388         __asm__ __volatile__("  .code16         \n"
389                              "debughandle:              \n"
390                              "  pushw   %cx     \n"
391                              "  movw    $250, %cx \n"
392                              "dbh1:                     \n"
393                              "  loop    dbh1    \n"
394                              "  popw    %cx     \n"
395                              "  iret            \n"
396                              "end_debughandle:  \n" ".code32            \n");
397 }
398
399 // Calling conventions. The first C function is called with this stuff
400 // on the stack. They look like value parameters, but note that if you
401 // modify them they will go back to the INTx function modified. 
402 // the C function will call the biosint function with these as
403 // REFERENCE parameters. In this way, we can easily get 
404 // returns back to the INTx caller (i.e. vgabios)
405 void callbiosint(void)
406 {
407         __asm__ __volatile__("  .code16         \n"
408                              "callbiosint16:            \n"
409                              "  push    %ds     \n"
410                              "  push    %es     \n"
411                              "  push    %fs     \n" "   push    %gs     \n"
412                              // clean up the int #. To save space we put it in the lower
413                              // byte. But the top 24 bits are junk. 
414                              "  andl    $0xff, %eax\n"
415                              // this push does two things:
416                              // - put the INT # on the stack as a parameter
417                              // - provides us with a temp for the %cr0 mods.
418                              "  pushl   %eax    \n" "   movl    %cr0, %eax\n" " orl     $0x00000001, %eax\n"    /* PE = 1 */
419                              "  movl    %eax, %cr0\n"
420                              /* Now that we are in protected mode jump to a 32 bit code segment. */
421                              "  data32  ljmp    $0x10, $biosprotect\n"
422                              "biosprotect:              \n"
423                              "  .code32         \n"
424                              "  movw    $0x18, %ax          \n"
425                              "  mov     %ax, %ds          \n"
426                              "  mov     %ax, %es          \n"
427                              "  mov     %ax, %fs          \n"
428                              "  mov     %ax, %gs          \n"
429                              "  mov     %ax, %ss          \n"
430                              "  lidt    idtarg         \n"
431                              "  call    biosint \n"
432                              // back to real mode ...
433                              "  ljmp    $0x28, $__rms_16bit2\n"
434                              "__rms_16bit2:                     \n"
435                              "  .code16                 \n"
436                              /* 16 bit code from here on... */
437                              /* Load the segment registers w/ properly configured segment
438                               * descriptors.  They will retain these configurations (limits,
439                               * writability, etc.) once protected mode is turned off. */
440                              "  mov     $0x30, %ax      \n"
441                              "  mov     %ax, %ds        \n"
442                              "  mov     %ax, %es        \n"
443                              "  mov     %ax, %fs        \n"
444                              "  mov     %ax, %gs        \n"
445                              "  mov     %ax, %ss        \n"
446                              /* Turn off protection (bit 0 in CR0) */
447                              "  movl    %cr0, %eax              \n"
448                              "  andl    $0xFFFFFFFE, %eax       \n"
449                              "  movl    %eax, %cr0              \n"
450                              /* Now really going into real mode */
451                              "  ljmp $0,  $__rms_real2  \n"
452                              "__rms_real2:                      \n"
453                              /* Setup a stack
454                               * FixME: where is esp? */
455                              "  mov     $0x0, %ax       \n"
456                              "  mov     %ax, %ss        \n"
457                              /* ebugging for RGM */
458                              "  mov     $0x11, %al      \n"
459                              "  outb    %al, $0x80      \n"
460                              /* Load our 16 it idt */
461                              "  xor     %ax, %ax        \n"
462                              "  mov     %ax, %ds        \n"
463                              "  lidt    __myidt         \n"
464                              /* Dump zeros in the other segregs */
465                              "  mov     %ax, %es        \n"
466                              "  mov     %ax, %fs        \n"
467                              "  mov     %ax, %gs        \n"
468                              "  mov     $0x40, %ax      \n"
469                              "  mov     %ax, %ds        \n"
470                              /* pop the INT # that you pushed earlier */
471                              "  popl    %eax            \n"
472                              "  pop     %gs             \n"
473                              "  pop     %fs             \n"
474                              "  pop     %es             \n"
475                              "  pop     %ds             \n"
476                              "  popal                   \n"
477                              "  iret                    \n"
478                              "  .code32                 \n");
479 }
480
481 enum {
482         PCIBIOS = 0x1a,
483         MEMSIZE = 0x12
484 };
485
486 int pcibios(unsigned long *pedi, unsigned long *pesi, unsigned long *pebp,
487             unsigned long *pesp, unsigned long *pebx, unsigned long *pedx,
488             unsigned long *pecx, unsigned long *peax, unsigned long *pflags);
489
490 int handleint21(unsigned long *pedi, unsigned long *pesi, unsigned long *pebp,
491                 unsigned long *pesp, unsigned long *pebx, unsigned long *pedx,
492                 unsigned long *pecx, unsigned long *peax,
493                 unsigned long *pflags);
494
495 extern void vga_exit(void);
496
497 int biosint(unsigned long intnumber,
498             unsigned long gsfs, unsigned long dses,
499             unsigned long edi, unsigned long esi,
500             unsigned long ebp, unsigned long esp,
501             unsigned long ebx, unsigned long edx,
502             unsigned long ecx, unsigned long eax,
503             unsigned long cs_ip, unsigned short stackflags)
504 {
505         unsigned long ip;
506         unsigned long cs;
507         unsigned long flags;
508         int ret = -1;
509
510         ip = cs_ip & 0xffff;
511         cs = cs_ip >> 16;
512         flags = stackflags;
513
514         printk_debug("biosint: INT# 0x%lx\n", intnumber);
515         printk_debug("biosint: eax 0x%lx ebx 0x%lx ecx 0x%lx edx 0x%lx\n",
516                      eax, ebx, ecx, edx);
517         printk_debug("biosint: ebp 0x%lx esp 0x%lx edi 0x%lx esi 0x%lx\n",
518                      ebp, esp, edi, esi);
519         printk_debug("biosint:  ip 0x%x   cs 0x%x  flags 0x%x\n",
520                      ip, cs, flags);
521
522         // cases in a good compiler are just as good as your own tables. 
523         switch (intnumber) {
524         case 0 ... 15:
525                 // These are not BIOS service, but the CPU-generated exceptions
526                 printk_info("biosint: Oops, exception %u\n", intnumber);
527                 if (esp < 0x1000) {
528                         printk_debug("Stack contents: ");
529                         while (esp < 0x1000) {
530                                 printk_debug("0x%04x ", *(unsigned short *)esp);
531                                 esp += 2;
532                         }
533                         printk_debug("\n");
534                 }
535                 printk_debug("biosint: Bailing out\n");
536                 // "longjmp"
537                 vga_exit();
538                 break;
539
540         case PCIBIOS:
541                 ret = pcibios(&edi, &esi, &ebp, &esp,
542                               &ebx, &edx, &ecx, &eax, &flags);
543                 break;
544         case MEMSIZE:
545                 // who cares. 
546                 eax = 64 * 1024;
547                 ret = 0;
548                 break;
549         case 0x15:
550                 ret = handleint21(&edi, &esi, &ebp, &esp,
551                                   &ebx, &edx, &ecx, &eax, &flags);
552                 break;
553         default:
554                 printk_info("BIOSINT: Unsupport int #0x%x\n", intnumber);
555                 break;
556         }
557         if (ret)
558                 flags |= 1;     // carry flags
559         else
560                 flags &= ~1;
561         stackflags = flags;
562         return ret;
563 }
564
565 void setup_realmode_idt(void)
566 {
567         extern unsigned char idthandle, end_idthandle;
568         extern unsigned char debughandle, end_debughandle;
569
570         int i;
571         struct realidt *idts = (struct realidt *)0;
572         int codesize = &end_idthandle - &idthandle;
573         unsigned char *intbyte, *codeptr;
574
575         // for each int, we create a customized little handler
576         // that just pushes %ax, puts the int # in %al, 
577         // then calls the common interrupt handler. 
578         // this necessitated because intel didn't know much about 
579         // architecture when they did the 8086 (it shows)
580         // (hmm do they know anymore even now :-)
581         // obviously you can see I don't really care about memory 
582         // efficiency. If I did I would probe back through the stack
583         // and get it that way. But that's really disgusting.
584         for (i = 0; i < 256; i++) {
585                 idts[i].cs = 0;
586                 codeptr = (unsigned char *)4096 + i * codesize;
587                 idts[i].offset = (unsigned)codeptr;
588                 memcpy(codeptr, &idthandle, codesize);
589                 intbyte = codeptr + 3;
590                 *intbyte = i;
591         }
592
593         // fixed entry points
594
595         // VGA BIOSes tend to hardcode f000:f065 as the previous handler of
596         // int10. 
597         // calling convention here is the same as INTs, we can reuse
598         // the int entry code.
599         codeptr = (unsigned char *)0xff065;
600         memcpy(codeptr, &idthandle, codesize);
601         intbyte = codeptr + 3;
602         *intbyte = 0x42;        /* int42 is the relocated int10 */
603
604         // VIA's VBIOS will call f000:f859 instead of sending int15.
605         codeptr = (unsigned char *)0xff859;
606         memcpy(codeptr, &idthandle, codesize);
607         intbyte = codeptr + 3;
608         *intbyte = 0x15;
609
610         /* debug handler - useful to set a programmable delay between instructions if the
611            TF bit is set upon call to real mode */
612         idts[1].cs = 0;
613         idts[1].offset = 16384;
614         memcpy((void *)16384UL, &debughandle, &end_debughandle - &debughandle);
615 }
616
617 enum {
618         CHECK = 0xb001,
619         FINDDEV = 0xb102,
620         READCONFBYTE = 0xb108,
621         READCONFWORD = 0xb109,
622         READCONFDWORD = 0xb10a,
623         WRITECONFBYTE = 0xb10b,
624         WRITECONFWORD = 0xb10c,
625         WRITECONFDWORD = 0xb10d
626 };
627
628 // errors go in AH. Just set these up so that word assigns
629 // will work. KISS. 
630 enum {
631         PCIBIOS_NODEV = 0x8600,
632         PCIBIOS_BADREG = 0x8700
633 };
634
635 int
636 pcibios(unsigned long *pedi, unsigned long *pesi, unsigned long *pebp,
637         unsigned long *pesp, unsigned long *pebx, unsigned long *pedx,
638         unsigned long *pecx, unsigned long *peax, unsigned long *pflags)
639 {
640         unsigned short func = (unsigned short)(*peax);
641         int retval = 0;
642         unsigned short devid, vendorid, devfn;
643         short devindex;         /* Use short to get rid of garbage in upper half of 32-bit register */
644         unsigned char bus;
645         device_t dev;
646
647         switch (func) {
648         case CHECK:
649                 *pedx = 0x4350;
650                 *pecx = 0x2049;
651                 retval = 0;
652                 break;
653         case FINDDEV:
654                 {
655                         devid = *pecx;
656                         vendorid = *pedx;
657                         devindex = *pesi;
658                         dev = 0;
659                         while ((dev = dev_find_device(vendorid, devid, dev))) {
660                                 if (devindex <= 0)
661                                         break;
662                                 devindex--;
663                         }
664                         if (dev) {
665                                 unsigned short busdevfn;
666                                 *peax = 0;
667                                 // busnum is an unsigned char;
668                                 // devfn is an int, so we mask it off. 
669                                 busdevfn = (dev->bus->secondary << 8)
670                                     | (dev->path.pci.devfn & 0xff);
671                                 printk_debug("0x%x: return 0x%x\n", func,
672                                              busdevfn);
673                                 *pebx = busdevfn;
674                                 retval = 0;
675                         } else {
676                                 *peax = PCIBIOS_NODEV;
677                                 retval = -1;
678                         }
679                 }
680                 break;
681         case READCONFDWORD:
682         case READCONFWORD:
683         case READCONFBYTE:
684         case WRITECONFDWORD:
685         case WRITECONFWORD:
686         case WRITECONFBYTE:
687                 {
688                         unsigned long dword;
689                         unsigned short word;
690                         unsigned char byte;
691                         unsigned char reg;
692
693                         devfn = *pebx & 0xff;
694                         bus = *pebx >> 8;
695                         reg = *pedi;
696                         dev = dev_find_slot(bus, devfn);
697                         if (!dev) {
698                                 printk_debug
699                                     ("0x%x: BAD DEVICE bus %d devfn 0x%x\n",
700                                      func, bus, devfn);
701                                 // idiots. the pcibios guys assumed you'd never pass a bad bus/devfn!
702                                 *peax = PCIBIOS_BADREG;
703                                 retval = -1;
704                         }
705                         switch (func) {
706                         case READCONFBYTE:
707                                 byte = pci_read_config8(dev, reg);
708                                 *pecx = byte;
709                                 break;
710                         case READCONFWORD:
711                                 word = pci_read_config16(dev, reg);
712                                 *pecx = word;
713                                 break;
714                         case READCONFDWORD:
715                                 dword = pci_read_config32(dev, reg);
716                                 *pecx = dword;
717                                 break;
718                         case WRITECONFBYTE:
719                                 byte = *pecx;
720                                 pci_write_config8(dev, reg, byte);
721                                 break;
722                         case WRITECONFWORD:
723                                 word = *pecx;
724                                 pci_write_config16(dev, reg, word);
725                                 break;
726                         case WRITECONFDWORD:
727                                 dword = *pecx;
728                                 pci_write_config32(dev, reg, dword);
729                                 break;
730                         }
731
732                         if (retval)
733                                 retval = PCIBIOS_BADREG;
734                         printk_debug
735                             ("0x%x: bus %d devfn 0x%x reg 0x%x val 0x%lx\n",
736                              func, bus, devfn, reg, *pecx);
737                         *peax = 0;
738                         retval = 0;
739                 }
740                 break;
741         default:
742                 printk_err("UNSUPPORTED PCIBIOS FUNCTION 0x%x\n", func);
743                 break;
744         }
745
746         return retval;
747 }
748
749 int handleint21(unsigned long *edi, unsigned long *esi, unsigned long *ebp,
750                 unsigned long *esp, unsigned long *ebx, unsigned long *edx,
751                 unsigned long *ecx, unsigned long *eax, unsigned long *flags)
752 {
753         int res = -1;
754         switch (*eax & 0xffff) {
755         case 0x5f19:
756                 break;
757         case 0x5f18:
758                 *eax = 0x5f;
759                 *ebx = 0x545;   // MCLK = 133, 32M frame buffer, 256 M main memory
760                 *ecx = 0x060;
761                 res = 0;
762                 break;
763         case 0x5f00:
764                 *eax = 0x8600;
765                 break;
766         case 0x5f01:
767                 *eax = 0x5f;
768                 *ecx = (*ecx & 0xffffff00) | 2; // panel type =  2 = 1024 * 768
769                 res = 0;
770                 break;
771         case 0x5f02:
772                 *eax = 0x5f;
773                 *ebx = (*ebx & 0xffff0000) | 2;
774                 *ecx = (*ecx & 0xffff0000) | 0x401;     // PAL + crt only 
775                 *edx = (*edx & 0xffff0000) | 0; // TV Layout - default
776                 res = 0;
777                 break;
778         case 0x5f0f:
779                 *eax = 0x860f;
780                 break;
781         }
782         return res;
783 }