xref: /qemu/include/hw/sysbus.h (revision 046f370fb44172a34e54e341e7aeca4405af67a1)
1aae9460eSPaul Brook #ifndef HW_SYSBUS_H
2175de524SMarkus Armbruster #define HW_SYSBUS_H
3aae9460eSPaul Brook 
4aae9460eSPaul Brook /* Devices attached directly to the main system bus.  */
5aae9460eSPaul Brook 
683c9f4caSPaolo Bonzini #include "hw/qdev.h"
7022c62cbSPaolo Bonzini #include "exec/memory.h"
8aae9460eSPaul Brook 
9f40070c3SBlue Swirl #define QDEV_MAX_MMIO 32
10c646f74fSGleb Natapov #define QDEV_MAX_PIO 32
11aae9460eSPaul Brook 
120d936928SAnthony Liguori #define TYPE_SYSTEM_BUS "System"
1300c2275cSGonglei #define SYSTEM_BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_SYSTEM_BUS)
140d936928SAnthony Liguori 
15aae9460eSPaul Brook typedef struct SysBusDevice SysBusDevice;
16aae9460eSPaul Brook 
17999e12bbSAnthony Liguori #define TYPE_SYS_BUS_DEVICE "sys-bus-device"
18999e12bbSAnthony Liguori #define SYS_BUS_DEVICE(obj) \
19999e12bbSAnthony Liguori      OBJECT_CHECK(SysBusDevice, (obj), TYPE_SYS_BUS_DEVICE)
20999e12bbSAnthony Liguori #define SYS_BUS_DEVICE_CLASS(klass) \
21999e12bbSAnthony Liguori      OBJECT_CLASS_CHECK(SysBusDeviceClass, (klass), TYPE_SYS_BUS_DEVICE)
22999e12bbSAnthony Liguori #define SYS_BUS_DEVICE_GET_CLASS(obj) \
23999e12bbSAnthony Liguori      OBJECT_GET_CLASS(SysBusDeviceClass, (obj), TYPE_SYS_BUS_DEVICE)
24999e12bbSAnthony Liguori 
25ce724398SHu Tao /**
26ce724398SHu Tao  * SysBusDeviceClass:
27ce724398SHu Tao  * @init: Callback function invoked when the #DeviceState.realized property
28ce724398SHu Tao  * is changed to %true. Deprecated, new types inheriting directly from
29ce724398SHu Tao  * TYPE_SYS_BUS_DEVICE should use #DeviceClass.realize instead, new leaf
30ce724398SHu Tao  * types should consult their respective parent type.
31ce724398SHu Tao  *
32ce724398SHu Tao  * SysBusDeviceClass is not overriding #DeviceClass.realize, so derived
33ce724398SHu Tao  * classes overriding it are not required to invoke its implementation.
34ce724398SHu Tao  */
35b5917219SPeter Crosthwaite 
36b5917219SPeter Crosthwaite #define SYSBUS_DEVICE_GPIO_IRQ "sysbus-irq"
37b5917219SPeter Crosthwaite 
38999e12bbSAnthony Liguori typedef struct SysBusDeviceClass {
39ce724398SHu Tao     /*< private >*/
40999e12bbSAnthony Liguori     DeviceClass parent_class;
41ce724398SHu Tao     /*< public >*/
42999e12bbSAnthony Liguori 
43999e12bbSAnthony Liguori     int (*init)(SysBusDevice *dev);
440b336b3bSLaszlo Ersek 
450b336b3bSLaszlo Ersek     /*
460b336b3bSLaszlo Ersek      * Let the sysbus device format its own non-PIO, non-MMIO unit address.
470b336b3bSLaszlo Ersek      *
480b336b3bSLaszlo Ersek      * Sometimes a class of SysBusDevices has neither MMIO nor PIO resources,
490b336b3bSLaszlo Ersek      * yet instances of it would like to distinguish themselves, in
500b336b3bSLaszlo Ersek      * OpenFirmware device paths, from other instances of the same class on the
510b336b3bSLaszlo Ersek      * sysbus. For that end we expose this callback.
520b336b3bSLaszlo Ersek      *
530b336b3bSLaszlo Ersek      * The implementation is not supposed to change *@dev, or incur other
540b336b3bSLaszlo Ersek      * observable change.
550b336b3bSLaszlo Ersek      *
560b336b3bSLaszlo Ersek      * The function returns a dynamically allocated string. On error, NULL
570b336b3bSLaszlo Ersek      * should be returned; the unit address portion of the OFW node will be
580b336b3bSLaszlo Ersek      * omitted then. (This is not considered a fatal error.)
590b336b3bSLaszlo Ersek      */
600b336b3bSLaszlo Ersek     char *(*explicit_ofw_unit_address)(const SysBusDevice *dev);
61715ca691SEric Auger     void (*connect_irq_notifier)(SysBusDevice *dev, qemu_irq irq);
62999e12bbSAnthony Liguori } SysBusDeviceClass;
63999e12bbSAnthony Liguori 
64aae9460eSPaul Brook struct SysBusDevice {
65b67964d7SAndreas Färber     /*< private >*/
66b67964d7SAndreas Färber     DeviceState parent_obj;
67b67964d7SAndreas Färber     /*< public >*/
68b67964d7SAndreas Färber 
69aae9460eSPaul Brook     int num_mmio;
70aae9460eSPaul Brook     struct {
71a8170e5eSAvi Kivity         hwaddr addr;
72ec3bb837SAvi Kivity         MemoryRegion *memory;
73aae9460eSPaul Brook     } mmio[QDEV_MAX_MMIO];
74c646f74fSGleb Natapov     int num_pio;
7589a80e74SPaolo Bonzini     uint32_t pio[QDEV_MAX_PIO];
76aae9460eSPaul Brook };
77aae9460eSPaul Brook 
784f01a637SDavid Gibson typedef void FindSysbusDeviceFunc(SysBusDevice *sbdev, void *opaque);
79eb572280SAlexander Graf 
80750ecd44SAvi Kivity void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory);
8146c305efSPeter Maydell MemoryRegion *sysbus_mmio_get_region(SysBusDevice *dev, int n);
82aae9460eSPaul Brook void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p);
83aae9460eSPaul Brook void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target);
8489a80e74SPaolo Bonzini void sysbus_init_ioports(SysBusDevice *dev, uint32_t ioport, uint32_t size);
85aae9460eSPaul Brook 
86aae9460eSPaul Brook 
87b7973186SAlexander Graf bool sysbus_has_irq(SysBusDevice *dev, int n);
88471a9bc1SAlexander Graf bool sysbus_has_mmio(SysBusDevice *dev, unsigned int n);
89aae9460eSPaul Brook void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq);
90b7973186SAlexander Graf bool sysbus_is_irq_connected(SysBusDevice *dev, int n);
91b7973186SAlexander Graf qemu_irq sysbus_get_connected_irq(SysBusDevice *dev, int n);
92a8170e5eSAvi Kivity void sysbus_mmio_map(SysBusDevice *dev, int n, hwaddr addr);
937feb640cSAlexey Korolev void sysbus_mmio_map_overlap(SysBusDevice *dev, int n, hwaddr addr,
94a1ff8ae0SMarcel Apfelbaum                              int priority);
95a8170e5eSAvi Kivity void sysbus_add_io(SysBusDevice *dev, hwaddr addr,
962b985d9cSAvi Kivity                    MemoryRegion *mem);
9762ec4832SAvi Kivity MemoryRegion *sysbus_address_space(SysBusDevice *dev);
98aae9460eSPaul Brook 
99*046f370fSThomas Huth /**
100*046f370fSThomas Huth  * sysbus_init_child_obj:
101*046f370fSThomas Huth  * @parent: The parent object
102*046f370fSThomas Huth  * @childname: Used as name of the "child<>" property in the parent
103*046f370fSThomas Huth  * @child: A pointer to the memory to be used for the object.
104*046f370fSThomas Huth  * @childsize: The maximum size available at @child for the object.
105*046f370fSThomas Huth  * @childtype: The name of the type of the object to instantiate.
106*046f370fSThomas Huth  *
107*046f370fSThomas Huth  * This function will initialize an object and attach it to the main system
108*046f370fSThomas Huth  * bus. The memory for the object should have already been allocated. The
109*046f370fSThomas Huth  * object will then be added as child to the given parent. The returned object
110*046f370fSThomas Huth  * has a reference count of 1 (for the "child<...>" property from the parent),
111*046f370fSThomas Huth  * so the object will be finalized automatically when the parent gets removed.
112*046f370fSThomas Huth  */
113*046f370fSThomas Huth void sysbus_init_child_obj(Object *parent, const char *childname, void *child,
114*046f370fSThomas Huth                            size_t childsize, const char *childtype);
115*046f370fSThomas Huth 
116eb572280SAlexander Graf /* Call func for every dynamically created sysbus device in the system */
117eb572280SAlexander Graf void foreach_dynamic_sysbus_device(FindSysbusDeviceFunc *func, void *opaque);
118eb572280SAlexander Graf 
119aae9460eSPaul Brook /* Legacy helper function for creating devices.  */
120aae9460eSPaul Brook DeviceState *sysbus_create_varargs(const char *name,
121a8170e5eSAvi Kivity                                  hwaddr addr, ...);
1224912371fSBlue Swirl DeviceState *sysbus_try_create_varargs(const char *name,
123a8170e5eSAvi Kivity                                        hwaddr addr, ...);
124aae9460eSPaul Brook static inline DeviceState *sysbus_create_simple(const char *name,
125a8170e5eSAvi Kivity                                               hwaddr addr,
126aae9460eSPaul Brook                                               qemu_irq irq)
127aae9460eSPaul Brook {
128aae9460eSPaul Brook     return sysbus_create_varargs(name, addr, irq, NULL);
129aae9460eSPaul Brook }
130aae9460eSPaul Brook 
1314912371fSBlue Swirl static inline DeviceState *sysbus_try_create_simple(const char *name,
132a8170e5eSAvi Kivity                                                     hwaddr addr,
1334912371fSBlue Swirl                                                     qemu_irq irq)
1344912371fSBlue Swirl {
1354912371fSBlue Swirl     return sysbus_try_create_varargs(name, addr, irq, NULL);
1364912371fSBlue Swirl }
1374912371fSBlue Swirl 
138175de524SMarkus Armbruster #endif /* HW_SYSBUS_H */
139