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