1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44#include <linux/module.h>
45
46#include <acpi/acpi.h>
47#include "accommon.h"
48#include "acnamesp.h"
49
50#define _COMPONENT ACPI_UTILITIES
51ACPI_MODULE_NAME("utmisc")
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66const char *acpi_ut_validate_exception(acpi_status status)
67{
68 u32 sub_status;
69 const char *exception = NULL;
70
71 ACPI_FUNCTION_ENTRY();
72
73
74
75
76 sub_status = (status & ~AE_CODE_MASK);
77
78 switch (status & AE_CODE_MASK) {
79 case AE_CODE_ENVIRONMENTAL:
80
81 if (sub_status <= AE_CODE_ENV_MAX) {
82 exception = acpi_gbl_exception_names_env[sub_status];
83 }
84 break;
85
86 case AE_CODE_PROGRAMMER:
87
88 if (sub_status <= AE_CODE_PGM_MAX) {
89 exception = acpi_gbl_exception_names_pgm[sub_status];
90 }
91 break;
92
93 case AE_CODE_ACPI_TABLES:
94
95 if (sub_status <= AE_CODE_TBL_MAX) {
96 exception = acpi_gbl_exception_names_tbl[sub_status];
97 }
98 break;
99
100 case AE_CODE_AML:
101
102 if (sub_status <= AE_CODE_AML_MAX) {
103 exception = acpi_gbl_exception_names_aml[sub_status];
104 }
105 break;
106
107 case AE_CODE_CONTROL:
108
109 if (sub_status <= AE_CODE_CTRL_MAX) {
110 exception = acpi_gbl_exception_names_ctrl[sub_status];
111 }
112 break;
113
114 default:
115 break;
116 }
117
118 return (ACPI_CAST_PTR(const char, exception));
119}
120
121
122
123
124
125
126
127
128
129
130
131
132
133u8 acpi_ut_is_pci_root_bridge(char *id)
134{
135
136
137
138
139
140 if (!(ACPI_STRCMP(id,
141 PCI_ROOT_HID_STRING)) ||
142 !(ACPI_STRCMP(id, PCI_EXPRESS_ROOT_HID_STRING))) {
143 return (TRUE);
144 }
145
146 return (FALSE);
147}
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163u8 acpi_ut_is_aml_table(struct acpi_table_header *table)
164{
165
166
167
168 if (ACPI_COMPARE_NAME(table->signature, ACPI_SIG_DSDT) ||
169 ACPI_COMPARE_NAME(table->signature, ACPI_SIG_PSDT) ||
170 ACPI_COMPARE_NAME(table->signature, ACPI_SIG_SSDT)) {
171 return (TRUE);
172 }
173
174 return (FALSE);
175}
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191acpi_status acpi_ut_allocate_owner_id(acpi_owner_id * owner_id)
192{
193 u32 i;
194 u32 j;
195 u32 k;
196 acpi_status status;
197
198 ACPI_FUNCTION_TRACE(ut_allocate_owner_id);
199
200
201
202 if (*owner_id) {
203 ACPI_ERROR((AE_INFO, "Owner ID [0x%2.2X] already exists",
204 *owner_id));
205 return_ACPI_STATUS(AE_ALREADY_EXISTS);
206 }
207
208
209
210 status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
211 if (ACPI_FAILURE(status)) {
212 return_ACPI_STATUS(status);
213 }
214
215
216
217
218
219
220 for (i = 0, j = acpi_gbl_last_owner_id_index;
221 i < (ACPI_NUM_OWNERID_MASKS + 1); i++, j++) {
222 if (j >= ACPI_NUM_OWNERID_MASKS) {
223 j = 0;
224 }
225
226 for (k = acpi_gbl_next_owner_id_offset; k < 32; k++) {
227 if (acpi_gbl_owner_id_mask[j] == ACPI_UINT32_MAX) {
228
229
230
231 break;
232 }
233
234 if (!(acpi_gbl_owner_id_mask[j] & (1 << k))) {
235
236
237
238
239
240 acpi_gbl_owner_id_mask[j] |= (1 << k);
241
242 acpi_gbl_last_owner_id_index = (u8) j;
243 acpi_gbl_next_owner_id_offset = (u8) (k + 1);
244
245
246
247
248
249
250
251 *owner_id =
252 (acpi_owner_id) ((k + 1) + ACPI_MUL_32(j));
253
254 ACPI_DEBUG_PRINT((ACPI_DB_VALUES,
255 "Allocated OwnerId: %2.2X\n",
256 (unsigned int)*owner_id));
257 goto exit;
258 }
259 }
260
261 acpi_gbl_next_owner_id_offset = 0;
262 }
263
264
265
266
267
268
269
270
271
272
273
274 status = AE_OWNER_ID_LIMIT;
275 ACPI_ERROR((AE_INFO,
276 "Could not allocate new OwnerId (255 max), AE_OWNER_ID_LIMIT"));
277
278 exit:
279 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
280 return_ACPI_STATUS(status);
281}
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297void acpi_ut_release_owner_id(acpi_owner_id * owner_id_ptr)
298{
299 acpi_owner_id owner_id = *owner_id_ptr;
300 acpi_status status;
301 u32 index;
302 u32 bit;
303
304 ACPI_FUNCTION_TRACE_U32(ut_release_owner_id, owner_id);
305
306
307
308 *owner_id_ptr = 0;
309
310
311
312 if (owner_id == 0) {
313 ACPI_ERROR((AE_INFO, "Invalid OwnerId: 0x%2.2X", owner_id));
314 return_VOID;
315 }
316
317
318
319 status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
320 if (ACPI_FAILURE(status)) {
321 return_VOID;
322 }
323
324
325
326 owner_id--;
327
328
329
330 index = ACPI_DIV_32(owner_id);
331 bit = 1 << ACPI_MOD_32(owner_id);
332
333
334
335 if (acpi_gbl_owner_id_mask[index] & bit) {
336 acpi_gbl_owner_id_mask[index] ^= bit;
337 } else {
338 ACPI_ERROR((AE_INFO,
339 "Release of non-allocated OwnerId: 0x%2.2X",
340 owner_id + 1));
341 }
342
343 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
344 return_VOID;
345}
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361void acpi_ut_strupr(char *src_string)
362{
363 char *string;
364
365 ACPI_FUNCTION_ENTRY();
366
367 if (!src_string) {
368 return;
369 }
370
371
372
373 for (string = src_string; *string; string++) {
374 *string = (char)ACPI_TOUPPER(*string);
375 }
376
377 return;
378}
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394void acpi_ut_print_string(char *string, u8 max_length)
395{
396 u32 i;
397
398 if (!string) {
399 acpi_os_printf("<\"NULL STRING PTR\">");
400 return;
401 }
402
403 acpi_os_printf("\"");
404 for (i = 0; string[i] && (i < max_length); i++) {
405
406
407
408 switch (string[i]) {
409 case 0x07:
410 acpi_os_printf("\\a");
411 break;
412
413 case 0x08:
414 acpi_os_printf("\\b");
415 break;
416
417 case 0x0C:
418 acpi_os_printf("\\f");
419 break;
420
421 case 0x0A:
422 acpi_os_printf("\\n");
423 break;
424
425 case 0x0D:
426 acpi_os_printf("\\r");
427 break;
428
429 case 0x09:
430 acpi_os_printf("\\t");
431 break;
432
433 case 0x0B:
434 acpi_os_printf("\\v");
435 break;
436
437 case '\'':
438 case '\"':
439 case '\\':
440 acpi_os_printf("\\%c", (int)string[i]);
441 break;
442
443 default:
444
445
446
447 if (ACPI_IS_PRINT(string[i])) {
448
449
450 acpi_os_printf("%c", (int)string[i]);
451 } else {
452
453
454 acpi_os_printf("\\x%2.2X", (s32) string[i]);
455 }
456 break;
457 }
458 }
459 acpi_os_printf("\"");
460
461 if (i == max_length && string[i]) {
462 acpi_os_printf("...");
463 }
464}
465
466
467
468
469
470
471
472
473
474
475
476
477
478u32 acpi_ut_dword_byte_swap(u32 value)
479{
480 union {
481 u32 value;
482 u8 bytes[4];
483 } out;
484 union {
485 u32 value;
486 u8 bytes[4];
487 } in;
488
489 ACPI_FUNCTION_ENTRY();
490
491 in.value = value;
492
493 out.bytes[0] = in.bytes[3];
494 out.bytes[1] = in.bytes[2];
495 out.bytes[2] = in.bytes[1];
496 out.bytes[3] = in.bytes[0];
497
498 return (out.value);
499}
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516void acpi_ut_set_integer_width(u8 revision)
517{
518
519 if (revision < 2) {
520
521
522
523 acpi_gbl_integer_bit_width = 32;
524 acpi_gbl_integer_nybble_width = 8;
525 acpi_gbl_integer_byte_width = 4;
526 } else {
527
528
529 acpi_gbl_integer_bit_width = 64;
530 acpi_gbl_integer_nybble_width = 16;
531 acpi_gbl_integer_byte_width = 8;
532 }
533}
534
535#ifdef ACPI_DEBUG_OUTPUT
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551void
552acpi_ut_display_init_pathname(u8 type,
553 struct acpi_namespace_node *obj_handle,
554 char *path)
555{
556 acpi_status status;
557 struct acpi_buffer buffer;
558
559 ACPI_FUNCTION_ENTRY();
560
561
562
563 if (!(acpi_dbg_level & ACPI_LV_INIT_NAMES)) {
564 return;
565 }
566
567
568
569 buffer.length = ACPI_ALLOCATE_LOCAL_BUFFER;
570 status = acpi_ns_handle_to_pathname(obj_handle, &buffer);
571 if (ACPI_FAILURE(status)) {
572 return;
573 }
574
575
576
577 switch (type) {
578 case ACPI_TYPE_METHOD:
579 acpi_os_printf("Executing ");
580 break;
581
582 default:
583 acpi_os_printf("Initializing ");
584 break;
585 }
586
587
588
589 acpi_os_printf("%-12s %s",
590 acpi_ut_get_type_name(type), (char *)buffer.pointer);
591
592
593
594 if (path) {
595 acpi_os_printf(".%s", path);
596 }
597 acpi_os_printf("\n");
598
599 ACPI_FREE(buffer.pointer);
600}
601#endif
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621u8 acpi_ut_valid_acpi_char(char character, u32 position)
622{
623
624 if (!((character >= 'A' && character <= 'Z') ||
625 (character >= '0' && character <= '9') || (character == '_'))) {
626
627
628
629 if (character == '!' && position == 3) {
630 return (TRUE);
631 }
632
633 return (FALSE);
634 }
635
636 return (TRUE);
637}
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654u8 acpi_ut_valid_acpi_name(u32 name)
655{
656 u32 i;
657
658 ACPI_FUNCTION_ENTRY();
659
660 for (i = 0; i < ACPI_NAME_SIZE; i++) {
661 if (!acpi_ut_valid_acpi_char
662 ((ACPI_CAST_PTR(char, &name))[i], i)) {
663 return (FALSE);
664 }
665 }
666
667 return (TRUE);
668}
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683acpi_name acpi_ut_repair_name(char *name)
684{
685 u32 i;
686 char new_name[ACPI_NAME_SIZE];
687
688 for (i = 0; i < ACPI_NAME_SIZE; i++) {
689 new_name[i] = name[i];
690
691
692
693
694
695
696 if (!acpi_ut_valid_acpi_char(name[i], i)) {
697 new_name[i] = '*';
698 }
699 }
700
701 return (*(u32 *) new_name);
702}
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722acpi_status acpi_ut_strtoul64(char *string, u32 base, u64 * ret_integer)
723{
724 u32 this_digit = 0;
725 u64 return_value = 0;
726 u64 quotient;
727 u64 dividend;
728 u32 to_integer_op = (base == ACPI_ANY_BASE);
729 u32 mode32 = (acpi_gbl_integer_byte_width == 4);
730 u8 valid_digits = 0;
731 u8 sign_of0x = 0;
732 u8 term = 0;
733
734 ACPI_FUNCTION_TRACE_STR(ut_stroul64, string);
735
736 switch (base) {
737 case ACPI_ANY_BASE:
738 case 16:
739 break;
740
741 default:
742
743 return_ACPI_STATUS(AE_BAD_PARAMETER);
744 }
745
746 if (!string) {
747 goto error_exit;
748 }
749
750
751
752 while ((*string) && (ACPI_IS_SPACE(*string) || *string == '\t')) {
753 string++;
754 }
755
756 if (to_integer_op) {
757
758
759
760
761 if ((*string == '0') && (ACPI_TOLOWER(*(string + 1)) == 'x')) {
762 sign_of0x = 1;
763 base = 16;
764
765
766 string += 2;
767 } else {
768 base = 10;
769 }
770 }
771
772
773
774 if (!(*string) || ACPI_IS_SPACE(*string) || *string == '\t') {
775 if (to_integer_op) {
776 goto error_exit;
777 } else {
778 goto all_done;
779 }
780 }
781
782
783
784
785
786 dividend = (mode32) ? ACPI_UINT32_MAX : ACPI_UINT64_MAX;
787
788
789
790 while (*string) {
791 if (ACPI_IS_DIGIT(*string)) {
792
793
794
795 this_digit = ((u8) * string) - '0';
796 } else if (base == 10) {
797
798
799
800 term = 1;
801 } else {
802 this_digit = (u8) ACPI_TOUPPER(*string);
803 if (ACPI_IS_XDIGIT((char)this_digit)) {
804
805
806
807 this_digit = this_digit - 'A' + 10;
808 } else {
809 term = 1;
810 }
811 }
812
813 if (term) {
814 if (to_integer_op) {
815 goto error_exit;
816 } else {
817 break;
818 }
819 } else if ((valid_digits == 0) && (this_digit == 0)
820 && !sign_of0x) {
821
822
823 string++;
824 continue;
825 }
826
827 valid_digits++;
828
829 if (sign_of0x && ((valid_digits > 16)
830 || ((valid_digits > 8) && mode32))) {
831
832
833
834
835
836 goto error_exit;
837 }
838
839
840
841 (void)acpi_ut_short_divide((dividend - (u64) this_digit),
842 base, "ient, NULL);
843
844 if (return_value > quotient) {
845 if (to_integer_op) {
846 goto error_exit;
847 } else {
848 break;
849 }
850 }
851
852 return_value *= base;
853 return_value += this_digit;
854 string++;
855 }
856
857
858
859 all_done:
860
861 ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n",
862 ACPI_FORMAT_UINT64(return_value)));
863
864 *ret_integer = return_value;
865 return_ACPI_STATUS(AE_OK);
866
867 error_exit:
868
869
870 if (base == 10) {
871 return_ACPI_STATUS(AE_BAD_DECIMAL_CONSTANT);
872 } else {
873 return_ACPI_STATUS(AE_BAD_HEX_CONSTANT);
874 }
875}
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891acpi_status
892acpi_ut_create_update_state_and_push(union acpi_operand_object *object,
893 u16 action,
894 union acpi_generic_state **state_list)
895{
896 union acpi_generic_state *state;
897
898 ACPI_FUNCTION_ENTRY();
899
900
901
902 if (!object) {
903 return (AE_OK);
904 }
905
906 state = acpi_ut_create_update_state(object, action);
907 if (!state) {
908 return (AE_NO_MEMORY);
909 }
910
911 acpi_ut_push_generic_state(state_list, state);
912 return (AE_OK);
913}
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930acpi_status
931acpi_ut_walk_package_tree(union acpi_operand_object * source_object,
932 void *target_object,
933 acpi_pkg_callback walk_callback, void *context)
934{
935 acpi_status status = AE_OK;
936 union acpi_generic_state *state_list = NULL;
937 union acpi_generic_state *state;
938 u32 this_index;
939 union acpi_operand_object *this_source_obj;
940
941 ACPI_FUNCTION_TRACE(ut_walk_package_tree);
942
943 state = acpi_ut_create_pkg_state(source_object, target_object, 0);
944 if (!state) {
945 return_ACPI_STATUS(AE_NO_MEMORY);
946 }
947
948 while (state) {
949
950
951
952 this_index = state->pkg.index;
953 this_source_obj = (union acpi_operand_object *)
954 state->pkg.source_object->package.elements[this_index];
955
956
957
958
959
960
961
962
963
964 if ((!this_source_obj) ||
965 (ACPI_GET_DESCRIPTOR_TYPE(this_source_obj) !=
966 ACPI_DESC_TYPE_OPERAND)
967 || (this_source_obj->common.type != ACPI_TYPE_PACKAGE)) {
968 status =
969 walk_callback(ACPI_COPY_TYPE_SIMPLE,
970 this_source_obj, state, context);
971 if (ACPI_FAILURE(status)) {
972 return_ACPI_STATUS(status);
973 }
974
975 state->pkg.index++;
976 while (state->pkg.index >=
977 state->pkg.source_object->package.count) {
978
979
980
981
982
983
984
985 acpi_ut_delete_generic_state(state);
986 state = acpi_ut_pop_generic_state(&state_list);
987
988
989
990 if (!state) {
991
992
993
994
995
996 return_ACPI_STATUS(AE_OK);
997 }
998
999
1000
1001
1002
1003 state->pkg.index++;
1004 }
1005 } else {
1006
1007
1008 status =
1009 walk_callback(ACPI_COPY_TYPE_PACKAGE,
1010 this_source_obj, state, context);
1011 if (ACPI_FAILURE(status)) {
1012 return_ACPI_STATUS(status);
1013 }
1014
1015
1016
1017
1018
1019 acpi_ut_push_generic_state(&state_list, state);
1020 state = acpi_ut_create_pkg_state(this_source_obj,
1021 state->pkg.
1022 this_target_obj, 0);
1023 if (!state) {
1024
1025
1026
1027 while (state_list) {
1028 state =
1029 acpi_ut_pop_generic_state
1030 (&state_list);
1031 acpi_ut_delete_generic_state(state);
1032 }
1033 return_ACPI_STATUS(AE_NO_MEMORY);
1034 }
1035 }
1036 }
1037
1038
1039
1040 return_ACPI_STATUS(AE_AML_INTERNAL);
1041}
1042