blob: 38d40afa37989097b476174eb6985c160ed4eb85 [file] [log] [blame]
Paul Durrant108f7bb2019-01-08 14:48:47 +00001/*
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
David Woodhouseb6cacfe2023-01-01 17:54:41 +000011#include "hw/xen/xen_backend_ops.h"
Paul Durrant108f7bb2019-01-08 14:48:47 +000012#include "hw/sysbus.h"
Paul Durrant82a29e32019-01-08 14:48:50 +000013#include "qemu/notify.h"
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040014#include "qom/object.h"
Paul Durrant82a29e32019-01-08 14:48:50 +000015
Paul Durrantc0b336e2019-04-08 16:16:15 +010016typedef struct XenEventChannel XenEventChannel;
Paul Durrant108f7bb2019-01-08 14:48:47 +000017
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040018struct XenDevice {
Paul Durrant108f7bb2019-01-08 14:48:47 +000019 DeviceState qdev;
Paul Durrant094a2232019-01-08 14:48:49 +000020 domid_t frontend_id;
21 char *name;
Paul Durrantba2a92d2023-01-02 11:05:16 +000022 struct qemu_xs_handle *xsh;
Paul Durrant094a2232019-01-08 14:48:49 +000023 char *backend_path, *frontend_path;
24 enum xenbus_state backend_state, frontend_state;
25 Notifier exit;
Paul Durrantba2a92d2023-01-02 11:05:16 +000026 struct qemu_xs_watch *backend_state_watch, *frontend_state_watch;
Paul Durrantb6af8922019-01-08 14:48:59 +000027 bool backend_online;
Paul Durrantba2a92d2023-01-02 11:05:16 +000028 struct qemu_xs_watch *backend_online_watch;
Paul Durrant4b34b5b2019-01-08 14:48:51 +000029 xengnttab_handle *xgth;
Paul Durrant3809f752019-09-13 09:21:58 +010030 bool inactive;
Paul Durrantc0b336e2019-04-08 16:16:15 +010031 QLIST_HEAD(, XenEventChannel) event_channels;
Paul Durrant3809f752019-09-13 09:21:58 +010032 QLIST_ENTRY(XenDevice) list;
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040033};
34typedef struct XenDevice XenDevice;
Paul Durrant108f7bb2019-01-08 14:48:47 +000035
David Woodhouse523b6b32023-10-14 16:53:23 +010036typedef char *(*XenDeviceGetFrontendPath)(XenDevice *xendev, Error **errp);
Paul Durrant094a2232019-01-08 14:48:49 +000037typedef char *(*XenDeviceGetName)(XenDevice *xendev, Error **errp);
Paul Durrant108f7bb2019-01-08 14:48:47 +000038typedef void (*XenDeviceRealize)(XenDevice *xendev, Error **errp);
Paul Durrant82a29e32019-01-08 14:48:50 +000039typedef void (*XenDeviceFrontendChanged)(XenDevice *xendev,
40 enum xenbus_state frontend_state,
41 Error **errp);
Markus Armbrusterb69c3c22020-05-05 17:29:24 +020042typedef void (*XenDeviceUnrealize)(XenDevice *xendev);
Paul Durrant108f7bb2019-01-08 14:48:47 +000043
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040044struct XenDeviceClass {
Paul Durrant108f7bb2019-01-08 14:48:47 +000045 /*< private >*/
46 DeviceClass parent_class;
47 /*< public >*/
Paul Durrant094a2232019-01-08 14:48:49 +000048 const char *backend;
49 const char *device;
David Woodhouse523b6b32023-10-14 16:53:23 +010050 XenDeviceGetFrontendPath get_frontend_path;
Paul Durrant094a2232019-01-08 14:48:49 +000051 XenDeviceGetName get_name;
Paul Durrant108f7bb2019-01-08 14:48:47 +000052 XenDeviceRealize realize;
Paul Durrant82a29e32019-01-08 14:48:50 +000053 XenDeviceFrontendChanged frontend_changed;
Paul Durrant108f7bb2019-01-08 14:48:47 +000054 XenDeviceUnrealize unrealize;
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040055};
Paul Durrant108f7bb2019-01-08 14:48:47 +000056
57#define TYPE_XEN_DEVICE "xen-device"
Eduardo Habkosta489d192020-09-16 14:25:18 -040058OBJECT_DECLARE_TYPE(XenDevice, XenDeviceClass, XEN_DEVICE)
Paul Durrant108f7bb2019-01-08 14:48:47 +000059
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040060struct XenBus {
Paul Durrant108f7bb2019-01-08 14:48:47 +000061 BusState qbus;
Paul Durrant094a2232019-01-08 14:48:49 +000062 domid_t backend_id;
Paul Durrantba2a92d2023-01-02 11:05:16 +000063 struct qemu_xs_handle *xsh;
Paul Durrantc4583c82020-10-01 09:15:00 +010064 unsigned int backend_types;
Paul Durrantba2a92d2023-01-02 11:05:16 +000065 struct qemu_xs_watch **backend_watch;
Paul Durrant3809f752019-09-13 09:21:58 +010066 QLIST_HEAD(, XenDevice) inactive_devices;
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040067};
Paul Durrant108f7bb2019-01-08 14:48:47 +000068
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040069struct XenBusClass {
Paul Durrant108f7bb2019-01-08 14:48:47 +000070 /*< private >*/
71 BusClass parent_class;
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040072};
Paul Durrant108f7bb2019-01-08 14:48:47 +000073
74#define TYPE_XEN_BUS "xen-bus"
Eduardo Habkostc821774a2020-08-31 17:07:37 -040075OBJECT_DECLARE_TYPE(XenBus, XenBusClass,
Eduardo Habkost30b57072020-09-16 14:25:17 -040076 XEN_BUS)
Paul Durrant108f7bb2019-01-08 14:48:47 +000077
David Woodhouse7d6eff12023-10-17 17:53:58 +010078void xen_bus_init(void);
Paul Durrant108f7bb2019-01-08 14:48:47 +000079
Paul Durrant82a29e32019-01-08 14:48:50 +000080void xen_device_backend_set_state(XenDevice *xendev,
81 enum xenbus_state state);
82enum xenbus_state xen_device_backend_get_state(XenDevice *xendev);
83
Paul Durrantb6af8922019-01-08 14:48:59 +000084void xen_device_backend_printf(XenDevice *xendev, const char *key,
85 const char *fmt, ...)
Marc-André Lureau9edc6312022-02-20 20:39:25 +040086 G_GNUC_PRINTF(3, 4);
Paul Durrantb6af8922019-01-08 14:48:59 +000087void xen_device_frontend_printf(XenDevice *xendev, const char *key,
88 const char *fmt, ...)
Marc-André Lureau9edc6312022-02-20 20:39:25 +040089 G_GNUC_PRINTF(3, 4);
Paul Durrantb6af8922019-01-08 14:48:59 +000090
91int xen_device_frontend_scanf(XenDevice *xendev, const char *key,
Daniel P. Berrangéd62449d2022-12-19 08:02:01 -050092 const char *fmt, ...)
93 G_GNUC_SCANF(3, 4);
Paul Durrantb6af8922019-01-08 14:48:59 +000094
Paul Durrant4b34b5b2019-01-08 14:48:51 +000095void xen_device_set_max_grant_refs(XenDevice *xendev, unsigned int nr_refs,
96 Error **errp);
97void *xen_device_map_grant_refs(XenDevice *xendev, uint32_t *refs,
98 unsigned int nr_refs, int prot,
99 Error **errp);
David Woodhousef80fad12023-01-10 00:03:49 +0000100void xen_device_unmap_grant_refs(XenDevice *xendev, void *map, uint32_t *refs,
Paul Durrant4b34b5b2019-01-08 14:48:51 +0000101 unsigned int nr_refs, Error **errp);
102
103typedef struct XenDeviceGrantCopySegment {
104 union {
105 void *virt;
106 struct {
107 uint32_t ref;
108 off_t offset;
109 } foreign;
110 } source, dest;
111 size_t len;
112} XenDeviceGrantCopySegment;
113
114void xen_device_copy_grant_refs(XenDevice *xendev, bool to_domain,
115 XenDeviceGrantCopySegment segs[],
116 unsigned int nr_segs, Error **errp);
117
Paul Durrant345f42b2019-04-08 16:16:17 +0100118typedef bool (*XenEventHandler)(void *opaque);
Paul Durranta3d669c2019-01-08 14:48:52 +0000119
120XenEventChannel *xen_device_bind_event_channel(XenDevice *xendev,
121 unsigned int port,
122 XenEventHandler handler,
123 void *opaque, Error **errp);
Paul Durrant32d0b7b2019-12-16 14:34:51 +0000124void xen_device_set_event_channel_context(XenDevice *xendev,
125 XenEventChannel *channel,
126 AioContext *ctx,
127 Error **errp);
Paul Durranta3d669c2019-01-08 14:48:52 +0000128void xen_device_notify_event_channel(XenDevice *xendev,
129 XenEventChannel *channel,
130 Error **errp);
131void xen_device_unbind_event_channel(XenDevice *xendev,
132 XenEventChannel *channel,
133 Error **errp);
David Woodhousea72ccc72023-10-16 16:00:23 +0100134unsigned int xen_event_channel_get_local_port(XenEventChannel *channel);
Paul Durranta3d669c2019-01-08 14:48:52 +0000135
Paul Durrant108f7bb2019-01-08 14:48:47 +0000136#endif /* HW_XEN_BUS_H */