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 	xe_pm_runtime_get(xe);
36 	ret = sysfs_emit(buf, "%d\n", xe->d3cold.vram_threshold);
37 	xe_pm_runtime_put(xe);
38 
39 	return ret;
40 }
41 
42 static ssize_t
vram_d3cold_threshold_store(struct device * dev,struct device_attribute * attr,const char * buff,size_t count)43 vram_d3cold_threshold_store(struct device *dev, struct device_attribute *attr,
44 			    const char *buff, size_t count)
45 {
46 	struct pci_dev *pdev = to_pci_dev(dev);
47 	struct xe_device *xe = pdev_to_xe_device(pdev);
48 	u32 vram_d3cold_threshold;
49 	int ret;
50 
51 	ret = kstrtou32(buff, 0, &vram_d3cold_threshold);
52 	if (ret)
53 		return ret;
54 
55 	drm_dbg(&xe->drm, "vram_d3cold_threshold: %u\n", vram_d3cold_threshold);
56 
57 	xe_pm_runtime_get(xe);
58 	ret = xe_pm_set_vram_threshold(xe, vram_d3cold_threshold);
59 	xe_pm_runtime_put(xe);
60 
61 	return ret ?: count;
62 }
63 
64 static DEVICE_ATTR_RW(vram_d3cold_threshold);
65 
xe_device_sysfs_fini(void * arg)66 static void xe_device_sysfs_fini(void *arg)
67 {
68 	struct xe_device *xe = arg;
69 
70 	sysfs_remove_file(&xe->drm.dev->kobj, &dev_attr_vram_d3cold_threshold.attr);
71 }
72 
xe_device_sysfs_init(struct xe_device * xe)73 int xe_device_sysfs_init(struct xe_device *xe)
74 {
75 	struct device *dev = xe->drm.dev;
76 	int ret;
77 
78 	ret = sysfs_create_file(&dev->kobj, &dev_attr_vram_d3cold_threshold.attr);
79 	if (ret)
80 		return ret;
81 
82 	return devm_add_action_or_reset(dev, xe_device_sysfs_fini, xe);
83 }
84