blob: 99b7d405767cf6c55fb18c7253e5b146867549db [file] [log] [blame]
Will Deacon9031fef2012-03-05 11:49:31 +00001/*
2 * Userspace implementations of gettimeofday() and friends.
3 *
4 * Copyright (C) 2012 ARM Limited
5 *
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.
9 *
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.
14 *
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/>.
17 *
18 * Author: Will Deacon <will.deacon@arm.com>
19 */
20
21#include <linux/linkage.h>
22#include <asm/asm-offsets.h>
23#include <asm/unistd.h>
24
25#define NSEC_PER_SEC_LO16 0xca00
26#define NSEC_PER_SEC_HI16 0x3b9a
27
28vdso_data .req x6
29use_syscall .req w7
30seqcnt .req w8
31
32 .macro seqcnt_acquire
339999: ldr seqcnt, [vdso_data, #VDSO_TB_SEQ_COUNT]
34 tbnz seqcnt, #0, 9999b
35 dmb ishld
36 ldr use_syscall, [vdso_data, #VDSO_USE_SYSCALL]
37 .endm
38
39 .macro seqcnt_read, cnt
40 dmb ishld
41 ldr \cnt, [vdso_data, #VDSO_TB_SEQ_COUNT]
42 .endm
43
44 .macro seqcnt_check, cnt, fail
45 cmp \cnt, seqcnt
46 b.ne \fail
47 .endm
48
49 .text
50
51/* int __kernel_gettimeofday(struct timeval *tv, struct timezone *tz); */
52ENTRY(__kernel_gettimeofday)
53 .cfi_startproc
54 mov x2, x30
55 .cfi_register x30, x2
56
57 /* Acquire the sequence counter and get the timespec. */
58 adr vdso_data, _vdso_data
591: seqcnt_acquire
60 cbnz use_syscall, 4f
61
62 /* If tv is NULL, skip to the timezone code. */
63 cbz x0, 2f
64 bl __do_get_tspec
Will Deacond91fb5c2012-11-29 22:19:01 +000065 seqcnt_check w9, 1b
Will Deacon9031fef2012-03-05 11:49:31 +000066
67 /* Convert ns to us. */
Will Deacond91fb5c2012-11-29 22:19:01 +000068 mov x13, #1000
69 udiv x11, x11, x13
70 stp x10, x11, [x0, #TVAL_TV_SEC]
Will Deacon9031fef2012-03-05 11:49:31 +0000712:
72 /* If tz is NULL, return 0. */
73 cbz x1, 3f
74 ldp w4, w5, [vdso_data, #VDSO_TZ_MINWEST]
Will Deacond91fb5c2012-11-29 22:19:01 +000075 seqcnt_read w9
76 seqcnt_check w9, 1b
Will Deacon9031fef2012-03-05 11:49:31 +000077 stp w4, w5, [x1, #TZ_MINWEST]
783:
79 mov x0, xzr
80 ret x2
814:
82 /* Syscall fallback. */
83 mov x8, #__NR_gettimeofday
84 svc #0
85 ret x2
86 .cfi_endproc
87ENDPROC(__kernel_gettimeofday)
88
89/* int __kernel_clock_gettime(clockid_t clock_id, struct timespec *tp); */
90ENTRY(__kernel_clock_gettime)
91 .cfi_startproc
92 cmp w0, #CLOCK_REALTIME
93 ccmp w0, #CLOCK_MONOTONIC, #0x4, ne
94 b.ne 2f
95
96 mov x2, x30
97 .cfi_register x30, x2
98
99 /* Get kernel timespec. */
100 adr vdso_data, _vdso_data
1011: seqcnt_acquire
102 cbnz use_syscall, 7f
103
104 bl __do_get_tspec
Will Deacond91fb5c2012-11-29 22:19:01 +0000105 seqcnt_check w9, 1b
Will Deacon9031fef2012-03-05 11:49:31 +0000106
107 cmp w0, #CLOCK_MONOTONIC
108 b.ne 6f
109
110 /* Get wtm timespec. */
Will Deacond91fb5c2012-11-29 22:19:01 +0000111 ldp x13, x14, [vdso_data, #VDSO_WTM_CLK_SEC]
Will Deacon9031fef2012-03-05 11:49:31 +0000112
113 /* Check the sequence counter. */
Will Deacond91fb5c2012-11-29 22:19:01 +0000114 seqcnt_read w9
115 seqcnt_check w9, 1b
Will Deacon9031fef2012-03-05 11:49:31 +0000116 b 4f
1172:
118 cmp w0, #CLOCK_REALTIME_COARSE
119 ccmp w0, #CLOCK_MONOTONIC_COARSE, #0x4, ne
120 b.ne 8f
121
122 /* Get coarse timespec. */
123 adr vdso_data, _vdso_data
1243: seqcnt_acquire
Will Deacond91fb5c2012-11-29 22:19:01 +0000125 ldp x10, x11, [vdso_data, #VDSO_XTIME_CRS_SEC]
Will Deacon9031fef2012-03-05 11:49:31 +0000126
Will Deacon9031fef2012-03-05 11:49:31 +0000127 /* Get wtm timespec. */
Will Deacond91fb5c2012-11-29 22:19:01 +0000128 ldp x13, x14, [vdso_data, #VDSO_WTM_CLK_SEC]
Will Deacon9031fef2012-03-05 11:49:31 +0000129
130 /* Check the sequence counter. */
Will Deacond91fb5c2012-11-29 22:19:01 +0000131 seqcnt_read w9
132 seqcnt_check w9, 3b
Will Deaconf84a9352012-11-29 22:11:51 +0000133
134 cmp w0, #CLOCK_MONOTONIC_COARSE
135 b.ne 6f
Will Deacon9031fef2012-03-05 11:49:31 +00001364:
137 /* Add on wtm timespec. */
Will Deacond91fb5c2012-11-29 22:19:01 +0000138 add x10, x10, x13
139 add x11, x11, x14
Will Deacon9031fef2012-03-05 11:49:31 +0000140
141 /* Normalise the new timespec. */
Will Deacond91fb5c2012-11-29 22:19:01 +0000142 mov x15, #NSEC_PER_SEC_LO16
143 movk x15, #NSEC_PER_SEC_HI16, lsl #16
144 cmp x11, x15
Will Deacon9031fef2012-03-05 11:49:31 +0000145 b.lt 5f
Will Deacond91fb5c2012-11-29 22:19:01 +0000146 sub x11, x11, x15
147 add x10, x10, #1
Will Deacon9031fef2012-03-05 11:49:31 +00001485:
Will Deacond91fb5c2012-11-29 22:19:01 +0000149 cmp x11, #0
Will Deacon9031fef2012-03-05 11:49:31 +0000150 b.ge 6f
Will Deacond91fb5c2012-11-29 22:19:01 +0000151 add x11, x11, x15
152 sub x10, x10, #1
Will Deacon9031fef2012-03-05 11:49:31 +0000153
1546: /* Store to the user timespec. */
Will Deacond91fb5c2012-11-29 22:19:01 +0000155 stp x10, x11, [x1, #TSPEC_TV_SEC]
Will Deacon9031fef2012-03-05 11:49:31 +0000156 mov x0, xzr
157 ret x2
1587:
159 mov x30, x2
1608: /* Syscall fallback. */
161 mov x8, #__NR_clock_gettime
162 svc #0
163 ret
164 .cfi_endproc
165ENDPROC(__kernel_clock_gettime)
166
167/* int __kernel_clock_getres(clockid_t clock_id, struct timespec *res); */
168ENTRY(__kernel_clock_getres)
169 .cfi_startproc
170 cbz w1, 3f
171
172 cmp w0, #CLOCK_REALTIME
173 ccmp w0, #CLOCK_MONOTONIC, #0x4, ne
174 b.ne 1f
175
176 ldr x2, 5f
177 b 2f
1781:
179 cmp w0, #CLOCK_REALTIME_COARSE
180 ccmp w0, #CLOCK_MONOTONIC_COARSE, #0x4, ne
181 b.ne 4f
182 ldr x2, 6f
1832:
184 stp xzr, x2, [x1]
185
1863: /* res == NULL. */
187 mov w0, wzr
188 ret
189
1904: /* Syscall fallback. */
191 mov x8, #__NR_clock_getres
192 svc #0
193 ret
1945:
195 .quad CLOCK_REALTIME_RES
1966:
197 .quad CLOCK_COARSE_RES
198 .cfi_endproc
199ENDPROC(__kernel_clock_getres)
200
201/*
202 * Read the current time from the architected counter.
203 * Expects vdso_data to be initialised.
204 * Clobbers the temporary registers (x9 - x15).
205 * Returns:
Will Deacond91fb5c2012-11-29 22:19:01 +0000206 * - w9 = vDSO sequence counter
207 * - (x10, x11) = (ts->tv_sec, ts->tv_nsec)
208 * - w12 = cs_shift
Will Deacon9031fef2012-03-05 11:49:31 +0000209 */
210ENTRY(__do_get_tspec)
211 .cfi_startproc
212
213 /* Read from the vDSO data page. */
214 ldr x10, [vdso_data, #VDSO_CS_CYCLE_LAST]
Will Deacond91fb5c2012-11-29 22:19:01 +0000215 ldp x13, x14, [vdso_data, #VDSO_XTIME_CLK_SEC]
216 ldp w11, w12, [vdso_data, #VDSO_CS_MULT]
217 seqcnt_read w9
Will Deacon9031fef2012-03-05 11:49:31 +0000218
219 /* Read the physical counter. */
220 isb
Will Deacond91fb5c2012-11-29 22:19:01 +0000221 mrs x15, cntpct_el0
Will Deacon9031fef2012-03-05 11:49:31 +0000222
223 /* Calculate cycle delta and convert to ns. */
Will Deacond91fb5c2012-11-29 22:19:01 +0000224 sub x10, x15, x10
Will Deacon9031fef2012-03-05 11:49:31 +0000225 /* We can only guarantee 56 bits of precision. */
Will Deacond91fb5c2012-11-29 22:19:01 +0000226 movn x15, #0xff00, lsl #48
227 and x10, x15, x10
228 mul x10, x10, x11
229 lsr x10, x10, x12
Will Deacon9031fef2012-03-05 11:49:31 +0000230
231 /* Use the kernel time to calculate the new timespec. */
Will Deacond91fb5c2012-11-29 22:19:01 +0000232 mov x11, #NSEC_PER_SEC_LO16
233 movk x11, #NSEC_PER_SEC_HI16, lsl #16
234 add x15, x10, x14
235 udiv x14, x15, x11
236 add x10, x13, x14
237 mul x13, x14, x11
238 sub x11, x15, x13
Will Deacon9031fef2012-03-05 11:49:31 +0000239
240 ret
241 .cfi_endproc
242ENDPROC(__do_get_tspec)