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