xref: /openbmc/qemu/include/hw/sysbus.h (revision 7d87775f)
1 #ifndef HW_SYSBUS_H
2 #define HW_SYSBUS_H
3 
4 /* Devices attached directly to the main system bus.  */
5 
6 #include "hw/qdev-core.h"
7 #include "exec/memory.h"
8 #include "qom/object.h"
9 
10 #define QDEV_MAX_MMIO 32
11 #define QDEV_MAX_PIO 32
12 
13 #define TYPE_SYSTEM_BUS "System"
14 DECLARE_INSTANCE_CHECKER(BusState, SYSTEM_BUS,
15                          TYPE_SYSTEM_BUS)
16 
17 
18 #define TYPE_SYS_BUS_DEVICE "sys-bus-device"
19 OBJECT_DECLARE_TYPE(SysBusDevice, SysBusDeviceClass,
20                     SYS_BUS_DEVICE)
21 
22 /**
23  * SysBusDeviceClass:
24  *
25  * SysBusDeviceClass is not overriding #DeviceClass.realize, so derived
26  * classes overriding it are not required to invoke its implementation.
27  */
28 
29 #define SYSBUS_DEVICE_GPIO_IRQ "sysbus-irq"
30 
31 struct SysBusDeviceClass {
32     /*< private >*/
33     DeviceClass parent_class;
34 
35     /*
36      * Let the sysbus device format its own non-PIO, non-MMIO unit address.
37      *
38      * Sometimes a class of SysBusDevices has neither MMIO nor PIO resources,
39      * yet instances of it would like to distinguish themselves, in
40      * OpenFirmware device paths, from other instances of the same class on the
41      * sysbus. For that end we expose this callback.
42      *
43      * The implementation is not supposed to change *@dev, or incur other
44      * observable change.
45      *
46      * The function returns a dynamically allocated string. On error, NULL
47      * should be returned; the unit address portion of the OFW node will be
48      * omitted then. (This is not considered a fatal error.)
49      */
50     char *(*explicit_ofw_unit_address)(const SysBusDevice *dev);
51     void (*connect_irq_notifier)(SysBusDevice *dev, qemu_irq irq);
52 };
53 
54 struct SysBusDevice {
55     /*< private >*/
56     DeviceState parent_obj;
57     /*< public >*/
58 
59     int num_mmio;
60     struct {
61         hwaddr addr;
62         MemoryRegion *memory;
63     } mmio[QDEV_MAX_MMIO];
64     int num_pio;
65     uint32_t pio[QDEV_MAX_PIO];
66 };
67 
68 typedef void FindSysbusDeviceFunc(SysBusDevice *sbdev, void *opaque);
69 
70 void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory);
71 MemoryRegion *sysbus_mmio_get_region(SysBusDevice *dev, int n);
72 void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p);
73 void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target);
74 void sysbus_init_ioports(SysBusDevice *dev, uint32_t ioport, uint32_t size);
75 
76 
77 bool sysbus_has_irq(SysBusDevice *dev, int n);
78 bool sysbus_has_mmio(SysBusDevice *dev, unsigned int n);
79 void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq);
80 bool sysbus_is_irq_connected(SysBusDevice *dev, int n);
81 qemu_irq sysbus_get_connected_irq(SysBusDevice *dev, int n);
82 void sysbus_mmio_map(SysBusDevice *dev, int n, hwaddr addr);
83 void sysbus_mmio_map_overlap(SysBusDevice *dev, int n, hwaddr addr,
84                              int priority);
85 
86 bool sysbus_realize(SysBusDevice *dev, Error **errp);
87 bool sysbus_realize_and_unref(SysBusDevice *dev, Error **errp);
88 
89 /* Call func for every dynamically created sysbus device in the system */
90 void foreach_dynamic_sysbus_device(FindSysbusDeviceFunc *func, void *opaque);
91 
92 /* Legacy helper function for creating devices.  */
93 DeviceState *sysbus_create_varargs(const char *name,
94                                  hwaddr addr, ...);
95 
96 static inline DeviceState *sysbus_create_simple(const char *name,
97                                               hwaddr addr,
98                                               qemu_irq irq)
99 {
100     return sysbus_create_varargs(name, addr, irq, NULL);
101 }
102 
103 #endif /* HW_SYSBUS_H */
104