GNU Linux-libre 4.19.264-gnu1
[releases.git] / arch / powerpc / kernel / vdso64 / gettimeofday.S
1 /*
2  * Userland implementation of gettimeofday() for 64 bits processes in a
3  * ppc64 kernel for use in the vDSO
4  *
5  * Copyright (C) 2004 Benjamin Herrenschmuidt (benh@kernel.crashing.org),
6  *                    IBM Corp.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version
11  * 2 of the License, or (at your option) any later version.
12  */
13 #include <asm/processor.h>
14 #include <asm/ppc_asm.h>
15 #include <asm/vdso.h>
16 #include <asm/asm-offsets.h>
17 #include <asm/unistd.h>
18
19         .text
20 /*
21  * Exact prototype of gettimeofday
22  *
23  * int __kernel_gettimeofday(struct timeval *tv, struct timezone *tz);
24  *
25  */
26 V_FUNCTION_BEGIN(__kernel_gettimeofday)
27   .cfi_startproc
28         mflr    r12
29   .cfi_register lr,r12
30
31         mr      r11,r3                  /* r11 holds tv */
32         mr      r10,r4                  /* r10 holds tz */
33         bl      V_LOCAL_FUNC(__get_datapage)    /* get data page */
34         cmpldi  r11,0                   /* check if tv is NULL */
35         beq     2f
36         lis     r7,1000000@ha           /* load up USEC_PER_SEC */
37         addi    r7,r7,1000000@l
38         bl      V_LOCAL_FUNC(__do_get_tspec) /* get sec/us from tb & kernel */
39         std     r4,TVAL64_TV_SEC(r11)   /* store sec in tv */
40         std     r5,TVAL64_TV_USEC(r11)  /* store usec in tv */
41 2:      cmpldi  r10,0                   /* check if tz is NULL */
42         beq     1f
43         lwz     r4,CFG_TZ_MINUTEWEST(r3)/* fill tz */
44         lwz     r5,CFG_TZ_DSTTIME(r3)
45         stw     r4,TZONE_TZ_MINWEST(r10)
46         stw     r5,TZONE_TZ_DSTTIME(r10)
47 1:      mtlr    r12
48         crclr   cr0*4+so
49         li      r3,0                    /* always success */
50         blr
51   .cfi_endproc
52 V_FUNCTION_END(__kernel_gettimeofday)
53
54
55 /*
56  * Exact prototype of clock_gettime()
57  *
58  * int __kernel_clock_gettime(clockid_t clock_id, struct timespec *tp);
59  *
60  */
61 V_FUNCTION_BEGIN(__kernel_clock_gettime)
62   .cfi_startproc
63         /* Check for supported clock IDs */
64         cmpwi   cr0,r3,CLOCK_REALTIME
65         cmpwi   cr1,r3,CLOCK_MONOTONIC
66         cror    cr0*4+eq,cr0*4+eq,cr1*4+eq
67
68         cmpwi   cr5,r3,CLOCK_REALTIME_COARSE
69         cmpwi   cr6,r3,CLOCK_MONOTONIC_COARSE
70         cror    cr5*4+eq,cr5*4+eq,cr6*4+eq
71
72         cror    cr0*4+eq,cr0*4+eq,cr5*4+eq
73         bne     cr0,99f
74
75         mflr    r12                     /* r12 saves lr */
76   .cfi_register lr,r12
77         mr      r11,r4                  /* r11 saves tp */
78         bl      V_LOCAL_FUNC(__get_datapage)    /* get data page */
79         lis     r7,NSEC_PER_SEC@h       /* want nanoseconds */
80         ori     r7,r7,NSEC_PER_SEC@l
81         beq     cr5,70f
82 50:     bl      V_LOCAL_FUNC(__do_get_tspec)    /* get time from tb & kernel */
83         bne     cr1,80f                 /* if not monotonic, all done */
84
85         /*
86          * CLOCK_MONOTONIC
87          */
88
89         /* now we must fixup using wall to monotonic. We need to snapshot
90          * that value and do the counter trick again. Fortunately, we still
91          * have the counter value in r8 that was returned by __do_get_tspec.
92          * At this point, r4,r5 contain our sec/nsec values.
93          */
94
95         ld      r6,WTOM_CLOCK_SEC(r3)
96         lwa     r9,WTOM_CLOCK_NSEC(r3)
97
98         /* We now have our result in r6,r9. We create a fake dependency
99          * on that result and re-check the counter
100          */
101         or      r0,r6,r9
102         xor     r0,r0,r0
103         add     r3,r3,r0
104         ld      r0,CFG_TB_UPDATE_COUNT(r3)
105         cmpld   cr0,r0,r8               /* check if updated */
106         bne-    50b
107         b       78f
108
109         /*
110          * For coarse clocks we get data directly from the vdso data page, so
111          * we don't need to call __do_get_tspec, but we still need to do the
112          * counter trick.
113          */
114 70:     ld      r8,CFG_TB_UPDATE_COUNT(r3)
115         andi.   r0,r8,1                 /* pending update ? loop */
116         bne-    70b
117         add     r3,r3,r0                /* r0 is already 0 */
118
119         /*
120          * CLOCK_REALTIME_COARSE, below values are needed for MONOTONIC_COARSE
121          * too
122          */
123         ld      r4,STAMP_XTIME+TSPC64_TV_SEC(r3)
124         ld      r5,STAMP_XTIME+TSPC64_TV_NSEC(r3)
125         bne     cr6,75f
126
127         /* CLOCK_MONOTONIC_COARSE */
128         ld      r6,WTOM_CLOCK_SEC(r3)
129         lwa     r9,WTOM_CLOCK_NSEC(r3)
130
131         /* check if counter has updated */
132         or      r0,r6,r9
133 75:     or      r0,r0,r4
134         or      r0,r0,r5
135         xor     r0,r0,r0
136         add     r3,r3,r0
137         ld      r0,CFG_TB_UPDATE_COUNT(r3)
138         cmpld   cr0,r0,r8               /* check if updated */
139         bne-    70b
140
141         /* Counter has not updated, so continue calculating proper values for
142          * sec and nsec if monotonic coarse, or just return with the proper
143          * values for realtime.
144          */
145         bne     cr6,80f
146
147         /* Add wall->monotonic offset and check for overflow or underflow */
148 78:     add     r4,r4,r6
149         add     r5,r5,r9
150         cmpd    cr0,r5,r7
151         cmpdi   cr1,r5,0
152         blt     79f
153         subf    r5,r7,r5
154         addi    r4,r4,1
155 79:     bge     cr1,80f
156         addi    r4,r4,-1
157         add     r5,r5,r7
158
159 80:     std     r4,TSPC64_TV_SEC(r11)
160         std     r5,TSPC64_TV_NSEC(r11)
161
162         mtlr    r12
163         crclr   cr0*4+so
164         li      r3,0
165         blr
166
167         /*
168          * syscall fallback
169          */
170 99:
171         li      r0,__NR_clock_gettime
172   .cfi_restore lr
173         sc
174         blr
175   .cfi_endproc
176 V_FUNCTION_END(__kernel_clock_gettime)
177
178
179 /*
180  * Exact prototype of clock_getres()
181  *
182  * int __kernel_clock_getres(clockid_t clock_id, struct timespec *res);
183  *
184  */
185 V_FUNCTION_BEGIN(__kernel_clock_getres)
186   .cfi_startproc
187         /* Check for supported clock IDs */
188         cmpwi   cr0,r3,CLOCK_REALTIME
189         cmpwi   cr1,r3,CLOCK_MONOTONIC
190         cror    cr0*4+eq,cr0*4+eq,cr1*4+eq
191         bne     cr0,99f
192
193         mflr    r12
194   .cfi_register lr,r12
195         bl      V_LOCAL_FUNC(__get_datapage)
196         lwz     r5, CLOCK_HRTIMER_RES(r3)
197         mtlr    r12
198         li      r3,0
199         cmpldi  cr0,r4,0
200         crclr   cr0*4+so
201         beqlr
202         std     r3,TSPC64_TV_SEC(r4)
203         std     r5,TSPC64_TV_NSEC(r4)
204         blr
205
206         /*
207          * syscall fallback
208          */
209 99:
210         li      r0,__NR_clock_getres
211         sc
212         blr
213   .cfi_endproc
214 V_FUNCTION_END(__kernel_clock_getres)
215
216 /*
217  * Exact prototype of time()
218  *
219  * time_t time(time *t);
220  *
221  */
222 V_FUNCTION_BEGIN(__kernel_time)
223   .cfi_startproc
224         mflr    r12
225   .cfi_register lr,r12
226
227         mr      r11,r3                  /* r11 holds t */
228         bl      V_LOCAL_FUNC(__get_datapage)
229
230         ld      r4,STAMP_XTIME+TSPC64_TV_SEC(r3)
231
232         cmpldi  r11,0                   /* check if t is NULL */
233         beq     2f
234         std     r4,0(r11)               /* store result at *t */
235 2:      mtlr    r12
236         crclr   cr0*4+so
237         mr      r3,r4
238         blr
239   .cfi_endproc
240 V_FUNCTION_END(__kernel_time)
241
242
243 /*
244  * This is the core of clock_gettime() and gettimeofday(),
245  * it returns the current time in r4 (seconds) and r5.
246  * On entry, r7 gives the resolution of r5, either USEC_PER_SEC
247  * or NSEC_PER_SEC, giving r5 in microseconds or nanoseconds.
248  * It expects the datapage ptr in r3 and doesn't clobber it.
249  * It clobbers r0, r6 and r9.
250  * On return, r8 contains the counter value that can be reused.
251  * This clobbers cr0 but not any other cr field.
252  */
253 V_FUNCTION_BEGIN(__do_get_tspec)
254   .cfi_startproc
255         /* check for update count & load values */
256 1:      ld      r8,CFG_TB_UPDATE_COUNT(r3)
257         andi.   r0,r8,1                 /* pending update ? loop */
258         bne-    1b
259         xor     r0,r8,r8                /* create dependency */
260         add     r3,r3,r0
261
262         /* Get TB & offset it. We use the MFTB macro which will generate
263          * workaround code for Cell.
264          */
265         MFTB(r6)
266         ld      r9,CFG_TB_ORIG_STAMP(r3)
267         subf    r6,r9,r6
268
269         /* Scale result */
270         ld      r5,CFG_TB_TO_XS(r3)
271         sldi    r6,r6,12                /* compute time since stamp_xtime */
272         mulhdu  r6,r6,r5                /* in units of 2^-32 seconds */
273
274         /* Add stamp since epoch */
275         ld      r4,STAMP_XTIME+TSPC64_TV_SEC(r3)
276         lwz     r5,STAMP_SEC_FRAC(r3)
277         or      r0,r4,r5
278         or      r0,r0,r6
279         xor     r0,r0,r0
280         add     r3,r3,r0
281         ld      r0,CFG_TB_UPDATE_COUNT(r3)
282         cmpld   r0,r8                   /* check if updated */
283         bne-    1b                      /* reload if so */
284
285         /* convert to seconds & nanoseconds and add to stamp */
286         add     r6,r6,r5                /* add on fractional seconds of xtime */
287         mulhwu  r5,r6,r7                /* compute micro or nanoseconds and */
288         srdi    r6,r6,32                /* seconds since stamp_xtime */
289         clrldi  r5,r5,32
290         add     r4,r4,r6
291         blr
292   .cfi_endproc
293 V_FUNCTION_END(__do_get_tspec)