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