aboutsummaryrefslogtreecommitdiff
path: root/include/hw/vfio/vfio-device.h
blob: 1901a35aa90214c36369f8ac0b0cfef36acbbaa8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * VFIO Device interface
 *
 * Copyright Red Hat, Inc. 2012
 *
 * Authors:
 *  Alex Williamson <alex.williamson@redhat.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 * the COPYING file in the top-level directory.
 *
 * Based on qemu-kvm device-assignment:
 *  Adapted for KVM by Qumranet.
 *  Copyright (c) 2007, Neocleus, Alex Novik (alex@neocleus.com)
 *  Copyright (c) 2007, Neocleus, Guy Zana (guy@neocleus.com)
 *  Copyright (C) 2008, Qumranet, Amit Shah (amit.shah@qumranet.com)
 *  Copyright (C) 2008, Red Hat, Amit Shah (amit.shah@redhat.com)
 *  Copyright (C) 2008, IBM, Muli Ben-Yehuda (muli@il.ibm.com)
 */

#ifndef HW_VFIO_VFIO_COMMON_H
#define HW_VFIO_VFIO_COMMON_H

#include "system/memory.h"
#include "qemu/queue.h"
#ifdef CONFIG_LINUX
#include <linux/vfio.h>
#endif
#include "system/system.h"
#include "hw/vfio/vfio-container-base.h"
#include "hw/vfio/vfio-cpr.h"
#include "system/host_iommu_device.h"
#include "system/iommufd.h"

#define VFIO_MSG_PREFIX "vfio %s: "

enum {
    VFIO_DEVICE_TYPE_PCI = 0,
    VFIO_DEVICE_TYPE_PLATFORM = 1,
    VFIO_DEVICE_TYPE_CCW = 2,
    VFIO_DEVICE_TYPE_AP = 3,
};

typedef struct VFIODeviceOps VFIODeviceOps;
typedef struct VFIODeviceIOOps VFIODeviceIOOps;
typedef struct VFIOMigration VFIOMigration;

typedef struct IOMMUFDBackend IOMMUFDBackend;
typedef struct VFIOIOASHwpt VFIOIOASHwpt;
typedef struct VFIOUserProxy VFIOUserProxy;

typedef struct VFIODevice {
    QLIST_ENTRY(VFIODevice) next;
    QLIST_ENTRY(VFIODevice) container_next;
    QLIST_ENTRY(VFIODevice) global_next;
    struct VFIOGroup *group;
    VFIOContainerBase *bcontainer;
    char *sysfsdev;
    char *name;
    DeviceState *dev;
    int fd;
    int type;
    bool mdev;
    bool reset_works;
    bool needs_reset;
    bool no_mmap;
    bool ram_block_discard_allowed;
    OnOffAuto enable_migration;
    OnOffAuto migration_multifd_transfer;
    bool migration_events;
    bool use_region_fds;
    VFIODeviceOps *ops;
    VFIODeviceIOOps *io_ops;
    unsigned int num_irqs;
    unsigned int num_regions;
    unsigned int flags;
    VFIOMigration *migration;
    Error *migration_blocker;
    OnOffAuto pre_copy_dirty_page_tracking;
    OnOffAuto device_dirty_page_tracking;
    bool dirty_pages_supported;
    bool dirty_tracking; /* Protected by BQL */
    bool iommu_dirty_tracking;
    HostIOMMUDevice *hiod;
    int devid;
    IOMMUFDBackend *iommufd;
    VFIOIOASHwpt *hwpt;
    QLIST_ENTRY(VFIODevice) hwpt_next;
    struct vfio_region_info **reginfo;
    int *region_fds;
    VFIODeviceCPR cpr;
    VFIOUserProxy *proxy;
} VFIODevice;

struct VFIODeviceOps {
    void (*vfio_compute_needs_reset)(VFIODevice *vdev);
    int (*vfio_hot_reset_multi)(VFIODevice *vdev);
    void (*vfio_eoi)(VFIODevice *vdev);
    Object *(*vfio_get_object)(VFIODevice *vdev);

    /**
     * @vfio_save_config
     *
     * Save device config state
     *
     * @vdev: #VFIODevice for which to save the config
     * @f: #QEMUFile where to send the data
     * @errp: pointer to Error*, to store an error if it happens.
     *
     * Returns zero to indicate success and negative for error
     */
    int (*vfio_save_config)(VFIODevice *vdev, QEMUFile *f, Error **errp);

    /**
     * @vfio_load_config
     *
     * Load device config state
     *
     * @vdev: #VFIODevice for which to load the config
     * @f: #QEMUFile where to get the data
     *
     * Returns zero to indicate success and negative for error
     */
    int (*vfio_load_config)(VFIODevice *vdev, QEMUFile *f);
};

/*
 * Given a return value of either a short number of bytes read or -errno,
 * construct a meaningful error message.
 */
#define strreaderror(ret) \
    (ret < 0 ? strerror(-ret) : "short read")

/*
 * Given a return value of either a short number of bytes written or -errno,
 * construct a meaningful error message.
 */
