root / ioport.c @ c1568af5
History | View | Annotate | Download (6.8 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 | 32993977 | Isaku Yamahata | |
30 | 32993977 | Isaku Yamahata | /***********************************************************/
|
31 | 32993977 | Isaku Yamahata | /* IO Port */
|
32 | 32993977 | Isaku Yamahata | |
33 | 32993977 | Isaku Yamahata | //#define DEBUG_UNUSED_IOPORT
|
34 | 32993977 | Isaku Yamahata | //#define DEBUG_IOPORT
|
35 | 32993977 | Isaku Yamahata | |
36 | fc7083b5 | Isaku Yamahata | #ifdef DEBUG_UNUSED_IOPORT
|
37 | fc7083b5 | Isaku Yamahata | # define LOG_UNUSED_IOPORT(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__) |
38 | fc7083b5 | Isaku Yamahata | #else
|
39 | fc7083b5 | Isaku Yamahata | # define LOG_UNUSED_IOPORT(fmt, ...) do{ } while (0) |
40 | fc7083b5 | Isaku Yamahata | #endif
|
41 | fc7083b5 | Isaku Yamahata | |
42 | 32993977 | Isaku Yamahata | #ifdef DEBUG_IOPORT
|
43 | 32993977 | Isaku Yamahata | # define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__) |
44 | 32993977 | Isaku Yamahata | #else
|
45 | 32993977 | Isaku Yamahata | # define LOG_IOPORT(...) do { } while (0) |
46 | 32993977 | Isaku Yamahata | #endif
|
47 | 32993977 | Isaku Yamahata | |
48 | 32993977 | Isaku Yamahata | /* XXX: use a two level table to limit memory usage */
|
49 | 32993977 | Isaku Yamahata | |
50 | 32993977 | Isaku Yamahata | static void *ioport_opaque[MAX_IOPORTS]; |
51 | 32993977 | Isaku Yamahata | static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS]; |
52 | 32993977 | Isaku Yamahata | static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS]; |
53 | 32993977 | Isaku Yamahata | |
54 | 32993977 | Isaku Yamahata | static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
|
55 | 32993977 | Isaku Yamahata | static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
|
56 | 32993977 | Isaku Yamahata | |
57 | 32993977 | Isaku Yamahata | static uint32_t ioport_read(int index, uint32_t address) |
58 | 32993977 | Isaku Yamahata | { |
59 | 1dde6fcc | Blue Swirl | static IOPortReadFunc * const default_func[3] = { |
60 | 32993977 | Isaku Yamahata | default_ioport_readb, |
61 | 32993977 | Isaku Yamahata | default_ioport_readw, |
62 | 32993977 | Isaku Yamahata | default_ioport_readl |
63 | 32993977 | Isaku Yamahata | }; |
64 | 32993977 | Isaku Yamahata | IOPortReadFunc *func = ioport_read_table[index][address]; |
65 | 32993977 | Isaku Yamahata | if (!func)
|
66 | 32993977 | Isaku Yamahata | func = default_func[index]; |
67 | 32993977 | Isaku Yamahata | return func(ioport_opaque[address], address);
|
68 | 32993977 | Isaku Yamahata | } |
69 | 32993977 | Isaku Yamahata | |
70 | 32993977 | Isaku Yamahata | static void ioport_write(int index, uint32_t address, uint32_t data) |
71 | 32993977 | Isaku Yamahata | { |
72 | 1dde6fcc | Blue Swirl | static IOPortWriteFunc * const default_func[3] = { |
73 | 32993977 | Isaku Yamahata | default_ioport_writeb, |
74 | 32993977 | Isaku Yamahata | default_ioport_writew, |
75 | 32993977 | Isaku Yamahata | default_ioport_writel |
76 | 32993977 | Isaku Yamahata | }; |
77 | 32993977 | Isaku Yamahata | IOPortWriteFunc *func = ioport_write_table[index][address]; |
78 | 32993977 | Isaku Yamahata | if (!func)
|
79 | 32993977 | Isaku Yamahata | func = default_func[index]; |
80 | 32993977 | Isaku Yamahata | func(ioport_opaque[address], address, data); |
81 | 32993977 | Isaku Yamahata | } |
82 | 32993977 | Isaku Yamahata | |
83 | 32993977 | Isaku Yamahata | static uint32_t default_ioport_readb(void *opaque, uint32_t address) |
84 | 32993977 | Isaku Yamahata | { |
85 | fc7083b5 | Isaku Yamahata | LOG_UNUSED_IOPORT("unused inb: port=0x%04"PRIx32"\n", address); |
86 | 32993977 | Isaku Yamahata | return 0xff; |
87 | 32993977 | Isaku Yamahata | } |
88 | 32993977 | Isaku Yamahata | |
89 | 32993977 | Isaku Yamahata | static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data) |
90 | 32993977 | Isaku Yamahata | { |
91 | fc7083b5 | Isaku Yamahata | LOG_UNUSED_IOPORT("unused outb: port=0x%04"PRIx32" data=0x%02"PRIx32"\n", |
92 | fc7083b5 | Isaku Yamahata | address, data); |
93 | 32993977 | Isaku Yamahata | } |
94 | 32993977 | Isaku Yamahata | |
95 | 32993977 | Isaku Yamahata | /* default is to make two byte accesses */
|
96 | 32993977 | Isaku Yamahata | static uint32_t default_ioport_readw(void *opaque, uint32_t address) |
97 | 32993977 | Isaku Yamahata | { |
98 | 32993977 | Isaku Yamahata | uint32_t data; |
99 | 32993977 | Isaku Yamahata | data = ioport_read(0, address);
|
100 | d56dd6cf | Isaku Yamahata | address = (address + 1) & IOPORTS_MASK;
|
101 | 32993977 | Isaku Yamahata | data |= ioport_read(0, address) << 8; |
102 | 32993977 | Isaku Yamahata | return data;
|
103 | 32993977 | Isaku Yamahata | } |
104 | 32993977 | Isaku Yamahata | |
105 | 32993977 | Isaku Yamahata | static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data) |
106 | 32993977 | Isaku Yamahata | { |
107 | 32993977 | Isaku Yamahata | ioport_write(0, address, data & 0xff); |
108 | d56dd6cf | Isaku Yamahata | address = (address + 1) & IOPORTS_MASK;
|
109 | 32993977 | Isaku Yamahata | ioport_write(0, address, (data >> 8) & 0xff); |
110 | 32993977 | Isaku Yamahata | } |
111 | 32993977 | Isaku Yamahata | |
112 | 32993977 | Isaku Yamahata | static uint32_t default_ioport_readl(void *opaque, uint32_t address) |
113 | 32993977 | Isaku Yamahata | { |
114 | fc7083b5 | Isaku Yamahata | LOG_UNUSED_IOPORT("unused inl: port=0x%04"PRIx32"\n", address); |
115 | 32993977 | Isaku Yamahata | return 0xffffffff; |
116 | 32993977 | Isaku Yamahata | } |
117 | 32993977 | Isaku Yamahata | |
118 | 32993977 | Isaku Yamahata | static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data) |
119 | 32993977 | Isaku Yamahata | { |
120 | fc7083b5 | Isaku Yamahata | LOG_UNUSED_IOPORT("unused outl: port=0x%04"PRIx32" data=0x%02"PRIx32"\n", |
121 | fc7083b5 | Isaku Yamahata | address, data); |
122 | 32993977 | Isaku Yamahata | } |
123 | 32993977 | Isaku Yamahata | |
124 | 23e0affd | Isaku Yamahata | static int ioport_bsize(int size, int *bsize) |
125 | 23e0affd | Isaku Yamahata | { |
126 | 23e0affd | Isaku Yamahata | if (size == 1) { |
127 | 23e0affd | Isaku Yamahata | *bsize = 0;
|
128 | 23e0affd | Isaku Yamahata | } else if (size == 2) { |
129 | 23e0affd | Isaku Yamahata | *bsize = 1;
|
130 | 23e0affd | Isaku Yamahata | } else if (size == 4) { |
131 | 23e0affd | Isaku Yamahata | *bsize = 2;
|
132 | 23e0affd | Isaku Yamahata | } else {
|
133 | 23e0affd | Isaku Yamahata | return -1; |
134 | 23e0affd | Isaku Yamahata | } |
135 | 23e0affd | Isaku Yamahata | return 0; |
136 | 23e0affd | Isaku Yamahata | } |
137 | 23e0affd | Isaku Yamahata | |
138 | 32993977 | Isaku Yamahata | /* size is the word size in byte */
|
139 | c227f099 | Anthony Liguori | int register_ioport_read(pio_addr_t start, int length, int size, |
140 | 32993977 | Isaku Yamahata | IOPortReadFunc *func, void *opaque)
|
141 | 32993977 | Isaku Yamahata | { |
142 | 32993977 | Isaku Yamahata | int i, bsize;
|
143 | 32993977 | Isaku Yamahata | |
144 | 23e0affd | Isaku Yamahata | if (ioport_bsize(size, &bsize)) {
|
145 | 32993977 | Isaku Yamahata | hw_error("register_ioport_read: invalid size");
|
146 | 32993977 | Isaku Yamahata | return -1; |
147 | 32993977 | Isaku Yamahata | } |
148 | 32993977 | Isaku Yamahata | for(i = start; i < start + length; i += size) {
|
149 | 32993977 | Isaku Yamahata | ioport_read_table[bsize][i] = func; |
150 | 32993977 | Isaku Yamahata | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
151 | 32993977 | Isaku Yamahata | hw_error("register_ioport_read: invalid opaque");
|
152 | 32993977 | Isaku Yamahata | ioport_opaque[i] = opaque; |
153 | 32993977 | Isaku Yamahata | } |
154 | 32993977 | Isaku Yamahata | return 0; |
155 | 32993977 | Isaku Yamahata | } |
156 | 32993977 | Isaku Yamahata | |
157 | 32993977 | Isaku Yamahata | /* size is the word size in byte */
|
158 | c227f099 | Anthony Liguori | int register_ioport_write(pio_addr_t start, int length, int size, |
159 | 32993977 | Isaku Yamahata | IOPortWriteFunc *func, void *opaque)
|
160 | 32993977 | Isaku Yamahata | { |
161 | 32993977 | Isaku Yamahata | int i, bsize;
|
162 | 32993977 | Isaku Yamahata | |
163 | 23e0affd | Isaku Yamahata | if (ioport_bsize(size, &bsize)) {
|
164 | 32993977 | Isaku Yamahata | hw_error("register_ioport_write: invalid size");
|
165 | 32993977 | Isaku Yamahata | return -1; |
166 | 32993977 | Isaku Yamahata | } |
167 | 32993977 | Isaku Yamahata | for(i = start; i < start + length; i += size) {
|
168 | 32993977 | Isaku Yamahata | ioport_write_table[bsize][i] = func; |
169 | 32993977 | Isaku Yamahata | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
170 | 32993977 | Isaku Yamahata | hw_error("register_ioport_write: invalid opaque");
|
171 | 32993977 | Isaku Yamahata | ioport_opaque[i] = opaque; |
172 | 32993977 | Isaku Yamahata | } |
173 | 32993977 | Isaku Yamahata | return 0; |
174 | 32993977 | Isaku Yamahata | } |
175 | 32993977 | Isaku Yamahata | |
176 | c227f099 | Anthony Liguori | void isa_unassign_ioport(pio_addr_t start, int length) |
177 | 32993977 | Isaku Yamahata | { |
178 | 32993977 | Isaku Yamahata | int i;
|
179 | 32993977 | Isaku Yamahata | |
180 | 32993977 | Isaku Yamahata | for(i = start; i < start + length; i++) {
|
181 | 32993977 | Isaku Yamahata | ioport_read_table[0][i] = default_ioport_readb;
|
182 | 32993977 | Isaku Yamahata | ioport_read_table[1][i] = default_ioport_readw;
|
183 | 32993977 | Isaku Yamahata | ioport_read_table[2][i] = default_ioport_readl;
|
184 | 32993977 | Isaku Yamahata | |
185 | 32993977 | Isaku Yamahata | ioport_write_table[0][i] = default_ioport_writeb;
|
186 | 32993977 | Isaku Yamahata | ioport_write_table[1][i] = default_ioport_writew;
|
187 | 32993977 | Isaku Yamahata | ioport_write_table[2][i] = default_ioport_writel;
|
188 | 32993977 | Isaku Yamahata | |
189 | 32993977 | Isaku Yamahata | ioport_opaque[i] = NULL;
|
190 | 32993977 | Isaku Yamahata | } |
191 | 32993977 | Isaku Yamahata | } |
192 | 32993977 | Isaku Yamahata | |
193 | 32993977 | Isaku Yamahata | /***********************************************************/
|
194 | 32993977 | Isaku Yamahata | |
195 | c227f099 | Anthony Liguori | void cpu_outb(pio_addr_t addr, uint8_t val)
|
196 | 32993977 | Isaku Yamahata | { |
197 | 07323531 | Isaku Yamahata | LOG_IOPORT("outb: %04"FMT_pioaddr" %02"PRIx8"\n", addr, val); |
198 | 32993977 | Isaku Yamahata | ioport_write(0, addr, val);
|
199 | 32993977 | Isaku Yamahata | } |
200 | 32993977 | Isaku Yamahata | |
201 | c227f099 | Anthony Liguori | void cpu_outw(pio_addr_t addr, uint16_t val)
|
202 | 32993977 | Isaku Yamahata | { |
203 | 07323531 | Isaku Yamahata | LOG_IOPORT("outw: %04"FMT_pioaddr" %04"PRIx16"\n", addr, val); |
204 | 32993977 | Isaku Yamahata | ioport_write(1, addr, val);
|
205 | 32993977 | Isaku Yamahata | } |
206 | 32993977 | Isaku Yamahata | |
207 | c227f099 | Anthony Liguori | void cpu_outl(pio_addr_t addr, uint32_t val)
|
208 | 32993977 | Isaku Yamahata | { |
209 | 07323531 | Isaku Yamahata | LOG_IOPORT("outl: %04"FMT_pioaddr" %08"PRIx32"\n", addr, val); |
210 | 32993977 | Isaku Yamahata | ioport_write(2, addr, val);
|
211 | 32993977 | Isaku Yamahata | } |
212 | 32993977 | Isaku Yamahata | |
213 | c227f099 | Anthony Liguori | uint8_t cpu_inb(pio_addr_t addr) |
214 | 32993977 | Isaku Yamahata | { |
215 | 07323531 | Isaku Yamahata | uint8_t val; |
216 | 32993977 | Isaku Yamahata | val = ioport_read(0, addr);
|
217 | 07323531 | Isaku Yamahata | LOG_IOPORT("inb : %04"FMT_pioaddr" %02"PRIx8"\n", addr, val); |
218 | 32993977 | Isaku Yamahata | return val;
|
219 | 32993977 | Isaku Yamahata | } |
220 | 32993977 | Isaku Yamahata | |
221 | c227f099 | Anthony Liguori | uint16_t cpu_inw(pio_addr_t addr) |
222 | 32993977 | Isaku Yamahata | { |
223 | 07323531 | Isaku Yamahata | uint16_t val; |
224 | 32993977 | Isaku Yamahata | val = ioport_read(1, addr);
|
225 | 07323531 | Isaku Yamahata | LOG_IOPORT("inw : %04"FMT_pioaddr" %04"PRIx16"\n", addr, val); |
226 | 32993977 | Isaku Yamahata | return val;
|
227 | 32993977 | Isaku Yamahata | } |
228 | 32993977 | Isaku Yamahata | |
229 | c227f099 | Anthony Liguori | uint32_t cpu_inl(pio_addr_t addr) |
230 | 32993977 | Isaku Yamahata | { |
231 | 07323531 | Isaku Yamahata | uint32_t val; |
232 | 32993977 | Isaku Yamahata | val = ioport_read(2, addr);
|
233 | 07323531 | Isaku Yamahata | LOG_IOPORT("inl : %04"FMT_pioaddr" %08"PRIx32"\n", addr, val); |
234 | 32993977 | Isaku Yamahata | return val;
|
235 | 32993977 | Isaku Yamahata | } |