blob: 2e971fa3e5cd89ff3c8c175214e15d7632c71b42 [file] [log] [blame]
Isaku Yamahata32993972009-07-02 19:32:06 +09001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
24/*
25 * splitted out ioport related stuffs from vl.c.
26 */
27
28#include "ioport.h"
Prerna Saxenabd3c9aa2010-08-11 17:15:11 +053029#include "trace.h"
Isaku Yamahata32993972009-07-02 19:32:06 +090030
31/***********************************************************/
32/* IO Port */
33
34//#define DEBUG_UNUSED_IOPORT
35//#define DEBUG_IOPORT
36
Isaku Yamahatafc7083b2009-07-14 19:10:42 +090037#ifdef DEBUG_UNUSED_IOPORT
38# define LOG_UNUSED_IOPORT(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__)
39#else
40# define LOG_UNUSED_IOPORT(fmt, ...) do{ } while (0)
41#endif
42
Isaku Yamahata32993972009-07-02 19:32:06 +090043#ifdef DEBUG_IOPORT
44# define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__)
45#else
46# define LOG_IOPORT(...) do { } while (0)
47#endif
48
49/* XXX: use a two level table to limit memory usage */
50
51static void *ioport_opaque[MAX_IOPORTS];
52static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
53static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
54
55static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
56static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
57
58static uint32_t ioport_read(int index, uint32_t address)
59{
Blue Swirl1dde6fc2009-09-06 16:32:13 +000060 static IOPortReadFunc * const default_func[3] = {
Isaku Yamahata32993972009-07-02 19:32:06 +090061 default_ioport_readb,
62 default_ioport_readw,
63 default_ioport_readl
64 };
65 IOPortReadFunc *func = ioport_read_table[index][address];
66 if (!func)
67 func = default_func[index];
68 return func(ioport_opaque[address], address);
69}
70
71static void ioport_write(int index, uint32_t address, uint32_t data)
72{
Blue Swirl1dde6fc2009-09-06 16:32:13 +000073 static IOPortWriteFunc * const default_func[3] = {
Isaku Yamahata32993972009-07-02 19:32:06 +090074 default_ioport_writeb,
75 default_ioport_writew,
76 default_ioport_writel
77 };
78 IOPortWriteFunc *func = ioport_write_table[index][address];
79 if (!func)
80 func = default_func[index];
81 func(ioport_opaque[address], address, data);
82}
83
84static uint32_t default_ioport_readb(void *opaque, uint32_t address)
85{
Isaku Yamahatafc7083b2009-07-14 19:10:42 +090086 LOG_UNUSED_IOPORT("unused inb: port=0x%04"PRIx32"\n", address);
Isaku Yamahata32993972009-07-02 19:32:06 +090087 return 0xff;
88}
89
90static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
91{
Isaku Yamahatafc7083b2009-07-14 19:10:42 +090092 LOG_UNUSED_IOPORT("unused outb: port=0x%04"PRIx32" data=0x%02"PRIx32"\n",
93 address, data);
Isaku Yamahata32993972009-07-02 19:32:06 +090094}
95
96/* default is to make two byte accesses */
97static uint32_t default_ioport_readw(void *opaque, uint32_t address)
98{
99 uint32_t data;
100 data = ioport_read(0, address);
Isaku Yamahatad56dd6c2009-07-02 19:32:07 +0900101 address = (address + 1) & IOPORTS_MASK;
Isaku Yamahata32993972009-07-02 19:32:06 +0900102 data |= ioport_read(0, address) << 8;
103 return data;
104}
105
106static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
107{
108 ioport_write(0, address, data & 0xff);
Isaku Yamahatad56dd6c2009-07-02 19:32:07 +0900109 address = (address + 1) & IOPORTS_MASK;
Isaku Yamahata32993972009-07-02 19:32:06 +0900110 ioport_write(0, address, (data >> 8) & 0xff);
111}
112
113static uint32_t default_ioport_readl(void *opaque, uint32_t address)
114{
Isaku Yamahatafc7083b2009-07-14 19:10:42 +0900115 LOG_UNUSED_IOPORT("unused inl: port=0x%04"PRIx32"\n", address);
Isaku Yamahata32993972009-07-02 19:32:06 +0900116 return 0xffffffff;
117}
118
119static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
120{
Isaku Yamahatafc7083b2009-07-14 19:10:42 +0900121 LOG_UNUSED_IOPORT("unused outl: port=0x%04"PRIx32" data=0x%02"PRIx32"\n",
122 address, data);
Isaku Yamahata32993972009-07-02 19:32:06 +0900123}
124
Isaku Yamahata23e0aff2009-07-02 19:32:08 +0900125static int ioport_bsize(int size, int *bsize)
126{
127 if (size == 1) {
128 *bsize = 0;
129 } else if (size == 2) {
130 *bsize = 1;
131 } else if (size == 4) {
132 *bsize = 2;
133 } else {
134 return -1;
135 }
136 return 0;
137}
138
Isaku Yamahata32993972009-07-02 19:32:06 +0900139/* size is the word size in byte */
Anthony Liguoric227f092009-10-01 16:12:16 -0500140int register_ioport_read(pio_addr_t start, int length, int size,
Isaku Yamahata32993972009-07-02 19:32:06 +0900141 IOPortReadFunc *func, void *opaque)
142{
143 int i, bsize;
144
Isaku Yamahata23e0aff2009-07-02 19:32:08 +0900145 if (ioport_bsize(size, &bsize)) {
Isaku Yamahata32993972009-07-02 19:32:06 +0900146 hw_error("register_ioport_read: invalid size");
147 return -1;
148 }
149 for(i = start; i < start + length; i += size) {
150 ioport_read_table[bsize][i] = func;
151 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
Andreas Färberf66a99d2011-03-06 15:48:13 +0100152 hw_error("register_ioport_read: invalid opaque for address 0x%x",
153 i);
Isaku Yamahata32993972009-07-02 19:32:06 +0900154 ioport_opaque[i] = opaque;
155 }
156 return 0;
157}
158
159/* size is the word size in byte */
Anthony Liguoric227f092009-10-01 16:12:16 -0500160int register_ioport_write(pio_addr_t start, int length, int size,
Isaku Yamahata32993972009-07-02 19:32:06 +0900161 IOPortWriteFunc *func, void *opaque)
162{
163 int i, bsize;
164
Isaku Yamahata23e0aff2009-07-02 19:32:08 +0900165 if (ioport_bsize(size, &bsize)) {
Isaku Yamahata32993972009-07-02 19:32:06 +0900166 hw_error("register_ioport_write: invalid size");
167 return -1;
168 }
169 for(i = start; i < start + length; i += size) {
170 ioport_write_table[bsize][i] = func;
171 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
Andreas Färberf66a99d2011-03-06 15:48:13 +0100172 hw_error("register_ioport_write: invalid opaque for address 0x%x",
173 i);
Isaku Yamahata32993972009-07-02 19:32:06 +0900174 ioport_opaque[i] = opaque;
175 }
176 return 0;
177}
178
Avi Kivityacd1c812010-11-17 11:50:09 +0200179static uint32_t ioport_readb_thunk(void *opaque, uint32_t addr)
180{
181 IORange *ioport = opaque;
182 uint64_t data;
183
184 ioport->ops->read(ioport, addr - ioport->base, 1, &data);
185 return data;
186}
187
188static uint32_t ioport_readw_thunk(void *opaque, uint32_t addr)
189{
190 IORange *ioport = opaque;
191 uint64_t data;
192
193 ioport->ops->read(ioport, addr - ioport->base, 2, &data);
194 return data;
195}
196
197static uint32_t ioport_readl_thunk(void *opaque, uint32_t addr)
198{
199 IORange *ioport = opaque;
200 uint64_t data;
201
202 ioport->ops->read(ioport, addr - ioport->base, 4, &data);
203 return data;
204}
205
206static void ioport_writeb_thunk(void *opaque, uint32_t addr, uint32_t data)
207{
208 IORange *ioport = opaque;
209
210 ioport->ops->write(ioport, addr - ioport->base, 1, data);
211}
212
213static void ioport_writew_thunk(void *opaque, uint32_t addr, uint32_t data)
214{
215 IORange *ioport = opaque;
216
217 ioport->ops->write(ioport, addr - ioport->base, 2, data);
218}
219
220static void ioport_writel_thunk(void *opaque, uint32_t addr, uint32_t data)
221{
222 IORange *ioport = opaque;
223
224 ioport->ops->write(ioport, addr - ioport->base, 4, data);
225}
226
227void ioport_register(IORange *ioport)
228{
229 register_ioport_read(ioport->base, ioport->len, 1,
230 ioport_readb_thunk, ioport);
231 register_ioport_read(ioport->base, ioport->len, 2,
232 ioport_readw_thunk, ioport);
233 register_ioport_read(ioport->base, ioport->len, 4,
234 ioport_readl_thunk, ioport);
235 register_ioport_write(ioport->base, ioport->len, 1,
236 ioport_writeb_thunk, ioport);
237 register_ioport_write(ioport->base, ioport->len, 2,
238 ioport_writew_thunk, ioport);
239 register_ioport_write(ioport->base, ioport->len, 4,
240 ioport_writel_thunk, ioport);
241}
242
Anthony Liguoric227f092009-10-01 16:12:16 -0500243void isa_unassign_ioport(pio_addr_t start, int length)
Isaku Yamahata32993972009-07-02 19:32:06 +0900244{
245 int i;
246
247 for(i = start; i < start + length; i++) {
248 ioport_read_table[0][i] = default_ioport_readb;
249 ioport_read_table[1][i] = default_ioport_readw;
250 ioport_read_table[2][i] = default_ioport_readl;
251
252 ioport_write_table[0][i] = default_ioport_writeb;
253 ioport_write_table[1][i] = default_ioport_writew;
254 ioport_write_table[2][i] = default_ioport_writel;
255
256 ioport_opaque[i] = NULL;
257 }
258}
259
260/***********************************************************/
261
Anthony Liguoric227f092009-10-01 16:12:16 -0500262void cpu_outb(pio_addr_t addr, uint8_t val)
Isaku Yamahata32993972009-07-02 19:32:06 +0900263{
Isaku Yamahata07323532009-07-14 19:10:43 +0900264 LOG_IOPORT("outb: %04"FMT_pioaddr" %02"PRIx8"\n", addr, val);
Prerna Saxenabd3c9aa2010-08-11 17:15:11 +0530265 trace_cpu_out(addr, val);
Isaku Yamahata32993972009-07-02 19:32:06 +0900266 ioport_write(0, addr, val);
Isaku Yamahata32993972009-07-02 19:32:06 +0900267}
268
Anthony Liguoric227f092009-10-01 16:12:16 -0500269void cpu_outw(pio_addr_t addr, uint16_t val)
Isaku Yamahata32993972009-07-02 19:32:06 +0900270{
Isaku Yamahata07323532009-07-14 19:10:43 +0900271 LOG_IOPORT("outw: %04"FMT_pioaddr" %04"PRIx16"\n", addr, val);
Prerna Saxenabd3c9aa2010-08-11 17:15:11 +0530272 trace_cpu_out(addr, val);
Isaku Yamahata32993972009-07-02 19:32:06 +0900273 ioport_write(1, addr, val);
Isaku Yamahata32993972009-07-02 19:32:06 +0900274}
275
Anthony Liguoric227f092009-10-01 16:12:16 -0500276void cpu_outl(pio_addr_t addr, uint32_t val)
Isaku Yamahata32993972009-07-02 19:32:06 +0900277{
Isaku Yamahata07323532009-07-14 19:10:43 +0900278 LOG_IOPORT("outl: %04"FMT_pioaddr" %08"PRIx32"\n", addr, val);
Prerna Saxenabd3c9aa2010-08-11 17:15:11 +0530279 trace_cpu_out(addr, val);
Isaku Yamahata32993972009-07-02 19:32:06 +0900280 ioport_write(2, addr, val);
Isaku Yamahata32993972009-07-02 19:32:06 +0900281}
282
Anthony Liguoric227f092009-10-01 16:12:16 -0500283uint8_t cpu_inb(pio_addr_t addr)
Isaku Yamahata32993972009-07-02 19:32:06 +0900284{
Isaku Yamahata07323532009-07-14 19:10:43 +0900285 uint8_t val;
Isaku Yamahata32993972009-07-02 19:32:06 +0900286 val = ioport_read(0, addr);
Prerna Saxenabd3c9aa2010-08-11 17:15:11 +0530287 trace_cpu_in(addr, val);
Isaku Yamahata07323532009-07-14 19:10:43 +0900288 LOG_IOPORT("inb : %04"FMT_pioaddr" %02"PRIx8"\n", addr, val);
Isaku Yamahata32993972009-07-02 19:32:06 +0900289 return val;
290}
291
Anthony Liguoric227f092009-10-01 16:12:16 -0500292uint16_t cpu_inw(pio_addr_t addr)
Isaku Yamahata32993972009-07-02 19:32:06 +0900293{
Isaku Yamahata07323532009-07-14 19:10:43 +0900294 uint16_t val;
Isaku Yamahata32993972009-07-02 19:32:06 +0900295 val = ioport_read(1, addr);
Prerna Saxenabd3c9aa2010-08-11 17:15:11 +0530296 trace_cpu_in(addr, val);
Isaku Yamahata07323532009-07-14 19:10:43 +0900297 LOG_IOPORT("inw : %04"FMT_pioaddr" %04"PRIx16"\n", addr, val);
Isaku Yamahata32993972009-07-02 19:32:06 +0900298 return val;
299}
300
Anthony Liguoric227f092009-10-01 16:12:16 -0500301uint32_t cpu_inl(pio_addr_t addr)
Isaku Yamahata32993972009-07-02 19:32:06 +0900302{
Isaku Yamahata07323532009-07-14 19:10:43 +0900303 uint32_t val;
Isaku Yamahata32993972009-07-02 19:32:06 +0900304 val = ioport_read(2, addr);
Prerna Saxenabd3c9aa2010-08-11 17:15:11 +0530305 trace_cpu_in(addr, val);
Isaku Yamahata07323532009-07-14 19:10:43 +0900306 LOG_IOPORT("inl : %04"FMT_pioaddr" %08"PRIx32"\n", addr, val);
Isaku Yamahata32993972009-07-02 19:32:06 +0900307 return val;
308}