2.9
[glibc/nacl-glibc.git] / sysdeps / unix / sysv / linux / sparc / sparc64 / get_clockfreq.c
blobe2d31a2e459168b803b42a82e14b84b39b387982
1 /* Get frequency of the system processor. sparc64 version.
2 Copyright (C) 2001 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <ctype.h>
21 #include <fcntl.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <dirent.h>
25 #include <stdlib.h>
26 #include <sys/ioctl.h>
27 #include <libc-internal.h>
28 #include <asm/openpromio.h>
30 static hp_timing_t
31 __get_clockfreq_via_cpuinfo (void)
33 hp_timing_t result;
34 int fd;
36 result = 0;
38 fd = open ("/proc/cpuinfo", O_RDONLY);
39 if (fd != -1)
41 char buf[8192];
42 ssize_t n;
44 n = read (fd, buf, sizeof buf);
45 if (n > 0)
47 char *mhz = memmem (buf, n, "Cpu0ClkTck", 7);
49 if (mhz != NULL)
51 char *endp = buf + n;
53 /* Search for the beginning of the string. */
54 while (mhz < endp
55 && (*mhz < '0' || *mhz > '9')
56 && (*mhz < 'a' || *mhz > 'f')
57 && *mhz != '\n')
58 ++mhz;
60 while (mhz < endp && *mhz != '\n')
62 if ((*mhz >= '0' && *mhz <= '9') ||
63 (*mhz >= 'a' && *mhz <= 'f'))
65 result <<= 4;
66 if (*mhz >= '0' && *mhz <= '9')
67 result += *mhz - '0';
68 else
69 result += (*mhz - 'a') + 10;
71 ++mhz;
76 close (fd);
79 return result;
82 static hp_timing_t
83 __get_clockfreq_via_proc_openprom (void)
85 hp_timing_t result;
86 int obp_fd;
88 result = 0;
90 obp_fd = open ("/proc/openprom", O_RDONLY);
91 if (obp_fd != -1)
93 unsigned long int buf[4096 / sizeof (unsigned long int)];
94 struct dirent *dirp = (struct dirent *) buf;
95 off_t dbase = (off_t) 0;
96 ssize_t len;
98 while ((len = getdirentries (obp_fd, (char *) dirp,
99 sizeof (buf), &dbase)) > 0)
101 struct dirent *this_dirp = dirp;
103 while (len > 0)
105 char node[strlen ("/proc/openprom/")
106 + _D_ALLOC_NAMLEN (this_dirp)
107 + strlen ("/clock-frequency")];
108 char *prop;
109 int fd;
111 /* Note that
112 strlen("/clock-frequency") > strlen("/device_type")
114 __stpcpy (prop = __stpcpy (__stpcpy (node, "/proc/openprom/"),
115 this_dirp->d_name),
116 "/device_type");
117 fd = open (node, O_RDONLY);
118 if (fd != -1)
120 char type_string[128];
121 int ret;
123 ret = read (fd, type_string, sizeof (type_string));
124 if (ret > 0 && strncmp (type_string, "'cpu'", 5) == 0)
126 int clkfreq_fd;
128 __stpcpy (prop, "/clock-frequency");
129 clkfreq_fd = open (node, O_RDONLY);
130 if (fd != -1)
132 if (read (clkfreq_fd, type_string,
133 sizeof (type_string)) > 0)
134 result = (hp_timing_t)
135 strtoull (type_string, NULL, 16);
136 close (clkfreq_fd);
139 close (fd);
142 if (result != 0)
143 break;
145 len -= this_dirp->d_reclen;
146 this_dirp = (struct dirent *)
147 ((char *) this_dirp + this_dirp->d_reclen);
149 if (result != 0)
150 break;
152 close (obp_fd);
155 return result;
158 static hp_timing_t
159 __get_clockfreq_via_dev_openprom (void)
161 hp_timing_t result;
162 int obp_dev_fd;
164 result = 0;
166 obp_dev_fd = open ("/dev/openprom", O_RDONLY);
167 if (obp_dev_fd != -1)
169 char obp_buf[8192];
170 struct openpromio *obp_cmd = (struct openpromio *)obp_buf;
171 int ret;
173 obp_cmd->oprom_size =
174 sizeof (obp_buf) - sizeof (unsigned int);
175 *(int *) obp_cmd->oprom_array = 0;
176 ret = ioctl (obp_dev_fd, OPROMCHILD, (char *) obp_cmd);
177 if (ret == 0)
179 int cur_node = *(int *) obp_cmd->oprom_array;
181 while (cur_node != 0 && cur_node != -1)
183 obp_cmd->oprom_size = sizeof (obp_buf) - sizeof (unsigned int);
184 strcpy (obp_cmd->oprom_array, "device_type");
185 ret = ioctl (obp_dev_fd, OPROMGETPROP, (char *) obp_cmd);
186 if (ret == 0
187 && strncmp (obp_cmd->oprom_array, "cpu", 3) == 0)
189 obp_cmd->oprom_size = (sizeof (obp_buf)
190 - sizeof (unsigned int));
191 strcpy (obp_cmd->oprom_array, "clock-frequency");
192 ret = ioctl (obp_dev_fd, OPROMGETPROP, (char *) obp_cmd);
193 if (ret == 0)
194 result =
195 (hp_timing_t) *(unsigned int *) obp_cmd->oprom_array;
197 obp_cmd->oprom_size = sizeof (obp_buf) - sizeof (unsigned int);
198 *(int *) obp_cmd->oprom_array = cur_node;
199 ret = ioctl (obp_dev_fd, OPROMNEXT, (char *) obp_cmd);
200 if (ret < 0)
201 break;
202 cur_node = *(int *)obp_cmd->oprom_array;
207 return result;
210 hp_timing_t
211 __get_clockfreq (void)
213 static hp_timing_t result;
215 /* If this function was called before, we know the result. */
216 if (result != 0)
217 return result;
219 /* We first read the information from the /proc/cpuinfo file.
220 It contains at least one line like
221 Cpu0ClkTick : 000000002cb41780
222 We search for this line and convert the number in an integer. */
223 result = __get_clockfreq_via_cpuinfo ();
224 if (result != 0)
225 return result;
227 /* If that did not work, try to find an OpenPROM node
228 with device_type equal to 'cpu' using /dev/openprom
229 and fetch the clock-frequency property from there. */
230 result = __get_clockfreq_via_dev_openprom ();
231 if (result != 0)
232 return result;
234 /* Finally, try the same lookup as above but using /proc/openprom. */
235 result = __get_clockfreq_via_proc_openprom ();
237 return result;