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 - 2019, Intel Corp.
8 *****************************************************************************/
10 #include <acpi/acpi.h>
16 #define _COMPONENT ACPI_EVENTS
17 ACPI_MODULE_NAME("evrgnini")
19 /*******************************************************************************
21 * FUNCTION: acpi_ev_system_memory_region_setup
23 * PARAMETERS: handle - Region we are interested in
24 * function - Start or stop
25 * handler_context - Address space handler context
26 * region_context - Region specific context
30 * DESCRIPTION: Setup a system_memory operation region
32 ******************************************************************************/
34 acpi_ev_system_memory_region_setup(acpi_handle handle
,
36 void *handler_context
, void **region_context
)
38 union acpi_operand_object
*region_desc
=
39 (union acpi_operand_object
*)handle
;
40 struct acpi_mem_space_context
*local_region_context
;
42 ACPI_FUNCTION_TRACE(ev_system_memory_region_setup
);
44 if (function
== ACPI_REGION_DEACTIVATE
) {
45 if (*region_context
) {
46 local_region_context
=
47 (struct acpi_mem_space_context
*)*region_context
;
49 /* Delete a cached mapping if present */
51 if (local_region_context
->mapped_length
) {
52 acpi_os_unmap_memory(local_region_context
->
53 mapped_logical_address
,
54 local_region_context
->
57 ACPI_FREE(local_region_context
);
58 *region_context
= NULL
;
60 return_ACPI_STATUS(AE_OK
);
63 /* Create a new context */
65 local_region_context
=
66 ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_mem_space_context
));
67 if (!(local_region_context
)) {
68 return_ACPI_STATUS(AE_NO_MEMORY
);
71 /* Save the region length and address for use in the handler */
73 local_region_context
->length
= region_desc
->region
.length
;
74 local_region_context
->address
= region_desc
->region
.address
;
76 *region_context
= local_region_context
;
77 return_ACPI_STATUS(AE_OK
);
80 /*******************************************************************************
82 * FUNCTION: acpi_ev_io_space_region_setup
84 * PARAMETERS: handle - Region we are interested in
85 * function - Start or stop
86 * handler_context - Address space handler context
87 * region_context - Region specific context
91 * DESCRIPTION: Setup a IO operation region
93 ******************************************************************************/
96 acpi_ev_io_space_region_setup(acpi_handle handle
,
98 void *handler_context
, void **region_context
)
100 ACPI_FUNCTION_TRACE(ev_io_space_region_setup
);
102 if (function
== ACPI_REGION_DEACTIVATE
) {
103 *region_context
= NULL
;
105 *region_context
= handler_context
;
108 return_ACPI_STATUS(AE_OK
);
111 /*******************************************************************************
113 * FUNCTION: acpi_ev_pci_config_region_setup
115 * PARAMETERS: handle - Region we are interested in
116 * function - Start or stop
117 * handler_context - Address space handler context
118 * region_context - Region specific context
122 * DESCRIPTION: Setup a PCI_Config operation region
124 * MUTEX: Assumes namespace is not locked
126 ******************************************************************************/
129 acpi_ev_pci_config_region_setup(acpi_handle handle
,
131 void *handler_context
, void **region_context
)
133 acpi_status status
= AE_OK
;
135 struct acpi_pci_id
*pci_id
= *region_context
;
136 union acpi_operand_object
*handler_obj
;
137 struct acpi_namespace_node
*parent_node
;
138 struct acpi_namespace_node
*pci_root_node
;
139 struct acpi_namespace_node
*pci_device_node
;
140 union acpi_operand_object
*region_obj
=
141 (union acpi_operand_object
*)handle
;
143 ACPI_FUNCTION_TRACE(ev_pci_config_region_setup
);
145 handler_obj
= region_obj
->region
.handler
;
148 * No installed handler. This shouldn't happen because the dispatch
149 * routine checks before we get here, but we check again just in case.
151 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION
,
152 "Attempting to init a region %p, with no handler\n",
154 return_ACPI_STATUS(AE_NOT_EXIST
);
157 *region_context
= NULL
;
158 if (function
== ACPI_REGION_DEACTIVATE
) {
162 return_ACPI_STATUS(status
);
165 parent_node
= region_obj
->region
.node
->parent
;
168 * Get the _SEG and _BBN values from the device upon which the handler
171 * We need to get the _SEG and _BBN objects relative to the PCI BUS device.
172 * This is the device the handler has been registered to handle.
176 * If the address_space.Node is still pointing to the root, we need
177 * to scan upward for a PCI Root bridge and re-associate the op_region
178 * handlers with that device.
180 if (handler_obj
->address_space
.node
== acpi_gbl_root_node
) {
182 /* Start search from the parent object */
184 pci_root_node
= parent_node
;
185 while (pci_root_node
!= acpi_gbl_root_node
) {
187 /* Get the _HID/_CID in order to detect a root_bridge */
189 if (acpi_ev_is_pci_root_bridge(pci_root_node
)) {
191 /* Install a handler for this PCI root bridge */
193 status
= acpi_install_address_space_handler((acpi_handle
)pci_root_node
, ACPI_ADR_SPACE_PCI_CONFIG
, ACPI_DEFAULT_HANDLER
, NULL
, NULL
);
194 if (ACPI_FAILURE(status
)) {
195 if (status
== AE_SAME_HANDLER
) {
197 * It is OK if the handler is already installed on the
198 * root bridge. Still need to return a context object
199 * for the new PCI_Config operation region, however.
203 ACPI_EXCEPTION((AE_INFO
, status
,
204 "Could not install PciConfig handler "
205 "for Root Bridge %4.4s",
206 acpi_ut_get_node_name
213 pci_root_node
= pci_root_node
->parent
;
216 /* PCI root bridge not found, use namespace root node */
218 pci_root_node
= handler_obj
->address_space
.node
;
222 * If this region is now initialized, we are done.
223 * (install_address_space_handler could have initialized it)
225 if (region_obj
->region
.flags
& AOPOBJ_SETUP_COMPLETE
) {
226 return_ACPI_STATUS(AE_OK
);
229 /* Region is still not initialized. Create a new context */
231 pci_id
= ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_pci_id
));
233 return_ACPI_STATUS(AE_NO_MEMORY
);
237 * For PCI_Config space access, we need the segment, bus, device and
238 * function numbers. Acquire them here.
240 * Find the parent device object. (This allows the operation region to be
241 * within a subscope under the device, such as a control method.)
243 pci_device_node
= region_obj
->region
.node
;
244 while (pci_device_node
&& (pci_device_node
->type
!= ACPI_TYPE_DEVICE
)) {
245 pci_device_node
= pci_device_node
->parent
;
248 if (!pci_device_node
) {
250 return_ACPI_STATUS(AE_AML_OPERAND_TYPE
);
254 * Get the PCI device and function numbers from the _ADR object
255 * contained in the parent's scope.
257 status
= acpi_ut_evaluate_numeric_object(METHOD_NAME__ADR
,
258 pci_device_node
, &pci_value
);
261 * The default is zero, and since the allocation above zeroed the data,
262 * just do nothing on failure.
264 if (ACPI_SUCCESS(status
)) {
265 pci_id
->device
= ACPI_HIWORD(ACPI_LODWORD(pci_value
));
266 pci_id
->function
= ACPI_LOWORD(ACPI_LODWORD(pci_value
));
269 /* The PCI segment number comes from the _SEG method */
271 status
= acpi_ut_evaluate_numeric_object(METHOD_NAME__SEG
,
272 pci_root_node
, &pci_value
);
273 if (ACPI_SUCCESS(status
)) {
274 pci_id
->segment
= ACPI_LOWORD(pci_value
);
277 /* The PCI bus number comes from the _BBN method */
279 status
= acpi_ut_evaluate_numeric_object(METHOD_NAME__BBN
,
280 pci_root_node
, &pci_value
);
281 if (ACPI_SUCCESS(status
)) {
282 pci_id
->bus
= ACPI_LOWORD(pci_value
);
285 /* Complete/update the PCI ID for this device */
288 acpi_hw_derive_pci_id(pci_id
, pci_root_node
,
289 region_obj
->region
.node
);
290 if (ACPI_FAILURE(status
)) {
292 return_ACPI_STATUS(status
);
295 *region_context
= pci_id
;
296 return_ACPI_STATUS(AE_OK
);
299 /*******************************************************************************
301 * FUNCTION: acpi_ev_is_pci_root_bridge
303 * PARAMETERS: node - Device node being examined
305 * RETURN: TRUE if device is a PCI/PCI-Express Root Bridge
307 * DESCRIPTION: Determine if the input device represents a PCI Root Bridge by
308 * examining the _HID and _CID for the device.
310 ******************************************************************************/
312 u8
acpi_ev_is_pci_root_bridge(struct acpi_namespace_node
*node
)
315 struct acpi_pnp_device_id
*hid
;
316 struct acpi_pnp_device_id_list
*cid
;
320 /* Get the _HID and check for a PCI Root Bridge */
322 status
= acpi_ut_execute_HID(node
, &hid
);
323 if (ACPI_FAILURE(status
)) {
327 match
= acpi_ut_is_pci_root_bridge(hid
->string
);
334 /* The _HID did not match. Get the _CID and check for a PCI Root Bridge */
336 status
= acpi_ut_execute_CID(node
, &cid
);
337 if (ACPI_FAILURE(status
)) {
341 /* Check all _CIDs in the returned list */
343 for (i
= 0; i
< cid
->count
; i
++) {
344 if (acpi_ut_is_pci_root_bridge(cid
->ids
[i
].string
)) {
354 /*******************************************************************************
356 * FUNCTION: acpi_ev_pci_bar_region_setup
358 * PARAMETERS: handle - Region we are interested in
359 * function - Start or stop
360 * handler_context - Address space handler context
361 * region_context - Region specific context
365 * DESCRIPTION: Setup a pci_BAR operation region
367 * MUTEX: Assumes namespace is not locked
369 ******************************************************************************/
372 acpi_ev_pci_bar_region_setup(acpi_handle handle
,
374 void *handler_context
, void **region_context
)
376 ACPI_FUNCTION_TRACE(ev_pci_bar_region_setup
);
378 return_ACPI_STATUS(AE_OK
);
381 /*******************************************************************************
383 * FUNCTION: acpi_ev_cmos_region_setup
385 * PARAMETERS: handle - Region we are interested in
386 * function - Start or stop
387 * handler_context - Address space handler context
388 * region_context - Region specific context
392 * DESCRIPTION: Setup a CMOS operation region
394 * MUTEX: Assumes namespace is not locked
396 ******************************************************************************/
399 acpi_ev_cmos_region_setup(acpi_handle handle
,
401 void *handler_context
, void **region_context
)
403 ACPI_FUNCTION_TRACE(ev_cmos_region_setup
);
405 return_ACPI_STATUS(AE_OK
);
408 /*******************************************************************************
410 * FUNCTION: acpi_ev_default_region_setup
412 * PARAMETERS: handle - Region we are interested in
413 * function - Start or stop
414 * handler_context - Address space handler context
415 * region_context - Region specific context
419 * DESCRIPTION: Default region initialization
421 ******************************************************************************/
424 acpi_ev_default_region_setup(acpi_handle handle
,
426 void *handler_context
, void **region_context
)
428 ACPI_FUNCTION_TRACE(ev_default_region_setup
);
430 if (function
== ACPI_REGION_DEACTIVATE
) {
431 *region_context
= NULL
;
433 *region_context
= handler_context
;
436 return_ACPI_STATUS(AE_OK
);
439 /*******************************************************************************
441 * FUNCTION: acpi_ev_initialize_region
443 * PARAMETERS: region_obj - Region we are initializing
447 * DESCRIPTION: Initializes the region, finds any _REG methods and saves them
448 * for execution at a later time
450 * Get the appropriate address space handler for a newly
453 * This also performs address space specific initialization. For
454 * example, PCI regions must have an _ADR object that contains
455 * a PCI address in the scope of the definition. This address is
456 * required to perform an access to PCI config space.
458 * MUTEX: Interpreter should be unlocked, because we may run the _REG
459 * method for this region.
461 * NOTE: Possible incompliance:
462 * There is a behavior conflict in automatic _REG execution:
463 * 1. When the interpreter is evaluating a method, we can only
464 * automatically run _REG for the following case:
465 * operation_region (OPR1, 0x80, 0x1000010, 0x4)
466 * 2. When the interpreter is loading a table, we can also
467 * automatically run _REG for the following case:
468 * operation_region (OPR1, 0x80, 0x1000010, 0x4)
469 * Though this may not be compliant to the de-facto standard, the
470 * logic is kept in order not to trigger regressions. And keeping
471 * this logic should be taken care by the caller of this function.
473 ******************************************************************************/
475 acpi_status
acpi_ev_initialize_region(union acpi_operand_object
*region_obj
)
477 union acpi_operand_object
*handler_obj
;
478 union acpi_operand_object
*obj_desc
;
479 acpi_adr_space_type space_id
;
480 struct acpi_namespace_node
*node
;
482 ACPI_FUNCTION_TRACE(ev_initialize_region
);
485 return_ACPI_STATUS(AE_BAD_PARAMETER
);
488 if (region_obj
->common
.flags
& AOPOBJ_OBJECT_INITIALIZED
) {
489 return_ACPI_STATUS(AE_OK
);
492 region_obj
->common
.flags
|= AOPOBJ_OBJECT_INITIALIZED
;
494 node
= region_obj
->region
.node
->parent
;
495 space_id
= region_obj
->region
.space_id
;
498 * The following loop depends upon the root Node having no parent
499 * ie: acpi_gbl_root_node->Parent being set to NULL
503 /* Check to see if a handler exists */
506 obj_desc
= acpi_ns_get_attached_object(node
);
509 /* Can only be a handler if the object exists */
511 switch (node
->type
) {
512 case ACPI_TYPE_DEVICE
:
513 case ACPI_TYPE_PROCESSOR
:
514 case ACPI_TYPE_THERMAL
:
516 handler_obj
= obj_desc
->common_notify
.handler
;
521 /* Ignore other objects */
527 acpi_ev_find_region_handler(space_id
, handler_obj
);
530 /* Found correct handler */
532 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION
,
533 "Found handler %p for region %p in obj %p\n",
534 handler_obj
, region_obj
,
537 (void)acpi_ev_attach_region(handler_obj
,
541 * Tell all users that this region is usable by
542 * running the _REG method
544 acpi_ex_exit_interpreter();
545 (void)acpi_ev_execute_reg_method(region_obj
,
547 acpi_ex_enter_interpreter();
548 return_ACPI_STATUS(AE_OK
);
552 /* This node does not have the handler we need; Pop up one level */
558 * If we get here, there is no handler for this region. This is not
559 * fatal because many regions get created before a handler is installed
562 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION
,
563 "No handler for RegionType %s(%X) (RegionObj %p)\n",
564 acpi_ut_get_region_name(space_id
), space_id
,
567 return_ACPI_STATUS(AE_OK
);