e354a5
commit a98dc92dd1e278df4c501deb07985018bc2b06de
e354a5
Author: mayshao-oc <mayshao-oc@zhaoxin.com>
e354a5
Date:   Sun Apr 26 13:48:27 2020 +0800
e354a5
e354a5
    x86: Add cache information support for Zhaoxin processors
e354a5
    
e354a5
    To obtain Zhaoxin CPU cache information, add a new function
e354a5
    handle_zhaoxin().
e354a5
    
e354a5
    Add a new function get_common_cache_info() that extracts the code
e354a5
    in init_cacheinfo() to get the value of the variable shared, threads.
e354a5
    
e354a5
    Add Zhaoxin branch in init_cacheinfo() for initializing variables,
e354a5
    such as __x86_shared_cache_size.
e354a5
e354a5
diff --git a/sysdeps/x86/cacheinfo.c b/sysdeps/x86/cacheinfo.c
e354a5
index f1125f30223f5ca3..aa7cb705d546bcd0 100644
e354a5
--- a/sysdeps/x86/cacheinfo.c
e354a5
+++ b/sysdeps/x86/cacheinfo.c
e354a5
@@ -436,6 +436,57 @@ handle_amd (int name)
e354a5
 }
e354a5
 
e354a5
 
e354a5
+static long int __attribute__ ((noinline))
e354a5
+handle_zhaoxin (int name)
e354a5
+{
e354a5
+  unsigned int eax;
e354a5
+  unsigned int ebx;
e354a5
+  unsigned int ecx;
e354a5
+  unsigned int edx;
e354a5
+
e354a5
+  int folded_rel_name = (M(name) / 3) * 3;
e354a5
+
e354a5
+  unsigned int round = 0;
e354a5
+  while (1)
e354a5
+    {
e354a5
+      __cpuid_count (4, round, eax, ebx, ecx, edx);
e354a5
+
e354a5
+      enum { null = 0, data = 1, inst = 2, uni = 3 } type = eax & 0x1f;
e354a5
+      if (type == null)
e354a5
+        break;
e354a5
+
e354a5
+      unsigned int level = (eax >> 5) & 0x7;
e354a5
+
e354a5
+      if ((level == 1 && type == data
e354a5
+        && folded_rel_name == M(_SC_LEVEL1_DCACHE_SIZE))
e354a5
+        || (level == 1 && type == inst
e354a5
+            && folded_rel_name == M(_SC_LEVEL1_ICACHE_SIZE))
e354a5
+        || (level == 2 && folded_rel_name == M(_SC_LEVEL2_CACHE_SIZE))
e354a5
+        || (level == 3 && folded_rel_name == M(_SC_LEVEL3_CACHE_SIZE)))
e354a5
+        {
e354a5
+          unsigned int offset = M(name) - folded_rel_name;
e354a5
+
e354a5
+          if (offset == 0)
e354a5
+            /* Cache size.  */
e354a5
+            return (((ebx >> 22) + 1)
e354a5
+                * (((ebx >> 12) & 0x3ff) + 1)
e354a5
+                * ((ebx & 0xfff) + 1)
e354a5
+                * (ecx + 1));
e354a5
+          if (offset == 1)
e354a5
+            return (ebx >> 22) + 1;
e354a5
+
e354a5
+          assert (offset == 2);
e354a5
+          return (ebx & 0xfff) + 1;
e354a5
+        }
e354a5
+
e354a5
+      ++round;
e354a5
+    }
e354a5
+
e354a5
+  /* Nothing found.  */
e354a5
+  return 0;
e354a5
+}
e354a5
+
e354a5
+
e354a5
 /* Get the value of the system variable NAME.  */
e354a5
 long int
e354a5
 attribute_hidden
e354a5
@@ -449,6 +500,9 @@ __cache_sysconf (int name)
e354a5
   if (cpu_features->basic.kind == arch_kind_amd)
e354a5
     return handle_amd (name);
e354a5
 
