]> git.ozlabs.org Git - ccan/blobdiff - ccan/cpuid/cpuid.c
cpuid: prefix every function with cpuid_
[ccan] / ccan / cpuid / cpuid.c
index f25688e5e6c8b093c95768f6d0fd9be903378a8a..6ab0e50f4256fa30e8678667336f89313ace5677 100644 (file)
@@ -23,6 +23,7 @@
  *     http://en.wikipedia.org/wiki/CPUID
  */
 #include <stdint.h>
+#include <string.h>
 
 #include "cpuid.h"
 
@@ -57,19 +58,49 @@ static void ___cpuid(cpuid_t info, uint32_t *eax, uint32_t *ebx, uint32_t *ecx,
 }
 #endif
 
-int highest_ext_func_supported(void)
-{
-       static int highest;
+static struct {
+       int feature;
+       unsigned mask;
+       int instruction;        /* 0 = ecx, 1 = edx.  */
+} features[] = {
+       { CF_MMX,               1 << 23,        1 },
+       { CF_SSE,               1 << 25,        1 },
+       { CF_SSE2,              1 << 26,        1 },
+       { CF_SSE3,              1 << 9,         0 },
+       { CF_FPU,               1 << 0,         1 },
+
+       { CF_TSC,               1 << 4,         1 },
+       { CF_MSR,               1 << 5,         1 },
+
+       { CF_SSSE3,             1 << 9,         0 },
+       { CF_AVX,               1 << 28,        0 },
+
+       /* Extended ones.  */
+       { CEF_x64,              1 << 30,        1 },
+       { CEF_FPU,              1 << 0,         1 },
+       { CEF_DE,               1 << 2,         1 },
+       { CEF_SYSCALLRET,       1 << 11,        1 },
+       { CEF_CMOV,             1 << 15,        1 },
+
+       { CEF_SSE4a,            1 << 6,         0 },
+       { CEF_FMA4,             1 << 16,        0 },
+       { CEF_XOP,              1 << 11,        0 }
+};
 
-       if (!highest) {
-               asm volatile(
-                       "cpuid\n\t"
-                       : "=a" (highest)
-                       : "a" (CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED)
-               );
+static int has_feature(int feature, uint32_t ecx, uint32_t edx)
+{
+       int i;
+
+       for (i = 0; i < sizeof(features) / sizeof(features[0]); ++i) {
+               if (features[i].feature == feature) {
+                       if (features[i].instruction == 0)
+                               return (ecx & features[i].mask);
+                       else
+                               return (edx & features[i].mask);
+               }
        }
 
-       return highest;
+       return 0;
 }
 
 int cpuid_test_feature(cpuid_t feature)
@@ -77,48 +108,82 @@ int cpuid_test_feature(cpuid_t feature)
        if (feature > CPU_VIRT_PHYS_ADDR_SIZES || feature < CPU_EXTENDED_PROC_INFO_FEATURE_BITS)
                return 0;
 
-       return (feature <= highest_ext_func_supported());
+       return (feature <= cpuid_highest_ext_func_supported());
 }
 
-int cpuid_has_feature(cpufeature_t feature)
+int cpuid_has_feature(int feature, int extended)
 {
        uint32_t eax, ebx, ecx, edx;
 
-       ___cpuid(CPU_PROCINFO_AND_FEATUREBITS, &eax, &ebx, &ecx, &edx);
-       switch (feature) {
-               case CF_MMX:
-               case CF_SSE:
-               case CF_SSE2:
-                       return (edx & ((int)feature)) != 0;
-               case CF_SSE3:
-               case CF_SSSE3:
-               case CF_SSE41:
-               case CF_SSE42:
-               case CF_AVX:
-               case CF_FMA:
-                       return (ecx & ((int)feature)) != 0;
+       if (extended == 0)
+               ___cpuid(CPU_PROCINFO_AND_FEATUREBITS, &eax, &ebx, &ecx, &edx);
+       else
+               ___cpuid(CPU_EXTENDED_PROC_INFO_FEATURE_BITS, &eax, &ebx, &ecx, &edx);
+
+       return has_feature(feature, ecx, edx);
+}
+
+static const char *cpuids[] = {
+       "Nooooooooone",
+       "AMDisbetter!",
+       "AuthenticAMD",
+       "CentaurHauls",
+       "CyrixInstead",
+       "GenuineIntel",
+       "TransmetaCPU",
+       "GeniuneTMx86",
+       "Geode by NSC",
+       "NexGenDriven",
+       "RiseRiseRise",
+       "SiS SiS SiS ",
+       "UMC UMC UMC ",
+       "VIA VIA VIA ",
+       "Vortex86 SoC",
+       "KVMKVMKVMKVM"
+};
+
+cputype_t cpuid_get_cpu_type(void)
+{
+       static cputype_t cputype;
+       if (cputype == CT_NONE) {
+               union {
+                       char buf[12];
+                       uint32_t bufu32[3];
+               } u;
+               uint32_t i;
+
+               ___cpuid(CPU_VENDORID, &i, &u.bufu32[0], &u.bufu32[2], &u.bufu32[1]);
+               u.buf[12] = '\0';
+
+               for (i = 0; i < sizeof(cpuids) / sizeof(cpuids[0]); ++i) {
+                       if (strncmp(cpuids[i], u.buf, 12) == 0) {
+                               cputype = (cputype_t)i;
+                               break;
+                       }
+               }
        }
 
-       return 0;
+       return cputype;
 }
 
-int cpuid_has_ext_feature(cpuextfeature_t extfeature)
+const char *cpuid_get_cpu_type_string(const cputype_t cputype)
 {
-       uint32_t eax, ebx, ecx, edx;
-       if (!cpuid_test_feature(CPU_EXTENDED_PROC_INFO_FEATURE_BITS))
-               return 0;
+       return cpuids[(int)cputype];
+}
+
+int cpuid_highest_ext_func_supported(void)
+{
+       static int highest;
 
-       ___cpuid(CPU_EXTENDED_PROC_INFO_FEATURE_BITS, &eax, &ebx, &ecx, &edx);
-       switch (extfeature) {
-               case CEF_x64:
-                       return (edx & ((int)extfeature)) != 0;
-               case CEF_SSE4a:
-               case CEF_FMA4:
-               case CEF_XOP:
-                       return (ecx & ((int)extfeature)) != 0;
+       if (!highest) {
+               asm volatile(
+                       "cpuid\n\t"
+                       : "=a" (highest)
+                       : "a" (CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED)
+               );
        }
 
-       return 0;
+       return highest;
 }
 
 void cpuid(cpuid_t info, void *buf)
@@ -137,7 +202,7 @@ void cpuid(cpuid_t info, void *buf)
                ___cpuid(CPU_PROC_BRAND_STRING_INTERNAL1, &ubuf[8], &ubuf[9], &ubuf[10], &ubuf[11]);
                return;
        } else if (info == CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED) {
-               *ubuf = highest_ext_func_supported();
+               *ubuf = cpuid_highest_ext_func_supported();
                return;
        }
 
@@ -169,11 +234,10 @@ void cpuid(cpuid_t info, void *buf)
                case CPU_L1_CACHE_AND_TLB_IDS:
                        break;
                case CPU_EXTENDED_L2_CACHE_FEATURES:
-                       ubuf[0] = (ecx & 0xFF);                 /* Cache size  */
-                       ubuf[1] = (ecx >> 12) & 0xF;            /* Line size  */
-                       ubuf[2] = (ecx >> 16) & 0xFFFF;         /* Associativity  */
+                       *ubuf = ecx;
                        break;
                case CPU_ADV_POWER_MGT_INFO:
+                       *ubuf = edx;
                        break;
                case CPU_VIRT_PHYS_ADDR_SIZES:
                        *ubuf = eax;