1 /* acpi_thermal_rel.c driver for exporting ACPI thermal relationship
3 * Copyright (c) 2014 Intel Corp
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.
12 * Two functionalities included:
13 * 1. Export _TRT, _ART, via misc device interface to the userspace.
14 * 2. Provide parsing result to kernel drivers
17 #include <linux/init.h>
18 #include <linux/export.h>
19 #include <linux/module.h>
20 #include <linux/device.h>
21 #include <linux/platform_device.h>
23 #include <linux/acpi.h>
24 #include <linux/uaccess.h>
25 #include <linux/miscdevice.h>
26 #include "acpi_thermal_rel.h"
28 static acpi_handle acpi_thermal_rel_handle;
29 static DEFINE_SPINLOCK(acpi_thermal_rel_chrdev_lock);
30 static int acpi_thermal_rel_chrdev_count; /* #times opened */
31 static int acpi_thermal_rel_chrdev_exclu; /* already open exclusive? */
33 static int acpi_thermal_rel_open(struct inode *inode, struct file *file)
35 spin_lock(&acpi_thermal_rel_chrdev_lock);
36 if (acpi_thermal_rel_chrdev_exclu ||
37 (acpi_thermal_rel_chrdev_count && (file->f_flags & O_EXCL))) {
38 spin_unlock(&acpi_thermal_rel_chrdev_lock);
42 if (file->f_flags & O_EXCL)
43 acpi_thermal_rel_chrdev_exclu = 1;
44 acpi_thermal_rel_chrdev_count++;
46 spin_unlock(&acpi_thermal_rel_chrdev_lock);
48 return nonseekable_open(inode, file);
51 static int acpi_thermal_rel_release(struct inode *inode, struct file *file)
53 spin_lock(&acpi_thermal_rel_chrdev_lock);
54 acpi_thermal_rel_chrdev_count--;
55 acpi_thermal_rel_chrdev_exclu = 0;
56 spin_unlock(&acpi_thermal_rel_chrdev_lock);
62 * acpi_parse_trt - Thermal Relationship Table _TRT for passive cooling
64 * @handle: ACPI handle of the device contains _TRT
65 * @art_count: the number of valid entries resulted from parsing _TRT
66 * @artp: pointer to pointer of array of art entries in parsing result
67 * @create_dev: whether to create platform devices for target and source
70 int acpi_parse_trt(acpi_handle handle, int *trt_count, struct trt **trtp,
76 int nr_bad_entries = 0;
78 struct acpi_device *adev;
80 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
81 struct acpi_buffer element = { 0, NULL };
82 struct acpi_buffer trt_format = { sizeof("RRNNNNNN"), "RRNNNNNN" };
84 if (!acpi_has_method(handle, "_TRT"))
87 status = acpi_evaluate_object(handle, "_TRT", NULL, &buffer);
88 if (ACPI_FAILURE(status))
92 if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
93 pr_err("Invalid _TRT data\n");
98 *trt_count = p->package.count;
99 trts = kzalloc(*trt_count * sizeof(struct trt), GFP_KERNEL);
105 for (i = 0; i < *trt_count; i++) {
106 struct trt *trt = &trts[i - nr_bad_entries];
108 element.length = sizeof(struct trt);
109 element.pointer = trt;
111 status = acpi_extract_package(&(p->package.elements[i]),
112 &trt_format, &element);
113 if (ACPI_FAILURE(status)) {
115 pr_warn("_TRT package %d is invalid, ignored\n", i);
121 result = acpi_bus_get_device(trt->source, &adev);
123 acpi_create_platform_device(adev);
125 pr_warn("Failed to get source ACPI device\n");
127 result = acpi_bus_get_device(trt->target, &adev);
129 acpi_create_platform_device(adev);
131 pr_warn("Failed to get target ACPI device\n");
135 /* don't count bad entries */
136 *trt_count -= nr_bad_entries;
138 kfree(buffer.pointer);
141 EXPORT_SYMBOL(acpi_parse_trt);
144 * acpi_parse_art - Parse Active Relationship Table _ART
146 * @handle: ACPI handle of the device contains _ART
147 * @art_count: the number of valid entries resulted from parsing _ART
148 * @artp: pointer to pointer of array of art entries in parsing result
149 * @create_dev: whether to create platform devices for target and source
152 int acpi_parse_art(acpi_handle handle, int *art_count, struct art **artp,
158 int nr_bad_entries = 0;
160 struct acpi_device *adev;
161 union acpi_object *p;
162 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
163 struct acpi_buffer element = { 0, NULL };
164 struct acpi_buffer art_format = {
165 sizeof("RRNNNNNNNNNNN"), "RRNNNNNNNNNNN" };
167 if (!acpi_has_method(handle, "_ART"))
170 status = acpi_evaluate_object(handle, "_ART", NULL, &buffer);
171 if (ACPI_FAILURE(status))
175 if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
176 pr_err("Invalid _ART data\n");
181 /* ignore p->package.elements[0], as this is _ART Revision field */
182 *art_count = p->package.count - 1;
183 arts = kzalloc(*art_count * sizeof(struct art), GFP_KERNEL);
189 for (i = 0; i < *art_count; i++) {
190 struct art *art = &arts[i - nr_bad_entries];
192 element.length = sizeof(struct art);
193 element.pointer = art;
195 status = acpi_extract_package(&(p->package.elements[i + 1]),
196 &art_format, &element);
197 if (ACPI_FAILURE(status)) {
198 pr_warn("_ART package %d is invalid, ignored", i);
206 result = acpi_bus_get_device(art->source, &adev);
208 acpi_create_platform_device(adev);
210 pr_warn("Failed to get source ACPI device\n");
213 result = acpi_bus_get_device(art->target, &adev);
215 acpi_create_platform_device(adev);
217 pr_warn("Failed to get source ACPI device\n");
222 /* don't count bad entries */
223 *art_count -= nr_bad_entries;
225 kfree(buffer.pointer);
228 EXPORT_SYMBOL(acpi_parse_art);
231 /* get device name from acpi handle */
232 static void get_single_name(acpi_handle handle, char *name)
234 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER};
236 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)))
237 pr_warn("Failed get name from handle\n");
239 memcpy(name, buffer.pointer, ACPI_NAME_SIZE);
240 kfree(buffer.pointer);
244 static int fill_art(char __user *ubuf)
250 struct art *arts = NULL;
251 union art_object *art_user;
253 ret = acpi_parse_art(acpi_thermal_rel_handle, &count, &arts, false);
256 art_len = count * sizeof(union art_object);
257 art_user = kzalloc(art_len, GFP_KERNEL);
262 /* now fill in user art data */
263 for (i = 0; i < count; i++) {
264 /* userspace art needs device name instead of acpi reference */
265 get_single_name(arts[i].source, art_user[i].source_device);
266 get_single_name(arts[i].target, art_user[i].target_device);
267 /* copy the rest int data in addition to source and target */
268 memcpy(&art_user[i].weight, &arts[i].weight,
269 sizeof(u64) * (ACPI_NR_ART_ELEMENTS - 2));
272 if (copy_to_user(ubuf, art_user, art_len))
280 static int fill_trt(char __user *ubuf)
286 struct trt *trts = NULL;
287 union trt_object *trt_user;
289 ret = acpi_parse_trt(acpi_thermal_rel_handle, &count, &trts, false);
292 trt_len = count * sizeof(union trt_object);
293 trt_user = kzalloc(trt_len, GFP_KERNEL);
298 /* now fill in user trt data */
299 for (i = 0; i < count; i++) {
300 /* userspace trt needs device name instead of acpi reference */
301 get_single_name(trts[i].source, trt_user[i].source_device);
302 get_single_name(trts[i].target, trt_user[i].target_device);
303 trt_user[i].sample_period = trts[i].sample_period;
304 trt_user[i].influence = trts[i].influence;
307 if (copy_to_user(ubuf, trt_user, trt_len))
315 static long acpi_thermal_rel_ioctl(struct file *f, unsigned int cmd,
319 unsigned long length = 0;
320 unsigned long count = 0;
321 char __user *arg = (void __user *)__arg;
326 case ACPI_THERMAL_GET_TRT_COUNT:
327 ret = acpi_parse_trt(acpi_thermal_rel_handle, (int *)&count,
331 return put_user(count, (unsigned long __user *)__arg);
333 case ACPI_THERMAL_GET_TRT_LEN:
334 ret = acpi_parse_trt(acpi_thermal_rel_handle, (int *)&count,
337 length = count * sizeof(union trt_object);
339 return put_user(length, (unsigned long __user *)__arg);
341 case ACPI_THERMAL_GET_TRT:
342 return fill_trt(arg);
343 case ACPI_THERMAL_GET_ART_COUNT:
344 ret = acpi_parse_art(acpi_thermal_rel_handle, (int *)&count,
348 return put_user(count, (unsigned long __user *)__arg);
350 case ACPI_THERMAL_GET_ART_LEN:
351 ret = acpi_parse_art(acpi_thermal_rel_handle, (int *)&count,
354 length = count * sizeof(union art_object);
356 return put_user(length, (unsigned long __user *)__arg);
359 case ACPI_THERMAL_GET_ART:
360 return fill_art(arg);
367 static const struct file_operations acpi_thermal_rel_fops = {
368 .owner = THIS_MODULE,
369 .open = acpi_thermal_rel_open,
370 .release = acpi_thermal_rel_release,
371 .unlocked_ioctl = acpi_thermal_rel_ioctl,
375 static struct miscdevice acpi_thermal_rel_misc_device = {
376 .minor = MISC_DYNAMIC_MINOR,
378 &acpi_thermal_rel_fops
381 int acpi_thermal_rel_misc_device_add(acpi_handle handle)
383 acpi_thermal_rel_handle = handle;
385 return misc_register(&acpi_thermal_rel_misc_device);
387 EXPORT_SYMBOL(acpi_thermal_rel_misc_device_add);
389 int acpi_thermal_rel_misc_device_remove(acpi_handle handle)
391 misc_deregister(&acpi_thermal_rel_misc_device);
395 EXPORT_SYMBOL(acpi_thermal_rel_misc_device_remove);
397 MODULE_AUTHOR("Zhang Rui <rui.zhang@intel.com>");
398 MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com");
399 MODULE_DESCRIPTION("Intel acpi thermal rel misc dev driver");
400 MODULE_LICENSE("GPL v2");