coretemp.c 21 KB

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