1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
4 * Module Name: exregion - ACPI default op_region (address space) handlers
6 * Copyright (C) 2000 - 2020, Intel Corp.
8 *****************************************************************************/
10 #include <acpi/acpi.h>
14 #define _COMPONENT ACPI_EXECUTER
15 ACPI_MODULE_NAME("exregion")
17 /*******************************************************************************
19 * FUNCTION: acpi_ex_system_memory_space_handler
21 * PARAMETERS: function - Read or Write operation
22 * address - Where in the space to read or write
23 * bit_width - Field width in bits (8, 16, or 32)
24 * value - Pointer to in or out value
25 * handler_context - Pointer to Handler's context
26 * region_context - Pointer to context specific to the
31 * DESCRIPTION: Handler for the System Memory address space (Op Region)
33 ******************************************************************************/
35 acpi_ex_system_memory_space_handler(u32 function
,
36 acpi_physical_address address
,
39 void *handler_context
, void *region_context
)
41 acpi_status status
= AE_OK
;
42 void *logical_addr_ptr
= NULL
;
43 struct acpi_mem_space_context
*mem_info
= region_context
;
44 struct acpi_mem_mapping
*mm
= mem_info
->cur_mm
;
47 acpi_size page_boundary_map_length
;
48 #ifdef ACPI_MISALIGNMENT_NOT_SUPPORTED
52 ACPI_FUNCTION_TRACE(ex_system_memory_space_handler
);
54 /* Validate and translate the bit width */
79 ACPI_ERROR((AE_INFO
, "Invalid SystemMemory width %u",
81 return_ACPI_STATUS(AE_AML_OPERAND_VALUE
);
84 #ifdef ACPI_MISALIGNMENT_NOT_SUPPORTED
86 * Hardware does not support non-aligned data transfers, we must verify
89 (void)acpi_ut_short_divide((u64
) address
, length
, NULL
, &remainder
);
91 return_ACPI_STATUS(AE_AML_ALIGNMENT
);
96 * Does the request fit into the cached memory mapping?
97 * Is 1) Address below the current mapping? OR
98 * 2) Address beyond the current mapping?
100 if (!mm
|| (address
< mm
->physical_address
) ||
101 ((u64
) address
+ length
> (u64
) mm
->physical_address
+ mm
->length
)) {
103 * The request cannot be resolved by the current memory mapping.
105 * Look for an existing saved mapping covering the address range
106 * at hand. If found, save it as the current one and carry out
109 for (mm
= mem_info
->first_mm
; mm
; mm
= mm
->next_mm
) {
110 if (mm
== mem_info
->cur_mm
)
113 if (address
< mm
->physical_address
)
116 if ((u64
) address
+ length
>
117 (u64
) mm
->physical_address
+ mm
->length
)
120 mem_info
->cur_mm
= mm
;
124 /* Create a new mappings list entry */
125 mm
= ACPI_ALLOCATE_ZEROED(sizeof(*mm
));
128 "Unable to save memory mapping at 0x%8.8X%8.8X, size %u",
129 ACPI_FORMAT_UINT64(address
), length
));
130 return_ACPI_STATUS(AE_NO_MEMORY
);
134 * October 2009: Attempt to map from the requested address to the
135 * end of the region. However, we will never map more than one
136 * page, nor will we cross a page boundary.
138 map_length
= (acpi_size
)
139 ((mem_info
->address
+ mem_info
->length
) - address
);
142 * If mapping the entire remaining portion of the region will cross
143 * a page boundary, just map up to the page boundary, do not cross.
144 * On some systems, crossing a page boundary while mapping regions
145 * can cause warnings if the pages have different attributes
146 * due to resource management.
148 * This has the added benefit of constraining a single mapping to
149 * one page, which is similar to the original code that used a 4k
152 page_boundary_map_length
= (acpi_size
)
153 (ACPI_ROUND_UP(address
, ACPI_DEFAULT_PAGE_SIZE
) - address
);
154 if (page_boundary_map_length
== 0) {
155 page_boundary_map_length
= ACPI_DEFAULT_PAGE_SIZE
;
158 if (map_length
> page_boundary_map_length
) {
159 map_length
= page_boundary_map_length
;
162 /* Create a new mapping starting at the address given */
164 logical_addr_ptr
= acpi_os_map_memory(address
, map_length
);
165 if (!logical_addr_ptr
) {
167 "Could not map memory at 0x%8.8X%8.8X, size %u",
168 ACPI_FORMAT_UINT64(address
),
171 return_ACPI_STATUS(AE_NO_MEMORY
);
174 /* Save the physical address and mapping size */
176 mm
->logical_address
= logical_addr_ptr
;
177 mm
->physical_address
= address
;
178 mm
->length
= map_length
;
181 * Add the new entry to the mappigs list and save it as the
184 mm
->next_mm
= mem_info
->first_mm
;
185 mem_info
->first_mm
= mm
;
187 mem_info
->cur_mm
= mm
;
192 * Generate a logical pointer corresponding to the address we want to
195 logical_addr_ptr
= mm
->logical_address
+
196 ((u64
) address
- (u64
) mm
->physical_address
);
198 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
199 "System-Memory (width %u) R/W %u Address=%8.8X%8.8X\n",
200 bit_width
, function
, ACPI_FORMAT_UINT64(address
)));
203 * Perform the memory read or write
205 * Note: For machines that do not support non-aligned transfers, the target
206 * address was checked for alignment above. We do not attempt to break the
207 * transfer up into smaller (byte-size) chunks because the AML specifically
208 * asked for a transfer width that the hardware may require.
217 *value
= (u64
)ACPI_GET8(logical_addr_ptr
);
222 *value
= (u64
)ACPI_GET16(logical_addr_ptr
);
227 *value
= (u64
)ACPI_GET32(logical_addr_ptr
);
232 *value
= (u64
)ACPI_GET64(logical_addr_ptr
);
237 /* bit_width was already validated */
248 ACPI_SET8(logical_addr_ptr
, *value
);
253 ACPI_SET16(logical_addr_ptr
, *value
);
258 ACPI_SET32(logical_addr_ptr
, *value
);
263 ACPI_SET64(logical_addr_ptr
, *value
);
268 /* bit_width was already validated */
276 status
= AE_BAD_PARAMETER
;
280 return_ACPI_STATUS(status
);
283 /*******************************************************************************
285 * FUNCTION: acpi_ex_system_io_space_handler
287 * PARAMETERS: function - Read or Write operation
288 * address - Where in the space to read or write
289 * bit_width - Field width in bits (8, 16, or 32)
290 * value - Pointer to in or out value
291 * handler_context - Pointer to Handler's context
292 * region_context - Pointer to context specific to the
297 * DESCRIPTION: Handler for the System IO address space (Op Region)
299 ******************************************************************************/
302 acpi_ex_system_io_space_handler(u32 function
,
303 acpi_physical_address address
,
306 void *handler_context
, void *region_context
)
308 acpi_status status
= AE_OK
;
311 ACPI_FUNCTION_TRACE(ex_system_io_space_handler
);
313 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
314 "System-IO (width %u) R/W %u Address=%8.8X%8.8X\n",
315 bit_width
, function
, ACPI_FORMAT_UINT64(address
)));
317 /* Decode the function parameter */
322 status
= acpi_hw_read_port((acpi_io_address
)address
,
323 &value32
, bit_width
);
329 status
= acpi_hw_write_port((acpi_io_address
)address
,
330 (u32
)*value
, bit_width
);
335 status
= AE_BAD_PARAMETER
;
339 return_ACPI_STATUS(status
);
342 #ifdef ACPI_PCI_CONFIGURED
343 /*******************************************************************************
345 * FUNCTION: acpi_ex_pci_config_space_handler
347 * PARAMETERS: function - Read or Write operation
348 * address - Where in the space to read or write
349 * bit_width - Field width in bits (8, 16, or 32)
350 * value - Pointer to in or out value
351 * handler_context - Pointer to Handler's context
352 * region_context - Pointer to context specific to the
357 * DESCRIPTION: Handler for the PCI Config address space (Op Region)
359 ******************************************************************************/
362 acpi_ex_pci_config_space_handler(u32 function
,
363 acpi_physical_address address
,
366 void *handler_context
, void *region_context
)
368 acpi_status status
= AE_OK
;
369 struct acpi_pci_id
*pci_id
;
372 ACPI_FUNCTION_TRACE(ex_pci_config_space_handler
);
375 * The arguments to acpi_os(Read|Write)pci_configuration are:
377 * pci_segment is the PCI bus segment range 0-31
378 * pci_bus is the PCI bus number range 0-255
379 * pci_device is the PCI device number range 0-31
380 * pci_function is the PCI device function number
381 * pci_register is the Config space register range 0-255 bytes
383 * value - input value for write, output address for read
386 pci_id
= (struct acpi_pci_id
*)region_context
;
387 pci_register
= (u16
) (u32
) address
;
389 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
390 "Pci-Config %u (%u) Seg(%04x) Bus(%04x) "
391 "Dev(%04x) Func(%04x) Reg(%04x)\n",
392 function
, bit_width
, pci_id
->segment
, pci_id
->bus
,
393 pci_id
->device
, pci_id
->function
, pci_register
));
400 acpi_os_read_pci_configuration(pci_id
, pci_register
, value
,
407 acpi_os_write_pci_configuration(pci_id
, pci_register
,
413 status
= AE_BAD_PARAMETER
;
417 return_ACPI_STATUS(status
);
421 /*******************************************************************************
423 * FUNCTION: acpi_ex_cmos_space_handler
425 * PARAMETERS: function - Read or Write operation
426 * address - Where in the space to read or write
427 * bit_width - Field width in bits (8, 16, or 32)
428 * value - Pointer to in or out value
429 * handler_context - Pointer to Handler's context
430 * region_context - Pointer to context specific to the
435 * DESCRIPTION: Handler for the CMOS address space (Op Region)
437 ******************************************************************************/
440 acpi_ex_cmos_space_handler(u32 function
,
441 acpi_physical_address address
,
444 void *handler_context
, void *region_context
)
446 acpi_status status
= AE_OK
;
448 ACPI_FUNCTION_TRACE(ex_cmos_space_handler
);
450 return_ACPI_STATUS(status
);
453 #ifdef ACPI_PCI_CONFIGURED
454 /*******************************************************************************
456 * FUNCTION: acpi_ex_pci_bar_space_handler
458 * PARAMETERS: function - Read or Write operation
459 * address - Where in the space to read or write
460 * bit_width - Field width in bits (8, 16, or 32)
461 * value - Pointer to in or out value
462 * handler_context - Pointer to Handler's context
463 * region_context - Pointer to context specific to the
468 * DESCRIPTION: Handler for the PCI bar_target address space (Op Region)
470 ******************************************************************************/
473 acpi_ex_pci_bar_space_handler(u32 function
,
474 acpi_physical_address address
,
477 void *handler_context
, void *region_context
)
479 acpi_status status
= AE_OK
;
481 ACPI_FUNCTION_TRACE(ex_pci_bar_space_handler
);
483 return_ACPI_STATUS(status
);
487 /*******************************************************************************
489 * FUNCTION: acpi_ex_data_table_space_handler
491 * PARAMETERS: function - Read or Write operation
492 * address - Where in the space to read or write
493 * bit_width - Field width in bits (8, 16, or 32)
494 * value - Pointer to in or out value
495 * handler_context - Pointer to Handler's context
496 * region_context - Pointer to context specific to the
501 * DESCRIPTION: Handler for the Data Table address space (Op Region)
503 ******************************************************************************/
506 acpi_ex_data_table_space_handler(u32 function
,
507 acpi_physical_address address
,
510 void *handler_context
, void *region_context
)
512 ACPI_FUNCTION_TRACE(ex_data_table_space_handler
);
515 * Perform the memory read or write. The bit_width was already
521 memcpy(ACPI_CAST_PTR(char, value
),
522 ACPI_PHYSADDR_TO_PTR(address
), ACPI_DIV_8(bit_width
));
527 memcpy(ACPI_PHYSADDR_TO_PTR(address
),
528 ACPI_CAST_PTR(char, value
), ACPI_DIV_8(bit_width
));
533 return_ACPI_STATUS(AE_BAD_PARAMETER
);
536 return_ACPI_STATUS(AE_OK
);