a1843088e3bec92298e96cde2b364d4d26075167
[qemu] / hw / sysbus.c
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, DeviceInfo *base)
101 {
102     SysBusDeviceInfo *info = container_of(base, SysBusDeviceInfo, qdev);
103
104     info->init(sysbus_from_qdev(dev));
105 }
106
107 void sysbus_register_withprop(const char *name, size_t size,
108                               SysBusDeviceInfo *info)
109 {
110     info->qdev.init = sysbus_device_init;
111     info->qdev.bus_type = BUS_TYPE_SYSTEM;
112
113     assert(size >= sizeof(SysBusDevice));
114     qdev_register(name, size, &info->qdev);
115 }
116
117 void sysbus_register_dev(const char *name, size_t size, sysbus_initfn init)
118 {
119     SysBusDeviceInfo *info;
120
121     info = qemu_mallocz(sizeof(*info));
122     info->init = init;
123     sysbus_register_withprop(name, size, info);
124 }
125
126 DeviceState *sysbus_create_varargs(const char *name,
127                                    target_phys_addr_t addr, ...)
128 {
129     DeviceState *dev;
130     SysBusDevice *s;
131     va_list va;
132     qemu_irq irq;
133     int n;
134
135     dev = qdev_create(NULL, name);
136     s = sysbus_from_qdev(dev);
137     qdev_init(dev);
138     if (addr != (target_phys_addr_t)-1) {
139         sysbus_mmio_map(s, 0, addr);
140     }
141     va_start(va, addr);
142     n = 0;
143     while (1) {
144         irq = va_arg(va, qemu_irq);
145         if (!irq) {
146             break;
147         }
148         sysbus_connect_irq(s, n, irq);
149         n++;
150     }
151     return dev;
152 }