treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / acpi / acpica / exregion.c
blobd15a66de26c07232e4bf9a24d828e3b3e5eae9a5
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>
11 #include "accommon.h"
12 #include "acinterp.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
27 * accessed region
29 * RETURN: Status
31 * DESCRIPTION: Handler for the System Memory address space (Op Region)
33 ******************************************************************************/
34 acpi_status
35 acpi_ex_system_memory_space_handler(u32 function,
36 acpi_physical_address address,
37 u32 bit_width,
38 u64 *value,
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 u32 length;
45 acpi_size map_length;
46 acpi_size page_boundary_map_length;
47 #ifdef ACPI_MISALIGNMENT_NOT_SUPPORTED
48 u32 remainder;
49 #endif
51 ACPI_FUNCTION_TRACE(ex_system_memory_space_handler);
53 /* Validate and translate the bit width */
55 switch (bit_width) {
56 case 8:
58 length = 1;
59 break;
61 case 16:
63 length = 2;
64 break;
66 case 32:
68 length = 4;
69 break;
71 case 64:
73 length = 8;
74 break;
76 default:
78 ACPI_ERROR((AE_INFO, "Invalid SystemMemory width %u",
79 bit_width));
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
86 * the request.
88 (void)acpi_ut_short_divide((u64) address, length, NULL, &remainder);
89 if (remainder != 0) {
90 return_ACPI_STATUS(AE_AML_ALIGNMENT);
92 #endif
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 ((address < mem_info->mapped_physical_address) ||
100 (((u64) address + length) > ((u64)
101 mem_info->mapped_physical_address +
102 mem_info->mapped_length))) {
104 * The request cannot be resolved by the current memory mapping;
105 * Delete the existing mapping and create a new one.
107 if (mem_info->mapped_length) {
109 /* Valid mapping, delete it */
111 acpi_os_unmap_memory(mem_info->mapped_logical_address,
112 mem_info->mapped_length);
116 * October 2009: Attempt to map from the requested address to the
117 * end of the region. However, we will never map more than one
118 * page, nor will we cross a page boundary.
120 map_length = (acpi_size)
121 ((mem_info->address + mem_info->length) - address);
124 * If mapping the entire remaining portion of the region will cross
125 * a page boundary, just map up to the page boundary, do not cross.
126 * On some systems, crossing a page boundary while mapping regions
127 * can cause warnings if the pages have different attributes
128 * due to resource management.
130 * This has the added benefit of constraining a single mapping to
131 * one page, which is similar to the original code that used a 4k
132 * maximum window.
134 page_boundary_map_length = (acpi_size)
135 (ACPI_ROUND_UP(address, ACPI_DEFAULT_PAGE_SIZE) - address);
136 if (page_boundary_map_length == 0) {
137 page_boundary_map_length = ACPI_DEFAULT_PAGE_SIZE;
140 if (map_length > page_boundary_map_length) {
141 map_length = page_boundary_map_length;
144 /* Create a new mapping starting at the address given */
146 mem_info->mapped_logical_address =
147 acpi_os_map_memory(address, map_length);
148 if (!mem_info->mapped_logical_address) {
149 ACPI_ERROR((AE_INFO,
150 "Could not map memory at 0x%8.8X%8.8X, size %u",
151 ACPI_FORMAT_UINT64(address),
152 (u32)map_length));
153 mem_info->mapped_length = 0;
154 return_ACPI_STATUS(AE_NO_MEMORY);
157 /* Save the physical address and mapping size */
159 mem_info->mapped_physical_address = address;
160 mem_info->mapped_length = map_length;
164 * Generate a logical pointer corresponding to the address we want to
165 * access
167 logical_addr_ptr = mem_info->mapped_logical_address +
168 ((u64) address - (u64) mem_info->mapped_physical_address);
170 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
171 "System-Memory (width %u) R/W %u Address=%8.8X%8.8X\n",
172 bit_width, function, ACPI_FORMAT_UINT64(address)));
175 * Perform the memory read or write
177 * Note: For machines that do not support non-aligned transfers, the target
178 * address was checked for alignment above. We do not attempt to break the
179 * transfer up into smaller (byte-size) chunks because the AML specifically
180 * asked for a transfer width that the hardware may require.
182 switch (function) {
183 case ACPI_READ:
185 *value = 0;
186 switch (bit_width) {
187 case 8:
189 *value = (u64)ACPI_GET8(logical_addr_ptr);
190 break;
192 case 16:
194 *value = (u64)ACPI_GET16(logical_addr_ptr);
195 break;
197 case 32:
199 *value = (u64)ACPI_GET32(logical_addr_ptr);
200 break;
202 case 64:
204 *value = (u64)ACPI_GET64(logical_addr_ptr);
205 break;
207 default:
209 /* bit_width was already validated */
211 break;
213 break;
215 case ACPI_WRITE:
217 switch (bit_width) {
218 case 8:
220 ACPI_SET8(logical_addr_ptr, *value);
221 break;
223 case 16:
225 ACPI_SET16(logical_addr_ptr, *value);
226 break;
228 case 32:
230 ACPI_SET32(logical_addr_ptr, *value);
231 break;
233 case 64:
235 ACPI_SET64(logical_addr_ptr, *value);
236 break;
238 default:
240 /* bit_width was already validated */
242 break;
244 break;
246 default:
248 status = AE_BAD_PARAMETER;
249 break;
252 return_ACPI_STATUS(status);
255 /*******************************************************************************
257 * FUNCTION: acpi_ex_system_io_space_handler
259 * PARAMETERS: function - Read or Write operation
260 * address - Where in the space to read or write
261 * bit_width - Field width in bits (8, 16, or 32)
262 * value - Pointer to in or out value
263 * handler_context - Pointer to Handler's context
264 * region_context - Pointer to context specific to the
265 * accessed region
267 * RETURN: Status
269 * DESCRIPTION: Handler for the System IO address space (Op Region)
271 ******************************************************************************/
273 acpi_status
274 acpi_ex_system_io_space_handler(u32 function,
275 acpi_physical_address address,
276 u32 bit_width,
277 u64 *value,
278 void *handler_context, void *region_context)
280 acpi_status status = AE_OK;
281 u32 value32;
283 ACPI_FUNCTION_TRACE(ex_system_io_space_handler);
285 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
286 "System-IO (width %u) R/W %u Address=%8.8X%8.8X\n",
287 bit_width, function, ACPI_FORMAT_UINT64(address)));
289 /* Decode the function parameter */
291 switch (function) {
292 case ACPI_READ:
294 status = acpi_hw_read_port((acpi_io_address)address,
295 &value32, bit_width);
296 *value = value32;
297 break;
299 case ACPI_WRITE:
301 status = acpi_hw_write_port((acpi_io_address)address,
302 (u32)*value, bit_width);
303 break;
305 default:
307 status = AE_BAD_PARAMETER;
308 break;
311 return_ACPI_STATUS(status);
314 #ifdef ACPI_PCI_CONFIGURED
315 /*******************************************************************************
317 * FUNCTION: acpi_ex_pci_config_space_handler
319 * PARAMETERS: function - Read or Write operation
320 * address - Where in the space to read or write
321 * bit_width - Field width in bits (8, 16, or 32)
322 * value - Pointer to in or out value
323 * handler_context - Pointer to Handler's context
324 * region_context - Pointer to context specific to the
325 * accessed region
327 * RETURN: Status
329 * DESCRIPTION: Handler for the PCI Config address space (Op Region)
331 ******************************************************************************/
333 acpi_status
334 acpi_ex_pci_config_space_handler(u32 function,
335 acpi_physical_address address,
336 u32 bit_width,
337 u64 *value,
338 void *handler_context, void *region_context)
340 acpi_status status = AE_OK;
341 struct acpi_pci_id *pci_id;
342 u16 pci_register;
344 ACPI_FUNCTION_TRACE(ex_pci_config_space_handler);
347 * The arguments to acpi_os(Read|Write)pci_configuration are:
349 * pci_segment is the PCI bus segment range 0-31
350 * pci_bus is the PCI bus number range 0-255
351 * pci_device is the PCI device number range 0-31
352 * pci_function is the PCI device function number
353 * pci_register is the Config space register range 0-255 bytes
355 * value - input value for write, output address for read
358 pci_id = (struct acpi_pci_id *)region_context;
359 pci_register = (u16) (u32) address;
361 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
362 "Pci-Config %u (%u) Seg(%04x) Bus(%04x) "
363 "Dev(%04x) Func(%04x) Reg(%04x)\n",
364 function, bit_width, pci_id->segment, pci_id->bus,
365 pci_id->device, pci_id->function, pci_register));
367 switch (function) {
368 case ACPI_READ:
370 *value = 0;
371 status =
372 acpi_os_read_pci_configuration(pci_id, pci_register, value,
373 bit_width);
374 break;
376 case ACPI_WRITE:
378 status =
379 acpi_os_write_pci_configuration(pci_id, pci_register,
380 *value, bit_width);
381 break;
383 default:
385 status = AE_BAD_PARAMETER;
386 break;
389 return_ACPI_STATUS(status);
391 #endif
393 /*******************************************************************************
395 * FUNCTION: acpi_ex_cmos_space_handler
397 * PARAMETERS: function - Read or Write operation
398 * address - Where in the space to read or write
399 * bit_width - Field width in bits (8, 16, or 32)
400 * value - Pointer to in or out value
401 * handler_context - Pointer to Handler's context
402 * region_context - Pointer to context specific to the
403 * accessed region
405 * RETURN: Status
407 * DESCRIPTION: Handler for the CMOS address space (Op Region)
409 ******************************************************************************/
411 acpi_status
412 acpi_ex_cmos_space_handler(u32 function,
413 acpi_physical_address address,
414 u32 bit_width,
415 u64 *value,
416 void *handler_context, void *region_context)
418 acpi_status status = AE_OK;
420 ACPI_FUNCTION_TRACE(ex_cmos_space_handler);
422 return_ACPI_STATUS(status);
425 #ifdef ACPI_PCI_CONFIGURED
426 /*******************************************************************************
428 * FUNCTION: acpi_ex_pci_bar_space_handler
430 * PARAMETERS: function - Read or Write operation
431 * address - Where in the space to read or write
432 * bit_width - Field width in bits (8, 16, or 32)
433 * value - Pointer to in or out value
434 * handler_context - Pointer to Handler's context
435 * region_context - Pointer to context specific to the
436 * accessed region
438 * RETURN: Status
440 * DESCRIPTION: Handler for the PCI bar_target address space (Op Region)
442 ******************************************************************************/
444 acpi_status
445 acpi_ex_pci_bar_space_handler(u32 function,
446 acpi_physical_address address,
447 u32 bit_width,
448 u64 *value,
449 void *handler_context, void *region_context)
451 acpi_status status = AE_OK;
453 ACPI_FUNCTION_TRACE(ex_pci_bar_space_handler);
455 return_ACPI_STATUS(status);
457 #endif
459 /*******************************************************************************
461 * FUNCTION: acpi_ex_data_table_space_handler
463 * PARAMETERS: function - Read or Write operation
464 * address - Where in the space to read or write
465 * bit_width - Field width in bits (8, 16, or 32)
466 * value - Pointer to in or out value
467 * handler_context - Pointer to Handler's context
468 * region_context - Pointer to context specific to the
469 * accessed region
471 * RETURN: Status
473 * DESCRIPTION: Handler for the Data Table address space (Op Region)
475 ******************************************************************************/
477 acpi_status
478 acpi_ex_data_table_space_handler(u32 function,
479 acpi_physical_address address,
480 u32 bit_width,
481 u64 *value,
482 void *handler_context, void *region_context)
484 ACPI_FUNCTION_TRACE(ex_data_table_space_handler);
487 * Perform the memory read or write. The bit_width was already
488 * validated.
490 switch (function) {
491 case ACPI_READ:
493 memcpy(ACPI_CAST_PTR(char, value),
494 ACPI_PHYSADDR_TO_PTR(address), ACPI_DIV_8(bit_width));
495 break;
497 case ACPI_WRITE:
499 memcpy(ACPI_PHYSADDR_TO_PTR(address),
500 ACPI_CAST_PTR(char, value), ACPI_DIV_8(bit_width));
501 break;
503 default:
505 return_ACPI_STATUS(AE_BAD_PARAMETER);
508 return_ACPI_STATUS(AE_OK);