1 // SPDX-License-Identifier: GPL-2.0-only
2 /* acpi_thermal_rel.c driver for exporting ACPI thermal relationship
4 * Copyright (c) 2014 Intel Corp
8 * Two functionalities included:
9 * 1. Export _TRT, _ART, via misc device interface to the userspace.
10 * 2. Provide parsing result to kernel drivers
13 #include <linux/init.h>
14 #include <linux/export.h>
15 #include <linux/module.h>
16 #include <linux/device.h>
17 #include <linux/platform_device.h>
19 #include <linux/acpi.h>
20 #include <linux/uaccess.h>
21 #include <linux/miscdevice.h>
23 #include "acpi_thermal_rel.h"
25 static acpi_handle acpi_thermal_rel_handle;
26 static DEFINE_SPINLOCK(acpi_thermal_rel_chrdev_lock);
27 static int acpi_thermal_rel_chrdev_count; /* #times opened */
28 static int acpi_thermal_rel_chrdev_exclu; /* already open exclusive? */
30 static int acpi_thermal_rel_open(struct inode *inode, struct file *file)
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);
39 if (file->f_flags & O_EXCL)
40 acpi_thermal_rel_chrdev_exclu = 1;
41 acpi_thermal_rel_chrdev_count++;
43 spin_unlock(&acpi_thermal_rel_chrdev_lock);
45 return nonseekable_open(inode, file);
48 static int acpi_thermal_rel_release(struct inode *inode, struct file *file)
50 spin_lock(&acpi_thermal_rel_chrdev_lock);
51 acpi_thermal_rel_chrdev_count--;
52 acpi_thermal_rel_chrdev_exclu = 0;
53 spin_unlock(&acpi_thermal_rel_chrdev_lock);
59 * acpi_parse_trt - Thermal Relationship Table _TRT for passive cooling
61 * @handle: ACPI handle of the device contains _TRT
62 * @trt_count: the number of valid entries resulted from parsing _TRT
63 * @trtp: pointer to pointer of array of _TRT entries in parsing result
64 * @create_dev: whether to create platform devices for target and source
67 int acpi_parse_trt(acpi_handle handle, int *trt_count, struct trt **trtp,
73 int nr_bad_entries = 0;
76 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
77 struct acpi_buffer element = { 0, NULL };
78 struct acpi_buffer trt_format = { sizeof("RRNNNNNN"), "RRNNNNNN" };
80 status = acpi_evaluate_object(handle, "_TRT", NULL, &buffer);
81 if (ACPI_FAILURE(status))
85 if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
86 pr_err("Invalid _TRT data\n");
91 *trt_count = p->package.count;
92 trts = kcalloc(*trt_count, sizeof(struct trt), GFP_KERNEL);
98 for (i = 0; i < *trt_count; i++) {
99 struct trt *trt = &trts[i - nr_bad_entries];
101 element.length = sizeof(struct trt);
102 element.pointer = trt;
104 status = acpi_extract_package(&(p->package.elements[i]),
105 &trt_format, &element);
106 if (ACPI_FAILURE(status)) {
108 pr_warn("_TRT package %d is invalid, ignored\n", i);
114 if (!acpi_fetch_acpi_dev(trt->source))
115 pr_warn("Failed to get source ACPI device\n");
117 if (!acpi_fetch_acpi_dev(trt->target))
118 pr_warn("Failed to get target ACPI device\n");
124 /* don't count bad entries */
125 *trt_count -= nr_bad_entries;
127 kfree(buffer.pointer);
130 EXPORT_SYMBOL(acpi_parse_trt);
133 * acpi_parse_art - Parse Active Relationship Table _ART
135 * @handle: ACPI handle of the device contains _ART
136 * @art_count: the number of valid entries resulted from parsing _ART
137 * @artp: pointer to pointer of array of art entries in parsing result
138 * @create_dev: whether to create platform devices for target and source
141 int acpi_parse_art(acpi_handle handle, int *art_count, struct art **artp,
147 int nr_bad_entries = 0;
149 union acpi_object *p;
150 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
151 struct acpi_buffer element = { 0, NULL };
152 struct acpi_buffer art_format = {
153 sizeof("RRNNNNNNNNNNN"), "RRNNNNNNNNNNN" };
155 status = acpi_evaluate_object(handle, "_ART", NULL, &buffer);
156 if (ACPI_FAILURE(status))
160 if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
161 pr_err("Invalid _ART data\n");
166 /* ignore p->package.elements[0], as this is _ART Revision field */
167 *art_count = p->package.count - 1;
168 arts = kcalloc(*art_count, sizeof(struct art), GFP_KERNEL);
174 for (i = 0; i < *art_count; i++) {
175 struct art *art = &arts[i - nr_bad_entries];
177 element.length = sizeof(struct art);
178 element.pointer = art;
180 status = acpi_extract_package(&(p->package.elements[i + 1]),
181 &art_format, &element);
182 if (ACPI_FAILURE(status)) {
183 pr_warn("_ART package %d is invalid, ignored", i);
190 if (!acpi_fetch_acpi_dev(art->source))
191 pr_warn("Failed to get source ACPI device\n");
193 if (!acpi_fetch_acpi_dev(art->target))
194 pr_warn("Failed to get target ACPI device\n");
198 /* don't count bad entries */
199 *art_count -= nr_bad_entries;
201 kfree(buffer.pointer);
204 EXPORT_SYMBOL(acpi_parse_art);
207 /* get device name from acpi handle */
208 static void get_single_name(acpi_handle handle, char *name)
210 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER};
212 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)))
213 pr_warn("Failed to get device name from acpi handle\n");
215 memcpy(name, buffer.pointer, ACPI_NAMESEG_SIZE);
216 kfree(buffer.pointer);
220 static int fill_art(char __user *ubuf)
226 struct art *arts = NULL;
227 union art_object *art_user;
229 ret = acpi_parse_art(acpi_thermal_rel_handle, &count, &arts, false);
232 art_len = count * sizeof(union art_object);
233 art_user = kzalloc(art_len, GFP_KERNEL);
238 /* now fill in user art data */
239 for (i = 0; i < count; i++) {
240 /* userspace art needs device name instead of acpi reference */
241 get_single_name(arts[i].source, art_user[i].source_device);
242 get_single_name(arts[i].target, art_user[i].target_device);
243 /* copy the rest int data in addition to source and target */
244 BUILD_BUG_ON(sizeof(art_user[i].data) !=
245 sizeof(u64) * (ACPI_NR_ART_ELEMENTS - 2));
246 memcpy(&art_user[i].data, &arts[i].data, sizeof(art_user[i].data));
249 if (copy_to_user(ubuf, art_user, art_len))
257 static int fill_trt(char __user *ubuf)
263 struct trt *trts = NULL;
264 union trt_object *trt_user;
266 ret = acpi_parse_trt(acpi_thermal_rel_handle, &count, &trts, false);
269 trt_len = count * sizeof(union trt_object);
270 trt_user = kzalloc(trt_len, GFP_KERNEL);
275 /* now fill in user trt data */
276 for (i = 0; i < count; i++) {
277 /* userspace trt needs device name instead of acpi reference */
278 get_single_name(trts[i].source, trt_user[i].source_device);
279 get_single_name(trts[i].target, trt_user[i].target_device);
280 trt_user[i].sample_period = trts[i].sample_period;
281 trt_user[i].influence = trts[i].influence;
284 if (copy_to_user(ubuf, trt_user, trt_len))
292 static long acpi_thermal_rel_ioctl(struct file *f, unsigned int cmd,
296 unsigned long length = 0;
298 char __user *arg = (void __user *)__arg;
299 struct trt *trts = NULL;
300 struct art *arts = NULL;
303 case ACPI_THERMAL_GET_TRT_COUNT:
304 ret = acpi_parse_trt(acpi_thermal_rel_handle, &count,
308 return put_user(count, (unsigned long __user *)__arg);
310 case ACPI_THERMAL_GET_TRT_LEN:
311 ret = acpi_parse_trt(acpi_thermal_rel_handle, &count,
314 length = count * sizeof(union trt_object);
316 return put_user(length, (unsigned long __user *)__arg);
318 case ACPI_THERMAL_GET_TRT:
319 return fill_trt(arg);
320 case ACPI_THERMAL_GET_ART_COUNT:
321 ret = acpi_parse_art(acpi_thermal_rel_handle, &count,
325 return put_user(count, (unsigned long __user *)__arg);
327 case ACPI_THERMAL_GET_ART_LEN:
328 ret = acpi_parse_art(acpi_thermal_rel_handle, &count,
331 length = count * sizeof(union art_object);
333 return put_user(length, (unsigned long __user *)__arg);
336 case ACPI_THERMAL_GET_ART:
337 return fill_art(arg);
344 static const struct file_operations acpi_thermal_rel_fops = {
345 .owner = THIS_MODULE,
346 .open = acpi_thermal_rel_open,
347 .release = acpi_thermal_rel_release,
348 .unlocked_ioctl = acpi_thermal_rel_ioctl,
352 static struct miscdevice acpi_thermal_rel_misc_device = {
353 .minor = MISC_DYNAMIC_MINOR,
355 &acpi_thermal_rel_fops
358 int acpi_thermal_rel_misc_device_add(acpi_handle handle)
360 acpi_thermal_rel_handle = handle;
362 return misc_register(&acpi_thermal_rel_misc_device);
364 EXPORT_SYMBOL(acpi_thermal_rel_misc_device_add);
366 int acpi_thermal_rel_misc_device_remove(acpi_handle handle)
368 misc_deregister(&acpi_thermal_rel_misc_device);
372 EXPORT_SYMBOL(acpi_thermal_rel_misc_device_remove);
376 MODULE_DESCRIPTION("Intel acpi thermal rel misc dev driver");
377 MODULE_LICENSE("GPL v2");