Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * arch/s390/kernel/vtime.c |
| 3 | * Virtual cpu timer based timer functions. |
| 4 | * |
| 5 | * S390 version |
| 6 | * Copyright (C) 2004 IBM Deutschland Entwicklung GmbH, IBM Corporation |
| 7 | * Author(s): Jan Glauber <jan.glauber@de.ibm.com> |
| 8 | */ |
| 9 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/module.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/time.h> |
| 13 | #include <linux/delay.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/smp.h> |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/timex.h> |
| 18 | #include <linux/notifier.h> |
| 19 | #include <linux/kernel_stat.h> |
| 20 | #include <linux/rcupdate.h> |
| 21 | #include <linux/posix-timers.h> |
| 22 | |
| 23 | #include <asm/s390_ext.h> |
| 24 | #include <asm/timer.h> |
Heiko Carstens | 5a489b9 | 2006-10-06 16:38:35 +0200 | [diff] [blame] | 25 | #include <asm/irq_regs.h> |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 26 | #include <asm/cpu.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | static ext_int_info_t ext_int_info_timer; |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 29 | |
Heiko Carstens | 2b67fc4 | 2007-02-05 21:16:47 +0100 | [diff] [blame] | 30 | static DEFINE_PER_CPU(struct vtimer_queue, virt_cpu_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 32 | DEFINE_PER_CPU(struct s390_idle_data, s390_idle) = { |
| 33 | .lock = __SPIN_LOCK_UNLOCKED(s390_idle.lock) |
| 34 | }; |
| 35 | |
| 36 | static inline __u64 get_vtimer(void) |
| 37 | { |
| 38 | __u64 timer; |
| 39 | |
| 40 | asm volatile("STPT %0" : "=m" (timer)); |
| 41 | return timer; |
| 42 | } |
| 43 | |
| 44 | static inline void set_vtimer(__u64 expires) |
| 45 | { |
| 46 | __u64 timer; |
| 47 | |
| 48 | asm volatile (" STPT %0\n" /* Store current cpu timer value */ |
| 49 | " SPT %1" /* Set new value immediatly afterwards */ |
| 50 | : "=m" (timer) : "m" (expires) ); |
| 51 | S390_lowcore.system_timer += S390_lowcore.last_update_timer - timer; |
| 52 | S390_lowcore.last_update_timer = expires; |
| 53 | } |
| 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | /* |
| 56 | * Update process times based on virtual cpu times stored by entry.S |
| 57 | * to the lowcore fields user_timer, system_timer & steal_clock. |
| 58 | */ |
Martin Schwidefsky | aa5e97c | 2008-12-31 15:11:39 +0100 | [diff] [blame] | 59 | static void do_account_vtime(struct task_struct *tsk, int hardirq_offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | { |
Martin Schwidefsky | aa5e97c | 2008-12-31 15:11:39 +0100 | [diff] [blame] | 61 | struct thread_info *ti = task_thread_info(tsk); |
| 62 | __u64 timer, clock, user, system, steal; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
| 64 | timer = S390_lowcore.last_update_timer; |
| 65 | clock = S390_lowcore.last_update_clock; |
| 66 | asm volatile (" STPT %0\n" /* Store current cpu timer value */ |
| 67 | " STCK %1" /* Store current tod clock value */ |
| 68 | : "=m" (S390_lowcore.last_update_timer), |
| 69 | "=m" (S390_lowcore.last_update_clock) ); |
| 70 | S390_lowcore.system_timer += timer - S390_lowcore.last_update_timer; |
Martin Schwidefsky | aa5e97c | 2008-12-31 15:11:39 +0100 | [diff] [blame] | 71 | S390_lowcore.steal_timer += S390_lowcore.last_update_clock - clock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
Martin Schwidefsky | aa5e97c | 2008-12-31 15:11:39 +0100 | [diff] [blame] | 73 | user = S390_lowcore.user_timer - ti->user_timer; |
| 74 | S390_lowcore.steal_timer -= user; |
| 75 | ti->user_timer = S390_lowcore.user_timer; |
| 76 | account_user_time(tsk, user, user); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Martin Schwidefsky | aa5e97c | 2008-12-31 15:11:39 +0100 | [diff] [blame] | 78 | system = S390_lowcore.system_timer - ti->system_timer; |
| 79 | S390_lowcore.steal_timer -= system; |
| 80 | ti->system_timer = S390_lowcore.system_timer; |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 81 | account_system_time(tsk, hardirq_offset, system, system); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
Martin Schwidefsky | aa5e97c | 2008-12-31 15:11:39 +0100 | [diff] [blame] | 83 | steal = S390_lowcore.steal_timer; |
| 84 | if ((s64) steal > 0) { |
| 85 | S390_lowcore.steal_timer = 0; |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 86 | account_steal_time(steal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Martin Schwidefsky | aa5e97c | 2008-12-31 15:11:39 +0100 | [diff] [blame] | 90 | void account_vtime(struct task_struct *prev, struct task_struct *next) |
Martin Schwidefsky | 1f1c12a | 2006-01-14 13:21:03 -0800 | [diff] [blame] | 91 | { |
Martin Schwidefsky | aa5e97c | 2008-12-31 15:11:39 +0100 | [diff] [blame] | 92 | struct thread_info *ti; |
Martin Schwidefsky | 1f1c12a | 2006-01-14 13:21:03 -0800 | [diff] [blame] | 93 | |
Martin Schwidefsky | aa5e97c | 2008-12-31 15:11:39 +0100 | [diff] [blame] | 94 | do_account_vtime(prev, 0); |
| 95 | ti = task_thread_info(prev); |
| 96 | ti->user_timer = S390_lowcore.user_timer; |
| 97 | ti->system_timer = S390_lowcore.system_timer; |
| 98 | ti = task_thread_info(next); |
| 99 | S390_lowcore.user_timer = ti->user_timer; |
| 100 | S390_lowcore.system_timer = ti->system_timer; |
| 101 | } |
Martin Schwidefsky | 1f1c12a | 2006-01-14 13:21:03 -0800 | [diff] [blame] | 102 | |
Martin Schwidefsky | aa5e97c | 2008-12-31 15:11:39 +0100 | [diff] [blame] | 103 | void account_process_tick(struct task_struct *tsk, int user_tick) |
| 104 | { |
| 105 | do_account_vtime(tsk, HARDIRQ_OFFSET); |
Martin Schwidefsky | 1f1c12a | 2006-01-14 13:21:03 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /* |
| 109 | * Update process times based on virtual cpu times stored by entry.S |
| 110 | * to the lowcore fields user_timer, system_timer & steal_clock. |
| 111 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | void account_system_vtime(struct task_struct *tsk) |
| 113 | { |
Martin Schwidefsky | aa5e97c | 2008-12-31 15:11:39 +0100 | [diff] [blame] | 114 | struct thread_info *ti = task_thread_info(tsk); |
| 115 | __u64 timer, system; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
| 117 | timer = S390_lowcore.last_update_timer; |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 118 | S390_lowcore.last_update_timer = get_vtimer(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | S390_lowcore.system_timer += timer - S390_lowcore.last_update_timer; |
| 120 | |
Martin Schwidefsky | aa5e97c | 2008-12-31 15:11:39 +0100 | [diff] [blame] | 121 | system = S390_lowcore.system_timer - ti->system_timer; |
| 122 | S390_lowcore.steal_timer -= system; |
| 123 | ti->system_timer = S390_lowcore.system_timer; |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 124 | account_system_time(tsk, 0, system, system); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 126 | EXPORT_SYMBOL_GPL(account_system_vtime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 128 | void vtime_start_cpu(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | { |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 130 | struct s390_idle_data *idle = &__get_cpu_var(s390_idle); |
Martin Schwidefsky | 6f43092 | 2008-12-31 15:11:40 +0100 | [diff] [blame] | 131 | struct vtimer_queue *vq = &__get_cpu_var(virt_cpu_timer); |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 132 | __u64 idle_time, expires; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 134 | /* Account time spent with enabled wait psw loaded as idle time. */ |
| 135 | idle_time = S390_lowcore.int_clock - idle->idle_enter; |
| 136 | account_idle_time(idle_time); |
| 137 | S390_lowcore.last_update_clock = S390_lowcore.int_clock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 139 | /* Account system time spent going idle. */ |
| 140 | S390_lowcore.system_timer += S390_lowcore.last_update_timer - vq->idle; |
| 141 | S390_lowcore.last_update_timer = S390_lowcore.async_enter_timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 143 | /* Restart vtime CPU timer */ |
| 144 | if (vq->do_spt) { |
| 145 | /* Program old expire value but first save progress. */ |
| 146 | expires = vq->idle - S390_lowcore.async_enter_timer; |
| 147 | expires += get_vtimer(); |
| 148 | set_vtimer(expires); |
| 149 | } else { |
| 150 | /* Don't account the CPU timer delta while the cpu was idle. */ |
| 151 | vq->elapsed -= vq->idle - S390_lowcore.async_enter_timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 154 | spin_lock(&idle->lock); |
| 155 | idle->idle_time += idle_time; |
| 156 | idle->idle_enter = 0ULL; |
| 157 | idle->idle_count++; |
| 158 | spin_unlock(&idle->lock); |
| 159 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 161 | void vtime_stop_cpu(void) |
| 162 | { |
| 163 | struct s390_idle_data *idle = &__get_cpu_var(s390_idle); |
| 164 | struct vtimer_queue *vq = &__get_cpu_var(virt_cpu_timer); |
| 165 | psw_t psw; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 167 | /* Wait for external, I/O or machine check interrupt. */ |
| 168 | psw.mask = psw_kernel_bits | PSW_MASK_WAIT | PSW_MASK_IO | PSW_MASK_EXT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 170 | /* Check if the CPU timer needs to be reprogrammed. */ |
| 171 | if (vq->do_spt) { |
| 172 | __u64 vmax = VTIMER_MAX_SLICE; |
| 173 | /* |
| 174 | * The inline assembly is equivalent to |
| 175 | * vq->idle = get_cpu_timer(); |
| 176 | * set_cpu_timer(VTIMER_MAX_SLICE); |
| 177 | * idle->idle_enter = get_clock(); |
| 178 | * __load_psw_mask(psw_kernel_bits | PSW_MASK_WAIT | |
| 179 | * PSW_MASK_IO | PSW_MASK_EXT); |
| 180 | * The difference is that the inline assembly makes sure that |
| 181 | * the last three instruction are stpt, stck and lpsw in that |
| 182 | * order. This is done to increase the precision. |
| 183 | */ |
| 184 | asm volatile( |
| 185 | #ifndef CONFIG_64BIT |
| 186 | " basr 1,0\n" |
| 187 | "0: ahi 1,1f-0b\n" |
| 188 | " st 1,4(%2)\n" |
| 189 | #else /* CONFIG_64BIT */ |
| 190 | " larl 1,1f\n" |
| 191 | " stg 1,8(%2)\n" |
| 192 | #endif /* CONFIG_64BIT */ |
| 193 | " stpt 0(%4)\n" |
| 194 | " spt 0(%5)\n" |
| 195 | " stck 0(%3)\n" |
| 196 | #ifndef CONFIG_64BIT |
| 197 | " lpsw 0(%2)\n" |
| 198 | #else /* CONFIG_64BIT */ |
| 199 | " lpswe 0(%2)\n" |
| 200 | #endif /* CONFIG_64BIT */ |
| 201 | "1:" |
| 202 | : "=m" (idle->idle_enter), "=m" (vq->idle) |
| 203 | : "a" (&psw), "a" (&idle->idle_enter), |
| 204 | "a" (&vq->idle), "a" (&vmax), "m" (vmax), "m" (psw) |
| 205 | : "memory", "cc", "1"); |
| 206 | } else { |
| 207 | /* |
| 208 | * The inline assembly is equivalent to |
| 209 | * vq->idle = get_cpu_timer(); |
| 210 | * idle->idle_enter = get_clock(); |
| 211 | * __load_psw_mask(psw_kernel_bits | PSW_MASK_WAIT | |
| 212 | * PSW_MASK_IO | PSW_MASK_EXT); |
| 213 | * The difference is that the inline assembly makes sure that |
| 214 | * the last three instruction are stpt, stck and lpsw in that |
| 215 | * order. This is done to increase the precision. |
| 216 | */ |
| 217 | asm volatile( |
| 218 | #ifndef CONFIG_64BIT |
| 219 | " basr 1,0\n" |
| 220 | "0: ahi 1,1f-0b\n" |
| 221 | " st 1,4(%2)\n" |
| 222 | #else /* CONFIG_64BIT */ |
| 223 | " larl 1,1f\n" |
| 224 | " stg 1,8(%2)\n" |
| 225 | #endif /* CONFIG_64BIT */ |
| 226 | " stpt 0(%4)\n" |
| 227 | " stck 0(%3)\n" |
| 228 | #ifndef CONFIG_64BIT |
| 229 | " lpsw 0(%2)\n" |
| 230 | #else /* CONFIG_64BIT */ |
| 231 | " lpswe 0(%2)\n" |
| 232 | #endif /* CONFIG_64BIT */ |
| 233 | "1:" |
| 234 | : "=m" (idle->idle_enter), "=m" (vq->idle) |
| 235 | : "a" (&psw), "a" (&idle->idle_enter), |
| 236 | "a" (&vq->idle), "m" (psw) |
| 237 | : "memory", "cc", "1"); |
| 238 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /* |
| 242 | * Sorted add to a list. List is linear searched until first bigger |
| 243 | * element is found. |
| 244 | */ |
| 245 | static void list_add_sorted(struct vtimer_list *timer, struct list_head *head) |
| 246 | { |
| 247 | struct vtimer_list *event; |
| 248 | |
| 249 | list_for_each_entry(event, head, entry) { |
| 250 | if (event->expires > timer->expires) { |
| 251 | list_add_tail(&timer->entry, &event->entry); |
| 252 | return; |
| 253 | } |
| 254 | } |
| 255 | list_add_tail(&timer->entry, head); |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * Do the callback functions of expired vtimer events. |
| 260 | * Called from within the interrupt handler. |
| 261 | */ |
Heiko Carstens | 9d0a57c | 2006-10-11 15:31:26 +0200 | [diff] [blame] | 262 | static void do_callbacks(struct list_head *cb_list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | { |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 264 | struct vtimer_queue *vq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | struct vtimer_list *event, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
| 267 | if (list_empty(cb_list)) |
| 268 | return; |
| 269 | |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 270 | vq = &__get_cpu_var(virt_cpu_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
| 272 | list_for_each_entry_safe(event, tmp, cb_list, entry) { |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 273 | list_del_init(&event->entry); |
| 274 | (event->function)(event->data); |
| 275 | if (event->interval) { |
| 276 | /* Recharge interval timer */ |
| 277 | event->expires = event->interval + vq->elapsed; |
| 278 | spin_lock(&vq->lock); |
| 279 | list_add_sorted(event, &vq->list); |
| 280 | spin_unlock(&vq->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | * Handler for the virtual CPU timer. |
| 287 | */ |
Heiko Carstens | 5a489b9 | 2006-10-06 16:38:35 +0200 | [diff] [blame] | 288 | static void do_cpu_timer_interrupt(__u16 error_code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | { |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 290 | struct vtimer_queue *vq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | struct vtimer_list *event, *tmp; |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 292 | struct list_head cb_list; /* the callback queue */ |
| 293 | __u64 elapsed, next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | |
| 295 | INIT_LIST_HEAD(&cb_list); |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 296 | vq = &__get_cpu_var(virt_cpu_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
| 298 | /* walk timer list, fire all expired events */ |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 299 | spin_lock(&vq->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 301 | elapsed = vq->elapsed + (vq->timer - S390_lowcore.async_enter_timer); |
| 302 | BUG_ON((s64) elapsed < 0); |
| 303 | vq->elapsed = 0; |
| 304 | list_for_each_entry_safe(event, tmp, &vq->list, entry) { |
| 305 | if (event->expires < elapsed) |
| 306 | /* move expired timer to the callback queue */ |
| 307 | list_move_tail(&event->entry, &cb_list); |
| 308 | else |
| 309 | event->expires -= elapsed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | } |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 311 | spin_unlock(&vq->lock); |
| 312 | |
| 313 | vq->do_spt = list_empty(&cb_list); |
Heiko Carstens | 9d0a57c | 2006-10-11 15:31:26 +0200 | [diff] [blame] | 314 | do_callbacks(&cb_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | |
| 316 | /* next event is first in list */ |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 317 | next = VTIMER_MAX_SLICE; |
| 318 | spin_lock(&vq->lock); |
| 319 | if (!list_empty(&vq->list)) { |
| 320 | event = list_first_entry(&vq->list, struct vtimer_list, entry); |
| 321 | next = event->expires; |
| 322 | } else |
| 323 | vq->do_spt = 0; |
| 324 | spin_unlock(&vq->lock); |
| 325 | /* |
| 326 | * To improve precision add the time spent by the |
| 327 | * interrupt handler to the elapsed time. |
| 328 | * Note: CPU timer counts down and we got an interrupt, |
| 329 | * the current content is negative |
| 330 | */ |
| 331 | elapsed = S390_lowcore.async_enter_timer - get_vtimer(); |
| 332 | set_vtimer(next - elapsed); |
| 333 | vq->timer = next - elapsed; |
| 334 | vq->elapsed = elapsed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | void init_virt_timer(struct vtimer_list *timer) |
| 338 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | timer->function = NULL; |
| 340 | INIT_LIST_HEAD(&timer->entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | } |
| 342 | EXPORT_SYMBOL(init_virt_timer); |
| 343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | static inline int vtimer_pending(struct vtimer_list *timer) |
| 345 | { |
| 346 | return (!list_empty(&timer->entry)); |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | * this function should only run on the specified CPU |
| 351 | */ |
| 352 | static void internal_add_vtimer(struct vtimer_list *timer) |
| 353 | { |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 354 | struct vtimer_queue *vq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | unsigned long flags; |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 356 | __u64 left, expires; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 358 | vq = &per_cpu(virt_cpu_timer, timer->cpu); |
| 359 | spin_lock_irqsave(&vq->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | |
Martin Schwidefsky | ca366a3 | 2008-07-14 09:59:23 +0200 | [diff] [blame] | 361 | BUG_ON(timer->cpu != smp_processor_id()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 363 | if (list_empty(&vq->list)) { |
| 364 | /* First timer on this cpu, just program it. */ |
| 365 | list_add(&timer->entry, &vq->list); |
| 366 | set_vtimer(timer->expires); |
| 367 | vq->timer = timer->expires; |
| 368 | vq->elapsed = 0; |
| 369 | } else { |
| 370 | /* Check progress of old timers. */ |
| 371 | expires = timer->expires; |
| 372 | left = get_vtimer(); |
| 373 | if (likely((s64) expires < (s64) left)) { |
| 374 | /* The new timer expires before the current timer. */ |
| 375 | set_vtimer(expires); |
| 376 | vq->elapsed += vq->timer - left; |
| 377 | vq->timer = expires; |
| 378 | } else { |
| 379 | vq->elapsed += vq->timer - left; |
| 380 | vq->timer = left; |
| 381 | } |
| 382 | /* Insert new timer into per cpu list. */ |
| 383 | timer->expires += vq->elapsed; |
| 384 | list_add_sorted(timer, &vq->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 387 | spin_unlock_irqrestore(&vq->lock, flags); |
Andreas Mohr | d6e05ed | 2006-06-26 18:35:02 +0200 | [diff] [blame] | 388 | /* release CPU acquired in prepare_vtimer or mod_virt_timer() */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | put_cpu(); |
| 390 | } |
| 391 | |
Martin Schwidefsky | ca366a3 | 2008-07-14 09:59:23 +0200 | [diff] [blame] | 392 | static inline void prepare_vtimer(struct vtimer_list *timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | { |
Martin Schwidefsky | ca366a3 | 2008-07-14 09:59:23 +0200 | [diff] [blame] | 394 | BUG_ON(!timer->function); |
| 395 | BUG_ON(!timer->expires || timer->expires > VTIMER_MAX_SLICE); |
| 396 | BUG_ON(vtimer_pending(timer)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | timer->cpu = get_cpu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | /* |
| 401 | * add_virt_timer - add an oneshot virtual CPU timer |
| 402 | */ |
| 403 | void add_virt_timer(void *new) |
| 404 | { |
| 405 | struct vtimer_list *timer; |
| 406 | |
| 407 | timer = (struct vtimer_list *)new; |
Martin Schwidefsky | ca366a3 | 2008-07-14 09:59:23 +0200 | [diff] [blame] | 408 | prepare_vtimer(timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | timer->interval = 0; |
| 410 | internal_add_vtimer(timer); |
| 411 | } |
| 412 | EXPORT_SYMBOL(add_virt_timer); |
| 413 | |
| 414 | /* |
| 415 | * add_virt_timer_int - add an interval virtual CPU timer |
| 416 | */ |
| 417 | void add_virt_timer_periodic(void *new) |
| 418 | { |
| 419 | struct vtimer_list *timer; |
| 420 | |
| 421 | timer = (struct vtimer_list *)new; |
Martin Schwidefsky | ca366a3 | 2008-07-14 09:59:23 +0200 | [diff] [blame] | 422 | prepare_vtimer(timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | timer->interval = timer->expires; |
| 424 | internal_add_vtimer(timer); |
| 425 | } |
| 426 | EXPORT_SYMBOL(add_virt_timer_periodic); |
| 427 | |
| 428 | /* |
| 429 | * If we change a pending timer the function must be called on the CPU |
Heiko Carstens | 3bb447f | 2007-07-27 12:29:08 +0200 | [diff] [blame] | 430 | * where the timer is running on, e.g. by smp_call_function_single() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | * |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 432 | * The original mod_timer adds the timer if it is not pending. For |
| 433 | * compatibility we do the same. The timer will be added on the current |
| 434 | * CPU as a oneshot timer. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | * |
| 436 | * returns whether it has modified a pending timer (1) or not (0) |
| 437 | */ |
| 438 | int mod_virt_timer(struct vtimer_list *timer, __u64 expires) |
| 439 | { |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 440 | struct vtimer_queue *vq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | unsigned long flags; |
| 442 | int cpu; |
| 443 | |
Martin Schwidefsky | ca366a3 | 2008-07-14 09:59:23 +0200 | [diff] [blame] | 444 | BUG_ON(!timer->function); |
| 445 | BUG_ON(!expires || expires > VTIMER_MAX_SLICE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | |
| 447 | /* |
| 448 | * This is a common optimization triggered by the |
| 449 | * networking code - if the timer is re-modified |
| 450 | * to be the same thing then just return: |
| 451 | */ |
| 452 | if (timer->expires == expires && vtimer_pending(timer)) |
| 453 | return 1; |
| 454 | |
| 455 | cpu = get_cpu(); |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 456 | vq = &per_cpu(virt_cpu_timer, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
Martin Schwidefsky | ca366a3 | 2008-07-14 09:59:23 +0200 | [diff] [blame] | 458 | /* check if we run on the right CPU */ |
| 459 | BUG_ON(timer->cpu != cpu); |
| 460 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | /* disable interrupts before test if timer is pending */ |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 462 | spin_lock_irqsave(&vq->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | |
| 464 | /* if timer isn't pending add it on the current CPU */ |
| 465 | if (!vtimer_pending(timer)) { |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 466 | spin_unlock_irqrestore(&vq->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | /* we do not activate an interval timer with mod_virt_timer */ |
| 468 | timer->interval = 0; |
| 469 | timer->expires = expires; |
| 470 | timer->cpu = cpu; |
| 471 | internal_add_vtimer(timer); |
| 472 | return 0; |
| 473 | } |
| 474 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | list_del_init(&timer->entry); |
| 476 | timer->expires = expires; |
| 477 | |
| 478 | /* also change the interval if we have an interval timer */ |
| 479 | if (timer->interval) |
| 480 | timer->interval = expires; |
| 481 | |
| 482 | /* the timer can't expire anymore so we can release the lock */ |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 483 | spin_unlock_irqrestore(&vq->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | internal_add_vtimer(timer); |
| 485 | return 1; |
| 486 | } |
| 487 | EXPORT_SYMBOL(mod_virt_timer); |
| 488 | |
| 489 | /* |
| 490 | * delete a virtual timer |
| 491 | * |
| 492 | * returns whether the deleted timer was pending (1) or not (0) |
| 493 | */ |
| 494 | int del_virt_timer(struct vtimer_list *timer) |
| 495 | { |
| 496 | unsigned long flags; |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 497 | struct vtimer_queue *vq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | /* check if timer is pending */ |
| 500 | if (!vtimer_pending(timer)) |
| 501 | return 0; |
| 502 | |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 503 | vq = &per_cpu(virt_cpu_timer, timer->cpu); |
| 504 | spin_lock_irqsave(&vq->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | |
| 506 | /* we don't interrupt a running timer, just let it expire! */ |
| 507 | list_del_init(&timer->entry); |
| 508 | |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 509 | spin_unlock_irqrestore(&vq->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | return 1; |
| 511 | } |
| 512 | EXPORT_SYMBOL(del_virt_timer); |
| 513 | |
| 514 | /* |
| 515 | * Start the virtual CPU timer on the current CPU. |
| 516 | */ |
| 517 | void init_cpu_vtimer(void) |
| 518 | { |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 519 | struct vtimer_queue *vq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
| 521 | /* kick the virtual timer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | asm volatile ("STCK %0" : "=m" (S390_lowcore.last_update_clock)); |
Martin Schwidefsky | 9cfb9b3 | 2008-12-31 15:11:41 +0100 | [diff] [blame^] | 523 | asm volatile ("STPT %0" : "=m" (S390_lowcore.last_update_timer)); |
| 524 | |
| 525 | /* initialize per cpu vtimer structure */ |
| 526 | vq = &__get_cpu_var(virt_cpu_timer); |
| 527 | INIT_LIST_HEAD(&vq->list); |
| 528 | spin_lock_init(&vq->lock); |
Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 529 | |
| 530 | /* enable cpu timer interrupts */ |
| 531 | __ctl_set_bit(0,10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | } |
| 533 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | void __init vtime_init(void) |
| 535 | { |
| 536 | /* request the cpu timer external interrupt */ |
| 537 | if (register_early_external_interrupt(0x1005, do_cpu_timer_interrupt, |
| 538 | &ext_int_info_timer) != 0) |
| 539 | panic("Couldn't request external interrupt 0x1005"); |
| 540 | |
Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 541 | /* Enable cpu timer interrupts on the boot cpu. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | init_cpu_vtimer(); |
| 543 | } |
| 544 | |