coretemp.c 21.1 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/pci.h>
R
Rudolf Marek 已提交
38 39
#include <asm/msr.h>
#include <asm/processor.h>
40
#include <asm/smp.h>
R
Rudolf Marek 已提交
41 42 43

#define DRVNAME	"coretemp"

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#define BASE_SYSFS_ATTR_NO	2	/* Sysfs Base attr no for coretemp */
#define NUM_REAL_CORES		16	/* Number of Real cores per cpu */
#define CORETEMP_NAME_LENGTH	17	/* String Length of attrs */
#define MAX_ATTRS		5	/* Maximum no of per-core attrs */
#define MAX_CORE_DATA		(NUM_REAL_CORES + BASE_SYSFS_ATTR_NO)

#ifdef CONFIG_SMP
#define TO_PHYS_ID(cpu)		cpu_data(cpu).phys_proc_id
#define TO_CORE_ID(cpu)		cpu_data(cpu).cpu_core_id
#define TO_ATTR_NO(cpu)		(TO_CORE_ID(cpu) + BASE_SYSFS_ATTR_NO)
#else
#define TO_PHYS_ID(cpu)		(cpu)
#define TO_CORE_ID(cpu)		(cpu)
#define TO_ATTR_NO(cpu)		(cpu)
#endif
R
Rudolf Marek 已提交
59 60

/*
61 62 63 64 65 66 67 68 69 70
 * 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.
 * @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 已提交
71
 */
72
struct temp_data {
R
Rudolf Marek 已提交
73
	int temp;
74
	int ttarget;
75 76 77 78 79 80 81 82 83 84
	int tjmax;
	unsigned long last_updated;
	unsigned int cpu;
	u32 cpu_core_id;
	u32 status_reg;
	bool is_pkg_data;
	bool valid;
	struct sensor_device_attribute sd_attrs[MAX_ATTRS];
	char attr_name[MAX_ATTRS][CORETEMP_NAME_LENGTH];
	struct mutex update_lock;
R
Rudolf Marek 已提交
85 86
};

87 88 89 90 91 92 93
/* Platform Data per Physical CPU */
struct platform_data {
	struct device *hwmon_dev;
	u16 phys_proc_id;
	struct temp_data *core_data[MAX_CORE_DATA];
	struct device_attribute name_attr;
};
R
Rudolf Marek 已提交
94

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
struct pdev_entry {
	struct list_head list;
	struct platform_device *pdev;
	unsigned int cpu;
	u16 phys_proc_id;
	u16 cpu_core_id;
};

static LIST_HEAD(pdev_list);
static DEFINE_MUTEX(pdev_list_mutex);

static ssize_t show_name(struct device *dev,
			struct device_attribute *devattr, char *buf)
{
	return sprintf(buf, "%s\n", DRVNAME);
}

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

	if (tdata->is_pkg_data)
		return sprintf(buf, "Physical id %u\n", pdata->phys_proc_id);
R
Rudolf Marek 已提交
121

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

125 126
static ssize_t show_crit_alarm(struct device *dev,
				struct device_attribute *devattr, char *buf)
R
Rudolf Marek 已提交
127
{
128 129 130 131 132 133 134 135
	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];

	rdmsr_on_cpu(tdata->cpu, tdata->status_reg, &eax, &edx);

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

138 139
static ssize_t show_tjmax(struct device *dev,
			struct device_attribute *devattr, char *buf)
R
Rudolf Marek 已提交
140 141
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
142
	struct platform_data *pdata = dev_get_drvdata(dev);
R
Rudolf Marek 已提交
143

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

147 148 149 150 151
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 已提交
152

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

156 157
static ssize_t show_temp(struct device *dev,
			struct device_attribute *devattr, char *buf)
R
Rudolf Marek 已提交
158
{
159 160 161 162
	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 已提交
163

164
	mutex_lock(&tdata->update_lock);
R
Rudolf Marek 已提交
165

166 167 168 169 170
	/* 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);
		tdata->valid = 0;
		/* Check whether the data is valid */
R
Rudolf Marek 已提交
171
		if (eax & 0x80000000) {
172 173 174
			tdata->temp = tdata->tjmax -
					(((eax >> 16) & 0x7f) * 1000);
			tdata->valid = 1;
R
Rudolf Marek 已提交
175
		}
176
		tdata->last_updated = jiffies;
R
Rudolf Marek 已提交
177 178
	}

