xref: /openbmc/qemu/hw/vfio/vfio-migration-internal.h (revision 0d70c5aa1bbfb0f5099d53d6e084337a8246cc0c)
1 /*
2  * VFIO migration
3  *
4  * Copyright Red Hat, Inc. 2025
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8 
9 #ifndef HW_VFIO_VFIO_MIGRATION_INTERNAL_H
10 #define HW_VFIO_VFIO_MIGRATION_INTERNAL_H
11 
12 #ifdef CONFIG_LINUX
13 #include <linux/vfio.h>
14 #endif
15 
16 #include "qemu/typedefs.h"
17 #include "qemu/notify.h"
18 
19 /*
20  * Flags to be used as unique delimiters for VFIO devices in the migration
21  * stream. These flags are composed as:
22  * 0xffffffff => MSB 32-bit all 1s
23  * 0xef10     => Magic ID, represents emulated (virtual) function IO
24  * 0x0000     => 16-bits reserved for flags
25  *
26  * The beginning of state information is marked by _DEV_CONFIG_STATE,
27  * _DEV_SETUP_STATE, or _DEV_DATA_STATE, respectively. The end of a
28  * certain state information is marked by _END_OF_STATE.
29  */
30 #define VFIO_MIG_FLAG_END_OF_STATE      (0xffffffffef100001ULL)
31 #define VFIO_MIG_FLAG_DEV_CONFIG_STATE  (0xffffffffef100002ULL)
32 #define VFIO_MIG_FLAG_DEV_SETUP_STATE   (0xffffffffef100003ULL)
33 #define VFIO_MIG_FLAG_DEV_DATA_STATE    (0xffffffffef100004ULL)
34 #define VFIO_MIG_FLAG_DEV_INIT_DATA_SENT (0xffffffffef100005ULL)
35 #define VFIO_MIG_FLAG_DEV_CONFIG_LOAD_READY (0xffffffffef100006ULL)
36 
37 typedef struct VFIODevice VFIODevice;
38 typedef struct VFIOMultifd VFIOMultifd;
39 
40 typedef struct VFIOMigration {
41     struct VFIODevice *vbasedev;
42     VMChangeStateEntry *vm_state;
43     NotifierWithReturn migration_state;
44     uint32_t device_state;
45     int data_fd;
46     void *data_buffer;
47     size_t data_buffer_size;
48     uint64_t mig_flags;
49     uint64_t precopy_init_size;
50     uint64_t precopy_dirty_size;
51     bool multifd_transfer;
52     VFIOMultifd *multifd;
53     bool initial_data_sent;
54 
55     bool event_save_iterate_started;
56     bool event_precopy_empty_hit;
57 } VFIOMigration;
58 
59 bool vfio_migration_realize(VFIODevice *vbasedev, Error **errp);
60 void vfio_migration_exit(VFIODevice *vbasedev);
61 bool vfio_device_state_is_running(VFIODevice *vbasedev);
62 bool vfio_device_state_is_precopy(VFIODevice *vbasedev);
63 int vfio_save_device_config_state(QEMUFile *f, void *opaque, Error **errp);
64 int vfio_load_device_config_state(QEMUFile *f, void *opaque);
65 
66 #ifdef CONFIG_LINUX
67 int vfio_migration_set_state(VFIODevice *vbasedev,
68                              enum vfio_device_mig_state new_state,
69                              enum vfio_device_mig_state recover_state,
70                              Error **errp);
71 #endif
72 
73 void vfio_migration_add_bytes_transferred(unsigned long val);
74 
75 #endif /* HW_VFIO_VFIO_MIGRATION_INTERNAL_H */
76