xref: /qemu/hw/vfio/vfio-migration-internal.h (revision eb6caa79162a89a8dcbe6a6d4788acd813b687a2)
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 
36 typedef struct VFIODevice VFIODevice;
37 typedef struct VFIOMultifd VFIOMultifd;
38 
39 typedef struct VFIOMigration {
40     struct VFIODevice *vbasedev;
41     VMChangeStateEntry *vm_state;
42     NotifierWithReturn migration_state;
43     uint32_t device_state;
44     int data_fd;
45     void *data_buffer;
46     size_t data_buffer_size;
47     uint64_t mig_flags;
48     uint64_t precopy_init_size;
49     uint64_t precopy_dirty_size;
50     bool multifd_transfer;
51     VFIOMultifd *multifd;
52     bool initial_data_sent;
53 
54     bool event_save_iterate_started;
55     bool event_precopy_empty_hit;
56 } VFIOMigration;
57 
58 bool vfio_migration_realize(VFIODevice *vbasedev, Error **errp);
59 void vfio_migration_exit(VFIODevice *vbasedev);
60 bool vfio_device_state_is_running(VFIODevice *vbasedev);
61 bool vfio_device_state_is_precopy(VFIODevice *vbasedev);
62 int vfio_save_device_config_state(QEMUFile *f, void *opaque, Error **errp);
63 int vfio_load_device_config_state(QEMUFile *f, void *opaque);
64 
65 #ifdef CONFIG_LINUX
66 int vfio_migration_set_state(VFIODevice *vbasedev,
67                              enum vfio_device_mig_state new_state,
68                              enum vfio_device_mig_state recover_state,
69                              Error **errp);
70 #endif
71 
72 void vfio_migration_add_bytes_transferred(unsigned long val);
73 
74 #endif /* HW_VFIO_VFIO_MIGRATION_INTERNAL_H */
75