migration: move some declarations to migration.h
[qemu/lumag.git] / hw / isa-bus.c
blob27655436a0f423eb6d618ece3e6128121c1d8b54
1 /*
2 * isa bus support for qdev.
4 * Copyright (c) 2009 Gerd Hoffmann <kraxel@redhat.com>
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.
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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include "hw.h"
20 #include "monitor.h"
21 #include "sysbus.h"
22 #include "isa.h"
24 struct ISABus {
25 BusState qbus;
26 qemu_irq *irqs;
28 static ISABus *isabus;
29 target_phys_addr_t isa_mem_base = 0;
31 static void isabus_dev_print(Monitor *mon, DeviceState *dev, int indent);
32 static char *isabus_get_fw_dev_path(DeviceState *dev);
34 static struct BusInfo isa_bus_info = {
35 .name = "ISA",
36 .size = sizeof(ISABus),
37 .print_dev = isabus_dev_print,
38 .get_fw_dev_path = isabus_get_fw_dev_path,
41 ISABus *isa_bus_new(DeviceState *dev)
43 if (isabus) {
44 fprintf(stderr, "Can't create a second ISA bus\n");
45 return NULL;
47 if (NULL == dev) {
48 dev = qdev_create(NULL, "isabus-bridge");
49 qdev_init_nofail(dev);
52 isabus = FROM_QBUS(ISABus, qbus_create(&isa_bus_info, dev, NULL));
53 return isabus;
56 void isa_bus_irqs(qemu_irq *irqs)
58 isabus->irqs = irqs;
62 * isa_get_irq() returns the corresponding qemu_irq entry for the i8259.
64 * This function is only for special cases such as the 'ferr', and
65 * temporary use for normal devices until they are converted to qdev.
67 qemu_irq isa_get_irq(int isairq)
69 if (isairq < 0 || isairq > 15) {
70 hw_error("isa irq %d invalid", isairq);
72 return isabus->irqs[isairq];
75 void isa_init_irq(ISADevice *dev, qemu_irq *p, int isairq)
77 assert(dev->nirqs < ARRAY_SIZE(dev->isairq));
78 dev->isairq[dev->nirqs] = isairq;
79 *p = isa_get_irq(isairq);
80 dev->nirqs++;
83 static void isa_init_ioport_one(ISADevice *dev, uint16_t ioport)
85 assert(dev->nioports < ARRAY_SIZE(dev->ioports));
86 dev->ioports[dev->nioports++] = ioport;
89 static int isa_cmp_ports(const void *p1, const void *p2)
91 return *(uint16_t*)p1 - *(uint16_t*)p2;
94 void isa_init_ioport_range(ISADevice *dev, uint16_t start, uint16_t length)
96 int i;
97 for (i = start; i < start + length; i++) {
98 isa_init_ioport_one(dev, i);
100 qsort(dev->ioports, dev->nioports, sizeof(dev->ioports[0]), isa_cmp_ports);
103 void isa_init_ioport(ISADevice *dev, uint16_t ioport)
105 isa_init_ioport_range(dev, ioport, 1);
108 static int isa_qdev_init(DeviceState *qdev, DeviceInfo *base)
110 ISADevice *dev = DO_UPCAST(ISADevice, qdev, qdev);
111 ISADeviceInfo *info = DO_UPCAST(ISADeviceInfo, qdev, base);
113 dev->isairq[0] = -1;
114 dev->isairq[1] = -1;
116 return info->init(dev);
119 void isa_qdev_register(ISADeviceInfo *info)
121 info->qdev.init = isa_qdev_init;
122 info->qdev.bus_info = &isa_bus_info;
123 qdev_register(&info->qdev);
126 ISADevice *isa_create(const char *name)
128 DeviceState *dev;
130 if (!isabus) {
131 hw_error("Tried to create isa device %s with no isa bus present.",
132 name);
134 dev = qdev_create(&isabus->qbus, name);
135 return DO_UPCAST(ISADevice, qdev, dev);
138 ISADevice *isa_try_create(const char *name)
140 DeviceState *dev;
142 if (!isabus) {
143 hw_error("Tried to create isa device %s with no isa bus present.",
144 name);
146 dev = qdev_try_create(&isabus->qbus, name);
147 return DO_UPCAST(ISADevice, qdev, dev);
150 ISADevice *isa_create_simple(const char *name)
152 ISADevice *dev;
154 dev = isa_create(name);
155 qdev_init_nofail(&dev->qdev);
156 return dev;
159 static void isabus_dev_print(Monitor *mon, DeviceState *dev, int indent)
161 ISADevice *d = DO_UPCAST(ISADevice, qdev, dev);
163 if (d->isairq[1] != -1) {
164 monitor_printf(mon, "%*sisa irqs %d,%d\n", indent, "",
165 d->isairq[0], d->isairq[1]);
166 } else if (d->isairq[0] != -1) {
167 monitor_printf(mon, "%*sisa irq %d\n", indent, "",
168 d->isairq[0]);
172 static int isabus_bridge_init(SysBusDevice *dev)
174 /* nothing */
175 return 0;
178 static SysBusDeviceInfo isabus_bridge_info = {
179 .init = isabus_bridge_init,
180 .qdev.name = "isabus-bridge",
181 .qdev.fw_name = "isa",
182 .qdev.size = sizeof(SysBusDevice),
183 .qdev.no_user = 1,
186 static void isabus_register_devices(void)
188 sysbus_register_withprop(&isabus_bridge_info);
191 static char *isabus_get_fw_dev_path(DeviceState *dev)
193 ISADevice *d = (ISADevice*)dev;
194 char path[40];
195 int off;
197 off = snprintf(path, sizeof(path), "%s", qdev_fw_name(dev));
198 if (d->nioports) {
199 snprintf(path + off, sizeof(path) - off, "@%04x", d->ioports[0]);
202 return strdup(path);
205 device_init(isabus_register_devices)