2 * Userspace implementations of gettimeofday() and friends.
4 * Copyright (C) 2012 ARM Limited
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program 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
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * Author: Will Deacon <will.deacon@arm.com>
21 #include <linux/linkage.h>
22 #include <asm/asm-offsets.h>
23 #include <asm/unistd.h>
25 #define NSEC_PER_SEC_LO16 0xca00
26 #define NSEC_PER_SEC_HI16 0x3b9a
33 9999: ldr seqcnt, [vdso_data, #VDSO_TB_SEQ_COUNT]
34 tbnz seqcnt, #0, 9999b
36 ldr use_syscall, [vdso_data, #VDSO_USE_SYSCALL]
39 .macro seqcnt_read, cnt
41 ldr \cnt, [vdso_data, #VDSO_TB_SEQ_COUNT]
44 .macro seqcnt_check, cnt, fail
51 /* int __kernel_gettimeofday(struct timeval *tv, struct timezone *tz); */
52 ENTRY(__kernel_gettimeofday)
57 /* Acquire the sequence counter and get the timespec. */
58 adr vdso_data, _vdso_data
62 /* If tv is NULL, skip to the timezone code. */
67 /* Convert ns to us. */
70 stp x9, x10, [x0, #TVAL_TV_SEC]
72 /* If tz is NULL, return 0. */
74 ldp w4, w5, [vdso_data, #VDSO_TZ_MINWEST]
77 stp w4, w5, [x1, #TZ_MINWEST]
82 /* Syscall fallback. */
83 mov x8, #__NR_gettimeofday
87 ENDPROC(__kernel_gettimeofday)
89 /* int __kernel_clock_gettime(clockid_t clock_id, struct timespec *tp); */
90 ENTRY(__kernel_clock_gettime)
92 cmp w0, #CLOCK_REALTIME
93 ccmp w0, #CLOCK_MONOTONIC, #0x4, ne
99 /* Get kernel timespec. */
100 adr vdso_data, _vdso_data
107 cmp w0, #CLOCK_MONOTONIC
110 /* Get wtm timespec. */
111 ldp x14, x15, [vdso_data, #VDSO_WTM_CLK_SEC]
113 /* Check the sequence counter. */
118 cmp w0, #CLOCK_REALTIME_COARSE
119 ccmp w0, #CLOCK_MONOTONIC_COARSE, #0x4, ne
122 /* Get coarse timespec. */
123 adr vdso_data, _vdso_data
125 ldp x9, x10, [vdso_data, #VDSO_XTIME_CRS_SEC]
127 cmp w0, #CLOCK_MONOTONIC_COARSE
130 /* Get wtm timespec. */
131 ldp x14, x15, [vdso_data, #VDSO_WTM_CLK_SEC]
133 /* Check the sequence counter. */
137 /* Add on wtm timespec. */
141 /* Normalise the new timespec. */
142 mov x14, #NSEC_PER_SEC_LO16
143 movk x14, #NSEC_PER_SEC_HI16, lsl #16
154 6: /* Store to the user timespec. */
155 stp x9, x10, [x1, #TSPEC_TV_SEC]
160 8: /* Syscall fallback. */
161 mov x8, #__NR_clock_gettime
165 ENDPROC(__kernel_clock_gettime)
167 /* int __kernel_clock_getres(clockid_t clock_id, struct timespec *res); */
168 ENTRY(__kernel_clock_getres)
172 cmp w0, #CLOCK_REALTIME
173 ccmp w0, #CLOCK_MONOTONIC, #0x4, ne
179 cmp w0, #CLOCK_REALTIME_COARSE
180 ccmp w0, #CLOCK_MONOTONIC_COARSE, #0x4, ne
186 3: /* res == NULL. */
190 4: /* Syscall fallback. */
191 mov x8, #__NR_clock_getres
195 .quad CLOCK_REALTIME_RES
197 .quad CLOCK_COARSE_RES
199 ENDPROC(__kernel_clock_getres)
202 * Read the current time from the architected counter.
203 * Expects vdso_data to be initialised.
204 * Clobbers the temporary registers (x9 - x15).
206 * - (x9, x10) = (ts->tv_sec, ts->tv_nsec)
207 * - (x11, x12) = (xtime->tv_sec, xtime->tv_nsec)
208 * - w13 = vDSO sequence counter
210 ENTRY(__do_get_tspec)
213 /* Read from the vDSO data page. */
214 ldr x10, [vdso_data, #VDSO_CS_CYCLE_LAST]
215 ldp x11, x12, [vdso_data, #VDSO_XTIME_CLK_SEC]
216 ldp w14, w15, [vdso_data, #VDSO_CS_MULT]
219 /* Read the physical counter. */
223 /* Calculate cycle delta and convert to ns. */
225 /* We can only guarantee 56 bits of precision. */
226 movn x9, #0xff0, lsl #48
231 /* Use the kernel time to calculate the new timespec. */
233 mov x14, #NSEC_PER_SEC_LO16
234 movk x14, #NSEC_PER_SEC_HI16, lsl #16
242 ENDPROC(__do_get_tspec)