xref: /linux/drivers/thermal/intel/int340x_thermal/acpi_thermal_rel.c (revision 64b14a184e83eb62ea0615e31a409956049d40e7)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* acpi_thermal_rel.c driver for exporting ACPI thermal relationship
3  *
4  * Copyright (c) 2014 Intel Corp
5  */
6 
7 /*
8  * Two functionalities included:
9  * 1. Export _TRT, _ART, via misc device interface to the userspace.
10  * 2. Provide parsing result to kernel drivers
11  *
12  */
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>
18 #include <linux/io.h>
19 #include <linux/acpi.h>
20 #include <linux/uaccess.h>
21 #include <linux/miscdevice.h>
22 #include <linux/fs.h>
23 #include "acpi_thermal_rel.h"
24 
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? */
29 
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 
39 	if (file->f_flags & O_EXCL)
40 		acpi_thermal_rel_chrdev_exclu = 1;
41 	acpi_thermal_rel_chrdev_count++;
42 
43 	spin_unlock(&acpi_thermal_rel_chrdev_lock);
44 
45 	return nonseekable_open(inode, file);
46 }
47 
48 static int acpi_thermal_rel_release(struct inode *inode, struct file *file)
49 {
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);
54 
55 	return 0;
56 }
57 
58 /**
59  * acpi_parse_trt - Thermal Relationship Table _TRT for passive cooling
60  *
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
65  *
66  */
67 int acpi_parse_trt(acpi_handle handle, int *trt_count, struct trt **trtp,
68 		bool create_dev)
69 {
70 	acpi_status status;
71 	int result = 0;
72 	int i;
73 	int nr_bad_entries = 0;
74 	struct trt *trts;
75 	struct acpi_device *adev;
76 	union acpi_object *p;
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" };
80 
81 	status = acpi_evaluate_object(handle, "_TRT", NULL, &buffer);
82 	if (ACPI_FAILURE(status))
83 		return -ENODEV;
84 
85 	p = buffer.pointer;
86 	if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
87 		pr_err("Invalid _TRT data\n");
88 		result = -EFAULT;
89 		goto end;
90 	}
91 
92 	*trt_count = p->package.count;
93 	trts = kcalloc(*trt_count, sizeof(struct trt), GFP_KERNEL);
94 	if (!trts) {
95 		result = -ENOMEM;
96 		goto end;
97 	}
98 
99 	for (i = 0; i < *trt_count; i++) {
100 		struct trt *trt = &trts[i - nr_bad_entries];
101 
102 		element.length = sizeof(struct trt);
103 		element.pointer = trt;
104 
105 		status = acpi_extract_package(&(p->package.elements[i]),
106 					      &trt_format, &element);
107 		if (ACPI_FAILURE(status)) {
108 			nr_bad_entries++;
109 			pr_warn("_TRT package %d is invalid, ignored\n", i);
110 			continue;
111 		}
112 		if (!create_dev)
113 			continue;
114 
115 		result = acpi_bus_get_device(trt->source, &adev);
116 		if (result)
117 			pr_warn("Failed to get source ACPI device\n");
118 
119 		result = acpi_bus_get_device(trt->target, &adev);
120 		if (result)
121 			pr_warn("Failed to get target ACPI device\n");
122 	}
123 
124 	result = 0;
125 
126 	*trtp = trts;
127 	/* don't count bad entries */
128 	*trt_count -= nr_bad_entries;
129 end:
130 	kfree(buffer.pointer);
131 	return result;
132 }
133 EXPORT_SYMBOL(acpi_parse_trt);
134 
135 /**
136  * acpi_parse_art - Parse Active Relationship Table _ART
137  *
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
142  *
143  */
144 int acpi_parse_art(acpi_handle handle, int *art_count, struct art **artp,
145 		bool create_dev)
146 {
147 	acpi_status status;
148 	int result = 0;
149 	int i;
150 	int nr_bad_entries = 0;
151 	struct art *arts;
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" };
158 
159 	status = acpi_evaluate_object(handle, "_ART", NULL, &buffer);
160 	if (ACPI_FAILURE(status))
161 		return -ENODEV;
162 
163 	p = buffer.pointer;
164 	if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
165 		pr_err("Invalid _ART data\n");
166 		result = -EFAULT;
167 		goto end;
168 	}
169 
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);
173 	if (!arts) {
174 		result = -ENOMEM;
175 		goto end;
176 	}
177 
178 	for (i = 0; i < *art_count; i++) {
179 		struct art *art = &arts[i - nr_bad_entries];
180 
181 		element.length = sizeof(struct art);
182 		element.pointer = art;
183 
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);
188 			nr_bad_entries++;
189 			continue;
190 		}
191 		if (!create_dev)
192 			continue;
193 
194 		if (art->source) {
195 			result = acpi_bus_get_device(art->source, &adev);
196 			if (result)
197 				pr_warn("Failed to get source ACPI device\n");
198 		}
199 		if (art->target) {
200 			result = acpi_bus_get_device(art->target, &adev);
201 			if (result)
202 				pr_warn("Failed to get target ACPI device\n");
203 		}
204 	}
205 
206 	*artp = arts;
207 	/* don't count bad entries */
208 	*art_count -= nr_bad_entries;
209 end:
210 	kfree(buffer.pointer);
211 	return result;
212 }
213 EXPORT_SYMBOL(acpi_parse_art);
214 
215 
216 /* get device name from acpi handle */
217 static void get_single_name(acpi_handle handle, char *name)
218 {
219 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER};
220 
221 	if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)))
222 		pr_warn("Failed to get device name from acpi handle\n");
223 	else {
224 		memcpy(name, buffer.pointer, ACPI_NAMESEG_SIZE);
225 		kfree(buffer.pointer);
226 	}
227 }
228 
229 static int fill_art(char __user *ubuf)
230 {
231 	int i;
232 	int ret;
233 	int count;
234 	int art_len;
235 	struct art *arts = NULL;
236 	union art_object *art_user;
237 
238 	ret = acpi_parse_art(acpi_thermal_rel_handle, &count, &arts, false);
239 	if (ret)
240 		goto free_art;
241 	art_len = count * sizeof(union art_object);
242 	art_user = kzalloc(art_len, GFP_KERNEL);
243 	if (!art_user) {
244 		ret = -ENOMEM;
245 		goto free_art;
246 	}
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 		BUILD_BUG_ON(sizeof(art_user[i].data) !=
254 			     sizeof(u64) * (ACPI_NR_ART_ELEMENTS - 2));
255 		memcpy(&art_user[i].data, &arts[i].data, sizeof(art_user[i].data));
256 	}
257 
258 	if (copy_to_user(ubuf, art_user, art_len))
259 		ret = -EFAULT;
260 	kfree(art_user);
261 free_art:
262 	kfree(arts);
263 	return ret;
264 }
265 
266 static int fill_trt(char __user *ubuf)
267 {
268 	int i;
269 	int ret;
270 	int count;
271 	int trt_len;
272 	struct trt *trts = NULL;
273 	union trt_object *trt_user;
274 
275 	ret = acpi_parse_trt(acpi_thermal_rel_handle, &count, &trts, false);
276 	if (ret)
277 		goto free_trt;
278 	trt_len = count * sizeof(union trt_object);
279 	trt_user = kzalloc(trt_len, GFP_KERNEL);
280 	if (!trt_user) {
281 		ret = -ENOMEM;
282 		goto free_trt;
283 	}
284 	/* now fill in user trt data */
285 	for (i = 0; i < count; i++) {
286 		/* userspace trt needs device name instead of acpi reference */
287 		get_single_name(trts[i].source, trt_user[i].source_device);
288 		get_single_name(trts[i].target, trt_user[i].target_device);
289 		trt_user[i].sample_period = trts[i].sample_period;
290 		trt_user[i].influence = trts[i].influence;
291 	}
292 
293 	if (copy_to_user(ubuf, trt_user, trt_len))
294 		ret = -EFAULT;
295 	kfree(trt_user);
296 free_trt:
297 	kfree(trts);
298 	return ret;
299 }
300 
301 static long acpi_thermal_rel_ioctl(struct file *f, unsigned int cmd,
302 				   unsigned long __arg)
303 {
304 	int ret = 0;
305 	unsigned long length = 0;
306 	int count = 0;
307 	char __user *arg = (void __user *)__arg;
308 	struct trt *trts = NULL;
309 	struct art *arts = NULL;
310 
311 	switch (cmd) {
312 	case ACPI_THERMAL_GET_TRT_COUNT:
313 		ret = acpi_parse_trt(acpi_thermal_rel_handle, &count,
314 				&trts, false);
315 		kfree(trts);
316 		if (!ret)
317 			return put_user(count, (unsigned long __user *)__arg);
318 		return ret;
319 	case ACPI_THERMAL_GET_TRT_LEN:
320 		ret = acpi_parse_trt(acpi_thermal_rel_handle, &count,
321 				&trts, false);
322 		kfree(trts);
323 		length = count * sizeof(union trt_object);
324 		if (!ret)
325 			return put_user(length, (unsigned long __user *)__arg);
326 		return ret;
327 	case ACPI_THERMAL_GET_TRT:
328 		return fill_trt(arg);
329 	case ACPI_THERMAL_GET_ART_COUNT:
330 		ret = acpi_parse_art(acpi_thermal_rel_handle, &count,
331 				&arts, false);
332 		kfree(arts);
333 		if (!ret)
334 			return put_user(count, (unsigned long __user *)__arg);
335 		return ret;
336 	case ACPI_THERMAL_GET_ART_LEN:
337 		ret = acpi_parse_art(acpi_thermal_rel_handle, &count,
338 				&arts, false);
339 		kfree(arts);
340 		length = count * sizeof(union art_object);
341 		if (!ret)
342 			return put_user(length, (unsigned long __user *)__arg);
343 		return ret;
344 
345 	case ACPI_THERMAL_GET_ART:
346 		return fill_art(arg);
347 
348 	default:
349 		return -ENOTTY;
350 	}
351 }
352 
353 static const struct file_operations acpi_thermal_rel_fops = {
354 	.owner		= THIS_MODULE,
355 	.open		= acpi_thermal_rel_open,
356 	.release	= acpi_thermal_rel_release,
357 	.unlocked_ioctl	= acpi_thermal_rel_ioctl,
358 	.llseek		= no_llseek,
359 };
360 
361 static struct miscdevice acpi_thermal_rel_misc_device = {
362 	.minor	= MISC_DYNAMIC_MINOR,
363 	"acpi_thermal_rel",
364 	&acpi_thermal_rel_fops
365 };
366 
367 int acpi_thermal_rel_misc_device_add(acpi_handle handle)
368 {
369 	acpi_thermal_rel_handle = handle;
370 
371 	return misc_register(&acpi_thermal_rel_misc_device);
372 }
373 EXPORT_SYMBOL(acpi_thermal_rel_misc_device_add);
374 
375 int acpi_thermal_rel_misc_device_remove(acpi_handle handle)
376 {
377 	misc_deregister(&acpi_thermal_rel_misc_device);
378 
379 	return 0;
380 }
381 EXPORT_SYMBOL(acpi_thermal_rel_misc_device_remove);
382 
383 MODULE_AUTHOR("Zhang Rui <rui.zhang@intel.com>");
384 MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com");
385 MODULE_DESCRIPTION("Intel acpi thermal rel misc dev driver");
386 MODULE_LICENSE("GPL v2");
387