alarmtimer.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. /*
  2. * Alarmtimer interface
  3. *
  4. * This interface provides a timer which is similarto hrtimers,
  5. * but triggers a RTC alarm if the box is suspend.
  6. *
  7. * This interface is influenced by the Android RTC Alarm timer
  8. * interface.
  9. *
  10. * Copyright (C) 2010 IBM Corperation
  11. *
  12. * Author: John Stultz <john.stultz@linaro.org>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. */
  18. #include <linux/time.h>
  19. #include <linux/hrtimer.h>
  20. #include <linux/timerqueue.h>
  21. #include <linux/rtc.h>
  22. #include <linux/sched/signal.h>
  23. #include <linux/sched/debug.h>
  24. #include <linux/alarmtimer.h>
  25. #include <linux/mutex.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/posix-timers.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/freezer.h>
  30. #include <linux/compat.h>
  31. #include <linux/module.h>
  32. #include "posix-timers.h"
  33. #define CREATE_TRACE_POINTS
  34. #include <trace/events/alarmtimer.h>
  35. /**
  36. * struct alarm_base - Alarm timer bases
  37. * @lock: Lock for syncrhonized access to the base
  38. * @timerqueue: Timerqueue head managing the list of events
  39. * @gettime: Function to read the time correlating to the base
  40. * @base_clockid: clockid for the base
  41. */
  42. static struct alarm_base {
  43. spinlock_t lock;
  44. struct timerqueue_head timerqueue;
  45. ktime_t (*gettime)(void);
  46. clockid_t base_clockid;
  47. } alarm_bases[ALARM_NUMTYPE];
  48. #if defined(CONFIG_POSIX_TIMERS) || defined(CONFIG_RTC_CLASS)
  49. /* freezer information to handle clock_nanosleep triggered wakeups */
  50. static enum alarmtimer_type freezer_alarmtype;
  51. static ktime_t freezer_expires;
  52. static ktime_t freezer_delta;
  53. static DEFINE_SPINLOCK(freezer_delta_lock);
  54. #endif
  55. #ifdef CONFIG_RTC_CLASS
  56. static struct wakeup_source *ws;
  57. /* rtc timer and device for setting alarm wakeups at suspend */
  58. static struct rtc_timer rtctimer;
  59. static struct rtc_device *rtcdev;
  60. static DEFINE_SPINLOCK(rtcdev_lock);
  61. /**
  62. * alarmtimer_get_rtcdev - Return selected rtcdevice
  63. *
  64. * This function returns the rtc device to use for wakealarms.
  65. * If one has not already been chosen, it checks to see if a
  66. * functional rtc device is available.
  67. */
  68. struct rtc_device *alarmtimer_get_rtcdev(void)
  69. {
  70. unsigned long flags;
  71. struct rtc_device *ret;
  72. spin_lock_irqsave(&rtcdev_lock, flags);
  73. ret = rtcdev;
  74. spin_unlock_irqrestore(&rtcdev_lock, flags);
  75. return ret;
  76. }
  77. EXPORT_SYMBOL_GPL(alarmtimer_get_rtcdev);
  78. static int alarmtimer_rtc_add_device(struct device *dev,
  79. struct class_interface *class_intf)
  80. {
  81. unsigned long flags;
  82. struct rtc_device *rtc = to_rtc_device(dev);
  83. struct wakeup_source *__ws;
  84. int ret = 0;
  85. if (rtcdev)
  86. return -EBUSY;
  87. if (!rtc->ops->set_alarm)
  88. return -1;
  89. if (!device_may_wakeup(rtc->dev.parent))
  90. return -1;
  91. __ws = wakeup_source_register(dev, "alarmtimer");
  92. spin_lock_irqsave(&rtcdev_lock, flags);
  93. if (!rtcdev) {
  94. if (!try_module_get(rtc->owner)) {
  95. ret = -1;
  96. goto unlock;
  97. }
  98. rtcdev = rtc;
  99. /* hold a reference so it doesn't go away */
  100. get_device(dev);
  101. ws = __ws;
  102. __ws = NULL;
  103. }
  104. unlock:
  105. spin_unlock_irqrestore(&rtcdev_lock, flags);
  106. wakeup_source_unregister(__ws);
  107. return ret;
  108. }
  109. static inline void alarmtimer_rtc_timer_init(void)
  110. {
  111. rtc_timer_init(&rtctimer, NULL, NULL);
  112. }
  113. static struct class_interface alarmtimer_rtc_interface = {
  114. .add_dev = &alarmtimer_rtc_add_device,
  115. };
  116. static int alarmtimer_rtc_interface_setup(void)
  117. {
  118. alarmtimer_rtc_interface.class = rtc_class;
  119. return class_interface_register(&alarmtimer_rtc_interface);
  120. }
  121. static void alarmtimer_rtc_interface_remove(void)
  122. {
  123. class_interface_unregister(&alarmtimer_rtc_interface);
  124. }
  125. #else
  126. struct rtc_device *alarmtimer_get_rtcdev(void)
  127. {
  128. return NULL;
  129. }
  130. #define rtcdev (NULL)
  131. static inline int alarmtimer_rtc_interface_setup(void) { return 0; }
  132. static inline void alarmtimer_rtc_interface_remove(void) { }
  133. static inline void alarmtimer_rtc_timer_init(void) { }
  134. #endif
  135. /**
  136. * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
  137. * @base: pointer to the base where the timer is being run
  138. * @alarm: pointer to alarm being enqueued.
  139. *
  140. * Adds alarm to a alarm_base timerqueue
  141. *
  142. * Must hold base->lock when calling.
  143. */
  144. static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
  145. {
  146. static DEFINE_RATELIMIT_STATE(ratelimit, HZ - 1, 5);
  147. if (alarm->state & ALARMTIMER_STATE_ENQUEUED)
  148. timerqueue_del(&base->timerqueue, &alarm->node);
  149. if (__ratelimit(&ratelimit)) {
  150. ratelimit.begin = jiffies;
  151. pr_notice("%s, %lld\n", __func__, alarm->node.expires);
  152. }
  153. timerqueue_add(&base->timerqueue, &alarm->node);
  154. alarm->state |= ALARMTIMER_STATE_ENQUEUED;
  155. }
  156. /**
  157. * alarmtimer_dequeue - Removes an alarm timer from an alarm_base timerqueue
  158. * @base: pointer to the base where the timer is running
  159. * @alarm: pointer to alarm being removed
  160. *
  161. * Removes alarm to a alarm_base timerqueue
  162. *
  163. * Must hold base->lock when calling.
  164. */
  165. static void alarmtimer_dequeue(struct alarm_base *base, struct alarm *alarm)
  166. {
  167. if (!(alarm->state & ALARMTIMER_STATE_ENQUEUED))
  168. return;
  169. timerqueue_del(&base->timerqueue, &alarm->node);
  170. alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
  171. }
  172. /**
  173. * alarmtimer_fired - Handles alarm hrtimer being fired.
  174. * @timer: pointer to hrtimer being run
  175. *
  176. * When a alarm timer fires, this runs through the timerqueue to
  177. * see which alarms expired, and runs those. If there are more alarm
  178. * timers queued for the future, we set the hrtimer to fire when
  179. * when the next future alarm timer expires.
  180. */
  181. static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
  182. {
  183. struct alarm *alarm = container_of(timer, struct alarm, timer);
  184. struct alarm_base *base = &alarm_bases[alarm->type];
  185. unsigned long flags;
  186. int ret = HRTIMER_NORESTART;
  187. int restart = ALARMTIMER_NORESTART;
  188. spin_lock_irqsave(&base->lock, flags);
  189. alarmtimer_dequeue(base, alarm);
  190. spin_unlock_irqrestore(&base->lock, flags);
  191. if (alarm->function)
  192. restart = alarm->function(alarm, base->gettime());
  193. spin_lock_irqsave(&base->lock, flags);
  194. if (restart != ALARMTIMER_NORESTART) {
  195. hrtimer_set_expires(&alarm->timer, alarm->node.expires);
  196. alarmtimer_enqueue(base, alarm);
  197. ret = HRTIMER_RESTART;
  198. }
  199. spin_unlock_irqrestore(&base->lock, flags);
  200. trace_alarmtimer_fired(alarm, base->gettime());
  201. return ret;
  202. }
  203. ktime_t alarm_expires_remaining(const struct alarm *alarm)
  204. {
  205. struct alarm_base *base = &alarm_bases[alarm->type];
  206. return ktime_sub(alarm->node.expires, base->gettime());
  207. }
  208. EXPORT_SYMBOL_GPL(alarm_expires_remaining);
  209. #ifdef CONFIG_RTC_CLASS
  210. /**
  211. * alarmtimer_suspend - Suspend time callback
  212. * @dev: unused
  213. * @state: unused
  214. *
  215. * When we are going into suspend, we look through the bases
  216. * to see which is the soonest timer to expire. We then
  217. * set an rtc timer to fire that far into the future, which
  218. * will wake us from suspend.
  219. */
  220. static int alarmtimer_suspend(struct device *dev)
  221. {
  222. ktime_t min, now, expires;
  223. int i, ret, type;
  224. struct rtc_device *rtc;
  225. unsigned long flags;
  226. struct rtc_time tm, time;
  227. spin_lock_irqsave(&freezer_delta_lock, flags);
  228. min = freezer_delta;
  229. expires = freezer_expires;
  230. type = freezer_alarmtype;
  231. freezer_delta = 0;
  232. spin_unlock_irqrestore(&freezer_delta_lock, flags);
  233. rtc = alarmtimer_get_rtcdev();
  234. /* If we have no rtcdev, just return */
  235. if (!rtc)
  236. return 0;
  237. /* Find the soonest timer to expire*/
  238. for (i = 0; i < ALARM_NUMTYPE; i++) {
  239. struct alarm_base *base = &alarm_bases[i];
  240. struct timerqueue_node *next;
  241. ktime_t delta;
  242. spin_lock_irqsave(&base->lock, flags);
  243. next = timerqueue_getnext(&base->timerqueue);
  244. spin_unlock_irqrestore(&base->lock, flags);
  245. if (!next)
  246. continue;
  247. delta = ktime_sub(next->expires, base->gettime());
  248. if (!min || (delta < min)) {
  249. expires = next->expires;
  250. min = delta;
  251. type = i;
  252. }
  253. }
  254. if (min == 0)
  255. return 0;
  256. if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) {
  257. __pm_wakeup_event(ws, 2 * MSEC_PER_SEC);
  258. return -EBUSY;
  259. }
  260. trace_alarmtimer_suspend(expires, type);
  261. /* Setup an rtc timer to fire that far in the future */
  262. rtc_timer_cancel(rtc, &rtctimer);
  263. rtc_read_time(rtc, &tm);
  264. now = rtc_tm_to_ktime(tm);
  265. now = ktime_add(now, min);
  266. time = rtc_ktime_to_tm(now);
  267. pr_notice_ratelimited("%s convert %lld to %04d/%02d/%02d %02d:%02d:%02d (now = %04d/%02d/%02d %02d:%02d:%02d)\n",
  268. __func__, expires,
  269. time.tm_year+1900, time.tm_mon+1, time.tm_mday,
  270. time.tm_hour, time.tm_min, time.tm_sec,
  271. tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
  272. tm.tm_hour, tm.tm_min, tm.tm_sec);
  273. /* Set alarm, if in the past reject suspend briefly to handle */
  274. ret = rtc_timer_start(rtc, &rtctimer, now, 0);
  275. if (ret < 0)
  276. __pm_wakeup_event(ws, MSEC_PER_SEC);
  277. return ret;
  278. }
  279. static int alarmtimer_resume(struct device *dev)
  280. {
  281. struct rtc_device *rtc;
  282. rtc = alarmtimer_get_rtcdev();
  283. if (rtc)
  284. rtc_timer_cancel(rtc, &rtctimer);
  285. return 0;
  286. }
  287. #else
  288. static int alarmtimer_suspend(struct device *dev)
  289. {
  290. return 0;
  291. }
  292. static int alarmtimer_resume(struct device *dev)
  293. {
  294. return 0;
  295. }
  296. #endif
  297. static void
  298. __alarm_init(struct alarm *alarm, enum alarmtimer_type type,
  299. enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
  300. {
  301. timerqueue_init(&alarm->node);
  302. alarm->timer.function = alarmtimer_fired;
  303. alarm->function = function;
  304. alarm->type = type;
  305. alarm->state = ALARMTIMER_STATE_INACTIVE;
  306. }
  307. /**
  308. * alarm_init - Initialize an alarm structure
  309. * @alarm: ptr to alarm to be initialized
  310. * @type: the type of the alarm
  311. * @function: callback that is run when the alarm fires
  312. */
  313. void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
  314. enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
  315. {
  316. hrtimer_init(&alarm->timer, alarm_bases[type].base_clockid,
  317. HRTIMER_MODE_ABS);
  318. __alarm_init(alarm, type, function);
  319. }
  320. EXPORT_SYMBOL_GPL(alarm_init);
  321. /**
  322. * alarm_start - Sets an absolute alarm to fire
  323. * @alarm: ptr to alarm to set
  324. * @start: time to run the alarm
  325. */
  326. void alarm_start(struct alarm *alarm, ktime_t start)
  327. {
  328. struct alarm_base *base = &alarm_bases[alarm->type];
  329. unsigned long flags;
  330. spin_lock_irqsave(&base->lock, flags);
  331. alarm->node.expires = start;
  332. alarmtimer_enqueue(base, alarm);
  333. hrtimer_start(&alarm->timer, alarm->node.expires, HRTIMER_MODE_ABS);
  334. spin_unlock_irqrestore(&base->lock, flags);
  335. trace_alarmtimer_start(alarm, base->gettime());
  336. }
  337. EXPORT_SYMBOL_GPL(alarm_start);
  338. /**
  339. * alarm_start_relative - Sets a relative alarm to fire
  340. * @alarm: ptr to alarm to set
  341. * @start: time relative to now to run the alarm
  342. */
  343. void alarm_start_relative(struct alarm *alarm, ktime_t start)
  344. {
  345. struct alarm_base *base = &alarm_bases[alarm->type];
  346. start = ktime_add_safe(start, base->gettime());
  347. alarm_start(alarm, start);
  348. }
  349. EXPORT_SYMBOL_GPL(alarm_start_relative);
  350. void alarm_restart(struct alarm *alarm)
  351. {
  352. struct alarm_base *base = &alarm_bases[alarm->type];
  353. unsigned long flags;
  354. spin_lock_irqsave(&base->lock, flags);
  355. hrtimer_set_expires(&alarm->timer, alarm->node.expires);
  356. hrtimer_restart(&alarm->timer);
  357. alarmtimer_enqueue(base, alarm);
  358. spin_unlock_irqrestore(&base->lock, flags);
  359. }
  360. EXPORT_SYMBOL_GPL(alarm_restart);
  361. /**
  362. * alarm_try_to_cancel - Tries to cancel an alarm timer
  363. * @alarm: ptr to alarm to be canceled
  364. *
  365. * Returns 1 if the timer was canceled, 0 if it was not running,
  366. * and -1 if the callback was running
  367. */
  368. int alarm_try_to_cancel(struct alarm *alarm)
  369. {
  370. struct alarm_base *base = &alarm_bases[alarm->type];
  371. unsigned long flags;
  372. int ret;
  373. spin_lock_irqsave(&base->lock, flags);
  374. ret = hrtimer_try_to_cancel(&alarm->timer);
  375. if (ret >= 0)
  376. alarmtimer_dequeue(base, alarm);
  377. spin_unlock_irqrestore(&base->lock, flags);
  378. trace_alarmtimer_cancel(alarm, base->gettime());
  379. return ret;
  380. }
  381. EXPORT_SYMBOL_GPL(alarm_try_to_cancel);
  382. /**
  383. * alarm_cancel - Spins trying to cancel an alarm timer until it is done
  384. * @alarm: ptr to alarm to be canceled
  385. *
  386. * Returns 1 if the timer was canceled, 0 if it was not active.
  387. */
  388. int alarm_cancel(struct alarm *alarm)
  389. {
  390. for (;;) {
  391. int ret = alarm_try_to_cancel(alarm);
  392. if (ret >= 0)
  393. return ret;
  394. cpu_relax();
  395. }
  396. }
  397. EXPORT_SYMBOL_GPL(alarm_cancel);
  398. u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval)
  399. {
  400. u64 overrun = 1;
  401. ktime_t delta;
  402. delta = ktime_sub(now, alarm->node.expires);
  403. if (delta < 0)
  404. return 0;
  405. if (unlikely(delta >= interval)) {
  406. s64 incr = ktime_to_ns(interval);
  407. overrun = ktime_divns(delta, incr);
  408. alarm->node.expires = ktime_add_ns(alarm->node.expires,
  409. incr*overrun);
  410. if (alarm->node.expires > now)
  411. return overrun;
  412. /*
  413. * This (and the ktime_add() below) is the
  414. * correction for exact:
  415. */
  416. overrun++;
  417. }
  418. alarm->node.expires = ktime_add_safe(alarm->node.expires, interval);
  419. return overrun;
  420. }
  421. EXPORT_SYMBOL_GPL(alarm_forward);
  422. u64 alarm_forward_now(struct alarm *alarm, ktime_t interval)
  423. {
  424. struct alarm_base *base = &alarm_bases[alarm->type];
  425. return alarm_forward(alarm, base->gettime(), interval);
  426. }
  427. EXPORT_SYMBOL_GPL(alarm_forward_now);
  428. #ifdef CONFIG_POSIX_TIMERS
  429. static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
  430. {
  431. struct alarm_base *base;
  432. unsigned long flags;
  433. ktime_t delta;
  434. switch(type) {
  435. case ALARM_REALTIME:
  436. base = &alarm_bases[ALARM_REALTIME];
  437. type = ALARM_REALTIME_FREEZER;
  438. break;
  439. case ALARM_BOOTTIME:
  440. base = &alarm_bases[ALARM_BOOTTIME];
  441. type = ALARM_BOOTTIME_FREEZER;
  442. break;
  443. default:
  444. WARN_ONCE(1, "Invalid alarm type: %d\n", type);
  445. return;
  446. }
  447. delta = ktime_sub(absexp, base->gettime());
  448. spin_lock_irqsave(&freezer_delta_lock, flags);
  449. if (!freezer_delta || (delta < freezer_delta)) {
  450. freezer_delta = delta;
  451. freezer_expires = absexp;
  452. freezer_alarmtype = type;
  453. }
  454. spin_unlock_irqrestore(&freezer_delta_lock, flags);
  455. }
  456. /**
  457. * clock2alarm - helper that converts from clockid to alarmtypes
  458. * @clockid: clockid.
  459. */
  460. static enum alarmtimer_type clock2alarm(clockid_t clockid)
  461. {
  462. if (clockid == CLOCK_REALTIME_ALARM)
  463. return ALARM_REALTIME;
  464. if (clockid == CLOCK_BOOTTIME_ALARM)
  465. return ALARM_BOOTTIME;
  466. return -1;
  467. }
  468. /**
  469. * alarm_handle_timer - Callback for posix timers
  470. * @alarm: alarm that fired
  471. *
  472. * Posix timer callback for expired alarm timers.
  473. */
  474. static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm,
  475. ktime_t now)
  476. {
  477. struct k_itimer *ptr = container_of(alarm, struct k_itimer,
  478. it.alarm.alarmtimer);
  479. enum alarmtimer_restart result = ALARMTIMER_NORESTART;
  480. unsigned long flags;
  481. int si_private = 0;
  482. spin_lock_irqsave(&ptr->it_lock, flags);
  483. ptr->it_active = 0;
  484. if (ptr->it_interval)
  485. si_private = ++ptr->it_requeue_pending;
  486. if (posix_timer_event(ptr, si_private) && ptr->it_interval) {
  487. /*
  488. * Handle ignored signals and rearm the timer. This will go
  489. * away once we handle ignored signals proper.
  490. */
  491. ptr->it_overrun += alarm_forward_now(alarm, ptr->it_interval);
  492. ++ptr->it_requeue_pending;
  493. ptr->it_active = 1;
  494. result = ALARMTIMER_RESTART;
  495. }
  496. spin_unlock_irqrestore(&ptr->it_lock, flags);
  497. return result;
  498. }
  499. /**
  500. * alarm_timer_rearm - Posix timer callback for rearming timer
  501. * @timr: Pointer to the posixtimer data struct
  502. */
  503. static void alarm_timer_rearm(struct k_itimer *timr)
  504. {
  505. struct alarm *alarm = &timr->it.alarm.alarmtimer;
  506. timr->it_overrun += alarm_forward_now(alarm, timr->it_interval);
  507. alarm_start(alarm, alarm->node.expires);
  508. }
  509. /**
  510. * alarm_timer_forward - Posix timer callback for forwarding timer
  511. * @timr: Pointer to the posixtimer data struct
  512. * @now: Current time to forward the timer against
  513. */
  514. static s64 alarm_timer_forward(struct k_itimer *timr, ktime_t now)
  515. {
  516. struct alarm *alarm = &timr->it.alarm.alarmtimer;
  517. return alarm_forward(alarm, timr->it_interval, now);
  518. }
  519. /**
  520. * alarm_timer_remaining - Posix timer callback to retrieve remaining time
  521. * @timr: Pointer to the posixtimer data struct
  522. * @now: Current time to calculate against
  523. */
  524. static ktime_t alarm_timer_remaining(struct k_itimer *timr, ktime_t now)
  525. {
  526. struct alarm *alarm = &timr->it.alarm.alarmtimer;
  527. return ktime_sub(alarm->node.expires, now);
  528. }
  529. /**
  530. * alarm_timer_try_to_cancel - Posix timer callback to cancel a timer
  531. * @timr: Pointer to the posixtimer data struct
  532. */
  533. static int alarm_timer_try_to_cancel(struct k_itimer *timr)
  534. {
  535. return alarm_try_to_cancel(&timr->it.alarm.alarmtimer);
  536. }
  537. /**
  538. * alarm_timer_arm - Posix timer callback to arm a timer
  539. * @timr: Pointer to the posixtimer data struct
  540. * @expires: The new expiry time
  541. * @absolute: Expiry value is absolute time
  542. * @sigev_none: Posix timer does not deliver signals
  543. */
  544. static void alarm_timer_arm(struct k_itimer *timr, ktime_t expires,
  545. bool absolute, bool sigev_none)
  546. {
  547. struct alarm *alarm = &timr->it.alarm.alarmtimer;
  548. struct alarm_base *base = &alarm_bases[alarm->type];
  549. if (!absolute)
  550. expires = ktime_add_safe(expires, base->gettime());
  551. if (sigev_none)
  552. alarm->node.expires = expires;
  553. else
  554. alarm_start(&timr->it.alarm.alarmtimer, expires);
  555. }
  556. /**
  557. * alarm_clock_getres - posix getres interface
  558. * @which_clock: clockid
  559. * @tp: timespec to fill
  560. *
  561. * Returns the granularity of underlying alarm base clock
  562. */
  563. static int alarm_clock_getres(const clockid_t which_clock, struct timespec64 *tp)
  564. {
  565. if (!alarmtimer_get_rtcdev())
  566. return -EINVAL;
  567. tp->tv_sec = 0;
  568. tp->tv_nsec = hrtimer_resolution;
  569. return 0;
  570. }
  571. /**
  572. * alarm_clock_get - posix clock_get interface
  573. * @which_clock: clockid
  574. * @tp: timespec to fill.
  575. *
  576. * Provides the underlying alarm base time.
  577. */
  578. static int alarm_clock_get(clockid_t which_clock, struct timespec64 *tp)
  579. {
  580. struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
  581. if (!alarmtimer_get_rtcdev())
  582. return -EINVAL;
  583. *tp = ktime_to_timespec64(base->gettime());
  584. return 0;
  585. }
  586. /**
  587. * alarm_timer_create - posix timer_create interface
  588. * @new_timer: k_itimer pointer to manage
  589. *
  590. * Initializes the k_itimer structure.
  591. */
  592. static int alarm_timer_create(struct k_itimer *new_timer)
  593. {
  594. enum alarmtimer_type type;
  595. if (!alarmtimer_get_rtcdev())
  596. return -EOPNOTSUPP;
  597. if (!capable(CAP_WAKE_ALARM))
  598. return -EPERM;
  599. type = clock2alarm(new_timer->it_clock);
  600. alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer);
  601. return 0;
  602. }
  603. /**
  604. * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
  605. * @alarm: ptr to alarm that fired
  606. *
  607. * Wakes up the task that set the alarmtimer
  608. */
  609. static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm,
  610. ktime_t now)
  611. {
  612. struct task_struct *task = (struct task_struct *)alarm->data;
  613. alarm->data = NULL;
  614. if (task)
  615. wake_up_process(task);
  616. return ALARMTIMER_NORESTART;
  617. }
  618. /**
  619. * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
  620. * @alarm: ptr to alarmtimer
  621. * @absexp: absolute expiration time
  622. *
  623. * Sets the alarm timer and sleeps until it is fired or interrupted.
  624. */
  625. static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp,
  626. enum alarmtimer_type type)
  627. {
  628. struct restart_block *restart;
  629. alarm->data = (void *)current;
  630. do {
  631. set_current_state(TASK_INTERRUPTIBLE);
  632. alarm_start(alarm, absexp);
  633. if (likely(alarm->data))
  634. schedule();
  635. alarm_cancel(alarm);
  636. } while (alarm->data && !signal_pending(current));
  637. __set_current_state(TASK_RUNNING);
  638. destroy_hrtimer_on_stack(&alarm->timer);
  639. if (!alarm->data)
  640. return 0;
  641. if (freezing(current))
  642. alarmtimer_freezerset(absexp, type);
  643. restart = &current->restart_block;
  644. if (restart->nanosleep.type != TT_NONE) {
  645. struct timespec64 rmt;
  646. ktime_t rem;
  647. rem = ktime_sub(absexp, alarm_bases[type].gettime());
  648. if (rem <= 0)
  649. return 0;
  650. rmt = ktime_to_timespec64(rem);
  651. return nanosleep_copyout(restart, &rmt);
  652. }
  653. return -ERESTART_RESTARTBLOCK;
  654. }
  655. static void
  656. alarm_init_on_stack(struct alarm *alarm, enum alarmtimer_type type,
  657. enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
  658. {
  659. hrtimer_init_on_stack(&alarm->timer, alarm_bases[type].base_clockid,
  660. HRTIMER_MODE_ABS);
  661. __alarm_init(alarm, type, function);
  662. }
  663. /**
  664. * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
  665. * @restart: ptr to restart block
  666. *
  667. * Handles restarted clock_nanosleep calls
  668. */
  669. static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
  670. {
  671. enum alarmtimer_type type = restart->nanosleep.clockid;
  672. ktime_t exp = restart->nanosleep.expires;
  673. struct alarm alarm;
  674. alarm_init_on_stack(&alarm, type, alarmtimer_nsleep_wakeup);
  675. return alarmtimer_do_nsleep(&alarm, exp, type);
  676. }
  677. /**
  678. * alarm_timer_nsleep - alarmtimer nanosleep
  679. * @which_clock: clockid
  680. * @flags: determins abstime or relative
  681. * @tsreq: requested sleep time (abs or rel)
  682. * @rmtp: remaining sleep time saved
  683. *
  684. * Handles clock_nanosleep calls against _ALARM clockids
  685. */
  686. static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
  687. const struct timespec64 *tsreq)
  688. {
  689. enum alarmtimer_type type = clock2alarm(which_clock);
  690. struct restart_block *restart = &current->restart_block;
  691. struct alarm alarm;
  692. ktime_t exp;
  693. int ret = 0;
  694. if (!alarmtimer_get_rtcdev())
  695. return -EOPNOTSUPP;
  696. if (flags & ~TIMER_ABSTIME)
  697. return -EINVAL;
  698. if (!capable(CAP_WAKE_ALARM))
  699. return -EPERM;
  700. alarm_init_on_stack(&alarm, type, alarmtimer_nsleep_wakeup);
  701. exp = timespec64_to_ktime(*tsreq);
  702. /* Convert (if necessary) to absolute time */
  703. if (flags != TIMER_ABSTIME) {
  704. ktime_t now = alarm_bases[type].gettime();
  705. exp = ktime_add_safe(now, exp);
  706. }
  707. ret = alarmtimer_do_nsleep(&alarm, exp, type);
  708. if (ret != -ERESTART_RESTARTBLOCK)
  709. return ret;
  710. /* abs timers don't set remaining time or restart */
  711. if (flags == TIMER_ABSTIME)
  712. return -ERESTARTNOHAND;
  713. restart->nanosleep.clockid = type;
  714. restart->nanosleep.expires = exp;
  715. set_restart_fn(restart, alarm_timer_nsleep_restart);
  716. return ret;
  717. }
  718. const struct k_clock alarm_clock = {
  719. .clock_getres = alarm_clock_getres,
  720. .clock_get = alarm_clock_get,
  721. .timer_create = alarm_timer_create,
  722. .timer_set = common_timer_set,
  723. .timer_del = common_timer_del,
  724. .timer_get = common_timer_get,
  725. .timer_arm = alarm_timer_arm,
  726. .timer_rearm = alarm_timer_rearm,
  727. .timer_forward = alarm_timer_forward,
  728. .timer_remaining = alarm_timer_remaining,
  729. .timer_try_to_cancel = alarm_timer_try_to_cancel,
  730. .nsleep = alarm_timer_nsleep,
  731. };
  732. #endif /* CONFIG_POSIX_TIMERS */
  733. /* Suspend hook structures */
  734. static const struct dev_pm_ops alarmtimer_pm_ops = {
  735. .suspend = alarmtimer_suspend,
  736. .resume = alarmtimer_resume,
  737. };
  738. static struct platform_driver alarmtimer_driver = {
  739. .driver = {
  740. .name = "alarmtimer",
  741. .pm = &alarmtimer_pm_ops,
  742. }
  743. };
  744. /**
  745. * alarmtimer_init - Initialize alarm timer code
  746. *
  747. * This function initializes the alarm bases and registers
  748. * the posix clock ids.
  749. */
  750. static int __init alarmtimer_init(void)
  751. {
  752. struct platform_device *pdev;
  753. int error = 0;
  754. int i;
  755. alarmtimer_rtc_timer_init();
  756. /* Initialize alarm bases */
  757. alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
  758. alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
  759. alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
  760. alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
  761. for (i = 0; i < ALARM_NUMTYPE; i++) {
  762. timerqueue_init_head(&alarm_bases[i].timerqueue);
  763. spin_lock_init(&alarm_bases[i].lock);
  764. }
  765. error = alarmtimer_rtc_interface_setup();
  766. if (error)
  767. return error;
  768. error = platform_driver_register(&alarmtimer_driver);
  769. if (error)
  770. goto out_if;
  771. pdev = platform_device_register_simple("alarmtimer", -1, NULL, 0);
  772. if (IS_ERR(pdev)) {
  773. error = PTR_ERR(pdev);
  774. goto out_drv;
  775. }
  776. return 0;
  777. out_drv:
  778. platform_driver_unregister(&alarmtimer_driver);
  779. out_if:
  780. alarmtimer_rtc_interface_remove();
  781. return error;
  782. }
  783. device_initcall(alarmtimer_init);