Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
0002 /*******************************************************************************
0003  *
0004  * Module Name: rscreate - Create resource lists/tables
0005  *
0006  ******************************************************************************/
0007 
0008 #include <acpi/acpi.h>
0009 #include "accommon.h"
0010 #include "acresrc.h"
0011 #include "acnamesp.h"
0012 
0013 #define _COMPONENT          ACPI_RESOURCES
0014 ACPI_MODULE_NAME("rscreate")
0015 
0016 /*******************************************************************************
0017  *
0018  * FUNCTION:    acpi_buffer_to_resource
0019  *
0020  * PARAMETERS:  aml_buffer          - Pointer to the resource byte stream
0021  *              aml_buffer_length   - Length of the aml_buffer
0022  *              resource_ptr        - Where the converted resource is returned
0023  *
0024  * RETURN:      Status
0025  *
0026  * DESCRIPTION: Convert a raw AML buffer to a resource list
0027  *
0028  ******************************************************************************/
0029 acpi_status
0030 acpi_buffer_to_resource(u8 *aml_buffer,
0031             u16 aml_buffer_length,
0032             struct acpi_resource **resource_ptr)
0033 {
0034     acpi_status status;
0035     acpi_size list_size_needed;
0036     void *resource;
0037     void *current_resource_ptr;
0038 
0039     ACPI_FUNCTION_TRACE(acpi_buffer_to_resource);
0040 
0041     /*
0042      * Note: we allow AE_AML_NO_RESOURCE_END_TAG, since an end tag
0043      * is not required here.
0044      */
0045 
0046     /* Get the required length for the converted resource */
0047 
0048     status =
0049         acpi_rs_get_list_length(aml_buffer, aml_buffer_length,
0050                     &list_size_needed);
0051     if (status == AE_AML_NO_RESOURCE_END_TAG) {
0052         status = AE_OK;
0053     }
0054     if (ACPI_FAILURE(status)) {
0055         return_ACPI_STATUS(status);
0056     }
0057 
0058     /* Allocate a buffer for the converted resource */
0059 
0060     resource = ACPI_ALLOCATE_ZEROED(list_size_needed);
0061     current_resource_ptr = resource;
0062     if (!resource) {
0063         return_ACPI_STATUS(AE_NO_MEMORY);
0064     }
0065 
0066     /* Perform the AML-to-Resource conversion */
0067 
0068     status = acpi_ut_walk_aml_resources(NULL, aml_buffer, aml_buffer_length,
0069                         acpi_rs_convert_aml_to_resources,
0070                         &current_resource_ptr);
0071     if (status == AE_AML_NO_RESOURCE_END_TAG) {
0072         status = AE_OK;
0073     }
0074     if (ACPI_FAILURE(status)) {
0075         ACPI_FREE(resource);
0076     } else {
0077         *resource_ptr = resource;
0078     }
0079 
0080     return_ACPI_STATUS(status);
0081 }
0082 
0083 ACPI_EXPORT_SYMBOL(acpi_buffer_to_resource)
0084 
0085 /*******************************************************************************
0086  *
0087  * FUNCTION:    acpi_rs_create_resource_list
0088  *
0089  * PARAMETERS:  aml_buffer          - Pointer to the resource byte stream
0090  *              output_buffer       - Pointer to the user's buffer
0091  *
0092  * RETURN:      Status: AE_OK if okay, else a valid acpi_status code
0093  *              If output_buffer is not large enough, output_buffer_length
0094  *              indicates how large output_buffer should be, else it
0095  *              indicates how may u8 elements of output_buffer are valid.
0096  *
0097  * DESCRIPTION: Takes the byte stream returned from a _CRS, _PRS control method
0098  *              execution and parses the stream to create a linked list
0099  *              of device resources.
0100  *
0101  ******************************************************************************/
0102 acpi_status
0103 acpi_rs_create_resource_list(union acpi_operand_object *aml_buffer,
0104                  struct acpi_buffer *output_buffer)
0105 {
0106 
0107     acpi_status status;
0108     u8 *aml_start;
0109     acpi_size list_size_needed = 0;
0110     u32 aml_buffer_length;
0111     void *resource;
0112 
0113     ACPI_FUNCTION_TRACE(rs_create_resource_list);
0114 
0115     ACPI_DEBUG_PRINT((ACPI_DB_INFO, "AmlBuffer = %p\n", aml_buffer));
0116 
0117     /* Params already validated, so we don't re-validate here */
0118 
0119     aml_buffer_length = aml_buffer->buffer.length;
0120     aml_start = aml_buffer->buffer.pointer;
0121 
0122     /*
0123      * Pass the aml_buffer into a module that can calculate
0124      * the buffer size needed for the linked list
0125      */
0126     status = acpi_rs_get_list_length(aml_start, aml_buffer_length,
0127                      &list_size_needed);
0128 
0129     ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Status=%X ListSizeNeeded=%X\n",
0130               status, (u32) list_size_needed));
0131     if (ACPI_FAILURE(status)) {
0132         return_ACPI_STATUS(status);
0133     }
0134 
0135     /* Validate/Allocate/Clear caller buffer */
0136 
0137     status = acpi_ut_initialize_buffer(output_buffer, list_size_needed);
0138     if (ACPI_FAILURE(status)) {
0139         return_ACPI_STATUS(status);
0140     }
0141 
0142     /* Do the conversion */
0143 
0144     resource = output_buffer->pointer;
0145     status = acpi_ut_walk_aml_resources(NULL, aml_start, aml_buffer_length,
0146                         acpi_rs_convert_aml_to_resources,
0147                         &resource);
0148     if (ACPI_FAILURE(status)) {
0149         return_ACPI_STATUS(status);
0150     }
0151 
0152     ACPI_DEBUG_PRINT((ACPI_DB_INFO, "OutputBuffer %p Length %X\n",
0153               output_buffer->pointer, (u32) output_buffer->length));
0154     return_ACPI_STATUS(AE_OK);
0155 }
0156 
0157 /*******************************************************************************
0158  *
0159  * FUNCTION:    acpi_rs_create_pci_routing_table
0160  *
0161  * PARAMETERS:  package_object          - Pointer to a package containing one
0162  *                                        of more ACPI_OPERAND_OBJECTs
0163  *              output_buffer           - Pointer to the user's buffer
0164  *
0165  * RETURN:      Status  AE_OK if okay, else a valid acpi_status code.
0166  *              If the output_buffer is too small, the error will be
0167  *              AE_BUFFER_OVERFLOW and output_buffer->Length will point
0168  *              to the size buffer needed.
0169  *
0170  * DESCRIPTION: Takes the union acpi_operand_object package and creates a
0171  *              linked list of PCI interrupt descriptions
0172  *
0173  * NOTE: It is the caller's responsibility to ensure that the start of the
0174  * output buffer is aligned properly (if necessary).
0175  *
0176  ******************************************************************************/
0177 
0178 acpi_status
0179 acpi_rs_create_pci_routing_table(union acpi_operand_object *package_object,
0180                  struct acpi_buffer *output_buffer)
0181 {
0182     u8 *buffer;
0183     union acpi_operand_object **top_object_list;
0184     union acpi_operand_object **sub_object_list;
0185     union acpi_operand_object *obj_desc;
0186     acpi_size buffer_size_needed = 0;
0187     u32 number_of_elements;
0188     u32 index;
0189     struct acpi_pci_routing_table *user_prt;
0190     struct acpi_namespace_node *node;
0191     acpi_status status;
0192     struct acpi_buffer path_buffer;
0193 
0194     ACPI_FUNCTION_TRACE(rs_create_pci_routing_table);
0195 
0196     /* Params already validated, so we don't re-validate here */
0197 
0198     /* Get the required buffer length */
0199 
0200     status =
0201         acpi_rs_get_pci_routing_table_length(package_object,
0202                          &buffer_size_needed);
0203     if (ACPI_FAILURE(status)) {
0204         return_ACPI_STATUS(status);
0205     }
0206 
0207     ACPI_DEBUG_PRINT((ACPI_DB_INFO, "BufferSizeNeeded = %X\n",
0208               (u32) buffer_size_needed));
0209 
0210     /* Validate/Allocate/Clear caller buffer */
0211 
0212     status = acpi_ut_initialize_buffer(output_buffer, buffer_size_needed);
0213     if (ACPI_FAILURE(status)) {
0214         return_ACPI_STATUS(status);
0215     }
0216 
0217     /*
0218      * Loop through the ACPI_INTERNAL_OBJECTS - Each object should be a
0219      * package that in turn contains an u64 Address, a u8 Pin,
0220      * a Name, and a u8 source_index.
0221      */
0222     top_object_list = package_object->package.elements;
0223     number_of_elements = package_object->package.count;
0224     buffer = output_buffer->pointer;
0225     user_prt = ACPI_CAST_PTR(struct acpi_pci_routing_table, buffer);
0226 
0227     for (index = 0; index < number_of_elements; index++) {
0228 
0229         /*
0230          * Point user_prt past this current structure
0231          *
0232          * NOTE: On the first iteration, user_prt->Length will
0233          * be zero because we cleared the return buffer earlier
0234          */
0235         buffer += user_prt->length;
0236         user_prt = ACPI_CAST_PTR(struct acpi_pci_routing_table, buffer);
0237 
0238         /*
0239          * Fill in the Length field with the information we have at this
0240          * point. The minus four is to subtract the size of the u8
0241          * Source[4] member because it is added below.
0242          */
0243         user_prt->length = (sizeof(struct acpi_pci_routing_table) - 4);
0244 
0245         /* Each subpackage must be of length 4 */
0246 
0247         if ((*top_object_list)->package.count != 4) {
0248             ACPI_ERROR((AE_INFO,
0249                     "(PRT[%u]) Need package of length 4, found length %u",
0250                     index, (*top_object_list)->package.count));
0251             return_ACPI_STATUS(AE_AML_PACKAGE_LIMIT);
0252         }
0253 
0254         /*
0255          * Dereference the subpackage.
0256          * The sub_object_list will now point to an array of the four IRQ
0257          * elements: [Address, Pin, Source, source_index]
0258          */
0259         sub_object_list = (*top_object_list)->package.elements;
0260 
0261         /* 1) First subobject: Dereference the PRT.Address */
0262 
0263         obj_desc = sub_object_list[0];
0264         if (!obj_desc || obj_desc->common.type != ACPI_TYPE_INTEGER) {
0265             ACPI_ERROR((AE_INFO,
0266                     "(PRT[%u].Address) Need Integer, found %s",
0267                     index,
0268                     acpi_ut_get_object_type_name(obj_desc)));
0269             return_ACPI_STATUS(AE_BAD_DATA);
0270         }
0271 
0272         user_prt->address = obj_desc->integer.value;
0273 
0274         /* 2) Second subobject: Dereference the PRT.Pin */
0275 
0276         obj_desc = sub_object_list[1];
0277         if (!obj_desc || obj_desc->common.type != ACPI_TYPE_INTEGER) {
0278             ACPI_ERROR((AE_INFO,
0279                     "(PRT[%u].Pin) Need Integer, found %s",
0280                     index,
0281                     acpi_ut_get_object_type_name(obj_desc)));
0282             return_ACPI_STATUS(AE_BAD_DATA);
0283         }
0284 
0285         user_prt->pin = (u32) obj_desc->integer.value;
0286 
0287         /*
0288          * 3) Third subobject: Dereference the PRT.source_name
0289          * The name may be unresolved (slack mode), so allow a null object
0290          */
0291         obj_desc = sub_object_list[2];
0292         if (obj_desc) {
0293             switch (obj_desc->common.type) {
0294             case ACPI_TYPE_LOCAL_REFERENCE:
0295 
0296                 if (obj_desc->reference.class !=
0297                     ACPI_REFCLASS_NAME) {
0298                     ACPI_ERROR((AE_INFO,
0299                             "(PRT[%u].Source) Need name, found Reference Class 0x%X",
0300                             index,
0301                             obj_desc->reference.class));
0302                     return_ACPI_STATUS(AE_BAD_DATA);
0303                 }
0304 
0305                 node = obj_desc->reference.node;
0306 
0307                 /* Use *remaining* length of the buffer as max for pathname */
0308 
0309                 path_buffer.length = output_buffer->length -
0310                     (u32) ((u8 *) user_prt->source -
0311                        (u8 *) output_buffer->pointer);
0312                 path_buffer.pointer = user_prt->source;
0313 
0314                 status = acpi_ns_handle_to_pathname((acpi_handle)node, &path_buffer, FALSE);
0315                 if (ACPI_FAILURE(status)) {
0316                     return_ACPI_STATUS(status);
0317                 }
0318 
0319                 /* +1 to include null terminator */
0320 
0321                 user_prt->length +=
0322                     (u32)strlen(user_prt->source) + 1;
0323                 break;
0324 
0325             case ACPI_TYPE_STRING:
0326 
0327                 strcpy(user_prt->source,
0328                        obj_desc->string.pointer);
0329 
0330                 /*
0331                  * Add to the Length field the length of the string
0332                  * (add 1 for terminator)
0333                  */
0334                 user_prt->length += obj_desc->string.length + 1;
0335                 break;
0336 
0337             case ACPI_TYPE_INTEGER:
0338                 /*
0339                  * If this is a number, then the Source Name is NULL, since
0340                  * the entire buffer was zeroed out, we can leave this alone.
0341                  *
0342                  * Add to the Length field the length of the u32 NULL
0343                  */
0344                 user_prt->length += sizeof(u32);
0345                 break;
0346 
0347             default:
0348 
0349                 ACPI_ERROR((AE_INFO,
0350                         "(PRT[%u].Source) Need Ref/String/Integer, found %s",
0351                         index,
0352                         acpi_ut_get_object_type_name
0353                         (obj_desc)));
0354                 return_ACPI_STATUS(AE_BAD_DATA);
0355             }
0356         }
0357 
0358         /* Now align the current length */
0359 
0360         user_prt->length =
0361             (u32) ACPI_ROUND_UP_TO_64BIT(user_prt->length);
0362 
0363         /* 4) Fourth subobject: Dereference the PRT.source_index */
0364 
0365         obj_desc = sub_object_list[3];
0366         if (!obj_desc || obj_desc->common.type != ACPI_TYPE_INTEGER) {
0367             ACPI_ERROR((AE_INFO,
0368                     "(PRT[%u].SourceIndex) Need Integer, found %s",
0369                     index,
0370                     acpi_ut_get_object_type_name(obj_desc)));
0371             return_ACPI_STATUS(AE_BAD_DATA);
0372         }
0373 
0374         user_prt->source_index = (u32) obj_desc->integer.value;
0375 
0376         /* Point to the next union acpi_operand_object in the top level package */
0377 
0378         top_object_list++;
0379     }
0380 
0381     ACPI_DEBUG_PRINT((ACPI_DB_INFO, "OutputBuffer %p Length %X\n",
0382               output_buffer->pointer, (u32) output_buffer->length));
0383     return_ACPI_STATUS(AE_OK);
0384 }
0385 
0386 /*******************************************************************************
0387  *
0388  * FUNCTION:    acpi_rs_create_aml_resources
0389  *
0390  * PARAMETERS:  resource_list           - Pointer to the resource list buffer
0391  *              output_buffer           - Where the AML buffer is returned
0392  *
0393  * RETURN:      Status  AE_OK if okay, else a valid acpi_status code.
0394  *              If the output_buffer is too small, the error will be
0395  *              AE_BUFFER_OVERFLOW and output_buffer->Length will point
0396  *              to the size buffer needed.
0397  *
0398  * DESCRIPTION: Converts a list of device resources to an AML bytestream
0399  *              to be used as input for the _SRS control method.
0400  *
0401  ******************************************************************************/
0402 
0403 acpi_status
0404 acpi_rs_create_aml_resources(struct acpi_buffer *resource_list,
0405                  struct acpi_buffer *output_buffer)
0406 {
0407     acpi_status status;
0408     acpi_size aml_size_needed = 0;
0409 
0410     ACPI_FUNCTION_TRACE(rs_create_aml_resources);
0411 
0412     /* Params already validated, no need to re-validate here */
0413 
0414     ACPI_DEBUG_PRINT((ACPI_DB_INFO, "ResourceList Buffer = %p\n",
0415               resource_list->pointer));
0416 
0417     /* Get the buffer size needed for the AML byte stream */
0418 
0419     status =
0420         acpi_rs_get_aml_length(resource_list->pointer,
0421                    resource_list->length, &aml_size_needed);
0422 
0423     ACPI_DEBUG_PRINT((ACPI_DB_INFO, "AmlSizeNeeded=%X, %s\n",
0424               (u32)aml_size_needed, acpi_format_exception(status)));
0425     if (ACPI_FAILURE(status)) {
0426         return_ACPI_STATUS(status);
0427     }
0428 
0429     /* Validate/Allocate/Clear caller buffer */
0430 
0431     status = acpi_ut_initialize_buffer(output_buffer, aml_size_needed);
0432     if (ACPI_FAILURE(status)) {
0433         return_ACPI_STATUS(status);
0434     }
0435 
0436     /* Do the conversion */
0437 
0438     status = acpi_rs_convert_resources_to_aml(resource_list->pointer,
0439                           aml_size_needed,
0440                           output_buffer->pointer);
0441     if (ACPI_FAILURE(status)) {
0442         return_ACPI_STATUS(status);
0443     }
0444 
0445     ACPI_DEBUG_PRINT((ACPI_DB_INFO, "OutputBuffer %p Length %X\n",
0446               output_buffer->pointer, (u32) output_buffer->length));
0447     return_ACPI_STATUS(AE_OK);
0448 }