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