exregion.c 15.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8

/******************************************************************************
 *
 * Module Name: exregion - ACPI default op_region (address space) handlers
 *
 *****************************************************************************/

/*
9
 * Copyright (C) 2000 - 2010, Intel Corp.
L
Linus Torvalds 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions, and the following disclaimer,
 *    without modification.
 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
 *    substantially similar to the "NO WARRANTY" disclaimer below
 *    ("Disclaimer") and any redistribution must be conditioned upon
 *    including a substantially similar Disclaimer requirement for further
 *    binary redistribution.
 * 3. Neither the names of the above-listed copyright holders nor the names
 *    of any contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * Alternatively, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") version 2 as published by the Free
 * Software Foundation.
 *
 * NO WARRANTY
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 */

#include <acpi/acpi.h>
L
Len Brown 已提交
46 47
#include "accommon.h"
#include "acinterp.h"
L
Linus Torvalds 已提交
48 49

#define _COMPONENT          ACPI_EXECUTER
L
Len Brown 已提交
50
ACPI_MODULE_NAME("exregion")
L
Linus Torvalds 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

/*******************************************************************************
 *
 * FUNCTION:    acpi_ex_system_memory_space_handler
 *
 * PARAMETERS:  Function            - Read or Write operation
 *              Address             - Where in the space to read or write
 *              bit_width           - Field width in bits (8, 16, or 32)
 *              Value               - Pointer to in or out value
 *              handler_context     - Pointer to Handler's context
 *              region_context      - Pointer to context specific to the
 *                                    accessed region
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Handler for the System Memory address space (Op Region)
 *
 ******************************************************************************/
acpi_status
L
Len Brown 已提交
70 71 72 73 74
acpi_ex_system_memory_space_handler(u32 function,
				    acpi_physical_address address,
				    u32 bit_width,
				    acpi_integer * value,
				    void *handler_context, void *region_context)
L
Linus Torvalds 已提交
75
{
L
Len Brown 已提交
76 77 78 79
	acpi_status status = AE_OK;
	void *logical_addr_ptr = NULL;
	struct acpi_mem_space_context *mem_info = region_context;
	u32 length;
80 81
	acpi_size map_length;
	acpi_size page_boundary_map_length;
B
Bob Moore 已提交
82
#ifdef ACPI_MISALIGNMENT_NOT_SUPPORTED
L
Len Brown 已提交
83
	u32 remainder;
L
Linus Torvalds 已提交
84 85
#endif

B
Bob Moore 已提交
86
	ACPI_FUNCTION_TRACE(ex_system_memory_space_handler);
L
Linus Torvalds 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

	/* Validate and translate the bit width */

	switch (bit_width) {
	case 8:
		length = 1;
		break;

	case 16:
		length = 2;
		break;

	case 32:
		length = 4;
		break;

	case 64:
		length = 8;
		break;

	default:
B
Bob Moore 已提交
108
		ACPI_ERROR((AE_INFO, "Invalid SystemMemory width %d",
B
Bob Moore 已提交
109
			    bit_width));
L
Len Brown 已提交
110
		return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
L
Linus Torvalds 已提交
111 112
	}

B
Bob Moore 已提交
113
#ifdef ACPI_MISALIGNMENT_NOT_SUPPORTED
L
Linus Torvalds 已提交
114 115 116 117
	/*
	 * Hardware does not support non-aligned data transfers, we must verify
	 * the request.
	 */
L
Len Brown 已提交
118 119
	(void)acpi_ut_short_divide((acpi_integer) address, length, NULL,
				   &remainder);
L
Linus Torvalds 已提交
120
	if (remainder != 0) {
L
Len Brown 已提交
121
		return_ACPI_STATUS(AE_AML_ALIGNMENT);
L
Linus Torvalds 已提交
122 123 124 125 126 127 128 129 130
	}
#endif

	/*
	 * Does the request fit into the cached memory mapping?
	 * Is 1) Address below the current mapping? OR
	 *    2) Address beyond the current mapping?
	 */
	if ((address < mem_info->mapped_physical_address) ||
L
Len Brown 已提交
131 132 133 134
	    (((acpi_integer) address + length) > ((acpi_integer)
						  mem_info->
						  mapped_physical_address +
						  mem_info->mapped_length))) {
L
Linus Torvalds 已提交
135 136 137 138 139
		/*
		 * The request cannot be resolved by the current memory mapping;
		 * Delete the existing mapping and create a new one.
		 */
		if (mem_info->mapped_length) {
B
Bob Moore 已提交
140

L
Linus Torvalds 已提交
141 142
			/* Valid mapping, delete it */

L
Len Brown 已提交
143 144
			acpi_os_unmap_memory(mem_info->mapped_logical_address,
					     mem_info->mapped_length);
L
Linus Torvalds 已提交
145 146 147
		}

		/*
148 149 150
		 * Attempt to map from the requested address to the end of the region.
		 * However, we will never map more than one page, nor will we cross
		 * a page boundary.
L
Linus Torvalds 已提交
151
		 */
152
		map_length = (acpi_size)
L
Len Brown 已提交
153
		    ((mem_info->address + mem_info->length) - address);
R
Robert Moore 已提交
154

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
		/*
		 * If mapping the entire remaining portion of the region will cross
		 * a page boundary, just map up to the page boundary, do not cross.
		 * On some systems, crossing a page boundary while mapping regions
		 * can cause warnings if the pages have different attributes
		 * due to resource management
		 */
		page_boundary_map_length =
		    ACPI_ROUND_UP(address, ACPI_DEFAULT_PAGE_SIZE) - address;

		if (!page_boundary_map_length) {
			page_boundary_map_length = ACPI_DEFAULT_PAGE_SIZE;
		}

		if (map_length > page_boundary_map_length) {
			map_length = page_boundary_map_length;
L
Linus Torvalds 已提交
171 172 173 174
		}

		/* Create a new mapping starting at the address given */

175
		mem_info->mapped_logical_address = acpi_os_map_memory((acpi_physical_address) address, map_length);
176
		if (!mem_info->mapped_logical_address) {
B
Bob Moore 已提交
177 178
			ACPI_ERROR((AE_INFO,
				    "Could not map memory at %8.8X%8.8X, size %X",
179
				    ACPI_FORMAT_NATIVE_UINT(address),
180
				    (u32) map_length));
L
Linus Torvalds 已提交
181
			mem_info->mapped_length = 0;
182
			return_ACPI_STATUS(AE_NO_MEMORY);
L
Linus Torvalds 已提交
183 184 185 186 187
		}

		/* Save the physical address and mapping size */

		mem_info->mapped_physical_address = address;
188
		mem_info->mapped_length = map_length;
L
Linus Torvalds 已提交
189 190 191 192 193 194 195
	}

	/*
	 * Generate a logical pointer corresponding to the address we want to
	 * access
	 */
	logical_addr_ptr = mem_info->mapped_logical_address +
L
Len Brown 已提交
196 197 198 199
	    ((acpi_integer) address -
	     (acpi_integer) mem_info->mapped_physical_address);

	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
B
Bob Moore 已提交
200
			  "System-Memory (width %d) R/W %d Address=%8.8X%8.8X\n",
201 202
			  bit_width, function,
			  ACPI_FORMAT_NATIVE_UINT(address)));
