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;
75 struct acpi_device
*adev
;
77 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
78 struct acpi_buffer element
= { 0, NULL
};
79 struct acpi_buffer trt_format
= { sizeof("RRNNNNNN"), "RRNNNNNN" };
81 status
= acpi_evaluate_object(handle
, "_TRT", NULL
, &buffer
);
82 if (ACPI_FAILURE(status
))
86 if (!p
|| (p
->type
!= ACPI_TYPE_PACKAGE
)) {
87 pr_err("Invalid _TRT data\n");
92 *trt_count
= p
->package
.count
;
93 trts
= kcalloc(*trt_count
, sizeof(struct trt
), GFP_KERNEL
);
99 for (i
= 0; i
< *trt_count
; i
++) {
100 struct trt
*trt
= &trts
[i
- nr_bad_entries
];
102 element
.length
= sizeof(struct trt
);
103 element
.pointer
= trt
;
105 status
= acpi_extract_package(&(p
->package
.elements
[i
]),
106 &trt_format
, &element
);
107 if (ACPI_FAILURE(status
)) {
109 pr_warn("_TRT package %d is invalid, ignored\n", i
);
115 result
= acpi_bus_get_device(trt
->source
, &adev
);
117 pr_warn("Failed to get source ACPI device\n");
119 result
= acpi_bus_get_device(trt
->target
, &adev
);
121 pr_warn("Failed to get target ACPI device\n");
127 /* don't count bad entries */
128 *trt_count
-= nr_bad_entries
;
130 kfree(buffer
.pointer
);
133 EXPORT_SYMBOL(acpi_parse_trt
);
136 * acpi_parse_art - Parse Active Relationship Table _ART
138 * @handle: ACPI handle of the device contains _ART
139 * @art_count: the number of valid entries resulted from parsing _ART
140 * @artp: pointer to pointer of array of art entries in parsing result
141 * @create_dev: whether to create platform devices for target and source
144 int acpi_parse_art(acpi_handle handle
, int *art_count
, struct art
**artp
,
150 int nr_bad_entries
= 0;
152 struct acpi_device
*adev
;
153 union acpi_object
*p
;
154 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
155 struct acpi_buffer element
= { 0, NULL
};
156 struct acpi_buffer art_format
= {
157 sizeof("RRNNNNNNNNNNN"), "RRNNNNNNNNNNN" };
159 status
= acpi_evaluate_object(handle
, "_ART", NULL
, &buffer
);
160 if (ACPI_FAILURE(status
))
164 if (!p
|| (p
->type
!= ACPI_TYPE_PACKAGE
)) {
165 pr_err("Invalid _ART data\n");
170 /* ignore p->package.elements[0], as this is _ART Revision field */
171 *art_count
= p
->package
.count
- 1;
172 arts
= kcalloc(*art_count
, sizeof(struct art
), GFP_KERNEL
);
178 for (i
= 0; i
< *art_count
; i
++) {
179 struct art
*art
= &arts
[i
- nr_bad_entries
];
181 element
.length
= sizeof(struct art
);
182 element
.pointer
= art
;
184 status
= acpi_extract_package(&(p
->package
.elements
[i
+ 1]),
185 &art_format
, &element
);
186 if (ACPI_FAILURE(status
)) {
187 pr_warn("_ART package %d is invalid, ignored", i
);
195 result
= acpi_bus_get_device(art
->source
, &adev
);
197 pr_warn("Failed to get source ACPI device\n");
200 result
= acpi_bus_get_device(art
->target
, &adev
);
202 pr_warn("Failed to get target ACPI device\n");
207 /* don't count bad entries */
208 *art_count
-= nr_bad_entries
;
210 kfree(buffer
.pointer
);
213 EXPORT_SYMBOL(acpi_parse_art
);
216 /* get device name from acpi handle */
217 static void get_single_name(acpi_handle handle
, char *name
)
219 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
};
221 if (ACPI_FAILURE(acpi_get_name(handle
, ACPI_SINGLE_NAME
, &buffer
)))
222 pr_warn("Failed to get device name from acpi handle\n");
224 memcpy(name
, buffer
.pointer
, ACPI_NAMESEG_SIZE
);
225 kfree(buffer
.pointer
);
229 static int fill_art(char __user
*ubuf
)
235 struct art
*arts
= NULL
;
236 union art_object
*art_user
;
238 ret
= acpi_parse_art(acpi_thermal_rel_handle
, &count
, &arts
, false);
241 art_len
= count
* sizeof(union art_object
);
242 art_user
= kzalloc(art_len
, GFP_KERNEL
);
247 /* now fill in user art data */
248 for (i
= 0; i
< count
; i
++) {
249 /* userspace art needs device name instead of acpi reference */
250 get_single_name(arts
[i
].source
, art_user
[i
].source_device
);
251 get_single_name(arts
[i
].target
, art_user
[i
].target_device
);
252 /* copy the rest int data in addition to source and target */
253 memcpy(&art_user
[i
].weight
, &arts
[i
].weight
,
254 sizeof(u64
) * (ACPI_NR_ART_ELEMENTS
- 2));
257 if (copy_to_user(ubuf
, art_user
, art_len
))
265 static int fill_trt(char __user
*ubuf
)
271 struct trt
*trts
= NULL
;
272 union trt_object
*trt_user
;
274 ret
= acpi_parse_trt(acpi_thermal_rel_handle
, &count
, &trts
, false);
277 trt_len
= count
* sizeof(union trt_object
);
278 trt_user
= kzalloc(trt_len
, GFP_KERNEL
);
283 /* now fill in user trt data */
284 for (i
= 0; i
< count
; i
++) {
285 /* userspace trt needs device name instead of acpi reference */
286 get_single_name(trts
[i
].source
, trt_user
[i
].source_device
);
287 get_single_name(trts
[i
].target
, trt_user
[i
].target_device
);
288 trt_user
[i
].sample_period
= trts
[i
].sample_period
;
289 trt_user
[i
].influence
= trts
[i
].influence
;
292 if (copy_to_user(ubuf
, trt_user
, trt_len
))
300 static long acpi_thermal_rel_ioctl(struct file
*f
, unsigned int cmd
,
304 unsigned long length
= 0;
306 char __user
*arg
= (void __user
*)__arg
;
307 struct trt
*trts
= NULL
;
308 struct art
*arts
= NULL
;
311 case ACPI_THERMAL_GET_TRT_COUNT
:
312 ret
= acpi_parse_trt(acpi_thermal_rel_handle
, &count
,
316 return put_user(count
, (unsigned long __user
*)__arg
);
318 case ACPI_THERMAL_GET_TRT_LEN
:
319 ret
= acpi_parse_trt(acpi_thermal_rel_handle
, &count
,
322 length
= count
* sizeof(union trt_object
);
324 return put_user(length
, (unsigned long __user
*)__arg
);
326 case ACPI_THERMAL_GET_TRT
:
327 return fill_trt(arg
);
328 case ACPI_THERMAL_GET_ART_COUNT
:
329 ret
= acpi_parse_art(acpi_thermal_rel_handle
, &count
,
333 return put_user(count
, (unsigned long __user
*)__arg
);
335 case ACPI_THERMAL_GET_ART_LEN
:
336 ret
= acpi_parse_art(acpi_thermal_rel_handle
, &count
,
339 length
= count
* sizeof(union art_object
);
341 return put_user(length
, (unsigned long __user
*)__arg
);
344 case ACPI_THERMAL_GET_ART
:
345 return fill_art(arg
);
352 static const struct file_operations acpi_thermal_rel_fops
= {
353 .owner
= THIS_MODULE
,
354 .open
= acpi_thermal_rel_open
,
355 .release
= acpi_thermal_rel_release
,
356 .unlocked_ioctl
= acpi_thermal_rel_ioctl
,
360 static struct miscdevice acpi_thermal_rel_misc_device
= {
361 .minor
= MISC_DYNAMIC_MINOR
,
363 &acpi_thermal_rel_fops
366 int acpi_thermal_rel_misc_device_add(acpi_handle handle
)
368 acpi_thermal_rel_handle
= handle
;
370 return misc_register(&acpi_thermal_rel_misc_device
);
372 EXPORT_SYMBOL(acpi_thermal_rel_misc_device_add
);
374 int acpi_thermal_rel_misc_device_remove(acpi_handle handle
)
376 misc_deregister(&acpi_thermal_rel_misc_device
);
380 EXPORT_SYMBOL(acpi_thermal_rel_misc_device_remove
);
382 MODULE_AUTHOR("Zhang Rui <rui.zhang@intel.com>");
383 MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com");
384 MODULE_DESCRIPTION("Intel acpi thermal rel misc dev driver");
385 MODULE_LICENSE("GPL v2");