coretemp.c 20.8 KB
Newer Older
R
Rudolf Marek 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * coretemp.c - Linux kernel module for hardware monitoring
 *
 * Copyright (C) 2007 Rudolf Marek <r.marek@assembler.cz>
 *
 * Inspired from many hwmon drivers
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 */

23 24
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

R
Rudolf Marek 已提交
25 26 27 28 29 30 31 32 33 34 35 36
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/jiffies.h>
#include <linux/hwmon.h>
#include <linux/sysfs.h>
#include <linux/hwmon-sysfs.h>
#include <linux/err.h>
#include <linux/mutex.h>
#include <linux/list.h>
#include <linux/platform_device.h>
#include <linux/cpu.h>
37
#include <linux/smp.h>
38
#include <linux/moduleparam.h>
39
#include <linux/pci.h>
R
Rudolf Marek 已提交
40 41
#include <asm/msr.h>
#include <asm/processor.h>
42
#include <asm/cpu_device_id.h>
R
Rudolf Marek 已提交
43 44 45

#define DRVNAME	"coretemp"

46 47 48 49 50 51 52 53
/*
 * force_tjmax only matters when TjMax can't be read from the CPU itself.
 * When set, it replaces the driver's suboptimal heuristic.
 */
static int force_tjmax;
module_param_named(tjmax, force_tjmax, int, 0444);
MODULE_PARM_DESC(tjmax, "TjMax value in degrees Celsius");

54
#define PKG_SYSFS_ATTR_NO	1	/* Sysfs attribute for package temp */
55
#define BASE_SYSFS_ATTR_NO	2	/* Sysfs Base attr no for coretemp */
56
#define NUM_REAL_CORES		128	/* Number of Real cores per cpu */
57
#define CORETEMP_NAME_LENGTH	19	/* String Length of attrs */
58
#define MAX_CORE_ATTRS		4	/* Maximum no of basic attrs */
59
#define TOTAL_ATTRS		(MAX_CORE_ATTRS + 1)
60 61
#define MAX_CORE_DATA		(NUM_REAL_CORES + BASE_SYSFS_ATTR_NO)

62
#define TO_CORE_ID(cpu)		(cpu_data(cpu).cpu_core_id)
63 64 65
#define TO_ATTR_NO(cpu)		(TO_CORE_ID(cpu) + BASE_SYSFS_ATTR_NO)

#ifdef CONFIG_SMP
66 67
#define for_each_sibling(i, cpu) \
	for_each_cpu(i, topology_sibling_cpumask(cpu))
68
#else
69
#define for_each_sibling(i, cpu)	for (i = 0; false; )
70
#endif
R
Rudolf Marek 已提交
71 72

/*
73 74 75 76 77 78 79
 * Per-Core Temperature Data
 * @last_updated: The time when the current temperature value was updated
 *		earlier (in jiffies).
 * @cpu_core_id: The CPU Core from which temperature values should be read
 *		This value is passed as "id" field to rdmsr/wrmsr functions.
 * @status_reg: One of IA32_THERM_STATUS or IA32_PACKAGE_THERM_STATUS,
 *		from where the temperature values should be read.
80
 * @attr_size:  Total number of pre-core attrs displayed in the sysfs.
81 82 83
 * @is_pkg_data: If this is 1, the temp_data holds pkgtemp data.
 *		Otherwise, temp_data holds coretemp data.
 * @valid: If this is 1, the current temperature is valid.
R
Rudolf Marek 已提交
84
 */
85
struct temp_data {
R
Rudolf Marek 已提交
86
	int temp;
87
	int ttarget;
88 89 90 91 92
	int tjmax;
	unsigned long last_updated;
	unsigned int cpu;
	u32 cpu_core_id;
	u32 status_reg;
93
	int attr_size;
94 95
	bool is_pkg_data;
	bool valid;
96 97
	struct sensor_device_attribute sd_attrs[TOTAL_ATTRS];
	char attr_name[TOTAL_ATTRS][CORETEMP_NAME_LENGTH];
98 99
	struct attribute *attrs[TOTAL_ATTRS + 1];
	struct attribute_group attr_group;
100
	struct mutex update_lock;
R
Rudolf Marek 已提交
101 102
};

