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