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