vtime.c 10 KB

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