alarmtimer.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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/alarmtimer.h>
  23. #include <linux/mutex.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/posix-timers.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/freezer.h>
  28. #define ALARM_DELTA 300
  29. /**
  30. * struct alarm_base - Alarm timer bases
  31. * @lock: Lock for syncrhonized access to the base
  32. * @timerqueue: Timerqueue head managing the list of events
  33. * @timer: hrtimer used to schedule events while running
  34. * @gettime: Function to read the time correlating to the base
  35. * @base_clockid: clockid for the base
  36. */
  37. static struct alarm_base {
  38. spinlock_t lock;
  39. struct timerqueue_head timerqueue;
  40. ktime_t (*gettime)(void);
  41. clockid_t base_clockid;
  42. } alarm_bases[ALARM_NUMTYPE];
  43. /* freezer delta & lock used to handle clock_nanosleep triggered wakeups */
  44. static ktime_t freezer_delta;
  45. static DEFINE_SPINLOCK(freezer_delta_lock);
  46. static struct wakeup_source *ws;
  47. #ifdef CONFIG_RTC_CLASS
  48. /* rtc timer and device for setting alarm wakeups at suspend */
  49. static struct rtc_timer rtctimer;
  50. static struct rtc_device *rtcdev;
  51. static DEFINE_SPINLOCK(rtcdev_lock);
  52. static unsigned long power_on_alarm;
  53. static struct mutex power_on_alarm_lock;
  54. void power_on_alarm_init(void)
  55. {
  56. struct rtc_wkalrm rtc_alarm;
  57. struct rtc_time rt;
  58. unsigned long alarm_time;
  59. struct rtc_device *rtc;
  60. rtc = alarmtimer_get_rtcdev();
  61. /* If we have no rtcdev, just return */
  62. if (!rtc)
  63. return;
  64. rtc_read_alarm(rtc, &rtc_alarm);
  65. rt = rtc_alarm.time;
  66. rtc_tm_to_time(&rt, &alarm_time);
  67. if (alarm_time)
  68. power_on_alarm = alarm_time + ALARM_DELTA;
  69. else
  70. power_on_alarm = 0;
  71. }
  72. void set_power_on_alarm(long secs, bool enable)
  73. {
  74. int rc;
  75. struct timespec wall_time;
  76. long rtc_secs, alarm_time, alarm_delta;
  77. struct rtc_time rtc_time;
  78. struct rtc_wkalrm alarm;
  79. rc = mutex_lock_interruptible(&power_on_alarm_lock);
  80. if (rc != 0)
  81. return;
  82. if (enable) {
  83. power_on_alarm = secs;
  84. } else {
  85. if (power_on_alarm == secs)
  86. power_on_alarm = 0;
  87. else
  88. goto exit;
  89. }
  90. if (!power_on_alarm)
  91. goto disable_alarm;
  92. rtc_read_time(rtcdev, &rtc_time);
  93. getnstimeofday(&wall_time);
  94. rtc_tm_to_time(&rtc_time, &rtc_secs);
  95. alarm_delta = wall_time.tv_sec - rtc_secs;
  96. alarm_time = power_on_alarm - alarm_delta;
  97. /*
  98. *Substract ALARM_DELTA from actual alarm time
  99. *to power up the device before actual alarm
  100. *expiration
  101. */
  102. if ((alarm_time - ALARM_DELTA) > rtc_secs)
  103. alarm_time -= ALARM_DELTA;
  104. else
  105. goto disable_alarm;
  106. rtc_time_to_tm(alarm_time, &alarm.time);
  107. alarm.enabled = 1;
  108. rc = rtc_set_alarm(rtcdev, &alarm);
  109. if (rc)
  110. goto disable_alarm;
  111. mutex_unlock(&power_on_alarm_lock);
  112. return;
  113. disable_alarm:
  114. power_on_alarm = 0;
  115. rtc_alarm_irq_enable(rtcdev, 0);
  116. exit:
  117. mutex_unlock(&power_on_alarm_lock);
  118. }
  119. static void alarmtimer_triggered_func(void *p)
  120. {
  121. struct rtc_device *rtc = rtcdev;
  122. if (!(rtc->irq_data & RTC_AF))
  123. return;
  124. __pm_wakeup_event(ws, 2 * MSEC_PER_SEC);
  125. }
  126. static struct rtc_task alarmtimer_rtc_task = {
  127. .func = alarmtimer_triggered_func
  128. };
  129. /**
  130. * alarmtimer_get_rtcdev - Return selected rtcdevice
  131. *
  132. * This function returns the rtc device to use for wakealarms.
  133. * If one has not already been chosen, it checks to see if a
  134. * functional rtc device is available.
  135. */
  136. struct rtc_device *alarmtimer_get_rtcdev(void)
  137. {
  138. unsigned long flags;
  139. struct rtc_device *ret = NULL;
  140. spin_lock_irqsave(&rtcdev_lock, flags);
  141. ret = rtcdev;
  142. spin_unlock_irqrestore(&rtcdev_lock, flags);
  143. return ret;
  144. }
  145. static int alarmtimer_rtc_add_device(struct device *dev,
  146. struct class_interface *class_intf)
  147. {
  148. unsigned long flags;
  149. int err = 0;
  150. struct rtc_device *rtc = to_rtc_device(dev);
  151. if (rtcdev)
  152. return -EBUSY;
  153. if (!rtc->ops->set_alarm)
  154. return -1;
  155. spin_lock_irqsave(&rtcdev_lock, flags);
  156. if (!rtcdev) {
  157. err = rtc_irq_register(rtc, &alarmtimer_rtc_task);
  158. if (err)
  159. goto rtc_irq_reg_err;
  160. rtcdev = rtc;
  161. /* hold a reference so it doesn't go away */
  162. get_device(dev);
  163. }
  164. rtc_irq_reg_err:
  165. spin_unlock_irqrestore(&rtcdev_lock, flags);
  166. return err;
  167. }
  168. static void alarmtimer_rtc_remove_device(struct device *dev,
  169. struct class_interface *class_intf)
  170. {
  171. if (rtcdev && dev == &rtcdev->dev) {
  172. rtc_irq_unregister(rtcdev, &alarmtimer_rtc_task);
  173. rtcdev = NULL;
  174. }
  175. }
  176. static inline void alarmtimer_rtc_timer_init(void)
  177. {
  178. mutex_init(&power_on_alarm_lock);
  179. rtc_timer_init(&rtctimer, NULL, NULL);
  180. }
  181. static struct class_interface alarmtimer_rtc_interface = {
  182. .add_dev = &alarmtimer_rtc_add_device,
  183. .remove_dev = &alarmtimer_rtc_remove_device,
  184. };
  185. static int alarmtimer_rtc_interface_setup(void)
  186. {
  187. alarmtimer_rtc_interface.class = rtc_class;
  188. return class_interface_register(&alarmtimer_rtc_interface);
  189. }
  190. static void alarmtimer_rtc_interface_remove(void)
  191. {
  192. class_interface_unregister(&alarmtimer_rtc_interface);
  193. }
  194. #else
  195. struct rtc_device *alarmtimer_get_rtcdev(void)
  196. {
  197. return NULL;
  198. }
  199. #define rtcdev (NULL)
  200. static inline int alarmtimer_rtc_interface_setup(void) { return 0; }
  201. static inline void alarmtimer_rtc_interface_remove(void) { }
  202. static inline void alarmtimer_rtc_timer_init(void) { }
  203. void set_power_on_alarm(long secs, bool enable) { }
  204. #endif
  205. /**
  206. * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
  207. * @base: pointer to the base where the timer is being run
  208. * @alarm: pointer to alarm being enqueued.
  209. *
  210. * Adds alarm to a alarm_base timerqueue
  211. *
  212. * Must hold base->lock when calling.
  213. */
  214. static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
  215. {
  216. if (alarm->state & ALARMTIMER_STATE_ENQUEUED)
  217. timerqueue_del(&base->timerqueue, &alarm->node);
  218. timerqueue_add(&base->timerqueue, &alarm->node);
  219. alarm->state |= ALARMTIMER_STATE_ENQUEUED;
  220. }
  221. /**
  222. * alarmtimer_dequeue - Removes an alarm timer from an alarm_base timerqueue
  223. * @base: pointer to the base where the timer is running
  224. * @alarm: pointer to alarm being removed
  225. *
  226. * Removes alarm to a alarm_base timerqueue
  227. *
  228. * Must hold base->lock when calling.
  229. */
  230. static void alarmtimer_dequeue(struct alarm_base *base, struct alarm *alarm)
  231. {
  232. if (!(alarm->state & ALARMTIMER_STATE_ENQUEUED))
  233. return;
  234. timerqueue_del(&base->timerqueue, &alarm->node);
  235. alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
  236. }
  237. /**
  238. * alarmtimer_fired - Handles alarm hrtimer being fired.
  239. * @timer: pointer to hrtimer being run
  240. *
  241. * When a alarm timer fires, this runs through the timerqueue to
  242. * see which alarms expired, and runs those. If there are more alarm
  243. * timers queued for the future, we set the hrtimer to fire when
  244. * when the next future alarm timer expires.
  245. */
  246. static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
  247. {
  248. struct alarm *alarm = container_of(timer, struct alarm, timer);
  249. struct alarm_base *base = &alarm_bases[alarm->type];
  250. unsigned long flags;
  251. int ret = HRTIMER_NORESTART;
  252. int restart = ALARMTIMER_NORESTART;
  253. spin_lock_irqsave(&base->lock, flags);
  254. alarmtimer_dequeue(base, alarm);
  255. spin_unlock_irqrestore(&base->lock, flags);
  256. if (alarm->function)
  257. restart = alarm->function(alarm, base->gettime());
  258. spin_lock_irqsave(&base->lock, flags);
  259. if (restart != ALARMTIMER_NORESTART) {
  260. hrtimer_set_expires(&alarm->timer, alarm->node.expires);
  261. alarmtimer_enqueue(base, alarm);
  262. ret = HRTIMER_RESTART;
  263. }
  264. spin_unlock_irqrestore(&base->lock, flags);
  265. return ret;
  266. }
  267. ktime_t alarm_expires_remaining(const struct alarm *alarm)
  268. {
  269. struct alarm_base *base = &alarm_bases[alarm->type];
  270. return ktime_sub(alarm->node.expires, base->gettime());
  271. }
  272. #ifdef CONFIG_RTC_CLASS
  273. /**
  274. * alarmtimer_suspend - Suspend time callback
  275. * @dev: unused
  276. * @state: unused
  277. *
  278. * When we are going into suspend, we look through the bases
  279. * to see which is the soonest timer to expire. We then
  280. * set an rtc timer to fire that far into the future, which
  281. * will wake us from suspend.
  282. */
  283. static int alarmtimer_suspend(struct device *dev)
  284. {
  285. struct rtc_time tm;
  286. ktime_t min, now;
  287. unsigned long flags;
  288. struct rtc_device *rtc;
  289. int i;
  290. int ret;
  291. spin_lock_irqsave(&freezer_delta_lock, flags);
  292. min = freezer_delta;
  293. freezer_delta = ktime_set(0, 0);
  294. spin_unlock_irqrestore(&freezer_delta_lock, flags);
  295. rtc = alarmtimer_get_rtcdev();
  296. /* If we have no rtcdev, just return */
  297. if (!rtc)
  298. return 0;
  299. /* Find the soonest timer to expire*/
  300. for (i = 0; i < ALARM_NUMTYPE; i++) {
  301. struct alarm_base *base = &alarm_bases[i];
  302. struct timerqueue_node *next;
  303. ktime_t delta;
  304. spin_lock_irqsave(&base->lock, flags);
  305. next = timerqueue_getnext(&base->timerqueue);
  306. spin_unlock_irqrestore(&base->lock, flags);
  307. if (!next)
  308. continue;
  309. delta = ktime_sub(next->expires, base->gettime());
  310. if (!min.tv64 || (delta.tv64 < min.tv64))
  311. min = delta;
  312. }
  313. if (min.tv64 == 0)
  314. return 0;
  315. if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) {
  316. __pm_wakeup_event(ws, 2 * MSEC_PER_SEC);
  317. return -EBUSY;
  318. }
  319. /* Setup an rtc timer to fire that far in the future */
  320. rtc_timer_cancel(rtc, &rtctimer);
  321. rtc_read_time(rtc, &tm);
  322. now = rtc_tm_to_ktime(tm);
  323. now = ktime_add(now, min);
  324. /* Set alarm, if in the past reject suspend briefly to handle */
  325. ret = rtc_timer_start(rtc, &rtctimer, now, ktime_set(0, 0));
  326. if (ret < 0)
  327. __pm_wakeup_event(ws, 1 * MSEC_PER_SEC);
  328. return ret;
  329. }
  330. static int alarmtimer_resume(struct device *dev)
  331. {
  332. struct rtc_device *rtc;
  333. rtc = alarmtimer_get_rtcdev();
  334. /* If we have no rtcdev, just return */
  335. if (!rtc)
  336. return 0;
  337. rtc_timer_cancel(rtc, &rtctimer);
  338. return 0;
  339. }
  340. #else
  341. static int alarmtimer_suspend(struct device *dev)
  342. {
  343. return 0;
  344. }
  345. static int alarmtimer_resume(struct device *dev)
  346. {
  347. return 0;
  348. }
  349. #endif
  350. static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
  351. {
  352. ktime_t delta;
  353. unsigned long flags;
  354. struct alarm_base *base = &alarm_bases[type];
  355. delta = ktime_sub(absexp, base->gettime());
  356. spin_lock_irqsave(&freezer_delta_lock, flags);
  357. if (!freezer_delta.tv64 || (delta.tv64 < freezer_delta.tv64))
  358. freezer_delta = delta;
  359. spin_unlock_irqrestore(&freezer_delta_lock, flags);
  360. }
  361. /**
  362. * alarm_init - Initialize an alarm structure
  363. * @alarm: ptr to alarm to be initialized
  364. * @type: the type of the alarm
  365. * @function: callback that is run when the alarm fires
  366. */
  367. void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
  368. enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
  369. {
  370. timerqueue_init(&alarm->node);
  371. hrtimer_init(&alarm->timer, alarm_bases[type].base_clockid,
  372. HRTIMER_MODE_ABS);
  373. alarm->timer.function = alarmtimer_fired;
  374. alarm->function = function;
  375. alarm->type = type;
  376. alarm->state = ALARMTIMER_STATE_INACTIVE;
  377. }
  378. /**
  379. * alarm_start - Sets an absolute alarm to fire
  380. * @alarm: ptr to alarm to set
  381. * @start: time to run the alarm
  382. */
  383. int alarm_start(struct alarm *alarm, ktime_t start)
  384. {
  385. struct alarm_base *base = &alarm_bases[alarm->type];
  386. unsigned long flags;
  387. int ret;
  388. spin_lock_irqsave(&base->lock, flags);
  389. alarm->node.expires = start;
  390. alarmtimer_enqueue(base, alarm);
  391. ret = hrtimer_start(&alarm->timer, alarm->node.expires,
  392. HRTIMER_MODE_ABS);
  393. spin_unlock_irqrestore(&base->lock, flags);
  394. return ret;
  395. }
  396. /**
  397. * alarm_start_relative - Sets a relative alarm to fire
  398. * @alarm: ptr to alarm to set
  399. * @start: time relative to now to run the alarm
  400. */
  401. int alarm_start_relative(struct alarm *alarm, ktime_t start)
  402. {
  403. struct alarm_base *base;
  404. if (alarm->type >= ALARM_NUMTYPE) {
  405. pr_err("Array out of index\n");
  406. return -EINVAL;
  407. }
  408. base = &alarm_bases[alarm->type];
  409. start = ktime_add(start, base->gettime());
  410. return alarm_start(alarm, start);
  411. }
  412. void alarm_restart(struct alarm *alarm)
  413. {
  414. struct alarm_base *base = &alarm_bases[alarm->type];
  415. unsigned long flags;
  416. spin_lock_irqsave(&base->lock, flags);
  417. hrtimer_set_expires(&alarm->timer, alarm->node.expires);
  418. hrtimer_restart(&alarm->timer);
  419. alarmtimer_enqueue(base, alarm);
  420. spin_unlock_irqrestore(&base->lock, flags);
  421. }
  422. /**
  423. * alarm_try_to_cancel - Tries to cancel an alarm timer
  424. * @alarm: ptr to alarm to be canceled
  425. *
  426. * Returns 1 if the timer was canceled, 0 if it was not running,
  427. * and -1 if the callback was running
  428. */
  429. int alarm_try_to_cancel(struct alarm *alarm)
  430. {
  431. struct alarm_base *base;
  432. unsigned long flags;
  433. int ret;
  434. if (alarm->type >= ALARM_NUMTYPE) {
  435. pr_err("Array out of index\n");
  436. return -EINVAL;
  437. }
  438. base = &alarm_bases[alarm->type];
  439. spin_lock_irqsave(&base->lock, flags);
  440. ret = hrtimer_try_to_cancel(&alarm->timer);
  441. if (ret >= 0)
  442. alarmtimer_dequeue(base, alarm);
  443. spin_unlock_irqrestore(&base->lock, flags);
  444. return ret;
  445. }
  446. /**
  447. * alarm_cancel - Spins trying to cancel an alarm timer until it is done
  448. * @alarm: ptr to alarm to be canceled
  449. *
  450. * Returns 1 if the timer was canceled, 0 if it was not active.
  451. */
  452. int alarm_cancel(struct alarm *alarm)
  453. {
  454. for (;;) {
  455. int ret = alarm_try_to_cancel(alarm);
  456. if (ret >= 0)
  457. return ret;
  458. cpu_relax();
  459. }
  460. }
  461. u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval)
  462. {
  463. u64 overrun = 1;
  464. ktime_t delta;
  465. delta = ktime_sub(now, alarm->node.expires);
  466. if (delta.tv64 < 0)
  467. return 0;
  468. if (unlikely(delta.tv64 >= interval.tv64)) {
  469. s64 incr = ktime_to_ns(interval);
  470. overrun = ktime_divns(delta, incr);
  471. alarm->node.expires = ktime_add_ns(alarm->node.expires,
  472. incr*overrun);
  473. if (alarm->node.expires.tv64 > now.tv64)
  474. return overrun;
  475. /*
  476. * This (and the ktime_add() below) is the
  477. * correction for exact:
  478. */
  479. overrun++;
  480. }
  481. alarm->node.expires = ktime_add(alarm->node.expires, interval);
  482. return overrun;
  483. }
  484. u64 alarm_forward_now(struct alarm *alarm, ktime_t interval)
  485. {
  486. struct alarm_base *base = &alarm_bases[alarm->type];
  487. return alarm_forward(alarm, base->gettime(), interval);
  488. }
  489. /**
  490. * clock2alarm - helper that converts from clockid to alarmtypes
  491. * @clockid: clockid.
  492. */
  493. static enum alarmtimer_type clock2alarm(clockid_t clockid)
  494. {
  495. if (clockid == CLOCK_REALTIME_ALARM)
  496. return ALARM_REALTIME;
  497. if (clockid == CLOCK_BOOTTIME_ALARM)
  498. return ALARM_BOOTTIME;
  499. return -1;
  500. }
  501. /**
  502. * alarm_handle_timer - Callback for posix timers
  503. * @alarm: alarm that fired
  504. *
  505. * Posix timer callback for expired alarm timers.
  506. */
  507. static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm,
  508. ktime_t now)
  509. {
  510. unsigned long flags;
  511. struct k_itimer *ptr = container_of(alarm, struct k_itimer,
  512. it.alarm.alarmtimer);
  513. enum alarmtimer_restart result = ALARMTIMER_NORESTART;
  514. spin_lock_irqsave(&ptr->it_lock, flags);
  515. if ((ptr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) {
  516. if (posix_timer_event(ptr, 0) != 0)
  517. ptr->it_overrun++;
  518. }
  519. /* Re-add periodic timers */
  520. if (ptr->it.alarm.interval.tv64) {
  521. ptr->it_overrun += alarm_forward(alarm, now,
  522. ptr->it.alarm.interval);
  523. result = ALARMTIMER_RESTART;
  524. }
  525. spin_unlock_irqrestore(&ptr->it_lock, flags);
  526. return result;
  527. }
  528. /**
  529. * alarm_clock_getres - posix getres interface
  530. * @which_clock: clockid
  531. * @tp: timespec to fill
  532. *
  533. * Returns the granularity of underlying alarm base clock
  534. */
  535. static int alarm_clock_getres(const clockid_t which_clock, struct timespec *tp)
  536. {
  537. clockid_t baseid = alarm_bases[clock2alarm(which_clock)].base_clockid;
  538. if (!alarmtimer_get_rtcdev())
  539. return -EINVAL;
  540. return hrtimer_get_res(baseid, tp);
  541. }
  542. /**
  543. * alarm_clock_get - posix clock_get interface
  544. * @which_clock: clockid
  545. * @tp: timespec to fill.
  546. *
  547. * Provides the underlying alarm base time.
  548. */
  549. static int alarm_clock_get(clockid_t which_clock, struct timespec *tp)
  550. {
  551. struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
  552. if (!alarmtimer_get_rtcdev())
  553. return -EINVAL;
  554. *tp = ktime_to_timespec(base->gettime());
  555. return 0;
  556. }
  557. /**
  558. * alarm_timer_create - posix timer_create interface
  559. * @new_timer: k_itimer pointer to manage
  560. *
  561. * Initializes the k_itimer structure.
  562. */
  563. static int alarm_timer_create(struct k_itimer *new_timer)
  564. {
  565. enum alarmtimer_type type;
  566. struct alarm_base *base;
  567. if (!alarmtimer_get_rtcdev())
  568. return -EOPNOTSUPP;
  569. if (!capable(CAP_WAKE_ALARM))
  570. return -EPERM;
  571. type = clock2alarm(new_timer->it_clock);
  572. base = &alarm_bases[type];
  573. alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer);
  574. return 0;
  575. }
  576. /**
  577. * alarm_timer_get - posix timer_get interface
  578. * @new_timer: k_itimer pointer
  579. * @cur_setting: itimerspec data to fill
  580. *
  581. * Copies out the current itimerspec data
  582. */
  583. static void alarm_timer_get(struct k_itimer *timr,
  584. struct itimerspec *cur_setting)
  585. {
  586. ktime_t relative_expiry_time =
  587. alarm_expires_remaining(&(timr->it.alarm.alarmtimer));
  588. if (ktime_to_ns(relative_expiry_time) > 0) {
  589. cur_setting->it_value = ktime_to_timespec(relative_expiry_time);
  590. } else {
  591. cur_setting->it_value.tv_sec = 0;
  592. cur_setting->it_value.tv_nsec = 0;
  593. }
  594. cur_setting->it_interval = ktime_to_timespec(timr->it.alarm.interval);
  595. }
  596. /**
  597. * alarm_timer_del - posix timer_del interface
  598. * @timr: k_itimer pointer to be deleted
  599. *
  600. * Cancels any programmed alarms for the given timer.
  601. */
  602. static int alarm_timer_del(struct k_itimer *timr)
  603. {
  604. if (!rtcdev)
  605. return -EOPNOTSUPP;
  606. if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
  607. return TIMER_RETRY;
  608. return 0;
  609. }
  610. /**
  611. * alarm_timer_set - posix timer_set interface
  612. * @timr: k_itimer pointer to be deleted
  613. * @flags: timer flags
  614. * @new_setting: itimerspec to be used
  615. * @old_setting: itimerspec being replaced
  616. *
  617. * Sets the timer to new_setting, and starts the timer.
  618. */
  619. static int alarm_timer_set(struct k_itimer *timr, int flags,
  620. struct itimerspec *new_setting,
  621. struct itimerspec *old_setting)
  622. {
  623. ktime_t exp;
  624. if (!rtcdev)
  625. return -EOPNOTSUPP;
  626. if (flags & ~TIMER_ABSTIME)
  627. return -EINVAL;
  628. if (old_setting)
  629. alarm_timer_get(timr, old_setting);
  630. /* If the timer was already set, cancel it */
  631. if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
  632. return TIMER_RETRY;
  633. /* start the timer */
  634. timr->it.alarm.interval = timespec_to_ktime(new_setting->it_interval);
  635. /*
  636. * Rate limit to the tick as a hot fix to prevent DOS. Will be
  637. * mopped up later.
  638. */
  639. if (timr->it.alarm.interval.tv64 &&
  640. ktime_to_ns(timr->it.alarm.interval) < TICK_NSEC)
  641. timr->it.alarm.interval = ktime_set(0, TICK_NSEC);
  642. exp = timespec_to_ktime(new_setting->it_value);
  643. /* Convert (if necessary) to absolute time */
  644. if (flags != TIMER_ABSTIME) {
  645. ktime_t now;
  646. now = alarm_bases[timr->it.alarm.alarmtimer.type].gettime();
  647. exp = ktime_add(now, exp);
  648. }
  649. alarm_start(&timr->it.alarm.alarmtimer, exp);
  650. return 0;
  651. }
  652. /**
  653. * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
  654. * @alarm: ptr to alarm that fired
  655. *
  656. * Wakes up the task that set the alarmtimer
  657. */
  658. static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm,
  659. ktime_t now)
  660. {
  661. struct task_struct *task = (struct task_struct *)alarm->data;
  662. alarm->data = NULL;
  663. if (task)
  664. wake_up_process(task);
  665. return ALARMTIMER_NORESTART;
  666. }
  667. /**
  668. * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
  669. * @alarm: ptr to alarmtimer
  670. * @absexp: absolute expiration time
  671. *
  672. * Sets the alarm timer and sleeps until it is fired or interrupted.
  673. */
  674. static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp)
  675. {
  676. alarm->data = (void *)current;
  677. do {
  678. set_current_state(TASK_INTERRUPTIBLE);
  679. alarm_start(alarm, absexp);
  680. if (likely(alarm->data))
  681. schedule();
  682. alarm_cancel(alarm);
  683. } while (alarm->data && !signal_pending(current));
  684. __set_current_state(TASK_RUNNING);
  685. return (alarm->data == NULL);
  686. }
  687. /**
  688. * update_rmtp - Update remaining timespec value
  689. * @exp: expiration time
  690. * @type: timer type
  691. * @rmtp: user pointer to remaining timepsec value
  692. *
  693. * Helper function that fills in rmtp value with time between
  694. * now and the exp value
  695. */
  696. static int update_rmtp(ktime_t exp, enum alarmtimer_type type,
  697. struct timespec __user *rmtp)
  698. {
  699. struct timespec rmt;
  700. ktime_t rem;
  701. rem = ktime_sub(exp, alarm_bases[type].gettime());
  702. if (rem.tv64 <= 0)
  703. return 0;
  704. rmt = ktime_to_timespec(rem);
  705. if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
  706. return -EFAULT;
  707. return 1;
  708. }
  709. /**
  710. * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
  711. * @restart: ptr to restart block
  712. *
  713. * Handles restarted clock_nanosleep calls
  714. */
  715. static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
  716. {
  717. enum alarmtimer_type type = restart->nanosleep.clockid;
  718. ktime_t exp;
  719. struct timespec __user *rmtp;
  720. struct alarm alarm;
  721. int ret = 0;
  722. exp.tv64 = restart->nanosleep.expires;
  723. alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
  724. if (alarmtimer_do_nsleep(&alarm, exp))
  725. goto out;
  726. if (freezing(current))
  727. alarmtimer_freezerset(exp, type);
  728. rmtp = restart->nanosleep.rmtp;
  729. if (rmtp) {
  730. ret = update_rmtp(exp, type, rmtp);
  731. if (ret <= 0)
  732. goto out;
  733. }
  734. /* The other values in restart are already filled in */
  735. ret = -ERESTART_RESTARTBLOCK;
  736. out:
  737. return ret;
  738. }
  739. /**
  740. * alarm_timer_nsleep - alarmtimer nanosleep
  741. * @which_clock: clockid
  742. * @flags: determins abstime or relative
  743. * @tsreq: requested sleep time (abs or rel)
  744. * @rmtp: remaining sleep time saved
  745. *
  746. * Handles clock_nanosleep calls against _ALARM clockids
  747. */
  748. static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
  749. struct timespec *tsreq, struct timespec __user *rmtp)
  750. {
  751. enum alarmtimer_type type = clock2alarm(which_clock);
  752. struct alarm alarm;
  753. ktime_t exp;
  754. int ret = 0;
  755. struct restart_block *restart;
  756. if (!alarmtimer_get_rtcdev())
  757. return -EOPNOTSUPP;
  758. if (flags & ~TIMER_ABSTIME)
  759. return -EINVAL;
  760. if (!capable(CAP_WAKE_ALARM))
  761. return -EPERM;
  762. alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
  763. exp = timespec_to_ktime(*tsreq);
  764. /* Convert (if necessary) to absolute time */
  765. if (flags != TIMER_ABSTIME) {
  766. ktime_t now = alarm_bases[type].gettime();
  767. exp = ktime_add_safe(now, exp);
  768. }
  769. if (alarmtimer_do_nsleep(&alarm, exp))
  770. goto out;
  771. if (freezing(current))
  772. alarmtimer_freezerset(exp, type);
  773. /* abs timers don't set remaining time or restart */
  774. if (flags == TIMER_ABSTIME) {
  775. ret = -ERESTARTNOHAND;
  776. goto out;
  777. }
  778. if (rmtp) {
  779. ret = update_rmtp(exp, type, rmtp);
  780. if (ret <= 0)
  781. goto out;
  782. }
  783. restart = &current_thread_info()->restart_block;
  784. restart->fn = alarm_timer_nsleep_restart;
  785. restart->nanosleep.clockid = type;
  786. restart->nanosleep.expires = exp.tv64;
  787. restart->nanosleep.rmtp = rmtp;
  788. ret = -ERESTART_RESTARTBLOCK;
  789. out:
  790. return ret;
  791. }
  792. /* Suspend hook structures */
  793. static const struct dev_pm_ops alarmtimer_pm_ops = {
  794. .suspend = alarmtimer_suspend,
  795. .resume = alarmtimer_resume,
  796. };
  797. static struct platform_driver alarmtimer_driver = {
  798. .driver = {
  799. .name = "alarmtimer",
  800. .pm = &alarmtimer_pm_ops,
  801. }
  802. };
  803. /**
  804. * alarmtimer_init - Initialize alarm timer code
  805. *
  806. * This function initializes the alarm bases and registers
  807. * the posix clock ids.
  808. */
  809. static int __init alarmtimer_init(void)
  810. {
  811. struct platform_device *pdev;
  812. int error = 0;
  813. int i;
  814. struct k_clock alarm_clock = {
  815. .clock_getres = alarm_clock_getres,
  816. .clock_get = alarm_clock_get,
  817. .timer_create = alarm_timer_create,
  818. .timer_set = alarm_timer_set,
  819. .timer_del = alarm_timer_del,
  820. .timer_get = alarm_timer_get,
  821. .nsleep = alarm_timer_nsleep,
  822. };
  823. alarmtimer_rtc_timer_init();
  824. posix_timers_register_clock(CLOCK_REALTIME_ALARM, &alarm_clock);
  825. posix_timers_register_clock(CLOCK_BOOTTIME_ALARM, &alarm_clock);
  826. /* Initialize alarm bases */
  827. alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
  828. alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
  829. alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
  830. alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
  831. for (i = 0; i < ALARM_NUMTYPE; i++) {
  832. timerqueue_init_head(&alarm_bases[i].timerqueue);
  833. spin_lock_init(&alarm_bases[i].lock);
  834. }
  835. error = alarmtimer_rtc_interface_setup();
  836. if (error)
  837. return error;
  838. error = platform_driver_register(&alarmtimer_driver);
  839. if (error)
  840. goto out_if;
  841. pdev = platform_device_register_simple("alarmtimer", -1, NULL, 0);
  842. if (IS_ERR(pdev)) {
  843. error = PTR_ERR(pdev);
  844. goto out_drv;
  845. }
  846. ws = wakeup_source_register(NULL, "alarmtimer");
  847. return 0;
  848. out_drv:
  849. platform_driver_unregister(&alarmtimer_driver);
  850. out_if:
  851. alarmtimer_rtc_interface_remove();
  852. return error;
  853. }
  854. device_initcall(alarmtimer_init);