2 /*******************************************************************************
4 * Module Name: hwregs - Read/write access functions for the various ACPI
5 * control and status registers.
7 ******************************************************************************/
10 * Copyright (C) 2000 - 2007, R. Byron Moore
11 * All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions, and the following disclaimer,
18 * without modification.
19 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
20 * substantially similar to the "NO WARRANTY" disclaimer below
21 * ("Disclaimer") and any redistribution must be conditioned upon
22 * including a substantially similar Disclaimer requirement for further
23 * binary redistribution.
24 * 3. Neither the names of the above-listed copyright holders nor the names
25 * of any contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
28 * Alternatively, this software may be distributed under the terms of the
29 * GNU General Public License ("GPL") version 2 as published by the Free
30 * Software Foundation.
33 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
34 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
36 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
37 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
42 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
43 * POSSIBILITY OF SUCH DAMAGES.
46 #include <acpi/acpi.h>
47 #include <acpi/acnamesp.h>
48 #include <acpi/acevents.h>
50 #define _COMPONENT ACPI_HARDWARE
51 ACPI_MODULE_NAME("hwregs")
53 /*******************************************************************************
55 * FUNCTION: acpi_hw_clear_acpi_status
61 * DESCRIPTION: Clears all fixed and general purpose status bits
62 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
64 ******************************************************************************/
65 acpi_status acpi_hw_clear_acpi_status(void)
68 acpi_cpu_flags lock_flags = 0;
70 ACPI_FUNCTION_TRACE(hw_clear_acpi_status);
72 ACPI_DEBUG_PRINT((ACPI_DB_IO, "About to write %04X to %04X\n",
73 ACPI_BITMASK_ALL_FIXED_STATUS,
74 (u16) acpi_gbl_FADT.xpm1a_event_block.address));
76 lock_flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
78 status = acpi_hw_register_write(ACPI_REGISTER_PM1_STATUS,
79 ACPI_BITMASK_ALL_FIXED_STATUS);
80 if (ACPI_FAILURE(status)) {
84 /* Clear the fixed events */
86 if (acpi_gbl_FADT.xpm1b_event_block.address) {
88 acpi_hw_low_level_write(16, ACPI_BITMASK_ALL_FIXED_STATUS,
89 &acpi_gbl_FADT.xpm1b_event_block);
90 if (ACPI_FAILURE(status)) {
95 /* Clear the GPE Bits in all GPE registers in all GPE blocks */
97 status = acpi_ev_walk_gpe_list(acpi_hw_clear_gpe_block);
100 acpi_os_release_lock(acpi_gbl_hardware_lock, lock_flags);
101 return_ACPI_STATUS(status);
104 /*******************************************************************************
106 * FUNCTION: acpi_get_sleep_type_data
108 * PARAMETERS: sleep_state - Numeric sleep state
109 * *sleep_type_a - Where SLP_TYPa is returned
110 * *sleep_type_b - Where SLP_TYPb is returned
112 * RETURN: Status - ACPI status
114 * DESCRIPTION: Obtain the SLP_TYPa and SLP_TYPb values for the requested sleep
117 ******************************************************************************/
120 acpi_get_sleep_type_data(u8 sleep_state, u8 * sleep_type_a, u8 * sleep_type_b)
122 acpi_status status = AE_OK;
123 struct acpi_evaluate_info *info;
125 ACPI_FUNCTION_TRACE(acpi_get_sleep_type_data);
127 /* Validate parameters */
129 if ((sleep_state > ACPI_S_STATES_MAX) || !sleep_type_a || !sleep_type_b) {
130 return_ACPI_STATUS(AE_BAD_PARAMETER);
133 /* Allocate the evaluation information block */
135 info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
137 return_ACPI_STATUS(AE_NO_MEMORY);
141 ACPI_CAST_PTR(char, acpi_gbl_sleep_state_names[sleep_state]);
143 /* Evaluate the namespace object containing the values for this state */
145 status = acpi_ns_evaluate(info);
146 if (ACPI_FAILURE(status)) {
147 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
148 "%s while evaluating SleepState [%s]\n",
149 acpi_format_exception(status),
155 /* Must have a return object */
157 if (!info->return_object) {
158 ACPI_ERROR((AE_INFO, "No Sleep State object returned from [%s]",
160 status = AE_NOT_EXIST;
163 /* It must be of type Package */
165 else if (ACPI_GET_OBJECT_TYPE(info->return_object) != ACPI_TYPE_PACKAGE) {
167 "Sleep State return object is not a Package"));
168 status = AE_AML_OPERAND_TYPE;
172 * The package must have at least two elements. NOTE (March 2005): This
173 * goes against the current ACPI spec which defines this object as a
174 * package with one encoded DWORD element. However, existing practice
175 * by BIOS vendors seems to be to have 2 or more elements, at least
176 * one per sleep type (A/B).
178 else if (info->return_object->package.count < 2) {
180 "Sleep State return package does not have at least two elements"));
181 status = AE_AML_NO_OPERAND;
184 /* The first two elements must both be of type Integer */
186 else if ((ACPI_GET_OBJECT_TYPE(info->return_object->package.elements[0])
187 != ACPI_TYPE_INTEGER) ||
188 (ACPI_GET_OBJECT_TYPE(info->return_object->package.elements[1])
189 != ACPI_TYPE_INTEGER)) {
191 "Sleep State return package elements are not both Integers (%s, %s)",
192 acpi_ut_get_object_type_name(info->return_object->
193 package.elements[0]),
194 acpi_ut_get_object_type_name(info->return_object->
195 package.elements[1])));
196 status = AE_AML_OPERAND_TYPE;
198 /* Valid _Sx_ package size, type, and value */
201 (info->return_object->package.elements[0])->integer.value;
203 (info->return_object->package.elements[1])->integer.value;
206 if (ACPI_FAILURE(status)) {
207 ACPI_EXCEPTION((AE_INFO, status,
208 "While evaluating SleepState [%s], bad Sleep object %p type %s",
209 info->pathname, info->return_object,
210 acpi_ut_get_object_type_name(info->
214 acpi_ut_remove_reference(info->return_object);
218 return_ACPI_STATUS(status);
221 ACPI_EXPORT_SYMBOL(acpi_get_sleep_type_data)
223 /*******************************************************************************
225 * FUNCTION: acpi_hw_get_register_bit_mask
227 * PARAMETERS: register_id - Index of ACPI Register to access
229 * RETURN: The bitmask to be used when accessing the register
231 * DESCRIPTION: Map register_id into a register bitmask.
233 ******************************************************************************/
234 struct acpi_bit_register_info *acpi_hw_get_bit_register_info(u32 register_id)
236 ACPI_FUNCTION_ENTRY();
238 if (register_id > ACPI_BITREG_MAX) {
239 ACPI_ERROR((AE_INFO, "Invalid BitRegister ID: %X",
244 return (&acpi_gbl_bit_register_info[register_id]);
247 /*******************************************************************************
249 * FUNCTION: acpi_get_register
251 * PARAMETERS: register_id - ID of ACPI bit_register to access
252 * return_value - Value that was read from the register
254 * RETURN: Status and the value read from specified Register. Value
255 * returned is normalized to bit0 (is shifted all the way right)
257 * DESCRIPTION: ACPI bit_register read function.
259 ******************************************************************************/
261 acpi_status acpi_get_register_unlocked(u32 register_id, u32 * return_value)
263 u32 register_value = 0;
264 struct acpi_bit_register_info *bit_reg_info;
267 ACPI_FUNCTION_TRACE(acpi_get_register);
269 /* Get the info structure corresponding to the requested ACPI Register */
271 bit_reg_info = acpi_hw_get_bit_register_info(register_id);
273 return_ACPI_STATUS(AE_BAD_PARAMETER);
276 /* Read from the register */
278 status = acpi_hw_register_read(bit_reg_info->parent_register,
281 if (ACPI_SUCCESS(status)) {
283 /* Normalize the value that was read */
286 ((register_value & bit_reg_info->access_bit_mask)
287 >> bit_reg_info->bit_position);
289 *return_value = register_value;
291 ACPI_DEBUG_PRINT((ACPI_DB_IO, "Read value %8.8X register %X\n",
293 bit_reg_info->parent_register));
296 return_ACPI_STATUS(status);
299 acpi_status acpi_get_register(u32 register_id, u32 * return_value)
302 acpi_cpu_flags flags;
303 flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
304 status = acpi_get_register_unlocked(register_id, return_value);
305 acpi_os_release_lock(acpi_gbl_hardware_lock, flags);
309 ACPI_EXPORT_SYMBOL(acpi_get_register)
311 /*******************************************************************************
313 * FUNCTION: acpi_set_register
315 * PARAMETERS: register_id - ID of ACPI bit_register to access
316 * Value - (only used on write) value to write to the
317 * Register, NOT pre-normalized to the bit pos
321 * DESCRIPTION: ACPI Bit Register write function.
323 ******************************************************************************/
324 acpi_status acpi_set_register(u32 register_id, u32 value)
326 u32 register_value = 0;
327 struct acpi_bit_register_info *bit_reg_info;
329 acpi_cpu_flags lock_flags;
331 ACPI_FUNCTION_TRACE_U32(acpi_set_register, register_id);
333 /* Get the info structure corresponding to the requested ACPI Register */
335 bit_reg_info = acpi_hw_get_bit_register_info(register_id);
337 ACPI_ERROR((AE_INFO, "Bad ACPI HW RegisterId: %X",
339 return_ACPI_STATUS(AE_BAD_PARAMETER);
342 lock_flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
344 /* Always do a register read first so we can insert the new bits */
346 status = acpi_hw_register_read(bit_reg_info->parent_register,
348 if (ACPI_FAILURE(status)) {
349 goto unlock_and_exit;
353 * Decode the Register ID
354 * Register ID = [Register block ID] | [bit ID]
356 * Check bit ID to fine locate Register offset.
357 * Check Mask to determine Register offset, and then read-write.
359 switch (bit_reg_info->parent_register) {
360 case ACPI_REGISTER_PM1_STATUS:
363 * Status Registers are different from the rest. Clear by
364 * writing 1, and writing 0 has no effect. So, the only relevant
365 * information is the single bit we're interested in, all others should
366 * be written as 0 so they will be left unchanged.
368 value = ACPI_REGISTER_PREPARE_BITS(value,
369 bit_reg_info->bit_position,
373 status = acpi_hw_register_write(ACPI_REGISTER_PM1_STATUS,
379 case ACPI_REGISTER_PM1_ENABLE:
381 ACPI_REGISTER_INSERT_VALUE(register_value,
382 bit_reg_info->bit_position,
383 bit_reg_info->access_bit_mask,
386 status = acpi_hw_register_write(ACPI_REGISTER_PM1_ENABLE,
387 (u16) register_value);
390 case ACPI_REGISTER_PM1_CONTROL:
393 * Write the PM1 Control register.
394 * Note that at this level, the fact that there are actually TWO
395 * registers (A and B - and B may not exist) is abstracted.
397 ACPI_DEBUG_PRINT((ACPI_DB_IO, "PM1 control: Read %X\n",
400 ACPI_REGISTER_INSERT_VALUE(register_value,
401 bit_reg_info->bit_position,
402 bit_reg_info->access_bit_mask,
405 status = acpi_hw_register_write(ACPI_REGISTER_PM1_CONTROL,
406 (u16) register_value);
409 case ACPI_REGISTER_PM2_CONTROL:
411 status = acpi_hw_register_read(ACPI_REGISTER_PM2_CONTROL,
413 if (ACPI_FAILURE(status)) {
414 goto unlock_and_exit;
417 ACPI_DEBUG_PRINT((ACPI_DB_IO,
418 "PM2 control: Read %X from %8.8X%8.8X\n",
420 ACPI_FORMAT_UINT64(acpi_gbl_FADT.
424 ACPI_REGISTER_INSERT_VALUE(register_value,
425 bit_reg_info->bit_position,
426 bit_reg_info->access_bit_mask,
429 ACPI_DEBUG_PRINT((ACPI_DB_IO,
430 "About to write %4.4X to %8.8X%8.8X\n",
432 ACPI_FORMAT_UINT64(acpi_gbl_FADT.
436 status = acpi_hw_register_write(ACPI_REGISTER_PM2_CONTROL,
437 (u8) (register_value));
446 acpi_os_release_lock(acpi_gbl_hardware_lock, lock_flags);
448 /* Normalize the value that was read */
450 ACPI_DEBUG_EXEC(register_value =
451 ((register_value & bit_reg_info->access_bit_mask) >>
452 bit_reg_info->bit_position));
454 ACPI_DEBUG_PRINT((ACPI_DB_IO,
455 "Set bits: %8.8X actual %8.8X register %X\n", value,
456 register_value, bit_reg_info->parent_register));
457 return_ACPI_STATUS(status);
460 ACPI_EXPORT_SYMBOL(acpi_set_register)
462 /******************************************************************************
464 * FUNCTION: acpi_hw_register_read
466 * PARAMETERS: register_id - ACPI Register ID
467 * return_value - Where the register value is returned
469 * RETURN: Status and the value read.
471 * DESCRIPTION: Read from the specified ACPI register
473 ******************************************************************************/
475 acpi_hw_register_read(u32 register_id, u32 * return_value)
481 ACPI_FUNCTION_TRACE(hw_register_read);
483 switch (register_id) {
484 case ACPI_REGISTER_PM1_STATUS: /* 16-bit access */
487 acpi_hw_low_level_read(16, &value1,
488 &acpi_gbl_FADT.xpm1a_event_block);
489 if (ACPI_FAILURE(status)) {
493 /* PM1B is optional */
496 acpi_hw_low_level_read(16, &value2,
497 &acpi_gbl_FADT.xpm1b_event_block);
501 case ACPI_REGISTER_PM1_ENABLE: /* 16-bit access */
504 acpi_hw_low_level_read(16, &value1, &acpi_gbl_xpm1a_enable);
505 if (ACPI_FAILURE(status)) {
509 /* PM1B is optional */
512 acpi_hw_low_level_read(16, &value2, &acpi_gbl_xpm1b_enable);
516 case ACPI_REGISTER_PM1_CONTROL: /* 16-bit access */
519 acpi_hw_low_level_read(16, &value1,
520 &acpi_gbl_FADT.xpm1a_control_block);
521 if (ACPI_FAILURE(status)) {
526 acpi_hw_low_level_read(16, &value2,
527 &acpi_gbl_FADT.xpm1b_control_block);
531 case ACPI_REGISTER_PM2_CONTROL: /* 8-bit access */
534 acpi_hw_low_level_read(8, &value1,
535 &acpi_gbl_FADT.xpm2_control_block);
538 case ACPI_REGISTER_PM_TIMER: /* 32-bit access */
541 acpi_hw_low_level_read(32, &value1,
542 &acpi_gbl_FADT.xpm_timer_block);
545 case ACPI_REGISTER_SMI_COMMAND_BLOCK: /* 8-bit access */
548 acpi_os_read_port(acpi_gbl_FADT.smi_command, &value1, 8);
552 ACPI_ERROR((AE_INFO, "Unknown Register ID: %X", register_id));
553 status = AE_BAD_PARAMETER;
559 if (ACPI_SUCCESS(status)) {
560 *return_value = value1;
563 return_ACPI_STATUS(status);
566 /******************************************************************************
568 * FUNCTION: acpi_hw_register_write
570 * PARAMETERS: register_id - ACPI Register ID
571 * Value - The value to write
575 * DESCRIPTION: Write to the specified ACPI register
577 * NOTE: In accordance with the ACPI specification, this function automatically
578 * preserves the value of the following bits, meaning that these bits cannot be
579 * changed via this interface:
581 * PM1_CONTROL[0] = SCI_EN
586 * 1) Hardware Ignored Bits: When software writes to a register with ignored
587 * bit fields, it preserves the ignored bit fields
588 * 2) SCI_EN: OSPM always preserves this bit position
590 ******************************************************************************/
592 acpi_status acpi_hw_register_write(u32 register_id, u32 value)
597 ACPI_FUNCTION_TRACE(hw_register_write);
599 switch (register_id) {
600 case ACPI_REGISTER_PM1_STATUS: /* 16-bit access */
602 /* Perform a read first to preserve certain bits (per ACPI spec) */
604 status = acpi_hw_register_read(ACPI_REGISTER_PM1_STATUS,
606 if (ACPI_FAILURE(status)) {
610 /* Insert the bits to be preserved */
612 ACPI_INSERT_BITS(value, ACPI_PM1_STATUS_PRESERVED_BITS,
615 /* Now we can write the data */
618 acpi_hw_low_level_write(16, value,
619 &acpi_gbl_FADT.xpm1a_event_block);
620 if (ACPI_FAILURE(status)) {
624 /* PM1B is optional */
627 acpi_hw_low_level_write(16, value,
628 &acpi_gbl_FADT.xpm1b_event_block);
631 case ACPI_REGISTER_PM1_ENABLE: /* 16-bit access */
634 acpi_hw_low_level_write(16, value, &acpi_gbl_xpm1a_enable);
635 if (ACPI_FAILURE(status)) {
639 /* PM1B is optional */
642 acpi_hw_low_level_write(16, value, &acpi_gbl_xpm1b_enable);
645 case ACPI_REGISTER_PM1_CONTROL: /* 16-bit access */
648 * Perform a read first to preserve certain bits (per ACPI spec)
650 status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL,
652 if (ACPI_FAILURE(status)) {
656 /* Insert the bits to be preserved */
658 ACPI_INSERT_BITS(value, ACPI_PM1_CONTROL_PRESERVED_BITS,
661 /* Now we can write the data */
664 acpi_hw_low_level_write(16, value,
665 &acpi_gbl_FADT.xpm1a_control_block);
666 if (ACPI_FAILURE(status)) {
671 acpi_hw_low_level_write(16, value,
672 &acpi_gbl_FADT.xpm1b_control_block);
675 case ACPI_REGISTER_PM1A_CONTROL: /* 16-bit access */
678 acpi_hw_low_level_write(16, value,
679 &acpi_gbl_FADT.xpm1a_control_block);
682 case ACPI_REGISTER_PM1B_CONTROL: /* 16-bit access */
685 acpi_hw_low_level_write(16, value,
686 &acpi_gbl_FADT.xpm1b_control_block);
689 case ACPI_REGISTER_PM2_CONTROL: /* 8-bit access */
692 acpi_hw_low_level_write(8, value,
693 &acpi_gbl_FADT.xpm2_control_block);
696 case ACPI_REGISTER_PM_TIMER: /* 32-bit access */
699 acpi_hw_low_level_write(32, value,
700 &acpi_gbl_FADT.xpm_timer_block);
703 case ACPI_REGISTER_SMI_COMMAND_BLOCK: /* 8-bit access */
705 /* SMI_CMD is currently always in IO space */
708 acpi_os_write_port(acpi_gbl_FADT.smi_command, value, 8);
712 status = AE_BAD_PARAMETER;
717 return_ACPI_STATUS(status);
720 /******************************************************************************
722 * FUNCTION: acpi_hw_low_level_read
724 * PARAMETERS: Width - 8, 16, or 32
725 * Value - Where the value is returned
726 * Reg - GAS register structure
730 * DESCRIPTION: Read from either memory or IO space.
732 ******************************************************************************/
735 acpi_hw_low_level_read(u32 width, u32 * value, struct acpi_generic_address *reg)
740 ACPI_FUNCTION_NAME(hw_low_level_read);
743 * Must have a valid pointer to a GAS structure, and
744 * a non-zero address within. However, don't return an error
745 * because the PM1A/B code must not fail if B isn't present.
751 /* Get a local copy of the address. Handles possible alignment issues */
753 ACPI_MOVE_64_TO_64(&address, ®->address);
760 * Two address spaces supported: Memory or IO.
761 * PCI_Config is not supported here because the GAS struct is insufficient
763 switch (reg->space_id) {
764 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
766 status = acpi_os_read_memory((acpi_physical_address) address,
770 case ACPI_ADR_SPACE_SYSTEM_IO:
773 acpi_os_read_port((acpi_io_address) address, value, width);
778 "Unsupported address space: %X", reg->space_id));
779 return (AE_BAD_PARAMETER);
782 ACPI_DEBUG_PRINT((ACPI_DB_IO,
783 "Read: %8.8X width %2d from %8.8X%8.8X (%s)\n",
784 *value, width, ACPI_FORMAT_UINT64(address),
785 acpi_ut_get_region_name(reg->space_id)));
790 /******************************************************************************
792 * FUNCTION: acpi_hw_low_level_write
794 * PARAMETERS: Width - 8, 16, or 32
795 * Value - To be written
796 * Reg - GAS register structure
800 * DESCRIPTION: Write to either memory or IO space.
802 ******************************************************************************/
805 acpi_hw_low_level_write(u32 width, u32 value, struct acpi_generic_address * reg)
810 ACPI_FUNCTION_NAME(hw_low_level_write);
813 * Must have a valid pointer to a GAS structure, and
814 * a non-zero address within. However, don't return an error
815 * because the PM1A/B code must not fail if B isn't present.
821 /* Get a local copy of the address. Handles possible alignment issues */
823 ACPI_MOVE_64_TO_64(&address, ®->address);
829 * Two address spaces supported: Memory or IO.
830 * PCI_Config is not supported here because the GAS struct is insufficient
832 switch (reg->space_id) {
833 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
835 status = acpi_os_write_memory((acpi_physical_address) address,
839 case ACPI_ADR_SPACE_SYSTEM_IO:
841 status = acpi_os_write_port((acpi_io_address) address, value,
847 "Unsupported address space: %X", reg->space_id));
848 return (AE_BAD_PARAMETER);
851 ACPI_DEBUG_PRINT((ACPI_DB_IO,
852 "Wrote: %8.8X width %2d to %8.8X%8.8X (%s)\n",
853 value, width, ACPI_FORMAT_UINT64(address),
854 acpi_ut_get_region_name(reg->space_id)));