Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Masahiro Yamada | a7b8176 | 2016-12-28 00:36:00 +0900 | [diff] [blame] | 2 | |
| 3 | #ifndef _TIME_H |
| 4 | #define _TIME_H |
| 5 | |
Masahiro Yamada | 21cdd13 | 2016-12-28 00:36:02 +0900 | [diff] [blame] | 6 | #include <linux/typecheck.h> |
Heinrich Schuchardt | 6a853db | 2019-06-02 21:02:10 +0200 | [diff] [blame] | 7 | #include <linux/types.h> |
Masahiro Yamada | 21cdd13 | 2016-12-28 00:36:02 +0900 | [diff] [blame] | 8 | |
Masahiro Yamada | a7b8176 | 2016-12-28 00:36:00 +0900 | [diff] [blame] | 9 | unsigned long get_timer(unsigned long base); |
| 10 | |
| 11 | /* |
| 12 | * Return the current value of a monotonically increasing microsecond timer. |
| 13 | * Granularity may be larger than 1us if hardware does not support this. |
| 14 | */ |
| 15 | unsigned long timer_get_us(void); |
| 16 | |
Masahiro Yamada | 21cdd13 | 2016-12-28 00:36:02 +0900 | [diff] [blame] | 17 | /* |
Neil Armstrong | d0a9b82 | 2019-04-11 17:01:23 +0200 | [diff] [blame] | 18 | * timer_test_add_offset() |
| 19 | * |
| 20 | * Allow tests to add to the time reported through lib/time.c functions |
| 21 | * offset: number of milliseconds to advance the system time |
| 22 | */ |
| 23 | void timer_test_add_offset(unsigned long offset); |
| 24 | |
Heinrich Schuchardt | 6a853db | 2019-06-02 21:02:10 +0200 | [diff] [blame] | 25 | /** |
| 26 | * usec_to_tick() - convert microseconds to clock ticks |
| 27 | * |
| 28 | * @usec: duration in microseconds |
| 29 | * Return: duration in clock ticks |
| 30 | */ |
| 31 | uint64_t usec_to_tick(unsigned long usec); |
| 32 | |
Neil Armstrong | d0a9b82 | 2019-04-11 17:01:23 +0200 | [diff] [blame] | 33 | /* |
Masahiro Yamada | 21cdd13 | 2016-12-28 00:36:02 +0900 | [diff] [blame] | 34 | * These inlines deal with timer wrapping correctly. You are |
| 35 | * strongly encouraged to use them |
| 36 | * 1. Because people otherwise forget |
| 37 | * 2. Because if the timer wrap changes in future you won't have to |
| 38 | * alter your driver code. |
| 39 | * |
| 40 | * time_after(a,b) returns true if the time a is after time b. |
| 41 | * |
| 42 | * Do this with "<0" and ">=0" to only test the sign of the result. A |
| 43 | * good compiler would generate better code (and a really good compiler |
| 44 | * wouldn't care). Gcc is currently neither. |
| 45 | */ |
| 46 | #define time_after(a,b) \ |
| 47 | (typecheck(unsigned long, a) && \ |
| 48 | typecheck(unsigned long, b) && \ |
| 49 | ((long)((b) - (a)) < 0)) |
| 50 | #define time_before(a,b) time_after(b,a) |
| 51 | |
| 52 | #define time_after_eq(a,b) \ |
| 53 | (typecheck(unsigned long, a) && \ |
| 54 | typecheck(unsigned long, b) && \ |
| 55 | ((long)((a) - (b)) >= 0)) |
| 56 | #define time_before_eq(a,b) time_after_eq(b,a) |
| 57 | |
| 58 | /* |
| 59 | * Calculate whether a is in the range of [b, c]. |
| 60 | */ |
| 61 | #define time_in_range(a,b,c) \ |
| 62 | (time_after_eq(a,b) && \ |
| 63 | time_before_eq(a,c)) |
| 64 | |
| 65 | /* |
| 66 | * Calculate whether a is in the range of [b, c). |
| 67 | */ |
| 68 | #define time_in_range_open(a,b,c) \ |
| 69 | (time_after_eq(a,b) && \ |
| 70 | time_before(a,c)) |
| 71 | |
Masahiro Yamada | a7b8176 | 2016-12-28 00:36:00 +0900 | [diff] [blame] | 72 | #endif /* _TIME_H */ |