1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
4 * Module Name: evrgnini- ACPI address_space (op_region) init
6 * Copyright (C) 2000 - 2018, Intel Corp.
8 *****************************************************************************/
10 #include <acpi/acpi.h>
16 #define _COMPONENT ACPI_EVENTS
17 ACPI_MODULE_NAME("evrgnini")
19 /* Local prototypes */
20 static u8 acpi_ev_is_pci_root_bridge(struct acpi_namespace_node *node);
22 /*******************************************************************************
24 * FUNCTION: acpi_ev_system_memory_region_setup
26 * PARAMETERS: handle - Region we are interested in
27 * function - Start or stop
28 * handler_context - Address space handler context
29 * region_context - Region specific context
33 * DESCRIPTION: Setup a system_memory operation region
35 ******************************************************************************/
38 acpi_ev_system_memory_region_setup(acpi_handle handle,
40 void *handler_context, void **region_context)
42 union acpi_operand_object *region_desc =
43 (union acpi_operand_object *)handle;
44 struct acpi_mem_space_context *local_region_context;
46 ACPI_FUNCTION_TRACE(ev_system_memory_region_setup);
48 if (function == ACPI_REGION_DEACTIVATE) {
49 if (*region_context) {
50 local_region_context =
51 (struct acpi_mem_space_context *)*region_context;
53 /* Delete a cached mapping if present */
55 if (local_region_context->mapped_length) {
56 acpi_os_unmap_memory(local_region_context->
57 mapped_logical_address,
58 local_region_context->
61 ACPI_FREE(local_region_context);
62 *region_context = NULL;
64 return_ACPI_STATUS(AE_OK);
67 /* Create a new context */
69 local_region_context =
70 ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_mem_space_context));
71 if (!(local_region_context)) {
72 return_ACPI_STATUS(AE_NO_MEMORY);
75 /* Save the region length and address for use in the handler */
77 local_region_context->length = region_desc->region.length;
78 local_region_context->address = region_desc->region.address;
80 *region_context = local_region_context;
81 return_ACPI_STATUS(AE_OK);
84 /*******************************************************************************
86 * FUNCTION: acpi_ev_io_space_region_setup
88 * PARAMETERS: handle - Region we are interested in
89 * function - Start or stop
90 * handler_context - Address space handler context
91 * region_context - Region specific context
95 * DESCRIPTION: Setup a IO operation region
97 ******************************************************************************/
100 acpi_ev_io_space_region_setup(acpi_handle handle,
102 void *handler_context, void **region_context)
104 ACPI_FUNCTION_TRACE(ev_io_space_region_setup);
106 if (function == ACPI_REGION_DEACTIVATE) {
107 *region_context = NULL;
109 *region_context = handler_context;
112 return_ACPI_STATUS(AE_OK);
115 /*******************************************************************************
117 * FUNCTION: acpi_ev_pci_config_region_setup
119 * PARAMETERS: handle - Region we are interested in
120 * function - Start or stop
121 * handler_context - Address space handler context
122 * region_context - Region specific context
126 * DESCRIPTION: Setup a PCI_Config operation region
128 * MUTEX: Assumes namespace is not locked
130 ******************************************************************************/
133 acpi_ev_pci_config_region_setup(acpi_handle handle,
135 void *handler_context, void **region_context)
137 acpi_status status = AE_OK;
139 struct acpi_pci_id *pci_id = *region_context;
140 union acpi_operand_object *handler_obj;
141 struct acpi_namespace_node *parent_node;
142 struct acpi_namespace_node *pci_root_node;
143 struct acpi_namespace_node *pci_device_node;
144 union acpi_operand_object *region_obj =
145 (union acpi_operand_object *)handle;
147 ACPI_FUNCTION_TRACE(ev_pci_config_region_setup);
149 handler_obj = region_obj->region.handler;
152 * No installed handler. This shouldn't happen because the dispatch
153 * routine checks before we get here, but we check again just in case.
155 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
156 "Attempting to init a region %p, with no handler\n",
158 return_ACPI_STATUS(AE_NOT_EXIST);
161 *region_context = NULL;
162 if (function == ACPI_REGION_DEACTIVATE) {
166 return_ACPI_STATUS(status);
169 parent_node = region_obj->region.node->parent;
172 * Get the _SEG and _BBN values from the device upon which the handler
175 * We need to get the _SEG and _BBN objects relative to the PCI BUS device.
176 * This is the device the handler has been registered to handle.
180 * If the address_space.Node is still pointing to the root, we need
181 * to scan upward for a PCI Root bridge and re-associate the op_region
182 * handlers with that device.
184 if (handler_obj->address_space.node == acpi_gbl_root_node) {
186 /* Start search from the parent object */
188 pci_root_node = parent_node;
189 while (pci_root_node != acpi_gbl_root_node) {
191 /* Get the _HID/_CID in order to detect a root_bridge */
193 if (acpi_ev_is_pci_root_bridge(pci_root_node)) {
195 /* Install a handler for this PCI root bridge */
197 status = acpi_install_address_space_handler((acpi_handle)pci_root_node, ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
198 if (ACPI_FAILURE(status)) {
199 if (status == AE_SAME_HANDLER) {
201 * It is OK if the handler is already installed on the
202 * root bridge. Still need to return a context object
203 * for the new PCI_Config operation region, however.
207 ACPI_EXCEPTION((AE_INFO, status,
208 "Could not install PciConfig handler "
209 "for Root Bridge %4.4s",
210 acpi_ut_get_node_name
217 pci_root_node = pci_root_node->parent;
220 /* PCI root bridge not found, use namespace root node */
222 pci_root_node = handler_obj->address_space.node;
226 * If this region is now initialized, we are done.
227 * (install_address_space_handler could have initialized it)
229 if (region_obj->region.flags & AOPOBJ_SETUP_COMPLETE) {
230 return_ACPI_STATUS(AE_OK);
233 /* Region is still not initialized. Create a new context */
235 pci_id = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_pci_id));
237 return_ACPI_STATUS(AE_NO_MEMORY);
241 * For PCI_Config space access, we need the segment, bus, device and
242 * function numbers. Acquire them here.
244 * Find the parent device object. (This allows the operation region to be
245 * within a subscope under the device, such as a control method.)
247 pci_device_node = region_obj->region.node;
248 while (pci_device_node && (pci_device_node->type != ACPI_TYPE_DEVICE)) {
249 pci_device_node = pci_device_node->parent;
252 if (!pci_device_node) {
254 return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
258 * Get the PCI device and function numbers from the _ADR object
259 * contained in the parent's scope.
261 status = acpi_ut_evaluate_numeric_object(METHOD_NAME__ADR,
262 pci_device_node, &pci_value);
265 * The default is zero, and since the allocation above zeroed the data,
266 * just do nothing on failure.
268 if (ACPI_SUCCESS(status)) {
269 pci_id->device = ACPI_HIWORD(ACPI_LODWORD(pci_value));
270 pci_id->function = ACPI_LOWORD(ACPI_LODWORD(pci_value));
273 /* The PCI segment number comes from the _SEG method */
275 status = acpi_ut_evaluate_numeric_object(METHOD_NAME__SEG,
276 pci_root_node, &pci_value);
277 if (ACPI_SUCCESS(status)) {
278 pci_id->segment = ACPI_LOWORD(pci_value);
281 /* The PCI bus number comes from the _BBN method */
283 status = acpi_ut_evaluate_numeric_object(METHOD_NAME__BBN,
284 pci_root_node, &pci_value);
285 if (ACPI_SUCCESS(status)) {
286 pci_id->bus = ACPI_LOWORD(pci_value);
289 /* Complete/update the PCI ID for this device */
292 acpi_hw_derive_pci_id(pci_id, pci_root_node,
293 region_obj->region.node);
294 if (ACPI_FAILURE(status)) {
296 return_ACPI_STATUS(status);
299 *region_context = pci_id;
300 return_ACPI_STATUS(AE_OK);
303 /*******************************************************************************
305 * FUNCTION: acpi_ev_is_pci_root_bridge
307 * PARAMETERS: node - Device node being examined
309 * RETURN: TRUE if device is a PCI/PCI-Express Root Bridge
311 * DESCRIPTION: Determine if the input device represents a PCI Root Bridge by
312 * examining the _HID and _CID for the device.
314 ******************************************************************************/
316 static u8 acpi_ev_is_pci_root_bridge(struct acpi_namespace_node *node)
319 struct acpi_pnp_device_id *hid;
320 struct acpi_pnp_device_id_list *cid;
324 /* Get the _HID and check for a PCI Root Bridge */
326 status = acpi_ut_execute_HID(node, &hid);
327 if (ACPI_FAILURE(status)) {
331 match = acpi_ut_is_pci_root_bridge(hid->string);
338 /* The _HID did not match. Get the _CID and check for a PCI Root Bridge */
340 status = acpi_ut_execute_CID(node, &cid);
341 if (ACPI_FAILURE(status)) {
345 /* Check all _CIDs in the returned list */
347 for (i = 0; i < cid->count; i++) {
348 if (acpi_ut_is_pci_root_bridge(cid->ids[i].string)) {
358 /*******************************************************************************
360 * FUNCTION: acpi_ev_pci_bar_region_setup
362 * PARAMETERS: handle - Region we are interested in
363 * function - Start or stop
364 * handler_context - Address space handler context
365 * region_context - Region specific context
369 * DESCRIPTION: Setup a pci_BAR operation region
371 * MUTEX: Assumes namespace is not locked
373 ******************************************************************************/
376 acpi_ev_pci_bar_region_setup(acpi_handle handle,
378 void *handler_context, void **region_context)
380 ACPI_FUNCTION_TRACE(ev_pci_bar_region_setup);
382 return_ACPI_STATUS(AE_OK);
385 /*******************************************************************************
387 * FUNCTION: acpi_ev_cmos_region_setup
389 * PARAMETERS: handle - Region we are interested in
390 * function - Start or stop
391 * handler_context - Address space handler context
392 * region_context - Region specific context
396 * DESCRIPTION: Setup a CMOS operation region
398 * MUTEX: Assumes namespace is not locked
400 ******************************************************************************/
403 acpi_ev_cmos_region_setup(acpi_handle handle,
405 void *handler_context, void **region_context)
407 ACPI_FUNCTION_TRACE(ev_cmos_region_setup);
409 return_ACPI_STATUS(AE_OK);
412 /*******************************************************************************
414 * FUNCTION: acpi_ev_default_region_setup
416 * PARAMETERS: handle - Region we are interested in
417 * function - Start or stop
418 * handler_context - Address space handler context
419 * region_context - Region specific context
423 * DESCRIPTION: Default region initialization
425 ******************************************************************************/
428 acpi_ev_default_region_setup(acpi_handle handle,
430 void *handler_context, void **region_context)
432 ACPI_FUNCTION_TRACE(ev_default_region_setup);
434 if (function == ACPI_REGION_DEACTIVATE) {
435 *region_context = NULL;
437 *region_context = handler_context;
440 return_ACPI_STATUS(AE_OK);
443 /*******************************************************************************
445 * FUNCTION: acpi_ev_initialize_region
447 * PARAMETERS: region_obj - Region we are initializing
451 * DESCRIPTION: Initializes the region, finds any _REG methods and saves them
452 * for execution at a later time
454 * Get the appropriate address space handler for a newly
457 * This also performs address space specific initialization. For
458 * example, PCI regions must have an _ADR object that contains
459 * a PCI address in the scope of the definition. This address is
460 * required to perform an access to PCI config space.
462 * MUTEX: Interpreter should be unlocked, because we may run the _REG
463 * method for this region.
465 * NOTE: Possible incompliance:
466 * There is a behavior conflict in automatic _REG execution:
467 * 1. When the interpreter is evaluating a method, we can only
468 * automatically run _REG for the following case:
469 * operation_region (OPR1, 0x80, 0x1000010, 0x4)
470 * 2. When the interpreter is loading a table, we can also
471 * automatically run _REG for the following case:
472 * operation_region (OPR1, 0x80, 0x1000010, 0x4)
473 * Though this may not be compliant to the de-facto standard, the
474 * logic is kept in order not to trigger regressions. And keeping
475 * this logic should be taken care by the caller of this function.
477 ******************************************************************************/
479 acpi_status acpi_ev_initialize_region(union acpi_operand_object *region_obj)
481 union acpi_operand_object *handler_obj;
482 union acpi_operand_object *obj_desc;
483 acpi_adr_space_type space_id;
484 struct acpi_namespace_node *node;
486 ACPI_FUNCTION_TRACE(ev_initialize_region);
489 return_ACPI_STATUS(AE_BAD_PARAMETER);
492 if (region_obj->common.flags & AOPOBJ_OBJECT_INITIALIZED) {
493 return_ACPI_STATUS(AE_OK);
496 region_obj->common.flags |= AOPOBJ_OBJECT_INITIALIZED;
498 node = region_obj->region.node->parent;
499 space_id = region_obj->region.space_id;
502 * The following loop depends upon the root Node having no parent
503 * ie: acpi_gbl_root_node->Parent being set to NULL
507 /* Check to see if a handler exists */
510 obj_desc = acpi_ns_get_attached_object(node);
513 /* Can only be a handler if the object exists */
515 switch (node->type) {
516 case ACPI_TYPE_DEVICE:
517 case ACPI_TYPE_PROCESSOR:
518 case ACPI_TYPE_THERMAL:
520 handler_obj = obj_desc->common_notify.handler;
523 case ACPI_TYPE_METHOD:
525 * If we are executing module level code, the original
526 * Node's object was replaced by this Method object and we
527 * saved the handler in the method object.
529 * Note: Only used for the legacy MLC support. Will
530 * be removed in the future.
532 * See acpi_ns_exec_module_code
534 if (!acpi_gbl_execute_tables_as_methods &&
536 info_flags & ACPI_METHOD_MODULE_LEVEL) {
538 obj_desc->method.dispatch.handler;
544 /* Ignore other objects */
550 acpi_ev_find_region_handler(space_id, handler_obj);
553 /* Found correct handler */
555 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
556 "Found handler %p for region %p in obj %p\n",
557 handler_obj, region_obj,
560 (void)acpi_ev_attach_region(handler_obj,
564 * Tell all users that this region is usable by
565 * running the _REG method
567 acpi_ex_exit_interpreter();
568 (void)acpi_ev_execute_reg_method(region_obj,
570 acpi_ex_enter_interpreter();
571 return_ACPI_STATUS(AE_OK);
575 /* This node does not have the handler we need; Pop up one level */
581 * If we get here, there is no handler for this region. This is not
582 * fatal because many regions get created before a handler is installed
585 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
586 "No handler for RegionType %s(%X) (RegionObj %p)\n",
587 acpi_ut_get_region_name(space_id), space_id,
590 return_ACPI_STATUS(AE_OK);