Import upstream u-boot 1.1.4
[u-boot.git] / board / MAI / bios_emulator / scitech / src / common / gaos2.c
1 /****************************************************************************
2 *
3 *                   SciTech Nucleus Graphics Architecture
4 *
5 *               Copyright (C) 1991-1998 SciTech Software, Inc.
6 *                            All rights reserved.
7 *
8 *  ======================================================================
9 *  |REMOVAL OR MODIFICATION OF THIS HEADER IS STRICTLY PROHIBITED BY LAW|
10 *  |                                                                    |
11 *  |This copyrighted computer code contains proprietary technology      |
12 *  |owned by SciTech Software, Inc., located at 505 Wall Street,        |
13 *  |Chico, CA 95928 USA (http://www.scitechsoft.com).                   |
14 *  |                                                                    |
15 *  |The contents of this file are subject to the SciTech Nucleus        |
16 *  |License; you may *not* use this file or related software except in  |
17 *  |compliance with the License. You may obtain a copy of the License   |
18 *  |at http://www.scitechsoft.com/nucleus-license.txt                   |
19 *  |                                                                    |
20 *  |Software distributed under the License is distributed on an         |
21 *  |"AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or      |
22 *  |implied. See the License for the specific language governing        |
23 *  |rights and limitations under the License.                           |
24 *  |                                                                    |
25 *  |REMOVAL OR MODIFICATION OF THIS HEADER IS STRICTLY PROHIBITED BY LAW|
26 *  ======================================================================
27 *
28 * Language:     ANSI C
29 * Environment:  OS/2 32-bit
30 *
31 * Description:  OS specific Nucleus Graphics Architecture services for
32 *               the OS/2 operating system environments.
33 *
34 ****************************************************************************/
35
36 #include "pm_help.h"
37 #define INCL_DOSERRORS
38 #define INCL_DOS
39 #define INCL_SUB
40 #define INCL_VIO
41 #define INCL_KBD
42 #include <os2.h>
43
44 /*--------------------------- Global variables ----------------------------*/
45
46 static ibool    haveRDTSC = false;
47 static ulong    parms[3];       /* Must not cross 64Kb boundary!    */
48 static ulong    result[4];      /* Must not cross 64Kb boundary!    */
49
50 /*-------------------------- Implementation -------------------------------*/
51
52 /****************************************************************************
53 PARAMETERS:
54 func        - Helper device driver function to call
55
56 RETURNS:
57 First return value from the device driver in parmsOut[0]
58
59 REMARKS:
60 Function to open our helper device driver, call it and close the file
61 handle. Note that we have to open the device driver for every call because
62 of two problems:
63
64  1. We cannot open a single file handle in a DLL that is shared amongst
65     programs, since every process must have it's own open file handle.
66
67  2. For some reason there appears to be a limit of about 12 open file
68     handles on a device driver in the system. Hence when we open more
69     than about 12 file handles things start to go very strange.
70
71 Hence we simply open the file handle every time that we need to call the
72 device driver to work around these problems.
73 ****************************************************************************/
74 static ulong CallSDDHelp(
75     int func)
76 {
77     static ulong    inLen;          /* Must not cross 64Kb boundary!    */
78     static ulong    outLen;         /* Must not cross 64Kb boundary!    */
79     HFILE           hSDDHelp;
80
81     /* If this code in here fails, we are screwed! Many of our drivers
82      * use this code and don't have a C library, so we simply assume we
83      * can't fail here.
84      */
85     DosOpen(PMHELP_NAME,&hSDDHelp,&result[0],0,0,
86             FILE_OPEN, OPEN_SHARE_DENYNONE | OPEN_ACCESS_READWRITE,
87             NULL);
88     DosDevIOCtl(hSDDHelp,PMHELP_IOCTL,func,
89              &parms, inLen = sizeof(parms), &inLen,
90             &result, outLen = sizeof(result), &outLen);
91     DosClose(hSDDHelp);
92     return result[0];
93 }
94
95 /****************************************************************************
96 PARAMETERS:
97 path    - Local path to the Nucleus driver files.
98
99 REMARKS:
100 This function is used by the application program to override the location
101 of the Nucleus driver files that are loaded. Normally the loader code
102 will look in the system Nucleus directories first, then in the 'drivers'
103 directory relative to the current working directory, and finally relative
104 to the MGL_ROOT environment variable.
105 ****************************************************************************/
106 void NAPI GA_setLocalPath(
107     const char *path)
108 {
109     PM_setLocalBPDPath(path);
110 }
111
112 /****************************************************************************
113 RETURNS:
114 Pointer to the system wide PM library imports, or the internal version if none
115
116 REMARKS:
117 For OS/2 we don't need to do anything special because Nucleus is always
118 loaded via the shared SDDPMI driver when SDD is loaded so we don't need
119 a system wide PM library imports function.
120 ****************************************************************************/
121 PM_imports * NAPI GA_getSystemPMImports(void)
122 {
123     return &_PM_imports;
124 }
125
126 /****************************************************************************
127 PARAMETERS:
128 gaExp   - Place to store the exported functions
129 shared  - True if connecting to the shared, global Nucleus driver
130
131 REMARKS:
132 For OS/2 if SDD is loaded we *always* connect to the shared Nucleus functions
133 contained within the SDDPMI driver. This allows the Nucleus functions contained
134 within this driver to be utilised by all Nucleus apps in the system and
135 maintains a consistent state between versions.
136 ****************************************************************************/
137 ibool NAPI GA_getSharedExports(
138     GA_exports *gaExp,
139     ibool shared)
140 {
141     /* In test harness mode, we need to load a local copy of Nucleus */
142 #if !defined (TEST_HARNESS) || defined (DEBUG_SDDPMI)
143     HMODULE     hModSDDPMI;
144     char        buf[80];
145     GA_exports  *exp;
146
147     /* Initialise the PM library and connect to our runtime DLL's */
148     PM_init();
149     if (CallSDDHelp(PMHELP_GETSHAREDEXP) != 0) {
150         /* We have found the shared Nucleus exports. Because not all processes
151          * map to SDDPMI.DLL, we need to ensure that we connect to this
152          * DLL so that it gets mapped into our address space (that is
153          * where the shared Nucleus loader code is located). Simply doing a
154          * DosLoadModule on it is enough for this.
155          */
156         DosLoadModule((PSZ)buf,sizeof(buf),(PSZ)"SDDPMI.DLL",&hModSDDPMI);
157         exp = (GA_exports*)result[0];
158         memcpy(gaExp,exp,MIN(gaExp->dwSize,exp->dwSize));
159         return true;
160         }
161 #endif
162     (void)shared;
163     return false;
164 }
165
166 #ifndef TEST_HARNESS
167 /****************************************************************************
168 REMARKS:
169 Nothing special for this OS
170 ****************************************************************************/
171 ibool NAPI GA_queryFunctions(
172     GA_devCtx *dc,
173     N_uint32 id,
174     void _FAR_ *funcs)
175 {
176     return __GA_exports.GA_queryFunctions(dc,id,funcs);
177 }
178
179 /****************************************************************************
180 REMARKS:
181 Nothing special for this OS
182 ****************************************************************************/
183 ibool NAPI REF2D_queryFunctions(
184     REF2D_driver *ref2d,
185     N_uint32 id,
186     void _FAR_ *funcs)
187 {
188     return __GA_exports.REF2D_queryFunctions(ref2d,id,funcs);
189 }
190 #endif
191
192 /****************************************************************************
193 REMARKS:
194 This function initialises the high precision timing functions for the
195 Nucleus loader library.
196 ****************************************************************************/
197 ibool NAPI GA_TimerInit(void)
198 {
199     if (_GA_haveCPUID() && (_GA_getCPUIDFeatures() & CPU_HaveRDTSC) != 0)
200         haveRDTSC = true;
201     return true;
202 }
203
204 /****************************************************************************
205 REMARKS:
206 This function reads the high resolution timer.
207 ****************************************************************************/
208 void NAPI GA_TimerRead(
209     GA_largeInteger *value)
210 {
211     if (haveRDTSC)
212         _GA_readTimeStamp(value);
213     else
214         DosTmrQueryTime((QWORD*)value);
215 }
216
217 /****************************************************************************
218 REMARKS:
219 On OS/2, we need special memory allocation functions if we build SDDPMI in
220 test harness mode. But if we build GATest etc. in test mode, we want to use
221 the normal C runtime functions, so route them back here.
222 ****************************************************************************/
223
224 #if defined (TEST_HARNESS) && !defined (DEBUG_SDDPMI)
225
226 /* Undefine these macros first or we'll recurse to hell! */
227 #undef malloc
228 #undef calloc
229 #undef realloc
230 #undef free
231
232 void *SDDPMI_malloc(size_t size) {
233     return malloc(size);
234 }
235
236 void *SDDPMI_calloc(size_t num, size_t size) {
237     return calloc(num, size);
238 }
239
240 void SDDPMI_free(void *ptr) {
241     free(ptr);
242 }
243
244 void *SDDPMI_realloc(void *ptr, size_t size) {
245     return realloc(ptr, size);
246 }
247
248 #endif