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