]>
Commit | Line | Data |
---|---|---|
1 | /* | |
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 "exec/ioport.h" | |
29 | #include "trace.h" | |
30 | #include "exec/memory.h" | |
31 | #include "exec/address-spaces.h" | |
32 | ||
33 | //#define DEBUG_IOPORT | |
34 | ||
35 | #ifdef DEBUG_IOPORT | |
36 | # define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__) | |
37 | #else | |
38 | # define LOG_IOPORT(...) do { } while (0) | |
39 | #endif | |
40 | ||
41 | typedef struct MemoryRegionPortioList { | |
42 | MemoryRegion mr; | |
43 | void *portio_opaque; | |
44 | MemoryRegionPortio ports[]; | |
45 | } MemoryRegionPortioList; | |
46 | ||
47 | static uint64_t unassigned_io_read(void *opaque, hwaddr addr, unsigned size) | |
48 | { | |
49 | return -1ULL; | |
50 | } | |
51 | ||
52 | static void unassigned_io_write(void *opaque, hwaddr addr, uint64_t val, | |
53 | unsigned size) | |
54 | { | |
55 | } | |
56 | ||
57 | const MemoryRegionOps unassigned_io_ops = { | |
58 | .read = unassigned_io_read, | |
59 | .write = unassigned_io_write, | |
60 | .endianness = DEVICE_NATIVE_ENDIAN, | |
61 | }; | |
62 | ||
63 | void cpu_outb(pio_addr_t addr, uint8_t val) | |
64 | { | |
65 | LOG_IOPORT("outb: %04"FMT_pioaddr" %02"PRIx8"\n", addr, val); | |
66 | trace_cpu_out(addr, val); | |
67 | address_space_write(&address_space_io, addr, &val, 1); | |
68 | } | |
69 | ||
70 | void cpu_outw(pio_addr_t addr, uint16_t val) | |
71 | { | |
72 | uint8_t buf[2]; | |
73 | ||
74 | LOG_IOPORT("outw: %04"FMT_pioaddr" %04"PRIx16"\n", addr, val); | |
75 | trace_cpu_out(addr, val); | |
76 | stw_p(buf, val); | |
77 | address_space_write(&address_space_io, addr, buf, 2); | |
78 | } | |
79 | ||
80 | void cpu_outl(pio_addr_t addr, uint32_t val) | |
81 | { | |
82 | uint8_t buf[4]; | |
83 | ||
84 | LOG_IOPORT("outl: %04"FMT_pioaddr" %08"PRIx32"\n", addr, val); | |
85 | trace_cpu_out(addr, val); | |
86 | stl_p(buf, val); | |
87 | address_space_write(&address_space_io, addr, buf, 4); | |
88 | } | |
89 | ||
90 | uint8_t cpu_inb(pio_addr_t addr) | |
91 | { | |
92 | uint8_t val; | |
93 | ||
94 | address_space_read(&address_space_io, addr, &val, 1); | |
95 | trace_cpu_in(addr, val); | |
96 | LOG_IOPORT("inb : %04"FMT_pioaddr" %02"PRIx8"\n", addr, val); | |
97 | return val; | |
98 | } | |
99 | ||
100 | uint16_t cpu_inw(pio_addr_t addr) | |
101 | { | |
102 | uint8_t buf[2]; | |
103 | uint16_t val; | |
104 | ||
105 | address_space_read(&address_space_io, addr, buf, 2); | |
106 | val = lduw_p(buf); | |
107 | trace_cpu_in(addr, val); | |
108 | LOG_IOPORT("inw : %04"FMT_pioaddr" %04"PRIx16"\n", addr, val); | |
109 | return val; | |
110 | } | |
111 | ||
112 | uint32_t cpu_inl(pio_addr_t addr) | |
113 | { | |
114 | uint8_t buf[4]; | |
115 | uint32_t val; | |
116 | ||
117 | address_space_read(&address_space_io, addr, buf, 4); | |
118 | val = ldl_p(buf); | |
119 | trace_cpu_in(addr, val); | |
120 | LOG_IOPORT("inl : %04"FMT_pioaddr" %08"PRIx32"\n", addr, val); | |
121 | return val; | |
122 | } | |
123 | ||
124 | void portio_list_init(PortioList *piolist, | |
125 | Object *owner, | |
126 | const MemoryRegionPortio *callbacks, | |
127 | void *opaque, const char *name) | |
128 | { | |
129 | unsigned n = 0; | |
130 | ||
131 | while (callbacks[n].size) { | |
132 | ++n; | |
133 | } | |
134 | ||
135 | piolist->ports = callbacks; | |
136 | piolist->nr = 0; | |
137 | piolist->regions = g_new0(MemoryRegion *, n); | |
138 | piolist->address_space = NULL; | |
139 | piolist->opaque = opaque; | |
140 | piolist->owner = owner; | |
141 | piolist->name = name; | |
142 | } | |
143 | ||
144 | void portio_list_destroy(PortioList *piolist) | |
145 | { | |
146 | g_free(piolist->regions); | |
147 | } | |
148 | ||
149 | static const MemoryRegionPortio *find_portio(MemoryRegionPortioList *mrpio, | |
150 | uint64_t offset, unsigned size, | |
151 | bool write) | |
152 | { | |
153 | const MemoryRegionPortio *mrp; | |
154 | ||
155 | for (mrp = mrpio->ports; mrp->size; ++mrp) { | |
156 | if (offset >= mrp->offset && offset < mrp->offset + mrp->len && | |
157 | size == mrp->size && | |
158 | (write ? (bool)mrp->write : (bool)mrp->read)) { | |
159 | return mrp; | |
160 | } | |
161 | } | |
162 | return NULL; | |
163 | } | |
164 | ||
165 | static uint64_t portio_read(void *opaque, hwaddr addr, unsigned size) | |
166 | { | |
167 | MemoryRegionPortioList *mrpio = opaque; | |
168 | const MemoryRegionPortio *mrp = find_portio(mrpio, addr, size, false); | |
169 | uint64_t data; | |
170 | ||
171 | data = ((uint64_t)1 << (size * 8)) - 1; | |
172 | if (mrp) { | |
173 | data = mrp->read(mrpio->portio_opaque, mrp->base + addr); | |
174 | } else if (size == 2) { | |
175 | mrp = find_portio(mrpio, addr, 1, false); | |
176 | assert(mrp); | |
177 | data = mrp->read(mrpio->portio_opaque, mrp->base + addr) | | |
178 | (mrp->read(mrpio->portio_opaque, mrp->base + addr + 1) << 8); | |
179 | } | |
180 | return data; | |
181 | } | |
182 | ||
183 | static void portio_write(void *opaque, hwaddr addr, uint64_t data, | |
184 | unsigned size) | |
185 | { | |
186 | MemoryRegionPortioList *mrpio = opaque; | |
187 | const MemoryRegionPortio *mrp = find_portio(mrpio, addr, size, true); | |
188 | ||
189 | if (mrp) { | |
190 | mrp->write(mrpio->portio_opaque, mrp->base + addr, data); | |
191 | } else if (size == 2) { | |
192 | mrp = find_portio(mrpio, addr, 1, true); | |
193 | assert(mrp); | |
194 | mrp->write(mrpio->portio_opaque, mrp->base + addr, data & 0xff); | |
195 | mrp->write(mrpio->portio_opaque, mrp->base + addr + 1, data >> 8); | |
196 | } | |
197 | } | |
198 | ||
199 | static const MemoryRegionOps portio_ops = { | |
200 | .read = portio_read, | |
201 | .write = portio_write, | |
202 | .endianness = DEVICE_LITTLE_ENDIAN, | |
203 | .valid.unaligned = true, | |
204 | .impl.unaligned = true, | |
205 | }; | |
206 | ||
207 | static void portio_list_add_1(PortioList *piolist, | |
208 | const MemoryRegionPortio *pio_init, | |
209 | unsigned count, unsigned start, | |
210 | unsigned off_low, unsigned off_high) | |
211 | { | |
212 | MemoryRegionPortioList *mrpio; | |
213 | unsigned i; | |
214 | ||
215 | /* Copy the sub-list and null-terminate it. */ | |
216 | mrpio = g_malloc0(sizeof(MemoryRegionPortioList) + | |
217 | sizeof(MemoryRegionPortio) * (count + 1)); | |
218 | mrpio->portio_opaque = piolist->opaque; | |
219 | memcpy(mrpio->ports, pio_init, sizeof(MemoryRegionPortio) * count); | |
220 | memset(mrpio->ports + count, 0, sizeof(MemoryRegionPortio)); | |
221 | ||
222 | /* Adjust the offsets to all be zero-based for the region. */ | |
223 | for (i = 0; i < count; ++i) { | |
224 | mrpio->ports[i].offset -= off_low; | |
225 | mrpio->ports[i].base = start + off_low; | |
226 | } | |
227 | ||
228 | /* | |
229 | * Use an alias so that the callback is called with an absolute address, | |
230 | * rather than an offset relative to to start + off_low. | |
231 | */ | |
232 | memory_region_init_io(&mrpio->mr, piolist->owner, &portio_ops, mrpio, | |
233 | piolist->name, off_high - off_low); | |
234 | memory_region_add_subregion(piolist->address_space, | |
235 | start + off_low, &mrpio->mr); | |
236 | piolist->regions[piolist->nr] = &mrpio->mr; | |
237 | ++piolist->nr; | |
238 | } | |
239 | ||
240 | void portio_list_add(PortioList *piolist, | |
241 | MemoryRegion *address_space, | |
242 | uint32_t start) | |
243 | { | |
244 | const MemoryRegionPortio *pio, *pio_start = piolist->ports; | |
245 | unsigned int off_low, off_high, off_last, count; | |
246 | ||
247 | piolist->address_space = address_space; | |
248 | ||
249 | /* Handle the first entry specially. */ | |
250 | off_last = off_low = pio_start->offset; | |
251 | off_high = off_low + pio_start->len; | |
252 | count = 1; | |
253 | ||
254 | for (pio = pio_start + 1; pio->size != 0; pio++, count++) { | |
255 | /* All entries must be sorted by offset. */ | |
256 | assert(pio->offset >= off_last); | |
257 | off_last = pio->offset; | |
258 | ||
259 | /* If we see a hole, break the region. */ | |
260 | if (off_last > off_high) { | |
261 | portio_list_add_1(piolist, pio_start, count, start, off_low, | |
262 | off_high); | |
263 | /* ... and start collecting anew. */ | |
264 | pio_start = pio; | |
265 | off_low = off_last; | |
266 | off_high = off_low + pio->len; | |
267 | count = 0; | |
268 | } else if (off_last + pio->len > off_high) { | |
269 | off_high = off_last + pio->len; | |
270 | } | |
271 | } | |
272 | ||
273 | /* There will always be an open sub-list. */ | |
274 | portio_list_add_1(piolist, pio_start, count, start, off_low, off_high); | |
275 | } | |
276 | ||
277 | void portio_list_del(PortioList *piolist) | |
278 | { | |
279 | MemoryRegionPortioList *mrpio; | |
280 | unsigned i; | |
281 | ||
282 | for (i = 0; i < piolist->nr; ++i) { | |
283 | mrpio = container_of(piolist->regions[i], MemoryRegionPortioList, mr); | |
284 | memory_region_del_subregion(piolist->address_space, &mrpio->mr); | |
285 | memory_region_destroy(&mrpio->mr); | |
286 | g_free(mrpio); | |
287 | piolist->regions[i] = NULL; | |
288 | } | |
289 | } |