i5k_amb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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 DEVICE_STRING_ATTR_RO(name, 0444, DRVNAME);
  89. static struct platform_device *amb_pdev;
  90. static u8 amb_read_byte(struct i5k_amb_data *data, unsigned long offset)
  91. {
  92. return ioread8(data->amb_mmio + offset);
  93. }
  94. static void amb_write_byte(struct i5k_amb_data *data, unsigned long offset,
  95. u8 val)
  96. {
  97. iowrite8(val, data->amb_mmio + offset);
  98. }
  99. static ssize_t show_amb_alarm(struct device *dev,
  100. struct device_attribute *devattr,
  101. char *buf)
  102. {
  103. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  104. struct i5k_amb_data *data = dev_get_drvdata(dev);
  105. if (!(amb_read_byte(data, amb_reg_temp_status(attr->index)) & 0x20) &&
  106. (amb_read_byte(data, amb_reg_temp_status(attr->index)) & 0x8))
  107. return sprintf(buf, "1\n");
  108. else
  109. return sprintf(buf, "0\n");
  110. }
  111. static ssize_t store_amb_min(struct device *dev,
  112. struct device_attribute *devattr,
  113. const char *buf,
  114. size_t count)
  115. {
  116. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  117. struct i5k_amb_data *data = dev_get_drvdata(dev);
  118. unsigned long temp;
  119. int ret = kstrtoul(buf, 10, &temp);
  120. if (ret < 0)
  121. return ret;
  122. temp = temp / 500;
  123. if (temp > 255)
  124. temp = 255;
  125. amb_write_byte(data, amb_reg_temp_min(attr->index), temp);
  126. return count;
  127. }
  128. static ssize_t store_amb_mid(struct device *dev,
  129. struct device_attribute *devattr,
  130. const char *buf,
  131. size_t count)
  132. {
  133. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  134. struct i5k_amb_data *data = dev_get_drvdata(dev);
  135. unsigned long temp;
  136. int ret = kstrtoul(buf, 10, &temp);
  137. if (ret < 0)
  138. return ret;
  139. temp = temp / 500;
  140. if (temp > 255)
  141. temp = 255;
  142. amb_write_byte(data, amb_reg_temp_mid(attr->index), temp);
  143. return count;
  144. }
  145. static ssize_t store_amb_max(struct device *dev,
  146. struct device_attribute *devattr,
  147. const char *buf,
  148. size_t count)
  149. {
  150. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  151. struct i5k_amb_data *data = dev_get_drvdata(dev);
  152. unsigned long temp;
  153. int ret = kstrtoul(buf, 10, &temp);
  154. if (ret < 0)
  155. return ret;
  156. temp = temp / 500;
  157. if (temp > 255)
  158. temp = 255;
  159. amb_write_byte(data, amb_reg_temp_max(attr->index), temp);
  160. return count;
  161. }
  162. static ssize_t show_amb_min(struct device *dev,
  163. struct device_attribute *devattr,
  164. char *buf)
  165. {
  166. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  167. struct i5k_amb_data *data = dev_get_drvdata(dev);
  168. return sprintf(buf, "%d\n",
  169. 500 * amb_read_byte(data, amb_reg_temp_min(attr->index)));
  170. }
  171. static ssize_t show_amb_mid(struct device *dev,
  172. struct device_attribute *devattr,
  173. char *buf)
  174. {
  175. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  176. struct i5k_amb_data *data = dev_get_drvdata(dev);
  177. return sprintf(buf, "%d\n",
  178. 500 * amb_read_byte(data, amb_reg_temp_mid(attr->index)));
  179. }
  180. static ssize_t show_amb_max(struct device *dev,
  181. struct device_attribute *devattr,
  182. char *buf)
  183. {
  184. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  185. struct i5k_amb_data *data = dev_get_drvdata(dev);
  186. return sprintf(buf, "%d\n",
  187. 500 * amb_read_byte(data, amb_reg_temp_max(attr->index)));
  188. }
  189. static ssize_t show_amb_temp(struct device *dev,
  190. struct device_attribute *devattr,
  191. char *buf)
  192. {
  193. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  194. struct i5k_amb_data *data = dev_get_drvdata(dev);
  195. return sprintf(buf, "%d\n",
  196. 500 * amb_read_byte(data, amb_reg_temp(attr->index)));
  197. }
  198. static ssize_t show_label(struct device *dev,
  199. struct device_attribute *devattr,
  200. char *buf)
  201. {
  202. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  203. return sprintf(buf, "Ch. %d DIMM %d\n", attr->index >> CHANNEL_SHIFT,
  204. attr->index & DIMM_MASK);
  205. }
  206. static int i5k_amb_hwmon_init(struct platform_device *pdev)
  207. {
  208. int i, j, k, d = 0;
  209. u16 c;
  210. int res = 0;
  211. int num_ambs = 0;
  212. struct i5k_amb_data *data = platform_get_drvdata(pdev);
  213. /* Count the number of AMBs found */
  214. /* ignore the high-order bit, see "Ugly hack" comment above */
  215. for (i = 0; i < MAX_MEM_CHANNELS; i++)
  216. num_ambs += hweight16(data->amb_present[i] & 0x7fff);
  217. /* Set up sysfs stuff */
  218. data->attrs = kzalloc(array3_size(num_ambs, KNOBS_PER_AMB,
  219. sizeof(*data->attrs)),
  220. GFP_KERNEL);
  221. if (!data->attrs)
  222. return -ENOMEM;
  223. data->num_attrs = 0;
  224. for (i = 0; i < MAX_MEM_CHANNELS; i++) {
  225. c = data->amb_present[i];
  226. for (j = 0; j < REAL_MAX_AMBS_PER_CHANNEL; j++, c >>= 1) {
  227. struct i5k_device_attribute *iattr;
  228. k = amb_num_from_reg(i, j);
  229. if (!(c & 0x1))
  230. continue;
  231. d++;
  232. /* sysfs label */
  233. iattr = data->attrs + data->num_attrs;
  234. snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
  235. "temp%d_label", d);
  236. iattr->s_attr.dev_attr.attr.name = iattr->name;
  237. iattr->s_attr.dev_attr.attr.mode = 0444;
  238. iattr->s_attr.dev_attr.show = show_label;
  239. iattr->s_attr.index = k;
  240. sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
  241. res = device_create_file(&pdev->dev,
  242. &iattr->s_attr.dev_attr);
  243. if (res)
  244. goto exit_remove;
  245. data->num_attrs++;
  246. /* Temperature sysfs knob */
  247. iattr = data->attrs + data->num_attrs;
  248. snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
  249. "temp%d_input", d);
  250. iattr->s_attr.dev_attr.attr.name = iattr->name;
  251. iattr->s_attr.dev_attr.attr.mode = 0444;
  252. iattr->s_attr.dev_attr.show = show_amb_temp;
  253. iattr->s_attr.index = k;
  254. sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
  255. res = device_create_file(&pdev->dev,
  256. &iattr->s_attr.dev_attr);
  257. if (res)
  258. goto exit_remove;
  259. data->num_attrs++;
  260. /* Temperature min sysfs knob */
  261. iattr = data->attrs + data->num_attrs;
  262. snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
  263. "temp%d_min", d);
  264. iattr->s_attr.dev_attr.attr.name = iattr->name;
  265. iattr->s_attr.dev_attr.attr.mode = 0644;
  266. iattr->s_attr.dev_attr.show = show_amb_min;
  267. iattr->s_attr.dev_attr.store = store_amb_min;
  268. iattr->s_attr.index = k;
  269. sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
  270. res = device_create_file(&pdev->dev,
  271. &iattr->s_attr.dev_attr);
  272. if (res)
  273. goto exit_remove;
  274. data->num_attrs++;
  275. /* Temperature mid sysfs knob */
  276. iattr = data->attrs + data->num_attrs;
  277. snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
  278. "temp%d_mid", d);
  279. iattr->s_attr.dev_attr.attr.name = iattr->name;
  280. iattr->s_attr.dev_attr.attr.mode = 0644;
  281. iattr->s_attr.dev_attr.show = show_amb_mid;
  282. iattr->s_attr.dev_attr.store = store_amb_mid;
  283. iattr->s_attr.index = k;
  284. sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
  285. res = device_create_file(&pdev->dev,
  286. &iattr->s_attr.dev_attr);
  287. if (res)
  288. goto exit_remove;
  289. data->num_attrs++;
  290. /* Temperature max sysfs knob */
  291. iattr = data->attrs + data->num_attrs;
  292. snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
  293. "temp%d_max", d);
  294. iattr->s_attr.dev_attr.attr.name = iattr->name;
  295. iattr->s_attr.dev_attr.attr.mode = 0644;
  296. iattr->s_attr.dev_attr.show = show_amb_max;
  297. iattr->s_attr.dev_attr.store = store_amb_max;
  298. iattr->s_attr.index = k;
  299. sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
  300. res = device_create_file(&pdev->dev,
  301. &iattr->s_attr.dev_attr);
  302. if (res)
  303. goto exit_remove;
  304. data->num_attrs++;
  305. /* Temperature alarm sysfs knob */
  306. iattr = data->attrs + data->num_attrs;
  307. snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
  308. "temp%d_alarm", d);
  309. iattr->s_attr.dev_attr.attr.name = iattr->name;
  310. iattr->s_attr.dev_attr.attr.mode = 0444;
  311. iattr->s_attr.dev_attr.show = show_amb_alarm;
  312. iattr->s_attr.index = k;
  313. sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
  314. res = device_create_file(&pdev->dev,
  315. &iattr->s_attr.dev_attr);
  316. if (res)
  317. goto exit_remove;
  318. data->num_attrs++;
  319. }
  320. }
  321. res = device_create_file(&pdev->dev, &dev_attr_name.attr);
  322. if (res)
  323. goto exit_remove;
  324. data->hwmon_dev = hwmon_device_register(&pdev->dev);
  325. if (IS_ERR(data->hwmon_dev)) {
  326. res = PTR_ERR(data->hwmon_dev);
  327. goto exit_remove;
  328. }
  329. return res;
  330. exit_remove:
  331. device_remove_file(&pdev->dev, &dev_attr_name.attr);
  332. for (i = 0; i < data->num_attrs; i++)
  333. device_remove_file(&pdev->dev, &data->attrs[i].s_attr.dev_attr);
  334. kfree(data->attrs);
  335. return res;
  336. }
  337. static int i5k_amb_add(void)
  338. {
  339. int res;
  340. /* only ever going to be one of these */
  341. amb_pdev = platform_device_alloc(DRVNAME, 0);
  342. if (!amb_pdev)
  343. return -ENOMEM;
  344. res = platform_device_add(amb_pdev);
  345. if (res)
  346. goto err;
  347. return 0;
  348. err:
  349. platform_device_put(amb_pdev);
  350. return res;
  351. }
  352. static int i5k_find_amb_registers(struct i5k_amb_data *data,
  353. unsigned long devid)
  354. {
  355. struct pci_dev *pcidev;
  356. u32 val32;
  357. int res = -ENODEV;
  358. /* Find AMB register memory space */
  359. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  360. devid,
  361. NULL);
  362. if (!pcidev)
  363. return -ENODEV;
  364. pci_read_config_dword(pcidev, I5K_REG_AMB_BASE_ADDR, &val32);
  365. if (val32 == (u32)~0)
  366. goto out;
  367. data->amb_base = val32;
  368. pci_read_config_dword(pcidev, I5K_REG_AMB_LEN_ADDR, &val32);
  369. if (val32 == (u32)~0)
  370. goto out;
  371. data->amb_len = val32;
  372. /* Is it big enough? */
  373. if (data->amb_len < AMB_CONFIG_SIZE * MAX_AMBS) {
  374. dev_err(&pcidev->dev, "AMB region too small!\n");
  375. goto out;
  376. }
  377. res = 0;
  378. out:
  379. pci_dev_put(pcidev);
  380. return res;
  381. }
  382. static int i5k_channel_probe(u16 *amb_present, unsigned long dev_id)
  383. {
  384. struct pci_dev *pcidev;
  385. u16 val16;
  386. int res = -ENODEV;
  387. /* Copy the DIMM presence map for these two channels */
  388. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL, dev_id, NULL);
  389. if (!pcidev)
  390. return -ENODEV;
  391. pci_read_config_word(pcidev, I5K_REG_CHAN0_PRESENCE_ADDR, &val16);
  392. if (val16 == (u16)~0)
  393. goto out;
  394. amb_present[0] = val16;
  395. pci_read_config_word(pcidev, I5K_REG_CHAN1_PRESENCE_ADDR, &val16);
  396. if (val16 == (u16)~0)
  397. goto out;
  398. amb_present[1] = val16;
  399. res = 0;
  400. out:
  401. pci_dev_put(pcidev);
  402. return res;
  403. }
  404. static struct {
  405. unsigned long err;
  406. unsigned long fbd0;
  407. } chipset_ids[] = {
  408. { PCI_DEVICE_ID_INTEL_5000_ERR, PCI_DEVICE_ID_INTEL_5000_FBD0 },
  409. { PCI_DEVICE_ID_INTEL_5400_ERR, PCI_DEVICE_ID_INTEL_5400_FBD0 },
  410. { 0, 0 }
  411. };
  412. #ifdef MODULE
  413. static const struct pci_device_id i5k_amb_ids[] = {
  414. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_5000_ERR) },
  415. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_5400_ERR) },
  416. { 0, }
  417. };
  418. MODULE_DEVICE_TABLE(pci, i5k_amb_ids);
  419. #endif
  420. static int i5k_amb_probe(struct platform_device *pdev)
  421. {
  422. struct i5k_amb_data *data;
  423. struct resource *reso;
  424. int i, res;
  425. data = kzalloc_obj(*data);
  426. if (!data)
  427. return -ENOMEM;
  428. /* Figure out where the AMB registers live */
  429. i = 0;
  430. do {
  431. res = i5k_find_amb_registers(data, chipset_ids[i].err);
  432. if (res == 0)
  433. break;
  434. i++;
  435. } while (chipset_ids[i].err);
  436. if (res)
  437. goto err;
  438. /* Copy the DIMM presence map for the first two channels */
  439. res = i5k_channel_probe(&data->amb_present[0], chipset_ids[i].fbd0);
  440. if (res)
  441. goto err;
  442. /* Copy the DIMM presence map for the optional second two channels */
  443. i5k_channel_probe(&data->amb_present[2], chipset_ids[i].fbd0 + 1);
  444. /* Set up resource regions */
  445. reso = request_mem_region(data->amb_base, data->amb_len, DRVNAME);
  446. if (!reso) {
  447. res = -EBUSY;
  448. goto err;
  449. }
  450. data->amb_mmio = ioremap(data->amb_base, data->amb_len);
  451. if (!data->amb_mmio) {
  452. res = -EBUSY;
  453. goto err_map_failed;
  454. }
  455. platform_set_drvdata(pdev, data);
  456. res = i5k_amb_hwmon_init(pdev);
  457. if (res)
  458. goto err_init_failed;
  459. return res;
  460. err_init_failed:
  461. iounmap(data->amb_mmio);
  462. err_map_failed:
  463. release_mem_region(data->amb_base, data->amb_len);
  464. err:
  465. kfree(data);
  466. return res;
  467. }
  468. static void i5k_amb_remove(struct platform_device *pdev)
  469. {
  470. int i;
  471. struct i5k_amb_data *data = platform_get_drvdata(pdev);
  472. hwmon_device_unregister(data->hwmon_dev);
  473. device_remove_file(&pdev->dev, &dev_attr_name.attr);
  474. for (i = 0; i < data->num_attrs; i++)
  475. device_remove_file(&pdev->dev, &data->attrs[i].s_attr.dev_attr);
  476. kfree(data->attrs);
  477. iounmap(data->amb_mmio);
  478. release_mem_region(data->amb_base, data->amb_len);
  479. kfree(data);
  480. }
  481. static struct platform_driver i5k_amb_driver = {
  482. .driver = {
  483. .name = DRVNAME,
  484. },
  485. .probe = i5k_amb_probe,
  486. .remove = i5k_amb_remove,
  487. };
  488. static int __init i5k_amb_init(void)
  489. {
  490. int res;
  491. res = platform_driver_register(&i5k_amb_driver);
  492. if (res)
  493. return res;
  494. res = i5k_amb_add();
  495. if (res)
  496. platform_driver_unregister(&i5k_amb_driver);
  497. return res;
  498. }
  499. static void __exit i5k_amb_exit(void)
  500. {
  501. platform_device_unregister(amb_pdev);
  502. platform_driver_unregister(&i5k_amb_driver);
  503. }
  504. MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>");
  505. MODULE_DESCRIPTION("Intel 5000 chipset FB-DIMM AMB temperature sensor");
  506. MODULE_LICENSE("GPL");
  507. module_init(i5k_amb_init);
  508. module_exit(i5k_amb_exit);