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 - 2023, 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 #ifdef ACPI_MISALIGNMENT_NOT_SUPPORTED
51 ACPI_FUNCTION_TRACE(ex_system_memory_space_handler
);
53 /* Validate and translate the bit width */
78 ACPI_ERROR((AE_INFO
, "Invalid SystemMemory width %u",
80 return_ACPI_STATUS(AE_AML_OPERAND_VALUE
);
83 #ifdef ACPI_MISALIGNMENT_NOT_SUPPORTED
85 * Hardware does not support non-aligned data transfers, we must verify
88 (void)acpi_ut_short_divide((u64
) address
, length
, NULL
, &remainder
);
90 return_ACPI_STATUS(AE_AML_ALIGNMENT
);
95 * Does the request fit into the cached memory mapping?
96 * Is 1) Address below the current mapping? OR
97 * 2) Address beyond the current mapping?
99 if (!mm
|| (address
< mm
->physical_address
) ||
100 ((u64
) address
+ length
> (u64
) mm
->physical_address
+ mm
->length
)) {
102 * The request cannot be resolved by the current memory mapping.
104 * Look for an existing saved mapping covering the address range
105 * at hand. If found, save it as the current one and carry out
108 for (mm
= mem_info
->first_mm
; mm
; mm
= mm
->next_mm
) {
109 if (mm
== mem_info
->cur_mm
)
112 if (address
< mm
->physical_address
)
115 if ((u64
) address
+ length
>
116 (u64
) mm
->physical_address
+ mm
->length
)
119 mem_info
->cur_mm
= mm
;
123 /* Create a new mappings list entry */
124 mm
= ACPI_ALLOCATE_ZEROED(sizeof(*mm
));
127 "Unable to save memory mapping at 0x%8.8X%8.8X, size %u",
128 ACPI_FORMAT_UINT64(address
), length
));
129 return_ACPI_STATUS(AE_NO_MEMORY
);
133 * October 2009: Attempt to map from the requested address to the
134 * end of the region. However, we will never map more than one
135 * page, nor will we cross a page boundary.
137 map_length
= (acpi_size
)
138 ((mem_info
->address
+ mem_info
->length
) - address
);
140 if (map_length
> ACPI_DEFAULT_PAGE_SIZE
)
141 map_length
= ACPI_DEFAULT_PAGE_SIZE
;
143 /* Create a new mapping starting at the address given */
145 logical_addr_ptr
= acpi_os_map_memory(address
, map_length
);
146 if (!logical_addr_ptr
) {
148 "Could not map memory at 0x%8.8X%8.8X, size %u",
149 ACPI_FORMAT_UINT64(address
),
152 return_ACPI_STATUS(AE_NO_MEMORY
);
155 /* Save the physical address and mapping size */
157 mm
->logical_address
= logical_addr_ptr
;
158 mm
->physical_address
= address
;
159 mm
->length
= map_length
;
162 * Add the new entry to the mappigs list and save it as the
165 mm
->next_mm
= mem_info
->first_mm
;
166 mem_info
->first_mm
= mm
;
168 mem_info
->cur_mm
= mm
;
173 * Generate a logical pointer corresponding to the address we want to
176 logical_addr_ptr
= mm
->logical_address
+
177 ((u64
) address
- (u64
) mm
->physical_address
);
179 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
180 "System-Memory (width %u) R/W %u Address=%8.8X%8.8X\n",
181 bit_width
, function
, ACPI_FORMAT_UINT64(address
)));
184 * Perform the memory read or write
186 * Note: For machines that do not support non-aligned transfers, the target
187 * address was checked for alignment above. We do not attempt to break the
188 * transfer up into smaller (byte-size) chunks because the AML specifically
189 * asked for a transfer width that the hardware may require.
198 *value
= (u64
)ACPI_GET8(logical_addr_ptr
);
203 *value
= (u64
)ACPI_GET16(logical_addr_ptr
);
208 *value
= (u64
)ACPI_GET32(logical_addr_ptr
);
213 *value
= (u64
)ACPI_GET64(logical_addr_ptr
);
218 /* bit_width was already validated */
229 ACPI_SET8(logical_addr_ptr
, *value
);
234 ACPI_SET16(logical_addr_ptr
, *value
);
239 ACPI_SET32(logical_addr_ptr
, *value
);
244 ACPI_SET64(logical_addr_ptr
, *value
);
249 /* bit_width was already validated */
257 status
= AE_BAD_PARAMETER
;
261 return_ACPI_STATUS(status
);
264 /*******************************************************************************
266 * FUNCTION: acpi_ex_system_io_space_handler
268 * PARAMETERS: function - Read or Write operation
269 * address - Where in the space to read or write
270 * bit_width - Field width in bits (8, 16, or 32)
271 * value - Pointer to in or out value
272 * handler_context - Pointer to Handler's context
273 * region_context - Pointer to context specific to the
278 * DESCRIPTION: Handler for the System IO address space (Op Region)
280 ******************************************************************************/
283 acpi_ex_system_io_space_handler(u32 function
,
284 acpi_physical_address address
,
287 void *handler_context
, void *region_context
)
289 acpi_status status
= AE_OK
;
292 ACPI_FUNCTION_TRACE(ex_system_io_space_handler
);
294 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
295 "System-IO (width %u) R/W %u Address=%8.8X%8.8X\n",
296 bit_width
, function
, ACPI_FORMAT_UINT64(address
)));
298 /* Decode the function parameter */
303 status
= acpi_hw_read_port((acpi_io_address
)address
,
304 &value32
, bit_width
);
310 status
= acpi_hw_write_port((acpi_io_address
)address
,
311 (u32
)*value
, bit_width
);
316 status
= AE_BAD_PARAMETER
;
320 return_ACPI_STATUS(status
);
323 #ifdef ACPI_PCI_CONFIGURED
324 /*******************************************************************************
326 * FUNCTION: acpi_ex_pci_config_space_handler
328 * PARAMETERS: function - Read or Write operation
329 * address - Where in the space to read or write
330 * bit_width - Field width in bits (8, 16, or 32)
331 * value - Pointer to in or out value
332 * handler_context - Pointer to Handler's context
333 * region_context - Pointer to context specific to the
338 * DESCRIPTION: Handler for the PCI Config address space (Op Region)
340 ******************************************************************************/
343 acpi_ex_pci_config_space_handler(u32 function
,
344 acpi_physical_address address
,
347 void *handler_context
, void *region_context
)
349 acpi_status status
= AE_OK
;
350 struct acpi_pci_id
*pci_id
;
353 ACPI_FUNCTION_TRACE(ex_pci_config_space_handler
);
356 * The arguments to acpi_os(Read|Write)pci_configuration are:
358 * pci_segment is the PCI bus segment range 0-31
359 * pci_bus is the PCI bus number range 0-255
360 * pci_device is the PCI device number range 0-31
361 * pci_function is the PCI device function number
362 * pci_register is the Config space register range 0-255 bytes
364 * value - input value for write, output address for read
367 pci_id
= (struct acpi_pci_id
*)region_context
;
368 pci_register
= (u16
) (u32
) address
;
370 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
371 "Pci-Config %u (%u) Seg(%04x) Bus(%04x) "
372 "Dev(%04x) Func(%04x) Reg(%04x)\n",
373 function
, bit_width
, pci_id
->segment
, pci_id
->bus
,
374 pci_id
->device
, pci_id
->function
, pci_register
));
381 acpi_os_read_pci_configuration(pci_id
, pci_register
, value
,
388 acpi_os_write_pci_configuration(pci_id
, pci_register
,
394 status
= AE_BAD_PARAMETER
;
398 return_ACPI_STATUS(status
);
402 /*******************************************************************************
404 * FUNCTION: acpi_ex_cmos_space_handler
406 * PARAMETERS: function - Read or Write operation
407 * address - Where in the space to read or write
408 * bit_width - Field width in bits (8, 16, or 32)
409 * value - Pointer to in or out value
410 * handler_context - Pointer to Handler's context
411 * region_context - Pointer to context specific to the
416 * DESCRIPTION: Handler for the CMOS address space (Op Region)
418 ******************************************************************************/
421 acpi_ex_cmos_space_handler(u32 function
,
422 acpi_physical_address address
,
425 void *handler_context
, void *region_context
)
427 acpi_status status
= AE_OK
;
429 ACPI_FUNCTION_TRACE(ex_cmos_space_handler
);
431 return_ACPI_STATUS(status
);
434 #ifdef ACPI_PCI_CONFIGURED
435 /*******************************************************************************
437 * FUNCTION: acpi_ex_pci_bar_space_handler
439 * PARAMETERS: function - Read or Write operation
440 * address - Where in the space to read or write
441 * bit_width - Field width in bits (8, 16, or 32)
442 * value - Pointer to in or out value
443 * handler_context - Pointer to Handler's context
444 * region_context - Pointer to context specific to the
449 * DESCRIPTION: Handler for the PCI bar_target address space (Op Region)
451 ******************************************************************************/
454 acpi_ex_pci_bar_space_handler(u32 function
,
455 acpi_physical_address address
,
458 void *handler_context
, void *region_context
)
460 acpi_status status
= AE_OK
;
462 ACPI_FUNCTION_TRACE(ex_pci_bar_space_handler
);
464 return_ACPI_STATUS(status
);
468 /*******************************************************************************
470 * FUNCTION: acpi_ex_data_table_space_handler
472 * PARAMETERS: function - Read or Write operation
473 * address - Where in the space to read or write
474 * bit_width - Field width in bits (8, 16, or 32)
475 * value - Pointer to in or out value
476 * handler_context - Pointer to Handler's context
477 * region_context - Pointer to context specific to the
482 * DESCRIPTION: Handler for the Data Table address space (Op Region)
484 ******************************************************************************/
487 acpi_ex_data_table_space_handler(u32 function
,
488 acpi_physical_address address
,
491 void *handler_context
, void *region_context
)
493 struct acpi_data_table_mapping
*mapping
;
496 ACPI_FUNCTION_TRACE(ex_data_table_space_handler
);
498 mapping
= (struct acpi_data_table_mapping
*) region_context
;
499 pointer
= ACPI_CAST_PTR(char, mapping
->pointer
) +
500 (address
- ACPI_PTR_TO_PHYSADDR(mapping
->pointer
));
503 * Perform the memory read or write. The bit_width was already
509 memcpy(ACPI_CAST_PTR(char, value
), pointer
,
510 ACPI_DIV_8(bit_width
));
515 memcpy(pointer
, ACPI_CAST_PTR(char, value
),
516 ACPI_DIV_8(bit_width
));
521 return_ACPI_STATUS(AE_BAD_PARAMETER
);
524 return_ACPI_STATUS(AE_OK
);