// SPDX-License-Identifier: GPL-2.0-or-later/* * acpi_utils.c - ACPI Utility Functions ($Revision: 10 $) * * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> */ #define pr_fmt(fmt) "ACPI: utils: " fmt #include <linux/kernel.h>#include <linux/module.h>#include <linux/slab.h>#include <linux/init.h>#include <linux/types.h>#include <linux/hardirq.h>#include <linux/acpi.h>#include <linux/dynamic_debug.h> #include "internal.h"#include "sleep.h" /* -------------------------------------------------------------------------- Object Evaluation Helpers -------------------------------------------------------------------------- */static void acpi_util_eval_error(acpi_handle h, acpi_string p, acpi_status s){ acpi_handle_debug(h, "Evaluate [%s]: %s\n", p, acpi_format_exception(s));} acpi_statusacpi_extract_package(union acpi_object *package, struct acpi_buffer *format, struct acpi_buffer *buffer){ u32 size_required = 0; u32 tail_offset = 0; char *format_string = NULL; u32 format_count = 0; u32 i = 0; u8 *head = NULL; u8 *tail = NULL; if (!package || (package->type != ACPI_TYPE_PACKAGE) || (package->package.count < 1)) { pr_debug("Invalid package argument\n"); return AE_BAD_PARAMETER; } if (!format || !format->pointer || (format->length < 1)) { pr_debug("Invalid format argument\n"); return AE_BAD_PARAMETER; } if (!buffer) { pr_debug("Invalid buffer argument\n"); return AE_BAD_PARAMETER; } format_count = (format->length / sizeof(char)) - 1; if (format_count > package->package.count) { pr_debug("Format specifies more objects [%d] than present [%d]\n", format_count, package->package.count); return AE_BAD_DATA; } format_string = format->pointer; /* * Calculate size_required. */ for (i = 0; i < format_count; i++) { union acpi_object *element = &(package->package.elements[i]); switch (element->type) { case ACPI_TYPE_INTEGER: switch (format_string[i]) { case 'N': size_required += sizeof(u64); tail_offset += sizeof(u64); break; case 'S': size_required += sizeof(char *) + sizeof(u64) + sizeof(char); tail_offset += sizeof(char *); break; default: pr_debug("Invalid package element [%d]: got number, expected [%c]\n", i, format_string[i]); return AE_BAD_DATA; } break; case ACPI_TYPE_STRING: case ACPI_TYPE_BUFFER: switch (format_string[i]) { case 'S': size_required += sizeof(char *) + (element->string.length * sizeof(char)) + sizeof(char); tail_offset += sizeof(char *); break; case 'B': size_required += sizeof(u8 *) + element->buffer.length; tail_offset += sizeof(u8 *); break; default: pr_debug("Invalid package element [%d] got string/buffer, expected [%c]\n", i, format_string[i]); return AE_BAD_DATA; } break; case ACPI_TYPE_LOCAL_REFERENCE: switch (format_string[i]) { case 'R': size_required += sizeof(void *); tail_offset += sizeof(void *); break; default: pr_debug("Invalid package element [%d] got reference, expected [%c]\n", i, format_string[i]); return AE_BAD_DATA; } break; case ACPI_TYPE_PACKAGE: default: pr_debug("Unsupported element at index=%d\n", i); /* TBD: handle nested packages... */ return AE_SUPPORT; } } /* * Validate output buffer. */ if (buffer->length == ACPI_ALLOCATE_BUFFER) { buffer->pointer = ACPI_ALLOCATE_ZEROED(size_required); if (!buffer->pointer) return AE_NO_MEMORY; buffer->length = size_required; } else { if (buffer->length < size_required) { buffer->length = size_required; return AE_BUFFER_OVERFLOW; } else if (buffer->length != size_required || !buffer->pointer) { return AE_BAD_PARAMETER; } } head = buffer->pointer; tail = buffer->pointer + tail_offset; /* * Extract package data. */ for (i = 0; i < format_count; i++) { u8 **pointer = NULL; union acpi_object *element = &(package->package.elements[i]); switch (element->type) { case ACPI_TYPE_INTEGER: switch (format_string[i]) { case 'N': *((u64 *) head) = element->integer.value; head += sizeof(u64); break; case 'S': pointer = (u8 **) head; *pointer = tail; *((u64 *) tail) = element->integer.value; head += sizeof(u64 *); tail += sizeof(u64); /* NULL terminate string */ *tail = (char)0; tail += sizeof(char); break; default: /* Should never get here */ break; } break; case ACPI_TYPE_STRING: case ACPI_TYPE_BUFFER: switch (format_string[i]) { case 'S': pointer = (u8 **) head; *pointer = tail; memcpy(tail, element->string.pointer, element->string.length); head += sizeof(char *); tail += element->string.length * sizeof(char); /* NULL terminate string */ *tail = (char)0; tail += sizeof(char); break; case 'B': pointer = (u8 **) head; *pointer = tail; memcpy(tail, element->buffer.pointer, element->buffer.length); head += sizeof(u8 *); tail += element->buffer.length; break; default: /* Should never get here */ break; } break; case ACPI_TYPE_LOCAL_REFERENCE: switch (format_string[i]) { case 'R': *(void **)head = (void *)element->reference.handle; head += sizeof(void *); break; default: /* Should never get here */ break; } break; case ACPI_TYPE_PACKAGE: /* TBD: handle nested packages... */ default: /* Should never get here */ break; } } return AE_OK;} EXPORT_SYMBOL(acpi_extract_package); acpi_statusacpi_evaluate_integer(acpi_handle handle, acpi_string pathname, struct acpi_object_list *arguments, unsigned long long *data){ acpi_status status = AE_OK; union acpi_object element; struct acpi_buffer buffer = { 0, NULL }; if (!data) return AE_BAD_PARAMETER; buffer.length = sizeof(union acpi_object); buffer.pointer = &element; status = acpi_evaluate_object(handle, pathname, arguments, &buffer); if (ACPI_FAILURE(status)) { acpi_util_eval_error(handle, pathname, status); return status; } if (element.type != ACPI_TYPE_INTEGER) { acpi_util_eval_error(handle, pathname, AE_BAD_DATA); return AE_BAD_DATA; } *data = element.integer.value; acpi_handle_debug(handle, "Return value [%llu]\n", *data); return AE_OK;} EXPORT_SYMBOL(acpi_evaluate_integer); int acpi_get_local_u64_address(acpi_handle handle, u64 *addr){ acpi_status status; status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, addr); if (ACPI_FAILURE(status)) return -ENODATA; return 0;}EXPORT_SYMBOL(acpi_get_local_u64_address); int acpi_get_local_address(acpi_handle handle, u32 *addr){ u64 adr; int ret; ret = acpi_get_local_u64_address(handle, &adr); if (ret < 0) return ret; *addr = (u32)adr; return 0;}EXPORT_SYMBOL(acpi_get_local_address); #define ACPI_MAX_SUB_BUF_SIZE 9 const char *acpi_get_subsystem_id(acpi_handle handle){ struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; union acpi_object *obj; acpi_status status; const char *sub; size_t len; status = acpi_evaluate_object(handle, METHOD_NAME__SUB, NULL, &buffer); if (ACPI_FAILURE(status)) { acpi_handle_debug(handle, "Reading ACPI _SUB failed: %#x\n", status); return ERR_PTR(-ENODATA); } obj = buffer.pointer; if (obj->type == ACPI_TYPE_STRING) { len = strlen(obj->string.pointer); if (len < ACPI_MAX_SUB_BUF_SIZE && len > 0) { sub = kstrdup(obj->string.pointer, GFP_KERNEL); if (!sub) sub = ERR_PTR(-ENOMEM); } else { acpi_handle_err(handle, "ACPI _SUB Length %zu is Invalid\n", len); sub = ERR_PTR(-ENODATA); } } else { acpi_handle_warn(handle, "Warning ACPI _SUB did not return a string\n"); sub = ERR_PTR(-ENODATA); } acpi_os_free(buffer.pointer); return sub;}EXPORT_SYMBOL_GPL(acpi_get_subsystem_id); bool acpi_evaluate_reference(acpi_handle handle, acpi_string pathname, struct acpi_object_list *arguments, struct acpi_handle_list *list){ struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; union acpi_object *package; acpi_status status; bool ret = false; u32 i; if (!list) return false; /* Evaluate object. */ status = acpi_evaluate_object(handle, pathname, arguments, &buffer); if (ACPI_FAILURE(status)) goto end; package = buffer.pointer; if (buffer.length == 0 || !package || package->type != ACPI_TYPE_PACKAGE || !package->package.count) goto err; list->count = package->package.count; list->handles = kzalloc_objs(*list->handles, list->count); if (!list->handles) goto err_clear; /* Extract package data. */ for (i = 0; i < list->count; i++) { union acpi_object *element = &(package->package.elements[i]); if (element->type != ACPI_TYPE_LOCAL_REFERENCE || !element->reference.handle) goto err_free; /* Get the acpi_handle. */ list->handles[i] = element->reference.handle; acpi_handle_debug(list->handles[i], "Found in reference list\n"); } ret = true; end: kfree(buffer.pointer); return ret; err_free: kfree(list->handles); list->handles = NULL; err_clear: list->count = 0; err: acpi_util_eval_error(handle, pathname, status); goto end;} EXPORT_SYMBOL(acpi_evaluate_reference); /** * acpi_handle_list_equal - Check if two ACPI handle lists are the same * @list1: First list to compare. * @list2: Second list to compare. * * Return true if the given ACPI handle lists are of the same size and * contain the same ACPI handles in the same order. Otherwise, return false. */bool acpi_handle_list_equal(struct acpi_handle_list *list1, struct acpi_handle_list *list2){ return list1->count == list2->count && !memcmp(list1->handles, list2->handles, list1->count * sizeof(*list1->handles));}EXPORT_SYMBOL_GPL(acpi_handle_list_equal); /** * acpi_handle_list_replace - Replace one ACPI handle list with another * @dst: ACPI handle list to replace. * @src: Source ACPI handle list. * * Free the handles table in @dst, move the handles table from @src to @dst, * copy count from @src to @dst and clear @src. */void acpi_handle_list_replace(struct acpi_handle_list *dst, struct acpi_handle_list *src){ if (dst->count) kfree(dst->handles); dst->count = src->count; dst->handles = src->handles; src->handles = NULL; src->count = 0;}EXPORT_SYMBOL_GPL(acpi_handle_list_replace); /** * acpi_handle_list_free - Free the handles table in an ACPI handle list * @list: ACPI handle list to free. * * Free the handles table in @list and clear its count field. */void acpi_handle_list_free(struct acpi_handle_list *list){ if (!list->count) return; kfree(list->handles); list->count = 0;}EXPORT_SYMBOL_GPL(acpi_handle_list_free); /** * acpi_device_dep - Check ACPI device dependency * @target: ACPI handle of the target ACPI device. * @match: ACPI handle to look up in the target's _DEP list. * * Return true if @match is present in the list returned by _DEP for * @target or false otherwise. */bool acpi_device_dep(acpi_handle target, acpi_handle match){ struct acpi_handle_list dep_devices; bool ret = false; int i; if (!acpi_has_method(target, "_DEP")) return false; if (!acpi_evaluate_reference(target, "_DEP", NULL, &dep_devices)) { acpi_handle_debug(target, "Failed to evaluate _DEP.\n"); return false; } for (i = 0; i < dep_devices.count; i++) { if (dep_devices.handles[i] == match) { ret = true; break; } } acpi_handle_list_free(&dep_devices); return ret;}EXPORT_SYMBOL_GPL(acpi_device_dep); boolacpi_get_physical_device_location(acpi_handle handle, struct acpi_pld_info **pld){ acpi_status status;Lines 1–500 of 1101. Load more
// SPDX-License-Identifier: GPL-2.0-or-later /* * acpi_utils.c - ACPI Utility Functions ($Revision: 10 $) * * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> */ #define pr_fmt(fmt) "ACPI: utils: " fmt #include <linux/kernel.h> #include <linux/module.h> #include <linux/slab.h> #include <linux/init.h> #include <linux/types.h> #include <linux/hardirq.h> #include <linux/acpi.h> #include <linux/dynamic_debug.h> #include "internal.h" #include "sleep.h" /* -------------------------------------------------------------------------- Object Evaluation Helpers -------------------------------------------------------------------------- */ static void acpi_util_eval_error(acpi_handle h, acpi_string p, acpi_status s) { acpi_handle_debug(h, "Evaluate [%s]: %s\n", p, acpi_format_exception(s)); } acpi_status acpi_extract_package(union acpi_object *package, struct acpi_buffer *format, struct acpi_buffer *buffer) { u32 size_required = 0; u32 tail_offset = 0; char *format_string = NULL; u32 format_count = 0; u32 i = 0; u8 *head = NULL; u8 *tail = NULL; if (!package || (package->type != ACPI_TYPE_PACKAGE) || (package->package.count < 1)) { pr_debug("Invalid package argument\n"); return AE_BAD_PARAMETER; } if (!format || !format->pointer || (format->length < 1)) { pr_debug("Invalid format argument\n"); return AE_BAD_PARAMETER; } if (!buffer) { pr_debug("Invalid buffer argument\n"); return AE_BAD_PARAMETER; } format_count = (format->length / sizeof(char)) - 1; if (format_count > package->package.count) { pr_debug("Format specifies more objects [%d] than present [%d]\n", format_count, package->package.count); return AE_BAD_DATA; } format_string = format->pointer; /* * Calculate size_required. */ for (i = 0; i < format_count; i++) { union acpi_object *element = &(package->package.elements[i]); switch (element->type) { case ACPI_TYPE_INTEGER: switch (format_string[i]) { case 'N': size_required += sizeof(u64); tail_offset += sizeof(u64); break; case 'S': size_required += sizeof(char *) + sizeof(u64) + sizeof(char); tail_offset += sizeof(char *); break; default: pr_debug("Invalid package element [%d]: got number, expected [%c]\n", i, format_string[i]); return AE_BAD_DATA; } break; case ACPI_TYPE_STRING: case ACPI_TYPE_BUFFER: switch (format_string[i]) { case 'S': size_required += sizeof(char *) + (element->string.length * sizeof(char)) + sizeof(char); tail_offset += sizeof(char *); break; case 'B': size_required += sizeof(u8 *) + element->buffer.length; tail_offset += sizeof(u8 *); break; default: pr_debug("Invalid package element [%d] got string/buffer, expected [%c]\n", i, format_string[i]); return AE_BAD_DATA; } break; case ACPI_TYPE_LOCAL_REFERENCE: switch (format_string[i]) { case 'R': size_required += sizeof(void *); tail_offset += sizeof(void *); break; default: pr_debug("Invalid package element [%d] got reference, expected [%c]\n", i, format_string[i]); return AE_BAD_DATA; } break; case ACPI_TYPE_PACKAGE: default: pr_debug("Unsupported element at index=%d\n", i); /* TBD: handle nested packages... */ return AE_SUPPORT; } } /* * Validate output buffer. */ if (buffer->length == ACPI_ALLOCATE_BUFFER) { buffer->pointer = ACPI_ALLOCATE_ZEROED(size_required); if (!buffer->pointer) return AE_NO_MEMORY; buffer->length = size_required; } else { if (buffer->length < size_required) { buffer->length = size_required; return AE_BUFFER_OVERFLOW; } else if (buffer->length != size_required || !buffer->pointer) { return AE_BAD_PARAMETER; } } head = buffer->pointer; tail = buffer->pointer + tail_offset; /* * Extract package data. */ for (i = 0; i < format_count; i++) { u8 **pointer = NULL; union acpi_object *element = &(package->package.elements[i]); switch (element->type) { case ACPI_TYPE_INTEGER: switch (format_string[i]) { case 'N': *((u64 *) head) = element->integer.value; head += sizeof(u64); break; case 'S': pointer = (u8 **) head; *pointer = tail; *((u64 *) tail) = element->integer.value; head += sizeof(u64 *); tail += sizeof(u64); /* NULL terminate string */ *tail = (char)0; tail += sizeof(char); break; default: /* Should never get here */ break; } break; case ACPI_TYPE_STRING: case ACPI_TYPE_BUFFER: switch (format_string[i]) { case 'S': pointer = (u8 **) head; *pointer = tail; memcpy(tail, element->string.pointer, element->string.length); head += sizeof(char *); tail += element->string.length * sizeof(char); /* NULL terminate string */ *tail = (char)0; tail += sizeof(char); break; case 'B': pointer = (u8 **) head; *pointer = tail; memcpy(tail, element->buffer.pointer, element->buffer.length); head += sizeof(u8 *); tail += element->buffer.length; break; default: /* Should never get here */ break; } break; case ACPI_TYPE_LOCAL_REFERENCE: switch (format_string[i]) { case 'R': *(void **)head = (void *)element->reference.handle; head += sizeof(void *); break; default: /* Should never get here */ break; } break; case ACPI_TYPE_PACKAGE: /* TBD: handle nested packages... */ default: /* Should never get here */ break; } } return AE_OK; } EXPORT_SYMBOL(acpi_extract_package); acpi_status acpi_evaluate_integer(acpi_handle handle, acpi_string pathname, struct acpi_object_list *arguments, unsigned long long *data) { acpi_status status = AE_OK; union acpi_object element; struct acpi_buffer buffer = { 0, NULL }; if (!data) return AE_BAD_PARAMETER; buffer.length = sizeof(union acpi_object); buffer.pointer = &element; status = acpi_evaluate_object(handle, pathname, arguments, &buffer); if (ACPI_FAILURE(status)) { acpi_util_eval_error(handle, pathname, status); return status; } if (element.type != ACPI_TYPE_INTEGER) { acpi_util_eval_error(handle, pathname, AE_BAD_DATA); return AE_BAD_DATA; } *data = element.integer.value; acpi_handle_debug(handle, "Return value [%llu]\n", *data); return AE_OK; } EXPORT_SYMBOL(acpi_evaluate_integer); int acpi_get_local_u64_address(acpi_handle handle, u64 *addr) { acpi_status status; status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, addr); if (ACPI_FAILURE(status)) return -ENODATA; return 0; } EXPORT_SYMBOL(acpi_get_local_u64_address); int acpi_get_local_address(acpi_handle handle, u32 *addr) { u64 adr; int ret; ret = acpi_get_local_u64_address(handle, &adr); if (ret < 0) return ret; *addr = (u32)adr; return 0; } EXPORT_SYMBOL(acpi_get_local_address); #define ACPI_MAX_SUB_BUF_SIZE 9 const char *acpi_get_subsystem_id(acpi_handle handle) { struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; union acpi_object *obj; acpi_status status; const char *sub; size_t len; status = acpi_evaluate_object(handle, METHOD_NAME__SUB, NULL, &buffer); if (ACPI_FAILURE(status)) { acpi_handle_debug(handle, "Reading ACPI _SUB failed: %#x\n", status); return ERR_PTR(-ENODATA); } obj = buffer.pointer; if (obj->type == ACPI_TYPE_STRING) { len = strlen(obj->string.pointer); if (len < ACPI_MAX_SUB_BUF_SIZE && len > 0) { sub = kstrdup(obj->string.pointer, GFP_KERNEL); if (!sub) sub = ERR_PTR(-ENOMEM); } else { acpi_handle_err(handle, "ACPI _SUB Length %zu is Invalid\n", len); sub = ERR_PTR(-ENODATA); } } else { acpi_handle_warn(handle, "Warning ACPI _SUB did not return a string\n"); sub = ERR_PTR(-ENODATA); } acpi_os_free(buffer.pointer); return sub; } EXPORT_SYMBOL_GPL(acpi_get_subsystem_id); bool acpi_evaluate_reference(acpi_handle handle, acpi_string pathname, struct acpi_object_list *arguments, struct acpi_handle_list *list) { struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; union acpi_object *package; acpi_status status; bool ret = false; u32 i; if (!list) return false; /* Evaluate object. */ status = acpi_evaluate_object(handle, pathname, arguments, &buffer); if (ACPI_FAILURE(status)) goto end; package = buffer.pointer; if (buffer.length == 0 || !package || package->type != ACPI_TYPE_PACKAGE || !package->package.count) goto err; list->count = package->package.count; list->handles = kzalloc_objs(*list->handles, list->count); if (!list->handles) goto err_clear; /* Extract package data. */ for (i = 0; i < list->count; i++) { union acpi_object *element = &(package->package.elements[i]); if (element->type != ACPI_TYPE_LOCAL_REFERENCE || !element->reference.handle) goto err_free; /* Get the acpi_handle. */ list->handles[i] = element->reference.handle; acpi_handle_debug(list->handles[i], "Found in reference list\n"); } ret = true; end: kfree(buffer.pointer); return ret; err_free: kfree(list->handles); list->handles = NULL; err_clear: list->count = 0; err: acpi_util_eval_error(handle, pathname, status); goto end; } EXPORT_SYMBOL(acpi_evaluate_reference); /** * acpi_handle_list_equal - Check if two ACPI handle lists are the same * @list1: First list to compare. * @list2: Second list to compare. * * Return true if the given ACPI handle lists are of the same size and * contain the same ACPI handles in the same order. Otherwise, return false. */ bool acpi_handle_list_equal(struct acpi_handle_list *list1, struct acpi_handle_list *list2) { return list1->count == list2->count && !memcmp(list1->handles, list2->handles, list1->count * sizeof(*list1->handles)); } EXPORT_SYMBOL_GPL(acpi_handle_list_equal); /** * acpi_handle_list_replace - Replace one ACPI handle list with another * @dst: ACPI handle list to replace. * @src: Source ACPI handle list. * * Free the handles table in @dst, move the handles table from @src to @dst, * copy count from @src to @dst and clear @src. */ void acpi_handle_list_replace(struct acpi_handle_list *dst, struct acpi_handle_list *src) { if (dst->count) kfree(dst->handles); dst->count = src->count; dst->handles = src->handles; src->handles = NULL; src->count = 0; } EXPORT_SYMBOL_GPL(acpi_handle_list_replace); /** * acpi_handle_list_free - Free the handles table in an ACPI handle list * @list: ACPI handle list to free. * * Free the handles table in @list and clear its count field. */ void acpi_handle_list_free(struct acpi_handle_list *list) { if (!list->count) return; kfree(list->handles); list->count = 0; } EXPORT_SYMBOL_GPL(acpi_handle_list_free); /** * acpi_device_dep - Check ACPI device dependency * @target: ACPI handle of the target ACPI device. * @match: ACPI handle to look up in the target's _DEP list. * * Return true if @match is present in the list returned by _DEP for * @target or false otherwise. */ bool acpi_device_dep(acpi_handle target, acpi_handle match) { struct acpi_handle_list dep_devices; bool ret = false; int i; if (!acpi_has_method(target, "_DEP")) return false; if (!acpi_evaluate_reference(target, "_DEP", NULL, &dep_devices)) { acpi_handle_debug(target, "Failed to evaluate _DEP.\n"); return false; } for (i = 0; i < dep_devices.count; i++) { if (dep_devices.handles[i] == match) { ret = true; break; } } acpi_handle_list_free(&dep_devices); return ret; } EXPORT_SYMBOL_GPL(acpi_device_dep); bool acpi_get_physical_device_location(acpi_handle handle, struct acpi_pld_info **pld) { acpi_status status;