2 /*******************************************************************************
4 * Module Name: hwregs - Read/write access functions for the various ACPI
5 * control and status registers.
7 ******************************************************************************/
10 * Copyright (C) 2000 - 2011, 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.
48 #include <contrib/dev/acpica/include/acpi.h>
49 #include <contrib/dev/acpica/include/accommon.h>
50 #include <contrib/dev/acpica/include/acevents.h>
52 #define _COMPONENT ACPI_HARDWARE
53 ACPI_MODULE_NAME ("hwregs")
56 /* Local Prototypes */
61 ACPI_GENERIC_ADDRESS *RegisterA,
62 ACPI_GENERIC_ADDRESS *RegisterB);
67 ACPI_GENERIC_ADDRESS *RegisterA,
68 ACPI_GENERIC_ADDRESS *RegisterB);
71 /******************************************************************************
73 * FUNCTION: AcpiHwValidateRegister
75 * PARAMETERS: Reg - GAS register structure
76 * MaxBitWidth - Max BitWidth supported (32 or 64)
77 * Address - Pointer to where the gas->address
82 * DESCRIPTION: Validate the contents of a GAS register. Checks the GAS
83 * pointer, Address, SpaceId, BitWidth, and BitOffset.
85 ******************************************************************************/
88 AcpiHwValidateRegister (
89 ACPI_GENERIC_ADDRESS *Reg,
94 /* Must have a valid pointer to a GAS structure */
98 return (AE_BAD_PARAMETER);
102 * Copy the target address. This handles possible alignment issues.
103 * Address must not be null. A null address also indicates an optional
104 * ACPI register that is not supported, so no error message.
106 ACPI_MOVE_64_TO_64 (Address, &Reg->Address);
109 return (AE_BAD_ADDRESS);
112 /* Validate the SpaceID */
114 if ((Reg->SpaceId != ACPI_ADR_SPACE_SYSTEM_MEMORY) &&
115 (Reg->SpaceId != ACPI_ADR_SPACE_SYSTEM_IO))
117 ACPI_ERROR ((AE_INFO,
118 "Unsupported address space: 0x%X", Reg->SpaceId));
122 /* Validate the BitWidth */
124 if ((Reg->BitWidth != 8) &&
125 (Reg->BitWidth != 16) &&
126 (Reg->BitWidth != 32) &&
127 (Reg->BitWidth != MaxBitWidth))
129 ACPI_ERROR ((AE_INFO,
130 "Unsupported register bit width: 0x%X", Reg->BitWidth));
134 /* Validate the BitOffset. Just a warning for now. */
136 if (Reg->BitOffset != 0)
138 ACPI_WARNING ((AE_INFO,
139 "Unsupported register bit offset: 0x%X", Reg->BitOffset));
146 /******************************************************************************
148 * FUNCTION: AcpiHwRead
150 * PARAMETERS: Value - Where the value is returned
151 * Reg - GAS register structure
155 * DESCRIPTION: Read from either memory or IO space. This is a 32-bit max
156 * version of AcpiRead, used internally since the overhead of
157 * 64-bit values is not needed.
159 * LIMITATIONS: <These limitations also apply to AcpiHwWrite>
160 * BitWidth must be exactly 8, 16, or 32.
161 * SpaceID must be SystemMemory or SystemIO.
162 * BitOffset and AccessWidth are currently ignored, as there has
163 * not been a need to implement these.
165 ******************************************************************************/
170 ACPI_GENERIC_ADDRESS *Reg)
176 ACPI_FUNCTION_NAME (HwRead);
179 /* Validate contents of the GAS register */
181 Status = AcpiHwValidateRegister (Reg, 32, &Address);
182 if (ACPI_FAILURE (Status))
187 /* Initialize entire 32-bit return value to zero */
192 * Two address spaces supported: Memory or IO. PCI_Config is
193 * not supported here because the GAS structure is insufficient
195 if (Reg->SpaceId == ACPI_ADR_SPACE_SYSTEM_MEMORY)
197 Status = AcpiOsReadMemory ((ACPI_PHYSICAL_ADDRESS)
198 Address, Value, Reg->BitWidth);
200 else /* ACPI_ADR_SPACE_SYSTEM_IO, validated earlier */
202 Status = AcpiHwReadPort ((ACPI_IO_ADDRESS)
203 Address, Value, Reg->BitWidth);
206 ACPI_DEBUG_PRINT ((ACPI_DB_IO,
207 "Read: %8.8X width %2d from %8.8X%8.8X (%s)\n",
208 *Value, Reg->BitWidth, ACPI_FORMAT_UINT64 (Address),
209 AcpiUtGetRegionName (Reg->SpaceId)));
215 /******************************************************************************
217 * FUNCTION: AcpiHwWrite
219 * PARAMETERS: Value - Value to be written
220 * Reg - GAS register structure
224 * DESCRIPTION: Write to either memory or IO space. This is a 32-bit max
225 * version of AcpiWrite, used internally since the overhead of
226 * 64-bit values is not needed.
228 ******************************************************************************/
233 ACPI_GENERIC_ADDRESS *Reg)
239 ACPI_FUNCTION_NAME (HwWrite);
242 /* Validate contents of the GAS register */
244 Status = AcpiHwValidateRegister (Reg, 32, &Address);
245 if (ACPI_FAILURE (Status))
251 * Two address spaces supported: Memory or IO. PCI_Config is
252 * not supported here because the GAS structure is insufficient
254 if (Reg->SpaceId == ACPI_ADR_SPACE_SYSTEM_MEMORY)
256 Status = AcpiOsWriteMemory ((ACPI_PHYSICAL_ADDRESS)
257 Address, Value, Reg->BitWidth);
259 else /* ACPI_ADR_SPACE_SYSTEM_IO, validated earlier */
261 Status = AcpiHwWritePort ((ACPI_IO_ADDRESS)
262 Address, Value, Reg->BitWidth);
265 ACPI_DEBUG_PRINT ((ACPI_DB_IO,
266 "Wrote: %8.8X width %2d to %8.8X%8.8X (%s)\n",
267 Value, Reg->BitWidth, ACPI_FORMAT_UINT64 (Address),
268 AcpiUtGetRegionName (Reg->SpaceId)));
274 /*******************************************************************************
276 * FUNCTION: AcpiHwClearAcpiStatus
282 * DESCRIPTION: Clears all fixed and general purpose status bits
284 ******************************************************************************/
287 AcpiHwClearAcpiStatus (
291 ACPI_CPU_FLAGS LockFlags = 0;
294 ACPI_FUNCTION_TRACE (HwClearAcpiStatus);
297 ACPI_DEBUG_PRINT ((ACPI_DB_IO, "About to write %04X to %8.8X%8.8X\n",
298 ACPI_BITMASK_ALL_FIXED_STATUS,
299 ACPI_FORMAT_UINT64 (AcpiGbl_XPm1aStatus.Address)));
301 LockFlags = AcpiOsAcquireLock (AcpiGbl_HardwareLock);
303 /* Clear the fixed events in PM1 A/B */
305 Status = AcpiHwRegisterWrite (ACPI_REGISTER_PM1_STATUS,
306 ACPI_BITMASK_ALL_FIXED_STATUS);
307 if (ACPI_FAILURE (Status))
312 /* Clear the GPE Bits in all GPE registers in all GPE blocks */
314 Status = AcpiEvWalkGpeList (AcpiHwClearGpeBlock, NULL);
317 AcpiOsReleaseLock (AcpiGbl_HardwareLock, LockFlags);
318 return_ACPI_STATUS (Status);
322 /*******************************************************************************
324 * FUNCTION: AcpiHwGetRegisterBitMask
326 * PARAMETERS: RegisterId - Index of ACPI Register to access
328 * RETURN: The bitmask to be used when accessing the register
330 * DESCRIPTION: Map RegisterId into a register bitmask.
332 ******************************************************************************/
334 ACPI_BIT_REGISTER_INFO *
335 AcpiHwGetBitRegisterInfo (
338 ACPI_FUNCTION_ENTRY ();
341 if (RegisterId > ACPI_BITREG_MAX)
343 ACPI_ERROR ((AE_INFO, "Invalid BitRegister ID: 0x%X", RegisterId));
347 return (&AcpiGbl_BitRegisterInfo[RegisterId]);
351 /******************************************************************************
353 * FUNCTION: AcpiHwWritePm1Control
355 * PARAMETERS: Pm1aControl - Value to be written to PM1A control
356 * Pm1bControl - Value to be written to PM1B control
360 * DESCRIPTION: Write the PM1 A/B control registers. These registers are
361 * different than than the PM1 A/B status and enable registers
362 * in that different values can be written to the A/B registers.
363 * Most notably, the SLP_TYP bits can be different, as per the
364 * values returned from the _Sx predefined methods.
366 ******************************************************************************/
369 AcpiHwWritePm1Control (
376 ACPI_FUNCTION_TRACE (HwWritePm1Control);
379 Status = AcpiHwWrite (Pm1aControl, &AcpiGbl_FADT.XPm1aControlBlock);
380 if (ACPI_FAILURE (Status))
382 return_ACPI_STATUS (Status);
385 if (AcpiGbl_FADT.XPm1bControlBlock.Address)
387 Status = AcpiHwWrite (Pm1bControl, &AcpiGbl_FADT.XPm1bControlBlock);
389 return_ACPI_STATUS (Status);
393 /******************************************************************************
395 * FUNCTION: AcpiHwRegisterRead
397 * PARAMETERS: RegisterId - ACPI Register ID
398 * ReturnValue - Where the register value is returned
400 * RETURN: Status and the value read.
402 * DESCRIPTION: Read from the specified ACPI register
404 ******************************************************************************/
415 ACPI_FUNCTION_TRACE (HwRegisterRead);
420 case ACPI_REGISTER_PM1_STATUS: /* PM1 A/B: 16-bit access each */
422 Status = AcpiHwReadMultiple (&Value,
423 &AcpiGbl_XPm1aStatus,
424 &AcpiGbl_XPm1bStatus);
428 case ACPI_REGISTER_PM1_ENABLE: /* PM1 A/B: 16-bit access each */
430 Status = AcpiHwReadMultiple (&Value,
431 &AcpiGbl_XPm1aEnable,
432 &AcpiGbl_XPm1bEnable);
436 case ACPI_REGISTER_PM1_CONTROL: /* PM1 A/B: 16-bit access each */
438 Status = AcpiHwReadMultiple (&Value,
439 &AcpiGbl_FADT.XPm1aControlBlock,
440 &AcpiGbl_FADT.XPm1bControlBlock);
443 * Zero the write-only bits. From the ACPI specification, "Hardware
444 * Write-Only Bits": "Upon reads to registers with write-only bits,
445 * software masks out all write-only bits."
447 Value &= ~ACPI_PM1_CONTROL_WRITEONLY_BITS;
451 case ACPI_REGISTER_PM2_CONTROL: /* 8-bit access */
453 Status = AcpiHwRead (&Value, &AcpiGbl_FADT.XPm2ControlBlock);
457 case ACPI_REGISTER_PM_TIMER: /* 32-bit access */
459 Status = AcpiHwRead (&Value, &AcpiGbl_FADT.XPmTimerBlock);
463 case ACPI_REGISTER_SMI_COMMAND_BLOCK: /* 8-bit access */
465 Status = AcpiHwReadPort (AcpiGbl_FADT.SmiCommand, &Value, 8);
470 ACPI_ERROR ((AE_INFO, "Unknown Register ID: 0x%X",
472 Status = AE_BAD_PARAMETER;
476 if (ACPI_SUCCESS (Status))
478 *ReturnValue = Value;
481 return_ACPI_STATUS (Status);
485 /******************************************************************************
487 * FUNCTION: AcpiHwRegisterWrite
489 * PARAMETERS: RegisterId - ACPI Register ID
490 * Value - The value to write
494 * DESCRIPTION: Write to the specified ACPI register
496 * NOTE: In accordance with the ACPI specification, this function automatically
497 * preserves the value of the following bits, meaning that these bits cannot be
498 * changed via this interface:
500 * PM1_CONTROL[0] = SCI_EN
505 * 1) Hardware Ignored Bits: When software writes to a register with ignored
506 * bit fields, it preserves the ignored bit fields
507 * 2) SCI_EN: OSPM always preserves this bit position
509 ******************************************************************************/
512 AcpiHwRegisterWrite (
520 ACPI_FUNCTION_TRACE (HwRegisterWrite);
525 case ACPI_REGISTER_PM1_STATUS: /* PM1 A/B: 16-bit access each */
527 * Handle the "ignored" bit in PM1 Status. According to the ACPI
528 * specification, ignored bits are to be preserved when writing.
529 * Normally, this would mean a read/modify/write sequence. However,
530 * preserving a bit in the status register is different. Writing a
531 * one clears the status, and writing a zero preserves the status.
532 * Therefore, we must always write zero to the ignored bit.
534 * This behavior is clarified in the ACPI 4.0 specification.
536 Value &= ~ACPI_PM1_STATUS_PRESERVED_BITS;
538 Status = AcpiHwWriteMultiple (Value,
539 &AcpiGbl_XPm1aStatus,
540 &AcpiGbl_XPm1bStatus);
544 case ACPI_REGISTER_PM1_ENABLE: /* PM1 A/B: 16-bit access each */
546 Status = AcpiHwWriteMultiple (Value,
547 &AcpiGbl_XPm1aEnable,
548 &AcpiGbl_XPm1bEnable);
552 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);
578 case ACPI_REGISTER_PM2_CONTROL: /* 8-bit access */
581 * For control registers, all reserved bits must be preserved,
582 * as per the ACPI spec.
584 Status = AcpiHwRead (&ReadValue, &AcpiGbl_FADT.XPm2ControlBlock);
585 if (ACPI_FAILURE (Status))
590 /* Insert the bits to be preserved */
592 ACPI_INSERT_BITS (Value, ACPI_PM2_CONTROL_PRESERVED_BITS, ReadValue);
594 Status = AcpiHwWrite (Value, &AcpiGbl_FADT.XPm2ControlBlock);
598 case ACPI_REGISTER_PM_TIMER: /* 32-bit access */
600 Status = AcpiHwWrite (Value, &AcpiGbl_FADT.XPmTimerBlock);
604 case ACPI_REGISTER_SMI_COMMAND_BLOCK: /* 8-bit access */
606 /* SMI_CMD is currently always in IO space */
608 Status = AcpiHwWritePort (AcpiGbl_FADT.SmiCommand, Value, 8);
613 ACPI_ERROR ((AE_INFO, "Unknown Register ID: 0x%X",
615 Status = AE_BAD_PARAMETER;
620 return_ACPI_STATUS (Status);
624 /******************************************************************************
626 * FUNCTION: AcpiHwReadMultiple
628 * PARAMETERS: Value - Where the register value is returned
629 * RegisterA - First ACPI register (required)
630 * RegisterB - Second ACPI register (optional)
634 * DESCRIPTION: Read from the specified two-part ACPI register (such as PM1 A/B)
636 ******************************************************************************/
641 ACPI_GENERIC_ADDRESS *RegisterA,
642 ACPI_GENERIC_ADDRESS *RegisterB)
649 /* The first register is always required */
651 Status = AcpiHwRead (&ValueA, RegisterA);
652 if (ACPI_FAILURE (Status))
657 /* Second register is optional */
659 if (RegisterB->Address)
661 Status = AcpiHwRead (&ValueB, RegisterB);
662 if (ACPI_FAILURE (Status))
669 * OR the two return values together. No shifting or masking is necessary,
670 * because of how the PM1 registers are defined in the ACPI specification:
672 * "Although the bits can be split between the two register blocks (each
673 * register block has a unique pointer within the FADT), the bit positions
674 * are maintained. The register block with unimplemented bits (that is,
675 * those implemented in the other register block) always returns zeros,
676 * and writes have no side effects"
678 *Value = (ValueA | ValueB);
683 /******************************************************************************
685 * FUNCTION: AcpiHwWriteMultiple
687 * PARAMETERS: Value - The value to write
688 * RegisterA - First ACPI register (required)
689 * RegisterB - Second ACPI register (optional)
693 * DESCRIPTION: Write to the specified two-part ACPI register (such as PM1 A/B)
695 ******************************************************************************/
698 AcpiHwWriteMultiple (
700 ACPI_GENERIC_ADDRESS *RegisterA,
701 ACPI_GENERIC_ADDRESS *RegisterB)
706 /* The first register is always required */
708 Status = AcpiHwWrite (Value, RegisterA);
709 if (ACPI_FAILURE (Status))
715 * Second register is optional
717 * No bit shifting or clearing is necessary, because of how the PM1
718 * registers are defined in the ACPI specification:
720 * "Although the bits can be split between the two register blocks (each
721 * register block has a unique pointer within the FADT), the bit positions
722 * are maintained. The register block with unimplemented bits (that is,
723 * those implemented in the other register block) always returns zeros,
724 * and writes have no side effects"
726 if (RegisterB->Address)
728 Status = AcpiHwWrite (Value, RegisterB);