summaryrefslogtreecommitdiffstats
path: root/userspace/tests/test_guest_init.c
blob: 135c16f7c95aee93eac3a05924ab01d46e5c45c2 (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
/*
 * Test for guest vGPU driver initialization
 * This test verifies that the guest vGPU driver can initialize properly
 * when passed through to a VM via VFIO/mdev
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/vfio.h>
#include <linux/mdev.h>
#include <errno.h>

#define NV_VGPU_IOCTL_MAGIC 'N'
#define NV_VGPU_IOCTL_GET_INFO \
	_IOWR(NV_VGPU_IOCTL_MAGIC, 0x01, struct nv_vgpu_ioctl_get_info)
#define NV_VGPU_IOCTL_MAP_BAR \
	_IOWR(NV_VGPU_IOCTL_MAGIC, 0x02, struct nv_vgpu_ioctl_map_bar)
#define NV_VGPU_IOCTL_GSP_RPC \
	_IOWR(NV_VGPU_IOCTL_MAGIC, 0x06, struct nv_vgpu_ioctl_gsp_rpc)

struct nv_vgpu_ioctl_get_info {
    uint32_t vgpu_type;
    uint32_t vgpu_id;
    uint32_t gpu_instance_id;
    uint32_t compute_instance_id;
    uint32_t state;
    uint64_t fb_base;
    uint64_t fb_size;
    uint64_t bar0_config_base;
    uint32_t num_bars;
    struct {
        uint32_t index;
        uint64_t gpa;
        uint64_t size;
        uint64_t iova;
        bool mapped;
        bool trapped;
    } bars[8];
};

struct nv_vgpu_ioctl_map_bar {
    uint32_t bar_index;
    uint64_t gpa;
    uint64_t size;
    uint64_t iova;
    uint32_t flags;
};

struct nv_vgpu_ioctl_gsp_rpc {
    uint32_t type;
    uint32_t flags;
    uint64_t data_ptr;
    uint32_t data_size;
    uint32_t result;
};

int main(int argc, char *argv[])
{
    int ret = 0;
    int vfio_fd = -1;
    int mdev_fd = -1;
    char *mdev_path = NULL;
    struct nv_vgpu_ioctl_get_info info = {0};
    struct nv_vgpu_ioctl_map_bar map_bar = {0};
    struct vfio_device_info dev_info = {0};

    printf("Testing guest vGPU driver initialization...\n");

    if (argc < 2) {
        printf("Usage: %s <mdev_uuid>\n", argv[0]);
        printf("Example: %s 00000000-0000-0000-0000-000000000000\n", argv[0]);
        return 1;
    }

    mdev_path = argv[1];

    vfio_fd = open("/dev/vfio/vfio", O_RDWR);
    if (vfio_fd < 0) {
        printf("FAIL: Cannot open /dev/vfio/vfio: %s\n", strerror(errno));
        return 1;
    }

    dev_info.argsz = sizeof(dev_info);
    if (ioctl(vfio_fd, VFIO_GET_API_VERSION, &dev_info) < 0) {
        printf("FAIL: VFIO_GET_API_VERSION failed: %s\n", strerror(errno));
        ret = 1;
        goto cleanup;
    }

    if (dev_info.version != VFIO_API_VERSION) {
        printf("FAIL: VFIO API version mismatch\n");
        ret = 1;
        goto cleanup;
    }

    printf("PASS: VFIO API version %d\n", dev_info.version);

    char mdev_dev_path[256];
    snprintf(mdev_dev_path, sizeof(mdev_dev_path), "/dev/vfio/%s", mdev_path);

    mdev_fd = open(mdev_dev_path, O_RDWR);
    if (mdev_fd < 0) {
        printf("FAIL: Cannot open mdev device %s: %s\n", mdev_dev_path, strerror(errno));
        ret = 1;
        goto cleanup;
    }

    printf("PASS: Opened mdev device %s\n", mdev_dev_path);

    dev_info.argsz = sizeof(dev_info);
    if (ioctl(mdev_fd, VFIO_DEVICE_GET_INFO, &dev_info) < 0) {
        printf("FAIL: VFIO_DEVICE_GET_INFO failed: %s\n", strerror(errno));
        ret = 1;
        goto cleanup;
    }

    printf("PASS: Device info - flags: 0x%x, num_regions: %d, num_irqs: %d\n",
           dev_info.flags, dev_info.num_regions, dev_info.num_irqs);

    if (ioctl(mdev_fd, NV_VGPU_IOCTL_GET_INFO, &info) < 0) {
        printf("FAIL: NV_VGPU_IOCTL_GET_INFO failed: %s\n", strerror(errno));
        ret = 1;
        goto cleanup;
    }

    printf("PASS: vGPU info - type: %d, id: %d, state: %d\n",
           info.vgpu_type, info.vgpu_id, info.state);
    printf("       fb_base: 0x%lx, fb_size: 0x%lx\n", info.fb_base, info.fb_size);
    printf("       bar0_config_base: 0x%lx\n", info.bar0_config_base);
    printf("       num_bars: %d\n", info.num_bars);

    for (int i = 0; i < info.num_bars; i++) {
        printf("       BAR%d: gpa=0x%lx, size=0x%lx, iova=0x%lx, mapped=%d, trapped=%d\n",
               info.bars[i].index, info.bars[i].gpa, info.bars[i].size,
               info.bars[i].iova, info.bars[i].mapped, info.bars[i].trapped);
    }

    if (info.num_bars > 0) {
        map_bar.bar_index = 0;
        map_bar.gpa = info.bars[0].gpa;
        map_bar.size = info.bars[0].size;
        map_bar.flags = 0x1;

        if (ioctl(mdev_fd, NV_VGPU_IOCTL_MAP_BAR, &map_bar) < 0) {
            printf("FAIL: NV_VGPU_IOCTL_MAP_BAR failed: %s\n", strerror(errno));
            ret = 1;
            goto cleanup;
        }

        printf("PASS: Mapped BAR0 at IOVA 0x%lx\n", map_bar.iova);
    }

    struct nv_vgpu_ioctl_gsp_rpc rpc = {0};
    rpc.type = 0x01;
    rpc.data_size = 0;

    if (ioctl(mdev_fd, NV_VGPU_IOCTL_GSP_RPC, &rpc) < 0) {
        printf("WARN: NV_VGPU_IOCTL_GSP_RPC failed: %s (may not be implemented yet)\n", strerror(errno));
    } else {
        printf("PASS: GSP RPC test completed\n");
    }

    printf("\nAll guest initialization tests PASSED\n");

cleanup:
    if (mdev_fd >= 0)
        close(mdev_fd);
    if (vfio_fd >= 0)
        close(vfio_fd);

    return ret;
}