#define strwriteerror(ret) \
    (ret < 0 ? strerror(-ret) : "short write")

void vfio_device_irq_disable(VFIODevice *vbasedev, int index);
void vfio_device_irq_unmask(VFIODevice *vbasedev, int index);
void vfio_device_irq_mask(VFIODevice *vbasedev, int index);
bool vfio_device_irq_set_signaling(VFIODevice *vbasedev, int index, int subindex,
                                   int action, int fd, Error **errp);

void vfio_device_reset_handler(void *opaque);
bool vfio_device_is_mdev(VFIODevice *vbasedev);
bool vfio_device_hiod_create_and_realize(VFIODevice *vbasedev,
                                         const char *typename, Error **errp);
bool vfio_device_attach(char *name, VFIODevice *vbasedev,
                        AddressSpace *as, Error **errp);
bool vfio_device_attach_by_iommu_type(const char *iommu_type, char *name,
                                      VFIODevice *vbasedev, AddressSpace *as,
                                      Error **errp);
void vfio_device_detach(VFIODevice *vbasedev);
VFIODevice *vfio_get_vfio_device(Object *obj);

typedef QLIST_HEAD(VFIODeviceList, VFIODevice) VFIODeviceList;
extern VFIODeviceList vfio_device_list;

#ifdef CONFIG_LINUX
/*
 * How devices communicate with the server.  The default option is through
 * ioctl() to the kernel VFIO driver, but vfio-user can use a socket to a remote
 * process.
 */
struct VFIODeviceIOOps {
    /**
     * @device_feature
     *
     * Fill in feature info for the given device.
     *
     * @vdev: #VFIODevice to use
     * @feat: feature information to fill in
     *
     * Returns 0 on success or -errno.
     */
    int (*device_feature)(VFIODevice *vdev, struct vfio_device_feature *feat);

    /**
     * @get_region_info
     *
     * Get the information for a given region on the device.
     *
     * @vdev: #VFIODevice to use
     * @info: set @info->index to the region index to look up; the rest of the
     *        struct will be filled in on success
     * @fd: pointer to the fd for the region; will be -1 if not found
     *
     * Returns 0 on success or -errno.
     */
    int (*get_region_info)(VFIODevice *vdev,
                           struct vfio_region_info *info, int *fd);

    /**
     * @get_irq_info
     *
     * @vdev: #VFIODevice to use
     * @irq: set @irq->index to the IRQ index to look up; the rest of the struct
     *       will be filled in on success
     *
     * Returns 0 on success or -errno.
     */
    int (*get_irq_info)(VFIODevice *vdev, struct vfio_irq_info *irq);

    /**
     * @set_irqs
     *
     * Configure IRQs.
     *
     * @vdev: #VFIODevice to use
     * @irqs: IRQ configuration as defined by VFIO docs.
     *
     * Returns 0 on success or -errno.
     */
    int (*set_irqs)(VFIODevice *vdev, struct vfio_irq_set *irqs);

    /**
     * @region_read
     *
     * Read part of a region.
     *
     * @vdev: #VFIODevice to use
     * @nr: region index
     * @off: offset within the region
     * @size: size in bytes to read
     * @data: buffer to read into
     *
     * Returns number of bytes read on success or -errno.
     */
    int (*region_read)(VFIODevice *vdev, uint8_t nr, off_t off, uint32_t size,
                       void *data);

    /**
     * @region_write
     *
     * Write part of a region.
     *
     * @vdev: #VFIODevice to use
     * @nr: region index
     * @off: offset within the region
     * @size: size in bytes to write
     * @data: buffer to write from
     * @post: true if this is a posted write
     *
     * Returns number of bytes write on success or -errno.
     */
    int (*region_write)(VFIODevice *vdev, uint8_t nr, off_t off, uint32_t size,
                        void *data, bool post);
};

void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainerBase *bcontainer,
                         struct vfio_device_info *info);

void vfio_device_unprepare(VFIODevice *vbasedev);

int vfio_device_get_region_info(VFIODevice *vbasedev, int index,
                                struct vfio_region_info **info);
int vfio_device_get_region_info_type(VFIODevice *vbasedev, uint32_t type,
                                     uint32_t subtype, struct vfio_region_info **info);

/**
 * Return the fd for mapping this region. This is either the device's fd (for
 * e.g. kernel vfio), or a per-region fd (for vfio-user).
 *
 * @vbasedev: #VFIODevice to use
 * @index: region index
 *
 * Returns the fd.
 */
int vfio_device_get_region_fd(VFIODevice *vbasedev, int index);

bool vfio_device_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type);

int vfio_device_get_irq_info(VFIODevice *vbasedev, int index,
                                struct vfio_irq_info *info);
#endif

/* Returns 0 on success, or a negative errno. */
bool vfio_device_get_name(VFIODevice *vbasedev, Error **errp);
void vfio_device_free_name(VFIODevice *vbasedev);
void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp);
void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops,
                      DeviceState *dev, bool ram_discard);
int vfio_device_get_aw_bits(VFIODevice *vdev);

void vfio_kvm_device_close(void);
#endif /* HW_VFIO_VFIO_COMMON_H */