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 <acpi/acpi.h>
45#include "accommon.h"
46#include "acevents.h"
47#include "acnamesp.h"
48#include "acinterp.h"
49
50#define _COMPONENT ACPI_EVENTS
51ACPI_MODULE_NAME("evregion")
52
53
54static acpi_status
55acpi_ev_reg_run(acpi_handle obj_handle,
56 u32 level, void *context, void **return_value);
57
58static acpi_status
59acpi_ev_install_handler(acpi_handle obj_handle,
60 u32 level, void *context, void **return_value);
61
62
63
64#define ACPI_NUM_DEFAULT_SPACES 4
65
66static u8 acpi_gbl_default_address_spaces[ACPI_NUM_DEFAULT_SPACES] = {
67 ACPI_ADR_SPACE_SYSTEM_MEMORY,
68 ACPI_ADR_SPACE_SYSTEM_IO,
69 ACPI_ADR_SPACE_PCI_CONFIG,
70 ACPI_ADR_SPACE_DATA_TABLE
71};
72
73
74
75
76
77
78
79
80
81
82
83
84
85acpi_status acpi_ev_install_region_handlers(void)
86{
87 acpi_status status;
88 u32 i;
89
90 ACPI_FUNCTION_TRACE(ev_install_region_handlers);
91
92 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
93 if (ACPI_FAILURE(status)) {
94 return_ACPI_STATUS(status);
95 }
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116 for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++) {
117 status = acpi_ev_install_space_handler(acpi_gbl_root_node,
118 acpi_gbl_default_address_spaces
119 [i],
120 ACPI_DEFAULT_HANDLER,
121 NULL, NULL);
122 switch (status) {
123 case AE_OK:
124 case AE_SAME_HANDLER:
125 case AE_ALREADY_EXISTS:
126
127
128
129 status = AE_OK;
130 break;
131
132 default:
133
134 goto unlock_and_exit;
135 }
136 }
137
138 unlock_and_exit:
139 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
140 return_ACPI_STATUS(status);
141}
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156acpi_status acpi_ev_initialize_op_regions(void)
157{
158 acpi_status status;
159 u32 i;
160
161 ACPI_FUNCTION_TRACE(ev_initialize_op_regions);
162
163 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
164 if (ACPI_FAILURE(status)) {
165 return_ACPI_STATUS(status);
166 }
167
168
169
170 for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++) {
171
172
173
174
175 status = acpi_ev_execute_reg_methods(acpi_gbl_root_node,
176 acpi_gbl_default_address_spaces
177 [i]);
178 }
179
180 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
181 return_ACPI_STATUS(status);
182}
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197acpi_status
198acpi_ev_execute_reg_method(union acpi_operand_object *region_obj, u32 function)
199{
200 struct acpi_evaluate_info *info;
201 union acpi_operand_object *args[3];
202 union acpi_operand_object *region_obj2;
203 acpi_status status;
204
205 ACPI_FUNCTION_TRACE(ev_execute_reg_method);
206
207 region_obj2 = acpi_ns_get_secondary_object(region_obj);
208 if (!region_obj2) {
209 return_ACPI_STATUS(AE_NOT_EXIST);
210 }
211
212 if (region_obj2->extra.method_REG == NULL) {
213 return_ACPI_STATUS(AE_OK);
214 }
215
216
217
218 info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
219 if (!info) {
220 return_ACPI_STATUS(AE_NO_MEMORY);
221 }
222
223 info->prefix_node = region_obj2->extra.method_REG;
224 info->pathname = NULL;
225 info->parameters = args;
226 info->flags = ACPI_IGNORE_RETURN_VALUE;
227
228
229
230
231
232
233
234
235
236
237
238 args[0] = acpi_ut_create_internal_object(ACPI_TYPE_INTEGER);
239 if (!args[0]) {
240 status = AE_NO_MEMORY;
241 goto cleanup1;
242 }
243
244 args[1] = acpi_ut_create_internal_object(ACPI_TYPE_INTEGER);
245 if (!args[1]) {
246 status = AE_NO_MEMORY;
247 goto cleanup2;
248 }
249
250
251
252 args[0]->integer.value = region_obj->region.space_id;
253 args[1]->integer.value = function;
254 args[2] = NULL;
255
256
257
258 ACPI_DEBUG_EXEC(acpi_ut_display_init_pathname
259 (ACPI_TYPE_METHOD, info->prefix_node, NULL));
260
261 status = acpi_ns_evaluate(info);
262 acpi_ut_remove_reference(args[1]);
263
264 cleanup2:
265 acpi_ut_remove_reference(args[0]);
266
267 cleanup1:
268 ACPI_FREE(info);
269 return_ACPI_STATUS(status);
270}
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290acpi_status
291acpi_ev_address_space_dispatch(union acpi_operand_object *region_obj,
292 u32 function,
293 u32 region_offset,
294 u32 bit_width, acpi_integer * value)
295{
296 acpi_status status;
297 acpi_adr_space_handler handler;
298 acpi_adr_space_setup region_setup;
299 union acpi_operand_object *handler_desc;
300 union acpi_operand_object *region_obj2;
301 void *region_context = NULL;
302
303 ACPI_FUNCTION_TRACE(ev_address_space_dispatch);
304
305 region_obj2 = acpi_ns_get_secondary_object(region_obj);
306 if (!region_obj2) {
307 return_ACPI_STATUS(AE_NOT_EXIST);
308 }
309
310
311
312 handler_desc = region_obj->region.handler;
313 if (!handler_desc) {
314 ACPI_ERROR((AE_INFO,
315 "No handler for Region [%4.4s] (%p) [%s]",
316 acpi_ut_get_node_name(region_obj->region.node),
317 region_obj,
318 acpi_ut_get_region_name(region_obj->region.
319 space_id)));
320
321 return_ACPI_STATUS(AE_NOT_EXIST);
322 }
323
324
325
326
327
328 if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE)) {
329
330
331
332 region_setup = handler_desc->address_space.setup;
333 if (!region_setup) {
334
335
336
337 ACPI_ERROR((AE_INFO,
338 "No init routine for region(%p) [%s]",
339 region_obj,
340 acpi_ut_get_region_name(region_obj->region.
341 space_id)));
342 return_ACPI_STATUS(AE_NOT_EXIST);
343 }
344
345
346
347
348
349
350 acpi_ex_exit_interpreter();
351
352 status = region_setup(region_obj, ACPI_REGION_ACTIVATE,
353 handler_desc->address_space.context,
354 ®ion_context);
355
356
357
358 acpi_ex_enter_interpreter();
359
360
361
362 if (ACPI_FAILURE(status)) {
363 ACPI_EXCEPTION((AE_INFO, status,
364 "During region initialization: [%s]",
365 acpi_ut_get_region_name(region_obj->
366 region.
367 space_id)));
368 return_ACPI_STATUS(status);
369 }
370
371
372
373 if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE)) {
374 region_obj->region.flags |= AOPOBJ_SETUP_COMPLETE;
375
376 if (region_obj2->extra.region_context) {
377
378
379
380 ACPI_FREE(region_context);
381 } else {
382
383
384
385
386 region_obj2->extra.region_context =
387 region_context;
388 }
389 }
390 }
391
392
393
394 handler = handler_desc->address_space.handler;
395
396 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
397 "Handler %p (@%p) Address %8.8X%8.8X [%s]\n",
398 ®ion_obj->region.handler->address_space, handler,
399 ACPI_FORMAT_NATIVE_UINT(region_obj->region.address +
400 region_offset),
401 acpi_ut_get_region_name(region_obj->region.
402 space_id)));
403
404 if (!(handler_desc->address_space.handler_flags &
405 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {
406
407
408
409
410
411 acpi_ex_exit_interpreter();
412 }
413
414
415
416 status = handler(function,
417 (region_obj->region.address + region_offset),
418 bit_width, value, handler_desc->address_space.context,
419 region_obj2->extra.region_context);
420
421 if (ACPI_FAILURE(status)) {
422 ACPI_EXCEPTION((AE_INFO, status, "Returned by Handler for [%s]",
423 acpi_ut_get_region_name(region_obj->region.
424 space_id)));
425 }
426
427 if (!(handler_desc->address_space.handler_flags &
428 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {
429
430
431
432
433 acpi_ex_enter_interpreter();
434 }
435
436 return_ACPI_STATUS(status);
437}
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453void
454acpi_ev_detach_region(union acpi_operand_object *region_obj,
455 u8 acpi_ns_is_locked)
456{
457 union acpi_operand_object *handler_obj;
458 union acpi_operand_object *obj_desc;
459 union acpi_operand_object **last_obj_ptr;
460 acpi_adr_space_setup region_setup;
461 void **region_context;
462 union acpi_operand_object *region_obj2;
463 acpi_status status;
464
465 ACPI_FUNCTION_TRACE(ev_detach_region);
466
467 region_obj2 = acpi_ns_get_secondary_object(region_obj);
468 if (!region_obj2) {
469 return_VOID;
470 }
471 region_context = ®ion_obj2->extra.region_context;
472
473
474
475 handler_obj = region_obj->region.handler;
476 if (!handler_obj) {
477
478
479
480 return_VOID;
481 }
482
483
484
485 obj_desc = handler_obj->address_space.region_list;
486 last_obj_ptr = &handler_obj->address_space.region_list;
487
488 while (obj_desc) {
489
490
491
492 if (obj_desc == region_obj) {
493 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
494 "Removing Region %p from address handler %p\n",
495 region_obj, handler_obj));
496
497
498
499 *last_obj_ptr = obj_desc->region.next;
500 obj_desc->region.next = NULL;
501
502 if (acpi_ns_is_locked) {
503 status =
504 acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
505 if (ACPI_FAILURE(status)) {
506 return_VOID;
507 }
508 }
509
510
511
512 status = acpi_ev_execute_reg_method(region_obj, 0);
513 if (ACPI_FAILURE(status)) {
514 ACPI_EXCEPTION((AE_INFO, status,
515 "from region _REG, [%s]",
516 acpi_ut_get_region_name
517 (region_obj->region.space_id)));
518 }
519
520 if (acpi_ns_is_locked) {
521 status =
522 acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
523 if (ACPI_FAILURE(status)) {
524 return_VOID;
525 }
526 }
527
528
529
530
531
532 if (region_obj->region.flags & AOPOBJ_SETUP_COMPLETE) {
533 region_setup = handler_obj->address_space.setup;
534 status =
535 region_setup(region_obj,
536 ACPI_REGION_DEACTIVATE,
537 handler_obj->address_space.
538 context, region_context);
539
540
541
542 if (ACPI_FAILURE(status)) {
543 ACPI_EXCEPTION((AE_INFO, status,
544 "from region handler - deactivate, [%s]",
545 acpi_ut_get_region_name
546 (region_obj->region.
547 space_id)));
548 }
549
550 region_obj->region.flags &=
551 ~(AOPOBJ_SETUP_COMPLETE);
552 }
553
554
555
556
557
558
559
560
561
562
563 region_obj->region.handler = NULL;
564 acpi_ut_remove_reference(handler_obj);
565
566 return_VOID;
567 }
568
569
570
571 last_obj_ptr = &obj_desc->region.next;
572 obj_desc = obj_desc->region.next;
573 }
574
575
576
577 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
578 "Cannot remove region %p from address handler %p\n",
579 region_obj, handler_obj));
580
581 return_VOID;
582}
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599acpi_status
600acpi_ev_attach_region(union acpi_operand_object *handler_obj,
601 union acpi_operand_object *region_obj,
602 u8 acpi_ns_is_locked)
603{
604
605 ACPI_FUNCTION_TRACE(ev_attach_region);
606
607 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
608 "Adding Region [%4.4s] %p to address handler %p [%s]\n",
609 acpi_ut_get_node_name(region_obj->region.node),
610 region_obj, handler_obj,
611 acpi_ut_get_region_name(region_obj->region.
612 space_id)));
613
614
615
616 region_obj->region.next = handler_obj->address_space.region_list;
617 handler_obj->address_space.region_list = region_obj;
618
619
620
621 if (region_obj->region.handler) {
622 return_ACPI_STATUS(AE_ALREADY_EXISTS);
623 }
624
625 region_obj->region.handler = handler_obj;
626 acpi_ut_add_reference(handler_obj);
627
628 return_ACPI_STATUS(AE_OK);
629}
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648static acpi_status
649acpi_ev_install_handler(acpi_handle obj_handle,
650 u32 level, void *context, void **return_value)
651{
652 union acpi_operand_object *handler_obj;
653 union acpi_operand_object *next_handler_obj;
654 union acpi_operand_object *obj_desc;
655 struct acpi_namespace_node *node;
656 acpi_status status;
657
658 ACPI_FUNCTION_NAME(ev_install_handler);
659
660 handler_obj = (union acpi_operand_object *)context;
661
662
663
664 if (!handler_obj) {
665 return (AE_OK);
666 }
667
668
669
670 node = acpi_ns_map_handle_to_node(obj_handle);
671 if (!node) {
672 return (AE_BAD_PARAMETER);
673 }
674
675
676
677
678
679 if ((node->type != ACPI_TYPE_DEVICE) &&
680 (node->type != ACPI_TYPE_REGION) && (node != acpi_gbl_root_node)) {
681 return (AE_OK);
682 }
683
684
685
686 obj_desc = acpi_ns_get_attached_object(node);
687 if (!obj_desc) {
688
689
690
691 return (AE_OK);
692 }
693
694
695
696 if (obj_desc->common.type == ACPI_TYPE_DEVICE) {
697
698
699
700 next_handler_obj = obj_desc->device.handler;
701 while (next_handler_obj) {
702
703
704
705 if (next_handler_obj->address_space.space_id ==
706 handler_obj->address_space.space_id) {
707 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
708 "Found handler for region [%s] in device %p(%p) "
709 "handler %p\n",
710 acpi_ut_get_region_name
711 (handler_obj->address_space.
712 space_id), obj_desc,
713 next_handler_obj,
714 handler_obj));
715
716
717
718
719
720
721
722
723 return (AE_CTRL_DEPTH);
724 }
725
726
727
728 next_handler_obj = next_handler_obj->address_space.next;
729 }
730
731
732
733
734
735 return (AE_OK);
736 }
737
738
739
740 if (obj_desc->region.space_id != handler_obj->address_space.space_id) {
741
742
743
744 return (AE_OK);
745 }
746
747
748
749
750
751
752 acpi_ev_detach_region(obj_desc, FALSE);
753
754
755
756 status = acpi_ev_attach_region(handler_obj, obj_desc, FALSE);
757 return (status);
758}
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777acpi_status
778acpi_ev_install_space_handler(struct acpi_namespace_node * node,
779 acpi_adr_space_type space_id,
780 acpi_adr_space_handler handler,
781 acpi_adr_space_setup setup, void *context)
782{
783 union acpi_operand_object *obj_desc;
784 union acpi_operand_object *handler_obj;
785 acpi_status status;
786 acpi_object_type type;
787 u8 flags = 0;
788
789 ACPI_FUNCTION_TRACE(ev_install_space_handler);
790
791
792
793
794
795 if ((node->type != ACPI_TYPE_DEVICE) &&
796 (node->type != ACPI_TYPE_PROCESSOR) &&
797 (node->type != ACPI_TYPE_THERMAL) && (node != acpi_gbl_root_node)) {
798 status = AE_BAD_PARAMETER;
799 goto unlock_and_exit;
800 }
801
802 if (handler == ACPI_DEFAULT_HANDLER) {
803 flags = ACPI_ADDR_HANDLER_DEFAULT_INSTALLED;
804
805 switch (space_id) {
806 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
807 handler = acpi_ex_system_memory_space_handler;
808 setup = acpi_ev_system_memory_region_setup;
809 break;
810
811 case ACPI_ADR_SPACE_SYSTEM_IO:
812 handler = acpi_ex_system_io_space_handler;
813 setup = acpi_ev_io_space_region_setup;
814 break;
815
816 case ACPI_ADR_SPACE_PCI_CONFIG:
817 handler = acpi_ex_pci_config_space_handler;
818 setup = acpi_ev_pci_config_region_setup;
819 break;
820
821 case ACPI_ADR_SPACE_CMOS:
822 handler = acpi_ex_cmos_space_handler;
823 setup = acpi_ev_cmos_region_setup;
824 break;
825
826 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
827 handler = acpi_ex_pci_bar_space_handler;
828 setup = acpi_ev_pci_bar_region_setup;
829 break;
830
831 case ACPI_ADR_SPACE_DATA_TABLE:
832 handler = acpi_ex_data_table_space_handler;
833 setup = NULL;
834 break;
835
836 default:
837 status = AE_BAD_PARAMETER;
838 goto unlock_and_exit;
839 }
840 }
841
842
843
844 if (!setup) {
845 setup = acpi_ev_default_region_setup;
846 }
847
848
849
850 obj_desc = acpi_ns_get_attached_object(node);
851 if (obj_desc) {
852
853
854
855
856 handler_obj = obj_desc->device.handler;
857
858
859
860 while (handler_obj) {
861
862
863
864 if (handler_obj->address_space.space_id == space_id) {
865 if (handler_obj->address_space.handler ==
866 handler) {
867
868
869
870
871
872 status = AE_SAME_HANDLER;
873 goto unlock_and_exit;
874 } else {
875
876
877 status = AE_ALREADY_EXISTS;
878 }
879 goto unlock_and_exit;
880 }
881
882
883
884 handler_obj = handler_obj->address_space.next;
885 }
886 } else {
887 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
888 "Creating object on Device %p while installing handler\n",
889 node));
890
891
892
893 if (node->type == ACPI_TYPE_ANY) {
894 type = ACPI_TYPE_DEVICE;
895 } else {
896 type = node->type;
897 }
898
899 obj_desc = acpi_ut_create_internal_object(type);
900 if (!obj_desc) {
901 status = AE_NO_MEMORY;
902 goto unlock_and_exit;
903 }
904
905
906
907 obj_desc->common.type = (u8) type;
908
909
910
911 status = acpi_ns_attach_object(node, obj_desc, type);
912
913
914
915 acpi_ut_remove_reference(obj_desc);
916
917 if (ACPI_FAILURE(status)) {
918 goto unlock_and_exit;
919 }
920 }
921
922 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
923 "Installing address handler for region %s(%X) on Device %4.4s %p(%p)\n",
924 acpi_ut_get_region_name(space_id), space_id,
925 acpi_ut_get_node_name(node), node, obj_desc));
926
927
928
929
930
931
932
933 handler_obj =
934 acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_ADDRESS_HANDLER);
935 if (!handler_obj) {
936 status = AE_NO_MEMORY;
937 goto unlock_and_exit;
938 }
939
940
941
942 handler_obj->address_space.space_id = (u8) space_id;
943 handler_obj->address_space.handler_flags = flags;
944 handler_obj->address_space.region_list = NULL;
945 handler_obj->address_space.node = node;
946 handler_obj->address_space.handler = handler;
947 handler_obj->address_space.context = context;
948 handler_obj->address_space.setup = setup;
949
950
951
952 handler_obj->address_space.next = obj_desc->device.handler;
953
954
955
956
957
958 obj_desc->device.handler = handler_obj;
959
960
961
962
963
964
965
966
967
968
969
970
971
972 status = acpi_ns_walk_namespace(ACPI_TYPE_ANY, node, ACPI_UINT32_MAX,
973 ACPI_NS_WALK_UNLOCK,
974 acpi_ev_install_handler, handler_obj,
975 NULL);
976
977 unlock_and_exit:
978 return_ACPI_STATUS(status);
979}
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995acpi_status
996acpi_ev_execute_reg_methods(struct acpi_namespace_node *node,
997 acpi_adr_space_type space_id)
998{
999 acpi_status status;
1000
1001 ACPI_FUNCTION_TRACE(ev_execute_reg_methods);
1002
1003
1004
1005
1006
1007
1008
1009 status = acpi_ns_walk_namespace(ACPI_TYPE_ANY, node, ACPI_UINT32_MAX,
1010 ACPI_NS_WALK_UNLOCK, acpi_ev_reg_run,
1011 &space_id, NULL);
1012
1013 return_ACPI_STATUS(status);
1014}
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026static acpi_status
1027acpi_ev_reg_run(acpi_handle obj_handle,
1028 u32 level, void *context, void **return_value)
1029{
1030 union acpi_operand_object *obj_desc;
1031 struct acpi_namespace_node *node;
1032 acpi_adr_space_type space_id;
1033 acpi_status status;
1034
1035 space_id = *ACPI_CAST_PTR(acpi_adr_space_type, context);
1036
1037
1038
1039 node = acpi_ns_map_handle_to_node(obj_handle);
1040 if (!node) {
1041 return (AE_BAD_PARAMETER);
1042 }
1043
1044
1045
1046
1047
1048 if ((node->type != ACPI_TYPE_REGION) && (node != acpi_gbl_root_node)) {
1049 return (AE_OK);
1050 }
1051
1052
1053
1054 obj_desc = acpi_ns_get_attached_object(node);
1055 if (!obj_desc) {
1056
1057
1058
1059 return (AE_OK);
1060 }
1061
1062
1063
1064 if (obj_desc->region.space_id != space_id) {
1065
1066
1067
1068 return (AE_OK);
1069 }
1070
1071 status = acpi_ev_execute_reg_method(obj_desc, 1);
1072 return (status);
1073}
1074