acpi_thermal_rel.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 0;
  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. acpi_create_platform_device(adev);
  107. else
  108. pr_warn("Failed to get source ACPI device\n");
  109. result = acpi_bus_get_device(trt->target, &adev);
  110. if (!result)
  111. acpi_create_platform_device(adev);
  112. else
  113. pr_warn("Failed to get target ACPI device\n");
  114. }
  115. *trtp = trts;
  116. /* don't count bad entries */
  117. *trt_count -= nr_bad_entries;
  118. end:
  119. kfree(buffer.pointer);
  120. return result;
  121. }
  122. EXPORT_SYMBOL(acpi_parse_trt);
  123. /**
  124. * acpi_parse_art - Parse Active Relationship Table _ART
  125. *
  126. * @handle: ACPI handle of the device contains _ART
  127. * @art_count: the number of valid entries resulted from parsing _ART
  128. * @artp: pointer to pointer of array of art entries in parsing result
  129. * @create_dev: whether to create platform devices for target and source
  130. *
  131. */
  132. int acpi_parse_art(acpi_handle handle, int *art_count, struct art **artp,
  133. bool create_dev)
  134. {
  135. acpi_status status;
  136. int result = 0;
  137. int i;
  138. int nr_bad_entries = 0;
  139. struct art *arts;
  140. struct acpi_device *adev;
  141. union acpi_object *p;
  142. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  143. struct acpi_buffer element = { 0, NULL };
  144. struct acpi_buffer art_format = {
  145. sizeof("RRNNNNNNNNNNN"), "RRNNNNNNNNNNN" };
  146. if (!acpi_has_method(handle, "_ART"))
  147. return 0;
  148. status = acpi_evaluate_object(handle, "_ART", NULL, &buffer);
  149. if (ACPI_FAILURE(status))
  150. return -ENODEV;
  151. p = buffer.pointer;
  152. if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
  153. pr_err("Invalid _ART data\n");
  154. result = -EFAULT;
  155. goto end;
  156. }
  157. /* ignore p->package.elements[0], as this is _ART Revision field */
  158. *art_count = p->package.count - 1;
  159. arts = kzalloc(*art_count * sizeof(struct art), GFP_KERNEL);
  160. if (!arts) {
  161. result = -ENOMEM;
  162. goto end;
  163. }
  164. for (i = 0; i < *art_count; i++) {
  165. struct art *art = &arts[i - nr_bad_entries];
  166. element.length = sizeof(struct art);
  167. element.pointer = art;
  168. status = acpi_extract_package(&(p->package.elements[i + 1]),
  169. &art_format, &element);
  170. if (ACPI_FAILURE(status)) {
  171. pr_warn("_ART package %d is invalid, ignored", i);
  172. nr_bad_entries++;
  173. continue;
  174. }
  175. if (!create_dev)
  176. continue;
  177. if (art->source) {
  178. result = acpi_bus_get_device(art->source, &adev);
  179. if (!result)
  180. acpi_create_platform_device(adev);
  181. else
  182. pr_warn("Failed to get source ACPI device\n");
  183. }
  184. if (art->target) {
  185. result = acpi_bus_get_device(art->target, &adev);
  186. if (!result)
  187. acpi_create_platform_device(adev);
  188. else
  189. pr_warn("Failed to get source ACPI device\n");
  190. }
  191. }
  192. *artp = arts;
  193. /* don't count bad entries */
  194. *art_count -= nr_bad_entries;
  195. end:
  196. kfree(buffer.pointer);
  197. return result;
  198. }
  199. EXPORT_SYMBOL(acpi_parse_art);
  200. /* get device name from acpi handle */
  201. static void get_single_name(acpi_handle handle, char *name)
  202. {
  203. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER};
  204. if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)))
  205. pr_warn("Failed get name from handle\n");
  206. else {
  207. memcpy(name, buffer.pointer, ACPI_NAME_SIZE);
  208. kfree(buffer.pointer);
  209. }
  210. }
  211. static int fill_art(char __user *ubuf)
  212. {
  213. int i;
  214. int ret;
  215. int count;
  216. int art_len;
  217. struct art *arts = NULL;
  218. union art_object *art_user;
  219. ret = acpi_parse_art(acpi_thermal_rel_handle, &count, &arts, false);
  220. if (ret)
  221. goto free_art;
  222. art_len = count * sizeof(union art_object);
  223. art_user = kzalloc(art_len, GFP_KERNEL);
  224. if (!art_user) {
  225. ret = -ENOMEM;
  226. goto free_art;
  227. }
  228. /* now fill in user art data */
  229. for (i = 0; i < count; i++) {
  230. /* userspace art needs device name instead of acpi reference */
  231. get_single_name(arts[i].source, art_user[i].source_device);
  232. get_single_name(arts[i].target, art_user[i].target_device);
  233. /* copy the rest int data in addition to source and target */
  234. memcpy(&art_user[i].weight, &arts[i].weight,
  235. sizeof(u64) * (ACPI_NR_ART_ELEMENTS - 2));
  236. }
  237. if (copy_to_user(ubuf, art_user, art_len))
  238. ret = -EFAULT;
  239. kfree(art_user);
  240. free_art:
  241. kfree(arts);
  242. return ret;
  243. }
  244. static int fill_trt(char __user *ubuf)
  245. {
  246. int i;
  247. int ret;
  248. int count;
  249. int trt_len;
  250. struct trt *trts = NULL;
  251. union trt_object *trt_user;
  252. ret = acpi_parse_trt(acpi_thermal_rel_handle, &count, &trts, false);
  253. if (ret)
  254. goto free_trt;
  255. trt_len = count * sizeof(union trt_object);
  256. trt_user = kzalloc(trt_len, GFP_KERNEL);
  257. if (!trt_user) {
  258. ret = -ENOMEM;
  259. goto free_trt;
  260. }
  261. /* now fill in user trt data */
  262. for (i = 0; i < count; i++) {
  263. /* userspace trt needs device name instead of acpi reference */
  264. get_single_name(trts[i].source, trt_user[i].source_device);
  265. get_single_name(trts[i].target, trt_user[i].target_device);
  266. trt_user[i].sample_period = trts[i].sample_period;
  267. trt_user[i].influence = trts[i].influence;
  268. }
  269. if (copy_to_user(ubuf, trt_user, trt_len))
  270. ret = -EFAULT;
  271. kfree(trt_user);
  272. free_trt:
  273. kfree(trts);
  274. return ret;
  275. }
  276. static long acpi_thermal_rel_ioctl(struct file *f, unsigned int cmd,
  277. unsigned long __arg)
  278. {
  279. int ret = 0;
  280. unsigned long length = 0;
  281. unsigned long count = 0;
  282. char __user *arg = (void __user *)__arg;
  283. struct trt *trts;
  284. struct art *arts;
  285. switch (cmd) {
  286. case ACPI_THERMAL_GET_TRT_COUNT:
  287. ret = acpi_parse_trt(acpi_thermal_rel_handle, (int *)&count,
  288. &trts, false);
  289. kfree(trts);
  290. if (!ret)
  291. return put_user(count, (unsigned long __user *)__arg);
  292. return ret;
  293. case ACPI_THERMAL_GET_TRT_LEN:
  294. ret = acpi_parse_trt(acpi_thermal_rel_handle, (int *)&count,
  295. &trts, false);
  296. kfree(trts);
  297. length = count * sizeof(union trt_object);
  298. if (!ret)
  299. return put_user(length, (unsigned long __user *)__arg);
  300. return ret;
  301. case ACPI_THERMAL_GET_TRT:
  302. return fill_trt(arg);
  303. case ACPI_THERMAL_GET_ART_COUNT:
  304. ret = acpi_parse_art(acpi_thermal_rel_handle, (int *)&count,
  305. &arts, false);
  306. kfree(arts);
  307. if (!ret)
  308. return put_user(count, (unsigned long __user *)__arg);
  309. return ret;
  310. case ACPI_THERMAL_GET_ART_LEN:
  311. ret = acpi_parse_art(acpi_thermal_rel_handle, (int *)&count,
  312. &arts, false);
  313. kfree(arts);
  314. length = count * sizeof(union art_object);
  315. if (!ret)
  316. return put_user(length, (unsigned long __user *)__arg);
  317. return ret;
  318. case ACPI_THERMAL_GET_ART:
  319. return fill_art(arg);
  320. default:
  321. return -ENOTTY;
  322. }
  323. }
  324. static const struct file_operations acpi_thermal_rel_fops = {
  325. .owner = THIS_MODULE,
  326. .open = acpi_thermal_rel_open,
  327. .release = acpi_thermal_rel_release,
  328. .unlocked_ioctl = acpi_thermal_rel_ioctl,
  329. .llseek = no_llseek,
  330. };
  331. static struct miscdevice acpi_thermal_rel_misc_device = {
  332. .minor = MISC_DYNAMIC_MINOR,
  333. "acpi_thermal_rel",
  334. &acpi_thermal_rel_fops
  335. };
  336. int acpi_thermal_rel_misc_device_add(acpi_handle handle)
  337. {
  338. acpi_thermal_rel_handle = handle;
  339. return misc_register(&acpi_thermal_rel_misc_device);
  340. }
  341. EXPORT_SYMBOL(acpi_thermal_rel_misc_device_add);
  342. int acpi_thermal_rel_misc_device_remove(acpi_handle handle)
  343. {
  344. misc_deregister(&acpi_thermal_rel_misc_device);
  345. return 0;
  346. }
  347. EXPORT_SYMBOL(acpi_thermal_rel_misc_device_remove);
  348. MODULE_AUTHOR("Zhang Rui <rui.zhang@intel.com>");
  349. MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com");
  350. MODULE_DESCRIPTION("Intel acpi thermal rel misc dev driver");
  351. MODULE_LICENSE("GPL v2");