Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 14 May 2019 21:08:37 -0700
From:      Mark Millard <marklmi@yahoo.com>
To:        FreeBSD PowerPC ML <freebsd-ppc@freebsd.org>
Subject:   powerpc64 and 32-bit powerpc: get_cyclecount is messed up for both (presuming reliability is required)
Message-ID:  <366444D2-80EE-4A1B-BA75-322C20C5ECFE@yahoo.com>

next in thread | raw e-mail | index | archive | help
[I ignore here the difference between a processor-cycle-count
and the timebase register values, a distinction that might be
involved as well. This is just about tbr access techniques.]


/usr/src/sys/powerpc/include/cpu.h has:

static __inline u_int64_t
get_cyclecount(void)
{
        u_int32_t _upper, _lower;
        u_int64_t _time;

        __asm __volatile(
                "mftb %0\n"
                "mftbu %1"
                : "=r" (_lower), "=r" (_upper));

        _time = (u_int64_t)_upper;
        _time = (_time << 32) + _lower;
        return (_time);
}

Apparently used for both powerpc64 and 32-bit powerpc.

By contrast /usr/src/sys/powerpc/include/cpufunc.h
has:

static __inline u_quad_t
mftb(void)
{
        u_quad_t tb;
      #ifdef __powerpc64__
        __asm __volatile ("mftb %0" : "=r"(tb));
      #else
        uint32_t *tbup = (uint32_t *)&tb;
        uint32_t *tblp = tbup + 1;

        do {
                *tbup = mfspr(TBR_TBU);
                *tblp = mfspr(TBR_TBL);
        } while (*tbup != mfspr(TBR_TBU));
      #endif
 
        return (tb);
}

Back to get_cyclecount: Note the lack of
the loop for making sure the upper and
lower halves go together (upper half did
not change).

Note also that for powerpc64 there is a
more direct access available that avoids
needing to deal with such issues.

get_cyclecount seems to be referenced in:

# grep -lr get_cyclecount /usr/src/sys/ | grep -v /include/ | more
/usr/src/sys/dev/hwpmc/hwpmc_mod.c
/usr/src/sys/dev/ocs_fc/ocs_utils.c
/usr/src/sys/dev/random/random_harvestq.c
/usr/src/sys/dev/random/randomdev.c
/usr/src/sys/dev/random/unit_test.h
/usr/src/sys/dev/de/if_devar.h
/usr/src/sys/kern/init_main.c
/usr/src/sys/kern/subr_bus.c
/usr/src/sys/kern/kern_tslog.c
/usr/src/sys/kern/kern_ktr.c
/usr/src/sys/netinet/sctp_os_bsd.h
/usr/src/sys/libkern/arc4random.c

/random/ and arc4random might well not care abouy
extra variability. But the others?



===
Mark Millard
marklmi at yahoo.com
( dsl-only.net went
away in early 2018-Mar)




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?366444D2-80EE-4A1B-BA75-322C20C5ECFE>