ACPICA: Reformat comments, no functional changes
[linux-2.6-block.git] / drivers / acpi / events / evregion.c
1 /******************************************************************************
2  *
3  * Module Name: evregion - ACPI address_space (op_region) handler dispatch
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2008, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43
44 #include <acpi/acpi.h>
45 #include <acpi/acevents.h>
46 #include <acpi/acnamesp.h>
47 #include <acpi/acinterp.h>
48
49 #define _COMPONENT          ACPI_EVENTS
50 ACPI_MODULE_NAME("evregion")
51
52 /* Local prototypes */
53 static acpi_status
54 acpi_ev_reg_run(acpi_handle obj_handle,
55                 u32 level, void *context, void **return_value);
56
57 static acpi_status
58 acpi_ev_install_handler(acpi_handle obj_handle,
59                         u32 level, void *context, void **return_value);
60
61 /* These are the address spaces that will get default handlers */
62
63 #define ACPI_NUM_DEFAULT_SPACES     4
64
65 static u8 acpi_gbl_default_address_spaces[ACPI_NUM_DEFAULT_SPACES] = {
66         ACPI_ADR_SPACE_SYSTEM_MEMORY,
67         ACPI_ADR_SPACE_SYSTEM_IO,
68         ACPI_ADR_SPACE_PCI_CONFIG,
69         ACPI_ADR_SPACE_DATA_TABLE
70 };
71
72 /*******************************************************************************
73  *
74  * FUNCTION:    acpi_ev_install_region_handlers
75  *
76  * PARAMETERS:  None
77  *
78  * RETURN:      Status
79  *
80  * DESCRIPTION: Installs the core subsystem default address space handlers.
81  *
82  ******************************************************************************/
83
84 acpi_status acpi_ev_install_region_handlers(void)
85 {
86         acpi_status status;
87         u32 i;
88
89         ACPI_FUNCTION_TRACE(ev_install_region_handlers);
90
91         status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
92         if (ACPI_FAILURE(status)) {
93                 return_ACPI_STATUS(status);
94         }
95
96         /*
97          * All address spaces (PCI Config, EC, SMBus) are scope dependent and
98          * registration must occur for a specific device.
99          *
100          * In the case of the system memory and IO address spaces there is
101          * currently no device associated with the address space. For these we
102          * use the root.
103          *
104          * We install the default PCI config space handler at the root so that
105          * this space is immediately available even though the we have not
106          * enumerated all the PCI Root Buses yet. This is to conform to the ACPI
107          * specification which states that the PCI config space must be always
108          * available -- even though we are nowhere near ready to find the PCI root
109          * buses at this point.
110          *
111          * NOTE: We ignore AE_ALREADY_EXISTS because this means that a handler
112          * has already been installed (via acpi_install_address_space_handler).
113          * Similar for AE_SAME_HANDLER.
114          */
115         for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++) {
116                 status = acpi_ev_install_space_handler(acpi_gbl_root_node,
117                                                        acpi_gbl_default_address_spaces
118                                                        [i],
119                                                        ACPI_DEFAULT_HANDLER,
120                                                        NULL, NULL);
121                 switch (status) {
122                 case AE_OK:
123                 case AE_SAME_HANDLER:
124                 case AE_ALREADY_EXISTS:
125
126                         /* These exceptions are all OK */
127
128                         status = AE_OK;
129                         break;
130
131                 default:
132
133                         goto unlock_and_exit;
134                 }
135         }
136
137       unlock_and_exit:
138         (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
139         return_ACPI_STATUS(status);
140 }
141
142 /*******************************************************************************
143  *
144  * FUNCTION:    acpi_ev_initialize_op_regions
145  *
146  * PARAMETERS:  None
147  *
148  * RETURN:      Status
149  *
150  * DESCRIPTION: Execute _REG methods for all Operation Regions that have
151  *              an installed default region handler.
152  *
153  ******************************************************************************/
154
155 acpi_status acpi_ev_initialize_op_regions(void)
156 {
157         acpi_status status;
158         u32 i;
159
160         ACPI_FUNCTION_TRACE(ev_initialize_op_regions);
161
162         status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
163         if (ACPI_FAILURE(status)) {
164                 return_ACPI_STATUS(status);
165         }
166
167         /* Run the _REG methods for op_regions in each default address space */
168
169         for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++) {
170                 /*
171                  * TBD: Make sure handler is the DEFAULT handler, otherwise
172                  * _REG will have already been run.
173                  */
174                 status = acpi_ev_execute_reg_methods(acpi_gbl_root_node,
175                                                      acpi_gbl_default_address_spaces
176                                                      [i]);
177         }
178
179         (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
180         return_ACPI_STATUS(status);
181 }
182
183 /*******************************************************************************
184  *
185  * FUNCTION:    acpi_ev_execute_reg_method
186  *
187  * PARAMETERS:  region_obj          - Region object
188  *              Function            - Passed to _REG: On (1) or Off (0)
189  *
190  * RETURN:      Status
191  *
192  * DESCRIPTION: Execute _REG method for a region
193  *
194  ******************************************************************************/
195
196 acpi_status
197 acpi_ev_execute_reg_method(union acpi_operand_object *region_obj, u32 function)
198 {
199         struct acpi_evaluate_info *info;
200         union acpi_operand_object *args[3];
201         union acpi_operand_object *region_obj2;
202         acpi_status status;
203
204         ACPI_FUNCTION_TRACE(ev_execute_reg_method);
205
206         region_obj2 = acpi_ns_get_secondary_object(region_obj);
207         if (!region_obj2) {
208                 return_ACPI_STATUS(AE_NOT_EXIST);
209         }
210
211         if (region_obj2->extra.method_REG == NULL) {
212                 return_ACPI_STATUS(AE_OK);
213         }
214
215         /* Allocate and initialize the evaluation information block */
216
217         info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
218         if (!info) {
219                 return_ACPI_STATUS(AE_NO_MEMORY);
220         }
221
222         info->prefix_node = region_obj2->extra.method_REG;
223         info->pathname = NULL;
224         info->parameters = args;
225         info->flags = ACPI_IGNORE_RETURN_VALUE;
226
227         /*
228          * The _REG method has two arguments:
229          *
230          * Arg0 - Integer:
231          *  Operation region space ID Same value as region_obj->Region.space_id
232          *
233          * Arg1 - Integer:
234          *  connection status 1 for connecting the handler, 0 for disconnecting
235          *  the handler (Passed as a parameter)
236          */
237         args[0] = acpi_ut_create_internal_object(ACPI_TYPE_INTEGER);
238         if (!args[0]) {
239                 status = AE_NO_MEMORY;
240                 goto cleanup1;
241         }
242
243         args[1] = acpi_ut_create_internal_object(ACPI_TYPE_INTEGER);
244         if (!args[1]) {
245                 status = AE_NO_MEMORY;
246                 goto cleanup2;
247         }
248
249         /* Setup the parameter objects */
250
251         args[0]->integer.value = region_obj->region.space_id;
252         args[1]->integer.value = function;
253         args[2] = NULL;
254
255         /* Execute the method, no return value */
256
257         ACPI_DEBUG_EXEC(acpi_ut_display_init_pathname
258                         (ACPI_TYPE_METHOD, info->prefix_node, NULL));
259
260         status = acpi_ns_evaluate(info);
261         acpi_ut_remove_reference(args[1]);
262
263       cleanup2:
264         acpi_ut_remove_reference(args[0]);
265
266       cleanup1:
267         ACPI_FREE(info);
268         return_ACPI_STATUS(status);
269 }
270
271 /*******************************************************************************
272  *
273  * FUNCTION:    acpi_ev_address_space_dispatch
274  *
275  * PARAMETERS:  region_obj          - Internal region object
276  *              Function            - Read or Write operation
277  *              Address             - Where in the space to read or write
278  *              bit_width           - Field width in bits (8, 16, 32, or 64)
279  *              Value               - Pointer to in or out value, must be
280  *                                    full 64-bit acpi_integer
281  *
282  * RETURN:      Status
283  *
284  * DESCRIPTION: Dispatch an address space or operation region access to
285  *              a previously installed handler.
286  *
287  ******************************************************************************/
288
289 acpi_status
290 acpi_ev_address_space_dispatch(union acpi_operand_object *region_obj,
291                                u32 function,
292                                acpi_physical_address address,
293                                u32 bit_width, acpi_integer * value)
294 {
295         acpi_status status;
296         acpi_adr_space_handler handler;
297         acpi_adr_space_setup region_setup;
298         union acpi_operand_object *handler_desc;
299         union acpi_operand_object *region_obj2;
300         void *region_context = NULL;
301
302         ACPI_FUNCTION_TRACE(ev_address_space_dispatch);
303
304         region_obj2 = acpi_ns_get_secondary_object(region_obj);
305         if (!region_obj2) {
306                 return_ACPI_STATUS(AE_NOT_EXIST);
307         }
308
309         /* Ensure that there is a handler associated with this region */
310
311         handler_desc = region_obj->region.handler;
312         if (!handler_desc) {
313                 ACPI_ERROR((AE_INFO,
314                             "No handler for Region [%4.4s] (%p) [%s]",
315                             acpi_ut_get_node_name(region_obj->region.node),
316                             region_obj,
317                             acpi_ut_get_region_name(region_obj->region.
318                                                     space_id)));
319
320                 return_ACPI_STATUS(AE_NOT_EXIST);
321         }
322
323         /*
324          * It may be the case that the region has never been initialized.
325          * Some types of regions require special init code
326          */
327         if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE)) {
328
329                 /* This region has not been initialized yet, do it */
330
331                 region_setup = handler_desc->address_space.setup;
332                 if (!region_setup) {
333
334                         /* No initialization routine, exit with error */
335
336                         ACPI_ERROR((AE_INFO,
337                                     "No init routine for region(%p) [%s]",
338                                     region_obj,
339                                     acpi_ut_get_region_name(region_obj->region.
340                                                             space_id)));
341                         return_ACPI_STATUS(AE_NOT_EXIST);
342                 }
343
344                 /*
345                  * We must exit the interpreter because the region setup will
346                  * potentially execute control methods (for example, the _REG method
347                  * for this region)
348                  */
349                 acpi_ex_exit_interpreter();
350
351                 status = region_setup(region_obj, ACPI_REGION_ACTIVATE,
352                                       handler_desc->address_space.context,
353                                       &region_context);
354
355                 /* Re-enter the interpreter */
356
357                 acpi_ex_enter_interpreter();
358
359                 /* Check for failure of the Region Setup */
360
361                 if (ACPI_FAILURE(status)) {
362                         ACPI_EXCEPTION((AE_INFO, status,
363                                         "During region initialization: [%s]",
364                                         acpi_ut_get_region_name(region_obj->
365                                                                 region.
366                                                                 space_id)));
367                         return_ACPI_STATUS(status);
368                 }
369
370                 /* Region initialization may have been completed by region_setup */
371
372                 if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE)) {
373                         region_obj->region.flags |= AOPOBJ_SETUP_COMPLETE;
374
375                         if (region_obj2->extra.region_context) {
376
377                                 /* The handler for this region was already installed */
378
379                                 ACPI_FREE(region_context);
380                         } else {
381                                 /*
382                                  * Save the returned context for use in all accesses to
383                                  * this particular region
384                                  */
385                                 region_obj2->extra.region_context =
386                                     region_context;
387                         }
388                 }
389         }
390
391         /* We have everything we need, we can invoke the address space handler */
392
393         handler = handler_desc->address_space.handler;
394
395         ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
396                           "Handler %p (@%p) Address %8.8X%8.8X [%s]\n",
397                           &region_obj->region.handler->address_space, handler,
398                           ACPI_FORMAT_NATIVE_UINT(address),
399                           acpi_ut_get_region_name(region_obj->region.
400                                                   space_id)));
401
402         if (!(handler_desc->address_space.handler_flags &
403               ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {
404                 /*
405                  * For handlers other than the default (supplied) handlers, we must
406                  * exit the interpreter because the handler *might* block -- we don't
407                  * know what it will do, so we can't hold the lock on the intepreter.
408                  */
409                 acpi_ex_exit_interpreter();
410         }
411
412         /* Call the handler */
413
414         status = handler(function, address, bit_width, value,
415                          handler_desc->address_space.context,
416                          region_obj2->extra.region_context);
417
418         if (ACPI_FAILURE(status)) {
419                 ACPI_EXCEPTION((AE_INFO, status, "Returned by Handler for [%s]",
420                                 acpi_ut_get_region_name(region_obj->region.
421                                                         space_id)));
422         }
423
424         if (!(handler_desc->address_space.handler_flags &
425               ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {
426                 /*
427                  * We just returned from a non-default handler, we must re-enter the
428                  * interpreter
429                  */
430                 acpi_ex_enter_interpreter();
431         }
432
433         return_ACPI_STATUS(status);
434 }
435
436 /*******************************************************************************
437  *
438  * FUNCTION:    acpi_ev_detach_region
439  *
440  * PARAMETERS:  region_obj          - Region Object
441  *              acpi_ns_is_locked   - Namespace Region Already Locked?
442  *
443  * RETURN:      None
444  *
445  * DESCRIPTION: Break the association between the handler and the region
446  *              this is a two way association.
447  *
448  ******************************************************************************/
449
450 void
451 acpi_ev_detach_region(union acpi_operand_object *region_obj,
452                       u8 acpi_ns_is_locked)
453 {
454         union acpi_operand_object *handler_obj;
455         union acpi_operand_object *obj_desc;
456         union acpi_operand_object **last_obj_ptr;
457         acpi_adr_space_setup region_setup;
458         void **region_context;
459         union acpi_operand_object *region_obj2;
460         acpi_status status;
461
462         ACPI_FUNCTION_TRACE(ev_detach_region);
463
464         region_obj2 = acpi_ns_get_secondary_object(region_obj);
465         if (!region_obj2) {
466                 return_VOID;
467         }
468         region_context = &region_obj2->extra.region_context;
469
470         /* Get the address handler from the region object */
471
472         handler_obj = region_obj->region.handler;
473         if (!handler_obj) {
474
475                 /* This region has no handler, all done */
476
477                 return_VOID;
478         }
479
480         /* Find this region in the handler's list */
481
482         obj_desc = handler_obj->address_space.region_list;
483         last_obj_ptr = &handler_obj->address_space.region_list;
484
485         while (obj_desc) {
486
487                 /* Is this the correct Region? */
488
489                 if (obj_desc == region_obj) {
490                         ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
491                                           "Removing Region %p from address handler %p\n",
492                                           region_obj, handler_obj));
493
494                         /* This is it, remove it from the handler's list */
495
496                         *last_obj_ptr = obj_desc->region.next;
497                         obj_desc->region.next = NULL;   /* Must clear field */
498
499                         if (acpi_ns_is_locked) {
500                                 status =
501                                     acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
502                                 if (ACPI_FAILURE(status)) {
503                                         return_VOID;
504                                 }
505                         }
506
507                         /* Now stop region accesses by executing the _REG method */
508
509                         status = acpi_ev_execute_reg_method(region_obj, 0);
510                         if (ACPI_FAILURE(status)) {
511                                 ACPI_EXCEPTION((AE_INFO, status,
512                                                 "from region _REG, [%s]",
513                                                 acpi_ut_get_region_name
514                                                 (region_obj->region.space_id)));
515                         }
516
517                         if (acpi_ns_is_locked) {
518                                 status =
519                                     acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
520                                 if (ACPI_FAILURE(status)) {
521                                         return_VOID;
522                                 }
523                         }
524
525                         /*
526                          * If the region has been activated, call the setup handler with
527                          * the deactivate notification
528                          */
529                         if (region_obj->region.flags & AOPOBJ_SETUP_COMPLETE) {
530                                 region_setup = handler_obj->address_space.setup;
531                                 status =
532                                     region_setup(region_obj,
533                                                  ACPI_REGION_DEACTIVATE,
534                                                  handler_obj->address_space.
535                                                  context, region_context);
536
537                                 /* Init routine may fail, Just ignore errors */
538
539                                 if (ACPI_FAILURE(status)) {
540                                         ACPI_EXCEPTION((AE_INFO, status,
541                                                         "from region handler - deactivate, [%s]",
542                                                         acpi_ut_get_region_name
543                                                         (region_obj->region.
544                                                          space_id)));
545                                 }
546
547                                 region_obj->region.flags &=
548                                     ~(AOPOBJ_SETUP_COMPLETE);
549                         }
550
551                         /*
552                          * Remove handler reference in the region
553                          *
554                          * NOTE: this doesn't mean that the region goes away, the region
555                          * is just inaccessible as indicated to the _REG method
556                          *
557                          * If the region is on the handler's list, this must be the
558                          * region's handler
559                          */
560                         region_obj->region.handler = NULL;
561                         acpi_ut_remove_reference(handler_obj);
562
563                         return_VOID;
564                 }
565
566                 /* Walk the linked list of handlers */
567
568                 last_obj_ptr = &obj_desc->region.next;
569                 obj_desc = obj_desc->region.next;
570         }
571
572         /* If we get here, the region was not in the handler's region list */
573
574         ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
575                           "Cannot remove region %p from address handler %p\n",
576                           region_obj, handler_obj));
577
578         return_VOID;
579 }
580
581 /*******************************************************************************
582  *
583  * FUNCTION:    acpi_ev_attach_region
584  *
585  * PARAMETERS:  handler_obj         - Handler Object
586  *              region_obj          - Region Object
587  *              acpi_ns_is_locked   - Namespace Region Already Locked?
588  *
589  * RETURN:      None
590  *
591  * DESCRIPTION: Create the association between the handler and the region
592  *              this is a two way association.
593  *
594  ******************************************************************************/
595
596 acpi_status
597 acpi_ev_attach_region(union acpi_operand_object *handler_obj,
598                       union acpi_operand_object *region_obj,
599                       u8 acpi_ns_is_locked)
600 {
601
602         ACPI_FUNCTION_TRACE(ev_attach_region);
603
604         ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
605                           "Adding Region [%4.4s] %p to address handler %p [%s]\n",
606                           acpi_ut_get_node_name(region_obj->region.node),
607                           region_obj, handler_obj,
608                           acpi_ut_get_region_name(region_obj->region.
609                                                   space_id)));
610
611         /* Link this region to the front of the handler's list */
612
613         region_obj->region.next = handler_obj->address_space.region_list;
614         handler_obj->address_space.region_list = region_obj;
615
616         /* Install the region's handler */
617
618         if (region_obj->region.handler) {
619                 return_ACPI_STATUS(AE_ALREADY_EXISTS);
620         }
621
622         region_obj->region.handler = handler_obj;
623         acpi_ut_add_reference(handler_obj);
624
625         return_ACPI_STATUS(AE_OK);
626 }
627
628 /*******************************************************************************
629  *
630  * FUNCTION:    acpi_ev_install_handler
631  *
632  * PARAMETERS:  walk_namespace callback
633  *
634  * DESCRIPTION: This routine installs an address handler into objects that are
635  *              of type Region or Device.
636  *
637  *              If the Object is a Device, and the device has a handler of
638  *              the same type then the search is terminated in that branch.
639  *
640  *              This is because the existing handler is closer in proximity
641  *              to any more regions than the one we are trying to install.
642  *
643  ******************************************************************************/
644
645 static acpi_status
646 acpi_ev_install_handler(acpi_handle obj_handle,
647                         u32 level, void *context, void **return_value)
648 {
649         union acpi_operand_object *handler_obj;
650         union acpi_operand_object *next_handler_obj;
651         union acpi_operand_object *obj_desc;
652         struct acpi_namespace_node *node;
653         acpi_status status;
654
655         ACPI_FUNCTION_NAME(ev_install_handler);
656
657         handler_obj = (union acpi_operand_object *)context;
658
659         /* Parameter validation */
660
661         if (!handler_obj) {
662                 return (AE_OK);
663         }
664
665         /* Convert and validate the device handle */
666
667         node = acpi_ns_map_handle_to_node(obj_handle);
668         if (!node) {
669                 return (AE_BAD_PARAMETER);
670         }
671
672         /*
673          * We only care about regions and objects that are allowed to have
674          * address space handlers
675          */
676         if ((node->type != ACPI_TYPE_DEVICE) &&
677             (node->type != ACPI_TYPE_REGION) && (node != acpi_gbl_root_node)) {
678                 return (AE_OK);
679         }
680
681         /* Check for an existing internal object */
682
683         obj_desc = acpi_ns_get_attached_object(node);
684         if (!obj_desc) {
685
686                 /* No object, just exit */
687
688                 return (AE_OK);
689         }
690
691         /* Devices are handled different than regions */
692
693         if (ACPI_GET_OBJECT_TYPE(obj_desc) == ACPI_TYPE_DEVICE) {
694
695                 /* Check if this Device already has a handler for this address space */
696
697                 next_handler_obj = obj_desc->device.handler;
698                 while (next_handler_obj) {
699
700                         /* Found a handler, is it for the same address space? */
701
702                         if (next_handler_obj->address_space.space_id ==
703                             handler_obj->address_space.space_id) {
704                                 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
705                                                   "Found handler for region [%s] in device %p(%p) handler %p\n",
706                                                   acpi_ut_get_region_name
707                                                   (handler_obj->address_space.
708                                                    space_id), obj_desc,
709                                                   next_handler_obj,
710                                                   handler_obj));
711
712                                 /*
713                                  * Since the object we found it on was a device, then it
714                                  * means that someone has already installed a handler for
715                                  * the branch of the namespace from this device on. Just
716                                  * bail out telling the walk routine to not traverse this
717                                  * branch. This preserves the scoping rule for handlers.
718                                  */
719                                 return (AE_CTRL_DEPTH);
720                         }
721
722                         /* Walk the linked list of handlers attached to this device */
723
724                         next_handler_obj = next_handler_obj->address_space.next;
725                 }
726
727                 /*
728                  * As long as the device didn't have a handler for this space we
729                  * don't care about it. We just ignore it and proceed.
730                  */
731                 return (AE_OK);
732         }
733
734         /* Object is a Region */
735
736         if (obj_desc->region.space_id != handler_obj->address_space.space_id) {
737
738                 /* This region is for a different address space, just ignore it */
739
740                 return (AE_OK);
741         }
742
743         /*
744          * Now we have a region and it is for the handler's address space type.
745          *
746          * First disconnect region for any previous handler (if any)
747          */
748         acpi_ev_detach_region(obj_desc, FALSE);
749
750         /* Connect the region to the new handler */
751
752         status = acpi_ev_attach_region(handler_obj, obj_desc, FALSE);
753         return (status);
754 }
755
756 /*******************************************************************************
757  *
758  * FUNCTION:    acpi_ev_install_space_handler
759  *
760  * PARAMETERS:  Node            - Namespace node for the device
761  *              space_id        - The address space ID
762  *              Handler         - Address of the handler
763  *              Setup           - Address of the setup function
764  *              Context         - Value passed to the handler on each access
765  *
766  * RETURN:      Status
767  *
768  * DESCRIPTION: Install a handler for all op_regions of a given space_id.
769  *              Assumes namespace is locked
770  *
771  ******************************************************************************/
772
773 acpi_status
774 acpi_ev_install_space_handler(struct acpi_namespace_node * node,
775                               acpi_adr_space_type space_id,
776                               acpi_adr_space_handler handler,
777                               acpi_adr_space_setup setup, void *context)
778 {
779         union acpi_operand_object *obj_desc;
780         union acpi_operand_object *handler_obj;
781         acpi_status status;
782         acpi_object_type type;
783         u8 flags = 0;
784
785         ACPI_FUNCTION_TRACE(ev_install_space_handler);
786
787         /*
788          * This registration is valid for only the types below and the root. This
789          * is where the default handlers get placed.
790          */
791         if ((node->type != ACPI_TYPE_DEVICE) &&
792             (node->type != ACPI_TYPE_PROCESSOR) &&
793             (node->type != ACPI_TYPE_THERMAL) && (node != acpi_gbl_root_node)) {
794                 status = AE_BAD_PARAMETER;
795                 goto unlock_and_exit;
796         }
797
798         if (handler == ACPI_DEFAULT_HANDLER) {
799                 flags = ACPI_ADDR_HANDLER_DEFAULT_INSTALLED;
800
801                 switch (space_id) {
802                 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
803                         handler = acpi_ex_system_memory_space_handler;
804                         setup = acpi_ev_system_memory_region_setup;
805                         break;
806
807                 case ACPI_ADR_SPACE_SYSTEM_IO:
808                         handler = acpi_ex_system_io_space_handler;
809                         setup = acpi_ev_io_space_region_setup;
810                         break;
811
812                 case ACPI_ADR_SPACE_PCI_CONFIG:
813                         handler = acpi_ex_pci_config_space_handler;
814                         setup = acpi_ev_pci_config_region_setup;
815                         break;
816
817                 case ACPI_ADR_SPACE_CMOS:
818                         handler = acpi_ex_cmos_space_handler;
819                         setup = acpi_ev_cmos_region_setup;
820                         break;
821
822                 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
823                         handler = acpi_ex_pci_bar_space_handler;
824                         setup = acpi_ev_pci_bar_region_setup;
825                         break;
826
827                 case ACPI_ADR_SPACE_DATA_TABLE:
828                         handler = acpi_ex_data_table_space_handler;
829                         setup = NULL;
830                         break;
831
832                 default:
833                         status = AE_BAD_PARAMETER;
834                         goto unlock_and_exit;
835                 }
836         }
837
838         /* If the caller hasn't specified a setup routine, use the default */
839
840         if (!setup) {
841                 setup = acpi_ev_default_region_setup;
842         }
843
844         /* Check for an existing internal object */
845
846         obj_desc = acpi_ns_get_attached_object(node);
847         if (obj_desc) {
848                 /*
849                  * The attached device object already exists. Make sure the handler
850                  * is not already installed.
851                  */
852                 handler_obj = obj_desc->device.handler;
853
854                 /* Walk the handler list for this device */
855
856                 while (handler_obj) {
857
858                         /* Same space_id indicates a handler already installed */
859
860                         if (handler_obj->address_space.space_id == space_id) {
861                                 if (handler_obj->address_space.handler ==
862                                     handler) {
863                                         /*
864                                          * It is (relatively) OK to attempt to install the SAME
865                                          * handler twice. This can easily happen with the
866                                          * PCI_Config space.
867                                          */
868                                         status = AE_SAME_HANDLER;
869                                         goto unlock_and_exit;
870                                 } else {
871                                         /* A handler is already installed */
872
873                                         status = AE_ALREADY_EXISTS;
874                                 }
875                                 goto unlock_and_exit;
876                         }
877
878                         /* Walk the linked list of handlers */
879
880                         handler_obj = handler_obj->address_space.next;
881                 }
882         } else {
883                 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
884                                   "Creating object on Device %p while installing handler\n",
885                                   node));
886
887                 /* obj_desc does not exist, create one */
888
889                 if (node->type == ACPI_TYPE_ANY) {
890                         type = ACPI_TYPE_DEVICE;
891                 } else {
892                         type = node->type;
893                 }
894
895                 obj_desc = acpi_ut_create_internal_object(type);
896                 if (!obj_desc) {
897                         status = AE_NO_MEMORY;
898                         goto unlock_and_exit;
899                 }
900
901                 /* Init new descriptor */
902
903                 obj_desc->common.type = (u8) type;
904
905                 /* Attach the new object to the Node */
906
907                 status = acpi_ns_attach_object(node, obj_desc, type);
908
909                 /* Remove local reference to the object */
910
911                 acpi_ut_remove_reference(obj_desc);
912
913                 if (ACPI_FAILURE(status)) {
914                         goto unlock_and_exit;
915                 }
916         }
917
918         ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
919                           "Installing address handler for region %s(%X) on Device %4.4s %p(%p)\n",
920                           acpi_ut_get_region_name(space_id), space_id,
921                           acpi_ut_get_node_name(node), node, obj_desc));
922
923         /*
924          * Install the handler
925          *
926          * At this point there is no existing handler. Just allocate the object
927          * for the handler and link it into the list.
928          */
929         handler_obj =
930             acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_ADDRESS_HANDLER);
931         if (!handler_obj) {
932                 status = AE_NO_MEMORY;
933                 goto unlock_and_exit;
934         }
935
936         /* Init handler obj */
937
938         handler_obj->address_space.space_id = (u8) space_id;
939         handler_obj->address_space.handler_flags = flags;
940         handler_obj->address_space.region_list = NULL;
941         handler_obj->address_space.node = node;
942         handler_obj->address_space.handler = handler;
943         handler_obj->address_space.context = context;
944         handler_obj->address_space.setup = setup;
945
946         /* Install at head of Device.address_space list */
947
948         handler_obj->address_space.next = obj_desc->device.handler;
949
950         /*
951          * The Device object is the first reference on the handler_obj.
952          * Each region that uses the handler adds a reference.
953          */
954         obj_desc->device.handler = handler_obj;
955
956         /*
957          * Walk the namespace finding all of the regions this
958          * handler will manage.
959          *
960          * Start at the device and search the branch toward
961          * the leaf nodes until either the leaf is encountered or
962          * a device is detected that has an address handler of the
963          * same type.
964          *
965          * In either case, back up and search down the remainder
966          * of the branch
967          */
968         status = acpi_ns_walk_namespace(ACPI_TYPE_ANY, node, ACPI_UINT32_MAX,
969                                         ACPI_NS_WALK_UNLOCK,
970                                         acpi_ev_install_handler, handler_obj,
971                                         NULL);
972
973       unlock_and_exit:
974         return_ACPI_STATUS(status);
975 }
976
977 /*******************************************************************************
978  *
979  * FUNCTION:    acpi_ev_execute_reg_methods
980  *
981  * PARAMETERS:  Node            - Namespace node for the device
982  *              space_id        - The address space ID
983  *
984  * RETURN:      Status
985  *
986  * DESCRIPTION: Run all _REG methods for the input Space ID;
987  *              Note: assumes namespace is locked, or system init time.
988  *
989  ******************************************************************************/
990
991 acpi_status
992 acpi_ev_execute_reg_methods(struct acpi_namespace_node *node,
993                             acpi_adr_space_type space_id)
994 {
995         acpi_status status;
996
997         ACPI_FUNCTION_TRACE(ev_execute_reg_methods);
998
999         /*
1000          * Run all _REG methods for all Operation Regions for this space ID. This
1001          * is a separate walk in order to handle any interdependencies between
1002          * regions and _REG methods. (i.e. handlers must be installed for all
1003          * regions of this Space ID before we can run any _REG methods)
1004          */
1005         status = acpi_ns_walk_namespace(ACPI_TYPE_ANY, node, ACPI_UINT32_MAX,
1006                                         ACPI_NS_WALK_UNLOCK, acpi_ev_reg_run,
1007                                         &space_id, NULL);
1008
1009         return_ACPI_STATUS(status);
1010 }
1011
1012 /*******************************************************************************
1013  *
1014  * FUNCTION:    acpi_ev_reg_run
1015  *
1016  * PARAMETERS:  walk_namespace callback
1017  *
1018  * DESCRIPTION: Run _REG method for region objects of the requested space_iD
1019  *
1020  ******************************************************************************/
1021
1022 static acpi_status
1023 acpi_ev_reg_run(acpi_handle obj_handle,
1024                 u32 level, void *context, void **return_value)
1025 {
1026         union acpi_operand_object *obj_desc;
1027         struct acpi_namespace_node *node;
1028         acpi_adr_space_type space_id;
1029         acpi_status status;
1030
1031         space_id = *ACPI_CAST_PTR(acpi_adr_space_type, context);
1032
1033         /* Convert and validate the device handle */
1034
1035         node = acpi_ns_map_handle_to_node(obj_handle);
1036         if (!node) {
1037                 return (AE_BAD_PARAMETER);
1038         }
1039
1040         /*
1041          * We only care about regions.and objects that are allowed to have address
1042          * space handlers
1043          */
1044         if ((node->type != ACPI_TYPE_REGION) && (node != acpi_gbl_root_node)) {
1045                 return (AE_OK);
1046         }
1047
1048         /* Check for an existing internal object */
1049
1050         obj_desc = acpi_ns_get_attached_object(node);
1051         if (!obj_desc) {
1052
1053                 /* No object, just exit */
1054
1055                 return (AE_OK);
1056         }
1057
1058         /* Object is a Region */
1059
1060         if (obj_desc->region.space_id != space_id) {
1061
1062                 /* This region is for a different address space, just ignore it */
1063
1064                 return (AE_OK);
1065         }
1066
1067         status = acpi_ev_execute_reg_method(obj_desc, 1);
1068         return (status);
1069 }