diff options
Diffstat (limited to 'drivers/hwmon/coretemp.c')
| -rw-r--r-- | drivers/hwmon/coretemp.c | 865 | 
1 files changed, 561 insertions, 304 deletions
diff --git a/drivers/hwmon/coretemp.c b/drivers/hwmon/coretemp.c index 42de98d73ff..d76f0b70c6e 100644 --- a/drivers/hwmon/coretemp.c +++ b/drivers/hwmon/coretemp.c @@ -20,6 +20,8 @@   * 02110-1301 USA.   */ +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt +  #include <linux/module.h>  #include <linux/init.h>  #include <linux/slab.h> @@ -32,129 +34,209 @@  #include <linux/list.h>  #include <linux/platform_device.h>  #include <linux/cpu.h> +#include <linux/smp.h> +#include <linux/moduleparam.h>  #include <linux/pci.h>  #include <asm/msr.h>  #include <asm/processor.h> -#include <asm/smp.h> +#include <asm/cpu_device_id.h>  #define DRVNAME	"coretemp" -typedef enum { SHOW_TEMP, SHOW_TJMAX, SHOW_TTARGET, SHOW_LABEL, -		SHOW_NAME } SHOW; -  /* - * Functions declaration + * 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"); -static struct coretemp_data *coretemp_update_device(struct device *dev); +#define BASE_SYSFS_ATTR_NO	2	/* Sysfs Base attr no for coretemp */ +#define NUM_REAL_CORES		32	/* Number of Real cores per cpu */ +#define CORETEMP_NAME_LENGTH	19	/* String Length of attrs */ +#define MAX_CORE_ATTRS		4	/* Maximum no of basic attrs */ +#define TOTAL_ATTRS		(MAX_CORE_ATTRS + 1) +#define MAX_CORE_DATA		(NUM_REAL_CORES + BASE_SYSFS_ATTR_NO) -struct coretemp_data { -	struct device *hwmon_dev; -	struct mutex update_lock; -	const char *name; -	u32 id; -	u16 core_id; -	char valid;		/* zero until following fields are valid */ -	unsigned long last_updated;	/* in jiffies */ +#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) + +#ifdef CONFIG_SMP +#define for_each_sibling(i, cpu)	for_each_cpu(i, cpu_sibling_mask(cpu)) +#else +#define for_each_sibling(i, cpu)	for (i = 0; false; ) +#endif + +/* + * 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. + * @attr_size:  Total number of pre-core attrs displayed in the sysfs. + * @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. + */ +struct temp_data {  	int temp; -	int tjmax;  	int ttarget; -	u8 alarm; +	int tjmax; +	unsigned long last_updated; +	unsigned int cpu; +	u32 cpu_core_id; +	u32 status_reg; +	int attr_size; +	bool is_pkg_data; +	bool valid; +	struct sensor_device_attribute sd_attrs[TOTAL_ATTRS]; +	char attr_name[TOTAL_ATTRS][CORETEMP_NAME_LENGTH]; +	struct attribute *attrs[TOTAL_ATTRS + 1]; +	struct attribute_group attr_group; +	struct mutex update_lock;  }; -/* - * Sysfs stuff - */ +/* 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; +}; + +struct pdev_entry { +	struct list_head list; +	struct platform_device *pdev; +	u16 phys_proc_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) +static ssize_t show_label(struct device *dev, +				struct device_attribute *devattr, char *buf)  { -	int ret;  	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); -	struct coretemp_data *data = dev_get_drvdata(dev); +	struct platform_data *pdata = dev_get_drvdata(dev); +	struct temp_data *tdata = pdata->core_data[attr->index]; -	if (attr->index == SHOW_NAME) -		ret = sprintf(buf, "%s\n", data->name); -	else	/* show label */ -		ret = sprintf(buf, "Core %d\n", data->core_id); -	return ret; +	if (tdata->is_pkg_data) +		return sprintf(buf, "Physical id %u\n", pdata->phys_proc_id); + +	return sprintf(buf, "Core %u\n", tdata->cpu_core_id);  } -static ssize_t show_alarm(struct device *dev, struct device_attribute -			  *devattr, char *buf) +static ssize_t show_crit_alarm(struct device *dev, +				struct device_attribute *devattr, char *buf)  { -	struct coretemp_data *data = coretemp_update_device(dev); -	/* read the Out-of-spec log, never clear */ -	return sprintf(buf, "%d\n", data->alarm); +	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);  } -static ssize_t show_temp(struct device *dev, -			 struct device_attribute *devattr, char *buf) +static ssize_t show_tjmax(struct device *dev, +			struct device_attribute *devattr, char *buf)  {  	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); -	struct coretemp_data *data = coretemp_update_device(dev); -	int err; +	struct platform_data *pdata = dev_get_drvdata(dev); -	if (attr->index == SHOW_TEMP) -		err = data->valid ? sprintf(buf, "%d\n", data->temp) : -EAGAIN; -	else if (attr->index == SHOW_TJMAX) -		err = sprintf(buf, "%d\n", data->tjmax); -	else -		err = sprintf(buf, "%d\n", data->ttarget); -	return err; +	return sprintf(buf, "%d\n", pdata->core_data[attr->index]->tjmax);  } -static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, -			  SHOW_TEMP); -static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, NULL, -			  SHOW_TJMAX); -static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp, NULL, -			  SHOW_TTARGET); -static DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL); -static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, show_name, NULL, SHOW_LABEL); -static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, SHOW_NAME); - -static struct attribute *coretemp_attributes[] = { -	&sensor_dev_attr_name.dev_attr.attr, -	&sensor_dev_attr_temp1_label.dev_attr.attr, -	&dev_attr_temp1_crit_alarm.attr, -	&sensor_dev_attr_temp1_input.dev_attr.attr, -	&sensor_dev_attr_temp1_crit.dev_attr.attr, -	NULL -}; +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); -static const struct attribute_group coretemp_group = { -	.attrs = coretemp_attributes, -}; +	return sprintf(buf, "%d\n", pdata->core_data[attr->index]->ttarget); +} -static struct coretemp_data *coretemp_update_device(struct device *dev) +static ssize_t show_temp(struct device *dev, +			struct device_attribute *devattr, char *buf)  { -	struct coretemp_data *data = dev_get_drvdata(dev); - -	mutex_lock(&data->update_lock); +	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]; -	if (!data->valid || time_after(jiffies, data->last_updated + HZ)) { -		u32 eax, edx; +	mutex_lock(&tdata->update_lock); -		data->valid = 0; -		rdmsr_on_cpu(data->id, MSR_IA32_THERM_STATUS, &eax, &edx); -		data->alarm = (eax >> 5) & 1; -		/* update only if data has been valid */ -		if (eax & 0x80000000) { -			data->temp = data->tjmax - (((eax >> 16) -							& 0x7f) * 1000); -			data->valid = 1; -		} else { -			dev_dbg(dev, "Temperature data invalid (0x%x)\n", eax); -		} -		data->last_updated = jiffies; +	/* 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); +		/* +		 * 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; +		tdata->last_updated = jiffies;  	} -	mutex_unlock(&data->update_lock); -	return data; +	mutex_unlock(&tdata->update_lock); +	return sprintf(buf, "%d\n", tdata->temp);  } -static int __devinit adjust_tjmax(struct cpuinfo_x86 *c, u32 id, struct device *dev) +struct tjmax_pci { +	unsigned int device; +	int tjmax; +}; + +static const struct tjmax_pci tjmax_pci_table[] = { +	{ 0x0708, 110000 },	/* CE41x0 (Sodaville ) */ +	{ 0x0c72, 102000 },	/* Atom S1240 (Centerton) */ +	{ 0x0c73, 95000 },	/* Atom S1220 (Centerton) */ +	{ 0x0c75, 95000 },	/* Atom S1260 (Centerton) */ +}; + +struct tjmax { +	char const *id; +	int tjmax; +}; + +static const struct tjmax tjmax_table[] = { +	{ "CPU  230", 100000 },		/* Model 0x1c, stepping 2	*/ +	{ "CPU  330", 125000 },		/* Model 0x1c, stepping 2	*/ +}; + +struct tjmax_model { +	u8 model; +	u8 mask; +	int tjmax; +}; + +#define ANY 0xff + +static const struct tjmax_model tjmax_model_table[] = { +	{ 0x1c, 10, 100000 },	/* D4xx, K4xx, N4xx, D5xx, K5xx, N5xx */ +	{ 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) */ +	{ 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 +				 */ +}; + +static int adjust_tjmax(struct cpuinfo_x86 *c, u32 id, struct device *dev)  {  	/* The 100C is default for both mobile and non mobile CPUs */ @@ -163,39 +245,46 @@ static int __devinit adjust_tjmax(struct cpuinfo_x86 *c, u32 id, struct device *  	int usemsr_ee = 1;  	int err;  	u32 eax, edx; -	struct pci_dev *host_bridge; - -	/* Early chips have no MSR for TjMax */ +	int i; +	struct pci_dev *host_bridge = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0)); -	if ((c->x86_model == 0xf) && (c->x86_mask < 4)) { -		usemsr_ee = 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; +		}  	} -	/* Atom CPUs */ - -	if (c->x86_model == 0x1c) { -		usemsr_ee = 0; +	for (i = 0; i < ARRAY_SIZE(tjmax_table); i++) { +		if (strstr(c->x86_model_id, tjmax_table[i].id)) +			return tjmax_table[i].tjmax; +	} -		host_bridge = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0)); +	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; +	} -		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; +	/* Early chips have no MSR for TjMax */ -		pci_dev_put(host_bridge); -	} +	if (c->x86_model == 0xf && c->x86_mask < 4) +		usemsr_ee = 0; -	if ((c->x86_model > 0xe) && (usemsr_ee)) { +	if (c->x86_model > 0xe && usemsr_ee) {  		u8 platform_id; -		/* 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 -		*/ - +		/* +		 * 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, @@ -203,20 +292,26 @@ static int __devinit adjust_tjmax(struct cpuinfo_x86 *c, u32 id, struct device *  				 " CPU\n");  			usemsr_ee = 0;  		} 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 */ +			/* +			 * Trust bit 28 up to Penryn, I could not find any +			 * documentation on that; if you happen to know +			 * someone at Intel please ask +			 */  			usemsr_ee = 0;  		} 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 */ +			/* +			 * 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;  			} @@ -224,7 +319,6 @@ static int __devinit adjust_tjmax(struct cpuinfo_x86 *c, u32 id, struct device *  	}  	if (usemsr_ee) { -  		err = rdmsr_safe_on_cpu(id, 0xee, &eax, &edx);  		if (err) {  			dev_warn(dev, @@ -233,180 +327,270 @@ static int __devinit adjust_tjmax(struct cpuinfo_x86 *c, u32 id, struct device *  		} else if (eax & 0x40000000) {  			tjmax = tjmax_ee;  		} -	/* if we dont use msr EE it means we are desktop CPU (with exeception -	   of Atom) */  	} else if (tjmax == 100000) { +		/* +		 * If we don't use msr EE it means we are desktop CPU +		 * (with exeception of Atom) +		 */  		dev_warn(dev, "Using relative temperature scale!\n");  	}  	return tjmax;  } -static int __devinit get_tjmax(struct cpuinfo_x86 *c, u32 id, -			       struct device *dev) +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; +} + +static int get_tjmax(struct cpuinfo_x86 *c, u32 id, struct device *dev)  { -	/* 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 */ +	/* +	 * 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"); +		if (cpu_has_tjmax(c)) +			dev_warn(dev, "Unable to read TjMax from CPU %u\n", id);  	} 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); +		if (val) { +			dev_dbg(dev, "TjMax is %d degrees C\n", val);  			return val * 1000;  		}  	} +	if (force_tjmax) { +		dev_notice(dev, "TjMax forced to %d degrees C by user\n", +			   force_tjmax); +		return force_tjmax * 1000; +	} +  	/*  	 * An assumption is made for early CPUs and unreadable MSR. -	 * NOTE: the given value may not be correct. +	 * NOTE: the calculated value may not be correct.  	 */ +	return adjust_tjmax(c, id, dev); +} -	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; +static int create_core_attrs(struct temp_data *tdata, struct device *dev, +			     int attr_no) +{ +	int i; +	static ssize_t (*const rd_ptr[TOTAL_ATTRS]) (struct device *dev, +			struct device_attribute *devattr, char *buf) = { +			show_label, show_crit_alarm, show_temp, show_tjmax, +			show_ttarget }; +	static const char *const names[TOTAL_ATTRS] = { +					"temp%d_label", "temp%d_crit_alarm", +					"temp%d_input", "temp%d_crit", +					"temp%d_max" }; + +	for (i = 0; i < tdata->attr_size; i++) { +		snprintf(tdata->attr_name[i], CORETEMP_NAME_LENGTH, names[i], +			attr_no); +		sysfs_attr_init(&tdata->sd_attrs[i].dev_attr.attr); +		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; +		tdata->attrs[i] = &tdata->sd_attrs[i].dev_attr.attr;  	} +	tdata->attr_group.attrs = tdata->attrs; +	return sysfs_create_group(&dev->kobj, &tdata->attr_group);  } -static void __devinit get_ucode_rev_on_cpu(void *edx) + +static int chk_ucode_version(unsigned int cpu)  { -	u32 eax; +	struct cpuinfo_x86 *c = &cpu_data(cpu); -	wrmsr(MSR_IA32_UCODE_REV, 0, 0); -	sync_core(); -	rdmsr(MSR_IA32_UCODE_REV, eax, *(u32 *)edx); +	/* +	 * 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). +	 */ +	if (c->x86_model == 0xe && c->x86_mask < 0xc && c->microcode < 0x39) { +		pr_err("Errata AE18 not fixed, update BIOS or microcode of the CPU!\n"); +		return -ENODEV; +	} +	return 0;  } -static int __devinit coretemp_probe(struct platform_device *pdev) +static struct platform_device *coretemp_get_pdev(unsigned int cpu)  { -	struct coretemp_data *data; -	struct cpuinfo_x86 *c = &cpu_data(pdev->id); -	int err; -	u32 eax, edx; +	u16 phys_proc_id = TO_PHYS_ID(cpu); +	struct pdev_entry *p; -	if (!(data = kzalloc(sizeof(struct coretemp_data), GFP_KERNEL))) { -		err = -ENOMEM; -		dev_err(&pdev->dev, "Out of memory\n"); -		goto exit; -	} +	mutex_lock(&pdev_list_mutex); -	data->id = pdev->id; -#ifdef CONFIG_SMP -	data->core_id = c->cpu_core_id; -#endif -	data->name = "coretemp"; -	mutex_init(&data->update_lock); +	list_for_each_entry(p, &pdev_list, list) +		if (p->phys_proc_id == phys_proc_id) { +			mutex_unlock(&pdev_list_mutex); +			return p->pdev; +		} -	/* test if we can access the THERM_STATUS MSR */ -	err = rdmsr_safe_on_cpu(data->id, MSR_IA32_THERM_STATUS, &eax, &edx); -	if (err) { -		dev_err(&pdev->dev, -			"Unable to access THERM_STATUS MSR, giving up\n"); -		goto exit_free; -	} +	mutex_unlock(&pdev_list_mutex); +	return NULL; +} -	/* 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). -	*/ +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); +	tdata->attr_size = MAX_CORE_ATTRS; +	mutex_init(&tdata->update_lock); +	return tdata; +} -	if ((c->x86_model == 0xe) && (c->x86_mask < 0xc)) { -		/* check for microcode update */ -		err = smp_call_function_single(data->id, get_ucode_rev_on_cpu, -					       &edx, 1); -		if (err) { -			dev_err(&pdev->dev, -				"Cannot determine microcode revision of " -				"CPU#%u (%d)!\n", data->id, err); -			err = -ENODEV; -			goto exit_free; -		} else if (edx < 0x39) { -			err = -ENODEV; -			dev_err(&pdev->dev, -				"Errata AE18 not fixed, update BIOS or " -				"microcode of the CPU!\n"); -			goto exit_free; -		} -	} +static int create_core_data(struct platform_device *pdev, unsigned int cpu, +			    int pkg_flag) +{ +	struct temp_data *tdata; +	struct platform_data *pdata = platform_get_drvdata(pdev); +	struct cpuinfo_x86 *c = &cpu_data(cpu); +	u32 eax, edx; +	int err, attr_no; + +	/* +	 * 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 +	 */ +	attr_no = pkg_flag ? 1 : TO_ATTR_NO(cpu); -	data->tjmax = get_tjmax(c, data->id, &pdev->dev); -	platform_set_drvdata(pdev, data); +	if (attr_no > MAX_CORE_DATA - 1) +		return -ERANGE;  	/* -	 * read the still undocumented IA32_TEMPERATURE_TARGET. It exists -	 * on older CPUs but not in this register, -	 * Atoms don't have it either. +	 * Provide a single set of attributes for all HT siblings of a core +	 * to avoid duplicate sensors (the processor ID and core ID of all +	 * HT siblings of a core are the same). +	 * Skip if a HT sibling of this core is already registered. +	 * This is not an error.  	 */ +	if (pdata->core_data[attr_no] != NULL) +		return 0; -	if ((c->x86_model > 0xe) && (c->x86_model != 0x1c)) { -		err = rdmsr_safe_on_cpu(data->id, MSR_IA32_TEMPERATURE_TARGET, -		    &eax, &edx); -		if (err) { -			dev_warn(&pdev->dev, "Unable to read" -					" IA32_TEMPERATURE_TARGET MSR\n"); -		} else { -			data->ttarget = data->tjmax - -					(((eax >> 8) & 0xff) * 1000); -			err = device_create_file(&pdev->dev, -					&sensor_dev_attr_temp1_max.dev_attr); -			if (err) -				goto exit_free; +	tdata = init_temp_data(cpu, pkg_flag); +	if (!tdata) +		return -ENOMEM; + +	/* 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 */ +	tdata->tjmax = get_tjmax(c, cpu, &pdev->dev); + +	/* +	 * 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. +	 */ +	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++;  		}  	} -	if ((err = sysfs_create_group(&pdev->dev.kobj, &coretemp_group))) -		goto exit_dev; +	pdata->core_data[attr_no] = tdata; -	data->hwmon_dev = hwmon_device_register(&pdev->dev); -	if (IS_ERR(data->hwmon_dev)) { -		err = PTR_ERR(data->hwmon_dev); -		dev_err(&pdev->dev, "Class registration failed (%d)\n", -			err); -		goto exit_class; -	} +	/* Create sysfs interfaces */ +	err = create_core_attrs(tdata, pdata->hwmon_dev, attr_no); +	if (err) +		goto exit_free;  	return 0; - -exit_class: -	sysfs_remove_group(&pdev->dev.kobj, &coretemp_group); -exit_dev: -	device_remove_file(&pdev->dev, &sensor_dev_attr_temp1_max.dev_attr);  exit_free: -	kfree(data); -exit: +	pdata->core_data[attr_no] = NULL; +	kfree(tdata);  	return err;  } -static int __devexit coretemp_remove(struct platform_device *pdev) +static void coretemp_add_core(unsigned int cpu, int pkg_flag) +{ +	struct platform_device *pdev = coretemp_get_pdev(cpu); +	int err; + +	if (!pdev) +		return; + +	err = create_core_data(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, +				 int indx) +{ +	struct temp_data *tdata = pdata->core_data[indx]; + +	/* Remove the sysfs attributes */ +	sysfs_remove_group(&pdata->hwmon_dev->kobj, &tdata->attr_group); + +	kfree(pdata->core_data[indx]); +	pdata->core_data[indx] = NULL; +} + +static int coretemp_probe(struct platform_device *pdev) +{ +	struct device *dev = &pdev->dev; +	struct platform_data *pdata; + +	/* Initialize the per-package data structures */ +	pdata = devm_kzalloc(dev, sizeof(struct platform_data), GFP_KERNEL); +	if (!pdata) +		return -ENOMEM; + +	pdata->phys_proc_id = pdev->id; +	platform_set_drvdata(pdev, pdata); + +	pdata->hwmon_dev = devm_hwmon_device_register_with_groups(dev, DRVNAME, +								  pdata, NULL); +	return PTR_ERR_OR_ZERO(pdata->hwmon_dev); +} + +static int coretemp_remove(struct platform_device *pdev)  { -	struct coretemp_data *data = platform_get_drvdata(pdev); +	struct platform_data *pdata = platform_get_drvdata(pdev); +	int i; + +	for (i = MAX_CORE_DATA - 1; i >= 0; --i) +		if (pdata->core_data[i]) +			coretemp_remove_core(pdata, i); -	hwmon_device_unregister(data->hwmon_dev); -	sysfs_remove_group(&pdev->dev.kobj, &coretemp_group); -	device_remove_file(&pdev->dev, &sensor_dev_attr_temp1_max.dev_attr); -	platform_set_drvdata(pdev, NULL); -	kfree(data);  	return 0;  } @@ -416,57 +600,21 @@ static struct platform_driver coretemp_driver = {  		.name = DRVNAME,  	},  	.probe = coretemp_probe, -	.remove = __devexit_p(coretemp_remove), -}; - -struct pdev_entry { -	struct list_head list; -	struct platform_device *pdev; -	unsigned int cpu; -#ifdef CONFIG_SMP -	u16 phys_proc_id; -	u16 cpu_core_id; -#endif +	.remove = coretemp_remove,  }; -static LIST_HEAD(pdev_list); -static DEFINE_MUTEX(pdev_list_mutex); - -static int __cpuinit coretemp_device_add(unsigned int cpu) +static int coretemp_device_add(unsigned int cpu)  {  	int err;  	struct platform_device *pdev;  	struct pdev_entry *pdev_entry; -	struct cpuinfo_x86 *c = &cpu_data(cpu); - -	/* -	 * 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)) { -		printk(KERN_INFO DRVNAME ": CPU (model=0x%x)" -		       " has no thermal sensor.\n", c->x86_model); -		return 0; -	}  	mutex_lock(&pdev_list_mutex); -#ifdef CONFIG_SMP -	/* Skip second HT entry of each core */ -	list_for_each_entry(pdev_entry, &pdev_list, list) { -		if (c->phys_proc_id == pdev_entry->phys_proc_id && -		    c->cpu_core_id == pdev_entry->cpu_core_id) { -			err = 0;	/* Not an error */ -			goto exit; -		} -	} -#endif - -	pdev = platform_device_alloc(DRVNAME, cpu); +	pdev = platform_device_alloc(DRVNAME, TO_PHYS_ID(cpu));  	if (!pdev) {  		err = -ENOMEM; -		printk(KERN_ERR DRVNAME ": Device allocation failed\n"); +		pr_err("Device allocation failed\n");  		goto exit;  	} @@ -478,17 +626,13 @@ static int __cpuinit coretemp_device_add(unsigned int cpu)  	err = platform_device_add(pdev);  	if (err) { -		printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n", -		       err); +		pr_err("Device addition failed (%d)\n", err);  		goto exit_device_free;  	}  	pdev_entry->pdev = pdev; -	pdev_entry->cpu = cpu; -#ifdef CONFIG_SMP -	pdev_entry->phys_proc_id = c->phys_proc_id; -	pdev_entry->cpu_core_id = c->cpu_core_id; -#endif +	pdev_entry->phys_proc_id = pdev->id; +  	list_add_tail(&pdev_entry->list, &pdev_list);  	mutex_unlock(&pdev_list_mutex); @@ -503,29 +647,127 @@ exit:  	return err;  } -static void __cpuinit coretemp_device_remove(unsigned int cpu) +static void coretemp_device_remove(unsigned int cpu)  { -	struct pdev_entry *p; -	unsigned int i; +	struct pdev_entry *p, *n; +	u16 phys_proc_id = TO_PHYS_ID(cpu);  	mutex_lock(&pdev_list_mutex); -	list_for_each_entry(p, &pdev_list, list) { -		if (p->cpu != cpu) +	list_for_each_entry_safe(p, n, &pdev_list, list) { +		if (p->phys_proc_id != phys_proc_id)  			continue; -  		platform_device_unregister(p->pdev);  		list_del(&p->list); -		mutex_unlock(&pdev_list_mutex);  		kfree(p); -		for_each_cpu(i, cpu_sibling_mask(cpu)) -			if (i != cpu && !coretemp_device_add(i)) -				break; -		return;  	}  	mutex_unlock(&pdev_list_mutex);  } -static int __cpuinit coretemp_cpu_callback(struct notifier_block *nfb, +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 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_DTHERM)) +		return; + +	if (!pdev) { +		/* Check the microcode version of the CPU */ +		if (chk_ucode_version(cpu)) +			return; + +		/* +		 * 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 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); + +	/* The core id is too big, just return */ +	if (indx > MAX_CORE_DATA - 1) +		return; + +	if (pdata->core_data[indx] && pdata->core_data[indx]->cpu == cpu) +		coretemp_remove_core(pdata, indx); + +	/* +	 * If a HT sibling of a core is taken offline, but another HT sibling +	 * of the same core is still online, register the alternate sibling. +	 * This ensures that exactly one set of attributes is provided as long +	 * as at least one HT sibling of a core is online. +	 */ +	for_each_sibling(i, cpu) { +		if (i != cpu) { +			get_core_online(i); +			/* +			 * Display temperature sensor data for one HT sibling +			 * per core only, so abort the loop after one such +			 * sibling has been found. +			 */ +			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); +} + +static int coretemp_cpu_callback(struct notifier_block *nfb,  				 unsigned long action, void *hcpu)  {  	unsigned int cpu = (unsigned long) hcpu; @@ -533,10 +775,10 @@ static int __cpuinit coretemp_cpu_callback(struct notifier_block *nfb,  	switch (action) {  	case CPU_ONLINE:  	case CPU_DOWN_FAILED: -		coretemp_device_add(cpu); +		get_core_online(cpu);  		break;  	case CPU_DOWN_PREPARE: -		coretemp_device_remove(cpu); +		put_core_offline(cpu);  		break;  	}  	return NOTIFY_OK; @@ -546,29 +788,42 @@ static struct notifier_block coretemp_cpu_notifier __refdata = {  	.notifier_call = coretemp_cpu_callback,  }; +static const struct x86_cpu_id __initconst coretemp_ids[] = { +	{ X86_VENDOR_INTEL, X86_FAMILY_ANY, X86_MODEL_ANY, X86_FEATURE_DTHERM }, +	{} +}; +MODULE_DEVICE_TABLE(x86cpu, coretemp_ids); +  static int __init coretemp_init(void)  { -	int i, err = -ENODEV; +	int i, err; -	/* quick check if we run Intel */ -	if (cpu_data(0).x86_vendor != X86_VENDOR_INTEL) -		goto exit; +	/* +	 * 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;  	err = platform_driver_register(&coretemp_driver);  	if (err)  		goto exit; +	cpu_notifier_register_begin();  	for_each_online_cpu(i) -		coretemp_device_add(i); +		get_core_online(i);  #ifndef CONFIG_HOTPLUG_CPU  	if (list_empty(&pdev_list)) { +		cpu_notifier_register_done();  		err = -ENODEV;  		goto exit_driver_unreg;  	}  #endif -	register_hotcpu_notifier(&coretemp_cpu_notifier); +	__register_hotcpu_notifier(&coretemp_cpu_notifier); +	cpu_notifier_register_done();  	return 0;  #ifndef CONFIG_HOTPLUG_CPU @@ -583,7 +838,8 @@ static void __exit coretemp_exit(void)  {  	struct pdev_entry *p, *n; -	unregister_hotcpu_notifier(&coretemp_cpu_notifier); +	cpu_notifier_register_begin(); +	__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); @@ -591,6 +847,7 @@ static void __exit coretemp_exit(void)  		kfree(p);  	}  	mutex_unlock(&pdev_list_mutex); +	cpu_notifier_register_done();  	platform_driver_unregister(&coretemp_driver);  }  | 
