acpi_thermal_rel.c (9857B)
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 25static acpi_handle acpi_thermal_rel_handle; 26static DEFINE_SPINLOCK(acpi_thermal_rel_chrdev_lock); 27static int acpi_thermal_rel_chrdev_count; /* #times opened */ 28static int acpi_thermal_rel_chrdev_exclu; /* already open exclusive? */ 29 30static 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 48static 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 */ 67int 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 union acpi_object *p; 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" }; 79 80 status = acpi_evaluate_object(handle, "_TRT", NULL, &buffer); 81 if (ACPI_FAILURE(status)) 82 return -ENODEV; 83 84 p = buffer.pointer; 85 if (!p || (p->type != ACPI_TYPE_PACKAGE)) { 86 pr_err("Invalid _TRT data\n"); 87 result = -EFAULT; 88 goto end; 89 } 90 91 *trt_count = p->package.count; 92 trts = kcalloc(*trt_count, sizeof(struct trt), GFP_KERNEL); 93 if (!trts) { 94 result = -ENOMEM; 95 goto end; 96 } 97 98 for (i = 0; i < *trt_count; i++) { 99 struct trt *trt = &trts[i - nr_bad_entries]; 100 101 element.length = sizeof(struct trt); 102 element.pointer = trt; 103 104 status = acpi_extract_package(&(p->package.elements[i]), 105 &trt_format, &element); 106 if (ACPI_FAILURE(status)) { 107 nr_bad_entries++; 108 pr_warn("_TRT package %d is invalid, ignored\n", i); 109 continue; 110 } 111 if (!create_dev) 112 continue; 113 114 if (!acpi_fetch_acpi_dev(trt->source)) 115 pr_warn("Failed to get source ACPI device\n"); 116 117 if (!acpi_fetch_acpi_dev(trt->target)) 118 pr_warn("Failed to get target ACPI device\n"); 119 } 120 121 result = 0; 122 123 *trtp = trts; 124 /* don't count bad entries */ 125 *trt_count -= nr_bad_entries; 126end: 127 kfree(buffer.pointer); 128 return result; 129} 130EXPORT_SYMBOL(acpi_parse_trt); 131 132/** 133 * acpi_parse_art - Parse Active Relationship Table _ART 134 * 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 139 * 140 */ 141int acpi_parse_art(acpi_handle handle, int *art_count, struct art **artp, 142 bool create_dev) 143{ 144 acpi_status status; 145 int result = 0; 146 int i; 147 int nr_bad_entries = 0; 148 struct art *arts; 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" }; 154 155 status = acpi_evaluate_object(handle, "_ART", NULL, &buffer); 156 if (ACPI_FAILURE(status)) 157 return -ENODEV; 158 159 p = buffer.pointer; 160 if (!p || (p->type != ACPI_TYPE_PACKAGE)) { 161 pr_err("Invalid _ART data\n"); 162 result = -EFAULT; 163 goto end; 164 } 165 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); 169 if (!arts) { 170 result = -ENOMEM; 171 goto end; 172 } 173 174 for (i = 0; i < *art_count; i++) { 175 struct art *art = &arts[i - nr_bad_entries]; 176 177 element.length = sizeof(struct art); 178 element.pointer = art; 179 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); 184 nr_bad_entries++; 185 continue; 186 } 187 if (!create_dev) 188 continue; 189 190 if (!acpi_fetch_acpi_dev(art->source)) 191 pr_warn("Failed to get source ACPI device\n"); 192 193 if (!acpi_fetch_acpi_dev(art->target)) 194 pr_warn("Failed to get target ACPI device\n"); 195 } 196 197 *artp = arts; 198 /* don't count bad entries */ 199 *art_count -= nr_bad_entries; 200end: 201 kfree(buffer.pointer); 202 return result; 203} 204EXPORT_SYMBOL(acpi_parse_art); 205 206 207/* get device name from acpi handle */ 208static void get_single_name(acpi_handle handle, char *name) 209{ 210 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER}; 211 212 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer))) 213 pr_warn("Failed to get device name from acpi handle\n"); 214 else { 215 memcpy(name, buffer.pointer, ACPI_NAMESEG_SIZE); 216 kfree(buffer.pointer); 217 } 218} 219 220static int fill_art(char __user *ubuf) 221{ 222 int i; 223 int ret; 224 int count; 225 int art_len; 226 struct art *arts = NULL; 227 union art_object *art_user; 228 229 ret = acpi_parse_art(acpi_thermal_rel_handle, &count, &arts, false); 230 if (ret) 231 goto free_art; 232 art_len = count * sizeof(union art_object); 233 art_user = kzalloc(art_len, GFP_KERNEL); 234 if (!art_user) { 235 ret = -ENOMEM; 236 goto free_art; 237 } 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)); 247 } 248 249 if (copy_to_user(ubuf, art_user, art_len)) 250 ret = -EFAULT; 251 kfree(art_user); 252free_art: 253 kfree(arts); 254 return ret; 255} 256 257static int fill_trt(char __user *ubuf) 258{ 259 int i; 260 int ret; 261 int count; 262 int trt_len; 263 struct trt *trts = NULL; 264 union trt_object *trt_user; 265 266 ret = acpi_parse_trt(acpi_thermal_rel_handle, &count, &trts, false); 267 if (ret) 268 goto free_trt; 269 trt_len = count * sizeof(union trt_object); 270 trt_user = kzalloc(trt_len, GFP_KERNEL); 271 if (!trt_user) { 272 ret = -ENOMEM; 273 goto free_trt; 274 } 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; 282 } 283 284 if (copy_to_user(ubuf, trt_user, trt_len)) 285 ret = -EFAULT; 286 kfree(trt_user); 287free_trt: 288 kfree(trts); 289 return ret; 290} 291 292static long acpi_thermal_rel_ioctl(struct file *f, unsigned int cmd, 293 unsigned long __arg) 294{ 295 int ret = 0; 296 unsigned long length = 0; 297 int count = 0; 298 char __user *arg = (void __user *)__arg; 299 struct trt *trts = NULL; 300 struct art *arts = NULL; 301 302 switch (cmd) { 303 case ACPI_THERMAL_GET_TRT_COUNT: 304 ret = acpi_parse_trt(acpi_thermal_rel_handle, &count, 305 &trts, false); 306 kfree(trts); 307 if (!ret) 308 return put_user(count, (unsigned long __user *)__arg); 309 return ret; 310 case ACPI_THERMAL_GET_TRT_LEN: 311 ret = acpi_parse_trt(acpi_thermal_rel_handle, &count, 312 &trts, false); 313 kfree(trts); 314 length = count * sizeof(union trt_object); 315 if (!ret) 316 return put_user(length, (unsigned long __user *)__arg); 317 return ret; 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, 322 &arts, false); 323 kfree(arts); 324 if (!ret) 325 return put_user(count, (unsigned long __user *)__arg); 326 return ret; 327 case ACPI_THERMAL_GET_ART_LEN: 328 ret = acpi_parse_art(acpi_thermal_rel_handle, &count, 329 &arts, false); 330 kfree(arts); 331 length = count * sizeof(union art_object); 332 if (!ret) 333 return put_user(length, (unsigned long __user *)__arg); 334 return ret; 335 336 case ACPI_THERMAL_GET_ART: 337 return fill_art(arg); 338 339 default: 340 return -ENOTTY; 341 } 342} 343 344static 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, 349 .llseek = no_llseek, 350}; 351 352static struct miscdevice acpi_thermal_rel_misc_device = { 353 .minor = MISC_DYNAMIC_MINOR, 354 "acpi_thermal_rel", 355 &acpi_thermal_rel_fops 356}; 357 358int acpi_thermal_rel_misc_device_add(acpi_handle handle) 359{ 360 acpi_thermal_rel_handle = handle; 361 362 return misc_register(&acpi_thermal_rel_misc_device); 363} 364EXPORT_SYMBOL(acpi_thermal_rel_misc_device_add); 365 366int acpi_thermal_rel_misc_device_remove(acpi_handle handle) 367{ 368 misc_deregister(&acpi_thermal_rel_misc_device); 369 370 return 0; 371} 372EXPORT_SYMBOL(acpi_thermal_rel_misc_device_remove); 373 374MODULE_AUTHOR("Zhang Rui <rui.zhang@intel.com>"); 375MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com"); 376MODULE_DESCRIPTION("Intel acpi thermal rel misc dev driver"); 377MODULE_LICENSE("GPL v2");