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