vfio-common.h 6.0 KB
Newer Older
E
Eric Auger 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * common header for vfio based device assignment support
 *
 * 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)
 */
20

E
Eric Auger 已提交
21 22 23 24 25 26 27
#ifndef HW_VFIO_VFIO_COMMON_H
#define HW_VFIO_VFIO_COMMON_H

#include "qemu-common.h"
#include "exec/memory.h"
#include "qemu/queue.h"
#include "qemu/notify.h"
28
#include "ui/console.h"
G
Gerd Hoffmann 已提交
29
#include "hw/display/ramfb.h"
30 31 32
#ifdef CONFIG_LINUX
#include <linux/vfio.h>
#endif
E
Eric Auger 已提交
33

34 35 36
#define ERR_PREFIX "vfio error: %s: "
#define WARN_PREFIX "vfio warning: %s: "

E
Eric Auger 已提交
37 38
enum {
    VFIO_DEVICE_TYPE_PCI = 0,
39
    VFIO_DEVICE_TYPE_PLATFORM = 1,
40
    VFIO_DEVICE_TYPE_CCW = 2,
41
    VFIO_DEVICE_TYPE_AP = 3,
E
Eric Auger 已提交
42 43
};

44 45 46 47 48 49 50
typedef struct VFIOMmap {
    MemoryRegion mem;
    void *mmap;
    off_t offset;
    size_t size;
} VFIOMmap;

E
Eric Auger 已提交
51 52 53
typedef struct VFIORegion {
    struct VFIODevice *vbasedev;
    off_t fd_offset; /* offset of region within device fd */
54
    MemoryRegion *mem; /* slow, read/write access */
E
Eric Auger 已提交
55 56
    size_t size;
    uint32_t flags; /* VFIO region flags (rd/wr/mmap) */
57 58
    uint32_t nr_mmaps;
    VFIOMmap *mmaps;
E
Eric Auger 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72
    uint8_t nr; /* cache the region number for debug */
} VFIORegion;

typedef struct VFIOAddressSpace {
    AddressSpace *as;
    QLIST_HEAD(, VFIOContainer) containers;
    QLIST_ENTRY(VFIOAddressSpace) list;
} VFIOAddressSpace;

struct VFIOGroup;

typedef struct VFIOContainer {
    VFIOAddressSpace *space;
    int fd; /* /dev/vfio/vfio, empowered by the attached groups */
73
    MemoryListener listener;
74 75
    MemoryListener prereg_listener;
    unsigned iommu_type;
76 77
    int error;
    bool initialized;
78
    unsigned long pgsizes;
79 80 81 82 83
    /*
     * This assumes the host IOMMU can support only a single
     * contiguous IOVA window.  We may need to generalize that in
     * future
     */
E
Eric Auger 已提交
84
    QLIST_HEAD(, VFIOGuestIOMMU) giommu_list;
85
    QLIST_HEAD(, VFIOHostDMAWindow) hostwin_list;
E
Eric Auger 已提交
86 87 88 89 90 91
    QLIST_HEAD(, VFIOGroup) group_list;
    QLIST_ENTRY(VFIOContainer) next;
} VFIOContainer;

typedef struct VFIOGuestIOMMU {
    VFIOContainer *container;
92
    IOMMUMemoryRegion *iommu;
93
    hwaddr iommu_offset;
94
    IOMMUNotifier n;
E
Eric Auger 已提交
95 96 97
    QLIST_ENTRY(VFIOGuestIOMMU) giommu_next;
} VFIOGuestIOMMU;

98 99 100 101 102 103 104
typedef struct VFIOHostDMAWindow {
    hwaddr min_iova;
    hwaddr max_iova;
    uint64_t iova_pgsizes;
    QLIST_ENTRY(VFIOHostDMAWindow) hostwin_next;
} VFIOHostDMAWindow;

E
Eric Auger 已提交
105 106 107 108 109
typedef struct VFIODeviceOps VFIODeviceOps;