103 104
/* Platform Data per Physical CPU */
struct platform_data {
105
	struct device		*hwmon_dev;
106
	u16			pkg_id;
107 108
	struct cpumask		cpumask;
	struct temp_data	*core_data[MAX_CORE_DATA];
109 110
	struct device_attribute name_attr;
};
R
Rudolf Marek 已提交
111

112 113 114 115
/* Keep track of how many package pointers we allocated in init() */
static int max_packages __read_mostly;
/* Array of package pointers. Serialized by cpu hotplug lock */
static struct platform_device **pkg_devices;
116 117 118

static ssize_t show_label(struct device *dev,
				struct device_attribute *devattr, char *buf)
R
Rudolf Marek 已提交
119 120
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
121 122 123 124
	struct platform_data *pdata = dev_get_drvdata(dev);
	struct temp_data *tdata = pdata->core_data[attr->index];

	if (tdata->is_pkg_data)
125
		return sprintf(buf, "Package id %u\n", pdata->pkg_id);
R
Rudolf Marek 已提交
126

127
	return sprintf(buf, "Core %u\n", tdata->cpu_core_id);
R
Rudolf Marek 已提交
128 129
}

130 131
static ssize_t show_crit_alarm(struct device *dev,
				struct device_attribute *devattr, char *buf)
R
Rudolf Marek 已提交
132
{
133 134 135 136 137
	u32 eax, edx;
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	struct platform_data *pdata = dev_get_drvdata(dev);
	struct temp_data *tdata = pdata->core_data[attr->index];

138
	mutex_lock(&tdata->update_lock);
139
	rdmsr_on_cpu(tdata->cpu, tdata->status_reg, &eax, &edx);
140
	mutex_unlock(&tdata->update_lock);
141 142

	return sprintf(buf, "%d\n", (eax >> 5) & 1);
R
Rudolf Marek 已提交
143 144
}

145 146
static ssize_t show_tjmax(struct device *dev,
			struct device_attribute *devattr, char *buf)
R
Rudolf Marek 已提交
147 148
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
149
	struct platform_data *pdata = dev_get_drvdata(dev);
R
Rudolf Marek 已提交
150

151
	return sprintf(buf, "%d\n", pdata->core_data[attr->index]->tjmax);
R
Rudolf Marek 已提交
152 153
}

154 155 156 157 158
static ssize_t show_ttarget(struct device *dev,
				struct device_attribute *devattr, char *buf)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	struct platform_data *pdata = dev_get_drvdata(dev);
R
Rudolf Marek 已提交
159

160 161
	return sprintf(buf, "%d\n", pdata->core_data[attr->index]->ttarget);
}
R
Rudolf Marek 已提交
162

163 164
static ssize_t show_temp(struct device *dev,
			struct device_attribute *devattr, char *buf)
R
Rudolf Marek 已提交
165
{
166 167 168 169
	u32 eax, edx;
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	struct platform_data *pdata = dev_get_drvdata(dev);
	struct temp_data *tdata = pdata->core_data[attr->index];
R
Rudolf Marek 已提交
170

171
	mutex_lock(&tdata->update_lock);
R
Rudolf Marek 已提交
172

173 174 175
	/* Check whether the time interval has elapsed */
	if (!tdata->valid || time_after(jiffies, tdata->last_updated + HZ)) {
		rdmsr_on_cpu(tdata->cpu, tdata->status_reg, &eax, &edx);
176 177 178 179 180 181 182 183
		/*
		 * Ignore the valid bit. In all observed cases the register
		 * value is either low or zero if the valid bit is 0.
		 * Return it instead of reporting an error which doesn't
		 * really help at all.
		 */
		tdata->temp = tdata->tjmax - ((eax >> 16) & 0x7f) * 1000;
		tdata->valid = 1;
184
		tdata->last_updated = jiffies;
R
Rudolf Marek 已提交
185 186
	}

187
	mutex_unlock(&tdata->update_lock);
188
	return sprintf(buf, "%d\n", tdata->temp);
R
Rudolf Marek 已提交
189 190
}