179 180
	mutex_unlock(&tdata->update_lock);
	return tdata->valid ? sprintf(buf, "%d\n", tdata->temp) : -EAGAIN;
R
Rudolf Marek 已提交
181 182
}

183
static int adjust_tjmax(struct cpuinfo_x86 *c, u32 id, struct device *dev)
184 185 186 187
{
	/* The 100C is default for both mobile and non mobile CPUs */

	int tjmax = 100000;
188
	int tjmax_ee = 85000;
189
	int usemsr_ee = 1;
190 191
	int err;
	u32 eax, edx;
192
	struct pci_dev *host_bridge;
193 194 195 196

	/* Early chips have no MSR for TjMax */

	if ((c->x86_model == 0xf) && (c->x86_mask < 4)) {
197
		usemsr_ee = 0;
198 199
	}

200
	/* Atom CPUs */
201 202 203

	if (c->x86_model == 0x1c) {
		usemsr_ee = 0;
204 205 206 207 208 209 210 211 212 213 214

		host_bridge = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));

		if (host_bridge && host_bridge->vendor == PCI_VENDOR_ID_INTEL
		    && (host_bridge->device == 0xa000	/* NM10 based nettop */
		    || host_bridge->device == 0xa010))	/* NM10 based netbook */
			tjmax = 100000;
		else
			tjmax = 90000;

		pci_dev_put(host_bridge);
215 216 217
	}

	if ((c->x86_model > 0xe) && (usemsr_ee)) {
218
		u8 platform_id;
219 220 221 222 223 224 225 226 227 228 229

		/* 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
		*/

		err = rdmsr_safe_on_cpu(id, 0x17, &eax, &edx);
		if (err) {
			dev_warn(dev,
				 "Unable to access MSR 0x17, assuming desktop"
				 " CPU\n");
230
			usemsr_ee = 0;
231 232 233 234
		} else if (c->x86_model < 0x17 && !(eax & 0x10000000)) {
			/* Trust bit 28 up to Penryn, I could not find any
			   documentation on that; if you happen to know
			   someone at Intel please ask */
235
			usemsr_ee = 0;
236 237 238 239 240 241 242 243 244 245 246 247 248
		} else {
			/* Platform ID bits 52:50 (EDX starts at bit 32) */
			platform_id = (edx >> 18) & 0x7;

			/* 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 */
				tjmax_ee = 90000;
				tjmax = 105000;
			}
249 250 251
		}
	}

252
	if (usemsr_ee) {
253 254 255 256 257

		err = rdmsr_safe_on_cpu(id, 0xee, &eax, &edx);
		if (err) {
			dev_warn(dev,
				 "Unable to access MSR 0xEE, for Tjmax, left"
258
				 " at default\n");
259
		} else if (eax & 0x40000000) {
260
			tjmax = tjmax_ee;
261
		}
262 263 264
	/* if we dont use msr EE it means we are desktop CPU (with exeception
	   of Atom) */
	} else if (tjmax == 100000) {
265 266 267 268 269 270
		dev_warn(dev, "Using relative temperature scale!\n");
	}

	return tjmax;
}

