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 if (dev
->mmio
[n
].memory
) {
54 memory_region_del_subregion(get_system_memory(),
56 } else if (dev
->mmio
[n
].unmap
) {
57 dev
->mmio
[n
].unmap(dev
, dev
->mmio
[n
].addr
);
60 dev
->mmio
[n
].addr
= addr
;
61 if (dev
->mmio
[n
].memory
) {
62 memory_region_add_subregion(get_system_memory(),
65 } else if (dev
->mmio
[n
].cb
) {
66 dev
->mmio
[n
].cb(dev
, addr
);
71 /* Request an IRQ source. The actual IRQ object may be populated later. */
72 void sysbus_init_irq(SysBusDevice
*dev
, qemu_irq
*p
)
76 assert(dev
->num_irq
< QDEV_MAX_IRQ
);
81 /* Pass IRQs from a target device. */
82 void sysbus_pass_irq(SysBusDevice
*dev
, SysBusDevice
*target
)
85 assert(dev
->num_irq
== 0);
86 dev
->num_irq
= target
->num_irq
;
87 for (i
= 0; i
< dev
->num_irq
; i
++) {
88 dev
->irqp
[i
] = target
->irqp
[i
];
92 void sysbus_init_mmio_cb2(SysBusDevice
*dev
,
93 mmio_mapfunc cb
, mmio_mapfunc unmap
)
97 assert(dev
->num_mmio
< QDEV_MAX_MMIO
);
99 dev
->mmio
[n
].addr
= -1;
100 dev
->mmio
[n
].cb
= cb
;
101 dev
->mmio
[n
].unmap
= unmap
;
104 void sysbus_init_mmio(SysBusDevice
*dev
, MemoryRegion
*memory
)
108 assert(dev
->num_mmio
< QDEV_MAX_MMIO
);
110 dev
->mmio
[n
].addr
= -1;
111 dev
->mmio
[n
].memory
= memory
;
114 MemoryRegion
*sysbus_mmio_get_region(SysBusDevice
*dev
, int n
)
116 return dev
->mmio
[n
].memory
;
119 void sysbus_init_ioports(SysBusDevice
*dev
, pio_addr_t ioport
, pio_addr_t size
)
123 for (i
= 0; i
< size
; i
++) {
124 assert(dev
->num_pio
< QDEV_MAX_PIO
);
125 dev
->pio
[dev
->num_pio
++] = ioport
++;
129 static int sysbus_device_init(DeviceState
*dev
, DeviceInfo
*base
)
131 SysBusDeviceInfo
*info
= container_of(base
, SysBusDeviceInfo
, qdev
);
133 return info
->init(sysbus_from_qdev(dev
));
136 void sysbus_register_withprop(SysBusDeviceInfo
*info
)
138 info
->qdev
.init
= sysbus_device_init
;
139 info
->qdev
.bus_info
= &system_bus_info
;
141 assert(info
->qdev
.size
>= sizeof(SysBusDevice
));
142 qdev_register(&info
->qdev
);
145 void sysbus_register_dev(const char *name
, size_t size
, sysbus_initfn init
)
147 SysBusDeviceInfo
*info
;
149 info
= g_malloc0(sizeof(*info
));
150 info
->qdev
.name
= g_strdup(name
);
151 info
->qdev
.size
= size
;
153 sysbus_register_withprop(info
);
156 DeviceState
*sysbus_create_varargs(const char *name
,
157 target_phys_addr_t addr
, ...)
165 dev
= qdev_create(NULL
, name
);
166 s
= sysbus_from_qdev(dev
);
167 qdev_init_nofail(dev
);
168 if (addr
!= (target_phys_addr_t
)-1) {
169 sysbus_mmio_map(s
, 0, addr
);
174 irq
= va_arg(va
, qemu_irq
);
178 sysbus_connect_irq(s
, n
, irq
);
185 DeviceState
*sysbus_try_create_varargs(const char *name
,
186 target_phys_addr_t addr
, ...)
194 dev
= qdev_try_create(NULL
, name
);
198 s
= sysbus_from_qdev(dev
);
199 qdev_init_nofail(dev
);
200 if (addr
!= (target_phys_addr_t
)-1) {
201 sysbus_mmio_map(s
, 0, addr
);
206 irq
= va_arg(va
, qemu_irq
);
210 sysbus_connect_irq(s
, n
, irq
);
217 static void sysbus_dev_print(Monitor
*mon
, DeviceState
*dev
, int indent
)
219 SysBusDevice
*s
= sysbus_from_qdev(dev
);
220 target_phys_addr_t size
;
223 monitor_printf(mon
, "%*sirq %d\n", indent
, "", s
->num_irq
);
224 for (i
= 0; i
< s
->num_mmio
; i
++) {
226 if (s
->mmio
[i
].memory
) {
227 size
= memory_region_size(s
->mmio
[i
].memory
);
229 monitor_printf(mon
, "%*smmio " TARGET_FMT_plx
"/" TARGET_FMT_plx
"\n",
230 indent
, "", s
->mmio
[i
].addr
, size
);
234 static char *sysbus_get_fw_dev_path(DeviceState
*dev
)
236 SysBusDevice
*s
= sysbus_from_qdev(dev
);
240 off
= snprintf(path
, sizeof(path
), "%s", qdev_fw_name(dev
));
243 snprintf(path
+ off
, sizeof(path
) - off
, "@"TARGET_FMT_plx
,
245 } else if (s
->num_pio
) {
246 snprintf(path
+ off
, sizeof(path
) - off
, "@i%04x", s
->pio
[0]);
252 void sysbus_add_memory(SysBusDevice
*dev
, target_phys_addr_t addr
,
255 memory_region_add_subregion(get_system_memory(), addr
, mem
);
258 void sysbus_add_memory_overlap(SysBusDevice
*dev
, target_phys_addr_t addr
,
259 MemoryRegion
*mem
, unsigned priority
)
261 memory_region_add_subregion_overlap(get_system_memory(), addr
, mem
,
265 void sysbus_del_memory(SysBusDevice
*dev
, MemoryRegion
*mem
)
267 memory_region_del_subregion(get_system_memory(), mem
);
270 void sysbus_add_io(SysBusDevice
*dev
, target_phys_addr_t addr
,
273 memory_region_add_subregion(get_system_io(), addr
, mem
);
276 void sysbus_del_io(SysBusDevice
*dev
, MemoryRegion
*mem
)
278 memory_region_del_subregion(get_system_io(), mem
);