xref: /qemu/include/hw/xen/xen-bus.h (revision db1015e92e04835c9eb50c29625fe566d1202dbd)
1 /*
2  * Copyright (c) 2018  Citrix Systems Inc.
3  *
4  * This work is licensed under the terms of the GNU GPL, version 2 or later.
5  * See the COPYING file in the top-level directory.
6  */
7 
8 #ifndef HW_XEN_BUS_H
9 #define HW_XEN_BUS_H
10 
11 #include "hw/xen/xen_common.h"
12 #include "hw/sysbus.h"
13 #include "qemu/notify.h"
14 #include "qom/object.h"
15 
16 typedef void (*XenWatchHandler)(void *opaque);
17 
18 typedef struct XenWatchList XenWatchList;
19 typedef struct XenWatch XenWatch;
20 typedef struct XenEventChannel XenEventChannel;
21 
22 struct XenDevice {
23     DeviceState qdev;
24     domid_t frontend_id;
25     char *name;
26     struct xs_handle *xsh;
27     XenWatchList *watch_list;
28     char *backend_path, *frontend_path;
29     enum xenbus_state backend_state, frontend_state;
30     Notifier exit;
31     XenWatch *backend_state_watch, *frontend_state_watch;
32     bool backend_online;
33     XenWatch *backend_online_watch;
34     xengnttab_handle *xgth;
35     bool feature_grant_copy;
36     bool inactive;
37     QLIST_HEAD(, XenEventChannel) event_channels;
38     QLIST_ENTRY(XenDevice) list;
39 };
40 typedef struct XenDevice XenDevice;
41 
42 typedef char *(*XenDeviceGetName)(XenDevice *xendev, Error **errp);
43 typedef void (*XenDeviceRealize)(XenDevice *xendev, Error **errp);
44 typedef void (*XenDeviceFrontendChanged)(XenDevice *xendev,
45                                          enum xenbus_state frontend_state,
46                                          Error **errp);
47 typedef void (*XenDeviceUnrealize)(XenDevice *xendev);
48 
49 struct XenDeviceClass {
50     /*< private >*/
51     DeviceClass parent_class;
52     /*< public >*/
53     const char *backend;
54     const char *device;
55     XenDeviceGetName get_name;
56     XenDeviceRealize realize;
57     XenDeviceFrontendChanged frontend_changed;
58     XenDeviceUnrealize unrealize;
59 };
60 typedef struct XenDeviceClass XenDeviceClass;
61 
62 #define TYPE_XEN_DEVICE "xen-device"
63 #define XEN_DEVICE(obj) \
64      OBJECT_CHECK(XenDevice, (obj), TYPE_XEN_DEVICE)
65 #define XEN_DEVICE_CLASS(class) \
66      OBJECT_CLASS_CHECK(XenDeviceClass, (class), TYPE_XEN_DEVICE)
67 #define XEN_DEVICE_GET_CLASS(obj) \
68      OBJECT_GET_CLASS(XenDeviceClass, (obj), TYPE_XEN_DEVICE)
69 
70 struct XenBus {
71     BusState qbus;
72     domid_t backend_id;
73     struct xs_handle *xsh;
74     XenWatchList *watch_list;
75     XenWatch *backend_watch;
76     QLIST_HEAD(, XenDevice) inactive_devices;
77 };
78 typedef struct XenBus XenBus;
79 
80 struct XenBusClass {
81     /*< private >*/
82     BusClass parent_class;
83 };
84 typedef struct XenBusClass XenBusClass;
85 
86 #define TYPE_XEN_BUS "xen-bus"
87 #define XEN_BUS(obj) \
88     OBJECT_CHECK(XenBus, (obj), TYPE_XEN_BUS)
89 #define XEN_BUS_CLASS(class) \
90     OBJECT_CLASS_CHECK(XenBusClass, (class), TYPE_XEN_BUS)
91 #define XEN_BUS_GET_CLASS(obj) \
92     OBJECT_GET_CLASS(XenBusClass, (obj), TYPE_XEN_BUS)
93 
94 void xen_bus_init(void);
95 
96 void xen_device_backend_set_state(XenDevice *xendev,
97                                   enum xenbus_state state);
98 enum xenbus_state xen_device_backend_get_state(XenDevice *xendev);
99 
100 void xen_device_backend_printf(XenDevice *xendev, const char *key,
101                                const char *fmt, ...)
102     GCC_FMT_ATTR(3, 4);
103 void xen_device_frontend_printf(XenDevice *xendev, const char *key,
104                                 const char *fmt, ...)
105     GCC_FMT_ATTR(3, 4);
106 
107 int xen_device_frontend_scanf(XenDevice *xendev, const char *key,
108                               const char *fmt, ...);
109 
110 void xen_device_set_max_grant_refs(XenDevice *xendev, unsigned int nr_refs,
111                                    Error **errp);
112 void *xen_device_map_grant_refs(XenDevice *xendev, uint32_t *refs,
113                                 unsigned int nr_refs, int prot,
114                                 Error **errp);
115 void xen_device_unmap_grant_refs(XenDevice *xendev, void *map,
116                                  unsigned int nr_refs, Error **errp);
117 
118 typedef struct XenDeviceGrantCopySegment {
119     union {
120         void *virt;
121         struct {
122             uint32_t ref;
123             off_t offset;
124         } foreign;
125     } source, dest;
126     size_t len;
127 } XenDeviceGrantCopySegment;
128 
129 void xen_device_copy_grant_refs(XenDevice *xendev, bool to_domain,
130                                 XenDeviceGrantCopySegment segs[],
131                                 unsigned int nr_segs, Error **errp);
132 
133 typedef bool (*XenEventHandler)(void *opaque);
134 
135 XenEventChannel *xen_device_bind_event_channel(XenDevice *xendev,
136                                                unsigned int port,
137                                                XenEventHandler handler,
138                                                void *opaque, Error **errp);
139 void xen_device_set_event_channel_context(XenDevice *xendev,
140                                           XenEventChannel *channel,
141                                           AioContext *ctx,
142                                           Error **errp);
143 void xen_device_notify_event_channel(XenDevice *xendev,
144                                      XenEventChannel *channel,
145                                      Error **errp);
146 void xen_device_unbind_event_channel(XenDevice *xendev,
147                                      XenEventChannel *channel,
148                                      Error **errp);
149 
150 #endif /* HW_XEN_BUS_H */
151