accel/qaic: Add AIC200 support
[drm/drm-misc.git] / drivers / acpi / acpica / exregion.c
blobc49b9f8de723d81853ccf969c4237183599b8946
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>
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 struct acpi_mem_mapping *mm = mem_info->cur_mm;
45 u32 length;
46 acpi_size 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 (!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
106 * the access.
108 for (mm = mem_info->first_mm; mm; mm = mm->next_mm) {
109 if (mm == mem_info->cur_mm)
110 continue;
112 if (address < mm->physical_address)
113 continue;
115 if ((u64) address + length >
116 (u64) mm->physical_address + mm->length)
117 continue;
119 mem_info->cur_mm = mm;
120 goto access;
123 /* Create a new mappings list entry */
124 mm = ACPI_ALLOCATE_ZEROED(sizeof(*mm));
125 if (!mm) {
126 ACPI_ERROR((AE_INFO,
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) {
147 ACPI_ERROR((AE_INFO,
148 "Could not map memory at 0x%8.8X%8.8X, size %u",
149 ACPI_FORMAT_UINT64(address),
150 (u32)map_length));
151 ACPI_FREE(mm);
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
163 * current mapping.
165 mm->next_mm = mem_info->first_mm;
166 mem_info->first_mm = mm;
168 mem_info->cur_mm = mm;
171 access:
173 * Generate a logical pointer corresponding to the address we want to
174 * access
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.
191 switch (function) {
192 case ACPI_READ:
194 *value = 0;
195 switch (bit_width) {
196 case 8:
198 *value = (u64)ACPI_GET8(logical_addr_ptr);
199 break;
201 case 16:
203 *value = (u64)ACPI_GET16(logical_addr_ptr);
204 break;
206 case 32:
208 *value = (u64)ACPI_GET32(logical_addr_ptr);
209 break;
211 case 64:
213 *value = (u64)ACPI_GET64(logical_addr_ptr);
214 break;
216 default:
218 /* bit_width was already validated */
220 break;
222 break;
224 case ACPI_WRITE:
226 switch (bit_width) {
227 case 8:
229 ACPI_SET8(logical_addr_ptr, *value);
230 break;
232 case 16:
234 ACPI_SET16(logical_addr_ptr, *value);
235 break;
237 case 32:
239 ACPI_SET32(logical_addr_ptr, *value);
240 break;
242 case 64:
244 ACPI_SET64(logical_addr_ptr, *value);
245 break;
247 default:
249 /* bit_width was already validated */
251 break;
253 break;
255 default:
257 status = AE_BAD_PARAMETER;
258 break;
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
274 * accessed region
276 * RETURN: Status
278 * DESCRIPTION: Handler for the System IO address space (Op Region)
280 ******************************************************************************/
282 acpi_status
283 acpi_ex_system_io_space_handler(u32 function,
284 acpi_physical_address address,
285 u32 bit_width,
286 u64 *value,
287 void *handler_context, void *region_context)
289 acpi_status status = AE_OK;
290 u32 value32;
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 */
300 switch (function) {
301 case ACPI_READ:
303 status = acpi_hw_read_port((acpi_io_address)address,
304 &value32, bit_width);
305 *value = value32;
306 break;
308 case ACPI_WRITE:
310 status = acpi_hw_write_port((acpi_io_address)address,
311 (u32)*value, bit_width);
312 break;
314 default:
316 status = AE_BAD_PARAMETER;
317 break;
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
334 * accessed region
336 * RETURN: Status
338 * DESCRIPTION: Handler for the PCI Config address space (Op Region)
340 ******************************************************************************/
342 acpi_status
343 acpi_ex_pci_config_space_handler(u32 function,
344 acpi_physical_address address,
345 u32 bit_width,
346 u64 *value,
347 void *handler_context, void *region_context)
349 acpi_status status = AE_OK;
350 struct acpi_pci_id *pci_id;
351 u16 pci_register;
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));
376 switch (function) {
377 case ACPI_READ:
379 *value = 0;
380 status =
381 acpi_os_read_pci_configuration(pci_id, pci_register, value,
382 bit_width);
383 break;
385 case ACPI_WRITE:
387 status =
388 acpi_os_write_pci_configuration(pci_id, pci_register,
389 *value, bit_width);
390 break;
392 default:
394 status = AE_BAD_PARAMETER;
395 break;
398 return_ACPI_STATUS(status);
400 #endif
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
412 * accessed region
414 * RETURN: Status
416 * DESCRIPTION: Handler for the CMOS address space (Op Region)
418 ******************************************************************************/
420 acpi_status
421 acpi_ex_cmos_space_handler(u32 function,
422 acpi_physical_address address,
423 u32 bit_width,
424 u64 *value,
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
445 * accessed region
447 * RETURN: Status
449 * DESCRIPTION: Handler for the PCI bar_target address space (Op Region)
451 ******************************************************************************/
453 acpi_status
454 acpi_ex_pci_bar_space_handler(u32 function,
455 acpi_physical_address address,
456 u32 bit_width,
457 u64 *value,
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);
466 #endif
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
478 * accessed region
480 * RETURN: Status
482 * DESCRIPTION: Handler for the Data Table address space (Op Region)
484 ******************************************************************************/
486 acpi_status
487 acpi_ex_data_table_space_handler(u32 function,
488 acpi_physical_address address,
489 u32 bit_width,
490 u64 *value,
491 void *handler_context, void *region_context)
493 struct acpi_data_table_mapping *mapping;
494 char *pointer;
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
504 * validated.
506 switch (function) {
507 case ACPI_READ:
509 memcpy(ACPI_CAST_PTR(char, value), pointer,
510 ACPI_DIV_8(bit_width));
511 break;
513 case ACPI_WRITE:
515 memcpy(pointer, ACPI_CAST_PTR(char, value),
516 ACPI_DIV_8(bit_width));
517 break;
519 default:
521 return_ACPI_STATUS(AE_BAD_PARAMETER);
524 return_ACPI_STATUS(AE_OK);