]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/sparc/sparc64/get_clockfreq.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / sparc / sparc64 / get_clockfreq.c
1 /* Get frequency of the system processor. sparc64 version.
2 Copyright (C) 2001-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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/>. */
18
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 <asm/openpromio.h>
28
29 static hp_timing_t
30 __get_clockfreq_via_cpuinfo (void)
31 {
32 hp_timing_t result;
33 int fd;
34
35 result = 0;
36
37 fd = __open ("/proc/cpuinfo", O_RDONLY);
38 if (fd != -1)
39 {
40 char buf[8192];
41 ssize_t n;
42
43 n = __read (fd, buf, sizeof buf);
44 if (n > 0)
45 {
46 char *mhz = memmem (buf, n, "Cpu0ClkTck", 7);
47
48 if (mhz != NULL)
49 {
50 char *endp = buf + n;
51
52 /* Search for the beginning of the string. */
53 while (mhz < endp
54 && (*mhz < '0' || *mhz > '9')
55 && (*mhz < 'a' || *mhz > 'f')
56 && *mhz != '\n')
57 ++mhz;
58
59 while (mhz < endp && *mhz != '\n')
60 {
61 if ((*mhz >= '0' && *mhz <= '9') ||
62 (*mhz >= 'a' && *mhz <= 'f'))
63 {
64 result <<= 4;
65 if (*mhz >= '0' && *mhz <= '9')
66 result += *mhz - '0';
67 else
68 result += (*mhz - 'a') + 10;
69 }
70 ++mhz;
71 }
72 }
73 }
74
75 __close (fd);
76 }
77
78 return result;
79 }
80
81 static hp_timing_t
82 __get_clockfreq_via_proc_openprom (void)
83 {
84 hp_timing_t result;
85 int obp_fd;
86
87 result = 0;
88
89 obp_fd = __open ("/proc/openprom", O_RDONLY);
90 if (obp_fd != -1)
91 {
92 unsigned long int buf[4096 / sizeof (unsigned long int)];
93 struct dirent *dirp = (struct dirent *) buf;
94 ssize_t len;
95
96 while ((len = __getdents (obp_fd, (char *) dirp, sizeof (buf))) > 0)
97 {
98 struct dirent *this_dirp = dirp;
99
100 while (len > 0)
101 {
102 char node[strlen ("/proc/openprom/")
103 + _D_ALLOC_NAMLEN (this_dirp)
104 + strlen ("/clock-frequency")];
105 char *prop;
106 int fd;
107
108 /* Note that
109 strlen("/clock-frequency") > strlen("/device_type")
110 */
111 __stpcpy (prop = __stpcpy (__stpcpy (node, "/proc/openprom/"),
112 this_dirp->d_name),
113 "/device_type");
114 fd = __open (node, O_RDONLY);
115 if (fd != -1)
116 {
117 char type_string[128];
118 int ret;
119
120 ret = __read (fd, type_string, sizeof (type_string));
121 if (ret > 0 && strncmp (type_string, "'cpu'", 5) == 0)
122 {
123 int clkfreq_fd;
124
125 __stpcpy (prop, "/clock-frequency");
126 clkfreq_fd = __open (node, O_RDONLY);
127 if (clkfreq_fd != -1)
128 {
129 if (__read (clkfreq_fd, type_string,
130 sizeof (type_string)) > 0)
131 result = (hp_timing_t)
132 strtoumax (type_string, NULL, 16);
133 __close (clkfreq_fd);
134 }
135 }
136 __close (fd);
137 }
138
139 if (result != 0)
140 break;
141
142 len -= this_dirp->d_reclen;
143 this_dirp = (struct dirent *)
144 ((char *) this_dirp + this_dirp->d_reclen);
145 }
146 if (result != 0)
147 break;
148 }
149 __close (obp_fd);
150 }
151
152 return result;
153 }
154
155 static void set_obp_int (struct openpromio *op, int val)
156 {
157 char *cp = op->oprom_array;
158 int *ip = (int *) cp;
159
160 *ip = val;
161 }
162
163 static int get_obp_int (struct openpromio *op)
164 {
165 char *cp = op->oprom_array;
166 int *ip = (int *) cp;
167
168 return *ip;
169 }
170
171 static hp_timing_t
172 __get_clockfreq_via_dev_openprom (void)
173 {
174 hp_timing_t result;
175 int obp_dev_fd;
176
177 result = 0;
178
179 obp_dev_fd = __open ("/dev/openprom", O_RDONLY);
180 if (obp_dev_fd != -1)
181 {
182 char obp_buf[8192];
183 struct openpromio *obp_cmd = (struct openpromio *)obp_buf;
184 int ret;
185
186 obp_cmd->oprom_size =
187 sizeof (obp_buf) - sizeof (unsigned int);
188 set_obp_int (obp_cmd, 0);
189 ret = __ioctl (obp_dev_fd, OPROMCHILD, (char *) obp_cmd);
190 if (ret == 0)
191 {
192 int cur_node = get_obp_int (obp_cmd);
193
194 while (cur_node != 0 && cur_node != -1)
195 {
196 obp_cmd->oprom_size = sizeof (obp_buf) - sizeof (unsigned int);
197 strcpy (obp_cmd->oprom_array, "device_type");
198 ret = __ioctl (obp_dev_fd, OPROMGETPROP, (char *) obp_cmd);
199 if (ret == 0
200 && strncmp (obp_cmd->oprom_array, "cpu", 3) == 0)
201 {
202 obp_cmd->oprom_size = (sizeof (obp_buf)
203 - sizeof (unsigned int));
204 strcpy (obp_cmd->oprom_array, "clock-frequency");
205 ret = __ioctl (obp_dev_fd, OPROMGETPROP, (char *) obp_cmd);
206 if (ret == 0)
207 result = (hp_timing_t) get_obp_int (obp_cmd);
208 }
209 obp_cmd->oprom_size = sizeof (obp_buf) - sizeof (unsigned int);
210 set_obp_int (obp_cmd, cur_node);
211 ret = __ioctl (obp_dev_fd, OPROMNEXT, (char *) obp_cmd);
212 if (ret < 0)
213 break;
214 cur_node = get_obp_int (obp_cmd);
215 }
216 }
217 }
218
219 return result;
220 }
221
222 hp_timing_t
223 __get_clockfreq (void)
224 {
225 static hp_timing_t result;
226
227 /* If this function was called before, we know the result. */
228 if (result != 0)
229 return result;
230
231 /* We first read the information from the /proc/cpuinfo file.
232 It contains at least one line like
233 Cpu0ClkTick : 000000002cb41780
234 We search for this line and convert the number in an integer. */
235 result = __get_clockfreq_via_cpuinfo ();
236 if (result != 0)
237 return result;
238
239 /* If that did not work, try to find an OpenPROM node
240 with device_type equal to 'cpu' using /dev/openprom
241 and fetch the clock-frequency property from there. */
242 result = __get_clockfreq_via_dev_openprom ();
243 if (result != 0)
244 return result;
245
246 /* Finally, try the same lookup as above but using /proc/openprom. */
247 result = __get_clockfreq_via_proc_openprom ();
248
249 return result;
250 }