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