Import upstream u-boot 1.1.4
[u-boot.git] / board / MAI / bios_emulator / scitech / src / pm / dos / vflat.c
1 /****************************************************************************
2 *
3 *                   SciTech OS Portability Manager Library
4 *
5 *  ========================================================================
6 *
7 *    The contents of this file are subject to the SciTech MGL Public
8 *    License Version 1.0 (the "License"); you may not use this file
9 *    except in compliance with the License. You may obtain a copy of
10 *    the License at http://www.scitechsoft.com/mgl-license.txt
11 *
12 *    Software distributed under the License is distributed on an
13 *    "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14 *    implied. See the License for the specific language governing
15 *    rights and limitations under the License.
16 *
17 *    The Original Code is Copyright (C) 1991-1998 SciTech Software, Inc.
18 *
19 *    The Initial Developer of the Original Code is SciTech Software, Inc.
20 *    All Rights Reserved.
21 *
22 *  ========================================================================
23 *
24 * Language:     ANSI C
25 * Environment:  32-bit DOS
26 *
27 * Description:  Main C module for the VFlat framebuffer routines. The page
28 *               fault handler is always installed to handle up to a 4Mb
29 *               framebuffer with a window size of 4Kb or 64Kb in size.
30 *
31 ****************************************************************************/
32
33 #include "pmapi.h"
34 #include <stdlib.h>
35 #include <dos.h>
36
37 /*-------------------------------------------------------------------------*/
38 /* DOS4G/W, PMODE/W and CauseWay support.                                  */
39 /*-------------------------------------------------------------------------*/
40
41 #if defined(DOS4GW)
42
43 #define VFLAT_START_ADDR    0xF0000000U
44 #define VFLAT_END_ADDR      0xF03FFFFFU
45 #define VFLAT_LIMIT         (VFLAT_END_ADDR - VFLAT_START_ADDR)
46 #define PAGE_PRESENT        1
47 #define PAGE_NOTPRESENT     0
48 #define PAGE_READ           0
49 #define PAGE_WRITE          2
50
51 PRIVATE ibool   installed = false;
52 PRIVATE ibool   haveDPMI = false;
53 PUBLIC  ibool   _ASMAPI VF_haveCauseWay = false;
54 PUBLIC  uchar * _ASMAPI VF_zeroPtr = NULL;
55
56 /* Low level assembler code */
57
58 int     _ASMAPI InitPaging(void);
59 void    _ASMAPI ClosePaging(void);
60 void    _ASMAPI MapPhysical2Linear(ulong pAddr, ulong lAddr, int pages, int flags);
61 void    _ASMAPI InstallFaultHandler(ulong baseAddr,int bankSize);
62 void    _ASMAPI RemoveFaultHandler(void);
63 void    _ASMAPI InstallBankFunc(int codeLen,void *bankFunc);
64
65 void * _ASMAPI VF_malloc(uint size)
66 { return PM_malloc(size); }
67
68 void _ASMAPI VF_free(void *p)
69 { PM_free(p); }
70
71 PRIVATE ibool CheckDPMI(void)
72 /****************************************************************************
73 *
74 * Function:     CheckDPMI
75 * Returns:      True if we are running under DPMI
76 *
77 ****************************************************************************/
78 {
79     PMREGS  regs;
80
81     if (haveDPMI)
82         return true;
83
84     /* Check if we are running under DPMI in which case we will not be
85      * able to install our page fault handlers. We can however use the
86      * DVA.386 or VFLATD.386 virtual device drivers if they are present.
87      */
88     regs.x.ax = 0xFF00;
89     PM_int386(0x31,&regs,&regs);
90     if (!regs.x.cflag && (regs.e.edi & 8))
91         return (haveDPMI = true);
92     return false;
93 }
94
95 ibool PMAPI VF_available(void)
96 /****************************************************************************
97 *
98 * Function:     VF_available
99 * Returns:      True if virtual buffer is available, false if not.
100 *
101 ****************************************************************************/
102 {
103     if (!VF_zeroPtr)
104         VF_zeroPtr = PM_mapPhysicalAddr(0,0xFFFFFFFF,true);
105     if (CheckDPMI())
106         return false;
107
108     /* Standard DOS4GW, PMODE/W and Causeway */
109     if (InitPaging() == -1)
110         return false;
111     ClosePaging();
112     return true;
113 }
114
115 void * PMAPI InitDPMI(ulong baseAddr,int bankSize,int codeLen,void *bankFunc)
116 /****************************************************************************
117 *
118 * Function:     InitDOS4GW
119 * Parameters:   baseAddr    - Base address of framebuffer bank window
120 *               bankSize    - Physical size of banks in Kb (4 or 64)
121 *               codeLen     - Length of 32 bit bank switch function
122 *               bankFunc    - Pointer to protected mode bank function
123 * Returns:      Near pointer to virtual framebuffer, or NULL on failure.
124 *
125 * Description:  Installs the virtual linear framebuffer handling for
126 *               DPMI environments. This requires the DVA.386 or VFLATD.386
127 *               virtual device drivers to be installed and functioning.
128 *
129 ****************************************************************************/
130 {
131     (void)baseAddr;
132     (void)bankSize;
133     (void)codeLen;
134     (void)bankFunc;
135     return NULL;
136 }
137
138 void * PMAPI InitDOS4GW(ulong baseAddr,int bankSize,int codeLen,void *bankFunc)
139 /****************************************************************************
140 *
141 * Function:     InitDOS4GW
142 * Parameters:   baseAddr    - Base address of framebuffer bank window
143 *               bankSize    - Physical size of banks in Kb (4 or 64)
144 *               codeLen     - Length of 32 bit bank switch function
145 *               bankFunc    - Pointer to protected mode bank function
146 * Returns:      Near pointer to virtual framebuffer, or NULL on failure.
147 *
148 * Description:  Installs the virtual linear framebuffer handling for
149 *               the DOS4GW extender.
150 *
151 ****************************************************************************/
152 {
153     int     i;
154
155     if (InitPaging() == -1)
156         return NULL;            /* Cannot do hardware paging!       */
157
158     /* Map 4MB of video memory into linear address space (read/write) */
159     if (bankSize == 64) {
160         for (i = 0; i < 64; i++) {
161             MapPhysical2Linear(baseAddr,VFLAT_START_ADDR+(i<<16),16,
162                 PAGE_WRITE | PAGE_NOTPRESENT);
163             }
164         }
165     else {
166         for (i = 0; i < 1024; i++) {
167             MapPhysical2Linear(baseAddr,VFLAT_START_ADDR+(i<<12),1,
168                 PAGE_WRITE | PAGE_NOTPRESENT);
169             }
170         }
171
172     /* Install our page fault handler and banks switch function */
173     InstallFaultHandler(baseAddr,bankSize);
174     InstallBankFunc(codeLen,bankFunc);
175     installed = true;
176     return (void*)VFLAT_START_ADDR;
177 }
178
179 void * PMAPI VF_init(ulong baseAddr,int bankSize,int codeLen,void *bankFunc)
180 /****************************************************************************
181 *
182 * Function:     VF_init
183 * Parameters:   baseAddr    - Base address of framebuffer bank window
184 *               bankSize    - Physical size of banks in Kb (4 or 64)
185 *               codeLen     - Length of 32 bit bank switch function
186 *               bankFunc    - Pointer to protected mode bank function
187 * Returns:      Near pointer to virtual framebuffer, or NULL on failure.
188 *
189 * Description:  Installs the virtual linear framebuffer handling.
190 *
191 ****************************************************************************/
192 {
193     if (installed)
194         return (void*)VFLAT_START_ADDR;
195     if (codeLen > 100)
196         return NULL;                /* Bank function is too large!      */
197     if (!VF_zeroPtr)
198         VF_zeroPtr = PM_mapPhysicalAddr(0,0xFFFFFFFF,true);
199     if (CheckDPMI())
200         return InitDPMI(baseAddr,bankSize,codeLen,bankFunc);
201     return InitDOS4GW(baseAddr,bankSize,codeLen,bankFunc);
202 }
203
204 void PMAPI VF_exit(void)
205 /****************************************************************************
206 *
207 * Function:     VF_exit
208 *
209 * Description:  Closes down the virtual framebuffer services and
210 *               restores the previous page fault handler.
211 *
212 ****************************************************************************/
213 {
214     if (installed) {
215         if (haveDPMI) {
216             /* DPMI support */
217             }
218         else {
219             /* Standard DOS4GW and PMODE/W support */
220             RemoveFaultHandler();
221             ClosePaging();
222             }
223         installed = false;
224         }
225 }
226
227 /*-------------------------------------------------------------------------*/
228 /* Support mapped out for other compilers.                                 */
229 /*-------------------------------------------------------------------------*/
230
231 #else
232
233 ibool PMAPI VF_available(void)
234 {
235     return false;
236 }
237
238 void * PMAPI VF_init(ulong baseAddr,int bankSize,int codeLen,void *bankFunc)
239 {
240     (void)baseAddr;
241     (void)bankSize;
242     (void)codeLen;
243     (void)bankFunc;
244     return NULL;
245 }
246
247 void PMAPI VF_exit(void)
248 {
249 }
250
251 #endif