981dc9
commit 6e8ba7fd574f530afb9681f21604475d5756d773
981dc9
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
981dc9
Date:   Fri Feb 8 16:53:40 2019 +0000
981dc9
981dc9
    Remove __get_clockfreq
981dc9
    
981dc9
    With clock_getres, clock_gettime, and clock_settime refactor to remove the
981dc9
    generic CLOCK_PROCESS_CPUTIME_ID and CLOCK_THREAD_CPUTIME_ID support through
981dc9
    hp-timing, there is no usage of internal __get_clockfreq.  This patch removes
981dc9
    both generic and Linux implementation..
981dc9
    
981dc9
    Checked with a build against aarch64-linux-gnu, i686-linux-gnu, ia64-linux-gnu,
981dc9
    sparc64-linux-gnu, powerpc-linux-gnu-power4.
981dc9
    
981dc9
            * include/libc-internal.h (__get_clockfreq): Remove prototype.
981dc9
            * rt/Makefile (clock-routines): Remove get_clockfreq.
981dc9
            * rt/get_clockfreq.c: Remove file.
981dc9
            * sysdeps/unix/sysv/linux/i386/get_clockfreq.c: Likewise.
981dc9
            * sysdeps/unix/sysv/linux/ia64/get_clockfreq.c: Likewise.
981dc9
            * sysdeps/unix/sysv/linux/sparc/sparc64/get_clockfreq.c: Likewise.
981dc9
            * sysdeps/unix/sysv/linux/powerpc/get_clockfreq.c: Move code to ...
981dc9
            * sysdeps/unix/sysv/linux/powerpc/get_timebase_freq.c: ... here.
981dc9
981dc9
Conflicts:
981dc9
	rt/get_clockfreq.c
981dc9
	sysdeps/unix/sysv/linux/i386/get_clockfreq.c
981dc9
	sysdeps/unix/sysv/linux/ia64/get_clockfreq.c
981dc9
	sysdeps/unix/sysv/linux/powerpc/get_clockfreq.c
981dc9
	  (Removal after copyright year change upstream.)
981dc9
	sysdeps/unix/sysv/linux/sparc/sparc64/get_clockfreq.c
