vtime.c 14 KB

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