root / ioport.c @ 8ca209ad
History | View | Annotate | Download (7 kB)
1 | 32993977 | Isaku Yamahata | /*
|
---|---|---|---|
2 | 32993977 | Isaku Yamahata | * QEMU System Emulator
|
3 | 32993977 | Isaku Yamahata | *
|
4 | 32993977 | Isaku Yamahata | * Copyright (c) 2003-2008 Fabrice Bellard
|
5 | 32993977 | Isaku Yamahata | *
|
6 | 32993977 | Isaku Yamahata | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 32993977 | Isaku Yamahata | * of this software and associated documentation files (the "Software"), to deal
|
8 | 32993977 | Isaku Yamahata | * in the Software without restriction, including without limitation the rights
|
9 | 32993977 | Isaku Yamahata | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 32993977 | Isaku Yamahata | * copies of the Software, and to permit persons to whom the Software is
|
11 | 32993977 | Isaku Yamahata | * furnished to do so, subject to the following conditions:
|
12 | 32993977 | Isaku Yamahata | *
|
13 | 32993977 | Isaku Yamahata | * The above copyright notice and this permission notice shall be included in
|
14 | 32993977 | Isaku Yamahata | * all copies or substantial portions of the Software.
|
15 | 32993977 | Isaku Yamahata | *
|
16 | 32993977 | Isaku Yamahata | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 32993977 | Isaku Yamahata | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 32993977 | Isaku Yamahata | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 32993977 | Isaku Yamahata | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 32993977 | Isaku Yamahata | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 32993977 | Isaku Yamahata | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 32993977 | Isaku Yamahata | * THE SOFTWARE.
|
23 | 32993977 | Isaku Yamahata | */
|
24 | 32993977 | Isaku Yamahata | /*
|
25 | 32993977 | Isaku Yamahata | * splitted out ioport related stuffs from vl.c.
|
26 | 32993977 | Isaku Yamahata | */
|
27 | 32993977 | Isaku Yamahata | |
28 | 32993977 | Isaku Yamahata | #include "ioport.h" |
29 | bd3c9aa5 | Prerna Saxena | #include "trace.h" |
30 | 32993977 | Isaku Yamahata | |
31 | 32993977 | Isaku Yamahata | /***********************************************************/
|
32 | 32993977 | Isaku Yamahata | /* IO Port */
|
33 | 32993977 | Isaku Yamahata | |
34 | 32993977 | Isaku Yamahata | //#define DEBUG_UNUSED_IOPORT
|
35 | 32993977 | Isaku Yamahata | //#define DEBUG_IOPORT
|
36 | 32993977 | Isaku Yamahata | |
37 | fc7083b5 | Isaku Yamahata | #ifdef DEBUG_UNUSED_IOPORT
|
38 | fc7083b5 | Isaku Yamahata | # define LOG_UNUSED_IOPORT(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__) |
39 | fc7083b5 | Isaku Yamahata | #else
|
40 | fc7083b5 | Isaku Yamahata | # define LOG_UNUSED_IOPORT(fmt, ...) do{ } while (0) |
41 | fc7083b5 | Isaku Yamahata | #endif
|
42 | fc7083b5 | Isaku Yamahata | |
43 | 32993977 | Isaku Yamahata | #ifdef DEBUG_IOPORT
|
44 | 32993977 | Isaku Yamahata | # define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__) |
45 | 32993977 | Isaku Yamahata | #else
|
46 | 32993977 | Isaku Yamahata | # define LOG_IOPORT(...) do { } while (0) |
47 | 32993977 | Isaku Yamahata | #endif
|
48 | 32993977 | Isaku Yamahata | |
49 | 32993977 | Isaku Yamahata | /* XXX: use a two level table to limit memory usage */
|
50 | 32993977 | Isaku Yamahata | |
51 | 32993977 | Isaku Yamahata | static void *ioport_opaque[MAX_IOPORTS]; |
52 | 32993977 | Isaku Yamahata | static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS]; |
53 | 32993977 | Isaku Yamahata | static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS]; |
54 | 32993977 | Isaku Yamahata | |
55 | 32993977 | Isaku Yamahata | static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
|
56 | 32993977 | Isaku Yamahata | static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
|
57 | 32993977 | Isaku Yamahata | |
58 | 32993977 | Isaku Yamahata | static uint32_t ioport_read(int index, uint32_t address) |
59 | 32993977 | Isaku Yamahata | { |
60 | 1dde6fcc | Blue Swirl | static IOPortReadFunc * const default_func[3] = { |
61 | 32993977 | Isaku Yamahata | default_ioport_readb, |
62 | 32993977 | Isaku Yamahata | default_ioport_readw, |
63 | 32993977 | Isaku Yamahata | default_ioport_readl |
64 | 32993977 | Isaku Yamahata | }; |
65 | 32993977 | Isaku Yamahata | IOPortReadFunc *func = ioport_read_table[index][address]; |
66 | 32993977 | Isaku Yamahata | if (!func)
|
67 | 32993977 | Isaku Yamahata | func = default_func[index]; |
68 | 32993977 | Isaku Yamahata | return func(ioport_opaque[address], address);
|
69 | 32993977 | Isaku Yamahata | } |
70 | 32993977 | Isaku Yamahata | |
71 | 32993977 | Isaku Yamahata | static void ioport_write(int index, uint32_t address, uint32_t data) |
72 | 32993977 | Isaku Yamahata | { |
73 | 1dde6fcc | Blue Swirl | static IOPortWriteFunc * const default_func[3] = { |
74 | 32993977 | Isaku Yamahata | default_ioport_writeb, |
75 | 32993977 | Isaku Yamahata | default_ioport_writew, |
76 | 32993977 | Isaku Yamahata | default_ioport_writel |
77 | 32993977 | Isaku Yamahata | }; |
78 | 32993977 | Isaku Yamahata | IOPortWriteFunc *func = ioport_write_table[index][address]; |
79 | 32993977 | Isaku Yamahata | if (!func)
|
80 | 32993977 | Isaku Yamahata | func = default_func[index]; |
81 | 32993977 | Isaku Yamahata | func(ioport_opaque[address], address, data); |
82 | 32993977 | Isaku Yamahata | } |
83 | 32993977 | Isaku Yamahata | |
84 | 32993977 | Isaku Yamahata | static uint32_t default_ioport_readb(void *opaque, uint32_t address) |
85 | 32993977 | Isaku Yamahata | { |
86 | fc7083b5 | Isaku Yamahata | LOG_UNUSED_IOPORT("unused inb: port=0x%04"PRIx32"\n", address); |
87 | 32993977 | Isaku Yamahata | return 0xff; |
88 | 32993977 | Isaku Yamahata | } |
89 | 32993977 | Isaku Yamahata | |
90 | 32993977 | Isaku Yamahata | static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data) |
91 | 32993977 | Isaku Yamahata | { |
92 | fc7083b5 | Isaku Yamahata | LOG_UNUSED_IOPORT("unused outb: port=0x%04"PRIx32" data=0x%02"PRIx32"\n", |
93 | fc7083b5 | Isaku Yamahata | address, data); |
94 | 32993977 | Isaku Yamahata | } |
95 | 32993977 | Isaku Yamahata | |
96 | 32993977 | Isaku Yamahata | /* default is to make two byte accesses */
|
97 | 32993977 | Isaku Yamahata | static uint32_t default_ioport_readw(void *opaque, uint32_t address) |
98 | 32993977 | Isaku Yamahata | { |
99 | 32993977 | Isaku Yamahata | uint32_t data; |
100 | 32993977 | Isaku Yamahata | data = ioport_read(0, address);
|
101 | d56dd6cf | Isaku Yamahata | address = (address + 1) & IOPORTS_MASK;
|
102 | 32993977 | Isaku Yamahata | data |= ioport_read(0, address) << 8; |
103 | 32993977 | Isaku Yamahata | return data;
|
104 | 32993977 | Isaku Yamahata | } |
105 | 32993977 | Isaku Yamahata | |
106 | 32993977 | Isaku Yamahata | static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data) |
107 | 32993977 | Isaku Yamahata | { |
108 | 32993977 | Isaku Yamahata | ioport_write(0, address, data & 0xff); |
109 | d56dd6cf | Isaku Yamahata | address = (address + 1) & IOPORTS_MASK;
|
110 | 32993977 | Isaku Yamahata | ioport_write(0, address, (data >> 8) & 0xff); |
111 | 32993977 | Isaku Yamahata | } |
112 | 32993977 | Isaku Yamahata | |
113 | 32993977 | Isaku Yamahata | static uint32_t default_ioport_readl(void *opaque, uint32_t address) |
114 | 32993977 | Isaku Yamahata | { |
115 | fc7083b5 | Isaku Yamahata | LOG_UNUSED_IOPORT("unused inl: port=0x%04"PRIx32"\n", address); |
116 | 32993977 | Isaku Yamahata | return 0xffffffff; |
117 | 32993977 | Isaku Yamahata | } |
118 | 32993977 | Isaku Yamahata | |
119 | 32993977 | Isaku Yamahata | static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data) |
120 | 32993977 | Isaku Yamahata | { |
121 | fc7083b5 | Isaku Yamahata | LOG_UNUSED_IOPORT("unused outl: port=0x%04"PRIx32" data=0x%02"PRIx32"\n", |
122 | fc7083b5 | Isaku Yamahata | address, data); |
123 | 32993977 | Isaku Yamahata | } |
124 | 32993977 | Isaku Yamahata | |
125 | 23e0affd | Isaku Yamahata | static int ioport_bsize(int size, int *bsize) |
126 | 23e0affd | Isaku Yamahata | { |
127 | 23e0affd | Isaku Yamahata | if (size == 1) { |
128 | 23e0affd | Isaku Yamahata | *bsize = 0;
|
129 | 23e0affd | Isaku Yamahata | } else if (size == 2) { |
130 | 23e0affd | Isaku Yamahata | *bsize = 1;
|
131 | 23e0affd | Isaku Yamahata | } else if (size == 4) { |
132 | 23e0affd | Isaku Yamahata | *bsize = 2;
|
133 | 23e0affd | Isaku Yamahata | } else {
|
134 | 23e0affd | Isaku Yamahata | return -1; |
135 | 23e0affd | Isaku Yamahata | } |
136 | 23e0affd | Isaku Yamahata | return 0; |
137 | 23e0affd | Isaku Yamahata | } |
138 | 23e0affd | Isaku Yamahata | |
139 | 32993977 | Isaku Yamahata | /* size is the word size in byte */
|
140 | c227f099 | Anthony Liguori | int register_ioport_read(pio_addr_t start, int length, int size, |
141 | 32993977 | Isaku Yamahata | IOPortReadFunc *func, void *opaque)
|
142 | 32993977 | Isaku Yamahata | { |
143 | 32993977 | Isaku Yamahata | int i, bsize;
|
144 | 32993977 | Isaku Yamahata | |
145 | 23e0affd | Isaku Yamahata | if (ioport_bsize(size, &bsize)) {
|
146 | 32993977 | Isaku Yamahata | hw_error("register_ioport_read: invalid size");
|
147 | 32993977 | Isaku Yamahata | return -1; |
148 | 32993977 | Isaku Yamahata | } |
149 | 32993977 | Isaku Yamahata | for(i = start; i < start + length; i += size) {
|
150 | 32993977 | Isaku Yamahata | ioport_read_table[bsize][i] = func; |
151 | 32993977 | Isaku Yamahata | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
152 | 32993977 | Isaku Yamahata | hw_error("register_ioport_read: invalid opaque");
|
153 | 32993977 | Isaku Yamahata | ioport_opaque[i] = opaque; |
154 | 32993977 | Isaku Yamahata | } |
155 | 32993977 | Isaku Yamahata | return 0; |
156 | 32993977 | Isaku Yamahata | } |
157 | 32993977 | Isaku Yamahata | |
158 | 32993977 | Isaku Yamahata | /* size is the word size in byte */
|
159 | c227f099 | Anthony Liguori | int register_ioport_write(pio_addr_t start, int length, int size, |
160 | 32993977 | Isaku Yamahata | IOPortWriteFunc *func, void *opaque)
|
161 | 32993977 | Isaku Yamahata | { |
162 | 32993977 | Isaku Yamahata | int i, bsize;
|
163 | 32993977 | Isaku Yamahata | |
164 | 23e0affd | Isaku Yamahata | if (ioport_bsize(size, &bsize)) {
|
165 | 32993977 | Isaku Yamahata | hw_error("register_ioport_write: invalid size");
|
166 | 32993977 | Isaku Yamahata | return -1; |
167 | 32993977 | Isaku Yamahata | } |
168 | 32993977 | Isaku Yamahata | for(i = start; i < start + length; i += size) {
|
169 | 32993977 | Isaku Yamahata | ioport_write_table[bsize][i] = func; |
170 | 32993977 | Isaku Yamahata | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
171 | 32993977 | Isaku Yamahata | hw_error("register_ioport_write: invalid opaque");
|
172 | 32993977 | Isaku Yamahata | ioport_opaque[i] = opaque; |
173 | 32993977 | Isaku Yamahata | } |
174 | 32993977 | Isaku Yamahata | return 0; |
175 | 32993977 | Isaku Yamahata | } |
176 | 32993977 | Isaku Yamahata | |
177 | c227f099 | Anthony Liguori | void isa_unassign_ioport(pio_addr_t start, int length) |
178 | 32993977 | Isaku Yamahata | { |
179 | 32993977 | Isaku Yamahata | int i;
|
180 | 32993977 | Isaku Yamahata | |
181 | 32993977 | Isaku Yamahata | for(i = start; i < start + length; i++) {
|
182 | 32993977 | Isaku Yamahata | ioport_read_table[0][i] = default_ioport_readb;
|
183 | 32993977 | Isaku Yamahata | ioport_read_table[1][i] = default_ioport_readw;
|
184 | 32993977 | Isaku Yamahata | ioport_read_table[2][i] = default_ioport_readl;
|
185 | 32993977 | Isaku Yamahata | |
186 | 32993977 | Isaku Yamahata | ioport_write_table[0][i] = default_ioport_writeb;
|
187 | 32993977 | Isaku Yamahata | ioport_write_table[1][i] = default_ioport_writew;
|
188 | 32993977 | Isaku Yamahata | ioport_write_table[2][i] = default_ioport_writel;
|
189 | 32993977 | Isaku Yamahata | |
190 | 32993977 | Isaku Yamahata | ioport_opaque[i] = NULL;
|
191 | 32993977 | Isaku Yamahata | } |
192 | 32993977 | Isaku Yamahata | } |
193 | 32993977 | Isaku Yamahata | |
194 | 32993977 | Isaku Yamahata | /***********************************************************/
|
195 | 32993977 | Isaku Yamahata | |
196 | c227f099 | Anthony Liguori | void cpu_outb(pio_addr_t addr, uint8_t val)
|
197 | 32993977 | Isaku Yamahata | { |
198 | 07323531 | Isaku Yamahata | LOG_IOPORT("outb: %04"FMT_pioaddr" %02"PRIx8"\n", addr, val); |
199 | bd3c9aa5 | Prerna Saxena | trace_cpu_out(addr, val); |
200 | 32993977 | Isaku Yamahata | ioport_write(0, addr, val);
|
201 | 32993977 | Isaku Yamahata | } |
202 | 32993977 | Isaku Yamahata | |
203 | c227f099 | Anthony Liguori | void cpu_outw(pio_addr_t addr, uint16_t val)
|
204 | 32993977 | Isaku Yamahata | { |
205 | 07323531 | Isaku Yamahata | LOG_IOPORT("outw: %04"FMT_pioaddr" %04"PRIx16"\n", addr, val); |
206 | bd3c9aa5 | Prerna Saxena | trace_cpu_out(addr, val); |
207 | 32993977 | Isaku Yamahata | ioport_write(1, addr, val);
|
208 | 32993977 | Isaku Yamahata | } |
209 | 32993977 | Isaku Yamahata | |
210 | c227f099 | Anthony Liguori | void cpu_outl(pio_addr_t addr, uint32_t val)
|
211 | 32993977 | Isaku Yamahata | { |
212 | 07323531 | Isaku Yamahata | LOG_IOPORT("outl: %04"FMT_pioaddr" %08"PRIx32"\n", addr, val); |
213 | bd3c9aa5 | Prerna Saxena | trace_cpu_out(addr, val); |
214 | 32993977 | Isaku Yamahata | ioport_write(2, addr, val);
|
215 | 32993977 | Isaku Yamahata | } |
216 | 32993977 | Isaku Yamahata | |
217 | c227f099 | Anthony Liguori | uint8_t cpu_inb(pio_addr_t addr) |
218 | 32993977 | Isaku Yamahata | { |
219 | 07323531 | Isaku Yamahata | uint8_t val; |
220 | 32993977 | Isaku Yamahata | val = ioport_read(0, addr);
|
221 | bd3c9aa5 | Prerna Saxena | trace_cpu_in(addr, val); |
222 | 07323531 | Isaku Yamahata | LOG_IOPORT("inb : %04"FMT_pioaddr" %02"PRIx8"\n", addr, val); |
223 | 32993977 | Isaku Yamahata | return val;
|
224 | 32993977 | Isaku Yamahata | } |
225 | 32993977 | Isaku Yamahata | |
226 | c227f099 | Anthony Liguori | uint16_t cpu_inw(pio_addr_t addr) |
227 | 32993977 | Isaku Yamahata | { |
228 | 07323531 | Isaku Yamahata | uint16_t val; |
229 | 32993977 | Isaku Yamahata | val = ioport_read(1, addr);
|
230 | bd3c9aa5 | Prerna Saxena | trace_cpu_in(addr, val); |
231 | 07323531 | Isaku Yamahata | LOG_IOPORT("inw : %04"FMT_pioaddr" %04"PRIx16"\n", addr, val); |
232 | 32993977 | Isaku Yamahata | return val;
|
233 | 32993977 | Isaku Yamahata | } |
234 | 32993977 | Isaku Yamahata | |
235 | c227f099 | Anthony Liguori | uint32_t cpu_inl(pio_addr_t addr) |
236 | 32993977 | Isaku Yamahata | { |
237 | 07323531 | Isaku Yamahata | uint32_t val; |
238 | 32993977 | Isaku Yamahata | val = ioport_read(2, addr);
|
239 | bd3c9aa5 | Prerna Saxena | trace_cpu_in(addr, val); |
240 | 07323531 | Isaku Yamahata | LOG_IOPORT("inl : %04"FMT_pioaddr" %08"PRIx32"\n", addr, val); |
241 | 32993977 | Isaku Yamahata | return val;
|
242 | 32993977 | Isaku Yamahata | } |