ACPICA: Add repair for bad _FDE/_GTM buffers
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / acpi / acpica / nsrepair2.c
1 /******************************************************************************
2  *
3  * Module Name: nsrepair2 - Repair for objects returned by specific
4  *                          predefined methods
5  *
6  *****************************************************************************/
7
8 /*
9  * Copyright (C) 2000 - 2009, Intel Corp.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions, and the following disclaimer,
17  *    without modification.
18  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19  *    substantially similar to the "NO WARRANTY" disclaimer below
20  *    ("Disclaimer") and any redistribution must be conditioned upon
21  *    including a substantially similar Disclaimer requirement for further
22  *    binary redistribution.
23  * 3. Neither the names of the above-listed copyright holders nor the names
24  *    of any contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * Alternatively, this software may be distributed under the terms of the
28  * GNU General Public License ("GPL") version 2 as published by the Free
29  * Software Foundation.
30  *
31  * NO WARRANTY
32  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42  * POSSIBILITY OF SUCH DAMAGES.
43  */
44
45 #include <acpi/acpi.h>
46 #include "accommon.h"
47 #include "acnamesp.h"
48
49 #define _COMPONENT          ACPI_NAMESPACE
50 ACPI_MODULE_NAME("nsrepair2")
51
52 /*
53  * Information structure and handler for ACPI predefined names that can
54  * be repaired on a per-name basis.
55  */
56 typedef
57 acpi_status(*acpi_repair_function) (struct acpi_predefined_data *data,
58                                     union acpi_operand_object **return_object_ptr);
59
60 typedef struct acpi_repair_info {
61         char name[ACPI_NAME_SIZE];
62         acpi_repair_function repair_function;
63
64 } acpi_repair_info;
65
66 /* Local prototypes */
67
68 static const struct acpi_repair_info *acpi_ns_match_repairable_name(struct
69                                                                     acpi_namespace_node
70                                                                     *node);
71
72 static acpi_status
73 acpi_ns_repair_ALR(struct acpi_predefined_data *data,
74                    union acpi_operand_object **return_object_ptr);
75
76 static acpi_status
77 acpi_ns_repair_FDE(struct acpi_predefined_data *data,
78                    union acpi_operand_object **return_object_ptr);
79
80 static acpi_status
81 acpi_ns_repair_PSS(struct acpi_predefined_data *data,
82                    union acpi_operand_object **return_object_ptr);
83
84 static acpi_status
85 acpi_ns_repair_TSS(struct acpi_predefined_data *data,
86                    union acpi_operand_object **return_object_ptr);
87
88 static acpi_status
89 acpi_ns_check_sorted_list(struct acpi_predefined_data *data,
90                           union acpi_operand_object *return_object,
91                           u32 expected_count,
92                           u32 sort_index,
93                           u8 sort_direction, char *sort_key_name);
94
95 static acpi_status
96 acpi_ns_remove_null_elements(union acpi_operand_object *package);
97
98 static acpi_status
99 acpi_ns_sort_list(union acpi_operand_object **elements,
100                   u32 count, u32 index, u8 sort_direction);
101
102 /* Values for sort_direction above */
103
104 #define ACPI_SORT_ASCENDING     0
105 #define ACPI_SORT_DESCENDING    1
106
107 /*
108  * This table contains the names of the predefined methods for which we can
109  * perform more complex repairs.
110  *
111  * As necessary:
112  *
113  * _ALR: Sort the list ascending by ambient_illuminance
114  * _FDE: Convert a Package or Buffer of BYTEs to a Buffer of DWORDs
115  * _GTM: Convert a Package or Buffer of BYTEs to a Buffer of DWORDs
116  * _PSS: Sort the list descending by Power
117  * _TSS: Sort the list descending by Power
118  */
119 static const struct acpi_repair_info acpi_ns_repairable_names[] = {
120         {"_ALR", acpi_ns_repair_ALR},
121         {"_FDE", acpi_ns_repair_FDE},
122         {"_GTM", acpi_ns_repair_FDE},   /* _GTM has same repair as _FDE */
123         {"_PSS", acpi_ns_repair_PSS},
124         {"_TSS", acpi_ns_repair_TSS},
125         {{0, 0, 0, 0}, NULL}    /* Table terminator */
126 };
127
128 #define ACPI_FDE_FIELD_COUNT        5
129 #define ACPI_FDE_BYTE_BUFFER_SIZE   5
130 #define ACPI_FDE_DWORD_BUFFER_SIZE  (ACPI_FDE_FIELD_COUNT * sizeof (u32))
131
132 /******************************************************************************
133  *
134  * FUNCTION:    acpi_ns_complex_repairs
135  *
136  * PARAMETERS:  Data                - Pointer to validation data structure
137  *              Node                - Namespace node for the method/object
138  *              validate_status     - Original status of earlier validation
139  *              return_object_ptr   - Pointer to the object returned from the
140  *                                    evaluation of a method or object
141  *
142  * RETURN:      Status. AE_OK if repair was successful. If name is not
143  *              matched, validate_status is returned.
144  *
145  * DESCRIPTION: Attempt to repair/convert a return object of a type that was
146  *              not expected.
147  *
148  *****************************************************************************/
149
150 acpi_status
151 acpi_ns_complex_repairs(struct acpi_predefined_data *data,
152                         struct acpi_namespace_node *node,
153                         acpi_status validate_status,
154                         union acpi_operand_object **return_object_ptr)
155 {
156         const struct acpi_repair_info *predefined;
157         acpi_status status;
158
159         /* Check if this name is in the list of repairable names */
160
161         predefined = acpi_ns_match_repairable_name(node);
162         if (!predefined) {
163                 return (validate_status);
164         }
165
166         status = predefined->repair_function(data, return_object_ptr);
167         return (status);
168 }
169
170 /******************************************************************************
171  *
172  * FUNCTION:    acpi_ns_match_repairable_name
173  *
174  * PARAMETERS:  Node                - Namespace node for the method/object
175  *
176  * RETURN:      Pointer to entry in repair table. NULL indicates not found.
177  *
178  * DESCRIPTION: Check an object name against the repairable object list.
179  *
180  *****************************************************************************/
181
182 static const struct acpi_repair_info *acpi_ns_match_repairable_name(struct
183                                                                     acpi_namespace_node
184                                                                     *node)
185 {
186         const struct acpi_repair_info *this_name;
187
188         /* Search info table for a repairable predefined method/object name */
189
190         this_name = acpi_ns_repairable_names;
191         while (this_name->repair_function) {
192                 if (ACPI_COMPARE_NAME(node->name.ascii, this_name->name)) {
193                         return (this_name);
194                 }
195                 this_name++;
196         }
197
198         return (NULL);          /* Not found */
199 }
200
201 /******************************************************************************
202  *
203  * FUNCTION:    acpi_ns_repair_ALR
204  *
205  * PARAMETERS:  Data                - Pointer to validation data structure
206  *              return_object_ptr   - Pointer to the object returned from the
207  *                                    evaluation of a method or object
208  *
209  * RETURN:      Status. AE_OK if object is OK or was repaired successfully
210  *
211  * DESCRIPTION: Repair for the _ALR object. If necessary, sort the object list
212  *              ascending by the ambient illuminance values.
213  *
214  *****************************************************************************/
215
216 static acpi_status
217 acpi_ns_repair_ALR(struct acpi_predefined_data *data,
218                    union acpi_operand_object **return_object_ptr)
219 {
220         union acpi_operand_object *return_object = *return_object_ptr;
221         acpi_status status;
222
223         status = acpi_ns_check_sorted_list(data, return_object, 2, 1,
224                                            ACPI_SORT_ASCENDING,
225                                            "AmbientIlluminance");
226
227         return (status);
228 }
229
230 /******************************************************************************
231  *
232  * FUNCTION:    acpi_ns_repair_FDE
233  *
234  * PARAMETERS:  Data                - Pointer to validation data structure
235  *              return_object_ptr   - Pointer to the object returned from the
236  *                                    evaluation of a method or object
237  *
238  * RETURN:      Status. AE_OK if object is OK or was repaired successfully
239  *
240  * DESCRIPTION: Repair for the _FDE and _GTM objects. The expected return
241  *              value is a Buffer of 5 DWORDs. This function repairs two
242  *              possible problems:
243  *              1) The return value is a Buffer of BYTEs, not DWORDs
244  *              2) The return value is a Package of Integer objects
245  *
246  *****************************************************************************/
247
248 static acpi_status
249 acpi_ns_repair_FDE(struct acpi_predefined_data *data,
250                    union acpi_operand_object **return_object_ptr)
251 {
252         union acpi_operand_object *return_object = *return_object_ptr;
253         union acpi_operand_object **elements;
254         union acpi_operand_object *buffer_object;
255         u8 *byte_buffer;
256         u32 *dword_buffer;
257         u32 count;
258         u32 i;
259
260         switch (return_object->common.type) {
261         case ACPI_TYPE_BUFFER:
262
263                 /* This is the expected type. Length should be (at least) 5 DWORDs */
264
265                 if (return_object->buffer.length >= ACPI_FDE_DWORD_BUFFER_SIZE) {
266                         return (AE_OK);
267                 }
268
269                 /* We can only repair if we have exactly 5 BYTEs */
270
271                 if (return_object->buffer.length != ACPI_FDE_BYTE_BUFFER_SIZE) {
272                         ACPI_WARN_PREDEFINED((AE_INFO, data->pathname,
273                                               data->node_flags,
274                                               "Incorrect return buffer length %u, expected %u",
275                                               return_object->buffer.length,
276                                               ACPI_FDE_DWORD_BUFFER_SIZE));
277
278                         return (AE_AML_OPERAND_TYPE);
279                 }
280
281                 /* Create the new (larger) buffer object */
282
283                 buffer_object =
284                     acpi_ut_create_buffer_object(ACPI_FDE_DWORD_BUFFER_SIZE);
285                 if (!buffer_object) {
286                         return (AE_NO_MEMORY);
287                 }
288
289                 /* Expand each byte to a DWORD */
290
291                 byte_buffer = return_object->buffer.pointer;
292                 dword_buffer =
293                     ACPI_CAST_PTR(u32, buffer_object->buffer.pointer);
294
295                 for (i = 0; i < ACPI_FDE_FIELD_COUNT; i++) {
296                         *dword_buffer = (u32) *byte_buffer;
297                         dword_buffer++;
298                         byte_buffer++;
299                 }
300
301                 ACPI_INFO_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
302                                       "Expanded Byte Buffer to expected DWord Buffer"));
303                 break;
304
305         case ACPI_TYPE_PACKAGE:
306
307                 /* All elements of the Package must be integers */
308
309                 elements = return_object->package.elements;
310                 count =
311                     ACPI_MIN(ACPI_FDE_FIELD_COUNT,
312                              return_object->package.count);
313
314                 for (i = 0; i < count; i++) {
315                         if ((!*elements) ||
316                             ((*elements)->common.type != ACPI_TYPE_INTEGER)) {
317                                 return (AE_AML_OPERAND_TYPE);
318                         }
319                         elements++;
320                 }
321
322                 /* Create the new buffer object to replace the Package */
323
324                 buffer_object =
325                     acpi_ut_create_buffer_object(ACPI_FDE_DWORD_BUFFER_SIZE);
326                 if (!buffer_object) {
327                         return (AE_NO_MEMORY);
328                 }
329
330                 /* Copy the package elements (integers) to the buffer */
331
332                 elements = return_object->package.elements;
333                 dword_buffer =
334                     ACPI_CAST_PTR(u32, buffer_object->buffer.pointer);
335
336                 for (i = 0; i < count; i++) {
337                         *dword_buffer = (u32) (*elements)->integer.value;
338                         dword_buffer++;
339                         elements++;
340                 }
341
342                 ACPI_INFO_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
343                                       "Converted Package to expected Buffer"));
344                 break;
345
346         default:
347                 return (AE_AML_OPERAND_TYPE);
348         }
349
350         /* Delete the original return object, return the new buffer object */
351
352         acpi_ut_remove_reference(return_object);
353         *return_object_ptr = buffer_object;
354
355         data->flags |= ACPI_OBJECT_REPAIRED;
356         return (AE_OK);
357 }
358
359 /******************************************************************************
360  *
361  * FUNCTION:    acpi_ns_repair_TSS
362  *
363  * PARAMETERS:  Data                - Pointer to validation data structure
364  *              return_object_ptr   - Pointer to the object returned from the
365  *                                    evaluation of a method or object
366  *
367  * RETURN:      Status. AE_OK if object is OK or was repaired successfully
368  *
369  * DESCRIPTION: Repair for the _TSS object. If necessary, sort the object list
370  *              descending by the power dissipation values.
371  *
372  *****************************************************************************/
373
374 static acpi_status
375 acpi_ns_repair_TSS(struct acpi_predefined_data *data,
376                    union acpi_operand_object **return_object_ptr)
377 {
378         union acpi_operand_object *return_object = *return_object_ptr;
379         acpi_status status;
380
381         status = acpi_ns_check_sorted_list(data, return_object, 5, 1,
382                                            ACPI_SORT_DESCENDING,
383                                            "PowerDissipation");
384
385         return (status);
386 }
387
388 /******************************************************************************
389  *
390  * FUNCTION:    acpi_ns_repair_PSS
391  *
392  * PARAMETERS:  Data                - Pointer to validation data structure
393  *              return_object_ptr   - Pointer to the object returned from the
394  *                                    evaluation of a method or object
395  *
396  * RETURN:      Status. AE_OK if object is OK or was repaired successfully
397  *
398  * DESCRIPTION: Repair for the _PSS object. If necessary, sort the object list
399  *              by the CPU frequencies. Check that the power dissipation values
400  *              are all proportional to CPU frequency (i.e., sorting by
401  *              frequency should be the same as sorting by power.)
402  *
403  *****************************************************************************/
404
405 static acpi_status
406 acpi_ns_repair_PSS(struct acpi_predefined_data *data,
407                    union acpi_operand_object **return_object_ptr)
408 {
409         union acpi_operand_object *return_object = *return_object_ptr;
410         union acpi_operand_object **outer_elements;
411         u32 outer_element_count;
412         union acpi_operand_object **elements;
413         union acpi_operand_object *obj_desc;
414         u32 previous_value;
415         acpi_status status;
416         u32 i;
417
418         /*
419          * Entries (sub-packages) in the _PSS Package must be sorted by power
420          * dissipation, in descending order. If it appears that the list is
421          * incorrectly sorted, sort it. We sort by cpu_frequency, since this
422          * should be proportional to the power.
423          */
424         status = acpi_ns_check_sorted_list(data, return_object, 6, 0,
425                                            ACPI_SORT_DESCENDING,
426                                            "CpuFrequency");
427         if (ACPI_FAILURE(status)) {
428                 return (status);
429         }
430
431         /*
432          * We now know the list is correctly sorted by CPU frequency. Check if
433          * the power dissipation values are proportional.
434          */
435         previous_value = ACPI_UINT32_MAX;
436         outer_elements = return_object->package.elements;
437         outer_element_count = return_object->package.count;
438
439         for (i = 0; i < outer_element_count; i++) {
440                 elements = (*outer_elements)->package.elements;
441                 obj_desc = elements[1]; /* Index1 = power_dissipation */
442
443                 if ((u32) obj_desc->integer.value > previous_value) {
444                         ACPI_WARN_PREDEFINED((AE_INFO, data->pathname,
445                                               data->node_flags,
446                                               "SubPackage[%u,%u] - suspicious power dissipation values",
447                                               i - 1, i));
448                 }
449
450                 previous_value = (u32) obj_desc->integer.value;
451                 outer_elements++;
452         }
453
454         return (AE_OK);
455 }
456
457 /******************************************************************************
458  *
459  * FUNCTION:    acpi_ns_check_sorted_list
460  *
461  * PARAMETERS:  Data                - Pointer to validation data structure
462  *              return_object       - Pointer to the top-level returned object
463  *              expected_count      - Minimum length of each sub-package
464  *              sort_index          - Sub-package entry to sort on
465  *              sort_direction      - Ascending or descending
466  *              sort_key_name       - Name of the sort_index field
467  *
468  * RETURN:      Status. AE_OK if the list is valid and is sorted correctly or
469  *              has been repaired by sorting the list.
470  *
471  * DESCRIPTION: Check if the package list is valid and sorted correctly by the
472  *              sort_index. If not, then sort the list.
473  *
474  *****************************************************************************/
475
476 static acpi_status
477 acpi_ns_check_sorted_list(struct acpi_predefined_data *data,
478                           union acpi_operand_object *return_object,
479                           u32 expected_count,
480                           u32 sort_index,
481                           u8 sort_direction, char *sort_key_name)
482 {
483         u32 outer_element_count;
484         union acpi_operand_object **outer_elements;
485         union acpi_operand_object **elements;
486         union acpi_operand_object *obj_desc;
487         u32 i;
488         u32 previous_value;
489         acpi_status status;
490
491         /* The top-level object must be a package */
492
493         if (return_object->common.type != ACPI_TYPE_PACKAGE) {
494                 return (AE_AML_OPERAND_TYPE);
495         }
496
497         /*
498          * Detect any NULL package elements and remove them from the
499          * package.
500          *
501          * TBD: We may want to do this for all predefined names that
502          * return a variable-length package of packages.
503          */
504         status = acpi_ns_remove_null_elements(return_object);
505         if (status == AE_NULL_ENTRY) {
506                 ACPI_INFO_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
507                                       "NULL elements removed from package"));
508
509                 /* Exit if package is now zero length */
510
511                 if (!return_object->package.count) {
512                         return (AE_NULL_ENTRY);
513                 }
514         }
515
516         outer_elements = return_object->package.elements;
517         outer_element_count = return_object->package.count;
518         if (!outer_element_count) {
519                 return (AE_AML_PACKAGE_LIMIT);
520         }
521
522         previous_value = 0;
523         if (sort_direction == ACPI_SORT_DESCENDING) {
524                 previous_value = ACPI_UINT32_MAX;
525         }
526
527         /* Examine each subpackage */
528
529         for (i = 0; i < outer_element_count; i++) {
530
531                 /* Each element of the top-level package must also be a package */
532
533                 if ((*outer_elements)->common.type != ACPI_TYPE_PACKAGE) {
534                         return (AE_AML_OPERAND_TYPE);
535                 }
536
537                 /* Each sub-package must have the minimum length */
538
539                 if ((*outer_elements)->package.count < expected_count) {
540                         return (AE_AML_PACKAGE_LIMIT);
541                 }
542
543                 elements = (*outer_elements)->package.elements;
544                 obj_desc = elements[sort_index];
545
546                 if (obj_desc->common.type != ACPI_TYPE_INTEGER) {
547                         return (AE_AML_OPERAND_TYPE);
548                 }
549
550                 /*
551                  * The list must be sorted in the specified order. If we detect a
552                  * discrepancy, issue a warning and sort the entire list
553                  */
554                 if (((sort_direction == ACPI_SORT_ASCENDING) &&
555                      (obj_desc->integer.value < previous_value)) ||
556                     ((sort_direction == ACPI_SORT_DESCENDING) &&
557                      (obj_desc->integer.value > previous_value))) {
558                         status =
559                             acpi_ns_sort_list(return_object->package.elements,
560                                               outer_element_count, sort_index,
561                                               sort_direction);
562                         if (ACPI_FAILURE(status)) {
563                                 return (status);
564                         }
565
566                         data->flags |= ACPI_OBJECT_REPAIRED;
567
568                         ACPI_INFO_PREDEFINED((AE_INFO, data->pathname,
569                                               data->node_flags,
570                                               "Repaired unsorted list - now sorted by %s",
571                                               sort_key_name));
572                         return (AE_OK);
573                 }
574
575                 previous_value = (u32) obj_desc->integer.value;
576                 outer_elements++;
577         }
578
579         return (AE_OK);
580 }
581
582 /******************************************************************************
583  *
584  * FUNCTION:    acpi_ns_remove_null_elements
585  *
586  * PARAMETERS:  obj_desc            - A Package object
587  *
588  * RETURN:      Status. AE_NULL_ENTRY means that one or more elements were
589  *              removed.
590  *
591  * DESCRIPTION: Remove all NULL package elements and update the package count.
592  *
593  *****************************************************************************/
594
595 static acpi_status
596 acpi_ns_remove_null_elements(union acpi_operand_object *obj_desc)
597 {
598         union acpi_operand_object **source;
599         union acpi_operand_object **dest;
600         acpi_status status = AE_OK;
601         u32 count;
602         u32 new_count;
603         u32 i;
604
605         count = obj_desc->package.count;
606         new_count = count;
607
608         source = obj_desc->package.elements;
609         dest = source;
610
611         /* Examine all elements of the package object */
612
613         for (i = 0; i < count; i++) {
614                 if (!*source) {
615                         status = AE_NULL_ENTRY;
616                         new_count--;
617                 } else {
618                         *dest = *source;
619                         dest++;
620                 }
621                 source++;
622         }
623
624         if (status == AE_NULL_ENTRY) {
625
626                 /* NULL terminate list and update the package count */
627
628                 *dest = NULL;
629                 obj_desc->package.count = new_count;
630         }
631
632         return (status);
633 }
634
635 /******************************************************************************
636  *
637  * FUNCTION:    acpi_ns_sort_list
638  *
639  * PARAMETERS:  Elements            - Package object element list
640  *              Count               - Element count for above
641  *              Index               - Sort by which package element
642  *              sort_direction      - Ascending or Descending sort
643  *
644  * RETURN:      Status
645  *
646  * DESCRIPTION: Sort the objects that are in a package element list.
647  *
648  * NOTE: Assumes that all NULL elements have been removed from the package.
649  *
650  *****************************************************************************/
651
652 static acpi_status
653 acpi_ns_sort_list(union acpi_operand_object **elements,
654                   u32 count, u32 index, u8 sort_direction)
655 {
656         union acpi_operand_object *obj_desc1;
657         union acpi_operand_object *obj_desc2;
658         union acpi_operand_object *temp_obj;
659         u32 i;
660         u32 j;
661
662         /* Simple bubble sort */
663
664         for (i = 1; i < count; i++) {
665                 for (j = (count - 1); j >= i; j--) {
666                         obj_desc1 = elements[j - 1]->package.elements[index];
667                         obj_desc2 = elements[j]->package.elements[index];
668
669                         if (((sort_direction == ACPI_SORT_ASCENDING) &&
670                              (obj_desc1->integer.value >
671                               obj_desc2->integer.value))
672                             || ((sort_direction == ACPI_SORT_DESCENDING)
673                                 && (obj_desc1->integer.value <
674                                     obj_desc2->integer.value))) {
675                                 temp_obj = elements[j - 1];
676                                 elements[j - 1] = elements[j];
677                                 elements[j] = temp_obj;
678                         }
679                 }
680         }
681
682         return (AE_OK);
683 }