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