vtime.c 10 KB

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