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/>.
24 static void sysbus_dev_print(Monitor
*mon
, DeviceState
*dev
, int indent
);
26 struct BusInfo system_bus_info
= {
28 .size
= sizeof(BusState
),
29 .print_dev
= sysbus_dev_print
,
32 void sysbus_connect_irq(SysBusDevice
*dev
, int n
, qemu_irq irq
)
34 assert(n
>= 0 && n
< dev
->num_irq
);
41 void sysbus_mmio_map(SysBusDevice
*dev
, int n
, target_phys_addr_t addr
)
43 assert(n
>= 0 && n
< dev
->num_mmio
);
45 if (dev
->mmio
[n
].addr
== addr
) {
46 /* ??? region already mapped here. */
49 if (dev
->mmio
[n
].addr
!= (target_phys_addr_t
)-1) {
50 /* Unregister previous mapping. */
51 cpu_register_physical_memory(dev
->mmio
[n
].addr
, dev
->mmio
[n
].size
,
54 dev
->mmio
[n
].addr
= addr
;
55 if (dev
->mmio
[n
].cb
) {
56 dev
->mmio
[n
].cb(dev
, addr
);
58 cpu_register_physical_memory(addr
, dev
->mmio
[n
].size
,
64 /* Request an IRQ source. The actual IRQ object may be populated later. */
65 void sysbus_init_irq(SysBusDevice
*dev
, qemu_irq
*p
)
69 assert(dev
->num_irq
< QDEV_MAX_IRQ
);
74 /* Pass IRQs from a target device. */
75 void sysbus_pass_irq(SysBusDevice
*dev
, SysBusDevice
*target
)
78 assert(dev
->num_irq
== 0);
79 dev
->num_irq
= target
->num_irq
;
80 for (i
= 0; i
< dev
->num_irq
; i
++) {
81 dev
->irqp
[i
] = target
->irqp
[i
];
85 void sysbus_init_mmio(SysBusDevice
*dev
, target_phys_addr_t size
, int iofunc
)
89 assert(dev
->num_mmio
< QDEV_MAX_MMIO
);
91 dev
->mmio
[n
].addr
= -1;
92 dev
->mmio
[n
].size
= size
;
93 dev
->mmio
[n
].iofunc
= iofunc
;
96 void sysbus_init_mmio_cb(SysBusDevice
*dev
, target_phys_addr_t size
,
101 assert(dev
->num_mmio
< QDEV_MAX_MMIO
);
103 dev
->mmio
[n
].addr
= -1;
104 dev
->mmio
[n
].size
= size
;
105 dev
->mmio
[n
].cb
= cb
;
108 static int sysbus_device_init(DeviceState
*dev
, DeviceInfo
*base
)
110 SysBusDeviceInfo
*info
= container_of(base
, SysBusDeviceInfo
, qdev
);
112 return info
->init(sysbus_from_qdev(dev
));
115 void sysbus_register_withprop(SysBusDeviceInfo
*info
)
117 info
->qdev
.init
= sysbus_device_init
;
118 info
->qdev
.bus_info
= &system_bus_info
;
120 assert(info
->qdev
.size
>= sizeof(SysBusDevice
));
121 qdev_register(&info
->qdev
);
124 void sysbus_register_dev(const char *name
, size_t size
, sysbus_initfn init
)
126 SysBusDeviceInfo
*info
;
128 info
= qemu_mallocz(sizeof(*info
));
129 info
->qdev
.name
= qemu_strdup(name
);
130 info
->qdev
.size
= size
;
132 sysbus_register_withprop(info
);
135 DeviceState
*sysbus_create_varargs(const char *name
,
136 target_phys_addr_t addr
, ...)
144 dev
= qdev_create(NULL
, name
);
145 s
= sysbus_from_qdev(dev
);
146 qdev_init_nofail(dev
);
147 if (addr
!= (target_phys_addr_t
)-1) {
148 sysbus_mmio_map(s
, 0, addr
);
153 irq
= va_arg(va
, qemu_irq
);
157 sysbus_connect_irq(s
, n
, irq
);
163 static void sysbus_dev_print(Monitor
*mon
, DeviceState
*dev
, int indent
)
165 SysBusDevice
*s
= sysbus_from_qdev(dev
);
168 for (i
= 0; i
< s
->num_mmio
; i
++) {
169 monitor_printf(mon
, "%*smmio " TARGET_FMT_plx
"/" TARGET_FMT_plx
"\n",
170 indent
, "", s
->mmio
[i
].addr
, s
->mmio
[i
].size
);