source: GPL/trunk/include/linux/clocksource.h

Last change on this file was 679, checked in by David Azarewicz, 4 years ago

Merge changes from Paul's uniaud32next branch.

File size: 3.3 KB
Line 
1#ifndef _LINUX_CLOCKSOURCE_H
2#define _LINUX_CLOCKSOURCE_H
3
4#include <linux/init.h>
5#include <linux/types.h>
6#include <asm/io.h>
7
8/* clocksource cycle base type */
9typedef u64 cycle_t;
10struct clocksource;
11
12
13/**
14 * struct cyclecounter - hardware abstraction for a free running counter
15 * Provides completely state-free accessors to the underlying hardware.
16 * Depending on which hardware it reads, the cycle counter may wrap
17 * around quickly. Locking rules (if necessary) have to be defined
18 * by the implementor and user of specific instances of this API.
19 *
20 * @read: returns the current cycle value
21 * @mask: bitmask for two's complement
22 * subtraction of non 64 bit counters,
23 * see CLOCKSOURCE_MASK() helper macro
24 * @mult: cycle to nanosecond multiplier
25 * @shift: cycle to nanosecond divisor (power of two)
26 */
27struct cyclecounter {
28 cycle_t (*read)(const struct cyclecounter *cc);
29 cycle_t mask;
30 u32 mult;
31 u32 shift;
32};
33
34/**
35 * struct timecounter - layer above a %struct cyclecounter which counts nanoseconds
36 * Contains the state needed by timecounter_read() to detect
37 * cycle counter wrap around. Initialize with
38 * timecounter_init(). Also used to convert cycle counts into the
39 * corresponding nanosecond counts with timecounter_cyc2time(). Users
40 * of this code are responsible for initializing the underlying
41 * cycle counter hardware, locking issues and reading the time
42 * more often than the cycle counter wraps around. The nanosecond
43 * counter will only wrap around after ~585 years.
44 *
45 * @cc: the cycle counter used by this instance
46 * @cycle_last: most recent cycle counter value seen by
47 * timecounter_read()
48 * @nsec: continuously increasing count
49 */
50struct timecounter {
51 const struct cyclecounter *cc;
52 cycle_t cycle_last;
53 u64 nsec;
54};
55
56/**
57 * cyclecounter_cyc2ns - converts cycle counter cycles to nanoseconds
58 * @cc: Pointer to cycle counter.
59 * @cycles: Cycles
60 *
61 * XXX - This could use some mult_lxl_ll() asm optimization. Same code
62 * as in cyc2ns, but with unsigned result.
63 */
64static inline u64 cyclecounter_cyc2ns(const struct cyclecounter *cc,
65 cycle_t cycles)
66{
67 u64 ret = (u64)cycles;
68 ret = (ret * cc->mult) >> cc->shift;
69 return ret;
70}
71
72/**
73 * timecounter_init - initialize a time counter
74 * @tc: Pointer to time counter which is to be initialized/reset
75 * @cc: A cycle counter, ready to be used.
76 * @start_tstamp: Arbitrary initial time stamp.
77 *
78 * After this call the current cycle register (roughly) corresponds to
79 * the initial time stamp. Every call to timecounter_read() increments
80 * the time stamp counter by the number of elapsed nanoseconds.
81 */
82extern void timecounter_init(struct timecounter *tc,
83 const struct cyclecounter *cc,
84 u64 start_tstamp);
85
86/**
87 * timecounter_read - return nanoseconds elapsed since timecounter_init()
88 * plus the initial time stamp
89 * @tc: Pointer to time counter.
90 *
91 * In other words, keeps track of time since the same epoch as
92 * the function which generated the initial time stamp.
93 */
94extern u64 timecounter_read(struct timecounter *tc);
95
96/* simplify initialization of mask field */
97#define CLOCKSOURCE_MASK(bits) (cycle_t)((bits) < 64 ? ((1ULL<<(bits))-1) : -1)
98
99#endif /* _LINUX_CLOCKSOURCE_H */
Note: See TracBrowser for help on using the repository browser.