Home | History | Annotate | Download | only in cpufeatures
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  *  * Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  *  * Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in
     12  *    the documentation and/or other materials provided with the
     13  *    distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
     22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 #ifndef CPU_FEATURES_H
     29 #define CPU_FEATURES_H
     30 
     31 #include <sys/cdefs.h>
     32 #include <stdint.h>
     33 
     34 __BEGIN_DECLS
     35 
     36 /* A list of valid values returned by android_getCpuFamily().
     37  * They describe the CPU Architecture of the current process.
     38  */
     39 typedef enum {
     40     ANDROID_CPU_FAMILY_UNKNOWN = 0,
     41     ANDROID_CPU_FAMILY_ARM,
     42     ANDROID_CPU_FAMILY_X86,
     43     ANDROID_CPU_FAMILY_MIPS,
     44     ANDROID_CPU_FAMILY_ARM64,
     45     ANDROID_CPU_FAMILY_X86_64,
     46     ANDROID_CPU_FAMILY_MIPS64,
     47 
     48     ANDROID_CPU_FAMILY_MAX  /* do not remove */
     49 
     50 } AndroidCpuFamily;
     51 
     52 /* Return the CPU family of the current process.
     53  *
     54  * Note that this matches the bitness of the current process. I.e. when
     55  * running a 32-bit binary on a 64-bit capable CPU, this will return the
     56  * 32-bit CPU family value.
     57  */
     58 extern AndroidCpuFamily android_getCpuFamily(void);
     59 
     60 /* Return a bitmap describing a set of optional CPU features that are
     61  * supported by the current device's CPU. The exact bit-flags returned
     62  * depend on the value returned by android_getCpuFamily(). See the
     63  * documentation for the ANDROID_CPU_*_FEATURE_* flags below for details.
     64  *
     65  * NOTE: This will return 0 for the following architectures that don't have
     66  *       optional features listed at the moment:
     67  *
     68  *   ANDROID_CPU_FAMILY_MIPS
     69  *   ANDROID_CPU_FAMILY_ARM64
     70  *   ANDROID_CPU_FAMILY_X86_64
     71  *   ANDROID_CPU_FAMILY_MIPS64
     72  */
     73 extern uint64_t android_getCpuFeatures(void);
     74 
     75 /* The list of feature flags for ANDROID_CPU_FAMILY_ARM that can be
     76  * recognized by the library (see note below for 64-bit ARM). Value details
     77  * are:
     78  *
     79  *   VFPv2:
     80  *     CPU supports the VFPv2 instruction set. Many, but not all, ARMv6 CPUs
     81  *     support these instructions. VFPv2 is a subset of VFPv3 so this will
     82  *     be set whenever VFPv3 is set too.
     83  *
     84  *   ARMv7:
     85  *     CPU supports the ARMv7-A basic instruction set.
     86  *     This feature is mandated by the 'armeabi-v7a' ABI.
     87  *
     88  *   VFPv3:
     89  *     CPU supports the VFPv3-D16 instruction set, providing hardware FPU
     90  *     support for single and double precision floating point registers.
     91  *     Note that only 16 FPU registers are available by default, unless
     92  *     the D32 bit is set too. This feature is also mandated by the
     93  *     'armeabi-v7a' ABI.
     94  *
     95  *   VFP_D32:
     96  *     CPU VFP optional extension that provides 32 FPU registers,
     97  *     instead of 16. Note that ARM mandates this feature is the 'NEON'
     98  *     feature is implemented by the CPU.
     99  *
    100  *   NEON:
    101  *     CPU FPU supports "ARM Advanced SIMD" instructions, also known as
    102  *     NEON. Note that this mandates the VFP_D32 feature as well, per the
    103  *     ARM Architecture specification.
    104  *
    105  *   VFP_FP16:
    106  *     Half-width floating precision VFP extension. If set, the CPU
    107  *     supports instructions to perform floating-point operations on
    108  *     16-bit registers. This is part of the VFPv4 specification, but
    109  *     not mandated by any Android ABI.
    110  *
    111  *   VFP_FMA:
    112  *     Fused multiply-accumulate VFP instructions extension. Also part of
    113  *     the VFPv4 specification, but not mandated by any Android ABI.
    114  *
    115  *   NEON_FMA:
    116  *     Fused multiply-accumulate NEON instructions extension. Optional
    117  *     extension from the VFPv4 specification, but not mandated by any
    118  *     Android ABI.
    119  *
    120  *   IDIV_ARM:
    121  *     Integer division available in ARM mode. Only available
    122  *     on recent CPUs (e.g. Cortex-A15).
    123  *
    124  *   IDIV_THUMB2:
    125  *     Integer division available in Thumb-2 mode. Only available
    126  *     on recent CPUs (e.g. Cortex-A15).
    127  *
    128  *   iWMMXt:
    129  *     Optional extension that adds MMX registers and operations to an
    130  *     ARM CPU. This is only available on a few XScale-based CPU designs
    131  *     sold by Marvell. Pretty rare in practice.
    132  *
    133  * If you want to tell the compiler to generate code that targets one of
    134  * the feature set above, you should probably use one of the following
    135  * flags (for more details, see technical note at the end of this file):
    136  *
    137  *   -mfpu=vfp
    138  *   -mfpu=vfpv2
    139  *     These are equivalent and tell GCC to use VFPv2 instructions for
    140  *     floating-point operations. Use this if you want your code to
    141  *     run on *some* ARMv6 devices, and any ARMv7-A device supported
    142  *     by Android.
    143  *
    144  *     Generated code requires VFPv2 feature.
    145  *
    146  *   -mfpu=vfpv3-d16
    147  *     Tell GCC to use VFPv3 instructions (using only 16 FPU registers).
    148  *     This should be generic code that runs on any CPU that supports the
    149  *     'armeabi-v7a' Android ABI. Note that no ARMv6 CPU supports this.
    150  *
    151  *     Generated code requires VFPv3 feature.
    152  *
    153  *   -mfpu=vfpv3
    154  *     Tell GCC to use VFPv3 instructions with 32 FPU registers.
    155  *     Generated code requires VFPv3|VFP_D32 features.
    156  *
    157  *   -mfpu=neon
    158  *     Tell GCC to use VFPv3 instructions with 32 FPU registers, and
    159  *     also support NEON intrinsics (see <arm_neon.h>).
    160  *     Generated code requires VFPv3|VFP_D32|NEON features.
    161  *
    162  *   -mfpu=vfpv4-d16
    163  *     Generated code requires VFPv3|VFP_FP16|VFP_FMA features.
    164  *
    165  *   -mfpu=vfpv4
    166  *     Generated code requires VFPv3|VFP_FP16|VFP_FMA|VFP_D32 features.
    167  *
    168  *   -mfpu=neon-vfpv4
    169  *     Generated code requires VFPv3|VFP_FP16|VFP_FMA|VFP_D32|NEON|NEON_FMA
    170  *     features.
    171  *
    172  *   -mcpu=cortex-a7
    173  *   -mcpu=cortex-a15
    174  *     Generated code requires VFPv3|VFP_FP16|VFP_FMA|VFP_D32|
    175  *                             NEON|NEON_FMA|IDIV_ARM|IDIV_THUMB2
    176  *     This flag implies -mfpu=neon-vfpv4.
    177  *
    178  *   -mcpu=iwmmxt
    179  *     Allows the use of iWMMXt instrinsics with GCC.
    180  *
    181  * IMPORTANT NOTE: These flags should only be tested when
    182  * android_getCpuFamily() returns ANDROID_CPU_FAMILY_ARM, i.e. this is a
    183  * 32-bit process.
    184  *
    185  * When running a 64-bit ARM process on an ARMv8 CPU,
    186  * android_getCpuFeatures() will return a different set of bitflags
    187  * (currently always 0).
    188  */
    189 enum {
    190     ANDROID_CPU_ARM_FEATURE_ARMv7       = (1 << 0),
    191     ANDROID_CPU_ARM_FEATURE_VFPv3       = (1 << 1),
    192     ANDROID_CPU_ARM_FEATURE_NEON        = (1 << 2),
    193     ANDROID_CPU_ARM_FEATURE_LDREX_STREX = (1 << 3),
    194     ANDROID_CPU_ARM_FEATURE_VFPv2       = (1 << 4),
    195     ANDROID_CPU_ARM_FEATURE_VFP_D32     = (1 << 5),
    196     ANDROID_CPU_ARM_FEATURE_VFP_FP16    = (1 << 6),
    197     ANDROID_CPU_ARM_FEATURE_VFP_FMA     = (1 << 7),
    198     ANDROID_CPU_ARM_FEATURE_NEON_FMA    = (1 << 8),
    199     ANDROID_CPU_ARM_FEATURE_IDIV_ARM    = (1 << 9),
    200     ANDROID_CPU_ARM_FEATURE_IDIV_THUMB2 = (1 << 10),
    201     ANDROID_CPU_ARM_FEATURE_iWMMXt      = (1 << 11),
    202 };
    203 
    204 /* The bit flags corresponding to the output of android_getCpuFeatures()
    205  * when android_getCpuFamily() returns ANDROID_CPU_FAMILY_X86.
    206  */
    207 enum {
    208     ANDROID_CPU_X86_FEATURE_SSSE3  = (1 << 0),
    209     ANDROID_CPU_X86_FEATURE_POPCNT = (1 << 1),
    210     ANDROID_CPU_X86_FEATURE_MOVBE  = (1 << 2),
    211 };
    212 
    213 /* Return the number of CPU cores detected on this device. */
    214 extern int android_getCpuCount(void);
    215 
    216 /* The following is used to force the CPU count and features
    217  * mask in sandboxed processes. Under 4.1 and higher, these processes
    218  * cannot access /proc, which is the only way to get information from
    219  * the kernel about the current hardware (at least on ARM).
    220  *
    221  * It _must_ be called only once, and before any android_getCpuXXX
    222  * function, any other case will fail.
    223  *
    224  * This function return 1 on success, and 0 on failure.
    225  */
    226 extern int android_setCpu(int      cpu_count,
    227                           uint64_t cpu_features);
    228 
    229 #ifdef __arm__
    230 /* Retrieve the ARM 32-bit CPUID value from the kernel.
    231  * Note that this cannot work on sandboxed processes under 4.1 and
    232  * higher, unless you called android_setCpuArm() before.
    233  */
    234 extern uint32_t android_getCpuIdArm(void);
    235 
    236 /* An ARM-specific variant of android_setCpu() that also allows you
    237  * to set the ARM CPUID field.
    238  */
    239 extern int android_setCpuArm(int      cpu_count,
    240                              uint64_t cpu_features,
    241                              uint32_t cpu_id);
    242 #endif
    243 
    244 __END_DECLS
    245 
    246 #endif /* CPU_FEATURES_H */
    247