191 192 193 194 195 196
struct tjmax_pci {
	unsigned int device;
	int tjmax;
};

static const struct tjmax_pci tjmax_pci_table[] = {
197
	{ 0x0708, 110000 },	/* CE41x0 (Sodaville ) */
198 199 200 201 202
	{ 0x0c72, 102000 },	/* Atom S1240 (Centerton) */
	{ 0x0c73, 95000 },	/* Atom S1220 (Centerton) */
	{ 0x0c75, 95000 },	/* Atom S1260 (Centerton) */
};

203 204 205 206 207
struct tjmax {
	char const *id;
	int tjmax;
};

208
static const struct tjmax tjmax_table[] = {
209 210
	{ "CPU  230", 100000 },		/* Model 0x1c, stepping 2	*/
	{ "CPU  330", 125000 },		/* Model 0x1c, stepping 2	*/
211 212
};

213 214 215 216 217 218 219 220
struct tjmax_model {
	u8 model;
	u8 mask;
	int tjmax;
};

#define ANY 0xff

221
static const struct tjmax_model tjmax_model_table[] = {
222
	{ 0x1c, 10, 100000 },	/* D4xx, K4xx, N4xx, D5xx, K5xx, N5xx */
223 224 225 226 227 228 229 230 231
	{ 0x1c, ANY, 90000 },	/* Z5xx, N2xx, possibly others
				 * Note: Also matches 230 and 330,
				 * which are covered by tjmax_table
				 */
	{ 0x26, ANY, 90000 },	/* Atom Tunnel Creek (Exx), Lincroft (Z6xx)
				 * Note: TjMax for E6xxT is 110C, but CPU type
				 * is undetectable by software
				 */
	{ 0x27, ANY, 90000 },	/* Atom Medfield (Z2460) */
232 233 234 235 236
	{ 0x35, ANY, 90000 },	/* Atom Clover Trail/Cloverview (Z27x0) */
	{ 0x36, ANY, 100000 },	/* Atom Cedar Trail/Cedarview (N2xxx, D2xxx)
				 * Also matches S12x0 (stepping 9), covered by
				 * PCI table
				 */
237 238
};

239
static int adjust_tjmax(struct cpuinfo_x86 *c, u32 id, struct device *dev)
240 241 242 243
{
	/* The 100C is default for both mobile and non mobile CPUs */

	int tjmax = 100000;
244
	int tjmax_ee = 85000;
245
	int usemsr_ee = 1;
246 247
	int err;
	u32 eax, edx;
248
	int i;
249 250 251 252 253 254 255 256 257 258 259 260 261
	struct pci_dev *host_bridge = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));

	/*
	 * Explicit tjmax table entries override heuristics.
	 * First try PCI host bridge IDs, followed by model ID strings
	 * and model/stepping information.
	 */
	if (host_bridge && host_bridge->vendor == PCI_VENDOR_ID_INTEL) {
		for (i = 0; i < ARRAY_SIZE(tjmax_pci_table); i++) {
			if (host_bridge->device == tjmax_pci_table[i].device)
				return tjmax_pci_table[i].tjmax;
		}
	}
262 263 264 265 266

	for (i = 0; i < ARRAY_SIZE(tjmax_table); i++) {
		if (strstr(c->x86_model_id, tjmax_table[i].id))
			return tjmax_table[i].tjmax;
	}
267

268 269 270 271 272
	for (i = 0; i < ARRAY_SIZE(tjmax_model_table); i++) {
		const struct tjmax_model *tm = &tjmax_model_table[i];
		if (c->x86_model == tm->model &&
		    (tm->mask == ANY || c->x86_mask == tm->mask))
			return tm->tjmax;
273
	}
274

275
	/* Early chips have no MSR for TjMax */
276

