evrgnini.c 19.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7
/******************************************************************************
 *
 * Module Name: evrgnini- ACPI address_space (op_region) init
 *
 *****************************************************************************/

/*
L
Len Brown 已提交
8
 * Copyright (C) 2000 - 2008, Intel Corp.
L
Linus Torvalds 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions, and the following disclaimer,
 *    without modification.
 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
 *    substantially similar to the "NO WARRANTY" disclaimer below
 *    ("Disclaimer") and any redistribution must be conditioned upon
 *    including a substantially similar Disclaimer requirement for further
 *    binary redistribution.
 * 3. Neither the names of the above-listed copyright holders nor the names
 *    of any contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * Alternatively, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") version 2 as published by the Free
 * Software Foundation.
 *
 * NO WARRANTY
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 */

#include <acpi/acpi.h>
45
#include <acpi/accommon.h>
L
Linus Torvalds 已提交
46 47 48 49
#include <acpi/acevents.h>
#include <acpi/acnamesp.h>

#define _COMPONENT          ACPI_EVENTS
L
Len Brown 已提交
50
ACPI_MODULE_NAME("evrgnini")
L
Linus Torvalds 已提交
51

52 53 54 55 56
/* Local prototypes */
static u8 acpi_ev_match_pci_root_bridge(char *id);

static u8 acpi_ev_is_pci_root_bridge(struct acpi_namespace_node *node);

L
Linus Torvalds 已提交
57 58 59 60 61 62 63 64 65 66 67
/*******************************************************************************
 *
 * FUNCTION:    acpi_ev_system_memory_region_setup
 *
 * PARAMETERS:  Handle              - Region we are interested in
 *              Function            - Start or stop
 *              handler_context     - Address space handler context
 *              region_context      - Region specific context
 *
 * RETURN:      Status
 *
R
Robert Moore 已提交
68
 * DESCRIPTION: Setup a system_memory operation region
L
Linus Torvalds 已提交
69 70
 *
 ******************************************************************************/
71

L
Linus Torvalds 已提交
72
acpi_status
L
Len Brown 已提交
73 74 75
acpi_ev_system_memory_region_setup(acpi_handle handle,
				   u32 function,
				   void *handler_context, void **region_context)
L
Linus Torvalds 已提交
76
{
L
Len Brown 已提交
77 78 79
	union acpi_operand_object *region_desc =
	    (union acpi_operand_object *)handle;
	struct acpi_mem_space_context *local_region_context;
L
Linus Torvalds 已提交
80

B
Bob Moore 已提交
81
	ACPI_FUNCTION_TRACE(ev_system_memory_region_setup);
L
Linus Torvalds 已提交
82 83 84

	if (function == ACPI_REGION_DEACTIVATE) {
		if (*region_context) {
B
Bob Moore 已提交
85 86 87 88 89 90 91 92 93 94 95 96
			local_region_context =
			    (struct acpi_mem_space_context *)*region_context;

			/* Delete a cached mapping if present */

			if (local_region_context->mapped_length) {
				acpi_os_unmap_memory(local_region_context->
						     mapped_logical_address,
						     local_region_context->
						     mapped_length);
			}
			ACPI_FREE(local_region_context);
L
Linus Torvalds 已提交
97 98
			*region_context = NULL;
		}
L
Len Brown 已提交
99
		return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
100 101 102 103
	}

	/* Create a new context */

L
Len Brown 已提交
104
	local_region_context =
B
Bob Moore 已提交
105
	    ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_mem_space_context));
L
Linus Torvalds 已提交
106
	if (!(local_region_context)) {
L
Len Brown 已提交
107
		return_ACPI_STATUS(AE_NO_MEMORY);
L
Linus Torvalds 已提交
108 109 110 111 112 113 114 115
	}

	/* Save the region length and address for use in the handler */

	local_region_context->length = region_desc->region.length;
	local_region_context->address = region_desc->region.address;

	*region_context = local_region_context;
L
Len Brown 已提交
116
	return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ev_io_space_region_setup
 *
 * PARAMETERS:  Handle              - Region we are interested in
 *              Function            - Start or stop
 *              handler_context     - Address space handler context
 *              region_context      - Region specific context
 *
 * RETURN:      Status
 *
R
Robert Moore 已提交
130
 * DESCRIPTION: Setup a IO operation region
L
Linus Torvalds 已提交
131 132 133 134
 *
 ******************************************************************************/

