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