blob: 780d6e875b9851761b644d0cb47cab9cd5f07292 [file] [log] [blame]
Greg Reide5bf5b22012-10-22 16:05:45 -04001/* Copyright (c) 2012, The Linux Foundation. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
12 * * Neither the name of The Linux Foundation nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30#include <string.h>
31#include <stdint.h>
32#include <sys/time.h>
33#include <machine/cpu-features.h>
34#include <machine/kernel_user_helper.h>
35
36int gettimeofday(struct timeval *tv, struct timezone *tz)
37{
38 unsigned prelock, postlock;
39
40 /*
41 * Check if the offset in the kernel user helper page has
42 * the flag set appropriately to show that this feature is
43 * enabled in the kernel. If not, default to the original
44 * gettimeofday system call.
45 */
46 if (__kuser_gtod_feature != __kuser_gtod_feature_flag)
47 return gettimeofday_syscall(tv, tz);
48
49 if (tv) {
50 struct gtod_t dgtod;
51 uint32_t nscount, cycleoffset;
52 uint64_t cycle_delta;
53 uint32_t tmp = 0;
54
55 do {
56 prelock = __kuser_gtod_seqnum;
57
58 dgtod.cycle_last = __kuser_gtod_cycle_last;
59 dgtod.mask = __kuser_gtod_mask;
60 dgtod.mult = __kuser_gtod_mult;
61 dgtod.shift = __kuser_gtod_shift;
62 dgtod.tv_sec = __kuser_gtod_tv_sec;
63 dgtod.tv_nsec = __kuser_gtod_tv_nsec;
64
65 cycleoffset = __kuser_gtod_offset;
66 cycleoffset += __kuser_gtod_cycle_base;
67 nscount = *(uint32_t *)cycleoffset;
68
69 postlock = __kuser_gtod_seqnum;
70 } while (prelock != postlock);
71
72 cycle_delta = (nscount - dgtod.cycle_last) & dgtod.mask;
73 dgtod.tv_nsec += (cycle_delta * dgtod.mult) >> dgtod.shift;
74 while (dgtod.tv_nsec >= NSEC_PER_SEC) {
75 dgtod.tv_sec += 1;
76 dgtod.tv_nsec -= NSEC_PER_SEC;
77 }
78
79 tv->tv_sec = dgtod.tv_sec;
80 asm(" movw %[tmp], #0x4dd3\n\t"
81 " movt %[tmp], #0x1062\n\t"
82 " umull %[tmp], %[x], %[y], %[tmp]\n\t"
83 " lsr %[x], %[x], #6\n\t" :
84 [x] "=r" (tv->tv_usec) :
85 [y] "r" (dgtod.tv_nsec), [tmp] "r" (tmp)
86 : );
87 }
88
89 if (tz) {
90 do {
91 prelock = __kuser_gtod_seqnum;
92 tz->tz_minuteswest = __kuser_gtod_tz_minw;
93 tz->tz_dsttime = __kuser_gtod_tz_dst;
94 postlock = __kuser_gtod_seqnum;
95 } while (prelock != postlock);
96 }
97
98 return 0;
99}