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