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