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