exregion.c 14.8 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
acpi_ex_system_memory_space_handler(u32 function,
				    acpi_physical_address address,
				    u32 bit_width,
73
				    u64 *value,
L
Len Brown 已提交
74
				    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:
108
		ACPI_ERROR((AE_INFO, "Invalid SystemMemory width %u",
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.
	 */
118
	(void)acpi_ut_short_divide((u64) address, length, NULL, &remainder);
L
Linus Torvalds 已提交
119
	if (remainder != 0) {
L
Len Brown 已提交
120
		return_ACPI_STATUS(AE_AML_ALIGNMENT);
L
Linus Torvalds 已提交
121 122 123 124 125 126 127 128 129
	}
#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) ||
130 131 132
	    (((u64) address + length) > ((u64)
					 mem_info->mapped_physical_address +
					 mem_info->mapped_length))) {
L
Linus Torvalds 已提交
133 134 135 136 137
		/*
		 * 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 已提交
138

L
Linus Torvalds 已提交
139 140
			/* Valid mapping, delete it */

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

		/*
146 147 148
		 * 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 已提交
149
		 */
150
		map_length = (acpi_size)
L
Len Brown 已提交
151
		    ((mem_info->address + mem_info->length) - address);
R
Robert Moore 已提交
152

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
		/*
		 * 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 已提交
169 170 171 172
		}

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

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

		/* Save the physical address and mapping size */

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

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

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

	/*
	 * 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 已提交
209 210 211 212 213 214
	switch (function) {
	case ACPI_READ:

		*value = 0;
		switch (bit_width) {
		case 8:
215
			*value = (u64) ACPI_GET8(logical_addr_ptr);
L
Linus Torvalds 已提交
216 217 218
			break;

		case 16:
219
			*value = (u64) ACPI_GET16(logical_addr_ptr);
L
Linus Torvalds 已提交
220 221 222
			break;

		case 32:
223
			*value = (u64) ACPI_GET32(logical_addr_ptr);
L
Linus Torvalds 已提交
224 225 226
			break;

		case 64:
227
			*value = (u64) ACPI_GET64(logical_addr_ptr);
L
Linus Torvalds 已提交
228
			break;
B
Bob Moore 已提交
229

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

	case ACPI_WRITE:

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

		case 16:
B
Bob Moore 已提交
244
			ACPI_SET16(logical_addr_ptr) = (u16) * value;
L
Linus Torvalds 已提交
245 246 247
			break;

		case 32:
B
Bob Moore 已提交
248
			ACPI_SET32(logical_addr_ptr) = (u32) * value;
L
Linus Torvalds 已提交
249 250 251
			break;

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

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

	default:
		status = AE_BAD_PARAMETER;
		break;
	}

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

/*******************************************************************************
 *
 * 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 已提交
288 289 290
acpi_ex_system_io_space_handler(u32 function,
				acpi_physical_address address,
				u32 bit_width,
291
				u64 *value,
L
Len Brown 已提交
292
				void *handler_context, void *region_context)
L
Linus Torvalds 已提交
293
{
L
Len Brown 已提交
294 295
	acpi_status status = AE_OK;
	u32 value32;
L
Linus Torvalds 已提交
296

B
Bob Moore 已提交
297
	ACPI_FUNCTION_TRACE(ex_system_io_space_handler);
L
Linus Torvalds 已提交
298

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

	/* Decode the function parameter */

	switch (function) {
	case ACPI_READ:

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

	case ACPI_WRITE:

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

	default:
		status = AE_BAD_PARAMETER;
		break;
	}

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

/*******************************************************************************
 *
 * 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 已提交
347 348 349
acpi_ex_pci_config_space_handler(u32 function,
				 acpi_physical_address address,
				 u32 bit_width,
350
				 u64 *value,
L
Len Brown 已提交
351
				 void *handler_context, void *region_context)
L
Linus Torvalds 已提交
352
{
L
Len Brown 已提交
353 354 355
	acpi_status status = AE_OK;
	struct acpi_pci_id *pci_id;
	u16 pci_register;
M
Ming Lin 已提交
356
	u32 value32;
L
Linus Torvalds 已提交
357

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

	/*
	 *  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 已提交
372
	pci_id = (struct acpi_pci_id *)region_context;
L
Linus Torvalds 已提交
373 374
	pci_register = (u16) (u32) address;

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

	switch (function) {
	case ACPI_READ:

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

	case ACPI_WRITE:

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

	default:

		status = AE_BAD_PARAMETER;
		break;
	}

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

/*******************************************************************************
 *
 * 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 已提交
422 423 424
acpi_ex_cmos_space_handler(u32 function,
			   acpi_physical_address address,
			   u32 bit_width,
425
			   u64 *value,
L
Len Brown 已提交
426
			   void *handler_context, void *region_context)
L
Linus Torvalds 已提交
427
{
L
Len Brown 已提交
428
	acpi_status status = AE_OK;
L
Linus Torvalds 已提交
429

B
Bob Moore 已提交
430
	ACPI_FUNCTION_TRACE(ex_cmos_space_handler);
L
Linus Torvalds 已提交
431

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

/*******************************************************************************
 *
 * 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 已提交
454 455 456
acpi_ex_pci_bar_space_handler(u32 function,
			      acpi_physical_address address,
			      u32 bit_width,
457
			      u64 *value,
L
Len Brown 已提交
458
			      void *handler_context, void *region_context)
L
Linus Torvalds 已提交
459
{
L
Len Brown 已提交
460
	acpi_status status = AE_OK;
L
Linus Torvalds 已提交
461

B
Bob Moore 已提交
462
	ACPI_FUNCTION_TRACE(ex_pci_bar_space_handler);
L
Linus Torvalds 已提交
463

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

/*******************************************************************************
 *
 * 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 已提交
486 487 488
acpi_ex_data_table_space_handler(u32 function,
				 acpi_physical_address address,
				 u32 bit_width,
489
				 u64 *value,
L
Len Brown 已提交
490
				 void *handler_context, void *region_context)
L
Linus Torvalds 已提交
491
{
B
Bob Moore 已提交
492
	ACPI_FUNCTION_TRACE(ex_data_table_space_handler);
L
Linus Torvalds 已提交
493

R
Robert Moore 已提交
494
	/* Perform the memory read or write */
L
Linus Torvalds 已提交
495 496 497 498

	switch (function) {
	case ACPI_READ:

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

	case ACPI_WRITE:
	default:

L
Len Brown 已提交
507
		return_ACPI_STATUS(AE_SUPPORT);
L
Linus Torvalds 已提交
508 509
	}

B
Bob Moore 已提交
510
	return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
511
}