acpi_thermal_rel.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /* acpi_thermal_rel.c driver for exporting ACPI thermal relationship
  2. *
  3. * Copyright (c) 2014 Intel Corp
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. */
  10. /*
  11. * Two functionalities included:
  12. * 1. Export _TRT, _ART, via misc device interface to the userspace.
  13. * 2. Provide parsing result to kernel drivers
  14. *
  15. */
  16. #include <linux/init.h>
  17. #include <linux/export.h>
  18. #include <linux/module.h>
  19. #include <linux/device.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/io.h>
  22. #include <linux/acpi.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/miscdevice.h>
  25. #include "acpi_thermal_rel.h"
  26. static acpi_handle acpi_thermal_rel_handle;
  27. static DEFINE_SPINLOCK(acpi_thermal_rel_chrdev_lock);
  28. static int acpi_thermal_rel_chrdev_count; /* #times opened */
  29. static int acpi_thermal_rel_chrdev_exclu; /* already open exclusive? */
  30. static int acpi_thermal_rel_open(struct inode *inode, struct file *file)
  31. {
  32. spin_lock(&acpi_thermal_rel_chrdev_lock);
  33. if (acpi_thermal_rel_chrdev_exclu ||
  34. (acpi_thermal_rel_chrdev_count && (file->f_flags & O_EXCL))) {
  35. spin_unlock(&acpi_thermal_rel_chrdev_lock);
  36. return -EBUSY;
  37. }
  38. if (file->f_flags & O_EXCL)
  39. acpi_thermal_rel_chrdev_exclu = 1;
  40. acpi_thermal_rel_chrdev_count++;
  41. spin_unlock(&acpi_thermal_rel_chrdev_lock);
  42. return nonseekable_open(inode, file);
  43. }
  44. static int acpi_thermal_rel_release(struct inode *inode, struct file *file)
  45. {
  46. spin_lock(&acpi_thermal_rel_chrdev_lock);
  47. acpi_thermal_rel_chrdev_count--;
  48. acpi_thermal_rel_chrdev_exclu = 0;
  49. spin_unlock(&acpi_thermal_rel_chrdev_lock);
  50. return 0;
  51. }
  52. /**
  53. * acpi_parse_trt - Thermal Relationship Table _TRT for passive cooling
  54. *
  55. * @handle: ACPI handle of the device contains _TRT
  56. * @art_count: the number of valid entries resulted from parsing _TRT
  57. * @artp: pointer to pointer of array of art entries in parsing result
  58. * @create_dev: whether to create platform devices for target and source
  59. *
  60. */
  61. int acpi_parse_trt(acpi_handle handle, int *trt_count, struct trt **trtp,
  62. bool create_dev)
  63. {
  64. acpi_status status;
  65. int result = 0;
  66. int i;
  67. int nr_bad_entries = 0;
  68. struct trt *trts;
  69. struct acpi_device *adev;
  70. union acpi_object *p;
  71. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  72. struct acpi_buffer element = { 0, NULL };
  73. struct acpi_buffer trt_format = { sizeof("RRNNNNNN"), "RRNNNNNN" };
  74. if (!acpi_has_method(handle, "_TRT"))
  75. return -ENODEV;
  76. status = acpi_evaluate_object(handle, "_TRT", NULL, &buffer);
  77. if (ACPI_FAILURE(status))
  78. return -ENODEV;
  79. p = buffer.pointer;
  80. if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
  81. pr_err("Invalid _TRT data\n");
  82. result = -EFAULT;
  83. goto end;
  84. }
  85. *trt_count = p->package.count;
  86. trts = kzalloc(*trt_count * sizeof(struct trt), GFP_KERNEL);
  87. if (!trts) {
  88. result = -ENOMEM;
  89. goto end;
  90. }
  91. for (i = 0; i < *trt_count; i++) {
  92. struct trt *trt = &trts[i - nr_bad_entries];
  93. element.length = sizeof(struct trt);
  94. element.pointer = trt;
  95. status = acpi_extract_package(&(p->package.elements[i]),
  96. &trt_format, &element);
  97. if (ACPI_FAILURE(status)) {
  98. nr_bad_entries++;
  99. pr_warn("_TRT package %d is invalid, ignored\n", i);
  100. continue;
  101. }
  102. if (!create_dev)
  103. continue;
  104. result = acpi_bus_get_device(trt->source, &adev);
  105. if (result)
  106. pr_warn("Failed to get source ACPI device\n");
  107. result = acpi_bus_get_device(trt->target, &adev);
  108. if (result)
  109. pr_warn("Failed to get target ACPI device\n");
  110. }
  111. result = 0;
  112. *trtp = trts;
  113. /* don't count bad entries */
  114. *trt_count -= nr_bad_entries;
  115. end:
  116. kfree(buffer.pointer);
  117. return result;
  118. }
  119. EXPORT_SYMBOL(acpi_parse_trt);
  120. /**
  121. * acpi_parse_art - Parse Active Relationship Table _ART
  122. *
  123. * @handle: ACPI handle of the device contains _ART
  124. * @art_count: the number of valid entries resulted from parsing _ART
  125. * @artp: pointer to pointer of array of art entries in parsing result
  126. * @create_dev: whether to create platform devices for target and source
  127. *
  128. */
  129. int acpi_parse_art(acpi_handle handle, int *art_count, struct art **artp,
  130. bool create_dev)
  131. {
  132. acpi_status status;
  133. int result = 0;
  134. int i;
  135. int nr_bad_entries = 0;
  136. struct art *arts;
  137. struct acpi_device *adev;
  138. union acpi_object *p;
  139. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  140. struct acpi_buffer element = { 0, NULL };
  141. struct acpi_buffer art_format = {
  142. sizeof("RRNNNNNNNNNNN"), "RRNNNNNNNNNNN" };
  143. if (!acpi_has_method(handle, "_ART"))
  144. return -ENODEV;
  145. status = acpi_evaluate_object(handle, "_ART", NULL, &buffer);
  146. if (ACPI_FAILURE(status))
  147. return -ENODEV;
  148. p = buffer.pointer;
  149. if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
  150. pr_err("Invalid _ART data\n");
  151. result = -EFAULT;
  152. goto end;
  153. }
  154. /* ignore p->package.elements[0], as this is _ART Revision field */
  155. *art_count = p->package.count - 1;
  156. arts = kzalloc(*art_count * sizeof(struct art), GFP_KERNEL);
  157. if (!arts) {
  158. result = -ENOMEM;
  159. goto end;
  160. }
  161. for (i = 0; i < *art_count; i++) {
  162. struct art *art = &arts[i - nr_bad_entries];
  163. element.length = sizeof(struct art);
  164. element.pointer = art;
  165. status = acpi_extract_package(&(p->package.elements[i + 1]),
  166. &art_format, &element);
  167. if (ACPI_FAILURE(status)) {
  168. pr_warn("_ART package %d is invalid, ignored", i);
  169. nr_bad_entries++;
  170. continue;
  171. }
  172. if (!create_dev)
  173. continue;
  174. if (art->source) {
  175. result = acpi_bus_get_device(art->source, &adev);
  176. if (result)
  177. pr_warn("Failed to get source ACPI device\n");
  178. }
  179. if (art->target) {
  180. result = acpi_bus_get_device(art->target, &adev);
  181. if (result)
  182. pr_warn("Failed to get source ACPI device\n");
  183. }
  184. }
  185. *artp = arts;
  186. /* don't count bad entries */
  187. *art_count -= nr_bad_entries;
  188. end:
  189. kfree(buffer.pointer);
  190. return result;
  191. }
  192. EXPORT_SYMBOL(acpi_parse_art);
  193. /* get device name from acpi handle */
  194. static void get_single_name(acpi_handle handle, char *name)
  195. {
  196. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER};
  197. if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)))
  198. pr_warn("Failed get name from handle\n");
  199. else {
  200. memcpy(name, buffer.pointer, ACPI_NAME_SIZE);
  201. kfree(buffer.pointer);
  202. }
  203. }
  204. static int fill_art(char __user *ubuf)
  205. {
  206. int i;
  207. int ret;
  208. int count;
  209. int art_len;
  210. struct art *arts = NULL;
  211. union art_object *art_user;
  212. ret = acpi_parse_art(acpi_thermal_rel_handle, &count, &arts, false);
  213. if (ret)
  214. goto free_art;
  215. art_len = count * sizeof(union art_object);
  216. art_user = kzalloc(art_len, GFP_KERNEL);
  217. if (!art_user) {
  218. ret = -ENOMEM;
  219. goto free_art;
  220. }
  221. /* now fill in user art data */
  222. for (i = 0; i < count; i++) {
  223. /* userspace art needs device name instead of acpi reference */
  224. get_single_name(arts[i].source, art_user[i].source_device);
  225. get_single_name(arts[i].target, art_user[i].target_device);
  226. /* copy the rest int data in addition to source and target */
  227. memcpy(&art_user[i].weight, &arts[i].weight,
  228. sizeof(u64) * (ACPI_NR_ART_ELEMENTS - 2));
  229. }
  230. if (copy_to_user(ubuf, art_user, art_len))
  231. ret = -EFAULT;
  232. kfree(art_user);
  233. free_art:
  234. kfree(arts);
  235. return ret;
  236. }
  237. static int fill_trt(char __user *ubuf)
  238. {
  239. int i;
  240. int ret;
  241. int count;
  242. int trt_len;
  243. struct trt *trts = NULL;
  244. union trt_object *trt_user;
  245. ret = acpi_parse_trt(acpi_thermal_rel_handle, &count, &trts, false);
  246. if (ret)
  247. goto free_trt;
  248. trt_len = count * sizeof(union trt_object);
  249. trt_user = kzalloc(trt_len, GFP_KERNEL);
  250. if (!trt_user) {
  251. ret = -ENOMEM;
  252. goto free_trt;
  253. }
  254. /* now fill in user trt data */
  255. for (i = 0; i < count; i++) {
  256. /* userspace trt needs device name instead of acpi reference */
  257. get_single_name(trts[i].source, trt_user[i].source_device);
  258. get_single_name(trts[i].target, trt_user[i].target_device);
  259. trt_user[i].sample_period = trts[i].sample_period;
  260. trt_user[i].influence = trts[i].influence;
  261. }
  262. if (copy_to_user(ubuf, trt_user, trt_len))
  263. ret = -EFAULT;
  264. kfree(trt_user);
  265. free_trt:
  266. kfree(trts);
  267. return ret;
  268. }
  269. static long acpi_thermal_rel_ioctl(struct file *f, unsigned int cmd,
  270. unsigned long __arg)
  271. {
  272. int ret = 0;
  273. unsigned long length = 0;
  274. int count = 0;
  275. char __user *arg = (void __user *)__arg;
  276. struct trt *trts = NULL;
  277. struct art *arts = NULL;
  278. switch (cmd) {
  279. case ACPI_THERMAL_GET_TRT_COUNT:
  280. ret = acpi_parse_trt(acpi_thermal_rel_handle, &count,
  281. &trts, false);
  282. kfree(trts);
  283. if (!ret)
  284. return put_user(count, (unsigned long __user *)__arg);
  285. return ret;
  286. case ACPI_THERMAL_GET_TRT_LEN:
  287. ret = acpi_parse_trt(acpi_thermal_rel_handle, &count,
  288. &trts, false);
  289. kfree(trts);
  290. length = count * sizeof(union trt_object);
  291. if (!ret)
  292. return put_user(length, (unsigned long __user *)__arg);
  293. return ret;
  294. case ACPI_THERMAL_GET_TRT:
  295. return fill_trt(arg);
  296. case ACPI_THERMAL_GET_ART_COUNT:
  297. ret = acpi_parse_art(acpi_thermal_rel_handle, &count,
  298. &arts, false);
  299. kfree(arts);
  300. if (!ret)
  301. return put_user(count, (unsigned long __user *)__arg);
  302. return ret;
  303. case ACPI_THERMAL_GET_ART_LEN:
  304. ret = acpi_parse_art(acpi_thermal_rel_handle, &count,
  305. &arts, false);
  306. kfree(arts);
  307. length = count * sizeof(union art_object);
  308. if (!ret)
  309. return put_user(length, (unsigned long __user *)__arg);
  310. return ret;
  311. case ACPI_THERMAL_GET_ART:
  312. return fill_art(arg);
  313. default:
  314. return -ENOTTY;
  315. }
  316. }
  317. static const struct file_operations acpi_thermal_rel_fops = {
  318. .owner = THIS_MODULE,
  319. .open = acpi_thermal_rel_open,
  320. .release = acpi_thermal_rel_release,
  321. .unlocked_ioctl = acpi_thermal_rel_ioctl,
  322. .llseek = no_llseek,
  323. };
  324. static struct miscdevice acpi_thermal_rel_misc_device = {
  325. .minor = MISC_DYNAMIC_MINOR,
  326. "acpi_thermal_rel",
  327. &acpi_thermal_rel_fops
  328. };
  329. int acpi_thermal_rel_misc_device_add(acpi_handle handle)
  330. {
  331. acpi_thermal_rel_handle = handle;
  332. return misc_register(&acpi_thermal_rel_misc_device);
  333. }
  334. EXPORT_SYMBOL(acpi_thermal_rel_misc_device_add);
  335. int acpi_thermal_rel_misc_device_remove(acpi_handle handle)
  336. {
  337. misc_deregister(&acpi_thermal_rel_misc_device);
  338. return 0;
  339. }
  340. EXPORT_SYMBOL(acpi_thermal_rel_misc_device_remove);
  341. MODULE_AUTHOR("Zhang Rui <rui.zhang@intel.com>");
  342. MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com");
  343. MODULE_DESCRIPTION("Intel acpi thermal rel misc dev driver");
  344. MODULE_LICENSE("GPL v2");