1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2023 Intel Corporation
4  */
5 
6 #include <linux/kobject.h>
7 #include <linux/pci.h>
8 #include <linux/sysfs.h>
9 
10 #include <drm/drm_managed.h>
11 
12 #include "xe_device.h"
13 #include "xe_device_sysfs.h"
14 #include "xe_pm.h"
15 
16 /**
17  * DOC: Xe device sysfs
18  * Xe driver requires exposing certain tunable knobs controlled by user space for
19  * each graphics device. Considering this, we need to add sysfs attributes at device
20  * level granularity.
21  * These sysfs attributes will be available under pci device kobj directory.
22  *
23  * vram_d3cold_threshold - Report/change vram used threshold(in MB) below
24  * which vram save/restore is permissible during runtime D3cold entry/exit.
25  */
26 
27 static ssize_t
vram_d3cold_threshold_show(struct device * dev,struct device_attribute * attr,char * buf)28 vram_d3cold_threshold_show(struct device *dev,
29 			   struct device_attribute *attr, char *buf)
30 {
31 	struct pci_dev *pdev = to_pci_dev(dev);
32 	struct xe_device *xe = pdev_to_xe_device(pdev);
33 	int ret;
34 
35 	if (!xe)
36 		return -EINVAL;
37 
38 	xe_pm_runtime_get(xe);
39 	ret = sysfs_emit(buf, "%d\n", xe->d3cold.vram_threshold);
40 	xe_pm_runtime_put(xe);
41 
42 	return ret;
43 }
44 
45 static ssize_t
vram_d3cold_threshold_store(struct device * dev,struct device_attribute * attr,const char * buff,size_t count)46 vram_d3cold_threshold_store(struct device *dev, struct device_attribute *attr,
47 			    const char *buff, size_t count)
48 {
49 	struct pci_dev *pdev = to_pci_dev(dev);
50 	struct xe_device *xe = pdev_to_xe_device(pdev);
51 	u32 vram_d3cold_threshold;
52 	int ret;
53 
54 	if (!xe)
55 		return -EINVAL;
56 
57 	ret = kstrtou32(buff, 0, &vram_d3cold_threshold);
58 	if (ret)
59 		return ret;
60 
61 	drm_dbg(&xe->drm, "vram_d3cold_threshold: %u\n", vram_d3cold_threshold);
62 
63 	xe_pm_runtime_get(xe);
64 	ret = xe_pm_set_vram_threshold(xe, vram_d3cold_threshold);
65 	xe_pm_runtime_put(xe);
66 
67 	return ret ?: count;
68 }
69 
70 static DEVICE_ATTR_RW(vram_d3cold_threshold);
71 
xe_device_sysfs_fini(void * arg)72 static void xe_device_sysfs_fini(void *arg)
73 {
74 	struct xe_device *xe = arg;
75 
76 	sysfs_remove_file(&xe->drm.dev->kobj, &dev_attr_vram_d3cold_threshold.attr);
77 }
78 
xe_device_sysfs_init(struct xe_device * xe)79 int xe_device_sysfs_init(struct xe_device *xe)
80 {
81 	struct device *dev = xe->drm.dev;
82 	int ret;
83 
84 	ret = sysfs_create_file(&dev->kobj, &dev_attr_vram_d3cold_threshold.attr);
85 	if (ret)
86 		return ret;
87 
88 	return devm_add_action_or_reset(dev, xe_device_sysfs_fini, xe);
89 }
90