L
Len Brown 已提交
203 204 205 206 207 208 209 210 211

	/*
	 * Perform the memory read or write
	 *
	 * Note: For machines that do not support non-aligned transfers, the target
	 * address was checked for alignment above.  We do not attempt to break the
	 * transfer up into smaller (byte-size) chunks because the AML specifically
	 * asked for a transfer width that the hardware may require.
	 */
L
Linus Torvalds 已提交
212 213 214 215 216 217
	switch (function) {
	case ACPI_READ:

		*value = 0;
		switch (bit_width) {
		case 8:
B
Bob Moore 已提交
218
			*value = (acpi_integer) ACPI_GET8(logical_addr_ptr);
L
Linus Torvalds 已提交
219 220 221
			break;

		case 16:
B
Bob Moore 已提交
222
			*value = (acpi_integer) ACPI_GET16(logical_addr_ptr);
L
Linus Torvalds 已提交
223 224 225
			break;

		case 32:
B
Bob Moore 已提交
226
			*value = (acpi_integer) ACPI_GET32(logical_addr_ptr);
L
Linus Torvalds 已提交
227 228 229
			break;

		case 64:
B
Bob Moore 已提交
230
			*value = (acpi_integer) ACPI_GET64(logical_addr_ptr);
L
Linus Torvalds 已提交
231
			break;
B
Bob Moore 已提交
232

L
Linus Torvalds 已提交
233 234 235 236 237 238 239 240 241 242
		default:
			/* bit_width was already validated */
			break;
		}
		break;

	case ACPI_WRITE:

		switch (bit_width) {
		case 8:
B
Bob Moore 已提交
243
			ACPI_SET8(logical_addr_ptr) = (u8) * value;
L
Linus Torvalds 已提交
244 245 246
			break;

		case 16:
B
Bob Moore 已提交
247
			ACPI_SET16(logical_addr_ptr) = (u16) * value;
L
Linus Torvalds 已提交
248 249 250
			break;

		case 32:
B
Bob Moore 已提交
251
			ACPI_SET32(logical_addr_ptr) = (u32) * value;
L
Linus Torvalds 已提交
252 253 254
			break;

		case 64:
B
Bob Moore 已提交
255
			ACPI_SET64(logical_addr_ptr) = (u64) * value;
L
Linus Torvalds 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268
			break;

		default:
			/* bit_width was already validated */
			break;
		}
		break;

	default:
		status = AE_BAD_PARAMETER;
		break;
	}

