acpica.library: Initial import of Intel ACPICA, v20131115
[AROS.git] / arch / all-pc / acpica / source / components / hardware / hwxface.c
bloba1d5a04bb5dd0ec796bfbac78c23944836792035
1 /******************************************************************************
3 * Module Name: hwxface - Public ACPICA hardware interfaces
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2013, Intel Corp.
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #define EXPORT_ACPI_INTERFACES
46 #include "acpi.h"
47 #include "accommon.h"
48 #include "acnamesp.h"
50 #define _COMPONENT ACPI_HARDWARE
51 ACPI_MODULE_NAME ("hwxface")
54 /******************************************************************************
56 * FUNCTION: AcpiReset
58 * PARAMETERS: None
60 * RETURN: Status
62 * DESCRIPTION: Set reset register in memory or IO space. Note: Does not
63 * support reset register in PCI config space, this must be
64 * handled separately.
66 ******************************************************************************/
68 ACPI_STATUS
69 AcpiReset (
70 void)
72 ACPI_GENERIC_ADDRESS *ResetReg;
73 ACPI_STATUS Status;
76 ACPI_FUNCTION_TRACE (AcpiReset);
79 ResetReg = &AcpiGbl_FADT.ResetRegister;
81 /* Check if the reset register is supported */
83 if (!(AcpiGbl_FADT.Flags & ACPI_FADT_RESET_REGISTER) ||
84 !ResetReg->Address)
86 return_ACPI_STATUS (AE_NOT_EXIST);
89 if (ResetReg->SpaceId == ACPI_ADR_SPACE_SYSTEM_IO)
92 * For I/O space, write directly to the OSL. This bypasses the port
93 * validation mechanism, which may block a valid write to the reset
94 * register.
96 * NOTE:
97 * The ACPI spec requires the reset register width to be 8, so we
98 * hardcode it here and ignore the FADT value. This maintains
99 * compatibility with other ACPI implementations that have allowed
100 * BIOS code with bad register width values to go unnoticed.
102 Status = AcpiOsWritePort ((ACPI_IO_ADDRESS) ResetReg->Address,
103 AcpiGbl_FADT.ResetValue, ACPI_RESET_REGISTER_WIDTH);
105 else
107 /* Write the reset value to the reset register */
109 Status = AcpiHwWrite (AcpiGbl_FADT.ResetValue, ResetReg);
112 return_ACPI_STATUS (Status);
115 ACPI_EXPORT_SYMBOL (AcpiReset)
118 /******************************************************************************
120 * FUNCTION: AcpiRead
122 * PARAMETERS: Value - Where the value is returned
123 * Reg - GAS register structure
125 * RETURN: Status
127 * DESCRIPTION: Read from either memory or IO space.
129 * LIMITATIONS: <These limitations also apply to AcpiWrite>
130 * BitWidth must be exactly 8, 16, 32, or 64.
131 * SpaceID must be SystemMemory or SystemIO.
132 * BitOffset and AccessWidth are currently ignored, as there has
133 * not been a need to implement these.
135 ******************************************************************************/
137 ACPI_STATUS
138 AcpiRead (
139 UINT64 *ReturnValue,
140 ACPI_GENERIC_ADDRESS *Reg)
142 UINT32 ValueLo;
143 UINT32 ValueHi;
144 UINT32 Width;
145 UINT64 Address;
146 ACPI_STATUS Status;
149 ACPI_FUNCTION_NAME (AcpiRead);
152 if (!ReturnValue)
154 return (AE_BAD_PARAMETER);
157 /* Validate contents of the GAS register. Allow 64-bit transfers */
159 Status = AcpiHwValidateRegister (Reg, 64, &Address);
160 if (ACPI_FAILURE (Status))
162 return (Status);
166 * Two address spaces supported: Memory or I/O. PCI_Config is
167 * not supported here because the GAS structure is insufficient
169 if (Reg->SpaceId == ACPI_ADR_SPACE_SYSTEM_MEMORY)
171 Status = AcpiOsReadMemory ((ACPI_PHYSICAL_ADDRESS)
172 Address, ReturnValue, Reg->BitWidth);
173 if (ACPI_FAILURE (Status))
175 return (Status);
178 else /* ACPI_ADR_SPACE_SYSTEM_IO, validated earlier */
180 ValueLo = 0;
181 ValueHi = 0;
183 Width = Reg->BitWidth;
184 if (Width == 64)
186 Width = 32; /* Break into two 32-bit transfers */
189 Status = AcpiHwReadPort ((ACPI_IO_ADDRESS)
190 Address, &ValueLo, Width);
191 if (ACPI_FAILURE (Status))
193 return (Status);
196 if (Reg->BitWidth == 64)
198 /* Read the top 32 bits */
200 Status = AcpiHwReadPort ((ACPI_IO_ADDRESS)
201 (Address + 4), &ValueHi, 32);
202 if (ACPI_FAILURE (Status))
204 return (Status);
208 /* Set the return value only if status is AE_OK */
210 *ReturnValue = (ValueLo | ((UINT64) ValueHi << 32));
213 ACPI_DEBUG_PRINT ((ACPI_DB_IO,
214 "Read: %8.8X%8.8X width %2d from %8.8X%8.8X (%s)\n",
215 ACPI_FORMAT_UINT64 (*ReturnValue), Reg->BitWidth,
216 ACPI_FORMAT_UINT64 (Address),
217 AcpiUtGetRegionName (Reg->SpaceId)));
219 return (AE_OK);
222 ACPI_EXPORT_SYMBOL (AcpiRead)
225 /******************************************************************************
227 * FUNCTION: AcpiWrite
229 * PARAMETERS: Value - Value to be written
230 * Reg - GAS register structure
232 * RETURN: Status
234 * DESCRIPTION: Write to either memory or IO space.
236 ******************************************************************************/
238 ACPI_STATUS
239 AcpiWrite (
240 UINT64 Value,
241 ACPI_GENERIC_ADDRESS *Reg)
243 UINT32 Width;
244 UINT64 Address;
245 ACPI_STATUS Status;
248 ACPI_FUNCTION_NAME (AcpiWrite);
251 /* Validate contents of the GAS register. Allow 64-bit transfers */
253 Status = AcpiHwValidateRegister (Reg, 64, &Address);
254 if (ACPI_FAILURE (Status))
256 return (Status);
260 * Two address spaces supported: Memory or IO. PCI_Config is
261 * not supported here because the GAS structure is insufficient
263 if (Reg->SpaceId == ACPI_ADR_SPACE_SYSTEM_MEMORY)
265 Status = AcpiOsWriteMemory ((ACPI_PHYSICAL_ADDRESS)
266 Address, Value, Reg->BitWidth);
267 if (ACPI_FAILURE (Status))
269 return (Status);
272 else /* ACPI_ADR_SPACE_SYSTEM_IO, validated earlier */
274 Width = Reg->BitWidth;
275 if (Width == 64)
277 Width = 32; /* Break into two 32-bit transfers */
280 Status = AcpiHwWritePort ((ACPI_IO_ADDRESS)
281 Address, ACPI_LODWORD (Value), Width);
282 if (ACPI_FAILURE (Status))
284 return (Status);
287 if (Reg->BitWidth == 64)
289 Status = AcpiHwWritePort ((ACPI_IO_ADDRESS)
290 (Address + 4), ACPI_HIDWORD (Value), 32);
291 if (ACPI_FAILURE (Status))
293 return (Status);
298 ACPI_DEBUG_PRINT ((ACPI_DB_IO,
299 "Wrote: %8.8X%8.8X width %2d to %8.8X%8.8X (%s)\n",
300 ACPI_FORMAT_UINT64 (Value), Reg->BitWidth,
301 ACPI_FORMAT_UINT64 (Address),
302 AcpiUtGetRegionName (Reg->SpaceId)));
304 return (Status);
307 ACPI_EXPORT_SYMBOL (AcpiWrite)
310 #if (!ACPI_REDUCED_HARDWARE)
311 /*******************************************************************************
313 * FUNCTION: AcpiReadBitRegister
315 * PARAMETERS: RegisterId - ID of ACPI Bit Register to access
316 * ReturnValue - Value that was read from the register,
317 * normalized to bit position zero.
319 * RETURN: Status and the value read from the specified Register. Value
320 * returned is normalized to bit0 (is shifted all the way right)
322 * DESCRIPTION: ACPI BitRegister read function. Does not acquire the HW lock.
324 * SUPPORTS: Bit fields in PM1 Status, PM1 Enable, PM1 Control, and
325 * PM2 Control.
327 * Note: The hardware lock is not required when reading the ACPI bit registers
328 * since almost all of them are single bit and it does not matter that
329 * the parent hardware register can be split across two physical
330 * registers. The only multi-bit field is SLP_TYP in the PM1 control
331 * register, but this field does not cross an 8-bit boundary (nor does
332 * it make much sense to actually read this field.)
334 ******************************************************************************/
336 ACPI_STATUS
337 AcpiReadBitRegister (
338 UINT32 RegisterId,
339 UINT32 *ReturnValue)
341 ACPI_BIT_REGISTER_INFO *BitRegInfo;
342 UINT32 RegisterValue;
343 UINT32 Value;
344 ACPI_STATUS Status;
347 ACPI_FUNCTION_TRACE_U32 (AcpiReadBitRegister, RegisterId);
350 /* Get the info structure corresponding to the requested ACPI Register */
352 BitRegInfo = AcpiHwGetBitRegisterInfo (RegisterId);
353 if (!BitRegInfo)
355 return_ACPI_STATUS (AE_BAD_PARAMETER);
358 /* Read the entire parent register */
360 Status = AcpiHwRegisterRead (BitRegInfo->ParentRegister,
361 &RegisterValue);
362 if (ACPI_FAILURE (Status))
364 return_ACPI_STATUS (Status);
367 /* Normalize the value that was read, mask off other bits */
369 Value = ((RegisterValue & BitRegInfo->AccessBitMask)
370 >> BitRegInfo->BitPosition);
372 ACPI_DEBUG_PRINT ((ACPI_DB_IO,
373 "BitReg %X, ParentReg %X, Actual %8.8X, ReturnValue %8.8X\n",
374 RegisterId, BitRegInfo->ParentRegister, RegisterValue, Value));
376 *ReturnValue = Value;
377 return_ACPI_STATUS (AE_OK);
380 ACPI_EXPORT_SYMBOL (AcpiReadBitRegister)
383 /*******************************************************************************
385 * FUNCTION: AcpiWriteBitRegister
387 * PARAMETERS: RegisterId - ID of ACPI Bit Register to access
388 * Value - Value to write to the register, in bit
389 * position zero. The bit is automatically
390 * shifted to the correct position.
392 * RETURN: Status
394 * DESCRIPTION: ACPI Bit Register write function. Acquires the hardware lock
395 * since most operations require a read/modify/write sequence.
397 * SUPPORTS: Bit fields in PM1 Status, PM1 Enable, PM1 Control, and
398 * PM2 Control.
400 * Note that at this level, the fact that there may be actually two
401 * hardware registers (A and B - and B may not exist) is abstracted.
403 ******************************************************************************/
405 ACPI_STATUS
406 AcpiWriteBitRegister (
407 UINT32 RegisterId,
408 UINT32 Value)
410 ACPI_BIT_REGISTER_INFO *BitRegInfo;
411 ACPI_CPU_FLAGS LockFlags;
412 UINT32 RegisterValue;
413 ACPI_STATUS Status = AE_OK;
416 ACPI_FUNCTION_TRACE_U32 (AcpiWriteBitRegister, RegisterId);
419 /* Get the info structure corresponding to the requested ACPI Register */
421 BitRegInfo = AcpiHwGetBitRegisterInfo (RegisterId);
422 if (!BitRegInfo)
424 return_ACPI_STATUS (AE_BAD_PARAMETER);
427 LockFlags = AcpiOsAcquireLock (AcpiGbl_HardwareLock);
430 * At this point, we know that the parent register is one of the
431 * following: PM1 Status, PM1 Enable, PM1 Control, or PM2 Control
433 if (BitRegInfo->ParentRegister != ACPI_REGISTER_PM1_STATUS)
436 * 1) Case for PM1 Enable, PM1 Control, and PM2 Control
438 * Perform a register read to preserve the bits that we are not
439 * interested in
441 Status = AcpiHwRegisterRead (BitRegInfo->ParentRegister,
442 &RegisterValue);
443 if (ACPI_FAILURE (Status))
445 goto UnlockAndExit;
449 * Insert the input bit into the value that was just read
450 * and write the register
452 ACPI_REGISTER_INSERT_VALUE (RegisterValue, BitRegInfo->BitPosition,
453 BitRegInfo->AccessBitMask, Value);
455 Status = AcpiHwRegisterWrite (BitRegInfo->ParentRegister,
456 RegisterValue);
458 else
461 * 2) Case for PM1 Status
463 * The Status register is different from the rest. Clear an event
464 * by writing 1, writing 0 has no effect. So, the only relevant
465 * information is the single bit we're interested in, all others
466 * should be written as 0 so they will be left unchanged.
468 RegisterValue = ACPI_REGISTER_PREPARE_BITS (Value,
469 BitRegInfo->BitPosition, BitRegInfo->AccessBitMask);
471 /* No need to write the register if value is all zeros */
473 if (RegisterValue)
475 Status = AcpiHwRegisterWrite (ACPI_REGISTER_PM1_STATUS,
476 RegisterValue);
480 ACPI_DEBUG_PRINT ((ACPI_DB_IO,
481 "BitReg %X, ParentReg %X, Value %8.8X, Actual %8.8X\n",
482 RegisterId, BitRegInfo->ParentRegister, Value, RegisterValue));
485 UnlockAndExit:
487 AcpiOsReleaseLock (AcpiGbl_HardwareLock, LockFlags);
488 return_ACPI_STATUS (Status);
491 ACPI_EXPORT_SYMBOL (AcpiWriteBitRegister)
493 #endif /* !ACPI_REDUCED_HARDWARE */
496 /*******************************************************************************
498 * FUNCTION: AcpiGetSleepTypeData
500 * PARAMETERS: SleepState - Numeric sleep state
501 * *SleepTypeA - Where SLP_TYPa is returned
502 * *SleepTypeB - Where SLP_TYPb is returned
504 * RETURN: Status
506 * DESCRIPTION: Obtain the SLP_TYPa and SLP_TYPb values for the requested
507 * sleep state via the appropriate \_Sx object.
509 * The sleep state package returned from the corresponding \_Sx_ object
510 * must contain at least one integer.
512 * March 2005:
513 * Added support for a package that contains two integers. This
514 * goes against the ACPI specification which defines this object as a
515 * package with one encoded DWORD integer. However, existing practice
516 * by many BIOS vendors is to return a package with 2 or more integer
517 * elements, at least one per sleep type (A/B).
519 * January 2013:
520 * Therefore, we must be prepared to accept a package with either a
521 * single integer or multiple integers.
523 * The single integer DWORD format is as follows:
524 * BYTE 0 - Value for the PM1A SLP_TYP register
525 * BYTE 1 - Value for the PM1B SLP_TYP register
526 * BYTE 2-3 - Reserved
528 * The dual integer format is as follows:
529 * Integer 0 - Value for the PM1A SLP_TYP register
530 * Integer 1 - Value for the PM1A SLP_TYP register
532 ******************************************************************************/
534 ACPI_STATUS
535 AcpiGetSleepTypeData (
536 UINT8 SleepState,
537 UINT8 *SleepTypeA,
538 UINT8 *SleepTypeB)
540 ACPI_STATUS Status;
541 ACPI_EVALUATE_INFO *Info;
542 ACPI_OPERAND_OBJECT **Elements;
545 ACPI_FUNCTION_TRACE (AcpiGetSleepTypeData);
548 /* Validate parameters */
550 if ((SleepState > ACPI_S_STATES_MAX) ||
551 !SleepTypeA || !SleepTypeB)
553 return_ACPI_STATUS (AE_BAD_PARAMETER);
556 /* Allocate the evaluation information block */
558 Info = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_EVALUATE_INFO));
559 if (!Info)
561 return_ACPI_STATUS (AE_NO_MEMORY);
565 * Evaluate the \_Sx namespace object containing the register values
566 * for this state
568 Info->RelativePathname = ACPI_CAST_PTR (
569 char, AcpiGbl_SleepStateNames[SleepState]);
570 Status = AcpiNsEvaluate (Info);
571 if (ACPI_FAILURE (Status))
573 goto Cleanup;
576 /* Must have a return object */
578 if (!Info->ReturnObject)
580 ACPI_ERROR ((AE_INFO, "No Sleep State object returned from [%s]",
581 Info->RelativePathname));
582 Status = AE_AML_NO_RETURN_VALUE;
583 goto Cleanup;
586 /* Return object must be of type Package */
588 if (Info->ReturnObject->Common.Type != ACPI_TYPE_PACKAGE)
590 ACPI_ERROR ((AE_INFO, "Sleep State return object is not a Package"));
591 Status = AE_AML_OPERAND_TYPE;
592 goto Cleanup1;
596 * Any warnings about the package length or the object types have
597 * already been issued by the predefined name module -- there is no
598 * need to repeat them here.
600 Elements = Info->ReturnObject->Package.Elements;
601 switch (Info->ReturnObject->Package.Count)
603 case 0:
605 Status = AE_AML_PACKAGE_LIMIT;
606 break;
608 case 1:
610 if (Elements[0]->Common.Type != ACPI_TYPE_INTEGER)
612 Status = AE_AML_OPERAND_TYPE;
613 break;
616 /* A valid _Sx_ package with one integer */
618 *SleepTypeA = (UINT8) Elements[0]->Integer.Value;
619 *SleepTypeB = (UINT8) (Elements[0]->Integer.Value >> 8);
620 break;
622 case 2:
623 default:
625 if ((Elements[0]->Common.Type != ACPI_TYPE_INTEGER) ||
626 (Elements[1]->Common.Type != ACPI_TYPE_INTEGER))
628 Status = AE_AML_OPERAND_TYPE;
629 break;
632 /* A valid _Sx_ package with two integers */
634 *SleepTypeA = (UINT8) Elements[0]->Integer.Value;
635 *SleepTypeB = (UINT8) Elements[1]->Integer.Value;
636 break;
639 Cleanup1:
640 AcpiUtRemoveReference (Info->ReturnObject);
642 Cleanup:
643 if (ACPI_FAILURE (Status))
645 ACPI_EXCEPTION ((AE_INFO, Status,
646 "While evaluating Sleep State [%s]", Info->RelativePathname));
649 ACPI_FREE (Info);
650 return_ACPI_STATUS (Status);
653 ACPI_EXPORT_SYMBOL (AcpiGetSleepTypeData)