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