ACPICA: Update all ACPICA copyrights and signons to 2010
[linux-3.10.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 - 2010, 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 #include "acpredef.h"
49
50 #define _COMPONENT          ACPI_NAMESPACE
51 ACPI_MODULE_NAME("nsrepair2")
52
53 /*
54  * Information structure and handler for ACPI predefined names that can
55  * be repaired on a per-name basis.
56  */
57 typedef
58 acpi_status(*acpi_repair_function) (struct acpi_predefined_data *data,
59                                     union acpi_operand_object **return_object_ptr);
60
61 typedef struct acpi_repair_info {
62         char name[ACPI_NAME_SIZE];
63         acpi_repair_function repair_function;
64
65 } acpi_repair_info;
66
67 /* Local prototypes */
68
69 static const struct acpi_repair_info *acpi_ns_match_repairable_name(struct
70                                                                     acpi_namespace_node
71                                                                     *node);
72
73 static acpi_status
74 acpi_ns_repair_ALR(struct acpi_predefined_data *data,
75                    union acpi_operand_object **return_object_ptr);
76
77 static acpi_status
78 acpi_ns_repair_FDE(struct acpi_predefined_data *data,
79                    union acpi_operand_object **return_object_ptr);
80
81 static acpi_status
82 acpi_ns_repair_PSS(struct acpi_predefined_data *data,
83                    union acpi_operand_object **return_object_ptr);
84
85 static acpi_status
86 acpi_ns_repair_TSS(struct acpi_predefined_data *data,
87                    union acpi_operand_object **return_object_ptr);
88
89 static acpi_status
90 acpi_ns_check_sorted_list(struct acpi_predefined_data *data,
91                           union acpi_operand_object *return_object,
92                           u32 expected_count,
93                           u32 sort_index,
94                           u8 sort_direction, char *sort_key_name);
95
96 static void
97 acpi_ns_sort_list(union acpi_operand_object **elements,
98                   u32 count, u32 index, u8 sort_direction);
99
100 /* Values for sort_direction above */
101
102 #define ACPI_SORT_ASCENDING     0
103 #define ACPI_SORT_DESCENDING    1
104
105 /*
106  * This table contains the names of the predefined methods for which we can
107  * perform more complex repairs.
108  *
109  * As necessary:
110  *
111  * _ALR: Sort the list ascending by ambient_illuminance
112  * _FDE: Convert Buffer of BYTEs to a Buffer of DWORDs
113  * _GTM: Convert Buffer of BYTEs to a Buffer of DWORDs
114  * _PSS: Sort the list descending by Power
115  * _TSS: Sort the list descending by Power
116  */
117 static const struct acpi_repair_info acpi_ns_repairable_names[] = {
118         {"_ALR", acpi_ns_repair_ALR},
119         {"_FDE", acpi_ns_repair_FDE},
120         {"_GTM", acpi_ns_repair_FDE},   /* _GTM has same repair as _FDE */
121         {"_PSS", acpi_ns_repair_PSS},
122         {"_TSS", acpi_ns_repair_TSS},
123         {{0, 0, 0, 0}, NULL}    /* Table terminator */
124 };
125
126 #define ACPI_FDE_FIELD_COUNT        5
127 #define ACPI_FDE_BYTE_BUFFER_SIZE   5
128 #define ACPI_FDE_DWORD_BUFFER_SIZE  (ACPI_FDE_FIELD_COUNT * sizeof (u32))
129
130 /******************************************************************************
131  *
132  * FUNCTION:    acpi_ns_complex_repairs
133  *
134  * PARAMETERS:  Data                - Pointer to validation data structure
135  *              Node                - Namespace node for the method/object
136  *              validate_status     - Original status of earlier validation
137  *              return_object_ptr   - Pointer to the object returned from the
138  *                                    evaluation of a method or object
139  *
140  * RETURN:      Status. AE_OK if repair was successful. If name is not
141  *              matched, validate_status is returned.
142  *
143  * DESCRIPTION: Attempt to repair/convert a return object of a type that was
144  *              not expected.
145  *
146  *****************************************************************************/
147
148 acpi_status
149 acpi_ns_complex_repairs(struct acpi_predefined_data *data,
150                         struct acpi_namespace_node *node,
151                         acpi_status validate_status,
152                         union acpi_operand_object **return_object_ptr)
153 {
154         const struct acpi_repair_info *predefined;
155         acpi_status status;
156
157         /* Check if this name is in the list of repairable names */
158
159         predefined = acpi_ns_match_repairable_name(node);
160         if (!predefined) {
161                 return (validate_status);
162         }
163
164         status = predefined->repair_function(data, return_object_ptr);
165         return (status);
166 }
167
168 /******************************************************************************
169  *
170  * FUNCTION:    acpi_ns_match_repairable_name
171  *
172  * PARAMETERS:  Node                - Namespace node for the method/object
173  *
174  * RETURN:      Pointer to entry in repair table. NULL indicates not found.
175  *
176  * DESCRIPTION: Check an object name against the repairable object list.
177  *
178  *****************************************************************************/
179
180 static const struct acpi_repair_info *acpi_ns_match_repairable_name(struct
181                                                                     acpi_namespace_node
182                                                                     *node)
183 {
184         const struct acpi_repair_info *this_name;
185
186         /* Search info table for a repairable predefined method/object name */
187
188         this_name = acpi_ns_repairable_names;
189         while (this_name->repair_function) {
190                 if (ACPI_COMPARE_NAME(node->name.ascii, this_name->name)) {
191                         return (this_name);
192                 }
193                 this_name++;
194         }
195
196         return (NULL);          /* Not found */
197 }
198
199 /******************************************************************************
200  *
201  * FUNCTION:    acpi_ns_repair_ALR
202  *
203  * PARAMETERS:  Data                - Pointer to validation data structure
204  *              return_object_ptr   - Pointer to the object returned from the
205  *                                    evaluation of a method or object
206  *
207  * RETURN:      Status. AE_OK if object is OK or was repaired successfully
208  *
209  * DESCRIPTION: Repair for the _ALR object. If necessary, sort the object list
210  *              ascending by the ambient illuminance values.
211  *
212  *****************************************************************************/
213
214 static acpi_status
215 acpi_ns_repair_ALR(struct acpi_predefined_data *data,
216                    union acpi_operand_object **return_object_ptr)
217 {
218         union acpi_operand_object *return_object = *return_object_ptr;
219         acpi_status status;
220
221         status = acpi_ns_check_sorted_list(data, return_object, 2, 1,
222                                            ACPI_SORT_ASCENDING,
223                                            "AmbientIlluminance");
224
225         return (status);
226 }
227
228 /******************************************************************************
229  *
230  * FUNCTION:    acpi_ns_repair_FDE
231  *
232  * PARAMETERS:  Data                - Pointer to validation data structure
233  *              return_object_ptr   - Pointer to the object returned from the
234  *                                    evaluation of a method or object
235  *
236  * RETURN:      Status. AE_OK if object is OK or was repaired successfully
237  *
238  * DESCRIPTION: Repair for the _FDE and _GTM objects. The expected return
239  *              value is a Buffer of 5 DWORDs. This function repairs a common
240  *              problem where the return value is a Buffer of BYTEs, not
241  *              DWORDs.
242  *
243  *****************************************************************************/
244
245 static acpi_status
246 acpi_ns_repair_FDE(struct acpi_predefined_data *data,
247                    union acpi_operand_object **return_object_ptr)
248 {
249         union acpi_operand_object *return_object = *return_object_ptr;
250         union acpi_operand_object *buffer_object;
251         u8 *byte_buffer;
252         u32 *dword_buffer;
253         u32 i;
254
255         ACPI_FUNCTION_NAME(ns_repair_FDE);
256
257         switch (return_object->common.type) {
258         case ACPI_TYPE_BUFFER:
259
260                 /* This is the expected type. Length should be (at least) 5 DWORDs */
261
262                 if (return_object->buffer.length >= ACPI_FDE_DWORD_BUFFER_SIZE) {
263                         return (AE_OK);
264                 }
265
266                 /* We can only repair if we have exactly 5 BYTEs */
267
268                 if (return_object->buffer.length != ACPI_FDE_BYTE_BUFFER_SIZE) {
269                         ACPI_WARN_PREDEFINED((AE_INFO, data->pathname,
270                                               data->node_flags,
271                                               "Incorrect return buffer length %u, expected %u",
272                                               return_object->buffer.length,
273                                               ACPI_FDE_DWORD_BUFFER_SIZE));
274
275                         return (AE_AML_OPERAND_TYPE);
276                 }
277
278                 /* Create the new (larger) buffer object */
279
280                 buffer_object =
281                     acpi_ut_create_buffer_object(ACPI_FDE_DWORD_BUFFER_SIZE);
282                 if (!buffer_object) {
283                         return (AE_NO_MEMORY);
284                 }
285
286                 /* Expand each byte to a DWORD */
287
288                 byte_buffer = return_object->buffer.pointer;
289                 dword_buffer =
290                     ACPI_CAST_PTR(u32, buffer_object->buffer.pointer);
291
292                 for (i = 0; i < ACPI_FDE_FIELD_COUNT; i++) {
293                         *dword_buffer = (u32) *byte_buffer;
294                         dword_buffer++;
295                         byte_buffer++;
296                 }
297
298                 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
299                                   "%s Expanded Byte Buffer to expected DWord Buffer\n",
300                                   data->pathname));
301                 break;
302
303         default:
304                 return (AE_AML_OPERAND_TYPE);
305         }
306
307         /* Delete the original return object, return the new buffer object */
308
309         acpi_ut_remove_reference(return_object);
310         *return_object_ptr = buffer_object;
311
312         data->flags |= ACPI_OBJECT_REPAIRED;
313         return (AE_OK);
314 }
315
316 /******************************************************************************
317  *
318  * FUNCTION:    acpi_ns_repair_TSS
319  *
320  * PARAMETERS:  Data                - Pointer to validation data structure
321  *              return_object_ptr   - Pointer to the object returned from the
322  *                                    evaluation of a method or object
323  *
324  * RETURN:      Status. AE_OK if object is OK or was repaired successfully
325  *
326  * DESCRIPTION: Repair for the _TSS object. If necessary, sort the object list
327  *              descending by the power dissipation values.
328  *
329  *****************************************************************************/
330
331 static acpi_status
332 acpi_ns_repair_TSS(struct acpi_predefined_data *data,
333                    union acpi_operand_object **return_object_ptr)
334 {
335         union acpi_operand_object *return_object = *return_object_ptr;
336         acpi_status status;
337
338         status = acpi_ns_check_sorted_list(data, return_object, 5, 1,
339                                            ACPI_SORT_DESCENDING,
340                                            "PowerDissipation");
341
342         return (status);
343 }
344
345 /******************************************************************************
346  *
347  * FUNCTION:    acpi_ns_repair_PSS
348  *
349  * PARAMETERS:  Data                - Pointer to validation data structure
350  *              return_object_ptr   - Pointer to the object returned from the
351  *                                    evaluation of a method or object
352  *
353  * RETURN:      Status. AE_OK if object is OK or was repaired successfully
354  *
355  * DESCRIPTION: Repair for the _PSS object. If necessary, sort the object list
356  *              by the CPU frequencies. Check that the power dissipation values
357  *              are all proportional to CPU frequency (i.e., sorting by
358  *              frequency should be the same as sorting by power.)
359  *
360  *****************************************************************************/
361
362 static acpi_status
363 acpi_ns_repair_PSS(struct acpi_predefined_data *data,
364                    union acpi_operand_object **return_object_ptr)
365 {
366         union acpi_operand_object *return_object = *return_object_ptr;
367         union acpi_operand_object **outer_elements;
368         u32 outer_element_count;
369         union acpi_operand_object **elements;
370         union acpi_operand_object *obj_desc;
371         u32 previous_value;
372         acpi_status status;
373         u32 i;
374
375         /*
376          * Entries (sub-packages) in the _PSS Package must be sorted by power
377          * dissipation, in descending order. If it appears that the list is
378          * incorrectly sorted, sort it. We sort by cpu_frequency, since this
379          * should be proportional to the power.
380          */
381         status = acpi_ns_check_sorted_list(data, return_object, 6, 0,
382                                            ACPI_SORT_DESCENDING,
383                                            "CpuFrequency");
384         if (ACPI_FAILURE(status)) {
385                 return (status);
386         }
387
388         /*
389          * We now know the list is correctly sorted by CPU frequency. Check if
390          * the power dissipation values are proportional.
391          */
392         previous_value = ACPI_UINT32_MAX;
393         outer_elements = return_object->package.elements;
394         outer_element_count = return_object->package.count;
395
396         for (i = 0; i < outer_element_count; i++) {
397                 elements = (*outer_elements)->package.elements;
398                 obj_desc = elements[1]; /* Index1 = power_dissipation */
399
400                 if ((u32) obj_desc->integer.value > previous_value) {
401                         ACPI_WARN_PREDEFINED((AE_INFO, data->pathname,
402                                               data->node_flags,
403                                               "SubPackage[%u,%u] - suspicious power dissipation values",
404                                               i - 1, i));
405                 }
406
407                 previous_value = (u32) obj_desc->integer.value;
408                 outer_elements++;
409         }
410
411         return (AE_OK);
412 }
413
414 /******************************************************************************
415  *
416  * FUNCTION:    acpi_ns_check_sorted_list
417  *
418  * PARAMETERS:  Data                - Pointer to validation data structure
419  *              return_object       - Pointer to the top-level returned object
420  *              expected_count      - Minimum length of each sub-package
421  *              sort_index          - Sub-package entry to sort on
422  *              sort_direction      - Ascending or descending
423  *              sort_key_name       - Name of the sort_index field
424  *
425  * RETURN:      Status. AE_OK if the list is valid and is sorted correctly or
426  *              has been repaired by sorting the list.
427  *
428  * DESCRIPTION: Check if the package list is valid and sorted correctly by the
429  *              sort_index. If not, then sort the list.
430  *
431  *****************************************************************************/
432
433 static acpi_status
434 acpi_ns_check_sorted_list(struct acpi_predefined_data *data,
435                           union acpi_operand_object *return_object,
436                           u32 expected_count,
437                           u32 sort_index,
438                           u8 sort_direction, char *sort_key_name)
439 {
440         u32 outer_element_count;
441         union acpi_operand_object **outer_elements;
442         union acpi_operand_object **elements;
443         union acpi_operand_object *obj_desc;
444         u32 i;
445         u32 previous_value;
446
447         ACPI_FUNCTION_NAME(ns_check_sorted_list);
448
449         /* The top-level object must be a package */
450
451         if (return_object->common.type != ACPI_TYPE_PACKAGE) {
452                 return (AE_AML_OPERAND_TYPE);
453         }
454
455         /*
456          * NOTE: assumes list of sub-packages contains no NULL elements.
457          * Any NULL elements should have been removed by earlier call
458          * to acpi_ns_remove_null_elements.
459          */
460         outer_elements = return_object->package.elements;
461         outer_element_count = return_object->package.count;
462         if (!outer_element_count) {
463                 return (AE_AML_PACKAGE_LIMIT);
464         }
465
466         previous_value = 0;
467         if (sort_direction == ACPI_SORT_DESCENDING) {
468                 previous_value = ACPI_UINT32_MAX;
469         }
470
471         /* Examine each subpackage */
472
473         for (i = 0; i < outer_element_count; i++) {
474
475                 /* Each element of the top-level package must also be a package */
476
477                 if ((*outer_elements)->common.type != ACPI_TYPE_PACKAGE) {
478                         return (AE_AML_OPERAND_TYPE);
479                 }
480
481                 /* Each sub-package must have the minimum length */
482
483                 if ((*outer_elements)->package.count < expected_count) {
484                         return (AE_AML_PACKAGE_LIMIT);
485                 }
486
487                 elements = (*outer_elements)->package.elements;
488                 obj_desc = elements[sort_index];
489
490                 if (obj_desc->common.type != ACPI_TYPE_INTEGER) {
491                         return (AE_AML_OPERAND_TYPE);
492                 }
493
494                 /*
495                  * The list must be sorted in the specified order. If we detect a
496                  * discrepancy, sort the entire list.
497                  */
498                 if (((sort_direction == ACPI_SORT_ASCENDING) &&
499                      (obj_desc->integer.value < previous_value)) ||
500                     ((sort_direction == ACPI_SORT_DESCENDING) &&
501                      (obj_desc->integer.value > previous_value))) {
502                         acpi_ns_sort_list(return_object->package.elements,
503                                           outer_element_count, sort_index,
504                                           sort_direction);
505
506                         data->flags |= ACPI_OBJECT_REPAIRED;
507
508                         ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
509                                           "%s: Repaired unsorted list - now sorted by %s\n",
510                                           data->pathname, sort_key_name));
511                         return (AE_OK);
512                 }
513
514                 previous_value = (u32) obj_desc->integer.value;
515                 outer_elements++;
516         }
517
518         return (AE_OK);
519 }
520
521 /******************************************************************************
522  *
523  * FUNCTION:    acpi_ns_remove_null_elements
524  *
525  * PARAMETERS:  Data                - Pointer to validation data structure
526  *              package_type        - An acpi_return_package_types value
527  *              obj_desc            - A Package object
528  *
529  * RETURN:      None.
530  *
531  * DESCRIPTION: Remove all NULL package elements from packages that contain
532  *              a variable number of sub-packages.
533  *
534  *****************************************************************************/
535
536 void
537 acpi_ns_remove_null_elements(struct acpi_predefined_data *data,
538                              u8 package_type,
539                              union acpi_operand_object *obj_desc)
540 {
541         union acpi_operand_object **source;
542         union acpi_operand_object **dest;
543         u32 count;
544         u32 new_count;
545         u32 i;
546
547         ACPI_FUNCTION_NAME(ns_remove_null_elements);
548
549         /*
550          * PTYPE1 packages contain no subpackages.
551          * PTYPE2 packages contain a variable number of sub-packages. We can
552          * safely remove all NULL elements from the PTYPE2 packages.
553          */
554         switch (package_type) {
555         case ACPI_PTYPE1_FIXED:
556         case ACPI_PTYPE1_VAR:
557         case ACPI_PTYPE1_OPTION:
558                 return;
559
560         case ACPI_PTYPE2:
561         case ACPI_PTYPE2_COUNT:
562         case ACPI_PTYPE2_PKG_COUNT:
563         case ACPI_PTYPE2_FIXED:
564         case ACPI_PTYPE2_MIN:
565         case ACPI_PTYPE2_REV_FIXED:
566                 break;
567
568         default:
569                 return;
570         }
571
572         count = obj_desc->package.count;
573         new_count = count;
574
575         source = obj_desc->package.elements;
576         dest = source;
577
578         /* Examine all elements of the package object, remove nulls */
579
580         for (i = 0; i < count; i++) {
581                 if (!*source) {
582                         new_count--;
583                 } else {
584                         *dest = *source;
585                         dest++;
586                 }
587                 source++;
588         }
589
590         /* Update parent package if any null elements were removed */
591
592         if (new_count < count) {
593                 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
594                                   "%s: Found and removed %u NULL elements\n",
595                                   data->pathname, (count - new_count)));
596
597                 /* NULL terminate list and update the package count */
598
599                 *dest = NULL;
600                 obj_desc->package.count = new_count;
601         }
602 }
603
604 /******************************************************************************
605  *
606  * FUNCTION:    acpi_ns_sort_list
607  *
608  * PARAMETERS:  Elements            - Package object element list
609  *              Count               - Element count for above
610  *              Index               - Sort by which package element
611  *              sort_direction      - Ascending or Descending sort
612  *
613  * RETURN:      None
614  *
615  * DESCRIPTION: Sort the objects that are in a package element list.
616  *
617  * NOTE: Assumes that all NULL elements have been removed from the package,
618  *       and that all elements have been verified to be of type Integer.
619  *
620  *****************************************************************************/
621
622 static void
623 acpi_ns_sort_list(union acpi_operand_object **elements,
624                   u32 count, u32 index, u8 sort_direction)
625 {
626         union acpi_operand_object *obj_desc1;
627         union acpi_operand_object *obj_desc2;
628         union acpi_operand_object *temp_obj;
629         u32 i;
630         u32 j;
631
632         /* Simple bubble sort */
633
634         for (i = 1; i < count; i++) {
635                 for (j = (count - 1); j >= i; j--) {
636                         obj_desc1 = elements[j - 1]->package.elements[index];
637                         obj_desc2 = elements[j]->package.elements[index];
638
639                         if (((sort_direction == ACPI_SORT_ASCENDING) &&
640                              (obj_desc1->integer.value >
641                               obj_desc2->integer.value))
642                             || ((sort_direction == ACPI_SORT_DESCENDING)
643                                 && (obj_desc1->integer.value <
644                                     obj_desc2->integer.value))) {
645                                 temp_obj = elements[j - 1];
646                                 elements[j - 1] = elements[j];
647                                 elements[j] = temp_obj;
648                         }
649                 }
650         }
651 }