i5k_amb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * A hwmon driver for the Intel 5000 series chipset FB-DIMM AMB
  4. * temperature sensors
  5. * Copyright (C) 2007 IBM
  6. *
  7. * Author: Darrick J. Wong <darrick.wong@oracle.com>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/hwmon.h>
  11. #include <linux/hwmon-sysfs.h>
  12. #include <linux/err.h>
  13. #include <linux/mutex.h>
  14. #include <linux/log2.h>
  15. #include <linux/pci.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/slab.h>
  18. #define DRVNAME "i5k_amb"
  19. #define I5K_REG_AMB_BASE_ADDR 0x48
  20. #define I5K_REG_AMB_LEN_ADDR 0x50
  21. #define I5K_REG_CHAN0_PRESENCE_ADDR 0x64
  22. #define I5K_REG_CHAN1_PRESENCE_ADDR 0x66
  23. #define AMB_REG_TEMP_MIN_ADDR 0x80
  24. #define AMB_REG_TEMP_MID_ADDR 0x81
  25. #define AMB_REG_TEMP_MAX_ADDR 0x82
  26. #define AMB_REG_TEMP_STATUS_ADDR 0x84
  27. #define AMB_REG_TEMP_ADDR 0x85
  28. #define AMB_CONFIG_SIZE 2048
  29. #define AMB_FUNC_3_OFFSET 768
  30. static unsigned long amb_reg_temp_status(unsigned int amb)
  31. {
  32. return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_STATUS_ADDR +
  33. AMB_CONFIG_SIZE * amb;
  34. }
  35. static unsigned long amb_reg_temp_min(unsigned int amb)
  36. {
  37. return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_MIN_ADDR +
  38. AMB_CONFIG_SIZE * amb;
  39. }
  40. static unsigned long amb_reg_temp_mid(unsigned int amb)
  41. {
  42. return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_MID_ADDR +
  43. AMB_CONFIG_SIZE * amb;
  44. }
  45. static unsigned long amb_reg_temp_max(unsigned int amb)
  46. {
  47. return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_MAX_ADDR +
  48. AMB_CONFIG_SIZE * amb;
  49. }
  50. static unsigned long amb_reg_temp(unsigned int amb)
  51. {
  52. return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_ADDR +
  53. AMB_CONFIG_SIZE * amb;
  54. }
  55. #define MAX_MEM_CHANNELS 4
  56. #define MAX_AMBS_PER_CHANNEL 16
  57. #define MAX_AMBS (MAX_MEM_CHANNELS * \
  58. MAX_AMBS_PER_CHANNEL)
  59. #define CHANNEL_SHIFT 4
  60. #define DIMM_MASK 0xF
  61. /*
  62. * Ugly hack: For some reason the highest bit is set if there
  63. * are _any_ DIMMs in the channel. Attempting to read from
  64. * this "high-order" AMB results in a memory bus error, so
  65. * for now we'll just ignore that top bit, even though that
  66. * might prevent us from seeing the 16th DIMM in the channel.
  67. */
  68. #define REAL_MAX_AMBS_PER_CHANNEL 15
  69. #define KNOBS_PER_AMB 6
  70. static unsigned long amb_num_from_reg(unsigned int byte_num, unsigned int bit)
  71. {
  72. return byte_num * MAX_AMBS_PER_CHANNEL + bit;
  73. }
  74. #define AMB_SYSFS_NAME_LEN 16
  75. struct i5k_device_attribute {
  76. struct sensor_device_attribute s_attr;
  77. char name[AMB_SYSFS_NAME_LEN];
  78. };
  79. struct i5k_amb_data {
  80. struct device *hwmon_dev;
  81. unsigned long amb_base;
  82. unsigned long amb_len;
  83. u16 amb_present[MAX_MEM_CHANNELS];
  84. void __iomem *amb_mmio;
  85. struct i5k_device_attribute *attrs;
  86. unsigned int num_attrs;
  87. };
  88. static ssize_t name_show(struct device *dev, struct device_attribute *devattr,
  89. char *buf)
  90. {
  91. return sprintf(buf, "%s\n", DRVNAME);
  92. }
  93. static DEVICE_ATTR_RO(name);
  94. static struct platform_device *amb_pdev;
  95. static u8 amb_read_byte(struct i5k_amb_data *data, unsigned long offset)
  96. {
  97. return ioread8(data->amb_mmio + offset);
  98. }
  99. static void amb_write_byte(struct i5k_amb_data *data, unsigned long offset,
  100. u8 val)
  101. {
  102. iowrite8(val, data->amb_mmio + offset);
  103. }
  104. static ssize_t show_amb_alarm(struct device *dev,
  105. struct device_attribute *devattr,
  106. char *buf)
  107. {
  108. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  109. struct i5k_amb_data *data = dev_get_drvdata(dev);
  110. if (!(amb_read_byte(data, amb_reg_temp_status(attr->index)) & 0x20) &&
  111. (amb_read_byte(data, amb_reg_temp_status(attr->index)) & 0x8))
  112. return sprintf(buf, "1\n");
  113. else
  114. return sprintf(buf, "0\n");
  115. }
  116. static ssize_t store_amb_min(struct device *dev,
  117. struct device_attribute *devattr,
  118. const char *buf,
  119. size_t count)
  120. {
  121. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  122. struct i5k_amb_data *data = dev_get_drvdata(dev);
  123. unsigned long temp;
  124. int ret = kstrtoul(buf, 10, &temp);
  125. if (ret < 0)
  126. return ret;
  127. temp = temp / 500;
  128. if (temp > 255)
  129. temp = 255;
  130. amb_write_byte(data, amb_reg_temp_min(attr->index), temp);
  131. return count;
  132. }
  133. static ssize_t store_amb_mid(struct device *dev,
  134. struct device_attribute *devattr,
  135. const char *buf,
  136. size_t count)
  137. {
  138. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  139. struct i5k_amb_data *data = dev_get_drvdata(dev);
  140. unsigned long temp;
  141. int ret = kstrtoul(buf, 10, &temp);
  142. if (ret < 0)
  143. return ret;
  144. temp = temp / 500;
  145. if (temp > 255)
  146. temp = 255;
  147. amb_write_byte(data, amb_reg_temp_mid(attr->index), temp);
  148. return count;
  149. }
  150. static ssize_t store_amb_max(struct device *dev,
  151. struct device_attribute *devattr,
  152. const char *buf,
  153. size_t count)
  154. {
  155. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  156. struct i5k_amb_data *data = dev_get_drvdata(dev);
  157. unsigned long temp;
  158. int ret = kstrtoul(buf, 10, &temp);
  159. if (ret < 0)
  160. return ret;
  161. temp = temp / 500;
  162. if (temp > 255)
  163. temp = 255;
  164. amb_write_byte(data, amb_reg_temp_max(attr->index), temp);
  165. return count;
  166. }
  167. static ssize_t show_amb_min(struct device *dev,
  168. struct device_attribute *devattr,
  169. char *buf)
  170. {
  171. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  172. struct i5k_amb_data *data = dev_get_drvdata(dev);
  173. return sprintf(buf, "%d\n",
  174. 500 * amb_read_byte(data, amb_reg_temp_min(attr->index)));
  175. }
  176. static ssize_t show_amb_mid(struct device *dev,
  177. struct device_attribute *devattr,
  178. char *buf)
  179. {
  180. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  181. struct i5k_amb_data *data = dev_get_drvdata(dev);
  182. return sprintf(buf, "%d\n",
  183. 500 * amb_read_byte(data, amb_reg_temp_mid(attr->index)));
  184. }
  185. static ssize_t show_amb_max(struct device *dev,
  186. struct device_attribute *devattr,
  187. char *buf)
  188. {
  189. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  190. struct i5k_amb_data *data = dev_get_drvdata(dev);
  191. return sprintf(buf, "%d\n",
  192. 500 * amb_read_byte(data, amb_reg_temp_max(attr->index)));
  193. }
  194. static ssize_t show_amb_temp(struct device *dev,
  195. struct device_attribute *devattr,
  196. char *buf)
  197. {
  198. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  199. struct i5k_amb_data *data = dev_get_drvdata(dev);
  200. return sprintf(buf, "%d\n",
  201. 500 * amb_read_byte(data, amb_reg_temp(attr->index)));
  202. }
  203. static ssize_t show_label(struct device *dev,
  204. struct device_attribute *devattr,
  205. char *buf)
  206. {
  207. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  208. return sprintf(buf, "Ch. %d DIMM %d\n", attr->index >> CHANNEL_SHIFT,
  209. attr->index & DIMM_MASK);
  210. }
  211. static int i5k_amb_hwmon_init(struct platform_device *pdev)
  212. {
  213. int i, j, k, d = 0;
  214. u16 c;
  215. int res = 0;
  216. int num_ambs = 0;
  217. struct i5k_amb_data *data = platform_get_drvdata(pdev);
  218. /* Count the number of AMBs found */
  219. /* ignore the high-order bit, see "Ugly hack" comment above */
  220. for (i = 0; i < MAX_MEM_CHANNELS; i++)
  221. num_ambs += hweight16(data->amb_present[i] & 0x7fff);
  222. /* Set up sysfs stuff */
  223. data->attrs = kzalloc(array3_size(num_ambs, KNOBS_PER_AMB,
  224. sizeof(*data->attrs)),
  225. GFP_KERNEL);
  226. if (!data->attrs)
  227. return -ENOMEM;
  228. data->num_attrs = 0;
  229. for (i = 0; i < MAX_MEM_CHANNELS; i++) {
  230. c = data->amb_present[i];
  231. for (j = 0; j < REAL_MAX_AMBS_PER_CHANNEL; j++, c >>= 1) {
  232. struct i5k_device_attribute *iattr;
  233. k = amb_num_from_reg(i, j);
  234. if (!(c & 0x1))
  235. continue;
  236. d++;
  237. /* sysfs label */
  238. iattr = data->attrs + data->num_attrs;
  239. snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
  240. "temp%d_label", d);
  241. iattr->s_attr.dev_attr.attr.name = iattr->name;
  242. iattr->s_attr.dev_attr.attr.mode = 0444;
  243. iattr->s_attr.dev_attr.show = show_label;
  244. iattr->s_attr.index = k;
  245. sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
  246. res = device_create_file(&pdev->dev,
  247. &iattr->s_attr.dev_attr);
  248. if (res)
  249. goto exit_remove;
  250. data->num_attrs++;
  251. /* Temperature sysfs knob */
  252. iattr = data->attrs + data->num_attrs;
  253. snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
  254. "temp%d_input", d);
  255. iattr->s_attr.dev_attr.attr.name = iattr->name;
  256. iattr->s_attr.dev_attr.attr.mode = 0444;
  257. iattr->s_attr.dev_attr.show = show_amb_temp;
  258. iattr->s_attr.index = k;
  259. sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
  260. res = device_create_file(&pdev->dev,
  261. &iattr->s_attr.dev_attr);
  262. if (res)
  263. goto exit_remove;
  264. data->num_attrs++;
  265. /* Temperature min sysfs knob */
  266. iattr = data->attrs + data->num_attrs;
  267. snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
  268. "temp%d_min", d);
  269. iattr->s_attr.dev_attr.attr.name = iattr->name;
  270. iattr->s_attr.dev_attr.attr.mode = 0644;
  271. iattr->s_attr.dev_attr.show = show_amb_min;
  272. iattr->s_attr.dev_attr.store = store_amb_min;
  273. iattr->s_attr.index = k;
  274. sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
  275. res = device_create_file(&pdev->dev,
  276. &iattr->s_attr.dev_attr);
  277. if (res)
  278. goto exit_remove;
  279. data->num_attrs++;
  280. /* Temperature mid sysfs knob */
  281. iattr = data->attrs + data->num_attrs;
  282. snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
  283. "temp%d_mid", d);
  284. iattr->s_attr.dev_attr.attr.name = iattr->name;
  285. iattr->s_attr.dev_attr.attr.mode = 0644;
  286. iattr->s_attr.dev_attr.show = show_amb_mid;
  287. iattr->s_attr.dev_attr.store = store_amb_mid;
  288. iattr->s_attr.index = k;
  289. sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
  290. res = device_create_file(&pdev->dev,
  291. &iattr->s_attr.dev_attr);
  292. if (res)
  293. goto exit_remove;
  294. data->num_attrs++;
  295. /* Temperature max sysfs knob */
  296. iattr = data->attrs + data->num_attrs;
  297. snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
  298. "temp%d_max", d);
  299. iattr->s_attr.dev_attr.attr.name = iattr->name;
  300. iattr->s_attr.dev_attr.attr.mode = 0644;
  301. iattr->s_attr.dev_attr.show = show_amb_max;
  302. iattr->s_attr.dev_attr.store = store_amb_max;
  303. iattr->s_attr.index = k;
  304. sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
  305. res = device_create_file(&pdev->dev,
  306. &iattr->s_attr.dev_attr);
  307. if (res)
  308. goto exit_remove;
  309. data->num_attrs++;
  310. /* Temperature alarm sysfs knob */
  311. iattr = data->attrs + data->num_attrs;
  312. snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
  313. "temp%d_alarm", d);
  314. iattr->s_attr.dev_attr.attr.name = iattr->name;
  315. iattr->s_attr.dev_attr.attr.mode = 0444;
  316. iattr->s_attr.dev_attr.show = show_amb_alarm;
  317. iattr->s_attr.index = k;
  318. sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
  319. res = device_create_file(&pdev->dev,
  320. &iattr->s_attr.dev_attr);
  321. if (res)
  322. goto exit_remove;
  323. data->num_attrs++;
  324. }
  325. }
  326. res = device_create_file(&pdev->dev, &dev_attr_name);
  327. if (res)
  328. goto exit_remove;
  329. data->hwmon_dev = hwmon_device_register(&pdev->dev);
  330. if (IS_ERR(data->hwmon_dev)) {
  331. res = PTR_ERR(data->hwmon_dev);
  332. goto exit_remove;
  333. }
  334. return res;
  335. exit_remove:
  336. device_remove_file(&pdev->dev, &dev_attr_name);
  337. for (i = 0; i < data->num_attrs; i++)
  338. device_remove_file(&pdev->dev, &data->attrs[i].s_attr.dev_attr);
  339. kfree(data->attrs);
  340. return res;
  341. }
  342. static int i5k_amb_add(void)
  343. {
  344. int res;
  345. /* only ever going to be one of these */
  346. amb_pdev = platform_device_alloc(DRVNAME, 0);
  347. if (!amb_pdev)
  348. return -ENOMEM;
  349. res = platform_device_add(amb_pdev);
  350. if (res)
  351. goto err;
  352. return 0;
  353. err:
  354. platform_device_put(amb_pdev);
  355. return res;
  356. }
  357. static int i5k_find_amb_registers(struct i5k_amb_data *data,
  358. unsigned long devid)
  359. {
  360. struct pci_dev *pcidev;
  361. u32 val32;
  362. int res = -ENODEV;
  363. /* Find AMB register memory space */
  364. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  365. devid,
  366. NULL);
  367. if (!pcidev)
  368. return -ENODEV;
  369. pci_read_config_dword(pcidev, I5K_REG_AMB_BASE_ADDR, &val32);
  370. if (val32 == (u32)~0)
  371. goto out;
  372. data->amb_base = val32;
  373. pci_read_config_dword(pcidev, I5K_REG_AMB_LEN_ADDR, &val32);
  374. if (val32 == (u32)~0)
  375. goto out;
  376. data->amb_len = val32;
  377. /* Is it big enough? */
  378. if (data->amb_len < AMB_CONFIG_SIZE * MAX_AMBS) {
  379. dev_err(&pcidev->dev, "AMB region too small!\n");
  380. goto out;
  381. }
  382. res = 0;
  383. out:
  384. pci_dev_put(pcidev);
  385. return res;
  386. }
  387. static int i5k_channel_probe(u16 *amb_present, unsigned long dev_id)
  388. {
  389. struct pci_dev *pcidev;
  390. u16 val16;
  391. int res = -ENODEV;
  392. /* Copy the DIMM presence map for these two channels */
  393. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL, dev_id, NULL);
  394. if (!pcidev)
  395. return -ENODEV;
  396. pci_read_config_word(pcidev, I5K_REG_CHAN0_PRESENCE_ADDR, &val16);
  397. if (val16 == (u16)~0)
  398. goto out;
  399. amb_present[0] = val16;
  400. pci_read_config_word(pcidev, I5K_REG_CHAN1_PRESENCE_ADDR, &val16);
  401. if (val16 == (u16)~0)
  402. goto out;
  403. amb_present[1] = val16;
  404. res = 0;
  405. out:
  406. pci_dev_put(pcidev);
  407. return res;
  408. }
  409. static struct {
  410. unsigned long err;
  411. unsigned long fbd0;
  412. } chipset_ids[] = {
  413. { PCI_DEVICE_ID_INTEL_5000_ERR, PCI_DEVICE_ID_INTEL_5000_FBD0 },
  414. { PCI_DEVICE_ID_INTEL_5400_ERR, PCI_DEVICE_ID_INTEL_5400_FBD0 },
  415. { 0, 0 }
  416. };
  417. #ifdef MODULE
  418. static const struct pci_device_id i5k_amb_ids[] = {
  419. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_5000_ERR) },
  420. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_5400_ERR) },
  421. { 0, }
  422. };
  423. MODULE_DEVICE_TABLE(pci, i5k_amb_ids);
  424. #endif
  425. static int i5k_amb_probe(struct platform_device *pdev)
  426. {
  427. struct i5k_amb_data *data;
  428. struct resource *reso;
  429. int i, res;
  430. data = kzalloc(sizeof(*data), GFP_KERNEL);
  431. if (!data)
  432. return -ENOMEM;
  433. /* Figure out where the AMB registers live */
  434. i = 0;
  435. do {
  436. res = i5k_find_amb_registers(data, chipset_ids[i].err);
  437. if (res == 0)
  438. break;
  439. i++;
  440. } while (chipset_ids[i].err);
  441. if (res)
  442. goto err;
  443. /* Copy the DIMM presence map for the first two channels */
  444. res = i5k_channel_probe(&data->amb_present[0], chipset_ids[i].fbd0);
  445. if (res)
  446. goto err;
  447. /* Copy the DIMM presence map for the optional second two channels */
  448. i5k_channel_probe(&data->amb_present[2], chipset_ids[i].fbd0 + 1);
  449. /* Set up resource regions */
  450. reso = request_mem_region(data->amb_base, data->amb_len, DRVNAME);
  451. if (!reso) {
  452. res = -EBUSY;
  453. goto err;
  454. }
  455. data->amb_mmio = ioremap(data->amb_base, data->amb_len);
  456. if (!data->amb_mmio) {
  457. res = -EBUSY;
  458. goto err_map_failed;
  459. }
  460. platform_set_drvdata(pdev, data);
  461. res = i5k_amb_hwmon_init(pdev);
  462. if (res)
  463. goto err_init_failed;
  464. return res;
  465. err_init_failed:
  466. iounmap(data->amb_mmio);
  467. err_map_failed:
  468. release_mem_region(data->amb_base, data->amb_len);
  469. err:
  470. kfree(data);
  471. return res;
  472. }
  473. static int i5k_amb_remove(struct platform_device *pdev)
  474. {
  475. int i;
  476. struct i5k_amb_data *data = platform_get_drvdata(pdev);
  477. hwmon_device_unregister(data->hwmon_dev);
  478. device_remove_file(&pdev->dev, &dev_attr_name);
  479. for (i = 0; i < data->num_attrs; i++)
  480. device_remove_file(&pdev->dev, &data->attrs[i].s_attr.dev_attr);
  481. kfree(data->attrs);
  482. iounmap(data->amb_mmio);
  483. release_mem_region(data->amb_base, data->amb_len);
  484. kfree(data);
  485. return 0;
  486. }
  487. static struct platform_driver i5k_amb_driver = {
  488. .driver = {
  489. .name = DRVNAME,
  490. },
  491. .probe = i5k_amb_probe,
  492. .remove = i5k_amb_remove,
  493. };
  494. static int __init i5k_amb_init(void)
  495. {
  496. int res;
  497. res = platform_driver_register(&i5k_amb_driver);
  498. if (res)
  499. return res;
  500. res = i5k_amb_add();
  501. if (res)
  502. platform_driver_unregister(&i5k_amb_driver);
  503. return res;
  504. }
  505. static void __exit i5k_amb_exit(void)
  506. {
  507. platform_device_unregister(amb_pdev);
  508. platform_driver_unregister(&i5k_amb_driver);
  509. }
  510. MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>");
  511. MODULE_DESCRIPTION("Intel 5000 chipset FB-DIMM AMB temperature sensor");
  512. MODULE_LICENSE("GPL");
  513. module_init(i5k_amb_init);
  514. module_exit(i5k_amb_exit);