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