GNU Linux-libre 5.10.215-gnu1
[releases.git] / drivers / acpi / acpica / evregion.c
1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
3  *
4  * Module Name: evregion - Operation Region support
5  *
6  * Copyright (C) 2000 - 2020, Intel Corp.
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
17 ACPI_MODULE_NAME("evregion")
18
19 extern u8 acpi_gbl_default_address_spaces[];
20
21 /* Local prototypes */
22
23 static void
24 acpi_ev_orphan_ec_reg_method(struct acpi_namespace_node *ec_device_node);
25
26 static acpi_status
27 acpi_ev_reg_run(acpi_handle obj_handle,
28                 u32 level, void *context, void **return_value);
29
30 /*******************************************************************************
31  *
32  * FUNCTION:    acpi_ev_initialize_op_regions
33  *
34  * PARAMETERS:  None
35  *
36  * RETURN:      Status
37  *
38  * DESCRIPTION: Execute _REG methods for all Operation Regions that have
39  *              an installed default region handler.
40  *
41  ******************************************************************************/
42
43 acpi_status acpi_ev_initialize_op_regions(void)
44 {
45         acpi_status status;
46         u32 i;
47
48         ACPI_FUNCTION_TRACE(ev_initialize_op_regions);
49
50         status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
51         if (ACPI_FAILURE(status)) {
52                 return_ACPI_STATUS(status);
53         }
54
55         /* Run the _REG methods for op_regions in each default address space */
56
57         for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++) {
58                 /*
59                  * Make sure the installed handler is the DEFAULT handler. If not the
60                  * default, the _REG methods will have already been run (when the
61                  * handler was installed)
62                  */
63                 if (acpi_ev_has_default_handler(acpi_gbl_root_node,
64                                                 acpi_gbl_default_address_spaces
65                                                 [i])) {
66                         acpi_ev_execute_reg_methods(acpi_gbl_root_node,
67                                                     acpi_gbl_default_address_spaces
68                                                     [i], ACPI_REG_CONNECT);
69                 }
70         }
71
72         (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
73         return_ACPI_STATUS(status);
74 }
75
76 /*******************************************************************************
77  *
78  * FUNCTION:    acpi_ev_address_space_dispatch
79  *
80  * PARAMETERS:  region_obj          - Internal region object
81  *              field_obj           - Corresponding field. Can be NULL.
82  *              function            - Read or Write operation
83  *              region_offset       - Where in the region to read or write
84  *              bit_width           - Field width in bits (8, 16, 32, or 64)
85  *              value               - Pointer to in or out value, must be
86  *                                    a full 64-bit integer
87  *
88  * RETURN:      Status
89  *
90  * DESCRIPTION: Dispatch an address space or operation region access to
91  *              a previously installed handler.
92  *
93  * NOTE: During early initialization, we always install the default region
94  * handlers for Memory, I/O and PCI_Config. This ensures that these operation
95  * region address spaces are always available as per the ACPI specification.
96  * This is especially needed in order to support the execution of
97  * module-level AML code during loading of the ACPI tables.
98  *
99  ******************************************************************************/
100
101 acpi_status
102 acpi_ev_address_space_dispatch(union acpi_operand_object *region_obj,
103                                union acpi_operand_object *field_obj,
104                                u32 function,
105                                u32 region_offset, u32 bit_width, u64 *value)
106 {
107         acpi_status status;
108         acpi_adr_space_handler handler;
109         acpi_adr_space_setup region_setup;
110         union acpi_operand_object *handler_desc;
111         union acpi_operand_object *region_obj2;
112         void *region_context = NULL;
113         struct acpi_connection_info *context;
114         acpi_mutex context_mutex;
115         u8 context_locked;
116         acpi_physical_address address;
117
118         ACPI_FUNCTION_TRACE(ev_address_space_dispatch);
119
120         region_obj2 = acpi_ns_get_secondary_object(region_obj);
121         if (!region_obj2) {
122                 return_ACPI_STATUS(AE_NOT_EXIST);
123         }
124
125         /* Ensure that there is a handler associated with this region */
126
127         handler_desc = region_obj->region.handler;
128         if (!handler_desc) {
129                 ACPI_ERROR((AE_INFO,
130                             "No handler for Region [%4.4s] (%p) [%s]",
131                             acpi_ut_get_node_name(region_obj->region.node),
132                             region_obj,
133                             acpi_ut_get_region_name(region_obj->region.
134                                                     space_id)));
135
136                 return_ACPI_STATUS(AE_NOT_EXIST);
137         }
138
139         context = handler_desc->address_space.context;
140         context_mutex = handler_desc->address_space.context_mutex;
141         context_locked = FALSE;
142
143         /*
144          * It may be the case that the region has never been initialized.
145          * Some types of regions require special init code
146          */
147         if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE)) {
148
149                 /* This region has not been initialized yet, do it */
150
151                 region_setup = handler_desc->address_space.setup;
152                 if (!region_setup) {
153
154                         /* No initialization routine, exit with error */
155
156                         ACPI_ERROR((AE_INFO,
157                                     "No init routine for region(%p) [%s]",
158                                     region_obj,
159                                     acpi_ut_get_region_name(region_obj->region.
160                                                             space_id)));
161                         return_ACPI_STATUS(AE_NOT_EXIST);
162                 }
163
164                 /*
165                  * We must exit the interpreter because the region setup will
166                  * potentially execute control methods (for example, the _REG method
167                  * for this region)
168                  */
169                 acpi_ex_exit_interpreter();
170
171                 status = region_setup(region_obj, ACPI_REGION_ACTIVATE,
172                                       context, &region_context);
173
174                 /* Re-enter the interpreter */
175
176                 acpi_ex_enter_interpreter();
177
178                 /* Check for failure of the Region Setup */
179
180                 if (ACPI_FAILURE(status)) {
181                         ACPI_EXCEPTION((AE_INFO, status,
182                                         "During region initialization: [%s]",
183                                         acpi_ut_get_region_name(region_obj->
184                                                                 region.
185                                                                 space_id)));
186                         return_ACPI_STATUS(status);
187                 }
188
189                 /* Region initialization may have been completed by region_setup */
190
191                 if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE)) {
192                         region_obj->region.flags |= AOPOBJ_SETUP_COMPLETE;
193
194                         /*
195                          * Save the returned context for use in all accesses to
196                          * the handler for this particular region
197                          */
198                         if (!(region_obj2->extra.region_context)) {
199                                 region_obj2->extra.region_context =
200                                     region_context;
201                         }
202                 }
203         }
204
205         /* We have everything we need, we can invoke the address space handler */
206
207         handler = handler_desc->address_space.handler;
208         address = (region_obj->region.address + region_offset);
209
210         ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
211                           "Handler %p (@%p) Address %8.8X%8.8X [%s]\n",
212                           &region_obj->region.handler->address_space, handler,
213                           ACPI_FORMAT_UINT64(address),
214                           acpi_ut_get_region_name(region_obj->region.
215                                                   space_id)));
216
217         if (!(handler_desc->address_space.handler_flags &
218               ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {
219                 /*
220                  * For handlers other than the default (supplied) handlers, we must
221                  * exit the interpreter because the handler *might* block -- we don't
222                  * know what it will do, so we can't hold the lock on the interpreter.
223                  */
224                 acpi_ex_exit_interpreter();
225         }
226
227         /*
228          * Special handling for generic_serial_bus and general_purpose_io:
229          * There are three extra parameters that must be passed to the
230          * handler via the context:
231          *   1) Connection buffer, a resource template from Connection() op
232          *   2) Length of the above buffer
233          *   3) Actual access length from the access_as() op
234          *
235          * Since we pass these extra parameters via the context, which is
236          * shared between threads, we must lock the context to avoid these
237          * parameters being changed from another thread before the handler
238          * has completed running.
239          *
240          * In addition, for general_purpose_io, the Address and bit_width fields
241          * are defined as follows:
242          *   1) Address is the pin number index of the field (bit offset from
243          *      the previous Connection)
244          *   2) bit_width is the actual bit length of the field (number of pins)
245          */
246         if ((region_obj->region.space_id == ACPI_ADR_SPACE_GSBUS) &&
247             context && field_obj) {
248
249                 status =
250                     acpi_os_acquire_mutex(context_mutex, ACPI_WAIT_FOREVER);
251                 if (ACPI_FAILURE(status)) {
252                         goto re_enter_interpreter;
253                 }
254
255                 context_locked = TRUE;
256
257                 /* Get the Connection (resource_template) buffer */
258
259                 context->connection = field_obj->field.resource_buffer;
260                 context->length = field_obj->field.resource_length;
261                 context->access_length = field_obj->field.access_length;
262         }
263         if ((region_obj->region.space_id == ACPI_ADR_SPACE_GPIO) &&
264             context && field_obj) {
265
266                 status =
267                     acpi_os_acquire_mutex(context_mutex, ACPI_WAIT_FOREVER);
268                 if (ACPI_FAILURE(status)) {
269                         goto re_enter_interpreter;
270                 }
271
272                 context_locked = TRUE;
273
274                 /* Get the Connection (resource_template) buffer */
275
276                 context->connection = field_obj->field.resource_buffer;
277                 context->length = field_obj->field.resource_length;
278                 context->access_length = field_obj->field.access_length;
279                 address = field_obj->field.pin_number_index;
280                 bit_width = field_obj->field.bit_length;
281         }
282
283         /* Call the handler */
284
285         status = handler(function, address, bit_width, value, context,
286                          region_obj2->extra.region_context);
287
288         if (context_locked) {
289                 acpi_os_release_mutex(context_mutex);
290         }
291
292         if (ACPI_FAILURE(status)) {
293                 ACPI_EXCEPTION((AE_INFO, status, "Returned by Handler for [%s]",
294                                 acpi_ut_get_region_name(region_obj->region.
295                                                         space_id)));
296
297                 /*
298                  * Special case for an EC timeout. These are seen so frequently
299                  * that an additional error message is helpful
300                  */
301                 if ((region_obj->region.space_id == ACPI_ADR_SPACE_EC) &&
302                     (status == AE_TIME)) {
303                         ACPI_ERROR((AE_INFO,
304                                     "Timeout from EC hardware or EC device driver"));
305                 }
306         }
307
308 re_enter_interpreter:
309         if (!(handler_desc->address_space.handler_flags &
310               ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {
311                 /*
312                  * We just returned from a non-default handler, we must re-enter the
313                  * interpreter
314                  */
315                 acpi_ex_enter_interpreter();
316         }
317
318         return_ACPI_STATUS(status);
319 }
320
321 /*******************************************************************************
322  *
323  * FUNCTION:    acpi_ev_detach_region
324  *
325  * PARAMETERS:  region_obj          - Region Object
326  *              acpi_ns_is_locked   - Namespace Region Already Locked?
327  *
328  * RETURN:      None
329  *
330  * DESCRIPTION: Break the association between the handler and the region
331  *              this is a two way association.
332  *
333  ******************************************************************************/
334
335 void
336 acpi_ev_detach_region(union acpi_operand_object *region_obj,
337                       u8 acpi_ns_is_locked)
338 {
339         union acpi_operand_object *handler_obj;
340         union acpi_operand_object *obj_desc;
341         union acpi_operand_object *start_desc;
342         union acpi_operand_object **last_obj_ptr;
343         acpi_adr_space_setup region_setup;
344         void **region_context;
345         union acpi_operand_object *region_obj2;
346         acpi_status status;
347
348         ACPI_FUNCTION_TRACE(ev_detach_region);
349
350         region_obj2 = acpi_ns_get_secondary_object(region_obj);
351         if (!region_obj2) {
352                 return_VOID;
353         }
354         region_context = &region_obj2->extra.region_context;
355
356         /* Get the address handler from the region object */
357
358         handler_obj = region_obj->region.handler;
359         if (!handler_obj) {
360
361                 /* This region has no handler, all done */
362
363                 return_VOID;
364         }
365
366         /* Find this region in the handler's list */
367
368         obj_desc = handler_obj->address_space.region_list;
369         start_desc = obj_desc;
370         last_obj_ptr = &handler_obj->address_space.region_list;
371
372         while (obj_desc) {
373
374                 /* Is this the correct Region? */
375
376                 if (obj_desc == region_obj) {
377                         ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
378                                           "Removing Region %p from address handler %p\n",
379                                           region_obj, handler_obj));
380
381                         /* This is it, remove it from the handler's list */
382
383                         *last_obj_ptr = obj_desc->region.next;
384                         obj_desc->region.next = NULL;   /* Must clear field */
385
386                         if (acpi_ns_is_locked) {
387                                 status =
388                                     acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
389                                 if (ACPI_FAILURE(status)) {
390                                         return_VOID;
391                                 }
392                         }
393
394                         /* Now stop region accesses by executing the _REG method */
395
396                         status =
397                             acpi_ev_execute_reg_method(region_obj,
398                                                        ACPI_REG_DISCONNECT);
399                         if (ACPI_FAILURE(status)) {
400                                 ACPI_EXCEPTION((AE_INFO, status,
401                                                 "from region _REG, [%s]",
402                                                 acpi_ut_get_region_name
403                                                 (region_obj->region.space_id)));
404                         }
405
406                         if (acpi_ns_is_locked) {
407                                 status =
408                                     acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
409                                 if (ACPI_FAILURE(status)) {
410                                         return_VOID;
411                                 }
412                         }
413
414                         /*
415                          * If the region has been activated, call the setup handler with
416                          * the deactivate notification
417                          */
418                         if (region_obj->region.flags & AOPOBJ_SETUP_COMPLETE) {
419                                 region_setup = handler_obj->address_space.setup;
420                                 status =
421                                     region_setup(region_obj,
422                                                  ACPI_REGION_DEACTIVATE,
423                                                  handler_obj->address_space.
424                                                  context, region_context);
425
426                                 /*
427                                  * region_context should have been released by the deactivate
428                                  * operation. We don't need access to it anymore here.
429                                  */
430                                 if (region_context) {
431                                         *region_context = NULL;
432                                 }
433
434                                 /* Init routine may fail, Just ignore errors */
435
436                                 if (ACPI_FAILURE(status)) {
437                                         ACPI_EXCEPTION((AE_INFO, status,
438                                                         "from region handler - deactivate, [%s]",
439                                                         acpi_ut_get_region_name
440                                                         (region_obj->region.
441                                                          space_id)));
442                                 }
443
444                                 region_obj->region.flags &=
445                                     ~(AOPOBJ_SETUP_COMPLETE);
446                         }
447
448                         /*
449                          * Remove handler reference in the region
450                          *
451                          * NOTE: this doesn't mean that the region goes away, the region
452                          * is just inaccessible as indicated to the _REG method
453                          *
454                          * If the region is on the handler's list, this must be the
455                          * region's handler
456                          */
457                         region_obj->region.handler = NULL;
458                         acpi_ut_remove_reference(handler_obj);
459
460                         return_VOID;
461                 }
462
463                 /* Walk the linked list of handlers */
464
465                 last_obj_ptr = &obj_desc->region.next;
466                 obj_desc = obj_desc->region.next;
467
468                 /* Prevent infinite loop if list is corrupted */
469
470                 if (obj_desc == start_desc) {
471                         ACPI_ERROR((AE_INFO,
472                                     "Circular handler list in region object %p",
473                                     region_obj));
474                         return_VOID;
475                 }
476         }
477
478         /* If we get here, the region was not in the handler's region list */
479
480         ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
481                           "Cannot remove region %p from address handler %p\n",
482                           region_obj, handler_obj));
483
484         return_VOID;
485 }
486
487 /*******************************************************************************
488  *
489  * FUNCTION:    acpi_ev_attach_region
490  *
491  * PARAMETERS:  handler_obj         - Handler Object
492  *              region_obj          - Region Object
493  *              acpi_ns_is_locked   - Namespace Region Already Locked?
494  *
495  * RETURN:      None
496  *
497  * DESCRIPTION: Create the association between the handler and the region
498  *              this is a two way association.
499  *
500  ******************************************************************************/
501
502 acpi_status
503 acpi_ev_attach_region(union acpi_operand_object *handler_obj,
504                       union acpi_operand_object *region_obj,
505                       u8 acpi_ns_is_locked)
506 {
507
508         ACPI_FUNCTION_TRACE(ev_attach_region);
509
510         /* Install the region's handler */
511
512         if (region_obj->region.handler) {
513                 return_ACPI_STATUS(AE_ALREADY_EXISTS);
514         }
515
516         ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
517                           "Adding Region [%4.4s] %p to address handler %p [%s]\n",
518                           acpi_ut_get_node_name(region_obj->region.node),
519                           region_obj, handler_obj,
520                           acpi_ut_get_region_name(region_obj->region.
521                                                   space_id)));
522
523         /* Link this region to the front of the handler's list */
524
525         region_obj->region.next = handler_obj->address_space.region_list;
526         handler_obj->address_space.region_list = region_obj;
527         region_obj->region.handler = handler_obj;
528         acpi_ut_add_reference(handler_obj);
529
530         return_ACPI_STATUS(AE_OK);
531 }
532
533 /*******************************************************************************
534  *
535  * FUNCTION:    acpi_ev_execute_reg_method
536  *
537  * PARAMETERS:  region_obj          - Region object
538  *              function            - Passed to _REG: On (1) or Off (0)
539  *
540  * RETURN:      Status
541  *
542  * DESCRIPTION: Execute _REG method for a region
543  *
544  ******************************************************************************/
545
546 acpi_status
547 acpi_ev_execute_reg_method(union acpi_operand_object *region_obj, u32 function)
548 {
549         struct acpi_evaluate_info *info;
550         union acpi_operand_object *args[3];
551         union acpi_operand_object *region_obj2;
552         const acpi_name *reg_name_ptr =
553             ACPI_CAST_PTR(acpi_name, METHOD_NAME__REG);
554         struct acpi_namespace_node *method_node;
555         struct acpi_namespace_node *node;
556         acpi_status status;
557
558         ACPI_FUNCTION_TRACE(ev_execute_reg_method);
559
560         if (!acpi_gbl_namespace_initialized ||
561             region_obj->region.handler == NULL) {
562                 return_ACPI_STATUS(AE_OK);
563         }
564
565         region_obj2 = acpi_ns_get_secondary_object(region_obj);
566         if (!region_obj2) {
567                 return_ACPI_STATUS(AE_NOT_EXIST);
568         }
569
570         /*
571          * Find any "_REG" method associated with this region definition.
572          * The method should always be updated as this function may be
573          * invoked after a namespace change.
574          */
575         node = region_obj->region.node->parent;
576         status =
577             acpi_ns_search_one_scope(*reg_name_ptr, node, ACPI_TYPE_METHOD,
578                                      &method_node);
579         if (ACPI_SUCCESS(status)) {
580                 /*
581                  * The _REG method is optional and there can be only one per
582                  * region definition. This will be executed when the handler is
583                  * attached or removed.
584                  */
585                 region_obj2->extra.method_REG = method_node;
586         }
587         if (region_obj2->extra.method_REG == NULL) {
588                 return_ACPI_STATUS(AE_OK);
589         }
590
591         /* _REG(DISCONNECT) should be paired with _REG(CONNECT) */
592
593         if ((function == ACPI_REG_CONNECT &&
594              region_obj->common.flags & AOPOBJ_REG_CONNECTED) ||
595             (function == ACPI_REG_DISCONNECT &&
596              !(region_obj->common.flags & AOPOBJ_REG_CONNECTED))) {
597                 return_ACPI_STATUS(AE_OK);
598         }
599
600         /* Allocate and initialize the evaluation information block */
601
602         info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
603         if (!info) {
604                 return_ACPI_STATUS(AE_NO_MEMORY);
605         }
606
607         info->prefix_node = region_obj2->extra.method_REG;
608         info->relative_pathname = NULL;
609         info->parameters = args;
610         info->flags = ACPI_IGNORE_RETURN_VALUE;
611
612         /*
613          * The _REG method has two arguments:
614          *
615          * arg0 - Integer:
616          *  Operation region space ID Same value as region_obj->Region.space_id
617          *
618          * arg1 - Integer:
619          *  connection status 1 for connecting the handler, 0 for disconnecting
620          *  the handler (Passed as a parameter)
621          */
622         args[0] =
623             acpi_ut_create_integer_object((u64)region_obj->region.space_id);
624         if (!args[0]) {
625                 status = AE_NO_MEMORY;
626                 goto cleanup1;
627         }
628
629         args[1] = acpi_ut_create_integer_object((u64)function);
630         if (!args[1]) {
631                 status = AE_NO_MEMORY;
632                 goto cleanup2;
633         }
634
635         args[2] = NULL;         /* Terminate list */
636
637         /* Execute the method, no return value */
638
639         ACPI_DEBUG_EXEC(acpi_ut_display_init_pathname
640                         (ACPI_TYPE_METHOD, info->prefix_node, NULL));
641
642         status = acpi_ns_evaluate(info);
643         acpi_ut_remove_reference(args[1]);
644
645         if (ACPI_FAILURE(status)) {
646                 goto cleanup2;
647         }
648
649         if (function == ACPI_REG_CONNECT) {
650                 region_obj->common.flags |= AOPOBJ_REG_CONNECTED;
651         } else {
652                 region_obj->common.flags &= ~AOPOBJ_REG_CONNECTED;
653         }
654
655 cleanup2:
656         acpi_ut_remove_reference(args[0]);
657
658 cleanup1:
659         ACPI_FREE(info);
660         return_ACPI_STATUS(status);
661 }
662
663 /*******************************************************************************
664  *
665  * FUNCTION:    acpi_ev_execute_reg_methods
666  *
667  * PARAMETERS:  node            - Namespace node for the device
668  *              space_id        - The address space ID
669  *              function        - Passed to _REG: On (1) or Off (0)
670  *
671  * RETURN:      None
672  *
673  * DESCRIPTION: Run all _REG methods for the input Space ID;
674  *              Note: assumes namespace is locked, or system init time.
675  *
676  ******************************************************************************/
677
678 void
679 acpi_ev_execute_reg_methods(struct acpi_namespace_node *node,
680                             acpi_adr_space_type space_id, u32 function)
681 {
682         struct acpi_reg_walk_info info;
683
684         ACPI_FUNCTION_TRACE(ev_execute_reg_methods);
685
686         /*
687          * These address spaces do not need a call to _REG, since the ACPI
688          * specification defines them as: "must always be accessible". Since
689          * they never change state (never become unavailable), no need to ever
690          * call _REG on them. Also, a data_table is not a "real" address space,
691          * so do not call _REG. September 2018.
692          */
693         if ((space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) ||
694             (space_id == ACPI_ADR_SPACE_SYSTEM_IO) ||
695             (space_id == ACPI_ADR_SPACE_DATA_TABLE)) {
696                 return_VOID;
697         }
698
699         info.space_id = space_id;
700         info.function = function;
701         info.reg_run_count = 0;
702
703         ACPI_DEBUG_PRINT_RAW((ACPI_DB_NAMES,
704                               "    Running _REG methods for SpaceId %s\n",
705                               acpi_ut_get_region_name(info.space_id)));
706
707         /*
708          * Run all _REG methods for all Operation Regions for this space ID. This
709          * is a separate walk in order to handle any interdependencies between
710          * regions and _REG methods. (i.e. handlers must be installed for all
711          * regions of this Space ID before we can run any _REG methods)
712          */
713         (void)acpi_ns_walk_namespace(ACPI_TYPE_ANY, node, ACPI_UINT32_MAX,
714                                      ACPI_NS_WALK_UNLOCK, acpi_ev_reg_run, NULL,
715                                      &info, NULL);
716
717         /* Special case for EC: handle "orphan" _REG methods with no region */
718
719         if (space_id == ACPI_ADR_SPACE_EC) {
720                 acpi_ev_orphan_ec_reg_method(node);
721         }
722
723         ACPI_DEBUG_PRINT_RAW((ACPI_DB_NAMES,
724                               "    Executed %u _REG methods for SpaceId %s\n",
725                               info.reg_run_count,
726                               acpi_ut_get_region_name(info.space_id)));
727
728         return_VOID;
729 }
730
731 /*******************************************************************************
732  *
733  * FUNCTION:    acpi_ev_reg_run
734  *
735  * PARAMETERS:  walk_namespace callback
736  *
737  * DESCRIPTION: Run _REG method for region objects of the requested spaceID
738  *
739  ******************************************************************************/
740
741 static acpi_status
742 acpi_ev_reg_run(acpi_handle obj_handle,
743                 u32 level, void *context, void **return_value)
744 {
745         union acpi_operand_object *obj_desc;
746         struct acpi_namespace_node *node;
747         acpi_status status;
748         struct acpi_reg_walk_info *info;
749
750         info = ACPI_CAST_PTR(struct acpi_reg_walk_info, context);
751
752         /* Convert and validate the device handle */
753
754         node = acpi_ns_validate_handle(obj_handle);
755         if (!node) {
756                 return (AE_BAD_PARAMETER);
757         }
758
759         /*
760          * We only care about regions and objects that are allowed to have
761          * address space handlers
762          */
763         if ((node->type != ACPI_TYPE_REGION) && (node != acpi_gbl_root_node)) {
764                 return (AE_OK);
765         }
766
767         /* Check for an existing internal object */
768
769         obj_desc = acpi_ns_get_attached_object(node);
770         if (!obj_desc) {
771
772                 /* No object, just exit */
773
774                 return (AE_OK);
775         }
776
777         /* Object is a Region */
778
779         if (obj_desc->region.space_id != info->space_id) {
780
781                 /* This region is for a different address space, just ignore it */
782
783                 return (AE_OK);
784         }
785
786         info->reg_run_count++;
787         status = acpi_ev_execute_reg_method(obj_desc, info->function);
788         return (status);
789 }
790
791 /*******************************************************************************
792  *
793  * FUNCTION:    acpi_ev_orphan_ec_reg_method
794  *
795  * PARAMETERS:  ec_device_node      - Namespace node for an EC device
796  *
797  * RETURN:      None
798  *
799  * DESCRIPTION: Execute an "orphan" _REG method that appears under the EC
800  *              device. This is a _REG method that has no corresponding region
801  *              within the EC device scope. The orphan _REG method appears to
802  *              have been enabled by the description of the ECDT in the ACPI
803  *              specification: "The availability of the region space can be
804  *              detected by providing a _REG method object underneath the
805  *              Embedded Controller device."
806  *
807  *              To quickly access the EC device, we use the ec_device_node used
808  *              during EC handler installation. Otherwise, we would need to
809  *              perform a time consuming namespace walk, executing _HID
810  *              methods to find the EC device.
811  *
812  *  MUTEX:      Assumes the namespace is locked
813  *
814  ******************************************************************************/
815
816 static void
817 acpi_ev_orphan_ec_reg_method(struct acpi_namespace_node *ec_device_node)
818 {
819         acpi_handle reg_method;
820         struct acpi_namespace_node *next_node;
821         acpi_status status;
822         struct acpi_object_list args;
823         union acpi_object objects[2];
824
825         ACPI_FUNCTION_TRACE(ev_orphan_ec_reg_method);
826
827         if (!ec_device_node) {
828                 return_VOID;
829         }
830
831         /* Namespace is currently locked, must release */
832
833         (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
834
835         /* Get a handle to a _REG method immediately under the EC device */
836
837         status = acpi_get_handle(ec_device_node, METHOD_NAME__REG, &reg_method);
838         if (ACPI_FAILURE(status)) {
839                 goto exit;      /* There is no _REG method present */
840         }
841
842         /*
843          * Execute the _REG method only if there is no Operation Region in
844          * this scope with the Embedded Controller space ID. Otherwise, it
845          * will already have been executed. Note, this allows for Regions
846          * with other space IDs to be present; but the code below will then
847          * execute the _REG method with the embedded_control space_ID argument.
848          */
849         next_node = acpi_ns_get_next_node(ec_device_node, NULL);
850         while (next_node) {
851                 if ((next_node->type == ACPI_TYPE_REGION) &&
852                     (next_node->object) &&
853                     (next_node->object->region.space_id == ACPI_ADR_SPACE_EC)) {
854                         goto exit;      /* Do not execute the _REG */
855                 }
856
857                 next_node = acpi_ns_get_next_node(ec_device_node, next_node);
858         }
859
860         /* Evaluate the _REG(embedded_control,Connect) method */
861
862         args.count = 2;
863         args.pointer = objects;
864         objects[0].type = ACPI_TYPE_INTEGER;
865         objects[0].integer.value = ACPI_ADR_SPACE_EC;
866         objects[1].type = ACPI_TYPE_INTEGER;
867         objects[1].integer.value = ACPI_REG_CONNECT;
868
869         (void)acpi_evaluate_object(reg_method, NULL, &args, NULL);
870
871 exit:
872         /* We ignore all errors from above, don't care */
873
874         (void)acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
875         return_VOID;
876 }