Statistics
| Branch: | Revision:

root / hw / sysbus.c @ aae9460e

History | View | Annotate | Download (3.6 kB)

1
/*
2
 *  System (CPU) Bus device support code
3
 *
4
 *  Copyright (c) 2009 CodeSourcery
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
19
 */
20

    
21
#include "sysbus.h"
22
#include "sysemu.h"
23

    
24
void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq)
25
{
26
    assert(n >= 0 && n < dev->num_irq);
27
    dev->irqs[n] = 0;
28
    if (dev->irqp[n]) {
29
        *dev->irqp[n] = irq;
30
    }
31
}
32

    
33
void sysbus_mmio_map(SysBusDevice *dev, int n, target_phys_addr_t addr)
34
{
35
    assert(n >= 0 && n < dev->num_mmio);
36

    
37
    if (dev->mmio[n].addr == addr) {
38
        /* ??? region already mapped here.  */
39
        return;
40
    }
41
    if (dev->mmio[n].addr != (target_phys_addr_t)-1) {
42
        /* Unregister previous mapping.  */
43
        cpu_register_physical_memory(dev->mmio[n].addr, dev->mmio[n].size,
44
                                     IO_MEM_UNASSIGNED);
45
    }
46
    dev->mmio[n].addr = addr;
47
    if (dev->mmio[n].cb) {
48
        dev->mmio[n].cb(dev, addr);
49
    } else {
50
        cpu_register_physical_memory(addr, dev->mmio[n].size,
51
                                     dev->mmio[n].iofunc);
52
    }
53
}
54

    
55

    
56
/* Request an IRQ source.  The actual IRQ object may be populated later.  */
57
void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
58
{
59
    int n;
60

    
61
    assert(dev->num_irq < QDEV_MAX_IRQ);
62
    n = dev->num_irq++;
63
    dev->irqp[n] = p;
64
}
65

    
66
/* Pass IRQs from a target device.  */
67
void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
68
{
69
    int i;
70
    assert(dev->num_irq == 0);
71
    dev->num_irq = target->num_irq;
72
    for (i = 0; i < dev->num_irq; i++) {
73
        dev->irqp[i] = target->irqp[i];
74
    }
75
}
76

    
77
void sysbus_init_mmio(SysBusDevice *dev, target_phys_addr_t size, int iofunc)
78
{
79
    int n;
80

    
81
    assert(dev->num_mmio < QDEV_MAX_MMIO);
82
    n = dev->num_mmio++;
83
    dev->mmio[n].addr = -1;
84
    dev->mmio[n].size = size;
85
    dev->mmio[n].iofunc = iofunc;
86
}
87

    
88
void sysbus_init_mmio_cb(SysBusDevice *dev, target_phys_addr_t size,
89
                         mmio_mapfunc cb)
90
{
91
    int n;
92

    
93
    assert(dev->num_mmio < QDEV_MAX_MMIO);
94
    n = dev->num_mmio++;
95
    dev->mmio[n].addr = -1;
96
    dev->mmio[n].size = size;
97
    dev->mmio[n].cb = cb;
98
}
99

    
100
static void sysbus_device_init(DeviceState *dev, void *opaque)
101
{
102
    sysbus_initfn init = (sysbus_initfn)opaque;
103

    
104
    init(sysbus_from_qdev(dev));
105
}
106

    
107
void sysbus_register_dev(const char *name, size_t size, sysbus_initfn init)
108
{
109
    assert(size >= sizeof(SysBusDevice));
110
    qdev_register(name, size, sysbus_device_init, init);
111
}
112

    
113
DeviceState *sysbus_create_varargs(const char *name,
114
                                   target_phys_addr_t addr, ...)
115
{
116
    DeviceState *dev;
117
    SysBusDevice *s;
118
    va_list va;
119
    qemu_irq irq;
120
    int n;
121

    
122
    dev = qdev_create(NULL, name);
123
    s = sysbus_from_qdev(dev);
124
    qdev_init(dev);
125
    if (addr != (target_phys_addr_t)-1) {
126
        sysbus_mmio_map(s, 0, addr);
127
    }
128
    va_start(va, addr);
129
    n = 0;
130
    while (1) {
131
        irq = va_arg(va, qemu_irq);
132
        if (!irq) {
133
            break;
134
        }
135
        sysbus_connect_irq(s, n, irq);
136
        n++;
137
    }
138
    return dev;
139
}