1/*  *********************************************************************
2    *  Broadcom Common Firmware Environment (CFE)
3    *
4    *  X86 simulator sparse memory		File: X86MEM.H
5    *
6    *  This module implements X86 memory for the X86 emulator
7    *  used by the BIOS simulator.  To avoid allocating the
8    *  entire 1MB of PC's addressable memory, this is a "sparse"
9    *  memory model, allocating chunks of storage as needed.
10    *  VGA BIOSes seem to do all sorts of bizarre things to memory
11    *  so this helps reduce the total amount we need to allocate
12    *  significantly.
13    *
14    *  In addition, this module lets the simulator "hook"
15    *  ranges of memory to be handled by a callback
16    *  routine.  This is used so that we can redirect
17    *  accesses to VGA memory space to the PCI bus handler.
18    *
19    *  Author:  Mitch Lichtenberg
20    *
21    *********************************************************************
22    *
23    *  Copyright 2000,2001,2002,2003
24    *  Broadcom Corporation. All rights reserved.
25    *
26    *  This software is furnished under license and may be used and
27    *  copied only in accordance with the following terms and
28    *  conditions.  Subject to these conditions, you may download,
29    *  copy, install, use, modify and distribute modified or unmodified
30    *  copies of this software in source and/or binary form.  No title
31    *  or ownership is transferred hereby.
32    *
33    *  1) Any source code used, modified or distributed must reproduce
34    *     and retain this copyright notice and list of conditions
35    *     as they appear in the source file.
36    *
37    *  2) No right is granted to use any trade name, trademark, or
38    *     logo of Broadcom Corporation.  The "Broadcom Corporation"
39    *     name may not be used to endorse or promote products derived
40    *     from this software without the prior written permission of
41    *     Broadcom Corporation.
42    *
43    *  3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
44    *     IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
45    *     WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
46    *     PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT
47    *     SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN
48    *     PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
49    *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
50    *     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
51    *     GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
52    *     BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
53    *     OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
54    *     TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF
55    *     THE POSSIBILITY OF SUCH DAMAGE.
56    ********************************************************************* */
57
58
59/*  *********************************************************************
60    *  Constants
61    ********************************************************************* */
62
63#define X86MEM_CHUNKBITS    15
64#define X86MEM_ADDRESSBITS  20
65#define X86MEM_CHUNKSIZE    (1<<X86MEM_CHUNKBITS)
66#define X86MEM_CHUNKS	    (1<<(X86MEM_ADDRESSBITS-X86MEM_CHUNKBITS))
67#define X86MEM_REGION(addr) (((addr) >> X86MEM_CHUNKBITS) & (X86MEM_CHUNKS-1))
68#define X86MEM_OFFSET(addr) ((addr) & (X86MEM_CHUNKSIZE-1))
69
70#define M_BYTE	1
71#define M_WORD	2
72#define M_DWORD	4
73
74/*  *********************************************************************
75    *  Structures
76    ********************************************************************* */
77
78typedef struct x86mem_s {
79    uint8_t *data[X86MEM_CHUNKS];
80    uint32_t (*read[X86MEM_CHUNKS])(struct x86mem_s *x86mem,
81				    uint32_t addr,int size);
82    void  (*write[X86MEM_CHUNKS])(struct x86mem_s *x86mem,
83				  uint32_t addr,uint32_t val,int size);
84} x86mem_t;
85
86/*  *********************************************************************
87    *  Prototypes
88    ********************************************************************* */
89
90void x86mem_init(x86mem_t *mem);
91void x86mem_uninit(x86mem_t *mem);
92
93
94uint8_t x86mem_readb(x86mem_t *mem,uint32_t addr);
95uint16_t x86mem_readw(x86mem_t *mem,uint32_t addr);
96uint32_t x86mem_readl(x86mem_t *mem,uint32_t addr);
97
98
99void x86mem_writeb(x86mem_t *mem,uint32_t addr,uint8_t data);
100void x86mem_writew(x86mem_t *mem,uint32_t addr,uint16_t data);
101void x86mem_writel(x86mem_t *mem,uint32_t addr,uint32_t data);
102void x86mem_memcpy(x86mem_t *mem,uint32_t destaddr,uint8_t *src,int count);
103void x86mem_hook(x86mem_t *mem,uint32_t chunkaddr,
104		 uint32_t (*readf)(x86mem_t *mem,uint32_t addr,int size),
105		 void (*writef)(x86mem_t *mem,uint32_t addr,uint32_t val,int size));
106void x86mem_hook_range(x86mem_t *mem,uint32_t chunkaddr,int size,
107		 uint32_t (*readf)(x86mem_t *mem,uint32_t addr,int size),
108		 void (*writef)(x86mem_t *mem,uint32_t addr,uint32_t val,int size));
109
110