6954230d5588730871c31649533af3eab60f632e
[coreboot.git] / src / mainboard / via / epia-m / 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 #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 core coreboot 
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                 /* Setup a stack */
180                 "       mov     $0x0, %ax       \n"
181                 "       mov     %ax, %ss        \n"
182                 "       movl    $0x1000, %eax   \n"
183                 "       movl    %eax, %esp      \n"
184
185                 /* Load our 16 it idt */
186                 "       xor     %ax, %ax        \n"
187                 "       mov     %ax, %ds        \n"
188                 "       lidt    __myidt         \n"
189
190                 /* Dump zeros in the other segregs */
191                 "       mov     %ax, %es        \n"
192                 "       mov     %ax, %fs        \n"
193                 "       mov     %ax, %gs        \n"
194                 "       mov     $0x40, %ax      \n"
195                 "       mov     %ax, %ds        \n"
196                 "       mov     %cx, %ax        \n"
197
198                 /* run VGA BIOS at 0xc000:0003 */
199                 "       lcall   $0xc000, $0x0003\n"
200
201                 /* if we got here, just about done. 
202                  * Need to get back to protected mode */
203                 "       movl    %cr0, %eax      \n"
204                 "       orl     $0x0000001, %eax\n" /* PE = 1 */
205                 "       movl    %eax, %cr0      \n"
206
207                 /* Now that we are in protected mode jump to a 32 bit code segment. */
208                 "       data32  ljmp    $0x10, $vgarestart\n"
209                 "vgarestart:\n"
210                 "       .code32\n"
211                 "       movw    $0x18, %ax      \n"
212                 "       mov     %ax, %ds        \n"
213                 "       mov     %ax, %es        \n"
214                 "       mov     %ax, %fs        \n"
215                 "       mov     %ax, %gs        \n"
216                 "       mov     %ax, %ss        \n"
217
218                 /* restore proper gdt and idt */
219                 "       lgdt    %cs:gdtarg      \n"
220                 "       lidt    idtarg          \n"
221
222                 ".globl vga_exit                \n"
223                 "vga_exit:                      \n"
224                 "       mov     __stack, %esp   \n"
225                 "       popal                   \n"
226                 );
227 }
228
229 __asm__ (".text\n""real_mode_switch_end:\n");
230 extern char real_mode_switch_end[];
231
232 /* call vga bios int 10 function 0x4f14 to enable main console 
233    epia-m does not always autosence the main console so forcing it on is good !! */ 
234 void vga_enable_console()
235 {
236         __asm__ __volatile__ (
237                 /* paranoia -- does ecx get saved? not sure. This is 
238                  * the easiest safe thing to do. */
239                 "       pushal                  \n"
240                 /* save the stack */
241                 "       mov     %esp, __stack   \n"
242
243                 /* load 'our' gdt */
244                 "       lgdt    %cs:__mygdtaddr \n"
245
246                 /*  This configures CS properly for real mode. */
247                 "       ljmp    $0x28, $__vga_ec_16bit\n"
248                 "__vga_ec_16bit:                \n"
249                 "       .code16                 \n"
250                 /* 16 bit code from here on... */
251
252                 /* Load the segment registers w/ properly configured segment
253                  * descriptors.  They will retain these configurations (limits,
254                  * writability, etc.) once protected mode is turned off. */
255                 "       mov     $0x30, %ax      \n"
256                 "       mov     %ax, %ds        \n"
257                 "       mov     %ax, %es        \n"
258                 "       mov     %ax, %fs        \n"
259                 "       mov     %ax, %gs        \n"
260                 "       mov     %ax, %ss        \n"
261
262                 /* Turn off protection (bit 0 in CR0) */
263                 "       movl    %cr0, %eax      \n"
264                 "       andl    $0xFFFFFFFE, %eax\n"
265                 "       movl    %eax, %cr0      \n"
266
267                 /* Now really going into real mode */
268                 "       ljmp    $0, $__vga_ec_real \n"
269                 "__vga_ec_real:                  \n"
270
271                 /* put the stack at the end of page zero. 
272                  * that way we can easily share it between real and protected, 
273                  * since the 16-bit ESP at segment 0 will work for any case. 
274                 /* Setup a stack */
275                 "       mov     $0x0, %ax       \n"
276                 "       mov     %ax, %ss        \n"
277                 "       movl    $0x1000, %eax   \n"
278                 "       movl    %eax, %esp      \n"
279
280                 /* debugging for RGM */
281                 "       mov     $0x11, %al      \n"
282                 "       outb    %al, $0x80      \n"
283
284                 /* Load our 16 it idt */
285                 "       xor     %ax, %ax        \n"
286                 "       mov     %ax, %ds        \n"
287                 "       lidt    __myidt         \n"
288
289                 /* Dump zeros in the other segregs */
290                 "       mov     %ax, %ds        \n"
291                 "       mov     %ax, %es        \n"
292                 "       mov     %ax, %fs        \n"
293                 "       mov     %ax, %gs        \n"
294
295                 /* ask bios to enable main console */
296                 /* set up for int 10 call - values found from X server
297                  * bios call routines */
298                 "       movw    $0x4f14,%ax     \n"
299                 "       movw    $0x8003,%bx     \n"
300                 "       movw    $1, %cx         \n"
301                 "       movw    $0, %dx         \n"
302                 "       movw    $0, %di         \n"
303                 "       int     $0x10           \n"
304
305                 "       movb    $0x55, %al      \n"
306                 "       outb    %al, $0x80      \n"
307
308                 /* if we got here, just about done. 
309                  * Need to get back to protected mode */
310                 "       movl    %cr0, %eax      \n"
311                 "       orl     $0x0000001, %eax\n" /* PE = 1 */
312                 "       movl    %eax, %cr0      \n"
313
314                 /* Now that we are in protected mode jump to a 32 bit code segment. */
315                 "       data32  ljmp    $0x10, $vga_ec_restart\n"
316                 "vga_ec_restart:\n"
317                 "       .code32\n"
318                 "       movw    $0x18, %ax      \n"
319                 "       mov     %ax, %ds        \n"
320                 "       mov     %ax, %es        \n"
321                 "       mov     %ax, %fs        \n"
322                 "       mov     %ax, %gs        \n"
323                 "       mov     %ax, %ss        \n"
324
325                 /* restore proper gdt and idt */
326                 "       lgdt    %cs:gdtarg      \n"
327                 "       lidt    idtarg          \n"
328                 "       .globl  vga__ec_exit    \n"
329                 "vga_ec_exit:\n"
330                 "       mov     __stack, %esp   \n"
331                 "       popal\n"
332                 );
333 }
334
335 void do_vgabios(void)
336 {
337         device_t dev;
338         unsigned long busdevfn;
339         unsigned int rom = 0;
340         unsigned char *buf;
341         unsigned int size = 64*1024;
342         int i;
343         
344         /* clear vga bios data area */
345         for (i = 0x400; i < 0x500; i++) {
346                 *(unsigned char *) i = 0;
347         }
348
349         dev = dev_find_class(PCI_CLASS_DISPLAY_VGA<<8 , 0);
350
351         if (!dev) {
352                 printk_debug("NO VGA FOUND\n");
353                 return;
354         }
355         printk_debug("found VGA: vid=%x, did=%x\n", dev->vendor, dev->device);
356
357         /* declare rom address here - keep any config data out of the way
358          * of core LXB stuff */
359
360         rom = (unsigned int)cbfs_load_optionrom(dev->vendor, dev->device, 0); 
361         pci_write_config32(dev, PCI_ROM_ADDRESS, rom|1);
362         printk_debug("rom base, size: %x\n", rom);
363
364         buf = (unsigned char *) rom;
365         if ((buf[0] == 0x55) && (buf[1] == 0xaa)) {
366                 memcpy((void *) 0xc0000, buf, size);
367
368                 write_protect_vgabios();  // in northbridge
369
370                 // check signature again
371                 buf = (unsigned char *) 0xc0000;
372                 if (buf[0]==0x55 && buf[1]==0xAA) {
373                         busdevfn = (dev->bus->secondary << 8) | dev->path.pci.devfn;
374                         printk_debug("bus/devfn = %#x\n", busdevfn);
375
376                         real_mode_switch_call_vga(busdevfn);
377                 } else
378                         printk_debug("Failed to copy VGA BIOS to 0xc0000\n");
379         } else 
380                 printk_debug("BAD SIGNATURE 0x%x 0x%x\n", buf[0], buf[1]);
381
382         pci_write_config32(dev, PCI_ROM_ADDRESS, 0);
383 }
384
385
386 // we had hoped to avoid this. 
387 // this is a stub IDT only. It's main purpose is to ignore calls 
388 // to the BIOS. 
389 // no longer. Dammit. We have to respond to these.
390 struct realidt {
391         unsigned short offset, cs;
392 }; 
393
394 // from a handy writeup that andrey found.
395
396 // handler. 
397 // There are some assumptions we can make here. 
398 // First, the Top Of Stack (TOS) is located on the top of page zero. 
399 // we can share this stack between real and protected mode. 
400 // that simplifies a lot of things ...
401 // we'll just push all the registers on the stack as longwords, 
402 // and pop to protected mode. 
403 // second, since this only ever runs as part of coreboot, 
404 // we know all the segment register values -- so we don't save any.
405 // keep the handler that calls things small. It can do a call to 
406 // more complex code in coreboot itself. This helps a lot as we don't
407 // have to do address fixup in this little stub, and calls are absolute
408 // so the handler is relocatable.
409 void handler(void)
410 {
411         __asm__ __volatile__ ( 
412                 "       .code16         \n"
413                 "idthandle:             \n"
414                 "       pushal          \n"
415                 "       movb    $0, %al \n"
416                 "       ljmp    $0, $callbiosint16\n"
417                 "end_idthandle:         \n"
418                 "       .code32         \n"
419                 );
420 }
421
422 void debughandler(void)
423 {
424         __asm__ __volatile__ ( 
425                 "       .code16         \n"
426                 "debughandle:           \n"
427                 "       pushw   %cx     \n"
428                 "       movw    $250, %cx \n"
429                 "dbh1:                  \n"
430                 "       loop    dbh1    \n"
431                 "       popw    %cx     \n"
432                 "       iret            \n"
433                 "end_debughandle:       \n"
434                 ".code32                \n"
435                 );
436 }
437
438 // Calling conventions. The first C function is called with this stuff
439 // on the stack. They look like value parameters, but note that if you
440 // modify them they will go back to the INTx function modified. 
441 // the C function will call the biosint function with these as
442 // REFERENCE parameters. In this way, we can easily get 
443 // returns back to the INTx caller (i.e. vgabios)
444 void callbiosint(void)
445 {
446         __asm__ __volatile__ (
447                 "       .code16         \n"
448                 "callbiosint16:         \n"
449                 "       push    %ds     \n"
450                 "       push    %es     \n"
451                 "       push    %fs     \n"
452                 "       push    %gs     \n"
453                 // clean up the int #. To save space we put it in the lower
454                 // byte. But the top 24 bits are junk. 
455                 "       andl    $0xff, %eax\n"
456                 // this push does two things:
457                 // - put the INT # on the stack as a parameter
458                 // - provides us with a temp for the %cr0 mods.
459                 "       pushl   %eax    \n"
460                 "       movl    %cr0, %eax\n"
461                 "       orl     $0x00000001, %eax\n" /* PE = 1 */
462                 "       movl    %eax, %cr0\n"
463                 /* Now that we are in protected mode jump to a 32 bit code segment. */
464                 "       data32  ljmp    $0x10, $biosprotect\n"
465                 "biosprotect:           \n"
466                 "       .code32         \n"
467                 "       movw    $0x18, %ax          \n"
468                 "       mov     %ax, %ds          \n"
469                 "       mov     %ax, %es          \n"
470                 "       mov     %ax, %fs          \n"
471                 "       mov     %ax, %gs          \n"
472                 "       mov     %ax, %ss          \n"
473                 "       lidt    idtarg         \n"
474                 "       call    biosint \n"
475                 // back to real mode ...
476                 "       ljmp    $0x28, $__rms_16bit2\n"
477                 "__rms_16bit2:                  \n"
478                 "       .code16                 \n"
479                 /* 16 bit code from here on... */
480                 /* Load the segment registers w/ properly configured segment
481                  * descriptors.  They will retain these configurations (limits,
482                  * writability, etc.) once protected mode is turned off. */
483                 "       mov     $0x30, %ax      \n"
484                 "       mov     %ax, %ds        \n"
485                 "       mov     %ax, %es        \n"
486                 "       mov     %ax, %fs        \n"
487                 "       mov     %ax, %gs        \n"
488                 "       mov     %ax, %ss        \n"
489                 
490                 /* Turn off protection (bit 0 in CR0) */
491                 "       movl    %cr0, %eax              \n"
492                 "       andl    $0xFFFFFFFE, %eax       \n"
493                 "       movl    %eax, %cr0              \n"
494
495                 /* Now really going into real mode */
496                 "       ljmp $0,  $__rms_real2  \n"
497                 "__rms_real2:                   \n"
498
499                 /* Setup a stack
500                  * FixME: where is esp? */
501                 "       mov     $0x0, %ax       \n"
502                 "       mov     %ax, %ss        \n"
503
504                 /* ebugging for RGM */
505                 "       mov     $0x11, %al      \n"
506                 "       outb    %al, $0x80      \n"
507
508                 /* Load our 16 it idt */
509                 "       xor     %ax, %ax        \n"
510                 "       mov     %ax, %ds        \n"
511                 "       lidt    __myidt         \n"
512
513                 /* Dump zeros in the other segregs */
514                 "       mov     %ax, %es        \n"
515                 "       mov     %ax, %fs        \n"
516                 "       mov     %ax, %gs        \n"
517                 "       mov     $0x40, %ax      \n"
518                 "       mov     %ax, %ds        \n"
519
520                 /* pop the INT # that you pushed earlier */
521                 "       popl    %eax            \n"
522                 "       pop     %gs             \n"
523                 "       pop     %fs             \n"
524                 "       pop     %es             \n"
525                 "       pop     %ds             \n"
526                 "       popal                   \n"
527                 "       iret                    \n"
528                 "       .code32                 \n"
529                 );
530 }
531
532 enum {
533         PCIBIOS = 0x1a, 
534         MEMSIZE = 0x12
535 };
536
537 int pcibios(unsigned long *pedi, unsigned long *pesi, unsigned long *pebp,
538             unsigned long *pesp, unsigned long *pebx, unsigned long *pedx,
539             unsigned long *pecx, unsigned long *peax, unsigned long *pflags);
540
541 int handleint21(unsigned long *pedi, unsigned long *pesi, unsigned long *pebp,
542                 unsigned long *pesp, unsigned long *pebx, unsigned long *pedx,
543                 unsigned long *pecx, unsigned long *peax, unsigned long *pflags
544         );
545
546 extern void vga_exit(void);
547
548 int biosint(unsigned long intnumber,
549             unsigned long gsfs, unsigned long dses,
550             unsigned long edi, unsigned long esi,
551             unsigned long ebp, unsigned long esp, 
552             unsigned long ebx, unsigned long edx, 
553             unsigned long ecx, unsigned long eax, 
554             unsigned long cs_ip, unsigned short stackflags)
555 {
556         unsigned long ip; 
557         unsigned long cs; 
558         unsigned long flags;
559         int ret = -1;
560         
561         ip = cs_ip & 0xffff;
562         cs = cs_ip >> 16;
563         flags = stackflags;
564         
565         printk_debug("biosint: INT# 0x%lx\n", intnumber);
566         printk_debug("biosint: eax 0x%lx ebx 0x%lx ecx 0x%lx edx 0x%lx\n", 
567                       eax, ebx, ecx, edx);
568         printk_debug("biosint: ebp 0x%lx esp 0x%lx edi 0x%lx esi 0x%lx\n",
569                      ebp, esp, edi, esi);
570         printk_debug("biosint:  ip 0x%x   cs 0x%x  flags 0x%x\n",
571                      ip, cs, flags);
572
573         // cases in a good compiler are just as good as your own tables. 
574         switch (intnumber) {
575         case 0 ... 15:
576                 // These are not BIOS service, but the CPU-generated exceptions
577                 printk_info("biosint: Oops, exception %u\n", intnumber);
578                 if (esp < 0x1000) {
579                         printk_debug("Stack contents: ");
580                         while (esp < 0x1000) {
581                                 printk_debug("0x%04x ", *(unsigned short *) esp);
582                                 esp += 2;
583                         }
584                         printk_debug("\n");
585                 }
586                 printk_debug("biosint: Bailing out\n");
587                 // "longjmp"
588                 vga_exit();
589                 break;
590                 
591         case PCIBIOS:
592                 ret = pcibios( &edi, &esi, &ebp, &esp, 
593                                &ebx, &edx, &ecx, &eax, &flags);
594                 break;
595         case MEMSIZE: 
596                 // who cares. 
597                 eax = 64 * 1024;
598                 ret = 0;
599                 break;
600         case 0x15:
601                 ret=handleint21( &edi, &esi, &ebp, &esp, 
602                                 &ebx, &edx, &ecx, &eax, &flags);
603                 break;
604         default:
605                 printk_info("BIOSINT: Unsupport int #0x%x\n", 
606                             intnumber);
607                 break;
608         }
609         if (ret)
610                 flags |= 1; // carry flags
611         else
612                 flags &= ~1;
613         stackflags = flags;
614         return ret;
615
616
617
618 void setup_realmode_idt(void) 
619 {
620         extern unsigned char idthandle, end_idthandle;
621         extern unsigned char debughandle, end_debughandle;
622
623         int i;
624         struct realidt *idts = (struct realidt *) 0;
625         int codesize = &end_idthandle - &idthandle;
626         unsigned char *intbyte, *codeptr;
627         
628         // for each int, we create a customized little handler
629         // that just pushes %ax, puts the int # in %al, 
630         // then calls the common interrupt handler. 
631         // this necessitated because intel didn't know much about 
632         // architecture when they did the 8086 (it shows)
633         // (hmm do they know anymore even now :-)
634         // obviously you can see I don't really care about memory 
635         // efficiency. If I did I would probe back through the stack
636         // and get it that way. But that's really disgusting.
637         for (i = 0; i < 256; i++) {
638                 idts[i].cs = 0;
639                 codeptr = (char*) 4096 + i * codesize;
640                 idts[i].offset = (unsigned) codeptr;
641                 memcpy(codeptr, &idthandle, codesize);
642                 intbyte = codeptr + 3;
643                 *intbyte = i;
644         }
645         
646         // fixed entry points
647         
648         // VGA BIOSes tend to hardcode f000:f065 as the previous handler of
649         // int10. 
650         // calling convention here is the same as INTs, we can reuse
651         // the int entry code.
652         codeptr = (char*) 0xff065;
653         memcpy(codeptr, &idthandle, codesize);
654         intbyte = codeptr + 3;
655         *intbyte = 0x42; /* int42 is the relocated int10 */
656
657         /* debug handler - useful to set a programmable delay between instructions if the
658            TF bit is set upon call to real mode */
659         idts[1].cs = 0;
660         idts[1].offset = 16384;
661         memcpy(16384, &debughandle, &end_debughandle - &debughandle);
662
663         
664 }
665
666
667
668 enum {
669         CHECK = 0xb001,
670         FINDDEV = 0xb102,
671         READCONFBYTE = 0xb108,
672         READCONFWORD = 0xb109,
673         READCONFDWORD = 0xb10a,
674         WRITECONFBYTE = 0xb10b,
675         WRITECONFWORD = 0xb10c,
676         WRITECONFDWORD = 0xb10d
677 };
678
679 // errors go in AH. Just set these up so that word assigns
680 // will work. KISS. 
681 enum {
682         PCIBIOS_NODEV = 0x8600,
683         PCIBIOS_BADREG = 0x8700
684 };
685
686 int
687 pcibios(unsigned long *pedi, unsigned long *pesi, unsigned long *pebp, 
688         unsigned long *pesp, unsigned long *pebx, unsigned long *pedx, 
689         unsigned long *pecx, unsigned long *peax, unsigned long *pflags)
690 {
691         unsigned long edi = *pedi;
692         unsigned long esi = *pesi;
693         unsigned long ebp = *pebp;
694         unsigned long esp = *pesp;
695         unsigned long ebx = *pebx;
696         unsigned long edx = *pedx;
697         unsigned long ecx = *pecx;
698         unsigned long eax = *peax;
699         unsigned long flags = *pflags;
700         unsigned short func = (unsigned short) eax;
701         int retval = 0;
702         unsigned short devid, vendorid, devfn;
703         short devindex; /* Use short to get rid of gabage in upper half of 32-bit register */
704         unsigned char bus;
705         device_t dev;
706         
707         switch(func) {
708         case  CHECK:
709                 *pedx = 0x4350;
710                 *pecx = 0x2049;
711                 retval = 0;
712                 break;
713         case FINDDEV:
714         {
715                 devid = *pecx;
716                 vendorid = *pedx;
717                 devindex = *pesi;
718                 dev = 0;
719                 while ((dev = dev_find_device(vendorid, devid, dev))) {
720                         if (devindex <= 0)
721                                 break;
722                         devindex--;
723                 }
724                 if (dev) {
725                         unsigned short busdevfn;
726                         *peax = 0;
727                         // busnum is an unsigned char;
728                         // devfn is an int, so we mask it off. 
729                         busdevfn = (dev->bus->secondary << 8)
730                                 | (dev->path.pci.devfn & 0xff);
731                         printk_debug("0x%x: return 0x%x\n", func, busdevfn);
732                         *pebx = busdevfn;
733                         retval = 0;
734                 } else {
735                         *peax = PCIBIOS_NODEV;
736                         retval = -1;
737                 }
738         }
739         break;
740         case READCONFDWORD:
741         case READCONFWORD:
742         case READCONFBYTE:
743         case WRITECONFDWORD:
744         case WRITECONFWORD:
745         case WRITECONFBYTE:
746         {
747                 unsigned long dword;
748                 unsigned short word;
749                 unsigned char byte;
750                 unsigned char reg;
751                 
752                 devfn = *pebx & 0xff;
753                 bus = *pebx >> 8;
754                 reg = *pedi;
755                 dev = dev_find_slot(bus, devfn);
756                 if (! dev) {
757                         printk_debug("0x%x: BAD DEVICE bus %d devfn 0x%x\n", func, bus, devfn);
758                         // idiots. the pcibios guys assumed you'd never pass a bad bus/devfn!
759                         *peax = PCIBIOS_BADREG;
760                         retval = -1;
761                 }
762                 switch(func) {
763                 case READCONFBYTE:
764                         byte = pci_read_config8(dev, reg);
765                         *pecx = byte;
766                         break;
767                 case READCONFWORD:
768                         word = pci_read_config16(dev, reg);
769                         *pecx = word;
770                         break;
771                 case READCONFDWORD:
772                         dword = pci_read_config32(dev, reg);
773                         *pecx = dword;
774                         break;
775                 case WRITECONFBYTE:
776                         byte = *pecx;
777                         pci_write_config8(dev, reg, byte);
778                         break;
779                 case WRITECONFWORD:
780                         word = *pecx;
781                         pci_write_config16(dev, reg, word);
782                         break;
783                 case WRITECONFDWORD:
784                         dword = *pecx;
785                         pci_write_config32(dev, reg, dword);
786                         break;
787                 }
788                 
789                 if (retval) 
790                         retval = PCIBIOS_BADREG;
791                 printk_debug("0x%x: bus %d devfn 0x%x reg 0x%x val 0x%lx\n",
792                              func, bus, devfn, reg, *pecx);
793                 *peax = 0;
794                 retval = 0;
795         }
796         break;
797         default:
798                 printk_err("UNSUPPORTED PCIBIOS FUNCTION 0x%x\n",  func);
799                 break;
800         }
801         
802         return retval;
803
804
805 int handleint21(unsigned long *edi, unsigned long *esi, unsigned long *ebp,
806                 unsigned long *esp, unsigned long *ebx, unsigned long *edx,
807                 unsigned long *ecx, unsigned long *eax, unsigned long *flags)
808 {
809         int res=-1;
810         switch(*eax&0xffff)
811         {
812         case 0x5f19:
813                 break;
814         case 0x5f18:
815                 *eax=0x5f;
816                 *ebx=0x545; // MCLK = 133, 32M frame buffer, 256 M main memory
817                 *ecx=0x060;
818                 res=0;
819                 break;
820         case 0x5f00:
821                 *eax = 0x8600;
822                 break;
823         case 0x5f01:
824                 *eax = 0x5f;
825                 *ecx = (*ecx & 0xffffff00 ) | 2; // panel type =  2 = 1024 * 768
826                 res = 0;
827                 break;
828         case 0x5f02:
829                 *eax=0x5f;
830                 *ebx= (*ebx & 0xffff0000) | 2;
831                 *ecx= (*ecx & 0xffff0000) | 0x401;  // PAL + crt only 
832                 *edx= (*edx & 0xffff0000) | 0;  // TV Layout - default
833                 res=0;
834                 break;
835         case 0x5f0f:
836                 *eax=0x860f;
837                 break;
838         }
839         return res;
840 }