1*4882a593Smuzhiyun/* SPDX-License-Identifier: GPL-2.0-or-later */ 2*4882a593Smuzhiyun/* 3*4882a593Smuzhiyun * Userland implementation of gettimeofday() for 32 bits processes in a 4*4882a593Smuzhiyun * ppc64 kernel for use in the vDSO 5*4882a593Smuzhiyun * 6*4882a593Smuzhiyun * Copyright (C) 2004 Benjamin Herrenschmuidt (benh@kernel.crashing.org, 7*4882a593Smuzhiyun * IBM Corp. 8*4882a593Smuzhiyun */ 9*4882a593Smuzhiyun#include <asm/processor.h> 10*4882a593Smuzhiyun#include <asm/ppc_asm.h> 11*4882a593Smuzhiyun#include <asm/vdso.h> 12*4882a593Smuzhiyun#include <asm/vdso_datapage.h> 13*4882a593Smuzhiyun#include <asm/asm-offsets.h> 14*4882a593Smuzhiyun#include <asm/unistd.h> 15*4882a593Smuzhiyun 16*4882a593Smuzhiyun/* Offset for the low 32-bit part of a field of long type */ 17*4882a593Smuzhiyun#ifdef CONFIG_PPC64 18*4882a593Smuzhiyun#define LOPART 4 19*4882a593Smuzhiyun#else 20*4882a593Smuzhiyun#define LOPART 0 21*4882a593Smuzhiyun#endif 22*4882a593Smuzhiyun 23*4882a593Smuzhiyun .text 24*4882a593Smuzhiyun/* 25*4882a593Smuzhiyun * Exact prototype of gettimeofday 26*4882a593Smuzhiyun * 27*4882a593Smuzhiyun * int __kernel_gettimeofday(struct timeval *tv, struct timezone *tz); 28*4882a593Smuzhiyun * 29*4882a593Smuzhiyun */ 30*4882a593SmuzhiyunV_FUNCTION_BEGIN(__kernel_gettimeofday) 31*4882a593Smuzhiyun .cfi_startproc 32*4882a593Smuzhiyun mflr r12 33*4882a593Smuzhiyun .cfi_register lr,r12 34*4882a593Smuzhiyun 35*4882a593Smuzhiyun mr. r10,r3 /* r10 saves tv */ 36*4882a593Smuzhiyun mr r11,r4 /* r11 saves tz */ 37*4882a593Smuzhiyun get_datapage r9, r0 38*4882a593Smuzhiyun beq 3f 39*4882a593Smuzhiyun LOAD_REG_IMMEDIATE(r7, 1000000) /* load up USEC_PER_SEC */ 40*4882a593Smuzhiyun bl __do_get_tspec@local /* get sec/usec from tb & kernel */ 41*4882a593Smuzhiyun stw r3,TVAL32_TV_SEC(r10) 42*4882a593Smuzhiyun stw r4,TVAL32_TV_USEC(r10) 43*4882a593Smuzhiyun 44*4882a593Smuzhiyun3: cmplwi r11,0 /* check if tz is NULL */ 45*4882a593Smuzhiyun mtlr r12 46*4882a593Smuzhiyun crclr cr0*4+so 47*4882a593Smuzhiyun li r3,0 48*4882a593Smuzhiyun beqlr 49*4882a593Smuzhiyun 50*4882a593Smuzhiyun lwz r4,CFG_TZ_MINUTEWEST(r9)/* fill tz */ 51*4882a593Smuzhiyun lwz r5,CFG_TZ_DSTTIME(r9) 52*4882a593Smuzhiyun stw r4,TZONE_TZ_MINWEST(r11) 53*4882a593Smuzhiyun stw r5,TZONE_TZ_DSTTIME(r11) 54*4882a593Smuzhiyun 55*4882a593Smuzhiyun blr 56*4882a593Smuzhiyun .cfi_endproc 57*4882a593SmuzhiyunV_FUNCTION_END(__kernel_gettimeofday) 58*4882a593Smuzhiyun 59*4882a593Smuzhiyun/* 60*4882a593Smuzhiyun * Exact prototype of clock_gettime() 61*4882a593Smuzhiyun * 62*4882a593Smuzhiyun * int __kernel_clock_gettime(clockid_t clock_id, struct timespec *tp); 63*4882a593Smuzhiyun * 64*4882a593Smuzhiyun */ 65*4882a593SmuzhiyunV_FUNCTION_BEGIN(__kernel_clock_gettime) 66*4882a593Smuzhiyun .cfi_startproc 67*4882a593Smuzhiyun /* Check for supported clock IDs */ 68*4882a593Smuzhiyun cmpli cr0,r3,CLOCK_REALTIME 69*4882a593Smuzhiyun cmpli cr1,r3,CLOCK_MONOTONIC 70*4882a593Smuzhiyun cror cr0*4+eq,cr0*4+eq,cr1*4+eq 71*4882a593Smuzhiyun 72*4882a593Smuzhiyun cmpli cr5,r3,CLOCK_REALTIME_COARSE 73*4882a593Smuzhiyun cmpli cr6,r3,CLOCK_MONOTONIC_COARSE 74*4882a593Smuzhiyun cror cr5*4+eq,cr5*4+eq,cr6*4+eq 75*4882a593Smuzhiyun 76*4882a593Smuzhiyun cror cr0*4+eq,cr0*4+eq,cr5*4+eq 77*4882a593Smuzhiyun bne cr0, .Lgettime_fallback 78*4882a593Smuzhiyun 79*4882a593Smuzhiyun mflr r12 /* r12 saves lr */ 80*4882a593Smuzhiyun .cfi_register lr,r12 81*4882a593Smuzhiyun mr r11,r4 /* r11 saves tp */ 82*4882a593Smuzhiyun get_datapage r9, r0 83*4882a593Smuzhiyun LOAD_REG_IMMEDIATE(r7, NSEC_PER_SEC) /* load up NSEC_PER_SEC */ 84*4882a593Smuzhiyun beq cr5, .Lcoarse_clocks 85*4882a593Smuzhiyun.Lprecise_clocks: 86*4882a593Smuzhiyun bl __do_get_tspec@local /* get sec/nsec from tb & kernel */ 87*4882a593Smuzhiyun bne cr1, .Lfinish /* not monotonic -> all done */ 88*4882a593Smuzhiyun 89*4882a593Smuzhiyun /* 90*4882a593Smuzhiyun * CLOCK_MONOTONIC 91*4882a593Smuzhiyun */ 92*4882a593Smuzhiyun 93*4882a593Smuzhiyun /* now we must fixup using wall to monotonic. We need to snapshot 94*4882a593Smuzhiyun * that value and do the counter trick again. Fortunately, we still 95*4882a593Smuzhiyun * have the counter value in r8 that was returned by __do_get_xsec. 96*4882a593Smuzhiyun * At this point, r3,r4 contain our sec/nsec values, r5 and r6 97*4882a593Smuzhiyun * can be used, r7 contains NSEC_PER_SEC. 98*4882a593Smuzhiyun */ 99*4882a593Smuzhiyun 100*4882a593Smuzhiyun lwz r5,(WTOM_CLOCK_SEC+LOPART)(r9) 101*4882a593Smuzhiyun lwz r6,WTOM_CLOCK_NSEC(r9) 102*4882a593Smuzhiyun 103*4882a593Smuzhiyun /* We now have our offset in r5,r6. We create a fake dependency 104*4882a593Smuzhiyun * on that value and re-check the counter 105*4882a593Smuzhiyun */ 106*4882a593Smuzhiyun or r0,r6,r5 107*4882a593Smuzhiyun xor r0,r0,r0 108*4882a593Smuzhiyun add r9,r9,r0 109*4882a593Smuzhiyun lwz r0,(CFG_TB_UPDATE_COUNT+LOPART)(r9) 110*4882a593Smuzhiyun cmpl cr0,r8,r0 /* check if updated */ 111*4882a593Smuzhiyun bne- .Lprecise_clocks 112*4882a593Smuzhiyun b .Lfinish_monotonic 113*4882a593Smuzhiyun 114*4882a593Smuzhiyun /* 115*4882a593Smuzhiyun * For coarse clocks we get data directly from the vdso data page, so 116*4882a593Smuzhiyun * we don't need to call __do_get_tspec, but we still need to do the 117*4882a593Smuzhiyun * counter trick. 118*4882a593Smuzhiyun */ 119*4882a593Smuzhiyun.Lcoarse_clocks: 120*4882a593Smuzhiyun lwz r8,(CFG_TB_UPDATE_COUNT+LOPART)(r9) 121*4882a593Smuzhiyun andi. r0,r8,1 /* pending update ? loop */ 122*4882a593Smuzhiyun bne- .Lcoarse_clocks 123*4882a593Smuzhiyun add r9,r9,r0 /* r0 is already 0 */ 124*4882a593Smuzhiyun 125*4882a593Smuzhiyun /* 126*4882a593Smuzhiyun * CLOCK_REALTIME_COARSE, below values are needed for MONOTONIC_COARSE 127*4882a593Smuzhiyun * too 128*4882a593Smuzhiyun */ 129*4882a593Smuzhiyun lwz r3,STAMP_XTIME_SEC+LOPART(r9) 130*4882a593Smuzhiyun lwz r4,STAMP_XTIME_NSEC+LOPART(r9) 131*4882a593Smuzhiyun bne cr6,1f 132*4882a593Smuzhiyun 133*4882a593Smuzhiyun /* CLOCK_MONOTONIC_COARSE */ 134*4882a593Smuzhiyun lwz r5,(WTOM_CLOCK_SEC+LOPART)(r9) 135*4882a593Smuzhiyun lwz r6,WTOM_CLOCK_NSEC(r9) 136*4882a593Smuzhiyun 137*4882a593Smuzhiyun /* check if counter has updated */ 138*4882a593Smuzhiyun or r0,r6,r5 139*4882a593Smuzhiyun1: or r0,r0,r3 140*4882a593Smuzhiyun or r0,r0,r4 141*4882a593Smuzhiyun xor r0,r0,r0 142*4882a593Smuzhiyun add r3,r3,r0 143*4882a593Smuzhiyun lwz r0,CFG_TB_UPDATE_COUNT+LOPART(r9) 144*4882a593Smuzhiyun cmpl cr0,r0,r8 /* check if updated */ 145*4882a593Smuzhiyun bne- .Lcoarse_clocks 146*4882a593Smuzhiyun 147*4882a593Smuzhiyun /* Counter has not updated, so continue calculating proper values for 148*4882a593Smuzhiyun * sec and nsec if monotonic coarse, or just return with the proper 149*4882a593Smuzhiyun * values for realtime. 150*4882a593Smuzhiyun */ 151*4882a593Smuzhiyun bne cr6, .Lfinish 152*4882a593Smuzhiyun 153*4882a593Smuzhiyun /* Calculate and store result. Note that this mimics the C code, 154*4882a593Smuzhiyun * which may cause funny results if nsec goes negative... is that 155*4882a593Smuzhiyun * possible at all ? 156*4882a593Smuzhiyun */ 157*4882a593Smuzhiyun.Lfinish_monotonic: 158*4882a593Smuzhiyun add r3,r3,r5 159*4882a593Smuzhiyun add r4,r4,r6 160*4882a593Smuzhiyun cmpw cr0,r4,r7 161*4882a593Smuzhiyun cmpwi cr1,r4,0 162*4882a593Smuzhiyun blt 1f 163*4882a593Smuzhiyun subf r4,r7,r4 164*4882a593Smuzhiyun addi r3,r3,1 165*4882a593Smuzhiyun1: bge cr1, .Lfinish 166*4882a593Smuzhiyun addi r3,r3,-1 167*4882a593Smuzhiyun add r4,r4,r7 168*4882a593Smuzhiyun 169*4882a593Smuzhiyun.Lfinish: 170*4882a593Smuzhiyun stw r3,TSPC32_TV_SEC(r11) 171*4882a593Smuzhiyun stw r4,TSPC32_TV_NSEC(r11) 172*4882a593Smuzhiyun 173*4882a593Smuzhiyun mtlr r12 174*4882a593Smuzhiyun crclr cr0*4+so 175*4882a593Smuzhiyun li r3,0 176*4882a593Smuzhiyun blr 177*4882a593Smuzhiyun 178*4882a593Smuzhiyun /* 179*4882a593Smuzhiyun * syscall fallback 180*4882a593Smuzhiyun */ 181*4882a593Smuzhiyun.Lgettime_fallback: 182*4882a593Smuzhiyun li r0,__NR_clock_gettime 183*4882a593Smuzhiyun .cfi_restore lr 184*4882a593Smuzhiyun sc 185*4882a593Smuzhiyun blr 186*4882a593Smuzhiyun .cfi_endproc 187*4882a593SmuzhiyunV_FUNCTION_END(__kernel_clock_gettime) 188*4882a593Smuzhiyun 189*4882a593Smuzhiyun 190*4882a593Smuzhiyun/* 191*4882a593Smuzhiyun * Exact prototype of clock_getres() 192*4882a593Smuzhiyun * 193*4882a593Smuzhiyun * int __kernel_clock_getres(clockid_t clock_id, struct timespec *res); 194*4882a593Smuzhiyun * 195*4882a593Smuzhiyun */ 196*4882a593SmuzhiyunV_FUNCTION_BEGIN(__kernel_clock_getres) 197*4882a593Smuzhiyun .cfi_startproc 198*4882a593Smuzhiyun /* Check for supported clock IDs */ 199*4882a593Smuzhiyun cmplwi cr0, r3, CLOCK_MAX 200*4882a593Smuzhiyun cmpwi cr1, r3, CLOCK_REALTIME_COARSE 201*4882a593Smuzhiyun cmpwi cr7, r3, CLOCK_MONOTONIC_COARSE 202*4882a593Smuzhiyun bgt cr0, 99f 203*4882a593Smuzhiyun LOAD_REG_IMMEDIATE(r5, KTIME_LOW_RES) 204*4882a593Smuzhiyun beq cr1, 1f 205*4882a593Smuzhiyun beq cr7, 1f 206*4882a593Smuzhiyun 207*4882a593Smuzhiyun mflr r12 208*4882a593Smuzhiyun .cfi_register lr,r12 209*4882a593Smuzhiyun get_datapage r3, r0 210*4882a593Smuzhiyun lwz r5, CLOCK_HRTIMER_RES(r3) 211*4882a593Smuzhiyun mtlr r12 212*4882a593Smuzhiyun1: li r3,0 213*4882a593Smuzhiyun cmpli cr0,r4,0 214*4882a593Smuzhiyun crclr cr0*4+so 215*4882a593Smuzhiyun beqlr 216*4882a593Smuzhiyun stw r3,TSPC32_TV_SEC(r4) 217*4882a593Smuzhiyun stw r5,TSPC32_TV_NSEC(r4) 218*4882a593Smuzhiyun blr 219*4882a593Smuzhiyun 220*4882a593Smuzhiyun /* 221*4882a593Smuzhiyun * syscall fallback 222*4882a593Smuzhiyun */ 223*4882a593Smuzhiyun99: 224*4882a593Smuzhiyun li r0,__NR_clock_getres 225*4882a593Smuzhiyun sc 226*4882a593Smuzhiyun blr 227*4882a593Smuzhiyun .cfi_endproc 228*4882a593SmuzhiyunV_FUNCTION_END(__kernel_clock_getres) 229*4882a593Smuzhiyun 230*4882a593Smuzhiyun 231*4882a593Smuzhiyun/* 232*4882a593Smuzhiyun * Exact prototype of time() 233*4882a593Smuzhiyun * 234*4882a593Smuzhiyun * time_t time(time *t); 235*4882a593Smuzhiyun * 236*4882a593Smuzhiyun */ 237*4882a593SmuzhiyunV_FUNCTION_BEGIN(__kernel_time) 238*4882a593Smuzhiyun .cfi_startproc 239*4882a593Smuzhiyun mflr r12 240*4882a593Smuzhiyun .cfi_register lr,r12 241*4882a593Smuzhiyun 242*4882a593Smuzhiyun mr r11,r3 /* r11 holds t */ 243*4882a593Smuzhiyun get_datapage r9, r0 244*4882a593Smuzhiyun 245*4882a593Smuzhiyun lwz r3,STAMP_XTIME_SEC+LOPART(r9) 246*4882a593Smuzhiyun 247*4882a593Smuzhiyun cmplwi r11,0 /* check if t is NULL */ 248*4882a593Smuzhiyun mtlr r12 249*4882a593Smuzhiyun crclr cr0*4+so 250*4882a593Smuzhiyun beqlr 251*4882a593Smuzhiyun stw r3,0(r11) /* store result at *t */ 252*4882a593Smuzhiyun blr 253*4882a593Smuzhiyun .cfi_endproc 254*4882a593SmuzhiyunV_FUNCTION_END(__kernel_time) 255*4882a593Smuzhiyun 256*4882a593Smuzhiyun/* 257*4882a593Smuzhiyun * This is the core of clock_gettime() and gettimeofday(), 258*4882a593Smuzhiyun * it returns the current time in r3 (seconds) and r4. 259*4882a593Smuzhiyun * On entry, r7 gives the resolution of r4, either USEC_PER_SEC 260*4882a593Smuzhiyun * or NSEC_PER_SEC, giving r4 in microseconds or nanoseconds. 261*4882a593Smuzhiyun * It expects the datapage ptr in r9 and doesn't clobber it. 262*4882a593Smuzhiyun * It clobbers r0, r5 and r6. 263*4882a593Smuzhiyun * On return, r8 contains the counter value that can be reused. 264*4882a593Smuzhiyun * This clobbers cr0 but not any other cr field. 265*4882a593Smuzhiyun */ 266*4882a593Smuzhiyun__do_get_tspec: 267*4882a593Smuzhiyun .cfi_startproc 268*4882a593Smuzhiyun /* Check for update count & load values. We use the low 269*4882a593Smuzhiyun * order 32 bits of the update count 270*4882a593Smuzhiyun */ 271*4882a593Smuzhiyun1: lwz r8,(CFG_TB_UPDATE_COUNT+LOPART)(r9) 272*4882a593Smuzhiyun andi. r0,r8,1 /* pending update ? loop */ 273*4882a593Smuzhiyun bne- 1b 274*4882a593Smuzhiyun xor r0,r8,r8 /* create dependency */ 275*4882a593Smuzhiyun add r9,r9,r0 276*4882a593Smuzhiyun 277*4882a593Smuzhiyun /* Load orig stamp (offset to TB) */ 278*4882a593Smuzhiyun lwz r5,CFG_TB_ORIG_STAMP(r9) 279*4882a593Smuzhiyun lwz r6,(CFG_TB_ORIG_STAMP+4)(r9) 280*4882a593Smuzhiyun 281*4882a593Smuzhiyun /* Get a stable TB value */ 282*4882a593Smuzhiyun2: MFTBU(r3) 283*4882a593Smuzhiyun MFTBL(r4) 284*4882a593Smuzhiyun MFTBU(r0) 285*4882a593Smuzhiyun cmplw cr0,r3,r0 286*4882a593Smuzhiyun bne- 2b 287*4882a593Smuzhiyun 288*4882a593Smuzhiyun /* Subtract tb orig stamp and shift left 12 bits. 289*4882a593Smuzhiyun */ 290*4882a593Smuzhiyun subfc r4,r6,r4 291*4882a593Smuzhiyun subfe r0,r5,r3 292*4882a593Smuzhiyun slwi r0,r0,12 293*4882a593Smuzhiyun rlwimi. r0,r4,12,20,31 294*4882a593Smuzhiyun slwi r4,r4,12 295*4882a593Smuzhiyun 296*4882a593Smuzhiyun /* 297*4882a593Smuzhiyun * Load scale factor & do multiplication. 298*4882a593Smuzhiyun * We only use the high 32 bits of the tb_to_xs value. 299*4882a593Smuzhiyun * Even with a 1GHz timebase clock, the high 32 bits of 300*4882a593Smuzhiyun * tb_to_xs will be at least 4 million, so the error from 301*4882a593Smuzhiyun * ignoring the low 32 bits will be no more than 0.25ppm. 302*4882a593Smuzhiyun * The error will just make the clock run very very slightly 303*4882a593Smuzhiyun * slow until the next time the kernel updates the VDSO data, 304*4882a593Smuzhiyun * at which point the clock will catch up to the kernel's value, 305*4882a593Smuzhiyun * so there is no long-term error accumulation. 306*4882a593Smuzhiyun */ 307*4882a593Smuzhiyun lwz r5,CFG_TB_TO_XS(r9) /* load values */ 308*4882a593Smuzhiyun mulhwu r4,r4,r5 309*4882a593Smuzhiyun li r3,0 310*4882a593Smuzhiyun 311*4882a593Smuzhiyun beq+ 4f /* skip high part computation if 0 */ 312*4882a593Smuzhiyun mulhwu r3,r0,r5 313*4882a593Smuzhiyun mullw r5,r0,r5 314*4882a593Smuzhiyun addc r4,r4,r5 315*4882a593Smuzhiyun addze r3,r3 316*4882a593Smuzhiyun4: 317*4882a593Smuzhiyun /* At this point, we have seconds since the xtime stamp 318*4882a593Smuzhiyun * as a 32.32 fixed-point number in r3 and r4. 319*4882a593Smuzhiyun * Load & add the xtime stamp. 320*4882a593Smuzhiyun */ 321*4882a593Smuzhiyun lwz r5,STAMP_XTIME_SEC+LOPART(r9) 322*4882a593Smuzhiyun lwz r6,STAMP_SEC_FRAC(r9) 323*4882a593Smuzhiyun addc r4,r4,r6 324*4882a593Smuzhiyun adde r3,r3,r5 325*4882a593Smuzhiyun 326*4882a593Smuzhiyun /* We create a fake dependency on the result in r3/r4 327*4882a593Smuzhiyun * and re-check the counter 328*4882a593Smuzhiyun */ 329*4882a593Smuzhiyun or r6,r4,r3 330*4882a593Smuzhiyun xor r0,r6,r6 331*4882a593Smuzhiyun add r9,r9,r0 332*4882a593Smuzhiyun lwz r0,(CFG_TB_UPDATE_COUNT+LOPART)(r9) 333*4882a593Smuzhiyun cmplw cr0,r8,r0 /* check if updated */ 334*4882a593Smuzhiyun bne- 1b 335*4882a593Smuzhiyun 336*4882a593Smuzhiyun mulhwu r4,r4,r7 /* convert to micro or nanoseconds */ 337*4882a593Smuzhiyun 338*4882a593Smuzhiyun blr 339*4882a593Smuzhiyun .cfi_endproc 340