xref: /linux/drivers/gpu/drm/xe/xe_device_sysfs.c (revision cb8fef4b07f71da01843457b66e579ad248a625e)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2023 Intel Corporation
4  */
5 
6 #include <linux/device.h>
7 #include <linux/kobject.h>
8 #include <linux/pci.h>
9 #include <linux/sysfs.h>
10 
11 #include "regs/xe_regs.h"
12 #include "xe_device.h"
13 #include "xe_device_sysfs.h"
14 #include "xe_mmio.h"
15 #include "xe_pcode_api.h"
16 #include "xe_pcode.h"
17 #include "xe_pm.h"
18 
19 /**
20  * DOC: Xe device sysfs
21  * Xe driver requires exposing certain tunable knobs controlled by user space for
22  * each graphics device. Considering this, we need to add sysfs attributes at device
23  * level granularity.
24  * These sysfs attributes will be available under pci device kobj directory.
25  *
26  * vram_d3cold_threshold - Report/change vram used threshold(in MB) below
27  * which vram save/restore is permissible during runtime D3cold entry/exit.
28  *
29  * lb_fan_control_version - Fan control version provisioned by late binding.
30  * Exposed only if supported by the device.
31  *
32  * lb_voltage_regulator_version - Voltage regulator version provisioned by late
33  * binding. Exposed only if supported by the device.
34  */
35 
36 static ssize_t
37 vram_d3cold_threshold_show(struct device *dev,
38 			   struct device_attribute *attr, char *buf)
39 {
40 	struct pci_dev *pdev = to_pci_dev(dev);
41 	struct xe_device *xe = pdev_to_xe_device(pdev);
42 
43 	return sysfs_emit(buf, "%d\n", xe->d3cold.vram_threshold);
44 }
45 
46 static ssize_t
47 vram_d3cold_threshold_store(struct device *dev, struct device_attribute *attr,
48 			    const char *buff, size_t count)
49 {
50 	struct pci_dev *pdev = to_pci_dev(dev);
51 	struct xe_device *xe = pdev_to_xe_device(pdev);
52 	u32 vram_d3cold_threshold;
53 	int ret;
54 
55 	ret = kstrtou32(buff, 0, &vram_d3cold_threshold);
56 	if (ret)
57 		return ret;
58 
59 	drm_dbg(&xe->drm, "vram_d3cold_threshold: %u\n", vram_d3cold_threshold);
60 
61 	guard(xe_pm_runtime)(xe);
62 	ret = xe_pm_set_vram_threshold(xe, vram_d3cold_threshold);
63 
64 	return ret ?: count;
65 }
66 
67 static DEVICE_ATTR_RW(vram_d3cold_threshold);
68 
69 static struct attribute *vram_attrs[] = {
70 	&dev_attr_vram_d3cold_threshold.attr,
71 	NULL
72 };
73 
74 static const struct attribute_group vram_attr_group = {
75 	.attrs = vram_attrs,
76 };
77 
78 static ssize_t
79 lb_fan_control_version_show(struct device *dev, struct device_attribute *attr, char *buf)
80 {
81 	struct xe_device *xe = pdev_to_xe_device(to_pci_dev(dev));
82 	struct xe_tile *root = xe_device_get_root_tile(xe);
83 	u32 cap = 0, ver_low = FAN_TABLE, ver_high = FAN_TABLE;
84 	u16 major = 0, minor = 0, hotfix = 0, build = 0;
85 	int ret;
86 
87 	guard(xe_pm_runtime)(xe);
88 
89 	ret = xe_pcode_read(root, PCODE_MBOX(PCODE_LATE_BINDING, GET_CAPABILITY_STATUS, 0),
90 			    &cap, NULL);
91 	if (ret)
92 		return ret;
93 
94 	if (REG_FIELD_GET(V1_FAN_PROVISIONED, cap)) {
95 		ret = xe_pcode_read(root, PCODE_MBOX(PCODE_LATE_BINDING, GET_VERSION_LOW, 0),
96 				    &ver_low, NULL);
97 		if (ret)
98 			return ret;
99 
100 		ret = xe_pcode_read(root, PCODE_MBOX(PCODE_LATE_BINDING, GET_VERSION_HIGH, 0),
101 				    &ver_high, NULL);
102 		if (ret)
103 			return ret;
104 
105 		major = REG_FIELD_GET(MAJOR_VERSION_MASK, ver_low);
106 		minor = REG_FIELD_GET(MINOR_VERSION_MASK, ver_low);
107 		hotfix = REG_FIELD_GET(HOTFIX_VERSION_MASK, ver_high);
108 		build = REG_FIELD_GET(BUILD_VERSION_MASK, ver_high);
109 	}
110 
111 	return sysfs_emit(buf, "%u.%u.%u.%u\n", major, minor, hotfix, build);
112 }
113 static DEVICE_ATTR_ADMIN_RO(lb_fan_control_version);
114 
115 static ssize_t
116 lb_voltage_regulator_version_show(struct device *dev, struct device_attribute *attr, char *buf)
117 {
118 	struct xe_device *xe = pdev_to_xe_device(to_pci_dev(dev));
119 	struct xe_tile *root = xe_device_get_root_tile(xe);
120 	u32 cap = 0, ver_low = VR_CONFIG, ver_high = VR_CONFIG;
121 	u16 major = 0, minor = 0, hotfix = 0, build = 0;
122 	int ret;
123 
124 	guard(xe_pm_runtime)(xe);
125 
126 	ret = xe_pcode_read(root, PCODE_MBOX(PCODE_LATE_BINDING, GET_CAPABILITY_STATUS, 0),
127 			    &cap, NULL);
128 	if (ret)
129 		return ret;
130 
131 	if (REG_FIELD_GET(VR_PARAMS_PROVISIONED, cap)) {
132 		ret = xe_pcode_read(root, PCODE_MBOX(PCODE_LATE_BINDING, GET_VERSION_LOW, 0),
133 				    &ver_low, NULL);
134 		if (ret)
135 			return ret;
136 
137 		ret = xe_pcode_read(root, PCODE_MBOX(PCODE_LATE_BINDING, GET_VERSION_HIGH, 0),
138 				    &ver_high, NULL);
139 		if (ret)
140 			return ret;
141 
142 		major = REG_FIELD_GET(MAJOR_VERSION_MASK, ver_low);
143 		minor = REG_FIELD_GET(MINOR_VERSION_MASK, ver_low);
144 		hotfix = REG_FIELD_GET(HOTFIX_VERSION_MASK, ver_high);
145 		build = REG_FIELD_GET(BUILD_VERSION_MASK, ver_high);
146 	}
147 
148 	return sysfs_emit(buf, "%u.%u.%u.%u\n", major, minor, hotfix, build);
149 }
150 static DEVICE_ATTR_ADMIN_RO(lb_voltage_regulator_version);
151 
152 static struct attribute *late_bind_attrs[] = {
153 	&dev_attr_lb_fan_control_version.attr,
154 	&dev_attr_lb_voltage_regulator_version.attr,
155 	NULL
156 };
157 
158 static umode_t late_bind_attr_is_visible(struct kobject *kobj,
159 					 struct attribute *attr, int n)
160 {
161 	struct device *dev = kobj_to_dev(kobj);
162 	struct xe_device *xe = pdev_to_xe_device(to_pci_dev(dev));
163 	struct xe_tile *root = xe_device_get_root_tile(xe);
164 	u32 cap = 0;
165 	int ret;
166 
167 	ret = xe_pcode_read(root, PCODE_MBOX(PCODE_LATE_BINDING, GET_CAPABILITY_STATUS, 0),
168 			    &cap, NULL);
169 	if (ret)
170 		return 0;
171 
172 	if (attr == &dev_attr_lb_fan_control_version.attr &&
173 	    REG_FIELD_GET(V1_FAN_SUPPORTED, cap))
174 		return attr->mode;
175 	if (attr == &dev_attr_lb_voltage_regulator_version.attr &&
176 	    REG_FIELD_GET(VR_PARAMS_SUPPORTED, cap))
177 		return attr->mode;
178 
179 	return 0;
180 }
181 
182 static const struct attribute_group late_bind_attr_group = {
183 	.attrs = late_bind_attrs,
184 	.is_visible = late_bind_attr_is_visible,
185 };
186 
187 /**
188  * DOC: PCIe Gen5 Limitations
189  *
190  * Default link speed of discrete GPUs is determined by configuration parameters
191  * stored in their flash memory, which are subject to override through user
192  * initiated firmware updates. It has been observed that devices configured with
193  * PCIe Gen5 as their default link speed can come across link quality issues due
194  * to host or motherboard limitations and may have to auto-downgrade their link
195  * to PCIe Gen4 speed when faced with unstable link at Gen5, which makes
196  * firmware updates rather risky on such setups. It is required to ensure that
197  * the device is capable of auto-downgrading its link to PCIe Gen4 speed before
198  * pushing the firmware image with PCIe Gen5 as default configuration. This can
199  * be done by reading ``auto_link_downgrade_capable`` sysfs entry, which will
200  * denote if the device is capable of auto-downgrading its link to PCIe Gen4
201  * speed with boolean output value of ``0`` or ``1``, meaning `incapable` or
202  * `capable` respectively.
203  *
204  * .. code-block:: shell
205  *
206  *    $ cat /sys/bus/pci/devices/<bdf>/auto_link_downgrade_capable
207  *
208  * Pushing the firmware image with PCIe Gen5 as default configuration on a auto
209  * link downgrade incapable device and facing link instability due to host or
210  * motherboard limitations can result in driver failing to bind to the device,
211  * making further firmware updates impossible with RMA being the only last
212  * resort.
213  *
214  * Link downgrade status of auto link downgrade capable devices is available
215  * through ``auto_link_downgrade_status`` sysfs entry with boolean output value
216  * of ``0`` or ``1``, where ``0`` means no auto-downgrading was required during
217  * link training (which is the optimal scenario) and ``1`` means the device has
218  * auto-downgraded its link to PCIe Gen4 speed due to unstable Gen5 link.
219  *
220  * .. code-block:: shell
221  *
222  *    $ cat /sys/bus/pci/devices/<bdf>/auto_link_downgrade_status
223  */
224 
225 static ssize_t
226 auto_link_downgrade_capable_show(struct device *dev, struct device_attribute *attr, char *buf)
227 {
228 	struct pci_dev *pdev = to_pci_dev(dev);
229 	struct xe_device *xe = pdev_to_xe_device(pdev);
230 	u32 cap, val;
231 
232 	guard(xe_pm_runtime)(xe);
233 	val = xe_mmio_read32(xe_root_tile_mmio(xe), BMG_PCIE_CAP);
234 
235 	cap = REG_FIELD_GET(LINK_DOWNGRADE, val);
236 	return sysfs_emit(buf, "%u\n", cap == DOWNGRADE_CAPABLE);
237 }
238 static DEVICE_ATTR_ADMIN_RO(auto_link_downgrade_capable);
239 
240 static ssize_t
241 auto_link_downgrade_status_show(struct device *dev, struct device_attribute *attr, char *buf)
242 {
243 	struct pci_dev *pdev = to_pci_dev(dev);
244 	struct xe_device *xe = pdev_to_xe_device(pdev);
245 	/* default the auto_link_downgrade status to 0 */
246 	u32 val = 0;
247 	int ret;
248 
249 	guard(xe_pm_runtime)(xe);
250 	ret = xe_pcode_read(xe_device_get_root_tile(xe),
251 			    PCODE_MBOX(DGFX_PCODE_STATUS, DGFX_GET_INIT_STATUS, 0),
252 			    &val, NULL);
253 
254 	return ret ?: sysfs_emit(buf, "%u\n", REG_FIELD_GET(DGFX_LINK_DOWNGRADE_STATUS, val));
255 }
256 static DEVICE_ATTR_ADMIN_RO(auto_link_downgrade_status);
257 
258 static struct attribute *auto_link_downgrade_attrs[] = {
259 	&dev_attr_auto_link_downgrade_capable.attr,
260 	&dev_attr_auto_link_downgrade_status.attr,
261 	NULL
262 };
263 
264 static const struct attribute_group auto_link_downgrade_attr_group = {
265 	.attrs = auto_link_downgrade_attrs,
266 };
267 
268 /**
269  * DOC: Device Unique ID
270  *
271  * On supported platforms, Xe driver exposes a unique 64-bit GPU SOC
272  * device identifier through the 'device_uid' sysfs entry.
273  *
274  * See Documentation/ABI/testing/sysfs-driver-intel-xe-gpu for the ABI
275  * specification.
276  */
277 
278 static ssize_t
279 device_uid_show(struct device *dev, struct device_attribute *attr, char *buf)
280 {
281 	struct pci_dev *pdev = to_pci_dev(dev);
282 	struct xe_device *xe = pdev_to_xe_device(pdev);
283 
284 	return sysfs_emit(buf, "0x%016llx\n", xe->device_uid);
285 }
286 static DEVICE_ATTR_ADMIN_RO(device_uid);
287 
288 static struct attribute *device_uid_attrs[] = {
289 	&dev_attr_device_uid.attr,
290 	NULL
291 };
292 
293 static const struct attribute_group device_uid_attr_group = {
294 	.attrs = device_uid_attrs,
295 };
296 
297 int xe_device_sysfs_init(struct xe_device *xe)
298 {
299 	struct device *dev = xe->drm.dev;
300 	int ret;
301 
302 	if (xe->d3cold.capable) {
303 		ret = devm_device_add_group(dev, &vram_attr_group);
304 		if (ret)
305 			return ret;
306 	}
307 
308 	if (xe->info.platform == XE_BATTLEMAGE && !IS_SRIOV_VF(xe)) {
309 		ret = devm_device_add_group(dev, &auto_link_downgrade_attr_group);
310 		if (ret)
311 			return ret;
312 
313 		ret = devm_device_add_group(dev, &late_bind_attr_group);
314 		if (ret)
315 			return ret;
316 	}
317 
318 	if (xe->info.has_device_uid) {
319 		ret = devm_device_add_group(dev, &device_uid_attr_group);
320 		if (ret)
321 			return ret;
322 	}
323 
324 	return 0;
325 }
326