replace malloc with qemu_malloc
[qemu-kvm/fedora.git] / ioport.c
blob01cfaf706172fbd5c5baadd1bb6b43dcc8434de7
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 * splitted out ioport related stuffs from vl.c.
28 #include "ioport.h"
30 /***********************************************************/
31 /* IO Port */
33 //#define DEBUG_UNUSED_IOPORT
34 //#define DEBUG_IOPORT
36 #ifdef DEBUG_IOPORT
37 # define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__)
38 #else
39 # define LOG_IOPORT(...) do { } while (0)
40 #endif
42 /* XXX: use a two level table to limit memory usage */
44 static void *ioport_opaque[MAX_IOPORTS];
45 static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
46 static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
48 static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
49 static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
51 static uint32_t ioport_read(int index, uint32_t address)
53 static IOPortReadFunc *default_func[3] = {
54 default_ioport_readb,
55 default_ioport_readw,
56 default_ioport_readl
58 IOPortReadFunc *func = ioport_read_table[index][address];
59 if (!func)
60 func = default_func[index];
61 return func(ioport_opaque[address], address);
64 static void ioport_write(int index, uint32_t address, uint32_t data)
66 static IOPortWriteFunc *default_func[3] = {
67 default_ioport_writeb,
68 default_ioport_writew,
69 default_ioport_writel
71 IOPortWriteFunc *func = ioport_write_table[index][address];
72 if (!func)
73 func = default_func[index];
74 func(ioport_opaque[address], address, data);
77 static uint32_t default_ioport_readb(void *opaque, uint32_t address)
79 #ifdef DEBUG_UNUSED_IOPORT
80 fprintf(stderr, "unused inb: port=0x%04x\n", address);
81 #endif
82 return 0xff;
85 static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
87 #ifdef DEBUG_UNUSED_IOPORT
88 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
89 #endif
92 /* default is to make two byte accesses */
93 static uint32_t default_ioport_readw(void *opaque, uint32_t address)
95 uint32_t data;
96 data = ioport_read(0, address);
97 address = (address + 1) & IOPORTS_MASK;
98 data |= ioport_read(0, address) << 8;
99 return data;
102 static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
104 ioport_write(0, address, data & 0xff);
105 address = (address + 1) & IOPORTS_MASK;
106 ioport_write(0, address, (data >> 8) & 0xff);
109 static uint32_t default_ioport_readl(void *opaque, uint32_t address)
111 #ifdef DEBUG_UNUSED_IOPORT
112 fprintf(stderr, "unused inl: port=0x%04x\n", address);
113 #endif
114 return 0xffffffff;
117 static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
119 #ifdef DEBUG_UNUSED_IOPORT
120 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
121 #endif
124 static int ioport_bsize(int size, int *bsize)
126 if (size == 1) {
127 *bsize = 0;
128 } else if (size == 2) {
129 *bsize = 1;
130 } else if (size == 4) {
131 *bsize = 2;
132 } else {
133 return -1;
135 return 0;
138 /* size is the word size in byte */
139 int register_ioport_read(int start, int length, int size,
140 IOPortReadFunc *func, void *opaque)
142 int i, bsize;
144 if (ioport_bsize(size, &bsize)) {
145 hw_error("register_ioport_read: invalid size");
146 return -1;
148 for(i = start; i < start + length; i += size) {
149 ioport_read_table[bsize][i] = func;
150 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
151 hw_error("register_ioport_read: invalid opaque");
152 ioport_opaque[i] = opaque;
154 return 0;
157 /* size is the word size in byte */
158 int register_ioport_write(int start, int length, int size,
159 IOPortWriteFunc *func, void *opaque)
161 int i, bsize;
163 if (ioport_bsize(size, &bsize)) {
164 hw_error("register_ioport_write: invalid size");
165 return -1;
167 for(i = start; i < start + length; i += size) {
168 ioport_write_table[bsize][i] = func;
169 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
170 hw_error("register_ioport_write: invalid opaque");
171 ioport_opaque[i] = opaque;
173 return 0;
176 void isa_unassign_ioport(int start, int length)
178 int i;
180 for(i = start; i < start + length; i++) {
181 ioport_read_table[0][i] = default_ioport_readb;
182 ioport_read_table[1][i] = default_ioport_readw;
183 ioport_read_table[2][i] = default_ioport_readl;
185 ioport_write_table[0][i] = default_ioport_writeb;
186 ioport_write_table[1][i] = default_ioport_writew;
187 ioport_write_table[2][i] = default_ioport_writel;
189 ioport_opaque[i] = NULL;
193 /***********************************************************/
195 void cpu_outb(CPUState *env, int addr, int val)
197 LOG_IOPORT("outb: %04x %02x\n", addr, val);
198 ioport_write(0, addr, val);
199 #ifdef CONFIG_KQEMU
200 if (env)
201 env->last_io_time = cpu_get_time_fast();
202 #endif
205 void cpu_outw(CPUState *env, int addr, int val)
207 LOG_IOPORT("outw: %04x %04x\n", addr, val);
208 ioport_write(1, addr, val);
209 #ifdef CONFIG_KQEMU
210 if (env)
211 env->last_io_time = cpu_get_time_fast();
212 #endif
215 void cpu_outl(CPUState *env, int addr, int val)
217 LOG_IOPORT("outl: %04x %08x\n", addr, val);
218 ioport_write(2, addr, val);
219 #ifdef CONFIG_KQEMU
220 if (env)
221 env->last_io_time = cpu_get_time_fast();
222 #endif
225 int cpu_inb(CPUState *env, int addr)
227 int val;
228 val = ioport_read(0, addr);
229 LOG_IOPORT("inb : %04x %02x\n", addr, val);
230 #ifdef CONFIG_KQEMU
231 if (env)
232 env->last_io_time = cpu_get_time_fast();
233 #endif
234 return val;
237 int cpu_inw(CPUState *env, int addr)
239 int val;
240 val = ioport_read(1, addr);
241 LOG_IOPORT("inw : %04x %04x\n", addr, val);
242 #ifdef CONFIG_KQEMU
243 if (env)
244 env->last_io_time = cpu_get_time_fast();
245 #endif
246 return val;
249 int cpu_inl(CPUState *env, int addr)
251 int val;
252 val = ioport_read(2, addr);
253 LOG_IOPORT("inl : %04x %08x\n", addr, val);
254 #ifdef CONFIG_KQEMU
255 if (env)
256 env->last_io_time = cpu_get_time_fast();
257 #endif
258 return val;