summaryrefslogtreecommitdiffstats
path: root/userspace/examples/example_vgpu.c
blob: ede6873ac3cc4ad5a761d3cbe5baf58398eb1cb8 (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
/*
 * GSP vGPU-IO Example Application
 * Demonstrates basic VF creation, memory allocation, and BAR configuration
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <libvgpu.h>

static volatile bool running = true;

static void signal_handler(int sig)
{
    (void)sig;
    running = false;
}

int main(int argc, char *argv[])
{
    const char *device_path = "/dev/gsp-vgpu";
    vgpu_device_t device = NULL;
    vgpu_vf_t vf = NULL;
    vgpu_handle_t handle = 0;
    uint64_t va = 0;
    uint64_t dma_addr = 0;
    int ret;

    if (argc > 1)
        device_path = argv[1];

    printf("GSP vGPU-IO Example Application\n");
    printf("Device: %s\n", device_path);

    signal(SIGINT, signal_handler);
    signal(SIGTERM, signal_handler);

    /* Open device */
    ret = vgpu_open(device_path, &device);
    if (ret != VGPU_OK) {
        fprintf(stderr, "Failed to open device: %s\n", vgpu_strerror(ret));
        return 1;
    }
    printf("Device opened successfully\n");

    /* Get device info */
    char info[256];
    ret = vgpu_get_info(device, info, sizeof(info));
    if (ret == VGPU_OK)
        printf("Device info: %s\n", info);

    /* Create VF */
    vgpu_vf_config_t config = {
        .id = 1,
        .bar1_offset = 0x10000000,
        .bar1_size = 0x40000,
        .pt_base = 0,
        .pt_size = 0,
        .cmd_base = 0,
        .cmd_size = 0,
        .max_memory_mb = 1024,
        .num_engines = 4,
    };

    ret = vgpu_vf_create(device, &config, &vf);
    if (ret != VGPU_OK) {
        fprintf(stderr, "Failed to create VF: %s\n", vgpu_strerror(ret));
        vgpu_close(device);
        return 1;
    }
    printf("VF %u created\n", config.id);

    /* Configure BAR1 */
    ret = vgpu_bar_config(vf, 0x10000000, 0x40000);
    if (ret != VGPU_OK) {
        fprintf(stderr, "Failed to configure BAR: %s\n", vgpu_strerror(ret));
        vgpu_vf_destroy(vf);
        vgpu_close(device);
        return 1;
    }
    printf("BAR1 configured\n");

    /* Allocate VRAM */
    const uint64_t alloc_size = 1024 * 1024;  /* 1 MB */
    ret = vgpu_mem_alloc(vf, alloc_size, VGPU_MEM_VRAM, 0x1000, 0,
                         0, &handle, &va);
    if (ret != VGPU_OK) {
        fprintf(stderr, "Failed to allocate memory: %s\n", vgpu_strerror(ret));
        vgpu_vf_destroy(vf);
        vgpu_close(device);
        return 1;
    }
    printf("Allocated %llu bytes at VA 0x%llx (handle 0x%llx)\n",
           (unsigned long long)alloc_size, (unsigned long long)va,
           (unsigned long long)handle);

    /* Map memory */
    uint64_t mapped_va = 0;
    ret = vgpu_mem_map(vf, handle, va, alloc_size, 0x3, 0, 0, &mapped_va);
    if (ret != VGPU_OK) {
        fprintf(stderr, "Failed to map memory: %s\n", vgpu_strerror(ret));
        vgpu_mem_free(vf, handle);
        vgpu_vf_destroy(vf);
        vgpu_close(device);
        return 1;
    }
    printf("Mapped at 0x%llx\n", (unsigned long long)mapped_va);

    /* Pin for DMA */
    ret = vgpu_mem_pin(vf, handle, 0, alloc_size, VGPU_PIN_READ | VGPU_PIN_WRITE, &dma_addr);
    if (ret != VGPU_OK) {
        fprintf(stderr, "Failed to pin memory: %s\n", vgpu_strerror(ret));
    } else {
        printf("Pinned for DMA at 0x%llx\n", (unsigned long long)dma_addr);
    }

    /* Protect with WPR (write) */
    ret = vgpu_wpr_protect_write(vf, va, alloc_size);
    if (ret != VGPU_OK) {
        fprintf(stderr, "Failed to protect with WPR: %s\n", vgpu_strerror(ret));
    } else {
        printf("WPR write protection enabled\n");
    }

    /* Set WPR timeout */
    ret = vgpu_wpr_set_timeout(vf, 1000000000ULL);  /* 1 second */
    if (ret != VGPU_OK) {
        fprintf(stderr, "Failed to set WPR timeout: %s\n", vgpu_strerror(ret));
    }

    /* Schedule work */
    ret = vgpu_schedule(vf, handle, 0, 0, 0, 0);
    if (ret != VGPU_OK) {
        fprintf(stderr, "Failed to schedule: %s\n", vgpu_strerror(ret));
    } else {
        printf("Work scheduled\n");
    }

    /* Run loop */
    printf("\nRunning... Press Ctrl+C to exit\n");
    while (running) {
        ret = vgpu_process_events(device, 1000);
        if (ret != VGPU_OK && ret != VGPU_ERR_TIMEOUT) {
            fprintf(stderr, "Event processing error: %s\n", vgpu_strerror(ret));
        }
    }

    printf("\nShutting down...\n");

    /* Unprotect WPR */
    vgpu_wpr_unprotect(vf, va);

    /* Unpin */
    if (dma_addr)
        vgpu_mem_unpin(vf, handle, 0, alloc_size);

    /* Unmap */
    vgpu_mem_unmap(vf, handle, va, alloc_size);

    /* Free memory */
    vgpu_mem_free(vf, handle);

    /* Destroy VF */
    vgpu_vf_destroy(vf);

    /* Close device */
    vgpu_close(device);

    printf("Done\n");
    return 0;
}