summaryrefslogtreecommitdiffstats
path: root/legacy-driver-guest/vf/gsp_vf.c
blob: 951c84d803eb1aea0495657b929643c20a8949ec (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* SPDX-License-Identifier: GPL-2.0-only */
/*
 * GSP vGPU-IO Driver - VF Management
 */

#include "gsp_core.h"
#include <linux/slab.h>
#include <linux/idr.h>
#include <linux/list.h>

/* VF Reference Counting */
static void gsp_vf_release(struct kref *ref)
{
    struct gsp_vf *vf = container_of(ref, struct gsp_vf, ref);
    kfree(vf);
}

struct gsp_vf *gsp_vf_get(struct gsp_device *gsp, uint32_t id)
{
    struct gsp_vf *vf;
    
    mutex_lock(&gsp->vf_lock);
    vf = idr_find(&gsp->vf_idr, id);
    if (vf && vf->active)
        kref_get(&vf->ref);
    else
        vf = NULL;
    mutex_unlock(&gsp->vf_lock);
    
    return vf;
}

void gsp_vf_put(struct gsp_vf *vf)
{
    if (vf)
        kref_put(&vf->ref, gsp_vf_release);
}

/* VF Creation */
int gsp_vf_create(struct gsp_device *gsp, const struct gsp_vf_config *config)
{
    struct gsp_vf *vf;
    int ret, id;
    
    if (!config || config->id == 0 || config->id > gsp->max_vfs)
        return -EINVAL;
    
    vf = kzalloc(sizeof(*vf), GFP_KERNEL);
    if (!vf)
        return -ENOMEM;
    
    vf->gsp = gsp;
    vf->id = config->id;
    kref_init(&vf->ref);
    mutex_init(&vf->lock);
    mutex_init(&vf->wpr_lock);
    INIT_LIST_HEAD(&vf->wpr_list);
    INIT_LIST_HEAD(&vf->list);
    atomic_set(&vf->refcount, 1);
    
    /* Copy configuration */
    vf->bar1_offset = config->bar1_offset;
    vf->bar1_size = config->bar1_size;
    vf->pt_base = config->pt_base;
    vf->pt_size = config->pt_size;
    vf->cmd_base = config->cmd_base;
    vf->cmd_size = config->cmd_size;
    
    /* Allocate VF ID */
    mutex_lock(&gsp->vf_lock);
    id = idr_alloc(&gsp->vf_idr, vf, config->id, config->id + 1, GFP_KERNEL);
    if (id < 0) {
        ret = id;
        mutex_unlock(&gsp->vf_lock);
        goto err_free;
    }
    
    list_add(&vf->list, &gsp->vf_list);
    vf->active = true;
    vf->state = GSP_VF_ACTIVE;
    mutex_unlock(&gsp->vf_lock);
    
    /* Initialize subsystems */
    ret = gsp_vf_wpr_init(vf);
    if (ret) {
        dev_err(gsp->dev, "VF %u: WPR init failed: %d\n", vf->id, ret);
        goto err_wpr;
    }
    
    ret = gsp_vf_bar_init(vf);
    if (ret) {
        dev_err(gsp->dev, "VF %u: BAR init failed: %d\n", vf->id, ret);
        goto err_bar;
    }
    
    dev_info(gsp->dev, "VF %u created (bar1=0x%llx-0x%llx, pt=0x%llx-0x%llx)\n",
             vf->id, vf->bar1_offset, vf->bar1_offset + vf->bar1_size,
             vf->pt_base, vf->pt_base + vf->pt_size);
    
    return 0;

err_bar:
    gsp_vf_wpr_fini(vf);
err_wpr:
    mutex_lock(&gsp->vf_lock);
    list_del(&vf->list);
    idr_remove(&gsp->vf_idr, vf->id);
    vf->active = false;
    mutex_unlock(&gsp->vf_lock);
err_free:
    kfree(vf);
    return ret;
}

/* VF Destruction */
void gsp_vf_destroy(struct gsp_vf *vf)
{
    struct gsp_device *gsp = vf->gsp;
    uint32_t id = vf->id;
    
    if (!vf->active)
        return;
    
    /* Cancel any ongoing migration */
    mutex_lock(&vf->lock);
    if (vf->migration) {
        gsp_migration_cancel(vf->migration);
        vf->migration = NULL;
    }
    vf->active = false;
    vf->state = GSP_VF_DESTROYING;
    mutex_unlock(&vf->lock);
    
    /* Remove from device lists */
    mutex_lock(&gsp->vf_lock);
    list_del(&vf->list);
    idr_remove(&gsp->vf_idr, id);
    mutex_unlock(&gsp->vf_lock);
    
    /* Cleanup subsystems */
    gsp_vf_bar_fini(vf);
    gsp_vf_wpr_fini(vf);
    
    dev_info(gsp->dev, "VF %u destroyed\n", id);
    
    gsp_vf_put(vf);
}

/* Memory Allocation */
int gsp_vf_mem_alloc(struct gsp_vf *vf, uint64_t size, uint32_t flags,
                     uint64_t *handle, uint64_t *va)
{
    struct gsp_alloc_payload payload = {
        .size = size,
        .type = flags & 0x3,  /* Low bits: mem type */
        .alignment = 0x1000,
        .va_hint = 0,
        .flags = flags >> 2,
        .pid = current->pid,
        .vf_id = vf->id,
    };
    struct gsp_alloc_response resp;
    size_t resp_size = sizeof(resp);
    int ret;
    
    if (!handle || !va)
        return -EINVAL;
    
    /* In emulation mode, handle locally */
    if (emulation_mode) {
        static uint64_t next_handle = 0x1000;
        static uint64_t next_va = 0x100000000ULL;  /* 4GB base */
        
        *handle = next_handle++;
        *va = next_va;
        next_va += (size + 0xFFF) & ~0xFFF;  /* Align to 4KB */
        
        dev_dbg(vf->gsp->dev, "VF %u: alloc handle=0x%llx va=0x%llx size=0x%llx (emulation)\n",
                vf->id, *handle, *va, size);
        
        return 0;
    }
    
    ret = gsp_rpc_send_sync(vf->gsp->rpc, GSP_RPC_ALLOC,
                            &payload, sizeof(payload),
                            &resp, &resp_size);
    if (ret)
        return ret;
    
    *handle = resp.handle;
    *va = resp.va;
    
    dev_dbg(vf->gsp->dev, "VF %u: alloc handle=0x%llx va=0x%llx size=0x%llx\n",
            vf->id, *handle, *va, size);
    
    return 0;
}

int gsp_vf_mem_free(struct gsp_vf *vf, uint64_t handle)
{
    /* FREE uses primary dispatcher sub-command */
    struct gsp_subcmd_desc desc;
    size_t desc_size = sizeof(desc);
    int ret;
    
    /* Subcmd for FREE: need to determine which subcmd corresponds to FREE */
    /* For now, use a generic FREE RPC if implemented */
    
    /* Placeholder - actual implementation depends on FREE subcmd mapping */
    dev_dbg(vf->gsp->dev, "VF %u: free handle=0x%llx\n", vf->id, handle);
    
    return 0;
}

/* Memory Pinning */
int gsp_vf_mem_pin(struct gsp_vf *vf, uint64_t handle,
                   uint64_t offset, uint64_t size,
                   uint64_t *dma_addr)
{
    struct gsp_pin_payload payload = {
        .handle = handle,
        .offset = offset,
        .size = size,
        .vf_id = vf->id,
        .flags = GSP_PIN_READ | GSP_PIN_WRITE,
    };
    struct gsp_pin_response resp;
    size_t resp_size = sizeof(resp);
    int ret;
    
    if (!dma_addr)
        return -EINVAL;
    
    /* In emulation mode, handle locally */
    if (emulation_mode) {
        static uint64_t next_dma = 0x200000000ULL;  /* 8GB base */
        *dma_addr = next_dma;
        next_dma += (size + 0xFFF) & ~0xFFF;
        
        dev_dbg(vf->gsp->dev, "VF %u: pin handle=0x%llx offset=0x%llx size=0x%llx -> dma=0x%llx (emulation)\n",
                vf->id, handle, offset, size, *dma_addr);
        
        return 0;
    }
    
    ret = gsp_rpc_send_sync(vf->gsp->rpc, GSP_RPC_PIN,
                            &payload, sizeof(payload),
                            &resp, &resp_size);
    if (ret)
        return ret;
    
    *dma_addr = resp.dma_addr;
    
    dev_dbg(vf->gsp->dev, "VF %u: pin handle=0x%llx offset=0x%llx size=0x%llx -> dma=0x%llx\n",
            vf->id, handle, offset, size, *dma_addr);
    
    return 0;
}

int gsp_vf_mem_unpin(struct gsp_vf *vf, uint64_t handle)
{
    dev_dbg(vf->gsp->dev, "VF %u: unpin handle=0x%llx\n", vf->id, handle);
    return 0;
}

/* Memory Mapping */
int gsp_vf_mem_map(struct gsp_vf *vf, uint64_t handle, uint64_t gpu_va,
                   uint64_t size, uint32_t prot, uint32_t flags,
                   uint64_t offset, uint64_t *mapped_va)
{
    /* In emulation mode, just return the requested VA */
    if (emulation_mode) {
        if (mapped_va)
            *mapped_va = gpu_va;
        
        dev_dbg(vf->gsp->dev, "VF %u: map handle=0x%llx gpu_va=0x%llx size=0x%llx -> mapped_va=0x%llx (emulation)\n",
                vf->id, handle, gpu_va, size, gpu_va);
        
        return 0;
    }
    
    /* TODO: Implement GSP RPC for MAP */
    dev_dbg(vf->gsp->dev, "VF %u: map handle=0x%llx gpu_va=0x%llx size=0x%llx\n",
            vf->id, handle, gpu_va, size);
    
    if (mapped_va)
        *mapped_va = gpu_va;
    
    return 0;
}

int gsp_vf_mem_unmap(struct gsp_vf *vf, uint64_t handle, uint64_t gpu_va, uint64_t size)
{
    dev_dbg(vf->gsp->dev, "VF %u: unmap handle=0x%llx gpu_va=0x%llx size=0x%llx\n",
            vf->id, handle, gpu_va, size);
    return 0;
}

/* Scheduling */
int gsp_vf_schedule(struct gsp_vf *vf, uint32_t priority, uint64_t time_slice_ns)
{
    /* Use primary dispatcher SCHEDULE subcmd or secondary dispatcher */
    dev_dbg(vf->gsp->dev, "VF %u: schedule prio=%u slice=%llu ns\n",
            vf->id, priority, time_slice_ns);
    return 0;
}

int gsp_vf_yield(struct gsp_vf *vf)
{
    dev_dbg(vf->gsp->dev, "VF %u: yield\n", vf->id);
    return 0;
}