]> git.proxmox.com Git - qemu.git/blame - hw/isa-bus.c
Merge remote-tracking branch 'spice/spice.v39' into staging
[qemu.git] / hw / isa-bus.c
CommitLineData
f915a115
GH
1/*
2 * isa bus support for qdev.
3 *
4 * Copyright (c) 2009 Gerd Hoffmann <kraxel@redhat.com>
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, see <http://www.gnu.org/licenses/>.
18 */
19#include "hw.h"
2091ba23
GH
20#include "monitor.h"
21#include "sysbus.h"
f915a115
GH
22#include "isa.h"
23
24struct ISABus {
25 BusState qbus;
2091ba23 26 qemu_irq *irqs;
f915a115
GH
27};
28static ISABus *isabus;
fbe3288d 29target_phys_addr_t isa_mem_base = 0;
f915a115 30
2091ba23 31static void isabus_dev_print(Monitor *mon, DeviceState *dev, int indent);
6a26e119 32static char *isabus_get_fw_dev_path(DeviceState *dev);
2091ba23 33
f915a115 34static struct BusInfo isa_bus_info = {
2091ba23
GH
35 .name = "ISA",
36 .size = sizeof(ISABus),
37 .print_dev = isabus_dev_print,
6a26e119 38 .get_fw_dev_path = isabus_get_fw_dev_path,
f915a115
GH
39};
40
41ISABus *isa_bus_new(DeviceState *dev)
42{
43 if (isabus) {
44 fprintf(stderr, "Can't create a second ISA bus\n");
45 return NULL;
46 }
2091ba23
GH
47 if (NULL == dev) {
48 dev = qdev_create(NULL, "isabus-bridge");
e23a1b33 49 qdev_init_nofail(dev);
2091ba23 50 }
f915a115
GH
51
52 isabus = FROM_QBUS(ISABus, qbus_create(&isa_bus_info, dev, NULL));
53 return isabus;
54}
55
2091ba23 56void isa_bus_irqs(qemu_irq *irqs)
f915a115 57{
2091ba23
GH
58 isabus->irqs = irqs;
59}
60
3a38d437 61/*
ee951a37 62 * isa_get_irq() returns the corresponding qemu_irq entry for the i8259.
3a38d437
JS
63 *
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.
66 */
ee951a37 67qemu_irq isa_get_irq(int isairq)
3a38d437
JS
68{
69 if (isairq < 0 || isairq > 15) {
74782223 70 hw_error("isa irq %d invalid", isairq);
3a38d437 71 }
3a38d437
JS
72 return isabus->irqs[isairq];
73}
74
2e15e23b 75void isa_init_irq(ISADevice *dev, qemu_irq *p, int isairq)
f915a115 76{
2e15e23b 77 assert(dev->nirqs < ARRAY_SIZE(dev->isairq));
2e15e23b 78 dev->isairq[dev->nirqs] = isairq;
ee951a37 79 *p = isa_get_irq(isairq);
f915a115
GH
80 dev->nirqs++;
81}
82
dee41d58
GN
83static void isa_init_ioport_one(ISADevice *dev, uint16_t ioport)
84{
85 assert(dev->nioports < ARRAY_SIZE(dev->ioports));
86 dev->ioports[dev->nioports++] = ioport;
87}
88
89static int isa_cmp_ports(const void *p1, const void *p2)
90{
91 return *(uint16_t*)p1 - *(uint16_t*)p2;
92}
93
94void isa_init_ioport_range(ISADevice *dev, uint16_t start, uint16_t length)
95{
96 int i;
97 for (i = start; i < start + length; i++) {
98 isa_init_ioport_one(dev, i);
99 }
100 qsort(dev->ioports, dev->nioports, sizeof(dev->ioports[0]), isa_cmp_ports);
101}
102
103void isa_init_ioport(ISADevice *dev, uint16_t ioport)
104{
105 isa_init_ioport_range(dev, ioport, 1);
106}
107
81a322d4 108static int isa_qdev_init(DeviceState *qdev, DeviceInfo *base)
f915a115
GH
109{
110 ISADevice *dev = DO_UPCAST(ISADevice, qdev, qdev);
111 ISADeviceInfo *info = DO_UPCAST(ISADeviceInfo, qdev, base);
112
2091ba23
GH
113 dev->isairq[0] = -1;
114 dev->isairq[1] = -1;
81a322d4
GH
115
116 return info->init(dev);
f915a115
GH
117}
118
119void isa_qdev_register(ISADeviceInfo *info)
120{
121 info->qdev.init = isa_qdev_init;
122 info->qdev.bus_info = &isa_bus_info;
123 qdev_register(&info->qdev);
124}
125
924f6d72 126ISADevice *isa_create(const char *name)
f915a115
GH
127{
128 DeviceState *dev;
f915a115
GH
129
130 if (!isabus) {
74782223 131 hw_error("Tried to create isa device %s with no isa bus present.",
3f66aa9c 132 name);
f915a115
GH
133 }
134 dev = qdev_create(&isabus->qbus, name);
2e15e23b 135 return DO_UPCAST(ISADevice, qdev, dev);
f915a115 136}
2091ba23 137
86f4a9a5
BS
138ISADevice *isa_try_create(const char *name)
139{
140 DeviceState *dev;
141
142 if (!isabus) {
143 hw_error("Tried to create isa device %s with no isa bus present.",
144 name);
145 }
146 dev = qdev_try_create(&isabus->qbus, name);
147 return DO_UPCAST(ISADevice, qdev, dev);
148}
149
924f6d72
GH
150ISADevice *isa_create_simple(const char *name)
151{
152 ISADevice *dev;
153
154 dev = isa_create(name);
3f66aa9c 155 qdev_init_nofail(&dev->qdev);
924f6d72
GH
156 return dev;
157}
158
2091ba23
GH
159static void isabus_dev_print(Monitor *mon, DeviceState *dev, int indent)
160{
161 ISADevice *d = DO_UPCAST(ISADevice, qdev, dev);
162
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]);
169 }
170}
171
81a322d4 172static int isabus_bridge_init(SysBusDevice *dev)
2091ba23
GH
173{
174 /* nothing */
81a322d4 175 return 0;
2091ba23
GH
176}
177
178static SysBusDeviceInfo isabus_bridge_info = {
179 .init = isabus_bridge_init,
180 .qdev.name = "isabus-bridge",
779206de 181 .qdev.fw_name = "isa",
2091ba23 182 .qdev.size = sizeof(SysBusDevice),
787aa97a 183 .qdev.no_user = 1,
2091ba23
GH
184};
185
186static void isabus_register_devices(void)
187{
188 sysbus_register_withprop(&isabus_bridge_info);
189}
190
6a26e119
GN
191static char *isabus_get_fw_dev_path(DeviceState *dev)
192{
193 ISADevice *d = (ISADevice*)dev;
194 char path[40];
195 int off;
196
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]);
200 }
201
202 return strdup(path);
203}
204
2091ba23 205device_init(isabus_register_devices)