acpi_status
L
Len Brown 已提交
135 136 137
acpi_ev_io_space_region_setup(acpi_handle handle,
			      u32 function,
			      void *handler_context, void **region_context)
L
Linus Torvalds 已提交
138
{
B
Bob Moore 已提交
139
	ACPI_FUNCTION_TRACE(ev_io_space_region_setup);
L
Linus Torvalds 已提交
140 141 142

	if (function == ACPI_REGION_DEACTIVATE) {
		*region_context = NULL;
L
Len Brown 已提交
143
	} else {
L
Linus Torvalds 已提交
144 145 146
		*region_context = handler_context;
	}

L
Len Brown 已提交
147
	return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
148 149 150 151 152 153
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ev_pci_config_region_setup
 *
R
Robert Moore 已提交
154
 * PARAMETERS:  Handle              - Region we are interested in
L
Linus Torvalds 已提交
155 156 157 158 159 160
 *              Function            - Start or stop
 *              handler_context     - Address space handler context
 *              region_context      - Region specific context
 *
 * RETURN:      Status
 *
R
Robert Moore 已提交
161
 * DESCRIPTION: Setup a PCI_Config operation region
L
Linus Torvalds 已提交
162 163 164 165 166 167
 *
 * MUTEX:       Assumes namespace is not locked
 *
 ******************************************************************************/

acpi_status
L
Len Brown 已提交
168 169 170
acpi_ev_pci_config_region_setup(acpi_handle handle,
				u32 function,
				void *handler_context, void **region_context)
L
Linus Torvalds 已提交
171
{
L
Len Brown 已提交
172 173 174 175 176 177
	acpi_status status = AE_OK;
	acpi_integer pci_value;
	struct acpi_pci_id *pci_id = *region_context;
	union acpi_operand_object *handler_obj;
	struct acpi_namespace_node *parent_node;
	struct acpi_namespace_node *pci_root_node;
178
	struct acpi_namespace_node *pci_device_node;
L
Len Brown 已提交
179 180 181
	union acpi_operand_object *region_obj =
	    (union acpi_operand_object *)handle;

B
Bob Moore 已提交
182
	ACPI_FUNCTION_TRACE(ev_pci_config_region_setup);
L
Linus Torvalds 已提交
183 184 185 186 187 188 189

	handler_obj = region_obj->region.handler;
	if (!handler_obj) {
		/*
		 * No installed handler. This shouldn't happen because the dispatch
		 * routine checks before we get here, but we check again just in case.
		 */
L
Len Brown 已提交
190 191 192 193
		ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
				  "Attempting to init a region %p, with no handler\n",
				  region_obj));
		return_ACPI_STATUS(AE_NOT_EXIST);
L
Linus Torvalds 已提交
194 195 196 197 198
	}

	*region_context = NULL;
	if (function == ACPI_REGION_DEACTIVATE) {
		if (pci_id) {
B
Bob Moore 已提交
199
			ACPI_FREE(pci_id);
L
Linus Torvalds 已提交
200
		}
L
Len Brown 已提交
201
		return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
202 203
	}

L
Len Brown 已提交
204
	parent_node = acpi_ns_get_parent_node(region_obj->region.node);
