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 typedef enum {
     37     ANDROID_CPU_FAMILY_UNKNOWN = 0,
     38     ANDROID_CPU_FAMILY_ARM,
     39     ANDROID_CPU_FAMILY_X86,
     40     ANDROID_CPU_FAMILY_MIPS,
     41 
     42     ANDROID_CPU_FAMILY_MAX  /* do not remove */
     43 
     44 } AndroidCpuFamily;
     45 
     46 /* Return family of the device's CPU */
     47 extern AndroidCpuFamily   android_getCpuFamily(void);
     48 
     49 /* The list of feature flags for ARM CPUs that can be recognized by the
     50  * library. Value details are:
     51  *
     52  *   VFPv2:
     53  *     CPU supports the VFPv2 instruction set. Many, but not all, ARMv6 CPUs
     54  *     support these instructions. VFPv2 is a subset of VFPv3 so this will
     55  *     be set whenever VFPv3 is set too.
     56  *
     57  *   ARMv7:
     58  *     CPU supports the ARMv7-A basic instruction set.
     59  *     This feature is mandated by the 'armeabi-v7a' ABI.
     60  *
     61  *   VFPv3:
     62  *     CPU supports the VFPv3-D16 instruction set, providing hardware FPU
     63  *     support for single and double precision floating point registers.
     64  *     Note that only 16 FPU registers are available by default, unless
     65  *     the D32 bit is set too. This feature is also mandated by the
     66  *     'armeabi-v7a' ABI.
     67  *
     68  *   VFP_D32:
     69  *     CPU VFP optional extension that provides 32 FPU registers,
     70  *     instead of 16. Note that ARM mandates this feature is the 'NEON'
     71  *     feature is implemented by the CPU.
     72  *
     73  *   NEON:
     74  *     CPU FPU supports "ARM Advanced SIMD" instructions, also known as
     75  *     NEON. Note that this mandates the VFP_D32 feature as well, per the
     76  *     ARM Architecture specification.
     77  *
     78  *   VFP_FP16:
     79  *     Half-width floating precision VFP extension. If set, the CPU
     80  *     supports instructions to perform floating-point operations on
     81  *     16-bit registers. This is part of the VFPv4 specification, but
     82  *     not mandated by any Android ABI.
     83  *
     84  *   VFP_FMA:
     85  *     Fused multiply-accumulate VFP instructions extension. Also part of
     86  *     the VFPv4 specification, but not mandated by any Android ABI.
     87  *
     88  *   NEON_FMA:
     89  *     Fused multiply-accumulate NEON instructions extension. Optional
     90  *     extension from the VFPv4 specification, but not mandated by any
     91  *     Android ABI.
     92  *
     93  *   IDIV_ARM:
     94  *     Integer division available in ARM mode. Only available
     95  *     on recent CPUs (e.g. Cortex-A15).
     96  *
     97  *   IDIV_THUMB2:
     98  *     Integer division available in Thumb-2 mode. Only available
     99  *     on recent CPUs (e.g. Cortex-A15).
    100  *
    101  *   iWMMXt:
    102  *     Optional extension that adds MMX registers and operations to an
    103  *     ARM CPU. This is only available on a few XScale-based CPU designs
    104  *     sold by Marvell. Pretty rare in practice.
    105  *
    106  * If you want to tell the compiler to generate code that targets one of
    107  * the feature set above, you should probably use one of the following
    108  * flags (for more details, see technical note at the end of this file):
    109  *
    110  *   -mfpu=vfp
    111  *   -mfpu=vfpv2
    112  *     These are equivalent and tell GCC to use VFPv2 instructions for
    113  *     floating-point operations. Use this if you want your code to
    114  *     run on *some* ARMv6 devices, and any ARMv7-A device supported
    115  *     by Android.
    116  *
    117  *     Generated code requires VFPv2 feature.
    118  *
    119  *   -mfpu=vfpv3-d16
    120  *     Tell GCC to use VFPv3 instructions (using only 16 FPU registers).
    121  *     This should be generic code that runs on any CPU that supports the
    122  *     'armeabi-v7a' Android ABI. Note that no ARMv6 CPU supports this.
    123  *
    124  *     Generated code requires VFPv3 feature.
    125  *
    126  *   -mfpu=vfpv3
    127  *     Tell GCC to use VFPv3 instructions with 32 FPU registers.
    128  *     Generated code requires VFPv3|VFP_D32 features.
    129  *
    130  *   -mfpu=neon
    131  *     Tell GCC to use VFPv3 instructions with 32 FPU registers, and
    132  *     also support NEON intrinsics (see <arm_neon.h>).
    133  *     Generated code requires VFPv3|VFP_D32|NEON features.
    134  *
    135  *   -mfpu=vfpv4-d16
    136  *     Generated code requires VFPv3|VFP_FP16|VFP_FMA features.
    137  *
    138  *   -mfpu=vfpv4
    139  *     Generated code requires VFPv3|VFP_FP16|VFP_FMA|VFP_D32 features.
    140  *
    141  *   -mfpu=neon-vfpv4
    142  *     Generated code requires VFPv3|VFP_FP16|VFP_FMA|VFP_D32|NEON|NEON_FMA
    143  *     features.
    144  *
    145  *   -mcpu=cortex-a7
    146  *   -mcpu=cortex-a15
    147  *     Generated code requires VFPv3|VFP_FP16|VFP_FMA|VFP_D32|
    148  *                             NEON|NEON_FMA|IDIV_ARM|IDIV_THUMB2
    149  *     This flag implies -mfpu=neon-vfpv4.
    150  *
    151  *   -mcpu=iwmmxt
    152  *     Allows the use of iWMMXt instrinsics with GCC.
    153  */
    154 enum {
    155     ANDROID_CPU_ARM_FEATURE_ARMv7       = (1 << 0),
    156     ANDROID_CPU_ARM_FEATURE_VFPv3       = (1 << 1),
    157     ANDROID_CPU_ARM_FEATURE_NEON        = (1 << 2),
    158     ANDROID_CPU_ARM_FEATURE_LDREX_STREX = (1 << 3),
    159     ANDROID_CPU_ARM_FEATURE_VFPv2       = (1 << 4),
    160     ANDROID_CPU_ARM_FEATURE_VFP_D32     = (1 << 5),
    161     ANDROID_CPU_ARM_FEATURE_VFP_FP16    = (1 << 6),
    162     ANDROID_CPU_ARM_FEATURE_VFP_FMA     = (1 << 7),
    163     ANDROID_CPU_ARM_FEATURE_NEON_FMA    = (1 << 8),
    164     ANDROID_CPU_ARM_FEATURE_IDIV_ARM    = (1 << 9),
    165     ANDROID_CPU_ARM_FEATURE_IDIV_THUMB2 = (1 << 10),
    166     ANDROID_CPU_ARM_FEATURE_iWMMXt      = (1 << 11),
    167 };
    168 
    169 enum {
    170     ANDROID_CPU_X86_FEATURE_SSSE3  = (1 << 0),
    171     ANDROID_CPU_X86_FEATURE_POPCNT = (1 << 1),
    172     ANDROID_CPU_X86_FEATURE_MOVBE  = (1 << 2),
    173 };
    174 
    175 extern uint64_t    android_getCpuFeatures(void);
    176 
    177 /* Return the number of CPU cores detected on this device. */
    178 extern int         android_getCpuCount(void);
    179 
    180 /* The following is used to force the CPU count and features
    181  * mask in sandboxed processes. Under 4.1 and higher, these processes
    182  * cannot access /proc, which is the only way to get information from
    183  * the kernel about the current hardware (at least on ARM).
    184  *
    185  * It _must_ be called only once, and before any android_getCpuXXX
    186  * function, any other case will fail.
    187  *
    188  * This function return 1 on success, and 0 on failure.
    189  */
    190 extern int android_setCpu(int      cpu_count,
    191                           uint64_t cpu_features);
    192 
    193 #ifdef __arm__
    194 /* Retrieve the ARM 32-bit CPUID value from the kernel.
    195  * Note that this cannot work on sandboxed processes under 4.1 and
    196  * higher, unless you called android_setCpuArm() before.
    197  */
    198 extern uint32_t android_getCpuIdArm(void);
    199 
    200 /* An ARM-specific variant of android_setCpu() that also allows you
    201  * to set the ARM CPUID field.
    202  */
    203 extern int android_setCpuArm(int      cpu_count,
    204                              uint64_t cpu_features,
    205                              uint32_t cpu_id);
    206 #endif
    207 
    208 __END_DECLS
    209 
    210 #endif /* CPU_FEATURES_H */
    211