1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * processor thermal device for Workload type hints
4 * update from user space
5 *
6 * Copyright (c) 2020-2023, Intel Corporation.
7 */
8
9 #include <linux/pci.h>
10 #include <linux/sysfs.h>
11 #include "processor_thermal_device.h"
12
13 /* List of workload types */
14 static const char * const workload_types[] = {
15 "none",
16 "idle",
17 "semi_active",
18 "bursty",
19 "sustained",
20 "battery_life",
21 NULL
22 };
23
workload_available_types_show(struct device * dev,struct device_attribute * attr,char * buf)24 static ssize_t workload_available_types_show(struct device *dev,
25 struct device_attribute *attr,
26 char *buf)
27 {
28 int i = 0;
29 int ret = 0;
30
31 while (workload_types[i] != NULL)
32 ret += sysfs_emit_at(buf, ret, "%s ", workload_types[i++]);
33
34 ret += sysfs_emit_at(buf, ret, "\n");
35
36 return ret;
37 }
38
39 static DEVICE_ATTR_RO(workload_available_types);
40
workload_type_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)41 static ssize_t workload_type_store(struct device *dev,
42 struct device_attribute *attr,
43 const char *buf, size_t count)
44 {
45 struct pci_dev *pdev = to_pci_dev(dev);
46 char str_preference[15];
47 u32 data = 0;
48 ssize_t ret;
49
50 ret = sscanf(buf, "%14s", str_preference);
51 if (ret != 1)
52 return -EINVAL;
53
54 ret = match_string(workload_types, -1, str_preference);
55 if (ret < 0)
56 return ret;
57
58 ret &= 0xff;
59
60 if (ret)
61 data = BIT(MBOX_DATA_BIT_VALID) | BIT(MBOX_DATA_BIT_AC_DC);
62
63 data |= ret;
64
65 ret = processor_thermal_send_mbox_write_cmd(pdev, MBOX_CMD_WORKLOAD_TYPE_WRITE, data);
66 if (ret)
67 return false;
68
69 return count;
70 }
71
workload_type_show(struct device * dev,struct device_attribute * attr,char * buf)72 static ssize_t workload_type_show(struct device *dev,
73 struct device_attribute *attr,
74 char *buf)
75 {
76 struct pci_dev *pdev = to_pci_dev(dev);
77 u64 cmd_resp;
78 int ret;
79
80 ret = processor_thermal_send_mbox_read_cmd(pdev, MBOX_CMD_WORKLOAD_TYPE_READ, &cmd_resp);
81 if (ret)
82 return false;
83
84 cmd_resp &= 0xff;
85
86 if (cmd_resp > ARRAY_SIZE(workload_types) - 1)
87 return -EINVAL;
88
89 return sysfs_emit(buf, "%s\n", workload_types[cmd_resp]);
90 }
91
92 static DEVICE_ATTR_RW(workload_type);
93
94 static struct attribute *workload_req_attrs[] = {
95 &dev_attr_workload_available_types.attr,
96 &dev_attr_workload_type.attr,
97 NULL
98 };
99
100 static const struct attribute_group workload_req_attribute_group = {
101 .attrs = workload_req_attrs,
102 .name = "workload_request"
103 };
104
105 static bool workload_req_created;
106
proc_thermal_wt_req_add(struct pci_dev * pdev,struct proc_thermal_device * proc_priv)107 int proc_thermal_wt_req_add(struct pci_dev *pdev, struct proc_thermal_device *proc_priv)
108 {
109 u64 cmd_resp;
110 int ret;
111
112 /* Check if there is a mailbox support, if fails return success */
113 ret = processor_thermal_send_mbox_read_cmd(pdev, MBOX_CMD_WORKLOAD_TYPE_READ, &cmd_resp);
114 if (ret)
115 return 0;
116
117 ret = sysfs_create_group(&pdev->dev.kobj, &workload_req_attribute_group);
118 if (ret)
119 return ret;
120
121 workload_req_created = true;
122
123 return 0;
124 }
125 EXPORT_SYMBOL_GPL(proc_thermal_wt_req_add);
126
proc_thermal_wt_req_remove(struct pci_dev * pdev)127 void proc_thermal_wt_req_remove(struct pci_dev *pdev)
128 {
129 if (workload_req_created)
130 sysfs_remove_group(&pdev->dev.kobj, &workload_req_attribute_group);
131
132 workload_req_created = false;
133 }
134 EXPORT_SYMBOL_GPL(proc_thermal_wt_req_remove);
135
136 MODULE_IMPORT_NS("INT340X_THERMAL");
137 MODULE_LICENSE("GPL");
138 MODULE_DESCRIPTION("Processor Thermal Work Load type request Interface");
139