Coverage Report

Created: 2026-10-31 08:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/hw/pci/pcie_sriov.c
Line
Count
Source
1
/*
2
 * pcie_sriov.c:
3
 *
4
 * Implementation of SR/IOV emulation support.
5
 *
6
 * Copyright (c) 2015-2017 Knut Omang <knut.omang@oracle.com>
7
 *
8
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
9
 * See the COPYING file in the top-level directory.
10
 *
11
 */
12
13
#include "qemu/osdep.h"
14
#include "hw/pci/pci_device.h"
15
#include "hw/pci/pcie.h"
16
#include "hw/pci/pci_bus.h"
17
#include "hw/core/qdev-properties.h"
18
#include "qemu/range.h"
19
#include "qapi/error.h"
20
#include "trace.h"
21
22
static GHashTable *pfs;
23
24
static void unparent_vfs(PCIDevice *dev, uint16_t total_vfs)
25
0
{
26
0
    for (uint16_t i = 0; i < total_vfs; i++) {
27
0
        PCIDevice *vf = dev->exp.sriov_pf.vf[i];
28
0
        object_unparent(OBJECT(vf));
29
0
        object_unref(OBJECT(vf));
30
0
    }
31
0
    g_free(dev->exp.sriov_pf.vf);
32
0
    dev->exp.sriov_pf.vf = NULL;
33
0
}
34
35
static void register_vfs(PCIDevice *dev)
36
0
{
37
0
    uint16_t num_vfs;
38
0
    uint16_t i;
39
0
    uint16_t sriov_cap = dev->exp.sriov_cap;
40
41
0
    assert(sriov_cap > 0);
42
0
    num_vfs = pci_get_word(dev->config + sriov_cap + PCI_SRIOV_NUM_VF);
43
44
0
    trace_sriov_register_vfs(dev->name, PCI_SLOT(dev->devfn),
45
0
                             PCI_FUNC(dev->devfn), num_vfs);
46
0
    for (i = 0; i < num_vfs; i++) {
47
0
        pci_set_enabled(dev->exp.sriov_pf.vf[i], true);
48
0
    }
49
50
0
    pci_set_word(dev->wmask + sriov_cap + PCI_SRIOV_NUM_VF, 0);
51
0
}
52
53
static void unregister_vfs(PCIDevice *dev)
54
19
{
55
19
    uint8_t *cfg = dev->config + dev->exp.sriov_cap;
56
19
    uint16_t i;
57
58
19
    trace_sriov_unregister_vfs(dev->name, PCI_SLOT(dev->devfn),
59
19
                               PCI_FUNC(dev->devfn));
60
171
    for (i = 0; i < pci_get_word(cfg + PCI_SRIOV_TOTAL_VF); i++) {
61
152
        pci_set_enabled(dev->exp.sriov_pf.vf[i], false);
62
152
    }
63
64
19
    pci_set_word(dev->wmask + dev->exp.sriov_cap + PCI_SRIOV_NUM_VF, 0xffff);
65
19
}
66
67
static void consume_config(PCIDevice *dev)
68
0
{
69
0
    uint8_t *cfg = dev->config + dev->exp.sriov_cap;
70
71
0
    if (pci_get_word(cfg + PCI_SRIOV_CTRL) & PCI_SRIOV_CTRL_VFE) {
72
0
        register_vfs(dev);
73
0
    } else {
74
0
        uint8_t *wmask = dev->wmask + dev->exp.sriov_cap;
75
0
        uint16_t num_vfs = pci_get_word(cfg + PCI_SRIOV_NUM_VF);
76
0
        uint16_t wmask_val = PCI_SRIOV_CTRL_MSE | PCI_SRIOV_CTRL_ARI;
77
78
0
        unregister_vfs(dev);
79
80
0
        if (num_vfs <= pci_get_word(cfg + PCI_SRIOV_TOTAL_VF)) {
81
0
            wmask_val |= PCI_SRIOV_CTRL_VFE;
82
0
        }
83
84
0
        pci_set_word(wmask + PCI_SRIOV_CTRL, wmask_val);
85
0
    }
86
0
}
87
88
static bool pcie_sriov_pf_init_common(PCIDevice *dev, uint16_t offset,
89
                                      uint16_t vf_dev_id, uint16_t init_vfs,
90
                                      uint16_t total_vfs, uint16_t vf_offset,
91
                                      uint16_t vf_stride, Error **errp)
