Isaku Yamahata | 3299397 | 2009-07-02 19:32:06 +0900 | [diff] [blame] | 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 | |
Peter Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 28 | #include "qemu/osdep.h" |
Paolo Bonzini | 33c1187 | 2016-03-15 16:58:45 +0100 | [diff] [blame] | 29 | #include "qemu-common.h" |
| 30 | #include "cpu.h" |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 31 | #include "exec/ioport.h" |
Daniel P. Berrange | 0ab8ed1 | 2017-01-25 16:14:15 +0000 | [diff] [blame] | 32 | #include "trace-root.h" |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 33 | #include "exec/memory.h" |
Jan Kiszka | b40acf9 | 2013-06-24 10:45:09 +0200 | [diff] [blame] | 34 | #include "exec/address-spaces.h" |
Isaku Yamahata | 3299397 | 2009-07-02 19:32:06 +0900 | [diff] [blame] | 35 | |
Jan Kiszka | b40acf9 | 2013-06-24 10:45:09 +0200 | [diff] [blame] | 36 | typedef struct MemoryRegionPortioList { |
| 37 | MemoryRegion mr; |
| 38 | void *portio_opaque; |
| 39 | MemoryRegionPortio ports[]; |
| 40 | } MemoryRegionPortioList; |
| 41 | |
Jan Kiszka | 3bb28b7 | 2013-09-02 18:43:30 +0200 | [diff] [blame] | 42 | static uint64_t unassigned_io_read(void *opaque, hwaddr addr, unsigned size) |
| 43 | { |
| 44 | return -1ULL; |
| 45 | } |
| 46 | |
| 47 | static void unassigned_io_write(void *opaque, hwaddr addr, uint64_t val, |
| 48 | unsigned size) |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | const MemoryRegionOps unassigned_io_ops = { |
| 53 | .read = unassigned_io_read, |
| 54 | .write = unassigned_io_write, |
| 55 | .endianness = DEVICE_NATIVE_ENDIAN, |
| 56 | }; |
| 57 | |
Paolo Bonzini | 89a80e7 | 2016-03-16 10:20:34 +0100 | [diff] [blame] | 58 | void cpu_outb(uint32_t addr, uint8_t val) |
Isaku Yamahata | 3299397 | 2009-07-02 19:32:06 +0900 | [diff] [blame] | 59 | { |
Paolo Bonzini | 6f94b7d | 2015-10-16 15:08:34 +0200 | [diff] [blame] | 60 | trace_cpu_out(addr, 'b', val); |
Peter Maydell | 5c9eb02 | 2015-04-26 16:49:24 +0100 | [diff] [blame] | 61 | address_space_write(&address_space_io, addr, MEMTXATTRS_UNSPECIFIED, |
| 62 | &val, 1); |
Isaku Yamahata | 3299397 | 2009-07-02 19:32:06 +0900 | [diff] [blame] | 63 | } |
| 64 | |
Paolo Bonzini | 89a80e7 | 2016-03-16 10:20:34 +0100 | [diff] [blame] | 65 | void cpu_outw(uint32_t addr, uint16_t val) |
Isaku Yamahata | 3299397 | 2009-07-02 19:32:06 +0900 | [diff] [blame] | 66 | { |
Jan Kiszka | b40acf9 | 2013-06-24 10:45:09 +0200 | [diff] [blame] | 67 | uint8_t buf[2]; |
| 68 | |
Paolo Bonzini | 6f94b7d | 2015-10-16 15:08:34 +0200 | [diff] [blame] | 69 | trace_cpu_out(addr, 'w', val); |
Jan Kiszka | b40acf9 | 2013-06-24 10:45:09 +0200 | [diff] [blame] | 70 | stw_p(buf, val); |
Peter Maydell | 5c9eb02 | 2015-04-26 16:49:24 +0100 | [diff] [blame] | 71 | address_space_write(&address_space_io, addr, MEMTXATTRS_UNSPECIFIED, |
| 72 | buf, 2); |
Isaku Yamahata | 3299397 | 2009-07-02 19:32:06 +0900 | [diff] [blame] | 73 | } |
| 74 | |
Paolo Bonzini | 89a80e7 | 2016-03-16 10:20:34 +0100 | [diff] [blame] | 75 | void cpu_outl(uint32_t addr, uint32_t val) |
Isaku Yamahata | 3299397 | 2009-07-02 19:32:06 +0900 | [diff] [blame] | 76 | { |
Jan Kiszka | b40acf9 | 2013-06-24 10:45:09 +0200 | [diff] [blame] | 77 | uint8_t buf[4]; |
| 78 | |
Paolo Bonzini | 6f94b7d | 2015-10-16 15:08:34 +0200 | [diff] [blame] | 79 | trace_cpu_out(addr, 'l', val); |
Jan Kiszka | b40acf9 | 2013-06-24 10:45:09 +0200 | [diff] [blame] | 80 | stl_p(buf, val); |
Peter Maydell | 5c9eb02 | 2015-04-26 16:49:24 +0100 | [diff] [blame] | 81 | address_space_write(&address_space_io, addr, MEMTXATTRS_UNSPECIFIED, |
| 82 | buf, 4); |
Isaku Yamahata | 3299397 | 2009-07-02 19:32:06 +0900 | [diff] [blame] | 83 | } |
| 84 | |
Paolo Bonzini | 89a80e7 | 2016-03-16 10:20:34 +0100 | [diff] [blame] | 85 | uint8_t cpu_inb(uint32_t addr) |
Isaku Yamahata | 3299397 | 2009-07-02 19:32:06 +0900 | [diff] [blame] | 86 | { |
Isaku Yamahata | 0732353 | 2009-07-14 19:10:43 +0900 | [diff] [blame] | 87 | uint8_t val; |
Jan Kiszka | b40acf9 | 2013-06-24 10:45:09 +0200 | [diff] [blame] | 88 | |
Peter Maydell | 5c9eb02 | 2015-04-26 16:49:24 +0100 | [diff] [blame] | 89 | address_space_read(&address_space_io, addr, MEMTXATTRS_UNSPECIFIED, |
| 90 | &val, 1); |
Paolo Bonzini | 6f94b7d | 2015-10-16 15:08:34 +0200 | [diff] [blame] | 91 | trace_cpu_in(addr, 'b', val); |
Isaku Yamahata | 3299397 | 2009-07-02 19:32:06 +0900 | [diff] [blame] | 92 | return val; |
| 93 | } |
| 94 | |
Paolo Bonzini | 89a80e7 | 2016-03-16 10:20:34 +0100 | [diff] [blame] | 95 | uint16_t cpu_inw(uint32_t addr) |
Isaku Yamahata | 3299397 | 2009-07-02 19:32:06 +0900 | [diff] [blame] | 96 | { |
Jan Kiszka | b40acf9 | 2013-06-24 10:45:09 +0200 | [diff] [blame] | 97 | uint8_t buf[2]; |
Isaku Yamahata | 0732353 | 2009-07-14 19:10:43 +0900 | [diff] [blame] | 98 | uint16_t val; |
Jan Kiszka | b40acf9 | 2013-06-24 10:45:09 +0200 | [diff] [blame] | 99 | |
Peter Maydell | 5c9eb02 | 2015-04-26 16:49:24 +0100 | [diff] [blame] | 100 | address_space_read(&address_space_io, addr, MEMTXATTRS_UNSPECIFIED, buf, 2); |
Jan Kiszka | b40acf9 | 2013-06-24 10:45:09 +0200 | [diff] [blame] | 101 | val = lduw_p(buf); |
Paolo Bonzini | 6f94b7d | 2015-10-16 15:08:34 +0200 | [diff] [blame] | 102 | trace_cpu_in(addr, 'w', val); |
Isaku Yamahata | 3299397 | 2009-07-02 19:32:06 +0900 | [diff] [blame] | 103 | return val; |
| 104 | } |
| 105 | |
Paolo Bonzini | 89a80e7 | 2016-03-16 10:20:34 +0100 | [diff] [blame] | 106 | uint32_t cpu_inl(uint32_t addr) |
Isaku Yamahata | 3299397 | 2009-07-02 19:32:06 +0900 | [diff] [blame] | 107 | { |
Jan Kiszka | b40acf9 | 2013-06-24 10:45:09 +0200 | [diff] [blame] | 108 | uint8_t buf[4]; |
Isaku Yamahata | 0732353 | 2009-07-14 19:10:43 +0900 | [diff] [blame] | 109 | uint32_t val; |
Jan Kiszka | b40acf9 | 2013-06-24 10:45:09 +0200 | [diff] [blame] | 110 | |
Peter Maydell | 5c9eb02 | 2015-04-26 16:49:24 +0100 | [diff] [blame] | 111 | address_space_read(&address_space_io, addr, MEMTXATTRS_UNSPECIFIED, buf, 4); |
Jan Kiszka | b40acf9 | 2013-06-24 10:45:09 +0200 | [diff] [blame] | 112 | val = ldl_p(buf); |
Paolo Bonzini | 6f94b7d | 2015-10-16 15:08:34 +0200 | [diff] [blame] | 113 | trace_cpu_in(addr, 'l', val); |
Isaku Yamahata | 3299397 | 2009-07-02 19:32:06 +0900 | [diff] [blame] | 114 | return val; |
| 115 | } |
Avi Kivity | 6bf9fd4 | 2011-09-26 14:52:26 +0300 | [diff] [blame] | 116 | |
| 117 | void portio_list_init(PortioList *piolist, |
Paolo Bonzini | db10ca9 | 2013-06-06 21:19:53 -0400 | [diff] [blame] | 118 | Object *owner, |
Avi Kivity | 6bf9fd4 | 2011-09-26 14:52:26 +0300 | [diff] [blame] | 119 | const MemoryRegionPortio *callbacks, |
| 120 | void *opaque, const char *name) |
| 121 | { |
| 122 | unsigned n = 0; |
| 123 | |
| 124 | while (callbacks[n].size) { |
| 125 | ++n; |
| 126 | } |
| 127 | |
| 128 | piolist->ports = callbacks; |
| 129 | piolist->nr = 0; |
| 130 | piolist->regions = g_new0(MemoryRegion *, n); |
| 131 | piolist->address_space = NULL; |
| 132 | piolist->opaque = opaque; |
Paolo Bonzini | db10ca9 | 2013-06-06 21:19:53 -0400 | [diff] [blame] | 133 | piolist->owner = owner; |
Avi Kivity | 6bf9fd4 | 2011-09-26 14:52:26 +0300 | [diff] [blame] | 134 | piolist->name = name; |
Jan Kiszka | c76bc48 | 2013-07-02 20:22:37 +0200 | [diff] [blame] | 135 | piolist->flush_coalesced_mmio = false; |
| 136 | } |
| 137 | |
| 138 | void portio_list_set_flush_coalesced(PortioList *piolist) |
| 139 | { |
| 140 | piolist->flush_coalesced_mmio = true; |
Avi Kivity | 6bf9fd4 | 2011-09-26 14:52:26 +0300 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void portio_list_destroy(PortioList *piolist) |
| 144 | { |
Paolo Bonzini | e3fb0ad | 2014-06-11 13:02:51 +0200 | [diff] [blame] | 145 | MemoryRegionPortioList *mrpio; |
| 146 | unsigned i; |
| 147 | |
| 148 | for (i = 0; i < piolist->nr; ++i) { |
| 149 | mrpio = container_of(piolist->regions[i], MemoryRegionPortioList, mr); |
Paolo Bonzini | d8d9581 | 2014-06-11 12:42:01 +0200 | [diff] [blame] | 150 | object_unparent(OBJECT(&mrpio->mr)); |
Paolo Bonzini | e3fb0ad | 2014-06-11 13:02:51 +0200 | [diff] [blame] | 151 | g_free(mrpio); |
| 152 | } |
Avi Kivity | 6bf9fd4 | 2011-09-26 14:52:26 +0300 | [diff] [blame] | 153 | g_free(piolist->regions); |
| 154 | } |
| 155 | |
Jan Kiszka | b40acf9 | 2013-06-24 10:45:09 +0200 | [diff] [blame] | 156 | static const MemoryRegionPortio *find_portio(MemoryRegionPortioList *mrpio, |
| 157 | uint64_t offset, unsigned size, |
| 158 | bool write) |
| 159 | { |
| 160 | const MemoryRegionPortio *mrp; |
| 161 | |
| 162 | for (mrp = mrpio->ports; mrp->size; ++mrp) { |
| 163 | if (offset >= mrp->offset && offset < mrp->offset + mrp->len && |
| 164 | size == mrp->size && |
| 165 | (write ? (bool)mrp->write : (bool)mrp->read)) { |
| 166 | return mrp; |
| 167 | } |
| 168 | } |
| 169 | return NULL; |
| 170 | } |
| 171 | |
| 172 | static uint64_t portio_read(void *opaque, hwaddr addr, unsigned size) |
| 173 | { |
| 174 | MemoryRegionPortioList *mrpio = opaque; |
| 175 | const MemoryRegionPortio *mrp = find_portio(mrpio, addr, size, false); |
| 176 | uint64_t data; |
| 177 | |
| 178 | data = ((uint64_t)1 << (size * 8)) - 1; |
| 179 | if (mrp) { |
| 180 | data = mrp->read(mrpio->portio_opaque, mrp->base + addr); |
| 181 | } else if (size == 2) { |
| 182 | mrp = find_portio(mrpio, addr, 1, false); |
Paolo Bonzini | 147ed37 | 2015-03-30 12:49:40 +0200 | [diff] [blame] | 183 | if (mrp) { |
| 184 | data = mrp->read(mrpio->portio_opaque, mrp->base + addr); |
| 185 | if (addr + 1 < mrp->offset + mrp->len) { |
| 186 | data |= mrp->read(mrpio->portio_opaque, mrp->base + addr + 1) << 8; |
| 187 | } else { |
| 188 | data |= 0xff00; |
| 189 | } |
| 190 | } |
Jan Kiszka | b40acf9 | 2013-06-24 10:45:09 +0200 | [diff] [blame] | 191 | } |
| 192 | return data; |
| 193 | } |
| 194 | |
| 195 | static void portio_write(void *opaque, hwaddr addr, uint64_t data, |
| 196 | unsigned size) |
| 197 | { |
| 198 | MemoryRegionPortioList *mrpio = opaque; |
| 199 | const MemoryRegionPortio *mrp = find_portio(mrpio, addr, size, true); |
| 200 | |
| 201 | if (mrp) { |
| 202 | mrp->write(mrpio->portio_opaque, mrp->base + addr, data); |
| 203 | } else if (size == 2) { |
| 204 | mrp = find_portio(mrpio, addr, 1, true); |
Paolo Bonzini | 147ed37 | 2015-03-30 12:49:40 +0200 | [diff] [blame] | 205 | if (mrp) { |
| 206 | mrp->write(mrpio->portio_opaque, mrp->base + addr, data & 0xff); |
| 207 | if (addr + 1 < mrp->offset + mrp->len) { |
| 208 | mrp->write(mrpio->portio_opaque, mrp->base + addr + 1, data >> 8); |
| 209 | } |
| 210 | } |
Jan Kiszka | b40acf9 | 2013-06-24 10:45:09 +0200 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
| 214 | static const MemoryRegionOps portio_ops = { |
| 215 | .read = portio_read, |
| 216 | .write = portio_write, |
Paolo Bonzini | f36a638 | 2013-07-22 15:54:25 +0200 | [diff] [blame] | 217 | .endianness = DEVICE_LITTLE_ENDIAN, |
Jan Kiszka | b40acf9 | 2013-06-24 10:45:09 +0200 | [diff] [blame] | 218 | .valid.unaligned = true, |
| 219 | .impl.unaligned = true, |
| 220 | }; |
| 221 | |
Avi Kivity | 6bf9fd4 | 2011-09-26 14:52:26 +0300 | [diff] [blame] | 222 | static void portio_list_add_1(PortioList *piolist, |
| 223 | const MemoryRegionPortio *pio_init, |
| 224 | unsigned count, unsigned start, |
| 225 | unsigned off_low, unsigned off_high) |
| 226 | { |
Jan Kiszka | b40acf9 | 2013-06-24 10:45:09 +0200 | [diff] [blame] | 227 | MemoryRegionPortioList *mrpio; |
Avi Kivity | 6bf9fd4 | 2011-09-26 14:52:26 +0300 | [diff] [blame] | 228 | unsigned i; |
| 229 | |
| 230 | /* Copy the sub-list and null-terminate it. */ |
Jan Kiszka | b40acf9 | 2013-06-24 10:45:09 +0200 | [diff] [blame] | 231 | mrpio = g_malloc0(sizeof(MemoryRegionPortioList) + |
| 232 | sizeof(MemoryRegionPortio) * (count + 1)); |
| 233 | mrpio->portio_opaque = piolist->opaque; |
| 234 | memcpy(mrpio->ports, pio_init, sizeof(MemoryRegionPortio) * count); |
| 235 | memset(mrpio->ports + count, 0, sizeof(MemoryRegionPortio)); |
Avi Kivity | 6bf9fd4 | 2011-09-26 14:52:26 +0300 | [diff] [blame] | 236 | |
| 237 | /* Adjust the offsets to all be zero-based for the region. */ |
| 238 | for (i = 0; i < count; ++i) { |
Jan Kiszka | b40acf9 | 2013-06-24 10:45:09 +0200 | [diff] [blame] | 239 | mrpio->ports[i].offset -= off_low; |
| 240 | mrpio->ports[i].base = start + off_low; |
Avi Kivity | 6bf9fd4 | 2011-09-26 14:52:26 +0300 | [diff] [blame] | 241 | } |
| 242 | |
Paolo Bonzini | db10ca9 | 2013-06-06 21:19:53 -0400 | [diff] [blame] | 243 | memory_region_init_io(&mrpio->mr, piolist->owner, &portio_ops, mrpio, |
| 244 | piolist->name, off_high - off_low); |
Jan Kiszka | c76bc48 | 2013-07-02 20:22:37 +0200 | [diff] [blame] | 245 | if (piolist->flush_coalesced_mmio) { |
| 246 | memory_region_set_flush_coalesced(&mrpio->mr); |
| 247 | } |
Avi Kivity | 6bf9fd4 | 2011-09-26 14:52:26 +0300 | [diff] [blame] | 248 | memory_region_add_subregion(piolist->address_space, |
Jan Kiszka | b40acf9 | 2013-06-24 10:45:09 +0200 | [diff] [blame] | 249 | start + off_low, &mrpio->mr); |
| 250 | piolist->regions[piolist->nr] = &mrpio->mr; |
Avi Kivity | de58ac7 | 2012-01-08 19:46:17 +0200 | [diff] [blame] | 251 | ++piolist->nr; |
Avi Kivity | 6bf9fd4 | 2011-09-26 14:52:26 +0300 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | void portio_list_add(PortioList *piolist, |
| 255 | MemoryRegion *address_space, |
| 256 | uint32_t start) |
| 257 | { |
| 258 | const MemoryRegionPortio *pio, *pio_start = piolist->ports; |
| 259 | unsigned int off_low, off_high, off_last, count; |
| 260 | |
| 261 | piolist->address_space = address_space; |
| 262 | |
| 263 | /* Handle the first entry specially. */ |
| 264 | off_last = off_low = pio_start->offset; |
Paolo Bonzini | 4080a13 | 2015-03-30 12:35:00 +0200 | [diff] [blame] | 265 | off_high = off_low + pio_start->len + pio_start->size - 1; |
Avi Kivity | 6bf9fd4 | 2011-09-26 14:52:26 +0300 | [diff] [blame] | 266 | count = 1; |
| 267 | |
| 268 | for (pio = pio_start + 1; pio->size != 0; pio++, count++) { |
| 269 | /* All entries must be sorted by offset. */ |
| 270 | assert(pio->offset >= off_last); |
| 271 | off_last = pio->offset; |
| 272 | |
| 273 | /* If we see a hole, break the region. */ |
| 274 | if (off_last > off_high) { |
| 275 | portio_list_add_1(piolist, pio_start, count, start, off_low, |
| 276 | off_high); |
| 277 | /* ... and start collecting anew. */ |
| 278 | pio_start = pio; |
| 279 | off_low = off_last; |
Paolo Bonzini | 4080a13 | 2015-03-30 12:35:00 +0200 | [diff] [blame] | 280 | off_high = off_low + pio->len + pio_start->size - 1; |
Avi Kivity | 6bf9fd4 | 2011-09-26 14:52:26 +0300 | [diff] [blame] | 281 | count = 0; |
| 282 | } else if (off_last + pio->len > off_high) { |
Paolo Bonzini | 4080a13 | 2015-03-30 12:35:00 +0200 | [diff] [blame] | 283 | off_high = off_last + pio->len + pio_start->size - 1; |
Avi Kivity | 6bf9fd4 | 2011-09-26 14:52:26 +0300 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
| 287 | /* There will always be an open sub-list. */ |
| 288 | portio_list_add_1(piolist, pio_start, count, start, off_low, off_high); |
| 289 | } |
| 290 | |
| 291 | void portio_list_del(PortioList *piolist) |
| 292 | { |
Jan Kiszka | b40acf9 | 2013-06-24 10:45:09 +0200 | [diff] [blame] | 293 | MemoryRegionPortioList *mrpio; |
Avi Kivity | 6bf9fd4 | 2011-09-26 14:52:26 +0300 | [diff] [blame] | 294 | unsigned i; |
| 295 | |
| 296 | for (i = 0; i < piolist->nr; ++i) { |
Jan Kiszka | b40acf9 | 2013-06-24 10:45:09 +0200 | [diff] [blame] | 297 | mrpio = container_of(piolist->regions[i], MemoryRegionPortioList, mr); |
| 298 | memory_region_del_subregion(piolist->address_space, &mrpio->mr); |
Avi Kivity | 6bf9fd4 | 2011-09-26 14:52:26 +0300 | [diff] [blame] | 299 | } |
| 300 | } |