Merge master.kernel.org:/pub/scm/linux/kernel/git/jejb/scsi-for-linus-2.6
[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 - 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 #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                 /* TBD: Make sure handler is the DEFAULT handler, otherwise
168                  * _REG will have already been run.
169                  */
170                 status = acpi_ev_execute_reg_methods(acpi_gbl_root_node,
171                                                      acpi_gbl_default_address_spaces
172                                                      [i]);
173         }
174
175         (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
176         return_ACPI_STATUS(status);
177 }
178
179 /*******************************************************************************
180  *
181  * FUNCTION:    acpi_ev_execute_reg_method
182  *
183  * PARAMETERS:  region_obj          - Region object
184  *              Function            - Passed to _REG: On (1) or Off (0)
185  *
186  * RETURN:      Status
187  *
188  * DESCRIPTION: Execute _REG method for a region
189  *
190  ******************************************************************************/
191
192 acpi_status
193 acpi_ev_execute_reg_method(union acpi_operand_object *region_obj, u32 function)
194 {
195         struct acpi_parameter_info info;
196         union acpi_operand_object *params[3];
197         union acpi_operand_object *region_obj2;
198         acpi_status status;
199
200         ACPI_FUNCTION_TRACE("ev_execute_reg_method");
201
202         region_obj2 = acpi_ns_get_secondary_object(region_obj);
203         if (!region_obj2) {
204                 return_ACPI_STATUS(AE_NOT_EXIST);
205         }
206
207         if (region_obj2->extra.method_REG == NULL) {
208                 return_ACPI_STATUS(AE_OK);
209         }
210
211         /*
212          * The _REG method has two arguments:
213          *
214          * Arg0, Integer: Operation region space ID
215          *          Same value as region_obj->Region.space_id
216          * Arg1, Integer: connection status
217          *          1 for connecting the handler,
218          *          0 for disconnecting the handler
219          *          Passed as a parameter
220          */
221         params[0] = acpi_ut_create_internal_object(ACPI_TYPE_INTEGER);
222         if (!params[0]) {
223                 return_ACPI_STATUS(AE_NO_MEMORY);
224         }
225
226         params[1] = acpi_ut_create_internal_object(ACPI_TYPE_INTEGER);
227         if (!params[1]) {
228                 status = AE_NO_MEMORY;
229                 goto cleanup;
230         }
231
232         /* Setup the parameter objects */
233
234         params[0]->integer.value = region_obj->region.space_id;
235         params[1]->integer.value = function;
236         params[2] = NULL;
237
238         info.node = region_obj2->extra.method_REG;
239         info.parameters = params;
240         info.parameter_type = ACPI_PARAM_ARGS;
241
242         /* Execute the method, no return value */
243
244         ACPI_DEBUG_EXEC(acpi_ut_display_init_pathname
245                         (ACPI_TYPE_METHOD, info.node, NULL));
246         status = acpi_ns_evaluate_by_handle(&info);
247
248         acpi_ut_remove_reference(params[1]);
249
250       cleanup:
251         acpi_ut_remove_reference(params[0]);
252
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_DEBUG_PRINT((ACPI_DB_ERROR,
299                                   "No handler for Region [%4.4s] (%p) [%s]\n",
300                                   acpi_ut_get_node_name(region_obj->region.
301                                                         node), 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                         /* No initialization routine, exit with error */
319
320                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
321                                           "No init routine for region(%p) [%s]\n",
322                                           region_obj,
323                                           acpi_ut_get_region_name(region_obj->
324                                                                   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_DEBUG_PRINT((ACPI_DB_ERROR,
351                                           "Region Init: %s [%s]\n",
352                                           acpi_format_exception(status),
353                                           acpi_ut_get_region_name(region_obj->
354                                                                   region.
355                                                                   space_id)));
356                         return_ACPI_STATUS(status);
357                 }
358
359                 /*
360                  * Region initialization may have been completed by region_setup
361                  */
362                 if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE)) {
363                         region_obj->region.flags |= AOPOBJ_SETUP_COMPLETE;
364
365                         if (region_obj2->extra.region_context) {
366                                 /* The handler for this region was already installed */
367
368                                 ACPI_MEM_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 (!
392             (handler_desc->address_space.
393              hflags & ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {
394                 /*
395                  * For handlers other than the default (supplied) handlers, we must
396                  * exit the interpreter because the handler *might* block -- we don't
397                  * know what it will do, so we can't hold the lock on the intepreter.
398                  */
399                 acpi_ex_exit_interpreter();
400         }
401
402         /* Call the handler */
403
404         status = handler(function, address, bit_width, value,
405                          handler_desc->address_space.context,
406                          region_obj2->extra.region_context);
407
408         if (ACPI_FAILURE(status)) {
409                 ACPI_REPORT_ERROR(("Handler for [%s] returned %s\n",
410                                    acpi_ut_get_region_name(region_obj->region.
411                                                            space_id),
412                                    acpi_format_exception(status)));
413         }
414
415         if (!
416             (handler_desc->address_space.
417              hflags & ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {
418                 /*
419                  * We just returned from a non-default handler, we must re-enter the
420                  * interpreter
421                  */
422                 status2 = acpi_ex_enter_interpreter();
423                 if (ACPI_FAILURE(status2)) {
424                         return_ACPI_STATUS(status2);
425                 }
426         }
427
428         return_ACPI_STATUS(status);
429 }
430
431 /*******************************************************************************
432  *
433  * FUNCTION:    acpi_ev_detach_region
434  *
435  * PARAMETERS:  region_obj          - Region Object
436  *              acpi_ns_is_locked   - Namespace Region Already Locked?
437  *
438  * RETURN:      None
439  *
440  * DESCRIPTION: Break the association between the handler and the region
441  *              this is a two way association.
442  *
443  ******************************************************************************/
444
445 void
446 acpi_ev_detach_region(union acpi_operand_object *region_obj,
447                       u8 acpi_ns_is_locked)
448 {
449         union acpi_operand_object *handler_obj;
450         union acpi_operand_object *obj_desc;
451         union acpi_operand_object **last_obj_ptr;
452         acpi_adr_space_setup region_setup;
453         void **region_context;
454         union acpi_operand_object *region_obj2;
455         acpi_status status;
456
457         ACPI_FUNCTION_TRACE("ev_detach_region");
458
459         region_obj2 = acpi_ns_get_secondary_object(region_obj);
460         if (!region_obj2) {
461                 return_VOID;
462         }
463         region_context = &region_obj2->extra.region_context;
464
465         /* Get the address handler from the region object */
466
467         handler_obj = region_obj->region.handler;
468         if (!handler_obj) {
469                 /* This region has no handler, all done */
470
471                 return_VOID;
472         }
473
474         /* Find this region in the handler's list */
475
476         obj_desc = handler_obj->address_space.region_list;
477         last_obj_ptr = &handler_obj->address_space.region_list;
478
479         while (obj_desc) {
480                 /* Is this the correct Region? */
481
482                 if (obj_desc == region_obj) {
483                         ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
484                                           "Removing Region %p from address handler %p\n",
485                                           region_obj, handler_obj));
486
487                         /* This is it, remove it from the handler's list */
488
489                         *last_obj_ptr = obj_desc->region.next;
490                         obj_desc->region.next = NULL;   /* Must clear field */
491
492                         if (acpi_ns_is_locked) {
493                                 status =
494                                     acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
495                                 if (ACPI_FAILURE(status)) {
496                                         return_VOID;
497                                 }
498                         }
499
500                         /* Now stop region accesses by executing the _REG method */
501
502                         status = acpi_ev_execute_reg_method(region_obj, 0);
503                         if (ACPI_FAILURE(status)) {
504                                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
505                                                   "%s from region _REG, [%s]\n",
506                                                   acpi_format_exception(status),
507                                                   acpi_ut_get_region_name
508                                                   (region_obj->region.
509                                                    space_id)));
510                         }
511
512                         if (acpi_ns_is_locked) {
513                                 status =
514                                     acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
515                                 if (ACPI_FAILURE(status)) {
516                                         return_VOID;
517                                 }
518                         }
519
520                         /* Call the setup handler with the deactivate notification */
521
522                         region_setup = handler_obj->address_space.setup;
523                         status =
524                             region_setup(region_obj, ACPI_REGION_DEACTIVATE,
525                                          handler_obj->address_space.context,
526                                          region_context);
527
528                         /* Init routine may fail, Just ignore errors */
529
530                         if (ACPI_FAILURE(status)) {
531                                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
532                                                   "%s from region init, [%s]\n",
533                                                   acpi_format_exception(status),
534                                                   acpi_ut_get_region_name
535                                                   (region_obj->region.
536                                                    space_id)));
537                         }
538
539                         region_obj->region.flags &= ~(AOPOBJ_SETUP_COMPLETE);
540
541                         /*
542                          * Remove handler reference in the region
543                          *
544                          * NOTE: this doesn't mean that the region goes away
545                          * The region is just inaccessible as indicated to
546                          * the _REG method
547                          *
548                          * If the region is on the handler's list
549                          * this better be the region's handler
550                          */
551                         region_obj->region.handler = NULL;
552                         acpi_ut_remove_reference(handler_obj);
553
554                         return_VOID;
555                 }
556
557                 /* Walk the linked list of handlers */
558
559                 last_obj_ptr = &obj_desc->region.next;
560                 obj_desc = obj_desc->region.next;
561         }
562
563         /* If we get here, the region was not in the handler's region list */
564
565         ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
566                           "Cannot remove region %p from address handler %p\n",
567                           region_obj, handler_obj));
568
569         return_VOID;
570 }
571
572 /*******************************************************************************
573  *
574  * FUNCTION:    acpi_ev_attach_region
575  *
576  * PARAMETERS:  handler_obj         - Handler Object
577  *              region_obj          - Region Object
578  *              acpi_ns_is_locked   - Namespace Region Already Locked?
579  *
580  * RETURN:      None
581  *
582  * DESCRIPTION: Create the association between the handler and the region
583  *              this is a two way association.
584  *
585  ******************************************************************************/
586
587 acpi_status
588 acpi_ev_attach_region(union acpi_operand_object *handler_obj,
589                       union acpi_operand_object *region_obj,
590                       u8 acpi_ns_is_locked)
591 {
592
593         ACPI_FUNCTION_TRACE("ev_attach_region");
594
595         ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
596                           "Adding Region [%4.4s] %p to address handler %p [%s]\n",
597                           acpi_ut_get_node_name(region_obj->region.node),
598                           region_obj, handler_obj,
599                           acpi_ut_get_region_name(region_obj->region.
600                                                   space_id)));
601
602         /* Link this region to the front of the handler's list */
603
604         region_obj->region.next = handler_obj->address_space.region_list;
605         handler_obj->address_space.region_list = region_obj;
606
607         /* Install the region's handler */
608
609         if (region_obj->region.handler) {
610                 return_ACPI_STATUS(AE_ALREADY_EXISTS);
611         }
612
613         region_obj->region.handler = handler_obj;
614         acpi_ut_add_reference(handler_obj);
615
616         return_ACPI_STATUS(AE_OK);
617 }
618
619 /*******************************************************************************
620  *
621  * FUNCTION:    acpi_ev_install_handler
622  *
623  * PARAMETERS:  walk_namespace callback
624  *
625  * DESCRIPTION: This routine installs an address handler into objects that are
626  *              of type Region or Device.
627  *
628  *              If the Object is a Device, and the device has a handler of
629  *              the same type then the search is terminated in that branch.
630  *
631  *              This is because the existing handler is closer in proximity
632  *              to any more regions than the one we are trying to install.
633  *
634  ******************************************************************************/
635
636 static acpi_status
637 acpi_ev_install_handler(acpi_handle obj_handle,
638                         u32 level, void *context, void **return_value)
639 {
640         union acpi_operand_object *handler_obj;
641         union acpi_operand_object *next_handler_obj;
642         union acpi_operand_object *obj_desc;
643         struct acpi_namespace_node *node;
644         acpi_status status;
645
646         ACPI_FUNCTION_NAME("ev_install_handler");
647
648         handler_obj = (union acpi_operand_object *)context;
649
650         /* Parameter validation */
651
652         if (!handler_obj) {
653                 return (AE_OK);
654         }
655
656         /* Convert and validate the device handle */
657
658         node = acpi_ns_map_handle_to_node(obj_handle);
659         if (!node) {
660                 return (AE_BAD_PARAMETER);
661         }
662
663         /*
664          * We only care about regions.and objects
665          * that are allowed to have address space handlers
666          */
667         if ((node->type != ACPI_TYPE_DEVICE) &&
668             (node->type != ACPI_TYPE_REGION) && (node != acpi_gbl_root_node)) {
669                 return (AE_OK);
670         }
671
672         /* Check for an existing internal object */
673
674         obj_desc = acpi_ns_get_attached_object(node);
675         if (!obj_desc) {
676                 /* No object, just exit */
677
678                 return (AE_OK);
679         }
680
681         /* Devices are handled different than regions */
682
683         if (ACPI_GET_OBJECT_TYPE(obj_desc) == ACPI_TYPE_DEVICE) {
684                 /* Check if this Device already has a handler for this address space */
685
686                 next_handler_obj = obj_desc->device.handler;
687                 while (next_handler_obj) {
688                         /* Found a handler, is it for the same address space? */
689
690                         if (next_handler_obj->address_space.space_id ==
691                             handler_obj->address_space.space_id) {
692                                 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
693                                                   "Found handler for region [%s] in device %p(%p) handler %p\n",
694                                                   acpi_ut_get_region_name
695                                                   (handler_obj->address_space.
696                                                    space_id), obj_desc,
697                                                   next_handler_obj,
698                                                   handler_obj));
699
700                                 /*
701                                  * Since the object we found it on was a device, then it
702                                  * means that someone has already installed a handler for
703                                  * the branch of the namespace from this device on.  Just
704                                  * bail out telling the walk routine to not traverse this
705                                  * branch.  This preserves the scoping rule for handlers.
706                                  */
707                                 return (AE_CTRL_DEPTH);
708                         }
709
710                         /* Walk the linked list of handlers attached to this device */
711
712                         next_handler_obj = next_handler_obj->address_space.next;
713                 }
714
715                 /*
716                  * As long as the device didn't have a handler for this
717                  * space we don't care about it.  We just ignore it and
718                  * proceed.
719                  */
720                 return (AE_OK);
721         }
722
723         /* Object is a Region */
724
725         if (obj_desc->region.space_id != handler_obj->address_space.space_id) {
726                 /*
727                  * This region is for a different address space
728                  * -- just ignore it
729                  */
730                 return (AE_OK);
731         }
732
733         /*
734          * Now we have a region and it is for the handler's address
735          * space type.
736          *
737          * First disconnect region for any previous handler (if any)
738          */
739         acpi_ev_detach_region(obj_desc, FALSE);
740
741         /* Connect the region to the new handler */
742
743         status = acpi_ev_attach_region(handler_obj, obj_desc, FALSE);
744         return (status);
745 }
746
747 /*******************************************************************************
748  *
749  * FUNCTION:    acpi_ev_install_space_handler
750  *
751  * PARAMETERS:  Node            - Namespace node for the device
752  *              space_id        - The address space ID
753  *              Handler         - Address of the handler
754  *              Setup           - Address of the setup function
755  *              Context         - Value passed to the handler on each access
756  *
757  * RETURN:      Status
758  *
759  * DESCRIPTION: Install a handler for all op_regions of a given space_id.
760  *              Assumes namespace is locked
761  *
762  ******************************************************************************/
763
764 acpi_status
765 acpi_ev_install_space_handler(struct acpi_namespace_node * node,
766                               acpi_adr_space_type space_id,
767                               acpi_adr_space_handler handler,
768                               acpi_adr_space_setup setup, void *context)
769 {
770         union acpi_operand_object *obj_desc;
771         union acpi_operand_object *handler_obj;
772         acpi_status status;
773         acpi_object_type type;
774         u16 flags = 0;
775
776         ACPI_FUNCTION_TRACE("ev_install_space_handler");
777
778         /*
779          * This registration is valid for only the types below
780          * and the root.  This is where the default handlers
781          * get placed.
782          */
783         if ((node->type != ACPI_TYPE_DEVICE) &&
784             (node->type != ACPI_TYPE_PROCESSOR) &&
785             (node->type != ACPI_TYPE_THERMAL) && (node != acpi_gbl_root_node)) {
786                 status = AE_BAD_PARAMETER;
787                 goto unlock_and_exit;
788         }
789
790         if (handler == ACPI_DEFAULT_HANDLER) {
791                 flags = ACPI_ADDR_HANDLER_DEFAULT_INSTALLED;
792
793                 switch (space_id) {
794                 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
795                         handler = acpi_ex_system_memory_space_handler;
796                         setup = acpi_ev_system_memory_region_setup;
797                         break;
798
799                 case ACPI_ADR_SPACE_SYSTEM_IO:
800                         handler = acpi_ex_system_io_space_handler;
801                         setup = acpi_ev_io_space_region_setup;
802                         break;
803
804                 case ACPI_ADR_SPACE_PCI_CONFIG:
805                         handler = acpi_ex_pci_config_space_handler;
806                         setup = acpi_ev_pci_config_region_setup;
807                         break;
808
809                 case ACPI_ADR_SPACE_CMOS:
810                         handler = acpi_ex_cmos_space_handler;
811                         setup = acpi_ev_cmos_region_setup;
812                         break;
813
814                 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
815                         handler = acpi_ex_pci_bar_space_handler;
816                         setup = acpi_ev_pci_bar_region_setup;
817                         break;
818
819                 case ACPI_ADR_SPACE_DATA_TABLE:
820                         handler = acpi_ex_data_table_space_handler;
821                         setup = NULL;
822                         break;
823
824                 default:
825                         status = AE_BAD_PARAMETER;
826                         goto unlock_and_exit;
827                 }
828         }
829
830         /* If the caller hasn't specified a setup routine, use the default */
831
832         if (!setup) {
833                 setup = acpi_ev_default_region_setup;
834         }
835
836         /* Check for an existing internal object */
837
838         obj_desc = acpi_ns_get_attached_object(node);
839         if (obj_desc) {
840                 /*
841                  * The attached device object already exists.
842                  * Make sure the handler is not already installed.
843                  */
844                 handler_obj = obj_desc->device.handler;
845
846                 /* Walk the handler list for this device */
847
848                 while (handler_obj) {
849                         /* Same space_id indicates a handler already installed */
850
851                         if (handler_obj->address_space.space_id == space_id) {
852                                 if (handler_obj->address_space.handler ==
853                                     handler) {
854                                         /*
855                                          * It is (relatively) OK to attempt to install the SAME
856                                          * handler twice. This can easily happen
857                                          * with PCI_Config space.
858                                          */
859                                         status = AE_SAME_HANDLER;
860                                         goto unlock_and_exit;
861                                 } else {
862                                         /* A handler is already installed */
863
864                                         status = AE_ALREADY_EXISTS;
865                                 }
866                                 goto unlock_and_exit;
867                         }
868
869                         /* Walk the linked list of handlers */
870
871                         handler_obj = handler_obj->address_space.next;
872                 }
873         } else {
874                 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
875                                   "Creating object on Device %p while installing handler\n",
876                                   node));
877
878                 /* obj_desc does not exist, create one */
879
880                 if (node->type == ACPI_TYPE_ANY) {
881                         type = ACPI_TYPE_DEVICE;
882                 } else {
883                         type = node->type;
884                 }
885
886                 obj_desc = acpi_ut_create_internal_object(type);
887                 if (!obj_desc) {
888                         status = AE_NO_MEMORY;
889                         goto unlock_and_exit;
890                 }
891
892                 /* Init new descriptor */
893
894                 obj_desc->common.type = (u8) type;
895
896                 /* Attach the new object to the Node */
897
898                 status = acpi_ns_attach_object(node, obj_desc, type);
899
900                 /* Remove local reference to the object */
901
902                 acpi_ut_remove_reference(obj_desc);
903
904                 if (ACPI_FAILURE(status)) {
905                         goto unlock_and_exit;
906                 }
907         }
908
909         ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
910                           "Installing address handler for region %s(%X) on Device %4.4s %p(%p)\n",
911                           acpi_ut_get_region_name(space_id), space_id,
912                           acpi_ut_get_node_name(node), node, obj_desc));
913
914         /*
915          * Install the handler
916          *
917          * At this point there is no existing handler.
918          * Just allocate the object for the handler and link it
919          * into the list.
920          */
921         handler_obj =
922             acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_ADDRESS_HANDLER);
923         if (!handler_obj) {
924                 status = AE_NO_MEMORY;
925                 goto unlock_and_exit;
926         }
927
928         /* Init handler obj */
929
930         handler_obj->address_space.space_id = (u8) space_id;
931         handler_obj->address_space.hflags = flags;
932         handler_obj->address_space.region_list = NULL;
933         handler_obj->address_space.node = node;
934         handler_obj->address_space.handler = handler;
935         handler_obj->address_space.context = context;
936         handler_obj->address_space.setup = setup;
937
938         /* Install at head of Device.address_space list */
939
940         handler_obj->address_space.next = obj_desc->device.handler;
941
942         /*
943          * The Device object is the first reference on the handler_obj.
944          * Each region that uses the handler adds a reference.
945          */
946         obj_desc->device.handler = handler_obj;
947
948         /*
949          * Walk the namespace finding all of the regions this
950          * handler will manage.
951          *
952          * Start at the device and search the branch toward
953          * the leaf nodes until either the leaf is encountered or
954          * a device is detected that has an address handler of the
955          * same type.
956          *
957          * In either case, back up and search down the remainder
958          * of the branch
959          */
960         status = acpi_ns_walk_namespace(ACPI_TYPE_ANY, node, ACPI_UINT32_MAX,
961                                         ACPI_NS_WALK_UNLOCK,
962                                         acpi_ev_install_handler, handler_obj,
963                                         NULL);
964
965       unlock_and_exit:
966         return_ACPI_STATUS(status);
967 }
968
969 /*******************************************************************************
970  *
971  * FUNCTION:    acpi_ev_execute_reg_methods
972  *
973  * PARAMETERS:  Node            - Namespace node for the device
974  *              space_id        - The address space ID
975  *
976  * RETURN:      Status
977  *
978  * DESCRIPTION: Run all _REG methods for the input Space ID;
979  *              Note: assumes namespace is locked, or system init time.
980  *
981  ******************************************************************************/
982
983 acpi_status
984 acpi_ev_execute_reg_methods(struct acpi_namespace_node *node,
985                             acpi_adr_space_type space_id)
986 {
987         acpi_status status;
988
989         ACPI_FUNCTION_TRACE("ev_execute_reg_methods");
990
991         /*
992          * Run all _REG methods for all Operation Regions for this
993          * space ID.  This is a separate walk in order to handle any
994          * interdependencies between regions and _REG methods.  (i.e. handlers
995          * must be installed for all regions of this Space ID before we
996          * can run any _REG methods)
997          */
998         status = acpi_ns_walk_namespace(ACPI_TYPE_ANY, node, ACPI_UINT32_MAX,
999                                         ACPI_NS_WALK_UNLOCK, acpi_ev_reg_run,
1000                                         &space_id, NULL);
1001
1002         return_ACPI_STATUS(status);
1003 }
1004
1005 /*******************************************************************************
1006  *
1007  * FUNCTION:    acpi_ev_reg_run
1008  *
1009  * PARAMETERS:  walk_namespace callback
1010  *
1011  * DESCRIPTION: Run _REg method for region objects of the requested space_iD
1012  *
1013  ******************************************************************************/
1014
1015 static acpi_status
1016 acpi_ev_reg_run(acpi_handle obj_handle,
1017                 u32 level, void *context, void **return_value)
1018 {
1019         union acpi_operand_object *obj_desc;
1020         struct acpi_namespace_node *node;
1021         acpi_adr_space_type space_id;
1022         acpi_status status;
1023
1024         space_id = *ACPI_CAST_PTR(acpi_adr_space_type, context);
1025
1026         /* Convert and validate the device handle */
1027
1028         node = acpi_ns_map_handle_to_node(obj_handle);
1029         if (!node) {
1030                 return (AE_BAD_PARAMETER);
1031         }
1032
1033         /*
1034          * We only care about regions.and objects
1035          * that are allowed to have address space handlers
1036          */
1037         if ((node->type != ACPI_TYPE_REGION) && (node != acpi_gbl_root_node)) {
1038                 return (AE_OK);
1039         }
1040
1041         /* Check for an existing internal object */
1042
1043         obj_desc = acpi_ns_get_attached_object(node);
1044         if (!obj_desc) {
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 }