2 * Copyright 2012 Red Hat Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
27 nvkm_timer_read(struct nvkm_timer *tmr)
29 return tmr->func->read(tmr);
33 nvkm_timer_alarm_trigger(struct nvkm_timer *tmr)
35 struct nvkm_alarm *alarm, *atemp;
39 /* Process pending alarms. */
40 spin_lock_irqsave(&tmr->lock, flags);
41 list_for_each_entry_safe(alarm, atemp, &tmr->alarms, head) {
42 /* Have we hit the earliest alarm that hasn't gone off? */
43 if (alarm->timestamp > nvkm_timer_read(tmr)) {
44 /* Schedule it. If we didn't race, we're done. */
45 tmr->func->alarm_init(tmr, alarm->timestamp);
46 if (alarm->timestamp > nvkm_timer_read(tmr))
50 /* Move to completed list. We'll drop the lock before
51 * executing the callback so it can reschedule itself.
53 list_del_init(&alarm->head);
54 list_add(&alarm->exec, &exec);
57 /* Shut down interrupt if no more pending alarms. */
58 if (list_empty(&tmr->alarms))
59 tmr->func->alarm_fini(tmr);
60 spin_unlock_irqrestore(&tmr->lock, flags);
62 /* Execute completed callbacks. */
63 list_for_each_entry_safe(alarm, atemp, &exec, exec) {
64 list_del(&alarm->exec);
70 nvkm_timer_alarm(struct nvkm_timer *tmr, u32 nsec, struct nvkm_alarm *alarm)
72 struct nvkm_alarm *list;
75 /* Remove alarm from pending list.
77 * This both protects against the corruption of the list,
78 * and implements alarm rescheduling/cancellation.
80 spin_lock_irqsave(&tmr->lock, flags);
81 list_del_init(&alarm->head);
84 /* Insert into pending list, ordered earliest to latest. */
85 alarm->timestamp = nvkm_timer_read(tmr) + nsec;
86 list_for_each_entry(list, &tmr->alarms, head) {
87 if (list->timestamp > alarm->timestamp)
91 list_add_tail(&alarm->head, &list->head);
93 /* Update HW if this is now the earliest alarm. */
94 list = list_first_entry(&tmr->alarms, typeof(*list), head);
96 tmr->func->alarm_init(tmr, alarm->timestamp);
97 /* This shouldn't happen if callers aren't stupid.
99 * Worst case scenario is that it'll take roughly
100 * 4 seconds for the next alarm to trigger.
102 WARN_ON(alarm->timestamp <= nvkm_timer_read(tmr));
105 spin_unlock_irqrestore(&tmr->lock, flags);
109 nvkm_timer_intr(struct nvkm_subdev *subdev)
111 struct nvkm_timer *tmr = nvkm_timer(subdev);
112 tmr->func->intr(tmr);
116 nvkm_timer_fini(struct nvkm_subdev *subdev, bool suspend)
118 struct nvkm_timer *tmr = nvkm_timer(subdev);
119 tmr->func->alarm_fini(tmr);
124 nvkm_timer_init(struct nvkm_subdev *subdev)
126 struct nvkm_timer *tmr = nvkm_timer(subdev);
128 tmr->func->init(tmr);
129 tmr->func->time(tmr, ktime_to_ns(ktime_get()));
130 nvkm_timer_alarm_trigger(tmr);
135 nvkm_timer_dtor(struct nvkm_subdev *subdev)
137 return nvkm_timer(subdev);
140 static const struct nvkm_subdev_func
142 .dtor = nvkm_timer_dtor,
143 .init = nvkm_timer_init,
144 .fini = nvkm_timer_fini,
145 .intr = nvkm_timer_intr,
149 nvkm_timer_new_(const struct nvkm_timer_func *func, struct nvkm_device *device,
150 int index, struct nvkm_timer **ptmr)
152 struct nvkm_timer *tmr;
154 if (!(tmr = *ptmr = kzalloc(sizeof(*tmr), GFP_KERNEL)))
157 nvkm_subdev_ctor(&nvkm_timer, device, index, &tmr->subdev);
159 INIT_LIST_HEAD(&tmr->alarms);
160 spin_lock_init(&tmr->lock);