vtime.c 14 KB

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