soc/intel/alderlake: Add ADL-P 4+4 with 28W TDP
[coreboot.git] / src / device / oprom / x86emu / sys.c
blob2f38ce582d2534f4a362578a34eadd0b8fa55eeb
1 /****************************************************************************
3 * Realmode X86 Emulator Library
5 * Copyright (C) 1996-1999 SciTech Software, Inc.
6 * Copyright (C) David Mosberger-Tang
7 * Copyright (C) 1999 Egbert Eich
9 * ========================================================================
11 * Permission to use, copy, modify, distribute, and sell this software and
12 * its documentation for any purpose is hereby granted without fee,
13 * provided that the above copyright notice appear in all copies and that
14 * both that copyright notice and this permission notice appear in
15 * supporting documentation, and that the name of the authors not be used
16 * in advertising or publicity pertaining to distribution of the software
17 * without specific, written prior permission. The authors makes no
18 * representations about the suitability of this software for any purpose.
19 * It is provided "as is" without express or implied warranty.
21 * THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
22 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
23 * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
24 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
25 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
26 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
27 * PERFORMANCE OF THIS SOFTWARE.
29 * ========================================================================
31 * Language: ANSI C
32 * Environment: Any
33 * Developer: Kendall Bennett
35 * Description: This file includes subroutines which are related to
36 * programmed I/O and memory access. Included in this module
37 * are default functions with limited usefulness. For real
38 * uses these functions will most likely be overridden by the
39 * user library.
41 ****************************************************************************/
43 #include <arch/io.h>
44 #include <x86emu/x86emu.h>
45 #include <x86emu/regs.h>
46 #include <device/oprom/include/io.h>
47 #include "debug.h"
48 #include "prim_ops.h"
50 #ifdef IN_MODULE
51 #include "xf86_ansic.h"
52 #else
53 #endif
54 /*------------------------- Global Variables ------------------------------*/
56 X86EMU_sysEnv _X86EMU_env; /* Global emulator machine state */
57 X86EMU_intrFuncs _X86EMU_intrTab[256];
59 /*----------------------------- Implementation ----------------------------*/
61 /* compute a pointer. This replaces code scattered all over the place! */
62 static u8 *mem_ptr(u32 addr, int size)
64 u8 *retaddr = 0;
66 if (addr > M.mem_size - size) {
67 DB(printf("%s: address %#x out of range!\n", __func__, addr);)
68 HALT_SYS();
70 if (addr < 0x200) {
71 //printf("%x:%x updating int vector 0x%x\n",
72 // M.x86.R_CS, M.x86.R_IP, addr >> 2);
74 retaddr = (u8 *) (M.mem_base + addr);
76 return retaddr;
79 /****************************************************************************
80 PARAMETERS:
81 addr - Emulator memory address to read
83 RETURNS:
84 Byte value read from emulator memory.
86 REMARKS:
87 Reads a byte value from the emulator memory.
88 ****************************************************************************/
89 u8 X86API rdb(u32 addr)
91 u8 val;
92 u8 *ptr;
94 ptr = mem_ptr(addr, 1);
96 val = *ptr;
97 DB(if (DEBUG_MEM_TRACE())
98 printf("%#08x 1 -> %#x\n", addr, val);)
99 return val;
102 /****************************************************************************
103 PARAMETERS:
104 addr - Emulator memory address to read
106 RETURNS:
107 Word value read from emulator memory.
109 REMARKS:
110 Reads a word value from the emulator memory.
111 ****************************************************************************/
112 u16 X86API rdw(u32 addr)
114 u16 val = 0;
115 u8 *ptr;
117 ptr = mem_ptr(addr, 2);
118 val = *(u16 *) (ptr);
120 DB(if (DEBUG_MEM_TRACE())
121 printf("%#08x 2 -> %#x\n", addr, val);)
122 return val;
125 /****************************************************************************
126 PARAMETERS:
127 addr - Emulator memory address to read
129 RETURNS:
130 Long value read from emulator memory.
131 REMARKS:
132 Reads a long value from the emulator memory.
133 ****************************************************************************/
134 u32 X86API rdl(u32 addr)
136 u32 val = 0;
137 u8 *ptr;
139 ptr = mem_ptr(addr, 4);
140 val = *(u32 *) (ptr);
142 DB(if (DEBUG_MEM_TRACE())
143 printf("%#08x 4 -> %#x\n", addr, val);)
144 return val;
147 /****************************************************************************
148 PARAMETERS:
149 addr - Emulator memory address to read
150 val - Value to store
152 REMARKS:
153 Writes a byte value to emulator memory.
154 ****************************************************************************/
155 void X86API wrb(u32 addr, u8 val)
157 u8 *ptr;
159 ptr = mem_ptr(addr, 1);
160 *(u8 *) (ptr) = val;
162 DB(if (DEBUG_MEM_TRACE())
163 printf("%#08x 1 <- %#x\n", addr, val);)
166 /****************************************************************************
167 PARAMETERS:
168 addr - Emulator memory address to read
169 val - Value to store
171 REMARKS:
172 Writes a word value to emulator memory.
173 ****************************************************************************/
174 void X86API wrw(u32 addr, u16 val)
176 u8 *ptr;
178 ptr = mem_ptr(addr, 2);
179 *(u16 *) (ptr) = val;
181 DB(if (DEBUG_MEM_TRACE())
182 printf("%#08x 2 <- %#x\n", addr, val);)
185 /****************************************************************************
186 PARAMETERS:
187 addr - Emulator memory address to read
188 val - Value to store
190 REMARKS:
191 Writes a long value to emulator memory.
192 ****************************************************************************/
193 void X86API wrl(u32 addr, u32 val)
195 u8 *ptr;
197 ptr = mem_ptr(addr, 4);
198 *(u32 *) (ptr) = val;
200 DB(if (DEBUG_MEM_TRACE())
201 printf("%#08x 4 <- %#x\n", addr, val);)
204 /****************************************************************************
205 PARAMETERS:
206 addr - PIO address to read
207 RETURN:
209 REMARKS:
210 Default PIO byte read function. Doesn't perform real inb.
211 ****************************************************************************/
212 static u8 X86API p_inb(X86EMU_pioAddr addr)
214 DB(if (DEBUG_IO_TRACE())
215 printf("inb %#04x\n", addr);)
216 return inb(addr);
219 /****************************************************************************
220 PARAMETERS:
221 addr - PIO address to read
222 RETURN:
224 REMARKS:
225 Default PIO word read function. Doesn't perform real inw.
226 ****************************************************************************/
227 static u16 X86API p_inw(X86EMU_pioAddr addr)
229 DB(if (DEBUG_IO_TRACE())
230 printf("inw %#04x\n", addr);)
231 return inw(addr);
234 /****************************************************************************
235 PARAMETERS:
236 addr - PIO address to read
237 RETURN:
239 REMARKS:
240 Default PIO long read function. Doesn't perform real inl.
241 ****************************************************************************/
242 static u32 X86API p_inl(X86EMU_pioAddr addr)
244 DB(if (DEBUG_IO_TRACE())
245 printf("inl %#04x\n", addr);)
246 return inl(addr);
249 /****************************************************************************
250 PARAMETERS:
251 addr - PIO address to write
252 val - Value to store
253 REMARKS:
254 Default PIO byte write function. Doesn't perform real outb.
255 ****************************************************************************/
256 static void X86API p_outb(X86EMU_pioAddr addr, u8 val)
258 DB(if (DEBUG_IO_TRACE())
259 printf("outb %#02x -> %#04x\n", val, addr);)
260 outb(val, addr);
261 return;
264 /****************************************************************************
265 PARAMETERS:
266 addr - PIO address to write
267 val - Value to store
268 REMARKS:
269 Default PIO word write function. Doesn't perform real outw.
270 ****************************************************************************/
271 static void X86API p_outw(X86EMU_pioAddr addr, u16 val)
273 DB(if (DEBUG_IO_TRACE())
274 printf("outw %#04x -> %#04x\n", val, addr);)
275 outw(val, addr);
276 return;
279 /****************************************************************************
280 PARAMETERS:
281 addr - PIO address to write
282 val - Value to store
283 REMARKS:
284 Default PIO ;ong write function. Doesn't perform real outl.
285 ****************************************************************************/
286 static void X86API p_outl(X86EMU_pioAddr addr, u32 val)
288 DB(if (DEBUG_IO_TRACE())
289 printf("outl %#08x -> %#04x\n", val, addr);)
291 outl(val, addr);
292 return;
295 /*------------------------- Global Variables ------------------------------*/
297 u8(X86APIP sys_rdb) (u32 addr) = rdb;
298 u16(X86APIP sys_rdw) (u32 addr) = rdw;
299 u32(X86APIP sys_rdl) (u32 addr) = rdl;
300 void (X86APIP sys_wrb) (u32 addr, u8 val) = wrb;
301 void (X86APIP sys_wrw) (u32 addr, u16 val) = wrw;
302 void (X86APIP sys_wrl) (u32 addr, u32 val) = wrl;
303 u8(X86APIP sys_inb) (X86EMU_pioAddr addr) = p_inb;
304 u16(X86APIP sys_inw) (X86EMU_pioAddr addr) = p_inw;
305 u32(X86APIP sys_inl) (X86EMU_pioAddr addr) = p_inl;
306 void (X86APIP sys_outb) (X86EMU_pioAddr addr, u8 val) = p_outb;
307 void (X86APIP sys_outw) (X86EMU_pioAddr addr, u16 val) = p_outw;
308 void (X86APIP sys_outl) (X86EMU_pioAddr addr, u32 val) = p_outl;
310 /*----------------------------- Setup -------------------------------------*/
312 /****************************************************************************
313 PARAMETERS:
314 funcs - New memory function pointers to make active
316 REMARKS:
317 This function is used to set the pointers to functions which access
318 memory space, allowing the user application to override these functions
319 and hook them out as necessary for their application.
320 ****************************************************************************/
321 void X86EMU_setupMemFuncs(X86EMU_memFuncs * funcs)
323 sys_rdb = funcs->rdb;
324 sys_rdw = funcs->rdw;
325 sys_rdl = funcs->rdl;
326 sys_wrb = funcs->wrb;
327 sys_wrw = funcs->wrw;
328 sys_wrl = funcs->wrl;
331 /****************************************************************************
332 PARAMETERS:
333 funcs - New programmed I/O function pointers to make active
335 REMARKS:
336 This function is used to set the pointers to functions which access
337 I/O space, allowing the user application to override these functions
338 and hook them out as necessary for their application.
339 ****************************************************************************/
340 void X86EMU_setupPioFuncs(X86EMU_pioFuncs * funcs)
342 sys_inb = funcs->inb;
343 sys_inw = funcs->inw;
344 sys_inl = funcs->inl;
345 sys_outb = funcs->outb;
346 sys_outw = funcs->outw;
347 sys_outl = funcs->outl;
350 /****************************************************************************
351 PARAMETERS:
352 funcs - New interrupt vector table to make active
354 REMARKS:
355 This function is used to set the pointers to functions which handle
356 interrupt processing in the emulator, allowing the user application to
357 hook interrupts as necessary for their application. Any interrupts that
358 are not hooked by the user application, and reflected and handled internally
359 in the emulator via the interrupt vector table. This allows the application
360 to get control when the code being emulated executes specific software
361 interrupts.
362 ****************************************************************************/
363 void X86EMU_setupIntrFuncs(X86EMU_intrFuncs funcs[])
365 int i;
367 for (i = 0; i < 256; i++)
368 _X86EMU_intrTab[i] = NULL;
369 if (funcs) {
370 for (i = 0; i < 256; i++)
371 _X86EMU_intrTab[i] = funcs[i];
375 /****************************************************************************
376 PARAMETERS:
377 int - New software interrupt to prepare for
379 REMARKS:
380 This function is used to set up the emulator state to execute a software
381 interrupt. This can be used by the user application code to allow an
382 interrupt to be hooked, examined and then reflected back to the emulator
383 so that the code in the emulator will continue processing the software
384 interrupt as per normal. This essentially allows system code to actively
385 hook and handle certain software interrupts as necessary.
386 ****************************************************************************/
387 void X86EMU_prepareForInt(int num)
389 push_word((u16) M.x86.R_FLG);
390 CLEAR_FLAG(F_IF);
391 CLEAR_FLAG(F_TF);
392 push_word(M.x86.R_CS);
393 M.x86.R_CS = mem_access_word(num * 4 + 2);
394 push_word(M.x86.R_IP);
395 M.x86.R_IP = mem_access_word(num * 4);
396 M.x86.intr = 0;
399 void X86EMU_setMemBase(void *base, size_t size)
401 M.mem_base = (unsigned long) base;
402 M.mem_size = size;