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