c5e828f4ed0bdf09d523155fbc6c1c1183091791
[firefly-linux-kernel-4.4.55.git] / drivers / acpi / acpica / nsrepair.c
1 /******************************************************************************
2  *
3  * Module Name: nsrepair - Repair for objects returned by predefined methods
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2013, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43
44 #include <acpi/acpi.h>
45 #include "accommon.h"
46 #include "acnamesp.h"
47 #include "acinterp.h"
48 #include "acpredef.h"
49 #include "amlresrc.h"
50
51 #define _COMPONENT          ACPI_NAMESPACE
52 ACPI_MODULE_NAME("nsrepair")
53
54 /*******************************************************************************
55  *
56  * This module attempts to repair or convert objects returned by the
57  * predefined methods to an object type that is expected, as per the ACPI
58  * specification. The need for this code is dictated by the many machines that
59  * return incorrect types for the standard predefined methods. Performing these
60  * conversions here, in one place, eliminates the need for individual ACPI
61  * device drivers to do the same. Note: Most of these conversions are different
62  * than the internal object conversion routines used for implicit object
63  * conversion.
64  *
65  * The following conversions can be performed as necessary:
66  *
67  * Integer -> String
68  * Integer -> Buffer
69  * String  -> Integer
70  * String  -> Buffer
71  * Buffer  -> Integer
72  * Buffer  -> String
73  * Buffer  -> Package of Integers
74  * Package -> Package of one Package
75  *
76  * Additional conversions that are available:
77  *  Convert a null return or zero return value to an end_tag descriptor
78  *  Convert an ASCII string to a Unicode buffer
79  *
80  * An incorrect standalone object is wrapped with required outer package
81  *
82  * Additional possible repairs:
83  * Required package elements that are NULL replaced by Integer/String/Buffer
84  *
85  ******************************************************************************/
86 /* Local prototypes */
87 static const struct acpi_simple_repair_info *acpi_ns_match_simple_repair(struct
88                                                                          acpi_namespace_node
89                                                                          *node,
90                                                                          u32
91                                                                          return_btype,
92                                                                          u32
93                                                                          package_index);
94
95 /*
96  * Special but simple repairs for some names.
97  *
98  * 2nd argument: Unexpected types that can be repaired
99  */
100 static const struct acpi_simple_repair_info acpi_object_repair_info[] = {
101         {{0, 0, 0, 0}, 0, 0, NULL}      /* Table terminator */
102 };
103
104 /*******************************************************************************
105  *
106  * FUNCTION:    acpi_ns_simple_repair
107  *
108  * PARAMETERS:  data                - Pointer to validation data structure
109  *              expected_btypes     - Object types expected
110  *              package_index       - Index of object within parent package (if
111  *                                    applicable - ACPI_NOT_PACKAGE_ELEMENT
112  *                                    otherwise)
113  *              return_object_ptr   - Pointer to the object returned from the
114  *                                    evaluation of a method or object
115  *
116  * RETURN:      Status. AE_OK if repair was successful.
117  *
118  * DESCRIPTION: Attempt to repair/convert a return object of a type that was
119  *              not expected.
120  *
121  ******************************************************************************/
122
123 acpi_status
124 acpi_ns_simple_repair(struct acpi_predefined_data *data,
125                       u32 expected_btypes,
126                       u32 package_index,
127                       union acpi_operand_object **return_object_ptr)
128 {
129         union acpi_operand_object *return_object = *return_object_ptr;
130         union acpi_operand_object *new_object = NULL;
131         acpi_status status;
132         const struct acpi_simple_repair_info *predefined;
133
134         ACPI_FUNCTION_NAME(ns_simple_repair);
135
136         /*
137          * Special repairs for certain names that are in the repair table.
138          * Check if this name is in the list of repairable names.
139          */
140         predefined = acpi_ns_match_simple_repair(data->node,
141                                                  data->return_btype,
142                                                  package_index);
143         if (predefined) {
144                 if (!return_object) {
145                         ACPI_WARN_PREDEFINED((AE_INFO, data->pathname,
146                                               ACPI_WARN_ALWAYS,
147                                               "Missing expected return value"));
148                 }
149
150                 status =
151                     predefined->object_converter(return_object, &new_object);
152                 if (ACPI_FAILURE(status)) {
153
154                         /* A fatal error occurred during a conversion */
155
156                         ACPI_EXCEPTION((AE_INFO, status,
157                                         "During return object analysis"));
158                         return (status);
159                 }
160                 if (new_object) {
161                         goto object_repaired;
162                 }
163         }
164
165         /*
166          * Do not perform simple object repair unless the return type is not
167          * expected.
168          */
169         if (data->return_btype & expected_btypes) {
170                 return (AE_OK);
171         }
172
173         /*
174          * At this point, we know that the type of the returned object was not
175          * one of the expected types for this predefined name. Attempt to
176          * repair the object by converting it to one of the expected object
177          * types for this predefined name.
178          */
179
180         /*
181          * If there is no return value, check if we require a return value for
182          * this predefined name. Either one return value is expected, or none,
183          * for both methods and other objects.
184          *
185          * Exit now if there is no return object. Warning if one was expected.
186          */
187         if (!return_object) {
188                 if (expected_btypes && (!(expected_btypes & ACPI_RTYPE_NONE))) {
189                         ACPI_WARN_PREDEFINED((AE_INFO, data->pathname,
190                                               ACPI_WARN_ALWAYS,
191                                               "Missing expected return value"));
192
193                         return (AE_AML_NO_RETURN_VALUE);
194                 }
195         }
196
197         if (expected_btypes & ACPI_RTYPE_INTEGER) {
198                 status = acpi_ns_convert_to_integer(return_object, &new_object);
199                 if (ACPI_SUCCESS(status)) {
200                         goto object_repaired;
201                 }
202         }
203         if (expected_btypes & ACPI_RTYPE_STRING) {
204                 status = acpi_ns_convert_to_string(return_object, &new_object);
205                 if (ACPI_SUCCESS(status)) {
206                         goto object_repaired;
207                 }
208         }
209         if (expected_btypes & ACPI_RTYPE_BUFFER) {
210                 status = acpi_ns_convert_to_buffer(return_object, &new_object);
211                 if (ACPI_SUCCESS(status)) {
212                         goto object_repaired;
213                 }
214         }
215         if (expected_btypes & ACPI_RTYPE_PACKAGE) {
216                 /*
217                  * A package is expected. We will wrap the existing object with a
218                  * new package object. It is often the case that if a variable-length
219                  * package is required, but there is only a single object needed, the
220                  * BIOS will return that object instead of wrapping it with a Package
221                  * object. Note: after the wrapping, the package will be validated
222                  * for correct contents (expected object type or types).
223                  */
224                 status =
225                     acpi_ns_wrap_with_package(data, return_object, &new_object);
226                 if (ACPI_SUCCESS(status)) {
227                         /*
228                          * The original object just had its reference count
229                          * incremented for being inserted into the new package.
230                          */
231                         *return_object_ptr = new_object;        /* New Package object */
232                         data->flags |= ACPI_OBJECT_REPAIRED;
233                         return (AE_OK);
234                 }
235         }
236
237         /* We cannot repair this object */
238
239         return (AE_AML_OPERAND_TYPE);
240
241       object_repaired:
242
243         /* Object was successfully repaired */
244
245         if (package_index != ACPI_NOT_PACKAGE_ELEMENT) {
246                 /*
247                  * The original object is a package element. We need to
248                  * decrement the reference count of the original object,
249                  * for removing it from the package.
250                  *
251                  * However, if the original object was just wrapped with a
252                  * package object as part of the repair, we don't need to
253                  * change the reference count.
254                  */
255                 if (!(data->flags & ACPI_OBJECT_WRAPPED)) {
256                         new_object->common.reference_count =
257                             return_object->common.reference_count;
258
259                         if (return_object->common.reference_count > 1) {
260                                 return_object->common.reference_count--;
261                         }
262                 }
263
264                 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
265                                   "%s: Converted %s to expected %s at Package index %u\n",
266                                   data->pathname,
267                                   acpi_ut_get_object_type_name(return_object),
268                                   acpi_ut_get_object_type_name(new_object),
269                                   package_index));
270         } else {
271                 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
272                                   "%s: Converted %s to expected %s\n",
273                                   data->pathname,
274                                   acpi_ut_get_object_type_name(return_object),
275                                   acpi_ut_get_object_type_name(new_object)));
276         }
277
278         /* Delete old object, install the new return object */
279
280         acpi_ut_remove_reference(return_object);
281         *return_object_ptr = new_object;
282         data->flags |= ACPI_OBJECT_REPAIRED;
283         return (AE_OK);
284 }
285
286 /******************************************************************************
287  *
288  * FUNCTION:    acpi_ns_match_simple_repair
289  *
290  * PARAMETERS:  node                - Namespace node for the method/object
291  *              return_btype        - Object type that was returned
292  *              package_index       - Index of object within parent package (if
293  *                                    applicable - ACPI_NOT_PACKAGE_ELEMENT
294  *                                    otherwise)
295  *
296  * RETURN:      Pointer to entry in repair table. NULL indicates not found.
297  *
298  * DESCRIPTION: Check an object name against the repairable object list.
299  *
300  *****************************************************************************/
301
302 static const struct acpi_simple_repair_info *acpi_ns_match_simple_repair(struct
303                                                                          acpi_namespace_node
304                                                                          *node,
305                                                                          u32
306                                                                          return_btype,
307                                                                          u32
308                                                                          package_index)
309 {
310         const struct acpi_simple_repair_info *this_name;
311
312         /* Search info table for a repairable predefined method/object name */
313
314         this_name = acpi_object_repair_info;
315         while (this_name->object_converter) {
316                 if (ACPI_COMPARE_NAME(node->name.ascii, this_name->name)) {
317
318                         /* Check if we can actually repair this name/type combination */
319
320                         if ((return_btype & this_name->unexpected_btypes) &&
321                             (package_index == this_name->package_index)) {
322                                 return (this_name);
323                         }
324
325                         return (NULL);
326                 }
327                 this_name++;
328         }
329
330         return (NULL);          /* Name was not found in the repair table */
331 }
332
333 /*******************************************************************************
334  *
335  * FUNCTION:    acpi_ns_repair_null_element
336  *
337  * PARAMETERS:  data                - Pointer to validation data structure
338  *              expected_btypes     - Object types expected
339  *              package_index       - Index of object within parent package (if
340  *                                    applicable - ACPI_NOT_PACKAGE_ELEMENT
341  *                                    otherwise)
342  *              return_object_ptr   - Pointer to the object returned from the
343  *                                    evaluation of a method or object
344  *
345  * RETURN:      Status. AE_OK if repair was successful.
346  *
347  * DESCRIPTION: Attempt to repair a NULL element of a returned Package object.
348  *
349  ******************************************************************************/
350
351 acpi_status
352 acpi_ns_repair_null_element(struct acpi_predefined_data *data,
353                             u32 expected_btypes,
354                             u32 package_index,
355                             union acpi_operand_object **return_object_ptr)
356 {
357         union acpi_operand_object *return_object = *return_object_ptr;
358         union acpi_operand_object *new_object;
359
360         ACPI_FUNCTION_NAME(ns_repair_null_element);
361
362         /* No repair needed if return object is non-NULL */
363
364         if (return_object) {
365                 return (AE_OK);
366         }
367
368         /*
369          * Attempt to repair a NULL element of a Package object. This applies to
370          * predefined names that return a fixed-length package and each element
371          * is required. It does not apply to variable-length packages where NULL
372          * elements are allowed, especially at the end of the package.
373          */
374         if (expected_btypes & ACPI_RTYPE_INTEGER) {
375
376                 /* Need an integer - create a zero-value integer */
377
378                 new_object = acpi_ut_create_integer_object((u64)0);
379         } else if (expected_btypes & ACPI_RTYPE_STRING) {
380
381                 /* Need a string - create a NULL string */
382
383                 new_object = acpi_ut_create_string_object(0);
384         } else if (expected_btypes & ACPI_RTYPE_BUFFER) {
385
386                 /* Need a buffer - create a zero-length buffer */
387
388                 new_object = acpi_ut_create_buffer_object(0);
389         } else {
390                 /* Error for all other expected types */
391
392                 return (AE_AML_OPERAND_TYPE);
393         }
394
395         if (!new_object) {
396                 return (AE_NO_MEMORY);
397         }
398
399         /* Set the reference count according to the parent Package object */
400
401         new_object->common.reference_count =
402             data->parent_package->common.reference_count;
403
404         ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
405                           "%s: Converted NULL package element to expected %s at index %u\n",
406                           data->pathname,
407                           acpi_ut_get_object_type_name(new_object),
408                           package_index));
409
410         *return_object_ptr = new_object;
411         data->flags |= ACPI_OBJECT_REPAIRED;
412         return (AE_OK);
413 }
414
415 /******************************************************************************
416  *
417  * FUNCTION:    acpi_ns_remove_null_elements
418  *
419  * PARAMETERS:  data                - Pointer to validation data structure
420  *              package_type        - An acpi_return_package_types value
421  *              obj_desc            - A Package object
422  *
423  * RETURN:      None.
424  *
425  * DESCRIPTION: Remove all NULL package elements from packages that contain
426  *              a variable number of sub-packages. For these types of
427  *              packages, NULL elements can be safely removed.
428  *
429  *****************************************************************************/
430
431 void
432 acpi_ns_remove_null_elements(struct acpi_predefined_data *data,
433                              u8 package_type,
434                              union acpi_operand_object *obj_desc)
435 {
436         union acpi_operand_object **source;
437         union acpi_operand_object **dest;
438         u32 count;
439         u32 new_count;
440         u32 i;
441
442         ACPI_FUNCTION_NAME(ns_remove_null_elements);
443
444         /*
445          * We can safely remove all NULL elements from these package types:
446          * PTYPE1_VAR packages contain a variable number of simple data types.
447          * PTYPE2 packages contain a variable number of sub-packages.
448          */
449         switch (package_type) {
450         case ACPI_PTYPE1_VAR:
451         case ACPI_PTYPE2:
452         case ACPI_PTYPE2_COUNT:
453         case ACPI_PTYPE2_PKG_COUNT:
454         case ACPI_PTYPE2_FIXED:
455         case ACPI_PTYPE2_MIN:
456         case ACPI_PTYPE2_REV_FIXED:
457         case ACPI_PTYPE2_FIX_VAR:
458                 break;
459
460         default:
461         case ACPI_PTYPE1_FIXED:
462         case ACPI_PTYPE1_OPTION:
463                 return;
464         }
465
466         count = obj_desc->package.count;
467         new_count = count;
468
469         source = obj_desc->package.elements;
470         dest = source;
471
472         /* Examine all elements of the package object, remove nulls */
473
474         for (i = 0; i < count; i++) {
475                 if (!*source) {
476                         new_count--;
477                 } else {
478                         *dest = *source;
479                         dest++;
480                 }
481                 source++;
482         }
483
484         /* Update parent package if any null elements were removed */
485
486         if (new_count < count) {
487                 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
488                                   "%s: Found and removed %u NULL elements\n",
489                                   data->pathname, (count - new_count)));
490
491                 /* NULL terminate list and update the package count */
492
493                 *dest = NULL;
494                 obj_desc->package.count = new_count;
495         }
496 }
497
498 /*******************************************************************************
499  *
500  * FUNCTION:    acpi_ns_wrap_with_package
501  *
502  * PARAMETERS:  data                - Pointer to validation data structure
503  *              original_object     - Pointer to the object to repair.
504  *              obj_desc_ptr        - The new package object is returned here
505  *
506  * RETURN:      Status, new object in *obj_desc_ptr
507  *
508  * DESCRIPTION: Repair a common problem with objects that are defined to
509  *              return a variable-length Package of sub-objects. If there is
510  *              only one sub-object, some BIOS code mistakenly simply declares
511  *              the single object instead of a Package with one sub-object.
512  *              This function attempts to repair this error by wrapping a
513  *              Package object around the original object, creating the
514  *              correct and expected Package with one sub-object.
515  *
516  *              Names that can be repaired in this manner include:
517  *              _ALR, _CSD, _HPX, _MLS, _PLD, _PRT, _PSS, _TRT, _TSS,
518  *              _BCL, _DOD, _FIX, _Sx
519  *
520  ******************************************************************************/
521
522 acpi_status
523 acpi_ns_wrap_with_package(struct acpi_predefined_data *data,
524                           union acpi_operand_object *original_object,
525                           union acpi_operand_object **obj_desc_ptr)
526 {
527         union acpi_operand_object *pkg_obj_desc;
528
529         ACPI_FUNCTION_NAME(ns_wrap_with_package);
530
531         /*
532          * Create the new outer package and populate it. The new package will
533          * have a single element, the lone sub-object.
534          */
535         pkg_obj_desc = acpi_ut_create_package_object(1);
536         if (!pkg_obj_desc) {
537                 return (AE_NO_MEMORY);
538         }
539
540         pkg_obj_desc->package.elements[0] = original_object;
541
542         ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
543                           "%s: Wrapped %s with expected Package object\n",
544                           data->pathname,
545                           acpi_ut_get_object_type_name(original_object)));
546
547         /* Return the new object in the object pointer */
548
549         *obj_desc_ptr = pkg_obj_desc;
550         data->flags |= ACPI_OBJECT_REPAIRED | ACPI_OBJECT_WRAPPED;
551         return (AE_OK);
552 }