1/******************************************************************************
2 *
3 * Module Name: hwxface - Public ACPICA hardware interfaces
4 *
5 *****************************************************************************/
6
7/*
8 * Copyright (C) 2000 - 2023, Intel Corp.
9 * All rights reserved.
10 *
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.
25 *
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.
29 *
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 MERCHANTABILITY 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.
42 */
43
44#define EXPORT_ACPI_INTERFACES
45
46#include "acpi.h"
47#include "accommon.h"
48#include "acnamesp.h"
49
50#define _COMPONENT          ACPI_HARDWARE
51        ACPI_MODULE_NAME    ("hwxface")
52
53
54/******************************************************************************
55 *
56 * FUNCTION:    AcpiReset
57 *
58 * PARAMETERS:  None
59 *
60 * RETURN:      Status
61 *
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.
65 *
66 ******************************************************************************/
67
68ACPI_STATUS
69AcpiReset (
70    void)
71{
72    ACPI_GENERIC_ADDRESS    *ResetReg;
73    ACPI_STATUS             Status;
74
75
76    ACPI_FUNCTION_TRACE (AcpiReset);
77
78
79    ResetReg = &AcpiGbl_FADT.ResetRegister;
80
81    /* Check if the reset register is supported */
82
83    if (!(AcpiGbl_FADT.Flags & ACPI_FADT_RESET_REGISTER) ||
84        !ResetReg->Address)
85    {
86        return_ACPI_STATUS (AE_NOT_EXIST);
87    }
88
89    if (ResetReg->SpaceId == ACPI_ADR_SPACE_SYSTEM_IO)
90    {
91        /*
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.
95         *
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.
101         */
102        Status = AcpiOsWritePort ((ACPI_IO_ADDRESS) ResetReg->Address,
103            AcpiGbl_FADT.ResetValue, ACPI_RESET_REGISTER_WIDTH);
104    }
105    else
106    {
107        /* Write the reset value to the reset register */
108
109        Status = AcpiHwWrite (AcpiGbl_FADT.ResetValue, ResetReg);
110    }
111
112    return_ACPI_STATUS (Status);
113}
114
115ACPI_EXPORT_SYMBOL (AcpiReset)
116
117
118/******************************************************************************
119 *
120 * FUNCTION:    AcpiRead
121 *
122 * PARAMETERS:  Value               - Where the value is returned
123 *              Reg                 - GAS register structure
124 *
125 * RETURN:      Status
126 *
127 * DESCRIPTION: Read from either memory or IO space.
128 *
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.
134 *
135 ******************************************************************************/
136
137ACPI_STATUS
138AcpiRead (
139    UINT64                  *ReturnValue,
140    ACPI_GENERIC_ADDRESS    *Reg)
141{
142    ACPI_STATUS             Status;
143
144
145    ACPI_FUNCTION_NAME (AcpiRead);
146
147
148    Status = AcpiHwRead (ReturnValue, Reg);
149    return (Status);
150}
151
152ACPI_EXPORT_SYMBOL (AcpiRead)
153
154
155/******************************************************************************
156 *
157 * FUNCTION:    AcpiWrite
158 *
159 * PARAMETERS:  Value               - Value to be written
160 *              Reg                 - GAS register structure
161 *
162 * RETURN:      Status
163 *
164 * DESCRIPTION: Write to either memory or IO space.
165 *
166 ******************************************************************************/
167
168ACPI_STATUS
169AcpiWrite (
170    UINT64                  Value,
171    ACPI_GENERIC_ADDRESS    *Reg)
172{
173    ACPI_STATUS             Status;
174
175
176    ACPI_FUNCTION_NAME (AcpiWrite);
177
178
179    Status = AcpiHwWrite (Value, Reg);
180    return (Status);
181}
182
183ACPI_EXPORT_SYMBOL (AcpiWrite)
184
185
186#if (!ACPI_REDUCED_HARDWARE)
187/*******************************************************************************
188 *
189 * FUNCTION:    AcpiReadBitRegister
190 *
191 * PARAMETERS:  RegisterId      - ID of ACPI Bit Register to access
192 *              ReturnValue     - Value that was read from the register,
193 *                                normalized to bit position zero.
194 *
195 * RETURN:      Status and the value read from the specified Register. Value
196 *              returned is normalized to bit0 (is shifted all the way right)
197 *
198 * DESCRIPTION: ACPI BitRegister read function. Does not acquire the HW lock.
199 *
200 * SUPPORTS:    Bit fields in PM1 Status, PM1 Enable, PM1 Control, and
201 *              PM2 Control.
202 *
203 * Note: The hardware lock is not required when reading the ACPI bit registers
204 *       since almost all of them are single bit and it does not matter that
205 *       the parent hardware register can be split across two physical
206 *       registers. The only multi-bit field is SLP_TYP in the PM1 control
207 *       register, but this field does not cross an 8-bit boundary (nor does
208 *       it make much sense to actually read this field.)
209 *
210 ******************************************************************************/
211
212ACPI_STATUS
213AcpiReadBitRegister (
214    UINT32                  RegisterId,
215    UINT32                  *ReturnValue)
216{
217    ACPI_BIT_REGISTER_INFO  *BitRegInfo;
218    UINT32                  RegisterValue;
219    UINT32                  Value;
220    ACPI_STATUS             Status;
221
222
223    ACPI_FUNCTION_TRACE_U32 (AcpiReadBitRegister, RegisterId);
224
225
226    /* Get the info structure corresponding to the requested ACPI Register */
227
228    BitRegInfo = AcpiHwGetBitRegisterInfo (RegisterId);
229    if (!BitRegInfo)
230    {
231        return_ACPI_STATUS (AE_BAD_PARAMETER);
232    }
233
234    /* Read the entire parent register */
235
236    Status = AcpiHwRegisterRead (BitRegInfo->ParentRegister,
237        &RegisterValue);
238    if (ACPI_FAILURE (Status))
239    {
240        return_ACPI_STATUS (Status);
241    }
242
243    /* Normalize the value that was read, mask off other bits */
244
245    Value = ((RegisterValue & BitRegInfo->AccessBitMask)
246        >> BitRegInfo->BitPosition);
247
248    ACPI_DEBUG_PRINT ((ACPI_DB_IO,
249        "BitReg %X, ParentReg %X, Actual %8.8X, ReturnValue %8.8X\n",
250        RegisterId, BitRegInfo->ParentRegister, RegisterValue, Value));
251
252    *ReturnValue = Value;
253    return_ACPI_STATUS (AE_OK);
254}
255
256ACPI_EXPORT_SYMBOL (AcpiReadBitRegister)
257
258
259/*******************************************************************************
260 *
261 * FUNCTION:    AcpiWriteBitRegister
262 *
263 * PARAMETERS:  RegisterId      - ID of ACPI Bit Register to access
264 *              Value           - Value to write to the register, in bit
265 *                                position zero. The bit is automatically
266 *                                shifted to the correct position.
267 *
268 * RETURN:      Status
269 *
270 * DESCRIPTION: ACPI Bit Register write function. Acquires the hardware lock
271 *              since most operations require a read/modify/write sequence.
272 *
273 * SUPPORTS:    Bit fields in PM1 Status, PM1 Enable, PM1 Control, and
274 *              PM2 Control.
275 *
276 * Note that at this level, the fact that there may be actually two
277 * hardware registers (A and B - and B may not exist) is abstracted.
278 *
279 ******************************************************************************/
280
281ACPI_STATUS
282AcpiWriteBitRegister (
283    UINT32                  RegisterId,
284    UINT32                  Value)
285{
286    ACPI_BIT_REGISTER_INFO  *BitRegInfo;
287    ACPI_CPU_FLAGS          LockFlags;
288    UINT32                  RegisterValue;
289    ACPI_STATUS             Status = AE_OK;
290
291
292    ACPI_FUNCTION_TRACE_U32 (AcpiWriteBitRegister, RegisterId);
293
294
295    /* Get the info structure corresponding to the requested ACPI Register */
296
297    BitRegInfo = AcpiHwGetBitRegisterInfo (RegisterId);
298    if (!BitRegInfo)
299    {
300        return_ACPI_STATUS (AE_BAD_PARAMETER);
301    }
302
303    LockFlags = AcpiOsAcquireLock (AcpiGbl_HardwareLock);
304
305    /*
306     * At this point, we know that the parent register is one of the
307     * following: PM1 Status, PM1 Enable, PM1 Control, or PM2 Control
308     */
309    if (BitRegInfo->ParentRegister != ACPI_REGISTER_PM1_STATUS)
310    {
311        /*
312         * 1) Case for PM1 Enable, PM1 Control, and PM2 Control
313         *
314         * Perform a register read to preserve the bits that we are not
315         * interested in
316         */
317        Status = AcpiHwRegisterRead (BitRegInfo->ParentRegister,
318            &RegisterValue);
319        if (ACPI_FAILURE (Status))
320        {
321            goto UnlockAndExit;
322        }
323
324        /*
325         * Insert the input bit into the value that was just read
326         * and write the register
327         */
328        ACPI_REGISTER_INSERT_VALUE (RegisterValue, BitRegInfo->BitPosition,
329            BitRegInfo->AccessBitMask, Value);
330
331        Status = AcpiHwRegisterWrite (BitRegInfo->ParentRegister,
332            RegisterValue);
333    }
334    else
335    {
336        /*
337         * 2) Case for PM1 Status
338         *
339         * The Status register is different from the rest. Clear an event
340         * by writing 1, writing 0 has no effect. So, the only relevant
341         * information is the single bit we're interested in, all others
342         * should be written as 0 so they will be left unchanged.
343         */
344        RegisterValue = ACPI_REGISTER_PREPARE_BITS (Value,
345            BitRegInfo->BitPosition, BitRegInfo->AccessBitMask);
346
347        /* No need to write the register if value is all zeros */
348
349        if (RegisterValue)
350        {
351            Status = AcpiHwRegisterWrite (ACPI_REGISTER_PM1_STATUS,
352                RegisterValue);
353        }
354    }
355
356    ACPI_DEBUG_PRINT ((ACPI_DB_IO,
357        "BitReg %X, ParentReg %X, Value %8.8X, Actual %8.8X\n",
358        RegisterId, BitRegInfo->ParentRegister, Value, RegisterValue));
359
360
361UnlockAndExit:
362
363    AcpiOsReleaseLock (AcpiGbl_HardwareLock, LockFlags);
364    return_ACPI_STATUS (Status);
365}
366
367ACPI_EXPORT_SYMBOL (AcpiWriteBitRegister)
368
369#endif /* !ACPI_REDUCED_HARDWARE */
370
371
372/*******************************************************************************
373 *
374 * FUNCTION:    AcpiGetSleepTypeData
375 *
376 * PARAMETERS:  SleepState          - Numeric sleep state
377 *              *SleepTypeA         - Where SLP_TYPa is returned
378 *              *SleepTypeB         - Where SLP_TYPb is returned
379 *
380 * RETURN:      Status
381 *
382 * DESCRIPTION: Obtain the SLP_TYPa and SLP_TYPb values for the requested
383 *              sleep state via the appropriate \_Sx object.
384 *
385 *  The sleep state package returned from the corresponding \_Sx_ object
386 *  must contain at least one integer.
387 *
388 *  March 2005:
389 *  Added support for a package that contains two integers. This
390 *  goes against the ACPI specification which defines this object as a
391 *  package with one encoded DWORD integer. However, existing practice
392 *  by many BIOS vendors is to return a package with 2 or more integer
393 *  elements, at least one per sleep type (A/B).
394 *
395 *  January 2013:
396 *  Therefore, we must be prepared to accept a package with either a
397 *  single integer or multiple integers.
398 *
399 *  The single integer DWORD format is as follows:
400 *      BYTE 0 - Value for the PM1A SLP_TYP register
401 *      BYTE 1 - Value for the PM1B SLP_TYP register
402 *      BYTE 2-3 - Reserved
403 *
404 *  The dual integer format is as follows:
405 *      Integer 0 - Value for the PM1A SLP_TYP register
406 *      Integer 1 - Value for the PM1A SLP_TYP register
407 *
408 ******************************************************************************/
409
410ACPI_STATUS
411AcpiGetSleepTypeData (
412    UINT8                   SleepState,
413    UINT8                   *SleepTypeA,
414    UINT8                   *SleepTypeB)
415{
416    ACPI_STATUS             Status;
417    ACPI_EVALUATE_INFO      *Info;
418    ACPI_OPERAND_OBJECT     **Elements;
419
420
421    ACPI_FUNCTION_TRACE (AcpiGetSleepTypeData);
422
423
424    /* Validate parameters */
425
426    if ((SleepState > ACPI_S_STATES_MAX) ||
427        !SleepTypeA || !SleepTypeB)
428    {
429        return_ACPI_STATUS (AE_BAD_PARAMETER);
430    }
431
432    /* Allocate the evaluation information block */
433
434    Info = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_EVALUATE_INFO));
435    if (!Info)
436    {
437        return_ACPI_STATUS (AE_NO_MEMORY);
438    }
439
440    /*
441     * Evaluate the \_Sx namespace object containing the register values
442     * for this state
443     */
444    Info->RelativePathname = AcpiGbl_SleepStateNames[SleepState];
445
446    Status = AcpiNsEvaluate (Info);
447    if (ACPI_FAILURE (Status))
448    {
449        if (Status == AE_NOT_FOUND)
450        {
451            /* The _Sx states are optional, ignore NOT_FOUND */
452
453            goto FinalCleanup;
454        }
455
456        goto WarningCleanup;
457    }
458
459    /* Must have a return object */
460
461    if (!Info->ReturnObject)
462    {
463        ACPI_ERROR ((AE_INFO, "No Sleep State object returned from [%s]",
464            Info->RelativePathname));
465        Status = AE_AML_NO_RETURN_VALUE;
466        goto WarningCleanup;
467    }
468
469    /* Return object must be of type Package */
470
471    if (Info->ReturnObject->Common.Type != ACPI_TYPE_PACKAGE)
472    {
473        ACPI_ERROR ((AE_INFO, "Sleep State return object is not a Package"));
474        Status = AE_AML_OPERAND_TYPE;
475        goto ReturnValueCleanup;
476    }
477
478    /*
479     * Any warnings about the package length or the object types have
480     * already been issued by the predefined name module -- there is no
481     * need to repeat them here.
482     */
483    Elements = Info->ReturnObject->Package.Elements;
484    switch (Info->ReturnObject->Package.Count)
485    {
486    case 0:
487
488        Status = AE_AML_PACKAGE_LIMIT;
489        break;
490
491    case 1:
492
493        if (Elements[0]->Common.Type != ACPI_TYPE_INTEGER)
494        {
495            Status = AE_AML_OPERAND_TYPE;
496            break;
497        }
498
499        /* A valid _Sx_ package with one integer */
500
501        *SleepTypeA = (UINT8) Elements[0]->Integer.Value;
502        *SleepTypeB = (UINT8) (Elements[0]->Integer.Value >> 8);
503        break;
504
505    case 2:
506    default:
507
508        if ((Elements[0]->Common.Type != ACPI_TYPE_INTEGER) ||
509            (Elements[1]->Common.Type != ACPI_TYPE_INTEGER))
510        {
511            Status = AE_AML_OPERAND_TYPE;
512            break;
513        }
514
515        /* A valid _Sx_ package with two integers */
516
517        *SleepTypeA = (UINT8) Elements[0]->Integer.Value;
518        *SleepTypeB = (UINT8) Elements[1]->Integer.Value;
519        break;
520    }
521
522ReturnValueCleanup:
523    AcpiUtRemoveReference (Info->ReturnObject);
524
525WarningCleanup:
526    if (ACPI_FAILURE (Status))
527    {
528        ACPI_EXCEPTION ((AE_INFO, Status,
529            "While evaluating Sleep State [%s]",
530            Info->RelativePathname));
531    }
532
533FinalCleanup:
534    ACPI_FREE (Info);
535    return_ACPI_STATUS (Status);
536}
537
538ACPI_EXPORT_SYMBOL (AcpiGetSleepTypeData)
539