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