271
static int get_tjmax(struct cpuinfo_x86 *c, u32 id, struct device *dev)
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
{
	/* The 100C is default for both mobile and non mobile CPUs */
	int err;
	u32 eax, edx;
	u32 val;

	/* A new feature of current Intel(R) processors, the
	   IA32_TEMPERATURE_TARGET contains the TjMax value */
	err = rdmsr_safe_on_cpu(id, MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
	if (err) {
		dev_warn(dev, "Unable to read TjMax from CPU.\n");
	} else {
		val = (eax >> 16) & 0xff;
		/*
		 * If the TjMax is not plausible, an assumption
		 * will be used
		 */
		if ((val > 80) && (val < 120)) {
			dev_info(dev, "TjMax is %d C.\n", val);
			return val * 1000;
		}
	}

	/*
	 * An assumption is made for early CPUs and unreadable MSR.
	 * NOTE: the given value may not be correct.
	 */

	switch (c->x86_model) {
	case 0xe:
	case 0xf:
	case 0x16:
	case 0x1a:
		dev_warn(dev, "TjMax is assumed as 100 C!\n");
		return 100000;
	case 0x17:
	case 0x1c:		/* Atom CPUs */
		return adjust_tjmax(c, id, dev);
	default:
		dev_warn(dev, "CPU (model=0x%x) is not supported yet,"
			" using default TjMax of 100C.\n", c->x86_model);
		return 100000;
	}
}

317 318 319 320 321 322 323 324 325
static void __devinit get_ucode_rev_on_cpu(void *edx)
{
	u32 eax;

	wrmsr(MSR_IA32_UCODE_REV, 0, 0);
	sync_core();
	rdmsr(MSR_IA32_UCODE_REV, eax, *(u32 *)edx);
}

326
static int get_pkg_tjmax(unsigned int cpu, struct device *dev)
R
Rudolf Marek 已提交
327 328
{
	int err;
329
	u32 eax, edx, val;
R
Rudolf Marek 已提交
330

331 332 333 334 335
	err = rdmsr_safe_on_cpu(cpu, MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
	if (!err) {
		val = (eax >> 16) & 0xff;
		if ((val > 80) && (val < 120))
			return val * 1000;
R
Rudolf Marek 已提交
336
	}
337 338 339
	dev_warn(dev, "Unable to read Pkg-TjMax from CPU:%u\n", cpu);
	return 100000; /* Default TjMax: 100 degree celsius */
}
R
Rudolf Marek 已提交
340

341 342 343 344 345 346 347
static int create_name_attr(struct platform_data *pdata, struct device *dev)
{
	pdata->name_attr.attr.name = "name";
	pdata->name_attr.attr.mode = S_IRUGO;
	pdata->name_attr.show = show_name;
	return device_create_file(dev, &pdata->name_attr);
}
R
Rudolf Marek 已提交
348

349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
static int create_core_attrs(struct temp_data *tdata, struct device *dev,
				int attr_no)
{
	int err, i;
	static ssize_t (*rd_ptr[MAX_ATTRS]) (struct device *dev,
			struct device_attribute *devattr, char *buf) = {
			show_label, show_crit_alarm, show_ttarget,
			show_temp, show_tjmax };
	static const char *names[MAX_ATTRS] = {
					"temp%d_label", "temp%d_crit_alarm",
					"temp%d_max", "temp%d_input",
					"temp%d_crit" };

	for (i = 0; i < MAX_ATTRS; i++) {
		snprintf(tdata->attr_name[i], CORETEMP_NAME_LENGTH, names[i],
			attr_no);
		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].dev_attr.store = NULL;
		tdata->sd_attrs[i].index = attr_no;
		err = device_create_file(dev, &tdata->sd_attrs[i].dev_attr);
		if (err)
			goto exit_free;
R
Rudolf Marek 已提交
373
	}
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
	return 0;

exit_free:
	while (--i >= 0)
		device_remove_file(dev, &tdata->sd_attrs[i].dev_attr);
	return err;
}

static void update_ttarget(__u8 cpu_model, struct temp_data *tdata,
				struct device *dev)
{
	int err;
	u32 eax, edx;

	/*
	 * Initialize ttarget value. Eventually this will be
	 * initialized with the value from MSR_IA32_THERM_INTERRUPT
	 * register. If IA32_TEMPERATURE_TARGET is supported, this
	 * value will be over written below.
	 * To Do: Patch to initialize ttarget from MSR_IA32_THERM_INTERRUPT
	 */
	tdata->ttarget = tdata->tjmax - 20000;
R
Rudolf Marek 已提交
396

397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
	/*
	 * Read the still undocumented IA32_TEMPERATURE_TARGET. It exists
	 * on older CPUs but not in this register,
	 * Atoms don't have it either.
	 */
	if ((cpu_model > 0xe) && (cpu_model != 0x1c)) {
		err = rdmsr_safe_on_cpu(tdata->cpu,
				MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
		if (err) {
			dev_warn(dev,
			"Unable to read IA32_TEMPERATURE_TARGET MSR\n");
		} else {
			tdata->ttarget = tdata->tjmax -
					(((eax >> 8) & 0xff) * 1000);
		}
	}
}

static int chk_ucode_version(struct platform_device *pdev)
{
	struct cpuinfo_x86 *c = &cpu_data(pdev->id);
	int err;
	u32 edx;
420

421 422 423 424 425
	/*
	 * 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).
	 */
426 427
	if ((c->x86_model == 0xe) && (c->x86_mask < 0xc)) {
		/* check for microcode update */
428
		err = smp_call_function_single(pdev->id, get_ucode_rev_on_cpu,
429 430 431 432
					       &edx, 1);
		if (err) {
			dev_err(&pdev->dev,
				"Cannot determine microcode revision of "
433 434
				"CPU#%u (%d)!\n", pdev->id, err);
			return -ENODEV;
435
		} else if (edx < 0x39) {
436 437 438
			dev_err(&pdev->dev,
				"Errata AE18 not fixed, update BIOS or "
				"microcode of the CPU!\n");
439
			return -ENODEV;
440 441
		}
	}
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
	return 0;
}

static struct platform_device *coretemp_get_pdev(unsigned int cpu)
{
	u16 phys_proc_id = TO_PHYS_ID(cpu);
	struct pdev_entry *p;

	mutex_lock(&pdev_list_mutex);

	list_for_each_entry(p, &pdev_list, list)
		if (p->phys_proc_id == phys_proc_id) {
			mutex_unlock(&pdev_list_mutex);
			return p->pdev;
		}

	mutex_unlock(&pdev_list_mutex);
	return NULL;
}

static struct temp_data *init_temp_data(unsigned int cpu, int pkg_flag)
{
	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);
	mutex_init(&tdata->update_lock);
	return tdata;
}
478

479 480 481 482 483 484 485 486
static int create_core_data(struct platform_data *pdata,
				struct platform_device *pdev,
				unsigned int cpu, int pkg_flag)
{
	struct temp_data *tdata;
	struct cpuinfo_x86 *c = &cpu_data(cpu);
	u32 eax, edx;
	int err, attr_no;
R
Rudolf Marek 已提交
487

488
	/*
489 490 491 492
	 * 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
493
	 */
494
	attr_no = pkg_flag ? 1 : TO_ATTR_NO(cpu);
495

496 497 498 499 500 501
	if (attr_no > MAX_CORE_DATA - 1)
		return -ERANGE;

	/* Skip if it is a HT core, Not an error */
	if (pdata->core_data[attr_no] != NULL)
		return 0;
502

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

507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
	/* 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 */
	if (pkg_flag)
		tdata->tjmax = get_pkg_tjmax(pdev->id, &pdev->dev);
	else
		tdata->tjmax = get_tjmax(c, cpu, &pdev->dev);

	update_ttarget(c->x86_model, tdata, &pdev->dev);
	pdata->core_data[attr_no] = tdata;

	/* Create sysfs interfaces */
	err = create_core_attrs(tdata, &pdev->dev, attr_no);
	if (err)
		goto exit_free;
R
Rudolf Marek 已提交
525 526

	return 0;
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
exit_free:
	kfree(tdata);
	return err;
}

static void coretemp_add_core(unsigned int cpu, int pkg_flag)
{
	struct platform_data *pdata;
	struct platform_device *pdev = coretemp_get_pdev(cpu);
	int err;

	if (!pdev)
		return;

	pdata = platform_get_drvdata(pdev);

	err = create_core_data(pdata, pdev, cpu, pkg_flag);
	if (err)
		dev_err(&pdev->dev, "Adding Core %u failed\n", cpu);
}

static void coretemp_remove_core(struct platform_data *pdata,
				struct device *dev, int indx)
{
	int i;
	struct temp_data *tdata = pdata->core_data[indx];

	/* Remove the sysfs attributes */
	for (i = 0; i < MAX_ATTRS; i++)
		device_remove_file(dev, &tdata->sd_attrs[i].dev_attr);

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

static int __devinit coretemp_probe(struct platform_device *pdev)
{
	struct platform_data *pdata;
	int err;
R
Rudolf Marek 已提交
566

567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
	/* Check the microcode version of the CPU */
	err = chk_ucode_version(pdev);
	if (err)
		return err;

	/* Initialize the per-package data structures */
	pdata = kzalloc(sizeof(struct platform_data), GFP_KERNEL);
	if (!pdata)
		return -ENOMEM;

	err = create_name_attr(pdata, &pdev->dev);
	if (err)
		goto exit_free;

	pdata->phys_proc_id = TO_PHYS_ID(pdev->id);
	platform_set_drvdata(pdev, pdata);

	pdata->hwmon_dev = hwmon_device_register(&pdev->dev);
	if (IS_ERR(pdata->hwmon_dev)) {
		err = PTR_ERR(pdata->hwmon_dev);
		dev_err(&pdev->dev, "Class registration failed (%d)\n", err);
		goto exit_name;
	}
	return 0;

exit_name:
	device_remove_file(&pdev->dev, &pdata->name_attr);
	platform_set_drvdata(pdev, NULL);
R
Rudolf Marek 已提交
595
exit_free:
596
	kfree(pdata);
R
Rudolf Marek 已提交
597 598 599 600 601
	return err;
}

static int __devexit coretemp_remove(struct platform_device *pdev)
{
602 603
	struct platform_data *pdata = platform_get_drvdata(pdev);
	int i;
R
Rudolf Marek 已提交
604

605 606 607 608 609 610
	for (i = MAX_CORE_DATA - 1; i >= 0; --i)
		if (pdata->core_data[i])
			coretemp_remove_core(pdata, &pdev->dev, i);

	device_remove_file(&pdev->dev, &pdata->name_attr);
	hwmon_device_unregister(pdata->hwmon_dev);
R
Rudolf Marek 已提交
611
	platform_set_drvdata(pdev, NULL);
612
	kfree(pdata);
R
Rudolf Marek 已提交
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
	return 0;
}

static struct platform_driver coretemp_driver = {
	.driver = {
		.owner = THIS_MODULE,
		.name = DRVNAME,
	},
	.probe = coretemp_probe,
	.remove = __devexit_p(coretemp_remove),
};

static int __cpuinit coretemp_device_add(unsigned int cpu)
{
	int err;
	struct platform_device *pdev;
	struct pdev_entry *pdev_entry;
630 631 632

	mutex_lock(&pdev_list_mutex);

R
Rudolf Marek 已提交
633 634 635
	pdev = platform_device_alloc(DRVNAME, cpu);
	if (!pdev) {
		err = -ENOMEM;
636
		pr_err("Device allocation failed\n");
R
Rudolf Marek 已提交
637 638 639 640 641 642 643 644 645 646 647
		goto exit;
	}

	pdev_entry = kzalloc(sizeof(struct pdev_entry), GFP_KERNEL);
	if (!pdev_entry) {
		err = -ENOMEM;
		goto exit_device_put;
	}

	err = platform_device_add(pdev);
	if (err) {
648
		pr_err("Device addition failed (%d)\n", err);
R
Rudolf Marek 已提交
649 650 651 652 653
		goto exit_device_free;
	}

	pdev_entry->pdev = pdev;
	pdev_entry->cpu = cpu;
654 655 656
	pdev_entry->phys_proc_id = TO_PHYS_ID(cpu);
	pdev_entry->cpu_core_id = TO_CORE_ID(cpu);

R
Rudolf Marek 已提交
657 658 659 660 661 662 663 664 665 666
	list_add_tail(&pdev_entry->list, &pdev_list);
	mutex_unlock(&pdev_list_mutex);

	return 0;

exit_device_free:
	kfree(pdev_entry);
exit_device_put:
	platform_device_put(pdev);
exit:
667
	mutex_unlock(&pdev_list_mutex);
R
Rudolf Marek 已提交
668 669 670
	return err;
}

671
static void coretemp_device_remove(unsigned int cpu)
R
Rudolf Marek 已提交
672
{
673 674
	struct pdev_entry *p, *n;
	u16 phys_proc_id = TO_PHYS_ID(cpu);
675

R
Rudolf Marek 已提交
676
	mutex_lock(&pdev_list_mutex);
677 678
	list_for_each_entry_safe(p, n, &pdev_list, list) {
		if (p->phys_proc_id != phys_proc_id)
679 680 681 682
			continue;
		platform_device_unregister(p->pdev);
		list_del(&p->list);
		kfree(p);
R
Rudolf Marek 已提交
683 684 685 686
	}
	mutex_unlock(&pdev_list_mutex);
}

687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
static bool is_any_core_online(struct platform_data *pdata)
{
	int i;

	/* Find online cores, except pkgtemp data */
	for (i = MAX_CORE_DATA - 1; i >= 0; --i) {
		if (pdata->core_data[i] &&
			!pdata->core_data[i]->is_pkg_data) {
			return true;
		}
	}
	return false;
}

static void __cpuinit get_core_online(unsigned int cpu)
{
	struct cpuinfo_x86 *c = &cpu_data(cpu);
	struct platform_device *pdev = coretemp_get_pdev(cpu);
	int err;

	/*
	 * 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 (!cpu_has(c, X86_FEATURE_DTS))
		return;

	if (!pdev) {
		/*
		 * 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.
		 */
		err = coretemp_device_add(cpu);
		if (err)
			return;
		/*
		 * Check whether pkgtemp support is available.
		 * If so, add interfaces for pkgtemp.
		 */
		if (cpu_has(c, X86_FEATURE_PTS))
			coretemp_add_core(cpu, 1);
	}
	/*
	 * Physical CPU device already exists.
	 * So, just add interfaces for this core.
	 */
	coretemp_add_core(cpu, 0);
}

static void __cpuinit put_core_offline(unsigned int cpu)
{
	int i, indx;
	struct platform_data *pdata;
	struct platform_device *pdev = coretemp_get_pdev(cpu);

	/* If the physical CPU device does not exist, just return */
	if (!pdev)
		return;

	pdata = platform_get_drvdata(pdev);

	indx = TO_ATTR_NO(cpu);

	if (pdata->core_data[indx] && pdata->core_data[indx]->cpu == cpu)
		coretemp_remove_core(pdata, &pdev->dev, indx);

	/* Online the HT version of this core, if any */
	for_each_cpu(i, cpu_sibling_mask(cpu)) {
		if (i != cpu) {
			get_core_online(i);
			break;
		}
	}
	/*
	 * If all cores in this pkg are offline, remove the device.
	 * coretemp_device_remove calls unregister_platform_device,
	 * which in turn calls coretemp_remove. This removes the
	 * pkgtemp entry and does other clean ups.
	 */
	if (!is_any_core_online(pdata))
		coretemp_device_remove(cpu);
}

773
static int __cpuinit coretemp_cpu_callback(struct notifier_block *nfb,
R
Rudolf Marek 已提交
774 775 776 777 778 779
				 unsigned long action, void *hcpu)
{
	unsigned int cpu = (unsigned long) hcpu;

	switch (action) {
	case CPU_ONLINE:
R
Rafael J. Wysocki 已提交
780
	case CPU_DOWN_FAILED:
781
		get_core_online(cpu);
R
Rudolf Marek 已提交
782
		break;
R
Rafael J. Wysocki 已提交
783
	case CPU_DOWN_PREPARE:
784
		put_core_offline(cpu);
R
Rudolf Marek 已提交
785 786 787 788 789
		break;
	}
	return NOTIFY_OK;
}

790
static struct notifier_block coretemp_cpu_notifier __refdata = {
R
Rudolf Marek 已提交
791 792 793
	.notifier_call = coretemp_cpu_callback,
};

794

R
Rudolf Marek 已提交
795 796 797 798 799
static int __init coretemp_init(void)
{
	int i, err = -ENODEV;

	/* quick check if we run Intel */
800
	if (cpu_data(0).x86_vendor != X86_VENDOR_INTEL)
R
Rudolf Marek 已提交
801 802 803 804 805 806
		goto exit;

	err = platform_driver_register(&coretemp_driver);
	if (err)
		goto exit;

807
	for_each_online_cpu(i)
808
		get_core_online(i);
809 810

#ifndef CONFIG_HOTPLUG_CPU
R
Rudolf Marek 已提交
811 812 813 814
	if (list_empty(&pdev_list)) {
		err = -ENODEV;
		goto exit_driver_unreg;
	}
815
#endif
R
Rudolf Marek 已提交
816 817 818 819

	register_hotcpu_notifier(&coretemp_cpu_notifier);
	return 0;

820
#ifndef CONFIG_HOTPLUG_CPU
821
exit_driver_unreg:
R
Rudolf Marek 已提交
822
	platform_driver_unregister(&coretemp_driver);
823
#endif
R
Rudolf Marek 已提交
824 825 826 827 828 829 830
exit:
	return err;
}

static void __exit coretemp_exit(void)
{
	struct pdev_entry *p, *n;
831

R
Rudolf Marek 已提交
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
	unregister_hotcpu_notifier(&coretemp_cpu_notifier);
	mutex_lock(&pdev_list_mutex);
	list_for_each_entry_safe(p, n, &pdev_list, list) {
		platform_device_unregister(p->pdev);
		list_del(&p->list);
		kfree(p);
	}
	mutex_unlock(&pdev_list_mutex);
	platform_driver_unregister(&coretemp_driver);
}

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

module_init(coretemp_init)
module_exit(coretemp_exit)