coretemp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. /*
  2. * coretemp.c - Linux kernel module for hardware monitoring
  3. *
  4. * Copyright (C) 2007 Rudolf Marek <r.marek@assembler.cz>
  5. *
  6. * Inspired from many hwmon drivers
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20. * 02110-1301 USA.
  21. */
  22. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/slab.h>
  26. #include <linux/jiffies.h>
  27. #include <linux/hwmon.h>
  28. #include <linux/sysfs.h>
  29. #include <linux/hwmon-sysfs.h>
  30. #include <linux/err.h>
  31. #include <linux/mutex.h>
  32. #include <linux/list.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/cpu.h>
  35. #include <linux/pci.h>
  36. #include <linux/smp.h>
  37. #include <linux/moduleparam.h>
  38. #include <asm/msr.h>
  39. #include <asm/processor.h>
  40. #include <asm/cpu_device_id.h>
  41. #define DRVNAME "coretemp"
  42. /*
  43. * force_tjmax only matters when TjMax can't be read from the CPU itself.
  44. * When set, it replaces the driver's suboptimal heuristic.
  45. */
  46. static int force_tjmax;
  47. module_param_named(tjmax, force_tjmax, int, 0444);
  48. MODULE_PARM_DESC(tjmax, "TjMax value in degrees Celsius");
  49. #define BASE_SYSFS_ATTR_NO 2 /* Sysfs Base attr no for coretemp */
  50. #define NUM_REAL_CORES 32 /* Number of Real cores per cpu */
  51. #define CORETEMP_NAME_LENGTH 19 /* String Length of attrs */
  52. #define MAX_CORE_ATTRS 4 /* Maximum no of basic attrs */
  53. #define TOTAL_ATTRS (MAX_CORE_ATTRS + 1)
  54. #define MAX_CORE_DATA (NUM_REAL_CORES + BASE_SYSFS_ATTR_NO)
  55. #define TO_PHYS_ID(cpu) (cpu_data(cpu).phys_proc_id)
  56. #define TO_CORE_ID(cpu) (cpu_data(cpu).cpu_core_id)
  57. #define TO_ATTR_NO(cpu) (TO_CORE_ID(cpu) + BASE_SYSFS_ATTR_NO)
  58. #ifdef CONFIG_SMP
  59. #define for_each_sibling(i, cpu) for_each_cpu(i, cpu_sibling_mask(cpu))
  60. #else
  61. #define for_each_sibling(i, cpu) for (i = 0; false; )
  62. #endif
  63. /*
  64. * Per-Core Temperature Data
  65. * @last_updated: The time when the current temperature value was updated
  66. * earlier (in jiffies).
  67. * @cpu_core_id: The CPU Core from which temperature values should be read
  68. * This value is passed as "id" field to rdmsr/wrmsr functions.
  69. * @status_reg: One of IA32_THERM_STATUS or IA32_PACKAGE_THERM_STATUS,
  70. * from where the temperature values should be read.
  71. * @attr_size: Total number of pre-core attrs displayed in the sysfs.
  72. * @is_pkg_data: If this is 1, the temp_data holds pkgtemp data.
  73. * Otherwise, temp_data holds coretemp data.
  74. * @valid: If this is 1, the current temperature is valid.
  75. */
  76. struct temp_data {
  77. int temp;
  78. int ttarget;
  79. int tjmax;
  80. unsigned long last_updated;
  81. unsigned int cpu;
  82. u32 cpu_core_id;
  83. u32 status_reg;
  84. int attr_size;
  85. bool is_pkg_data;
  86. bool valid;
  87. struct sensor_device_attribute sd_attrs[TOTAL_ATTRS];
  88. char attr_name[TOTAL_ATTRS][CORETEMP_NAME_LENGTH];
  89. struct mutex update_lock;
  90. };
  91. /* Platform Data per Physical CPU */
  92. struct platform_data {
  93. struct device *hwmon_dev;
  94. u16 phys_proc_id;
  95. struct temp_data *core_data[MAX_CORE_DATA];
  96. struct device_attribute name_attr;
  97. };
  98. struct pdev_entry {
  99. struct list_head list;
  100. struct platform_device *pdev;
  101. u16 phys_proc_id;
  102. };
  103. static LIST_HEAD(pdev_list);
  104. static DEFINE_MUTEX(pdev_list_mutex);
  105. static ssize_t show_name(struct device *dev,
  106. struct device_attribute *devattr, char *buf)
  107. {
  108. return sprintf(buf, "%s\n", DRVNAME);
  109. }
  110. static ssize_t show_label(struct device *dev,
  111. struct device_attribute *devattr, char *buf)
  112. {
  113. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  114. struct platform_data *pdata = dev_get_drvdata(dev);
  115. struct temp_data *tdata = pdata->core_data[attr->index];
  116. if (tdata->is_pkg_data)
  117. return sprintf(buf, "Physical id %u\n", pdata->phys_proc_id);
  118. return sprintf(buf, "Core %u\n", tdata->cpu_core_id);
  119. }
  120. static ssize_t show_crit_alarm(struct device *dev,
  121. struct device_attribute *devattr, char *buf)
  122. {
  123. u32 eax, edx;
  124. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  125. struct platform_data *pdata = dev_get_drvdata(dev);
  126. struct temp_data *tdata = pdata->core_data[attr->index];
  127. rdmsr_on_cpu(tdata->cpu, tdata->status_reg, &eax, &edx);
  128. return sprintf(buf, "%d\n", (eax >> 5) & 1);
  129. }
  130. static ssize_t show_tjmax(struct device *dev,
  131. struct device_attribute *devattr, char *buf)
  132. {
  133. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  134. struct platform_data *pdata = dev_get_drvdata(dev);
  135. return sprintf(buf, "%d\n", pdata->core_data[attr->index]->tjmax);
  136. }
  137. static ssize_t show_ttarget(struct device *dev,
  138. struct device_attribute *devattr, char *buf)
  139. {
  140. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  141. struct platform_data *pdata = dev_get_drvdata(dev);
  142. return sprintf(buf, "%d\n", pdata->core_data[attr->index]->ttarget);
  143. }
  144. static ssize_t show_temp(struct device *dev,
  145. struct device_attribute *devattr, char *buf)
  146. {
  147. u32 eax, edx;
  148. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  149. struct platform_data *pdata = dev_get_drvdata(dev);
  150. struct temp_data *tdata = pdata->core_data[attr->index];
  151. mutex_lock(&tdata->update_lock);
  152. /* Check whether the time interval has elapsed */
  153. if (!tdata->valid || time_after(jiffies, tdata->last_updated + HZ)) {
  154. rdmsr_on_cpu(tdata->cpu, tdata->status_reg, &eax, &edx);
  155. tdata->valid = 0;
  156. /* Check whether the data is valid */
  157. if (eax & 0x80000000) {
  158. tdata->temp = tdata->tjmax -
  159. ((eax >> 16) & 0x7f) * 1000;
  160. tdata->valid = 1;
  161. }
  162. tdata->last_updated = jiffies;
  163. }
  164. mutex_unlock(&tdata->update_lock);
  165. return tdata->valid ? sprintf(buf, "%d\n", tdata->temp) : -EAGAIN;
  166. }
  167. struct tjmax {
  168. char const *id;
  169. int tjmax;
  170. };
  171. static struct tjmax __cpuinitconst tjmax_table[] = {
  172. { "CPU D410", 100000 },
  173. { "CPU D425", 100000 },
  174. { "CPU D510", 100000 },
  175. { "CPU D525", 100000 },
  176. { "CPU N450", 100000 },
  177. { "CPU N455", 100000 },
  178. { "CPU N470", 100000 },
  179. { "CPU N475", 100000 },
  180. { "CPU 230", 100000 }, /* Model 0x1c, stepping 2 */
  181. { "CPU 330", 125000 }, /* Model 0x1c, stepping 2 */
  182. { "CPU CE4110", 110000 }, /* Model 0x1c, stepping 10 */
  183. { "CPU CE4150", 110000 }, /* Model 0x1c, stepping 10 */
  184. { "CPU CE4170", 110000 }, /* Model 0x1c, stepping 10 */
  185. };
  186. static int __cpuinit adjust_tjmax(struct cpuinfo_x86 *c, u32 id,
  187. struct device *dev)
  188. {
  189. /* The 100C is default for both mobile and non mobile CPUs */
  190. int tjmax = 100000;
  191. int tjmax_ee = 85000;
  192. int usemsr_ee = 1;
  193. int err;
  194. u32 eax, edx;
  195. struct pci_dev *host_bridge;
  196. int i;
  197. /* explicit tjmax table entries override heuristics */
  198. for (i = 0; i < ARRAY_SIZE(tjmax_table); i++) {
  199. if (strstr(c->x86_model_id, tjmax_table[i].id))
  200. return tjmax_table[i].tjmax;
  201. }
  202. /* Early chips have no MSR for TjMax */
  203. if (c->x86_model == 0xf && c->x86_mask < 4)
  204. usemsr_ee = 0;
  205. /* Atom CPUs */
  206. if (c->x86_model == 0x1c || c->x86_model == 0x26
  207. || c->x86_model == 0x27) {
  208. usemsr_ee = 0;
  209. host_bridge = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
  210. if (host_bridge && host_bridge->vendor == PCI_VENDOR_ID_INTEL
  211. && (host_bridge->device == 0xa000 /* NM10 based nettop */
  212. || host_bridge->device == 0xa010)) /* NM10 based netbook */
  213. tjmax = 100000;
  214. else
  215. tjmax = 90000;
  216. pci_dev_put(host_bridge);
  217. } else if (c->x86_model == 0x36) {
  218. usemsr_ee = 0;
  219. tjmax = 100000;
  220. }
  221. if (c->x86_model > 0xe && usemsr_ee) {
  222. u8 platform_id;
  223. /*
  224. * Now we can detect the mobile CPU using Intel provided table
  225. * http://softwarecommunity.intel.com/Wiki/Mobility/720.htm
  226. * For Core2 cores, check MSR 0x17, bit 28 1 = Mobile CPU
  227. */
  228. err = rdmsr_safe_on_cpu(id, 0x17, &eax, &edx);
  229. if (err) {
  230. dev_warn(dev,
  231. "Unable to access MSR 0x17, assuming desktop"
  232. " CPU\n");
  233. usemsr_ee = 0;
  234. } else if (c->x86_model < 0x17 && !(eax & 0x10000000)) {
  235. /*
  236. * Trust bit 28 up to Penryn, I could not find any
  237. * documentation on that; if you happen to know
  238. * someone at Intel please ask
  239. */
  240. usemsr_ee = 0;
  241. } else {
  242. /* Platform ID bits 52:50 (EDX starts at bit 32) */
  243. platform_id = (edx >> 18) & 0x7;
  244. /*
  245. * Mobile Penryn CPU seems to be platform ID 7 or 5
  246. * (guesswork)
  247. */
  248. if (c->x86_model == 0x17 &&
  249. (platform_id == 5 || platform_id == 7)) {
  250. /*
  251. * If MSR EE bit is set, set it to 90 degrees C,
  252. * otherwise 105 degrees C
  253. */
  254. tjmax_ee = 90000;
  255. tjmax = 105000;
  256. }
  257. }
  258. }
  259. if (usemsr_ee) {
  260. err = rdmsr_safe_on_cpu(id, 0xee, &eax, &edx);
  261. if (err) {
  262. dev_warn(dev,
  263. "Unable to access MSR 0xEE, for Tjmax, left"
  264. " at default\n");
  265. } else if (eax & 0x40000000) {
  266. tjmax = tjmax_ee;
  267. }
  268. } else if (tjmax == 100000) {
  269. /*
  270. * If we don't use msr EE it means we are desktop CPU
  271. * (with exeception of Atom)
  272. */
  273. dev_warn(dev, "Using relative temperature scale!\n");
  274. }
  275. return tjmax;
  276. }
  277. static int __cpuinit get_tjmax(struct cpuinfo_x86 *c, u32 id,
  278. struct device *dev)
  279. {
  280. int err;
  281. u32 eax, edx;
  282. u32 val;
  283. /*
  284. * A new feature of current Intel(R) processors, the
  285. * IA32_TEMPERATURE_TARGET contains the TjMax value
  286. */
  287. err = rdmsr_safe_on_cpu(id, MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
  288. if (err) {
  289. if (c->x86_model > 0xe && c->x86_model != 0x1c)
  290. dev_warn(dev, "Unable to read TjMax from CPU %u\n", id);
  291. } else {
  292. val = (eax >> 16) & 0xff;
  293. /*
  294. * If the TjMax is not plausible, an assumption
  295. * will be used
  296. */
  297. if (val) {
  298. dev_dbg(dev, "TjMax is %d degrees C\n", val);
  299. return val * 1000;
  300. }
  301. }
  302. if (force_tjmax) {
  303. dev_notice(dev, "TjMax forced to %d degrees C by user\n",
  304. force_tjmax);
  305. return force_tjmax * 1000;
  306. }
  307. /*
  308. * An assumption is made for early CPUs and unreadable MSR.
  309. * NOTE: the calculated value may not be correct.
  310. */
  311. return adjust_tjmax(c, id, dev);
  312. }
  313. static int __devinit create_name_attr(struct platform_data *pdata,
  314. struct device *dev)
  315. {
  316. sysfs_attr_init(&pdata->name_attr.attr);
  317. pdata->name_attr.attr.name = "name";
  318. pdata->name_attr.attr.mode = S_IRUGO;
  319. pdata->name_attr.show = show_name;
  320. return device_create_file(dev, &pdata->name_attr);
  321. }
  322. static int __cpuinit create_core_attrs(struct temp_data *tdata,
  323. struct device *dev, int attr_no)
  324. {
  325. int err, i;
  326. static ssize_t (*const rd_ptr[TOTAL_ATTRS]) (struct device *dev,
  327. struct device_attribute *devattr, char *buf) = {
  328. show_label, show_crit_alarm, show_temp, show_tjmax,
  329. show_ttarget };
  330. static const char *const names[TOTAL_ATTRS] = {
  331. "temp%d_label", "temp%d_crit_alarm",
  332. "temp%d_input", "temp%d_crit",
  333. "temp%d_max" };
  334. for (i = 0; i < tdata->attr_size; i++) {
  335. snprintf(tdata->attr_name[i], CORETEMP_NAME_LENGTH, names[i],
  336. attr_no);
  337. sysfs_attr_init(&tdata->sd_attrs[i].dev_attr.attr);
  338. tdata->sd_attrs[i].dev_attr.attr.name = tdata->attr_name[i];
  339. tdata->sd_attrs[i].dev_attr.attr.mode = S_IRUGO;
  340. tdata->sd_attrs[i].dev_attr.show = rd_ptr[i];
  341. tdata->sd_attrs[i].index = attr_no;
  342. err = device_create_file(dev, &tdata->sd_attrs[i].dev_attr);
  343. if (err)
  344. goto exit_free;
  345. }
  346. return 0;
  347. exit_free:
  348. while (--i >= 0)
  349. device_remove_file(dev, &tdata->sd_attrs[i].dev_attr);
  350. return err;
  351. }
  352. static int __cpuinit chk_ucode_version(unsigned int cpu)
  353. {
  354. struct cpuinfo_x86 *c = &cpu_data(cpu);
  355. /*
  356. * Check if we have problem with errata AE18 of Core processors:
  357. * Readings might stop update when processor visited too deep sleep,
  358. * fixed for stepping D0 (6EC).
  359. */
  360. if (c->x86_model == 0xe && c->x86_mask < 0xc && c->microcode < 0x39) {
  361. pr_err("Errata AE18 not fixed, update BIOS or "
  362. "microcode of the CPU!\n");
  363. return -ENODEV;
  364. }
  365. return 0;
  366. }
  367. static struct platform_device __cpuinit *coretemp_get_pdev(unsigned int cpu)
  368. {
  369. u16 phys_proc_id = TO_PHYS_ID(cpu);
  370. struct pdev_entry *p;
  371. mutex_lock(&pdev_list_mutex);
  372. list_for_each_entry(p, &pdev_list, list)
  373. if (p->phys_proc_id == phys_proc_id) {
  374. mutex_unlock(&pdev_list_mutex);
  375. return p->pdev;
  376. }
  377. mutex_unlock(&pdev_list_mutex);
  378. return NULL;
  379. }
  380. static struct temp_data __cpuinit *init_temp_data(unsigned int cpu,
  381. int pkg_flag)
  382. {
  383. struct temp_data *tdata;
  384. tdata = kzalloc(sizeof(struct temp_data), GFP_KERNEL);
  385. if (!tdata)
  386. return NULL;
  387. tdata->status_reg = pkg_flag ? MSR_IA32_PACKAGE_THERM_STATUS :
  388. MSR_IA32_THERM_STATUS;
  389. tdata->is_pkg_data = pkg_flag;
  390. tdata->cpu = cpu;
  391. tdata->cpu_core_id = TO_CORE_ID(cpu);
  392. tdata->attr_size = MAX_CORE_ATTRS;
  393. mutex_init(&tdata->update_lock);
  394. return tdata;
  395. }
  396. static int __cpuinit create_core_data(struct platform_device *pdev,
  397. unsigned int cpu, int pkg_flag)
  398. {
  399. struct temp_data *tdata;
  400. struct platform_data *pdata = platform_get_drvdata(pdev);
  401. struct cpuinfo_x86 *c = &cpu_data(cpu);
  402. u32 eax, edx;
  403. int err, attr_no;
  404. /*
  405. * Find attr number for sysfs:
  406. * We map the attr number to core id of the CPU
  407. * The attr number is always core id + 2
  408. * The Pkgtemp will always show up as temp1_*, if available
  409. */
  410. attr_no = pkg_flag ? 1 : TO_ATTR_NO(cpu);
  411. if (attr_no > MAX_CORE_DATA - 1)
  412. return -ERANGE;
  413. /*
  414. * Provide a single set of attributes for all HT siblings of a core
  415. * to avoid duplicate sensors (the processor ID and core ID of all
  416. * HT siblings of a core are the same).
  417. * Skip if a HT sibling of this core is already registered.
  418. * This is not an error.
  419. */
  420. if (pdata->core_data[attr_no] != NULL)
  421. return 0;
  422. tdata = init_temp_data(cpu, pkg_flag);
  423. if (!tdata)
  424. return -ENOMEM;
  425. /* Test if we can access the status register */
  426. err = rdmsr_safe_on_cpu(cpu, tdata->status_reg, &eax, &edx);
  427. if (err)
  428. goto exit_free;
  429. /* We can access status register. Get Critical Temperature */
  430. tdata->tjmax = get_tjmax(c, cpu, &pdev->dev);
  431. /*
  432. * Read the still undocumented bits 8:15 of IA32_TEMPERATURE_TARGET.
  433. * The target temperature is available on older CPUs but not in this
  434. * register. Atoms don't have the register at all.
  435. */
  436. if (c->x86_model > 0xe && c->x86_model != 0x1c) {
  437. err = rdmsr_safe_on_cpu(cpu, MSR_IA32_TEMPERATURE_TARGET,
  438. &eax, &edx);
  439. if (!err) {
  440. tdata->ttarget
  441. = tdata->tjmax - ((eax >> 8) & 0xff) * 1000;
  442. tdata->attr_size++;
  443. }
  444. }
  445. pdata->core_data[attr_no] = tdata;
  446. /* Create sysfs interfaces */
  447. err = create_core_attrs(tdata, &pdev->dev, attr_no);
  448. if (err)
  449. goto exit_free;
  450. return 0;
  451. exit_free:
  452. pdata->core_data[attr_no] = NULL;
  453. kfree(tdata);
  454. return err;
  455. }
  456. static void __cpuinit coretemp_add_core(unsigned int cpu, int pkg_flag)
  457. {
  458. struct platform_device *pdev = coretemp_get_pdev(cpu);
  459. int err;
  460. if (!pdev)
  461. return;
  462. err = create_core_data(pdev, cpu, pkg_flag);
  463. if (err)
  464. dev_err(&pdev->dev, "Adding Core %u failed\n", cpu);
  465. }
  466. static void coretemp_remove_core(struct platform_data *pdata,
  467. struct device *dev, int indx)
  468. {
  469. int i;
  470. struct temp_data *tdata = pdata->core_data[indx];
  471. /* Remove the sysfs attributes */
  472. for (i = 0; i < tdata->attr_size; i++)
  473. device_remove_file(dev, &tdata->sd_attrs[i].dev_attr);
  474. kfree(pdata->core_data[indx]);
  475. pdata->core_data[indx] = NULL;
  476. }
  477. static int __devinit coretemp_probe(struct platform_device *pdev)
  478. {
  479. struct platform_data *pdata;
  480. int err;
  481. /* Initialize the per-package data structures */
  482. pdata = kzalloc(sizeof(struct platform_data), GFP_KERNEL);
  483. if (!pdata)
  484. return -ENOMEM;
  485. err = create_name_attr(pdata, &pdev->dev);
  486. if (err)
  487. goto exit_free;
  488. pdata->phys_proc_id = pdev->id;
  489. platform_set_drvdata(pdev, pdata);
  490. pdata->hwmon_dev = hwmon_device_register(&pdev->dev);
  491. if (IS_ERR(pdata->hwmon_dev)) {
  492. err = PTR_ERR(pdata->hwmon_dev);
  493. dev_err(&pdev->dev, "Class registration failed (%d)\n", err);
  494. goto exit_name;
  495. }
  496. return 0;
  497. exit_name:
  498. device_remove_file(&pdev->dev, &pdata->name_attr);
  499. platform_set_drvdata(pdev, NULL);
  500. exit_free:
  501. kfree(pdata);
  502. return err;
  503. }
  504. static int __devexit coretemp_remove(struct platform_device *pdev)
  505. {
  506. struct platform_data *pdata = platform_get_drvdata(pdev);
  507. int i;
  508. for (i = MAX_CORE_DATA - 1; i >= 0; --i)
  509. if (pdata->core_data[i])
  510. coretemp_remove_core(pdata, &pdev->dev, i);
  511. device_remove_file(&pdev->dev, &pdata->name_attr);
  512. hwmon_device_unregister(pdata->hwmon_dev);
  513. platform_set_drvdata(pdev, NULL);
  514. kfree(pdata);
  515. return 0;
  516. }
  517. static struct platform_driver coretemp_driver = {
  518. .driver = {
  519. .owner = THIS_MODULE,
  520. .name = DRVNAME,
  521. },
  522. .probe = coretemp_probe,
  523. .remove = __devexit_p(coretemp_remove),
  524. };
  525. static int __cpuinit coretemp_device_add(unsigned int cpu)
  526. {
  527. int err;
  528. struct platform_device *pdev;
  529. struct pdev_entry *pdev_entry;
  530. mutex_lock(&pdev_list_mutex);
  531. pdev = platform_device_alloc(DRVNAME, TO_PHYS_ID(cpu));
  532. if (!pdev) {
  533. err = -ENOMEM;
  534. pr_err("Device allocation failed\n");
  535. goto exit;
  536. }
  537. pdev_entry = kzalloc(sizeof(struct pdev_entry), GFP_KERNEL);
  538. if (!pdev_entry) {
  539. err = -ENOMEM;
  540. goto exit_device_put;
  541. }
  542. err = platform_device_add(pdev);
  543. if (err) {
  544. pr_err("Device addition failed (%d)\n", err);
  545. goto exit_device_free;
  546. }
  547. pdev_entry->pdev = pdev;
  548. pdev_entry->phys_proc_id = pdev->id;
  549. list_add_tail(&pdev_entry->list, &pdev_list);
  550. mutex_unlock(&pdev_list_mutex);
  551. return 0;
  552. exit_device_free:
  553. kfree(pdev_entry);
  554. exit_device_put:
  555. platform_device_put(pdev);
  556. exit:
  557. mutex_unlock(&pdev_list_mutex);
  558. return err;
  559. }
  560. static void __cpuinit coretemp_device_remove(unsigned int cpu)
  561. {
  562. struct pdev_entry *p, *n;
  563. u16 phys_proc_id = TO_PHYS_ID(cpu);
  564. mutex_lock(&pdev_list_mutex);
  565. list_for_each_entry_safe(p, n, &pdev_list, list) {
  566. if (p->phys_proc_id != phys_proc_id)
  567. continue;
  568. platform_device_unregister(p->pdev);
  569. list_del(&p->list);
  570. kfree(p);
  571. }
  572. mutex_unlock(&pdev_list_mutex);
  573. }
  574. static bool __cpuinit is_any_core_online(struct platform_data *pdata)
  575. {
  576. int i;
  577. /* Find online cores, except pkgtemp data */
  578. for (i = MAX_CORE_DATA - 1; i >= 0; --i) {
  579. if (pdata->core_data[i] &&
  580. !pdata->core_data[i]->is_pkg_data) {
  581. return true;
  582. }
  583. }
  584. return false;
  585. }
  586. static void __cpuinit get_core_online(unsigned int cpu)
  587. {
  588. struct cpuinfo_x86 *c = &cpu_data(cpu);
  589. struct platform_device *pdev = coretemp_get_pdev(cpu);
  590. int err;
  591. /*
  592. * CPUID.06H.EAX[0] indicates whether the CPU has thermal
  593. * sensors. We check this bit only, all the early CPUs
  594. * without thermal sensors will be filtered out.
  595. */
  596. if (!cpu_has(c, X86_FEATURE_DTHERM))
  597. return;
  598. if (!pdev) {
  599. /* Check the microcode version of the CPU */
  600. if (chk_ucode_version(cpu))
  601. return;
  602. /*
  603. * Alright, we have DTS support.
  604. * We are bringing the _first_ core in this pkg
  605. * online. So, initialize per-pkg data structures and
  606. * then bring this core online.
  607. */
  608. err = coretemp_device_add(cpu);
  609. if (err)
  610. return;
  611. /*
  612. * Check whether pkgtemp support is available.
  613. * If so, add interfaces for pkgtemp.
  614. */
  615. if (cpu_has(c, X86_FEATURE_PTS))
  616. coretemp_add_core(cpu, 1);
  617. }
  618. /*
  619. * Physical CPU device already exists.
  620. * So, just add interfaces for this core.
  621. */
  622. coretemp_add_core(cpu, 0);
  623. }
  624. static void __cpuinit put_core_offline(unsigned int cpu)
  625. {
  626. int i, indx;
  627. struct platform_data *pdata;
  628. struct platform_device *pdev = coretemp_get_pdev(cpu);
  629. /* If the physical CPU device does not exist, just return */
  630. if (!pdev)
  631. return;
  632. pdata = platform_get_drvdata(pdev);
  633. indx = TO_ATTR_NO(cpu);
  634. /* The core id is too big, just return */
  635. if (indx > MAX_CORE_DATA - 1)
  636. return;
  637. if (pdata->core_data[indx] && pdata->core_data[indx]->cpu == cpu)
  638. coretemp_remove_core(pdata, &pdev->dev, indx);
  639. /*
  640. * If a HT sibling of a core is taken offline, but another HT sibling
  641. * of the same core is still online, register the alternate sibling.
  642. * This ensures that exactly one set of attributes is provided as long
  643. * as at least one HT sibling of a core is online.
  644. */
  645. for_each_sibling(i, cpu) {
  646. if (i != cpu) {
  647. get_core_online(i);
  648. /*
  649. * Display temperature sensor data for one HT sibling
  650. * per core only, so abort the loop after one such
  651. * sibling has been found.
  652. */
  653. break;
  654. }
  655. }
  656. /*
  657. * If all cores in this pkg are offline, remove the device.
  658. * coretemp_device_remove calls unregister_platform_device,
  659. * which in turn calls coretemp_remove. This removes the
  660. * pkgtemp entry and does other clean ups.
  661. */
  662. if (!is_any_core_online(pdata))
  663. coretemp_device_remove(cpu);
  664. }
  665. static int __cpuinit coretemp_cpu_callback(struct notifier_block *nfb,
  666. unsigned long action, void *hcpu)
  667. {
  668. unsigned int cpu = (unsigned long) hcpu;
  669. switch (action) {
  670. case CPU_ONLINE:
  671. case CPU_DOWN_FAILED:
  672. get_core_online(cpu);
  673. break;
  674. case CPU_DOWN_PREPARE:
  675. put_core_offline(cpu);
  676. break;
  677. }
  678. return NOTIFY_OK;
  679. }
  680. static struct notifier_block coretemp_cpu_notifier __refdata = {
  681. .notifier_call = coretemp_cpu_callback,
  682. };
  683. static const struct x86_cpu_id coretemp_ids[] = {
  684. { X86_VENDOR_INTEL, X86_FAMILY_ANY, X86_MODEL_ANY, X86_FEATURE_DTHERM },
  685. {}
  686. };
  687. MODULE_DEVICE_TABLE(x86cpu, coretemp_ids);
  688. static int __init coretemp_init(void)
  689. {
  690. int i, err = -ENODEV;
  691. /*
  692. * CPUID.06H.EAX[0] indicates whether the CPU has thermal
  693. * sensors. We check this bit only, all the early CPUs
  694. * without thermal sensors will be filtered out.
  695. */
  696. if (!x86_match_cpu(coretemp_ids))
  697. return -ENODEV;
  698. err = platform_driver_register(&coretemp_driver);
  699. if (err)
  700. goto exit;
  701. for_each_online_cpu(i)
  702. get_core_online(i);
  703. #ifndef CONFIG_HOTPLUG_CPU
  704. if (list_empty(&pdev_list)) {
  705. err = -ENODEV;
  706. goto exit_driver_unreg;
  707. }
  708. #endif
  709. register_hotcpu_notifier(&coretemp_cpu_notifier);
  710. return 0;
  711. #ifndef CONFIG_HOTPLUG_CPU
  712. exit_driver_unreg:
  713. platform_driver_unregister(&coretemp_driver);
  714. #endif
  715. exit:
  716. return err;
  717. }
  718. static void __exit coretemp_exit(void)
  719. {
  720. struct pdev_entry *p, *n;
  721. unregister_hotcpu_notifier(&coretemp_cpu_notifier);
  722. mutex_lock(&pdev_list_mutex);
  723. list_for_each_entry_safe(p, n, &pdev_list, list) {
  724. platform_device_unregister(p->pdev);
  725. list_del(&p->list);
  726. kfree(p);
  727. }
  728. mutex_unlock(&pdev_list_mutex);
  729. platform_driver_unregister(&coretemp_driver);
  730. }
  731. MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
  732. MODULE_DESCRIPTION("Intel Core temperature monitor");
  733. MODULE_LICENSE("GPL");
  734. module_init(coretemp_init)
  735. module_exit(coretemp_exit)