2 /*******************************************************************************
4 * Module Name: hwregs - Read/write access functions for the various ACPI
5 * control and status registers.
7 ******************************************************************************/
10 * Copyright (C) 2000 - 2012, Intel Corp.
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>
51 #define _COMPONENT ACPI_HARDWARE
52 ACPI_MODULE_NAME("hwregs")
54 #if (!ACPI_REDUCED_HARDWARE)
55 /* Local Prototypes */
57 acpi_hw_read_multiple(u32
*value
,
58 struct acpi_generic_address
*register_a
,
59 struct acpi_generic_address
*register_b
);
62 acpi_hw_write_multiple(u32 value
,
63 struct acpi_generic_address
*register_a
,
64 struct acpi_generic_address
*register_b
);
66 #endif /* !ACPI_REDUCED_HARDWARE */
68 /******************************************************************************
70 * FUNCTION: acpi_hw_validate_register
72 * PARAMETERS: reg - GAS register structure
73 * max_bit_width - Max bit_width supported (32 or 64)
74 * address - Pointer to where the gas->address
79 * DESCRIPTION: Validate the contents of a GAS register. Checks the GAS
80 * pointer, Address, space_id, bit_width, and bit_offset.
82 ******************************************************************************/
85 acpi_hw_validate_register(struct acpi_generic_address
*reg
,
86 u8 max_bit_width
, u64
*address
)
89 /* Must have a valid pointer to a GAS structure */
92 return (AE_BAD_PARAMETER
);
96 * Copy the target address. This handles possible alignment issues.
97 * Address must not be null. A null address also indicates an optional
98 * ACPI register that is not supported, so no error message.
100 ACPI_MOVE_64_TO_64(address
, ®
->address
);
102 return (AE_BAD_ADDRESS
);
105 /* Validate the space_ID */
107 if ((reg
->space_id
!= ACPI_ADR_SPACE_SYSTEM_MEMORY
) &&
108 (reg
->space_id
!= ACPI_ADR_SPACE_SYSTEM_IO
)) {
110 "Unsupported address space: 0x%X", reg
->space_id
));
114 /* Validate the bit_width */
116 if ((reg
->bit_width
!= 8) &&
117 (reg
->bit_width
!= 16) &&
118 (reg
->bit_width
!= 32) && (reg
->bit_width
!= max_bit_width
)) {
120 "Unsupported register bit width: 0x%X",
125 /* Validate the bit_offset. Just a warning for now. */
127 if (reg
->bit_offset
!= 0) {
128 ACPI_WARNING((AE_INFO
,
129 "Unsupported register bit offset: 0x%X",
136 /******************************************************************************
138 * FUNCTION: acpi_hw_read
140 * PARAMETERS: value - Where the value is returned
141 * reg - GAS register structure
145 * DESCRIPTION: Read from either memory or IO space. This is a 32-bit max
146 * version of acpi_read, used internally since the overhead of
147 * 64-bit values is not needed.
149 * LIMITATIONS: <These limitations also apply to acpi_hw_write>
150 * bit_width must be exactly 8, 16, or 32.
151 * space_ID must be system_memory or system_IO.
152 * bit_offset and access_width are currently ignored, as there has
153 * not been a need to implement these.
155 ******************************************************************************/
157 acpi_status
acpi_hw_read(u32
*value
, struct acpi_generic_address
*reg
)
163 ACPI_FUNCTION_NAME(hw_read
);
165 /* Validate contents of the GAS register */
167 status
= acpi_hw_validate_register(reg
, 32, &address
);
168 if (ACPI_FAILURE(status
)) {
172 /* Initialize entire 32-bit return value to zero */
177 * Two address spaces supported: Memory or IO. PCI_Config is
178 * not supported here because the GAS structure is insufficient
180 if (reg
->space_id
== ACPI_ADR_SPACE_SYSTEM_MEMORY
) {
181 status
= acpi_os_read_memory((acpi_physical_address
)
182 address
, &value64
, reg
->bit_width
);
184 *value
= (u32
)value64
;
185 } else { /* ACPI_ADR_SPACE_SYSTEM_IO, validated earlier */
187 status
= acpi_hw_read_port((acpi_io_address
)
188 address
, value
, reg
->bit_width
);
191 ACPI_DEBUG_PRINT((ACPI_DB_IO
,
192 "Read: %8.8X width %2d from %8.8X%8.8X (%s)\n",
193 *value
, reg
->bit_width
, ACPI_FORMAT_UINT64(address
),
194 acpi_ut_get_region_name(reg
->space_id
)));
199 /******************************************************************************
201 * FUNCTION: acpi_hw_write
203 * PARAMETERS: value - Value to be written
204 * reg - GAS register structure
208 * DESCRIPTION: Write to either memory or IO space. This is a 32-bit max
209 * version of acpi_write, used internally since the overhead of
210 * 64-bit values is not needed.
212 ******************************************************************************/
214 acpi_status
acpi_hw_write(u32 value
, struct acpi_generic_address
*reg
)
219 ACPI_FUNCTION_NAME(hw_write
);
221 /* Validate contents of the GAS register */
223 status
= acpi_hw_validate_register(reg
, 32, &address
);
224 if (ACPI_FAILURE(status
)) {
229 * Two address spaces supported: Memory or IO. PCI_Config is
230 * not supported here because the GAS structure is insufficient
232 if (reg
->space_id
== ACPI_ADR_SPACE_SYSTEM_MEMORY
) {
233 status
= acpi_os_write_memory((acpi_physical_address
)
236 } else { /* ACPI_ADR_SPACE_SYSTEM_IO, validated earlier */
238 status
= acpi_hw_write_port((acpi_io_address
)
239 address
, value
, reg
->bit_width
);
242 ACPI_DEBUG_PRINT((ACPI_DB_IO
,
243 "Wrote: %8.8X width %2d to %8.8X%8.8X (%s)\n",
244 value
, reg
->bit_width
, ACPI_FORMAT_UINT64(address
),
245 acpi_ut_get_region_name(reg
->space_id
)));
250 #if (!ACPI_REDUCED_HARDWARE)
251 /*******************************************************************************
253 * FUNCTION: acpi_hw_clear_acpi_status
259 * DESCRIPTION: Clears all fixed and general purpose status bits
261 ******************************************************************************/
263 acpi_status
acpi_hw_clear_acpi_status(void)
266 acpi_cpu_flags lock_flags
= 0;
268 ACPI_FUNCTION_TRACE(hw_clear_acpi_status
);
270 ACPI_DEBUG_PRINT((ACPI_DB_IO
, "About to write %04X to %8.8X%8.8X\n",
271 ACPI_BITMASK_ALL_FIXED_STATUS
,
272 ACPI_FORMAT_UINT64(acpi_gbl_xpm1a_status
.address
)));
274 lock_flags
= acpi_os_acquire_lock(acpi_gbl_hardware_lock
);
276 /* Clear the fixed events in PM1 A/B */
278 status
= acpi_hw_register_write(ACPI_REGISTER_PM1_STATUS
,
279 ACPI_BITMASK_ALL_FIXED_STATUS
);
281 acpi_os_release_lock(acpi_gbl_hardware_lock
, lock_flags
);
283 if (ACPI_FAILURE(status
))
286 /* Clear the GPE Bits in all GPE registers in all GPE blocks */
288 status
= acpi_ev_walk_gpe_list(acpi_hw_clear_gpe_block
, NULL
);
291 return_ACPI_STATUS(status
);
294 /*******************************************************************************
296 * FUNCTION: acpi_hw_get_bit_register_info
298 * PARAMETERS: register_id - Index of ACPI Register to access
300 * RETURN: The bitmask to be used when accessing the register
302 * DESCRIPTION: Map register_id into a register bitmask.
304 ******************************************************************************/
306 struct acpi_bit_register_info
*acpi_hw_get_bit_register_info(u32 register_id
)
308 ACPI_FUNCTION_ENTRY();
310 if (register_id
> ACPI_BITREG_MAX
) {
311 ACPI_ERROR((AE_INFO
, "Invalid BitRegister ID: 0x%X",
316 return (&acpi_gbl_bit_register_info
[register_id
]);
319 /******************************************************************************
321 * FUNCTION: acpi_hw_write_pm1_control
323 * PARAMETERS: pm1a_control - Value to be written to PM1A control
324 * pm1b_control - Value to be written to PM1B control
328 * DESCRIPTION: Write the PM1 A/B control registers. These registers are
329 * different than than the PM1 A/B status and enable registers
330 * in that different values can be written to the A/B registers.
331 * Most notably, the SLP_TYP bits can be different, as per the
332 * values returned from the _Sx predefined methods.
334 ******************************************************************************/
336 acpi_status
acpi_hw_write_pm1_control(u32 pm1a_control
, u32 pm1b_control
)
340 ACPI_FUNCTION_TRACE(hw_write_pm1_control
);
343 acpi_hw_write(pm1a_control
, &acpi_gbl_FADT
.xpm1a_control_block
);
344 if (ACPI_FAILURE(status
)) {
345 return_ACPI_STATUS(status
);
348 if (acpi_gbl_FADT
.xpm1b_control_block
.address
) {
350 acpi_hw_write(pm1b_control
,
351 &acpi_gbl_FADT
.xpm1b_control_block
);
353 return_ACPI_STATUS(status
);
356 /******************************************************************************
358 * FUNCTION: acpi_hw_register_read
360 * PARAMETERS: register_id - ACPI Register ID
361 * return_value - Where the register value is returned
363 * RETURN: Status and the value read.
365 * DESCRIPTION: Read from the specified ACPI register
367 ******************************************************************************/
369 acpi_hw_register_read(u32 register_id
, u32
* return_value
)
374 ACPI_FUNCTION_TRACE(hw_register_read
);
376 switch (register_id
) {
377 case ACPI_REGISTER_PM1_STATUS
: /* PM1 A/B: 16-bit access each */
379 status
= acpi_hw_read_multiple(&value
,
380 &acpi_gbl_xpm1a_status
,
381 &acpi_gbl_xpm1b_status
);
384 case ACPI_REGISTER_PM1_ENABLE
: /* PM1 A/B: 16-bit access each */
386 status
= acpi_hw_read_multiple(&value
,
387 &acpi_gbl_xpm1a_enable
,
388 &acpi_gbl_xpm1b_enable
);
391 case ACPI_REGISTER_PM1_CONTROL
: /* PM1 A/B: 16-bit access each */
393 status
= acpi_hw_read_multiple(&value
,
397 xpm1b_control_block
);
400 * Zero the write-only bits. From the ACPI specification, "Hardware
401 * Write-Only Bits": "Upon reads to registers with write-only bits,
402 * software masks out all write-only bits."
404 value
&= ~ACPI_PM1_CONTROL_WRITEONLY_BITS
;
407 case ACPI_REGISTER_PM2_CONTROL
: /* 8-bit access */
410 acpi_hw_read(&value
, &acpi_gbl_FADT
.xpm2_control_block
);
413 case ACPI_REGISTER_PM_TIMER
: /* 32-bit access */
415 status
= acpi_hw_read(&value
, &acpi_gbl_FADT
.xpm_timer_block
);
418 case ACPI_REGISTER_SMI_COMMAND_BLOCK
: /* 8-bit access */
421 acpi_hw_read_port(acpi_gbl_FADT
.smi_command
, &value
, 8);
425 ACPI_ERROR((AE_INFO
, "Unknown Register ID: 0x%X", register_id
));
426 status
= AE_BAD_PARAMETER
;
430 if (ACPI_SUCCESS(status
)) {
431 *return_value
= value
;
434 return_ACPI_STATUS(status
);
437 /******************************************************************************
439 * FUNCTION: acpi_hw_register_write
441 * PARAMETERS: register_id - ACPI Register ID
442 * value - The value to write
446 * DESCRIPTION: Write to the specified ACPI register
448 * NOTE: In accordance with the ACPI specification, this function automatically
449 * preserves the value of the following bits, meaning that these bits cannot be
450 * changed via this interface:
452 * PM1_CONTROL[0] = SCI_EN
457 * 1) Hardware Ignored Bits: When software writes to a register with ignored
458 * bit fields, it preserves the ignored bit fields
459 * 2) SCI_EN: OSPM always preserves this bit position
461 ******************************************************************************/
463 acpi_status
acpi_hw_register_write(u32 register_id
, u32 value
)
468 ACPI_FUNCTION_TRACE(hw_register_write
);
470 switch (register_id
) {
471 case ACPI_REGISTER_PM1_STATUS
: /* PM1 A/B: 16-bit access each */
473 * Handle the "ignored" bit in PM1 Status. According to the ACPI
474 * specification, ignored bits are to be preserved when writing.
475 * Normally, this would mean a read/modify/write sequence. However,
476 * preserving a bit in the status register is different. Writing a
477 * one clears the status, and writing a zero preserves the status.
478 * Therefore, we must always write zero to the ignored bit.
480 * This behavior is clarified in the ACPI 4.0 specification.
482 value
&= ~ACPI_PM1_STATUS_PRESERVED_BITS
;
484 status
= acpi_hw_write_multiple(value
,
485 &acpi_gbl_xpm1a_status
,
486 &acpi_gbl_xpm1b_status
);
489 case ACPI_REGISTER_PM1_ENABLE
: /* PM1 A/B: 16-bit access */
491 status
= acpi_hw_write_multiple(value
,
492 &acpi_gbl_xpm1a_enable
,
493 &acpi_gbl_xpm1b_enable
);
496 case ACPI_REGISTER_PM1_CONTROL
: /* PM1 A/B: 16-bit access each */
499 * Perform a read first to preserve certain bits (per ACPI spec)
500 * Note: This includes SCI_EN, we never want to change this bit
502 status
= acpi_hw_read_multiple(&read_value
,
506 xpm1b_control_block
);
507 if (ACPI_FAILURE(status
)) {
511 /* Insert the bits to be preserved */
513 ACPI_INSERT_BITS(value
, ACPI_PM1_CONTROL_PRESERVED_BITS
,
516 /* Now we can write the data */
518 status
= acpi_hw_write_multiple(value
,
522 xpm1b_control_block
);
525 case ACPI_REGISTER_PM2_CONTROL
: /* 8-bit access */
528 * For control registers, all reserved bits must be preserved,
529 * as per the ACPI spec.
532 acpi_hw_read(&read_value
,
533 &acpi_gbl_FADT
.xpm2_control_block
);
534 if (ACPI_FAILURE(status
)) {
538 /* Insert the bits to be preserved */
540 ACPI_INSERT_BITS(value
, ACPI_PM2_CONTROL_PRESERVED_BITS
,
544 acpi_hw_write(value
, &acpi_gbl_FADT
.xpm2_control_block
);
547 case ACPI_REGISTER_PM_TIMER
: /* 32-bit access */
549 status
= acpi_hw_write(value
, &acpi_gbl_FADT
.xpm_timer_block
);
552 case ACPI_REGISTER_SMI_COMMAND_BLOCK
: /* 8-bit access */
554 /* SMI_CMD is currently always in IO space */
557 acpi_hw_write_port(acpi_gbl_FADT
.smi_command
, value
, 8);
561 ACPI_ERROR((AE_INFO
, "Unknown Register ID: 0x%X", register_id
));
562 status
= AE_BAD_PARAMETER
;
567 return_ACPI_STATUS(status
);
570 /******************************************************************************
572 * FUNCTION: acpi_hw_read_multiple
574 * PARAMETERS: value - Where the register value is returned
575 * register_a - First ACPI register (required)
576 * register_b - Second ACPI register (optional)
580 * DESCRIPTION: Read from the specified two-part ACPI register (such as PM1 A/B)
582 ******************************************************************************/
585 acpi_hw_read_multiple(u32
*value
,
586 struct acpi_generic_address
*register_a
,
587 struct acpi_generic_address
*register_b
)
593 /* The first register is always required */
595 status
= acpi_hw_read(&value_a
, register_a
);
596 if (ACPI_FAILURE(status
)) {
600 /* Second register is optional */
602 if (register_b
->address
) {
603 status
= acpi_hw_read(&value_b
, register_b
);
604 if (ACPI_FAILURE(status
)) {
610 * OR the two return values together. No shifting or masking is necessary,
611 * because of how the PM1 registers are defined in the ACPI specification:
613 * "Although the bits can be split between the two register blocks (each
614 * register block has a unique pointer within the FADT), the bit positions
615 * are maintained. The register block with unimplemented bits (that is,
616 * those implemented in the other register block) always returns zeros,
617 * and writes have no side effects"
619 *value
= (value_a
| value_b
);
623 /******************************************************************************
625 * FUNCTION: acpi_hw_write_multiple
627 * PARAMETERS: value - The value to write
628 * register_a - First ACPI register (required)
629 * register_b - Second ACPI register (optional)
633 * DESCRIPTION: Write to the specified two-part ACPI register (such as PM1 A/B)
635 ******************************************************************************/
638 acpi_hw_write_multiple(u32 value
,
639 struct acpi_generic_address
*register_a
,
640 struct acpi_generic_address
*register_b
)
644 /* The first register is always required */
646 status
= acpi_hw_write(value
, register_a
);
647 if (ACPI_FAILURE(status
)) {
652 * Second register is optional
654 * No bit shifting or clearing is necessary, because of how the PM1
655 * registers are defined in the ACPI specification:
657 * "Although the bits can be split between the two register blocks (each
658 * register block has a unique pointer within the FADT), the bit positions
659 * are maintained. The register block with unimplemented bits (that is,
660 * those implemented in the other register block) always returns zeros,
661 * and writes have no side effects"
663 if (register_b
->address
) {
664 status
= acpi_hw_write(value
, register_b
);
670 #endif /* !ACPI_REDUCED_HARDWARE */