treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / acpi / acpica / evrgnini.c
blobaefc0145e583d76aa06205d6ef1adb83efae0b47
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 - 2020, Intel Corp.
8 *****************************************************************************/
10 #include <acpi/acpi.h>
11 #include "accommon.h"
12 #include "acevents.h"
13 #include "acnamesp.h"
14 #include "acinterp.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
28 * RETURN: Status
30 * DESCRIPTION: Setup a system_memory operation region
32 ******************************************************************************/
33 acpi_status
34 acpi_ev_system_memory_region_setup(acpi_handle handle,
35 u32 function,
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->
55 mapped_length);
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
89 * RETURN: Status
91 * DESCRIPTION: Setup a IO operation region
93 ******************************************************************************/
95 acpi_status
96 acpi_ev_io_space_region_setup(acpi_handle handle,
97 u32 function,
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;
104 } else {
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
120 * RETURN: Status
122 * DESCRIPTION: Setup a PCI_Config operation region
124 * MUTEX: Assumes namespace is not locked
126 ******************************************************************************/
128 acpi_status
129 acpi_ev_pci_config_region_setup(acpi_handle handle,
130 u32 function,
131 void *handler_context, void **region_context)
133 acpi_status status = AE_OK;
134 u64 pci_value;
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;
146 if (!handler_obj) {
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",
153 region_obj));
154 return_ACPI_STATUS(AE_NOT_EXIST);
157 *region_context = NULL;
158 if (function == ACPI_REGION_DEACTIVATE) {
159 if (pci_id) {
160 ACPI_FREE(pci_id);
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
169 * is installed.
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.
201 } else {
202 ACPI_EXCEPTION((AE_INFO, status,
203 "Could not install PciConfig handler "
204 "for Root Bridge %4.4s",
205 acpi_ut_get_node_name
206 (pci_root_node)));
209 break;
212 pci_root_node = pci_root_node->parent;
215 /* PCI root bridge not found, use namespace root node */
216 } else {
217 pci_root_node = handler_obj->address_space.node;
221 * If this region is now initialized, we are done.
222 * (install_address_space_handler could have initialized it)
224 if (region_obj->region.flags & AOPOBJ_SETUP_COMPLETE) {
225 return_ACPI_STATUS(AE_OK);
228 /* Region is still not initialized. Create a new context */
230 pci_id = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_pci_id));
231 if (!pci_id) {
232 return_ACPI_STATUS(AE_NO_MEMORY);
236 * For PCI_Config space access, we need the segment, bus, device and
237 * function numbers. Acquire them here.
239 * Find the parent device object. (This allows the operation region to be
240 * within a subscope under the device, such as a control method.)
242 pci_device_node = region_obj->region.node;
243 while (pci_device_node && (pci_device_node->type != ACPI_TYPE_DEVICE)) {
244 pci_device_node = pci_device_node->parent;
247 if (!pci_device_node) {
248 ACPI_FREE(pci_id);
249 return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
253 * Get the PCI device and function numbers from the _ADR object
254 * contained in the parent's scope.
256 status = acpi_ut_evaluate_numeric_object(METHOD_NAME__ADR,
257 pci_device_node, &pci_value);
260 * The default is zero, and since the allocation above zeroed the data,
261 * just do nothing on failure.
263 if (ACPI_SUCCESS(status)) {
264 pci_id->device = ACPI_HIWORD(ACPI_LODWORD(pci_value));
265 pci_id->function = ACPI_LOWORD(ACPI_LODWORD(pci_value));
268 /* The PCI segment number comes from the _SEG method */
270 status = acpi_ut_evaluate_numeric_object(METHOD_NAME__SEG,
271 pci_root_node, &pci_value);
272 if (ACPI_SUCCESS(status)) {
273 pci_id->segment = ACPI_LOWORD(pci_value);
276 /* The PCI bus number comes from the _BBN method */
278 status = acpi_ut_evaluate_numeric_object(METHOD_NAME__BBN,
279 pci_root_node, &pci_value);
280 if (ACPI_SUCCESS(status)) {
281 pci_id->bus = ACPI_LOWORD(pci_value);
284 /* Complete/update the PCI ID for this device */
286 status =
287 acpi_hw_derive_pci_id(pci_id, pci_root_node,
288 region_obj->region.node);
289 if (ACPI_FAILURE(status)) {
290 ACPI_FREE(pci_id);
291 return_ACPI_STATUS(status);
294 *region_context = pci_id;
295 return_ACPI_STATUS(AE_OK);
298 /*******************************************************************************
300 * FUNCTION: acpi_ev_is_pci_root_bridge
302 * PARAMETERS: node - Device node being examined
304 * RETURN: TRUE if device is a PCI/PCI-Express Root Bridge
306 * DESCRIPTION: Determine if the input device represents a PCI Root Bridge by
307 * examining the _HID and _CID for the device.
309 ******************************************************************************/
311 u8 acpi_ev_is_pci_root_bridge(struct acpi_namespace_node *node)
313 acpi_status status;
314 struct acpi_pnp_device_id *hid;
315 struct acpi_pnp_device_id_list *cid;
316 u32 i;
317 u8 match;
319 /* Get the _HID and check for a PCI Root Bridge */
321 status = acpi_ut_execute_HID(node, &hid);
322 if (ACPI_FAILURE(status)) {
323 return (FALSE);
326 match = acpi_ut_is_pci_root_bridge(hid->string);
327 ACPI_FREE(hid);
329 if (match) {
330 return (TRUE);
333 /* The _HID did not match. Get the _CID and check for a PCI Root Bridge */
335 status = acpi_ut_execute_CID(node, &cid);
336 if (ACPI_FAILURE(status)) {
337 return (FALSE);
340 /* Check all _CIDs in the returned list */
342 for (i = 0; i < cid->count; i++) {
343 if (acpi_ut_is_pci_root_bridge(cid->ids[i].string)) {
344 ACPI_FREE(cid);
345 return (TRUE);
349 ACPI_FREE(cid);
350 return (FALSE);
353 /*******************************************************************************
355 * FUNCTION: acpi_ev_pci_bar_region_setup
357 * PARAMETERS: handle - Region we are interested in
358 * function - Start or stop
359 * handler_context - Address space handler context
360 * region_context - Region specific context
362 * RETURN: Status
364 * DESCRIPTION: Setup a pci_BAR operation region
366 * MUTEX: Assumes namespace is not locked
368 ******************************************************************************/
370 acpi_status
371 acpi_ev_pci_bar_region_setup(acpi_handle handle,
372 u32 function,
373 void *handler_context, void **region_context)
375 ACPI_FUNCTION_TRACE(ev_pci_bar_region_setup);
377 return_ACPI_STATUS(AE_OK);
380 /*******************************************************************************
382 * FUNCTION: acpi_ev_cmos_region_setup
384 * PARAMETERS: handle - Region we are interested in
385 * function - Start or stop
386 * handler_context - Address space handler context
387 * region_context - Region specific context
389 * RETURN: Status
391 * DESCRIPTION: Setup a CMOS operation region
393 * MUTEX: Assumes namespace is not locked
395 ******************************************************************************/
397 acpi_status
398 acpi_ev_cmos_region_setup(acpi_handle handle,
399 u32 function,
400 void *handler_context, void **region_context)
402 ACPI_FUNCTION_TRACE(ev_cmos_region_setup);
404 return_ACPI_STATUS(AE_OK);
407 /*******************************************************************************
409 * FUNCTION: acpi_ev_default_region_setup
411 * PARAMETERS: handle - Region we are interested in
412 * function - Start or stop
413 * handler_context - Address space handler context
414 * region_context - Region specific context
416 * RETURN: Status
418 * DESCRIPTION: Default region initialization
420 ******************************************************************************/
422 acpi_status
423 acpi_ev_default_region_setup(acpi_handle handle,
424 u32 function,
425 void *handler_context, void **region_context)
427 ACPI_FUNCTION_TRACE(ev_default_region_setup);
429 if (function == ACPI_REGION_DEACTIVATE) {
430 *region_context = NULL;
431 } else {
432 *region_context = handler_context;
435 return_ACPI_STATUS(AE_OK);
438 /*******************************************************************************
440 * FUNCTION: acpi_ev_initialize_region
442 * PARAMETERS: region_obj - Region we are initializing
444 * RETURN: Status
446 * DESCRIPTION: Initializes the region, finds any _REG methods and saves them
447 * for execution at a later time
449 * Get the appropriate address space handler for a newly
450 * created region.
452 * This also performs address space specific initialization. For
453 * example, PCI regions must have an _ADR object that contains
454 * a PCI address in the scope of the definition. This address is
455 * required to perform an access to PCI config space.
457 * MUTEX: Interpreter should be unlocked, because we may run the _REG
458 * method for this region.
460 * NOTE: Possible incompliance:
461 * There is a behavior conflict in automatic _REG execution:
462 * 1. When the interpreter is evaluating a method, we can only
463 * automatically run _REG for the following case:
464 * operation_region (OPR1, 0x80, 0x1000010, 0x4)
465 * 2. When the interpreter is loading a table, we can also
466 * automatically run _REG for the following case:
467 * operation_region (OPR1, 0x80, 0x1000010, 0x4)
468 * Though this may not be compliant to the de-facto standard, the
469 * logic is kept in order not to trigger regressions. And keeping
470 * this logic should be taken care by the caller of this function.
472 ******************************************************************************/
474 acpi_status acpi_ev_initialize_region(union acpi_operand_object *region_obj)
476 union acpi_operand_object *handler_obj;
477 union acpi_operand_object *obj_desc;
478 acpi_adr_space_type space_id;
479 struct acpi_namespace_node *node;
481 ACPI_FUNCTION_TRACE(ev_initialize_region);
483 if (!region_obj) {
484 return_ACPI_STATUS(AE_BAD_PARAMETER);
487 if (region_obj->common.flags & AOPOBJ_OBJECT_INITIALIZED) {
488 return_ACPI_STATUS(AE_OK);
491 region_obj->common.flags |= AOPOBJ_OBJECT_INITIALIZED;
493 node = region_obj->region.node->parent;
494 space_id = region_obj->region.space_id;
497 * The following loop depends upon the root Node having no parent
498 * ie: acpi_gbl_root_node->Parent being set to NULL
500 while (node) {
502 /* Check to see if a handler exists */
504 handler_obj = NULL;
505 obj_desc = acpi_ns_get_attached_object(node);
506 if (obj_desc) {
508 /* Can only be a handler if the object exists */
510 switch (node->type) {
511 case ACPI_TYPE_DEVICE:
512 case ACPI_TYPE_PROCESSOR:
513 case ACPI_TYPE_THERMAL:
515 handler_obj = obj_desc->common_notify.handler;
516 break;
518 default:
520 /* Ignore other objects */
522 break;
525 handler_obj =
526 acpi_ev_find_region_handler(space_id, handler_obj);
527 if (handler_obj) {
529 /* Found correct handler */
531 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
532 "Found handler %p for region %p in obj %p\n",
533 handler_obj, region_obj,
534 obj_desc));
536 (void)acpi_ev_attach_region(handler_obj,
537 region_obj, FALSE);
540 * Tell all users that this region is usable by
541 * running the _REG method
543 acpi_ex_exit_interpreter();
544 (void)acpi_ev_execute_reg_method(region_obj,
545 ACPI_REG_CONNECT);
546 acpi_ex_enter_interpreter();
547 return_ACPI_STATUS(AE_OK);
551 /* This node does not have the handler we need; Pop up one level */
553 node = node->parent;
557 * If we get here, there is no handler for this region. This is not
558 * fatal because many regions get created before a handler is installed
559 * for said region.
561 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
562 "No handler for RegionType %s(%X) (RegionObj %p)\n",
563 acpi_ut_get_region_name(space_id), space_id,
564 region_obj));
566 return_ACPI_STATUS(AE_OK);