typedef struct VFIODevice {
    QLIST_ENTRY(VFIODevice) next;
    struct VFIOGroup *group;
110
    char *sysfsdev;
E
Eric Auger 已提交
111
    char *name;
112
    DeviceState *dev;
E
Eric Auger 已提交
113 114 115 116
    int fd;
    int type;
    bool reset_works;
    bool needs_reset;
117
    bool no_mmap;
118
    bool balloon_allowed;
E
Eric Auger 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    VFIODeviceOps *ops;
    unsigned int num_irqs;
    unsigned int num_regions;
    unsigned int flags;
} VFIODevice;

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

typedef struct VFIOGroup {
    int fd;
    int groupid;
    VFIOContainer *container;
    QLIST_HEAD(, VFIODevice) device_list;
    QLIST_ENTRY(VFIOGroup) next;
    QLIST_ENTRY(VFIOGroup) container_next;
138
    bool balloon_allowed;
E
Eric Auger 已提交
139 140
} VFIOGroup;

141 142 143 144 145 146 147 148
typedef struct VFIODMABuf {
    QemuDmaBuf buf;
    uint32_t pos_x, pos_y, pos_updates;
    uint32_t hot_x, hot_y, hot_updates;
    int dmabuf_id;
    QTAILQ_ENTRY(VFIODMABuf) next;
} VFIODMABuf;

149 150
typedef struct VFIODisplay {
    QemuConsole *con;
G
Gerd Hoffmann 已提交
151
    RAMFBState *ramfb;
152 153 154 155
    struct {
        VFIORegion buffer;
        DisplaySurface *surface;
    } region;
156 157 158 159 160
    struct {
        QTAILQ_HEAD(, VFIODMABuf) bufs;
        VFIODMABuf *primary;
        VFIODMABuf *cursor;
    } dmabuf;
161 162
} VFIODisplay;

E
Eric Auger 已提交
163 164 165 166 167 168 169 170
void vfio_put_base_device(VFIODevice *vbasedev);
void vfio_disable_irqindex(VFIODevice *vbasedev, int index);
void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index);
void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index);
void vfio_region_write(void *opaque, hwaddr addr,
                           uint64_t data, unsigned size);
uint64_t vfio_region_read(void *opaque,
                          hwaddr addr, unsigned size);
171 172 173 174 175 176
int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region,
                      int index, const char *name);
int vfio_region_mmap(VFIORegion *region);
void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled);
void vfio_region_exit(VFIORegion *region);
void vfio_region_finalize(VFIORegion *region);
E
Eric Auger 已提交
177
void vfio_reset_handler(void *opaque);
178
VFIOGroup *vfio_get_group(int groupid, AddressSpace *as, Error **errp);
E
Eric Auger 已提交
179 180
void vfio_put_group(VFIOGroup *group);
int vfio_get_device(VFIOGroup *group, const char *name,
181
                    VFIODevice *vbasedev, Error **errp);
E
Eric Auger 已提交
182 183 184 185 186

extern const MemoryRegionOps vfio_region_ops;
extern QLIST_HEAD(vfio_group_head, VFIOGroup) vfio_group_list;
extern QLIST_HEAD(vfio_as_head, VFIOAddressSpace) vfio_address_spaces;

187 188 189
#ifdef CONFIG_LINUX
int vfio_get_region_info(VFIODevice *vbasedev, int index,
                         struct vfio_region_info **info);
190 191
int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type,
                             uint32_t subtype, struct vfio_region_info **info);
192
bool vfio_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type);
193
#endif
194 195
extern const MemoryListener vfio_prereg_listener;

196 197 198 199 200 201
int vfio_spapr_create_window(VFIOContainer *container,
                             MemoryRegionSection *section,
                             hwaddr *pgsize);
int vfio_spapr_remove_window(VFIOContainer *container,
                             hwaddr offset_within_address_space);

202
#endif /* HW_VFIO_VFIO_COMMON_H */