2 * System (CPU) Bus device support code
4 * Copyright (c) 2009 CodeSourcery
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/>.
22 #include "exec-memory.h"
24 static void sysbus_dev_print(Monitor
*mon
, DeviceState
*dev
, int indent
);
25 static char *sysbus_get_fw_dev_path(DeviceState
*dev
);
27 struct BusInfo system_bus_info
= {
29 .size
= sizeof(BusState
),
30 .print_dev
= sysbus_dev_print
,
31 .get_fw_dev_path
= sysbus_get_fw_dev_path
,
34 void sysbus_connect_irq(SysBusDevice
*dev
, int n
, qemu_irq irq
)
36 assert(n
>= 0 && n
< dev
->num_irq
);
43 void sysbus_mmio_map(SysBusDevice
*dev
, int n
, target_phys_addr_t addr
)
45 assert(n
>= 0 && n
< dev
->num_mmio
);
47 if (dev
->mmio
[n
].addr
== addr
) {
48 /* ??? region already mapped here. */
51 if (dev
->mmio
[n
].addr
!= (target_phys_addr_t
)-1) {
52 /* Unregister previous mapping. */
53 memory_region_del_subregion(get_system_memory(), dev
->mmio
[n
].memory
);
55 dev
->mmio
[n
].addr
= addr
;
56 memory_region_add_subregion(get_system_memory(),
62 /* Request an IRQ source. The actual IRQ object may be populated later. */
63 void sysbus_init_irq(SysBusDevice
*dev
, qemu_irq
*p
)
67 assert(dev
->num_irq
< QDEV_MAX_IRQ
);
72 /* Pass IRQs from a target device. */
73 void sysbus_pass_irq(SysBusDevice
*dev
, SysBusDevice
*target
)
76 assert(dev
->num_irq
== 0);
77 dev
->num_irq
= target
->num_irq
;
78 for (i
= 0; i
< dev
->num_irq
; i
++) {
79 dev
->irqp
[i
] = target
->irqp
[i
];
83 void sysbus_init_mmio(SysBusDevice
*dev
, MemoryRegion
*memory
)
87 assert(dev
->num_mmio
< QDEV_MAX_MMIO
);
89 dev
->mmio
[n
].addr
= -1;
90 dev
->mmio
[n
].memory
= memory
;
93 MemoryRegion
*sysbus_mmio_get_region(SysBusDevice
*dev
, int n
)
95 return dev
->mmio
[n
].memory
;
98 void sysbus_init_ioports(SysBusDevice
*dev
, pio_addr_t ioport
, pio_addr_t size
)
102 for (i
= 0; i
< size
; i
++) {
103 assert(dev
->num_pio
< QDEV_MAX_PIO
);
104 dev
->pio
[dev
->num_pio
++] = ioport
++;
108 static int sysbus_device_init(DeviceState
*dev
)
110 SysBusDevice
*sd
= SYS_BUS_DEVICE(dev
);
111 SysBusDeviceClass
*sbc
= SYS_BUS_DEVICE_GET_CLASS(sd
);
113 return sbc
->init(sd
);
116 DeviceState
*sysbus_create_varargs(const char *name
,
117 target_phys_addr_t addr
, ...)
125 dev
= qdev_create(NULL
, name
);
126 s
= sysbus_from_qdev(dev
);
127 qdev_init_nofail(dev
);
128 if (addr
!= (target_phys_addr_t
)-1) {
129 sysbus_mmio_map(s
, 0, addr
);
134 irq
= va_arg(va
, qemu_irq
);
138 sysbus_connect_irq(s
, n
, irq
);
145 DeviceState
*sysbus_try_create_varargs(const char *name
,
146 target_phys_addr_t addr
, ...)
154 dev
= qdev_try_create(NULL
, name
);
158 s
= sysbus_from_qdev(dev
);
159 qdev_init_nofail(dev
);
160 if (addr
!= (target_phys_addr_t
)-1) {
161 sysbus_mmio_map(s
, 0, addr
);
166 irq
= va_arg(va
, qemu_irq
);
170 sysbus_connect_irq(s
, n
, irq
);
177 static void sysbus_dev_print(Monitor
*mon
, DeviceState
*dev
, int indent
)
179 SysBusDevice
*s
= sysbus_from_qdev(dev
);
180 target_phys_addr_t size
;
183 monitor_printf(mon
, "%*sirq %d\n", indent
, "", s
->num_irq
);
184 for (i
= 0; i
< s
->num_mmio
; i
++) {
185 size
= memory_region_size(s
->mmio
[i
].memory
);
186 monitor_printf(mon
, "%*smmio " TARGET_FMT_plx
"/" TARGET_FMT_plx
"\n",
187 indent
, "", s
->mmio
[i
].addr
, size
);
191 static char *sysbus_get_fw_dev_path(DeviceState
*dev
)
193 SysBusDevice
*s
= sysbus_from_qdev(dev
);
197 off
= snprintf(path
, sizeof(path
), "%s", qdev_fw_name(dev
));
200 snprintf(path
+ off
, sizeof(path
) - off
, "@"TARGET_FMT_plx
,
202 } else if (s
->num_pio
) {
203 snprintf(path
+ off
, sizeof(path
) - off
, "@i%04x", s
->pio
[0]);
209 void sysbus_add_memory(SysBusDevice
*dev
, target_phys_addr_t addr
,
212 memory_region_add_subregion(get_system_memory(), addr
, mem
);
215 void sysbus_add_memory_overlap(SysBusDevice
*dev
, target_phys_addr_t addr
,
216 MemoryRegion
*mem
, unsigned priority
)
218 memory_region_add_subregion_overlap(get_system_memory(), addr
, mem
,
222 void sysbus_del_memory(SysBusDevice
*dev
, MemoryRegion
*mem
)
224 memory_region_del_subregion(get_system_memory(), mem
);
227 void sysbus_add_io(SysBusDevice
*dev
, target_phys_addr_t addr
,
230 memory_region_add_subregion(get_system_io(), addr
, mem
);
233 void sysbus_del_io(SysBusDevice
*dev
, MemoryRegion
*mem
)
235 memory_region_del_subregion(get_system_io(), mem
);
238 MemoryRegion
*sysbus_address_space(SysBusDevice
*dev
)
240 return get_system_memory();
243 static void sysbus_device_class_init(ObjectClass
*klass
, void *data
)
245 DeviceClass
*k
= DEVICE_CLASS(klass
);
246 k
->init
= sysbus_device_init
;
247 k
->bus_info
= &system_bus_info
;
250 static TypeInfo sysbus_device_type_info
= {
251 .name
= TYPE_SYS_BUS_DEVICE
,
252 .parent
= TYPE_DEVICE
,
253 .instance_size
= sizeof(SysBusDevice
),
255 .class_size
= sizeof(SysBusDeviceClass
),
256 .class_init
= sysbus_device_class_init
,
259 static void sysbus_register(void)
261 type_register_static(&sysbus_device_type_info
);
264 device_init(sysbus_register
);