e354a5
+  if (cpu_features->basic.kind == arch_kind_zhaoxin)
e354a5
+    return handle_zhaoxin (name);
e354a5
+
e354a5
   // XXX Fill in more vendors.
e354a5
 
e354a5
   /* CPU not known, we have no information.  */
e354a5
@@ -482,6 +536,224 @@ int __x86_prefetchw attribute_hidden;
e354a5
 #endif
e354a5
 
e354a5
 
e354a5
+static void
e354a5
+get_common_cache_info (long int *shared_ptr, unsigned int *threads_ptr,
e354a5
+                long int core)
e354a5
+{
e354a5
+  unsigned int eax;
e354a5
+  unsigned int ebx;
e354a5
+  unsigned int ecx;
e354a5
+  unsigned int edx;
e354a5
+
e354a5
+  /* Number of logical processors sharing L2 cache.  */
e354a5
+  int threads_l2;
e354a5
+
e354a5
+  /* Number of logical processors sharing L3 cache.  */
e354a5
+  int threads_l3;
e354a5
+
e354a5
+  const struct cpu_features *cpu_features = __get_cpu_features ();
e354a5
+  int max_cpuid = cpu_features->basic.max_cpuid;
e354a5
+  unsigned int family = cpu_features->basic.family;
e354a5
+  unsigned int model = cpu_features->basic.model;
e354a5
+  long int shared = *shared_ptr;
e354a5
+  unsigned int threads = *threads_ptr;
e354a5
+  bool inclusive_cache = true;
e354a5
+  bool support_count_mask = true;
e354a5
+
e354a5
+  /* Try L3 first.  */
e354a5
+  unsigned int level = 3;
e354a5
+
e354a5
+  if (cpu_features->basic.kind == arch_kind_zhaoxin && family == 6)
e354a5
+    support_count_mask = false;
e354a5
+
e354a5
+  if (shared <= 0)
e354a5
+    {
e354a5
+      /* Try L2 otherwise.  */
e354a5
+      level  = 2;
e354a5
+      shared = core;
e354a5
+      threads_l2 = 0;
e354a5
+      threads_l3 = -1;
e354a5
+    }
e354a5
+  else
e354a5
+    {
e354a5
+      threads_l2 = 0;
e354a5
+      threads_l3 = 0;
e354a5
+    }
e354a5
+
e354a5
+  /* A value of 0 for the HTT bit indicates there is only a single
e354a5
+     logical processor.  */
e354a5
+  if (HAS_CPU_FEATURE (HTT))
e354a5
+    {
e354a5
+      /* Figure out the number of logical threads that share the
e354a5
+         highest cache level.  */
e354a5
+      if (max_cpuid >= 4)
e354a5
+        {
e354a5
+          int i = 0;
e354a5
+
e354a5
+          /* Query until cache level 2 and 3 are enumerated.  */
e354a5
+          int check = 0x1 | (threads_l3 == 0) << 1;
e354a5
+          do
e354a5
+            {
e354a5
+              __cpuid_count (4, i++, eax, ebx, ecx, edx);
e354a5
+
e354a5
+              /* There seems to be a bug in at least some Pentium Ds
e354a5
+                 which sometimes fail to iterate all cache parameters.
e354a5
+                 Do not loop indefinitely here, stop in this case and
e354a5
+                 assume there is no such information.  */
e354a5
+              if (cpu_features->basic.kind == arch_kind_intel
e354a5
+                  && (eax & 0x1f) == 0 )
e354a5
+                goto intel_bug_no_cache_info;
e354a5
+
e354a5
+              switch ((eax >> 5) & 0x7)
e354a5
+                {
e354a5
+                  default:
e354a5
+                    break;
e354a5
+                  case 2:
e354a5
+                    if ((check & 0x1))
e354a5
+                      {
e354a5
+                        /* Get maximum number of logical processors
e354a5
+                           sharing L2 cache.  */
e354a5
+                        threads_l2 = (eax >> 14) & 0x3ff;
e354a5
+                        check &= ~0x1;
e354a5
+                      }
e354a5
+                    break;
e354a5
+                  case 3:
e354a5
+                    if ((check & (0x1 << 1)))
e354a5
+                      {
e354a5
+                        /* Get maximum number of logical processors
e354a5
+                           sharing L3 cache.  */
e354a5
+                        threads_l3 = (eax >> 14) & 0x3ff;
e354a5
+
e354a5
+                        /* Check if L2 and L3 caches are inclusive.  */
e354a5
+                        inclusive_cache = (edx & 0x2) != 0;
e354a5
+                        check &= ~(0x1 << 1);
e354a5
+                      }
e354a5
+                    break;
e354a5
+                }
e354a5
+            }
e354a5
+          while (check);
e354a5
+
e354a5
+          /* If max_cpuid >= 11, THREADS_L2/THREADS_L3 are the maximum
e354a5
+             numbers of addressable IDs for logical processors sharing
e354a5
+             the cache, instead of the maximum number of threads
e354a5
+             sharing the cache.  */
e354a5
+          if (max_cpuid >= 11 && support_count_mask)
e354a5
+            {
e354a5
+              /* Find the number of logical processors shipped in
e354a5
+                 one core and apply count mask.  */
e354a5
+              i = 0;
e354a5
+
e354a5
+              /* Count SMT only if there is L3 cache.  Always count
e354a5
+                 core if there is no L3 cache.  */
e354a5
+              int count = ((threads_l2 > 0 && level == 3)
e354a5
+                           | ((threads_l3 > 0
e354a5
+                               || (threads_l2 > 0 && level == 2)) << 1));
e354a5
+
e354a5
+              while (count)
e354a5
+                {
e354a5
+                  __cpuid_count (11, i++, eax, ebx, ecx, edx);
e354a5
+
e354a5
+                  int shipped = ebx & 0xff;
e354a5
+                  int type = ecx & 0xff00;
e354a5
+                  if (shipped == 0 || type == 0)
e354a5
+                    break;
e354a5
+                  else if (type == 0x100)
e354a5
+                    {
e354a5
+                      /* Count SMT.  */
e354a5
+                      if ((count & 0x1))
e354a5
+                        {
e354a5
+                          int count_mask;
e354a5
+
e354a5
+                          /* Compute count mask.  */
e354a5
+                          asm ("bsr %1, %0"
e354a5
+                               : "=r" (count_mask) : "g" (threads_l2));
e354a5
+                          count_mask = ~(-1 << (count_mask + 1));
e354a5
+                          threads_l2 = (shipped - 1) & count_mask;
e354a5
+                          count &= ~0x1;
e354a5
+                        }
e354a5
+                    }
e354a5
+                  else if (type == 0x200)
e354a5
+                    {
e354a5
+                      /* Count core.  */
e354a5
+                      if ((count & (0x1 << 1)))
e354a5
+                        {
e354a5
+                          int count_mask;
e354a5
+                          int threads_core
e354a5
+                            = (level == 2 ? threads_l2 : threads_l3);
e354a5
+
e354a5
+                          /* Compute count mask.  */
e354a5
+                          asm ("bsr %1, %0"
e354a5
+                               : "=r" (count_mask) : "g" (threads_core));
e354a5
+                          count_mask = ~(-1 << (count_mask + 1));
e354a5
+                          threads_core = (shipped - 1) & count_mask;
e354a5
+                          if (level == 2)
e354a5
+                            threads_l2 = threads_core;
e354a5
+                          else
e354a5
+                            threads_l3 = threads_core;
e354a5
+                          count &= ~(0x1 << 1);
e354a5
+                        }
e354a5
+                    }
e354a5
+                }
e354a5
+            }
e354a5
+          if (threads_l2 > 0)
e354a5
+            threads_l2 += 1;
e354a5
+          if (threads_l3 > 0)
e354a5
+            threads_l3 += 1;
e354a5
+          if (level == 2)
e354a5
+            {
e354a5
+              if (threads_l2)
e354a5
+                {
e354a5
+                  threads = threads_l2;
e354a5
+                  if (cpu_features->basic.kind == arch_kind_intel
e354a5
+                      && threads > 2
e354a5
+                      && family == 6)
e354a5
+                    switch (model)
e354a5
+                      {
e354a5
+                        case 0x37:
e354a5
+                        case 0x4a:
e354a5
+                        case 0x4d:
e354a5
+                        case 0x5a:
e354a5
+                        case 0x5d:
e354a5
+                          /* Silvermont has L2 cache shared by 2 cores.  */
e354a5
+                          threads = 2;
e354a5
+                          break;
e354a5
+                        default:
e354a5
+                          break;
e354a5
+                      }
e354a5
+                }
e354a5
+            }
e354a5
+          else if (threads_l3)
e354a5
+            threads = threads_l3;
e354a5
+        }
e354a5
+      else
e354a5
+        {
e354a5
+intel_bug_no_cache_info:
e354a5
+          /* Assume that all logical threads share the highest cache
e354a5
+             level.  */
e354a5
+          threads
e354a5
+            = ((cpu_features->cpuid[COMMON_CPUID_INDEX_1].ebx
e354a5
+                >> 16) & 0xff);
e354a5
+        }
e354a5
+
e354a5
+        /* Cap usage of highest cache level to the number of supported
e354a5
+           threads.  */
e354a5
+        if (shared > 0 && threads > 0)
e354a5
+          shared /= threads;
e354a5
+    }
e354a5
+
e354a5
+  /* Account for non-inclusive L2 and L3 caches.  */
e354a5
+  if (!inclusive_cache)
e354a5
+    {
e354a5
+      if (threads_l2 > 0)
e354a5
+        core /= threads_l2;
e354a5
+      shared += core;
e354a5
+    }
e354a5
+
e354a5
+  *shared_ptr = shared;
e354a5
+  *threads_ptr = threads;
e354a5
+}
e354a5
+
e354a5
+
e354a5
 static void