277
	if (c->x86_model == 0xf && c->x86_mask < 4)
278
		usemsr_ee = 0;
279

280
	if (c->x86_model > 0xe && usemsr_ee) {
281
		u8 platform_id;
282

283 284 285 286 287
		/*
		 * Now we can detect the mobile CPU using Intel provided table
		 * http://softwarecommunity.intel.com/Wiki/Mobility/720.htm
		 * For Core2 cores, check MSR 0x17, bit 28 1 = Mobile CPU
		 */
288 289 290 291 292
		err = rdmsr_safe_on_cpu(id, 0x17, &eax, &edx);
		if (err) {
			dev_warn(dev,
				 "Unable to access MSR 0x17, assuming desktop"
				 " CPU\n");
293
			usemsr_ee = 0;
294
		} else if (c->x86_model < 0x17 && !(eax & 0x10000000)) {
295 296 297 298 299
			/*
			 * Trust bit 28 up to Penryn, I could not find any
			 * documentation on that; if you happen to know
			 * someone at Intel please ask
			 */
300
			usemsr_ee = 0;
301 302 303 304
		} else {
			/* Platform ID bits 52:50 (EDX starts at bit 32) */
			platform_id = (edx >> 18) & 0x7;

305 306 307 308 309 310 311 312 313 314
			/*
			 * Mobile Penryn CPU seems to be platform ID 7 or 5
			 * (guesswork)
			 */
			if (c->x86_model == 0x17 &&
			    (platform_id == 5 || platform_id == 7)) {
				/*
				 * If MSR EE bit is set, set it to 90 degrees C,
				 * otherwise 105 degrees C
				 */
315 316 317
				tjmax_ee = 90000;
				tjmax = 105000;
			}
318 319 320
		}
	}

321
	if (usemsr_ee) {
322 323 324 325
		err = rdmsr_safe_on_cpu(id, 0xee, &eax, &edx);
		if (err) {
			dev_warn(dev,
				 "Unable to access MSR 0xEE, for Tjmax, left"
326
				 " at default\n");
327
		} else if (eax & 0x40000000) {
328
			tjmax = tjmax_ee;
329
		}
330
	} else if (tjmax == 100000) {
331 332 333 334
		/*
		 * If we don't use msr EE it means we are desktop CPU
		 * (with exeception of Atom)
		 */
335 336 337 338 339 340
		dev_warn(dev, "Using relative temperature scale!\n");
	}

	return tjmax;
}

341 342 343 344 345 346 347 348 349 350 351 352
static bool cpu_has_tjmax(struct cpuinfo_x86 *c)
{
	u8 model = c->x86_model;

	return model > 0xe &&
	       model != 0x1c &&
	       model != 0x26 &&
	       model != 0x27 &&
	       model != 0x35 &&
	       model != 0x36;
}