92
2
{
93
2
    int32_t devfn = dev->devfn + vf_offset;
94
2
    uint8_t *cfg = dev->config + offset;
95
2
    uint8_t *wmask;
96
97
2
    if (!pci_is_express(dev)) {
98
0
        error_setg(errp, "PCI Express is required for SR-IOV PF");
99
0
        return false;
100
0
    }
101
102
2
    if (pci_is_vf(dev)) {
103
0
        error_setg(errp, "a device cannot be a SR-IOV PF and a VF at the same time");
104
0
        return false;
105
0
    }
106
107
2
    if (total_vfs &&
108
2
        (uint32_t)devfn + (uint32_t)(total_vfs - 1) * vf_stride >= PCI_DEVFN_MAX) {
109
0
        error_setg(errp, "VF addr overflows");
110
0
        return false;
111
0
    }
112
113
2
    pcie_add_capability(dev, PCI_EXT_CAP_ID_SRIOV, 1,
114
2
                        offset, PCI_EXT_CAP_SRIOV_SIZEOF);
115
2
    dev->exp.sriov_cap = offset;
116
2
    dev->exp.sriov_pf.vf = NULL;
117
118
2
    pci_set_word(cfg + PCI_SRIOV_VF_OFFSET, vf_offset);
119
2
    pci_set_word(cfg + PCI_SRIOV_VF_STRIDE, vf_stride);
120
121
    /*
122
     * Mandatory page sizes to support.
123
     * Device implementations can call pcie_sriov_pf_add_sup_pgsize()
124
     * to set more bits:
125
     */
126
2
    pci_set_word(cfg + PCI_SRIOV_SUP_PGSIZE, SRIOV_SUP_PGSIZE_MINREQ);
127
128
    /*
129
     * Default is to use 4K pages, software can modify it
130
     * to any of the supported bits
131
     */
132
2
    pci_set_word(cfg + PCI_SRIOV_SYS_PGSIZE, 0x1);
133
134
    /* Set up device ID and initial/total number of VFs available */
135
2
    pci_set_word(cfg + PCI_SRIOV_VF_DID, vf_dev_id);
136
2
    pci_set_word(cfg + PCI_SRIOV_INITIAL_VF, init_vfs);
137
2
    pci_set_word(cfg + PCI_SRIOV_TOTAL_VF, total_vfs);
138
2
    pci_set_word(cfg + PCI_SRIOV_NUM_VF, 0);
139
140
    /* Write enable control bits */
141
2
    wmask = dev->wmask + offset;
142
2
    pci_set_word(wmask + PCI_SRIOV_CTRL,
143
2
                 PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE | PCI_SRIOV_CTRL_ARI);
144
2
    pci_set_word(wmask + PCI_SRIOV_NUM_VF, 0xffff);
145
2
    pci_set_word(wmask + PCI_SRIOV_SYS_PGSIZE, 0x553);
146
147
2
    qdev_prop_set_bit(&dev->qdev, "multifunction", true);
148
149
2
    return true;
150
2
}
151
152
bool pcie_sriov_pf_init(PCIDevice *dev, uint16_t offset,
153
                        const char *vfname, uint16_t vf_dev_id,
154
                        uint16_t init_vfs, uint16_t total_vfs,
155
                        uint16_t vf_offset, uint16_t vf_stride,
156
                        Error **errp)
