intel_menlow.c 12 KB

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