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