coretemp.c 22 KB

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