1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * processor_thermal_device.c
4 * Copyright (c) 2014, Intel Corporation.
5 */
6 #include <linux/acpi.h>
7 #include <linux/intel_tcc.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/pci.h>
11 #include <linux/thermal.h>
12 #include "int340x_thermal_zone.h"
13 #include "processor_thermal_device.h"
14 #include "../intel_soc_dts_iosf.h"
15
16 #define DRV_NAME "proc_thermal"
17
18 #define POWER_LIMIT_SHOW(index, suffix) \
19 static ssize_t power_limit_##index##_##suffix##_show(struct device *dev, \
20 struct device_attribute *attr, \
21 char *buf) \
22 { \
23 struct proc_thermal_device *proc_dev = dev_get_drvdata(dev); \
24 \
25 return sprintf(buf, "%lu\n",\
26 (unsigned long)proc_dev->power_limits[index].suffix * 1000); \
27 }
28
29 POWER_LIMIT_SHOW(0, min_uw)
30 POWER_LIMIT_SHOW(0, max_uw)
31 POWER_LIMIT_SHOW(0, step_uw)
32 POWER_LIMIT_SHOW(0, tmin_us)
33 POWER_LIMIT_SHOW(0, tmax_us)
34
35 POWER_LIMIT_SHOW(1, min_uw)
36 POWER_LIMIT_SHOW(1, max_uw)
37 POWER_LIMIT_SHOW(1, step_uw)
38 POWER_LIMIT_SHOW(1, tmin_us)
39 POWER_LIMIT_SHOW(1, tmax_us)
40
41 static DEVICE_ATTR_RO(power_limit_0_min_uw);
42 static DEVICE_ATTR_RO(power_limit_0_max_uw);
43 static DEVICE_ATTR_RO(power_limit_0_step_uw);
44 static DEVICE_ATTR_RO(power_limit_0_tmin_us);
45 static DEVICE_ATTR_RO(power_limit_0_tmax_us);
46
47 static DEVICE_ATTR_RO(power_limit_1_min_uw);
48 static DEVICE_ATTR_RO(power_limit_1_max_uw);
49 static DEVICE_ATTR_RO(power_limit_1_step_uw);
50 static DEVICE_ATTR_RO(power_limit_1_tmin_us);
51 static DEVICE_ATTR_RO(power_limit_1_tmax_us);
52
53 static struct attribute *power_limit_attrs[] = {
54 &dev_attr_power_limit_0_min_uw.attr,
55 &dev_attr_power_limit_1_min_uw.attr,
56 &dev_attr_power_limit_0_max_uw.attr,
57 &dev_attr_power_limit_1_max_uw.attr,
58 &dev_attr_power_limit_0_step_uw.attr,
59 &dev_attr_power_limit_1_step_uw.attr,
60 &dev_attr_power_limit_0_tmin_us.attr,
61 &dev_attr_power_limit_1_tmin_us.attr,
62 &dev_attr_power_limit_0_tmax_us.attr,
63 &dev_attr_power_limit_1_tmax_us.attr,
64 NULL
65 };
66
67 static const struct attribute_group power_limit_attribute_group = {
68 .attrs = power_limit_attrs,
69 .name = "power_limits"
70 };
71
tcc_offset_degree_celsius_show(struct device * dev,struct device_attribute * attr,char * buf)72 static ssize_t tcc_offset_degree_celsius_show(struct device *dev,
73 struct device_attribute *attr,
74 char *buf)
75 {
76 int offset;
77
78 offset = intel_tcc_get_offset(-1);
79 if (offset < 0)
80 return offset;
81
82 return sprintf(buf, "%d\n", offset);
83 }
84
tcc_offset_degree_celsius_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)85 static ssize_t tcc_offset_degree_celsius_store(struct device *dev,
86 struct device_attribute *attr, const char *buf,
87 size_t count)
88 {
89 unsigned int tcc;
90 u64 val;
91 int err;
92
93 err = rdmsrl_safe(MSR_PLATFORM_INFO, &val);
94 if (err)
95 return err;
96
97 if (!(val & BIT(30)))
98 return -EACCES;
99
100 if (kstrtouint(buf, 0, &tcc))
101 return -EINVAL;
102
103 err = intel_tcc_set_offset(-1, tcc);
104 if (err)
105 return err;
106
107 return count;
108 }
109
110 static DEVICE_ATTR_RW(tcc_offset_degree_celsius);
111
proc_thermal_get_zone_temp(struct thermal_zone_device * zone,int * temp)112 static int proc_thermal_get_zone_temp(struct thermal_zone_device *zone,
113 int *temp)
114 {
115 int cpu;
116 int curr_temp;
117
118 *temp = 0;
119
120 for_each_online_cpu(cpu) {
121 curr_temp = intel_tcc_get_temp(cpu, false);
122 if (curr_temp < 0)
123 return curr_temp;
124 if (!*temp || curr_temp > *temp)
125 *temp = curr_temp;
126 }
127
128 *temp *= 1000;
129
130 return 0;
131 }
132
proc_thermal_read_ppcc(struct proc_thermal_device * proc_priv)133 static int proc_thermal_read_ppcc(struct proc_thermal_device *proc_priv)
134 {
135 int i;
136 acpi_status status;
137 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
138 union acpi_object *elements, *ppcc;
139 union acpi_object *p;
140 int ret = 0;
141
142 status = acpi_evaluate_object(proc_priv->adev->handle, "PPCC",
143 NULL, &buf);
144 if (ACPI_FAILURE(status))
145 return -ENODEV;
146
147 p = buf.pointer;
148 if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
149 dev_err(proc_priv->dev, "Invalid PPCC data\n");
150 ret = -EFAULT;
151 goto free_buffer;
152 }
153
154 if (!p->package.count) {
155 dev_err(proc_priv->dev, "Invalid PPCC package size\n");
156 ret = -EFAULT;
157 goto free_buffer;
158 }
159
160 for (i = 0; i < min((int)p->package.count - 1, 2); ++i) {
161 elements = &(p->package.elements[i+1]);
162 if (elements->type != ACPI_TYPE_PACKAGE ||
163 elements->package.count != 6) {
164 ret = -EFAULT;
165 goto free_buffer;
166 }
167 ppcc = elements->package.elements;
168 proc_priv->power_limits[i].index = ppcc[0].integer.value;
169 proc_priv->power_limits[i].min_uw = ppcc[1].integer.value;
170 proc_priv->power_limits[i].max_uw = ppcc[2].integer.value;
171 proc_priv->power_limits[i].tmin_us = ppcc[3].integer.value;
172 proc_priv->power_limits[i].tmax_us = ppcc[4].integer.value;
173 proc_priv->power_limits[i].step_uw = ppcc[5].integer.value;
174 }
175
176 free_buffer:
177 kfree(buf.pointer);
178
179 return ret;
180 }
181
182 #define PROC_POWER_CAPABILITY_CHANGED 0x83
proc_thermal_notify(acpi_handle handle,u32 event,void * data)183 static void proc_thermal_notify(acpi_handle handle, u32 event, void *data)
184 {
185 struct proc_thermal_device *proc_priv = data;
186
187 if (!proc_priv)
188 return;
189
190 switch (event) {
191 case PROC_POWER_CAPABILITY_CHANGED:
192 proc_thermal_read_ppcc(proc_priv);
193 int340x_thermal_zone_device_update(proc_priv->int340x_zone,
194 THERMAL_DEVICE_POWER_CAPABILITY_CHANGED);
195 break;
196 default:
197 dev_dbg(proc_priv->dev, "Unsupported event [0x%x]\n", event);
198 break;
199 }
200 }
201
proc_thermal_add(struct device * dev,struct proc_thermal_device * proc_priv)202 int proc_thermal_add(struct device *dev, struct proc_thermal_device *proc_priv)
203 {
204 struct acpi_device *adev;
205 acpi_status status;
206 unsigned long long tmp;
207 int (*get_temp) (struct thermal_zone_device *, int *) = NULL;
208 int ret;
209
210 adev = ACPI_COMPANION(dev);
211 if (!adev)
212 return -ENODEV;
213
214 proc_priv->dev = dev;
215 proc_priv->adev = adev;
216
217 ret = proc_thermal_read_ppcc(proc_priv);
218 if (ret)
219 return ret;
220
221 status = acpi_evaluate_integer(adev->handle, "_TMP", NULL, &tmp);
222 if (ACPI_FAILURE(status)) {
223 /* there is no _TMP method, add local method */
224 if (intel_tcc_get_tjmax(-1) > 0)
225 get_temp = proc_thermal_get_zone_temp;
226 }
227
228 proc_priv->int340x_zone = int340x_thermal_zone_add(adev, get_temp);
229 if (IS_ERR(proc_priv->int340x_zone)) {
230 return PTR_ERR(proc_priv->int340x_zone);
231 } else
232 ret = 0;
233
234 ret = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
235 proc_thermal_notify,
236 (void *)proc_priv);
237 if (ret)
238 goto remove_zone;
239
240 ret = sysfs_create_file(&dev->kobj, &dev_attr_tcc_offset_degree_celsius.attr);
241 if (ret)
242 goto remove_notify;
243
244 ret = sysfs_create_group(&dev->kobj, &power_limit_attribute_group);
245 if (ret) {
246 sysfs_remove_file(&dev->kobj, &dev_attr_tcc_offset_degree_celsius.attr);
247 goto remove_notify;
248 }
249
250 return 0;
251
252 remove_notify:
253 acpi_remove_notify_handler(adev->handle,
254 ACPI_DEVICE_NOTIFY, proc_thermal_notify);
255 remove_zone:
256 int340x_thermal_zone_remove(proc_priv->int340x_zone);
257
258 return ret;
259 }
260 EXPORT_SYMBOL_GPL(proc_thermal_add);
261
proc_thermal_remove(struct proc_thermal_device * proc_priv)262 void proc_thermal_remove(struct proc_thermal_device *proc_priv)
263 {
264 acpi_remove_notify_handler(proc_priv->adev->handle,
265 ACPI_DEVICE_NOTIFY, proc_thermal_notify);
266 int340x_thermal_zone_remove(proc_priv->int340x_zone);
267 sysfs_remove_file(&proc_priv->dev->kobj, &dev_attr_tcc_offset_degree_celsius.attr);
268 sysfs_remove_group(&proc_priv->dev->kobj,
269 &power_limit_attribute_group);
270 }
271 EXPORT_SYMBOL_GPL(proc_thermal_remove);
272
273 static int tcc_offset_save = -1;
274
proc_thermal_suspend(struct device * dev)275 int proc_thermal_suspend(struct device *dev)
276 {
277 tcc_offset_save = intel_tcc_get_offset(-1);
278 if (tcc_offset_save < 0)
279 dev_warn(dev, "failed to save offset (%d)\n", tcc_offset_save);
280
281 return 0;
282 }
283 EXPORT_SYMBOL_GPL(proc_thermal_suspend);
284
proc_thermal_resume(struct device * dev)285 int proc_thermal_resume(struct device *dev)
286 {
287 struct proc_thermal_device *proc_dev;
288
289 proc_dev = dev_get_drvdata(dev);
290 proc_thermal_read_ppcc(proc_dev);
291
292 /* Do not update if saving failed */
293 if (tcc_offset_save >= 0)
294 intel_tcc_set_offset(-1, tcc_offset_save);
295
296 return 0;
297 }
298 EXPORT_SYMBOL_GPL(proc_thermal_resume);
299
300 #define MCHBAR 0
301
proc_thermal_set_mmio_base(struct pci_dev * pdev,struct proc_thermal_device * proc_priv)302 static int proc_thermal_set_mmio_base(struct pci_dev *pdev, struct proc_thermal_device *proc_priv)
303 {
304 int ret;
305
306 ret = pcim_iomap_regions(pdev, 1 << MCHBAR, DRV_NAME);
307 if (ret) {
308 dev_err(&pdev->dev, "cannot reserve PCI memory region\n");
309 return -ENOMEM;
310 }
311
312 proc_priv->mmio_base = pcim_iomap_table(pdev)[MCHBAR];
313
314 return 0;
315 }
316
proc_thermal_mmio_add(struct pci_dev * pdev,struct proc_thermal_device * proc_priv,kernel_ulong_t feature_mask)317 int proc_thermal_mmio_add(struct pci_dev *pdev,
318 struct proc_thermal_device *proc_priv,
319 kernel_ulong_t feature_mask)
320 {
321 int ret;
322
323 proc_priv->mmio_feature_mask = feature_mask;
324
325 if (feature_mask) {
326 ret = proc_thermal_set_mmio_base(pdev, proc_priv);
327 if (ret)
328 return ret;
329 }
330
331 if (feature_mask & PROC_THERMAL_FEATURE_RAPL) {
332 ret = proc_thermal_rapl_add(pdev, proc_priv);
333 if (ret) {
334 dev_err(&pdev->dev, "failed to add RAPL MMIO interface\n");
335 return ret;
336 }
337 }
338
339 if (feature_mask & PROC_THERMAL_FEATURE_FIVR ||
340 feature_mask & PROC_THERMAL_FEATURE_DVFS ||
341 feature_mask & PROC_THERMAL_FEATURE_DLVR) {
342 ret = proc_thermal_rfim_add(pdev, proc_priv);
343 if (ret) {
344 dev_err(&pdev->dev, "failed to add RFIM interface\n");
345 goto err_rem_rapl;
346 }
347 }
348
349 if (feature_mask & PROC_THERMAL_FEATURE_MBOX) {
350 ret = proc_thermal_mbox_add(pdev, proc_priv);
351 if (ret) {
352 dev_err(&pdev->dev, "failed to add MBOX interface\n");
353 goto err_rem_rfim;
354 }
355 }
356
357 return 0;
358
359 err_rem_rfim:
360 proc_thermal_rfim_remove(pdev);
361 err_rem_rapl:
362 proc_thermal_rapl_remove();
363
364 return ret;
365 }
366 EXPORT_SYMBOL_GPL(proc_thermal_mmio_add);
367
proc_thermal_mmio_remove(struct pci_dev * pdev,struct proc_thermal_device * proc_priv)368 void proc_thermal_mmio_remove(struct pci_dev *pdev, struct proc_thermal_device *proc_priv)
369 {
370 if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_RAPL)
371 proc_thermal_rapl_remove();
372
373 if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_FIVR ||
374 proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_DVFS)
375 proc_thermal_rfim_remove(pdev);
376
377 if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_MBOX)
378 proc_thermal_mbox_remove(pdev);
379 }
380 EXPORT_SYMBOL_GPL(proc_thermal_mmio_remove);
381
382 MODULE_IMPORT_NS(INTEL_TCC);
383 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
384 MODULE_DESCRIPTION("Processor Thermal Reporting Device Driver");
385 MODULE_LICENSE("GPL v2");
386