1
2
3
4
5
6
7
8
9
10#include <acpi/acpi.h>
11#include "accommon.h"
12#include "acevents.h"
13#include "acnamesp.h"
14#include "acinterp.h"
15
16#define _COMPONENT ACPI_EVENTS
17ACPI_MODULE_NAME("evregion")
18
19extern u8 acpi_gbl_default_address_spaces[];
20
21
22
23static void
24acpi_ev_execute_orphan_reg_method(struct acpi_namespace_node *device_node,
25 acpi_adr_space_type space_id);
26
27static acpi_status
28acpi_ev_reg_run(acpi_handle obj_handle,
29 u32 level, void *context, void **return_value);
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44acpi_status acpi_ev_initialize_op_regions(void)
45{
46 acpi_status status;
47 u32 i;
48
49 ACPI_FUNCTION_TRACE(ev_initialize_op_regions);
50
51 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
52 if (ACPI_FAILURE(status)) {
53 return_ACPI_STATUS(status);
54 }
55
56
57
58 for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++) {
59
60
61
62
63
64 if (acpi_ev_has_default_handler(acpi_gbl_root_node,
65 acpi_gbl_default_address_spaces
66 [i])) {
67 acpi_ev_execute_reg_methods(acpi_gbl_root_node,
68 acpi_gbl_default_address_spaces
69 [i], ACPI_REG_CONNECT);
70 }
71 }
72
73 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
74 return_ACPI_STATUS(status);
75}
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102acpi_status
103acpi_ev_address_space_dispatch(union acpi_operand_object *region_obj,
104 union acpi_operand_object *field_obj,
105 u32 function,
106 u32 region_offset, u32 bit_width, u64 *value)
107{
108 acpi_status status;
109 acpi_adr_space_handler handler;
110 acpi_adr_space_setup region_setup;
111 union acpi_operand_object *handler_desc;
112 union acpi_operand_object *region_obj2;
113 void *region_context = NULL;
114 struct acpi_connection_info *context;
115 acpi_mutex context_mutex;
116 u8 context_locked;
117 acpi_physical_address address;
118
119 ACPI_FUNCTION_TRACE(ev_address_space_dispatch);
120
121 region_obj2 = acpi_ns_get_secondary_object(region_obj);
122 if (!region_obj2) {
123 return_ACPI_STATUS(AE_NOT_EXIST);
124 }
125
126
127
128 handler_desc = region_obj->region.handler;
129 if (!handler_desc) {
130 ACPI_ERROR((AE_INFO,
131 "No handler for Region [%4.4s] (%p) [%s]",
132 acpi_ut_get_node_name(region_obj->region.node),
133 region_obj,
134 acpi_ut_get_region_name(region_obj->region.
135 space_id)));
136
137 return_ACPI_STATUS(AE_NOT_EXIST);
138 }
139
140 context = handler_desc->address_space.context;
141 context_mutex = handler_desc->address_space.context_mutex;
142 context_locked = FALSE;
143
144
145
146
147
148 if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE)) {
149
150
151
152 region_setup = handler_desc->address_space.setup;
153 if (!region_setup) {
154
155
156
157 ACPI_ERROR((AE_INFO,
158 "No init routine for region(%p) [%s]",
159 region_obj,
160 acpi_ut_get_region_name(region_obj->region.
161 space_id)));
162 return_ACPI_STATUS(AE_NOT_EXIST);
163 }
164
165
166
167
168
169
170 acpi_ex_exit_interpreter();
171
172 status = region_setup(region_obj, ACPI_REGION_ACTIVATE,
173 context, ®ion_context);
174
175
176
177 acpi_ex_enter_interpreter();
178
179
180
181 if (ACPI_FAILURE(status)) {
182 ACPI_EXCEPTION((AE_INFO, status,
183 "During region initialization: [%s]",
184 acpi_ut_get_region_name(region_obj->
185 region.
186 space_id)));
187 return_ACPI_STATUS(status);
188 }
189
190
191
192 if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE)) {
193 region_obj->region.flags |= AOPOBJ_SETUP_COMPLETE;
194
195
196
197
198
199 if (!(region_obj2->extra.region_context)) {
200 region_obj2->extra.region_context =
201 region_context;
202 }
203 }
204 }
205
206
207
208 handler = handler_desc->address_space.handler;
209 address = (region_obj->region.address + region_offset);
210
211 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
212 "Handler %p (@%p) Address %8.8X%8.8X [%s]\n",
213 ®ion_obj->region.handler->address_space, handler,
214 ACPI_FORMAT_UINT64(address),
215 acpi_ut_get_region_name(region_obj->region.
216 space_id)));
217
218 if (!(handler_desc->address_space.handler_flags &
219 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {
220
221
222
223
224
225 acpi_ex_exit_interpreter();
226 }
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247 if ((region_obj->region.space_id == ACPI_ADR_SPACE_GSBUS ||
248 region_obj->region.space_id == ACPI_ADR_SPACE_GPIO) &&
249 context && field_obj) {
250
251 status =
252 acpi_os_acquire_mutex(context_mutex, ACPI_WAIT_FOREVER);
253 if (ACPI_FAILURE(status)) {
254 goto re_enter_interpreter;
255 }
256
257 context_locked = TRUE;
258
259
260
261 context->connection = field_obj->field.resource_buffer;
262 context->length = field_obj->field.resource_length;
263 context->access_length = field_obj->field.access_length;
264
265 if (region_obj->region.space_id == ACPI_ADR_SPACE_GPIO) {
266 address = field_obj->field.pin_number_index;
267 bit_width = field_obj->field.bit_length;
268 }
269 }
270
271
272
273 status = handler(function, address, bit_width, value, context,
274 region_obj2->extra.region_context);
275
276 if (context_locked) {
277 acpi_os_release_mutex(context_mutex);
278 }
279
280 if (ACPI_FAILURE(status)) {
281 ACPI_EXCEPTION((AE_INFO, status, "Returned by Handler for [%s]",
282 acpi_ut_get_region_name(region_obj->region.
283 space_id)));
284
285
286
287
288
289 if ((region_obj->region.space_id == ACPI_ADR_SPACE_EC) &&
290 (status == AE_TIME)) {
291 ACPI_ERROR((AE_INFO,
292 "Timeout from EC hardware or EC device driver"));
293 }
294 }
295
296re_enter_interpreter:
297 if (!(handler_desc->address_space.handler_flags &
298 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {
299
300
301
302
303 acpi_ex_enter_interpreter();
304 }
305
306 return_ACPI_STATUS(status);
307}
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323void
324acpi_ev_detach_region(union acpi_operand_object *region_obj,
325 u8 acpi_ns_is_locked)
326{
327 union acpi_operand_object *handler_obj;
328 union acpi_operand_object *obj_desc;
329 union acpi_operand_object *start_desc;
330 union acpi_operand_object **last_obj_ptr;
331 acpi_adr_space_setup region_setup;
332 void **region_context;
333 union acpi_operand_object *region_obj2;
334 acpi_status status;
335
336 ACPI_FUNCTION_TRACE(ev_detach_region);
337
338 region_obj2 = acpi_ns_get_secondary_object(region_obj);
339 if (!region_obj2) {
340 return_VOID;
341 }
342 region_context = ®ion_obj2->extra.region_context;
343
344
345
346 handler_obj = region_obj->region.handler;
347 if (!handler_obj) {
348
349
350
351 return_VOID;
352 }
353
354
355
356 obj_desc = handler_obj->address_space.region_list;
357 start_desc = obj_desc;
358 last_obj_ptr = &handler_obj->address_space.region_list;
359
360 while (obj_desc) {
361
362
363
364 if (obj_desc == region_obj) {
365 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
366 "Removing Region %p from address handler %p\n",
367 region_obj, handler_obj));
368
369
370
371 *last_obj_ptr = obj_desc->region.next;
372 obj_desc->region.next = NULL;
373
374 if (acpi_ns_is_locked) {
375 status =
376 acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
377 if (ACPI_FAILURE(status)) {
378 return_VOID;
379 }
380 }
381
382
383
384 status =
385 acpi_ev_execute_reg_method(region_obj,
386 ACPI_REG_DISCONNECT);
387 if (ACPI_FAILURE(status)) {
388 ACPI_EXCEPTION((AE_INFO, status,
389 "from region _REG, [%s]",
390 acpi_ut_get_region_name
391 (region_obj->region.space_id)));
392 }
393
394 if (acpi_ns_is_locked) {
395 status =
396 acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
397 if (ACPI_FAILURE(status)) {
398 return_VOID;
399 }
400 }
401
402
403
404
405
406 if (region_obj->region.flags & AOPOBJ_SETUP_COMPLETE) {
407 region_setup = handler_obj->address_space.setup;
408 status =
409 region_setup(region_obj,
410 ACPI_REGION_DEACTIVATE,
411 handler_obj->address_space.
412 context, region_context);
413
414
415
416
417
418 if (region_context) {
419 *region_context = NULL;
420 }
421
422
423
424 if (ACPI_FAILURE(status)) {
425 ACPI_EXCEPTION((AE_INFO, status,
426 "from region handler - deactivate, [%s]",
427 acpi_ut_get_region_name
428 (region_obj->region.
429 space_id)));
430 }
431
432 region_obj->region.flags &=
433 ~(AOPOBJ_SETUP_COMPLETE);
434 }
435
436
437
438
439
440
441
442
443
444
445 region_obj->region.handler = NULL;
446 acpi_ut_remove_reference(handler_obj);
447
448 return_VOID;
449 }
450
451
452
453 last_obj_ptr = &obj_desc->region.next;
454 obj_desc = obj_desc->region.next;
455
456
457
458 if (obj_desc == start_desc) {
459 ACPI_ERROR((AE_INFO,
460 "Circular handler list in region object %p",
461 region_obj));
462 return_VOID;
463 }
464 }
465
466
467
468 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
469 "Cannot remove region %p from address handler %p\n",
470 region_obj, handler_obj));
471
472 return_VOID;
473}
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490acpi_status
491acpi_ev_attach_region(union acpi_operand_object *handler_obj,
492 union acpi_operand_object *region_obj,
493 u8 acpi_ns_is_locked)
494{
495
496 ACPI_FUNCTION_TRACE(ev_attach_region);
497
498
499
500 if (region_obj->region.handler) {
501 return_ACPI_STATUS(AE_ALREADY_EXISTS);
502 }
503
504 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
505 "Adding Region [%4.4s] %p to address handler %p [%s]\n",
506 acpi_ut_get_node_name(region_obj->region.node),
507 region_obj, handler_obj,
508 acpi_ut_get_region_name(region_obj->region.
509 space_id)));
510
511
512
513 region_obj->region.next = handler_obj->address_space.region_list;
514 handler_obj->address_space.region_list = region_obj;
515 region_obj->region.handler = handler_obj;
516 acpi_ut_add_reference(handler_obj);
517
518 return_ACPI_STATUS(AE_OK);
519}
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534acpi_status
535acpi_ev_execute_reg_method(union acpi_operand_object *region_obj, u32 function)
536{
537 struct acpi_evaluate_info *info;
538 union acpi_operand_object *args[3];
539 union acpi_operand_object *region_obj2;
540 const acpi_name *reg_name_ptr =
541 ACPI_CAST_PTR(acpi_name, METHOD_NAME__REG);
542 struct acpi_namespace_node *method_node;
543 struct acpi_namespace_node *node;
544 acpi_status status;
545
546 ACPI_FUNCTION_TRACE(ev_execute_reg_method);
547
548 if (!acpi_gbl_namespace_initialized ||
549 region_obj->region.handler == NULL) {
550 return_ACPI_STATUS(AE_OK);
551 }
552
553 region_obj2 = acpi_ns_get_secondary_object(region_obj);
554 if (!region_obj2) {
555 return_ACPI_STATUS(AE_NOT_EXIST);
556 }
557
558
559
560
561
562
563 node = region_obj->region.node->parent;
564 status =
565 acpi_ns_search_one_scope(*reg_name_ptr, node, ACPI_TYPE_METHOD,
566 &method_node);
567 if (ACPI_SUCCESS(status)) {
568
569
570
571
572
573 region_obj2->extra.method_REG = method_node;
574 }
575 if (region_obj2->extra.method_REG == NULL) {
576 return_ACPI_STATUS(AE_OK);
577 }
578
579
580
581 if ((function == ACPI_REG_CONNECT &&
582 region_obj->common.flags & AOPOBJ_REG_CONNECTED) ||
583 (function == ACPI_REG_DISCONNECT &&
584 !(region_obj->common.flags & AOPOBJ_REG_CONNECTED))) {
585 return_ACPI_STATUS(AE_OK);
586 }
587
588
589
590 info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
591 if (!info) {
592 return_ACPI_STATUS(AE_NO_MEMORY);
593 }
594
595 info->prefix_node = region_obj2->extra.method_REG;
596 info->relative_pathname = NULL;
597 info->parameters = args;
598 info->flags = ACPI_IGNORE_RETURN_VALUE;
599
600
601
602
603
604
605
606
607
608
609
610 args[0] =
611 acpi_ut_create_integer_object((u64)region_obj->region.space_id);
612 if (!args[0]) {
613 status = AE_NO_MEMORY;
614 goto cleanup1;
615 }
616
617 args[1] = acpi_ut_create_integer_object((u64)function);
618 if (!args[1]) {
619 status = AE_NO_MEMORY;
620 goto cleanup2;
621 }
622
623 args[2] = NULL;
624
625
626
627 ACPI_DEBUG_EXEC(acpi_ut_display_init_pathname
628 (ACPI_TYPE_METHOD, info->prefix_node, NULL));
629
630 status = acpi_ns_evaluate(info);
631 acpi_ut_remove_reference(args[1]);
632
633 if (ACPI_FAILURE(status)) {
634 goto cleanup2;
635 }
636
637 if (function == ACPI_REG_CONNECT) {
638 region_obj->common.flags |= AOPOBJ_REG_CONNECTED;
639 } else {
640 region_obj->common.flags &= ~AOPOBJ_REG_CONNECTED;
641 }
642
643cleanup2:
644 acpi_ut_remove_reference(args[0]);
645
646cleanup1:
647 ACPI_FREE(info);
648 return_ACPI_STATUS(status);
649}
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666void
667acpi_ev_execute_reg_methods(struct acpi_namespace_node *node,
668 acpi_adr_space_type space_id, u32 function)
669{
670 struct acpi_reg_walk_info info;
671
672 ACPI_FUNCTION_TRACE(ev_execute_reg_methods);
673
674
675
676
677
678
679
680
681 if ((space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) ||
682 (space_id == ACPI_ADR_SPACE_SYSTEM_IO) ||
683 (space_id == ACPI_ADR_SPACE_DATA_TABLE)) {
684 return_VOID;
685 }
686
687 info.space_id = space_id;
688 info.function = function;
689 info.reg_run_count = 0;
690
691 ACPI_DEBUG_PRINT_RAW((ACPI_DB_NAMES,
692 " Running _REG methods for SpaceId %s\n",
693 acpi_ut_get_region_name(info.space_id)));
694
695
696
697
698
699
700
701 (void)acpi_ns_walk_namespace(ACPI_TYPE_ANY, node, ACPI_UINT32_MAX,
702 ACPI_NS_WALK_UNLOCK, acpi_ev_reg_run, NULL,
703 &info, NULL);
704
705
706
707
708
709 if (space_id == ACPI_ADR_SPACE_EC || space_id == ACPI_ADR_SPACE_GPIO) {
710 acpi_ev_execute_orphan_reg_method(node, space_id);
711 }
712
713 ACPI_DEBUG_PRINT_RAW((ACPI_DB_NAMES,
714 " Executed %u _REG methods for SpaceId %s\n",
715 info.reg_run_count,
716 acpi_ut_get_region_name(info.space_id)));
717
718 return_VOID;
719}
720
721
722
723
724
725
726
727
728
729
730
731static acpi_status
732acpi_ev_reg_run(acpi_handle obj_handle,
733 u32 level, void *context, void **return_value)
734{
735 union acpi_operand_object *obj_desc;
736 struct acpi_namespace_node *node;
737 acpi_status status;
738 struct acpi_reg_walk_info *info;
739
740 info = ACPI_CAST_PTR(struct acpi_reg_walk_info, context);
741
742
743
744 node = acpi_ns_validate_handle(obj_handle);
745 if (!node) {
746 return (AE_BAD_PARAMETER);
747 }
748
749
750
751
752
753 if ((node->type != ACPI_TYPE_REGION) && (node != acpi_gbl_root_node)) {
754 return (AE_OK);
755 }
756
757
758
759 obj_desc = acpi_ns_get_attached_object(node);
760 if (!obj_desc) {
761
762
763
764 return (AE_OK);
765 }
766
767
768
769 if (obj_desc->region.space_id != info->space_id) {
770
771
772
773 return (AE_OK);
774 }
775
776 info->reg_run_count++;
777 status = acpi_ev_execute_reg_method(obj_desc, info->function);
778 return (status);
779}
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802static void
803acpi_ev_execute_orphan_reg_method(struct acpi_namespace_node *device_node,
804 acpi_adr_space_type space_id)
805{
806 acpi_handle reg_method;
807 struct acpi_namespace_node *next_node;
808 acpi_status status;
809 struct acpi_object_list args;
810 union acpi_object objects[2];
811
812 ACPI_FUNCTION_TRACE(ev_execute_orphan_reg_method);
813
814 if (!device_node) {
815 return_VOID;
816 }
817
818
819
820 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
821
822
823
824 status = acpi_get_handle(device_node, METHOD_NAME__REG, ®_method);
825 if (ACPI_FAILURE(status)) {
826 goto exit;
827 }
828
829
830
831
832
833
834
835
836 next_node = acpi_ns_get_next_node(device_node, NULL);
837 while (next_node) {
838 if ((next_node->type == ACPI_TYPE_REGION) &&
839 (next_node->object) &&
840 (next_node->object->region.space_id == space_id)) {
841 goto exit;
842 }
843
844 next_node = acpi_ns_get_next_node(device_node, next_node);
845 }
846
847
848
849 args.count = 2;
850 args.pointer = objects;
851 objects[0].type = ACPI_TYPE_INTEGER;
852 objects[0].integer.value = space_id;
853 objects[1].type = ACPI_TYPE_INTEGER;
854 objects[1].integer.value = ACPI_REG_CONNECT;
855
856 (void)acpi_evaluate_object(reg_method, NULL, &args, NULL);
857
858exit:
859
860
861 (void)acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
862 return_VOID;
863}
864