Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / unix / sysv / linux / sparc / sparc64 / get_clockfreq.c
blob99ee15cfba44770b32bbf19528763921ada3ddc0
1 /* Get frequency of the system processor. sparc64 version.
2 Copyright (C) 2001-2014 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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <ctype.h>
20 #include <fcntl.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <dirent.h>
24 #include <stdlib.h>
25 #include <inttypes.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 ssize_t len;
97 while ((len = __getdents (obp_fd, (char *) dirp, sizeof (buf))) > 0)
99 struct dirent *this_dirp = dirp;
101 while (len > 0)
103 char node[strlen ("/proc/openprom/")
104 + _D_ALLOC_NAMLEN (this_dirp)
105 + strlen ("/clock-frequency")];
106 char *prop;
107 int fd;
109 /* Note that
110 strlen("/clock-frequency") > strlen("/device_type")
112 __stpcpy (prop = __stpcpy (__stpcpy (node, "/proc/openprom/"),
113 this_dirp->d_name),
114 "/device_type");
115 fd = __open (node, O_RDONLY);
116 if (fd != -1)
118 char type_string[128];
119 int ret;
121 ret = __read (fd, type_string, sizeof (type_string));
122 if (ret > 0 && strncmp (type_string, "'cpu'", 5) == 0)
124 int clkfreq_fd;
126 __stpcpy (prop, "/clock-frequency");
127 clkfreq_fd = __open (node, O_RDONLY);
128 if (clkfreq_fd != -1)
130 if (__read (clkfreq_fd, type_string,
131 sizeof (type_string)) > 0)
132 result = (hp_timing_t)
133 strtoumax (type_string, NULL, 16);
134 __close (clkfreq_fd);
137 __close (fd);
140 if (result != 0)
141 break;
143 len -= this_dirp->d_reclen;
144 this_dirp = (struct dirent *)
145 ((char *) this_dirp + this_dirp->d_reclen);
147 if (result != 0)
148 break;
150 __close (obp_fd);
153 return result;
156 static void set_obp_int (struct openpromio *op, int val)
158 char *cp = op->oprom_array;
159 int *ip = (int *) cp;
161 *ip = val;
164 static int get_obp_int (struct openpromio *op)
166 char *cp = op->oprom_array;
167 int *ip = (int *) cp;
169 return *ip;
172 static hp_timing_t
173 __get_clockfreq_via_dev_openprom (void)
175 hp_timing_t result;
176 int obp_dev_fd;
178 result = 0;
180 obp_dev_fd = __open ("/dev/openprom", O_RDONLY);
181 if (obp_dev_fd != -1)
183 char obp_buf[8192];
184 struct openpromio *obp_cmd = (struct openpromio *)obp_buf;
185 int ret;
187 obp_cmd->oprom_size =
188 sizeof (obp_buf) - sizeof (unsigned int);
189 set_obp_int (obp_cmd, 0);
190 ret = __ioctl (obp_dev_fd, OPROMCHILD, (char *) obp_cmd);
191 if (ret == 0)
193 int cur_node = get_obp_int (obp_cmd);
195 while (cur_node != 0 && cur_node != -1)
197 obp_cmd->oprom_size = sizeof (obp_buf) - sizeof (unsigned int);
198 strcpy (obp_cmd->oprom_array, "device_type");
199 ret = __ioctl (obp_dev_fd, OPROMGETPROP, (char *) obp_cmd);
200 if (ret == 0
201 && strncmp (obp_cmd->oprom_array, "cpu", 3) == 0)
203 obp_cmd->oprom_size = (sizeof (obp_buf)
204 - sizeof (unsigned int));
205 strcpy (obp_cmd->oprom_array, "clock-frequency");
206 ret = __ioctl (obp_dev_fd, OPROMGETPROP, (char *) obp_cmd);
207 if (ret == 0)
208 result = (hp_timing_t) get_obp_int (obp_cmd);
210 obp_cmd->oprom_size = sizeof (obp_buf) - sizeof (unsigned int);
211 set_obp_int (obp_cmd, cur_node);
212 ret = __ioctl (obp_dev_fd, OPROMNEXT, (char *) obp_cmd);
213 if (ret < 0)
214 break;
215 cur_node = get_obp_int (obp_cmd);
220 return result;
223 hp_timing_t
224 __get_clockfreq (void)
226 static hp_timing_t result;
228 /* If this function was called before, we know the result. */
229 if (result != 0)
230 return result;
232 /* We first read the information from the /proc/cpuinfo file.
233 It contains at least one line like
234 Cpu0ClkTick : 000000002cb41780
235 We search for this line and convert the number in an integer. */
236 result = __get_clockfreq_via_cpuinfo ();
237 if (result != 0)
238 return result;
240 /* If that did not work, try to find an OpenPROM node
241 with device_type equal to 'cpu' using /dev/openprom
242 and fetch the clock-frequency property from there. */
243 result = __get_clockfreq_via_dev_openprom ();
244 if (result != 0)
245 return result;
247 /* Finally, try the same lookup as above but using /proc/openprom. */
248 result = __get_clockfreq_via_proc_openprom ();
250 return result;