intel_menlow.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Intel menlow Driver for thermal management extension
  4. *
  5. * Copyright (C) 2008 Intel Corp
  6. * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
  7. * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
  8. *
  9. * This driver creates the sys I/F for programming the sensors.
  10. * It also implements the driver for intel menlow memory controller (hardware
  11. * id is INT0002) which makes use of the platform specific ACPI methods
  12. * to get/set bandwidth.
  13. */
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/acpi.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/pci.h>
  19. #include <linux/pm.h>
  20. #include <linux/slab.h>
  21. #include <linux/thermal.h>
  22. #include <linux/types.h>
  23. #include <linux/units.h>
  24. MODULE_AUTHOR("Thomas Sujith");
  25. MODULE_AUTHOR("Zhang Rui");
  26. MODULE_DESCRIPTION("Intel Menlow platform specific driver");
  27. MODULE_LICENSE("GPL v2");
  28. /*
  29. * Memory controller device control
  30. */
  31. #define MEMORY_GET_BANDWIDTH "GTHS"
  32. #define MEMORY_SET_BANDWIDTH "STHS"
  33. #define MEMORY_ARG_CUR_BANDWIDTH 1
  34. #define MEMORY_ARG_MAX_BANDWIDTH 0
  35. static void intel_menlow_unregister_sensor(void);
  36. /*
  37. * GTHS returning 'n' would mean that [0,n-1] states are supported
  38. * In that case max_cstate would be n-1
  39. * GTHS returning '0' would mean that no bandwidth control states are supported
  40. */
  41. static int memory_get_max_bandwidth(struct thermal_cooling_device *cdev,
  42. unsigned long *max_state)
  43. {
  44. struct acpi_device *device = cdev->devdata;
  45. acpi_handle handle = device->handle;
  46. unsigned long long value;
  47. struct acpi_object_list arg_list;
  48. union acpi_object arg;
  49. acpi_status status = AE_OK;
  50. arg_list.count = 1;
  51. arg_list.pointer = &arg;
  52. arg.type = ACPI_TYPE_INTEGER;
  53. arg.integer.value = MEMORY_ARG_MAX_BANDWIDTH;
  54. status = acpi_evaluate_integer(handle, MEMORY_GET_BANDWIDTH,
  55. &arg_list, &value);
  56. if (ACPI_FAILURE(status))
  57. return -EFAULT;
  58. if (!value)
  59. return -EINVAL;
  60. *max_state = value - 1;
  61. return 0;
  62. }
  63. static int memory_get_cur_bandwidth(struct thermal_cooling_device *cdev,
  64. unsigned long *value)
  65. {
  66. struct acpi_device *device = cdev->devdata;
  67. acpi_handle handle = device->handle;
  68. unsigned long long result;
  69. struct acpi_object_list arg_list;
  70. union acpi_object arg;
  71. acpi_status status = AE_OK;
  72. arg_list.count = 1;
  73. arg_list.pointer = &arg;
  74. arg.type = ACPI_TYPE_INTEGER;
  75. arg.integer.value = MEMORY_ARG_CUR_BANDWIDTH;
  76. status = acpi_evaluate_integer(handle, MEMORY_GET_BANDWIDTH,
  77. &arg_list, &result);
  78. if (ACPI_FAILURE(status))
  79. return -EFAULT;
  80. *value = result;
  81. return 0;
  82. }
  83. static int memory_set_cur_bandwidth(struct thermal_cooling_device *cdev,
  84. unsigned long state)
  85. {
  86. struct acpi_device *device = cdev->devdata;
  87. acpi_handle handle = device->handle;
  88. struct acpi_object_list arg_list;
  89. union acpi_object arg;
  90. acpi_status status;
  91. unsigned long long temp;
  92. unsigned long max_state;
  93. if (memory_get_max_bandwidth(cdev, &max_state))
  94. return -EFAULT;
  95. if (state > max_state)
  96. return -EINVAL;
  97. arg_list.count = 1;
  98. arg_list.pointer = &arg;
  99. arg.type = ACPI_TYPE_INTEGER;
  100. arg.integer.value = state;
  101. status =
  102. acpi_evaluate_integer(handle, MEMORY_SET_BANDWIDTH, &arg_list,
  103. &temp);
  104. pr_info("Bandwidth value was %ld: status is %d\n", state, status);
  105. if (ACPI_FAILURE(status))
  106. return -EFAULT;
  107. return 0;
  108. }
  109. static const struct thermal_cooling_device_ops memory_cooling_ops = {
  110. .get_max_state = memory_get_max_bandwidth,
  111. .get_cur_state = memory_get_cur_bandwidth,
  112. .set_cur_state = memory_set_cur_bandwidth,
  113. };
  114. /*
  115. * Memory Device Management
  116. */
  117. static int intel_menlow_memory_add(struct acpi_device *device)
  118. {
  119. int result = -ENODEV;
  120. struct thermal_cooling_device *cdev;
  121. if (!device)
  122. return -EINVAL;
  123. if (!acpi_has_method(device->handle, MEMORY_GET_BANDWIDTH))
  124. goto end;
  125. if (!acpi_has_method(device->handle, MEMORY_SET_BANDWIDTH))
  126. goto end;
  127. cdev = thermal_cooling_device_register("Memory controller", device,
  128. &memory_cooling_ops);
  129. if (IS_ERR(cdev)) {
  130. result = PTR_ERR(cdev);
  131. goto end;
  132. }
  133. device->driver_data = cdev;
  134. result = sysfs_create_link(&device->dev.kobj,
  135. &cdev->device.kobj, "thermal_cooling");
  136. if (result)
  137. goto unregister;
  138. result = sysfs_create_link(&cdev->device.kobj,
  139. &device->dev.kobj, "device");
  140. if (result) {
  141. sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
  142. goto unregister;
  143. }
  144. end:
  145. return result;
  146. unregister:
  147. thermal_cooling_device_unregister(cdev);
  148. return result;
  149. }
  150. static int intel_menlow_memory_remove(struct acpi_device *device)
  151. {
  152. struct thermal_cooling_device *cdev;
  153. if (!device)
  154. return -EINVAL;
  155. cdev = acpi_driver_data(device);
  156. if (!cdev)
  157. return -EINVAL;
  158. sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
  159. sysfs_remove_link(&cdev->device.kobj, "device");
  160. thermal_cooling_device_unregister(cdev);
  161. return 0;
  162. }
  163. static const struct acpi_device_id intel_menlow_memory_ids[] = {
  164. {"INT0002", 0},
  165. {"", 0},
  166. };
  167. static struct acpi_driver intel_menlow_memory_driver = {
  168. .name = "intel_menlow_thermal_control",
  169. .ids = intel_menlow_memory_ids,
  170. .ops = {
  171. .add = intel_menlow_memory_add,
  172. .remove = intel_menlow_memory_remove,
  173. },
  174. };
  175. /*
  176. * Sensor control on menlow platform
  177. */
  178. #define THERMAL_AUX0 0
  179. #define THERMAL_AUX1 1
  180. #define GET_AUX0 "GAX0"
  181. #define GET_AUX1 "GAX1"
  182. #define SET_AUX0 "SAX0"
  183. #define SET_AUX1 "SAX1"
  184. struct intel_menlow_attribute {
  185. struct device_attribute attr;
  186. struct device *device;
  187. acpi_handle handle;
  188. struct list_head node;
  189. };
  190. static LIST_HEAD(intel_menlow_attr_list);
  191. static DEFINE_MUTEX(intel_menlow_attr_lock);
  192. /*
  193. * sensor_get_auxtrip - get the current auxtrip value from sensor
  194. * @name: Thermalzone name
  195. * @auxtype : AUX0/AUX1
  196. * @buf: syfs buffer
  197. */
  198. static int sensor_get_auxtrip(acpi_handle handle, int index,
  199. unsigned long long *value)
  200. {
  201. acpi_status status;
  202. if ((index != 0 && index != 1) || !value)
  203. return -EINVAL;
  204. status = acpi_evaluate_integer(handle, index ? GET_AUX1 : GET_AUX0,
  205. NULL, value);
  206. if (ACPI_FAILURE(status))
  207. return -EIO;
  208. return 0;
  209. }
  210. /*
  211. * sensor_set_auxtrip - set the new auxtrip value to sensor
  212. * @name: Thermalzone name
  213. * @auxtype : AUX0/AUX1
  214. * @buf: syfs buffer
  215. */
  216. static int sensor_set_auxtrip(acpi_handle handle, int index, int value)
  217. {
  218. acpi_status status;
  219. union acpi_object arg = {
  220. ACPI_TYPE_INTEGER
  221. };
  222. struct acpi_object_list args = {
  223. 1, &arg
  224. };
  225. unsigned long long temp;
  226. if (index != 0 && index != 1)
  227. return -EINVAL;
  228. status = acpi_evaluate_integer(handle, index ? GET_AUX0 : GET_AUX1,
  229. NULL, &temp);
  230. if (ACPI_FAILURE(status))
  231. return -EIO;
  232. if ((index && value < temp) || (!index && value > temp))
  233. return -EINVAL;
  234. arg.integer.value = value;
  235. status = acpi_evaluate_integer(handle, index ? SET_AUX1 : SET_AUX0,
  236. &args, &temp);
  237. if (ACPI_FAILURE(status))
  238. return -EIO;
  239. /* do we need to check the return value of SAX0/SAX1 ? */
  240. return 0;
  241. }
  242. #define to_intel_menlow_attr(_attr) \
  243. container_of(_attr, struct intel_menlow_attribute, attr)
  244. static ssize_t aux_show(struct device *dev, struct device_attribute *dev_attr,
  245. char *buf, int idx)
  246. {
  247. struct intel_menlow_attribute *attr = to_intel_menlow_attr(dev_attr);
  248. unsigned long long value;
  249. int result;
  250. result = sensor_get_auxtrip(attr->handle, idx, &value);
  251. if (result)
  252. return result;
  253. return sprintf(buf, "%lu", deci_kelvin_to_celsius(value));
  254. }
  255. static ssize_t aux0_show(struct device *dev,
  256. struct device_attribute *dev_attr, char *buf)
  257. {
  258. return aux_show(dev, dev_attr, buf, 0);
  259. }
  260. static ssize_t aux1_show(struct device *dev,
  261. struct device_attribute *dev_attr, char *buf)
  262. {
  263. return aux_show(dev, dev_attr, buf, 1);
  264. }
  265. static ssize_t aux_store(struct device *dev, struct device_attribute *dev_attr,
  266. const char *buf, size_t count, int idx)
  267. {
  268. struct intel_menlow_attribute *attr = to_intel_menlow_attr(dev_attr);
  269. int value;
  270. int result;
  271. /*Sanity check; should be a positive integer */
  272. if (!sscanf(buf, "%d", &value))
  273. return -EINVAL;
  274. if (value < 0)
  275. return -EINVAL;
  276. result = sensor_set_auxtrip(attr->handle, idx,
  277. celsius_to_deci_kelvin(value));
  278. return result ? result : count;
  279. }
  280. static ssize_t aux0_store(struct device *dev,
  281. struct device_attribute *dev_attr,
  282. const char *buf, size_t count)
  283. {
  284. return aux_store(dev, dev_attr, buf, count, 0);
  285. }
  286. static ssize_t aux1_store(struct device *dev,
  287. struct device_attribute *dev_attr,
  288. const char *buf, size_t count)
  289. {
  290. return aux_store(dev, dev_attr, buf, count, 1);
  291. }
  292. /* BIOS can enable/disable the thermal user application in dabney platform */
  293. #define BIOS_ENABLED "\\_TZ.GSTS"
  294. static ssize_t bios_enabled_show(struct device *dev,
  295. struct device_attribute *attr, char *buf)
  296. {
  297. acpi_status status;
  298. unsigned long long bios_enabled;
  299. status = acpi_evaluate_integer(NULL, BIOS_ENABLED, NULL, &bios_enabled);
  300. if (ACPI_FAILURE(status))
  301. return -ENODEV;
  302. return sprintf(buf, "%s\n", bios_enabled ? "enabled" : "disabled");
  303. }
  304. static int intel_menlow_add_one_attribute(char *name, umode_t mode, void *show,
  305. void *store, struct device *dev,
  306. acpi_handle handle)
  307. {
  308. struct intel_menlow_attribute *attr;
  309. int result;
  310. attr = kzalloc(sizeof(struct intel_menlow_attribute), GFP_KERNEL);
  311. if (!attr)
  312. return -ENOMEM;
  313. sysfs_attr_init(&attr->attr.attr); /* That is consistent naming :D */
  314. attr->attr.attr.name = name;
  315. attr->attr.attr.mode = mode;
  316. attr->attr.show = show;
  317. attr->attr.store = store;
  318. attr->device = dev;
  319. attr->handle = handle;
  320. result = device_create_file(dev, &attr->attr);
  321. if (result) {
  322. kfree(attr);
  323. return result;
  324. }
  325. mutex_lock(&intel_menlow_attr_lock);
  326. list_add_tail(&attr->node, &intel_menlow_attr_list);
  327. mutex_unlock(&intel_menlow_attr_lock);
  328. return 0;
  329. }
  330. static acpi_status intel_menlow_register_sensor(acpi_handle handle, u32 lvl,
  331. void *context, void **rv)
  332. {
  333. acpi_status status;
  334. acpi_handle dummy;
  335. struct thermal_zone_device *thermal;
  336. int result;
  337. result = acpi_bus_get_private_data(handle, (void **)&thermal);
  338. if (result)
  339. return 0;
  340. /* _TZ must have the AUX0/1 methods */
  341. status = acpi_get_handle(handle, GET_AUX0, &dummy);
  342. if (ACPI_FAILURE(status))
  343. return (status == AE_NOT_FOUND) ? AE_OK : status;
  344. status = acpi_get_handle(handle, SET_AUX0, &dummy);
  345. if (ACPI_FAILURE(status))
  346. return (status == AE_NOT_FOUND) ? AE_OK : status;
  347. result = intel_menlow_add_one_attribute("aux0", 0644,
  348. aux0_show, aux0_store,
  349. &thermal->device, handle);
  350. if (result)
  351. return AE_ERROR;
  352. status = acpi_get_handle(handle, GET_AUX1, &dummy);
  353. if (ACPI_FAILURE(status))
  354. goto aux1_not_found;
  355. status = acpi_get_handle(handle, SET_AUX1, &dummy);
  356. if (ACPI_FAILURE(status))
  357. goto aux1_not_found;
  358. result = intel_menlow_add_one_attribute("aux1", 0644,
  359. aux1_show, aux1_store,
  360. &thermal->device, handle);
  361. if (result) {
  362. intel_menlow_unregister_sensor();
  363. return AE_ERROR;
  364. }
  365. /*
  366. * create the "dabney_enabled" attribute which means the user app
  367. * should be loaded or not
  368. */
  369. result = intel_menlow_add_one_attribute("bios_enabled", 0444,
  370. bios_enabled_show, NULL,
  371. &thermal->device, handle);
  372. if (result) {
  373. intel_menlow_unregister_sensor();
  374. return AE_ERROR;
  375. }
  376. return AE_OK;
  377. aux1_not_found:
  378. if (status == AE_NOT_FOUND)
  379. return AE_OK;
  380. intel_menlow_unregister_sensor();
  381. return status;
  382. }
  383. static void intel_menlow_unregister_sensor(void)
  384. {
  385. struct intel_menlow_attribute *pos, *next;
  386. mutex_lock(&intel_menlow_attr_lock);
  387. list_for_each_entry_safe(pos, next, &intel_menlow_attr_list, node) {
  388. list_del(&pos->node);
  389. device_remove_file(pos->device, &pos->attr);
  390. kfree(pos);
  391. }
  392. mutex_unlock(&intel_menlow_attr_lock);
  393. return;
  394. }
  395. static int __init intel_menlow_module_init(void)
  396. {
  397. int result = -ENODEV;
  398. acpi_status status;
  399. unsigned long long enable;
  400. if (acpi_disabled)
  401. return result;
  402. /* Looking for the \_TZ.GSTS method */
  403. status = acpi_evaluate_integer(NULL, BIOS_ENABLED, NULL, &enable);
  404. if (ACPI_FAILURE(status) || !enable)
  405. return -ENODEV;
  406. /* Looking for ACPI device MEM0 with hardware id INT0002 */
  407. result = acpi_bus_register_driver(&intel_menlow_memory_driver);
  408. if (result)
  409. return result;
  410. /* Looking for sensors in each ACPI thermal zone */
  411. status = acpi_walk_namespace(ACPI_TYPE_THERMAL, ACPI_ROOT_OBJECT,
  412. ACPI_UINT32_MAX,
  413. intel_menlow_register_sensor, NULL, NULL, NULL);
  414. if (ACPI_FAILURE(status)) {
  415. acpi_bus_unregister_driver(&intel_menlow_memory_driver);
  416. return -ENODEV;
  417. }
  418. return 0;
  419. }
  420. static void __exit intel_menlow_module_exit(void)
  421. {
  422. acpi_bus_unregister_driver(&intel_menlow_memory_driver);
  423. intel_menlow_unregister_sensor();
  424. }
  425. module_init(intel_menlow_module_init);
  426. module_exit(intel_menlow_module_exit);