1 /* 2 * Virtual cpu timer based timer functions. 3 * 4 * Copyright IBM Corp. 2004, 2012 5 * Author(s): Jan Glauber <jan.glauber@de.ibm.com> 6 */ 7 8 #include <linux/kernel_stat.h> 9 #include <linux/export.h> 10 #include <linux/kernel.h> 11 #include <linux/timex.h> 12 #include <linux/types.h> 13 #include <linux/time.h> 14 15 #include <asm/cputime.h> 16 #include <asm/vtimer.h> 17 #include <asm/vtime.h> 18 #include <asm/cpu_mf.h> 19 #include <asm/smp.h> 20 21 #include "entry.h" 22 23 static void virt_timer_expire(void); 24 25 static LIST_HEAD(virt_timer_list); 26 static DEFINE_SPINLOCK(virt_timer_lock); 27 static atomic64_t virt_timer_current; 28 static atomic64_t virt_timer_elapsed; 29 30 DEFINE_PER_CPU(u64, mt_cycles[8]); 31 static DEFINE_PER_CPU(u64, mt_scaling_mult) = { 1 }; 32 static DEFINE_PER_CPU(u64, mt_scaling_div) = { 1 }; 33 static DEFINE_PER_CPU(u64, mt_scaling_jiffies); 34 35 static inline u64 get_vtimer(void) 36 { 37 u64 timer; 38 39 asm volatile("stpt %0" : "=m" (timer)); 40 return timer; 41 } 42 43 static inline void set_vtimer(u64 expires) 44 { 45 u64 timer; 46 47 asm volatile( 48 " stpt %0\n" /* Store current cpu timer value */ 49 " spt %1" /* Set new value imm. 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 55 static inline int virt_timer_forward(u64 elapsed) 56 { 57 BUG_ON(!irqs_disabled()); 58 59 if (list_empty(&virt_timer_list)) 60 return 0; 61 elapsed = atomic64_add_return(elapsed, &virt_timer_elapsed); 62 return elapsed >= atomic64_read(&virt_timer_current); 63 } 64 65 static void update_mt_scaling(void) 66 { 67 u64 cycles_new[8], *cycles_old; 68 u64 delta, fac, mult, div; 69 int i; 70 71 stcctm5(smp_cpu_mtid + 1, cycles_new); 72 cycles_old = this_cpu_ptr(mt_cycles); 73 fac = 1; 74 mult = div = 0; 75 for (i = 0; i <= smp_cpu_mtid; i++) { 76 delta = cycles_new[i] - cycles_old[i]; 77 div += delta; 78 mult *= i + 1; 79 mult += delta * fac; 80 fac *= i + 1; 81 } 82 div *= fac; 83 if (div > 0) { 84 /* Update scaling factor */ 85 __this_cpu_write(mt_scaling_mult, mult); 86 __this_cpu_write(mt_scaling_div, div); 87 memcpy(cycles_old, cycles_new, 88 sizeof(u64) * (smp_cpu_mtid + 1)); 89 } 90 __this_cpu_write(mt_scaling_jiffies, jiffies_64); 91 } 92 93 /* 94 * Update process times based on virtual cpu times stored by entry.S 95 * to the lowcore fields user_timer, system_timer & steal_clock. 96 */ 97 static int do_account_vtime(struct task_struct *tsk, int hardirq_offset) 98 { 99 struct thread_info *ti = task_thread_info(tsk); 100 u64 timer, clock, user, system, steal; 101 u64 user_scaled, system_scaled; 102 103 timer = S390_lowcore.last_update_timer; 104 clock = S390_lowcore.last_update_clock; 105 asm volatile( 106 " stpt %0\n" /* Store current cpu timer value */ 107 #ifdef CONFIG_HAVE_MARCH_Z9_109_FEATURES 108 " stckf %1" /* Store current tod clock value */ 109 #else 110 " stck %1" /* Store current tod clock value */ 111 #endif 112 : "=m" (S390_lowcore.last_update_timer), 113 "=m" (S390_lowcore.last_update_clock)); 114 S390_lowcore.system_timer += timer - S390_lowcore.last_update_timer; 115 S390_lowcore.steal_timer += S390_lowcore.last_update_clock - clock; 116 117 /* Update MT utilization calculation */ 118 if (smp_cpu_mtid && 119 time_after64(jiffies_64, this_cpu_read(mt_scaling_jiffies))) 120 update_mt_scaling(); 121 122 user = S390_lowcore.user_timer - ti->user_timer; 123 S390_lowcore.steal_timer -= user; 124 ti->user_timer = S390_lowcore.user_timer; 125 126 system = S390_lowcore.system_timer - ti->system_timer; 127 S390_lowcore.steal_timer -= system; 128 ti->system_timer = S390_lowcore.system_timer; 129 130 user_scaled = user; 131 system_scaled = system; 132 /* Do MT utilization scaling */ 133 if (smp_cpu_mtid) { 134 u64 mult = __this_cpu_read(mt_scaling_mult); 135 u64 div = __this_cpu_read(mt_scaling_div); 136 137 user_scaled = (user_scaled * mult) / div; 138 system_scaled = (system_scaled * mult) / div; 139 } 140 account_user_time(tsk, user); 141 tsk->utimescaled += user_scaled; 142 account_system_time(tsk, hardirq_offset, system); 143 tsk->stimescaled += system_scaled; 144 145 steal = S390_lowcore.steal_timer; 146 if ((s64) steal > 0) { 147 S390_lowcore.steal_timer = 0; 148 account_steal_time(steal); 149 } 150 151 return virt_timer_forward(user + system); 152 } 153 154 void vtime_task_switch(struct task_struct *prev) 155 { 156 struct thread_info *ti; 157 158 do_account_vtime(prev, 0); 159 ti = task_thread_info(prev); 160 ti->user_timer = S390_lowcore.user_timer; 161 ti->system_timer = S390_lowcore.system_timer; 162 ti = task_thread_info(current); 163 S390_lowcore.user_timer = ti->user_timer; 164 S390_lowcore.system_timer = ti->system_timer; 165 } 166 167 /* 168 * In s390, accounting pending user time also implies 169 * accounting system time in order to correctly compute 170 * the stolen time accounting. 171 */ 172 void vtime_account_user(struct task_struct *tsk) 173 { 174 if (do_account_vtime(tsk, HARDIRQ_OFFSET)) 175 virt_timer_expire(); 176 } 177 178 /* 179 * Update process times based on virtual cpu times stored by entry.S 180 * to the lowcore fields user_timer, system_timer & steal_clock. 181 */ 182 void vtime_account_irq_enter(struct task_struct *tsk) 183 { 184 struct thread_info *ti = task_thread_info(tsk); 185 u64 timer, system, system_scaled; 186 187 timer = S390_lowcore.last_update_timer; 188 S390_lowcore.last_update_timer = get_vtimer(); 189 S390_lowcore.system_timer += timer - S390_lowcore.last_update_timer; 190 191 /* Update MT utilization calculation */ 192 if (smp_cpu_mtid && 193 time_after64(jiffies_64, this_cpu_read(mt_scaling_jiffies))) 194 update_mt_scaling(); 195 196 system = S390_lowcore.system_timer - ti->system_timer; 197 S390_lowcore.steal_timer -= system; 198 ti->system_timer = S390_lowcore.system_timer; 199 system_scaled = system; 200 /* Do MT utilization scaling */ 201 if (smp_cpu_mtid) { 202 u64 mult = __this_cpu_read(mt_scaling_mult); 203 u64 div = __this_cpu_read(mt_scaling_div); 204 205 system_scaled = (system_scaled * mult) / div; 206 } 207 account_system_time(tsk, 0, system); 208 tsk->stimescaled += system_scaled; 209 210 virt_timer_forward(system); 211 } 212 EXPORT_SYMBOL_GPL(vtime_account_irq_enter); 213 214 void vtime_account_system(struct task_struct *tsk) 215 __attribute__((alias("vtime_account_irq_enter"))); 216 EXPORT_SYMBOL_GPL(vtime_account_system); 217 218 /* 219 * Sorted add to a list. List is linear searched until first bigger 220 * element is found. 221 */ 222 static void list_add_sorted(struct vtimer_list *timer, struct list_head *head) 223 { 224 struct vtimer_list *tmp; 225 226 list_for_each_entry(tmp, head, entry) { 227 if (tmp->expires > timer->expires) { 228 list_add_tail(&timer->entry, &tmp->entry); 229 return; 230 } 231 } 232 list_add_tail(&timer->entry, head); 233 } 234 235 /* 236 * Handler for expired virtual CPU timer. 237 */ 238 static void virt_timer_expire(void) 239 { 240 struct vtimer_list *timer, *tmp; 241 unsigned long elapsed; 242 LIST_HEAD(cb_list); 243 244 /* walk timer list, fire all expired timers */ 245 spin_lock(&virt_timer_lock); 246 elapsed = atomic64_read(&virt_timer_elapsed); 247 list_for_each_entry_safe(timer, tmp, &virt_timer_list, entry) { 248 if (timer->expires < elapsed) 249 /* move expired timer to the callback queue */ 250 list_move_tail(&timer->entry, &cb_list); 251 else 252 timer->expires -= elapsed; 253 } 254 if (!list_empty(&virt_timer_list)) { 255 timer = list_first_entry(&virt_timer_list, 256 struct vtimer_list, entry); 257 atomic64_set(&virt_timer_current, timer->expires); 258 } 259 atomic64_sub(elapsed, &virt_timer_elapsed); 260 spin_unlock(&virt_timer_lock); 261 262 /* Do callbacks and recharge periodic timers */ 263 list_for_each_entry_safe(timer, tmp, &cb_list, entry) { 264 list_del_init(&timer->entry); 265 timer->function(timer->data); 266 if (timer->interval) { 267 /* Recharge interval timer */ 268 timer->expires = timer->interval + 269 atomic64_read(&virt_timer_elapsed); 270 spin_lock(&virt_timer_lock); 271 list_add_sorted(timer, &virt_timer_list); 272 spin_unlock(&virt_timer_lock); 273 } 274 } 275 } 276 277 void init_virt_timer(struct vtimer_list *timer) 278 { 279 timer->function = NULL; 280 INIT_LIST_HEAD(&timer->entry); 281 } 282 EXPORT_SYMBOL(init_virt_timer); 283 284 static inline int vtimer_pending(struct vtimer_list *timer) 285 { 286 return !list_empty(&timer->entry); 287 } 288 289 static void internal_add_vtimer(struct vtimer_list *timer) 290 { 291 if (list_empty(&virt_timer_list)) { 292 /* First timer, just program it. */ 293 atomic64_set(&virt_timer_current, timer->expires); 294 atomic64_set(&virt_timer_elapsed, 0); 295 list_add(&timer->entry, &virt_timer_list); 296 } else { 297 /* Update timer against current base. */ 298 timer->expires += atomic64_read(&virt_timer_elapsed); 299 if (likely((s64) timer->expires < 300 (s64) atomic64_read(&virt_timer_current))) 301 /* The new timer expires before the current timer. */ 302 atomic64_set(&virt_timer_current, timer->expires); 303 /* Insert new timer into the list. */ 304 list_add_sorted(timer, &virt_timer_list); 305 } 306 } 307 308 static void __add_vtimer(struct vtimer_list *timer, int periodic) 309 { 310 unsigned long flags; 311 312 timer->interval = periodic ? timer->expires : 0; 313 spin_lock_irqsave(&virt_timer_lock, flags); 314 internal_add_vtimer(timer); 315 spin_unlock_irqrestore(&virt_timer_lock, flags); 316 } 317 318 /* 319 * add_virt_timer - add an oneshot virtual CPU timer 320 */ 321 void add_virt_timer(struct vtimer_list *timer) 322 { 323 __add_vtimer(timer, 0); 324 } 325 EXPORT_SYMBOL(add_virt_timer); 326 327 /* 328 * add_virt_timer_int - add an interval virtual CPU timer 329 */ 330 void add_virt_timer_periodic(struct vtimer_list *timer) 331 { 332 __add_vtimer(timer, 1); 333 } 334 EXPORT_SYMBOL(add_virt_timer_periodic); 335 336 static int __mod_vtimer(struct vtimer_list *timer, u64 expires, int periodic) 337 { 338 unsigned long flags; 339 int rc; 340 341 BUG_ON(!timer->function); 342 343 if (timer->expires == expires && vtimer_pending(timer)) 344 return 1; 345 spin_lock_irqsave(&virt_timer_lock, flags); 346 rc = vtimer_pending(timer); 347 if (rc) 348 list_del_init(&timer->entry); 349 timer->interval = periodic ? expires : 0; 350 timer->expires = expires; 351 internal_add_vtimer(timer); 352 spin_unlock_irqrestore(&virt_timer_lock, flags); 353 return rc; 354 } 355 356 /* 357 * returns whether it has modified a pending timer (1) or not (0) 358 */ 359 int mod_virt_timer(struct vtimer_list *timer, u64 expires) 360 { 361 return __mod_vtimer(timer, expires, 0); 362 } 363 EXPORT_SYMBOL(mod_virt_timer); 364 365 /* 366 * returns whether it has modified a pending timer (1) or not (0) 367 */ 368 int mod_virt_timer_periodic(struct vtimer_list *timer, u64 expires) 369 { 370 return __mod_vtimer(timer, expires, 1); 371 } 372 EXPORT_SYMBOL(mod_virt_timer_periodic); 373 374 /* 375 * Delete a virtual timer. 376 * 377 * returns whether the deleted timer was pending (1) or not (0) 378 */ 379 int del_virt_timer(struct vtimer_list *timer) 380 { 381 unsigned long flags; 382 383 if (!vtimer_pending(timer)) 384 return 0; 385 spin_lock_irqsave(&virt_timer_lock, flags); 386 list_del_init(&timer->entry); 387 spin_unlock_irqrestore(&virt_timer_lock, flags); 388 return 1; 389 } 390 EXPORT_SYMBOL(del_virt_timer); 391 392 /* 393 * Start the virtual CPU timer on the current CPU. 394 */ 395 void vtime_init(void) 396 { 397 /* set initial cpu timer */ 398 set_vtimer(VTIMER_MAX_SLICE); 399 /* Setup initial MT scaling values */ 400 if (smp_cpu_mtid) { 401 __this_cpu_write(mt_scaling_jiffies, jiffies); 402 __this_cpu_write(mt_scaling_mult, 1); 403 __this_cpu_write(mt_scaling_div, 1); 404 stcctm5(smp_cpu_mtid + 1, this_cpu_ptr(mt_cycles)); 405 } 406 } 407