Ignore:
Timestamp:
Mar 18, 2021, 8:57:36 PM (4 years ago)
Author:
David Azarewicz
Message:

Merge changes from Paul's uniaud32next branch.

Location:
GPL/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • GPL/trunk

  • GPL/trunk/lib32/timer.c

    r445 r679  
    3030#include <asm/io.h>
    3131#include <linux/time.h>
     32#include <linux/math64.h>
     33#include <linux/clocksource.h>
    3234
    3335#define LINUX
     
    3638#include <dbgos2.h>
    3739
     40#pragma pack(1)
     41#include     "infoseg.h"
     42#pragma pack()
     43extern PVOID  KernSISData;
     44#define KernSISData             ((struct InfoSegGDT *)&KernSISData)
     45
    3846static   long          jiffiems    = 1000/HZ;
    3947static   long          lasttime    = 0;
     
    4755    long delta, newtime, remainder;
    4856
    49     newtime    = os2gettimemsec();
     57    newtime    = KernSISData->SIS_MsCount;
    5058    delta      = newtime - lasttime;
    5159
     
    95103{
    96104#if 0
    97     tv->tv_sec  = 0; //os2gettimesec();
    98     tv->tv_usec = os2gettimemsec() * 1000;
     105    tv->tv_sec  = 0; //KernSISData->SIS_BigTime;
     106    tv->tv_usec = KernSISData->SIS_MsCount * 1000;
    99107#else /* r.ihle patch */
    100     unsigned u = os2gettimemsec();
     108    unsigned u = KernSISData->SIS_MsCount;
    101109    tv->tv_sec  = u / 1000;
    102110    tv->tv_usec = (u % 1000) * 1000;
     
    140148//******************************************************************************
    141149//******************************************************************************
     150
     151/**
     152 * ns_to_timespec - Convert nanoseconds to timespec
     153 * @nsec:       the nanoseconds value to be converted
     154 *
     155 * Returns the timespec representation of the nsec parameter.
     156 */
     157struct timespec ns_to_timespec(const s64 nsec)
     158{
     159        struct timespec ts;
     160        s32 rem;
     161
     162        if (!nsec) {
     163                ts.tv_sec = 0;
     164                ts.tv_nsec = 0;
     165                return ts;
     166        }
     167
     168        ts.tv_sec = div_s64_rem(nsec, NSEC_PER_SEC, &rem);
     169        if (unlikely(rem < 0)) {
     170                ts.tv_sec--;
     171                rem += NSEC_PER_SEC;
     172        }
     173        ts.tv_nsec = rem;
     174
     175        return ts;
     176}
     177
     178
     179//******************************************************************************
     180//******************************************************************************
     181
     182/**
     183 * ns_to_timespec - Convert nanoseconds to timespec
     184 * @nsec:       the nanoseconds value to be converted
     185 *
     186 * Returns the timespec representation of the nsec parameter.
     187 */
     188struct timespec64 ns_to_timespec64(const s64 nsec)
     189{
     190        struct timespec64 ts;
     191        s32 rem;
     192
     193        if (!nsec) {
     194                ts.tv_sec = 0;
     195                ts.tv_nsec = 0;
     196                return ts;
     197        }
     198
     199        ts.tv_sec = div_s64_rem(nsec, NSEC_PER_SEC, &rem);
     200        if (unlikely(rem < 0)) {
     201                ts.tv_sec--;
     202                rem += NSEC_PER_SEC;
     203        }
     204        ts.tv_nsec = rem;
     205
     206        return ts;
     207}
     208//******************************************************************************
     209//******************************************************************************
     210
     211void timecounter_init(struct timecounter *tc,
     212                      const struct cyclecounter *cc,
     213                      u64 start_tstamp)
     214{
     215        tc->cc = cc;
     216        tc->cycle_last = cc->read(cc);
     217        tc->nsec = start_tstamp;
     218}
     219
     220/**
     221 * timecounter_read_delta - get nanoseconds since last call of this function
     222 * @tc:         Pointer to time counter
     223 *
     224 * When the underlying cycle counter runs over, this will be handled
     225 * correctly as long as it does not run over more than once between
     226 * calls.
     227 *
     228 * The first call to this function for a new time counter initializes
     229 * the time tracking and returns an undefined result.
     230 */
     231static u64 timecounter_read_delta(struct timecounter *tc)
     232{
     233        cycle_t cycle_now, cycle_delta;
     234        u64 ns_offset;
     235
     236        /* read cycle counter: */
     237        cycle_now = tc->cc->read(tc->cc);
     238
     239        /* calculate the delta since the last timecounter_read_delta(): */
     240        cycle_delta = (cycle_now - tc->cycle_last) & tc->cc->mask;
     241
     242        /* convert to nanoseconds: */
     243        ns_offset = cyclecounter_cyc2ns(tc->cc, cycle_delta);
     244
     245        /* update time stamp of timecounter_read_delta() call: */
     246        tc->cycle_last = cycle_now;
     247
     248        return ns_offset;
     249}
     250
     251u64 timecounter_read(struct timecounter *tc)
     252{
     253        u64 nsec;
     254
     255        /* increment time by nanoseconds since last call */
     256        nsec = timecounter_read_delta(tc);
     257        nsec += tc->nsec;
     258        tc->nsec = nsec;
     259
     260        return nsec;
     261}
     262
     263/**
     264 * set_normalized_timespec - set timespec sec and nsec parts and normalize
     265 *
     266 * @ts:         pointer to timespec variable to be set
     267 * @sec:        seconds to set
     268 * @nsec:       nanoseconds to set
     269 *
     270 * Set seconds and nanoseconds field of a timespec variable and
     271 * normalize to the timespec storage format
     272 *
     273 * Note: The tv_nsec part is always in the range of
     274 *      0 <= tv_nsec < NSEC_PER_SEC
     275 * For negative values only the tv_sec field is negative !
     276 */
     277void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec)
     278{
     279        while (nsec >= NSEC_PER_SEC) {
     280                /*
     281                 * The following asm() prevents the compiler from
     282                 * optimising this loop into a modulo operation. See
     283                 * also __iter_div_u64_rem() in include/linux/time.h
     284                 */
     285//              asm("" : "+rm"(nsec));
     286                nsec -= NSEC_PER_SEC;
     287                ++sec;
     288        }
     289        while (nsec < 0) {
     290//              asm("" : "+rm"(nsec));
     291                nsec += NSEC_PER_SEC;
     292                --sec;
     293        }
     294        ts->tv_sec = sec;
     295        ts->tv_nsec = nsec;
     296}
Note: See TracChangeset for help on using the changeset viewer.