Branch data Line data Source code
1 : : // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
2 : : /*
3 : : * Timebase helpers.
4 : : *
5 : : * Note: Only use after the TODs are in sync !
6 : : *
7 : : * Copyright 2013-2016 IBM Corp.
8 : : */
9 : :
10 : : #ifndef __TIME_H
11 : : #define __TIME_H
12 : :
13 : : #include <time.h>
14 : :
15 : : #ifndef __TEST__
16 : : static inline unsigned long mftb(void)
17 : : {
18 : : unsigned long tb;
19 : :
20 : : /* We use a memory clobber to avoid this being
21 : : * moved in the instruction stream
22 : : */
23 : : asm volatile("mftb %0" : "=r"(tb) : : "memory");
24 : : return tb;
25 : : }
26 : : #endif
27 : :
28 : : enum tb_cmpval {
29 : : TB_ABEFOREB = -1,
30 : : TB_AEQUALB = 0,
31 : : TB_AAFTERB = 1
32 : : };
33 : :
34 : 3 : static inline enum tb_cmpval tb_compare(unsigned long a,
35 : : unsigned long b)
36 : : {
37 : 3 : if (a == b)
38 : 1 : return TB_AEQUALB;
39 : 2 : return ((long)(b - a)) > 0 ? TB_ABEFOREB : TB_AAFTERB;
40 : : }
41 : :
42 : : /* Architected timebase */
43 : : extern unsigned long tb_hz;
44 : :
45 : 3 : static inline unsigned long secs_to_tb(unsigned long secs)
46 : : {
47 : 3 : return secs * tb_hz;
48 : : }
49 : :
50 : 1902 : static inline unsigned long tb_to_secs(unsigned long tb)
51 : : {
52 : 1902 : return tb / tb_hz;
53 : : }
54 : :
55 : 11 : static inline unsigned long tb_remaining_nsecs(unsigned long tb)
56 : : {
57 : 11 : return tb % tb_hz;
58 : : }
59 : :
60 : 7 : static inline unsigned long msecs_to_tb(unsigned long msecs)
61 : : {
62 : 7 : return msecs * (tb_hz / 1000);
63 : : }
64 : :
65 : 1 : static inline unsigned long tb_to_msecs(unsigned long tb)
66 : : {
67 : 1 : return (tb * 1000) / tb_hz;
68 : : }
69 : :
70 : 5 : static inline unsigned long usecs_to_tb(unsigned long usecs)
71 : : {
72 : 5 : return usecs * (tb_hz / 1000000);
73 : : }
74 : :
75 : 2 : static inline unsigned long tb_to_usecs(unsigned long tb)
76 : : {
77 : 2 : return (tb * 1000000) / tb_hz;
78 : : }
79 : :
80 : : extern unsigned long timespec_to_tb(const struct timespec *ts);
81 : :
82 : : /* time_wait - Wait a certain number of TB ticks while polling FSP */
83 : : extern void time_wait(unsigned long duration);
84 : : extern void time_wait_nopoll(unsigned long duration);
85 : :
86 : : /* time_wait_ms - Wait a certain number of milliseconds while polling FSP */
87 : : extern void time_wait_ms(unsigned long ms);
88 : : extern void time_wait_ms_nopoll(unsigned long ms);
89 : :
90 : : /* time_wait_us - Wait a certain number of microseconds while polling FSP */
91 : : extern void time_wait_us(unsigned long us);
92 : : extern void time_wait_us_nopoll(unsigned long us);
93 : :
94 : : /* nanosleep_nopoll - variant for use from hostservices */
95 : : extern int nanosleep_nopoll(const struct timespec *req, struct timespec *rem);
96 : : #endif /* __TIME_H */
|