xilinx: Add interrupt controller.
[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 typedef struct {
25     DeviceInfo qdev;
26     sysbus_initfn init;
27 } SysBusDeviceInfo;
28
29 void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq)
30 {
31     assert(n >= 0 && n < dev->num_irq);
32     dev->irqs[n] = 0;
33     if (dev->irqp[n]) {
34         *dev->irqp[n] = irq;
35     }
36 }
37
38 void sysbus_mmio_map(SysBusDevice *dev, int n, target_phys_addr_t addr)
39 {
40     assert(n >= 0 && n < dev->num_mmio);
41
42     if (dev->mmio[n].addr == addr) {
43         /* ??? region already mapped here.  */
44         return;
45     }
46     if (dev->mmio[n].addr != (target_phys_addr_t)-1) {
47         /* Unregister previous mapping.  */
48         cpu_register_physical_memory(dev->mmio[n].addr, dev->mmio[n].size,
49                                      IO_MEM_UNASSIGNED);
50     }
51     dev->mmio[n].addr = addr;
52     if (dev->mmio[n].cb) {
53         dev->mmio[n].cb(dev, addr);
54     } else {
55         cpu_register_physical_memory(addr, dev->mmio[n].size,
56                                      dev->mmio[n].iofunc);
57     }
58 }
59
60
61 /* Request an IRQ source.  The actual IRQ object may be populated later.  */
62 void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
63 {
64     int n;
65
66     assert(dev->num_irq < QDEV_MAX_IRQ);
67     n = dev->num_irq++;
68     dev->irqp[n] = p;
69 }
70
71 /* Pass IRQs from a target device.  */
72 void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
73 {
74     int i;
75     assert(dev->num_irq == 0);
76     dev->num_irq = target->num_irq;
77     for (i = 0; i < dev->num_irq; i++) {
78         dev->irqp[i] = target->irqp[i];
79     }
80 }
81
82 void sysbus_init_mmio(SysBusDevice *dev, target_phys_addr_t size, int iofunc)
83 {
84     int n;
85
86     assert(dev->num_mmio < QDEV_MAX_MMIO);
87     n = dev->num_mmio++;
88     dev->mmio[n].addr = -1;
89     dev->mmio[n].size = size;
90     dev->mmio[n].iofunc = iofunc;
91 }
92
93 void sysbus_init_mmio_cb(SysBusDevice *dev, target_phys_addr_t size,
94                          mmio_mapfunc cb)
95 {
96     int n;
97
98     assert(dev->num_mmio < QDEV_MAX_MMIO);
99     n = dev->num_mmio++;
100     dev->mmio[n].addr = -1;
101     dev->mmio[n].size = size;
102     dev->mmio[n].cb = cb;
103 }
104
105 static void sysbus_device_init(DeviceState *dev, DeviceInfo *base)
106 {
107     SysBusDeviceInfo *info = container_of(base, SysBusDeviceInfo, qdev);
108
109     info->init(sysbus_from_qdev(dev));
110 }
111
112 void sysbus_register_dev(const char *name, size_t size, sysbus_initfn init)
113 {
114     SysBusDeviceInfo *info;
115
116     info = qemu_mallocz(sizeof(*info));
117     info->init = init;
118     info->qdev.init = sysbus_device_init;
119     info->qdev.bus_type = BUS_TYPE_SYSTEM;
120
121     assert(size >= sizeof(SysBusDevice));
122     qdev_register(name, size, &info->qdev);
123 }
124
125 DeviceState *sysbus_create_varargs(const char *name,
126                                    target_phys_addr_t addr, ...)
127 {
128     DeviceState *dev;
129     SysBusDevice *s;
130     va_list va;
131     qemu_irq irq;
132     int n;
133
134     dev = qdev_create(NULL, name);
135     s = sysbus_from_qdev(dev);
136     qdev_init(dev);
137     if (addr != (target_phys_addr_t)-1) {
138         sysbus_mmio_map(s, 0, addr);
139     }
140     va_start(va, addr);
141     n = 0;
142     while (1) {
143         irq = va_arg(va, qemu_irq);
144         if (!irq) {
145             break;
146         }
147         sysbus_connect_irq(s, n, irq);
148         n++;
149     }
150     return dev;
151 }