L
Linus Torvalds 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

	/*
	 * Get the _SEG and _BBN values from the device upon which the handler
	 * is installed.
	 *
	 * We need to get the _SEG and _BBN objects relative to the PCI BUS device.
	 * This is the device the handler has been registered to handle.
	 */

	/*
	 * If the address_space.Node is still pointing to the root, we need
	 * to scan upward for a PCI Root bridge and re-associate the op_region
	 * handlers with that device.
	 */
	if (handler_obj->address_space.node == acpi_gbl_root_node) {
B
Bob Moore 已提交
220

L
Linus Torvalds 已提交
221 222 223 224
		/* Start search from the parent object */

		pci_root_node = parent_node;
		while (pci_root_node != acpi_gbl_root_node) {
225 226 227 228 229 230 231

			/* Get the _HID/_CID in order to detect a root_bridge */

			if (acpi_ev_is_pci_root_bridge(pci_root_node)) {

				/* Install a handler for this PCI root bridge */

L
Len Brown 已提交
232 233
				status =
				    acpi_install_address_space_handler((acpi_handle) pci_root_node, ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
234 235 236
				if (ACPI_FAILURE(status)) {
					if (status == AE_SAME_HANDLER) {
						/*
237 238 239
						 * It is OK if the handler is already installed on the
						 * root bridge. Still need to return a context object
						 * for the new PCI_Config operation region, however.
240 241 242 243 244 245 246
						 */
						status = AE_OK;
					} else {
						ACPI_EXCEPTION((AE_INFO, status,
								"Could not install PciConfig handler for Root Bridge %4.4s",
								acpi_ut_get_node_name
								(pci_root_node)));
L
Linus Torvalds 已提交
247 248
					}
				}
249
				break;
L
Linus Torvalds 已提交
250 251
			}

L
Len Brown 已提交
252
			pci_root_node = acpi_ns_get_parent_node(pci_root_node);
L
Linus Torvalds 已提交
253 254 255
		}

		/* PCI root bridge not found, use namespace root node */
L
Len Brown 已提交
256
	} else {
L
Linus Torvalds 已提交
257 258 259 260 261 262 263 264
		pci_root_node = handler_obj->address_space.node;
	}

	/*
	 * If this region is now initialized, we are done.
	 * (install_address_space_handler could have initialized it)
	 */
	if (region_obj->region.flags & AOPOBJ_SETUP_COMPLETE) {
L
Len Brown 已提交
265
		return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
266 267 268 269
	}

	/* Region is still not initialized. Create a new context */

B
Bob Moore 已提交
270
	pci_id = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_pci_id));
L
Linus Torvalds 已提交
271
	if (!pci_id) {
L
Len Brown 已提交
272
		return_ACPI_STATUS(AE_NO_MEMORY);
L
Linus Torvalds 已提交
273 274 275
	}

	/*
276 277
	 * For PCI_Config space access, we need the segment, bus, device and
	 * function numbers. Acquire them here.
278 279 280
	 *
	 * Find the parent device object. (This allows the operation region to be
	 * within a subscope under the device, such as a control method.)
L
Linus Torvalds 已提交
281
	 */
282 283 284 285 286 287
	pci_device_node = region_obj->region.node;
	while (pci_device_node && (pci_device_node->type != ACPI_TYPE_DEVICE)) {
		pci_device_node = acpi_ns_get_parent_node(pci_device_node);
	}

	if (!pci_device_node) {
288
		ACPI_FREE(pci_id);
289 290
		return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
	}
L
Linus Torvalds 已提交
291 292

	/*
293 294
	 * Get the PCI device and function numbers from the _ADR object contained
	 * in the parent's scope.
L
Linus Torvalds 已提交
295
	 */
L
Len Brown 已提交
296
	status =
297
	    acpi_ut_evaluate_numeric_object(METHOD_NAME__ADR, pci_device_node,
L
Len Brown 已提交
298
					    &pci_value);
L
Linus Torvalds 已提交
299 300

	/*
301 302
	 * The default is zero, and since the allocation above zeroed the data,
	 * just do nothing on failure.
L
Linus Torvalds 已提交
303
	 */
L
Len Brown 已提交
304 305 306
	if (ACPI_SUCCESS(status)) {
		pci_id->device = ACPI_HIWORD(ACPI_LODWORD(pci_value));
		pci_id->function = ACPI_LOWORD(ACPI_LODWORD(pci_value));
L
Linus Torvalds 已提交
307 308 309 310
	}

	/* The PCI segment number comes from the _SEG method */

L
Len Brown 已提交
311 312 313 314 315
	status =
	    acpi_ut_evaluate_numeric_object(METHOD_NAME__SEG, pci_root_node,
					    &pci_value);
	if (ACPI_SUCCESS(status)) {
		pci_id->segment = ACPI_LOWORD(pci_value);
L
Linus Torvalds 已提交
316 317 318 319
	}

	/* The PCI bus number comes from the _BBN method */

L
Len Brown 已提交
320 321 322 323 324
	status =
	    acpi_ut_evaluate_numeric_object(METHOD_NAME__BBN, pci_root_node,
					    &pci_value);
	if (ACPI_SUCCESS(status)) {
		pci_id->bus = ACPI_LOWORD(pci_value);
L
Linus Torvalds 已提交
325 326 327 328
	}

	/* Complete this device's pci_id */

L
Len Brown 已提交
329
	acpi_os_derive_pci_id(pci_root_node, region_obj->region.node, &pci_id);
L
Linus Torvalds 已提交
330 331

	*region_context = pci_id;
L
Len Brown 已提交
332
	return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
333 334
}

