coretemp.c 24 KB

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