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