root / ioport.c @ 250b086e
History | View | Annotate | Download (13.2 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 | 6bf9fd43 | Avi Kivity | #include "memory.h" |
31 | 32993977 | Isaku Yamahata | |
32 | 32993977 | Isaku Yamahata | /***********************************************************/
|
33 | 32993977 | Isaku Yamahata | /* IO Port */
|
34 | 32993977 | Isaku Yamahata | |
35 | 32993977 | Isaku Yamahata | //#define DEBUG_UNUSED_IOPORT
|
36 | 32993977 | Isaku Yamahata | //#define DEBUG_IOPORT
|
37 | 32993977 | Isaku Yamahata | |
38 | fc7083b5 | Isaku Yamahata | #ifdef DEBUG_UNUSED_IOPORT
|
39 | fc7083b5 | Isaku Yamahata | # define LOG_UNUSED_IOPORT(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__) |
40 | fc7083b5 | Isaku Yamahata | #else
|
41 | fc7083b5 | Isaku Yamahata | # define LOG_UNUSED_IOPORT(fmt, ...) do{ } while (0) |
42 | fc7083b5 | Isaku Yamahata | #endif
|
43 | fc7083b5 | Isaku Yamahata | |
44 | 32993977 | Isaku Yamahata | #ifdef DEBUG_IOPORT
|
45 | 32993977 | Isaku Yamahata | # define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__) |
46 | 32993977 | Isaku Yamahata | #else
|
47 | 32993977 | Isaku Yamahata | # define LOG_IOPORT(...) do { } while (0) |
48 | 32993977 | Isaku Yamahata | #endif
|
49 | 32993977 | Isaku Yamahata | |
50 | 32993977 | Isaku Yamahata | /* XXX: use a two level table to limit memory usage */
|
51 | 32993977 | Isaku Yamahata | |
52 | 32993977 | Isaku Yamahata | static void *ioport_opaque[MAX_IOPORTS]; |
53 | 32993977 | Isaku Yamahata | static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS]; |
54 | 32993977 | Isaku Yamahata | static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS]; |
55 | c5b703ac | Avi Kivity | static IOPortDestructor *ioport_destructor_table[MAX_IOPORTS];
|
56 | 32993977 | Isaku Yamahata | |
57 | 32993977 | Isaku Yamahata | static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
|
58 | 32993977 | Isaku Yamahata | static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
|
59 | 32993977 | Isaku Yamahata | |
60 | 32993977 | Isaku Yamahata | static uint32_t ioport_read(int index, uint32_t address) |
61 | 32993977 | Isaku Yamahata | { |
62 | 1dde6fcc | Blue Swirl | static IOPortReadFunc * const default_func[3] = { |
63 | 32993977 | Isaku Yamahata | default_ioport_readb, |
64 | 32993977 | Isaku Yamahata | default_ioport_readw, |
65 | 32993977 | Isaku Yamahata | default_ioport_readl |
66 | 32993977 | Isaku Yamahata | }; |
67 | 32993977 | Isaku Yamahata | IOPortReadFunc *func = ioport_read_table[index][address]; |
68 | 32993977 | Isaku Yamahata | if (!func)
|
69 | 32993977 | Isaku Yamahata | func = default_func[index]; |
70 | 32993977 | Isaku Yamahata | return func(ioport_opaque[address], address);
|
71 | 32993977 | Isaku Yamahata | } |
72 | 32993977 | Isaku Yamahata | |
73 | 32993977 | Isaku Yamahata | static void ioport_write(int index, uint32_t address, uint32_t data) |
74 | 32993977 | Isaku Yamahata | { |
75 | 1dde6fcc | Blue Swirl | static IOPortWriteFunc * const default_func[3] = { |
76 | 32993977 | Isaku Yamahata | default_ioport_writeb, |
77 | 32993977 | Isaku Yamahata | default_ioport_writew, |
78 | 32993977 | Isaku Yamahata | default_ioport_writel |
79 | 32993977 | Isaku Yamahata | }; |
80 | 32993977 | Isaku Yamahata | IOPortWriteFunc *func = ioport_write_table[index][address]; |
81 | 32993977 | Isaku Yamahata | if (!func)
|
82 | 32993977 | Isaku Yamahata | func = default_func[index]; |
83 | 32993977 | Isaku Yamahata | func(ioport_opaque[address], address, data); |
84 | 32993977 | Isaku Yamahata | } |
85 | 32993977 | Isaku Yamahata | |
86 | 32993977 | Isaku Yamahata | static uint32_t default_ioport_readb(void *opaque, uint32_t address) |
87 | 32993977 | Isaku Yamahata | { |
88 | fc7083b5 | Isaku Yamahata | LOG_UNUSED_IOPORT("unused inb: port=0x%04"PRIx32"\n", address); |
89 | 32993977 | Isaku Yamahata | return 0xff; |
90 | 32993977 | Isaku Yamahata | } |
91 | 32993977 | Isaku Yamahata | |
92 | 32993977 | Isaku Yamahata | static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data) |
93 | 32993977 | Isaku Yamahata | { |
94 | fc7083b5 | Isaku Yamahata | LOG_UNUSED_IOPORT("unused outb: port=0x%04"PRIx32" data=0x%02"PRIx32"\n", |
95 | fc7083b5 | Isaku Yamahata | address, data); |
96 | 32993977 | Isaku Yamahata | } |
97 | 32993977 | Isaku Yamahata | |
98 | 32993977 | Isaku Yamahata | /* default is to make two byte accesses */
|
99 | 32993977 | Isaku Yamahata | static uint32_t default_ioport_readw(void *opaque, uint32_t address) |
100 | 32993977 | Isaku Yamahata | { |
101 | 32993977 | Isaku Yamahata | uint32_t data; |
102 | 32993977 | Isaku Yamahata | data = ioport_read(0, address);
|
103 | d56dd6cf | Isaku Yamahata | address = (address + 1) & IOPORTS_MASK;
|
104 | 32993977 | Isaku Yamahata | data |= ioport_read(0, address) << 8; |
105 | 32993977 | Isaku Yamahata | return data;
|
106 | 32993977 | Isaku Yamahata | } |
107 | 32993977 | Isaku Yamahata | |
108 | 32993977 | Isaku Yamahata | static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data) |
109 | 32993977 | Isaku Yamahata | { |
110 | 32993977 | Isaku Yamahata | ioport_write(0, address, data & 0xff); |
111 | d56dd6cf | Isaku Yamahata | address = (address + 1) & IOPORTS_MASK;
|
112 | 32993977 | Isaku Yamahata | ioport_write(0, address, (data >> 8) & 0xff); |
113 | 32993977 | Isaku Yamahata | } |
114 | 32993977 | Isaku Yamahata | |
115 | 32993977 | Isaku Yamahata | static uint32_t default_ioport_readl(void *opaque, uint32_t address) |
116 | 32993977 | Isaku Yamahata | { |
117 | fc7083b5 | Isaku Yamahata | LOG_UNUSED_IOPORT("unused inl: port=0x%04"PRIx32"\n", address); |
118 | 32993977 | Isaku Yamahata | return 0xffffffff; |
119 | 32993977 | Isaku Yamahata | } |
120 | 32993977 | Isaku Yamahata | |
121 | 32993977 | Isaku Yamahata | static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data) |
122 | 32993977 | Isaku Yamahata | { |
123 | fc7083b5 | Isaku Yamahata | LOG_UNUSED_IOPORT("unused outl: port=0x%04"PRIx32" data=0x%02"PRIx32"\n", |
124 | fc7083b5 | Isaku Yamahata | address, data); |
125 | 32993977 | Isaku Yamahata | } |
126 | 32993977 | Isaku Yamahata | |
127 | 23e0affd | Isaku Yamahata | static int ioport_bsize(int size, int *bsize) |
128 | 23e0affd | Isaku Yamahata | { |
129 | 23e0affd | Isaku Yamahata | if (size == 1) { |
130 | 23e0affd | Isaku Yamahata | *bsize = 0;
|
131 | 23e0affd | Isaku Yamahata | } else if (size == 2) { |
132 | 23e0affd | Isaku Yamahata | *bsize = 1;
|
133 | 23e0affd | Isaku Yamahata | } else if (size == 4) { |
134 | 23e0affd | Isaku Yamahata | *bsize = 2;
|
135 | 23e0affd | Isaku Yamahata | } else {
|
136 | 23e0affd | Isaku Yamahata | return -1; |
137 | 23e0affd | Isaku Yamahata | } |
138 | 23e0affd | Isaku Yamahata | return 0; |
139 | 23e0affd | Isaku Yamahata | } |
140 | 23e0affd | Isaku Yamahata | |
141 | 32993977 | Isaku Yamahata | /* size is the word size in byte */
|
142 | c227f099 | Anthony Liguori | int register_ioport_read(pio_addr_t start, int length, int size, |
143 | 32993977 | Isaku Yamahata | IOPortReadFunc *func, void *opaque)
|
144 | 32993977 | Isaku Yamahata | { |
145 | 32993977 | Isaku Yamahata | int i, bsize;
|
146 | 32993977 | Isaku Yamahata | |
147 | 23e0affd | Isaku Yamahata | if (ioport_bsize(size, &bsize)) {
|
148 | 32993977 | Isaku Yamahata | hw_error("register_ioport_read: invalid size");
|
149 | 32993977 | Isaku Yamahata | return -1; |
150 | 32993977 | Isaku Yamahata | } |
151 | bf3fb0e1 | Avi Kivity | for(i = start; i < start + length; ++i) {
|
152 | 32993977 | Isaku Yamahata | ioport_read_table[bsize][i] = func; |
153 | 32993977 | Isaku Yamahata | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
154 | f66a99d7 | Andreas Fรคrber | hw_error("register_ioport_read: invalid opaque for address 0x%x",
|
155 | f66a99d7 | Andreas Fรคrber | i); |
156 | 32993977 | Isaku Yamahata | ioport_opaque[i] = opaque; |
157 | 32993977 | Isaku Yamahata | } |
158 | 32993977 | Isaku Yamahata | return 0; |
159 | 32993977 | Isaku Yamahata | } |
160 | 32993977 | Isaku Yamahata | |
161 | 32993977 | Isaku Yamahata | /* size is the word size in byte */
|
162 | c227f099 | Anthony Liguori | int register_ioport_write(pio_addr_t start, int length, int size, |
163 | 32993977 | Isaku Yamahata | IOPortWriteFunc *func, void *opaque)
|
164 | 32993977 | Isaku Yamahata | { |
165 | 32993977 | Isaku Yamahata | int i, bsize;
|
166 | 32993977 | Isaku Yamahata | |
167 | 23e0affd | Isaku Yamahata | if (ioport_bsize(size, &bsize)) {
|
168 | 32993977 | Isaku Yamahata | hw_error("register_ioport_write: invalid size");
|
169 | 32993977 | Isaku Yamahata | return -1; |
170 | 32993977 | Isaku Yamahata | } |
171 | bf3fb0e1 | Avi Kivity | for(i = start; i < start + length; ++i) {
|
172 | 32993977 | Isaku Yamahata | ioport_write_table[bsize][i] = func; |
173 | 32993977 | Isaku Yamahata | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
174 | f66a99d7 | Andreas Fรคrber | hw_error("register_ioport_write: invalid opaque for address 0x%x",
|
175 | f66a99d7 | Andreas Fรคrber | i); |
176 | 32993977 | Isaku Yamahata | ioport_opaque[i] = opaque; |
177 | 32993977 | Isaku Yamahata | } |
178 | 32993977 | Isaku Yamahata | return 0; |
179 | 32993977 | Isaku Yamahata | } |
180 | 32993977 | Isaku Yamahata | |
181 | acd1c812 | Avi Kivity | static uint32_t ioport_readb_thunk(void *opaque, uint32_t addr) |
182 | acd1c812 | Avi Kivity | { |
183 | acd1c812 | Avi Kivity | IORange *ioport = opaque; |
184 | acd1c812 | Avi Kivity | uint64_t data; |
185 | acd1c812 | Avi Kivity | |
186 | acd1c812 | Avi Kivity | ioport->ops->read(ioport, addr - ioport->base, 1, &data);
|
187 | acd1c812 | Avi Kivity | return data;
|
188 | acd1c812 | Avi Kivity | } |
189 | acd1c812 | Avi Kivity | |
190 | acd1c812 | Avi Kivity | static uint32_t ioport_readw_thunk(void *opaque, uint32_t addr) |
191 | acd1c812 | Avi Kivity | { |
192 | acd1c812 | Avi Kivity | IORange *ioport = opaque; |
193 | acd1c812 | Avi Kivity | uint64_t data; |
194 | acd1c812 | Avi Kivity | |
195 | acd1c812 | Avi Kivity | ioport->ops->read(ioport, addr - ioport->base, 2, &data);
|
196 | acd1c812 | Avi Kivity | return data;
|
197 | acd1c812 | Avi Kivity | } |
198 | acd1c812 | Avi Kivity | |
199 | acd1c812 | Avi Kivity | static uint32_t ioport_readl_thunk(void *opaque, uint32_t addr) |
200 | acd1c812 | Avi Kivity | { |
201 | acd1c812 | Avi Kivity | IORange *ioport = opaque; |
202 | acd1c812 | Avi Kivity | uint64_t data; |
203 | acd1c812 | Avi Kivity | |
204 | acd1c812 | Avi Kivity | ioport->ops->read(ioport, addr - ioport->base, 4, &data);
|
205 | acd1c812 | Avi Kivity | return data;
|
206 | acd1c812 | Avi Kivity | } |
207 | acd1c812 | Avi Kivity | |
208 | acd1c812 | Avi Kivity | static void ioport_writeb_thunk(void *opaque, uint32_t addr, uint32_t data) |
209 | acd1c812 | Avi Kivity | { |
210 | acd1c812 | Avi Kivity | IORange *ioport = opaque; |
211 | acd1c812 | Avi Kivity | |
212 | acd1c812 | Avi Kivity | ioport->ops->write(ioport, addr - ioport->base, 1, data);
|
213 | acd1c812 | Avi Kivity | } |
214 | acd1c812 | Avi Kivity | |
215 | acd1c812 | Avi Kivity | static void ioport_writew_thunk(void *opaque, uint32_t addr, uint32_t data) |
216 | acd1c812 | Avi Kivity | { |
217 | acd1c812 | Avi Kivity | IORange *ioport = opaque; |
218 | acd1c812 | Avi Kivity | |
219 | acd1c812 | Avi Kivity | ioport->ops->write(ioport, addr - ioport->base, 2, data);
|
220 | acd1c812 | Avi Kivity | } |
221 | acd1c812 | Avi Kivity | |
222 | acd1c812 | Avi Kivity | static void ioport_writel_thunk(void *opaque, uint32_t addr, uint32_t data) |
223 | acd1c812 | Avi Kivity | { |
224 | acd1c812 | Avi Kivity | IORange *ioport = opaque; |
225 | acd1c812 | Avi Kivity | |
226 | acd1c812 | Avi Kivity | ioport->ops->write(ioport, addr - ioport->base, 4, data);
|
227 | acd1c812 | Avi Kivity | } |
228 | acd1c812 | Avi Kivity | |
229 | c5b703ac | Avi Kivity | static void iorange_destructor_thunk(void *opaque) |
230 | c5b703ac | Avi Kivity | { |
231 | c5b703ac | Avi Kivity | IORange *iorange = opaque; |
232 | c5b703ac | Avi Kivity | |
233 | c5b703ac | Avi Kivity | if (iorange->ops->destructor) {
|
234 | c5b703ac | Avi Kivity | iorange->ops->destructor(iorange); |
235 | c5b703ac | Avi Kivity | } |
236 | c5b703ac | Avi Kivity | } |
237 | c5b703ac | Avi Kivity | |
238 | acd1c812 | Avi Kivity | void ioport_register(IORange *ioport)
|
239 | acd1c812 | Avi Kivity | { |
240 | acd1c812 | Avi Kivity | register_ioport_read(ioport->base, ioport->len, 1,
|
241 | acd1c812 | Avi Kivity | ioport_readb_thunk, ioport); |
242 | acd1c812 | Avi Kivity | register_ioport_read(ioport->base, ioport->len, 2,
|
243 | acd1c812 | Avi Kivity | ioport_readw_thunk, ioport); |
244 | acd1c812 | Avi Kivity | register_ioport_read(ioport->base, ioport->len, 4,
|
245 | acd1c812 | Avi Kivity | ioport_readl_thunk, ioport); |
246 | acd1c812 | Avi Kivity | register_ioport_write(ioport->base, ioport->len, 1,
|
247 | acd1c812 | Avi Kivity | ioport_writeb_thunk, ioport); |
248 | acd1c812 | Avi Kivity | register_ioport_write(ioport->base, ioport->len, 2,
|
249 | acd1c812 | Avi Kivity | ioport_writew_thunk, ioport); |
250 | acd1c812 | Avi Kivity | register_ioport_write(ioport->base, ioport->len, 4,
|
251 | acd1c812 | Avi Kivity | ioport_writel_thunk, ioport); |
252 | c5b703ac | Avi Kivity | ioport_destructor_table[ioport->base] = iorange_destructor_thunk; |
253 | acd1c812 | Avi Kivity | } |
254 | acd1c812 | Avi Kivity | |
255 | c227f099 | Anthony Liguori | void isa_unassign_ioport(pio_addr_t start, int length) |
256 | 32993977 | Isaku Yamahata | { |
257 | 32993977 | Isaku Yamahata | int i;
|
258 | 32993977 | Isaku Yamahata | |
259 | c5b703ac | Avi Kivity | if (ioport_destructor_table[start]) {
|
260 | c5b703ac | Avi Kivity | ioport_destructor_table[start](ioport_opaque[start]); |
261 | c5b703ac | Avi Kivity | ioport_destructor_table[start] = NULL;
|
262 | c5b703ac | Avi Kivity | } |
263 | 32993977 | Isaku Yamahata | for(i = start; i < start + length; i++) {
|
264 | 6141dbfe | Paolo Bonzini | ioport_read_table[0][i] = NULL; |
265 | 6141dbfe | Paolo Bonzini | ioport_read_table[1][i] = NULL; |
266 | 6141dbfe | Paolo Bonzini | ioport_read_table[2][i] = NULL; |
267 | 32993977 | Isaku Yamahata | |
268 | 6141dbfe | Paolo Bonzini | ioport_write_table[0][i] = NULL; |
269 | 6141dbfe | Paolo Bonzini | ioport_write_table[1][i] = NULL; |
270 | 6141dbfe | Paolo Bonzini | ioport_write_table[2][i] = NULL; |
271 | 32993977 | Isaku Yamahata | |
272 | 32993977 | Isaku Yamahata | ioport_opaque[i] = NULL;
|
273 | 32993977 | Isaku Yamahata | } |
274 | 32993977 | Isaku Yamahata | } |
275 | 32993977 | Isaku Yamahata | |
276 | 6141dbfe | Paolo Bonzini | bool isa_is_ioport_assigned(pio_addr_t start)
|
277 | 6141dbfe | Paolo Bonzini | { |
278 | 6141dbfe | Paolo Bonzini | return (ioport_read_table[0][start] || ioport_write_table[0][start] || |
279 | 6141dbfe | Paolo Bonzini | ioport_read_table[1][start] || ioport_write_table[1][start] || |
280 | 6141dbfe | Paolo Bonzini | ioport_read_table[2][start] || ioport_write_table[2][start]); |
281 | 6141dbfe | Paolo Bonzini | } |
282 | 6141dbfe | Paolo Bonzini | |
283 | 32993977 | Isaku Yamahata | /***********************************************************/
|
284 | 32993977 | Isaku Yamahata | |
285 | c227f099 | Anthony Liguori | void cpu_outb(pio_addr_t addr, uint8_t val)
|
286 | 32993977 | Isaku Yamahata | { |
287 | 07323531 | Isaku Yamahata | LOG_IOPORT("outb: %04"FMT_pioaddr" %02"PRIx8"\n", addr, val); |
288 | bd3c9aa5 | Prerna Saxena | trace_cpu_out(addr, val); |
289 | 32993977 | Isaku Yamahata | ioport_write(0, addr, val);
|
290 | 32993977 | Isaku Yamahata | } |
291 | 32993977 | Isaku Yamahata | |
292 | c227f099 | Anthony Liguori | void cpu_outw(pio_addr_t addr, uint16_t val)
|
293 | 32993977 | Isaku Yamahata | { |
294 | 07323531 | Isaku Yamahata | LOG_IOPORT("outw: %04"FMT_pioaddr" %04"PRIx16"\n", addr, val); |
295 | bd3c9aa5 | Prerna Saxena | trace_cpu_out(addr, val); |
296 | 32993977 | Isaku Yamahata | ioport_write(1, addr, val);
|
297 | 32993977 | Isaku Yamahata | } |
298 | 32993977 | Isaku Yamahata | |
299 | c227f099 | Anthony Liguori | void cpu_outl(pio_addr_t addr, uint32_t val)
|
300 | 32993977 | Isaku Yamahata | { |
301 | 07323531 | Isaku Yamahata | LOG_IOPORT("outl: %04"FMT_pioaddr" %08"PRIx32"\n", addr, val); |
302 | bd3c9aa5 | Prerna Saxena | trace_cpu_out(addr, val); |
303 | 32993977 | Isaku Yamahata | ioport_write(2, addr, val);
|
304 | 32993977 | Isaku Yamahata | } |
305 | 32993977 | Isaku Yamahata | |
306 | c227f099 | Anthony Liguori | uint8_t cpu_inb(pio_addr_t addr) |
307 | 32993977 | Isaku Yamahata | { |
308 | 07323531 | Isaku Yamahata | uint8_t val; |
309 | 32993977 | Isaku Yamahata | val = ioport_read(0, addr);
|
310 | bd3c9aa5 | Prerna Saxena | trace_cpu_in(addr, val); |
311 | 07323531 | Isaku Yamahata | LOG_IOPORT("inb : %04"FMT_pioaddr" %02"PRIx8"\n", addr, val); |
312 | 32993977 | Isaku Yamahata | return val;
|
313 | 32993977 | Isaku Yamahata | } |
314 | 32993977 | Isaku Yamahata | |
315 | c227f099 | Anthony Liguori | uint16_t cpu_inw(pio_addr_t addr) |
316 | 32993977 | Isaku Yamahata | { |
317 | 07323531 | Isaku Yamahata | uint16_t val; |
318 | 32993977 | Isaku Yamahata | val = ioport_read(1, addr);
|
319 | bd3c9aa5 | Prerna Saxena | trace_cpu_in(addr, val); |
320 | 07323531 | Isaku Yamahata | LOG_IOPORT("inw : %04"FMT_pioaddr" %04"PRIx16"\n", addr, val); |
321 | 32993977 | Isaku Yamahata | return val;
|
322 | 32993977 | Isaku Yamahata | } |
323 | 32993977 | Isaku Yamahata | |
324 | c227f099 | Anthony Liguori | uint32_t cpu_inl(pio_addr_t addr) |
325 | 32993977 | Isaku Yamahata | { |
326 | 07323531 | Isaku Yamahata | uint32_t val; |
327 | 32993977 | Isaku Yamahata | val = ioport_read(2, addr);
|
328 | bd3c9aa5 | Prerna Saxena | trace_cpu_in(addr, val); |
329 | 07323531 | Isaku Yamahata | LOG_IOPORT("inl : %04"FMT_pioaddr" %08"PRIx32"\n", addr, val); |
330 | 32993977 | Isaku Yamahata | return val;
|
331 | 32993977 | Isaku Yamahata | } |
332 | 6bf9fd43 | Avi Kivity | |
333 | 6bf9fd43 | Avi Kivity | void portio_list_init(PortioList *piolist,
|
334 | 6bf9fd43 | Avi Kivity | const MemoryRegionPortio *callbacks,
|
335 | 6bf9fd43 | Avi Kivity | void *opaque, const char *name) |
336 | 6bf9fd43 | Avi Kivity | { |
337 | 6bf9fd43 | Avi Kivity | unsigned n = 0; |
338 | 6bf9fd43 | Avi Kivity | |
339 | 6bf9fd43 | Avi Kivity | while (callbacks[n].size) {
|
340 | 6bf9fd43 | Avi Kivity | ++n; |
341 | 6bf9fd43 | Avi Kivity | } |
342 | 6bf9fd43 | Avi Kivity | |
343 | 6bf9fd43 | Avi Kivity | piolist->ports = callbacks; |
344 | 6bf9fd43 | Avi Kivity | piolist->nr = 0;
|
345 | 6bf9fd43 | Avi Kivity | piolist->regions = g_new0(MemoryRegion *, n); |
346 | de58ac72 | Avi Kivity | piolist->aliases = g_new0(MemoryRegion *, n); |
347 | 6bf9fd43 | Avi Kivity | piolist->address_space = NULL;
|
348 | 6bf9fd43 | Avi Kivity | piolist->opaque = opaque; |
349 | 6bf9fd43 | Avi Kivity | piolist->name = name; |
350 | 6bf9fd43 | Avi Kivity | } |
351 | 6bf9fd43 | Avi Kivity | |
352 | 6bf9fd43 | Avi Kivity | void portio_list_destroy(PortioList *piolist)
|
353 | 6bf9fd43 | Avi Kivity | { |
354 | 6bf9fd43 | Avi Kivity | g_free(piolist->regions); |
355 | de58ac72 | Avi Kivity | g_free(piolist->aliases); |
356 | 6bf9fd43 | Avi Kivity | } |
357 | 6bf9fd43 | Avi Kivity | |
358 | 6bf9fd43 | Avi Kivity | static void portio_list_add_1(PortioList *piolist, |
359 | 6bf9fd43 | Avi Kivity | const MemoryRegionPortio *pio_init,
|
360 | 6bf9fd43 | Avi Kivity | unsigned count, unsigned start, |
361 | 6bf9fd43 | Avi Kivity | unsigned off_low, unsigned off_high) |
362 | 6bf9fd43 | Avi Kivity | { |
363 | 6bf9fd43 | Avi Kivity | MemoryRegionPortio *pio; |
364 | 6bf9fd43 | Avi Kivity | MemoryRegionOps *ops; |
365 | de58ac72 | Avi Kivity | MemoryRegion *region, *alias; |
366 | 6bf9fd43 | Avi Kivity | unsigned i;
|
367 | 6bf9fd43 | Avi Kivity | |
368 | 6bf9fd43 | Avi Kivity | /* Copy the sub-list and null-terminate it. */
|
369 | 6bf9fd43 | Avi Kivity | pio = g_new(MemoryRegionPortio, count + 1);
|
370 | 6bf9fd43 | Avi Kivity | memcpy(pio, pio_init, sizeof(MemoryRegionPortio) * count);
|
371 | 6bf9fd43 | Avi Kivity | memset(pio + count, 0, sizeof(MemoryRegionPortio)); |
372 | 6bf9fd43 | Avi Kivity | |
373 | 6bf9fd43 | Avi Kivity | /* Adjust the offsets to all be zero-based for the region. */
|
374 | 6bf9fd43 | Avi Kivity | for (i = 0; i < count; ++i) { |
375 | 6bf9fd43 | Avi Kivity | pio[i].offset -= off_low; |
376 | 6bf9fd43 | Avi Kivity | } |
377 | 6bf9fd43 | Avi Kivity | |
378 | 6bf9fd43 | Avi Kivity | ops = g_new0(MemoryRegionOps, 1);
|
379 | 6bf9fd43 | Avi Kivity | ops->old_portio = pio; |
380 | 6bf9fd43 | Avi Kivity | |
381 | 6bf9fd43 | Avi Kivity | region = g_new(MemoryRegion, 1);
|
382 | de58ac72 | Avi Kivity | alias = g_new(MemoryRegion, 1);
|
383 | de58ac72 | Avi Kivity | /*
|
384 | de58ac72 | Avi Kivity | * Use an alias so that the callback is called with an absolute address,
|
385 | de58ac72 | Avi Kivity | * rather than an offset relative to to start + off_low.
|
386 | de58ac72 | Avi Kivity | */
|
387 | 6bf9fd43 | Avi Kivity | memory_region_init_io(region, ops, piolist->opaque, piolist->name, |
388 | de58ac72 | Avi Kivity | UINT64_MAX); |
389 | de58ac72 | Avi Kivity | memory_region_init_alias(alias, piolist->name, |
390 | de58ac72 | Avi Kivity | region, start + off_low, off_high - off_low); |
391 | 6bf9fd43 | Avi Kivity | memory_region_add_subregion(piolist->address_space, |
392 | de58ac72 | Avi Kivity | start + off_low, alias); |
393 | de58ac72 | Avi Kivity | piolist->regions[piolist->nr] = region; |
394 | de58ac72 | Avi Kivity | piolist->aliases[piolist->nr] = alias; |
395 | de58ac72 | Avi Kivity | ++piolist->nr; |
396 | 6bf9fd43 | Avi Kivity | } |
397 | 6bf9fd43 | Avi Kivity | |
398 | 6bf9fd43 | Avi Kivity | void portio_list_add(PortioList *piolist,
|
399 | 6bf9fd43 | Avi Kivity | MemoryRegion *address_space, |
400 | 6bf9fd43 | Avi Kivity | uint32_t start) |
401 | 6bf9fd43 | Avi Kivity | { |
402 | 6bf9fd43 | Avi Kivity | const MemoryRegionPortio *pio, *pio_start = piolist->ports;
|
403 | 6bf9fd43 | Avi Kivity | unsigned int off_low, off_high, off_last, count; |
404 | 6bf9fd43 | Avi Kivity | |
405 | 6bf9fd43 | Avi Kivity | piolist->address_space = address_space; |
406 | 6bf9fd43 | Avi Kivity | |
407 | 6bf9fd43 | Avi Kivity | /* Handle the first entry specially. */
|
408 | 6bf9fd43 | Avi Kivity | off_last = off_low = pio_start->offset; |
409 | 6bf9fd43 | Avi Kivity | off_high = off_low + pio_start->len; |
410 | 6bf9fd43 | Avi Kivity | count = 1;
|
411 | 6bf9fd43 | Avi Kivity | |
412 | 6bf9fd43 | Avi Kivity | for (pio = pio_start + 1; pio->size != 0; pio++, count++) { |
413 | 6bf9fd43 | Avi Kivity | /* All entries must be sorted by offset. */
|
414 | 6bf9fd43 | Avi Kivity | assert(pio->offset >= off_last); |
415 | 6bf9fd43 | Avi Kivity | off_last = pio->offset; |
416 | 6bf9fd43 | Avi Kivity | |
417 | 6bf9fd43 | Avi Kivity | /* If we see a hole, break the region. */
|
418 | 6bf9fd43 | Avi Kivity | if (off_last > off_high) {
|
419 | 6bf9fd43 | Avi Kivity | portio_list_add_1(piolist, pio_start, count, start, off_low, |
420 | 6bf9fd43 | Avi Kivity | off_high); |
421 | 6bf9fd43 | Avi Kivity | /* ... and start collecting anew. */
|
422 | 6bf9fd43 | Avi Kivity | pio_start = pio; |
423 | 6bf9fd43 | Avi Kivity | off_low = off_last; |
424 | 6bf9fd43 | Avi Kivity | off_high = off_low + pio->len; |
425 | 6bf9fd43 | Avi Kivity | count = 0;
|
426 | 6bf9fd43 | Avi Kivity | } else if (off_last + pio->len > off_high) { |
427 | 6bf9fd43 | Avi Kivity | off_high = off_last + pio->len; |
428 | 6bf9fd43 | Avi Kivity | } |
429 | 6bf9fd43 | Avi Kivity | } |
430 | 6bf9fd43 | Avi Kivity | |
431 | 6bf9fd43 | Avi Kivity | /* There will always be an open sub-list. */
|
432 | 6bf9fd43 | Avi Kivity | portio_list_add_1(piolist, pio_start, count, start, off_low, off_high); |
433 | 6bf9fd43 | Avi Kivity | } |
434 | 6bf9fd43 | Avi Kivity | |
435 | 6bf9fd43 | Avi Kivity | void portio_list_del(PortioList *piolist)
|
436 | 6bf9fd43 | Avi Kivity | { |
437 | de58ac72 | Avi Kivity | MemoryRegion *mr, *alias; |
438 | 6bf9fd43 | Avi Kivity | unsigned i;
|
439 | 6bf9fd43 | Avi Kivity | |
440 | 6bf9fd43 | Avi Kivity | for (i = 0; i < piolist->nr; ++i) { |
441 | 6bf9fd43 | Avi Kivity | mr = piolist->regions[i]; |
442 | de58ac72 | Avi Kivity | alias = piolist->aliases[i]; |
443 | de58ac72 | Avi Kivity | memory_region_del_subregion(piolist->address_space, alias); |
444 | de58ac72 | Avi Kivity | memory_region_destroy(alias); |
445 | 6bf9fd43 | Avi Kivity | memory_region_destroy(mr); |
446 | 6bf9fd43 | Avi Kivity | g_free((MemoryRegionOps *)mr->ops); |
447 | 6bf9fd43 | Avi Kivity | g_free(mr); |
448 | de58ac72 | Avi Kivity | g_free(alias); |
449 | 6bf9fd43 | Avi Kivity | piolist->regions[i] = NULL;
|
450 | de58ac72 | Avi Kivity | piolist->aliases[i] = NULL;
|
451 | 6bf9fd43 | Avi Kivity | } |
452 | 6bf9fd43 | Avi Kivity | } |