335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
/*******************************************************************************
 *
 * FUNCTION:    acpi_ev_match_pci_root_bridge
 *
 * PARAMETERS:  Id              - The HID/CID in string format
 *
 * RETURN:      TRUE if the Id is a match for a PCI/PCI-Express Root Bridge
 *
 * DESCRIPTION: Determine if the input ID is a PCI Root Bridge ID.
 *
 ******************************************************************************/

static u8 acpi_ev_match_pci_root_bridge(char *id)
{

	/*
	 * Check if this is a PCI root.
	 * ACPI 3.0+: check for a PCI Express root also.
	 */
	if (!(ACPI_STRNCMP(id,
			   PCI_ROOT_HID_STRING,
			   sizeof(PCI_ROOT_HID_STRING))) ||
	    !(ACPI_STRNCMP(id,
			   PCI_EXPRESS_ROOT_HID_STRING,
			   sizeof(PCI_EXPRESS_ROOT_HID_STRING)))) {
		return (TRUE);
	}

	return (FALSE);
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ev_is_pci_root_bridge
 *
 * PARAMETERS:  Node            - Device node being examined
 *
 * RETURN:      TRUE if device is a PCI/PCI-Express Root Bridge
 *
 * DESCRIPTION: Determine if the input device represents a PCI Root Bridge by
 *              examining the _HID and _CID for the device.
 *
 ******************************************************************************/

static u8 acpi_ev_is_pci_root_bridge(struct acpi_namespace_node *node)
{
	acpi_status status;
382
	struct acpica_device_id hid;
383
	struct acpi_compatible_id_list *cid;
384
	u32 i;
385

386 387
	/* Get the _HID and check for a PCI Root Bridge */

388 389 390 391 392 393 394 395 396
	status = acpi_ut_execute_HID(node, &hid);
	if (ACPI_FAILURE(status)) {
		return (FALSE);
	}

	if (acpi_ev_match_pci_root_bridge(hid.value)) {
		return (TRUE);
	}

397 398
	/* The _HID did not match. Get the _CID and check for a PCI Root Bridge */

399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
	status = acpi_ut_execute_CID(node, &cid);
	if (ACPI_FAILURE(status)) {
		return (FALSE);
	}

	/* Check all _CIDs in the returned list */

	for (i = 0; i < cid->count; i++) {
		if (acpi_ev_match_pci_root_bridge(cid->id[i].value)) {
			ACPI_FREE(cid);
			return (TRUE);
		}
	}

	ACPI_FREE(cid);
	return (FALSE);
}

L
Linus Torvalds 已提交
417 418 419 420 421 422 423 424 425 426 427
/*******************************************************************************
 *
 * FUNCTION:    acpi_ev_pci_bar_region_setup
 *
 * PARAMETERS:  Handle              - Region we are interested in
 *              Function            - Start or stop
 *              handler_context     - Address space handler context
 *              region_context      - Region specific context
 *
 * RETURN:      Status
 *
R
Robert Moore 已提交
428
 * DESCRIPTION: Setup a pci_bAR operation region
L
Linus Torvalds 已提交
429 430 431 432 433 434
 *
 * MUTEX:       Assumes namespace is not locked
 *
 ******************************************************************************/

acpi_status
L
Len Brown 已提交
435 436 437
acpi_ev_pci_bar_region_setup(acpi_handle handle,
			     u32 function,
			     void *handler_context, void **region_context)
L
Linus Torvalds 已提交
438
{
B
Bob Moore 已提交
439
	ACPI_FUNCTION_TRACE(ev_pci_bar_region_setup);
L
Linus Torvalds 已提交
440

L
Len Brown 已提交
441
	return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
442 443 444 445 446 447 448 449 450 451 452 453 454
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ev_cmos_region_setup
 *
 * PARAMETERS:  Handle              - Region we are interested in
 *              Function            - Start or stop
 *              handler_context     - Address space handler context
 *              region_context      - Region specific context
 *
 * RETURN:      Status
 *
R
Robert Moore 已提交
455
 * DESCRIPTION: Setup a CMOS operation region
L
Linus Torvalds 已提交
456 457 458 459 460 461
 *
 * MUTEX:       Assumes namespace is not locked
 *
 ******************************************************************************/

acpi_status
L
Len Brown 已提交
462 463 464
acpi_ev_cmos_region_setup(acpi_handle handle,
			  u32 function,
			  void *handler_context, void **region_context)
L
Linus Torvalds 已提交
465
{
B
Bob Moore 已提交
466
	ACPI_FUNCTION_TRACE(ev_cmos_region_setup);
L
Linus Torvalds 已提交
467

L
Len Brown 已提交
468
	return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
469 470 471 472 473 474 475 476 477 478 479 480 481
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ev_default_region_setup
 *
 * PARAMETERS:  Handle              - Region we are interested in
 *              Function            - Start or stop
 *              handler_context     - Address space handler context
 *              region_context      - Region specific context
 *
 * RETURN:      Status
 *
R
Robert Moore 已提交
482
 * DESCRIPTION: Default region initialization
L
Linus Torvalds 已提交
483 484 485 486
 *
 ******************************************************************************/

acpi_status
L
Len Brown 已提交
487 488 489
acpi_ev_default_region_setup(acpi_handle handle,
			     u32 function,
			     void *handler_context, void **region_context)
L
Linus Torvalds 已提交
490
{
B
Bob Moore 已提交
491
	ACPI_FUNCTION_TRACE(ev_default_region_setup);
L
Linus Torvalds 已提交
492 493 494

	if (function == ACPI_REGION_DEACTIVATE) {
		*region_context = NULL;
L
Len Brown 已提交
495
	} else {
L
Linus Torvalds 已提交
496 497 498
		*region_context = handler_context;
	}

L
Len Brown 已提交
499
	return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ev_initialize_region
 *
 * PARAMETERS:  region_obj      - Region we are initializing
 *              acpi_ns_locked  - Is namespace locked?
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Initializes the region, finds any _REG methods and saves them
 *              for execution at a later time
 *
 *              Get the appropriate address space handler for a newly
 *              created region.
 *
517
 *              This also performs address space specific initialization. For
L
Linus Torvalds 已提交
518
 *              example, PCI regions must have an _ADR object that contains
519
 *              a PCI address in the scope of the definition. This address is
L
Linus Torvalds 已提交
520 521
 *              required to perform an access to PCI config space.
 *
B
Bob Moore 已提交
522 523 524
 * MUTEX:       Interpreter should be unlocked, because we may run the _REG
 *              method for this region.
 *
L
Linus Torvalds 已提交
525 526 527
 ******************************************************************************/

acpi_status
L
Len Brown 已提交
528 529
acpi_ev_initialize_region(union acpi_operand_object *region_obj,
			  u8 acpi_ns_locked)
L
Linus Torvalds 已提交
530
{
L
Len Brown 已提交
531 532 533 534 535 536 537 538
	union acpi_operand_object *handler_obj;
	union acpi_operand_object *obj_desc;
	acpi_adr_space_type space_id;
	struct acpi_namespace_node *node;
	acpi_status status;
	struct acpi_namespace_node *method_node;
	acpi_name *reg_name_ptr = (acpi_name *) METHOD_NAME__REG;
	union acpi_operand_object *region_obj2;
L
Linus Torvalds 已提交
539

B
Bob Moore 已提交
540
	ACPI_FUNCTION_TRACE_U32(ev_initialize_region, acpi_ns_locked);
L
Linus Torvalds 已提交
541 542

	if (!region_obj) {
L
Len Brown 已提交
543
		return_ACPI_STATUS(AE_BAD_PARAMETER);
L
Linus Torvalds 已提交
544 545 546
	}

	if (region_obj->common.flags & AOPOBJ_OBJECT_INITIALIZED) {
L
Len Brown 已提交
547
		return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
548 549
	}

L
Len Brown 已提交
550
	region_obj2 = acpi_ns_get_secondary_object(region_obj);
L
Linus Torvalds 已提交
551
	if (!region_obj2) {
L
Len Brown 已提交
552
		return_ACPI_STATUS(AE_NOT_EXIST);
L
Linus Torvalds 已提交
553 554
	}

L
Len Brown 已提交
555
	node = acpi_ns_get_parent_node(region_obj->region.node);
L
Linus Torvalds 已提交
556 557 558 559 560 561 562 563 564 565 566
	space_id = region_obj->region.space_id;

	/* Setup defaults */

	region_obj->region.handler = NULL;
	region_obj2->extra.method_REG = NULL;
	region_obj->common.flags &= ~(AOPOBJ_SETUP_COMPLETE);
	region_obj->common.flags |= AOPOBJ_OBJECT_INITIALIZED;

	/* Find any "_REG" method associated with this region definition */

B
Bob Moore 已提交
567 568 569
	status =
	    acpi_ns_search_one_scope(*reg_name_ptr, node, ACPI_TYPE_METHOD,
				     &method_node);
L
Len Brown 已提交
570
	if (ACPI_SUCCESS(status)) {
L
Linus Torvalds 已提交
571 572
		/*
		 * The _REG method is optional and there can be only one per region
573
		 * definition. This will be executed when the handler is attached
L
Linus Torvalds 已提交
574 575 576 577 578 579 580 581 582 583
		 * or removed
		 */
		region_obj2->extra.method_REG = method_node;
	}

	/*
	 * The following loop depends upon the root Node having no parent
	 * ie: acpi_gbl_root_node->parent_entry being set to NULL
	 */
	while (node) {
B
Bob Moore 已提交
584

L
Linus Torvalds 已提交
585 586 587
		/* Check to see if a handler exists */

		handler_obj = NULL;
L
Len Brown 已提交
588
		obj_desc = acpi_ns_get_attached_object(node);
L
Linus Torvalds 已提交
589
		if (obj_desc) {
B
Bob Moore 已提交
590

L
Linus Torvalds 已提交
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
			/* Can only be a handler if the object exists */

			switch (node->type) {
			case ACPI_TYPE_DEVICE:

				handler_obj = obj_desc->device.handler;
				break;

			case ACPI_TYPE_PROCESSOR:

				handler_obj = obj_desc->processor.handler;
				break;

			case ACPI_TYPE_THERMAL:

				handler_obj = obj_desc->thermal_zone.handler;
				break;

			default:
				/* Ignore other objects */
				break;
			}

			while (handler_obj) {
B
Bob Moore 已提交
615

L
Linus Torvalds 已提交
616 617
				/* Is this handler of the correct type? */

L
Len Brown 已提交
618 619
				if (handler_obj->address_space.space_id ==
				    space_id) {
B
Bob Moore 已提交
620

L
Linus Torvalds 已提交
621 622
					/* Found correct handler */

L
Len Brown 已提交
623 624 625 626 627
					ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
							  "Found handler %p for region %p in obj %p\n",
							  handler_obj,
							  region_obj,
							  obj_desc));
L
Linus Torvalds 已提交
628

L
Len Brown 已提交
629 630 631 632
					status =
					    acpi_ev_attach_region(handler_obj,
								  region_obj,
								  acpi_ns_locked);
L
Linus Torvalds 已提交
633 634 635 636 637 638

					/*
					 * Tell all users that this region is usable by running the _REG
					 * method
					 */
					if (acpi_ns_locked) {
L
Len Brown 已提交
639 640 641 642 643 644
						status =
						    acpi_ut_release_mutex
						    (ACPI_MTX_NAMESPACE);
						if (ACPI_FAILURE(status)) {
							return_ACPI_STATUS
							    (status);
L
Linus Torvalds 已提交
645 646 647
						}
					}

L
Len Brown 已提交
648 649 650
					status =
					    acpi_ev_execute_reg_method
					    (region_obj, 1);
L
Linus Torvalds 已提交
651 652

					if (acpi_ns_locked) {
L
Len Brown 已提交
653 654 655 656 657 658
						status =
						    acpi_ut_acquire_mutex
						    (ACPI_MTX_NAMESPACE);
						if (ACPI_FAILURE(status)) {
							return_ACPI_STATUS
							    (status);
L
Linus Torvalds 已提交
659 660 661
						}
					}

L
Len Brown 已提交
662
					return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
663 664 665 666 667 668 669 670
				}

				/* Try next handler in the list */

				handler_obj = handler_obj->address_space.next;
			}
		}

671 672
		/* This node does not have the handler we need; Pop up one level */

L
Len Brown 已提交
673
		node = acpi_ns_get_parent_node(node);
L
Linus Torvalds 已提交
674 675 676 677
	}

	/* If we get here, there is no handler for this region */

L
Len Brown 已提交
678
	ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
B
Bob Moore 已提交
679
			  "No handler for RegionType %s(%X) (RegionObj %p)\n",
L
Len Brown 已提交
680 681
			  acpi_ut_get_region_name(space_id), space_id,
			  region_obj));
L
Linus Torvalds 已提交
682

L
Len Brown 已提交
683
	return_ACPI_STATUS(AE_NOT_EXIST);
L
Linus Torvalds 已提交
684
}