blob: f2d53af97610075a653f15143e727fb65d52f657 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/******************************************************************************
2 *
3 * Module Name: evrgnini- ACPI address_space (op_region) init
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
49#define _COMPONENT ACPI_EVENTS
50 ACPI_MODULE_NAME ("evrgnini")
51
52
53/*******************************************************************************
54 *
55 * FUNCTION: acpi_ev_system_memory_region_setup
56 *
57 * PARAMETERS: Handle - Region we are interested in
58 * Function - Start or stop
59 * handler_context - Address space handler context
60 * region_context - Region specific context
61 *
62 * RETURN: Status
63 *
Robert Moore44f6c012005-04-18 22:49:35 -040064 * DESCRIPTION: Setup a system_memory operation region
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 *
66 ******************************************************************************/
67
68acpi_status
69acpi_ev_system_memory_region_setup (
70 acpi_handle handle,
71 u32 function,
72 void *handler_context,
73 void **region_context)
74{
75 union acpi_operand_object *region_desc = (union acpi_operand_object *) handle;
76 struct acpi_mem_space_context *local_region_context;
77
78
79 ACPI_FUNCTION_TRACE ("ev_system_memory_region_setup");
80
81
82 if (function == ACPI_REGION_DEACTIVATE) {
83 if (*region_context) {
84 ACPI_MEM_FREE (*region_context);
85 *region_context = NULL;
86 }
87 return_ACPI_STATUS (AE_OK);
88 }
89
90 /* Create a new context */
91
92 local_region_context = ACPI_MEM_CALLOCATE (sizeof (struct acpi_mem_space_context));
93 if (!(local_region_context)) {
94 return_ACPI_STATUS (AE_NO_MEMORY);
95 }
96
97 /* Save the region length and address for use in the handler */
98
99 local_region_context->length = region_desc->region.length;
100 local_region_context->address = region_desc->region.address;
101
102 *region_context = local_region_context;
103 return_ACPI_STATUS (AE_OK);
104}
105
106
107/*******************************************************************************
108 *
109 * FUNCTION: acpi_ev_io_space_region_setup
110 *
111 * PARAMETERS: Handle - Region we are interested in
112 * Function - Start or stop
113 * handler_context - Address space handler context
114 * region_context - Region specific context
115 *
116 * RETURN: Status
117 *
Robert Moore44f6c012005-04-18 22:49:35 -0400118 * DESCRIPTION: Setup a IO operation region
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 *
120 ******************************************************************************/
121
122acpi_status
123acpi_ev_io_space_region_setup (
124 acpi_handle handle,
125 u32 function,
126 void *handler_context,
127 void **region_context)
128{
129 ACPI_FUNCTION_TRACE ("ev_io_space_region_setup");
130
131
132 if (function == ACPI_REGION_DEACTIVATE) {
133 *region_context = NULL;
134 }
135 else {
136 *region_context = handler_context;
137 }
138
139 return_ACPI_STATUS (AE_OK);
140}
141
142
143/*******************************************************************************
144 *
145 * FUNCTION: acpi_ev_pci_config_region_setup
146 *
Robert Moore44f6c012005-04-18 22:49:35 -0400147 * PARAMETERS: Handle - Region we are interested in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 * Function - Start or stop
149 * handler_context - Address space handler context
150 * region_context - Region specific context
151 *
152 * RETURN: Status
153 *
Robert Moore44f6c012005-04-18 22:49:35 -0400154 * DESCRIPTION: Setup a PCI_Config operation region
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 *
156 * MUTEX: Assumes namespace is not locked
157 *
158 ******************************************************************************/
159
160acpi_status
161acpi_ev_pci_config_region_setup (
162 acpi_handle handle,
163 u32 function,
164 void *handler_context,
165 void **region_context)
166{
167 acpi_status status = AE_OK;
168 acpi_integer pci_value;
169 struct acpi_pci_id *pci_id = *region_context;
170 union acpi_operand_object *handler_obj;
171 struct acpi_namespace_node *parent_node;
172 struct acpi_namespace_node *pci_root_node;
173 union acpi_operand_object *region_obj = (union acpi_operand_object *) handle;
174 struct acpi_device_id object_hID;
175
176
177 ACPI_FUNCTION_TRACE ("ev_pci_config_region_setup");
178
179
180 handler_obj = region_obj->region.handler;
181 if (!handler_obj) {
182 /*
183 * No installed handler. This shouldn't happen because the dispatch
184 * routine checks before we get here, but we check again just in case.
185 */
186 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
187 "Attempting to init a region %p, with no handler\n", region_obj));
188 return_ACPI_STATUS (AE_NOT_EXIST);
189 }
190
191 *region_context = NULL;
192 if (function == ACPI_REGION_DEACTIVATE) {
193 if (pci_id) {
194 ACPI_MEM_FREE (pci_id);
195 }
196 return_ACPI_STATUS (status);
197 }
198
199 parent_node = acpi_ns_get_parent_node (region_obj->region.node);
200
201 /*
202 * Get the _SEG and _BBN values from the device upon which the handler
203 * is installed.
204 *
205 * We need to get the _SEG and _BBN objects relative to the PCI BUS device.
206 * This is the device the handler has been registered to handle.
207 */
208
209 /*
210 * If the address_space.Node is still pointing to the root, we need
211 * to scan upward for a PCI Root bridge and re-associate the op_region
212 * handlers with that device.
213 */
214 if (handler_obj->address_space.node == acpi_gbl_root_node) {
215 /* Start search from the parent object */
216
217 pci_root_node = parent_node;
218 while (pci_root_node != acpi_gbl_root_node) {
219 status = acpi_ut_execute_HID (pci_root_node, &object_hID);
220 if (ACPI_SUCCESS (status)) {
Robert Moore6f42ccf2005-05-13 00:00:00 -0400221 /*
222 * Got a valid _HID string, check if this is a PCI root.
223 * New for ACPI 3.0: check for a PCI Express root also.
224 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if (!(ACPI_STRNCMP (object_hID.value, PCI_ROOT_HID_STRING,
Robert Moore6f42ccf2005-05-13 00:00:00 -0400226 sizeof (PCI_ROOT_HID_STRING)) ||
227 !(ACPI_STRNCMP (object_hID.value, PCI_EXPRESS_ROOT_HID_STRING,
228 sizeof (PCI_EXPRESS_ROOT_HID_STRING))))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 /* Install a handler for this PCI root bridge */
230
231 status = acpi_install_address_space_handler ((acpi_handle) pci_root_node,
232 ACPI_ADR_SPACE_PCI_CONFIG,
233 ACPI_DEFAULT_HANDLER, NULL, NULL);
234 if (ACPI_FAILURE (status)) {
235 if (status == AE_SAME_HANDLER) {
236 /*
237 * It is OK if the handler is already installed on the root
238 * bridge. Still need to return a context object for the
239 * new PCI_Config operation region, however.
240 */
241 status = AE_OK;
242 }
243 else {
244 ACPI_REPORT_ERROR ((
245 "Could not install pci_config handler for Root Bridge %4.4s, %s\n",
246 acpi_ut_get_node_name (pci_root_node), acpi_format_exception (status)));
247 }
248 }
249 break;
250 }
251 }
252
253 pci_root_node = acpi_ns_get_parent_node (pci_root_node);
254 }
255
256 /* PCI root bridge not found, use namespace root node */
257 }
258 else {
259 pci_root_node = handler_obj->address_space.node;
260 }
261
262 /*
263 * If this region is now initialized, we are done.
264 * (install_address_space_handler could have initialized it)
265 */
266 if (region_obj->region.flags & AOPOBJ_SETUP_COMPLETE) {
267 return_ACPI_STATUS (AE_OK);
268 }
269
270 /* Region is still not initialized. Create a new context */
271
272 pci_id = ACPI_MEM_CALLOCATE (sizeof (struct acpi_pci_id));
273 if (!pci_id) {
274 return_ACPI_STATUS (AE_NO_MEMORY);
275 }
276
277 /*
278 * For PCI_Config space access, we need the segment, bus,
279 * device and function numbers. Acquire them here.
280 */
281
282 /*
283 * Get the PCI device and function numbers from the _ADR object
284 * contained in the parent's scope.
285 */
286 status = acpi_ut_evaluate_numeric_object (METHOD_NAME__ADR, parent_node, &pci_value);
287
288 /*
289 * The default is zero, and since the allocation above zeroed
290 * the data, just do nothing on failure.
291 */
292 if (ACPI_SUCCESS (status)) {
293 pci_id->device = ACPI_HIWORD (ACPI_LODWORD (pci_value));
294 pci_id->function = ACPI_LOWORD (ACPI_LODWORD (pci_value));
295 }
296
297 /* The PCI segment number comes from the _SEG method */
298
299 status = acpi_ut_evaluate_numeric_object (METHOD_NAME__SEG, pci_root_node, &pci_value);
300 if (ACPI_SUCCESS (status)) {
301 pci_id->segment = ACPI_LOWORD (pci_value);
302 }
303
304 /* The PCI bus number comes from the _BBN method */
305
306 status = acpi_ut_evaluate_numeric_object (METHOD_NAME__BBN, pci_root_node, &pci_value);
307 if (ACPI_SUCCESS (status)) {
308 pci_id->bus = ACPI_LOWORD (pci_value);
309 }
310
311 /* Complete this device's pci_id */
312
313 acpi_os_derive_pci_id (pci_root_node, region_obj->region.node, &pci_id);
314
315 *region_context = pci_id;
316 return_ACPI_STATUS (AE_OK);
317}
318
319
320/*******************************************************************************
321 *
322 * FUNCTION: acpi_ev_pci_bar_region_setup
323 *
324 * PARAMETERS: Handle - Region we are interested in
325 * Function - Start or stop
326 * handler_context - Address space handler context
327 * region_context - Region specific context
328 *
329 * RETURN: Status
330 *
Robert Moore44f6c012005-04-18 22:49:35 -0400331 * DESCRIPTION: Setup a pci_bAR operation region
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 *
333 * MUTEX: Assumes namespace is not locked
334 *
335 ******************************************************************************/
336
337acpi_status
338acpi_ev_pci_bar_region_setup (
339 acpi_handle handle,
340 u32 function,
341 void *handler_context,
342 void **region_context)
343{
344 ACPI_FUNCTION_TRACE ("ev_pci_bar_region_setup");
345
346
347 return_ACPI_STATUS (AE_OK);
348}
349
350
351/*******************************************************************************
352 *
353 * FUNCTION: acpi_ev_cmos_region_setup
354 *
355 * PARAMETERS: Handle - Region we are interested in
356 * Function - Start or stop
357 * handler_context - Address space handler context
358 * region_context - Region specific context
359 *
360 * RETURN: Status
361 *
Robert Moore44f6c012005-04-18 22:49:35 -0400362 * DESCRIPTION: Setup a CMOS operation region
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 *
364 * MUTEX: Assumes namespace is not locked
365 *
366 ******************************************************************************/
367
368acpi_status
369acpi_ev_cmos_region_setup (
370 acpi_handle handle,
371 u32 function,
372 void *handler_context,
373 void **region_context)
374{
375 ACPI_FUNCTION_TRACE ("ev_cmos_region_setup");
376
377
378 return_ACPI_STATUS (AE_OK);
379}
380
381
382/*******************************************************************************
383 *
384 * FUNCTION: acpi_ev_default_region_setup
385 *
386 * PARAMETERS: Handle - Region we are interested in
387 * Function - Start or stop
388 * handler_context - Address space handler context
389 * region_context - Region specific context
390 *
391 * RETURN: Status
392 *
Robert Moore44f6c012005-04-18 22:49:35 -0400393 * DESCRIPTION: Default region initialization
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 *
395 ******************************************************************************/
396
397acpi_status
398acpi_ev_default_region_setup (
399 acpi_handle handle,
400 u32 function,
401 void *handler_context,
402 void **region_context)
403{
404 ACPI_FUNCTION_TRACE ("ev_default_region_setup");
405
406
407 if (function == ACPI_REGION_DEACTIVATE) {
408 *region_context = NULL;
409 }
410 else {
411 *region_context = handler_context;
412 }
413
414 return_ACPI_STATUS (AE_OK);
415}
416
417
418/*******************************************************************************
419 *
420 * FUNCTION: acpi_ev_initialize_region
421 *
422 * PARAMETERS: region_obj - Region we are initializing
423 * acpi_ns_locked - Is namespace locked?
424 *
425 * RETURN: Status
426 *
427 * DESCRIPTION: Initializes the region, finds any _REG methods and saves them
428 * for execution at a later time
429 *
430 * Get the appropriate address space handler for a newly
431 * created region.
432 *
433 * This also performs address space specific initialization. For
434 * example, PCI regions must have an _ADR object that contains
435 * a PCI address in the scope of the definition. This address is
436 * required to perform an access to PCI config space.
437 *
438 ******************************************************************************/
439
440acpi_status
441acpi_ev_initialize_region (
442 union acpi_operand_object *region_obj,
443 u8 acpi_ns_locked)
444{
445 union acpi_operand_object *handler_obj;
446 union acpi_operand_object *obj_desc;
447 acpi_adr_space_type space_id;
448 struct acpi_namespace_node *node;
449 acpi_status status;
450 struct acpi_namespace_node *method_node;
451 acpi_name *reg_name_ptr = (acpi_name *) METHOD_NAME__REG;
452 union acpi_operand_object *region_obj2;
453
454
455 ACPI_FUNCTION_TRACE_U32 ("ev_initialize_region", acpi_ns_locked);
456
457
458 if (!region_obj) {
459 return_ACPI_STATUS (AE_BAD_PARAMETER);
460 }
461
462 if (region_obj->common.flags & AOPOBJ_OBJECT_INITIALIZED) {
463 return_ACPI_STATUS (AE_OK);
464 }
465
466 region_obj2 = acpi_ns_get_secondary_object (region_obj);
467 if (!region_obj2) {
468 return_ACPI_STATUS (AE_NOT_EXIST);
469 }
470
471 node = acpi_ns_get_parent_node (region_obj->region.node);
472 space_id = region_obj->region.space_id;
473
474 /* Setup defaults */
475
476 region_obj->region.handler = NULL;
477 region_obj2->extra.method_REG = NULL;
478 region_obj->common.flags &= ~(AOPOBJ_SETUP_COMPLETE);
479 region_obj->common.flags |= AOPOBJ_OBJECT_INITIALIZED;
480
481 /* Find any "_REG" method associated with this region definition */
482
483 status = acpi_ns_search_node (*reg_name_ptr, node,
484 ACPI_TYPE_METHOD, &method_node);
485 if (ACPI_SUCCESS (status)) {
486 /*
487 * The _REG method is optional and there can be only one per region
488 * definition. This will be executed when the handler is attached
489 * or removed
490 */
491 region_obj2->extra.method_REG = method_node;
492 }
493
494 /*
495 * The following loop depends upon the root Node having no parent
496 * ie: acpi_gbl_root_node->parent_entry being set to NULL
497 */
498 while (node) {
499 /* Check to see if a handler exists */
500
501 handler_obj = NULL;
502 obj_desc = acpi_ns_get_attached_object (node);
503 if (obj_desc) {
504 /* Can only be a handler if the object exists */
505
506 switch (node->type) {
507 case ACPI_TYPE_DEVICE:
508
509 handler_obj = obj_desc->device.handler;
510 break;
511
512 case ACPI_TYPE_PROCESSOR:
513
514 handler_obj = obj_desc->processor.handler;
515 break;
516
517 case ACPI_TYPE_THERMAL:
518
519 handler_obj = obj_desc->thermal_zone.handler;
520 break;
521
522 default:
523 /* Ignore other objects */
524 break;
525 }
526
527 while (handler_obj) {
528 /* Is this handler of the correct type? */
529
530 if (handler_obj->address_space.space_id == space_id) {
531 /* Found correct handler */
532
533 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
534 "Found handler %p for region %p in obj %p\n",
535 handler_obj, region_obj, obj_desc));
536
537 status = acpi_ev_attach_region (handler_obj, region_obj,
538 acpi_ns_locked);
539
540 /*
541 * Tell all users that this region is usable by running the _REG
542 * method
543 */
544 if (acpi_ns_locked) {
545 status = acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
546 if (ACPI_FAILURE (status)) {
547 return_ACPI_STATUS (status);
548 }
549 }
550
551 status = acpi_ev_execute_reg_method (region_obj, 1);
552
553 if (acpi_ns_locked) {
554 status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
555 if (ACPI_FAILURE (status)) {
556 return_ACPI_STATUS (status);
557 }
558 }
559
560 return_ACPI_STATUS (AE_OK);
561 }
562
563 /* Try next handler in the list */
564
565 handler_obj = handler_obj->address_space.next;
566 }
567 }
568
569 /*
570 * This node does not have the handler we need;
571 * Pop up one level
572 */
573 node = acpi_ns_get_parent_node (node);
574 }
575
576 /* If we get here, there is no handler for this region */
577
578 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
579 "No handler for region_type %s(%X) (region_obj %p)\n",
580 acpi_ut_get_region_name (space_id), space_id, region_obj));
581
582 return_ACPI_STATUS (AE_NOT_EXIST);
583}
584