353
static int get_tjmax(struct cpuinfo_x86 *c, u32 id, struct device *dev)
354 355 356 357 358
{
	int err;
	u32 eax, edx;
	u32 val;

359 360 361 362
	/*
	 * A new feature of current Intel(R) processors, the
	 * IA32_TEMPERATURE_TARGET contains the TjMax value
	 */
363 364
	err = rdmsr_safe_on_cpu(id, MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
	if (err) {
365
		if (cpu_has_tjmax(c))
366
			dev_warn(dev, "Unable to read TjMax from CPU %u\n", id);
367
	} else {
368
		val = (eax >> 16) & 0xff;
369 370 371 372
		/*
		 * If the TjMax is not plausible, an assumption
		 * will be used
		 */
373
		if (val) {
374
			dev_dbg(dev, "TjMax is %d degrees C\n", val);
375 376 377 378
			return val * 1000;
		}
	}

379 380 381 382 383 384
	if (force_tjmax) {
		dev_notice(dev, "TjMax forced to %d degrees C by user\n",
			   force_tjmax);
		return force_tjmax * 1000;
	}

385 386
	/*
	 * An assumption is made for early CPUs and unreadable MSR.
387
	 * NOTE: the calculated value may not be correct.
388
	 */
389
	return adjust_tjmax(c, id, dev);
390 391
}

392 393
static int create_core_attrs(struct temp_data *tdata, struct device *dev,
			     int attr_no)
394
{
395
	int i;
396
	static ssize_t (*const rd_ptr[TOTAL_ATTRS]) (struct device *dev,
397
			struct device_attribute *devattr, char *buf) = {
398
			show_label, show_crit_alarm, show_temp, show_tjmax,
399
			show_ttarget };
400 401 402
	static const char *const suffixes[TOTAL_ATTRS] = {
		"label", "crit_alarm", "input", "crit", "max"
	};
403

404
	for (i = 0; i < tdata->attr_size; i++) {
405 406
		snprintf(tdata->attr_name[i], CORETEMP_NAME_LENGTH,
			 "temp%d_%s", attr_no, suffixes[i]);
407
		sysfs_attr_init(&tdata->sd_attrs[i].dev_attr.attr);
408 409 410 411
		tdata->sd_attrs[i].dev_attr.attr.name = tdata->attr_name[i];
		tdata->sd_attrs[i].dev_attr.attr.mode = S_IRUGO;
		tdata->sd_attrs[i].dev_attr.show = rd_ptr[i];
		tdata->sd_attrs[i].index = attr_no;
412
		tdata->attrs[i] = &tdata->sd_attrs[i].dev_attr.attr;
R
Rudolf Marek 已提交
413
	}
414 415
	tdata->attr_group.attrs = tdata->attrs;
	return sysfs_create_group(&dev->kobj, &tdata->attr_group);
416 417 418
}


419
static int chk_ucode_version(unsigned int cpu)
420
{
421
	struct cpuinfo_x86 *c = &cpu_data(cpu);
422

423 424 425 426 427
	/*
	 * Check if we have problem with errata AE18 of Core processors:
	 * Readings might stop update when processor visited too deep sleep,
	 * fixed for stepping D0 (6EC).
	 */
428
	if (c->x86_model == 0xe && c->x86_mask < 0xc && c->microcode < 0x39) {
429
		pr_err("Errata AE18 not fixed, update BIOS or microcode of the CPU!\n");
430
		return -ENODEV;
431
	}
432 433 434
	return 0;
}

435
static struct platform_device *coretemp_get_pdev(unsigned int cpu)
436
{
437
	int pkgid = topology_logical_package_id(cpu);
438

439 440
	if (pkgid >= 0 && pkgid < max_packages)
		return pkg_devices[pkgid];
441 442 443
	return NULL;
}

444
static struct temp_data *init_temp_data(unsigned int cpu, int pkg_flag)
445 446 447 448 449 450 451 452 453 454 455 456
{
	struct temp_data *tdata;

	tdata = kzalloc(sizeof(struct temp_data), GFP_KERNEL);
	if (!tdata)
		return NULL;

	tdata->status_reg = pkg_flag ? MSR_IA32_PACKAGE_THERM_STATUS :
							MSR_IA32_THERM_STATUS;
	tdata->is_pkg_data = pkg_flag;
	tdata->cpu = cpu;
	tdata->cpu_core_id = TO_CORE_ID(cpu);
457
	tdata->attr_size = MAX_CORE_ATTRS;
458 459 460
	mutex_init(&tdata->update_lock);
	return tdata;
}
461

462 463
static int create_core_data(struct platform_device *pdev, unsigned int cpu,
			    int pkg_flag)
464 465
{
	struct temp_data *tdata;
466
	struct platform_data *pdata = platform_get_drvdata(pdev);
467 468 469
	struct cpuinfo_x86 *c = &cpu_data(cpu);
	u32 eax, edx;
	int err, attr_no;
R
Rudolf Marek 已提交
470

471
	/*
472 473 474 475
	 * Find attr number for sysfs:
	 * We map the attr number to core id of the CPU
	 * The attr number is always core id + 2
	 * The Pkgtemp will always show up as temp1_*, if available
476
	 */
477
	attr_no = pkg_flag ? PKG_SYSFS_ATTR_NO : TO_ATTR_NO(cpu);
478

479 480 481 482 483 484
	if (attr_no > MAX_CORE_DATA - 1)
		return -ERANGE;

	tdata = init_temp_data(cpu, pkg_flag);
	if (!tdata)
		return -ENOMEM;
R
Rudolf Marek 已提交
485

486 487 488 489 490 491
	/* Test if we can access the status register */
	err = rdmsr_safe_on_cpu(cpu, tdata->status_reg, &eax, &edx);
	if (err)
		goto exit_free;

	/* We can access status register. Get Critical Temperature */
492
	tdata->tjmax = get_tjmax(c, cpu, &pdev->dev);
493

494
	/*
495 496 497
	 * Read the still undocumented bits 8:15 of IA32_TEMPERATURE_TARGET.
	 * The target temperature is available on older CPUs but not in this
	 * register. Atoms don't have the register at all.
498
	 */
499 500 501 502 503 504 505 506
	if (c->x86_model > 0xe && c->x86_model != 0x1c) {
		err = rdmsr_safe_on_cpu(cpu, MSR_IA32_TEMPERATURE_TARGET,
					&eax, &edx);
		if (!err) {
			tdata->ttarget
			  = tdata->tjmax - ((eax >> 8) & 0xff) * 1000;
			tdata->attr_size++;
		}
507 508
	}

509 510 511
	pdata->core_data[attr_no] = tdata;

	/* Create sysfs interfaces */
512
	err = create_core_attrs(tdata, pdata->hwmon_dev, attr_no);
513 514
	if (err)
		goto exit_free;
R
Rudolf Marek 已提交
515 516

	return 0;
517
exit_free:
518
	pdata->core_data[attr_no] = NULL;
519 520 521 522
	kfree(tdata);
	return err;
}

523 524
static void
coretemp_add_core(struct platform_device *pdev, unsigned int cpu, int pkg_flag)
525
{
526
	if (create_core_data(pdev, cpu, pkg_flag))
527 528 529
		dev_err(&pdev->dev, "Adding Core %u failed\n", cpu);
}

530
static void coretemp_remove_core(struct platform_data *pdata, int indx)
531 532 533 534
{
	struct temp_data *tdata = pdata->core_data[indx];

	/* Remove the sysfs attributes */
535
	sysfs_remove_group(&pdata->hwmon_dev->kobj, &tdata->attr_group);
536 537 538 539 540

	kfree(pdata->core_data[indx]);
	pdata->core_data[indx] = NULL;
}

B
Bill Pemberton 已提交
541
static int coretemp_probe(struct platform_device *pdev)
542
{
543
	struct device *dev = &pdev->dev;
544
	struct platform_data *pdata;
R
Rudolf Marek 已提交
545

546
	/* Initialize the per-package data structures */
547
	pdata = devm_kzalloc(dev, sizeof(struct platform_data), GFP_KERNEL);
548 549 550
	if (!pdata)
		return -ENOMEM;

551
	pdata->pkg_id = pdev->id;
552 553
	platform_set_drvdata(pdev, pdata);

554 555 556
	pdata->hwmon_dev = devm_hwmon_device_register_with_groups(dev, DRVNAME,
								  pdata, NULL);
	return PTR_ERR_OR_ZERO(pdata->hwmon_dev);
R
Rudolf Marek 已提交
557 558
}

B
Bill Pemberton 已提交
559
static int coretemp_remove(struct platform_device *pdev)
R
Rudolf Marek 已提交
560
{
561 562
	struct platform_data *pdata = platform_get_drvdata(pdev);
	int i;
R
Rudolf Marek 已提交
563

564 565
	for (i = MAX_CORE_DATA - 1; i >= 0; --i)
		if (pdata->core_data[i])
566
			coretemp_remove_core(pdata, i);
567

R
Rudolf Marek 已提交
568 569 570 571 572 573 574 575
	return 0;
}

static struct platform_driver coretemp_driver = {
	.driver = {
		.name = DRVNAME,
	},
	.probe = coretemp_probe,
B
Bill Pemberton 已提交
576
	.remove = coretemp_remove,
R
Rudolf Marek 已提交
577 578
};

579
static struct platform_device *coretemp_device_add(unsigned int cpu)
R
Rudolf Marek 已提交
580
{
581
	int err, pkgid = topology_logical_package_id(cpu);
R
Rudolf Marek 已提交
582
	struct platform_device *pdev;
583

584 585
	if (pkgid < 0)
		return ERR_PTR(-ENOMEM);
586

587 588 589
	pdev = platform_device_alloc(DRVNAME, pkgid);
	if (!pdev)
		return ERR_PTR(-ENOMEM);
R
Rudolf Marek 已提交
590 591 592

	err = platform_device_add(pdev);
	if (err) {
593 594
		platform_device_put(pdev);
		return ERR_PTR(err);
R
Rudolf Marek 已提交
595 596
	}

597 598
	pkg_devices[pkgid] = pdev;
	return pdev;
R
Rudolf Marek 已提交
599 600
}

601
static int coretemp_cpu_online(unsigned int cpu)
602 603
{
	struct platform_device *pdev = coretemp_get_pdev(cpu);
604 605
	struct cpuinfo_x86 *c = &cpu_data(cpu);
	struct platform_data *pdata;
606

607 608 609 610 611 612 613
	/*
	 * Don't execute this on resume as the offline callback did
	 * not get executed on suspend.
	 */
	if (cpuhp_tasks_frozen)
		return 0;

614 615 616 617 618
	/*
	 * CPUID.06H.EAX[0] indicates whether the CPU has thermal
	 * sensors. We check this bit only, all the early CPUs
	 * without thermal sensors will be filtered out.
	 */
619
	if (!cpu_has(c, X86_FEATURE_DTHERM))
620
		return -ENODEV;
621 622

	if (!pdev) {
623 624
		/* Check the microcode version of the CPU */
		if (chk_ucode_version(cpu))
625
			return -EINVAL;
626

627 628 629 630 631 632
		/*
		 * Alright, we have DTS support.
		 * We are bringing the _first_ core in this pkg
		 * online. So, initialize per-pkg data structures and
		 * then bring this core online.
		 */
633 634 635
		pdev = coretemp_device_add(cpu);
		if (IS_ERR(pdev))
			return PTR_ERR(pdev);
636

637 638 639 640 641
		/*
		 * Check whether pkgtemp support is available.
		 * If so, add interfaces for pkgtemp.
		 */
		if (cpu_has(c, X86_FEATURE_PTS))
642
			coretemp_add_core(pdev, cpu, 1);
643
	}
644 645

	pdata = platform_get_drvdata(pdev);
646
	/*
647 648
	 * Check whether a thread sibling is already online. If not add the
	 * interface for this CPU core.
649
	 */
650
	if (!cpumask_intersects(&pdata->cpumask, topology_sibling_cpumask(cpu)))
651
		coretemp_add_core(pdev, cpu, 0);
652 653

	cpumask_set_cpu(cpu, &pdata->cpumask);
654
	return 0;
655 656
}

657
static int coretemp_cpu_offline(unsigned int cpu)
658 659
{
	struct platform_device *pdev = coretemp_get_pdev(cpu);
660
	struct platform_data *pd;
661
	struct temp_data *tdata;
662
	int indx, target;
663

664 665 666 667 668 669 670
	/*
	 * Don't execute this on suspend as the device remove locks
	 * up the machine.
	 */
	if (cpuhp_tasks_frozen)
		return 0;

671 672
	/* If the physical CPU device does not exist, just return */
	if (!pdev)
673
		return 0;
674

675
	/* The core id is too big, just return */
676
	indx = TO_ATTR_NO(cpu);
677
	if (indx > MAX_CORE_DATA - 1)
678
		return 0;
679

680 681 682 683
	pd = platform_get_drvdata(pdev);
	tdata = pd->core_data[indx];

	cpumask_clear_cpu(cpu, &pd->cpumask);
684

685
	/*
686 687 688
	 * If this is the last thread sibling, remove the CPU core
	 * interface, If there is still a sibling online, transfer the
	 * target cpu of that core interface to it.
689
	 */
690 691 692 693 694 695 696
	target = cpumask_any_and(&pd->cpumask, topology_sibling_cpumask(cpu));
	if (target >= nr_cpu_ids) {
		coretemp_remove_core(pd, indx);
	} else if (tdata && tdata->cpu == cpu) {
		mutex_lock(&tdata->update_lock);
		tdata->cpu = target;
		mutex_unlock(&tdata->update_lock);
697
	}
698

699
	/*
700 701 702
	 * If all cores in this pkg are offline, remove the device. This
	 * will invoke the platform driver remove function, which cleans up
	 * the rest.
703
	 */
704
	if (cpumask_empty(&pd->cpumask)) {
705 706
		pkg_devices[topology_logical_package_id(cpu)] = NULL;
		platform_device_unregister(pdev);
707
		return 0;
708
	}
709

710 711 712 713
	/*
	 * Check whether this core is the target for the package
	 * interface. We need to assign it to some other cpu.
	 */
714
	tdata = pd->core_data[PKG_SYSFS_ATTR_NO];
715
	if (tdata && tdata->cpu == cpu) {
716
		target = cpumask_first(&pd->cpumask);
717 718 719 720
		mutex_lock(&tdata->update_lock);
		tdata->cpu = target;
		mutex_unlock(&tdata->update_lock);
	}
721
	return 0;
722
}
723
static const struct x86_cpu_id __initconst coretemp_ids[] = {
724
	{ X86_VENDOR_INTEL, X86_FAMILY_ANY, X86_MODEL_ANY, X86_FEATURE_DTHERM },
725 726 727 728
	{}
};
MODULE_DEVICE_TABLE(x86cpu, coretemp_ids);

729 730
static enum cpuhp_state coretemp_hp_online;

R
Rudolf Marek 已提交
731 732
static int __init coretemp_init(void)
{
733
	int err;
R
Rudolf Marek 已提交
734

735 736 737 738 739 740 741
	/*
	 * CPUID.06H.EAX[0] indicates whether the CPU has thermal
	 * sensors. We check this bit only, all the early CPUs
	 * without thermal sensors will be filtered out.
	 */
	if (!x86_match_cpu(coretemp_ids))
		return -ENODEV;
R
Rudolf Marek 已提交
742

743 744 745 746 747 748
	max_packages = topology_max_packages();
	pkg_devices = kzalloc(max_packages * sizeof(struct platform_device *),
			      GFP_KERNEL);
	if (!pkg_devices)
		return -ENOMEM;

R
Rudolf Marek 已提交
749 750
	err = platform_driver_register(&coretemp_driver);
	if (err)
751
		return err;
R
Rudolf Marek 已提交
752

753 754 755
	err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "hwmon/coretemp:online",
				coretemp_cpu_online, coretemp_cpu_offline);
	if (err < 0)
756
		goto outdrv;
757
	coretemp_hp_online = err;
R
Rudolf Marek 已提交
758 759
	return 0;

760
outdrv:
R
Rudolf Marek 已提交
761
	platform_driver_unregister(&coretemp_driver);
762
	kfree(pkg_devices);
R
Rudolf Marek 已提交
763 764
	return err;
}
765
module_init(coretemp_init)
R
Rudolf Marek 已提交
766 767 768

static void __exit coretemp_exit(void)
{
769
	cpuhp_remove_state(coretemp_hp_online);
R
Rudolf Marek 已提交
770
	platform_driver_unregister(&coretemp_driver);
771
	kfree(pkg_devices);
R
Rudolf Marek 已提交
772
}
773
module_exit(coretemp_exit)
R
Rudolf Marek 已提交
774 775 776 777

MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
MODULE_DESCRIPTION("Intel Core temperature monitor");
MODULE_LICENSE("GPL");