157
2
{
158
2
    BusState *bus = qdev_get_parent_bus(&dev->qdev);
159
2
    int32_t devfn = dev->devfn + vf_offset;
160
161
2
    if (pfs && g_hash_table_contains(pfs, dev->qdev.id)) {
162
0
        error_setg(errp, "attaching user-created SR-IOV VF unsupported");
163
0
        return false;
164
0
    }
165
166
2
    if (!pcie_sriov_pf_init_common(dev, offset, vf_dev_id, init_vfs,
167
2
                                   total_vfs, vf_offset, vf_stride, errp)) {
168
0
        return false;
169
0
    }
170
171
2
    dev->exp.sriov_pf.vf = g_new(PCIDevice *, total_vfs);
172
173
18
    for (uint16_t i = 0; i < total_vfs; i++) {
174
16
        PCIDevice *vf = pci_new(devfn, vfname);
175
16
        vf->exp.sriov_vf.pf = dev;
176
16
        vf->exp.sriov_vf.vf_number = i;
177
178
16
        if (!qdev_realize(&vf->qdev, bus, errp)) {
179
0
            object_unparent(OBJECT(vf));
180
0
            object_unref(vf);
181
0
            unparent_vfs(dev, i);
182
0
            return false;
183
0
        }
184
185
        /* set vid/did according to sr/iov spec - they are not used */
186
16
        pci_config_set_vendor_id(vf->config, 0xffff);
187
16
        pci_config_set_device_id(vf->config, 0xffff);
188
189
16
        dev->exp.sriov_pf.vf[i] = vf;
190
16
        devfn += vf_stride;
191
16
    }
192
193
2
    return true;
194
2
}
195
196
void pcie_sriov_pf_exit(PCIDevice *dev)
197
0
{
198
0
    uint8_t *cfg;
199
200
0
    if (dev->exp.sriov_cap == 0) {
201
0
        return;
202
0
    }
203
0
    cfg = dev->config + dev->exp.sriov_cap;
204
205
0
    if (dev->exp.sriov_pf.vf_user_created) {
206
0
        uint16_t ven_id = pci_get_word(dev->config + PCI_VENDOR_ID);
207
0
        uint16_t total_vfs = pci_get_word(cfg + PCI_SRIOV_TOTAL_VF);
208
0
        uint16_t vf_dev_id = pci_get_word(cfg + PCI_SRIOV_VF_DID);
209
210
0
        unregister_vfs(dev);
211
212
0
        for (uint16_t i = 0; i < total_vfs; i++) {
213
0
            dev->exp.sriov_pf.vf[i]->exp.sriov_vf.pf = NULL;
214
215
0
            pci_config_set_vendor_id(dev->exp.sriov_pf.vf[i]->config, ven_id);
216
0
            pci_config_set_device_id(dev->exp.sriov_pf.vf[i]->config, vf_dev_id);
217
0
        }
218
0
    } else {
219
0
        unparent_vfs(dev, pci_get_word(cfg + PCI_SRIOV_TOTAL_VF));
220
0
    }
221
0
}
222
223
void pcie_sriov_pf_init_vf_bar(PCIDevice *dev, int region_num,
224
                               uint8_t type, dma_addr_t size)
225
4
{
226
4
    uint32_t addr;
227
4
    uint64_t wmask;
228
4
    uint16_t sriov_cap = dev->exp.sriov_cap;
229
230
4
    assert(sriov_cap > 0);
231
4
    assert(region_num >= 0);
232
4
    assert(region_num < PCI_SRIOV_NUM_BARS);
233
4
    assert(region_num != PCI_ROM_SLOT);
234
235
4
    wmask = ~(size - 1);
236
4
    addr = sriov_cap + PCI_SRIOV_BAR + region_num * 4;
237
238
4
    pci_set_long(dev->config + addr, type);
239
4
    if (!(type & PCI_BASE_ADDRESS_SPACE_IO) &&
240
4
        type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
241
4
        pci_set_quad(dev->wmask + addr, wmask);
242
4
        pci_set_quad(dev->cmask + addr, ~0ULL);
243
4
    } else {
244
0
        pci_set_long(dev->wmask + addr, wmask & 0xffffffff);
245
0
        pci_set_long(dev->cmask + addr, 0xffffffff);
246
0
    }
247
4
    dev->exp.sriov_pf.vf_bar_type[region_num] = type;
248
4
}
249
250
static gint compare_vf_devfns(gconstpointer a, gconstpointer b)
251
0
{
252
0
    return (*(PCIDevice **)a)->devfn - (*(PCIDevice **)b)->devfn;
253
0
}
254
255
int16_t pcie_sriov_pf_init_from_user_created_vfs(PCIDevice *dev,
256
                                                 uint16_t offset,
257
                                                 Error **errp)