L
Len Brown 已提交
269
	return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ex_system_io_space_handler
 *
 * PARAMETERS:  Function            - Read or Write operation
 *              Address             - Where in the space to read or write
 *              bit_width           - Field width in bits (8, 16, or 32)
 *              Value               - Pointer to in or out value
 *              handler_context     - Pointer to Handler's context
 *              region_context      - Pointer to context specific to the
 *                                    accessed region
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Handler for the System IO address space (Op Region)
 *
 ******************************************************************************/

acpi_status
L
Len Brown 已提交
291 292 293 294 295
acpi_ex_system_io_space_handler(u32 function,
				acpi_physical_address address,
				u32 bit_width,
				acpi_integer * value,
				void *handler_context, void *region_context)
L
Linus Torvalds 已提交
296
{
L
Len Brown 已提交
297 298
	acpi_status status = AE_OK;
	u32 value32;
L
Linus Torvalds 已提交
299

B
Bob Moore 已提交
300
	ACPI_FUNCTION_TRACE(ex_system_io_space_handler);
L
Linus Torvalds 已提交
301

L
Len Brown 已提交
302
	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
B
Bob Moore 已提交
303
			  "System-IO (width %d) R/W %d Address=%8.8X%8.8X\n",
304 305
			  bit_width, function,
			  ACPI_FORMAT_NATIVE_UINT(address)));
L
Linus Torvalds 已提交
306 307 308 309 310 311

	/* Decode the function parameter */

	switch (function) {
	case ACPI_READ:

B
Bob Moore 已提交
312
		status = acpi_hw_read_port((acpi_io_address) address,
L
Len Brown 已提交
313
					   &value32, bit_width);
L
Linus Torvalds 已提交
314 315 316 317 318
		*value = value32;
		break;

	case ACPI_WRITE:

B
Bob Moore 已提交
319
		status = acpi_hw_write_port((acpi_io_address) address,
L
Len Brown 已提交
320
					    (u32) * value, bit_width);
L
Linus Torvalds 已提交
321 322 323 324 325 326 327
		break;

	default:
		status = AE_BAD_PARAMETER;
		break;
	}

L
Len Brown 已提交
328
	return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ex_pci_config_space_handler
 *
 * PARAMETERS:  Function            - Read or Write operation
 *              Address             - Where in the space to read or write
 *              bit_width           - Field width in bits (8, 16, or 32)
 *              Value               - Pointer to in or out value
 *              handler_context     - Pointer to Handler's context
 *              region_context      - Pointer to context specific to the
 *                                    accessed region
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Handler for the PCI Config address space (Op Region)
 *
 ******************************************************************************/

acpi_status
L
Len Brown 已提交
350 351 352 353 354
acpi_ex_pci_config_space_handler(u32 function,
				 acpi_physical_address address,
				 u32 bit_width,
				 acpi_integer * value,
				 void *handler_context, void *region_context)
L
Linus Torvalds 已提交
355
{
L
Len Brown 已提交
356 357 358
	acpi_status status = AE_OK;
	struct acpi_pci_id *pci_id;
	u16 pci_register;
M
Ming Lin 已提交
359
	u32 value32;
L
Linus Torvalds 已提交
360

B
Bob Moore 已提交
361
	ACPI_FUNCTION_TRACE(ex_pci_config_space_handler);
L
Linus Torvalds 已提交
362 363 364 365 366 367 368 369 370 371 372 373 374

	/*
	 *  The arguments to acpi_os(Read|Write)pci_configuration are:
	 *
	 *  pci_segment is the PCI bus segment range 0-31
	 *  pci_bus     is the PCI bus number range 0-255
	 *  pci_device  is the PCI device number range 0-31
	 *  pci_function is the PCI device function number
	 *  pci_register is the Config space register range 0-255 bytes
	 *
	 *  Value - input value for write, output address for read
	 *
	 */
L
Len Brown 已提交
375
	pci_id = (struct acpi_pci_id *)region_context;
L
Linus Torvalds 已提交
376 377
	pci_register = (u16) (u32) address;

L
Len Brown 已提交
378
	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
B
Bob Moore 已提交
379
			  "Pci-Config %d (%d) Seg(%04x) Bus(%04x) Dev(%04x) Func(%04x) Reg(%04x)\n",
L
Len Brown 已提交
380 381
			  function, bit_width, pci_id->segment, pci_id->bus,
			  pci_id->device, pci_id->function, pci_register));
