Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * include/asm-s390/cputime.h |
| 3 | * |
| 4 | * (C) Copyright IBM Corp. 2004 |
| 5 | * |
| 6 | * Author: Martin Schwidefsky <schwidefsky@de.ibm.com> |
| 7 | */ |
| 8 | |
| 9 | #ifndef _S390_CPUTIME_H |
| 10 | #define _S390_CPUTIME_H |
| 11 | |
| 12 | #include <asm/div64.h> |
| 13 | |
Martin Schwidefsky | aa5e97c | 2008-12-31 15:11:39 +0100 | [diff] [blame] | 14 | /* We want to use full resolution of the CPU timer: 2**-12 micro-seconds. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
| 16 | typedef unsigned long long cputime_t; |
| 17 | typedef unsigned long long cputime64_t; |
| 18 | |
| 19 | #ifndef __s390x__ |
| 20 | |
| 21 | static inline unsigned int |
| 22 | __div(unsigned long long n, unsigned int base) |
| 23 | { |
| 24 | register_pair rp; |
| 25 | |
| 26 | rp.pair = n >> 1; |
| 27 | asm ("dr %0,%1" : "+d" (rp) : "d" (base >> 1)); |
| 28 | return rp.subreg.odd; |
| 29 | } |
| 30 | |
| 31 | #else /* __s390x__ */ |
| 32 | |
| 33 | static inline unsigned int |
| 34 | __div(unsigned long long n, unsigned int base) |
| 35 | { |
| 36 | return n / base; |
| 37 | } |
| 38 | |
| 39 | #endif /* __s390x__ */ |
| 40 | |
| 41 | #define cputime_zero (0ULL) |
| 42 | #define cputime_max ((~0UL >> 1) - 1) |
| 43 | #define cputime_add(__a, __b) ((__a) + (__b)) |
| 44 | #define cputime_sub(__a, __b) ((__a) - (__b)) |
| 45 | #define cputime_div(__a, __n) ({ \ |
| 46 | unsigned long long __div = (__a); \ |
| 47 | do_div(__div,__n); \ |
| 48 | __div; \ |
| 49 | }) |
| 50 | #define cputime_halve(__a) ((__a) >> 1) |
| 51 | #define cputime_eq(__a, __b) ((__a) == (__b)) |
| 52 | #define cputime_gt(__a, __b) ((__a) > (__b)) |
| 53 | #define cputime_ge(__a, __b) ((__a) >= (__b)) |
| 54 | #define cputime_lt(__a, __b) ((__a) < (__b)) |
| 55 | #define cputime_le(__a, __b) ((__a) <= (__b)) |
Martin Schwidefsky | aa5e97c | 2008-12-31 15:11:39 +0100 | [diff] [blame] | 56 | #define cputime_to_jiffies(__ct) (__div((__ct), 4096000000ULL / HZ)) |
Michael Neuling | 06b8e87 | 2008-02-06 01:36:12 -0800 | [diff] [blame] | 57 | #define cputime_to_scaled(__ct) (__ct) |
Martin Schwidefsky | aa5e97c | 2008-12-31 15:11:39 +0100 | [diff] [blame] | 58 | #define jiffies_to_cputime(__hz) ((cputime_t)(__hz) * (4096000000ULL / HZ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
| 60 | #define cputime64_zero (0ULL) |
| 61 | #define cputime64_add(__a, __b) ((__a) + (__b)) |
| 62 | #define cputime_to_cputime64(__ct) (__ct) |
| 63 | |
| 64 | static inline u64 |
| 65 | cputime64_to_jiffies64(cputime64_t cputime) |
| 66 | { |
Martin Schwidefsky | aa5e97c | 2008-12-31 15:11:39 +0100 | [diff] [blame] | 67 | do_div(cputime, 4096000000ULL / HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | return cputime; |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | * Convert cputime to milliseconds and back. |
| 73 | */ |
| 74 | static inline unsigned int |
| 75 | cputime_to_msecs(const cputime_t cputime) |
| 76 | { |
Martin Schwidefsky | aa5e97c | 2008-12-31 15:11:39 +0100 | [diff] [blame] | 77 | return __div(cputime, 4096000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static inline cputime_t |
| 81 | msecs_to_cputime(const unsigned int m) |
| 82 | { |
Martin Schwidefsky | aa5e97c | 2008-12-31 15:11:39 +0100 | [diff] [blame] | 83 | return (cputime_t) m * 4096000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Convert cputime to milliseconds and back. |
| 88 | */ |
| 89 | static inline unsigned int |
| 90 | cputime_to_secs(const cputime_t cputime) |
| 91 | { |
Martin Schwidefsky | aa5e97c | 2008-12-31 15:11:39 +0100 | [diff] [blame] | 92 | return __div(cputime, 2048000000) >> 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static inline cputime_t |
| 96 | secs_to_cputime(const unsigned int s) |
| 97 | { |
Martin Schwidefsky | aa5e97c | 2008-12-31 15:11:39 +0100 | [diff] [blame] | 98 | return (cputime_t) s * 4096000000ULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /* |
| 102 | * Convert cputime to timespec and back. |
| 103 | */ |
| 104 | static inline cputime_t |
| 105 | timespec_to_cputime(const struct timespec *value) |
| 106 | { |
Martin Schwidefsky | aa5e97c | 2008-12-31 15:11:39 +0100 | [diff] [blame] | 107 | return value->tv_nsec * 4096 / 1000 + (u64) value->tv_sec * 4096000000ULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static inline void |
| 111 | cputime_to_timespec(const cputime_t cputime, struct timespec *value) |
| 112 | { |
| 113 | #ifndef __s390x__ |
| 114 | register_pair rp; |
| 115 | |
| 116 | rp.pair = cputime >> 1; |
Martin Schwidefsky | aa5e97c | 2008-12-31 15:11:39 +0100 | [diff] [blame] | 117 | asm ("dr %0,%1" : "+d" (rp) : "d" (2048000000UL)); |
| 118 | value->tv_nsec = rp.subreg.even * 1000 / 4096; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | value->tv_sec = rp.subreg.odd; |
| 120 | #else |
Martin Schwidefsky | aa5e97c | 2008-12-31 15:11:39 +0100 | [diff] [blame] | 121 | value->tv_nsec = (cputime % 4096000000ULL) * 1000 / 4096; |
| 122 | value->tv_sec = cputime / 4096000000ULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | #endif |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Convert cputime to timeval and back. |
| 128 | * Since cputime and timeval have the same resolution (microseconds) |
| 129 | * this is easy. |
| 130 | */ |
| 131 | static inline cputime_t |
| 132 | timeval_to_cputime(const struct timeval *value) |
| 133 | { |
Martin Schwidefsky | aa5e97c | 2008-12-31 15:11:39 +0100 | [diff] [blame] | 134 | return value->tv_usec * 4096 + (u64) value->tv_sec * 4096000000ULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static inline void |
| 138 | cputime_to_timeval(const cputime_t cputime, struct timeval *value) |
| 139 | { |
| 140 | #ifndef __s390x__ |
| 141 | register_pair rp; |
| 142 | |
| 143 | rp.pair = cputime >> 1; |
Martin Schwidefsky | aa5e97c | 2008-12-31 15:11:39 +0100 | [diff] [blame] | 144 | asm ("dr %0,%1" : "+d" (rp) : "d" (2048000000UL)); |
| 145 | value->tv_usec = rp.subreg.even / 4096; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | value->tv_sec = rp.subreg.odd; |
| 147 | #else |
Christian Borntraeger | d5cd034 | 2009-02-19 15:19:00 +0100 | [diff] [blame] | 148 | value->tv_usec = (cputime % 4096000000ULL) / 4096; |
Martin Schwidefsky | aa5e97c | 2008-12-31 15:11:39 +0100 | [diff] [blame] | 149 | value->tv_sec = cputime / 4096000000ULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | #endif |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * Convert cputime to clock and back. |
| 155 | */ |
| 156 | static inline clock_t |
| 157 | cputime_to_clock_t(cputime_t cputime) |
| 158 | { |
Martin Schwidefsky | aa5e97c | 2008-12-31 15:11:39 +0100 | [diff] [blame] | 159 | return __div(cputime, 4096000000ULL / USER_HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static inline cputime_t |
| 163 | clock_t_to_cputime(unsigned long x) |
| 164 | { |
Martin Schwidefsky | aa5e97c | 2008-12-31 15:11:39 +0100 | [diff] [blame] | 165 | return (cputime_t) x * (4096000000ULL / USER_HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /* |
| 169 | * Convert cputime64 to clock. |
| 170 | */ |
| 171 | static inline clock_t |
| 172 | cputime64_to_clock_t(cputime64_t cputime) |
| 173 | { |
Martin Schwidefsky | aa5e97c | 2008-12-31 15:11:39 +0100 | [diff] [blame] | 174 | return __div(cputime, 4096000000ULL / USER_HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Martin Schwidefsky | e1c8053 | 2009-04-23 13:58:08 +0200 | [diff] [blame] | 177 | cputime64_t s390_get_idle_time(int cpu); |
| 178 | |
| 179 | #define arch_idle_time(cpu) s390_get_idle_time(cpu) |
| 180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | #endif /* _S390_CPUTIME_H */ |