258
0
{
259
0
    GPtrArray *pf;
260
0
    PCIDevice **vfs;
261
0
    BusState *bus = qdev_get_parent_bus(DEVICE(dev));
262
0
    uint16_t ven_id = pci_get_word(dev->config + PCI_VENDOR_ID);
263
0
    uint16_t size = PCI_EXT_CAP_SRIOV_SIZEOF;
264
0
    uint16_t vf_dev_id;
265
0
    uint16_t vf_offset;
266
0
    uint16_t vf_stride;
267
0
    uint16_t i;
268
269
0
    if (!pfs || !dev->qdev.id) {
270
0
        return 0;
271
0
    }
272
273
0
    pf = g_hash_table_lookup(pfs, dev->qdev.id);
274
0
    if (!pf) {
275
0
        return 0;
276
0
    }
277
278
0
    if (pf->len > UINT16_MAX) {
279
0
        error_setg(errp, "too many VFs");
280
0
        return -1;
281
0
    }
282
283
0
    g_ptr_array_sort(pf, compare_vf_devfns);
284
0
    vfs = (void *)pf->pdata;
285
286
0
    if (vfs[0]->devfn <= dev->devfn) {
287
0
        error_setg(errp, "a VF function number is less than the PF function number");
288
0
        return -1;
289
0
    }
290
291
0
    vf_dev_id = pci_get_word(vfs[0]->config + PCI_DEVICE_ID);
292
0
    vf_offset = vfs[0]->devfn - dev->devfn;
293
0
    vf_stride = pf->len < 2 ? 0 : vfs[1]->devfn - vfs[0]->devfn;
294
295
0
    for (i = 0; i < pf->len; i++) {
296
0
        if (bus != qdev_get_parent_bus(&vfs[i]->qdev)) {
297
0
            error_setg(errp, "SR-IOV VF parent bus mismatches with PF");
298
0
            return -1;
299
0
        }
300
301
0
        if (ven_id != pci_get_word(vfs[i]->config + PCI_VENDOR_ID)) {
302
0
            error_setg(errp, "SR-IOV VF vendor ID mismatches with PF");
303
0
            return -1;
304
0
        }
305
306
0
        if (vf_dev_id != pci_get_word(vfs[i]->config + PCI_DEVICE_ID)) {
307
0
            error_setg(errp, "inconsistent SR-IOV VF device IDs");
308
0
            return -1;
309
0
        }
310
311
0
        for (size_t j = 0; j < PCI_SRIOV_NUM_BARS; j++) {
312
0
            if (vfs[i]->io_regions[j].size != vfs[0]->io_regions[j].size ||
313
0
                vfs[i]->io_regions[j].type != vfs[0]->io_regions[j].type) {
314
0
                error_setg(errp, "inconsistent SR-IOV BARs");
315
0
                return -1;
316
0
            }
317
0
        }
318
319
0
        if (vfs[i]->devfn - vfs[0]->devfn != vf_stride * i) {
320
0
            error_setg(errp, "inconsistent SR-IOV stride");
321
0
            return -1;
322
0
        }
323
0
    }
324
325
0
    if (!pcie_sriov_pf_init_common(dev, offset, vf_dev_id, pf->len,
326
0
                                   pf->len, vf_offset, vf_stride, errp)) {
327
0
        return -1;
328
0
    }
329
330
0
    if (!pcie_find_capability(dev, PCI_EXT_CAP_ID_ARI)) {
331
0
        pcie_ari_init(dev, offset + size);
332
0
        size += PCI_ARI_SIZEOF;
333
0
    }
334
335
0
    for (i = 0; i < pf->len; i++) {
336
0
        vfs[i]->exp.sriov_vf.pf = dev;
337
0
        vfs[i]->exp.sriov_vf.vf_number = i;
338
339
        /* set vid/did according to sr/iov spec - they are not used */
340
0
        pci_config_set_vendor_id(vfs[i]->config, 0xffff);
341
0
        pci_config_set_device_id(vfs[i]->config, 0xffff);
342
0
    }
343
344
0
    dev->exp.sriov_pf.vf = vfs;
345
0
    dev->exp.sriov_pf.vf_user_created = true;
346
347
0
    for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
348
0
        PCIIORegion *region = &vfs[0]->io_regions[i];
349
350
0
        if (region->size) {
351
0
            pcie_sriov_pf_init_vf_bar(dev, i, region->type, region->size);
352
0
        }
353
0
    }
