Paul Durrant | 108f7bb | 2019-01-08 14:48:47 +0000 | [diff] [blame] | 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 | |
David Woodhouse | b6cacfe | 2023-01-01 17:54:41 +0000 | [diff] [blame] | 11 | #include "hw/xen/xen_backend_ops.h" |
Paul Durrant | 108f7bb | 2019-01-08 14:48:47 +0000 | [diff] [blame] | 12 | #include "hw/sysbus.h" |
Paul Durrant | 82a29e3 | 2019-01-08 14:48:50 +0000 | [diff] [blame] | 13 | #include "qemu/notify.h" |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 14 | #include "qom/object.h" |
Paul Durrant | 82a29e3 | 2019-01-08 14:48:50 +0000 | [diff] [blame] | 15 | |
Paul Durrant | c0b336e | 2019-04-08 16:16:15 +0100 | [diff] [blame] | 16 | typedef struct XenEventChannel XenEventChannel; |
Paul Durrant | 108f7bb | 2019-01-08 14:48:47 +0000 | [diff] [blame] | 17 | |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 18 | struct XenDevice { |
Paul Durrant | 108f7bb | 2019-01-08 14:48:47 +0000 | [diff] [blame] | 19 | DeviceState qdev; |
Paul Durrant | 094a223 | 2019-01-08 14:48:49 +0000 | [diff] [blame] | 20 | domid_t frontend_id; |
| 21 | char *name; |
Paul Durrant | ba2a92d | 2023-01-02 11:05:16 +0000 | [diff] [blame] | 22 | struct qemu_xs_handle *xsh; |
Paul Durrant | 094a223 | 2019-01-08 14:48:49 +0000 | [diff] [blame] | 23 | char *backend_path, *frontend_path; |
| 24 | enum xenbus_state backend_state, frontend_state; |
| 25 | Notifier exit; |
Paul Durrant | ba2a92d | 2023-01-02 11:05:16 +0000 | [diff] [blame] | 26 | struct qemu_xs_watch *backend_state_watch, *frontend_state_watch; |
Paul Durrant | b6af892 | 2019-01-08 14:48:59 +0000 | [diff] [blame] | 27 | bool backend_online; |
Paul Durrant | ba2a92d | 2023-01-02 11:05:16 +0000 | [diff] [blame] | 28 | struct qemu_xs_watch *backend_online_watch; |
Paul Durrant | 4b34b5b | 2019-01-08 14:48:51 +0000 | [diff] [blame] | 29 | xengnttab_handle *xgth; |
Paul Durrant | 3809f75 | 2019-09-13 09:21:58 +0100 | [diff] [blame] | 30 | bool inactive; |
Paul Durrant | c0b336e | 2019-04-08 16:16:15 +0100 | [diff] [blame] | 31 | QLIST_HEAD(, XenEventChannel) event_channels; |
Paul Durrant | 3809f75 | 2019-09-13 09:21:58 +0100 | [diff] [blame] | 32 | QLIST_ENTRY(XenDevice) list; |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 33 | }; |
| 34 | typedef struct XenDevice XenDevice; |
Paul Durrant | 108f7bb | 2019-01-08 14:48:47 +0000 | [diff] [blame] | 35 | |
David Woodhouse | 523b6b3 | 2023-10-14 16:53:23 +0100 | [diff] [blame] | 36 | typedef char *(*XenDeviceGetFrontendPath)(XenDevice *xendev, Error **errp); |
Paul Durrant | 094a223 | 2019-01-08 14:48:49 +0000 | [diff] [blame] | 37 | typedef char *(*XenDeviceGetName)(XenDevice *xendev, Error **errp); |
Paul Durrant | 108f7bb | 2019-01-08 14:48:47 +0000 | [diff] [blame] | 38 | typedef void (*XenDeviceRealize)(XenDevice *xendev, Error **errp); |
Paul Durrant | 82a29e3 | 2019-01-08 14:48:50 +0000 | [diff] [blame] | 39 | typedef void (*XenDeviceFrontendChanged)(XenDevice *xendev, |
| 40 | enum xenbus_state frontend_state, |
| 41 | Error **errp); |
Markus Armbruster | b69c3c2 | 2020-05-05 17:29:24 +0200 | [diff] [blame] | 42 | typedef void (*XenDeviceUnrealize)(XenDevice *xendev); |
Paul Durrant | 108f7bb | 2019-01-08 14:48:47 +0000 | [diff] [blame] | 43 | |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 44 | struct XenDeviceClass { |
Paul Durrant | 108f7bb | 2019-01-08 14:48:47 +0000 | [diff] [blame] | 45 | /*< private >*/ |
| 46 | DeviceClass parent_class; |
| 47 | /*< public >*/ |
Paul Durrant | 094a223 | 2019-01-08 14:48:49 +0000 | [diff] [blame] | 48 | const char *backend; |
| 49 | const char *device; |
David Woodhouse | 523b6b3 | 2023-10-14 16:53:23 +0100 | [diff] [blame] | 50 | XenDeviceGetFrontendPath get_frontend_path; |
Paul Durrant | 094a223 | 2019-01-08 14:48:49 +0000 | [diff] [blame] | 51 | XenDeviceGetName get_name; |
Paul Durrant | 108f7bb | 2019-01-08 14:48:47 +0000 | [diff] [blame] | 52 | XenDeviceRealize realize; |
Paul Durrant | 82a29e3 | 2019-01-08 14:48:50 +0000 | [diff] [blame] | 53 | XenDeviceFrontendChanged frontend_changed; |
Paul Durrant | 108f7bb | 2019-01-08 14:48:47 +0000 | [diff] [blame] | 54 | XenDeviceUnrealize unrealize; |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 55 | }; |
Paul Durrant | 108f7bb | 2019-01-08 14:48:47 +0000 | [diff] [blame] | 56 | |
| 57 | #define TYPE_XEN_DEVICE "xen-device" |
Eduardo Habkost | a489d19 | 2020-09-16 14:25:18 -0400 | [diff] [blame] | 58 | OBJECT_DECLARE_TYPE(XenDevice, XenDeviceClass, XEN_DEVICE) |
Paul Durrant | 108f7bb | 2019-01-08 14:48:47 +0000 | [diff] [blame] | 59 | |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 60 | struct XenBus { |
Paul Durrant | 108f7bb | 2019-01-08 14:48:47 +0000 | [diff] [blame] | 61 | BusState qbus; |
Paul Durrant | 094a223 | 2019-01-08 14:48:49 +0000 | [diff] [blame] | 62 | domid_t backend_id; |
Paul Durrant | ba2a92d | 2023-01-02 11:05:16 +0000 | [diff] [blame] | 63 | struct qemu_xs_handle *xsh; |
Paul Durrant | c4583c8 | 2020-10-01 09:15:00 +0100 | [diff] [blame] | 64 | unsigned int backend_types; |
Paul Durrant | ba2a92d | 2023-01-02 11:05:16 +0000 | [diff] [blame] | 65 | struct qemu_xs_watch **backend_watch; |
Paul Durrant | 3809f75 | 2019-09-13 09:21:58 +0100 | [diff] [blame] | 66 | QLIST_HEAD(, XenDevice) inactive_devices; |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 67 | }; |
Paul Durrant | 108f7bb | 2019-01-08 14:48:47 +0000 | [diff] [blame] | 68 | |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 69 | struct XenBusClass { |
Paul Durrant | 108f7bb | 2019-01-08 14:48:47 +0000 | [diff] [blame] | 70 | /*< private >*/ |
| 71 | BusClass parent_class; |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 72 | }; |
Paul Durrant | 108f7bb | 2019-01-08 14:48:47 +0000 | [diff] [blame] | 73 | |
| 74 | #define TYPE_XEN_BUS "xen-bus" |
Eduardo Habkost | c821774a | 2020-08-31 17:07:37 -0400 | [diff] [blame] | 75 | OBJECT_DECLARE_TYPE(XenBus, XenBusClass, |
Eduardo Habkost | 30b5707 | 2020-09-16 14:25:17 -0400 | [diff] [blame] | 76 | XEN_BUS) |
Paul Durrant | 108f7bb | 2019-01-08 14:48:47 +0000 | [diff] [blame] | 77 | |
David Woodhouse | 7d6eff1 | 2023-10-17 17:53:58 +0100 | [diff] [blame] | 78 | void xen_bus_init(void); |
Paul Durrant | 108f7bb | 2019-01-08 14:48:47 +0000 | [diff] [blame] | 79 | |
Paul Durrant | 82a29e3 | 2019-01-08 14:48:50 +0000 | [diff] [blame] | 80 | void xen_device_backend_set_state(XenDevice *xendev, |
| 81 | enum xenbus_state state); |
| 82 | enum xenbus_state xen_device_backend_get_state(XenDevice *xendev); |
| 83 | |
Paul Durrant | b6af892 | 2019-01-08 14:48:59 +0000 | [diff] [blame] | 84 | void xen_device_backend_printf(XenDevice *xendev, const char *key, |
| 85 | const char *fmt, ...) |
Marc-André Lureau | 9edc631 | 2022-02-20 20:39:25 +0400 | [diff] [blame] | 86 | G_GNUC_PRINTF(3, 4); |
Paul Durrant | b6af892 | 2019-01-08 14:48:59 +0000 | [diff] [blame] | 87 | void xen_device_frontend_printf(XenDevice *xendev, const char *key, |
| 88 | const char *fmt, ...) |
Marc-André Lureau | 9edc631 | 2022-02-20 20:39:25 +0400 | [diff] [blame] | 89 | G_GNUC_PRINTF(3, 4); |
Paul Durrant | b6af892 | 2019-01-08 14:48:59 +0000 | [diff] [blame] | 90 | |
| 91 | int xen_device_frontend_scanf(XenDevice *xendev, const char *key, |
Daniel P. Berrangé | d62449d | 2022-12-19 08:02:01 -0500 | [diff] [blame] | 92 | const char *fmt, ...) |
| 93 | G_GNUC_SCANF(3, 4); |
Paul Durrant | b6af892 | 2019-01-08 14:48:59 +0000 | [diff] [blame] | 94 | |
Paul Durrant | 4b34b5b | 2019-01-08 14:48:51 +0000 | [diff] [blame] | 95 | void xen_device_set_max_grant_refs(XenDevice *xendev, unsigned int nr_refs, |
| 96 | Error **errp); |
| 97 | void *xen_device_map_grant_refs(XenDevice *xendev, uint32_t *refs, |
| 98 | unsigned int nr_refs, int prot, |
| 99 | Error **errp); |
David Woodhouse | f80fad1 | 2023-01-10 00:03:49 +0000 | [diff] [blame] | 100 | void xen_device_unmap_grant_refs(XenDevice *xendev, void *map, uint32_t *refs, |
Paul Durrant | 4b34b5b | 2019-01-08 14:48:51 +0000 | [diff] [blame] | 101 | unsigned int nr_refs, Error **errp); |
| 102 | |
| 103 | typedef 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 | |
| 114 | void xen_device_copy_grant_refs(XenDevice *xendev, bool to_domain, |
| 115 | XenDeviceGrantCopySegment segs[], |
| 116 | unsigned int nr_segs, Error **errp); |
| 117 | |
Paul Durrant | 345f42b | 2019-04-08 16:16:17 +0100 | [diff] [blame] | 118 | typedef bool (*XenEventHandler)(void *opaque); |
Paul Durrant | a3d669c | 2019-01-08 14:48:52 +0000 | [diff] [blame] | 119 | |
| 120 | XenEventChannel *xen_device_bind_event_channel(XenDevice *xendev, |
| 121 | unsigned int port, |
| 122 | XenEventHandler handler, |
| 123 | void *opaque, Error **errp); |
Paul Durrant | 32d0b7b | 2019-12-16 14:34:51 +0000 | [diff] [blame] | 124 | void xen_device_set_event_channel_context(XenDevice *xendev, |
| 125 | XenEventChannel *channel, |
| 126 | AioContext *ctx, |
| 127 | Error **errp); |
Paul Durrant | a3d669c | 2019-01-08 14:48:52 +0000 | [diff] [blame] | 128 | void xen_device_notify_event_channel(XenDevice *xendev, |
| 129 | XenEventChannel *channel, |
| 130 | Error **errp); |
| 131 | void xen_device_unbind_event_channel(XenDevice *xendev, |
| 132 | XenEventChannel *channel, |
| 133 | Error **errp); |
David Woodhouse | a72ccc7 | 2023-10-16 16:00:23 +0100 | [diff] [blame] | 134 | unsigned int xen_event_channel_get_local_port(XenEventChannel *channel); |
Paul Durrant | a3d669c | 2019-01-08 14:48:52 +0000 | [diff] [blame] | 135 | |
Paul Durrant | 108f7bb | 2019-01-08 14:48:47 +0000 | [diff] [blame] | 136 | #endif /* HW_XEN_BUS_H */ |