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