1149703c7Sjruoho /****************************************************************************** 2149703c7Sjruoho * 3149703c7Sjruoho * Name: acmacros.h - C macros for the entire subsystem. 4149703c7Sjruoho * 5149703c7Sjruoho *****************************************************************************/ 6149703c7Sjruoho 77af23240Sjruoho /* 8*ec12a2faSchristos * Copyright (C) 2000 - 2022, Intel Corp. 9149703c7Sjruoho * All rights reserved. 10149703c7Sjruoho * 117af23240Sjruoho * Redistribution and use in source and binary forms, with or without 127af23240Sjruoho * modification, are permitted provided that the following conditions 137af23240Sjruoho * are met: 147af23240Sjruoho * 1. Redistributions of source code must retain the above copyright 157af23240Sjruoho * notice, this list of conditions, and the following disclaimer, 167af23240Sjruoho * without modification. 177af23240Sjruoho * 2. Redistributions in binary form must reproduce at minimum a disclaimer 187af23240Sjruoho * substantially similar to the "NO WARRANTY" disclaimer below 197af23240Sjruoho * ("Disclaimer") and any redistribution must be conditioned upon 207af23240Sjruoho * including a substantially similar Disclaimer requirement for further 217af23240Sjruoho * binary redistribution. 227af23240Sjruoho * 3. Neither the names of the above-listed copyright holders nor the names 237af23240Sjruoho * of any contributors may be used to endorse or promote products derived 247af23240Sjruoho * from this software without specific prior written permission. 25149703c7Sjruoho * 267af23240Sjruoho * Alternatively, this software may be distributed under the terms of the 277af23240Sjruoho * GNU General Public License ("GPL") version 2 as published by the Free 287af23240Sjruoho * Software Foundation. 29149703c7Sjruoho * 307af23240Sjruoho * NO WARRANTY 317af23240Sjruoho * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 327af23240Sjruoho * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 33adee7055Schristos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 347af23240Sjruoho * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 357af23240Sjruoho * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 367af23240Sjruoho * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 377af23240Sjruoho * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 387af23240Sjruoho * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 397af23240Sjruoho * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 407af23240Sjruoho * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 417af23240Sjruoho * POSSIBILITY OF SUCH DAMAGES. 427af23240Sjruoho */ 43149703c7Sjruoho 44149703c7Sjruoho #ifndef __ACMACROS_H__ 45149703c7Sjruoho #define __ACMACROS_H__ 46149703c7Sjruoho 47149703c7Sjruoho 48149703c7Sjruoho /* 49149703c7Sjruoho * Extract data using a pointer. Any more than a byte and we 50d592fd16Schristos * get into potential alignment issues -- see the STORE macros below. 51149703c7Sjruoho * Use with care. 52149703c7Sjruoho */ 5333841f6dSchristos #define ACPI_CAST8(ptr) ACPI_CAST_PTR (UINT8, (ptr)) 5433841f6dSchristos #define ACPI_CAST16(ptr) ACPI_CAST_PTR (UINT16, (ptr)) 5533841f6dSchristos #define ACPI_CAST32(ptr) ACPI_CAST_PTR (UINT32, (ptr)) 5633841f6dSchristos #define ACPI_CAST64(ptr) ACPI_CAST_PTR (UINT64, (ptr)) 5733841f6dSchristos #define ACPI_GET8(ptr) (*ACPI_CAST8 (ptr)) 5833841f6dSchristos #define ACPI_GET16(ptr) (*ACPI_CAST16 (ptr)) 5933841f6dSchristos #define ACPI_GET32(ptr) (*ACPI_CAST32 (ptr)) 6033841f6dSchristos #define ACPI_GET64(ptr) (*ACPI_CAST64 (ptr)) 6133841f6dSchristos #define ACPI_SET8(ptr, val) (*ACPI_CAST8 (ptr) = (UINT8) (val)) 6233841f6dSchristos #define ACPI_SET16(ptr, val) (*ACPI_CAST16 (ptr) = (UINT16) (val)) 6333841f6dSchristos #define ACPI_SET32(ptr, val) (*ACPI_CAST32 (ptr) = (UINT32) (val)) 6433841f6dSchristos #define ACPI_SET64(ptr, val) (*ACPI_CAST64 (ptr) = (UINT64) (val)) 65149703c7Sjruoho 66149703c7Sjruoho /* 67d592fd16Schristos * printf() format helper. This macro is a workaround for the difficulties 68ea4d7e80Schristos * with emitting 64-bit integers and 64-bit pointers with the same code 69ea4d7e80Schristos * for both 32-bit and 64-bit hosts. 70149703c7Sjruoho */ 71149703c7Sjruoho #define ACPI_FORMAT_UINT64(i) ACPI_HIDWORD(i), ACPI_LODWORD(i) 72149703c7Sjruoho 73149703c7Sjruoho 74149703c7Sjruoho /* 75149703c7Sjruoho * Macros for moving data around to/from buffers that are possibly unaligned. 76149703c7Sjruoho * If the hardware supports the transfer of unaligned data, just do the store. 77149703c7Sjruoho * Otherwise, we have to move one byte at a time. 78149703c7Sjruoho */ 79149703c7Sjruoho #ifdef ACPI_BIG_ENDIAN 80149703c7Sjruoho /* 81149703c7Sjruoho * Macros for big-endian machines 82149703c7Sjruoho */ 83149703c7Sjruoho 84149703c7Sjruoho /* These macros reverse the bytes during the move, converting little-endian to big endian */ 85149703c7Sjruoho 86149703c7Sjruoho /* Big Endian <== Little Endian */ 87149703c7Sjruoho /* Hi...Lo Lo...Hi */ 88149703c7Sjruoho /* 16-bit source, 16/32/64 destination */ 89149703c7Sjruoho 905fa0b4b7Sjruoho #define ACPI_MOVE_16_TO_16(d, s) {(( UINT8 *)(void *)(d))[0] = ((const UINT8 *)(const void *)(s))[1];\ 915fa0b4b7Sjruoho (( UINT8 *)(void *)(d))[1] = ((const UINT8 *)(const void *)(s))[0];} 92149703c7Sjruoho 93149703c7Sjruoho #define ACPI_MOVE_16_TO_32(d, s) {(*(UINT32 *)(void *)(d))=0;\ 945fa0b4b7Sjruoho ((UINT8 *)(void *)(d))[2] = ((const UINT8 *)(const void *)(s))[1];\ 955fa0b4b7Sjruoho ((UINT8 *)(void *)(d))[3] = ((const UINT8 *)(const void *)(s))[0];} 96149703c7Sjruoho 97149703c7Sjruoho #define ACPI_MOVE_16_TO_64(d, s) {(*(UINT64 *)(void *)(d))=0;\ 985fa0b4b7Sjruoho ((UINT8 *)(void *)(d))[6] = ((const UINT8 *)(const void *)(s))[1];\ 995fa0b4b7Sjruoho ((UINT8 *)(void *)(d))[7] = ((const UINT8 *)(const void *)(s))[0];} 100149703c7Sjruoho 101149703c7Sjruoho /* 32-bit source, 16/32/64 destination */ 102149703c7Sjruoho 103149703c7Sjruoho #define ACPI_MOVE_32_TO_16(d, s) ACPI_MOVE_16_TO_16(d, s) /* Truncate to 16 */ 104149703c7Sjruoho 1055fa0b4b7Sjruoho #define ACPI_MOVE_32_TO_32(d, s) {(( UINT8 *)(void *)(d))[0] = ((const UINT8 *)(const void *)(s))[3];\ 1065fa0b4b7Sjruoho (( UINT8 *)(void *)(d))[1] = ((const UINT8 *)(const void *)(s))[2];\ 1075fa0b4b7Sjruoho (( UINT8 *)(void *)(d))[2] = ((const UINT8 *)(const void *)(s))[1];\ 1085fa0b4b7Sjruoho (( UINT8 *)(void *)(d))[3] = ((const UINT8 *)(const void *)(s))[0];} 109149703c7Sjruoho 110149703c7Sjruoho #define ACPI_MOVE_32_TO_64(d, s) {(*(UINT64 *)(void *)(d))=0;\ 1115fa0b4b7Sjruoho ((UINT8 *)(void *)(d))[4] = ((const UINT8 *)(const void *)(s))[3];\ 1125fa0b4b7Sjruoho ((UINT8 *)(void *)(d))[5] = ((const UINT8 *)(const void *)(s))[2];\ 1135fa0b4b7Sjruoho ((UINT8 *)(void *)(d))[6] = ((const UINT8 *)(const void *)(s))[1];\ 1145fa0b4b7Sjruoho ((UINT8 *)(void *)(d))[7] = ((const UINT8 *)(const void *)(s))[0];} 115149703c7Sjruoho 116149703c7Sjruoho /* 64-bit source, 16/32/64 destination */ 117149703c7Sjruoho 118149703c7Sjruoho #define ACPI_MOVE_64_TO_16(d, s) ACPI_MOVE_16_TO_16(d, s) /* Truncate to 16 */ 119149703c7Sjruoho 120149703c7Sjruoho #define ACPI_MOVE_64_TO_32(d, s) ACPI_MOVE_32_TO_32(d, s) /* Truncate to 32 */ 121149703c7Sjruoho 1225fa0b4b7Sjruoho #define ACPI_MOVE_64_TO_64(d, s) {(( UINT8 *)(void *)(d))[0] = ((const UINT8 *)(const void *)(s))[7];\ 1235fa0b4b7Sjruoho (( UINT8 *)(void *)(d))[1] = ((const UINT8 *)(const void *)(s))[6];\ 1245fa0b4b7Sjruoho (( UINT8 *)(void *)(d))[2] = ((const UINT8 *)(const void *)(s))[5];\ 1255fa0b4b7Sjruoho (( UINT8 *)(void *)(d))[3] = ((const UINT8 *)(const void *)(s))[4];\ 1265fa0b4b7Sjruoho (( UINT8 *)(void *)(d))[4] = ((const UINT8 *)(const void *)(s))[3];\ 1275fa0b4b7Sjruoho (( UINT8 *)(void *)(d))[5] = ((const UINT8 *)(const void *)(s))[2];\ 1285fa0b4b7Sjruoho (( UINT8 *)(void *)(d))[6] = ((const UINT8 *)(const void *)(s))[1];\ 1295fa0b4b7Sjruoho (( UINT8 *)(void *)(d))[7] = ((const UINT8 *)(const void *)(s))[0];} 130149703c7Sjruoho #else 131149703c7Sjruoho /* 132149703c7Sjruoho * Macros for little-endian machines 133149703c7Sjruoho */ 134149703c7Sjruoho 135149703c7Sjruoho #ifndef ACPI_MISALIGNMENT_NOT_SUPPORTED 136149703c7Sjruoho 137149703c7Sjruoho /* The hardware supports unaligned transfers, just do the little-endian move */ 138149703c7Sjruoho 139149703c7Sjruoho /* 16-bit source, 16/32/64 destination */ 140149703c7Sjruoho 1415fa0b4b7Sjruoho #define ACPI_MOVE_16_TO_16(d, s) *(UINT16 *)(void *)(d) = *(const UINT16 *)(const void *)(s) 1425fa0b4b7Sjruoho #define ACPI_MOVE_16_TO_32(d, s) *(UINT32 *)(void *)(d) = *(const UINT16 *)(const void *)(s) 1435fa0b4b7Sjruoho #define ACPI_MOVE_16_TO_64(d, s) *(UINT64 *)(void *)(d) = *(const UINT16 *)(const void *)(s) 144149703c7Sjruoho 145149703c7Sjruoho /* 32-bit source, 16/32/64 destination */ 146149703c7Sjruoho 147149703c7Sjruoho #define ACPI_MOVE_32_TO_16(d, s) ACPI_MOVE_16_TO_16(d, s) /* Truncate to 16 */ 1485fa0b4b7Sjruoho #define ACPI_MOVE_32_TO_32(d, s) *(UINT32 *)(void *)(d) = *(const UINT32 *)(const void *)(s) 1495fa0b4b7Sjruoho #define ACPI_MOVE_32_TO_64(d, s) *(UINT64 *)(void *)(d) = *(const UINT32 *)(const void *)(s) 150149703c7Sjruoho 151149703c7Sjruoho /* 64-bit source, 16/32/64 destination */ 152149703c7Sjruoho 153149703c7Sjruoho #define ACPI_MOVE_64_TO_16(d, s) ACPI_MOVE_16_TO_16(d, s) /* Truncate to 16 */ 154149703c7Sjruoho #define ACPI_MOVE_64_TO_32(d, s) ACPI_MOVE_32_TO_32(d, s) /* Truncate to 32 */ 1555fa0b4b7Sjruoho #define ACPI_MOVE_64_TO_64(d, s) *(UINT64 *)(void *)(d) = *(const UINT64 *)(const void *)(s) 156149703c7Sjruoho 157149703c7Sjruoho #else 158149703c7Sjruoho /* 159149703c7Sjruoho * The hardware does not support unaligned transfers. We must move the 160149703c7Sjruoho * data one byte at a time. These macros work whether the source or 161149703c7Sjruoho * the destination (or both) is/are unaligned. (Little-endian move) 162149703c7Sjruoho */ 163149703c7Sjruoho 164149703c7Sjruoho /* 16-bit source, 16/32/64 destination */ 165149703c7Sjruoho 1665fa0b4b7Sjruoho #define ACPI_MOVE_16_TO_16(d, s) {(( UINT8 *)(void *)(d))[0] = ((const UINT8 *)(const void *)(s))[0];\ 1675fa0b4b7Sjruoho (( UINT8 *)(void *)(d))[1] = ((const UINT8 *)(const void *)(s))[1];} 168149703c7Sjruoho 169149703c7Sjruoho #define ACPI_MOVE_16_TO_32(d, s) {(*(UINT32 *)(void *)(d)) = 0; ACPI_MOVE_16_TO_16(d, s);} 170149703c7Sjruoho #define ACPI_MOVE_16_TO_64(d, s) {(*(UINT64 *)(void *)(d)) = 0; ACPI_MOVE_16_TO_16(d, s);} 171149703c7Sjruoho 172149703c7Sjruoho /* 32-bit source, 16/32/64 destination */ 173149703c7Sjruoho 174149703c7Sjruoho #define ACPI_MOVE_32_TO_16(d, s) ACPI_MOVE_16_TO_16(d, s) /* Truncate to 16 */ 175149703c7Sjruoho 1765fa0b4b7Sjruoho #define ACPI_MOVE_32_TO_32(d, s) {(( UINT8 *)(void *)(d))[0] = ((const UINT8 *)(const void *)(s))[0];\ 1775fa0b4b7Sjruoho (( UINT8 *)(void *)(d))[1] = ((const UINT8 *)(const void *)(s))[1];\ 1785fa0b4b7Sjruoho (( UINT8 *)(void *)(d))[2] = ((const UINT8 *)(const void *)(s))[2];\ 1795fa0b4b7Sjruoho (( UINT8 *)(void *)(d))[3] = ((const UINT8 *)(const void *)(s))[3];} 180149703c7Sjruoho 181149703c7Sjruoho #define ACPI_MOVE_32_TO_64(d, s) {(*(UINT64 *)(void *)(d)) = 0; ACPI_MOVE_32_TO_32(d, s);} 182149703c7Sjruoho 183149703c7Sjruoho /* 64-bit source, 16/32/64 destination */ 184149703c7Sjruoho 185149703c7Sjruoho #define ACPI_MOVE_64_TO_16(d, s) ACPI_MOVE_16_TO_16(d, s) /* Truncate to 16 */ 186149703c7Sjruoho #define ACPI_MOVE_64_TO_32(d, s) ACPI_MOVE_32_TO_32(d, s) /* Truncate to 32 */ 1875fa0b4b7Sjruoho #define ACPI_MOVE_64_TO_64(d, s) {(( UINT8 *)(void *)(d))[0] = ((const UINT8 *)(const void *)(s))[0];\ 1885fa0b4b7Sjruoho (( UINT8 *)(void *)(d))[1] = ((const UINT8 *)(const void *)(s))[1];\ 1895fa0b4b7Sjruoho (( UINT8 *)(void *)(d))[2] = ((const UINT8 *)(const void *)(s))[2];\ 1905fa0b4b7Sjruoho (( UINT8 *)(void *)(d))[3] = ((const UINT8 *)(const void *)(s))[3];\ 1915fa0b4b7Sjruoho (( UINT8 *)(void *)(d))[4] = ((const UINT8 *)(const void *)(s))[4];\ 1925fa0b4b7Sjruoho (( UINT8 *)(void *)(d))[5] = ((const UINT8 *)(const void *)(s))[5];\ 1935fa0b4b7Sjruoho (( UINT8 *)(void *)(d))[6] = ((const UINT8 *)(const void *)(s))[6];\ 1945fa0b4b7Sjruoho (( UINT8 *)(void *)(d))[7] = ((const UINT8 *)(const void *)(s))[7];} 195149703c7Sjruoho #endif 196149703c7Sjruoho #endif 197149703c7Sjruoho 198149703c7Sjruoho 199149703c7Sjruoho /* 200149703c7Sjruoho * Fast power-of-two math macros for non-optimized compilers 201149703c7Sjruoho */ 202149703c7Sjruoho #define _ACPI_DIV(value, PowerOf2) ((UINT32) ((value) >> (PowerOf2))) 203149703c7Sjruoho #define _ACPI_MUL(value, PowerOf2) ((UINT32) ((value) << (PowerOf2))) 204149703c7Sjruoho #define _ACPI_MOD(value, Divisor) ((UINT32) ((value) & ((Divisor) -1))) 205149703c7Sjruoho 206149703c7Sjruoho #define ACPI_DIV_2(a) _ACPI_DIV(a, 1) 207149703c7Sjruoho #define ACPI_MUL_2(a) _ACPI_MUL(a, 1) 208149703c7Sjruoho #define ACPI_MOD_2(a) _ACPI_MOD(a, 2) 209149703c7Sjruoho 210149703c7Sjruoho #define ACPI_DIV_4(a) _ACPI_DIV(a, 2) 211149703c7Sjruoho #define ACPI_MUL_4(a) _ACPI_MUL(a, 2) 212149703c7Sjruoho #define ACPI_MOD_4(a) _ACPI_MOD(a, 4) 213149703c7Sjruoho 214149703c7Sjruoho #define ACPI_DIV_8(a) _ACPI_DIV(a, 3) 215149703c7Sjruoho #define ACPI_MUL_8(a) _ACPI_MUL(a, 3) 216149703c7Sjruoho #define ACPI_MOD_8(a) _ACPI_MOD(a, 8) 217149703c7Sjruoho 218149703c7Sjruoho #define ACPI_DIV_16(a) _ACPI_DIV(a, 4) 219149703c7Sjruoho #define ACPI_MUL_16(a) _ACPI_MUL(a, 4) 220149703c7Sjruoho #define ACPI_MOD_16(a) _ACPI_MOD(a, 16) 221149703c7Sjruoho 222149703c7Sjruoho #define ACPI_DIV_32(a) _ACPI_DIV(a, 5) 223149703c7Sjruoho #define ACPI_MUL_32(a) _ACPI_MUL(a, 5) 224149703c7Sjruoho #define ACPI_MOD_32(a) _ACPI_MOD(a, 32) 225149703c7Sjruoho 226e34402d4Schristos /* Test for ASCII character */ 227e34402d4Schristos 228e34402d4Schristos #define ACPI_IS_ASCII(c) ((c) < 0x80) 229e34402d4Schristos 230e34402d4Schristos /* Signed integers */ 231e34402d4Schristos 232e34402d4Schristos #define ACPI_SIGN_POSITIVE 0 233e34402d4Schristos #define ACPI_SIGN_NEGATIVE 1 234e34402d4Schristos 235e34402d4Schristos 236149703c7Sjruoho /* 237149703c7Sjruoho * Rounding macros (Power of two boundaries only) 238149703c7Sjruoho */ 239149703c7Sjruoho #define ACPI_ROUND_DOWN(value, boundary) (((ACPI_SIZE)(value)) & \ 240149703c7Sjruoho (~(((ACPI_SIZE) boundary)-1))) 241149703c7Sjruoho 242149703c7Sjruoho #define ACPI_ROUND_UP(value, boundary) ((((ACPI_SIZE)(value)) + \ 243149703c7Sjruoho (((ACPI_SIZE) boundary)-1)) & \ 244149703c7Sjruoho (~(((ACPI_SIZE) boundary)-1))) 245149703c7Sjruoho 246149703c7Sjruoho /* Note: sizeof(ACPI_SIZE) evaluates to either 4 or 8 (32- vs 64-bit mode) */ 247149703c7Sjruoho 248149703c7Sjruoho #define ACPI_ROUND_DOWN_TO_32BIT(a) ACPI_ROUND_DOWN(a, 4) 249149703c7Sjruoho #define ACPI_ROUND_DOWN_TO_64BIT(a) ACPI_ROUND_DOWN(a, 8) 250149703c7Sjruoho #define ACPI_ROUND_DOWN_TO_NATIVE_WORD(a) ACPI_ROUND_DOWN(a, sizeof(ACPI_SIZE)) 251149703c7Sjruoho 252149703c7Sjruoho #define ACPI_ROUND_UP_TO_32BIT(a) ACPI_ROUND_UP(a, 4) 253149703c7Sjruoho #define ACPI_ROUND_UP_TO_64BIT(a) ACPI_ROUND_UP(a, 8) 254149703c7Sjruoho #define ACPI_ROUND_UP_TO_NATIVE_WORD(a) ACPI_ROUND_UP(a, sizeof(ACPI_SIZE)) 255149703c7Sjruoho 256149703c7Sjruoho #define ACPI_ROUND_BITS_UP_TO_BYTES(a) ACPI_DIV_8((a) + 7) 257149703c7Sjruoho #define ACPI_ROUND_BITS_DOWN_TO_BYTES(a) ACPI_DIV_8((a)) 258149703c7Sjruoho 259149703c7Sjruoho #define ACPI_ROUND_UP_TO_1K(a) (((a) + 1023) >> 10) 260149703c7Sjruoho 261149703c7Sjruoho /* Generic (non-power-of-two) rounding */ 262149703c7Sjruoho 263149703c7Sjruoho #define ACPI_ROUND_UP_TO(value, boundary) (((value) + ((boundary)-1)) / (boundary)) 264149703c7Sjruoho 265149703c7Sjruoho #define ACPI_IS_MISALIGNED(value) (((ACPI_SIZE) value) & (sizeof(ACPI_SIZE)-1)) 266149703c7Sjruoho 267d592fd16Schristos /* Generic bit manipulation */ 268d592fd16Schristos 269d592fd16Schristos #ifndef ACPI_USE_NATIVE_BIT_FINDER 270d592fd16Schristos 271d592fd16Schristos #define __ACPI_FIND_LAST_BIT_2(a, r) ((((UINT8) (a)) & 0x02) ? (r)+1 : (r)) 272d592fd16Schristos #define __ACPI_FIND_LAST_BIT_4(a, r) ((((UINT8) (a)) & 0x0C) ? \ 273d592fd16Schristos __ACPI_FIND_LAST_BIT_2 ((a)>>2, (r)+2) : \ 274d592fd16Schristos __ACPI_FIND_LAST_BIT_2 ((a), (r))) 275d592fd16Schristos #define __ACPI_FIND_LAST_BIT_8(a, r) ((((UINT8) (a)) & 0xF0) ? \ 276d592fd16Schristos __ACPI_FIND_LAST_BIT_4 ((a)>>4, (r)+4) : \ 277d592fd16Schristos __ACPI_FIND_LAST_BIT_4 ((a), (r))) 278d592fd16Schristos #define __ACPI_FIND_LAST_BIT_16(a, r) ((((UINT16) (a)) & 0xFF00) ? \ 279d592fd16Schristos __ACPI_FIND_LAST_BIT_8 ((a)>>8, (r)+8) : \ 280d592fd16Schristos __ACPI_FIND_LAST_BIT_8 ((a), (r))) 281d592fd16Schristos #define __ACPI_FIND_LAST_BIT_32(a, r) ((((UINT32) (a)) & 0xFFFF0000) ? \ 282d592fd16Schristos __ACPI_FIND_LAST_BIT_16 ((a)>>16, (r)+16) : \ 283d592fd16Schristos __ACPI_FIND_LAST_BIT_16 ((a), (r))) 284d592fd16Schristos #define __ACPI_FIND_LAST_BIT_64(a, r) ((((UINT64) (a)) & 0xFFFFFFFF00000000) ? \ 285d592fd16Schristos __ACPI_FIND_LAST_BIT_32 ((a)>>32, (r)+32) : \ 286d592fd16Schristos __ACPI_FIND_LAST_BIT_32 ((a), (r))) 287d592fd16Schristos 288d592fd16Schristos #define ACPI_FIND_LAST_BIT_8(a) ((a) ? __ACPI_FIND_LAST_BIT_8 (a, 1) : 0) 289d592fd16Schristos #define ACPI_FIND_LAST_BIT_16(a) ((a) ? __ACPI_FIND_LAST_BIT_16 (a, 1) : 0) 290d592fd16Schristos #define ACPI_FIND_LAST_BIT_32(a) ((a) ? __ACPI_FIND_LAST_BIT_32 (a, 1) : 0) 291d592fd16Schristos #define ACPI_FIND_LAST_BIT_64(a) ((a) ? __ACPI_FIND_LAST_BIT_64 (a, 1) : 0) 292d592fd16Schristos 293d592fd16Schristos #define __ACPI_FIND_FIRST_BIT_2(a, r) ((((UINT8) (a)) & 0x01) ? (r) : (r)+1) 294d592fd16Schristos #define __ACPI_FIND_FIRST_BIT_4(a, r) ((((UINT8) (a)) & 0x03) ? \ 295d592fd16Schristos __ACPI_FIND_FIRST_BIT_2 ((a), (r)) : \ 296d592fd16Schristos __ACPI_FIND_FIRST_BIT_2 ((a)>>2, (r)+2)) 297d592fd16Schristos #define __ACPI_FIND_FIRST_BIT_8(a, r) ((((UINT8) (a)) & 0x0F) ? \ 298d592fd16Schristos __ACPI_FIND_FIRST_BIT_4 ((a), (r)) : \ 299d592fd16Schristos __ACPI_FIND_FIRST_BIT_4 ((a)>>4, (r)+4)) 300d592fd16Schristos #define __ACPI_FIND_FIRST_BIT_16(a, r) ((((UINT16) (a)) & 0x00FF) ? \ 301d592fd16Schristos __ACPI_FIND_FIRST_BIT_8 ((a), (r)) : \ 302d592fd16Schristos __ACPI_FIND_FIRST_BIT_8 ((a)>>8, (r)+8)) 303d592fd16Schristos #define __ACPI_FIND_FIRST_BIT_32(a, r) ((((UINT32) (a)) & 0x0000FFFF) ? \ 304d592fd16Schristos __ACPI_FIND_FIRST_BIT_16 ((a), (r)) : \ 305d592fd16Schristos __ACPI_FIND_FIRST_BIT_16 ((a)>>16, (r)+16)) 306d592fd16Schristos #define __ACPI_FIND_FIRST_BIT_64(a, r) ((((UINT64) (a)) & 0x00000000FFFFFFFF) ? \ 307d592fd16Schristos __ACPI_FIND_FIRST_BIT_32 ((a), (r)) : \ 308d592fd16Schristos __ACPI_FIND_FIRST_BIT_32 ((a)>>32, (r)+32)) 309d592fd16Schristos 310d592fd16Schristos #define ACPI_FIND_FIRST_BIT_8(a) ((a) ? __ACPI_FIND_FIRST_BIT_8 (a, 1) : 0) 311d592fd16Schristos #define ACPI_FIND_FIRST_BIT_16(a) ((a) ? __ACPI_FIND_FIRST_BIT_16 (a, 1) : 0) 312d592fd16Schristos #define ACPI_FIND_FIRST_BIT_32(a) ((a) ? __ACPI_FIND_FIRST_BIT_32 (a, 1) : 0) 313d592fd16Schristos #define ACPI_FIND_FIRST_BIT_64(a) ((a) ? __ACPI_FIND_FIRST_BIT_64 (a, 1) : 0) 314d592fd16Schristos 315d592fd16Schristos #endif /* ACPI_USE_NATIVE_BIT_FINDER */ 316d592fd16Schristos 317d592fd16Schristos /* Generic (power-of-two) rounding */ 318d592fd16Schristos 319d592fd16Schristos #define ACPI_ROUND_UP_POWER_OF_TWO_8(a) ((UINT8) \ 320d592fd16Schristos (((UINT16) 1) << ACPI_FIND_LAST_BIT_8 ((a) - 1))) 321d592fd16Schristos #define ACPI_ROUND_DOWN_POWER_OF_TWO_8(a) ((UINT8) \ 322d592fd16Schristos (((UINT16) 1) << (ACPI_FIND_LAST_BIT_8 ((a)) - 1))) 323d592fd16Schristos #define ACPI_ROUND_UP_POWER_OF_TWO_16(a) ((UINT16) \ 324d592fd16Schristos (((UINT32) 1) << ACPI_FIND_LAST_BIT_16 ((a) - 1))) 325d592fd16Schristos #define ACPI_ROUND_DOWN_POWER_OF_TWO_16(a) ((UINT16) \ 326d592fd16Schristos (((UINT32) 1) << (ACPI_FIND_LAST_BIT_16 ((a)) - 1))) 327d592fd16Schristos #define ACPI_ROUND_UP_POWER_OF_TWO_32(a) ((UINT32) \ 328d592fd16Schristos (((UINT64) 1) << ACPI_FIND_LAST_BIT_32 ((a) - 1))) 329d592fd16Schristos #define ACPI_ROUND_DOWN_POWER_OF_TWO_32(a) ((UINT32) \ 330d592fd16Schristos (((UINT64) 1) << (ACPI_FIND_LAST_BIT_32 ((a)) - 1))) 331d592fd16Schristos #define ACPI_IS_ALIGNED(a, s) (((a) & ((s) - 1)) == 0) 332d592fd16Schristos #define ACPI_IS_POWER_OF_TWO(a) ACPI_IS_ALIGNED(a, a) 333d592fd16Schristos 334149703c7Sjruoho /* 335149703c7Sjruoho * Bitmask creation 336149703c7Sjruoho * Bit positions start at zero. 337149703c7Sjruoho * MASK_BITS_ABOVE creates a mask starting AT the position and above 338149703c7Sjruoho * MASK_BITS_BELOW creates a mask starting one bit BELOW the position 339d592fd16Schristos * MASK_BITS_ABOVE/BELOW accepts a bit offset to create a mask 340d592fd16Schristos * MASK_BITS_ABOVE/BELOW_32/64 accepts a bit width to create a mask 341d592fd16Schristos * Note: The ACPI_INTEGER_BIT_SIZE check is used to bypass compiler 342d592fd16Schristos * differences with the shift operator 343149703c7Sjruoho */ 344149703c7Sjruoho #define ACPI_MASK_BITS_ABOVE(position) (~((ACPI_UINT64_MAX) << ((UINT32) (position)))) 345149703c7Sjruoho #define ACPI_MASK_BITS_BELOW(position) ((ACPI_UINT64_MAX) << ((UINT32) (position))) 346d592fd16Schristos #define ACPI_MASK_BITS_ABOVE_32(width) ((UINT32) ACPI_MASK_BITS_ABOVE(width)) 347d592fd16Schristos #define ACPI_MASK_BITS_BELOW_32(width) ((UINT32) ACPI_MASK_BITS_BELOW(width)) 348d592fd16Schristos #define ACPI_MASK_BITS_ABOVE_64(width) ((width) == ACPI_INTEGER_BIT_SIZE ? \ 349d592fd16Schristos ACPI_UINT64_MAX : \ 350d592fd16Schristos ACPI_MASK_BITS_ABOVE(width)) 351d592fd16Schristos #define ACPI_MASK_BITS_BELOW_64(width) ((width) == ACPI_INTEGER_BIT_SIZE ? \ 352d592fd16Schristos (UINT64) 0 : \ 353d592fd16Schristos ACPI_MASK_BITS_BELOW(width)) 354149703c7Sjruoho 355149703c7Sjruoho /* Bitfields within ACPI registers */ 356149703c7Sjruoho 35733841f6dSchristos #define ACPI_REGISTER_PREPARE_BITS(Val, Pos, Mask) \ 35833841f6dSchristos ((Val << Pos) & Mask) 359149703c7Sjruoho 36033841f6dSchristos #define ACPI_REGISTER_INSERT_VALUE(Reg, Pos, Mask, Val) \ 36133841f6dSchristos Reg = (Reg & (~(Mask))) | ACPI_REGISTER_PREPARE_BITS(Val, Pos, Mask) 36233841f6dSchristos 36333841f6dSchristos #define ACPI_INSERT_BITS(Target, Mask, Source) \ 36433841f6dSchristos Target = ((Target & (~(Mask))) | (Source & Mask)) 36533841f6dSchristos 36633841f6dSchristos /* Generic bitfield macros and masks */ 36733841f6dSchristos 36833841f6dSchristos #define ACPI_GET_BITS(SourcePtr, Position, Mask) \ 369dc79220aSchristos ((*(SourcePtr) >> (Position)) & (Mask)) 37033841f6dSchristos 37133841f6dSchristos #define ACPI_SET_BITS(TargetPtr, Position, Mask, Value) \ 372dc79220aSchristos (*(TargetPtr) |= (((Value) & (Mask)) << (Position))) 37333841f6dSchristos 37433841f6dSchristos #define ACPI_1BIT_MASK 0x00000001 37533841f6dSchristos #define ACPI_2BIT_MASK 0x00000003 37633841f6dSchristos #define ACPI_3BIT_MASK 0x00000007 37733841f6dSchristos #define ACPI_4BIT_MASK 0x0000000F 37833841f6dSchristos #define ACPI_5BIT_MASK 0x0000001F 37933841f6dSchristos #define ACPI_6BIT_MASK 0x0000003F 38033841f6dSchristos #define ACPI_7BIT_MASK 0x0000007F 38133841f6dSchristos #define ACPI_8BIT_MASK 0x000000FF 38233841f6dSchristos #define ACPI_16BIT_MASK 0x0000FFFF 38333841f6dSchristos #define ACPI_24BIT_MASK 0x00FFFFFF 38433841f6dSchristos 38533841f6dSchristos /* Macros to extract flag bits from position zero */ 38633841f6dSchristos 38733841f6dSchristos #define ACPI_GET_1BIT_FLAG(Value) ((Value) & ACPI_1BIT_MASK) 38833841f6dSchristos #define ACPI_GET_2BIT_FLAG(Value) ((Value) & ACPI_2BIT_MASK) 38933841f6dSchristos #define ACPI_GET_3BIT_FLAG(Value) ((Value) & ACPI_3BIT_MASK) 39033841f6dSchristos #define ACPI_GET_4BIT_FLAG(Value) ((Value) & ACPI_4BIT_MASK) 39133841f6dSchristos 39233841f6dSchristos /* Macros to extract flag bits from position one and above */ 39333841f6dSchristos 39433841f6dSchristos #define ACPI_EXTRACT_1BIT_FLAG(Field, Position) (ACPI_GET_1BIT_FLAG ((Field) >> Position)) 39533841f6dSchristos #define ACPI_EXTRACT_2BIT_FLAG(Field, Position) (ACPI_GET_2BIT_FLAG ((Field) >> Position)) 39633841f6dSchristos #define ACPI_EXTRACT_3BIT_FLAG(Field, Position) (ACPI_GET_3BIT_FLAG ((Field) >> Position)) 39733841f6dSchristos #define ACPI_EXTRACT_4BIT_FLAG(Field, Position) (ACPI_GET_4BIT_FLAG ((Field) >> Position)) 39833841f6dSchristos 39933841f6dSchristos /* ACPI Pathname helpers */ 40033841f6dSchristos 40133841f6dSchristos #define ACPI_IS_ROOT_PREFIX(c) ((c) == (UINT8) 0x5C) /* Backslash */ 40233841f6dSchristos #define ACPI_IS_PARENT_PREFIX(c) ((c) == (UINT8) 0x5E) /* Carat */ 40333841f6dSchristos #define ACPI_IS_PATH_SEPARATOR(c) ((c) == (UINT8) 0x2E) /* Period (dot) */ 404149703c7Sjruoho 405149703c7Sjruoho /* 40633841f6dSchristos * An object of type ACPI_NAMESPACE_NODE can appear in some contexts 40733841f6dSchristos * where a pointer to an object of type ACPI_OPERAND_OBJECT can also 408149703c7Sjruoho * appear. This macro is used to distinguish them. 409149703c7Sjruoho * 41033841f6dSchristos * The "DescriptorType" field is the second field in both structures. 411149703c7Sjruoho */ 41233841f6dSchristos #define ACPI_GET_DESCRIPTOR_PTR(d) (((ACPI_DESCRIPTOR *)(void *)(d))->Common.CommonPointer) 41333841f6dSchristos #define ACPI_SET_DESCRIPTOR_PTR(d, p) (((ACPI_DESCRIPTOR *)(void *)(d))->Common.CommonPointer = (p)) 414149703c7Sjruoho #define ACPI_GET_DESCRIPTOR_TYPE(d) (((ACPI_DESCRIPTOR *)(void *)(d))->Common.DescriptorType) 41533841f6dSchristos #define ACPI_SET_DESCRIPTOR_TYPE(d, t) (((ACPI_DESCRIPTOR *)(void *)(d))->Common.DescriptorType = (t)) 416149703c7Sjruoho 417149703c7Sjruoho /* 418149703c7Sjruoho * Macros for the master AML opcode table 419149703c7Sjruoho */ 420149703c7Sjruoho #if defined (ACPI_DISASSEMBLER) || defined (ACPI_DEBUG_OUTPUT) 421149703c7Sjruoho #define ACPI_OP(Name, PArgs, IArgs, ObjType, Class, Type, Flags) \ 422149703c7Sjruoho {Name, (UINT32)(PArgs), (UINT32)(IArgs), (UINT32)(Flags), ObjType, Class, Type} 423149703c7Sjruoho #else 424149703c7Sjruoho #define ACPI_OP(Name, PArgs, IArgs, ObjType, Class, Type, Flags) \ 425149703c7Sjruoho {(UINT32)(PArgs), (UINT32)(IArgs), (UINT32)(Flags), ObjType, Class, Type} 426149703c7Sjruoho #endif 427149703c7Sjruoho 428149703c7Sjruoho #define ARG_TYPE_WIDTH 5 429149703c7Sjruoho #define ARG_1(x) ((UINT32)(x)) 430149703c7Sjruoho #define ARG_2(x) ((UINT32)(x) << (1 * ARG_TYPE_WIDTH)) 431149703c7Sjruoho #define ARG_3(x) ((UINT32)(x) << (2 * ARG_TYPE_WIDTH)) 432149703c7Sjruoho #define ARG_4(x) ((UINT32)(x) << (3 * ARG_TYPE_WIDTH)) 433149703c7Sjruoho #define ARG_5(x) ((UINT32)(x) << (4 * ARG_TYPE_WIDTH)) 434149703c7Sjruoho #define ARG_6(x) ((UINT32)(x) << (5 * ARG_TYPE_WIDTH)) 435149703c7Sjruoho 436149703c7Sjruoho #define ARGI_LIST1(a) (ARG_1(a)) 437149703c7Sjruoho #define ARGI_LIST2(a, b) (ARG_1(b)|ARG_2(a)) 438149703c7Sjruoho #define ARGI_LIST3(a, b, c) (ARG_1(c)|ARG_2(b)|ARG_3(a)) 439149703c7Sjruoho #define ARGI_LIST4(a, b, c, d) (ARG_1(d)|ARG_2(c)|ARG_3(b)|ARG_4(a)) 440149703c7Sjruoho #define ARGI_LIST5(a, b, c, d, e) (ARG_1(e)|ARG_2(d)|ARG_3(c)|ARG_4(b)|ARG_5(a)) 441149703c7Sjruoho #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)) 442149703c7Sjruoho 443149703c7Sjruoho #define ARGP_LIST1(a) (ARG_1(a)) 444149703c7Sjruoho #define ARGP_LIST2(a, b) (ARG_1(a)|ARG_2(b)) 445149703c7Sjruoho #define ARGP_LIST3(a, b, c) (ARG_1(a)|ARG_2(b)|ARG_3(c)) 446149703c7Sjruoho #define ARGP_LIST4(a, b, c, d) (ARG_1(a)|ARG_2(b)|ARG_3(c)|ARG_4(d)) 447149703c7Sjruoho #define ARGP_LIST5(a, b, c, d, e) (ARG_1(a)|ARG_2(b)|ARG_3(c)|ARG_4(d)|ARG_5(e)) 448149703c7Sjruoho #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)) 449149703c7Sjruoho 450149703c7Sjruoho #define GET_CURRENT_ARG_TYPE(List) (List & ((UINT32) 0x1F)) 451149703c7Sjruoho #define INCREMENT_ARG_LIST(List) (List >>= ((UINT32) ARG_TYPE_WIDTH)) 452149703c7Sjruoho 453149703c7Sjruoho /* 454149703c7Sjruoho * Ascii error messages can be configured out 455149703c7Sjruoho */ 456149703c7Sjruoho #ifndef ACPI_NO_ERROR_MESSAGES 457149703c7Sjruoho /* 458d592fd16Schristos * Error reporting. The callers module and line number are inserted by AE_INFO, 459149703c7Sjruoho * the plist contains a set of parens to allow variable-length lists. 460149703c7Sjruoho * These macros are used for both the debug and non-debug versions of the code. 461149703c7Sjruoho */ 462232afe62Schristos #define ACPI_ERROR_NAMESPACE(s, p, e) AcpiUtPrefixedNamespaceError (AE_INFO, s, p, e); 4637af23240Sjruoho #define ACPI_ERROR_METHOD(s, n, p, e) AcpiUtMethodError (AE_INFO, s, n, p, e); 464149703c7Sjruoho #define ACPI_WARN_PREDEFINED(plist) AcpiUtPredefinedWarning plist 465149703c7Sjruoho #define ACPI_INFO_PREDEFINED(plist) AcpiUtPredefinedInfo plist 46633841f6dSchristos #define ACPI_BIOS_ERROR_PREDEFINED(plist) AcpiUtPredefinedBiosError plist 467116aa910Schristos #define ACPI_ERROR_ONLY(s) s 468149703c7Sjruoho 469149703c7Sjruoho #else 470149703c7Sjruoho 471149703c7Sjruoho /* No error messages */ 472149703c7Sjruoho 473116aa910Schristos #define ACPI_ERROR_NAMESPACE(s, p, e) 474149703c7Sjruoho #define ACPI_ERROR_METHOD(s, n, p, e) 475149703c7Sjruoho #define ACPI_WARN_PREDEFINED(plist) 476149703c7Sjruoho #define ACPI_INFO_PREDEFINED(plist) 47733841f6dSchristos #define ACPI_BIOS_ERROR_PREDEFINED(plist) 478116aa910Schristos #define ACPI_ERROR_ONLY(s) 479149703c7Sjruoho 480149703c7Sjruoho #endif /* ACPI_NO_ERROR_MESSAGES */ 481149703c7Sjruoho 48233841f6dSchristos #if (!ACPI_REDUCED_HARDWARE) 48333841f6dSchristos #define ACPI_HW_OPTIONAL_FUNCTION(addr) addr 484149703c7Sjruoho #else 48533841f6dSchristos #define ACPI_HW_OPTIONAL_FUNCTION(addr) NULL 486149703c7Sjruoho #endif 487149703c7Sjruoho 488149703c7Sjruoho 489149703c7Sjruoho /* 490149703c7Sjruoho * Macros used for ACPICA utilities only 491149703c7Sjruoho */ 492149703c7Sjruoho 493149703c7Sjruoho /* Generate a UUID */ 494149703c7Sjruoho 495149703c7Sjruoho #define ACPI_INIT_UUID(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \ 496149703c7Sjruoho (a) & 0xFF, ((a) >> 8) & 0xFF, ((a) >> 16) & 0xFF, ((a) >> 24) & 0xFF, \ 497149703c7Sjruoho (b) & 0xFF, ((b) >> 8) & 0xFF, \ 498149703c7Sjruoho (c) & 0xFF, ((c) >> 8) & 0xFF, \ 499149703c7Sjruoho (d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7) 500149703c7Sjruoho 501149703c7Sjruoho #define ACPI_IS_OCTAL_DIGIT(d) (((char)(d) >= '0') && ((char)(d) <= '7')) 502149703c7Sjruoho 503149703c7Sjruoho 504e9d4a2c3Schristos /* 505f5a85da7Schristos * Macros used for the ASL-/ASL+ converter utility 506e9d4a2c3Schristos */ 507e9d4a2c3Schristos #ifdef ACPI_ASL_COMPILER 508e9d4a2c3Schristos 509e9d4a2c3Schristos #define ASL_CV_LABEL_FILENODE(a) CvLabelFileNode(a); 510e9d4a2c3Schristos #define ASL_CV_CAPTURE_COMMENTS_ONLY(a) CvCaptureCommentsOnly (a); 511e9d4a2c3Schristos #define ASL_CV_CAPTURE_COMMENTS(a) CvCaptureComments (a); 512e9d4a2c3Schristos #define ASL_CV_TRANSFER_COMMENTS(a) CvTransferComments (a); 513e9d4a2c3Schristos #define ASL_CV_CLOSE_PAREN(a,b) CvCloseParenWriteComment(a,b); 514e9d4a2c3Schristos #define ASL_CV_CLOSE_BRACE(a,b) CvCloseBraceWriteComment(a,b); 515e9d4a2c3Schristos #define ASL_CV_SWITCH_FILES(a,b) CvSwitchFiles(a,b); 516e9d4a2c3Schristos #define ASL_CV_CLEAR_OP_COMMENTS(a) CvClearOpComments(a); 517e9d4a2c3Schristos #define ASL_CV_PRINT_ONE_COMMENT(a,b,c,d) CvPrintOneCommentType (a,b,c,d); 518e9d4a2c3Schristos #define ASL_CV_PRINT_ONE_COMMENT_LIST(a,b) CvPrintOneCommentList (a,b); 519e9d4a2c3Schristos #define ASL_CV_FILE_HAS_SWITCHED(a) CvFileHasSwitched(a) 520bd973ebcSchristos #define ASL_CV_INIT_FILETREE(a,b) CvInitFileTree(a,b); 521e9d4a2c3Schristos 522e9d4a2c3Schristos #else 523e9d4a2c3Schristos 524e9d4a2c3Schristos #define ASL_CV_LABEL_FILENODE(a) 525e9d4a2c3Schristos #define ASL_CV_CAPTURE_COMMENTS_ONLY(a) 526e9d4a2c3Schristos #define ASL_CV_CAPTURE_COMMENTS(a) 527e9d4a2c3Schristos #define ASL_CV_TRANSFER_COMMENTS(a) 528e9d4a2c3Schristos #define ASL_CV_CLOSE_PAREN(a,b) AcpiOsPrintf (")"); 529e9d4a2c3Schristos #define ASL_CV_CLOSE_BRACE(a,b) AcpiOsPrintf ("}"); 530e9d4a2c3Schristos #define ASL_CV_SWITCH_FILES(a,b) 531e9d4a2c3Schristos #define ASL_CV_CLEAR_OP_COMMENTS(a) 532e9d4a2c3Schristos #define ASL_CV_PRINT_ONE_COMMENT(a,b,c,d) 533e9d4a2c3Schristos #define ASL_CV_PRINT_ONE_COMMENT_LIST(a,b) 534e9d4a2c3Schristos #define ASL_CV_FILE_HAS_SWITCHED(a) 0 535bd973ebcSchristos #define ASL_CV_INIT_FILETREE(a,b) 536e9d4a2c3Schristos 537e9d4a2c3Schristos #endif 538e9d4a2c3Schristos 539149703c7Sjruoho #endif /* ACMACROS_H */ 540