e354a5
 __attribute__((constructor))
e354a5
 init_cacheinfo (void)
e354a5
@@ -494,211 +766,25 @@ init_cacheinfo (void)
e354a5
   int max_cpuid_ex;
e354a5
   long int data = -1;
e354a5
   long int shared = -1;
e354a5
-  unsigned int level;
e354a5
+  long int core;
e354a5
   unsigned int threads = 0;
e354a5
   const struct cpu_features *cpu_features = __get_cpu_features ();
e354a5
-  int max_cpuid = cpu_features->basic.max_cpuid;
e354a5
 
e354a5
   if (cpu_features->basic.kind == arch_kind_intel)
e354a5
     {
e354a5
       data = handle_intel (_SC_LEVEL1_DCACHE_SIZE, cpu_features);
e354a5
-
e354a5
-      long int core = handle_intel (_SC_LEVEL2_CACHE_SIZE, cpu_features);
e354a5
-      bool inclusive_cache = true;
e354a5
-
e354a5
-      /* Try L3 first.  */
e354a5
-      level  = 3;
e354a5
+      core = handle_intel (_SC_LEVEL2_CACHE_SIZE, cpu_features);
e354a5
       shared = handle_intel (_SC_LEVEL3_CACHE_SIZE, cpu_features);
e354a5
 
e354a5
-      /* Number of logical processors sharing L2 cache.  */
e354a5
-      int threads_l2;
e354a5
-
e354a5
-      /* Number of logical processors sharing L3 cache.  */
e354a5
-      int threads_l3;
e354a5
-
e354a5
-      if (shared <= 0)
e354a5
-	{
e354a5
-	  /* Try L2 otherwise.  */
e354a5
-	  level  = 2;
e354a5
-	  shared = core;
e354a5
-	  threads_l2 = 0;
e354a5
-	  threads_l3 = -1;
e354a5
-	}
e354a5
-      else
e354a5
-	{
e354a5
-	  threads_l2 = 0;
e354a5
-	  threads_l3 = 0;
e354a5
-	}
e354a5
-
e354a5
-      /* A value of 0 for the HTT bit indicates there is only a single
e354a5
-	 logical processor.  */
e354a5
-      if (HAS_CPU_FEATURE (HTT))
e354a5
-	{
e354a5
-	  /* Figure out the number of logical threads that share the
e354a5
-	     highest cache level.  */
e354a5
-	  if (max_cpuid >= 4)
e354a5
-	    {
e354a5
-	      unsigned int family = cpu_features->basic.family;
e354a5
-	      unsigned int model = cpu_features->basic.model;
e354a5
-
e354a5
-	      int i = 0;
e354a5
-
e354a5
-	      /* Query until cache level 2 and 3 are enumerated.  */
e354a5
-	      int check = 0x1 | (threads_l3 == 0) << 1;
e354a5
-	      do
e354a5
-		{
e354a5
-		  __cpuid_count (4, i++, eax, ebx, ecx, edx);
e354a5
-
e354a5
-		  /* There seems to be a bug in at least some Pentium Ds
e354a5
-		     which sometimes fail to iterate all cache parameters.
e354a5
-		     Do not loop indefinitely here, stop in this case and
e354a5
-		     assume there is no such information.  */
e354a5
-		  if ((eax & 0x1f) == 0)
e354a5
-		    goto intel_bug_no_cache_info;
e354a5
-
e354a5
-		  switch ((eax >> 5) & 0x7)
e354a5
-		    {
e354a5
-		    default:
e354a5
-		      break;
e354a5
-		    case 2:
e354a5
-		      if ((check & 0x1))
e354a5
-			{
e354a5
-			  /* Get maximum number of logical processors
e354a5
-			     sharing L2 cache.  */
e354a5
-			  threads_l2 = (eax >> 14) & 0x3ff;
e354a5
-			  check &= ~0x1;
e354a5
-			}
e354a5
-		      break;
e354a5
-		    case 3:
e354a5
-		      if ((check & (0x1 << 1)))
e354a5
-			{
e354a5
-			  /* Get maximum number of logical processors
e354a5
-			     sharing L3 cache.  */
e354a5
-			  threads_l3 = (eax >> 14) & 0x3ff;
e354a5
-
e354a5
-			  /* Check if L2 and L3 caches are inclusive.  */
e354a5
-			  inclusive_cache = (edx & 0x2) != 0;
e354a5
-			  check &= ~(0x1 << 1);
e354a5
-			}
e354a5
-		      break;
e354a5
-		    }
e354a5
-		}
e354a5
-	      while (check);
e354a5
-
e354a5
-	      /* If max_cpuid >= 11, THREADS_L2/THREADS_L3 are the maximum
e354a5
-		 numbers of addressable IDs for logical processors sharing
e354a5
-		 the cache, instead of the maximum number of threads
e354a5
-		 sharing the cache.  */
e354a5
-	      if (max_cpuid >= 11)
e354a5
-		{
e354a5
-		  /* Find the number of logical processors shipped in
e354a5
-		     one core and apply count mask.  */
e354a5
-		  i = 0;
e354a5
-
e354a5
-		  /* Count SMT only if there is L3 cache.  Always count
e354a5
-		     core if there is no L3 cache.  */
e354a5
-		  int count = ((threads_l2 > 0 && level == 3)
e354a5
-			       | ((threads_l3 > 0
e354a5
-				   || (threads_l2 > 0 && level == 2)) << 1));
e354a5
-
e354a5
-		  while (count)
e354a5
-		    {
e354a5
-		      __cpuid_count (11, i++, eax, ebx, ecx, edx);
e354a5
-
e354a5
-		      int shipped = ebx & 0xff;
e354a5
-		      int type = ecx & 0xff00;
e354a5
-		      if (shipped == 0 || type == 0)
e354a5
-			break;
e354a5
-		      else if (type == 0x100)
e354a5
-			{
e354a5
-			  /* Count SMT.  */
e354a5
-			  if ((count & 0x1))
e354a5
-			    {
e354a5
-			      int count_mask;
e354a5
-
e354a5
-			      /* Compute count mask.  */
e354a5
-			      asm ("bsr %1, %0"
e354a5
-				   : "=r" (count_mask) : "g" (threads_l2));
e354a5
-			      count_mask = ~(-1 << (count_mask + 1));
e354a5
-			      threads_l2 = (shipped - 1) & count_mask;
e354a5
-			      count &= ~0x1;
e354a5
-			    }
e354a5
-			}
e354a5
-		      else if (type == 0x200)
e354a5
-			{
e354a5
-			  /* Count core.  */
e354a5
-			  if ((count & (0x1 << 1)))
e354a5
-			    {
e354a5
-			      int count_mask;
e354a5
-			      int threads_core
e354a5
-				= (level == 2 ? threads_l2 : threads_l3);
e354a5
-
e354a5
-			      /* Compute count mask.  */
e354a5
-			      asm ("bsr %1, %0"
e354a5
-				   : "=r" (count_mask) : "g" (threads_core));
e354a5
-			      count_mask = ~(-1 << (count_mask + 1));
e354a5
-			      threads_core = (shipped - 1) & count_mask;
e354a5
-			      if (level == 2)
e354a5
-				threads_l2 = threads_core;
e354a5
-			      else
e354a5
-				threads_l3 = threads_core;
e354a5
-			      count &= ~(0x1 << 1);
e354a5
-			    }
e354a5
-			}
e354a5
-		    }
e354a5
-		}
e354a5
-	      if (threads_l2 > 0)
e354a5
-		threads_l2 += 1;
e354a5
-	      if (threads_l3 > 0)
e354a5
-		threads_l3 += 1;
e354a5
-	      if (level == 2)
e354a5
-		{
e354a5
-		  if (threads_l2)
e354a5
-		    {
e354a5
-		      threads = threads_l2;
e354a5
-		      if (threads > 2 && family == 6)
e354a5
-			switch (model)
e354a5
-			  {
e354a5
-			  case 0x37:
e354a5
-			  case 0x4a:
e354a5
-			  case 0x4d:
e354a5
-			  case 0x5a:
e354a5
-			  case 0x5d:
e354a5
-			    /* Silvermont has L2 cache shared by 2 cores.  */
e354a5
-			    threads = 2;
e354a5
-			    break;
e354a5
-			  default:
e354a5
-			    break;
e354a5
-			  }
e354a5
-		    }
e354a5
-		}
e354a5
-	      else if (threads_l3)
e354a5
-		threads = threads_l3;
e354a5
-	    }
e354a5
-	  else
e354a5
-	    {
e354a5
-intel_bug_no_cache_info:
e354a5
-	      /* Assume that all logical threads share the highest cache
e354a5
-		 level.  */
e354a5
-
e354a5
-	      threads
e354a5
-		= ((cpu_features->cpuid[COMMON_CPUID_INDEX_1].ebx
e354a5
-		    >> 16) & 0xff);
e354a5
-	    }
e354a5
-
e354a5
-	  /* Cap usage of highest cache level to the number of supported
e354a5
-	     threads.  */
e354a5
-	  if (shared > 0 && threads > 0)
e354a5
-	    shared /= threads;
e354a5
-	}
e354a5
+      get_common_cache_info (&shared, &threads, core);
e354a5
+    }
e354a5
+  else if (cpu_features->basic.kind == arch_kind_zhaoxin)
e354a5
+    {
e354a5
+      data = handle_zhaoxin (_SC_LEVEL1_DCACHE_SIZE);
e354a5
+      core = handle_zhaoxin (_SC_LEVEL2_CACHE_SIZE);
e354a5
+      shared = handle_zhaoxin (_SC_LEVEL3_CACHE_SIZE);
e354a5
 
e354a5
-      /* Account for non-inclusive L2 and L3 caches.  */
e354a5
-      if (!inclusive_cache)
e354a5
-	{
e354a5
-	  if (threads_l2 > 0)
e354a5
-	    core /= threads_l2;
e354a5
-	  shared += core;
e354a5
-	}
e354a5
+      get_common_cache_info (&shared, &threads, core);
e354a5
     }
e354a5
   else if (cpu_features->basic.kind == arch_kind_amd)
e354a5
     {