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
|
// SPDX-License-Identifier: MIT
/*
* Copyright © 2023-2024 Intel Corporation
*/
#include "xe_assert.h"
#include "xe_device.h"
#include "xe_gt_sriov_pf_config.h"
#include "xe_gt_sriov_pf_control.h"
#include "xe_pci_sriov.h"
#include "xe_pm.h"
#include "xe_sriov.h"
#include "xe_sriov_pf_helpers.h"
#include "xe_sriov_printk.h"
static int pf_needs_provisioning(struct xe_gt *gt, unsigned int num_vfs)
{
unsigned int n;
for (n = 1; n <= num_vfs; n++)
if (!xe_gt_sriov_pf_config_is_empty(gt, n))
return false;
return true;
}
static int pf_provision_vfs(struct xe_device *xe, unsigned int num_vfs)
{
struct xe_gt *gt;
unsigned int id;
int result = 0, err;
for_each_gt(gt, xe, id) {
if (!pf_needs_provisioning(gt, num_vfs))
continue;
err = xe_gt_sriov_pf_config_set_fair(gt, VFID(1), num_vfs);
result = result ?: err;
}
return result;
}
static void pf_unprovision_vfs(struct xe_device *xe, unsigned int num_vfs)
{
struct xe_gt *gt;
unsigned int id;
unsigned int n;
for_each_gt(gt, xe, id)
for (n = 1; n <= num_vfs; n++)
xe_gt_sriov_pf_config_release(gt, n, true);
}
static void pf_reset_vfs(struct xe_device *xe, unsigned int num_vfs)
{
struct xe_gt *gt;
unsigned int id;
unsigned int n;
for_each_gt(gt, xe, id)
for (n = 1; n <= num_vfs; n++)
xe_gt_sriov_pf_control_trigger_flr(gt, n);
}
static int pf_enable_vfs(struct xe_device *xe, int num_vfs)
{
struct pci_dev *pdev = to_pci_dev(xe->drm.dev);
int total_vfs = xe_sriov_pf_get_totalvfs(xe);
int err;
xe_assert(xe, IS_SRIOV_PF(xe));
xe_assert(xe, num_vfs > 0);
xe_assert(xe, num_vfs <= total_vfs);
xe_sriov_dbg(xe, "enabling %u VF%s\n", num_vfs, str_plural(num_vfs));
/*
* We must hold additional reference to the runtime PM to keep PF in D0
* during VFs lifetime, as our VFs do not implement the PM capability.
*
* With PF being in D0 state, all VFs will also behave as in D0 state.
* This will also keep GuC alive with all VFs' configurations.
*
* We will release this additional PM reference in pf_disable_vfs().
*/
xe_pm_runtime_get_noresume(xe);
err = pf_provision_vfs(xe, num_vfs);
if (err < 0)
goto failed;
err = pci_enable_sriov(pdev, num_vfs);
if (err < 0)
goto failed;
xe_sriov_info(xe, "Enabled %u of %u VF%s\n",
num_vfs, total_vfs, str_plural(total_vfs));
return num_vfs;
failed:
pf_unprovision_vfs(xe, num_vfs);
xe_pm_runtime_put(xe);
xe_sriov_notice(xe, "Failed to enable %u VF%s (%pe)\n",
num_vfs, str_plural(num_vfs), ERR_PTR(err));
return err;
}
static int pf_disable_vfs(struct xe_device *xe)
{
struct device *dev = xe->drm.dev;
struct pci_dev *pdev = to_pci_dev(dev);
u16 num_vfs = pci_num_vf(pdev);
xe_assert(xe, IS_SRIOV_PF(xe));
xe_sriov_dbg(xe, "disabling %u VF%s\n", num_vfs, str_plural(num_vfs));
if (!num_vfs)
return 0;
pci_disable_sriov(pdev);
pf_reset_vfs(xe, num_vfs);
pf_unprovision_vfs(xe, num_vfs);
/* not needed anymore - see pf_enable_vfs() */
xe_pm_runtime_put(xe);
xe_sriov_info(xe, "Disabled %u VF%s\n", num_vfs, str_plural(num_vfs));
return 0;
}
/**
* xe_pci_sriov_configure - Configure SR-IOV (enable/disable VFs).
* @pdev: the &pci_dev
* @num_vfs: number of VFs to enable or zero to disable all VFs
*
* This is the Xe implementation of struct pci_driver.sriov_configure callback.
*
* This callback will be called by the PCI subsystem to enable or disable SR-IOV
* Virtual Functions (VFs) as requested by the used via the PCI sysfs interface.
*
* Return: number of configured VFs or a negative error code on failure.
*/
int xe_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
{
struct xe_device *xe = pdev_to_xe_device(pdev);
int ret;
if (!IS_SRIOV_PF(xe))
return -ENODEV;
if (num_vfs < 0)
return -EINVAL;
if (num_vfs > xe_sriov_pf_get_totalvfs(xe))
return -ERANGE;
if (num_vfs && pci_num_vf(pdev))
return -EBUSY;
xe_pm_runtime_get(xe);
if (num_vfs > 0)
ret = pf_enable_vfs(xe, num_vfs);
else
ret = pf_disable_vfs(xe);
xe_pm_runtime_put(xe);
return ret;
}
|