sys/dev/drm/linux_hrtimer.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | /* * Copyright (c) 2017-2019 François Tigeot <ftigeot@wolfpond.org> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice unmodified, this list of conditions, and the following * disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include <linux/hrtimer.h> #include <linux/bug.h> #include <sys/systimer.h> #include <sys/taskqueue.h> static void __hrtimer_function(systimer_t info, int in_ipi, struct intrframe *frame) { struct hrtimer *timer = info->data; struct taskqueue *tq = taskqueue_thread[mycpuid]; BUG_ON(taskqueue_enqueue(tq, &timer->task) != 0); } static void __hrtimer_task(void *arg, int pending) { struct hrtimer *timer = arg; enum hrtimer_restart restart; lwkt_gettoken(&timer->timer_token); timer->running = true; if (timer->cancel) { timer->running = false; timer->active = false; goto done; } lwkt_reltoken(&timer->timer_token); KKASSERT(timer->function != NULL); restart = timer->function(timer); lwkt_gettoken(&timer->timer_token); timer->running = false; timer->active = false; if (!timer->cancel && restart == HRTIMER_RESTART) { KKASSERT(timer->is_rel); timer->active = true; systimer_init_oneshot(&timer->st, __hrtimer_function, timer, timer->timeout_us); } done: lwkt_reltoken(&timer->timer_token); } void hrtimer_init(struct hrtimer *timer, clockid_t clock_id, enum hrtimer_mode mode) { BUG_ON(clock_id != CLOCK_MONOTONIC); memset(timer, 0, sizeof(struct hrtimer)); timer->clock_id = clock_id; timer->ht_mode = mode; timer->active = false; timer->running = false; timer->cancel = false; timer->gd = NULL; TASK_INIT(&timer->task, 1, __hrtimer_task, timer); lwkt_token_init(&timer->timer_token, "timer token"); } void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim, u64 range_ns, const enum hrtimer_mode mode) { if (mode == HRTIMER_MODE_ABS) { struct timespec ts; ktime_t curtime; timer->is_rel = false; nanouptime(&ts); curtime = ts.tv_sec * 1000000000 + ts.tv_nsec; if (curtime > tim) timer->timeout_us = 500; else timer->timeout_us = (tim - curtime) / 1000; } else { timer->timeout_us = tim / 1000; timer->is_rel = true; } /* Prevent arbitrarily short timeouts from being scheduled. */ timer->timeout_us = max(500, timer->timeout_us); lwkt_gettoken(&timer->timer_token); timer->cancel = false; timer->running = false; timer->active = true; timer->gd = mycpu; systimer_init_oneshot(&timer->st, __hrtimer_function, timer, timer->timeout_us); lwkt_reltoken(&timer->timer_token); } int hrtimer_cancel(struct hrtimer *timer) { int ret = 0; lwkt_gettoken(&timer->timer_token); if (timer->active) { struct taskqueue *tq = taskqueue_thread[timer->gd->gd_cpuid]; bool running = timer->running; ret = 1; timer->cancel = true; lwkt_reltoken(&timer->timer_token); if (running) { /* Nothing */ } else if (timer->gd == mycpu) { systimer_del(&timer->st); } else { struct globaldata *oldcpu = mycpu; lwkt_setcpu_self(timer->gd); systimer_del(&timer->st); lwkt_setcpu_self(oldcpu); } while (taskqueue_cancel(tq, &timer->task, NULL)) taskqueue_drain(tq, &timer->task); } else { lwkt_reltoken(&timer->timer_token); } return ret; } /* Returns non-zero if the timer is queued or running */ bool hrtimer_active(const struct hrtimer *const_timer) { bool ret; struct hrtimer *timer = __DECONST(struct hrtimer *, const_timer); lwkt_gettoken(&timer->timer_token); ret = timer->active; lwkt_reltoken(&timer->timer_token); return ret; } |