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>
22 #include "acpi_thermal_rel.h"
24 static acpi_handle acpi_thermal_rel_handle
;
25 static DEFINE_SPINLOCK(acpi_thermal_rel_chrdev_lock
);
26 static int acpi_thermal_rel_chrdev_count
; /* #times opened */
27 static int acpi_thermal_rel_chrdev_exclu
; /* already open exclusive? */
29 static int acpi_thermal_rel_open(struct inode
*inode
, struct file
*file
)
31 spin_lock(&acpi_thermal_rel_chrdev_lock
);
32 if (acpi_thermal_rel_chrdev_exclu
||
33 (acpi_thermal_rel_chrdev_count
&& (file
->f_flags
& O_EXCL
))) {
34 spin_unlock(&acpi_thermal_rel_chrdev_lock
);
38 if (file
->f_flags
& O_EXCL
)
39 acpi_thermal_rel_chrdev_exclu
= 1;
40 acpi_thermal_rel_chrdev_count
++;
42 spin_unlock(&acpi_thermal_rel_chrdev_lock
);
44 return nonseekable_open(inode
, file
);
47 static int acpi_thermal_rel_release(struct inode
*inode
, struct file
*file
)
49 spin_lock(&acpi_thermal_rel_chrdev_lock
);
50 acpi_thermal_rel_chrdev_count
--;
51 acpi_thermal_rel_chrdev_exclu
= 0;
52 spin_unlock(&acpi_thermal_rel_chrdev_lock
);
58 * acpi_parse_trt - Thermal Relationship Table _TRT for passive cooling
60 * @handle: ACPI handle of the device contains _TRT
61 * @trt_count: the number of valid entries resulted from parsing _TRT
62 * @trtp: pointer to pointer of array of _TRT entries in parsing result
63 * @create_dev: whether to create platform devices for target and source
66 int acpi_parse_trt(acpi_handle handle
, int *trt_count
, struct trt
**trtp
,
72 int nr_bad_entries
= 0;
74 struct acpi_device
*adev
;
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 if (!acpi_has_method(handle
, "_TRT"))
83 status
= acpi_evaluate_object(handle
, "_TRT", NULL
, &buffer
);
84 if (ACPI_FAILURE(status
))
88 if (!p
|| (p
->type
!= ACPI_TYPE_PACKAGE
)) {
89 pr_err("Invalid _TRT data\n");
94 *trt_count
= p
->package
.count
;
95 trts
= kcalloc(*trt_count
, sizeof(struct trt
), GFP_KERNEL
);
101 for (i
= 0; i
< *trt_count
; i
++) {
102 struct trt
*trt
= &trts
[i
- nr_bad_entries
];
104 element
.length
= sizeof(struct trt
);
105 element
.pointer
= trt
;
107 status
= acpi_extract_package(&(p
->package
.elements
[i
]),
108 &trt_format
, &element
);
109 if (ACPI_FAILURE(status
)) {
111 pr_warn("_TRT package %d is invalid, ignored\n", i
);
117 result
= acpi_bus_get_device(trt
->source
, &adev
);
119 pr_warn("Failed to get source ACPI device\n");
121 result
= acpi_bus_get_device(trt
->target
, &adev
);
123 pr_warn("Failed to get target ACPI device\n");
129 /* don't count bad entries */
130 *trt_count
-= nr_bad_entries
;
132 kfree(buffer
.pointer
);
135 EXPORT_SYMBOL(acpi_parse_trt
);
138 * acpi_parse_art - Parse Active Relationship Table _ART
140 * @handle: ACPI handle of the device contains _ART
141 * @art_count: the number of valid entries resulted from parsing _ART
142 * @artp: pointer to pointer of array of art entries in parsing result
143 * @create_dev: whether to create platform devices for target and source
146 int acpi_parse_art(acpi_handle handle
, int *art_count
, struct art
**artp
,
152 int nr_bad_entries
= 0;
154 struct acpi_device
*adev
;
155 union acpi_object
*p
;
156 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
157 struct acpi_buffer element
= { 0, NULL
};
158 struct acpi_buffer art_format
= {
159 sizeof("RRNNNNNNNNNNN"), "RRNNNNNNNNNNN" };
161 if (!acpi_has_method(handle
, "_ART"))
164 status
= acpi_evaluate_object(handle
, "_ART", NULL
, &buffer
);
165 if (ACPI_FAILURE(status
))
169 if (!p
|| (p
->type
!= ACPI_TYPE_PACKAGE
)) {
170 pr_err("Invalid _ART data\n");
175 /* ignore p->package.elements[0], as this is _ART Revision field */
176 *art_count
= p
->package
.count
- 1;
177 arts
= kcalloc(*art_count
, sizeof(struct art
), GFP_KERNEL
);
183 for (i
= 0; i
< *art_count
; i
++) {
184 struct art
*art
= &arts
[i
- nr_bad_entries
];
186 element
.length
= sizeof(struct art
);
187 element
.pointer
= art
;
189 status
= acpi_extract_package(&(p
->package
.elements
[i
+ 1]),
190 &art_format
, &element
);
191 if (ACPI_FAILURE(status
)) {
192 pr_warn("_ART package %d is invalid, ignored", i
);
200 result
= acpi_bus_get_device(art
->source
, &adev
);
202 pr_warn("Failed to get source ACPI device\n");
205 result
= acpi_bus_get_device(art
->target
, &adev
);
207 pr_warn("Failed to get target ACPI device\n");
212 /* don't count bad entries */
213 *art_count
-= nr_bad_entries
;
215 kfree(buffer
.pointer
);
218 EXPORT_SYMBOL(acpi_parse_art
);
221 /* get device name from acpi handle */
222 static void get_single_name(acpi_handle handle
, char *name
)
224 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
};
226 if (ACPI_FAILURE(acpi_get_name(handle
, ACPI_SINGLE_NAME
, &buffer
)))
227 pr_warn("Failed to get device name from acpi handle\n");
229 memcpy(name
, buffer
.pointer
, ACPI_NAMESEG_SIZE
);
230 kfree(buffer
.pointer
);
234 static int fill_art(char __user
*ubuf
)
240 struct art
*arts
= NULL
;
241 union art_object
*art_user
;
243 ret
= acpi_parse_art(acpi_thermal_rel_handle
, &count
, &arts
, false);
246 art_len
= count
* sizeof(union art_object
);
247 art_user
= kzalloc(art_len
, GFP_KERNEL
);
252 /* now fill in user art data */
253 for (i
= 0; i
< count
; i
++) {
254 /* userspace art needs device name instead of acpi reference */
255 get_single_name(arts
[i
].source
, art_user
[i
].source_device
);
256 get_single_name(arts
[i
].target
, art_user
[i
].target_device
);
257 /* copy the rest int data in addition to source and target */
258 memcpy(&art_user
[i
].weight
, &arts
[i
].weight
,
259 sizeof(u64
) * (ACPI_NR_ART_ELEMENTS
- 2));
262 if (copy_to_user(ubuf
, art_user
, art_len
))
270 static int fill_trt(char __user
*ubuf
)
276 struct trt
*trts
= NULL
;
277 union trt_object
*trt_user
;
279 ret
= acpi_parse_trt(acpi_thermal_rel_handle
, &count
, &trts
, false);
282 trt_len
= count
* sizeof(union trt_object
);
283 trt_user
= kzalloc(trt_len
, GFP_KERNEL
);
288 /* now fill in user trt data */
289 for (i
= 0; i
< count
; i
++) {
290 /* userspace trt needs device name instead of acpi reference */
291 get_single_name(trts
[i
].source
, trt_user
[i
].source_device
);
292 get_single_name(trts
[i
].target
, trt_user
[i
].target_device
);
293 trt_user
[i
].sample_period
= trts
[i
].sample_period
;
294 trt_user
[i
].influence
= trts
[i
].influence
;
297 if (copy_to_user(ubuf
, trt_user
, trt_len
))
305 static long acpi_thermal_rel_ioctl(struct file
*f
, unsigned int cmd
,
309 unsigned long length
= 0;
311 char __user
*arg
= (void __user
*)__arg
;
312 struct trt
*trts
= NULL
;
313 struct art
*arts
= NULL
;
316 case ACPI_THERMAL_GET_TRT_COUNT
:
317 ret
= acpi_parse_trt(acpi_thermal_rel_handle
, &count
,
321 return put_user(count
, (unsigned long __user
*)__arg
);
323 case ACPI_THERMAL_GET_TRT_LEN
:
324 ret
= acpi_parse_trt(acpi_thermal_rel_handle
, &count
,
327 length
= count
* sizeof(union trt_object
);
329 return put_user(length
, (unsigned long __user
*)__arg
);
331 case ACPI_THERMAL_GET_TRT
:
332 return fill_trt(arg
);
333 case ACPI_THERMAL_GET_ART_COUNT
:
334 ret
= acpi_parse_art(acpi_thermal_rel_handle
, &count
,
338 return put_user(count
, (unsigned long __user
*)__arg
);
340 case ACPI_THERMAL_GET_ART_LEN
:
341 ret
= acpi_parse_art(acpi_thermal_rel_handle
, &count
,
344 length
= count
* sizeof(union art_object
);
346 return put_user(length
, (unsigned long __user
*)__arg
);
349 case ACPI_THERMAL_GET_ART
:
350 return fill_art(arg
);
357 static const struct file_operations acpi_thermal_rel_fops
= {
358 .owner
= THIS_MODULE
,
359 .open
= acpi_thermal_rel_open
,
360 .release
= acpi_thermal_rel_release
,
361 .unlocked_ioctl
= acpi_thermal_rel_ioctl
,
365 static struct miscdevice acpi_thermal_rel_misc_device
= {
366 .minor
= MISC_DYNAMIC_MINOR
,
368 &acpi_thermal_rel_fops
371 int acpi_thermal_rel_misc_device_add(acpi_handle handle
)
373 acpi_thermal_rel_handle
= handle
;
375 return misc_register(&acpi_thermal_rel_misc_device
);
377 EXPORT_SYMBOL(acpi_thermal_rel_misc_device_add
);
379 int acpi_thermal_rel_misc_device_remove(acpi_handle handle
)
381 misc_deregister(&acpi_thermal_rel_misc_device
);
385 EXPORT_SYMBOL(acpi_thermal_rel_misc_device_remove
);
387 MODULE_AUTHOR("Zhang Rui <rui.zhang@intel.com>");
388 MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com");
389 MODULE_DESCRIPTION("Intel acpi thermal rel misc dev driver");
390 MODULE_LICENSE("GPL v2");