1 /******************************************************************************
2  *
3  * Module Name: evrgnini- ACPI address_space (op_region) init
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2004, 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:  region_obj          - 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  *
64  * DESCRIPTION: Do any prep work for region handling, a nop for now
65  *
66  ******************************************************************************/
67 
68 acpi_status
acpi_ev_system_memory_region_setup(acpi_handle handle,u32 function,void * handler_context,void ** region_context)69 acpi_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:  region_obj          - 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  *
118  * DESCRIPTION: Do any prep work for region handling
119  *
120  ******************************************************************************/
121 
122 acpi_status
acpi_ev_io_space_region_setup(acpi_handle handle,u32 function,void * handler_context,void ** region_context)123 acpi_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  *
147  * PARAMETERS:  region_obj          - Region we are interested in
148  *              Function            - Start or stop
149  *              handler_context     - Address space handler context
150  *              region_context      - Region specific context
151  *
152  * RETURN:      Status
153  *
154  * DESCRIPTION: Do any prep work for region handling
155  *
156  * MUTEX:       Assumes namespace is not locked
157  *
158  ******************************************************************************/
159 
160 acpi_status
acpi_ev_pci_config_region_setup(acpi_handle handle,u32 function,void * handler_context,void ** region_context)161 acpi_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)) {
221 				/* Got a valid _HID, check if this is a PCI root */
222 
223 				if (!(ACPI_STRNCMP (object_hID.value, PCI_ROOT_HID_STRING,
224 						   sizeof (PCI_ROOT_HID_STRING)))) {
225 					/* Install a handler for this PCI root bridge */
226 
227 					status = acpi_install_address_space_handler ((acpi_handle) pci_root_node,
228 							   ACPI_ADR_SPACE_PCI_CONFIG,
229 							   ACPI_DEFAULT_HANDLER, NULL, NULL);
230 					if (ACPI_FAILURE (status)) {
231 						if (status == AE_SAME_HANDLER) {
232 							/*
233 							 * It is OK if the handler is already installed on the root
234 							 * bridge.  Still need to return a context object for the
235 							 * new PCI_Config operation region, however.
236 							 */
237 							status = AE_OK;
238 						}
239 						else {
240 							ACPI_REPORT_ERROR ((
241 								"Could not install pci_config handler for Root Bridge %4.4s, %s\n",
242 								acpi_ut_get_node_name (pci_root_node), acpi_format_exception (status)));
243 						}
244 					}
245 					break;
246 				}
247 			}
248 
249 			pci_root_node = acpi_ns_get_parent_node (pci_root_node);
250 		}
251 
252 		/* PCI root bridge not found, use namespace root node */
253 	}
254 	else {
255 		pci_root_node = handler_obj->address_space.node;
256 	}
257 
258 	/*
259 	 * If this region is now initialized, we are done.
260 	 * (install_address_space_handler could have initialized it)
261 	 */
262 	if (region_obj->region.flags & AOPOBJ_SETUP_COMPLETE) {
263 		return_ACPI_STATUS (AE_OK);
264 	}
265 
266 	/* Region is still not initialized. Create a new context */
267 
268 	pci_id = ACPI_MEM_CALLOCATE (sizeof (struct acpi_pci_id));
269 	if (!pci_id) {
270 		return_ACPI_STATUS (AE_NO_MEMORY);
271 	}
272 
273 	/*
274 	 * For PCI_Config space access, we need the segment, bus,
275 	 * device and function numbers.  Acquire them here.
276 	 */
277 
278 	/*
279 	 * Get the PCI device and function numbers from the _ADR object
280 	 * contained in the parent's scope.
281 	 */
282 	status = acpi_ut_evaluate_numeric_object (METHOD_NAME__ADR, parent_node, &pci_value);
283 
284 	/*
285 	 * The default is zero, and since the allocation above zeroed
286 	 * the data, just do nothing on failure.
287 	 */
288 	if (ACPI_SUCCESS (status)) {
289 		pci_id->device  = ACPI_HIWORD (ACPI_LODWORD (pci_value));
290 		pci_id->function = ACPI_LOWORD (ACPI_LODWORD (pci_value));
291 	}
292 
293 	/* The PCI segment number comes from the _SEG method */
294 
295 	status = acpi_ut_evaluate_numeric_object (METHOD_NAME__SEG, pci_root_node, &pci_value);
296 	if (ACPI_SUCCESS (status)) {
297 		pci_id->segment = ACPI_LOWORD (pci_value);
298 	}
299 
300 	/* The PCI bus number comes from the _BBN method */
301 
302 	status = acpi_ut_evaluate_numeric_object (METHOD_NAME__BBN, pci_root_node, &pci_value);
303 	if (ACPI_SUCCESS (status)) {
304 		pci_id->bus = ACPI_LOWORD (pci_value);
305 	}
306 
307 	/* Complete this device's pci_id */
308 
309 	acpi_os_derive_pci_id (pci_root_node, region_obj->region.node, &pci_id);
310 
311 	*region_context = pci_id;
312 	return_ACPI_STATUS (AE_OK);
313 }
314 
315 
316 /*******************************************************************************
317  *
318  * FUNCTION:    acpi_ev_pci_bar_region_setup
319  *
320  * PARAMETERS:  region_obj          - Region we are interested in
321  *              Function            - Start or stop
322  *              handler_context     - Address space handler context
323  *              region_context      - Region specific context
324  *
325  * RETURN:      Status
326  *
327  * DESCRIPTION: Do any prep work for region handling
328  *
329  * MUTEX:       Assumes namespace is not locked
330  *
331  ******************************************************************************/
332 
333 acpi_status
acpi_ev_pci_bar_region_setup(acpi_handle handle,u32 function,void * handler_context,void ** region_context)334 acpi_ev_pci_bar_region_setup (
335 	acpi_handle                     handle,
336 	u32                             function,
337 	void                            *handler_context,
338 	void                            **region_context)
339 {
340 	ACPI_FUNCTION_TRACE ("ev_pci_bar_region_setup");
341 
342 
343 	return_ACPI_STATUS (AE_OK);
344 }
345 
346 
347 /*******************************************************************************
348  *
349  * FUNCTION:    acpi_ev_cmos_region_setup
350  *
351  * PARAMETERS:  region_obj          - Region we are interested in
352  *              Function            - Start or stop
353  *              handler_context     - Address space handler context
354  *              region_context      - Region specific context
355  *
356  * RETURN:      Status
357  *
358  * DESCRIPTION: Do any prep work for region handling
359  *
360  * MUTEX:       Assumes namespace is not locked
361  *
362  ******************************************************************************/
363 
364 acpi_status
acpi_ev_cmos_region_setup(acpi_handle handle,u32 function,void * handler_context,void ** region_context)365 acpi_ev_cmos_region_setup (
366 	acpi_handle                     handle,
367 	u32                             function,
368 	void                            *handler_context,
369 	void                            **region_context)
370 {
371 	ACPI_FUNCTION_TRACE ("ev_cmos_region_setup");
372 
373 
374 	return_ACPI_STATUS (AE_OK);
375 }
376 
377 
378 /*******************************************************************************
379  *
380  * FUNCTION:    acpi_ev_default_region_setup
381  *
382  * PARAMETERS:  region_obj          - Region we are interested in
383  *              Function            - Start or stop
384  *              handler_context     - Address space handler context
385  *              region_context      - Region specific context
386  *
387  * RETURN:      Status
388  *
389  * DESCRIPTION: Do any prep work for region handling
390  *
391  ******************************************************************************/
392 
393 acpi_status
acpi_ev_default_region_setup(acpi_handle handle,u32 function,void * handler_context,void ** region_context)394 acpi_ev_default_region_setup (
395 	acpi_handle                     handle,
396 	u32                             function,
397 	void                            *handler_context,
398 	void                            **region_context)
399 {
400 	ACPI_FUNCTION_TRACE ("ev_default_region_setup");
401 
402 
403 	if (function == ACPI_REGION_DEACTIVATE) {
404 		*region_context = NULL;
405 	}
406 	else {
407 		*region_context = handler_context;
408 	}
409 
410 	return_ACPI_STATUS (AE_OK);
411 }
412 
413 
414 /*******************************************************************************
415  *
416  * FUNCTION:    acpi_ev_initialize_region
417  *
418  * PARAMETERS:  region_obj      - Region we are initializing
419  *              acpi_ns_locked  - Is namespace locked?
420  *
421  * RETURN:      Status
422  *
423  * DESCRIPTION: Initializes the region, finds any _REG methods and saves them
424  *              for execution at a later time
425  *
426  *              Get the appropriate address space handler for a newly
427  *              created region.
428  *
429  *              This also performs address space specific initialization.  For
430  *              example, PCI regions must have an _ADR object that contains
431  *              a PCI address in the scope of the definition.  This address is
432  *              required to perform an access to PCI config space.
433  *
434  ******************************************************************************/
435 
436 acpi_status
acpi_ev_initialize_region(union acpi_operand_object * region_obj,u8 acpi_ns_locked)437 acpi_ev_initialize_region (
438 	union acpi_operand_object       *region_obj,
439 	u8                              acpi_ns_locked)
440 {
441 	union acpi_operand_object       *handler_obj;
442 	union acpi_operand_object       *obj_desc;
443 	acpi_adr_space_type             space_id;
444 	struct acpi_namespace_node      *node;
445 	acpi_status                     status;
446 	struct acpi_namespace_node      *method_node;
447 	acpi_name                       *reg_name_ptr = (acpi_name *) METHOD_NAME__REG;
448 	union acpi_operand_object       *region_obj2;
449 
450 
451 	ACPI_FUNCTION_TRACE_U32 ("ev_initialize_region", acpi_ns_locked);
452 
453 
454 	if (!region_obj) {
455 		return_ACPI_STATUS (AE_BAD_PARAMETER);
456 	}
457 
458 	if (region_obj->common.flags & AOPOBJ_OBJECT_INITIALIZED) {
459 		return_ACPI_STATUS (AE_OK);
460 	}
461 
462 	region_obj2 = acpi_ns_get_secondary_object (region_obj);
463 	if (!region_obj2) {
464 		return_ACPI_STATUS (AE_NOT_EXIST);
465 	}
466 
467 	node = acpi_ns_get_parent_node (region_obj->region.node);
468 	space_id = region_obj->region.space_id;
469 
470 	/* Setup defaults */
471 
472 	region_obj->region.handler = NULL;
473 	region_obj2->extra.method_REG = NULL;
474 	region_obj->common.flags &= ~(AOPOBJ_SETUP_COMPLETE);
475 	region_obj->common.flags |= AOPOBJ_OBJECT_INITIALIZED;
476 
477 	/* Find any "_REG" method associated with this region definition */
478 
479 	status = acpi_ns_search_node (*reg_name_ptr, node,
480 			  ACPI_TYPE_METHOD, &method_node);
481 	if (ACPI_SUCCESS (status)) {
482 		/*
483 		 * The _REG method is optional and there can be only one per region
484 		 * definition.  This will be executed when the handler is attached
485 		 * or removed
486 		 */
487 		region_obj2->extra.method_REG = method_node;
488 	}
489 
490 	/*
491 	 * The following loop depends upon the root Node having no parent
492 	 * ie: acpi_gbl_root_node->parent_entry being set to NULL
493 	 */
494 	while (node) {
495 		/* Check to see if a handler exists */
496 
497 		handler_obj = NULL;
498 		obj_desc = acpi_ns_get_attached_object (node);
499 		if (obj_desc) {
500 			/* Can only be a handler if the object exists */
501 
502 			switch (node->type) {
503 			case ACPI_TYPE_DEVICE:
504 
505 				handler_obj = obj_desc->device.handler;
506 				break;
507 
508 			case ACPI_TYPE_PROCESSOR:
509 
510 				handler_obj = obj_desc->processor.handler;
511 				break;
512 
513 			case ACPI_TYPE_THERMAL:
514 
515 				handler_obj = obj_desc->thermal_zone.handler;
516 				break;
517 
518 			default:
519 				/* Ignore other objects */
520 				break;
521 			}
522 
523 			while (handler_obj) {
524 				/* Is this handler of the correct type? */
525 
526 				if (handler_obj->address_space.space_id == space_id) {
527 					/* Found correct handler */
528 
529 					ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
530 						"Found handler %p for region %p in obj %p\n",
531 						handler_obj, region_obj, obj_desc));
532 
533 					status = acpi_ev_attach_region (handler_obj, region_obj,
534 							 acpi_ns_locked);
535 
536 					/*
537 					 * Tell all users that this region is usable by running the _REG
538 					 * method
539 					 */
540 					if (acpi_ns_locked) {
541 						status = acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
542 						if (ACPI_FAILURE (status)) {
543 							return_ACPI_STATUS (status);
544 						}
545 					}
546 
547 					status = acpi_ev_execute_reg_method (region_obj, 1);
548 
549 					if (acpi_ns_locked) {
550 						status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
551 						if (ACPI_FAILURE (status)) {
552 							return_ACPI_STATUS (status);
553 						}
554 					}
555 
556 					return_ACPI_STATUS (AE_OK);
557 				}
558 
559 				/* Try next handler in the list */
560 
561 				handler_obj = handler_obj->address_space.next;
562 			}
563 		}
564 
565 		/*
566 		 * This node does not have the handler we need;
567 		 * Pop up one level
568 		 */
569 		node = acpi_ns_get_parent_node (node);
570 	}
571 
572 	/* If we get here, there is no handler for this region */
573 
574 	ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
575 		"No handler for region_type %s(%X) (region_obj %p)\n",
576 		acpi_ut_get_region_name (space_id), space_id, region_obj));
577 
578 	return_ACPI_STATUS (AE_NOT_EXIST);
579 }
580 
581