Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
0002 /*******************************************************************************
0003  *
0004  * Module Name: rslist - Linked list utilities
0005  *
0006  ******************************************************************************/
0007 
0008 #include <acpi/acpi.h>
0009 #include "accommon.h"
0010 #include "acresrc.h"
0011 
0012 #define _COMPONENT          ACPI_RESOURCES
0013 ACPI_MODULE_NAME("rslist")
0014 
0015 /*******************************************************************************
0016  *
0017  * FUNCTION:    acpi_rs_convert_aml_to_resources
0018  *
0019  * PARAMETERS:  acpi_walk_aml_callback
0020  *              resource_ptr            - Pointer to the buffer that will
0021  *                                        contain the output structures
0022  *
0023  * RETURN:      Status
0024  *
0025  * DESCRIPTION: Convert an AML resource to an internal representation of the
0026  *              resource that is aligned and easier to access.
0027  *
0028  ******************************************************************************/
0029 acpi_status
0030 acpi_rs_convert_aml_to_resources(u8 * aml,
0031                  u32 length,
0032                  u32 offset, u8 resource_index, void **context)
0033 {
0034     struct acpi_resource **resource_ptr =
0035         ACPI_CAST_INDIRECT_PTR(struct acpi_resource, context);
0036     struct acpi_resource *resource;
0037     union aml_resource *aml_resource;
0038     struct acpi_rsconvert_info *conversion_table;
0039     acpi_status status;
0040 
0041     ACPI_FUNCTION_TRACE(rs_convert_aml_to_resources);
0042 
0043     /*
0044      * Check that the input buffer and all subsequent pointers into it
0045      * are aligned on a native word boundary. Most important on IA64
0046      */
0047     resource = *resource_ptr;
0048     if (ACPI_IS_MISALIGNED(resource)) {
0049         ACPI_WARNING((AE_INFO,
0050                   "Misaligned resource pointer %p", resource));
0051     }
0052 
0053     /* Get the appropriate conversion info table */
0054 
0055     aml_resource = ACPI_CAST_PTR(union aml_resource, aml);
0056 
0057     if (acpi_ut_get_resource_type(aml) == ACPI_RESOURCE_NAME_SERIAL_BUS) {
0058         if (aml_resource->common_serial_bus.type >
0059             AML_RESOURCE_MAX_SERIALBUSTYPE) {
0060             conversion_table = NULL;
0061         } else {
0062             /* This is an I2C, SPI, UART, or CSI2 serial_bus descriptor */
0063 
0064             conversion_table =
0065                 acpi_gbl_convert_resource_serial_bus_dispatch
0066                 [aml_resource->common_serial_bus.type];
0067         }
0068     } else {
0069         conversion_table =
0070             acpi_gbl_get_resource_dispatch[resource_index];
0071     }
0072 
0073     if (!conversion_table) {
0074         ACPI_ERROR((AE_INFO,
0075                 "Invalid/unsupported resource descriptor: Type 0x%2.2X",
0076                 resource_index));
0077         return_ACPI_STATUS(AE_AML_INVALID_RESOURCE_TYPE);
0078     }
0079 
0080     /* Convert the AML byte stream resource to a local resource struct */
0081 
0082     status =
0083         acpi_rs_convert_aml_to_resource(resource, aml_resource,
0084                         conversion_table);
0085     if (ACPI_FAILURE(status)) {
0086         ACPI_EXCEPTION((AE_INFO, status,
0087                 "Could not convert AML resource (Type 0x%X)",
0088                 *aml));
0089         return_ACPI_STATUS(status);
0090     }
0091 
0092     if (!resource->length) {
0093         ACPI_EXCEPTION((AE_INFO, status,
0094                 "Zero-length resource returned from RsConvertAmlToResource"));
0095     }
0096 
0097     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
0098               "Type %.2X, AmlLength %.2X InternalLength %.2X\n",
0099               acpi_ut_get_resource_type(aml), length,
0100               resource->length));
0101 
0102     /* Point to the next structure in the output buffer */
0103 
0104     *resource_ptr = ACPI_NEXT_RESOURCE(resource);
0105     return_ACPI_STATUS(AE_OK);
0106 }
0107 
0108 /*******************************************************************************
0109  *
0110  * FUNCTION:    acpi_rs_convert_resources_to_aml
0111  *
0112  * PARAMETERS:  resource            - Pointer to the resource linked list
0113  *              aml_size_needed     - Calculated size of the byte stream
0114  *                                    needed from calling acpi_rs_get_aml_length()
0115  *                                    The size of the output_buffer is
0116  *                                    guaranteed to be >= aml_size_needed
0117  *              output_buffer       - Pointer to the buffer that will
0118  *                                    contain the byte stream
0119  *
0120  * RETURN:      Status
0121  *
0122  * DESCRIPTION: Takes the resource linked list and parses it, creating a
0123  *              byte stream of resources in the caller's output buffer
0124  *
0125  ******************************************************************************/
0126 
0127 acpi_status
0128 acpi_rs_convert_resources_to_aml(struct acpi_resource *resource,
0129                  acpi_size aml_size_needed, u8 * output_buffer)
0130 {
0131     u8 *aml = output_buffer;
0132     u8 *end_aml = output_buffer + aml_size_needed;
0133     struct acpi_rsconvert_info *conversion_table;
0134     acpi_status status;
0135 
0136     ACPI_FUNCTION_TRACE(rs_convert_resources_to_aml);
0137 
0138     /* Walk the resource descriptor list, convert each descriptor */
0139 
0140     while (aml < end_aml) {
0141 
0142         /* Validate the (internal) Resource Type */
0143 
0144         if (resource->type > ACPI_RESOURCE_TYPE_MAX) {
0145             ACPI_ERROR((AE_INFO,
0146                     "Invalid descriptor type (0x%X) in resource list",
0147                     resource->type));
0148             return_ACPI_STATUS(AE_BAD_DATA);
0149         }
0150 
0151         /* Sanity check the length. It must not be zero, or we loop forever */
0152 
0153         if (!resource->length) {
0154             ACPI_ERROR((AE_INFO,
0155                     "Invalid zero length descriptor in resource list\n"));
0156             return_ACPI_STATUS(AE_AML_BAD_RESOURCE_LENGTH);
0157         }
0158 
0159         /* Perform the conversion */
0160 
0161         if (resource->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
0162             if (resource->data.common_serial_bus.type >
0163                 AML_RESOURCE_MAX_SERIALBUSTYPE) {
0164                 conversion_table = NULL;
0165             } else {
0166                 /* This is an I2C, SPI, UART or CSI2 serial_bus descriptor */
0167 
0168                 conversion_table =
0169                     acpi_gbl_convert_resource_serial_bus_dispatch
0170                     [resource->data.common_serial_bus.type];
0171             }
0172         } else {
0173             conversion_table =
0174                 acpi_gbl_set_resource_dispatch[resource->type];
0175         }
0176 
0177         if (!conversion_table) {
0178             ACPI_ERROR((AE_INFO,
0179                     "Invalid/unsupported resource descriptor: Type 0x%2.2X",
0180                     resource->type));
0181             return_ACPI_STATUS(AE_AML_INVALID_RESOURCE_TYPE);
0182         }
0183 
0184         status = acpi_rs_convert_resource_to_aml(resource,
0185                                  ACPI_CAST_PTR(union
0186                                        aml_resource,
0187                                        aml),
0188                              conversion_table);
0189         if (ACPI_FAILURE(status)) {
0190             ACPI_EXCEPTION((AE_INFO, status,
0191                     "Could not convert resource (type 0x%X) to AML",
0192                     resource->type));
0193             return_ACPI_STATUS(status);
0194         }
0195 
0196         /* Perform final sanity check on the new AML resource descriptor */
0197 
0198         status =
0199             acpi_ut_validate_resource(NULL,
0200                           ACPI_CAST_PTR(union aml_resource,
0201                                 aml), NULL);
0202         if (ACPI_FAILURE(status)) {
0203             return_ACPI_STATUS(status);
0204         }
0205 
0206         /* Check for end-of-list, normal exit */
0207 
0208         if (resource->type == ACPI_RESOURCE_TYPE_END_TAG) {
0209 
0210             /* An End Tag indicates the end of the input Resource Template */
0211 
0212             return_ACPI_STATUS(AE_OK);
0213         }
0214 
0215         /*
0216          * Extract the total length of the new descriptor and set the
0217          * Aml to point to the next (output) resource descriptor
0218          */
0219         aml += acpi_ut_get_descriptor_length(aml);
0220 
0221         /* Point to the next input resource descriptor */
0222 
0223         resource = ACPI_NEXT_RESOURCE(resource);
0224     }
0225 
0226     /* Completed buffer, but did not find an end_tag resource descriptor */
0227 
0228     return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
0229 }