blob: f0a6d10b52114953dcfd818c66ad85f6cccccbd8 [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
Will Deacon45a79052012-11-29 22:33:29 +000069 lsl x13, x13, x12
Will Deacond91fb5c2012-11-29 22:19:01 +000070 udiv x11, x11, x13
71 stp x10, x11, [x0, #TVAL_TV_SEC]
Will Deacon9031fef2012-03-05 11:49:31 +0000722:
73 /* If tz is NULL, return 0. */
74 cbz x1, 3f
75 ldp w4, w5, [vdso_data, #VDSO_TZ_MINWEST]
Will Deacon9031fef2012-03-05 11:49:31 +000076 stp w4, w5, [x1, #TZ_MINWEST]
773:
78 mov x0, xzr
79 ret x2
804:
81 /* Syscall fallback. */
82 mov x8, #__NR_gettimeofday
83 svc #0
84 ret x2
85 .cfi_endproc
86ENDPROC(__kernel_gettimeofday)
87
88/* int __kernel_clock_gettime(clockid_t clock_id, struct timespec *tp); */
89ENTRY(__kernel_clock_gettime)
90 .cfi_startproc
91 cmp w0, #CLOCK_REALTIME
92 ccmp w0, #CLOCK_MONOTONIC, #0x4, ne
93 b.ne 2f
94
95 mov x2, x30
96 .cfi_register x30, x2
97
98 /* Get kernel timespec. */
99 adr vdso_data, _vdso_data
1001: seqcnt_acquire
101 cbnz use_syscall, 7f
102
103 bl __do_get_tspec
Will Deacond91fb5c2012-11-29 22:19:01 +0000104 seqcnt_check w9, 1b
Will Deacon9031fef2012-03-05 11:49:31 +0000105
106 cmp w0, #CLOCK_MONOTONIC
107 b.ne 6f
108
109 /* Get wtm timespec. */
Will Deacond91fb5c2012-11-29 22:19:01 +0000110 ldp x13, x14, [vdso_data, #VDSO_WTM_CLK_SEC]
Will Deacon9031fef2012-03-05 11:49:31 +0000111
112 /* Check the sequence counter. */
Will Deacond91fb5c2012-11-29 22:19:01 +0000113 seqcnt_read w9
114 seqcnt_check w9, 1b
Will Deacon9031fef2012-03-05 11:49:31 +0000115 b 4f
1162:
117 cmp w0, #CLOCK_REALTIME_COARSE
118 ccmp w0, #CLOCK_MONOTONIC_COARSE, #0x4, ne
119 b.ne 8f
120
121 /* Get coarse timespec. */
122 adr vdso_data, _vdso_data
1233: seqcnt_acquire
Will Deacond91fb5c2012-11-29 22:19:01 +0000124 ldp x10, x11, [vdso_data, #VDSO_XTIME_CRS_SEC]
Will Deacon9031fef2012-03-05 11:49:31 +0000125
Will Deacon9031fef2012-03-05 11:49:31 +0000126 /* Get wtm timespec. */
Will Deacond91fb5c2012-11-29 22:19:01 +0000127 ldp x13, x14, [vdso_data, #VDSO_WTM_CLK_SEC]
Will Deacon9031fef2012-03-05 11:49:31 +0000128
129 /* Check the sequence counter. */
Will Deacond91fb5c2012-11-29 22:19:01 +0000130 seqcnt_read w9
131 seqcnt_check w9, 3b
Will Deaconf84a9352012-11-29 22:11:51 +0000132
133 cmp w0, #CLOCK_MONOTONIC_COARSE
134 b.ne 6f
Will Deacon9031fef2012-03-05 11:49:31 +00001354:
136 /* Add on wtm timespec. */
Will Deacond91fb5c2012-11-29 22:19:01 +0000137 add x10, x10, x13
Will Deacon45a79052012-11-29 22:33:29 +0000138 lsl x14, x14, x12
Will Deacond91fb5c2012-11-29 22:19:01 +0000139 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
Will Deacon45a79052012-11-29 22:33:29 +0000144 lsl x15, x15, x12
Will Deacond91fb5c2012-11-29 22:19:01 +0000145 cmp x11, x15
Will Deacon9031fef2012-03-05 11:49:31 +0000146 b.lt 5f
Will Deacond91fb5c2012-11-29 22:19:01 +0000147 sub x11, x11, x15
148 add x10, x10, #1
Will Deacon9031fef2012-03-05 11:49:31 +00001495:
Will Deacond91fb5c2012-11-29 22:19:01 +0000150 cmp x11, #0
Will Deacon9031fef2012-03-05 11:49:31 +0000151 b.ge 6f
Will Deacond91fb5c2012-11-29 22:19:01 +0000152 add x11, x11, x15
153 sub x10, x10, #1
Will Deacon9031fef2012-03-05 11:49:31 +0000154
1556: /* Store to the user timespec. */
Will Deacon45a79052012-11-29 22:33:29 +0000156 lsr x11, x11, x12
Will Deacond91fb5c2012-11-29 22:19:01 +0000157 stp x10, x11, [x1, #TSPEC_TV_SEC]
Will Deacon9031fef2012-03-05 11:49:31 +0000158 mov x0, xzr
159 ret x2
1607:
161 mov x30, x2
1628: /* Syscall fallback. */
163 mov x8, #__NR_clock_gettime
164 svc #0
165 ret
166 .cfi_endproc
167ENDPROC(__kernel_clock_gettime)
168
169/* int __kernel_clock_getres(clockid_t clock_id, struct timespec *res); */
170ENTRY(__kernel_clock_getres)
171 .cfi_startproc
172 cbz w1, 3f
173
174 cmp w0, #CLOCK_REALTIME
175 ccmp w0, #CLOCK_MONOTONIC, #0x4, ne
176 b.ne 1f
177
178 ldr x2, 5f
179 b 2f
1801:
181 cmp w0, #CLOCK_REALTIME_COARSE
182 ccmp w0, #CLOCK_MONOTONIC_COARSE, #0x4, ne
183 b.ne 4f
184 ldr x2, 6f
1852:
186 stp xzr, x2, [x1]
187
1883: /* res == NULL. */
189 mov w0, wzr
190 ret
191
1924: /* Syscall fallback. */
193 mov x8, #__NR_clock_getres
194 svc #0
195 ret
1965:
197 .quad CLOCK_REALTIME_RES
1986:
199 .quad CLOCK_COARSE_RES
200 .cfi_endproc
201ENDPROC(__kernel_clock_getres)
202
203/*
204 * Read the current time from the architected counter.
205 * Expects vdso_data to be initialised.
206 * Clobbers the temporary registers (x9 - x15).
207 * Returns:
Will Deacond91fb5c2012-11-29 22:19:01 +0000208 * - w9 = vDSO sequence counter
Will Deacon45a79052012-11-29 22:33:29 +0000209 * - (x10, x11) = (ts->tv_sec, shifted ts->tv_nsec)
Will Deacond91fb5c2012-11-29 22:19:01 +0000210 * - w12 = cs_shift
Will Deacon9031fef2012-03-05 11:49:31 +0000211 */
212ENTRY(__do_get_tspec)
213 .cfi_startproc
214
215 /* Read from the vDSO data page. */
216 ldr x10, [vdso_data, #VDSO_CS_CYCLE_LAST]
Will Deacond91fb5c2012-11-29 22:19:01 +0000217 ldp x13, x14, [vdso_data, #VDSO_XTIME_CLK_SEC]
218 ldp w11, w12, [vdso_data, #VDSO_CS_MULT]
219 seqcnt_read w9
Will Deacon9031fef2012-03-05 11:49:31 +0000220
Will Deacon1f75ff02012-11-29 22:48:31 +0000221 /* Read the virtual counter. */
Will Deacon9031fef2012-03-05 11:49:31 +0000222 isb
Will Deacon1f75ff02012-11-29 22:48:31 +0000223 mrs x15, cntvct_el0
Will Deacon9031fef2012-03-05 11:49:31 +0000224
225 /* Calculate cycle delta and convert to ns. */
Will Deacond91fb5c2012-11-29 22:19:01 +0000226 sub x10, x15, x10
Will Deacon9031fef2012-03-05 11:49:31 +0000227 /* We can only guarantee 56 bits of precision. */
Will Deacond91fb5c2012-11-29 22:19:01 +0000228 movn x15, #0xff00, lsl #48
229 and x10, x15, x10
230 mul x10, x10, x11
Will Deacon9031fef2012-03-05 11:49:31 +0000231
232 /* Use the kernel time to calculate the new timespec. */
Will Deacond91fb5c2012-11-29 22:19:01 +0000233 mov x11, #NSEC_PER_SEC_LO16
234 movk x11, #NSEC_PER_SEC_HI16, lsl #16
Will Deacon45a79052012-11-29 22:33:29 +0000235 lsl x11, x11, x12
Will Deacond91fb5c2012-11-29 22:19:01 +0000236 add x15, x10, x14
237 udiv x14, x15, x11
238 add x10, x13, x14
239 mul x13, x14, x11
240 sub x11, x15, x13
Will Deacon9031fef2012-03-05 11:49:31 +0000241
242 ret
243 .cfi_endproc
244ENDPROC(__do_get_tspec)