Merge branch 'akpm'
[linux-2.6/next.git] / drivers / acpi / acpica / hwxface.c
blob460e96ef07b1e339788e8a637d6db2b6291717c2
2 /******************************************************************************
4 * Module Name: hwxface - Public ACPICA hardware interfaces
6 *****************************************************************************/
8 /*
9 * Copyright (C) 2000 - 2011, Intel Corp.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
31 * NO WARRANTY
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
45 #include <acpi/acpi.h>
46 #include "accommon.h"
47 #include "acnamesp.h"
49 #define _COMPONENT ACPI_HARDWARE
50 ACPI_MODULE_NAME("hwxface")
52 /******************************************************************************
54 * FUNCTION: acpi_reset
56 * PARAMETERS: None
58 * RETURN: Status
60 * DESCRIPTION: Set reset register in memory or IO space. Note: Does not
61 * support reset register in PCI config space, this must be
62 * handled separately.
64 ******************************************************************************/
65 acpi_status acpi_reset(void)
67 struct acpi_generic_address *reset_reg;
68 acpi_status status;
70 ACPI_FUNCTION_TRACE(acpi_reset);
72 reset_reg = &acpi_gbl_FADT.reset_register;
74 /* Check if the reset register is supported */
76 if (!reset_reg->address) {
77 return_ACPI_STATUS(AE_NOT_EXIST);
80 if (reset_reg->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
82 * For I/O space, write directly to the OSL. This
83 * bypasses the port validation mechanism, which may
84 * block a valid write to the reset register. Spec
85 * section 4.7.3.6 requires register width to be 8.
87 status =
88 acpi_os_write_port((acpi_io_address) reset_reg->address,
89 acpi_gbl_FADT.reset_value, 8);
90 } else {
91 /* Write the reset value to the reset register */
93 status = acpi_hw_write(acpi_gbl_FADT.reset_value, reset_reg);
96 return_ACPI_STATUS(status);
99 ACPI_EXPORT_SYMBOL(acpi_reset)
101 /******************************************************************************
103 * FUNCTION: acpi_read
105 * PARAMETERS: Value - Where the value is returned
106 * Reg - GAS register structure
108 * RETURN: Status
110 * DESCRIPTION: Read from either memory or IO space.
112 * LIMITATIONS: <These limitations also apply to acpi_write>
113 * bit_width must be exactly 8, 16, 32, or 64.
114 * space_iD must be system_memory or system_iO.
115 * bit_offset and access_width are currently ignored, as there has
116 * not been a need to implement these.
118 ******************************************************************************/
119 acpi_status acpi_read(u64 *return_value, struct acpi_generic_address *reg)
121 u32 value;
122 u32 width;
123 u64 address;
124 acpi_status status;
126 ACPI_FUNCTION_NAME(acpi_read);
128 if (!return_value) {
129 return (AE_BAD_PARAMETER);
132 /* Validate contents of the GAS register. Allow 64-bit transfers */
134 status = acpi_hw_validate_register(reg, 64, &address);
135 if (ACPI_FAILURE(status)) {
136 return (status);
139 width = reg->bit_width;
140 if (width == 64) {
141 width = 32; /* Break into two 32-bit transfers */
144 /* Initialize entire 64-bit return value to zero */
146 *return_value = 0;
147 value = 0;
150 * Two address spaces supported: Memory or IO. PCI_Config is
151 * not supported here because the GAS structure is insufficient
153 if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
154 status = acpi_os_read_memory((acpi_physical_address)
155 address, &value, width);
156 if (ACPI_FAILURE(status)) {
157 return (status);
159 *return_value = value;
161 if (reg->bit_width == 64) {
163 /* Read the top 32 bits */
165 status = acpi_os_read_memory((acpi_physical_address)
166 (address + 4), &value, 32);
167 if (ACPI_FAILURE(status)) {
168 return (status);
170 *return_value |= ((u64)value << 32);
172 } else { /* ACPI_ADR_SPACE_SYSTEM_IO, validated earlier */
174 status = acpi_hw_read_port((acpi_io_address)
175 address, &value, width);
176 if (ACPI_FAILURE(status)) {
177 return (status);
179 *return_value = value;
181 if (reg->bit_width == 64) {
183 /* Read the top 32 bits */
185 status = acpi_hw_read_port((acpi_io_address)
186 (address + 4), &value, 32);
187 if (ACPI_FAILURE(status)) {
188 return (status);
190 *return_value |= ((u64)value << 32);
194 ACPI_DEBUG_PRINT((ACPI_DB_IO,
195 "Read: %8.8X%8.8X width %2d from %8.8X%8.8X (%s)\n",
196 ACPI_FORMAT_UINT64(*return_value), reg->bit_width,
197 ACPI_FORMAT_UINT64(address),
198 acpi_ut_get_region_name(reg->space_id)));
200 return (status);
203 ACPI_EXPORT_SYMBOL(acpi_read)
205 /******************************************************************************
207 * FUNCTION: acpi_write
209 * PARAMETERS: Value - Value to be written
210 * Reg - GAS register structure
212 * RETURN: Status
214 * DESCRIPTION: Write to either memory or IO space.
216 ******************************************************************************/
217 acpi_status acpi_write(u64 value, struct acpi_generic_address *reg)
219 u32 width;
220 u64 address;
221 acpi_status status;
223 ACPI_FUNCTION_NAME(acpi_write);
225 /* Validate contents of the GAS register. Allow 64-bit transfers */
227 status = acpi_hw_validate_register(reg, 64, &address);
228 if (ACPI_FAILURE(status)) {
229 return (status);
232 width = reg->bit_width;
233 if (width == 64) {
234 width = 32; /* Break into two 32-bit transfers */
238 * Two address spaces supported: Memory or IO. PCI_Config is
239 * not supported here because the GAS structure is insufficient
241 if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
242 status = acpi_os_write_memory((acpi_physical_address)
243 address, ACPI_LODWORD(value),
244 width);
245 if (ACPI_FAILURE(status)) {
246 return (status);
249 if (reg->bit_width == 64) {
250 status = acpi_os_write_memory((acpi_physical_address)
251 (address + 4),
252 ACPI_HIDWORD(value), 32);
253 if (ACPI_FAILURE(status)) {
254 return (status);
257 } else { /* ACPI_ADR_SPACE_SYSTEM_IO, validated earlier */
259 status = acpi_hw_write_port((acpi_io_address)
260 address, ACPI_LODWORD(value),
261 width);
262 if (ACPI_FAILURE(status)) {
263 return (status);
266 if (reg->bit_width == 64) {
267 status = acpi_hw_write_port((acpi_io_address)
268 (address + 4),
269 ACPI_HIDWORD(value), 32);
270 if (ACPI_FAILURE(status)) {
271 return (status);
276 ACPI_DEBUG_PRINT((ACPI_DB_IO,
277 "Wrote: %8.8X%8.8X width %2d to %8.8X%8.8X (%s)\n",
278 ACPI_FORMAT_UINT64(value), reg->bit_width,
279 ACPI_FORMAT_UINT64(address),
280 acpi_ut_get_region_name(reg->space_id)));
282 return (status);
285 ACPI_EXPORT_SYMBOL(acpi_write)
287 /*******************************************************************************
289 * FUNCTION: acpi_read_bit_register
291 * PARAMETERS: register_id - ID of ACPI Bit Register to access
292 * return_value - Value that was read from the register,
293 * normalized to bit position zero.
295 * RETURN: Status and the value read from the specified Register. Value
296 * returned is normalized to bit0 (is shifted all the way right)
298 * DESCRIPTION: ACPI bit_register read function. Does not acquire the HW lock.
300 * SUPPORTS: Bit fields in PM1 Status, PM1 Enable, PM1 Control, and
301 * PM2 Control.
303 * Note: The hardware lock is not required when reading the ACPI bit registers
304 * since almost all of them are single bit and it does not matter that
305 * the parent hardware register can be split across two physical
306 * registers. The only multi-bit field is SLP_TYP in the PM1 control
307 * register, but this field does not cross an 8-bit boundary (nor does
308 * it make much sense to actually read this field.)
310 ******************************************************************************/
311 acpi_status acpi_read_bit_register(u32 register_id, u32 *return_value)
313 struct acpi_bit_register_info *bit_reg_info;
314 u32 register_value;
315 u32 value;
316 acpi_status status;
318 ACPI_FUNCTION_TRACE_U32(acpi_read_bit_register, register_id);
320 /* Get the info structure corresponding to the requested ACPI Register */
322 bit_reg_info = acpi_hw_get_bit_register_info(register_id);
323 if (!bit_reg_info) {
324 return_ACPI_STATUS(AE_BAD_PARAMETER);
327 /* Read the entire parent register */
329 status = acpi_hw_register_read(bit_reg_info->parent_register,
330 &register_value);
331 if (ACPI_FAILURE(status)) {
332 return_ACPI_STATUS(status);
335 /* Normalize the value that was read, mask off other bits */
337 value = ((register_value & bit_reg_info->access_bit_mask)
338 >> bit_reg_info->bit_position);
340 ACPI_DEBUG_PRINT((ACPI_DB_IO,
341 "BitReg %X, ParentReg %X, Actual %8.8X, ReturnValue %8.8X\n",
342 register_id, bit_reg_info->parent_register,
343 register_value, value));
345 *return_value = value;
346 return_ACPI_STATUS(AE_OK);
349 ACPI_EXPORT_SYMBOL(acpi_read_bit_register)
351 /*******************************************************************************
353 * FUNCTION: acpi_write_bit_register
355 * PARAMETERS: register_id - ID of ACPI Bit Register to access
356 * Value - Value to write to the register, in bit
357 * position zero. The bit is automaticallly
358 * shifted to the correct position.
360 * RETURN: Status
362 * DESCRIPTION: ACPI Bit Register write function. Acquires the hardware lock
363 * since most operations require a read/modify/write sequence.
365 * SUPPORTS: Bit fields in PM1 Status, PM1 Enable, PM1 Control, and
366 * PM2 Control.
368 * Note that at this level, the fact that there may be actually two
369 * hardware registers (A and B - and B may not exist) is abstracted.
371 ******************************************************************************/
372 acpi_status acpi_write_bit_register(u32 register_id, u32 value)
374 struct acpi_bit_register_info *bit_reg_info;
375 acpi_cpu_flags lock_flags;
376 u32 register_value;
377 acpi_status status = AE_OK;
379 ACPI_FUNCTION_TRACE_U32(acpi_write_bit_register, register_id);
381 /* Get the info structure corresponding to the requested ACPI Register */
383 bit_reg_info = acpi_hw_get_bit_register_info(register_id);
384 if (!bit_reg_info) {
385 return_ACPI_STATUS(AE_BAD_PARAMETER);
388 lock_flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
391 * At this point, we know that the parent register is one of the
392 * following: PM1 Status, PM1 Enable, PM1 Control, or PM2 Control
394 if (bit_reg_info->parent_register != ACPI_REGISTER_PM1_STATUS) {
396 * 1) Case for PM1 Enable, PM1 Control, and PM2 Control
398 * Perform a register read to preserve the bits that we are not
399 * interested in
401 status = acpi_hw_register_read(bit_reg_info->parent_register,
402 &register_value);
403 if (ACPI_FAILURE(status)) {
404 goto unlock_and_exit;
408 * Insert the input bit into the value that was just read
409 * and write the register
411 ACPI_REGISTER_INSERT_VALUE(register_value,
412 bit_reg_info->bit_position,
413 bit_reg_info->access_bit_mask,
414 value);
416 status = acpi_hw_register_write(bit_reg_info->parent_register,
417 register_value);
418 } else {
420 * 2) Case for PM1 Status
422 * The Status register is different from the rest. Clear an event
423 * by writing 1, writing 0 has no effect. So, the only relevant
424 * information is the single bit we're interested in, all others
425 * should be written as 0 so they will be left unchanged.
427 register_value = ACPI_REGISTER_PREPARE_BITS(value,
428 bit_reg_info->
429 bit_position,
430 bit_reg_info->
431 access_bit_mask);
433 /* No need to write the register if value is all zeros */
435 if (register_value) {
436 status =
437 acpi_hw_register_write(ACPI_REGISTER_PM1_STATUS,
438 register_value);
442 ACPI_DEBUG_PRINT((ACPI_DB_IO,
443 "BitReg %X, ParentReg %X, Value %8.8X, Actual %8.8X\n",
444 register_id, bit_reg_info->parent_register, value,
445 register_value));
447 unlock_and_exit:
449 acpi_os_release_lock(acpi_gbl_hardware_lock, lock_flags);
450 return_ACPI_STATUS(status);
453 ACPI_EXPORT_SYMBOL(acpi_write_bit_register)
455 /*******************************************************************************
457 * FUNCTION: acpi_get_sleep_type_data
459 * PARAMETERS: sleep_state - Numeric sleep state
460 * *sleep_type_a - Where SLP_TYPa is returned
461 * *sleep_type_b - Where SLP_TYPb is returned
463 * RETURN: Status - ACPI status
465 * DESCRIPTION: Obtain the SLP_TYPa and SLP_TYPb values for the requested sleep
466 * state.
468 ******************************************************************************/
469 acpi_status
470 acpi_get_sleep_type_data(u8 sleep_state, u8 *sleep_type_a, u8 *sleep_type_b)
472 acpi_status status = AE_OK;
473 struct acpi_evaluate_info *info;
475 ACPI_FUNCTION_TRACE(acpi_get_sleep_type_data);
477 /* Validate parameters */
479 if ((sleep_state > ACPI_S_STATES_MAX) || !sleep_type_a || !sleep_type_b) {
480 return_ACPI_STATUS(AE_BAD_PARAMETER);
483 /* Allocate the evaluation information block */
485 info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
486 if (!info) {
487 return_ACPI_STATUS(AE_NO_MEMORY);
490 info->pathname =
491 ACPI_CAST_PTR(char, acpi_gbl_sleep_state_names[sleep_state]);
493 /* Evaluate the namespace object containing the values for this state */
495 status = acpi_ns_evaluate(info);
496 if (ACPI_FAILURE(status)) {
497 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
498 "%s while evaluating SleepState [%s]\n",
499 acpi_format_exception(status),
500 info->pathname));
502 goto cleanup;
505 /* Must have a return object */
507 if (!info->return_object) {
508 ACPI_ERROR((AE_INFO, "No Sleep State object returned from [%s]",
509 info->pathname));
510 status = AE_NOT_EXIST;
513 /* It must be of type Package */
515 else if (info->return_object->common.type != ACPI_TYPE_PACKAGE) {
516 ACPI_ERROR((AE_INFO,
517 "Sleep State return object is not a Package"));
518 status = AE_AML_OPERAND_TYPE;
522 * The package must have at least two elements. NOTE (March 2005): This
523 * goes against the current ACPI spec which defines this object as a
524 * package with one encoded DWORD element. However, existing practice
525 * by BIOS vendors seems to be to have 2 or more elements, at least
526 * one per sleep type (A/B).
528 else if (info->return_object->package.count < 2) {
529 ACPI_ERROR((AE_INFO,
530 "Sleep State return package does not have at least two elements"));
531 status = AE_AML_NO_OPERAND;
534 /* The first two elements must both be of type Integer */
536 else if (((info->return_object->package.elements[0])->common.type
537 != ACPI_TYPE_INTEGER) ||
538 ((info->return_object->package.elements[1])->common.type
539 != ACPI_TYPE_INTEGER)) {
540 ACPI_ERROR((AE_INFO,
541 "Sleep State return package elements are not both Integers "
542 "(%s, %s)",
543 acpi_ut_get_object_type_name(info->return_object->
544 package.elements[0]),
545 acpi_ut_get_object_type_name(info->return_object->
546 package.elements[1])));
547 status = AE_AML_OPERAND_TYPE;
548 } else {
549 /* Valid _Sx_ package size, type, and value */
551 *sleep_type_a = (u8)
552 (info->return_object->package.elements[0])->integer.value;
553 *sleep_type_b = (u8)
554 (info->return_object->package.elements[1])->integer.value;
557 if (ACPI_FAILURE(status)) {
558 ACPI_EXCEPTION((AE_INFO, status,
559 "While evaluating SleepState [%s], bad Sleep object %p type %s",
560 info->pathname, info->return_object,
561 acpi_ut_get_object_type_name(info->
562 return_object)));
565 acpi_ut_remove_reference(info->return_object);
567 cleanup:
568 ACPI_FREE(info);
569 return_ACPI_STATUS(status);
572 ACPI_EXPORT_SYMBOL(acpi_get_sleep_type_data)