vtime.c 14 KB

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