xref: /openbmc/linux/drivers/acpi/acpica/nsrepair2.c (revision 4da722ca)
1 /******************************************************************************
2  *
3  * Module Name: nsrepair2 - Repair for objects returned by specific
4  *                          predefined methods
5  *
6  *****************************************************************************/
7 
8 /*
9  * Copyright (C) 2000 - 2017, 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_evaluate_info * info,
58 				     union acpi_operand_object **
59 				     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_complex_repair(struct
70 								   acpi_namespace_node
71 								   *node);
72 
73 static acpi_status
74 acpi_ns_repair_ALR(struct acpi_evaluate_info *info,
75 		   union acpi_operand_object **return_object_ptr);
76 
77 static acpi_status
78 acpi_ns_repair_CID(struct acpi_evaluate_info *info,
79 		   union acpi_operand_object **return_object_ptr);
80 
81 static acpi_status
82 acpi_ns_repair_CST(struct acpi_evaluate_info *info,
83 		   union acpi_operand_object **return_object_ptr);
84 
85 static acpi_status
86 acpi_ns_repair_FDE(struct acpi_evaluate_info *info,
87 		   union acpi_operand_object **return_object_ptr);
88 
89 static acpi_status
90 acpi_ns_repair_HID(struct acpi_evaluate_info *info,
91 		   union acpi_operand_object **return_object_ptr);
92 
93 static acpi_status
94 acpi_ns_repair_PRT(struct acpi_evaluate_info *info,
95 		   union acpi_operand_object **return_object_ptr);
96 
97 static acpi_status
98 acpi_ns_repair_PSS(struct acpi_evaluate_info *info,
99 		   union acpi_operand_object **return_object_ptr);
100 
101 static acpi_status
102 acpi_ns_repair_TSS(struct acpi_evaluate_info *info,
103 		   union acpi_operand_object **return_object_ptr);
104 
105 static acpi_status
106 acpi_ns_check_sorted_list(struct acpi_evaluate_info *info,
107 			  union acpi_operand_object *return_object,
108 			  u32 start_index,
109 			  u32 expected_count,
110 			  u32 sort_index,
111 			  u8 sort_direction, char *sort_key_name);
112 
113 /* Values for sort_direction above */
114 
115 #define ACPI_SORT_ASCENDING     0
116 #define ACPI_SORT_DESCENDING    1
117 
118 static void
119 acpi_ns_remove_element(union acpi_operand_object *obj_desc, u32 index);
120 
121 static void
122 acpi_ns_sort_list(union acpi_operand_object **elements,
123 		  u32 count, u32 index, u8 sort_direction);
124 
125 /*
126  * This table contains the names of the predefined methods for which we can
127  * perform more complex repairs.
128  *
129  * As necessary:
130  *
131  * _ALR: Sort the list ascending by ambient_illuminance
132  * _CID: Strings: uppercase all, remove any leading asterisk
133  * _CST: Sort the list ascending by C state type
134  * _FDE: Convert Buffer of BYTEs to a Buffer of DWORDs
135  * _GTM: Convert Buffer of BYTEs to a Buffer of DWORDs
136  * _HID: Strings: uppercase all, remove any leading asterisk
137  * _PRT: Fix reversed source_name and source_index
138  * _PSS: Sort the list descending by Power
139  * _TSS: Sort the list descending by Power
140  *
141  * Names that must be packages, but cannot be sorted:
142  *
143  * _BCL: Values are tied to the Package index where they appear, and cannot
144  * be moved or sorted. These index values are used for _BQC and _BCM.
145  * However, we can fix the case where a buffer is returned, by converting
146  * it to a Package of integers.
147  */
148 static const struct acpi_repair_info acpi_ns_repairable_names[] = {
149 	{"_ALR", acpi_ns_repair_ALR},
150 	{"_CID", acpi_ns_repair_CID},
151 	{"_CST", acpi_ns_repair_CST},
152 	{"_FDE", acpi_ns_repair_FDE},
153 	{"_GTM", acpi_ns_repair_FDE},	/* _GTM has same repair as _FDE */
154 	{"_HID", acpi_ns_repair_HID},
155 	{"_PRT", acpi_ns_repair_PRT},
156 	{"_PSS", acpi_ns_repair_PSS},
157 	{"_TSS", acpi_ns_repair_TSS},
158 	{{0, 0, 0, 0}, NULL}	/* Table terminator */
159 };
160 
161 #define ACPI_FDE_FIELD_COUNT        5
162 #define ACPI_FDE_BYTE_BUFFER_SIZE   5
163 #define ACPI_FDE_DWORD_BUFFER_SIZE  (ACPI_FDE_FIELD_COUNT * sizeof (u32))
164 
165 /******************************************************************************
166  *
167  * FUNCTION:    acpi_ns_complex_repairs
168  *
169  * PARAMETERS:  info                - Method execution information block
170  *              node                - Namespace node for the method/object
171  *              validate_status     - Original status of earlier validation
172  *              return_object_ptr   - Pointer to the object returned from the
173  *                                    evaluation of a method or object
174  *
175  * RETURN:      Status. AE_OK if repair was successful. If name is not
176  *              matched, validate_status is returned.
177  *
178  * DESCRIPTION: Attempt to repair/convert a return object of a type that was
179  *              not expected.
180  *
181  *****************************************************************************/
182 
183 acpi_status
184 acpi_ns_complex_repairs(struct acpi_evaluate_info *info,
185 			struct acpi_namespace_node *node,
186 			acpi_status validate_status,
187 			union acpi_operand_object **return_object_ptr)
188 {
189 	const struct acpi_repair_info *predefined;
190 	acpi_status status;
191 
192 	/* Check if this name is in the list of repairable names */
193 
194 	predefined = acpi_ns_match_complex_repair(node);
195 	if (!predefined) {
196 		return (validate_status);
197 	}
198 
199 	status = predefined->repair_function(info, return_object_ptr);
200 	return (status);
201 }
202 
203 /******************************************************************************
204  *
205  * FUNCTION:    acpi_ns_match_complex_repair
206  *
207  * PARAMETERS:  node                - Namespace node for the method/object
208  *
209  * RETURN:      Pointer to entry in repair table. NULL indicates not found.
210  *
211  * DESCRIPTION: Check an object name against the repairable object list.
212  *
213  *****************************************************************************/
214 
215 static const struct acpi_repair_info *acpi_ns_match_complex_repair(struct
216 								   acpi_namespace_node
217 								   *node)
218 {
219 	const struct acpi_repair_info *this_name;
220 
221 	/* Search info table for a repairable predefined method/object name */
222 
223 	this_name = acpi_ns_repairable_names;
224 	while (this_name->repair_function) {
225 		if (ACPI_COMPARE_NAME(node->name.ascii, this_name->name)) {
226 			return (this_name);
227 		}
228 
229 		this_name++;
230 	}
231 
232 	return (NULL);		/* Not found */
233 }
234 
235 /******************************************************************************
236  *
237  * FUNCTION:    acpi_ns_repair_ALR
238  *
239  * PARAMETERS:  info                - Method execution information block
240  *              return_object_ptr   - Pointer to the object returned from the
241  *                                    evaluation of a method or object
242  *
243  * RETURN:      Status. AE_OK if object is OK or was repaired successfully
244  *
245  * DESCRIPTION: Repair for the _ALR object. If necessary, sort the object list
246  *              ascending by the ambient illuminance values.
247  *
248  *****************************************************************************/
249 
250 static acpi_status
251 acpi_ns_repair_ALR(struct acpi_evaluate_info *info,
252 		   union acpi_operand_object **return_object_ptr)
253 {
254 	union acpi_operand_object *return_object = *return_object_ptr;
255 	acpi_status status;
256 
257 	status = acpi_ns_check_sorted_list(info, return_object, 0, 2, 1,
258 					   ACPI_SORT_ASCENDING,
259 					   "AmbientIlluminance");
260 
261 	return (status);
262 }
263 
264 /******************************************************************************
265  *
266  * FUNCTION:    acpi_ns_repair_FDE
267  *
268  * PARAMETERS:  info                - Method execution information block
269  *              return_object_ptr   - Pointer to the object returned from the
270  *                                    evaluation of a method or object
271  *
272  * RETURN:      Status. AE_OK if object is OK or was repaired successfully
273  *
274  * DESCRIPTION: Repair for the _FDE and _GTM objects. The expected return
275  *              value is a Buffer of 5 DWORDs. This function repairs a common
276  *              problem where the return value is a Buffer of BYTEs, not
277  *              DWORDs.
278  *
279  *****************************************************************************/
280 
281 static acpi_status
282 acpi_ns_repair_FDE(struct acpi_evaluate_info *info,
283 		   union acpi_operand_object **return_object_ptr)
284 {
285 	union acpi_operand_object *return_object = *return_object_ptr;
286 	union acpi_operand_object *buffer_object;
287 	u8 *byte_buffer;
288 	u32 *dword_buffer;
289 	u32 i;
290 
291 	ACPI_FUNCTION_NAME(ns_repair_FDE);
292 
293 	switch (return_object->common.type) {
294 	case ACPI_TYPE_BUFFER:
295 
296 		/* This is the expected type. Length should be (at least) 5 DWORDs */
297 
298 		if (return_object->buffer.length >= ACPI_FDE_DWORD_BUFFER_SIZE) {
299 			return (AE_OK);
300 		}
301 
302 		/* We can only repair if we have exactly 5 BYTEs */
303 
304 		if (return_object->buffer.length != ACPI_FDE_BYTE_BUFFER_SIZE) {
305 			ACPI_WARN_PREDEFINED((AE_INFO,
306 					      info->full_pathname,
307 					      info->node_flags,
308 					      "Incorrect return buffer length %u, expected %u",
309 					      return_object->buffer.length,
310 					      ACPI_FDE_DWORD_BUFFER_SIZE));
311 
312 			return (AE_AML_OPERAND_TYPE);
313 		}
314 
315 		/* Create the new (larger) buffer object */
316 
317 		buffer_object =
318 		    acpi_ut_create_buffer_object(ACPI_FDE_DWORD_BUFFER_SIZE);
319 		if (!buffer_object) {
320 			return (AE_NO_MEMORY);
321 		}
322 
323 		/* Expand each byte to a DWORD */
324 
325 		byte_buffer = return_object->buffer.pointer;
326 		dword_buffer = ACPI_CAST_PTR(u32,
327 					     buffer_object->buffer.pointer);
328 
329 		for (i = 0; i < ACPI_FDE_FIELD_COUNT; i++) {
330 			*dword_buffer = (u32) *byte_buffer;
331 			dword_buffer++;
332 			byte_buffer++;
333 		}
334 
335 		ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
336 				  "%s Expanded Byte Buffer to expected DWord Buffer\n",
337 				  info->full_pathname));
338 		break;
339 
340 	default:
341 
342 		return (AE_AML_OPERAND_TYPE);
343 	}
344 
345 	/* Delete the original return object, return the new buffer object */
346 
347 	acpi_ut_remove_reference(return_object);
348 	*return_object_ptr = buffer_object;
349 
350 	info->return_flags |= ACPI_OBJECT_REPAIRED;
351 	return (AE_OK);
352 }
353 
354 /******************************************************************************
355  *
356  * FUNCTION:    acpi_ns_repair_CID
357  *
358  * PARAMETERS:  info                - Method execution information block
359  *              return_object_ptr   - Pointer to the object returned from the
360  *                                    evaluation of a method or object
361  *
362  * RETURN:      Status. AE_OK if object is OK or was repaired successfully
363  *
364  * DESCRIPTION: Repair for the _CID object. If a string, ensure that all
365  *              letters are uppercase and that there is no leading asterisk.
366  *              If a Package, ensure same for all string elements.
367  *
368  *****************************************************************************/
369 
370 static acpi_status
371 acpi_ns_repair_CID(struct acpi_evaluate_info *info,
372 		   union acpi_operand_object **return_object_ptr)
373 {
374 	acpi_status status;
375 	union acpi_operand_object *return_object = *return_object_ptr;
376 	union acpi_operand_object **element_ptr;
377 	union acpi_operand_object *original_element;
378 	u16 original_ref_count;
379 	u32 i;
380 
381 	/* Check for _CID as a simple string */
382 
383 	if (return_object->common.type == ACPI_TYPE_STRING) {
384 		status = acpi_ns_repair_HID(info, return_object_ptr);
385 		return (status);
386 	}
387 
388 	/* Exit if not a Package */
389 
390 	if (return_object->common.type != ACPI_TYPE_PACKAGE) {
391 		return (AE_OK);
392 	}
393 
394 	/* Examine each element of the _CID package */
395 
396 	element_ptr = return_object->package.elements;
397 	for (i = 0; i < return_object->package.count; i++) {
398 		original_element = *element_ptr;
399 		original_ref_count = original_element->common.reference_count;
400 
401 		status = acpi_ns_repair_HID(info, element_ptr);
402 		if (ACPI_FAILURE(status)) {
403 			return (status);
404 		}
405 
406 		if (original_element != *element_ptr) {
407 
408 			/* Update reference count of new object */
409 
410 			(*element_ptr)->common.reference_count =
411 			    original_ref_count;
412 		}
413 
414 		element_ptr++;
415 	}
416 
417 	return (AE_OK);
418 }
419 
420 /******************************************************************************
421  *
422  * FUNCTION:    acpi_ns_repair_CST
423  *
424  * PARAMETERS:  info                - Method execution information block
425  *              return_object_ptr   - Pointer to the object returned from the
426  *                                    evaluation of a method or object
427  *
428  * RETURN:      Status. AE_OK if object is OK or was repaired successfully
429  *
430  * DESCRIPTION: Repair for the _CST object:
431  *              1. Sort the list ascending by C state type
432  *              2. Ensure type cannot be zero
433  *              3. A subpackage count of zero means _CST is meaningless
434  *              4. Count must match the number of C state subpackages
435  *
436  *****************************************************************************/
437 
438 static acpi_status
439 acpi_ns_repair_CST(struct acpi_evaluate_info *info,
440 		   union acpi_operand_object **return_object_ptr)
441 {
442 	union acpi_operand_object *return_object = *return_object_ptr;
443 	union acpi_operand_object **outer_elements;
444 	u32 outer_element_count;
445 	union acpi_operand_object *obj_desc;
446 	acpi_status status;
447 	u8 removing;
448 	u32 i;
449 
450 	ACPI_FUNCTION_NAME(ns_repair_CST);
451 
452 	/*
453 	 * Check if the C-state type values are proportional.
454 	 */
455 	outer_element_count = return_object->package.count - 1;
456 	i = 0;
457 	while (i < outer_element_count) {
458 		outer_elements = &return_object->package.elements[i + 1];
459 		removing = FALSE;
460 
461 		if ((*outer_elements)->package.count == 0) {
462 			ACPI_WARN_PREDEFINED((AE_INFO,
463 					      info->full_pathname,
464 					      info->node_flags,
465 					      "SubPackage[%u] - removing entry due to zero count",
466 					      i));
467 			removing = TRUE;
468 			goto remove_element;
469 		}
470 
471 		obj_desc = (*outer_elements)->package.elements[1];	/* Index1 = Type */
472 		if ((u32)obj_desc->integer.value == 0) {
473 			ACPI_WARN_PREDEFINED((AE_INFO,
474 					      info->full_pathname,
475 					      info->node_flags,
476 					      "SubPackage[%u] - removing entry due to invalid Type(0)",
477 					      i));
478 			removing = TRUE;
479 		}
480 
481 remove_element:
482 		if (removing) {
483 			acpi_ns_remove_element(return_object, i + 1);
484 			outer_element_count--;
485 		} else {
486 			i++;
487 		}
488 	}
489 
490 	/* Update top-level package count, Type "Integer" checked elsewhere */
491 
492 	obj_desc = return_object->package.elements[0];
493 	obj_desc->integer.value = outer_element_count;
494 
495 	/*
496 	 * Entries (subpackages) in the _CST Package must be sorted by the
497 	 * C-state type, in ascending order.
498 	 */
499 	status = acpi_ns_check_sorted_list(info, return_object, 1, 4, 1,
500 					   ACPI_SORT_ASCENDING, "C-State Type");
501 	if (ACPI_FAILURE(status)) {
502 		return (status);
503 	}
504 
505 	return (AE_OK);
506 }
507 
508 /******************************************************************************
509  *
510  * FUNCTION:    acpi_ns_repair_HID
511  *
512  * PARAMETERS:  info                - Method execution information block
513  *              return_object_ptr   - Pointer to the object returned from the
514  *                                    evaluation of a method or object
515  *
516  * RETURN:      Status. AE_OK if object is OK or was repaired successfully
517  *
518  * DESCRIPTION: Repair for the _HID object. If a string, ensure that all
519  *              letters are uppercase and that there is no leading asterisk.
520  *
521  *****************************************************************************/
522 
523 static acpi_status
524 acpi_ns_repair_HID(struct acpi_evaluate_info *info,
525 		   union acpi_operand_object **return_object_ptr)
526 {
527 	union acpi_operand_object *return_object = *return_object_ptr;
528 	union acpi_operand_object *new_string;
529 	char *source;
530 	char *dest;
531 
532 	ACPI_FUNCTION_NAME(ns_repair_HID);
533 
534 	/* We only care about string _HID objects (not integers) */
535 
536 	if (return_object->common.type != ACPI_TYPE_STRING) {
537 		return (AE_OK);
538 	}
539 
540 	if (return_object->string.length == 0) {
541 		ACPI_WARN_PREDEFINED((AE_INFO,
542 				      info->full_pathname, info->node_flags,
543 				      "Invalid zero-length _HID or _CID string"));
544 
545 		/* Return AE_OK anyway, let driver handle it */
546 
547 		info->return_flags |= ACPI_OBJECT_REPAIRED;
548 		return (AE_OK);
549 	}
550 
551 	/* It is simplest to always create a new string object */
552 
553 	new_string = acpi_ut_create_string_object(return_object->string.length);
554 	if (!new_string) {
555 		return (AE_NO_MEMORY);
556 	}
557 
558 	/*
559 	 * Remove a leading asterisk if present. For some unknown reason, there
560 	 * are many machines in the field that contains IDs like this.
561 	 *
562 	 * Examples: "*PNP0C03", "*ACPI0003"
563 	 */
564 	source = return_object->string.pointer;
565 	if (*source == '*') {
566 		source++;
567 		new_string->string.length--;
568 
569 		ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
570 				  "%s: Removed invalid leading asterisk\n",
571 				  info->full_pathname));
572 	}
573 
574 	/*
575 	 * Copy and uppercase the string. From the ACPI 5.0 specification:
576 	 *
577 	 * A valid PNP ID must be of the form "AAA####" where A is an uppercase
578 	 * letter and # is a hex digit. A valid ACPI ID must be of the form
579 	 * "NNNN####" where N is an uppercase letter or decimal digit, and
580 	 * # is a hex digit.
581 	 */
582 	for (dest = new_string->string.pointer; *source; dest++, source++) {
583 		*dest = (char)toupper((int)*source);
584 	}
585 
586 	acpi_ut_remove_reference(return_object);
587 	*return_object_ptr = new_string;
588 	return (AE_OK);
589 }
590 
591 /******************************************************************************
592  *
593  * FUNCTION:    acpi_ns_repair_PRT
594  *
595  * PARAMETERS:  info                - Method execution information block
596  *              return_object_ptr   - Pointer to the object returned from the
597  *                                    evaluation of a method or object
598  *
599  * RETURN:      Status. AE_OK if object is OK or was repaired successfully
600  *
601  * DESCRIPTION: Repair for the _PRT object. If necessary, fix reversed
602  *              source_name and source_index field, a common BIOS bug.
603  *
604  *****************************************************************************/
605 
606 static acpi_status
607 acpi_ns_repair_PRT(struct acpi_evaluate_info *info,
608 		   union acpi_operand_object **return_object_ptr)
609 {
610 	union acpi_operand_object *package_object = *return_object_ptr;
611 	union acpi_operand_object **top_object_list;
612 	union acpi_operand_object **sub_object_list;
613 	union acpi_operand_object *obj_desc;
614 	union acpi_operand_object *sub_package;
615 	u32 element_count;
616 	u32 index;
617 
618 	/* Each element in the _PRT package is a subpackage */
619 
620 	top_object_list = package_object->package.elements;
621 	element_count = package_object->package.count;
622 
623 	/* Examine each subpackage */
624 
625 	for (index = 0; index < element_count; index++, top_object_list++) {
626 		sub_package = *top_object_list;
627 		sub_object_list = sub_package->package.elements;
628 
629 		/* Check for minimum required element count */
630 
631 		if (sub_package->package.count < 4) {
632 			continue;
633 		}
634 
635 		/*
636 		 * If the BIOS has erroneously reversed the _PRT source_name (index 2)
637 		 * and the source_index (index 3), fix it. _PRT is important enough to
638 		 * workaround this BIOS error. This also provides compatibility with
639 		 * other ACPI implementations.
640 		 */
641 		obj_desc = sub_object_list[3];
642 		if (!obj_desc || (obj_desc->common.type != ACPI_TYPE_INTEGER)) {
643 			sub_object_list[3] = sub_object_list[2];
644 			sub_object_list[2] = obj_desc;
645 			info->return_flags |= ACPI_OBJECT_REPAIRED;
646 
647 			ACPI_WARN_PREDEFINED((AE_INFO,
648 					      info->full_pathname,
649 					      info->node_flags,
650 					      "PRT[%X]: Fixed reversed SourceName and SourceIndex",
651 					      index));
652 		}
653 	}
654 
655 	return (AE_OK);
656 }
657 
658 /******************************************************************************
659  *
660  * FUNCTION:    acpi_ns_repair_PSS
661  *
662  * PARAMETERS:  info                - Method execution information block
663  *              return_object_ptr   - Pointer to the object returned from the
664  *                                    evaluation of a method or object
665  *
666  * RETURN:      Status. AE_OK if object is OK or was repaired successfully
667  *
668  * DESCRIPTION: Repair for the _PSS object. If necessary, sort the object list
669  *              by the CPU frequencies. Check that the power dissipation values
670  *              are all proportional to CPU frequency (i.e., sorting by
671  *              frequency should be the same as sorting by power.)
672  *
673  *****************************************************************************/
674 
675 static acpi_status
676 acpi_ns_repair_PSS(struct acpi_evaluate_info *info,
677 		   union acpi_operand_object **return_object_ptr)
678 {
679 	union acpi_operand_object *return_object = *return_object_ptr;
680 	union acpi_operand_object **outer_elements;
681 	u32 outer_element_count;
682 	union acpi_operand_object **elements;
683 	union acpi_operand_object *obj_desc;
684 	u32 previous_value;
685 	acpi_status status;
686 	u32 i;
687 
688 	/*
689 	 * Entries (subpackages) in the _PSS Package must be sorted by power
690 	 * dissipation, in descending order. If it appears that the list is
691 	 * incorrectly sorted, sort it. We sort by cpu_frequency, since this
692 	 * should be proportional to the power.
693 	 */
694 	status = acpi_ns_check_sorted_list(info, return_object, 0, 6, 0,
695 					   ACPI_SORT_DESCENDING,
696 					   "CpuFrequency");
697 	if (ACPI_FAILURE(status)) {
698 		return (status);
699 	}
700 
701 	/*
702 	 * We now know the list is correctly sorted by CPU frequency. Check if
703 	 * the power dissipation values are proportional.
704 	 */
705 	previous_value = ACPI_UINT32_MAX;
706 	outer_elements = return_object->package.elements;
707 	outer_element_count = return_object->package.count;
708 
709 	for (i = 0; i < outer_element_count; i++) {
710 		elements = (*outer_elements)->package.elements;
711 		obj_desc = elements[1];	/* Index1 = power_dissipation */
712 
713 		if ((u32)obj_desc->integer.value > previous_value) {
714 			ACPI_WARN_PREDEFINED((AE_INFO,
715 					      info->full_pathname,
716 					      info->node_flags,
717 					      "SubPackage[%u,%u] - suspicious power dissipation values",
718 					      i - 1, i));
719 		}
720 
721 		previous_value = (u32) obj_desc->integer.value;
722 		outer_elements++;
723 	}
724 
725 	return (AE_OK);
726 }
727 
728 /******************************************************************************
729  *
730  * FUNCTION:    acpi_ns_repair_TSS
731  *
732  * PARAMETERS:  info                - Method execution information block
733  *              return_object_ptr   - Pointer to the object returned from the
734  *                                    evaluation of a method or object
735  *
736  * RETURN:      Status. AE_OK if object is OK or was repaired successfully
737  *
738  * DESCRIPTION: Repair for the _TSS object. If necessary, sort the object list
739  *              descending by the power dissipation values.
740  *
741  *****************************************************************************/
742 
743 static acpi_status
744 acpi_ns_repair_TSS(struct acpi_evaluate_info *info,
745 		   union acpi_operand_object **return_object_ptr)
746 {
747 	union acpi_operand_object *return_object = *return_object_ptr;
748 	acpi_status status;
749 	struct acpi_namespace_node *node;
750 
751 	/*
752 	 * We can only sort the _TSS return package if there is no _PSS in the
753 	 * same scope. This is because if _PSS is present, the ACPI specification
754 	 * dictates that the _TSS Power Dissipation field is to be ignored, and
755 	 * therefore some BIOSs leave garbage values in the _TSS Power field(s).
756 	 * In this case, it is best to just return the _TSS package as-is.
757 	 * (May, 2011)
758 	 */
759 	status = acpi_ns_get_node(info->node, "^_PSS",
760 				  ACPI_NS_NO_UPSEARCH, &node);
761 	if (ACPI_SUCCESS(status)) {
762 		return (AE_OK);
763 	}
764 
765 	status = acpi_ns_check_sorted_list(info, return_object, 0, 5, 1,
766 					   ACPI_SORT_DESCENDING,
767 					   "PowerDissipation");
768 
769 	return (status);
770 }
771 
772 /******************************************************************************
773  *
774  * FUNCTION:    acpi_ns_check_sorted_list
775  *
776  * PARAMETERS:  info                - Method execution information block
777  *              return_object       - Pointer to the top-level returned object
778  *              start_index         - Index of the first subpackage
779  *              expected_count      - Minimum length of each subpackage
780  *              sort_index          - Subpackage entry to sort on
781  *              sort_direction      - Ascending or descending
782  *              sort_key_name       - Name of the sort_index field
783  *
784  * RETURN:      Status. AE_OK if the list is valid and is sorted correctly or
785  *              has been repaired by sorting the list.
786  *
787  * DESCRIPTION: Check if the package list is valid and sorted correctly by the
788  *              sort_index. If not, then sort the list.
789  *
790  *****************************************************************************/
791 
792 static acpi_status
793 acpi_ns_check_sorted_list(struct acpi_evaluate_info *info,
794 			  union acpi_operand_object *return_object,
795 			  u32 start_index,
796 			  u32 expected_count,
797 			  u32 sort_index,
798 			  u8 sort_direction, char *sort_key_name)
799 {
800 	u32 outer_element_count;
801 	union acpi_operand_object **outer_elements;
802 	union acpi_operand_object **elements;
803 	union acpi_operand_object *obj_desc;
804 	u32 i;
805 	u32 previous_value;
806 
807 	ACPI_FUNCTION_NAME(ns_check_sorted_list);
808 
809 	/* The top-level object must be a package */
810 
811 	if (return_object->common.type != ACPI_TYPE_PACKAGE) {
812 		return (AE_AML_OPERAND_TYPE);
813 	}
814 
815 	/*
816 	 * NOTE: assumes list of subpackages contains no NULL elements.
817 	 * Any NULL elements should have been removed by earlier call
818 	 * to acpi_ns_remove_null_elements.
819 	 */
820 	outer_element_count = return_object->package.count;
821 	if (!outer_element_count || start_index >= outer_element_count) {
822 		return (AE_AML_PACKAGE_LIMIT);
823 	}
824 
825 	outer_elements = &return_object->package.elements[start_index];
826 	outer_element_count -= start_index;
827 
828 	previous_value = 0;
829 	if (sort_direction == ACPI_SORT_DESCENDING) {
830 		previous_value = ACPI_UINT32_MAX;
831 	}
832 
833 	/* Examine each subpackage */
834 
835 	for (i = 0; i < outer_element_count; i++) {
836 
837 		/* Each element of the top-level package must also be a package */
838 
839 		if ((*outer_elements)->common.type != ACPI_TYPE_PACKAGE) {
840 			return (AE_AML_OPERAND_TYPE);
841 		}
842 
843 		/* Each subpackage must have the minimum length */
844 
845 		if ((*outer_elements)->package.count < expected_count) {
846 			return (AE_AML_PACKAGE_LIMIT);
847 		}
848 
849 		elements = (*outer_elements)->package.elements;
850 		obj_desc = elements[sort_index];
851 
852 		if (obj_desc->common.type != ACPI_TYPE_INTEGER) {
853 			return (AE_AML_OPERAND_TYPE);
854 		}
855 
856 		/*
857 		 * The list must be sorted in the specified order. If we detect a
858 		 * discrepancy, sort the entire list.
859 		 */
860 		if (((sort_direction == ACPI_SORT_ASCENDING) &&
861 		     (obj_desc->integer.value < previous_value)) ||
862 		    ((sort_direction == ACPI_SORT_DESCENDING) &&
863 		     (obj_desc->integer.value > previous_value))) {
864 			acpi_ns_sort_list(&return_object->package.
865 					  elements[start_index],
866 					  outer_element_count, sort_index,
867 					  sort_direction);
868 
869 			info->return_flags |= ACPI_OBJECT_REPAIRED;
870 
871 			ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
872 					  "%s: Repaired unsorted list - now sorted by %s\n",
873 					  info->full_pathname, sort_key_name));
874 			return (AE_OK);
875 		}
876 
877 		previous_value = (u32) obj_desc->integer.value;
878 		outer_elements++;
879 	}
880 
881 	return (AE_OK);
882 }
883 
884 /******************************************************************************
885  *
886  * FUNCTION:    acpi_ns_sort_list
887  *
888  * PARAMETERS:  elements            - Package object element list
889  *              count               - Element count for above
890  *              index               - Sort by which package element
891  *              sort_direction      - Ascending or Descending sort
892  *
893  * RETURN:      None
894  *
895  * DESCRIPTION: Sort the objects that are in a package element list.
896  *
897  * NOTE: Assumes that all NULL elements have been removed from the package,
898  *       and that all elements have been verified to be of type Integer.
899  *
900  *****************************************************************************/
901 
902 static void
903 acpi_ns_sort_list(union acpi_operand_object **elements,
904 		  u32 count, u32 index, u8 sort_direction)
905 {
906 	union acpi_operand_object *obj_desc1;
907 	union acpi_operand_object *obj_desc2;
908 	union acpi_operand_object *temp_obj;
909 	u32 i;
910 	u32 j;
911 
912 	/* Simple bubble sort */
913 
914 	for (i = 1; i < count; i++) {
915 		for (j = (count - 1); j >= i; j--) {
916 			obj_desc1 = elements[j - 1]->package.elements[index];
917 			obj_desc2 = elements[j]->package.elements[index];
918 
919 			if (((sort_direction == ACPI_SORT_ASCENDING) &&
920 			     (obj_desc1->integer.value >
921 			      obj_desc2->integer.value))
922 			    || ((sort_direction == ACPI_SORT_DESCENDING)
923 				&& (obj_desc1->integer.value <
924 				    obj_desc2->integer.value))) {
925 				temp_obj = elements[j - 1];
926 				elements[j - 1] = elements[j];
927 				elements[j] = temp_obj;
928 			}
929 		}
930 	}
931 }
932 
933 /******************************************************************************
934  *
935  * FUNCTION:    acpi_ns_remove_element
936  *
937  * PARAMETERS:  obj_desc            - Package object element list
938  *              index               - Index of element to remove
939  *
940  * RETURN:      None
941  *
942  * DESCRIPTION: Remove the requested element of a package and delete it.
943  *
944  *****************************************************************************/
945 
946 static void
947 acpi_ns_remove_element(union acpi_operand_object *obj_desc, u32 index)
948 {
949 	union acpi_operand_object **source;
950 	union acpi_operand_object **dest;
951 	u32 count;
952 	u32 new_count;
953 	u32 i;
954 
955 	ACPI_FUNCTION_NAME(ns_remove_element);
956 
957 	count = obj_desc->package.count;
958 	new_count = count - 1;
959 
960 	source = obj_desc->package.elements;
961 	dest = source;
962 
963 	/* Examine all elements of the package object, remove matched index */
964 
965 	for (i = 0; i < count; i++) {
966 		if (i == index) {
967 			acpi_ut_remove_reference(*source);	/* Remove one ref for being in pkg */
968 			acpi_ut_remove_reference(*source);
969 		} else {
970 			*dest = *source;
971 			dest++;
972 		}
973 
974 		source++;
975 	}
976 
977 	/* NULL terminate list and update the package count */
978 
979 	*dest = NULL;
980 	obj_desc->package.count = new_count;
981 }
982