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