File Index
Files and directories
Sign in
FILE

processor_thermal_device.c

File preview with syntax highlighting.

linux-6.19.14/drivers/thermal/intel/int340x_thermal/processor_thermal_device.c C
  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 <asm/msr.h>
 13#include "int340x_thermal_zone.h"
 14#include "processor_thermal_device.h"
 15#include "../intel_soc_dts_iosf.h"
 16
 17#define DRV_NAME "proc_thermal"
 18
 19#define POWER_LIMIT_SHOW(index, suffix) \
 20static ssize_t power_limit_##index##_##suffix##_show(struct device *dev, \
 21					struct device_attribute *attr, \
 22					char *buf) \
 23{ \
 24	struct proc_thermal_device *proc_dev = dev_get_drvdata(dev); \
 25	\
 26	return sprintf(buf, "%lu\n",\
 27	(unsigned long)proc_dev->power_limits[index].suffix * 1000); \
 28}
 29
 30static ssize_t power_floor_status_show(struct device *dev,
 31				       struct device_attribute *attr,
 32				       char *buf)
 33{
 34	struct proc_thermal_device *proc_dev = dev_get_drvdata(dev);
 35	int ret;
 36
 37	ret = proc_thermal_read_power_floor_status(proc_dev);
 38
 39	return sysfs_emit(buf, "%d\n", ret);
 40}
 41
 42static ssize_t power_floor_enable_show(struct device *dev,
 43				       struct device_attribute *attr,
 44				       char *buf)
 45{
 46	struct proc_thermal_device *proc_dev = dev_get_drvdata(dev);
 47	bool ret;
 48
 49	ret = proc_thermal_power_floor_get_state(proc_dev);
 50
 51	return sysfs_emit(buf, "%d\n", ret);
 52}
 53
 54static ssize_t power_floor_enable_store(struct device *dev,
 55					struct device_attribute *attr,
 56					const char *buf, size_t count)
 57{
 58	struct proc_thermal_device *proc_dev = dev_get_drvdata(dev);
 59	u8 state;
 60	int ret;
 61
 62	if (kstrtou8(buf, 0, &state))
 63		return -EINVAL;
 64
 65	ret = proc_thermal_power_floor_set_state(proc_dev, !!state);
 66	if (ret)
 67		return ret;
 68
 69	return count;
 70}
 71
 72POWER_LIMIT_SHOW(0, min_uw)
 73POWER_LIMIT_SHOW(0, max_uw)
 74POWER_LIMIT_SHOW(0, step_uw)
 75POWER_LIMIT_SHOW(0, tmin_us)
 76POWER_LIMIT_SHOW(0, tmax_us)
 77
 78POWER_LIMIT_SHOW(1, min_uw)
 79POWER_LIMIT_SHOW(1, max_uw)
 80POWER_LIMIT_SHOW(1, step_uw)
 81POWER_LIMIT_SHOW(1, tmin_us)
 82POWER_LIMIT_SHOW(1, tmax_us)
 83
 84static DEVICE_ATTR_RO(power_limit_0_min_uw);
 85static DEVICE_ATTR_RO(power_limit_0_max_uw);
 86static DEVICE_ATTR_RO(power_limit_0_step_uw);
 87static DEVICE_ATTR_RO(power_limit_0_tmin_us);
 88static DEVICE_ATTR_RO(power_limit_0_tmax_us);
 89
 90static DEVICE_ATTR_RO(power_limit_1_min_uw);
 91static DEVICE_ATTR_RO(power_limit_1_max_uw);
 92static DEVICE_ATTR_RO(power_limit_1_step_uw);
 93static DEVICE_ATTR_RO(power_limit_1_tmin_us);
 94static DEVICE_ATTR_RO(power_limit_1_tmax_us);
 95
 96static DEVICE_ATTR_RO(power_floor_status);
 97static DEVICE_ATTR_RW(power_floor_enable);
 98
 99static struct attribute *power_limit_attrs[] = {
100	&dev_attr_power_limit_0_min_uw.attr,
101	&dev_attr_power_limit_1_min_uw.attr,
102	&dev_attr_power_limit_0_max_uw.attr,
103	&dev_attr_power_limit_1_max_uw.attr,
104	&dev_attr_power_limit_0_step_uw.attr,
105	&dev_attr_power_limit_1_step_uw.attr,
106	&dev_attr_power_limit_0_tmin_us.attr,
107	&dev_attr_power_limit_1_tmin_us.attr,
108	&dev_attr_power_limit_0_tmax_us.attr,
109	&dev_attr_power_limit_1_tmax_us.attr,
110	&dev_attr_power_floor_status.attr,
111	&dev_attr_power_floor_enable.attr,
112	NULL
113};
114
115static umode_t power_limit_attr_visible(struct kobject *kobj, struct attribute *attr, int unused)
116{
117	struct device *dev = kobj_to_dev(kobj);
118	struct proc_thermal_device *proc_dev;
119
120	if (attr != &dev_attr_power_floor_status.attr && attr != &dev_attr_power_floor_enable.attr)
121		return attr->mode;
122
123	proc_dev = dev_get_drvdata(dev);
124	if (!proc_dev || !(proc_dev->mmio_feature_mask & PROC_THERMAL_FEATURE_POWER_FLOOR))
125		return 0;
126
127	return attr->mode;
128}
129
130static const struct attribute_group power_limit_attribute_group = {
131	.attrs = power_limit_attrs,
132	.name = "power_limits",
133	.is_visible = power_limit_attr_visible,
134};
135
136static ssize_t tcc_offset_degree_celsius_show(struct device *dev,
137					      struct device_attribute *attr,
138					      char *buf)
139{
140	int offset;
141
142	offset = intel_tcc_get_offset(-1);
143	if (offset < 0)
144		return offset;
145
146	return sprintf(buf, "%d\n", offset);
147}
148
149static ssize_t tcc_offset_degree_celsius_store(struct device *dev,
150				struct device_attribute *attr, const char *buf,
151				size_t count)
152{
153	unsigned int tcc;
154	u64 val;
155	int err;
156
157	err = rdmsrq_safe(MSR_PLATFORM_INFO, &val);
158	if (err)
159		return err;
160
161	if (!(val & BIT(30)))
162		return -EACCES;
163
164	if (kstrtouint(buf, 0, &tcc))
165		return -EINVAL;
166
167	err = intel_tcc_set_offset(-1, tcc);
168	if (err)
169		return err;
170
171	return count;
172}
173
174static DEVICE_ATTR_RW(tcc_offset_degree_celsius);
175
176static int proc_thermal_get_zone_temp(struct thermal_zone_device *zone,
177					 int *temp)
178{
179	int cpu;
180	int curr_temp, ret;
181
182	*temp = 0;
183
184	for_each_online_cpu(cpu) {
185		ret = intel_tcc_get_temp(cpu, &curr_temp, false);
186		if (ret < 0)
187			return ret;
188		if (!*temp || curr_temp > *temp)
189			*temp = curr_temp;
190	}
191
192	*temp *= 1000;
193
194	return 0;
195}
196
197static int proc_thermal_read_ppcc(struct proc_thermal_device *proc_priv)
198{
199	int i;
200	acpi_status status;
201	struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
202	union acpi_object *elements, *ppcc;
203	union acpi_object *p;
204	int ret = 0;
205
206	status = acpi_evaluate_object(proc_priv->adev->handle, "PPCC",
207				      NULL, &buf);
208	if (ACPI_FAILURE(status))
209		return -ENODEV;
210
211	p = buf.pointer;
212	if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
213		dev_err(proc_priv->dev, "Invalid PPCC data\n");
214		ret = -EFAULT;
215		goto free_buffer;
216	}
217
218	if (!p->package.count) {
219		dev_err(proc_priv->dev, "Invalid PPCC package size\n");
220		ret = -EFAULT;
221		goto free_buffer;
222	}
223
224	for (i = 0; i < min((int)p->package.count - 1, 2); ++i) {
225		elements = &(p->package.elements[i+1]);
226		if (elements->type != ACPI_TYPE_PACKAGE ||
227		    elements->package.count != 6) {
228			ret = -EFAULT;
229			goto free_buffer;
230		}
231		ppcc = elements->package.elements;
232		proc_priv->power_limits[i].index = ppcc[0].integer.value;
233		proc_priv->power_limits[i].min_uw = ppcc[1].integer.value;
234		proc_priv->power_limits[i].max_uw = ppcc[2].integer.value;
235		proc_priv->power_limits[i].tmin_us = ppcc[3].integer.value;
236		proc_priv->power_limits[i].tmax_us = ppcc[4].integer.value;
237		proc_priv->power_limits[i].step_uw = ppcc[5].integer.value;
238	}
239
240free_buffer:
241	kfree(buf.pointer);
242
243	return ret;
244}
245
246#define PROC_POWER_CAPABILITY_CHANGED	0x83
247static void proc_thermal_notify(acpi_handle handle, u32 event, void *data)
248{
249	struct proc_thermal_device *proc_priv = data;
250
251	if (!proc_priv)
252		return;
253
254	switch (event) {
255	case PROC_POWER_CAPABILITY_CHANGED:
256		proc_thermal_read_ppcc(proc_priv);
257		int340x_thermal_zone_device_update(proc_priv->int340x_zone,
258				THERMAL_DEVICE_POWER_CAPABILITY_CHANGED);
259		break;
260	default:
261		dev_dbg(proc_priv->dev, "Unsupported event [0x%x]\n", event);
262		break;
263	}
264}
265
266int proc_thermal_add(struct device *dev, struct proc_thermal_device *proc_priv)
267{
268	struct acpi_device *adev;
269	acpi_status status;
270	unsigned long long tmp;
271	int (*get_temp) (struct thermal_zone_device *, int *) = NULL;
272	int ret;
273
274	adev = ACPI_COMPANION(dev);
275	if (!adev)
276		return -ENODEV;
277
278	proc_priv->dev = dev;
279	proc_priv->adev = adev;
280
281	ret = proc_thermal_read_ppcc(proc_priv);
282	if (ret)
283		return ret;
284
285	status = acpi_evaluate_integer(adev->handle, "_TMP", NULL, &tmp);
286	if (ACPI_FAILURE(status)) {
287		/* there is no _TMP method, add local method */
288		if (intel_tcc_get_tjmax(-1) > 0)
289			get_temp = proc_thermal_get_zone_temp;
290	}
291
292	proc_priv->int340x_zone = int340x_thermal_zone_add(adev, get_temp);
293	if (IS_ERR(proc_priv->int340x_zone)) {
294		return PTR_ERR(proc_priv->int340x_zone);
295	} else
296		ret = 0;
297
298	ret = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
299					  proc_thermal_notify,
300					  (void *)proc_priv);
301	if (ret)
302		goto remove_zone;
303
304	ret = sysfs_create_file(&dev->kobj, &dev_attr_tcc_offset_degree_celsius.attr);
305	if (ret)
306		goto remove_notify;
307
308	ret = sysfs_create_group(&dev->kobj, &power_limit_attribute_group);
309	if (ret) {
310		sysfs_remove_file(&dev->kobj, &dev_attr_tcc_offset_degree_celsius.attr);
311		goto remove_notify;
312	}
313
314	return 0;
315
316remove_notify:
317	acpi_remove_notify_handler(adev->handle,
318				    ACPI_DEVICE_NOTIFY, proc_thermal_notify);
319remove_zone:
320	int340x_thermal_zone_remove(proc_priv->int340x_zone);
321
322	return ret;
323}
324EXPORT_SYMBOL_GPL(proc_thermal_add);
325
326void proc_thermal_remove(struct proc_thermal_device *proc_priv)
327{
328	acpi_remove_notify_handler(proc_priv->adev->handle,
329				   ACPI_DEVICE_NOTIFY, proc_thermal_notify);
330	int340x_thermal_zone_remove(proc_priv->int340x_zone);
331	sysfs_remove_file(&proc_priv->dev->kobj, &dev_attr_tcc_offset_degree_celsius.attr);
332	sysfs_remove_group(&proc_priv->dev->kobj,
333			   &power_limit_attribute_group);
334}
335EXPORT_SYMBOL_GPL(proc_thermal_remove);
336
337static int tcc_offset_save = -1;
338
339int proc_thermal_suspend(struct device *dev)
340{
341	struct proc_thermal_device *proc_dev;
342
343	tcc_offset_save = intel_tcc_get_offset(-1);
344	if (tcc_offset_save < 0)
345		dev_warn(dev, "failed to save offset (%d)\n", tcc_offset_save);
346
347	proc_dev = dev_get_drvdata(dev);
348
349	if (proc_dev->mmio_feature_mask & PROC_THERMAL_FEATURE_SOC_POWER_SLIDER)
350		proc_thermal_soc_power_slider_suspend(proc_dev);
351
352	return 0;
353}
354EXPORT_SYMBOL_GPL(proc_thermal_suspend);
355
356int proc_thermal_resume(struct device *dev)
357{
358	struct proc_thermal_device *proc_dev;
359
360	proc_dev = dev_get_drvdata(dev);
361	proc_thermal_read_ppcc(proc_dev);
362
363	/* Do not update if saving failed */
364	if (tcc_offset_save >= 0)
365		intel_tcc_set_offset(-1, tcc_offset_save);
366
367	if (proc_dev->mmio_feature_mask & PROC_THERMAL_FEATURE_SOC_POWER_SLIDER)
368		proc_thermal_soc_power_slider_resume(proc_dev);
369
370	return 0;
371}
372EXPORT_SYMBOL_GPL(proc_thermal_resume);
373
374#define MCHBAR 0
375
376static int proc_thermal_set_mmio_base(struct pci_dev *pdev, struct proc_thermal_device *proc_priv)
377{
378	int ret;
379
380	ret = pcim_iomap_regions(pdev, 1 << MCHBAR, DRV_NAME);
381	if (ret) {
382		dev_err(&pdev->dev, "cannot reserve PCI memory region\n");
383		return -ENOMEM;
384	}
385
386	proc_priv->mmio_base = pcim_iomap_table(pdev)[MCHBAR];
387
388	return 0;
389}
390
391int proc_thermal_mmio_add(struct pci_dev *pdev,
392			  struct proc_thermal_device *proc_priv,
393			  kernel_ulong_t feature_mask)
394{
395	int ret;
396
397	proc_priv->mmio_feature_mask = feature_mask;
398
399	if (feature_mask) {
400		ret = proc_thermal_set_mmio_base(pdev, proc_priv);
401		if (ret)
402			return ret;
403	}
404
405	if (feature_mask & PROC_THERMAL_FEATURE_RAPL) {
406		ret = proc_thermal_rapl_add(pdev, proc_priv);
407		if (ret) {
408			dev_err(&pdev->dev, "failed to add RAPL MMIO interface\n");
409			return ret;
410		}
411	}
412
413	if (feature_mask & PROC_THERMAL_FEATURE_PTC) {
414		ret = proc_thermal_ptc_add(pdev, proc_priv);
415		if (ret) {
416			dev_err(&pdev->dev, "failed to add PTC MMIO interface\n");
417			goto err_rem_rapl;
418		}
419	}
420
421	if (feature_mask & PROC_THERMAL_FEATURE_FIVR ||
422	    feature_mask & PROC_THERMAL_FEATURE_DVFS ||
423	    feature_mask & PROC_THERMAL_FEATURE_DLVR) {
424		ret = proc_thermal_rfim_add(pdev, proc_priv);
425		if (ret) {
426			dev_err(&pdev->dev, "failed to add RFIM interface\n");
427			goto err_rem_ptc;
428		}
429	}
430
431	if (feature_mask & PROC_THERMAL_FEATURE_WT_REQ) {
432		ret = proc_thermal_wt_req_add(pdev, proc_priv);
433		if (ret) {
434			dev_err(&pdev->dev, "failed to add MBOX interface\n");
435			goto err_rem_rfim;
436		}
437	} else if (feature_mask & PROC_THERMAL_FEATURE_WT_HINT) {
438		ret = proc_thermal_wt_hint_add(pdev, proc_priv);
439		if (ret) {
440			dev_err(&pdev->dev, "failed to add WT Hint\n");
441			goto err_rem_rfim;
442		}
443	}
444
445	if (feature_mask & PROC_THERMAL_FEATURE_SOC_POWER_SLIDER) {
446		ret = proc_thermal_soc_power_slider_add(pdev, proc_priv);
447		if (ret) {
448			dev_info(&pdev->dev, "failed to add soc power efficiency slider\n");
449			goto err_rem_wlt;
450		}
451	}
452
453	return 0;
454
455err_rem_wlt:
456	proc_thermal_wt_hint_remove(pdev);
457err_rem_rfim:
458	proc_thermal_rfim_remove(pdev);
459err_rem_ptc:
460	proc_thermal_ptc_remove(pdev);
461err_rem_rapl:
462	proc_thermal_rapl_remove();
463
464	return ret;
465}
466EXPORT_SYMBOL_GPL(proc_thermal_mmio_add);
467
468void proc_thermal_mmio_remove(struct pci_dev *pdev, struct proc_thermal_device *proc_priv)
469{
470	if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_RAPL)
471		proc_thermal_rapl_remove();
472
473	if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_PTC)
474		proc_thermal_ptc_remove(pdev);
475
476	if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_FIVR ||
477	    proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_DVFS ||
478	    proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_DLVR)
479		proc_thermal_rfim_remove(pdev);
480
481	if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_POWER_FLOOR)
482		proc_thermal_power_floor_set_state(proc_priv, false);
483
484	if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_WT_REQ)
485		proc_thermal_wt_req_remove(pdev);
486	else if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_WT_HINT)
487		proc_thermal_wt_hint_remove(pdev);
488}
489EXPORT_SYMBOL_GPL(proc_thermal_mmio_remove);
490
491MODULE_IMPORT_NS("INTEL_TCC");
492MODULE_IMPORT_NS("INT340X_THERMAL");
493MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
494MODULE_DESCRIPTION("Processor Thermal Reporting Device Driver");
495MODULE_LICENSE("GPL v2");
~/ Read-only file preview.