vtime.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. * arch/s390/kernel/vtime.c
  3. * Virtual cpu timer based timer functions.
  4. *
  5. * S390 version
  6. * Copyright (C) 2004 IBM Deutschland Entwicklung GmbH, IBM Corporation
  7. * Author(s): Jan Glauber <jan.glauber@de.ibm.com>
  8. */
  9. #include <linux/config.h>
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/time.h>
  13. #include <linux/delay.h>
  14. #include <linux/init.h>
  15. #include <linux/smp.h>
  16. #include <linux/types.h>
  17. #include <linux/timex.h>
  18. #include <linux/notifier.h>
  19. #include <linux/kernel_stat.h>
  20. #include <linux/rcupdate.h>
  21. #include <linux/posix-timers.h>
  22. #include <asm/s390_ext.h>
  23. #include <asm/timer.h>
  24. static ext_int_info_t ext_int_info_timer;
  25. DEFINE_PER_CPU(struct vtimer_queue, virt_cpu_timer);
  26. #ifdef CONFIG_VIRT_CPU_ACCOUNTING
  27. /*
  28. * Update process times based on virtual cpu times stored by entry.S
  29. * to the lowcore fields user_timer, system_timer & steal_clock.
  30. */
  31. void account_user_vtime(struct task_struct *tsk)
  32. {
  33. cputime_t cputime;
  34. __u64 timer, clock;
  35. int rcu_user_flag;
  36. timer = S390_lowcore.last_update_timer;
  37. clock = S390_lowcore.last_update_clock;
  38. asm volatile (" STPT %0\n" /* Store current cpu timer value */
  39. " STCK %1" /* Store current tod clock value */
  40. : "=m" (S390_lowcore.last_update_timer),
  41. "=m" (S390_lowcore.last_update_clock) );
  42. S390_lowcore.system_timer += timer - S390_lowcore.last_update_timer;
  43. S390_lowcore.steal_clock += S390_lowcore.last_update_clock - clock;
  44. cputime = S390_lowcore.user_timer >> 12;
  45. rcu_user_flag = cputime != 0;
  46. S390_lowcore.user_timer -= cputime << 12;
  47. S390_lowcore.steal_clock -= cputime << 12;
  48. account_user_time(tsk, cputime);
  49. cputime = S390_lowcore.system_timer >> 12;
  50. S390_lowcore.system_timer -= cputime << 12;
  51. S390_lowcore.steal_clock -= cputime << 12;
  52. account_system_time(tsk, HARDIRQ_OFFSET, cputime);
  53. cputime = S390_lowcore.steal_clock;
  54. if ((__s64) cputime > 0) {
  55. cputime >>= 12;
  56. S390_lowcore.steal_clock -= cputime << 12;
  57. account_steal_time(tsk, cputime);
  58. }
  59. run_local_timers();
  60. if (rcu_pending(smp_processor_id()))
  61. rcu_check_callbacks(smp_processor_id(), rcu_user_flag);
  62. scheduler_tick();
  63. run_posix_cpu_timers(tsk);
  64. }
  65. /*
  66. * Update process times based on virtual cpu times stored by entry.S
  67. * to the lowcore fields user_timer, system_timer & steal_clock.
  68. */
  69. void account_system_vtime(struct task_struct *tsk)
  70. {
  71. cputime_t cputime;
  72. __u64 timer;
  73. timer = S390_lowcore.last_update_timer;
  74. asm volatile (" STPT %0" /* Store current cpu timer value */
  75. : "=m" (S390_lowcore.last_update_timer) );
  76. S390_lowcore.system_timer += timer - S390_lowcore.last_update_timer;
  77. cputime = S390_lowcore.system_timer >> 12;
  78. S390_lowcore.system_timer -= cputime << 12;
  79. S390_lowcore.steal_clock -= cputime << 12;
  80. account_system_time(tsk, 0, cputime);
  81. }
  82. static inline void set_vtimer(__u64 expires)
  83. {
  84. __u64 timer;
  85. asm volatile (" STPT %0\n" /* Store current cpu timer value */
  86. " SPT %1" /* Set new value immediatly afterwards */
  87. : "=m" (timer) : "m" (expires) );
  88. S390_lowcore.system_timer += S390_lowcore.last_update_timer - timer;
  89. S390_lowcore.last_update_timer = expires;
  90. /* store expire time for this CPU timer */
  91. per_cpu(virt_cpu_timer, smp_processor_id()).to_expire = expires;
  92. }
  93. #else
  94. static inline void set_vtimer(__u64 expires)
  95. {
  96. S390_lowcore.last_update_timer = expires;
  97. asm volatile ("SPT %0" : : "m" (S390_lowcore.last_update_timer));
  98. /* store expire time for this CPU timer */
  99. per_cpu(virt_cpu_timer, smp_processor_id()).to_expire = expires;
  100. }
  101. #endif
  102. static void start_cpu_timer(void)
  103. {
  104. struct vtimer_queue *vt_list;
  105. vt_list = &per_cpu(virt_cpu_timer, smp_processor_id());
  106. /* CPU timer interrupt is pending, don't reprogramm it */
  107. if (vt_list->idle & 1LL<<63)
  108. return;
  109. if (!list_empty(&vt_list->list))
  110. set_vtimer(vt_list->idle);
  111. }
  112. static void stop_cpu_timer(void)
  113. {
  114. struct vtimer_queue *vt_list;
  115. vt_list = &per_cpu(virt_cpu_timer, smp_processor_id());
  116. /* nothing to do */
  117. if (list_empty(&vt_list->list)) {
  118. vt_list->idle = VTIMER_MAX_SLICE;
  119. goto fire;
  120. }
  121. /* store the actual expire value */
  122. asm volatile ("STPT %0" : "=m" (vt_list->idle));
  123. /*
  124. * If the CPU timer is negative we don't reprogramm
  125. * it because we will get instantly an interrupt.
  126. */
  127. if (vt_list->idle & 1LL<<63)
  128. return;
  129. vt_list->offset += vt_list->to_expire - vt_list->idle;
  130. /*
  131. * We cannot halt the CPU timer, we just write a value that
  132. * nearly never expires (only after 71 years) and re-write
  133. * the stored expire value if we continue the timer
  134. */
  135. fire:
  136. set_vtimer(VTIMER_MAX_SLICE);
  137. }
  138. /*
  139. * Sorted add to a list. List is linear searched until first bigger
  140. * element is found.
  141. */
  142. static void list_add_sorted(struct vtimer_list *timer, struct list_head *head)
  143. {
  144. struct vtimer_list *event;
  145. list_for_each_entry(event, head, entry) {
  146. if (event->expires > timer->expires) {
  147. list_add_tail(&timer->entry, &event->entry);
  148. return;
  149. }
  150. }
  151. list_add_tail(&timer->entry, head);
  152. }
  153. /*
  154. * Do the callback functions of expired vtimer events.
  155. * Called from within the interrupt handler.
  156. */
  157. static void do_callbacks(struct list_head *cb_list, struct pt_regs *regs)
  158. {
  159. struct vtimer_queue *vt_list;
  160. struct vtimer_list *event, *tmp;
  161. void (*fn)(unsigned long, struct pt_regs*);
  162. unsigned long data;
  163. if (list_empty(cb_list))
  164. return;
  165. vt_list = &per_cpu(virt_cpu_timer, smp_processor_id());
  166. list_for_each_entry_safe(event, tmp, cb_list, entry) {
  167. fn = event->function;
  168. data = event->data;
  169. fn(data, regs);
  170. if (!event->interval)
  171. /* delete one shot timer */
  172. list_del_init(&event->entry);
  173. else {
  174. /* move interval timer back to list */
  175. spin_lock(&vt_list->lock);
  176. list_del_init(&event->entry);
  177. list_add_sorted(event, &vt_list->list);
  178. spin_unlock(&vt_list->lock);
  179. }
  180. }
  181. }
  182. /*
  183. * Handler for the virtual CPU timer.
  184. */
  185. static void do_cpu_timer_interrupt(struct pt_regs *regs, __u16 error_code)
  186. {
  187. int cpu;
  188. __u64 next, delta;
  189. struct vtimer_queue *vt_list;
  190. struct vtimer_list *event, *tmp;
  191. struct list_head *ptr;
  192. /* the callback queue */
  193. struct list_head cb_list;
  194. INIT_LIST_HEAD(&cb_list);
  195. cpu = smp_processor_id();
  196. vt_list = &per_cpu(virt_cpu_timer, cpu);
  197. /* walk timer list, fire all expired events */
  198. spin_lock(&vt_list->lock);
  199. if (vt_list->to_expire < VTIMER_MAX_SLICE)
  200. vt_list->offset += vt_list->to_expire;
  201. list_for_each_entry_safe(event, tmp, &vt_list->list, entry) {
  202. if (event->expires > vt_list->offset)
  203. /* found first unexpired event, leave */
  204. break;
  205. /* re-charge interval timer, we have to add the offset */
  206. if (event->interval)
  207. event->expires = event->interval + vt_list->offset;
  208. /* move expired timer to the callback queue */
  209. list_move_tail(&event->entry, &cb_list);
  210. }
  211. spin_unlock(&vt_list->lock);
  212. do_callbacks(&cb_list, regs);
  213. /* next event is first in list */
  214. spin_lock(&vt_list->lock);
  215. if (!list_empty(&vt_list->list)) {
  216. ptr = vt_list->list.next;
  217. event = list_entry(ptr, struct vtimer_list, entry);
  218. next = event->expires - vt_list->offset;
  219. /* add the expired time from this interrupt handler
  220. * and the callback functions
  221. */
  222. asm volatile ("STPT %0" : "=m" (delta));
  223. delta = 0xffffffffffffffffLL - delta + 1;
  224. vt_list->offset += delta;
  225. next -= delta;
  226. } else {
  227. vt_list->offset = 0;
  228. next = VTIMER_MAX_SLICE;
  229. }
  230. spin_unlock(&vt_list->lock);
  231. set_vtimer(next);
  232. }
  233. void init_virt_timer(struct vtimer_list *timer)
  234. {
  235. timer->function = NULL;
  236. INIT_LIST_HEAD(&timer->entry);
  237. spin_lock_init(&timer->lock);
  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. /*
  245. * this function should only run on the specified CPU
  246. */
  247. static void internal_add_vtimer(struct vtimer_list *timer)
  248. {
  249. unsigned long flags;
  250. __u64 done;
  251. struct vtimer_list *event;
  252. struct vtimer_queue *vt_list;
  253. vt_list = &per_cpu(virt_cpu_timer, timer->cpu);
  254. spin_lock_irqsave(&vt_list->lock, flags);
  255. if (timer->cpu != smp_processor_id())
  256. printk("internal_add_vtimer: BUG, running on wrong CPU");
  257. /* if list is empty we only have to set the timer */
  258. if (list_empty(&vt_list->list)) {
  259. /* reset the offset, this may happen if the last timer was
  260. * just deleted by mod_virt_timer and the interrupt
  261. * didn't happen until here
  262. */
  263. vt_list->offset = 0;
  264. goto fire;
  265. }
  266. /* save progress */
  267. asm volatile ("STPT %0" : "=m" (done));
  268. /* calculate completed work */
  269. done = vt_list->to_expire - done + vt_list->offset;
  270. vt_list->offset = 0;
  271. list_for_each_entry(event, &vt_list->list, entry)
  272. event->expires -= done;
  273. fire:
  274. list_add_sorted(timer, &vt_list->list);
  275. /* get first element, which is the next vtimer slice */
  276. event = list_entry(vt_list->list.next, struct vtimer_list, entry);
  277. set_vtimer(event->expires);
  278. spin_unlock_irqrestore(&vt_list->lock, flags);
  279. /* release CPU aquired in prepare_vtimer or mod_virt_timer() */
  280. put_cpu();
  281. }
  282. static inline int prepare_vtimer(struct vtimer_list *timer)
  283. {
  284. if (!timer->function) {
  285. printk("add_virt_timer: uninitialized timer\n");
  286. return -EINVAL;
  287. }
  288. if (!timer->expires || timer->expires > VTIMER_MAX_SLICE) {
  289. printk("add_virt_timer: invalid timer expire value!\n");
  290. return -EINVAL;
  291. }
  292. if (vtimer_pending(timer)) {
  293. printk("add_virt_timer: timer pending\n");
  294. return -EBUSY;
  295. }
  296. timer->cpu = get_cpu();
  297. return 0;
  298. }
  299. /*
  300. * add_virt_timer - add an oneshot virtual CPU timer
  301. */
  302. void add_virt_timer(void *new)
  303. {
  304. struct vtimer_list *timer;
  305. timer = (struct vtimer_list *)new;
  306. if (prepare_vtimer(timer) < 0)
  307. return;
  308. timer->interval = 0;
  309. internal_add_vtimer(timer);
  310. }
  311. EXPORT_SYMBOL(add_virt_timer);
  312. /*
  313. * add_virt_timer_int - add an interval virtual CPU timer
  314. */
  315. void add_virt_timer_periodic(void *new)
  316. {
  317. struct vtimer_list *timer;
  318. timer = (struct vtimer_list *)new;
  319. if (prepare_vtimer(timer) < 0)
  320. return;
  321. timer->interval = timer->expires;
  322. internal_add_vtimer(timer);
  323. }
  324. EXPORT_SYMBOL(add_virt_timer_periodic);
  325. /*
  326. * If we change a pending timer the function must be called on the CPU
  327. * where the timer is running on, e.g. by smp_call_function_on()
  328. *
  329. * The original mod_timer adds the timer if it is not pending. For compatibility
  330. * we do the same. The timer will be added on the current CPU as a oneshot timer.
  331. *
  332. * returns whether it has modified a pending timer (1) or not (0)
  333. */
  334. int mod_virt_timer(struct vtimer_list *timer, __u64 expires)
  335. {
  336. struct vtimer_queue *vt_list;
  337. unsigned long flags;
  338. int cpu;
  339. if (!timer->function) {
  340. printk("mod_virt_timer: uninitialized timer\n");
  341. return -EINVAL;
  342. }
  343. if (!expires || expires > VTIMER_MAX_SLICE) {
  344. printk("mod_virt_timer: invalid expire range\n");
  345. return -EINVAL;
  346. }
  347. /*
  348. * This is a common optimization triggered by the
  349. * networking code - if the timer is re-modified
  350. * to be the same thing then just return:
  351. */
  352. if (timer->expires == expires && vtimer_pending(timer))
  353. return 1;
  354. cpu = get_cpu();
  355. vt_list = &per_cpu(virt_cpu_timer, cpu);
  356. /* disable interrupts before test if timer is pending */
  357. spin_lock_irqsave(&vt_list->lock, flags);
  358. /* if timer isn't pending add it on the current CPU */
  359. if (!vtimer_pending(timer)) {
  360. spin_unlock_irqrestore(&vt_list->lock, flags);
  361. /* we do not activate an interval timer with mod_virt_timer */
  362. timer->interval = 0;
  363. timer->expires = expires;
  364. timer->cpu = cpu;
  365. internal_add_vtimer(timer);
  366. return 0;
  367. }
  368. /* check if we run on the right CPU */
  369. if (timer->cpu != cpu) {
  370. printk("mod_virt_timer: running on wrong CPU, check your code\n");
  371. spin_unlock_irqrestore(&vt_list->lock, flags);
  372. put_cpu();
  373. return -EINVAL;
  374. }
  375. list_del_init(&timer->entry);
  376. timer->expires = expires;
  377. /* also change the interval if we have an interval timer */
  378. if (timer->interval)
  379. timer->interval = expires;
  380. /* the timer can't expire anymore so we can release the lock */
  381. spin_unlock_irqrestore(&vt_list->lock, flags);
  382. internal_add_vtimer(timer);
  383. return 1;
  384. }
  385. EXPORT_SYMBOL(mod_virt_timer);
  386. /*
  387. * delete a virtual timer
  388. *
  389. * returns whether the deleted timer was pending (1) or not (0)
  390. */
  391. int del_virt_timer(struct vtimer_list *timer)
  392. {
  393. unsigned long flags;
  394. struct vtimer_queue *vt_list;
  395. /* check if timer is pending */
  396. if (!vtimer_pending(timer))
  397. return 0;
  398. vt_list = &per_cpu(virt_cpu_timer, timer->cpu);
  399. spin_lock_irqsave(&vt_list->lock, flags);
  400. /* we don't interrupt a running timer, just let it expire! */
  401. list_del_init(&timer->entry);
  402. /* last timer removed */
  403. if (list_empty(&vt_list->list)) {
  404. vt_list->to_expire = 0;
  405. vt_list->offset = 0;
  406. }
  407. spin_unlock_irqrestore(&vt_list->lock, flags);
  408. return 1;
  409. }
  410. EXPORT_SYMBOL(del_virt_timer);
  411. /*
  412. * Start the virtual CPU timer on the current CPU.
  413. */
  414. void init_cpu_vtimer(void)
  415. {
  416. struct vtimer_queue *vt_list;
  417. unsigned long cr0;
  418. /* kick the virtual timer */
  419. S390_lowcore.exit_timer = VTIMER_MAX_SLICE;
  420. S390_lowcore.last_update_timer = VTIMER_MAX_SLICE;
  421. asm volatile ("SPT %0" : : "m" (S390_lowcore.last_update_timer));
  422. asm volatile ("STCK %0" : "=m" (S390_lowcore.last_update_clock));
  423. __ctl_store(cr0, 0, 0);
  424. cr0 |= 0x400;
  425. __ctl_load(cr0, 0, 0);
  426. vt_list = &per_cpu(virt_cpu_timer, smp_processor_id());
  427. INIT_LIST_HEAD(&vt_list->list);
  428. spin_lock_init(&vt_list->lock);
  429. vt_list->to_expire = 0;
  430. vt_list->offset = 0;
  431. vt_list->idle = 0;
  432. }
  433. static int vtimer_idle_notify(struct notifier_block *self,
  434. unsigned long action, void *hcpu)
  435. {
  436. switch (action) {
  437. case CPU_IDLE:
  438. stop_cpu_timer();
  439. break;
  440. case CPU_NOT_IDLE:
  441. start_cpu_timer();
  442. break;
  443. }
  444. return NOTIFY_OK;
  445. }
  446. static struct notifier_block vtimer_idle_nb = {
  447. .notifier_call = vtimer_idle_notify,
  448. };
  449. void __init vtime_init(void)
  450. {
  451. /* request the cpu timer external interrupt */
  452. if (register_early_external_interrupt(0x1005, do_cpu_timer_interrupt,
  453. &ext_int_info_timer) != 0)
  454. panic("Couldn't request external interrupt 0x1005");
  455. if (register_idle_notifier(&vtimer_idle_nb))
  456. panic("Couldn't register idle notifier");
  457. init_cpu_vtimer();
  458. }