1 /*******************************************************************************
3 * Module Name: hwregs - Read/write access functions for the various ACPI
4 * control and status registers.
6 ******************************************************************************/
9 * Copyright (C) 2000 - 2014, 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.
51 #define _COMPONENT ACPI_HARDWARE
52 ACPI_MODULE_NAME ("hwregs")
55 #if (!ACPI_REDUCED_HARDWARE)
57 /* Local Prototypes */
62 ACPI_GENERIC_ADDRESS *RegisterA,
63 ACPI_GENERIC_ADDRESS *RegisterB);
68 ACPI_GENERIC_ADDRESS *RegisterA,
69 ACPI_GENERIC_ADDRESS *RegisterB);
71 #endif /* !ACPI_REDUCED_HARDWARE */
73 /******************************************************************************
75 * FUNCTION: AcpiHwValidateRegister
77 * PARAMETERS: Reg - GAS register structure
78 * MaxBitWidth - Max BitWidth supported (32 or 64)
79 * Address - Pointer to where the gas->address
84 * DESCRIPTION: Validate the contents of a GAS register. Checks the GAS
85 * pointer, Address, SpaceId, BitWidth, and BitOffset.
87 ******************************************************************************/
90 AcpiHwValidateRegister (
91 ACPI_GENERIC_ADDRESS *Reg,
96 /* Must have a valid pointer to a GAS structure */
100 return (AE_BAD_PARAMETER);
104 * Copy the target address. This handles possible alignment issues.
105 * Address must not be null. A null address also indicates an optional
106 * ACPI register that is not supported, so no error message.
108 ACPI_MOVE_64_TO_64 (Address, &Reg->Address);
111 return (AE_BAD_ADDRESS);
114 /* Validate the SpaceID */
116 if ((Reg->SpaceId != ACPI_ADR_SPACE_SYSTEM_MEMORY) &&
117 (Reg->SpaceId != ACPI_ADR_SPACE_SYSTEM_IO))
119 ACPI_ERROR ((AE_INFO,
120 "Unsupported address space: 0x%X", Reg->SpaceId));
124 /* Validate the BitWidth */
126 if ((Reg->BitWidth != 8) &&
127 (Reg->BitWidth != 16) &&
128 (Reg->BitWidth != 32) &&
129 (Reg->BitWidth != MaxBitWidth))
131 ACPI_ERROR ((AE_INFO,
132 "Unsupported register bit width: 0x%X", Reg->BitWidth));
136 /* Validate the BitOffset. Just a warning for now. */
138 if (Reg->BitOffset != 0)
140 ACPI_WARNING ((AE_INFO,
141 "Unsupported register bit offset: 0x%X", Reg->BitOffset));
148 /******************************************************************************
150 * FUNCTION: AcpiHwRead
152 * PARAMETERS: Value - Where the value is returned
153 * Reg - GAS register structure
157 * DESCRIPTION: Read from either memory or IO space. This is a 32-bit max
158 * version of AcpiRead, used internally since the overhead of
159 * 64-bit values is not needed.
161 * LIMITATIONS: <These limitations also apply to AcpiHwWrite>
162 * BitWidth must be exactly 8, 16, or 32.
163 * SpaceID must be SystemMemory or SystemIO.
164 * BitOffset and AccessWidth are currently ignored, as there has
165 * not been a need to implement these.
167 ******************************************************************************/
172 ACPI_GENERIC_ADDRESS *Reg)
179 ACPI_FUNCTION_NAME (HwRead);
182 /* Validate contents of the GAS register */
184 Status = AcpiHwValidateRegister (Reg, 32, &Address);
185 if (ACPI_FAILURE (Status))
190 /* Initialize entire 32-bit return value to zero */
195 * Two address spaces supported: Memory or IO. PCI_Config is
196 * not supported here because the GAS structure is insufficient
198 if (Reg->SpaceId == ACPI_ADR_SPACE_SYSTEM_MEMORY)
200 Status = AcpiOsReadMemory ((ACPI_PHYSICAL_ADDRESS)
201 Address, &Value64, Reg->BitWidth);
203 *Value = (UINT32) Value64;
205 else /* ACPI_ADR_SPACE_SYSTEM_IO, validated earlier */
207 Status = AcpiHwReadPort ((ACPI_IO_ADDRESS)
208 Address, Value, Reg->BitWidth);
211 ACPI_DEBUG_PRINT ((ACPI_DB_IO,
212 "Read: %8.8X width %2d from %8.8X%8.8X (%s)\n",
213 *Value, Reg->BitWidth, ACPI_FORMAT_UINT64 (Address),
214 AcpiUtGetRegionName (Reg->SpaceId)));
220 /******************************************************************************
222 * FUNCTION: AcpiHwWrite
224 * PARAMETERS: Value - Value to be written
225 * Reg - GAS register structure
229 * DESCRIPTION: Write to either memory or IO space. This is a 32-bit max
230 * version of AcpiWrite, used internally since the overhead of
231 * 64-bit values is not needed.
233 ******************************************************************************/
238 ACPI_GENERIC_ADDRESS *Reg)
244 ACPI_FUNCTION_NAME (HwWrite);
247 /* Validate contents of the GAS register */
249 Status = AcpiHwValidateRegister (Reg, 32, &Address);
250 if (ACPI_FAILURE (Status))
256 * Two address spaces supported: Memory or IO. PCI_Config is
257 * not supported here because the GAS structure is insufficient
259 if (Reg->SpaceId == ACPI_ADR_SPACE_SYSTEM_MEMORY)
261 Status = AcpiOsWriteMemory ((ACPI_PHYSICAL_ADDRESS)
262 Address, (UINT64) Value, Reg->BitWidth);
264 else /* ACPI_ADR_SPACE_SYSTEM_IO, validated earlier */
266 Status = AcpiHwWritePort ((ACPI_IO_ADDRESS)
267 Address, Value, Reg->BitWidth);
270 ACPI_DEBUG_PRINT ((ACPI_DB_IO,
271 "Wrote: %8.8X width %2d to %8.8X%8.8X (%s)\n",
272 Value, Reg->BitWidth, ACPI_FORMAT_UINT64 (Address),
273 AcpiUtGetRegionName (Reg->SpaceId)));
279 #if (!ACPI_REDUCED_HARDWARE)
280 /*******************************************************************************
282 * FUNCTION: AcpiHwClearAcpiStatus
288 * DESCRIPTION: Clears all fixed and general purpose status bits
290 ******************************************************************************/
293 AcpiHwClearAcpiStatus (
297 ACPI_CPU_FLAGS LockFlags = 0;
300 ACPI_FUNCTION_TRACE (HwClearAcpiStatus);
303 ACPI_DEBUG_PRINT ((ACPI_DB_IO, "About to write %04X to %8.8X%8.8X\n",
304 ACPI_BITMASK_ALL_FIXED_STATUS,
305 ACPI_FORMAT_UINT64 (AcpiGbl_XPm1aStatus.Address)));
307 LockFlags = AcpiOsAcquireLock (AcpiGbl_HardwareLock);
309 /* Clear the fixed events in PM1 A/B */
311 Status = AcpiHwRegisterWrite (ACPI_REGISTER_PM1_STATUS,
312 ACPI_BITMASK_ALL_FIXED_STATUS);
314 AcpiOsReleaseLock (AcpiGbl_HardwareLock, LockFlags);
316 if (ACPI_FAILURE (Status))
321 /* Clear the GPE Bits in all GPE registers in all GPE blocks */
323 Status = AcpiEvWalkGpeList (AcpiHwClearGpeBlock, NULL);
326 return_ACPI_STATUS (Status);
330 /*******************************************************************************
332 * FUNCTION: AcpiHwGetBitRegisterInfo
334 * PARAMETERS: RegisterId - Index of ACPI Register to access
336 * RETURN: The bitmask to be used when accessing the register
338 * DESCRIPTION: Map RegisterId into a register bitmask.
340 ******************************************************************************/
342 ACPI_BIT_REGISTER_INFO *
343 AcpiHwGetBitRegisterInfo (
346 ACPI_FUNCTION_ENTRY ();
349 if (RegisterId > ACPI_BITREG_MAX)
351 ACPI_ERROR ((AE_INFO, "Invalid BitRegister ID: 0x%X", RegisterId));
355 return (&AcpiGbl_BitRegisterInfo[RegisterId]);
359 /******************************************************************************
361 * FUNCTION: AcpiHwWritePm1Control
363 * PARAMETERS: Pm1aControl - Value to be written to PM1A control
364 * Pm1bControl - Value to be written to PM1B control
368 * DESCRIPTION: Write the PM1 A/B control registers. These registers are
369 * different than than the PM1 A/B status and enable registers
370 * in that different values can be written to the A/B registers.
371 * Most notably, the SLP_TYP bits can be different, as per the
372 * values returned from the _Sx predefined methods.
374 ******************************************************************************/
377 AcpiHwWritePm1Control (
384 ACPI_FUNCTION_TRACE (HwWritePm1Control);
387 Status = AcpiHwWrite (Pm1aControl, &AcpiGbl_FADT.XPm1aControlBlock);
388 if (ACPI_FAILURE (Status))
390 return_ACPI_STATUS (Status);
393 if (AcpiGbl_FADT.XPm1bControlBlock.Address)
395 Status = AcpiHwWrite (Pm1bControl, &AcpiGbl_FADT.XPm1bControlBlock);
397 return_ACPI_STATUS (Status);
401 /******************************************************************************
403 * FUNCTION: AcpiHwRegisterRead
405 * PARAMETERS: RegisterId - ACPI Register ID
406 * ReturnValue - Where the register value is returned
408 * RETURN: Status and the value read.
410 * DESCRIPTION: Read from the specified ACPI register
412 ******************************************************************************/
423 ACPI_FUNCTION_TRACE (HwRegisterRead);
428 case ACPI_REGISTER_PM1_STATUS: /* PM1 A/B: 16-bit access each */
430 Status = AcpiHwReadMultiple (&Value,
431 &AcpiGbl_XPm1aStatus,
432 &AcpiGbl_XPm1bStatus);
435 case ACPI_REGISTER_PM1_ENABLE: /* PM1 A/B: 16-bit access each */
437 Status = AcpiHwReadMultiple (&Value,
438 &AcpiGbl_XPm1aEnable,
439 &AcpiGbl_XPm1bEnable);
442 case ACPI_REGISTER_PM1_CONTROL: /* PM1 A/B: 16-bit access each */
444 Status = AcpiHwReadMultiple (&Value,
445 &AcpiGbl_FADT.XPm1aControlBlock,
446 &AcpiGbl_FADT.XPm1bControlBlock);
449 * Zero the write-only bits. From the ACPI specification, "Hardware
450 * Write-Only Bits": "Upon reads to registers with write-only bits,
451 * software masks out all write-only bits."
453 Value &= ~ACPI_PM1_CONTROL_WRITEONLY_BITS;
456 case ACPI_REGISTER_PM2_CONTROL: /* 8-bit access */
458 Status = AcpiHwRead (&Value, &AcpiGbl_FADT.XPm2ControlBlock);
461 case ACPI_REGISTER_PM_TIMER: /* 32-bit access */
463 Status = AcpiHwRead (&Value, &AcpiGbl_FADT.XPmTimerBlock);
466 case ACPI_REGISTER_SMI_COMMAND_BLOCK: /* 8-bit access */
468 Status = AcpiHwReadPort (AcpiGbl_FADT.SmiCommand, &Value, 8);
473 ACPI_ERROR ((AE_INFO, "Unknown Register ID: 0x%X",
475 Status = AE_BAD_PARAMETER;
479 if (ACPI_SUCCESS (Status))
481 *ReturnValue = Value;
484 return_ACPI_STATUS (Status);
488 /******************************************************************************
490 * FUNCTION: AcpiHwRegisterWrite
492 * PARAMETERS: RegisterId - ACPI Register ID
493 * Value - The value to write
497 * DESCRIPTION: Write to the specified ACPI register
499 * NOTE: In accordance with the ACPI specification, this function automatically
500 * preserves the value of the following bits, meaning that these bits cannot be
501 * changed via this interface:
503 * PM1_CONTROL[0] = SCI_EN
508 * 1) Hardware Ignored Bits: When software writes to a register with ignored
509 * bit fields, it preserves the ignored bit fields
510 * 2) SCI_EN: OSPM always preserves this bit position
512 ******************************************************************************/
515 AcpiHwRegisterWrite (
523 ACPI_FUNCTION_TRACE (HwRegisterWrite);
528 case ACPI_REGISTER_PM1_STATUS: /* PM1 A/B: 16-bit access each */
530 * Handle the "ignored" bit in PM1 Status. According to the ACPI
531 * specification, ignored bits are to be preserved when writing.
532 * Normally, this would mean a read/modify/write sequence. However,
533 * preserving a bit in the status register is different. Writing a
534 * one clears the status, and writing a zero preserves the status.
535 * Therefore, we must always write zero to the ignored bit.
537 * This behavior is clarified in the ACPI 4.0 specification.
539 Value &= ~ACPI_PM1_STATUS_PRESERVED_BITS;
541 Status = AcpiHwWriteMultiple (Value,
542 &AcpiGbl_XPm1aStatus,
543 &AcpiGbl_XPm1bStatus);
546 case ACPI_REGISTER_PM1_ENABLE: /* PM1 A/B: 16-bit access each */
548 Status = AcpiHwWriteMultiple (Value,
549 &AcpiGbl_XPm1aEnable,
550 &AcpiGbl_XPm1bEnable);
553 case ACPI_REGISTER_PM1_CONTROL: /* PM1 A/B: 16-bit access each */
555 * Perform a read first to preserve certain bits (per ACPI spec)
556 * Note: This includes SCI_EN, we never want to change this bit
558 Status = AcpiHwReadMultiple (&ReadValue,
559 &AcpiGbl_FADT.XPm1aControlBlock,
560 &AcpiGbl_FADT.XPm1bControlBlock);
561 if (ACPI_FAILURE (Status))
566 /* Insert the bits to be preserved */
568 ACPI_INSERT_BITS (Value, ACPI_PM1_CONTROL_PRESERVED_BITS, ReadValue);
570 /* Now we can write the data */
572 Status = AcpiHwWriteMultiple (Value,
573 &AcpiGbl_FADT.XPm1aControlBlock,
574 &AcpiGbl_FADT.XPm1bControlBlock);
577 case ACPI_REGISTER_PM2_CONTROL: /* 8-bit access */
579 * For control registers, all reserved bits must be preserved,
580 * as per the ACPI spec.
582 Status = AcpiHwRead (&ReadValue, &AcpiGbl_FADT.XPm2ControlBlock);
583 if (ACPI_FAILURE (Status))
588 /* Insert the bits to be preserved */
590 ACPI_INSERT_BITS (Value, ACPI_PM2_CONTROL_PRESERVED_BITS, ReadValue);
592 Status = AcpiHwWrite (Value, &AcpiGbl_FADT.XPm2ControlBlock);
595 case ACPI_REGISTER_PM_TIMER: /* 32-bit access */
597 Status = AcpiHwWrite (Value, &AcpiGbl_FADT.XPmTimerBlock);
600 case ACPI_REGISTER_SMI_COMMAND_BLOCK: /* 8-bit access */
602 /* SMI_CMD is currently always in IO space */
604 Status = AcpiHwWritePort (AcpiGbl_FADT.SmiCommand, Value, 8);
609 ACPI_ERROR ((AE_INFO, "Unknown Register ID: 0x%X",
611 Status = AE_BAD_PARAMETER;
616 return_ACPI_STATUS (Status);
620 /******************************************************************************
622 * FUNCTION: AcpiHwReadMultiple
624 * PARAMETERS: Value - Where the register value is returned
625 * RegisterA - First ACPI register (required)
626 * RegisterB - Second ACPI register (optional)
630 * DESCRIPTION: Read from the specified two-part ACPI register (such as PM1 A/B)
632 ******************************************************************************/
637 ACPI_GENERIC_ADDRESS *RegisterA,
638 ACPI_GENERIC_ADDRESS *RegisterB)
645 /* The first register is always required */
647 Status = AcpiHwRead (&ValueA, RegisterA);
648 if (ACPI_FAILURE (Status))
653 /* Second register is optional */
655 if (RegisterB->Address)
657 Status = AcpiHwRead (&ValueB, RegisterB);
658 if (ACPI_FAILURE (Status))
665 * OR the two return values together. No shifting or masking is necessary,
666 * because of how the PM1 registers are defined in the ACPI specification:
668 * "Although the bits can be split between the two register blocks (each
669 * register block has a unique pointer within the FADT), the bit positions
670 * are maintained. The register block with unimplemented bits (that is,
671 * those implemented in the other register block) always returns zeros,
672 * and writes have no side effects"
674 *Value = (ValueA | ValueB);
679 /******************************************************************************
681 * FUNCTION: AcpiHwWriteMultiple
683 * PARAMETERS: Value - The value to write
684 * RegisterA - First ACPI register (required)
685 * RegisterB - Second ACPI register (optional)
689 * DESCRIPTION: Write to the specified two-part ACPI register (such as PM1 A/B)
691 ******************************************************************************/
694 AcpiHwWriteMultiple (
696 ACPI_GENERIC_ADDRESS *RegisterA,
697 ACPI_GENERIC_ADDRESS *RegisterB)
702 /* The first register is always required */
704 Status = AcpiHwWrite (Value, RegisterA);
705 if (ACPI_FAILURE (Status))
711 * Second register is optional
713 * No bit shifting or clearing is necessary, because of how the PM1
714 * registers are defined in the ACPI specification:
716 * "Although the bits can be split between the two register blocks (each
717 * register block has a unique pointer within the FADT), the bit positions
718 * are maintained. The register block with unimplemented bits (that is,
719 * those implemented in the other register block) always returns zeros,
720 * and writes have no side effects"
722 if (RegisterB->Address)
724 Status = AcpiHwWrite (Value, RegisterB);
730 #endif /* !ACPI_REDUCED_HARDWARE */