vtime.c 10 KB

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