team: fix memory leaks
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / acpi / acpica / exregion.c
CommitLineData
1da177e4
LT
1/******************************************************************************
2 *
3 * Module Name: exregion - ACPI default op_region (address space) handlers
4 *
5 *****************************************************************************/
6
7/*
25f044e6 8 * Copyright (C) 2000 - 2013, Intel Corp.
1da177e4
LT
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
29 *
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
42 */
43
1da177e4 44#include <acpi/acpi.h>
e2f7a777
LB
45#include "accommon.h"
46#include "acinterp.h"
1da177e4 47
1da177e4 48#define _COMPONENT ACPI_EXECUTER
4be44fcd 49ACPI_MODULE_NAME("exregion")
1da177e4
LT
50
51/*******************************************************************************
52 *
53 * FUNCTION: acpi_ex_system_memory_space_handler
54 *
ba494bee
BM
55 * PARAMETERS: function - Read or Write operation
56 * address - Where in the space to read or write
1da177e4 57 * bit_width - Field width in bits (8, 16, or 32)
ba494bee 58 * value - Pointer to in or out value
1da177e4
LT
59 * handler_context - Pointer to Handler's context
60 * region_context - Pointer to context specific to the
61 * accessed region
62 *
63 * RETURN: Status
64 *
65 * DESCRIPTION: Handler for the System Memory address space (Op Region)
66 *
67 ******************************************************************************/
1da177e4 68acpi_status
4be44fcd
LB
69acpi_ex_system_memory_space_handler(u32 function,
70 acpi_physical_address address,
71 u32 bit_width,
5df7e6cb 72 u64 *value,
4be44fcd 73 void *handler_context, void *region_context)
1da177e4 74{
4be44fcd
LB
75 acpi_status status = AE_OK;
76 void *logical_addr_ptr = NULL;
77 struct acpi_mem_space_context *mem_info = region_context;
78 u32 length;
d410ee51
BM
79 acpi_size map_length;
80 acpi_size page_boundary_map_length;
0897831b 81#ifdef ACPI_MISALIGNMENT_NOT_SUPPORTED
4be44fcd 82 u32 remainder;
1da177e4
LT
83#endif
84
b229cf92 85 ACPI_FUNCTION_TRACE(ex_system_memory_space_handler);
1da177e4
LT
86
87 /* Validate and translate the bit width */
88
89 switch (bit_width) {
90 case 8:
91 length = 1;
92 break;
93
94 case 16:
95 length = 2;
96 break;
97
98 case 32:
99 length = 4;
100 break;
101
102 case 64:
103 length = 8;
104 break;
105
106 default:
f6a22b0b 107 ACPI_ERROR((AE_INFO, "Invalid SystemMemory width %u",
b8e4d893 108 bit_width));
4be44fcd 109 return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
1da177e4
LT
110 }
111
0897831b 112#ifdef ACPI_MISALIGNMENT_NOT_SUPPORTED
1da177e4
LT
113 /*
114 * Hardware does not support non-aligned data transfers, we must verify
115 * the request.
116 */
5df7e6cb 117 (void)acpi_ut_short_divide((u64) address, length, NULL, &remainder);
1da177e4 118 if (remainder != 0) {
4be44fcd 119 return_ACPI_STATUS(AE_AML_ALIGNMENT);
1da177e4
LT
120 }
121#endif
122
123 /*
124 * Does the request fit into the cached memory mapping?
125 * Is 1) Address below the current mapping? OR
126 * 2) Address beyond the current mapping?
127 */
128 if ((address < mem_info->mapped_physical_address) ||
5df7e6cb
BM
129 (((u64) address + length) > ((u64)
130 mem_info->mapped_physical_address +
131 mem_info->mapped_length))) {
1da177e4
LT
132 /*
133 * The request cannot be resolved by the current memory mapping;
134 * Delete the existing mapping and create a new one.
135 */
136 if (mem_info->mapped_length) {
52fc0b02 137
1da177e4
LT
138 /* Valid mapping, delete it */
139
4be44fcd
LB
140 acpi_os_unmap_memory(mem_info->mapped_logical_address,
141 mem_info->mapped_length);
1da177e4
LT
142 }
143
144 /*
75c8044f
LZ
145 * October 2009: Attempt to map from the requested address to the
146 * end of the region. However, we will never map more than one
147 * page, nor will we cross a page boundary.
1da177e4 148 */
d410ee51 149 map_length = (acpi_size)
4be44fcd 150 ((mem_info->address + mem_info->length) - address);
44f6c012 151
d410ee51
BM
152 /*
153 * If mapping the entire remaining portion of the region will cross
154 * a page boundary, just map up to the page boundary, do not cross.
155 * On some systems, crossing a page boundary while mapping regions
156 * can cause warnings if the pages have different attributes
75c8044f
LZ
157 * due to resource management.
158 *
159 * This has the added benefit of constraining a single mapping to
160 * one page, which is similar to the original code that used a 4k
161 * maximum window.
d410ee51
BM
162 */
163 page_boundary_map_length =
164 ACPI_ROUND_UP(address, ACPI_DEFAULT_PAGE_SIZE) - address;
739dcbb9 165 if (page_boundary_map_length == 0) {
d410ee51
BM
166 page_boundary_map_length = ACPI_DEFAULT_PAGE_SIZE;
167 }
168
169 if (map_length > page_boundary_map_length) {
170 map_length = page_boundary_map_length;
1da177e4
LT
171 }
172
173 /* Create a new mapping starting at the address given */
174
d410ee51 175 mem_info->mapped_logical_address = acpi_os_map_memory((acpi_physical_address) address, map_length);
f3d2e786 176 if (!mem_info->mapped_logical_address) {
b8e4d893 177 ACPI_ERROR((AE_INFO,
f6a22b0b 178 "Could not map memory at 0x%8.8X%8.8X, size %u",
a2dac7a1 179 ACPI_FORMAT_UINT64(address),
d410ee51 180 (u32) map_length));
1da177e4 181 mem_info->mapped_length = 0;
f3d2e786 182 return_ACPI_STATUS(AE_NO_MEMORY);
1da177e4
LT
183 }
184
185 /* Save the physical address and mapping size */
186
187 mem_info->mapped_physical_address = address;
d410ee51 188 mem_info->mapped_length = map_length;
1da177e4
LT
189 }
190
191 /*
192 * Generate a logical pointer corresponding to the address we want to
193 * access
194 */
195 logical_addr_ptr = mem_info->mapped_logical_address +
5df7e6cb 196 ((u64) address - (u64) mem_info->mapped_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",
a2dac7a1 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:
5df7e6cb 216 *value = (u64) ACPI_GET8(logical_addr_ptr);
1da177e4
LT
217 break;
218
219 case 16:
5df7e6cb 220 *value = (u64) ACPI_GET16(logical_addr_ptr);
1da177e4
LT
221 break;
222
223 case 32:
5df7e6cb 224 *value = (u64) ACPI_GET32(logical_addr_ptr);
1da177e4
LT
225 break;
226
1da177e4 227 case 64:
5df7e6cb 228 *value = (u64) ACPI_GET64(logical_addr_ptr);
1da177e4 229 break;
59fa8505 230
1da177e4
LT
231 default:
232 /* bit_width was already validated */
233 break;
234 }
235 break;
236
237 case ACPI_WRITE:
238
239 switch (bit_width) {
240 case 8:
57bf6aef 241 ACPI_SET8(logical_addr_ptr, *value);
1da177e4
LT
242 break;
243
244 case 16:
57bf6aef 245 ACPI_SET16(logical_addr_ptr, *value);
1da177e4
LT
246 break;
247
248 case 32:
57bf6aef 249 ACPI_SET32(logical_addr_ptr, *value);
1da177e4
LT
250 break;
251
1da177e4 252 case 64:
57bf6aef 253 ACPI_SET64(logical_addr_ptr, *value);
1da177e4 254 break;
1da177e4
LT
255
256 default:
257 /* bit_width was already validated */
258 break;
259 }
260 break;
261
262 default:
263 status = AE_BAD_PARAMETER;
264 break;
265 }
266
4be44fcd 267 return_ACPI_STATUS(status);
1da177e4
LT
268}
269
1da177e4
LT
270/*******************************************************************************
271 *
272 * FUNCTION: acpi_ex_system_io_space_handler
273 *
ba494bee
BM
274 * PARAMETERS: function - Read or Write operation
275 * address - Where in the space to read or write
1da177e4 276 * bit_width - Field width in bits (8, 16, or 32)
ba494bee 277 * value - Pointer to in or out value
1da177e4
LT
278 * handler_context - Pointer to Handler's context
279 * region_context - Pointer to context specific to the
280 * accessed region
281 *
282 * RETURN: Status
283 *
284 * DESCRIPTION: Handler for the System IO address space (Op Region)
285 *
286 ******************************************************************************/
287
288acpi_status
4be44fcd
LB
289acpi_ex_system_io_space_handler(u32 function,
290 acpi_physical_address address,
291 u32 bit_width,
5df7e6cb 292 u64 *value,
4be44fcd 293 void *handler_context, void *region_context)
1da177e4 294{
4be44fcd
LB
295 acpi_status status = AE_OK;
296 u32 value32;
1da177e4 297
b229cf92 298 ACPI_FUNCTION_TRACE(ex_system_io_space_handler);
1da177e4 299
4be44fcd 300 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
b27d6597 301 "System-IO (width %u) R/W %u Address=%8.8X%8.8X\n",
a2dac7a1 302 bit_width, function, ACPI_FORMAT_UINT64(address)));
1da177e4
LT
303
304 /* Decode the function parameter */
305
306 switch (function) {
307 case ACPI_READ:
308
7f071903 309 status = acpi_hw_read_port((acpi_io_address) address,
4be44fcd 310 &value32, bit_width);
1da177e4
LT
311 *value = value32;
312 break;
313
314 case ACPI_WRITE:
315
7f071903 316 status = acpi_hw_write_port((acpi_io_address) address,
4be44fcd 317 (u32) * value, bit_width);
1da177e4
LT
318 break;
319
320 default:
321 status = AE_BAD_PARAMETER;
322 break;
323 }
324
4be44fcd 325 return_ACPI_STATUS(status);
1da177e4
LT
326}
327
1da177e4
LT
328/*******************************************************************************
329 *
330 * FUNCTION: acpi_ex_pci_config_space_handler
331 *
ba494bee
BM
332 * PARAMETERS: function - Read or Write operation
333 * address - Where in the space to read or write
1da177e4 334 * bit_width - Field width in bits (8, 16, or 32)
ba494bee 335 * value - Pointer to in or out value
1da177e4
LT
336 * handler_context - Pointer to Handler's context
337 * region_context - Pointer to context specific to the
338 * accessed region
339 *
340 * RETURN: Status
341 *
342 * DESCRIPTION: Handler for the PCI Config address space (Op Region)
343 *
344 ******************************************************************************/
345
346acpi_status
4be44fcd
LB
347acpi_ex_pci_config_space_handler(u32 function,
348 acpi_physical_address address,
349 u32 bit_width,
5df7e6cb 350 u64 *value,
4be44fcd 351 void *handler_context, void *region_context)
1da177e4 352{
4be44fcd
LB
353 acpi_status status = AE_OK;
354 struct acpi_pci_id *pci_id;
355 u16 pci_register;
1da177e4 356
b229cf92 357 ACPI_FUNCTION_TRACE(ex_pci_config_space_handler);
1da177e4
LT
358
359 /*
360 * The arguments to acpi_os(Read|Write)pci_configuration are:
361 *
362 * pci_segment is the PCI bus segment range 0-31
363 * pci_bus is the PCI bus number range 0-255
364 * pci_device is the PCI device number range 0-31
365 * pci_function is the PCI device function number
366 * pci_register is the Config space register range 0-255 bytes
367 *
ba494bee 368 * value - input value for write, output address for read
1da177e4
LT
369 *
370 */
4be44fcd 371 pci_id = (struct acpi_pci_id *)region_context;
1da177e4
LT
372 pci_register = (u16) (u32) address;
373
4be44fcd 374 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
b27d6597 375 "Pci-Config %u (%u) Seg(%04x) Bus(%04x) Dev(%04x) Func(%04x) Reg(%04x)\n",
4be44fcd
LB
376 function, bit_width, pci_id->segment, pci_id->bus,
377 pci_id->device, pci_id->function, pci_register));
1da177e4
LT
378
379 switch (function) {
380 case ACPI_READ:
381
4be44fcd 382 status = acpi_os_read_pci_configuration(pci_id, pci_register,
c5f0231e 383 value, bit_width);
1da177e4
LT
384 break;
385
386 case ACPI_WRITE:
387
4be44fcd
LB
388 status = acpi_os_write_pci_configuration(pci_id, pci_register,
389 *value, bit_width);
1da177e4
LT
390 break;
391
392 default:
393
394 status = AE_BAD_PARAMETER;
395 break;
396 }
397
4be44fcd 398 return_ACPI_STATUS(status);
1da177e4
LT
399}
400
1da177e4
LT
401/*******************************************************************************
402 *
403 * FUNCTION: acpi_ex_cmos_space_handler
404 *
ba494bee
BM
405 * PARAMETERS: function - Read or Write operation
406 * address - Where in the space to read or write
1da177e4 407 * bit_width - Field width in bits (8, 16, or 32)
ba494bee 408 * value - Pointer to in or out value
1da177e4
LT
409 * handler_context - Pointer to Handler's context
410 * region_context - Pointer to context specific to the
411 * accessed region
412 *
413 * RETURN: Status
414 *
415 * DESCRIPTION: Handler for the CMOS address space (Op Region)
416 *
417 ******************************************************************************/
418
419acpi_status
4be44fcd
LB
420acpi_ex_cmos_space_handler(u32 function,
421 acpi_physical_address address,
422 u32 bit_width,
5df7e6cb 423 u64 *value,
4be44fcd 424 void *handler_context, void *region_context)
1da177e4 425{
4be44fcd 426 acpi_status status = AE_OK;
1da177e4 427
b229cf92 428 ACPI_FUNCTION_TRACE(ex_cmos_space_handler);
1da177e4 429
4be44fcd 430 return_ACPI_STATUS(status);
1da177e4
LT
431}
432
1da177e4
LT
433/*******************************************************************************
434 *
435 * FUNCTION: acpi_ex_pci_bar_space_handler
436 *
ba494bee
BM
437 * PARAMETERS: function - Read or Write operation
438 * address - Where in the space to read or write
1da177e4 439 * bit_width - Field width in bits (8, 16, or 32)
ba494bee 440 * value - Pointer to in or out value
1da177e4
LT
441 * handler_context - Pointer to Handler's context
442 * region_context - Pointer to context specific to the
443 * accessed region
444 *
445 * RETURN: Status
446 *
447 * DESCRIPTION: Handler for the PCI bar_target address space (Op Region)
448 *
449 ******************************************************************************/
450
451acpi_status
4be44fcd
LB
452acpi_ex_pci_bar_space_handler(u32 function,
453 acpi_physical_address address,
454 u32 bit_width,
5df7e6cb 455 u64 *value,
4be44fcd 456 void *handler_context, void *region_context)
1da177e4 457{
4be44fcd 458 acpi_status status = AE_OK;
1da177e4 459
b229cf92 460 ACPI_FUNCTION_TRACE(ex_pci_bar_space_handler);
1da177e4 461
4be44fcd 462 return_ACPI_STATUS(status);
1da177e4
LT
463}
464
1da177e4
LT
465/*******************************************************************************
466 *
467 * FUNCTION: acpi_ex_data_table_space_handler
468 *
ba494bee
BM
469 * PARAMETERS: function - Read or Write operation
470 * address - Where in the space to read or write
1da177e4 471 * bit_width - Field width in bits (8, 16, or 32)
ba494bee 472 * value - Pointer to in or out value
1da177e4
LT
473 * handler_context - Pointer to Handler's context
474 * region_context - Pointer to context specific to the
475 * accessed region
476 *
477 * RETURN: Status
478 *
479 * DESCRIPTION: Handler for the Data Table address space (Op Region)
480 *
481 ******************************************************************************/
482
483acpi_status
4be44fcd
LB
484acpi_ex_data_table_space_handler(u32 function,
485 acpi_physical_address address,
486 u32 bit_width,
5df7e6cb 487 u64 *value,
4be44fcd 488 void *handler_context, void *region_context)
1da177e4 489{
b229cf92 490 ACPI_FUNCTION_TRACE(ex_data_table_space_handler);
1da177e4 491
c1637e9c
BM
492 /*
493 * Perform the memory read or write. The bit_width was already
494 * validated.
495 */
1da177e4
LT
496 switch (function) {
497 case ACPI_READ:
498
4119532c
BM
499 ACPI_MEMCPY(ACPI_CAST_PTR(char, value),
500 ACPI_PHYSADDR_TO_PTR(address),
501 ACPI_DIV_8(bit_width));
1da177e4
LT
502 break;
503
504 case ACPI_WRITE:
c1637e9c
BM
505
506 ACPI_MEMCPY(ACPI_PHYSADDR_TO_PTR(address),
507 ACPI_CAST_PTR(char, value), ACPI_DIV_8(bit_width));
508 break;
509
1da177e4
LT
510 default:
511
c1637e9c 512 return_ACPI_STATUS(AE_BAD_PARAMETER);
1da177e4
LT
513 }
514
4119532c 515 return_ACPI_STATUS(AE_OK);
1da177e4 516}