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