354
355
0
    return size;
356
0
}
357
358
bool pcie_sriov_register_device(PCIDevice *dev, Error **errp)
359
324
{
360
324
    if (dev->sriov_pf) {
361
0
        PCIDevice *pci_pf;
362
0
        GPtrArray *pf;
363
364
0
        if (!PCI_DEVICE_GET_CLASS(dev)->sriov_vf_user_creatable) {
365
0
            error_setg(errp, "user cannot create SR-IOV VF with this device type");
366
0
            return false;
367
0
        }
368
369
0
        if (!pci_is_express(dev)) {
370
0
            error_setg(errp, "PCI Express is required for SR-IOV VF");
371
0
            return false;
372
0
        }
373
374
0
        if (!pci_qdev_find_device(dev->sriov_pf, &pci_pf)) {
375
0
            error_setg(errp, "PCI device specified as SR-IOV PF already exists");
376
0
            return false;
377
0
        }
378
379
0
        if (!pfs) {
380
0
            pfs = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
381
0
        }
382
383
0
        pf = g_hash_table_lookup(pfs, dev->sriov_pf);
384
0
        if (!pf) {
385
0
            pf = g_ptr_array_new();
386
0
            g_hash_table_insert(pfs, g_strdup(dev->sriov_pf), pf);
387
0
        }
388
389
0
        g_ptr_array_add(pf, dev);
390
0
    }
391
392
324
    return true;
393
324
}
394
395
void pcie_sriov_unregister_device(PCIDevice *dev)
396
0
{
397
0
    if (dev->sriov_pf && pfs) {
398
0
        GPtrArray *pf = g_hash_table_lookup(pfs, dev->sriov_pf);
399
400
0
        if (pf) {
401
0
            g_ptr_array_remove_fast(pf, dev);
402
403
0
            if (!pf->len) {
404
0
                g_hash_table_remove(pfs, dev->sriov_pf);
405
0
                g_ptr_array_free(pf, FALSE);
406
0
            }
407
0
        }
408
0
    }
409
0
}
410
411
void pcie_sriov_config_write(PCIDevice *dev, uint32_t address,
412
                             uint32_t val, int len)
