1 /*******************************************************************************
3 * Module Name: hwregs - Read/write access functions for the various ACPI
4 * control and status registers.
6 ******************************************************************************/
9 * Copyright (C) 2000 - 2016, 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
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.
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>
49 #define _COMPONENT ACPI_HARDWARE
50 ACPI_MODULE_NAME("hwregs")
52 #if (!ACPI_REDUCED_HARDWARE)
53 /* Local Prototypes */
55 acpi_hw_get_access_bit_width(struct acpi_generic_address
*reg
,
59 acpi_hw_read_multiple(u32
*value
,
60 struct acpi_generic_address
*register_a
,
61 struct acpi_generic_address
*register_b
);
64 acpi_hw_write_multiple(u32 value
,
65 struct acpi_generic_address
*register_a
,
66 struct acpi_generic_address
*register_b
);
68 #endif /* !ACPI_REDUCED_HARDWARE */
70 /******************************************************************************
72 * FUNCTION: acpi_hw_get_access_bit_width
74 * PARAMETERS: reg - GAS register structure
75 * max_bit_width - Max bit_width supported (32 or 64)
79 * DESCRIPTION: Obtain optimal access bit width
81 ******************************************************************************/
84 acpi_hw_get_access_bit_width(struct acpi_generic_address
*reg
, u8 max_bit_width
)
86 if (!reg
->access_width
) {
87 if (reg
->space_id
== ACPI_ADR_SPACE_SYSTEM_IO
) {
92 * Detect old register descriptors where only the bit_width field
95 if (reg
->bit_width
< max_bit_width
&&
96 !reg
->bit_offset
&& reg
->bit_width
&&
97 ACPI_IS_POWER_OF_TWO(reg
->bit_width
) &&
98 ACPI_IS_ALIGNED(reg
->bit_width
, 8)) {
99 return (reg
->bit_width
);
101 return (max_bit_width
);
103 return (1 << (reg
->access_width
+ 2));
107 /******************************************************************************
109 * FUNCTION: acpi_hw_validate_register
111 * PARAMETERS: reg - GAS register structure
112 * max_bit_width - Max bit_width supported (32 or 64)
113 * address - Pointer to where the gas->address
118 * DESCRIPTION: Validate the contents of a GAS register. Checks the GAS
119 * pointer, Address, space_id, bit_width, and bit_offset.
121 ******************************************************************************/
124 acpi_hw_validate_register(struct acpi_generic_address
*reg
,
125 u8 max_bit_width
, u64
*address
)
130 /* Must have a valid pointer to a GAS structure */
133 return (AE_BAD_PARAMETER
);
137 * Copy the target address. This handles possible alignment issues.
138 * Address must not be null. A null address also indicates an optional
139 * ACPI register that is not supported, so no error message.
141 ACPI_MOVE_64_TO_64(address
, ®
->address
);
143 return (AE_BAD_ADDRESS
);
146 /* Validate the space_ID */
148 if ((reg
->space_id
!= ACPI_ADR_SPACE_SYSTEM_MEMORY
) &&
149 (reg
->space_id
!= ACPI_ADR_SPACE_SYSTEM_IO
)) {
151 "Unsupported address space: 0x%X", reg
->space_id
));
155 /* Validate the access_width */
157 if (reg
->access_width
> 4) {
159 "Unsupported register access width: 0x%X",
164 /* Validate the bit_width, convert access_width into number of bits */
166 access_width
= acpi_hw_get_access_bit_width(reg
, max_bit_width
);
168 ACPI_ROUND_UP(reg
->bit_offset
+ reg
->bit_width
, access_width
);
169 if (max_bit_width
< bit_width
) {
170 ACPI_WARNING((AE_INFO
,
171 "Requested bit width 0x%X is smaller than register bit width 0x%X",
172 max_bit_width
, bit_width
));
179 /******************************************************************************
181 * FUNCTION: acpi_hw_read
183 * PARAMETERS: value - Where the value is returned
184 * reg - GAS register structure
188 * DESCRIPTION: Read from either memory or IO space. This is a 32-bit max
189 * version of acpi_read, used internally since the overhead of
190 * 64-bit values is not needed.
192 * LIMITATIONS: <These limitations also apply to acpi_hw_write>
193 * space_ID must be system_memory or system_IO.
195 ******************************************************************************/
197 acpi_status
acpi_hw_read(u32
*value
, struct acpi_generic_address
*reg
)
208 ACPI_FUNCTION_NAME(hw_read
);
210 /* Validate contents of the GAS register */
212 status
= acpi_hw_validate_register(reg
, 32, &address
);
213 if (ACPI_FAILURE(status
)) {
218 * Initialize entire 32-bit return value to zero, convert access_width
219 * into number of bits based
222 access_width
= acpi_hw_get_access_bit_width(reg
, 32);
223 bit_width
= reg
->bit_offset
+ reg
->bit_width
;
224 bit_offset
= reg
->bit_offset
;
227 * Two address spaces supported: Memory or IO. PCI_Config is
228 * not supported here because the GAS structure is insufficient
232 if (bit_offset
>= access_width
) {
234 bit_offset
-= access_width
;
236 if (reg
->space_id
== ACPI_ADR_SPACE_SYSTEM_MEMORY
) {
238 acpi_os_read_memory((acpi_physical_address
)
243 &value64
, access_width
);
244 value32
= (u32
)value64
;
245 } else { /* ACPI_ADR_SPACE_SYSTEM_IO, validated earlier */
247 status
= acpi_hw_read_port((acpi_io_address
)
258 * Use offset style bit writes because "Index * AccessWidth" is
259 * ensured to be less than 32-bits by acpi_hw_validate_register().
261 ACPI_SET_BITS(value
, index
* access_width
,
262 ACPI_MASK_BITS_ABOVE_32(access_width
), value32
);
265 bit_width
> access_width
? access_width
: bit_width
;
269 ACPI_DEBUG_PRINT((ACPI_DB_IO
,
270 "Read: %8.8X width %2d from %8.8X%8.8X (%s)\n",
271 *value
, access_width
, ACPI_FORMAT_UINT64(address
),
272 acpi_ut_get_region_name(reg
->space_id
)));
277 /******************************************************************************
279 * FUNCTION: acpi_hw_write
281 * PARAMETERS: value - Value to be written
282 * reg - GAS register structure
286 * DESCRIPTION: Write to either memory or IO space. This is a 32-bit max
287 * version of acpi_write, used internally since the overhead of
288 * 64-bit values is not needed.
290 ******************************************************************************/
292 acpi_status
acpi_hw_write(u32 value
, struct acpi_generic_address
*reg
)
303 ACPI_FUNCTION_NAME(hw_write
);
305 /* Validate contents of the GAS register */
307 status
= acpi_hw_validate_register(reg
, 32, &address
);
308 if (ACPI_FAILURE(status
)) {
312 /* Convert access_width into number of bits based */
314 access_width
= acpi_hw_get_access_bit_width(reg
, 32);
315 bit_width
= reg
->bit_offset
+ reg
->bit_width
;
316 bit_offset
= reg
->bit_offset
;
319 * Two address spaces supported: Memory or IO. PCI_Config is
320 * not supported here because the GAS structure is insufficient
325 * Use offset style bit reads because "Index * AccessWidth" is
326 * ensured to be less than 32-bits by acpi_hw_validate_register().
328 value32
= ACPI_GET_BITS(&value
, index
* access_width
,
329 ACPI_MASK_BITS_ABOVE_32(access_width
));
331 if (bit_offset
>= access_width
) {
332 bit_offset
-= access_width
;
334 if (reg
->space_id
== ACPI_ADR_SPACE_SYSTEM_MEMORY
) {
335 value64
= (u64
)value32
;
337 acpi_os_write_memory((acpi_physical_address
)
342 value64
, access_width
);
343 } else { /* ACPI_ADR_SPACE_SYSTEM_IO, validated earlier */
345 status
= acpi_hw_write_port((acpi_io_address
)
356 * Index * access_width is ensured to be less than 32-bits by
357 * acpi_hw_validate_register().
360 bit_width
> access_width
? access_width
: bit_width
;
364 ACPI_DEBUG_PRINT((ACPI_DB_IO
,
365 "Wrote: %8.8X width %2d to %8.8X%8.8X (%s)\n",
366 value
, access_width
, ACPI_FORMAT_UINT64(address
),
367 acpi_ut_get_region_name(reg
->space_id
)));
372 #if (!ACPI_REDUCED_HARDWARE)
373 /*******************************************************************************
375 * FUNCTION: acpi_hw_clear_acpi_status
381 * DESCRIPTION: Clears all fixed and general purpose status bits
383 ******************************************************************************/
385 acpi_status
acpi_hw_clear_acpi_status(void)
388 acpi_cpu_flags lock_flags
= 0;
390 ACPI_FUNCTION_TRACE(hw_clear_acpi_status
);
392 ACPI_DEBUG_PRINT((ACPI_DB_IO
, "About to write %04X to %8.8X%8.8X\n",
393 ACPI_BITMASK_ALL_FIXED_STATUS
,
394 ACPI_FORMAT_UINT64(acpi_gbl_xpm1a_status
.address
)));
396 lock_flags
= acpi_os_acquire_lock(acpi_gbl_hardware_lock
);
398 /* Clear the fixed events in PM1 A/B */
400 status
= acpi_hw_register_write(ACPI_REGISTER_PM1_STATUS
,
401 ACPI_BITMASK_ALL_FIXED_STATUS
);
403 acpi_os_release_lock(acpi_gbl_hardware_lock
, lock_flags
);
405 if (ACPI_FAILURE(status
)) {
409 /* Clear the GPE Bits in all GPE registers in all GPE blocks */
411 status
= acpi_ev_walk_gpe_list(acpi_hw_clear_gpe_block
, NULL
);
414 return_ACPI_STATUS(status
);
417 /*******************************************************************************
419 * FUNCTION: acpi_hw_get_bit_register_info
421 * PARAMETERS: register_id - Index of ACPI Register to access
423 * RETURN: The bitmask to be used when accessing the register
425 * DESCRIPTION: Map register_id into a register bitmask.
427 ******************************************************************************/
429 struct acpi_bit_register_info
*acpi_hw_get_bit_register_info(u32 register_id
)
431 ACPI_FUNCTION_ENTRY();
433 if (register_id
> ACPI_BITREG_MAX
) {
434 ACPI_ERROR((AE_INFO
, "Invalid BitRegister ID: 0x%X",
439 return (&acpi_gbl_bit_register_info
[register_id
]);
442 /******************************************************************************
444 * FUNCTION: acpi_hw_write_pm1_control
446 * PARAMETERS: pm1a_control - Value to be written to PM1A control
447 * pm1b_control - Value to be written to PM1B control
451 * DESCRIPTION: Write the PM1 A/B control registers. These registers are
452 * different than than the PM1 A/B status and enable registers
453 * in that different values can be written to the A/B registers.
454 * Most notably, the SLP_TYP bits can be different, as per the
455 * values returned from the _Sx predefined methods.
457 ******************************************************************************/
459 acpi_status
acpi_hw_write_pm1_control(u32 pm1a_control
, u32 pm1b_control
)
463 ACPI_FUNCTION_TRACE(hw_write_pm1_control
);
466 acpi_hw_write(pm1a_control
, &acpi_gbl_FADT
.xpm1a_control_block
);
467 if (ACPI_FAILURE(status
)) {
468 return_ACPI_STATUS(status
);
471 if (acpi_gbl_FADT
.xpm1b_control_block
.address
) {
473 acpi_hw_write(pm1b_control
,
474 &acpi_gbl_FADT
.xpm1b_control_block
);
476 return_ACPI_STATUS(status
);
479 /******************************************************************************
481 * FUNCTION: acpi_hw_register_read
483 * PARAMETERS: register_id - ACPI Register ID
484 * return_value - Where the register value is returned
486 * RETURN: Status and the value read.
488 * DESCRIPTION: Read from the specified ACPI register
490 ******************************************************************************/
491 acpi_status
acpi_hw_register_read(u32 register_id
, u32
*return_value
)
496 ACPI_FUNCTION_TRACE(hw_register_read
);
498 switch (register_id
) {
499 case ACPI_REGISTER_PM1_STATUS
: /* PM1 A/B: 16-bit access each */
501 status
= acpi_hw_read_multiple(&value
,
502 &acpi_gbl_xpm1a_status
,
503 &acpi_gbl_xpm1b_status
);
506 case ACPI_REGISTER_PM1_ENABLE
: /* PM1 A/B: 16-bit access each */
508 status
= acpi_hw_read_multiple(&value
,
509 &acpi_gbl_xpm1a_enable
,
510 &acpi_gbl_xpm1b_enable
);
513 case ACPI_REGISTER_PM1_CONTROL
: /* PM1 A/B: 16-bit access each */
515 status
= acpi_hw_read_multiple(&value
,
519 xpm1b_control_block
);
522 * Zero the write-only bits. From the ACPI specification, "Hardware
523 * Write-Only Bits": "Upon reads to registers with write-only bits,
524 * software masks out all write-only bits."
526 value
&= ~ACPI_PM1_CONTROL_WRITEONLY_BITS
;
529 case ACPI_REGISTER_PM2_CONTROL
: /* 8-bit access */
532 acpi_hw_read(&value
, &acpi_gbl_FADT
.xpm2_control_block
);
535 case ACPI_REGISTER_PM_TIMER
: /* 32-bit access */
537 status
= acpi_hw_read(&value
, &acpi_gbl_FADT
.xpm_timer_block
);
540 case ACPI_REGISTER_SMI_COMMAND_BLOCK
: /* 8-bit access */
543 acpi_hw_read_port(acpi_gbl_FADT
.smi_command
, &value
, 8);
548 ACPI_ERROR((AE_INFO
, "Unknown Register ID: 0x%X", register_id
));
549 status
= AE_BAD_PARAMETER
;
553 if (ACPI_SUCCESS(status
)) {
554 *return_value
= value
;
557 return_ACPI_STATUS(status
);
560 /******************************************************************************
562 * FUNCTION: acpi_hw_register_write
564 * PARAMETERS: register_id - ACPI Register ID
565 * value - The value to write
569 * DESCRIPTION: Write to the specified ACPI register
571 * NOTE: In accordance with the ACPI specification, this function automatically
572 * preserves the value of the following bits, meaning that these bits cannot be
573 * changed via this interface:
575 * PM1_CONTROL[0] = SCI_EN
580 * 1) Hardware Ignored Bits: When software writes to a register with ignored
581 * bit fields, it preserves the ignored bit fields
582 * 2) SCI_EN: OSPM always preserves this bit position
584 ******************************************************************************/
586 acpi_status
acpi_hw_register_write(u32 register_id
, u32 value
)
591 ACPI_FUNCTION_TRACE(hw_register_write
);
593 switch (register_id
) {
594 case ACPI_REGISTER_PM1_STATUS
: /* PM1 A/B: 16-bit access each */
596 * Handle the "ignored" bit in PM1 Status. According to the ACPI
597 * specification, ignored bits are to be preserved when writing.
598 * Normally, this would mean a read/modify/write sequence. However,
599 * preserving a bit in the status register is different. Writing a
600 * one clears the status, and writing a zero preserves the status.
601 * Therefore, we must always write zero to the ignored bit.
603 * This behavior is clarified in the ACPI 4.0 specification.
605 value
&= ~ACPI_PM1_STATUS_PRESERVED_BITS
;
607 status
= acpi_hw_write_multiple(value
,
608 &acpi_gbl_xpm1a_status
,
609 &acpi_gbl_xpm1b_status
);
612 case ACPI_REGISTER_PM1_ENABLE
: /* PM1 A/B: 16-bit access each */
614 status
= acpi_hw_write_multiple(value
,
615 &acpi_gbl_xpm1a_enable
,
616 &acpi_gbl_xpm1b_enable
);
619 case ACPI_REGISTER_PM1_CONTROL
: /* PM1 A/B: 16-bit access each */
621 * Perform a read first to preserve certain bits (per ACPI spec)
622 * Note: This includes SCI_EN, we never want to change this bit
624 status
= acpi_hw_read_multiple(&read_value
,
628 xpm1b_control_block
);
629 if (ACPI_FAILURE(status
)) {
633 /* Insert the bits to be preserved */
635 ACPI_INSERT_BITS(value
, ACPI_PM1_CONTROL_PRESERVED_BITS
,
638 /* Now we can write the data */
640 status
= acpi_hw_write_multiple(value
,
644 xpm1b_control_block
);
647 case ACPI_REGISTER_PM2_CONTROL
: /* 8-bit access */
649 * For control registers, all reserved bits must be preserved,
650 * as per the ACPI spec.
653 acpi_hw_read(&read_value
,
654 &acpi_gbl_FADT
.xpm2_control_block
);
655 if (ACPI_FAILURE(status
)) {
659 /* Insert the bits to be preserved */
661 ACPI_INSERT_BITS(value
, ACPI_PM2_CONTROL_PRESERVED_BITS
,
665 acpi_hw_write(value
, &acpi_gbl_FADT
.xpm2_control_block
);
668 case ACPI_REGISTER_PM_TIMER
: /* 32-bit access */
670 status
= acpi_hw_write(value
, &acpi_gbl_FADT
.xpm_timer_block
);
673 case ACPI_REGISTER_SMI_COMMAND_BLOCK
: /* 8-bit access */
675 /* SMI_CMD is currently always in IO space */
678 acpi_hw_write_port(acpi_gbl_FADT
.smi_command
, value
, 8);
683 ACPI_ERROR((AE_INFO
, "Unknown Register ID: 0x%X", register_id
));
684 status
= AE_BAD_PARAMETER
;
689 return_ACPI_STATUS(status
);
692 /******************************************************************************
694 * FUNCTION: acpi_hw_read_multiple
696 * PARAMETERS: value - Where the register value is returned
697 * register_a - First ACPI register (required)
698 * register_b - Second ACPI register (optional)
702 * DESCRIPTION: Read from the specified two-part ACPI register (such as PM1 A/B)
704 ******************************************************************************/
707 acpi_hw_read_multiple(u32
*value
,
708 struct acpi_generic_address
*register_a
,
709 struct acpi_generic_address
*register_b
)
715 /* The first register is always required */
717 status
= acpi_hw_read(&value_a
, register_a
);
718 if (ACPI_FAILURE(status
)) {
722 /* Second register is optional */
724 if (register_b
->address
) {
725 status
= acpi_hw_read(&value_b
, register_b
);
726 if (ACPI_FAILURE(status
)) {
732 * OR the two return values together. No shifting or masking is necessary,
733 * because of how the PM1 registers are defined in the ACPI specification:
735 * "Although the bits can be split between the two register blocks (each
736 * register block has a unique pointer within the FADT), the bit positions
737 * are maintained. The register block with unimplemented bits (that is,
738 * those implemented in the other register block) always returns zeros,
739 * and writes have no side effects"
741 *value
= (value_a
| value_b
);
745 /******************************************************************************
747 * FUNCTION: acpi_hw_write_multiple
749 * PARAMETERS: value - The value to write
750 * register_a - First ACPI register (required)
751 * register_b - Second ACPI register (optional)
755 * DESCRIPTION: Write to the specified two-part ACPI register (such as PM1 A/B)
757 ******************************************************************************/
760 acpi_hw_write_multiple(u32 value
,
761 struct acpi_generic_address
*register_a
,
762 struct acpi_generic_address
*register_b
)
766 /* The first register is always required */
768 status
= acpi_hw_write(value
, register_a
);
769 if (ACPI_FAILURE(status
)) {
774 * Second register is optional
776 * No bit shifting or clearing is necessary, because of how the PM1
777 * registers are defined in the ACPI specification:
779 * "Although the bits can be split between the two register blocks (each
780 * register block has a unique pointer within the FADT), the bit positions
781 * are maintained. The register block with unimplemented bits (that is,
782 * those implemented in the other register block) always returns zeros,
783 * and writes have no side effects"
785 if (register_b
->address
) {
786 status
= acpi_hw_write(value
, register_b
);
792 #endif /* !ACPI_REDUCED_HARDWARE */