vtime.c 9.8 KB

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