ioport.c 9.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * QEMU System Emulator
 *
 * Copyright (c) 2003-2008 Fabrice Bellard
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
/*
 * splitted out ioport related stuffs from vl.c.
 */

#include "ioport.h"
P
Prerna Saxena 已提交
29
#include "trace.h"
30 31 32 33 34 35 36

/***********************************************************/
/* IO Port */

//#define DEBUG_UNUSED_IOPORT
//#define DEBUG_IOPORT

37 38 39 40 41 42
#ifdef DEBUG_UNUSED_IOPORT
#  define LOG_UNUSED_IOPORT(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__)
#else
#  define LOG_UNUSED_IOPORT(fmt, ...) do{ } while (0)
#endif

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#ifdef DEBUG_IOPORT
#  define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__)
#else
#  define LOG_IOPORT(...) do { } while (0)
#endif

/* XXX: use a two level table to limit memory usage */

static void *ioport_opaque[MAX_IOPORTS];
static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];

static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;

static uint32_t ioport_read(int index, uint32_t address)
{
B
Blue Swirl 已提交
60
    static IOPortReadFunc * const default_func[3] = {
61 62 63 64 65 66 67 68 69 70 71 72
        default_ioport_readb,
        default_ioport_readw,
        default_ioport_readl
    };
    IOPortReadFunc *func = ioport_read_table[index][address];
    if (!func)
        func = default_func[index];
    return func(ioport_opaque[address], address);
}

static void ioport_write(int index, uint32_t address, uint32_t data)
{
B
Blue Swirl 已提交
73
    static IOPortWriteFunc * const default_func[3] = {
74 75 76 77 78 79 80 81 82 83 84 85
        default_ioport_writeb,
        default_ioport_writew,
        default_ioport_writel
    };
    IOPortWriteFunc *func = ioport_write_table[index][address];
    if (!func)
        func = default_func[index];
    func(ioport_opaque[address], address, data);
}

static uint32_t default_ioport_readb(void *opaque, uint32_t address)
{
86
    LOG_UNUSED_IOPORT("unused inb: port=0x%04"PRIx32"\n", address);
87 88 89 90 91
    return 0xff;
}

static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
{
92 93
    LOG_UNUSED_IOPORT("unused outb: port=0x%04"PRIx32" data=0x%02"PRIx32"\n",
                      address, data);
94 95 96 97 98 99 100
}

/* default is to make two byte accesses */
static uint32_t default_ioport_readw(void *opaque, uint32_t address)
{
    uint32_t data;
    data = ioport_read(0, address);
101
    address = (address + 1) & IOPORTS_MASK;
102 103 104 105 106 107 108
    data |= ioport_read(0, address) << 8;
    return data;
}

static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
{
    ioport_write(0, address, data & 0xff);
109
    address = (address + 1) & IOPORTS_MASK;
110 111 112 113 114
    ioport_write(0, address, (data >> 8) & 0xff);
}

static uint32_t default_ioport_readl(void *opaque, uint32_t address)
{
115
    LOG_UNUSED_IOPORT("unused inl: port=0x%04"PRIx32"\n", address);
116 117 118 119 120
    return 0xffffffff;
}

static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
{
121 122
    LOG_UNUSED_IOPORT("unused outl: port=0x%04"PRIx32" data=0x%02"PRIx32"\n",
                      address, data);
123 124
}

125 126 127 128 129 130 131 132 133 134 135 136 137 138
static int ioport_bsize(int size, int *bsize)
{
    if (size == 1) {
        *bsize = 0;
    } else if (size == 2) {
        *bsize = 1;
    } else if (size == 4) {
        *bsize = 2;
    } else {
        return -1;
    }
    return 0;
}

139
/* size is the word size in byte */
A
Anthony Liguori 已提交
140
int register_ioport_read(pio_addr_t start, int length, int size,
141 142 143 144
                         IOPortReadFunc *func, void *opaque)
{
    int i, bsize;

145
    if (ioport_bsize(size, &bsize)) {
146 147 148 149 150 151
        hw_error("register_ioport_read: invalid size");
        return -1;
    }
    for(i = start; i < start + length; i += size) {
        ioport_read_table[bsize][i] = func;
        if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
A
Andreas Färber 已提交
152 153
            hw_error("register_ioport_read: invalid opaque for address 0x%x",
                     i);
154 155 156 157 158 159
        ioport_opaque[i] = opaque;
    }
    return 0;
}

/* size is the word size in byte */
A
Anthony Liguori 已提交
160
int register_ioport_write(pio_addr_t start, int length, int size,
161 162 163 164
                          IOPortWriteFunc *func, void *opaque)
{
    int i, bsize;

165
    if (ioport_bsize(size, &bsize)) {
166 167 168 169 170 171
        hw_error("register_ioport_write: invalid size");
        return -1;
    }
    for(i = start; i < start + length; i += size) {
        ioport_write_table[bsize][i] = func;
        if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
A
Andreas Färber 已提交
172 173
            hw_error("register_ioport_write: invalid opaque for address 0x%x",
                     i);
174 175 176 177 178
        ioport_opaque[i] = opaque;
    }
    return 0;
}