L
Linus Torvalds 已提交
382 383 384 385

	switch (function) {
	case ACPI_READ:

L
Len Brown 已提交
386
		status = acpi_os_read_pci_configuration(pci_id, pci_register,
M
Ming Lin 已提交
387 388
							&value32, bit_width);
		*value = value32;
L
Linus Torvalds 已提交
389 390 391 392
		break;

	case ACPI_WRITE:

L
Len Brown 已提交
393 394
		status = acpi_os_write_pci_configuration(pci_id, pci_register,
							 *value, bit_width);
L
Linus Torvalds 已提交
395 396 397 398 399 400 401 402
		break;

	default:

		status = AE_BAD_PARAMETER;
		break;
	}

L
Len Brown 已提交
403
	return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ex_cmos_space_handler
 *
 * PARAMETERS:  Function            - Read or Write operation
 *              Address             - Where in the space to read or write
 *              bit_width           - Field width in bits (8, 16, or 32)
 *              Value               - Pointer to in or out value
 *              handler_context     - Pointer to Handler's context
 *              region_context      - Pointer to context specific to the
 *                                    accessed region
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Handler for the CMOS address space (Op Region)
 *
 ******************************************************************************/

acpi_status
L
Len Brown 已提交
425 426 427 428 429
acpi_ex_cmos_space_handler(u32 function,
			   acpi_physical_address address,
			   u32 bit_width,
			   acpi_integer * value,
			   void *handler_context, void *region_context)
L
Linus Torvalds 已提交
430
{
L
Len Brown 已提交
431
	acpi_status status = AE_OK;
L
Linus Torvalds 已提交
432

B
Bob Moore 已提交
433
	ACPI_FUNCTION_TRACE(ex_cmos_space_handler);
L
Linus Torvalds 已提交
434

L
Len Brown 已提交
435
	return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ex_pci_bar_space_handler
 *
 * PARAMETERS:  Function            - Read or Write operation
 *              Address             - Where in the space to read or write
 *              bit_width           - Field width in bits (8, 16, or 32)
 *              Value               - Pointer to in or out value
 *              handler_context     - Pointer to Handler's context
 *              region_context      - Pointer to context specific to the
 *                                    accessed region
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Handler for the PCI bar_target address space (Op Region)
 *
 ******************************************************************************/

acpi_status
L
Len Brown 已提交
457 458 459 460 461
acpi_ex_pci_bar_space_handler(u32 function,
			      acpi_physical_address address,
			      u32 bit_width,
			      acpi_integer * value,
			      void *handler_context, void *region_context)
L
Linus Torvalds 已提交
462
{
L
Len Brown 已提交
463
	acpi_status status = AE_OK;
L
Linus Torvalds 已提交
464

B
Bob Moore 已提交
465
	ACPI_FUNCTION_TRACE(ex_pci_bar_space_handler);
L
Linus Torvalds 已提交
466

L
Len Brown 已提交
467
	return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ex_data_table_space_handler
 *
 * PARAMETERS:  Function            - Read or Write operation
 *              Address             - Where in the space to read or write
 *              bit_width           - Field width in bits (8, 16, or 32)
 *              Value               - Pointer to in or out value
 *              handler_context     - Pointer to Handler's context
 *              region_context      - Pointer to context specific to the
 *                                    accessed region
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Handler for the Data Table address space (Op Region)
 *
 ******************************************************************************/

acpi_status
L
Len Brown 已提交
489 490 491 492 493
acpi_ex_data_table_space_handler(u32 function,
				 acpi_physical_address address,
				 u32 bit_width,
				 acpi_integer * value,
				 void *handler_context, void *region_context)
L
Linus Torvalds 已提交
494
{
B
Bob Moore 已提交
495
	ACPI_FUNCTION_TRACE(ex_data_table_space_handler);
L
Linus Torvalds 已提交
496

R
Robert Moore 已提交
497
	/* Perform the memory read or write */
L
Linus Torvalds 已提交
498 499 500 501

	switch (function) {
	case ACPI_READ:

B
Bob Moore 已提交
502 503 504
		ACPI_MEMCPY(ACPI_CAST_PTR(char, value),
			    ACPI_PHYSADDR_TO_PTR(address),
			    ACPI_DIV_8(bit_width));
L
Linus Torvalds 已提交
505 506 507 508 509
		break;

	case ACPI_WRITE:
	default:

L
Len Brown 已提交
510
		return_ACPI_STATUS(AE_SUPPORT);
L
Linus Torvalds 已提交
511 512
	}

B
Bob Moore 已提交
513
	return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
514
}