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
27static ssize_t
28vram_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	ret = sysfs_emit(buf, "%d\n", xe->d3cold.vram_threshold);
39
40	return ret;
41}
42
43static ssize_t
44vram_d3cold_threshold_store(struct device *dev, struct device_attribute *attr,
45			    const char *buff, size_t count)
46{
47	struct pci_dev *pdev = to_pci_dev(dev);
48	struct xe_device *xe = pdev_to_xe_device(pdev);
49	u32 vram_d3cold_threshold;
50	int ret;
51
52	if (!xe)
53		return -EINVAL;
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	ret = xe_pm_set_vram_threshold(xe, vram_d3cold_threshold);
62
63	return ret ?: count;
64}
65
66static DEVICE_ATTR_RW(vram_d3cold_threshold);
67
68static void xe_device_sysfs_fini(struct drm_device *drm, void *arg)
69{
70	struct xe_device *xe = arg;
71
72	sysfs_remove_file(&xe->drm.dev->kobj, &dev_attr_vram_d3cold_threshold.attr);
73}
74
75void xe_device_sysfs_init(struct xe_device *xe)
76{
77	struct device *dev = xe->drm.dev;
78	int ret;
79
80	ret = sysfs_create_file(&dev->kobj, &dev_attr_vram_d3cold_threshold.attr);
81	if (ret) {
82		drm_warn(&xe->drm, "Failed to create sysfs file\n");
83		return;
84	}
85
86	ret = drmm_add_action_or_reset(&xe->drm, xe_device_sysfs_fini, xe);
87	if (ret)
88		drm_warn(&xe->drm, "Failed to add sysfs fini drm action\n");
89}
90