1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/init.h>
18#include <linux/pci.h>
19#include <linux/interrupt.h>
20#include <linux/platform_device.h>
21#include <linux/acpi.h>
22#include <linux/thermal.h>
23#include "int340x_thermal_zone.h"
24#include "../intel_soc_dts_iosf.h"
25
26
27#define PCI_DEVICE_ID_PROC_BDW_THERMAL 0x1603
28#define PCI_DEVICE_ID_PROC_HSB_THERMAL 0x0A03
29
30
31#define PCI_DEVICE_ID_PROC_SKL_THERMAL 0x1903
32
33
34#define PCI_DEVICE_ID_PROC_BSW_THERMAL 0x22DC
35
36
37#define PCI_DEVICE_ID_PROC_BXT0_THERMAL 0x0A8C
38#define PCI_DEVICE_ID_PROC_BXT1_THERMAL 0x1A8C
39#define PCI_DEVICE_ID_PROC_BXTX_THERMAL 0x4A8C
40#define PCI_DEVICE_ID_PROC_BXTP_THERMAL 0x5A8C
41
42struct power_config {
43 u32 index;
44 u32 min_uw;
45 u32 max_uw;
46 u32 tmin_us;
47 u32 tmax_us;
48 u32 step_uw;
49};
50
51struct proc_thermal_device {
52 struct device *dev;
53 struct acpi_device *adev;
54 struct power_config power_limits[2];
55 struct int34x_thermal_zone *int340x_zone;
56 struct intel_soc_dts_sensors *soc_dts;
57};
58
59enum proc_thermal_emum_mode_type {
60 PROC_THERMAL_NONE,
61 PROC_THERMAL_PCI,
62 PROC_THERMAL_PLATFORM_DEV
63};
64
65
66
67
68
69static enum proc_thermal_emum_mode_type proc_thermal_emum_mode =
70 PROC_THERMAL_NONE;
71
72#define POWER_LIMIT_SHOW(index, suffix) \
73static ssize_t power_limit_##index##_##suffix##_show(struct device *dev, \
74 struct device_attribute *attr, \
75 char *buf) \
76{ \
77 struct pci_dev *pci_dev; \
78 struct platform_device *pdev; \
79 struct proc_thermal_device *proc_dev; \
80\
81 if (proc_thermal_emum_mode == PROC_THERMAL_PLATFORM_DEV) { \
82 pdev = to_platform_device(dev); \
83 proc_dev = platform_get_drvdata(pdev); \
84 } else { \
85 pci_dev = to_pci_dev(dev); \
86 proc_dev = pci_get_drvdata(pci_dev); \
87 } \
88 return sprintf(buf, "%lu\n",\
89 (unsigned long)proc_dev->power_limits[index].suffix * 1000); \
90}
91
92POWER_LIMIT_SHOW(0, min_uw)
93POWER_LIMIT_SHOW(0, max_uw)
94POWER_LIMIT_SHOW(0, step_uw)
95POWER_LIMIT_SHOW(0, tmin_us)
96POWER_LIMIT_SHOW(0, tmax_us)
97
98POWER_LIMIT_SHOW(1, min_uw)
99POWER_LIMIT_SHOW(1, max_uw)
100POWER_LIMIT_SHOW(1, step_uw)
101POWER_LIMIT_SHOW(1, tmin_us)
102POWER_LIMIT_SHOW(1, tmax_us)
103
104static DEVICE_ATTR_RO(power_limit_0_min_uw);
105static DEVICE_ATTR_RO(power_limit_0_max_uw);
106static DEVICE_ATTR_RO(power_limit_0_step_uw);
107static DEVICE_ATTR_RO(power_limit_0_tmin_us);
108static DEVICE_ATTR_RO(power_limit_0_tmax_us);
109
110static DEVICE_ATTR_RO(power_limit_1_min_uw);
111static DEVICE_ATTR_RO(power_limit_1_max_uw);
112static DEVICE_ATTR_RO(power_limit_1_step_uw);
113static DEVICE_ATTR_RO(power_limit_1_tmin_us);
114static DEVICE_ATTR_RO(power_limit_1_tmax_us);
115
116static struct attribute *power_limit_attrs[] = {
117 &dev_attr_power_limit_0_min_uw.attr,
118 &dev_attr_power_limit_1_min_uw.attr,
119 &dev_attr_power_limit_0_max_uw.attr,
120 &dev_attr_power_limit_1_max_uw.attr,
121 &dev_attr_power_limit_0_step_uw.attr,
122 &dev_attr_power_limit_1_step_uw.attr,
123 &dev_attr_power_limit_0_tmin_us.attr,
124 &dev_attr_power_limit_1_tmin_us.attr,
125 &dev_attr_power_limit_0_tmax_us.attr,
126 &dev_attr_power_limit_1_tmax_us.attr,
127 NULL
128};
129
130static struct attribute_group power_limit_attribute_group = {
131 .attrs = power_limit_attrs,
132 .name = "power_limits"
133};
134
135static int stored_tjmax;
136
137static int get_tjmax(void)
138{
139 u32 eax, edx;
140 u32 val;
141 int err;
142
143 err = rdmsr_safe(MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
144 if (err)
145 return err;
146
147 val = (eax >> 16) & 0xff;
148 if (val)
149 return val;
150
151 return -EINVAL;
152}
153
154static int read_temp_msr(int *temp)
155{
156 int cpu;
157 u32 eax, edx;
158 int err;
159 unsigned long curr_temp_off = 0;
160
161 *temp = 0;
162
163 for_each_online_cpu(cpu) {
164 err = rdmsr_safe_on_cpu(cpu, MSR_IA32_THERM_STATUS, &eax,
165 &edx);
166 if (err)
167 goto err_ret;
168 else {
169 if (eax & 0x80000000) {
170 curr_temp_off = (eax >> 16) & 0x7f;
171 if (!*temp || curr_temp_off < *temp)
172 *temp = curr_temp_off;
173 } else {
174 err = -EINVAL;
175 goto err_ret;
176 }
177 }
178 }
179
180 return 0;
181err_ret:
182 return err;
183}
184
185static int proc_thermal_get_zone_temp(struct thermal_zone_device *zone,
186 int *temp)
187{
188 int ret;
189
190 ret = read_temp_msr(temp);
191 if (!ret)
192 *temp = (stored_tjmax - *temp) * 1000;
193
194 return ret;
195}
196
197static struct thermal_zone_device_ops proc_thermal_local_ops = {
198 .get_temp = proc_thermal_get_zone_temp,
199};
200
201static int proc_thermal_add(struct device *dev,
202 struct proc_thermal_device **priv)
203{
204 struct proc_thermal_device *proc_priv;
205 struct acpi_device *adev;
206 acpi_status status;
207 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
208 union acpi_object *elements, *ppcc;
209 union acpi_object *p;
210 unsigned long long tmp;
211 struct thermal_zone_device_ops *ops = NULL;
212 int i;
213 int ret;
214
215 adev = ACPI_COMPANION(dev);
216 if (!adev)
217 return -ENODEV;
218
219 status = acpi_evaluate_object(adev->handle, "PPCC", NULL, &buf);
220 if (ACPI_FAILURE(status))
221 return -ENODEV;
222
223 p = buf.pointer;
224 if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
225 dev_err(dev, "Invalid PPCC data\n");
226 ret = -EFAULT;
227 goto free_buffer;
228 }
229 if (!p->package.count) {
230 dev_err(dev, "Invalid PPCC package size\n");
231 ret = -EFAULT;
232 goto free_buffer;
233 }
234
235 proc_priv = devm_kzalloc(dev, sizeof(*proc_priv), GFP_KERNEL);
236 if (!proc_priv) {
237 ret = -ENOMEM;
238 goto free_buffer;
239 }
240
241 proc_priv->dev = dev;
242 proc_priv->adev = adev;
243
244 for (i = 0; i < min((int)p->package.count - 1, 2); ++i) {
245 elements = &(p->package.elements[i+1]);
246 if (elements->type != ACPI_TYPE_PACKAGE ||
247 elements->package.count != 6) {
248 ret = -EFAULT;
249 goto free_buffer;
250 }
251 ppcc = elements->package.elements;
252 proc_priv->power_limits[i].index = ppcc[0].integer.value;
253 proc_priv->power_limits[i].min_uw = ppcc[1].integer.value;
254 proc_priv->power_limits[i].max_uw = ppcc[2].integer.value;
255 proc_priv->power_limits[i].tmin_us = ppcc[3].integer.value;
256 proc_priv->power_limits[i].tmax_us = ppcc[4].integer.value;
257 proc_priv->power_limits[i].step_uw = ppcc[5].integer.value;
258 }
259
260 *priv = proc_priv;
261
262 ret = sysfs_create_group(&dev->kobj,
263 &power_limit_attribute_group);
264 if (ret)
265 goto free_buffer;
266
267 status = acpi_evaluate_integer(adev->handle, "_TMP", NULL, &tmp);
268 if (ACPI_FAILURE(status)) {
269
270 stored_tjmax = get_tjmax();
271 if (stored_tjmax > 0)
272 ops = &proc_thermal_local_ops;
273 }
274
275 proc_priv->int340x_zone = int340x_thermal_zone_add(adev, ops);
276 if (IS_ERR(proc_priv->int340x_zone)) {
277 sysfs_remove_group(&proc_priv->dev->kobj,
278 &power_limit_attribute_group);
279 ret = PTR_ERR(proc_priv->int340x_zone);
280 } else
281 ret = 0;
282
283free_buffer:
284 kfree(buf.pointer);
285
286 return ret;
287}
288
289static void proc_thermal_remove(struct proc_thermal_device *proc_priv)
290{
291 int340x_thermal_zone_remove(proc_priv->int340x_zone);
292 sysfs_remove_group(&proc_priv->dev->kobj,
293 &power_limit_attribute_group);
294}
295
296static int int3401_add(struct platform_device *pdev)
297{
298 struct proc_thermal_device *proc_priv;
299 int ret;
300
301 if (proc_thermal_emum_mode == PROC_THERMAL_PCI) {
302 dev_err(&pdev->dev, "error: enumerated as PCI dev\n");
303 return -ENODEV;
304 }
305
306 ret = proc_thermal_add(&pdev->dev, &proc_priv);
307 if (ret)
308 return ret;
309
310 platform_set_drvdata(pdev, proc_priv);
311 proc_thermal_emum_mode = PROC_THERMAL_PLATFORM_DEV;
312
313 return 0;
314}
315
316static int int3401_remove(struct platform_device *pdev)
317{
318 proc_thermal_remove(platform_get_drvdata(pdev));
319
320 return 0;
321}
322
323static irqreturn_t proc_thermal_pci_msi_irq(int irq, void *devid)
324{
325 struct proc_thermal_device *proc_priv;
326 struct pci_dev *pdev = devid;
327
328 proc_priv = pci_get_drvdata(pdev);
329
330 intel_soc_dts_iosf_interrupt_handler(proc_priv->soc_dts);
331
332 return IRQ_HANDLED;
333}
334
335static int proc_thermal_pci_probe(struct pci_dev *pdev,
336 const struct pci_device_id *unused)
337{
338 struct proc_thermal_device *proc_priv;
339 int ret;
340
341 if (proc_thermal_emum_mode == PROC_THERMAL_PLATFORM_DEV) {
342 dev_err(&pdev->dev, "error: enumerated as platform dev\n");
343 return -ENODEV;
344 }
345
346 ret = pci_enable_device(pdev);
347 if (ret < 0) {
348 dev_err(&pdev->dev, "error: could not enable device\n");
349 return ret;
350 }
351
352 ret = proc_thermal_add(&pdev->dev, &proc_priv);
353 if (ret) {
354 pci_disable_device(pdev);
355 return ret;
356 }
357
358 pci_set_drvdata(pdev, proc_priv);
359 proc_thermal_emum_mode = PROC_THERMAL_PCI;
360
361 if (pdev->device == PCI_DEVICE_ID_PROC_BSW_THERMAL) {
362
363
364
365
366
367
368
369 proc_priv->soc_dts = intel_soc_dts_iosf_init(
370 INTEL_SOC_DTS_INTERRUPT_MSI, 2, 0);
371
372 if (proc_priv->soc_dts && pdev->irq) {
373 ret = pci_enable_msi(pdev);
374 if (!ret) {
375 ret = request_threaded_irq(pdev->irq, NULL,
376 proc_thermal_pci_msi_irq,
377 IRQF_ONESHOT, "proc_thermal",
378 pdev);
379 if (ret) {
380 intel_soc_dts_iosf_exit(
381 proc_priv->soc_dts);
382 pci_disable_msi(pdev);
383 proc_priv->soc_dts = NULL;
384 }
385 }
386 } else
387 dev_err(&pdev->dev, "No auxiliary DTSs enabled\n");
388 }
389
390 return 0;
391}
392
393static void proc_thermal_pci_remove(struct pci_dev *pdev)
394{
395 struct proc_thermal_device *proc_priv = pci_get_drvdata(pdev);
396
397 if (proc_priv->soc_dts) {
398 intel_soc_dts_iosf_exit(proc_priv->soc_dts);
399 if (pdev->irq) {
400 free_irq(pdev->irq, pdev);
401 pci_disable_msi(pdev);
402 }
403 }
404 proc_thermal_remove(proc_priv);
405 pci_disable_device(pdev);
406}
407
408static const struct pci_device_id proc_thermal_pci_ids[] = {
409 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BDW_THERMAL)},
410 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_HSB_THERMAL)},
411 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_SKL_THERMAL)},
412 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BSW_THERMAL)},
413 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXT0_THERMAL)},
414 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXT1_THERMAL)},
415 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXTX_THERMAL)},
416 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXTP_THERMAL)},
417 { 0, },
418};
419
420MODULE_DEVICE_TABLE(pci, proc_thermal_pci_ids);
421
422static struct pci_driver proc_thermal_pci_driver = {
423 .name = "proc_thermal",
424 .probe = proc_thermal_pci_probe,
425 .remove = proc_thermal_pci_remove,
426 .id_table = proc_thermal_pci_ids,
427};
428
429static const struct acpi_device_id int3401_device_ids[] = {
430 {"INT3401", 0},
431 {"", 0},
432};
433MODULE_DEVICE_TABLE(acpi, int3401_device_ids);
434
435static struct platform_driver int3401_driver = {
436 .probe = int3401_add,
437 .remove = int3401_remove,
438 .driver = {
439 .name = "int3401 thermal",
440 .acpi_match_table = int3401_device_ids,
441 },
442};
443
444static int __init proc_thermal_init(void)
445{
446 int ret;
447
448 ret = platform_driver_register(&int3401_driver);
449 if (ret)
450 return ret;
451
452 ret = pci_register_driver(&proc_thermal_pci_driver);
453
454 return ret;
455}
456
457static void __exit proc_thermal_exit(void)
458{
459 platform_driver_unregister(&int3401_driver);
460 pci_unregister_driver(&proc_thermal_pci_driver);
461}
462
463module_init(proc_thermal_init);
464module_exit(proc_thermal_exit);
465
466MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
467MODULE_DESCRIPTION("Processor Thermal Reporting Device Driver");
468MODULE_LICENSE("GPL v2");
469