1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * acpi_utils.c - ACPI Utility Functions ($Revision: 10 $) 4 * 5 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> 6 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> 7 */ 8 9 #define pr_fmt(fmt) "ACPI: utils: " fmt 10 11 #include <linux/kernel.h> 12 #include <linux/module.h> 13 #include <linux/mutex.h> 14 #include <linux/slab.h> 15 #include <linux/init.h> 16 #include <linux/types.h> 17 #include <linux/hardirq.h> 18 #include <linux/acpi.h> 19 #include <linux/dynamic_debug.h> 20 21 #include "internal.h" 22 #include "sleep.h" 23 24 /* -------------------------------------------------------------------------- 25 Object Evaluation Helpers 26 -------------------------------------------------------------------------- */ 27 static void acpi_util_eval_error(acpi_handle h, acpi_string p, acpi_status s) 28 { 29 acpi_handle_debug(h, "Evaluate [%s]: %s\n", p, acpi_format_exception(s)); 30 } 31 32 acpi_status 33 acpi_extract_package(union acpi_object *package, 34 struct acpi_buffer *format, struct acpi_buffer *buffer) 35 { 36 u32 size_required = 0; 37 u32 tail_offset = 0; 38 char *format_string = NULL; 39 u32 format_count = 0; 40 u32 i = 0; 41 u8 *head = NULL; 42 u8 *tail = NULL; 43 44 45 if (!package || (package->type != ACPI_TYPE_PACKAGE) 46 || (package->package.count < 1)) { 47 pr_debug("Invalid package argument\n"); 48 return AE_BAD_PARAMETER; 49 } 50 51 if (!format || !format->pointer || (format->length < 1)) { 52 pr_debug("Invalid format argument\n"); 53 return AE_BAD_PARAMETER; 54 } 55 56 if (!buffer) { 57 pr_debug("Invalid buffer argument\n"); 58 return AE_BAD_PARAMETER; 59 } 60 61 format_count = (format->length / sizeof(char)) - 1; 62 if (format_count > package->package.count) { 63 pr_debug("Format specifies more objects [%d] than present [%d]\n", 64 format_count, package->package.count); 65 return AE_BAD_DATA; 66 } 67 68 format_string = format->pointer; 69 70 /* 71 * Calculate size_required. 72 */ 73 for (i = 0; i < format_count; i++) { 74 75 union acpi_object *element = &(package->package.elements[i]); 76 77 switch (element->type) { 78 79 case ACPI_TYPE_INTEGER: 80 switch (format_string[i]) { 81 case 'N': 82 size_required += sizeof(u64); 83 tail_offset += sizeof(u64); 84 break; 85 case 'S': 86 size_required += 87 sizeof(char *) + sizeof(u64) + 88 sizeof(char); 89 tail_offset += sizeof(char *); 90 break; 91 default: 92 pr_debug("Invalid package element [%d]: got number, expected [%c]\n", 93 i, format_string[i]); 94 return AE_BAD_DATA; 95 } 96 break; 97 98 case ACPI_TYPE_STRING: 99 case ACPI_TYPE_BUFFER: 100 switch (format_string[i]) { 101 case 'S': 102 size_required += 103 sizeof(char *) + 104 (element->string.length * sizeof(char)) + 105 sizeof(char); 106 tail_offset += sizeof(char *); 107 break; 108 case 'B': 109 size_required += 110 sizeof(u8 *) + element->buffer.length; 111 tail_offset += sizeof(u8 *); 112 break; 113 default: 114 pr_debug("Invalid package element [%d] got string/buffer, expected [%c]\n", 115 i, format_string[i]); 116 return AE_BAD_DATA; 117 } 118 break; 119 case ACPI_TYPE_LOCAL_REFERENCE: 120 switch (format_string[i]) { 121 case 'R': 122 size_required += sizeof(void *); 123 tail_offset += sizeof(void *); 124 break; 125 default: 126 pr_debug("Invalid package element [%d] got reference, expected [%c]\n", 127 i, format_string[i]); 128 return AE_BAD_DATA; 129 } 130 break; 131 132 case ACPI_TYPE_PACKAGE: 133 default: 134 pr_debug("Unsupported element at index=%d\n", i); 135 /* TBD: handle nested packages... */ 136 return AE_SUPPORT; 137 } 138 } 139 140 /* 141 * Validate output buffer. 142 */ 143 if (buffer->length == ACPI_ALLOCATE_BUFFER) { 144 buffer->pointer = ACPI_ALLOCATE_ZEROED(size_required); 145 if (!buffer->pointer) 146 return AE_NO_MEMORY; 147 buffer->length = size_required; 148 } else { 149 if (buffer->length < size_required) { 150 buffer->length = size_required; 151 return AE_BUFFER_OVERFLOW; 152 } else if (buffer->length != size_required || 153 !buffer->pointer) { 154 return AE_BAD_PARAMETER; 155 } 156 } 157 158 head = buffer->pointer; 159 tail = buffer->pointer + tail_offset; 160 161 /* 162 * Extract package data. 163 */ 164 for (i = 0; i < format_count; i++) { 165 166 u8 **pointer = NULL; 167 union acpi_object *element = &(package->package.elements[i]); 168 169 switch (element->type) { 170 171 case ACPI_TYPE_INTEGER: 172 switch (format_string[i]) { 173 case 'N': 174 *((u64 *) head) = 175 element->integer.value; 176 head += sizeof(u64); 177 break; 178 case 'S': 179 pointer = (u8 **) head; 180 *pointer = tail; 181 *((u64 *) tail) = 182 element->integer.value; 183 head += sizeof(u64 *); 184 tail += sizeof(u64); 185 /* NULL terminate string */ 186 *tail = (char)0; 187 tail += sizeof(char); 188 break; 189 default: 190 /* Should never get here */ 191 break; 192 } 193 break; 194 195 case ACPI_TYPE_STRING: 196 case ACPI_TYPE_BUFFER: 197 switch (format_string[i]) { 198 case 'S': 199 pointer = (u8 **) head; 200 *pointer = tail; 201 memcpy(tail, element->string.pointer, 202 element->string.length); 203 head += sizeof(char *); 204 tail += element->string.length * sizeof(char); 205 /* NULL terminate string */ 206 *tail = (char)0; 207 tail += sizeof(char); 208 break; 209 case 'B': 210 pointer = (u8 **) head; 211 *pointer = tail; 212 memcpy(tail, element->buffer.pointer, 213 element->buffer.length); 214 head += sizeof(u8 *); 215 tail += element->buffer.length; 216 break; 217 default: 218 /* Should never get here */ 219 break; 220 } 221 break; 222 case ACPI_TYPE_LOCAL_REFERENCE: 223 switch (format_string[i]) { 224 case 'R': 225 *(void **)head = 226 (void *)element->reference.handle; 227 head += sizeof(void *); 228 break; 229 default: 230 /* Should never get here */ 231 break; 232 } 233 break; 234 case ACPI_TYPE_PACKAGE: 235 /* TBD: handle nested packages... */ 236 default: 237 /* Should never get here */ 238 break; 239 } 240 } 241 242 return AE_OK; 243 } 244 245 EXPORT_SYMBOL(acpi_extract_package); 246 247 static DEFINE_MUTEX(acpi_exclusive_eval_lock); 248 249 /** 250 * acpi_evaluate_object_exclusive - Evaluate an object without interleaving 251 * @handle: ACPI handle of the object or its scope. 252 * @pathname: Path of the object relative to @handle. 253 * @arguments: Arguments to pass, or NULL. 254 * @buffer: Buffer for the return value, or NULL. 255 * 256 * Same as acpi_evaluate_object(), but serialized with other exclusive 257 * evaluations. Use it only for methods known to break when interleaved. 258 */ 259 acpi_status acpi_evaluate_object_exclusive(acpi_handle handle, acpi_string pathname, 260 struct acpi_object_list *arguments, 261 struct acpi_buffer *buffer) 262 { 263 guard(mutex)(&acpi_exclusive_eval_lock); 264 265 return acpi_evaluate_object(handle, pathname, arguments, buffer); 266 } 267 EXPORT_SYMBOL_GPL(acpi_evaluate_object_exclusive); 268 269 acpi_status 270 acpi_evaluate_integer(acpi_handle handle, 271 acpi_string pathname, 272 struct acpi_object_list *arguments, unsigned long long *data) 273 { 274 acpi_status status = AE_OK; 275 union acpi_object element; 276 struct acpi_buffer buffer = { 0, NULL }; 277 278 if (!data) 279 return AE_BAD_PARAMETER; 280 281 buffer.length = sizeof(union acpi_object); 282 buffer.pointer = &element; 283 status = acpi_evaluate_object(handle, pathname, arguments, &buffer); 284 if (ACPI_FAILURE(status)) { 285 acpi_util_eval_error(handle, pathname, status); 286 return status; 287 } 288 289 if (element.type != ACPI_TYPE_INTEGER) { 290 acpi_util_eval_error(handle, pathname, AE_BAD_DATA); 291 return AE_BAD_DATA; 292 } 293 294 *data = element.integer.value; 295 296 acpi_handle_debug(handle, "Return value [%llu]\n", *data); 297 298 return AE_OK; 299 } 300 301 EXPORT_SYMBOL(acpi_evaluate_integer); 302 303 acpi_status acpi_evaluate_integer_exclusive(acpi_handle handle, acpi_string pathname, 304 struct acpi_object_list *arguments, 305 unsigned long long *data) 306 { 307 guard(mutex)(&acpi_exclusive_eval_lock); 308 309 return acpi_evaluate_integer(handle, pathname, arguments, data); 310 } 311 EXPORT_SYMBOL_GPL(acpi_evaluate_integer_exclusive); 312 313 int acpi_get_local_u64_address(acpi_handle handle, u64 *addr) 314 { 315 acpi_status status; 316 317 status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, addr); 318 if (ACPI_FAILURE(status)) 319 return -ENODATA; 320 return 0; 321 } 322 EXPORT_SYMBOL(acpi_get_local_u64_address); 323 324 int acpi_get_local_address(acpi_handle handle, u32 *addr) 325 { 326 u64 adr; 327 int ret; 328 329 ret = acpi_get_local_u64_address(handle, &adr); 330 if (ret < 0) 331 return ret; 332 *addr = (u32)adr; 333 return 0; 334 } 335 EXPORT_SYMBOL(acpi_get_local_address); 336 337 #define ACPI_MAX_SUB_BUF_SIZE 9 338 339 const char *acpi_get_subsystem_id(acpi_handle handle) 340 { 341 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 342 union acpi_object *obj; 343 acpi_status status; 344 const char *sub; 345 size_t len; 346 347 status = acpi_evaluate_object(handle, METHOD_NAME__SUB, NULL, &buffer); 348 if (ACPI_FAILURE(status)) { 349 acpi_handle_debug(handle, "Reading ACPI _SUB failed: %#x\n", status); 350 return ERR_PTR(-ENODATA); 351 } 352 353 obj = buffer.pointer; 354 if (obj->type == ACPI_TYPE_STRING) { 355 len = strlen(obj->string.pointer); 356 if (len < ACPI_MAX_SUB_BUF_SIZE && len > 0) { 357 sub = kstrdup(obj->string.pointer, GFP_KERNEL); 358 if (!sub) 359 sub = ERR_PTR(-ENOMEM); 360 } else { 361 acpi_handle_err(handle, "ACPI _SUB Length %zu is Invalid\n", len); 362 sub = ERR_PTR(-ENODATA); 363 } 364 } else { 365 acpi_handle_warn(handle, "Warning ACPI _SUB did not return a string\n"); 366 sub = ERR_PTR(-ENODATA); 367 } 368 369 acpi_os_free(buffer.pointer); 370 371 return sub; 372 } 373 EXPORT_SYMBOL_GPL(acpi_get_subsystem_id); 374 375 bool acpi_evaluate_reference(acpi_handle handle, acpi_string pathname, 376 struct acpi_object_list *arguments, 377 struct acpi_handle_list *list) 378 { 379 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 380 union acpi_object *package; 381 acpi_status status; 382 bool ret = false; 383 u32 i; 384 385 if (!list) 386 return false; 387 388 /* Evaluate object. */ 389 390 status = acpi_evaluate_object(handle, pathname, arguments, &buffer); 391 if (ACPI_FAILURE(status)) 392 goto end; 393 394 package = buffer.pointer; 395 396 if (buffer.length == 0 || !package || 397 package->type != ACPI_TYPE_PACKAGE || !package->package.count) 398 goto err; 399 400 list->count = package->package.count; 401 list->handles = kzalloc_objs(*list->handles, list->count); 402 if (!list->handles) 403 goto err_clear; 404 405 /* Extract package data. */ 406 407 for (i = 0; i < list->count; i++) { 408 union acpi_object *element = &(package->package.elements[i]); 409 410 if (element->type != ACPI_TYPE_LOCAL_REFERENCE || 411 !element->reference.handle) 412 goto err_free; 413 414 /* Get the acpi_handle. */ 415 416 list->handles[i] = element->reference.handle; 417 acpi_handle_debug(list->handles[i], "Found in reference list\n"); 418 } 419 420 ret = true; 421 422 end: 423 kfree(buffer.pointer); 424 425 return ret; 426 427 err_free: 428 kfree(list->handles); 429 list->handles = NULL; 430 431 err_clear: 432 list->count = 0; 433 434 err: 435 acpi_util_eval_error(handle, pathname, status); 436 goto end; 437 } 438 439 EXPORT_SYMBOL(acpi_evaluate_reference); 440 441 /** 442 * acpi_handle_list_equal - Check if two ACPI handle lists are the same 443 * @list1: First list to compare. 444 * @list2: Second list to compare. 445 * 446 * Return true if the given ACPI handle lists are of the same size and 447 * contain the same ACPI handles in the same order. Otherwise, return false. 448 */ 449 bool acpi_handle_list_equal(struct acpi_handle_list *list1, 450 struct acpi_handle_list *list2) 451 { 452 return list1->count == list2->count && 453 !memcmp(list1->handles, list2->handles, 454 list1->count * sizeof(*list1->handles)); 455 } 456 EXPORT_SYMBOL_GPL(acpi_handle_list_equal); 457 458 /** 459 * acpi_handle_list_replace - Replace one ACPI handle list with another 460 * @dst: ACPI handle list to replace. 461 * @src: Source ACPI handle list. 462 * 463 * Free the handles table in @dst, move the handles table from @src to @dst, 464 * copy count from @src to @dst and clear @src. 465 */ 466 void acpi_handle_list_replace(struct acpi_handle_list *dst, 467 struct acpi_handle_list *src) 468 { 469 if (dst->count) 470 kfree(dst->handles); 471 472 dst->count = src->count; 473 dst->handles = src->handles; 474 475 src->handles = NULL; 476 src->count = 0; 477 } 478 EXPORT_SYMBOL_GPL(acpi_handle_list_replace); 479 480 /** 481 * acpi_handle_list_free - Free the handles table in an ACPI handle list 482 * @list: ACPI handle list to free. 483 * 484 * Free the handles table in @list and clear its count field. 485 */ 486 void acpi_handle_list_free(struct acpi_handle_list *list) 487 { 488 if (!list->count) 489 return; 490 491 kfree(list->handles); 492 list->count = 0; 493 } 494 EXPORT_SYMBOL_GPL(acpi_handle_list_free); 495 496 /** 497 * acpi_device_dep - Check ACPI device dependency 498 * @target: ACPI handle of the target ACPI device. 499 * @match: ACPI handle to look up in the target's _DEP list. 500 * 501 * Return true if @match is present in the list returned by _DEP for 502 * @target or false otherwise. 503 */ 504 bool acpi_device_dep(acpi_handle target, acpi_handle match) 505 { 506 struct acpi_handle_list dep_devices; 507 bool ret = false; 508 int i; 509 510 if (!acpi_has_method(target, "_DEP")) 511 return false; 512 513 if (!acpi_evaluate_reference(target, "_DEP", NULL, &dep_devices)) { 514 acpi_handle_debug(target, "Failed to evaluate _DEP.\n"); 515 return false; 516 } 517 518 for (i = 0; i < dep_devices.count; i++) { 519 if (dep_devices.handles[i] == match) { 520 ret = true; 521 break; 522 } 523 } 524 525 acpi_handle_list_free(&dep_devices); 526 return ret; 527 } 528 EXPORT_SYMBOL_GPL(acpi_device_dep); 529 530 bool 531 acpi_get_physical_device_location(acpi_handle handle, struct acpi_pld_info **pld) 532 { 533 acpi_status status; 534 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 535 union acpi_object *output; 536 537 status = acpi_evaluate_object(handle, "_PLD", NULL, &buffer); 538 if (ACPI_FAILURE(status)) 539 return false; 540 541 output = buffer.pointer; 542 543 if (!output || output->type != ACPI_TYPE_PACKAGE 544 || !output->package.count 545 || output->package.elements[0].type != ACPI_TYPE_BUFFER 546 || output->package.elements[0].buffer.length < ACPI_PLD_REV1_BUFFER_SIZE) { 547 status = AE_TYPE; 548 goto out; 549 } 550 551 status = acpi_decode_pld_buffer( 552 output->package.elements[0].buffer.pointer, 553 output->package.elements[0].buffer.length, 554 pld); 555 556 out: 557 kfree(buffer.pointer); 558 return ACPI_SUCCESS(status); 559 } 560 EXPORT_SYMBOL(acpi_get_physical_device_location); 561 562 /** 563 * acpi_evaluate_ost: Evaluate _OST for hotplug operations 564 * @handle: ACPI device handle 565 * @source_event: source event code 566 * @status_code: status code 567 * @status_buf: optional detailed information (NULL if none) 568 * 569 * Evaluate _OST for hotplug operations. All ACPI hotplug handlers 570 * must call this function when evaluating _OST for hotplug operations. 571 * When the platform does not support _OST, this function has no effect. 572 */ 573 acpi_status 574 acpi_evaluate_ost(acpi_handle handle, u32 source_event, u32 status_code, 575 struct acpi_buffer *status_buf) 576 { 577 union acpi_object params[3] = { 578 {.type = ACPI_TYPE_INTEGER,}, 579 {.type = ACPI_TYPE_INTEGER,}, 580 {.type = ACPI_TYPE_BUFFER,} 581 }; 582 struct acpi_object_list arg_list = {3, params}; 583 584 params[0].integer.value = source_event; 585 params[1].integer.value = status_code; 586 if (status_buf != NULL) { 587 params[2].buffer.pointer = status_buf->pointer; 588 params[2].buffer.length = status_buf->length; 589 } else { 590 params[2].buffer.pointer = NULL; 591 params[2].buffer.length = 0; 592 } 593 594 return acpi_evaluate_object(handle, "_OST", &arg_list, NULL); 595 } 596 EXPORT_SYMBOL(acpi_evaluate_ost); 597 598 /** 599 * acpi_handle_path: Return the object path of handle 600 * @handle: ACPI device handle 601 * 602 * Caller must free the returned buffer 603 */ 604 char *acpi_handle_path(acpi_handle handle) 605 { 606 struct acpi_buffer buffer = { 607 .length = ACPI_ALLOCATE_BUFFER, 608 .pointer = NULL 609 }; 610 611 if (in_interrupt() || 612 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer) != AE_OK) 613 return NULL; 614 return buffer.pointer; 615 } 616 617 /** 618 * acpi_handle_printk: Print message with ACPI prefix and object path 619 * @level: log level 620 * @handle: ACPI device handle 621 * @fmt: format string 622 * 623 * This function is called through acpi_handle_<level> macros and prints 624 * a message with ACPI prefix and object path. This function acquires 625 * the global namespace mutex to obtain an object path. In interrupt 626 * context, it shows the object path as <n/a>. 627 */ 628 void 629 acpi_handle_printk(const char *level, acpi_handle handle, const char *fmt, ...) 630 { 631 struct va_format vaf; 632 va_list args; 633 const char *path; 634 635 va_start(args, fmt); 636 vaf.fmt = fmt; 637 vaf.va = &args; 638 639 path = acpi_handle_path(handle); 640 printk("%sACPI: %s: %pV", level, path ? path : "<n/a>", &vaf); 641 642 va_end(args); 643 kfree(path); 644 } 645 EXPORT_SYMBOL(acpi_handle_printk); 646 647 #if defined(CONFIG_DYNAMIC_DEBUG) 648 /** 649 * __acpi_handle_debug: pr_debug with ACPI prefix and object path 650 * @descriptor: Dynamic Debug descriptor 651 * @handle: ACPI device handle 652 * @fmt: format string 653 * 654 * This function is called through acpi_handle_debug macro and debug 655 * prints a message with ACPI prefix and object path. This function 656 * acquires the global namespace mutex to obtain an object path. In 657 * interrupt context, it shows the object path as <n/a>. 658 */ 659 void 660 __acpi_handle_debug(struct _ddebug *descriptor, acpi_handle handle, 661 const char *fmt, ...) 662 { 663 struct va_format vaf; 664 va_list args; 665 const char *path; 666 667 va_start(args, fmt); 668 vaf.fmt = fmt; 669 vaf.va = &args; 670 671 path = acpi_handle_path(handle); 672 __dynamic_pr_debug(descriptor, "ACPI: %s: %pV", path ? path : "<n/a>", &vaf); 673 674 va_end(args); 675 kfree(path); 676 } 677 EXPORT_SYMBOL(__acpi_handle_debug); 678 #endif 679 680 /** 681 * acpi_evaluation_failure_warn - Log evaluation failure warning. 682 * @handle: Parent object handle. 683 * @name: Name of the object whose evaluation has failed. 684 * @status: Status value returned by the failing object evaluation. 685 */ 686 void acpi_evaluation_failure_warn(acpi_handle handle, const char *name, 687 acpi_status status) 688 { 689 acpi_handle_warn(handle, "%s evaluation failed: %s\n", name, 690 acpi_format_exception(status)); 691 } 692 EXPORT_SYMBOL_GPL(acpi_evaluation_failure_warn); 693 694 /** 695 * acpi_has_method: Check whether @handle has a method named @name 696 * @handle: ACPI device handle 697 * @name: name of object or method 698 * 699 * Check whether @handle has a method named @name. 700 */ 701 bool acpi_has_method(acpi_handle handle, char *name) 702 { 703 acpi_handle tmp; 704 705 return ACPI_SUCCESS(acpi_get_handle(handle, name, &tmp)); 706 } 707 EXPORT_SYMBOL(acpi_has_method); 708 709 acpi_status acpi_execute_simple_method(acpi_handle handle, char *method, 710 u64 arg) 711 { 712 union acpi_object obj = { .type = ACPI_TYPE_INTEGER }; 713 struct acpi_object_list arg_list = { .count = 1, .pointer = &obj, }; 714 715 obj.integer.value = arg; 716 717 return acpi_evaluate_object(handle, method, &arg_list, NULL); 718 } 719 EXPORT_SYMBOL(acpi_execute_simple_method); 720 721 acpi_status acpi_execute_simple_method_exclusive(acpi_handle handle, char *method, u64 arg) 722 { 723 guard(mutex)(&acpi_exclusive_eval_lock); 724 725 return acpi_execute_simple_method(handle, method, arg); 726 } 727 EXPORT_SYMBOL_GPL(acpi_execute_simple_method_exclusive); 728 729 /** 730 * acpi_evaluate_ej0: Evaluate _EJ0 method for hotplug operations 731 * @handle: ACPI device handle 732 * 733 * Evaluate device's _EJ0 method for hotplug operations. 734 */ 735 acpi_status acpi_evaluate_ej0(acpi_handle handle) 736 { 737 acpi_status status; 738 739 status = acpi_execute_simple_method(handle, "_EJ0", 1); 740 if (status == AE_NOT_FOUND) 741 acpi_handle_warn(handle, "No _EJ0 support for device\n"); 742 else if (ACPI_FAILURE(status)) 743 acpi_handle_warn(handle, "Eject failed (0x%x)\n", status); 744 745 return status; 746 } 747 748 /** 749 * acpi_evaluate_lck: Evaluate _LCK method to lock/unlock device 750 * @handle: ACPI device handle 751 * @lock: lock device if non-zero, otherwise unlock device 752 * 753 * Evaluate device's _LCK method if present to lock/unlock device 754 */ 755 acpi_status acpi_evaluate_lck(acpi_handle handle, int lock) 756 { 757 acpi_status status; 758 759 status = acpi_execute_simple_method(handle, "_LCK", !!lock); 760 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) { 761 if (lock) 762 acpi_handle_warn(handle, 763 "Locking device failed (0x%x)\n", status); 764 else 765 acpi_handle_warn(handle, 766 "Unlocking device failed (0x%x)\n", status); 767 } 768 769 return status; 770 } 771 772 /** 773 * acpi_evaluate_reg: Evaluate _REG method to register OpRegion presence 774 * @handle: ACPI device handle 775 * @space_id: ACPI address space id to register OpRegion presence for 776 * @function: Parameter to pass to _REG one of ACPI_REG_CONNECT or 777 * ACPI_REG_DISCONNECT 778 * 779 * Evaluate device's _REG method to register OpRegion presence. 780 */ 781 acpi_status acpi_evaluate_reg(acpi_handle handle, u8 space_id, u32 function) 782 { 783 struct acpi_object_list arg_list; 784 union acpi_object params[2]; 785 786 params[0].type = ACPI_TYPE_INTEGER; 787 params[0].integer.value = space_id; 788 params[1].type = ACPI_TYPE_INTEGER; 789 params[1].integer.value = function; 790 arg_list.count = 2; 791 arg_list.pointer = params; 792 793 return acpi_evaluate_object(handle, "_REG", &arg_list, NULL); 794 } 795 EXPORT_SYMBOL(acpi_evaluate_reg); 796 797 /** 798 * acpi_evaluate_dsm - evaluate device's _DSM method 799 * @handle: ACPI device handle 800 * @guid: GUID of requested functions, should be 16 bytes 801 * @rev: revision number of requested function 802 * @func: requested function number 803 * @argv4: the function specific parameter 804 * 805 * Evaluate device's _DSM method with specified GUID, revision id and 806 * function number. Caller needs to free the returned object. 807 * 808 * Though ACPI defines the fourth parameter for _DSM should be a package, 809 * some old BIOSes do expect a buffer or an integer etc. 810 */ 811 union acpi_object * 812 acpi_evaluate_dsm(acpi_handle handle, const guid_t *guid, u64 rev, u64 func, 813 union acpi_object *argv4) 814 { 815 acpi_status ret; 816 struct acpi_buffer buf = {ACPI_ALLOCATE_BUFFER, NULL}; 817 union acpi_object params[4]; 818 struct acpi_object_list input = { 819 .count = 4, 820 .pointer = params, 821 }; 822 823 params[0].type = ACPI_TYPE_BUFFER; 824 params[0].buffer.length = 16; 825 params[0].buffer.pointer = (u8 *)guid; 826 params[1].type = ACPI_TYPE_INTEGER; 827 params[1].integer.value = rev; 828 params[2].type = ACPI_TYPE_INTEGER; 829 params[2].integer.value = func; 830 if (argv4) { 831 params[3] = *argv4; 832 } else { 833 params[3].type = ACPI_TYPE_PACKAGE; 834 params[3].package.count = 0; 835 params[3].package.elements = NULL; 836 } 837 838 ret = acpi_evaluate_object(handle, "_DSM", &input, &buf); 839 if (ACPI_SUCCESS(ret)) 840 return (union acpi_object *)buf.pointer; 841 842 if (ret != AE_NOT_FOUND) 843 acpi_handle_warn(handle, 844 "failed to evaluate _DSM %pUb rev:%lld func:%lld (0x%x)\n", 845 guid, rev, func, ret); 846 847 return NULL; 848 } 849 EXPORT_SYMBOL(acpi_evaluate_dsm); 850 851 /** 852 * acpi_check_dsm - check if _DSM method supports requested functions. 853 * @handle: ACPI device handle 854 * @guid: GUID of requested functions, should be 16 bytes at least 855 * @rev: revision number of requested functions 856 * @funcs: bitmap of requested functions 857 * 858 * Evaluate device's _DSM method to check whether it supports requested 859 * functions. Currently only support 64 functions at maximum, should be 860 * enough for now. 861 */ 862 bool acpi_check_dsm(acpi_handle handle, const guid_t *guid, u64 rev, u64 funcs) 863 { 864 int i; 865 u64 mask = 0; 866 union acpi_object *obj; 867 868 if (funcs == 0) 869 return false; 870 871 obj = acpi_evaluate_dsm(handle, guid, rev, 0, NULL); 872 if (!obj) 873 return false; 874 875 /* For compatibility, old BIOSes may return an integer */ 876 if (obj->type == ACPI_TYPE_INTEGER) 877 mask = obj->integer.value; 878 else if (obj->type == ACPI_TYPE_BUFFER) 879 for (i = 0; i < obj->buffer.length && i < 8; i++) 880 mask |= (((u64)obj->buffer.pointer[i]) << (i * 8)); 881 ACPI_FREE(obj); 882 883 /* 884 * Bit 0 indicates whether there's support for any functions other than 885 * function 0 for the specified GUID and revision. 886 */ 887 if ((mask & 0x1) && (mask & funcs) == funcs) 888 return true; 889 890 return false; 891 } 892 EXPORT_SYMBOL(acpi_check_dsm); 893 894 /** 895 * acpi_dev_uid_to_integer - treat ACPI device _UID as integer 896 * @adev: ACPI device to get _UID from 897 * @integer: output buffer for integer 898 * 899 * Considers _UID as integer and converts it to @integer. 900 * 901 * Returns 0 on success, or negative error code otherwise. 902 */ 903 int acpi_dev_uid_to_integer(struct acpi_device *adev, u64 *integer) 904 { 905 const char *uid; 906 907 if (!adev) 908 return -ENODEV; 909 910 uid = acpi_device_uid(adev); 911 if (!uid) 912 return -ENODATA; 913 914 return kstrtou64(uid, 0, integer); 915 } 916 EXPORT_SYMBOL(acpi_dev_uid_to_integer); 917 918 /** 919 * acpi_dev_found - Detect presence of a given ACPI device in the namespace. 920 * @hid: Hardware ID of the device. 921 * 922 * Return %true if the device was present at the moment of invocation. 923 * Note that if the device is pluggable, it may since have disappeared. 924 * 925 * For this function to work, acpi_bus_scan() must have been executed 926 * which happens in the subsys_initcall() subsection. Hence, do not 927 * call from a subsys_initcall() or earlier (use acpi_get_devices() 928 * instead). Calling from module_init() is fine (which is synonymous 929 * with device_initcall()). 930 */ 931 bool acpi_dev_found(const char *hid) 932 { 933 struct acpi_device_bus_id *acpi_device_bus_id; 934 bool found = false; 935 936 mutex_lock(&acpi_device_lock); 937 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) 938 if (!strcmp(acpi_device_bus_id->bus_id, hid)) { 939 found = true; 940 break; 941 } 942 mutex_unlock(&acpi_device_lock); 943 944 return found; 945 } 946 EXPORT_SYMBOL(acpi_dev_found); 947 948 struct acpi_dev_match_info { 949 struct acpi_device_id hid[2]; 950 const char *uid; 951 s64 hrv; 952 }; 953 954 static int acpi_dev_match_cb(struct device *dev, const void *data) 955 { 956 struct acpi_device *adev = to_acpi_device(dev); 957 const struct acpi_dev_match_info *match = data; 958 unsigned long long hrv; 959 acpi_status status; 960 961 if (acpi_match_device_ids(adev, match->hid)) 962 return 0; 963 964 if (match->uid && !acpi_dev_uid_match(adev, match->uid)) 965 return 0; 966 967 if (match->hrv == -1) 968 return 1; 969 970 status = acpi_evaluate_integer(adev->handle, "_HRV", NULL, &hrv); 971 if (ACPI_FAILURE(status)) 972 return 0; 973 974 return hrv == match->hrv; 975 } 976 977 /** 978 * acpi_dev_present - Detect that a given ACPI device is present 979 * @hid: Hardware ID of the device. 980 * @uid: Unique ID of the device, pass NULL to not check _UID 981 * @hrv: Hardware Revision of the device, pass -1 to not check _HRV 982 * 983 * Return %true if a matching device was present at the moment of invocation. 984 * Note that if the device is pluggable, it may since have disappeared. 985 * 986 * Note that unlike acpi_dev_found() this function checks the status 987 * of the device. So for devices which are present in the DSDT, but 988 * which are disabled (their _STA callback returns 0) this function 989 * will return false. 990 * 991 * For this function to work, acpi_bus_scan() must have been executed 992 * which happens in the subsys_initcall() subsection. Hence, do not 993 * call from a subsys_initcall() or earlier (use acpi_get_devices() 994 * instead). Calling from module_init() is fine (which is synonymous 995 * with device_initcall()). 996 */ 997 bool acpi_dev_present(const char *hid, const char *uid, s64 hrv) 998 { 999 struct acpi_dev_match_info match = {}; 1000 struct device *dev; 1001 1002 strscpy(match.hid[0].id, hid, sizeof(match.hid[0].id)); 1003 match.uid = uid; 1004 match.hrv = hrv; 1005 1006 dev = bus_find_device(&acpi_bus_type, NULL, &match, acpi_dev_match_cb); 1007 put_device(dev); 1008 return !!dev; 1009 } 1010 EXPORT_SYMBOL(acpi_dev_present); 1011 1012 /** 1013 * acpi_dev_get_next_match_dev - Return the next match of ACPI device 1014 * @adev: Pointer to the previous ACPI device matching this @hid, @uid and @hrv 1015 * @hid: Hardware ID of the device. 1016 * @uid: Unique ID of the device, pass NULL to not check _UID 1017 * @hrv: Hardware Revision of the device, pass -1 to not check _HRV 1018 * 1019 * Return the next match of ACPI device if another matching device was present 1020 * at the moment of invocation, or NULL otherwise. 1021 * 1022 * The caller is responsible for invoking acpi_dev_put() on the returned device. 1023 * On the other hand the function invokes acpi_dev_put() on the given @adev 1024 * assuming that its reference counter had been increased beforehand. 1025 * 1026 * See additional information in acpi_dev_present() as well. 1027 */ 1028 struct acpi_device * 1029 acpi_dev_get_next_match_dev(struct acpi_device *adev, const char *hid, const char *uid, s64 hrv) 1030 { 1031 struct device *start = adev ? &adev->dev : NULL; 1032 struct acpi_dev_match_info match = {}; 1033 struct device *dev; 1034 1035 strscpy(match.hid[0].id, hid, sizeof(match.hid[0].id)); 1036 match.uid = uid; 1037 match.hrv = hrv; 1038 1039 dev = bus_find_device(&acpi_bus_type, start, &match, acpi_dev_match_cb); 1040 acpi_dev_put(adev); 1041 return dev ? to_acpi_device(dev) : NULL; 1042 } 1043 EXPORT_SYMBOL(acpi_dev_get_next_match_dev); 1044 1045 /** 1046 * acpi_dev_get_first_match_dev - Return the first match of ACPI device 1047 * @hid: Hardware ID of the device. 1048 * @uid: Unique ID of the device, pass NULL to not check _UID 1049 * @hrv: Hardware Revision of the device, pass -1 to not check _HRV 1050 * 1051 * Return the first match of ACPI device if a matching device was present 1052 * at the moment of invocation, or NULL otherwise. 1053 * 1054 * The caller is responsible for invoking acpi_dev_put() on the returned device. 1055 * 1056 * See additional information in acpi_dev_present() as well. 1057 */ 1058 struct acpi_device * 1059 acpi_dev_get_first_match_dev(const char *hid, const char *uid, s64 hrv) 1060 { 1061 return acpi_dev_get_next_match_dev(NULL, hid, uid, hrv); 1062 } 1063 EXPORT_SYMBOL(acpi_dev_get_first_match_dev); 1064 1065 /** 1066 * acpi_reduced_hardware - Return if this is an ACPI-reduced-hw machine 1067 * 1068 * Return true when running on an ACPI-reduced-hw machine, false otherwise. 1069 */ 1070 bool acpi_reduced_hardware(void) 1071 { 1072 return acpi_gbl_reduced_hardware; 1073 } 1074 EXPORT_SYMBOL_GPL(acpi_reduced_hardware); 1075 1076 /* 1077 * acpi_backlight= handling, this is done here rather then in video_detect.c 1078 * because __setup cannot be used in modules. 1079 */ 1080 char acpi_video_backlight_string[16]; 1081 EXPORT_SYMBOL(acpi_video_backlight_string); 1082 1083 static int __init acpi_backlight(char *str) 1084 { 1085 strscpy(acpi_video_backlight_string, str, 1086 sizeof(acpi_video_backlight_string)); 1087 return 1; 1088 } 1089 __setup("acpi_backlight=", acpi_backlight); 1090 1091 /** 1092 * acpi_dev_is_video_device - test if device matches against ACPI video device IDs 1093 * @adev: ACPI device to test 1094 * 1095 * Return: true when matches, otherwise false. 1096 */ 1097 bool acpi_dev_is_video_device(struct acpi_device *adev) 1098 { 1099 static const struct acpi_device_id video_device_ids[] = { 1100 { .id = ACPI_VIDEO_HID }, 1101 { } 1102 }; 1103 1104 return adev && !acpi_match_device_ids(adev, video_device_ids); 1105 } 1106 EXPORT_SYMBOL(acpi_dev_is_video_device); 1107 1108 /** 1109 * acpi_match_platform_list - Check if the system matches with a given list 1110 * @plat: pointer to acpi_platform_list table terminated by a NULL entry 1111 * 1112 * Return the matched index if the system is found in the platform list. 1113 * Return -ENODEV for no match, or another negative error code if a table 1114 * header could not be read and no entry matched. 1115 */ 1116 int acpi_match_platform_list(const struct acpi_platform_list *plat) 1117 { 1118 struct acpi_table_header hdr; 1119 acpi_status status; 1120 int ret = -ENODEV; 1121 int idx = 0; 1122 1123 if (acpi_disabled) 1124 return -ENODEV; 1125 1126 for (; plat->oem_id[0]; plat++, idx++) { 1127 status = acpi_get_table_header(plat->table, 0, &hdr); 1128 if (ACPI_FAILURE(status)) { 1129 if (status != AE_NOT_FOUND) 1130 ret = status == AE_NO_MEMORY ? -ENOMEM : -EIO; 1131 continue; 1132 } 1133 1134 if (strncmp(plat->oem_id, hdr.oem_id, ACPI_OEM_ID_SIZE)) 1135 continue; 1136 1137 if (strncmp(plat->oem_table_id, hdr.oem_table_id, ACPI_OEM_TABLE_ID_SIZE)) 1138 continue; 1139 1140 if ((plat->pred == all_versions) || 1141 (plat->pred == less_than_or_equal && hdr.oem_revision <= plat->oem_revision) || 1142 (plat->pred == greater_than_or_equal && hdr.oem_revision >= plat->oem_revision) || 1143 (plat->pred == equal && hdr.oem_revision == plat->oem_revision)) 1144 return idx; 1145 } 1146 1147 return ret; 1148 } 1149 EXPORT_SYMBOL(acpi_match_platform_list); 1150