Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:41

0001 /******************************************************************************
0002  *
0003  * Name: acmacros.h - C macros for the entire subsystem.
0004  *
0005  *****************************************************************************/
0006 
0007 /******************************************************************************
0008  *
0009  * 1. Copyright Notice
0010  *
0011  * Some or all of this work - Copyright (c) 1999 - 2024, Intel Corp.
0012  * All rights reserved.
0013  *
0014  * 2. License
0015  *
0016  * 2.1. This is your license from Intel Corp. under its intellectual property
0017  * rights. You may have additional license terms from the party that provided
0018  * you this software, covering your right to use that party's intellectual
0019  * property rights.
0020  *
0021  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
0022  * copy of the source code appearing in this file ("Covered Code") an
0023  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
0024  * base code distributed originally by Intel ("Original Intel Code") to copy,
0025  * make derivatives, distribute, use and display any portion of the Covered
0026  * Code in any form, with the right to sublicense such rights; and
0027  *
0028  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
0029  * license (with the right to sublicense), under only those claims of Intel
0030  * patents that are infringed by the Original Intel Code, to make, use, sell,
0031  * offer to sell, and import the Covered Code and derivative works thereof
0032  * solely to the minimum extent necessary to exercise the above copyright
0033  * license, and in no event shall the patent license extend to any additions
0034  * to or modifications of the Original Intel Code. No other license or right
0035  * is granted directly or by implication, estoppel or otherwise;
0036  *
0037  * The above copyright and patent license is granted only if the following
0038  * conditions are met:
0039  *
0040  * 3. Conditions
0041  *
0042  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
0043  * Redistribution of source code of any substantial portion of the Covered
0044  * Code or modification with rights to further distribute source must include
0045  * the above Copyright Notice, the above License, this list of Conditions,
0046  * and the following Disclaimer and Export Compliance provision. In addition,
0047  * Licensee must cause all Covered Code to which Licensee contributes to
0048  * contain a file documenting the changes Licensee made to create that Covered
0049  * Code and the date of any change. Licensee must include in that file the
0050  * documentation of any changes made by any predecessor Licensee. Licensee
0051  * must include a prominent statement that the modification is derived,
0052  * directly or indirectly, from Original Intel Code.
0053  *
0054  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
0055  * Redistribution of source code of any substantial portion of the Covered
0056  * Code or modification without rights to further distribute source must
0057  * include the following Disclaimer and Export Compliance provision in the
0058  * documentation and/or other materials provided with distribution. In
0059  * addition, Licensee may not authorize further sublicense of source of any
0060  * portion of the Covered Code, and must include terms to the effect that the
0061  * license from Licensee to its licensee is limited to the intellectual
0062  * property embodied in the software Licensee provides to its licensee, and
0063  * not to intellectual property embodied in modifications its licensee may
0064  * make.
0065  *
0066  * 3.3. Redistribution of Executable. Redistribution in executable form of any
0067  * substantial portion of the Covered Code or modification must reproduce the
0068  * above Copyright Notice, and the following Disclaimer and Export Compliance
0069  * provision in the documentation and/or other materials provided with the
0070  * distribution.
0071  *
0072  * 3.4. Intel retains all right, title, and interest in and to the Original
0073  * Intel Code.
0074  *
0075  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
0076  * Intel shall be used in advertising or otherwise to promote the sale, use or
0077  * other dealings in products derived from or relating to the Covered Code
0078  * without prior written authorization from Intel.
0079  *
0080  * 4. Disclaimer and Export Compliance
0081  *
0082  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
0083  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
0084  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
0085  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
0086  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
0087  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
0088  * PARTICULAR PURPOSE.
0089  *
0090  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
0091  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
0092  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
0093  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
0094  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
0095  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
0096  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
0097  * LIMITED REMEDY.
0098  *
0099  * 4.3. Licensee shall not export, either directly or indirectly, any of this
0100  * software or system incorporating such software without first obtaining any
0101  * required license or other approval from the U. S. Department of Commerce or
0102  * any other agency or department of the United States Government. In the
0103  * event Licensee exports any such software from the United States or
0104  * re-exports any such software from a foreign destination, Licensee shall
0105  * ensure that the distribution and export/re-export of the software is in
0106  * compliance with all laws, regulations, orders, or other restrictions of the
0107  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
0108  * any of its subsidiaries will export/re-export any technical data, process,
0109  * software, or service, directly or indirectly, to any country for which the
0110  * United States government or any agency thereof requires an export license,
0111  * other governmental approval, or letter of assurance, without first obtaining
0112  * such license, approval or letter.
0113  *
0114  *****************************************************************************
0115  *
0116  * Alternatively, you may choose to be licensed under the terms of the
0117  * following license:
0118  *
0119  * Redistribution and use in source and binary forms, with or without
0120  * modification, are permitted provided that the following conditions
0121  * are met:
0122  * 1. Redistributions of source code must retain the above copyright
0123  *    notice, this list of conditions, and the following disclaimer,
0124  *    without modification.
0125  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
0126  *    substantially similar to the "NO WARRANTY" disclaimer below
0127  *    ("Disclaimer") and any redistribution must be conditioned upon
0128  *    including a substantially similar Disclaimer requirement for further
0129  *    binary redistribution.
0130  * 3. Neither the names of the above-listed copyright holders nor the names
0131  *    of any contributors may be used to endorse or promote products derived
0132  *    from this software without specific prior written permission.
0133  *
0134  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0135  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0136  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0137  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0138  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0139  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0140  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0141  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0142  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0143  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0144  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0145  *
0146  * Alternatively, you may choose to be licensed under the terms of the
0147  * GNU General Public License ("GPL") version 2 as published by the Free
0148  * Software Foundation.
0149  *
0150  *****************************************************************************/
0151 
0152 #ifndef __ACMACROS_H__
0153 #define __ACMACROS_H__
0154 
0155 
0156 /*
0157  * Extract data using a pointer. Any more than a byte and we
0158  * get into potential alignment issues -- see the STORE macros below.
0159  * Use with care.
0160  */
0161 #define ACPI_CAST8(ptr)                 ACPI_CAST_PTR (UINT8, (ptr))
0162 #define ACPI_CAST16(ptr)                ACPI_CAST_PTR (UINT16, (ptr))
0163 #define ACPI_CAST32(ptr)                ACPI_CAST_PTR (UINT32, (ptr))
0164 #define ACPI_CAST64(ptr)                ACPI_CAST_PTR (UINT64, (ptr))
0165 #define ACPI_GET8(ptr)                  (*ACPI_CAST8 (ptr))
0166 #define ACPI_GET16(ptr)                 (*ACPI_CAST16 (ptr))
0167 #define ACPI_GET32(ptr)                 (*ACPI_CAST32 (ptr))
0168 #define ACPI_GET64(ptr)                 (*ACPI_CAST64 (ptr))
0169 #define ACPI_SET8(ptr, val)             (*ACPI_CAST8 (ptr) = (UINT8) (val))
0170 #define ACPI_SET16(ptr, val)            (*ACPI_CAST16 (ptr) = (UINT16) (val))
0171 #define ACPI_SET32(ptr, val)            (*ACPI_CAST32 (ptr) = (UINT32) (val))
0172 #define ACPI_SET64(ptr, val)            (*ACPI_CAST64 (ptr) = (UINT64) (val))
0173 
0174 /*
0175  * printf() format helper. This macro is a workaround for the difficulties
0176  * with emitting 64-bit integers and 64-bit pointers with the same code
0177  * for both 32-bit and 64-bit hosts.
0178  */
0179 #define ACPI_FORMAT_UINT64(i)           ACPI_HIDWORD(i), ACPI_LODWORD(i)
0180 
0181 
0182 /*
0183  * Macros for moving data around to/from buffers that are possibly unaligned.
0184  * If the hardware supports the transfer of unaligned data, just do the store.
0185  * Otherwise, we have to move one byte at a time.
0186  */
0187 #ifdef ACPI_BIG_ENDIAN
0188 /*
0189  * Macros for big-endian machines
0190  */
0191 
0192 /* These macros reverse the bytes during the move, converting little-endian to big endian */
0193 
0194                                                      /* Big Endian      <==        Little Endian */
0195                                                      /*  Hi...Lo                     Lo...Hi     */
0196 /* 16-bit source, 16/32/64 destination */
0197 
0198 #define ACPI_MOVE_16_TO_16(d, s)        {((  UINT8 *)(void *)(d))[0] = ((UINT8 *)(void *)(s))[1];\
0199                                          ((  UINT8 *)(void *)(d))[1] = ((UINT8 *)(void *)(s))[0];}
0200 
0201 #define ACPI_MOVE_16_TO_32(d, s)        {(*(UINT32 *)(void *)(d))=0;\
0202                                            ((UINT8 *)(void *)(d))[2] = ((UINT8 *)(void *)(s))[1];\
0203                                            ((UINT8 *)(void *)(d))[3] = ((UINT8 *)(void *)(s))[0];}
0204 
0205 #define ACPI_MOVE_16_TO_64(d, s)        {(*(UINT64 *)(void *)(d))=0;\
0206                                            ((UINT8 *)(void *)(d))[6] = ((UINT8 *)(void *)(s))[1];\
0207                                            ((UINT8 *)(void *)(d))[7] = ((UINT8 *)(void *)(s))[0];}
0208 
0209 /* 32-bit source, 16/32/64 destination */
0210 
0211 #define ACPI_MOVE_32_TO_16(d, s)        ACPI_MOVE_16_TO_16(d, s)    /* Truncate to 16 */
0212 
0213 #define ACPI_MOVE_32_TO_32(d, s)        {((  UINT8 *)(void *)(d))[0] = ((UINT8 *)(void *)(s))[3];\
0214                                          ((  UINT8 *)(void *)(d))[1] = ((UINT8 *)(void *)(s))[2];\
0215                                          ((  UINT8 *)(void *)(d))[2] = ((UINT8 *)(void *)(s))[1];\
0216                                          ((  UINT8 *)(void *)(d))[3] = ((UINT8 *)(void *)(s))[0];}
0217 
0218 #define ACPI_MOVE_32_TO_64(d, s)        {(*(UINT64 *)(void *)(d))=0;\
0219                                            ((UINT8 *)(void *)(d))[4] = ((UINT8 *)(void *)(s))[3];\
0220                                            ((UINT8 *)(void *)(d))[5] = ((UINT8 *)(void *)(s))[2];\
0221                                            ((UINT8 *)(void *)(d))[6] = ((UINT8 *)(void *)(s))[1];\
0222                                            ((UINT8 *)(void *)(d))[7] = ((UINT8 *)(void *)(s))[0];}
0223 
0224 /* 64-bit source, 16/32/64 destination */
0225 
0226 #define ACPI_MOVE_64_TO_16(d, s)        ACPI_MOVE_16_TO_16(d, s)    /* Truncate to 16 */
0227 
0228 #define ACPI_MOVE_64_TO_32(d, s)        ACPI_MOVE_32_TO_32(d, s)    /* Truncate to 32 */
0229 
0230 #define ACPI_MOVE_64_TO_64(d, s)        {((  UINT8 *)(void *)(d))[0] = ((UINT8 *)(void *)(s))[7];\
0231                                          ((  UINT8 *)(void *)(d))[1] = ((UINT8 *)(void *)(s))[6];\
0232                                          ((  UINT8 *)(void *)(d))[2] = ((UINT8 *)(void *)(s))[5];\
0233                                          ((  UINT8 *)(void *)(d))[3] = ((UINT8 *)(void *)(s))[4];\
0234                                          ((  UINT8 *)(void *)(d))[4] = ((UINT8 *)(void *)(s))[3];\
0235                                          ((  UINT8 *)(void *)(d))[5] = ((UINT8 *)(void *)(s))[2];\
0236                                          ((  UINT8 *)(void *)(d))[6] = ((UINT8 *)(void *)(s))[1];\
0237                                          ((  UINT8 *)(void *)(d))[7] = ((UINT8 *)(void *)(s))[0];}
0238 #else
0239 /*
0240  * Macros for little-endian machines
0241  */
0242 
0243 #ifndef ACPI_MISALIGNMENT_NOT_SUPPORTED
0244 
0245 /* The hardware supports unaligned transfers, just do the little-endian move */
0246 
0247 /* 16-bit source, 16/32/64 destination */
0248 
0249 #define ACPI_MOVE_16_TO_16(d, s)        *(UINT16 *)(void *)(d) = *(UINT16 *)(void *)(s)
0250 #define ACPI_MOVE_16_TO_32(d, s)        *(UINT32 *)(void *)(d) = *(UINT16 *)(void *)(s)
0251 #define ACPI_MOVE_16_TO_64(d, s)        *(UINT64 *)(void *)(d) = *(UINT16 *)(void *)(s)
0252 
0253 /* 32-bit source, 16/32/64 destination */
0254 
0255 #define ACPI_MOVE_32_TO_16(d, s)        ACPI_MOVE_16_TO_16(d, s)    /* Truncate to 16 */
0256 #define ACPI_MOVE_32_TO_32(d, s)        *(UINT32 *)(void *)(d) = *(UINT32 *)(void *)(s)
0257 #define ACPI_MOVE_32_TO_64(d, s)        *(UINT64 *)(void *)(d) = *(UINT32 *)(void *)(s)
0258 
0259 /* 64-bit source, 16/32/64 destination */
0260 
0261 #define ACPI_MOVE_64_TO_16(d, s)        ACPI_MOVE_16_TO_16(d, s)    /* Truncate to 16 */
0262 #define ACPI_MOVE_64_TO_32(d, s)        ACPI_MOVE_32_TO_32(d, s)    /* Truncate to 32 */
0263 #define ACPI_MOVE_64_TO_64(d, s)        *(UINT64 *)(void *)(d) = *(UINT64 *)(void *)(s)
0264 
0265 #else
0266 /*
0267  * The hardware does not support unaligned transfers. We must move the
0268  * data one byte at a time. These macros work whether the source or
0269  * the destination (or both) is/are unaligned. (Little-endian move)
0270  */
0271 
0272 /* 16-bit source, 16/32/64 destination */
0273 
0274 #define ACPI_MOVE_16_TO_16(d, s)        {((  UINT8 *)(void *)(d))[0] = ((UINT8 *)(void *)(s))[0];\
0275                                          ((  UINT8 *)(void *)(d))[1] = ((UINT8 *)(void *)(s))[1];}
0276 
0277 #define ACPI_MOVE_16_TO_32(d, s)        {(*(UINT32 *)(void *)(d)) = 0; ACPI_MOVE_16_TO_16(d, s);}
0278 #define ACPI_MOVE_16_TO_64(d, s)        {(*(UINT64 *)(void *)(d)) = 0; ACPI_MOVE_16_TO_16(d, s);}
0279 
0280 /* 32-bit source, 16/32/64 destination */
0281 
0282 #define ACPI_MOVE_32_TO_16(d, s)        ACPI_MOVE_16_TO_16(d, s)    /* Truncate to 16 */
0283 
0284 #define ACPI_MOVE_32_TO_32(d, s)        {((  UINT8 *)(void *)(d))[0] = ((UINT8 *)(void *)(s))[0];\
0285                                          ((  UINT8 *)(void *)(d))[1] = ((UINT8 *)(void *)(s))[1];\
0286                                          ((  UINT8 *)(void *)(d))[2] = ((UINT8 *)(void *)(s))[2];\
0287                                          ((  UINT8 *)(void *)(d))[3] = ((UINT8 *)(void *)(s))[3];}
0288 
0289 #define ACPI_MOVE_32_TO_64(d, s)        {(*(UINT64 *)(void *)(d)) = 0; ACPI_MOVE_32_TO_32(d, s);}
0290 
0291 /* 64-bit source, 16/32/64 destination */
0292 
0293 #define ACPI_MOVE_64_TO_16(d, s)        ACPI_MOVE_16_TO_16(d, s)    /* Truncate to 16 */
0294 #define ACPI_MOVE_64_TO_32(d, s)        ACPI_MOVE_32_TO_32(d, s)    /* Truncate to 32 */
0295 #define ACPI_MOVE_64_TO_64(d, s)        {((  UINT8 *)(void *)(d))[0] = ((UINT8 *)(void *)(s))[0];\
0296                                          ((  UINT8 *)(void *)(d))[1] = ((UINT8 *)(void *)(s))[1];\
0297                                          ((  UINT8 *)(void *)(d))[2] = ((UINT8 *)(void *)(s))[2];\
0298                                          ((  UINT8 *)(void *)(d))[3] = ((UINT8 *)(void *)(s))[3];\
0299                                          ((  UINT8 *)(void *)(d))[4] = ((UINT8 *)(void *)(s))[4];\
0300                                          ((  UINT8 *)(void *)(d))[5] = ((UINT8 *)(void *)(s))[5];\
0301                                          ((  UINT8 *)(void *)(d))[6] = ((UINT8 *)(void *)(s))[6];\
0302                                          ((  UINT8 *)(void *)(d))[7] = ((UINT8 *)(void *)(s))[7];}
0303 #endif
0304 #endif
0305 
0306 
0307 /*
0308  * Fast power-of-two math macros for non-optimized compilers
0309  */
0310 #define _ACPI_DIV(value, PowerOf2)      ((UINT32) ((value) >> (PowerOf2)))
0311 #define _ACPI_MUL(value, PowerOf2)      ((UINT32) ((value) << (PowerOf2)))
0312 #define _ACPI_MOD(value, Divisor)       ((UINT32) ((value) & ((Divisor) -1)))
0313 
0314 #define ACPI_DIV_2(a)                   _ACPI_DIV(a, 1)
0315 #define ACPI_MUL_2(a)                   _ACPI_MUL(a, 1)
0316 #define ACPI_MOD_2(a)                   _ACPI_MOD(a, 2)
0317 
0318 #define ACPI_DIV_4(a)                   _ACPI_DIV(a, 2)
0319 #define ACPI_MUL_4(a)                   _ACPI_MUL(a, 2)
0320 #define ACPI_MOD_4(a)                   _ACPI_MOD(a, 4)
0321 
0322 #define ACPI_DIV_8(a)                   _ACPI_DIV(a, 3)
0323 #define ACPI_MUL_8(a)                   _ACPI_MUL(a, 3)
0324 #define ACPI_MOD_8(a)                   _ACPI_MOD(a, 8)
0325 
0326 #define ACPI_DIV_16(a)                  _ACPI_DIV(a, 4)
0327 #define ACPI_MUL_16(a)                  _ACPI_MUL(a, 4)
0328 #define ACPI_MOD_16(a)                  _ACPI_MOD(a, 16)
0329 
0330 #define ACPI_DIV_32(a)                  _ACPI_DIV(a, 5)
0331 #define ACPI_MUL_32(a)                  _ACPI_MUL(a, 5)
0332 #define ACPI_MOD_32(a)                  _ACPI_MOD(a, 32)
0333 
0334 /* Test for ASCII character */
0335 
0336 #define ACPI_IS_ASCII(c)                ((c) < 0x80)
0337 
0338 /* Signed integers */
0339 
0340 #define ACPI_SIGN_POSITIVE              0
0341 #define ACPI_SIGN_NEGATIVE              1
0342 
0343 
0344 /*
0345  * Rounding macros (Power of two boundaries only)
0346  */
0347 #define ACPI_ROUND_DOWN(value, boundary)    (((ACPI_SIZE)(value)) & \
0348                                                 (~(((ACPI_SIZE) boundary)-1)))
0349 
0350 #define ACPI_ROUND_UP(value, boundary)      ((((ACPI_SIZE)(value)) + \
0351                                                 (((ACPI_SIZE) boundary)-1)) & \
0352                                                 (~(((ACPI_SIZE) boundary)-1)))
0353 
0354 /* Note: sizeof(ACPI_SIZE) evaluates to either 4 or 8 (32- vs 64-bit mode) */
0355 
0356 #define ACPI_ROUND_DOWN_TO_32BIT(a)         ACPI_ROUND_DOWN(a, 4)
0357 #define ACPI_ROUND_DOWN_TO_64BIT(a)         ACPI_ROUND_DOWN(a, 8)
0358 #define ACPI_ROUND_DOWN_TO_NATIVE_WORD(a)   ACPI_ROUND_DOWN(a, sizeof(ACPI_SIZE))
0359 
0360 #define ACPI_ROUND_UP_TO_32BIT(a)           ACPI_ROUND_UP(a, 4)
0361 #define ACPI_ROUND_UP_TO_64BIT(a)           ACPI_ROUND_UP(a, 8)
0362 #define ACPI_ROUND_UP_TO_NATIVE_WORD(a)     ACPI_ROUND_UP(a, sizeof(ACPI_SIZE))
0363 
0364 #define ACPI_ROUND_BITS_UP_TO_BYTES(a)      ACPI_DIV_8((a) + 7)
0365 #define ACPI_ROUND_BITS_DOWN_TO_BYTES(a)    ACPI_DIV_8((a))
0366 
0367 #define ACPI_ROUND_UP_TO_1K(a)              (((a) + 1023) >> 10)
0368 
0369 /* Generic (non-power-of-two) rounding */
0370 
0371 #define ACPI_ROUND_UP_TO(value, boundary)   (((value) + ((boundary)-1)) / (boundary))
0372 
0373 #define ACPI_IS_MISALIGNED(value)           (((ACPI_SIZE) value) & (sizeof(ACPI_SIZE)-1))
0374 
0375 /* Generic bit manipulation */
0376 
0377 #ifndef ACPI_USE_NATIVE_BIT_FINDER
0378 
0379 #define __ACPI_FIND_LAST_BIT_2(a, r)        ((((UINT8)  (a)) & 0x02) ? (r)+1 : (r))
0380 #define __ACPI_FIND_LAST_BIT_4(a, r)        ((((UINT8)  (a)) & 0x0C) ? \
0381                                              __ACPI_FIND_LAST_BIT_2  ((a)>>2,  (r)+2) : \
0382                                              __ACPI_FIND_LAST_BIT_2  ((a), (r)))
0383 #define __ACPI_FIND_LAST_BIT_8(a, r)        ((((UINT8)  (a)) & 0xF0) ? \
0384                                              __ACPI_FIND_LAST_BIT_4  ((a)>>4,  (r)+4) : \
0385                                              __ACPI_FIND_LAST_BIT_4  ((a), (r)))
0386 #define __ACPI_FIND_LAST_BIT_16(a, r)       ((((UINT16) (a)) & 0xFF00) ? \
0387                                              __ACPI_FIND_LAST_BIT_8  ((a)>>8,  (r)+8) : \
0388                                              __ACPI_FIND_LAST_BIT_8  ((a), (r)))
0389 #define __ACPI_FIND_LAST_BIT_32(a, r)       ((((UINT32) (a)) & 0xFFFF0000) ? \
0390                                              __ACPI_FIND_LAST_BIT_16 ((a)>>16, (r)+16) : \
0391                                              __ACPI_FIND_LAST_BIT_16 ((a), (r)))
0392 #define __ACPI_FIND_LAST_BIT_64(a, r)       ((((UINT64) (a)) & 0xFFFFFFFF00000000) ? \
0393                                              __ACPI_FIND_LAST_BIT_32 ((a)>>32, (r)+32) : \
0394                                              __ACPI_FIND_LAST_BIT_32 ((a), (r)))
0395 
0396 #define ACPI_FIND_LAST_BIT_8(a)             ((a) ? __ACPI_FIND_LAST_BIT_8 (a, 1) : 0)
0397 #define ACPI_FIND_LAST_BIT_16(a)            ((a) ? __ACPI_FIND_LAST_BIT_16 (a, 1) : 0)
0398 #define ACPI_FIND_LAST_BIT_32(a)            ((a) ? __ACPI_FIND_LAST_BIT_32 (a, 1) : 0)
0399 #define ACPI_FIND_LAST_BIT_64(a)            ((a) ? __ACPI_FIND_LAST_BIT_64 (a, 1) : 0)
0400 
0401 #define __ACPI_FIND_FIRST_BIT_2(a, r)       ((((UINT8) (a)) & 0x01) ? (r) : (r)+1)
0402 #define __ACPI_FIND_FIRST_BIT_4(a, r)       ((((UINT8) (a)) & 0x03) ? \
0403                                              __ACPI_FIND_FIRST_BIT_2  ((a), (r)) : \
0404                                              __ACPI_FIND_FIRST_BIT_2  ((a)>>2, (r)+2))
0405 #define __ACPI_FIND_FIRST_BIT_8(a, r)       ((((UINT8) (a)) & 0x0F) ? \
0406                                              __ACPI_FIND_FIRST_BIT_4  ((a), (r)) : \
0407                                              __ACPI_FIND_FIRST_BIT_4  ((a)>>4, (r)+4))
0408 #define __ACPI_FIND_FIRST_BIT_16(a, r)      ((((UINT16) (a)) & 0x00FF) ? \
0409                                              __ACPI_FIND_FIRST_BIT_8  ((a), (r)) : \
0410                                              __ACPI_FIND_FIRST_BIT_8  ((a)>>8, (r)+8))
0411 #define __ACPI_FIND_FIRST_BIT_32(a, r)      ((((UINT32) (a)) & 0x0000FFFF) ? \
0412                                              __ACPI_FIND_FIRST_BIT_16 ((a), (r)) : \
0413                                              __ACPI_FIND_FIRST_BIT_16 ((a)>>16, (r)+16))
0414 #define __ACPI_FIND_FIRST_BIT_64(a, r)      ((((UINT64) (a)) & 0x00000000FFFFFFFF) ? \
0415                                              __ACPI_FIND_FIRST_BIT_32 ((a), (r)) : \
0416                                              __ACPI_FIND_FIRST_BIT_32 ((a)>>32, (r)+32))
0417 
0418 #define ACPI_FIND_FIRST_BIT_8(a)            ((a) ? __ACPI_FIND_FIRST_BIT_8 (a, 1) : 0)
0419 #define ACPI_FIND_FIRST_BIT_16(a)           ((a) ? __ACPI_FIND_FIRST_BIT_16 (a, 1) : 0)
0420 #define ACPI_FIND_FIRST_BIT_32(a)           ((a) ? __ACPI_FIND_FIRST_BIT_32 (a, 1) : 0)
0421 #define ACPI_FIND_FIRST_BIT_64(a)           ((a) ? __ACPI_FIND_FIRST_BIT_64 (a, 1) : 0)
0422 
0423 #endif /* ACPI_USE_NATIVE_BIT_FINDER */
0424 
0425 /* Generic (power-of-two) rounding */
0426 
0427 #define ACPI_ROUND_UP_POWER_OF_TWO_8(a)     ((UINT8) \
0428                                             (((UINT16) 1) <<  ACPI_FIND_LAST_BIT_8  ((a)  - 1)))
0429 #define ACPI_ROUND_DOWN_POWER_OF_TWO_8(a)   ((UINT8) \
0430                                             (((UINT16) 1) << (ACPI_FIND_LAST_BIT_8  ((a)) - 1)))
0431 #define ACPI_ROUND_UP_POWER_OF_TWO_16(a)    ((UINT16) \
0432                                             (((UINT32) 1) <<  ACPI_FIND_LAST_BIT_16 ((a)  - 1)))
0433 #define ACPI_ROUND_DOWN_POWER_OF_TWO_16(a)  ((UINT16) \
0434                                             (((UINT32) 1) << (ACPI_FIND_LAST_BIT_16 ((a)) - 1)))
0435 #define ACPI_ROUND_UP_POWER_OF_TWO_32(a)    ((UINT32) \
0436                                             (((UINT64) 1) <<  ACPI_FIND_LAST_BIT_32 ((a)  - 1)))
0437 #define ACPI_ROUND_DOWN_POWER_OF_TWO_32(a)  ((UINT32) \
0438                                             (((UINT64) 1) << (ACPI_FIND_LAST_BIT_32 ((a)) - 1)))
0439 #define ACPI_IS_ALIGNED(a, s)               (((a) & ((s) - 1)) == 0)
0440 #define ACPI_IS_POWER_OF_TWO(a)             ACPI_IS_ALIGNED(a, a)
0441 
0442 /*
0443  * Bitmask creation
0444  * Bit positions start at zero.
0445  * MASK_BITS_ABOVE creates a mask starting AT the position and above
0446  * MASK_BITS_BELOW creates a mask starting one bit BELOW the position
0447  * MASK_BITS_ABOVE/BELOW accepts a bit offset to create a mask
0448  * MASK_BITS_ABOVE/BELOW_32/64 accepts a bit width to create a mask
0449  * Note: The ACPI_INTEGER_BIT_SIZE check is used to bypass compiler
0450  * differences with the shift operator
0451  */
0452 #define ACPI_MASK_BITS_ABOVE(position)      (~((ACPI_UINT64_MAX) << ((UINT32) (position))))
0453 #define ACPI_MASK_BITS_BELOW(position)      ((ACPI_UINT64_MAX) << ((UINT32) (position)))
0454 #define ACPI_MASK_BITS_ABOVE_32(width)      ((UINT32) ACPI_MASK_BITS_ABOVE(width))
0455 #define ACPI_MASK_BITS_BELOW_32(width)      ((UINT32) ACPI_MASK_BITS_BELOW(width))
0456 #define ACPI_MASK_BITS_ABOVE_64(width)      ((width) == ACPI_INTEGER_BIT_SIZE ? \
0457                                                 ACPI_UINT64_MAX : \
0458                                                 ACPI_MASK_BITS_ABOVE(width))
0459 #define ACPI_MASK_BITS_BELOW_64(width)      ((width) == ACPI_INTEGER_BIT_SIZE ? \
0460                                                 (UINT64) 0 : \
0461                                                 ACPI_MASK_BITS_BELOW(width))
0462 
0463 /* Bitfields within ACPI registers */
0464 
0465 #define ACPI_REGISTER_PREPARE_BITS(Val, Pos, Mask) \
0466     ((Val << Pos) & Mask)
0467 
0468 #define ACPI_REGISTER_INSERT_VALUE(Reg, Pos, Mask, Val) \
0469     Reg = (Reg & (~(Mask))) | ACPI_REGISTER_PREPARE_BITS(Val, Pos, Mask)
0470 
0471 #define ACPI_INSERT_BITS(Target, Mask, Source) \
0472     Target = ((Target & (~(Mask))) | (Source & Mask))
0473 
0474 /* Generic bitfield macros and masks */
0475 
0476 #define ACPI_GET_BITS(SourcePtr, Position, Mask) \
0477     ((*(SourcePtr) >> (Position)) & (Mask))
0478 
0479 #define ACPI_SET_BITS(TargetPtr, Position, Mask, Value) \
0480     (*(TargetPtr) |= (((Value) & (Mask)) << (Position)))
0481 
0482 #define ACPI_1BIT_MASK      0x00000001
0483 #define ACPI_2BIT_MASK      0x00000003
0484 #define ACPI_3BIT_MASK      0x00000007
0485 #define ACPI_4BIT_MASK      0x0000000F
0486 #define ACPI_5BIT_MASK      0x0000001F
0487 #define ACPI_6BIT_MASK      0x0000003F
0488 #define ACPI_7BIT_MASK      0x0000007F
0489 #define ACPI_8BIT_MASK      0x000000FF
0490 #define ACPI_16BIT_MASK     0x0000FFFF
0491 #define ACPI_24BIT_MASK     0x00FFFFFF
0492 
0493 /* Macros to extract flag bits from position zero */
0494 
0495 #define ACPI_GET_1BIT_FLAG(Value)                   ((Value) & ACPI_1BIT_MASK)
0496 #define ACPI_GET_2BIT_FLAG(Value)                   ((Value) & ACPI_2BIT_MASK)
0497 #define ACPI_GET_3BIT_FLAG(Value)                   ((Value) & ACPI_3BIT_MASK)
0498 #define ACPI_GET_4BIT_FLAG(Value)                   ((Value) & ACPI_4BIT_MASK)
0499 
0500 /* Macros to extract flag bits from position one and above */
0501 
0502 #define ACPI_EXTRACT_1BIT_FLAG(Field, Position)     (ACPI_GET_1BIT_FLAG ((Field) >> Position))
0503 #define ACPI_EXTRACT_2BIT_FLAG(Field, Position)     (ACPI_GET_2BIT_FLAG ((Field) >> Position))
0504 #define ACPI_EXTRACT_3BIT_FLAG(Field, Position)     (ACPI_GET_3BIT_FLAG ((Field) >> Position))
0505 #define ACPI_EXTRACT_4BIT_FLAG(Field, Position)     (ACPI_GET_4BIT_FLAG ((Field) >> Position))
0506 
0507 /* ACPI Pathname helpers */
0508 
0509 #define ACPI_IS_ROOT_PREFIX(c)      ((c) == (UINT8) 0x5C) /* Backslash */
0510 #define ACPI_IS_PARENT_PREFIX(c)    ((c) == (UINT8) 0x5E) /* Carat */
0511 #define ACPI_IS_PATH_SEPARATOR(c)   ((c) == (UINT8) 0x2E) /* Period (dot) */
0512 
0513 /*
0514  * An object of type ACPI_NAMESPACE_NODE can appear in some contexts
0515  * where a pointer to an object of type ACPI_OPERAND_OBJECT can also
0516  * appear. This macro is used to distinguish them.
0517  *
0518  * The "DescriptorType" field is the second field in both structures.
0519  */
0520 #define ACPI_GET_DESCRIPTOR_PTR(d)      (((ACPI_DESCRIPTOR *)(void *)(d))->Common.CommonPointer)
0521 #define ACPI_SET_DESCRIPTOR_PTR(d, p)   (((ACPI_DESCRIPTOR *)(void *)(d))->Common.CommonPointer = (p))
0522 #define ACPI_GET_DESCRIPTOR_TYPE(d)     (((ACPI_DESCRIPTOR *)(void *)(d))->Common.DescriptorType)
0523 #define ACPI_SET_DESCRIPTOR_TYPE(d, t)  (((ACPI_DESCRIPTOR *)(void *)(d))->Common.DescriptorType = (t))
0524 
0525 /*
0526  * Macros for the master AML opcode table
0527  */
0528 #if defined (ACPI_DISASSEMBLER) || defined (ACPI_DEBUG_OUTPUT)
0529 #define ACPI_OP(Name, PArgs, IArgs, ObjType, Class, Type, Flags) \
0530     {Name, (UINT32)(PArgs), (UINT32)(IArgs), (UINT32)(Flags), ObjType, Class, Type}
0531 #else
0532 #define ACPI_OP(Name, PArgs, IArgs, ObjType, Class, Type, Flags) \
0533     {(UINT32)(PArgs), (UINT32)(IArgs), (UINT32)(Flags), ObjType, Class, Type}
0534 #endif
0535 
0536 #define ARG_TYPE_WIDTH                  5
0537 #define ARG_1(x)                        ((UINT32)(x))
0538 #define ARG_2(x)                        ((UINT32)(x) << (1 * ARG_TYPE_WIDTH))
0539 #define ARG_3(x)                        ((UINT32)(x) << (2 * ARG_TYPE_WIDTH))
0540 #define ARG_4(x)                        ((UINT32)(x) << (3 * ARG_TYPE_WIDTH))
0541 #define ARG_5(x)                        ((UINT32)(x) << (4 * ARG_TYPE_WIDTH))
0542 #define ARG_6(x)                        ((UINT32)(x) << (5 * ARG_TYPE_WIDTH))
0543 
0544 #define ARGI_LIST1(a)                   (ARG_1(a))
0545 #define ARGI_LIST2(a, b)                (ARG_1(b)|ARG_2(a))
0546 #define ARGI_LIST3(a, b, c)             (ARG_1(c)|ARG_2(b)|ARG_3(a))
0547 #define ARGI_LIST4(a, b, c, d)          (ARG_1(d)|ARG_2(c)|ARG_3(b)|ARG_4(a))
0548 #define ARGI_LIST5(a, b, c, d, e)       (ARG_1(e)|ARG_2(d)|ARG_3(c)|ARG_4(b)|ARG_5(a))
0549 #define ARGI_LIST6(a, b, c, d, e, f)    (ARG_1(f)|ARG_2(e)|ARG_3(d)|ARG_4(c)|ARG_5(b)|ARG_6(a))
0550 
0551 #define ARGP_LIST1(a)                   (ARG_1(a))
0552 #define ARGP_LIST2(a, b)                (ARG_1(a)|ARG_2(b))
0553 #define ARGP_LIST3(a, b, c)             (ARG_1(a)|ARG_2(b)|ARG_3(c))
0554 #define ARGP_LIST4(a, b, c, d)          (ARG_1(a)|ARG_2(b)|ARG_3(c)|ARG_4(d))
0555 #define ARGP_LIST5(a, b, c, d, e)       (ARG_1(a)|ARG_2(b)|ARG_3(c)|ARG_4(d)|ARG_5(e))
0556 #define ARGP_LIST6(a, b, c, d, e, f)    (ARG_1(a)|ARG_2(b)|ARG_3(c)|ARG_4(d)|ARG_5(e)|ARG_6(f))
0557 
0558 #define GET_CURRENT_ARG_TYPE(List)      (List & ((UINT32) 0x1F))
0559 #define INCREMENT_ARG_LIST(List)        (List >>= ((UINT32) ARG_TYPE_WIDTH))
0560 
0561 /*
0562  * Ascii error messages can be configured out
0563  */
0564 #ifndef ACPI_NO_ERROR_MESSAGES
0565 /*
0566  * Error reporting. The callers module and line number are inserted by AE_INFO,
0567  * the plist contains a set of parens to allow variable-length lists.
0568  * These macros are used for both the debug and non-debug versions of the code.
0569  */
0570 #define ACPI_ERROR_NAMESPACE(s, p, e)       AcpiUtPrefixedNamespaceError (AE_INFO, s, p, e);
0571 #define ACPI_ERROR_METHOD(s, n, p, e)       AcpiUtMethodError (AE_INFO, s, n, p, e);
0572 #define ACPI_WARN_PREDEFINED(plist)         AcpiUtPredefinedWarning plist
0573 #define ACPI_INFO_PREDEFINED(plist)         AcpiUtPredefinedInfo plist
0574 #define ACPI_BIOS_ERROR_PREDEFINED(plist)   AcpiUtPredefinedBiosError plist
0575 #define ACPI_ERROR_ONLY(s)                  s
0576 
0577 #else
0578 
0579 /* No error messages */
0580 
0581 #define ACPI_ERROR_NAMESPACE(s, p, e)
0582 #define ACPI_ERROR_METHOD(s, n, p, e)
0583 #define ACPI_WARN_PREDEFINED(plist)
0584 #define ACPI_INFO_PREDEFINED(plist)
0585 #define ACPI_BIOS_ERROR_PREDEFINED(plist)
0586 #define ACPI_ERROR_ONLY(s)
0587 
0588 #endif /* ACPI_NO_ERROR_MESSAGES */
0589 
0590 #if (!ACPI_REDUCED_HARDWARE)
0591 #define ACPI_HW_OPTIONAL_FUNCTION(addr)     addr
0592 #else
0593 #define ACPI_HW_OPTIONAL_FUNCTION(addr)     NULL
0594 #endif
0595 
0596 
0597 /*
0598  * Macros used for ACPICA utilities only
0599  */
0600 
0601 /* Generate a UUID */
0602 
0603 #define ACPI_INIT_UUID(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \
0604     (a) & 0xFF, ((a) >> 8) & 0xFF, ((a) >> 16) & 0xFF, ((a) >> 24) & 0xFF, \
0605     (b) & 0xFF, ((b) >> 8) & 0xFF, \
0606     (c) & 0xFF, ((c) >> 8) & 0xFF, \
0607     (d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7)
0608 
0609 #define ACPI_IS_OCTAL_DIGIT(d)              (((char)(d) >= '0') && ((char)(d) <= '7'))
0610 
0611 
0612 /*
0613  * Macros used for the ASL-/ASL+ converter utility
0614  */
0615 #ifdef ACPI_ASL_COMPILER
0616 
0617 #define ASL_CV_LABEL_FILENODE(a)         CvLabelFileNode(a);
0618 #define ASL_CV_CAPTURE_COMMENTS_ONLY(a)   CvCaptureCommentsOnly (a);
0619 #define ASL_CV_CAPTURE_COMMENTS(a)       CvCaptureComments (a);
0620 #define ASL_CV_TRANSFER_COMMENTS(a)      CvTransferComments (a);
0621 #define ASL_CV_CLOSE_PAREN(a,b)          CvCloseParenWriteComment(a,b);
0622 #define ASL_CV_CLOSE_BRACE(a,b)          CvCloseBraceWriteComment(a,b);
0623 #define ASL_CV_SWITCH_FILES(a,b)         CvSwitchFiles(a,b);
0624 #define ASL_CV_CLEAR_OP_COMMENTS(a)       CvClearOpComments(a);
0625 #define ASL_CV_PRINT_ONE_COMMENT(a,b,c,d) CvPrintOneCommentType (a,b,c,d);
0626 #define ASL_CV_PRINT_ONE_COMMENT_LIST(a,b) CvPrintOneCommentList (a,b);
0627 #define ASL_CV_FILE_HAS_SWITCHED(a)       CvFileHasSwitched(a)
0628 #define ASL_CV_INIT_FILETREE(a,b)      CvInitFileTree(a,b);
0629 
0630 #else
0631 
0632 #define ASL_CV_LABEL_FILENODE(a)
0633 #define ASL_CV_CAPTURE_COMMENTS_ONLY(a)
0634 #define ASL_CV_CAPTURE_COMMENTS(a)
0635 #define ASL_CV_TRANSFER_COMMENTS(a)
0636 #define ASL_CV_CLOSE_PAREN(a,b)          AcpiOsPrintf (")");
0637 #define ASL_CV_CLOSE_BRACE(a,b)          AcpiOsPrintf ("}");
0638 #define ASL_CV_SWITCH_FILES(a,b)
0639 #define ASL_CV_CLEAR_OP_COMMENTS(a)
0640 #define ASL_CV_PRINT_ONE_COMMENT(a,b,c,d)
0641 #define ASL_CV_PRINT_ONE_COMMENT_LIST(a,b)
0642 #define ASL_CV_FILE_HAS_SWITCHED(a)       0
0643 #define ASL_CV_INIT_FILETREE(a,b)
0644 
0645 #endif
0646 
0647 #endif /* ACMACROS_H */