A
Avi Kivity 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
static uint32_t ioport_readb_thunk(void *opaque, uint32_t addr)
{
    IORange *ioport = opaque;
    uint64_t data;

    ioport->ops->read(ioport, addr - ioport->base, 1, &data);
    return data;
}

static uint32_t ioport_readw_thunk(void *opaque, uint32_t addr)
{
    IORange *ioport = opaque;
    uint64_t data;

    ioport->ops->read(ioport, addr - ioport->base, 2, &data);
    return data;
}

static uint32_t ioport_readl_thunk(void *opaque, uint32_t addr)
{
    IORange *ioport = opaque;
    uint64_t data;

    ioport->ops->read(ioport, addr - ioport->base, 4, &data);
    return data;
}

static void ioport_writeb_thunk(void *opaque, uint32_t addr, uint32_t data)
{
    IORange *ioport = opaque;

    ioport->ops->write(ioport, addr - ioport->base, 1, data);
}

static void ioport_writew_thunk(void *opaque, uint32_t addr, uint32_t data)
{
    IORange *ioport = opaque;

    ioport->ops->write(ioport, addr - ioport->base, 2, data);
}

static void ioport_writel_thunk(void *opaque, uint32_t addr, uint32_t data)
{
    IORange *ioport = opaque;

    ioport->ops->write(ioport, addr - ioport->base, 4, data);
}

void ioport_register(IORange *ioport)
{
    register_ioport_read(ioport->base, ioport->len, 1,
                         ioport_readb_thunk, ioport);
    register_ioport_read(ioport->base, ioport->len, 2,
                         ioport_readw_thunk, ioport);
    register_ioport_read(ioport->base, ioport->len, 4,
                         ioport_readl_thunk, ioport);
    register_ioport_write(ioport->base, ioport->len, 1,
                          ioport_writeb_thunk, ioport);
    register_ioport_write(ioport->base, ioport->len, 2,
                          ioport_writew_thunk, ioport);
    register_ioport_write(ioport->base, ioport->len, 4,
                          ioport_writel_thunk, ioport);
}

A
Anthony Liguori 已提交
243
void isa_unassign_ioport(pio_addr_t start, int length)
244 245 246 247
{
    int i;

    for(i = start; i < start + length; i++) {
248 249 250
        ioport_read_table[0][i] = NULL;
        ioport_read_table[1][i] = NULL;
        ioport_read_table[2][i] = NULL;
251

252 253 254
        ioport_write_table[0][i] = NULL;
        ioport_write_table[1][i] = NULL;
        ioport_write_table[2][i] = NULL;
255 256 257 258 259

        ioport_opaque[i] = NULL;
    }
}

260 261 262 263 264 265 266
bool isa_is_ioport_assigned(pio_addr_t start)
{
    return (ioport_read_table[0][start] || ioport_write_table[0][start] ||
	    ioport_read_table[1][start] || ioport_write_table[1][start] ||
	    ioport_read_table[2][start] || ioport_write_table[2][start]);
}

267 268
/***********************************************************/

A
Anthony Liguori 已提交
269
void cpu_outb(pio_addr_t addr, uint8_t val)
270
{
271
    LOG_IOPORT("outb: %04"FMT_pioaddr" %02"PRIx8"\n", addr, val);
P
Prerna Saxena 已提交
272
    trace_cpu_out(addr, val);
273 274 275
    ioport_write(0, addr, val);
}

A
Anthony Liguori 已提交
276
void cpu_outw(pio_addr_t addr, uint16_t val)
277
{
278
    LOG_IOPORT("outw: %04"FMT_pioaddr" %04"PRIx16"\n", addr, val);
P
Prerna Saxena 已提交
279
    trace_cpu_out(addr, val);
280 281 282
    ioport_write(1, addr, val);
}

A
Anthony Liguori 已提交
283
void cpu_outl(pio_addr_t addr, uint32_t val)
284
{
285
    LOG_IOPORT("outl: %04"FMT_pioaddr" %08"PRIx32"\n", addr, val);
P
Prerna Saxena 已提交
286
    trace_cpu_out(addr, val);
287 288 289
    ioport_write(2, addr, val);
}

A
Anthony Liguori 已提交
290
uint8_t cpu_inb(pio_addr_t addr)
291
{
292
    uint8_t val;
293
    val = ioport_read(0, addr);
P
Prerna Saxena 已提交
294
    trace_cpu_in(addr, val);
295
    LOG_IOPORT("inb : %04"FMT_pioaddr" %02"PRIx8"\n", addr, val);
296 297 298
    return val;
}

A
Anthony Liguori 已提交
299
uint16_t cpu_inw(pio_addr_t addr)
300
{
301
    uint16_t val;
302
    val = ioport_read(1, addr);
P
Prerna Saxena 已提交
303
    trace_cpu_in(addr, val);
304
    LOG_IOPORT("inw : %04"FMT_pioaddr" %04"PRIx16"\n", addr, val);
305 306 307
    return val;
}

A
Anthony Liguori 已提交
308
uint32_t cpu_inl(pio_addr_t addr)
309
{
310
    uint32_t val;
311
    val = ioport_read(2, addr);
P
Prerna Saxena 已提交
312
    trace_cpu_in(addr, val);
313
    LOG_IOPORT("inl : %04"FMT_pioaddr" %08"PRIx32"\n", addr, val);
314 315
    return val;
}