xref: /openbmc/qemu/include/hw/sysbus.h (revision 0b336b3b98d8983d821ef9b0f159acc7c77cbac7)
10d09e41aSPaolo Bonzini #ifndef HW_SYSBUS_H
20d09e41aSPaolo Bonzini #define HW_SYSBUS_H 1
30d09e41aSPaolo Bonzini 
40d09e41aSPaolo Bonzini /* Devices attached directly to the main system bus.  */
50d09e41aSPaolo Bonzini 
60d09e41aSPaolo Bonzini #include "hw/qdev.h"
70d09e41aSPaolo Bonzini #include "exec/memory.h"
80d09e41aSPaolo Bonzini 
90d09e41aSPaolo Bonzini #define QDEV_MAX_MMIO 32
100d09e41aSPaolo Bonzini #define QDEV_MAX_PIO 32
110d09e41aSPaolo Bonzini 
120d09e41aSPaolo Bonzini #define TYPE_SYSTEM_BUS "System"
1300c2275cSGonglei #define SYSTEM_BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_SYSTEM_BUS)
140d09e41aSPaolo Bonzini 
150d09e41aSPaolo Bonzini typedef struct SysBusDevice SysBusDevice;
160d09e41aSPaolo Bonzini 
170d09e41aSPaolo Bonzini #define TYPE_SYS_BUS_DEVICE "sys-bus-device"
180d09e41aSPaolo Bonzini #define SYS_BUS_DEVICE(obj) \
190d09e41aSPaolo Bonzini      OBJECT_CHECK(SysBusDevice, (obj), TYPE_SYS_BUS_DEVICE)
200d09e41aSPaolo Bonzini #define SYS_BUS_DEVICE_CLASS(klass) \
210d09e41aSPaolo Bonzini      OBJECT_CLASS_CHECK(SysBusDeviceClass, (klass), TYPE_SYS_BUS_DEVICE)
220d09e41aSPaolo Bonzini #define SYS_BUS_DEVICE_GET_CLASS(obj) \
230d09e41aSPaolo Bonzini      OBJECT_GET_CLASS(SysBusDeviceClass, (obj), TYPE_SYS_BUS_DEVICE)
240d09e41aSPaolo Bonzini 
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 
380d09e41aSPaolo Bonzini typedef struct SysBusDeviceClass {
39ce724398SHu Tao     /*< private >*/
400d09e41aSPaolo Bonzini     DeviceClass parent_class;
41ce724398SHu Tao     /*< public >*/
420d09e41aSPaolo Bonzini 
430d09e41aSPaolo Bonzini     int (*init)(SysBusDevice *dev);
44*0b336b3bSLaszlo Ersek 
45*0b336b3bSLaszlo Ersek     /*
46*0b336b3bSLaszlo Ersek      * Let the sysbus device format its own non-PIO, non-MMIO unit address.
47*0b336b3bSLaszlo Ersek      *
48*0b336b3bSLaszlo Ersek      * Sometimes a class of SysBusDevices has neither MMIO nor PIO resources,
49*0b336b3bSLaszlo Ersek      * yet instances of it would like to distinguish themselves, in
50*0b336b3bSLaszlo Ersek      * OpenFirmware device paths, from other instances of the same class on the
51*0b336b3bSLaszlo Ersek      * sysbus. For that end we expose this callback.
52*0b336b3bSLaszlo Ersek      *
53*0b336b3bSLaszlo Ersek      * The implementation is not supposed to change *@dev, or incur other
54*0b336b3bSLaszlo Ersek      * observable change.
55*0b336b3bSLaszlo Ersek      *
56*0b336b3bSLaszlo Ersek      * The function returns a dynamically allocated string. On error, NULL
57*0b336b3bSLaszlo Ersek      * should be returned; the unit address portion of the OFW node will be
58*0b336b3bSLaszlo Ersek      * omitted then. (This is not considered a fatal error.)
59*0b336b3bSLaszlo Ersek      */
60*0b336b3bSLaszlo Ersek     char *(*explicit_ofw_unit_address)(const SysBusDevice *dev);
610d09e41aSPaolo Bonzini } SysBusDeviceClass;
620d09e41aSPaolo Bonzini 
630d09e41aSPaolo Bonzini struct SysBusDevice {
64b67964d7SAndreas Färber     /*< private >*/
65b67964d7SAndreas Färber     DeviceState parent_obj;
66b67964d7SAndreas Färber     /*< public >*/
67b67964d7SAndreas Färber 
680d09e41aSPaolo Bonzini     int num_mmio;
690d09e41aSPaolo Bonzini     struct {
700d09e41aSPaolo Bonzini         hwaddr addr;
710d09e41aSPaolo Bonzini         MemoryRegion *memory;
720d09e41aSPaolo Bonzini     } mmio[QDEV_MAX_MMIO];
730d09e41aSPaolo Bonzini     int num_pio;
740d09e41aSPaolo Bonzini     pio_addr_t pio[QDEV_MAX_PIO];
750d09e41aSPaolo Bonzini };
760d09e41aSPaolo Bonzini 
77eb572280SAlexander Graf typedef int FindSysbusDeviceFunc(SysBusDevice *sbdev, void *opaque);
78eb572280SAlexander Graf 
790d09e41aSPaolo Bonzini void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory);
800d09e41aSPaolo Bonzini MemoryRegion *sysbus_mmio_get_region(SysBusDevice *dev, int n);
810d09e41aSPaolo Bonzini void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p);
820d09e41aSPaolo Bonzini void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target);
830d09e41aSPaolo Bonzini void sysbus_init_ioports(SysBusDevice *dev, pio_addr_t ioport, pio_addr_t size);
840d09e41aSPaolo Bonzini 
850d09e41aSPaolo Bonzini 
86b7973186SAlexander Graf bool sysbus_has_irq(SysBusDevice *dev, int n);
87471a9bc1SAlexander Graf bool sysbus_has_mmio(SysBusDevice *dev, unsigned int n);
880d09e41aSPaolo Bonzini void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq);
89b7973186SAlexander Graf bool sysbus_is_irq_connected(SysBusDevice *dev, int n);
90b7973186SAlexander Graf qemu_irq sysbus_get_connected_irq(SysBusDevice *dev, int n);
910d09e41aSPaolo Bonzini void sysbus_mmio_map(SysBusDevice *dev, int n, hwaddr addr);
920d09e41aSPaolo Bonzini void sysbus_mmio_map_overlap(SysBusDevice *dev, int n, hwaddr addr,
93a1ff8ae0SMarcel Apfelbaum                              int priority);
940d09e41aSPaolo Bonzini void sysbus_add_io(SysBusDevice *dev, hwaddr addr,
950d09e41aSPaolo Bonzini                    MemoryRegion *mem);
960d09e41aSPaolo Bonzini MemoryRegion *sysbus_address_space(SysBusDevice *dev);
970d09e41aSPaolo Bonzini 
98eb572280SAlexander Graf /* Call func for every dynamically created sysbus device in the system */
99eb572280SAlexander Graf void foreach_dynamic_sysbus_device(FindSysbusDeviceFunc *func, void *opaque);
100eb572280SAlexander Graf 
1010d09e41aSPaolo Bonzini /* Legacy helper function for creating devices.  */
1020d09e41aSPaolo Bonzini DeviceState *sysbus_create_varargs(const char *name,
1030d09e41aSPaolo Bonzini                                  hwaddr addr, ...);
1040d09e41aSPaolo Bonzini DeviceState *sysbus_try_create_varargs(const char *name,
1050d09e41aSPaolo Bonzini                                        hwaddr addr, ...);
1060d09e41aSPaolo Bonzini static inline DeviceState *sysbus_create_simple(const char *name,
1070d09e41aSPaolo Bonzini                                               hwaddr addr,
1080d09e41aSPaolo Bonzini                                               qemu_irq irq)
1090d09e41aSPaolo Bonzini {
1100d09e41aSPaolo Bonzini     return sysbus_create_varargs(name, addr, irq, NULL);
1110d09e41aSPaolo Bonzini }
1120d09e41aSPaolo Bonzini 
1130d09e41aSPaolo Bonzini static inline DeviceState *sysbus_try_create_simple(const char *name,
1140d09e41aSPaolo Bonzini                                                     hwaddr addr,
1150d09e41aSPaolo Bonzini                                                     qemu_irq irq)
1160d09e41aSPaolo Bonzini {
1170d09e41aSPaolo Bonzini     return sysbus_try_create_varargs(name, addr, irq, NULL);
1180d09e41aSPaolo Bonzini }
1190d09e41aSPaolo Bonzini 
1200d09e41aSPaolo Bonzini #endif /* !HW_SYSBUS_H */
121