981dc9
	  (Likewise.  Missing backport of aa0e46636a5b71 ("Break
981dc9
	  further lines before not after operators.").)
981dc9
981dc9
diff --git a/include/libc-internal.h b/include/libc-internal.h
981dc9
index 2167990c52bd708b..2ec07d9355db1808 100644
981dc9
--- a/include/libc-internal.h
981dc9
+++ b/include/libc-internal.h
981dc9
@@ -36,9 +36,6 @@ libc_hidden_proto (__profile_frequency)
981dc9
 extern void __cyg_profile_func_enter (void *this_fn, void *call_site);
981dc9
 extern void __cyg_profile_func_exit (void *this_fn, void *call_site);
981dc9
 
981dc9
-/* Get frequency of the system processor.  */
981dc9
-extern hp_timing_t __get_clockfreq (void);
981dc9
-
981dc9
 /* Free all allocated resources.  */
981dc9
 extern void __libc_freeres (void);
981dc9
 libc_hidden_proto (__libc_freeres)
981dc9
diff --git a/rt/Makefile b/rt/Makefile
981dc9
index 6d6b896ee9817c5c..14f38ef74d262188 100644
981dc9
--- a/rt/Makefile
981dc9
+++ b/rt/Makefile
981dc9
@@ -28,7 +28,7 @@ aio-routines   := aio_cancel aio_error aio_fsync aio_misc aio_read	\
981dc9
 		  aio_read64 aio_return aio_suspend aio_write		\
981dc9
 		  aio_write64 lio_listio lio_listio64 aio_sigqueue	\
981dc9
 		  aio_notify
981dc9
-clock-routines := get_clockfreq clock_getcpuclockid			\
981dc9
+clock-routines := clock_getcpuclockid					\
981dc9
 		  clock_getres clock_gettime clock_settime		\
981dc9
 		  clock_nanosleep
981dc9
 timer-routines := timer_create timer_delete timer_getoverr		\
981dc9
diff --git a/rt/get_clockfreq.c b/rt/get_clockfreq.c
981dc9
deleted file mode 100644
981dc9
index e695a6018c0b8322..0000000000000000
981dc9
--- a/rt/get_clockfreq.c
981dc9
+++ /dev/null
981dc9
@@ -1,27 +0,0 @@
981dc9
-/* Get frequency of the system processor.
981dc9
-   Copyright (C) 2000-2018 Free Software Foundation, Inc.
981dc9
-   This file is part of the GNU C Library.
981dc9
-
981dc9
-   The GNU C Library is free software; you can redistribute it and/or
981dc9
-   modify it under the terms of the GNU Lesser General Public
981dc9
-   License as published by the Free Software Foundation; either
981dc9
-   version 2.1 of the License, or (at your option) any later version.
981dc9
-
981dc9
-   The GNU C Library is distributed in the hope that it will be useful,
981dc9
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
981dc9
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
981dc9
-   Lesser General Public License for more details.
981dc9
-
981dc9
-   You should have received a copy of the GNU Lesser General Public
981dc9
-   License along with the GNU C Library; if not, see
981dc9
-   <http://www.gnu.org/licenses/>.  */
981dc9
-
981dc9
-#include <libc-internal.h>
981dc9
-
981dc9
-hp_timing_t
981dc9
-__get_clockfreq (void)
981dc9
-{
981dc9
-  /* There is no generic way to find this out since we have in general
981dc9
-     no counter register either.  */
981dc9
-  return 0;
981dc9
-}
981dc9
diff --git a/sysdeps/unix/sysv/linux/i386/get_clockfreq.c b/sysdeps/unix/sysv/linux/i386/get_clockfreq.c
981dc9
deleted file mode 100644
981dc9
index 633f186be80f10b0..0000000000000000
981dc9
--- a/sysdeps/unix/sysv/linux/i386/get_clockfreq.c
981dc9
+++ /dev/null
981dc9
@@ -1,88 +0,0 @@
981dc9
-/* Get frequency of the system processor.  i386/Linux version.
981dc9
-   Copyright (C) 2000-2018 Free Software Foundation, Inc.
981dc9
-   This file is part of the GNU C Library.
981dc9
-
981dc9
-   The GNU C Library is free software; you can redistribute it and/or
981dc9
-   modify it under the terms of the GNU Lesser General Public
981dc9
-   License as published by the Free Software Foundation; either
981dc9
-   version 2.1 of the License, or (at your option) any later version.
981dc9
-
981dc9
-   The GNU C Library is distributed in the hope that it will be useful,
981dc9
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
981dc9
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
981dc9
-   Lesser General Public License for more details.
981dc9
-
981dc9
-   You should have received a copy of the GNU Lesser General Public
981dc9
-   License along with the GNU C Library; if not, see
981dc9
-   <http://www.gnu.org/licenses/>.  */
981dc9
-
981dc9
-#include <ctype.h>
981dc9
-#include <fcntl.h>
981dc9
-#include <string.h>
981dc9
-#include <unistd.h>
981dc9
-
981dc9
-hp_timing_t
981dc9
-__get_clockfreq (void)
981dc9
-{
981dc9
-  /* We read the information from the /proc filesystem.  It contains at
981dc9
-     least one line like
981dc9
-	cpu MHz         : 497.840237
981dc9
-     or also
981dc9
-	cpu MHz         : 497.841
981dc9
-     We search for this line and convert the number in an integer.  */
981dc9
-  static hp_timing_t result;
981dc9
-  int fd;
981dc9
-
981dc9
-  /* If this function was called before, we know the result.  */
981dc9
-  if (result != 0)
981dc9
-    return result;
981dc9
-
981dc9
-  fd = __open ("/proc/cpuinfo", O_RDONLY);
981dc9
-  if (__glibc_likely (fd != -1))
981dc9
-    {
981dc9
-      /* XXX AFAIK the /proc filesystem can generate "files" only up
981dc9
-         to a size of 4096 bytes.  */
981dc9
-      char buf[4096];
981dc9
-      ssize_t n;
981dc9
-
981dc9
-      n = __read (fd, buf, sizeof buf);
981dc9
-      if (__builtin_expect (n, 1) > 0)
981dc9
-	{
981dc9
-	  char *mhz = memmem (buf, n, "cpu MHz", 7);
981dc9
-
981dc9
-	  if (__glibc_likely (mhz != NULL))
981dc9
-	    {
981dc9
-	      char *endp = buf + n;
981dc9
-	      int seen_decpoint = 0;
981dc9
-	      int ndigits = 0;
981dc9
-
981dc9
-	      /* Search for the beginning of the string.  */
981dc9
-	      while (mhz < endp && (*mhz < '0' || *mhz > '9') && *mhz != '\n')
981dc9
-		++mhz;
981dc9
-
981dc9
-	      while (mhz < endp && *mhz != '\n')
981dc9
-		{
981dc9
-		  if (*mhz >= '0' && *mhz <= '9')
981dc9
-		    {
981dc9
-		      result *= 10;
981dc9
-		      result += *mhz - '0';
981dc9
-		      if (seen_decpoint)
981dc9
-			++ndigits;
981dc9
-		    }
981dc9
-		  else if (*mhz == '.')
981dc9
-		    seen_decpoint = 1;
981dc9
-
981dc9
-		  ++mhz;
981dc9
-		}
981dc9
-
981dc9
-	      /* Compensate for missing digits at the end.  */
981dc9
-	      while (ndigits++ < 6)
981dc9
-		result *= 10;
981dc9
-	    }
981dc9
-	}
981dc9
-
981dc9
-      __close (fd);
981dc9
-    }
981dc9
-
981dc9
-  return result;
981dc9
-}
981dc9
diff --git a/sysdeps/unix/sysv/linux/ia64/get_clockfreq.c b/sysdeps/unix/sysv/linux/ia64/get_clockfreq.c
981dc9
deleted file mode 100644
981dc9
index f9d683a90247ac60..0000000000000000
981dc9
--- a/sysdeps/unix/sysv/linux/ia64/get_clockfreq.c
981dc9
+++ /dev/null
981dc9
@@ -1,87 +0,0 @@
981dc9
-/* Get frequency of the system processor.  IA-64/Linux version.
981dc9
-   Copyright (C) 2001-2018 Free Software Foundation, Inc.
981dc9
-   This file is part of the GNU C Library.
981dc9
-
981dc9
-   The GNU C Library is free software; you can redistribute it and/or
981dc9
-   modify it under the terms of the GNU Lesser General Public
981dc9
-   License as published by the Free Software Foundation; either
981dc9
-   version 2.1 of the License, or (at your option) any later version.
981dc9
-
981dc9
-   The GNU C Library is distributed in the hope that it will be useful,
981dc9
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
981dc9
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
981dc9
-   Lesser General Public License for more details.
981dc9
-
981dc9
-   You should have received a copy of the GNU Lesser General Public
981dc9
-   License along with the GNU C Library; if not, see
981dc9
-   <http://www.gnu.org/licenses/>.  */
981dc9
-
981dc9
-#include <ctype.h>
981dc9
-#include <fcntl.h>
981dc9
-#include <string.h>
981dc9
-#include <unistd.h>
981dc9
-
981dc9
-
981dc9
-hp_timing_t
981dc9
-__get_clockfreq (void)
981dc9
-{
981dc9
-  /* We read the information from the /proc filesystem.  It contains at
981dc9
-     least one line like
981dc9
-	itc MHz    : 733.390988
981dc9
-     We search for this line and convert the number in an integer.  */
981dc9
-  static hp_timing_t result;
981dc9
-  int fd;
981dc9
-
981dc9
-  /* If this function was called before, we know the result.  */
981dc9
-  if (result != 0)
981dc9
-    return result;
981dc9
-
981dc9
-  fd = __open ("/proc/cpuinfo", O_RDONLY);
981dc9
-  if (__builtin_expect (fd != -1, 1))
981dc9
-    {
981dc9
-      /* XXX AFAIK the /proc filesystem can generate "files" only up
981dc9
-         to a size of 4096 bytes.  */
981dc9
-      char buf[4096];
981dc9
-      ssize_t n;
981dc9
-
981dc9
-      n = __read (fd, buf, sizeof buf);
981dc9
-      if (__builtin_expect (n, 1) > 0)
981dc9
-	{
981dc9
-	  char *mhz = memmem (buf, n, "itc MHz", 7);
981dc9
-
981dc9
-	  if (__builtin_expect (mhz != NULL, 1))
981dc9
-	    {
981dc9
-	      char *endp = buf + n;
981dc9
-	      int seen_decpoint = 0;
981dc9
-	      int ndigits = 0;
981dc9
-
981dc9
-	      /* Search for the beginning of the string.  */
981dc9
-	      while (mhz < endp && (*mhz < '0' || *mhz > '9') && *mhz != '\n')
981dc9
-		++mhz;
981dc9
-
981dc9
-	      while (mhz < endp && *mhz != '\n')
981dc9
-		{
981dc9
-		  if (*mhz >= '0' && *mhz <= '9')
981dc9
-		    {
981dc9
-		      result *= 10;
981dc9
-		      result += *mhz - '0';
981dc9
-		      if (seen_decpoint)
981dc9
-			++ndigits;
981dc9
-		    }
981dc9
-		  else if (*mhz == '.')
981dc9
-		    seen_decpoint = 1;
981dc9
-
981dc9
-		  ++mhz;
981dc9
-		}
981dc9
-
981dc9
-	      /* Compensate for missing digits at the end.  */
981dc9
-	      while (ndigits++ < 6)
981dc9
-		result *= 10;
981dc9
-	    }
981dc9
-	}
981dc9
-
981dc9
-      __close (fd);
981dc9
-    }
981dc9
-
981dc9
-  return result;
981dc9
-}
981dc9
diff --git a/sysdeps/unix/sysv/linux/powerpc/get_clockfreq.c b/sysdeps/unix/sysv/linux/powerpc/get_clockfreq.c
981dc9
deleted file mode 100644
981dc9
index 3a2216021bfb5408..0000000000000000
981dc9
--- a/sysdeps/unix/sysv/linux/powerpc/get_clockfreq.c
981dc9
+++ /dev/null
981dc9
@@ -1,107 +0,0 @@
981dc9
-/* Get frequency of the system processor.  powerpc/Linux version.
981dc9
-   Copyright (C) 2000-2018 Free Software Foundation, Inc.
981dc9
-   This file is part of the GNU C Library.
981dc9
-
981dc9
-   The GNU C Library is free software; you can redistribute it and/or
981dc9
-   modify it under the terms of the GNU Lesser General Public
981dc9
-   License as published by the Free Software Foundation; either
981dc9
-   version 2.1 of the License, or (at your option) any later version.
981dc9
-
981dc9
-   The GNU C Library is distributed in the hope that it will be useful,
981dc9
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
981dc9
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
981dc9
-   Lesser General Public License for more details.
981dc9
-
981dc9
-   You should have received a copy of the GNU Lesser General Public
981dc9
-   License along with the GNU C Library; if not, see
981dc9
-   <http://www.gnu.org/licenses/>.  */
981dc9
-
981dc9
-#include <ctype.h>
981dc9
-#include <fcntl.h>
981dc9
-#include <stdint.h>
981dc9
-#include <string.h>
981dc9
-#include <unistd.h>
981dc9
-#include <sysdep.h>
981dc9
-#include <libc-vdso.h>
981dc9
-#include <not-cancel.h>
981dc9
-
981dc9
-hp_timing_t
981dc9
-__get_clockfreq (void)
981dc9
-{
981dc9
-  hp_timing_t result = 0L;
981dc9
-
981dc9
-#ifdef SHARED
981dc9
-  /* The vDSO does not return an error (it clear cr0.so on returning).  */
981dc9
-  INTERNAL_SYSCALL_DECL (err);
981dc9
-  result =
981dc9
-    INTERNAL_VSYSCALL_NO_SYSCALL_FALLBACK (get_tbfreq, err, uint64_t, 0);
981dc9
-#else
981dc9
-  /* We read the information from the /proc filesystem.  /proc/cpuinfo
981dc9
-     contains at least one line like:
981dc9
-     timebase        : 33333333
981dc9
-     We search for this line and convert the number into an integer.  */
981dc9
-  int fd = __open_nocancel ("/proc/cpuinfo", O_RDONLY);
981dc9
-  if (__glibc_likely (fd != -1))
981dc9
-    return result;
981dc9
-
981dc9
-  /* The timebase will be in the 1st 1024 bytes for systems with up
981dc9
-     to 8 processors.  If the first read returns less then 1024
981dc9
-     bytes read,  we have the whole cpuinfo and can start the scan.
981dc9
-     Otherwise we will have to read more to insure we have the
981dc9
-     timebase value in the scan.  */
981dc9
-  char buf[1024];
981dc9
-  ssize_t n;
981dc9
-
981dc9
-  n = __read_nocancel (fd, buf, sizeof (buf));
981dc9
-  if (n == sizeof (buf))
981dc9
-    {
981dc9
-      /* We are here because the 1st read returned exactly sizeof
981dc9
-         (buf) bytes.  This implies that we are not at EOF and may
981dc9
-         not have read the timebase value yet.  So we need to read
981dc9
-         more bytes until we know we have EOF.  We copy the lower
981dc9
-         half of buf to the upper half and read sizeof (buf)/2
981dc9
-         bytes into the lower half of buf and repeat until we
981dc9
-         reach EOF.  We can assume that the timebase will be in
981dc9
-         the last 512 bytes of cpuinfo, so two 512 byte half_bufs
981dc9
-         will be sufficient to contain the timebase and will
981dc9
-         handle the case where the timebase spans the half_buf
981dc9
-         boundry.  */
981dc9
-      const ssize_t half_buf = sizeof (buf) / 2;
981dc9
-      while (n >= half_buf)
981dc9
-	{
981dc9
-	  memcpy (buf, buf + half_buf, half_buf);
981dc9
-	  n = __read_nocancel (fd, buf + half_buf, half_buf);
981dc9
-	}
981dc9
-      if (n >= 0)
981dc9
-	n += half_buf;
981dc9
-    }
981dc9
-  __close_nocancel (fd);
981dc9
-
981dc9
-  if (__glibc_likely (n > 0))
981dc9
-    {
981dc9
-      char *mhz = memmem (buf, n, "timebase", 7);
981dc9
-
981dc9
-      if (__glibc_likely (mhz != NULL))
981dc9
-	{
981dc9
-	  char *endp = buf + n;
981dc9
-
981dc9
-	  /* Search for the beginning of the string.  */
981dc9
-	  while (mhz < endp && (*mhz < '0' || *mhz > '9') && *mhz != '\n')
981dc9
-	    ++mhz;
981dc9
-
981dc9
-	  while (mhz < endp && *mhz != '\n')
981dc9
-	    {
981dc9
-	      if (*mhz >= '0' && *mhz <= '9')
981dc9
-		{
981dc9
-		  result *= 10;
981dc9
-		  result += *mhz - '0';
981dc9
-		}
981dc9
-
981dc9
-	      ++mhz;
981dc9
-	    }
981dc9
-	}
981dc9
-    }
981dc9
-#endif
981dc9
-
981dc9
-  return result;
981dc9
-}
981dc9
diff --git a/sysdeps/unix/sysv/linux/powerpc/get_timebase_freq.c b/sysdeps/unix/sysv/linux/powerpc/get_timebase_freq.c
981dc9
index 6435e1f31529c7de..1177ccbc8faeafe7 100644
981dc9
--- a/sysdeps/unix/sysv/linux/powerpc/get_timebase_freq.c
981dc9
+++ b/sysdeps/unix/sysv/linux/powerpc/get_timebase_freq.c
981dc9
@@ -17,11 +17,90 @@
981dc9
    <http://www.gnu.org/licenses/>.  */
981dc9
 
981dc9
 #include <stdint.h>
981dc9
+#include <string.h>
981dc9
+
981dc9
 #include <libc-internal.h>
981dc9
+#include <not-cancel.h>
981dc9
+#include <libc-vdso.h>
981dc9
 
981dc9
 uint64_t
981dc9
 __get_timebase_freq (void)
981dc9
 {
981dc9
-  return (uint64_t) __get_clockfreq ();
981dc9
+  hp_timing_t result = 0L;
981dc9
+
981dc9
+#ifdef SHARED
981dc9
+  /* The vDSO does not return an error (it clear cr0.so on returning).  */
981dc9
+  INTERNAL_SYSCALL_DECL (err);
981dc9
+  result =
981dc9
+    INTERNAL_VSYSCALL_NO_SYSCALL_FALLBACK (get_tbfreq, err, uint64_t, 0);
981dc9
+#else
981dc9
+  /* We read the information from the /proc filesystem.  /proc/cpuinfo
981dc9
+     contains at least one line like:
981dc9
+     timebase        : 33333333
981dc9
+     We search for this line and convert the number into an integer.  */
981dc9
+  int fd = __open_nocancel ("/proc/cpuinfo", O_RDONLY);
981dc9
+  if (__glibc_likely (fd != -1))
981dc9
+    return result;
981dc9
+
981dc9
+  /* The timebase will be in the 1st 1024 bytes for systems with up
981dc9
+     to 8 processors.  If the first read returns less then 1024
981dc9
+     bytes read,  we have the whole cpuinfo and can start the scan.
981dc9
+     Otherwise we will have to read more to insure we have the
981dc9
+     timebase value in the scan.  */
981dc9
+  char buf[1024];
981dc9
+  ssize_t n;
981dc9
+
981dc9
+  n = __read_nocancel (fd, buf, sizeof (buf));
981dc9
+  if (n == sizeof (buf))
981dc9
+    {
981dc9
+      /* We are here because the 1st read returned exactly sizeof
981dc9
+         (buf) bytes.  This implies that we are not at EOF and may
981dc9
+         not have read the timebase value yet.  So we need to read
981dc9
+         more bytes until we know we have EOF.  We copy the lower
981dc9
+         half of buf to the upper half and read sizeof (buf)/2
981dc9
+         bytes into the lower half of buf and repeat until we
981dc9
+         reach EOF.  We can assume that the timebase will be in
981dc9
+         the last 512 bytes of cpuinfo, so two 512 byte half_bufs
981dc9
+         will be sufficient to contain the timebase and will
981dc9
+         handle the case where the timebase spans the half_buf
981dc9
+         boundry.  */
981dc9
+      const ssize_t half_buf = sizeof (buf) / 2;
981dc9
+      while (n >= half_buf)
981dc9
+	{
981dc9
+	  memcpy (buf, buf + half_buf, half_buf);
981dc9
+	  n = __read_nocancel (fd, buf + half_buf, half_buf);
981dc9
+	}
981dc9
+      if (n >= 0)
981dc9
+	n += half_buf;
981dc9
+    }
981dc9
+  __close_nocancel (fd);
981dc9
+
981dc9
+  if (__glibc_likely (n > 0))
981dc9
+    {
981dc9
+      char *mhz = memmem (buf, n, "timebase", 7);
981dc9
+
981dc9
+      if (__glibc_likely (mhz != NULL))
981dc9
+	{
981dc9
+	  char *endp = buf + n;
981dc9
+
981dc9
+	  /* Search for the beginning of the string.  */
981dc9
+	  while (mhz < endp && (*mhz < '0' || *mhz > '9') && *mhz != '\n')
981dc9
+	    ++mhz;
981dc9
+
981dc9
+	  while (mhz < endp && *mhz != '\n')
981dc9
+	    {
981dc9
+	      if (*mhz >= '0' && *mhz <= '9')
981dc9
+		{
981dc9
+		  result *= 10;
981dc9
+		  result += *mhz - '0';
981dc9
+		}
981dc9
+
981dc9
+	      ++mhz;
981dc9
+	    }
981dc9
+	}
981dc9
+    }
981dc9
+#endif
981dc9
+
981dc9
+  return result;
981dc9
 }
981dc9
 weak_alias (__get_timebase_freq, __ppc_get_timebase_freq)
981dc9
diff --git a/sysdeps/unix/sysv/linux/sparc/sparc64/get_clockfreq.c b/sysdeps/unix/sysv/linux/sparc/sparc64/get_clockfreq.c
981dc9
deleted file mode 100644
981dc9
index 6838a77a769ddae8..0000000000000000
981dc9
--- a/sysdeps/unix/sysv/linux/sparc/sparc64/get_clockfreq.c
981dc9
+++ /dev/null
981dc9
@@ -1,250 +0,0 @@
981dc9
-/* Get frequency of the system processor.  sparc64 version.
981dc9
-   Copyright (C) 2001-2018 Free Software Foundation, Inc.
981dc9
-   This file is part of the GNU C Library.
981dc9
-
981dc9
-   The GNU C Library is free software; you can redistribute it and/or
981dc9
-   modify it under the terms of the GNU Lesser General Public
981dc9
-   License as published by the Free Software Foundation; either
981dc9
-   version 2.1 of the License, or (at your option) any later version.
981dc9
-
981dc9
-   The GNU C Library is distributed in the hope that it will be useful,
981dc9
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
981dc9
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
981dc9
-   Lesser General Public License for more details.
981dc9
-
981dc9
-   You should have received a copy of the GNU Lesser General Public
981dc9
-   License along with the GNU C Library; if not, see
981dc9
-   <http://www.gnu.org/licenses/>.  */
981dc9
-
981dc9
-#include <ctype.h>
981dc9
-#include <fcntl.h>
981dc9
-#include <string.h>
981dc9
-#include <unistd.h>
981dc9
-#include <dirent.h>
981dc9
-#include <stdlib.h>
981dc9
-#include <inttypes.h>
981dc9
-#include <sys/ioctl.h>
981dc9
-#include <asm/openpromio.h>
981dc9
-
981dc9
-static hp_timing_t
981dc9
-__get_clockfreq_via_cpuinfo (void)
981dc9
-{
981dc9
-  hp_timing_t result;
981dc9
-  int fd;
981dc9
-
981dc9
-  result = 0;
981dc9
-
981dc9
-  fd = __open ("/proc/cpuinfo", O_RDONLY);
981dc9
-  if (fd != -1)
981dc9
-    {
981dc9
-      char buf[8192];
981dc9
-      ssize_t n;
981dc9
-
981dc9
-      n = __read (fd, buf, sizeof buf);
981dc9
-      if (n > 0)
981dc9
-	{
981dc9
-	  char *mhz = memmem (buf, n, "Cpu0ClkTck", 7);
981dc9
-
981dc9
-	  if (mhz != NULL)
981dc9
-	    {
981dc9
-	      char *endp = buf + n;
981dc9
-
981dc9
-	      /* Search for the beginning of the string.  */
981dc9
-	      while (mhz < endp
981dc9
-		     && (*mhz < '0' || *mhz > '9')
981dc9
-		     && (*mhz < 'a' || *mhz > 'f')
981dc9
-		     && *mhz != '\n')
981dc9
-		++mhz;
981dc9
-
981dc9
-	      while (mhz < endp && *mhz != '\n')
981dc9
-		{
981dc9
-		  if ((*mhz >= '0' && *mhz <= '9') ||
981dc9
-		      (*mhz >= 'a' && *mhz <= 'f'))
981dc9
-		    {
981dc9
-		      result <<= 4;
981dc9
-		      if (*mhz >= '0' && *mhz <= '9')
981dc9
-			result += *mhz - '0';
981dc9
-		      else
981dc9
-			result += (*mhz - 'a') + 10;
981dc9
-		    }
981dc9
-		  ++mhz;
981dc9
-		}
981dc9
-	    }
981dc9
-	}
981dc9
-
981dc9
-      __close (fd);
981dc9
-    }
981dc9
-
981dc9
-  return result;
981dc9
-}
981dc9
-
981dc9
-static hp_timing_t
981dc9
-__get_clockfreq_via_proc_openprom (void)
981dc9
-{
981dc9
-  hp_timing_t result;
981dc9
-  int obp_fd;
981dc9
-
981dc9
-  result = 0;
981dc9
-
981dc9
-  obp_fd = __open ("/proc/openprom", O_RDONLY);
981dc9
-  if (obp_fd != -1)
981dc9
-    {
981dc9
-      unsigned long int buf[4096 / sizeof (unsigned long int)];
981dc9
-      struct dirent64 *dirp = (struct dirent64 *) buf;
981dc9
-      ssize_t len;
981dc9
-
981dc9
-      while ((len = __getdents64 (obp_fd, (char *) dirp, sizeof (buf))) > 0)
981dc9
-	{
981dc9
-	  struct dirent64 *this_dirp = dirp;
981dc9
-
981dc9
-	  while (len > 0)
981dc9
-	    {
981dc9
-	      char node[strlen ("/proc/openprom/")
981dc9
-			+ _D_ALLOC_NAMLEN (this_dirp)
981dc9
-			+ strlen ("/clock-frequency")];
981dc9
-	      char *prop;
981dc9
-	      int fd;
981dc9
-
981dc9
-	      /* Note that
981dc9
-		   strlen("/clock-frequency") > strlen("/device_type")
981dc9
-	      */
981dc9
-	      __stpcpy (prop = __stpcpy (__stpcpy (node, "/proc/openprom/"),
981dc9
-					 this_dirp->d_name),
981dc9
-			"/device_type");
981dc9
-	      fd = __open (node, O_RDONLY);
981dc9
-	      if (fd != -1)
981dc9
-		{
981dc9
-		  char type_string[128];
981dc9
-		  int ret;
981dc9
-
981dc9
-		  ret = __read (fd, type_string, sizeof (type_string));
981dc9
-		  if (ret > 0 && strncmp (type_string, "'cpu'", 5) == 0)
981dc9
-		    {
981dc9
-		      int clkfreq_fd;
981dc9
-
981dc9
-		      __stpcpy (prop, "/clock-frequency");
981dc9
-		      clkfreq_fd = __open (node, O_RDONLY);
981dc9
-		      if (clkfreq_fd != -1)
981dc9
-			{
981dc9
-			  if (__read (clkfreq_fd, type_string,
981dc9
-				      sizeof (type_string)) > 0)
981dc9
-			    result = (hp_timing_t)
981dc9
-			      strtoumax (type_string, NULL, 16);
981dc9
-			  __close (clkfreq_fd);
981dc9
-			}
981dc9
-		    }
981dc9
-		  __close (fd);
981dc9
-		}
981dc9
-
981dc9
-	      if (result != 0)
981dc9
-		break;
981dc9
-
981dc9
-	      len -= this_dirp->d_reclen;
981dc9
-	      this_dirp = (struct dirent64 *)
981dc9
-		((char *) this_dirp + this_dirp->d_reclen);
981dc9
-	    }
981dc9
-	  if (result != 0)
981dc9
-	    break;
981dc9
-	}
981dc9
-      __close (obp_fd);
981dc9
-    }
981dc9
-
981dc9
-  return result;
981dc9
-}
981dc9
-
981dc9
-static void set_obp_int (struct openpromio *op, int val)
981dc9
-{
981dc9
-  char *cp = op->oprom_array;
981dc9
-  int *ip = (int *) cp;
981dc9
-
981dc9
-  *ip = val;
981dc9
-}
981dc9
-
981dc9
-static int get_obp_int (struct openpromio *op)
981dc9
-{
981dc9
-  char *cp = op->oprom_array;
981dc9
-  int *ip = (int *) cp;
981dc9
-
981dc9
-  return *ip;
981dc9
-}
981dc9
-
981dc9
-static hp_timing_t
981dc9
-__get_clockfreq_via_dev_openprom (void)
981dc9
-{
981dc9
-  hp_timing_t result;
981dc9
-  int obp_dev_fd;
981dc9
-
981dc9
-  result = 0;
981dc9
-
981dc9
-  obp_dev_fd = __open ("/dev/openprom", O_RDONLY);
981dc9
-  if (obp_dev_fd != -1)
981dc9
-    {
981dc9
-      char obp_buf[8192];
981dc9
-      struct openpromio *obp_cmd = (struct openpromio *)obp_buf;
981dc9
-      int ret;
981dc9
-
981dc9
-      obp_cmd->oprom_size =
981dc9
-	sizeof (obp_buf) - sizeof (unsigned int);
981dc9
-      set_obp_int (obp_cmd, 0);
981dc9
-      ret = __ioctl (obp_dev_fd, OPROMCHILD, (char *) obp_cmd);
981dc9
-      if (ret == 0)
981dc9
-	{
981dc9
-	  int cur_node = get_obp_int (obp_cmd);
981dc9
-
981dc9
-	  while (cur_node != 0 && cur_node != -1)
981dc9
-	    {
981dc9
-	      obp_cmd->oprom_size = sizeof (obp_buf) - sizeof (unsigned int);
981dc9
-	      strcpy (obp_cmd->oprom_array, "device_type");
981dc9
-	      ret = __ioctl (obp_dev_fd, OPROMGETPROP, (char *) obp_cmd);
981dc9
-	      if (ret == 0
981dc9
-		  && strncmp (obp_cmd->oprom_array, "cpu", 3) == 0)
981dc9
-		{
981dc9
-		  obp_cmd->oprom_size = (sizeof (obp_buf)
981dc9
-					 - sizeof (unsigned int));
981dc9
-		  strcpy (obp_cmd->oprom_array, "clock-frequency");
981dc9
-		  ret = __ioctl (obp_dev_fd, OPROMGETPROP, (char *) obp_cmd);
981dc9
-		  if (ret == 0)
981dc9
-		    result = (hp_timing_t) get_obp_int (obp_cmd);
981dc9
-		}
981dc9
-	      obp_cmd->oprom_size = sizeof (obp_buf) - sizeof (unsigned int);
981dc9
-	      set_obp_int (obp_cmd, cur_node);
981dc9
-	      ret = __ioctl (obp_dev_fd, OPROMNEXT, (char *) obp_cmd);
981dc9
-	      if (ret < 0)
981dc9
-		break;
981dc9
-	      cur_node = get_obp_int (obp_cmd);
981dc9
-	    }
981dc9
-	}
981dc9
-    }
981dc9
-
981dc9
-  return result;
981dc9
-}
981dc9
-
981dc9
-hp_timing_t
981dc9
-__get_clockfreq (void)
981dc9
-{
981dc9
-  static hp_timing_t result;
981dc9
-
981dc9
-  /* If this function was called before, we know the result.  */
981dc9
-  if (result != 0)
981dc9
-    return result;
981dc9
-
981dc9
-  /* We first read the information from the /proc/cpuinfo file.
981dc9
-     It contains at least one line like
981dc9
-	Cpu0ClkTick         : 000000002cb41780
981dc9
-     We search for this line and convert the number in an integer.  */
981dc9
-  result = __get_clockfreq_via_cpuinfo ();
981dc9
-  if (result != 0)
981dc9
-    return result;
981dc9
-
981dc9
-  /* If that did not work, try to find an OpenPROM node
981dc9
-     with device_type equal to 'cpu' using /dev/openprom
981dc9
-     and fetch the clock-frequency property from there.  */
981dc9
-  result = __get_clockfreq_via_dev_openprom ();
981dc9
-  if (result != 0)
981dc9
-    return result;
981dc9
-
981dc9
-  /* Finally, try the same lookup as above but using /proc/openprom.  */
981dc9
-  result = __get_clockfreq_via_proc_openprom ();
981dc9
-
981dc9
-  return result;
981dc9
-}