]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/acpi/acpica/exregion.c
net: ethernet: ixp4xx: Set the DMA masks explicitly
[mirror_ubuntu-hirsute-kernel.git] / drivers / acpi / acpica / exregion.c
CommitLineData
95857638 1// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
1da177e4
LT
2/******************************************************************************
3 *
4 * Module Name: exregion - ACPI default op_region (address space) handlers
5 *
800ba7c5 6 * Copyright (C) 2000 - 2020, Intel Corp.
1da177e4 7 *
95857638 8 *****************************************************************************/
1da177e4 9
1da177e4 10#include <acpi/acpi.h>
e2f7a777
LB
11#include "accommon.h"
12#include "acinterp.h"
1da177e4 13
1da177e4 14#define _COMPONENT ACPI_EXECUTER
4be44fcd 15ACPI_MODULE_NAME("exregion")
1da177e4
LT
16
17/*******************************************************************************
18 *
19 * FUNCTION: acpi_ex_system_memory_space_handler
20 *
ba494bee
BM
21 * PARAMETERS: function - Read or Write operation
22 * address - Where in the space to read or write
1da177e4 23 * bit_width - Field width in bits (8, 16, or 32)
ba494bee 24 * value - Pointer to in or out value
1da177e4
LT
25 * handler_context - Pointer to Handler's context
26 * region_context - Pointer to context specific to the
27 * accessed region
28 *
29 * RETURN: Status
30 *
31 * DESCRIPTION: Handler for the System Memory address space (Op Region)
32 *
33 ******************************************************************************/
1da177e4 34acpi_status
4be44fcd
LB
35acpi_ex_system_memory_space_handler(u32 function,
36 acpi_physical_address address,
37 u32 bit_width,
5df7e6cb 38 u64 *value,
4be44fcd 39 void *handler_context, void *region_context)
1da177e4 40{
4be44fcd
LB
41 acpi_status status = AE_OK;
42 void *logical_addr_ptr = NULL;
43 struct acpi_mem_space_context *mem_info = region_context;
b8fcd0e5 44 struct acpi_mem_mapping *mm = mem_info->cur_mm;
4be44fcd 45 u32 length;
d410ee51
BM
46 acpi_size map_length;
47 acpi_size page_boundary_map_length;
0897831b 48#ifdef ACPI_MISALIGNMENT_NOT_SUPPORTED
4be44fcd 49 u32 remainder;
1da177e4
LT
50#endif
51
b229cf92 52 ACPI_FUNCTION_TRACE(ex_system_memory_space_handler);
1da177e4
LT
53
54 /* Validate and translate the bit width */
55
56 switch (bit_width) {
57 case 8:
1d1ea1b7 58
1da177e4
LT
59 length = 1;
60 break;
61
62 case 16:
1d1ea1b7 63
1da177e4
LT
64 length = 2;
65 break;
66
67 case 32:
1d1ea1b7 68
1da177e4
LT
69 length = 4;
70 break;
71
72 case 64:
1d1ea1b7 73
1da177e4
LT
74 length = 8;
75 break;
76
77 default:
1d1ea1b7 78
f6a22b0b 79 ACPI_ERROR((AE_INFO, "Invalid SystemMemory width %u",
b8e4d893 80 bit_width));
4be44fcd 81 return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
1da177e4
LT
82 }
83
0897831b 84#ifdef ACPI_MISALIGNMENT_NOT_SUPPORTED
1da177e4
LT
85 /*
86 * Hardware does not support non-aligned data transfers, we must verify
87 * the request.
88 */
5df7e6cb 89 (void)acpi_ut_short_divide((u64) address, length, NULL, &remainder);
1da177e4 90 if (remainder != 0) {
4be44fcd 91 return_ACPI_STATUS(AE_AML_ALIGNMENT);
1da177e4
LT
92 }
93#endif
94
95 /*
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?
99 */
b8fcd0e5
RW
100 if (!mm || (address < mm->physical_address) ||
101 ((u64) address + length > (u64) mm->physical_address + mm->length)) {
1da177e4 102 /*
b8fcd0e5
RW
103 * The request cannot be resolved by the current memory mapping.
104 *
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
107 * the access.
1da177e4 108 */
b8fcd0e5
RW
109 for (mm = mem_info->first_mm; mm; mm = mm->next_mm) {
110 if (mm == mem_info->cur_mm)
111 continue;
112
113 if (address < mm->physical_address)
114 continue;
52fc0b02 115
b8fcd0e5
RW
116 if ((u64) address + length >
117 (u64) mm->physical_address + mm->length)
118 continue;
1da177e4 119
b8fcd0e5
RW
120 mem_info->cur_mm = mm;
121 goto access;
122 }
123
124 /* Create a new mappings list entry */
125 mm = ACPI_ALLOCATE_ZEROED(sizeof(*mm));
126 if (!mm) {
127 ACPI_ERROR((AE_INFO,
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);
1da177e4
LT
131 }
132
133 /*
75c8044f
LZ
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.
1da177e4 137 */
d410ee51 138 map_length = (acpi_size)
4be44fcd 139 ((mem_info->address + mem_info->length) - address);
44f6c012 140
d410ee51
BM
141 /*
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
75c8044f
LZ
146 * due to resource management.
147 *
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
150 * maximum window.
d410ee51 151 */
938ed102
BM
152 page_boundary_map_length = (acpi_size)
153 (ACPI_ROUND_UP(address, ACPI_DEFAULT_PAGE_SIZE) - address);
739dcbb9 154 if (page_boundary_map_length == 0) {
d410ee51
BM
155 page_boundary_map_length = ACPI_DEFAULT_PAGE_SIZE;
156 }
157
158 if (map_length > page_boundary_map_length) {
159 map_length = page_boundary_map_length;
1da177e4
LT
160 }
161
162 /* Create a new mapping starting at the address given */
163
b8fcd0e5
RW
164 logical_addr_ptr = acpi_os_map_memory(address, map_length);
165 if (!logical_addr_ptr) {
b8e4d893 166 ACPI_ERROR((AE_INFO,
f6a22b0b 167 "Could not map memory at 0x%8.8X%8.8X, size %u",
1d0a0b2f 168 ACPI_FORMAT_UINT64(address),
18ae9021 169 (u32)map_length));
b8fcd0e5 170 ACPI_FREE(mm);
f3d2e786 171 return_ACPI_STATUS(AE_NO_MEMORY);
1da177e4
LT
172 }
173
174 /* Save the physical address and mapping size */
175
b8fcd0e5
RW
176 mm->logical_address = logical_addr_ptr;
177 mm->physical_address = address;
178 mm->length = map_length;
179
180 /*
181 * Add the new entry to the mappigs list and save it as the
182 * current mapping.
183 */
184 mm->next_mm = mem_info->first_mm;
185 mem_info->first_mm = mm;
186
187 mem_info->cur_mm = mm;
1da177e4
LT
188 }
189
b8fcd0e5 190access:
1da177e4
LT
191 /*
192 * Generate a logical pointer corresponding to the address we want to
193 * access
194 */
b8fcd0e5
RW
195 logical_addr_ptr = mm->logical_address +
196 ((u64) address - (u64) mm->physical_address);
4be44fcd
LB
197
198 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
b27d6597 199 "System-Memory (width %u) R/W %u Address=%8.8X%8.8X\n",
1d0a0b2f 200 bit_width, function, ACPI_FORMAT_UINT64(address)));
4be44fcd
LB
201
202 /*
203 * Perform the memory read or write
204 *
205 * Note: For machines that do not support non-aligned transfers, the target
73a3090a 206 * address was checked for alignment above. We do not attempt to break the
4be44fcd
LB
207 * transfer up into smaller (byte-size) chunks because the AML specifically
208 * asked for a transfer width that the hardware may require.
209 */
1da177e4
LT
210 switch (function) {
211 case ACPI_READ:
212
213 *value = 0;
214 switch (bit_width) {
215 case 8:
1d1ea1b7
CG
216
217 *value = (u64)ACPI_GET8(logical_addr_ptr);
1da177e4
LT
218 break;
219
220 case 16:
1d1ea1b7
CG
221
222 *value = (u64)ACPI_GET16(logical_addr_ptr);
1da177e4
LT
223 break;
224
225 case 32:
1d1ea1b7
CG
226
227 *value = (u64)ACPI_GET32(logical_addr_ptr);
1da177e4
LT
228 break;
229
1da177e4 230 case 64:
1d1ea1b7
CG
231
232 *value = (u64)ACPI_GET64(logical_addr_ptr);
1da177e4 233 break;
59fa8505 234
1da177e4 235 default:
1d1ea1b7 236
1da177e4 237 /* bit_width was already validated */
1d1ea1b7 238
1da177e4
LT
239 break;
240 }
241 break;
242
243 case ACPI_WRITE:
244
245 switch (bit_width) {
246 case 8:
1d1ea1b7 247
57bf6aef 248 ACPI_SET8(logical_addr_ptr, *value);
1da177e4
LT
249 break;
250
251 case 16:
1d1ea1b7 252
57bf6aef 253 ACPI_SET16(logical_addr_ptr, *value);
1da177e4
LT
254 break;
255
256 case 32:
1d1ea1b7 257
57bf6aef 258 ACPI_SET32(logical_addr_ptr, *value);
1da177e4
LT
259 break;
260
1da177e4 261 case 64:
1d1ea1b7 262
57bf6aef 263 ACPI_SET64(logical_addr_ptr, *value);
1da177e4 264 break;
1da177e4
LT
265
266 default:
1d1ea1b7 267
1da177e4 268 /* bit_width was already validated */
1d1ea1b7 269
1da177e4
LT
270 break;
271 }
272 break;
273
274 default:
1d1ea1b7 275
1da177e4
LT
276 status = AE_BAD_PARAMETER;
277 break;
278 }
279
4be44fcd 280 return_ACPI_STATUS(status);
1da177e4
LT
281}
282
1da177e4
LT
283/*******************************************************************************
284 *
285 * FUNCTION: acpi_ex_system_io_space_handler
286 *
ba494bee
BM
287 * PARAMETERS: function - Read or Write operation
288 * address - Where in the space to read or write
1da177e4 289 * bit_width - Field width in bits (8, 16, or 32)
ba494bee 290 * value - Pointer to in or out value
1da177e4
LT
291 * handler_context - Pointer to Handler's context
292 * region_context - Pointer to context specific to the
293 * accessed region
294 *
295 * RETURN: Status
296 *
297 * DESCRIPTION: Handler for the System IO address space (Op Region)
298 *
299 ******************************************************************************/
300
301acpi_status
4be44fcd
LB
302acpi_ex_system_io_space_handler(u32 function,
303 acpi_physical_address address,
304 u32 bit_width,
5df7e6cb 305 u64 *value,
4be44fcd 306 void *handler_context, void *region_context)
1da177e4 307{
4be44fcd
LB
308 acpi_status status = AE_OK;
309 u32 value32;
1da177e4 310
b229cf92 311 ACPI_FUNCTION_TRACE(ex_system_io_space_handler);
1da177e4 312
4be44fcd 313 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
b27d6597 314 "System-IO (width %u) R/W %u Address=%8.8X%8.8X\n",
1d0a0b2f 315 bit_width, function, ACPI_FORMAT_UINT64(address)));
1da177e4
LT
316
317 /* Decode the function parameter */
318
319 switch (function) {
320 case ACPI_READ:
321
f5c1e1c5 322 status = acpi_hw_read_port((acpi_io_address)address,
4be44fcd 323 &value32, bit_width);
1da177e4
LT
324 *value = value32;
325 break;
326
327 case ACPI_WRITE:
328
f5c1e1c5
LZ
329 status = acpi_hw_write_port((acpi_io_address)address,
330 (u32)*value, bit_width);
1da177e4
LT
331 break;
332
333 default:
1d1ea1b7 334
1da177e4
LT
335 status = AE_BAD_PARAMETER;
336 break;
337 }
338
4be44fcd 339 return_ACPI_STATUS(status);
1da177e4
LT
340}
341
bd23fac3 342#ifdef ACPI_PCI_CONFIGURED
1da177e4
LT
343/*******************************************************************************
344 *
345 * FUNCTION: acpi_ex_pci_config_space_handler
346 *
ba494bee
BM
347 * PARAMETERS: function - Read or Write operation
348 * address - Where in the space to read or write
1da177e4 349 * bit_width - Field width in bits (8, 16, or 32)
ba494bee 350 * value - Pointer to in or out value
1da177e4
LT
351 * handler_context - Pointer to Handler's context
352 * region_context - Pointer to context specific to the
353 * accessed region
354 *
355 * RETURN: Status
356 *
357 * DESCRIPTION: Handler for the PCI Config address space (Op Region)
358 *
359 ******************************************************************************/
360
361acpi_status
4be44fcd
LB
362acpi_ex_pci_config_space_handler(u32 function,
363 acpi_physical_address address,
364 u32 bit_width,
5df7e6cb 365 u64 *value,
4be44fcd 366 void *handler_context, void *region_context)
1da177e4 367{
4be44fcd
LB
368 acpi_status status = AE_OK;
369 struct acpi_pci_id *pci_id;
370 u16 pci_register;
1da177e4 371
b229cf92 372 ACPI_FUNCTION_TRACE(ex_pci_config_space_handler);
1da177e4
LT
373
374 /*
375 * The arguments to acpi_os(Read|Write)pci_configuration are:
376 *
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
382 *
ba494bee 383 * value - input value for write, output address for read
1da177e4
LT
384 *
385 */
4be44fcd 386 pci_id = (struct acpi_pci_id *)region_context;
1da177e4
LT
387 pci_register = (u16) (u32) address;
388
4be44fcd 389 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1fad8738
BM
390 "Pci-Config %u (%u) Seg(%04x) Bus(%04x) "
391 "Dev(%04x) Func(%04x) Reg(%04x)\n",
4be44fcd
LB
392 function, bit_width, pci_id->segment, pci_id->bus,
393 pci_id->device, pci_id->function, pci_register));
1da177e4
LT
394
395 switch (function) {
396 case ACPI_READ:
397
bb42cc22 398 *value = 0;
1fad8738
BM
399 status =
400 acpi_os_read_pci_configuration(pci_id, pci_register, value,
401 bit_width);
1da177e4
LT
402 break;
403
404 case ACPI_WRITE:
405
1fad8738
BM
406 status =
407 acpi_os_write_pci_configuration(pci_id, pci_register,
408 *value, bit_width);
1da177e4
LT
409 break;
410
411 default:
412
413 status = AE_BAD_PARAMETER;
414 break;
415 }
416
4be44fcd 417 return_ACPI_STATUS(status);
1da177e4 418}
bd23fac3 419#endif
1da177e4 420
1da177e4
LT
421/*******************************************************************************
422 *
423 * FUNCTION: acpi_ex_cmos_space_handler
424 *
ba494bee
BM
425 * PARAMETERS: function - Read or Write operation
426 * address - Where in the space to read or write
1da177e4 427 * bit_width - Field width in bits (8, 16, or 32)
ba494bee 428 * value - Pointer to in or out value
1da177e4
LT
429 * handler_context - Pointer to Handler's context
430 * region_context - Pointer to context specific to the
431 * accessed region
432 *
433 * RETURN: Status
434 *
435 * DESCRIPTION: Handler for the CMOS address space (Op Region)
436 *
437 ******************************************************************************/
438
439acpi_status
4be44fcd
LB
440acpi_ex_cmos_space_handler(u32 function,
441 acpi_physical_address address,
442 u32 bit_width,
5df7e6cb 443 u64 *value,
4be44fcd 444 void *handler_context, void *region_context)
1da177e4 445{
4be44fcd 446 acpi_status status = AE_OK;
1da177e4 447
b229cf92 448 ACPI_FUNCTION_TRACE(ex_cmos_space_handler);
1da177e4 449
4be44fcd 450 return_ACPI_STATUS(status);
1da177e4
LT
451}
452
bd23fac3 453#ifdef ACPI_PCI_CONFIGURED
1da177e4
LT
454/*******************************************************************************
455 *
456 * FUNCTION: acpi_ex_pci_bar_space_handler
457 *
ba494bee
BM
458 * PARAMETERS: function - Read or Write operation
459 * address - Where in the space to read or write
1da177e4 460 * bit_width - Field width in bits (8, 16, or 32)
ba494bee 461 * value - Pointer to in or out value
1da177e4
LT
462 * handler_context - Pointer to Handler's context
463 * region_context - Pointer to context specific to the
464 * accessed region
465 *
466 * RETURN: Status
467 *
468 * DESCRIPTION: Handler for the PCI bar_target address space (Op Region)
469 *
470 ******************************************************************************/
471
472acpi_status
4be44fcd
LB
473acpi_ex_pci_bar_space_handler(u32 function,
474 acpi_physical_address address,
475 u32 bit_width,
5df7e6cb 476 u64 *value,
4be44fcd 477 void *handler_context, void *region_context)
1da177e4 478{
4be44fcd 479 acpi_status status = AE_OK;
1da177e4 480
b229cf92 481 ACPI_FUNCTION_TRACE(ex_pci_bar_space_handler);
1da177e4 482
4be44fcd 483 return_ACPI_STATUS(status);
1da177e4 484}
bd23fac3 485#endif
1da177e4 486
1da177e4
LT
487/*******************************************************************************
488 *
489 * FUNCTION: acpi_ex_data_table_space_handler
490 *
ba494bee
BM
491 * PARAMETERS: function - Read or Write operation
492 * address - Where in the space to read or write
1da177e4 493 * bit_width - Field width in bits (8, 16, or 32)
ba494bee 494 * value - Pointer to in or out value
1da177e4
LT
495 * handler_context - Pointer to Handler's context
496 * region_context - Pointer to context specific to the
497 * accessed region
498 *
499 * RETURN: Status
500 *
501 * DESCRIPTION: Handler for the Data Table address space (Op Region)
502 *
503 ******************************************************************************/
504
505acpi_status
4be44fcd
LB
506acpi_ex_data_table_space_handler(u32 function,
507 acpi_physical_address address,
508 u32 bit_width,
5df7e6cb 509 u64 *value,
4be44fcd 510 void *handler_context, void *region_context)
1da177e4 511{
b229cf92 512 ACPI_FUNCTION_TRACE(ex_data_table_space_handler);
1da177e4 513
c1637e9c
BM
514 /*
515 * Perform the memory read or write. The bit_width was already
516 * validated.
517 */
1da177e4
LT
518 switch (function) {
519 case ACPI_READ:
520
4fa4616e
BM
521 memcpy(ACPI_CAST_PTR(char, value),
522 ACPI_PHYSADDR_TO_PTR(address), ACPI_DIV_8(bit_width));
1da177e4
LT
523 break;
524
525 case ACPI_WRITE:
c1637e9c 526
4fa4616e
BM
527 memcpy(ACPI_PHYSADDR_TO_PTR(address),
528 ACPI_CAST_PTR(char, value), ACPI_DIV_8(bit_width));
c1637e9c
BM
529 break;
530
1da177e4
LT
531 default:
532
c1637e9c 533 return_ACPI_STATUS(AE_BAD_PARAMETER);
1da177e4
LT
534 }
535
4119532c 536 return_ACPI_STATUS(AE_OK);
1da177e4 537}