413
2.02M
{
414
2.02M
    uint32_t off;
415
2.02M
    uint16_t sriov_cap = dev->exp.sriov_cap;
416
417
2.02M
    if (!sriov_cap || address < sriov_cap) {
418
2.02M
        return;
419
2.02M
    }
420
0
    off = address - sriov_cap;
421
0
    if (off >= PCI_EXT_CAP_SRIOV_SIZEOF) {
422
0
        return;
423
0
    }
424
425
0
    trace_sriov_config_write(dev->name, PCI_SLOT(dev->devfn),
426
0
                             PCI_FUNC(dev->devfn), off, val, len);
427
428
0
    consume_config(dev);
429
0
}
430
431
void pcie_sriov_pf_post_load(PCIDevice *dev)
432
0
{
433
0
    if (dev->exp.sriov_cap) {
434
0
        consume_config(dev);
435
0
    }
436
0
}
437
438
439
/* Reset SR/IOV */
440
void pcie_sriov_pf_reset(PCIDevice *dev)
441
485k
{
442
485k
    uint16_t sriov_cap = dev->exp.sriov_cap;
443
485k
    if (!sriov_cap) {
444
485k
        return;
445
485k
    }
446
447
19
    pci_set_word(dev->config + sriov_cap + PCI_SRIOV_CTRL, 0);
448
19
    unregister_vfs(dev);
449
450
19
    pci_set_word(dev->config + sriov_cap + PCI_SRIOV_NUM_VF, 0);
451
19
    pci_set_word(dev->wmask + sriov_cap + PCI_SRIOV_CTRL,
452
19
                 PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE | PCI_SRIOV_CTRL_ARI);
453
454
    /*
455
     * Default is to use 4K pages, software can modify it
456
     * to any of the supported bits
457
     */
458
19
    pci_set_word(dev->config + sriov_cap + PCI_SRIOV_SYS_PGSIZE, 0x1);
459
460
133
    for (uint16_t i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
461
114
        pci_set_long(dev->config + sriov_cap + PCI_SRIOV_BAR + i * 4,
462
114
                     dev->exp.sriov_pf.vf_bar_type[i]);
463
114
    }
464
19
}
465
466
/* Add optional supported page sizes to the mask of supported page sizes */
467
void pcie_sriov_pf_add_sup_pgsize(PCIDevice *dev, uint16_t opt_sup_pgsize)
468
0
{
469
0
    uint8_t *cfg = dev->config + dev->exp.sriov_cap;
470
0
    uint8_t *wmask = dev->wmask + dev->exp.sriov_cap;
471
472
0
    uint16_t sup_pgsize = pci_get_word(cfg + PCI_SRIOV_SUP_PGSIZE);
473
474
0
    sup_pgsize |= opt_sup_pgsize;
475
476
    /*
477
     * Make sure the new bits are set, and that system page size
478
     * also can be set to any of the new values according to spec:
479
     */
480
0
    pci_set_word(cfg + PCI_SRIOV_SUP_PGSIZE, sup_pgsize);
481
0
    pci_set_word(wmask + PCI_SRIOV_SYS_PGSIZE, sup_pgsize);
482
0
}
483
484
485
uint16_t pcie_sriov_vf_number(PCIDevice *dev)
486
152
{
487
152
    assert(dev->exp.sriov_vf.pf);
488
152
    return dev->exp.sriov_vf.vf_number;
489
152
}
490
491
PCIDevice *pcie_sriov_get_pf(PCIDevice *dev)
492
168
{
493
168
    return dev->exp.sriov_vf.pf;
494
168
}
495
496
PCIDevice *pcie_sriov_get_vf_at_index(PCIDevice *dev, int n)
497
1
{
498
1
    assert(!pci_is_vf(dev));
499
1
    if (n < pcie_sriov_num_vfs(dev)) {
500
0
        return dev->exp.sriov_pf.vf[n];
501
0
    }
502
1
    return NULL;
503
1
}
504
505
uint16_t pcie_sriov_num_vfs(PCIDevice *dev)
506
1
{
507
1
    uint16_t sriov_cap = dev->exp.sriov_cap;
508
1
    uint8_t *cfg = dev->config + sriov_cap;
509
510
1
    return sriov_cap &&
511
1
           (pci_get_word(cfg + PCI_SRIOV_CTRL) & PCI_SRIOV_CTRL_VFE) ?
512
1
           pci_get_word(cfg + PCI_SRIOV_NUM_VF) : 0;
513
1
}