blob: e7373eb6cfa5d0034384b7e577d6dcdfaa8a11b3 [file] [log] [blame]
Bob Mooread5babe2009-11-12 09:44:06 +08001/******************************************************************************
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
50ACPI_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 */
56typedef
57acpi_status(*acpi_repair_function) (struct acpi_predefined_data *data,
58 union acpi_operand_object **return_object_ptr);
59
60typedef 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
68static const struct acpi_repair_info *acpi_ns_match_repairable_name(struct
69 acpi_namespace_node
70 *node);
71
72static acpi_status
73acpi_ns_repair_ALR(struct acpi_predefined_data *data,
74 union acpi_operand_object **return_object_ptr);
75
76static acpi_status
Bob Moore34c39c72009-12-11 14:53:11 +080077acpi_ns_repair_FDE(struct acpi_predefined_data *data,
78 union acpi_operand_object **return_object_ptr);
79
80static acpi_status
Bob Mooread5babe2009-11-12 09:44:06 +080081acpi_ns_repair_PSS(struct acpi_predefined_data *data,
82 union acpi_operand_object **return_object_ptr);
83
84static acpi_status
85acpi_ns_repair_TSS(struct acpi_predefined_data *data,
86 union acpi_operand_object **return_object_ptr);
87
88static acpi_status
89acpi_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
95static acpi_status
96acpi_ns_remove_null_elements(union acpi_operand_object *package);
97
98static acpi_status
99acpi_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 *
Bob Moore34c39c72009-12-11 14:53:11 +0800111 * As necessary:
112 *
113 * _ALR: Sort the list ascending by ambient_illuminance
Bob Moore43420bb2009-12-11 15:24:27 +0800114 * _FDE: Convert Buffer of BYTEs to a Buffer of DWORDs
115 * _GTM: Convert Buffer of BYTEs to a Buffer of DWORDs
Bob Moore34c39c72009-12-11 14:53:11 +0800116 * _PSS: Sort the list descending by Power
117 * _TSS: Sort the list descending by Power
Bob Mooread5babe2009-11-12 09:44:06 +0800118 */
119static const struct acpi_repair_info acpi_ns_repairable_names[] = {
120 {"_ALR", acpi_ns_repair_ALR},
Bob Moore34c39c72009-12-11 14:53:11 +0800121 {"_FDE", acpi_ns_repair_FDE},
122 {"_GTM", acpi_ns_repair_FDE}, /* _GTM has same repair as _FDE */
Bob Mooread5babe2009-11-12 09:44:06 +0800123 {"_PSS", acpi_ns_repair_PSS},
124 {"_TSS", acpi_ns_repair_TSS},
125 {{0, 0, 0, 0}, NULL} /* Table terminator */
126};
127
Bob Moore34c39c72009-12-11 14:53:11 +0800128#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
Bob Mooread5babe2009-11-12 09:44:06 +0800132/******************************************************************************
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
150acpi_status
151acpi_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
182static 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
216static acpi_status
217acpi_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 *
Bob Moore34c39c72009-12-11 14:53:11 +0800232 * 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
Bob Moore43420bb2009-12-11 15:24:27 +0800241 * value is a Buffer of 5 DWORDs. This function repairs a common
242 * problem where the return value is a Buffer of BYTEs, not
243 * DWORDs.
Bob Moore34c39c72009-12-11 14:53:11 +0800244 *
245 *****************************************************************************/
246
247static acpi_status
248acpi_ns_repair_FDE(struct acpi_predefined_data *data,
249 union acpi_operand_object **return_object_ptr)
250{
251 union acpi_operand_object *return_object = *return_object_ptr;
Bob Moore34c39c72009-12-11 14:53:11 +0800252 union acpi_operand_object *buffer_object;
253 u8 *byte_buffer;
254 u32 *dword_buffer;
Bob Moore34c39c72009-12-11 14:53:11 +0800255 u32 i;
256
Bob Moore3a581762009-12-11 15:23:22 +0800257 ACPI_FUNCTION_NAME(ns_repair_FDE);
258
Bob Moore34c39c72009-12-11 14:53:11 +0800259 switch (return_object->common.type) {
260 case ACPI_TYPE_BUFFER:
261
262 /* This is the expected type. Length should be (at least) 5 DWORDs */
263
264 if (return_object->buffer.length >= ACPI_FDE_DWORD_BUFFER_SIZE) {
265 return (AE_OK);
266 }
267
268 /* We can only repair if we have exactly 5 BYTEs */
269
270 if (return_object->buffer.length != ACPI_FDE_BYTE_BUFFER_SIZE) {
271 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname,
272 data->node_flags,
273 "Incorrect return buffer length %u, expected %u",
274 return_object->buffer.length,
275 ACPI_FDE_DWORD_BUFFER_SIZE));
276
277 return (AE_AML_OPERAND_TYPE);
278 }
279
280 /* Create the new (larger) buffer object */
281
282 buffer_object =
283 acpi_ut_create_buffer_object(ACPI_FDE_DWORD_BUFFER_SIZE);
284 if (!buffer_object) {
285 return (AE_NO_MEMORY);
286 }
287
288 /* Expand each byte to a DWORD */
289
290 byte_buffer = return_object->buffer.pointer;
291 dword_buffer =
292 ACPI_CAST_PTR(u32, buffer_object->buffer.pointer);
293
294 for (i = 0; i < ACPI_FDE_FIELD_COUNT; i++) {
295 *dword_buffer = (u32) *byte_buffer;
296 dword_buffer++;
297 byte_buffer++;
298 }
299
Bob Moore3a581762009-12-11 15:23:22 +0800300 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
301 "%s Expanded Byte Buffer to expected DWord Buffer\n",
302 data->pathname));
Bob Moore34c39c72009-12-11 14:53:11 +0800303 break;
304
Bob Moore34c39c72009-12-11 14:53:11 +0800305 default:
306 return (AE_AML_OPERAND_TYPE);
307 }
308
309 /* Delete the original return object, return the new buffer object */
310
311 acpi_ut_remove_reference(return_object);
312 *return_object_ptr = buffer_object;
313
314 data->flags |= ACPI_OBJECT_REPAIRED;
315 return (AE_OK);
316}
317
318/******************************************************************************
319 *
Bob Mooread5babe2009-11-12 09:44:06 +0800320 * FUNCTION: acpi_ns_repair_TSS
321 *
322 * PARAMETERS: Data - Pointer to validation data structure
323 * return_object_ptr - Pointer to the object returned from the
324 * evaluation of a method or object
325 *
326 * RETURN: Status. AE_OK if object is OK or was repaired successfully
327 *
328 * DESCRIPTION: Repair for the _TSS object. If necessary, sort the object list
329 * descending by the power dissipation values.
330 *
331 *****************************************************************************/
332
333static acpi_status
334acpi_ns_repair_TSS(struct acpi_predefined_data *data,
335 union acpi_operand_object **return_object_ptr)
336{
337 union acpi_operand_object *return_object = *return_object_ptr;
338 acpi_status status;
339
340 status = acpi_ns_check_sorted_list(data, return_object, 5, 1,
341 ACPI_SORT_DESCENDING,
342 "PowerDissipation");
343
344 return (status);
345}
346
347/******************************************************************************
348 *
349 * FUNCTION: acpi_ns_repair_PSS
350 *
351 * PARAMETERS: Data - Pointer to validation data structure
352 * return_object_ptr - Pointer to the object returned from the
353 * evaluation of a method or object
354 *
355 * RETURN: Status. AE_OK if object is OK or was repaired successfully
356 *
357 * DESCRIPTION: Repair for the _PSS object. If necessary, sort the object list
358 * by the CPU frequencies. Check that the power dissipation values
359 * are all proportional to CPU frequency (i.e., sorting by
360 * frequency should be the same as sorting by power.)
361 *
362 *****************************************************************************/
363
364static acpi_status
365acpi_ns_repair_PSS(struct acpi_predefined_data *data,
366 union acpi_operand_object **return_object_ptr)
367{
368 union acpi_operand_object *return_object = *return_object_ptr;
369 union acpi_operand_object **outer_elements;
370 u32 outer_element_count;
371 union acpi_operand_object **elements;
372 union acpi_operand_object *obj_desc;
373 u32 previous_value;
374 acpi_status status;
375 u32 i;
376
377 /*
378 * Entries (sub-packages) in the _PSS Package must be sorted by power
379 * dissipation, in descending order. If it appears that the list is
380 * incorrectly sorted, sort it. We sort by cpu_frequency, since this
381 * should be proportional to the power.
382 */
383 status = acpi_ns_check_sorted_list(data, return_object, 6, 0,
384 ACPI_SORT_DESCENDING,
385 "CpuFrequency");
386 if (ACPI_FAILURE(status)) {
387 return (status);
388 }
389
390 /*
391 * We now know the list is correctly sorted by CPU frequency. Check if
392 * the power dissipation values are proportional.
393 */
394 previous_value = ACPI_UINT32_MAX;
395 outer_elements = return_object->package.elements;
396 outer_element_count = return_object->package.count;
397
398 for (i = 0; i < outer_element_count; i++) {
399 elements = (*outer_elements)->package.elements;
400 obj_desc = elements[1]; /* Index1 = power_dissipation */
401
402 if ((u32) obj_desc->integer.value > previous_value) {
403 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname,
404 data->node_flags,
405 "SubPackage[%u,%u] - suspicious power dissipation values",
406 i - 1, i));
407 }
408
409 previous_value = (u32) obj_desc->integer.value;
410 outer_elements++;
411 }
412
413 return (AE_OK);
414}
415
416/******************************************************************************
417 *
418 * FUNCTION: acpi_ns_check_sorted_list
419 *
420 * PARAMETERS: Data - Pointer to validation data structure
421 * return_object - Pointer to the top-level returned object
422 * expected_count - Minimum length of each sub-package
423 * sort_index - Sub-package entry to sort on
424 * sort_direction - Ascending or descending
425 * sort_key_name - Name of the sort_index field
426 *
427 * RETURN: Status. AE_OK if the list is valid and is sorted correctly or
428 * has been repaired by sorting the list.
429 *
430 * DESCRIPTION: Check if the package list is valid and sorted correctly by the
431 * sort_index. If not, then sort the list.
432 *
433 *****************************************************************************/
434
435static acpi_status
436acpi_ns_check_sorted_list(struct acpi_predefined_data *data,
437 union acpi_operand_object *return_object,
438 u32 expected_count,
439 u32 sort_index,
440 u8 sort_direction, char *sort_key_name)
441{
442 u32 outer_element_count;
443 union acpi_operand_object **outer_elements;
444 union acpi_operand_object **elements;
445 union acpi_operand_object *obj_desc;
446 u32 i;
447 u32 previous_value;
448 acpi_status status;
449
Bob Moore3a581762009-12-11 15:23:22 +0800450 ACPI_FUNCTION_NAME(ns_check_sorted_list);
451
Bob Mooread5babe2009-11-12 09:44:06 +0800452 /* The top-level object must be a package */
453
454 if (return_object->common.type != ACPI_TYPE_PACKAGE) {
455 return (AE_AML_OPERAND_TYPE);
456 }
457
458 /*
459 * Detect any NULL package elements and remove them from the
460 * package.
461 *
462 * TBD: We may want to do this for all predefined names that
463 * return a variable-length package of packages.
464 */
465 status = acpi_ns_remove_null_elements(return_object);
466 if (status == AE_NULL_ENTRY) {
Bob Moore3a581762009-12-11 15:23:22 +0800467 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
468 "%s: NULL elements removed from package\n",
469 data->pathname));
Bob Mooread5babe2009-11-12 09:44:06 +0800470
471 /* Exit if package is now zero length */
472
473 if (!return_object->package.count) {
474 return (AE_NULL_ENTRY);
475 }
476 }
477
478 outer_elements = return_object->package.elements;
479 outer_element_count = return_object->package.count;
480 if (!outer_element_count) {
481 return (AE_AML_PACKAGE_LIMIT);
482 }
483
484 previous_value = 0;
485 if (sort_direction == ACPI_SORT_DESCENDING) {
486 previous_value = ACPI_UINT32_MAX;
487 }
488
489 /* Examine each subpackage */
490
491 for (i = 0; i < outer_element_count; i++) {
492
493 /* Each element of the top-level package must also be a package */
494
495 if ((*outer_elements)->common.type != ACPI_TYPE_PACKAGE) {
496 return (AE_AML_OPERAND_TYPE);
497 }
498
499 /* Each sub-package must have the minimum length */
500
501 if ((*outer_elements)->package.count < expected_count) {
502 return (AE_AML_PACKAGE_LIMIT);
503 }
504
505 elements = (*outer_elements)->package.elements;
506 obj_desc = elements[sort_index];
507
508 if (obj_desc->common.type != ACPI_TYPE_INTEGER) {
509 return (AE_AML_OPERAND_TYPE);
510 }
511
512 /*
513 * The list must be sorted in the specified order. If we detect a
514 * discrepancy, issue a warning and sort the entire list
515 */
516 if (((sort_direction == ACPI_SORT_ASCENDING) &&
517 (obj_desc->integer.value < previous_value)) ||
518 ((sort_direction == ACPI_SORT_DESCENDING) &&
519 (obj_desc->integer.value > previous_value))) {
520 status =
521 acpi_ns_sort_list(return_object->package.elements,
522 outer_element_count, sort_index,
523 sort_direction);
524 if (ACPI_FAILURE(status)) {
525 return (status);
526 }
527
528 data->flags |= ACPI_OBJECT_REPAIRED;
529
Bob Moore3a581762009-12-11 15:23:22 +0800530 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
531 "%s: Repaired unsorted list - now sorted by %s\n",
532 data->pathname, sort_key_name));
Bob Mooread5babe2009-11-12 09:44:06 +0800533 return (AE_OK);
534 }
535
536 previous_value = (u32) obj_desc->integer.value;
537 outer_elements++;
538 }
539
540 return (AE_OK);
541}
542
543/******************************************************************************
544 *
545 * FUNCTION: acpi_ns_remove_null_elements
546 *
547 * PARAMETERS: obj_desc - A Package object
548 *
549 * RETURN: Status. AE_NULL_ENTRY means that one or more elements were
550 * removed.
551 *
552 * DESCRIPTION: Remove all NULL package elements and update the package count.
553 *
554 *****************************************************************************/
555
556static acpi_status
557acpi_ns_remove_null_elements(union acpi_operand_object *obj_desc)
558{
559 union acpi_operand_object **source;
560 union acpi_operand_object **dest;
561 acpi_status status = AE_OK;
562 u32 count;
563 u32 new_count;
564 u32 i;
565
566 count = obj_desc->package.count;
567 new_count = count;
568
569 source = obj_desc->package.elements;
570 dest = source;
571
572 /* Examine all elements of the package object */
573
574 for (i = 0; i < count; i++) {
575 if (!*source) {
576 status = AE_NULL_ENTRY;
577 new_count--;
578 } else {
579 *dest = *source;
580 dest++;
581 }
582 source++;
583 }
584
585 if (status == AE_NULL_ENTRY) {
586
587 /* NULL terminate list and update the package count */
588
589 *dest = NULL;
590 obj_desc->package.count = new_count;
591 }
592
593 return (status);
594}
595
596/******************************************************************************
597 *
598 * FUNCTION: acpi_ns_sort_list
599 *
600 * PARAMETERS: Elements - Package object element list
601 * Count - Element count for above
602 * Index - Sort by which package element
603 * sort_direction - Ascending or Descending sort
604 *
605 * RETURN: Status
606 *
607 * DESCRIPTION: Sort the objects that are in a package element list.
608 *
609 * NOTE: Assumes that all NULL elements have been removed from the package.
610 *
611 *****************************************************************************/
612
613static acpi_status
614acpi_ns_sort_list(union acpi_operand_object **elements,
615 u32 count, u32 index, u8 sort_direction)
616{
617 union acpi_operand_object *obj_desc1;
618 union acpi_operand_object *obj_desc2;
619 union acpi_operand_object *temp_obj;
620 u32 i;
621 u32 j;
622
623 /* Simple bubble sort */
624
625 for (i = 1; i < count; i++) {
626 for (j = (count - 1); j >= i; j--) {
627 obj_desc1 = elements[j - 1]->package.elements[index];
628 obj_desc2 = elements[j]->package.elements[index];
629
630 if (((sort_direction == ACPI_SORT_ASCENDING) &&
631 (obj_desc1->integer.value >
632 obj_desc2->integer.value))
633 || ((sort_direction == ACPI_SORT_DESCENDING)
634 && (obj_desc1->integer.value <
635 obj_desc2->integer.value))) {
636 temp_obj = elements[j - 1];
637 elements[j - 1] = elements[j];
638 elements[j] = temp_obj;
639 }
640 }
641 }
642
643 return (AE_OK);
644}