vtime.c 8.5 KB

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