xref: /openbsd/sys/dev/pci/drm/drm_vblank_work.c (revision 1bb76ff1)
1 // SPDX-License-Identifier: MIT
2 
3 #ifdef __linux__
4 #include <uapi/linux/sched/types.h>
5 #endif
6 
7 #include <drm/drm_print.h>
8 #include <drm/drm_vblank.h>
9 #include <drm/drm_vblank_work.h>
10 #include <drm/drm_crtc.h>
11 
12 #include "drm_internal.h"
13 
14 /**
15  * DOC: vblank works
16  *
17  * Many DRM drivers need to program hardware in a time-sensitive manner, many
18  * times with a deadline of starting and finishing within a certain region of
19  * the scanout. Most of the time the safest way to accomplish this is to
20  * simply do said time-sensitive programming in the driver's IRQ handler,
21  * which allows drivers to avoid being preempted during these critical
22  * regions. Or even better, the hardware may even handle applying such
23  * time-critical programming independently of the CPU.
24  *
25  * While there's a decent amount of hardware that's designed so that the CPU
26  * doesn't need to be concerned with extremely time-sensitive programming,
27  * there's a few situations where it can't be helped. Some unforgiving
28  * hardware may require that certain time-sensitive programming be handled
29  * completely by the CPU, and said programming may even take too long to
30  * handle in an IRQ handler. Another such situation would be where the driver
31  * needs to perform a task that needs to complete within a specific scanout
32  * period, but might possibly block and thus cannot be handled in an IRQ
33  * context. Both of these situations can't be solved perfectly in Linux since
34  * we're not a realtime kernel, and thus the scheduler may cause us to miss
35  * our deadline if it decides to preempt us. But for some drivers, it's good
36  * enough if we can lower our chance of being preempted to an absolute
37  * minimum.
38  *
39  * This is where &drm_vblank_work comes in. &drm_vblank_work provides a simple
40  * generic delayed work implementation which delays work execution until a
41  * particular vblank has passed, and then executes the work at realtime
42  * priority. This provides the best possible chance at performing
43  * time-sensitive hardware programming on time, even when the system is under
44  * heavy load. &drm_vblank_work also supports rescheduling, so that self
45  * re-arming work items can be easily implemented.
46  */
47 
drm_handle_vblank_works(struct drm_vblank_crtc * vblank)48 void drm_handle_vblank_works(struct drm_vblank_crtc *vblank)
49 {
50 	struct drm_vblank_work *work, *next;
51 	u64 count = atomic64_read(&vblank->count);
52 	bool wake = false;
53 
54 	assert_spin_locked(&vblank->dev->event_lock);
55 
56 	list_for_each_entry_safe(work, next, &vblank->pending_work, node) {
57 		if (!drm_vblank_passed(count, work->count))
58 			continue;
59 
60 		list_del_init(&work->node);
61 		drm_vblank_put(vblank->dev, vblank->pipe);
62 		kthread_queue_work(vblank->worker, &work->base);
63 		wake = true;
64 	}
65 	if (wake)
66 		wake_up_all(&vblank->work_wait_queue);
67 }
68 
69 /* Handle cancelling any pending vblank work items and drop respective vblank
70  * references in response to vblank interrupts being disabled.
71  */
drm_vblank_cancel_pending_works(struct drm_vblank_crtc * vblank)72 void drm_vblank_cancel_pending_works(struct drm_vblank_crtc *vblank)
73 {
74 	struct drm_vblank_work *work, *next;
75 
76 	assert_spin_locked(&vblank->dev->event_lock);
77 
78 	list_for_each_entry_safe(work, next, &vblank->pending_work, node) {
79 		list_del_init(&work->node);
80 		drm_vblank_put(vblank->dev, vblank->pipe);
81 	}
82 
83 	wake_up_all(&vblank->work_wait_queue);
84 }
85 
86 /**
87  * drm_vblank_work_schedule - schedule a vblank work
88  * @work: vblank work to schedule
89  * @count: target vblank count
90  * @nextonmiss: defer until the next vblank if target vblank was missed
91  *
92  * Schedule @work for execution once the crtc vblank count reaches @count.
93  *
94  * If the crtc vblank count has already reached @count and @nextonmiss is
95  * %false the work starts to execute immediately.
96  *
97  * If the crtc vblank count has already reached @count and @nextonmiss is
98  * %true the work is deferred until the next vblank (as if @count has been
99  * specified as crtc vblank count + 1).
100  *
101  * If @work is already scheduled, this function will reschedule said work
102  * using the new @count. This can be used for self-rearming work items.
103  *
104  * Returns:
105  * %1 if @work was successfully (re)scheduled, %0 if it was either already
106  * scheduled or cancelled, or a negative error code on failure.
107  */
drm_vblank_work_schedule(struct drm_vblank_work * work,u64 count,bool nextonmiss)108 int drm_vblank_work_schedule(struct drm_vblank_work *work,
109 			     u64 count, bool nextonmiss)
110 {
111 	struct drm_vblank_crtc *vblank = work->vblank;
112 	struct drm_device *dev = vblank->dev;
113 	u64 cur_vbl;
114 	unsigned long irqflags;
115 	bool passed, inmodeset, rescheduling = false, wake = false;
116 	int ret = 0;
117 
118 	spin_lock_irqsave(&dev->event_lock, irqflags);
119 	if (work->cancelling)
120 		goto out;
121 
122 	spin_lock(&dev->vbl_lock);
123 	inmodeset = vblank->inmodeset;
124 	spin_unlock(&dev->vbl_lock);
125 	if (inmodeset)
126 		goto out;
127 
128 	if (list_empty(&work->node)) {
129 		ret = drm_vblank_get(dev, vblank->pipe);
130 		if (ret < 0)
131 			goto out;
132 	} else if (work->count == count) {
133 		/* Already scheduled w/ same vbl count */
134 		goto out;
135 	} else {
136 		rescheduling = true;
137 	}
138 
139 	work->count = count;
140 	cur_vbl = drm_vblank_count(dev, vblank->pipe);
141 	passed = drm_vblank_passed(cur_vbl, count);
142 	if (passed)
143 		drm_dbg_core(dev,
144 			     "crtc %d vblank %llu already passed (current %llu)\n",
145 			     vblank->pipe, count, cur_vbl);
146 
147 	if (!nextonmiss && passed) {
148 		drm_vblank_put(dev, vblank->pipe);
149 		ret = kthread_queue_work(vblank->worker, &work->base);
150 
151 		if (rescheduling) {
152 			list_del_init(&work->node);
153 			wake = true;
154 		}
155 	} else {
156 		if (!rescheduling)
157 			list_add_tail(&work->node, &vblank->pending_work);
158 		ret = true;
159 	}
160 
161 out:
162 	spin_unlock_irqrestore(&dev->event_lock, irqflags);
163 	if (wake)
164 		wake_up_all(&vblank->work_wait_queue);
165 	return ret;
166 }
167 EXPORT_SYMBOL(drm_vblank_work_schedule);
168 
169 /**
170  * drm_vblank_work_cancel_sync - cancel a vblank work and wait for it to
171  * finish executing
172  * @work: vblank work to cancel
173  *
174  * Cancel an already scheduled vblank work and wait for its
175  * execution to finish.
176  *
177  * On return, @work is guaranteed to no longer be scheduled or running, even
178  * if it's self-arming.
179  *
180  * Returns:
181  * %True if the work was cancelled before it started to execute, %false
182  * otherwise.
183  */
drm_vblank_work_cancel_sync(struct drm_vblank_work * work)184 bool drm_vblank_work_cancel_sync(struct drm_vblank_work *work)
185 {
186 	struct drm_vblank_crtc *vblank = work->vblank;
187 	struct drm_device *dev = vblank->dev;
188 	bool ret = false;
189 
190 	spin_lock_irq(&dev->event_lock);
191 	if (!list_empty(&work->node)) {
192 		list_del_init(&work->node);
193 		drm_vblank_put(vblank->dev, vblank->pipe);
194 		ret = true;
195 	}
196 
197 	work->cancelling++;
198 	spin_unlock_irq(&dev->event_lock);
199 
200 	wake_up_all(&vblank->work_wait_queue);
201 
202 	if (kthread_cancel_work_sync(&work->base))
203 		ret = true;
204 
205 	spin_lock_irq(&dev->event_lock);
206 	work->cancelling--;
207 	spin_unlock_irq(&dev->event_lock);
208 
209 	return ret;
210 }
211 EXPORT_SYMBOL(drm_vblank_work_cancel_sync);
212 
213 /**
214  * drm_vblank_work_flush - wait for a scheduled vblank work to finish
215  * executing
216  * @work: vblank work to flush
217  *
218  * Wait until @work has finished executing once.
219  */
drm_vblank_work_flush(struct drm_vblank_work * work)220 void drm_vblank_work_flush(struct drm_vblank_work *work)
221 {
222 	struct drm_vblank_crtc *vblank = work->vblank;
223 	struct drm_device *dev = vblank->dev;
224 
225 	spin_lock_irq(&dev->event_lock);
226 	wait_event_lock_irq(vblank->work_wait_queue, list_empty(&work->node),
227 			    dev->event_lock);
228 	spin_unlock_irq(&dev->event_lock);
229 
230 	kthread_flush_work(&work->base);
231 }
232 EXPORT_SYMBOL(drm_vblank_work_flush);
233 
234 /**
235  * drm_vblank_work_init - initialize a vblank work item
236  * @work: vblank work item
237  * @crtc: CRTC whose vblank will trigger the work execution
238  * @func: work function to be executed
239  *
240  * Initialize a vblank work item for a specific crtc.
241  */
drm_vblank_work_init(struct drm_vblank_work * work,struct drm_crtc * crtc,void (* func)(struct kthread_work * work))242 void drm_vblank_work_init(struct drm_vblank_work *work, struct drm_crtc *crtc,
243 			  void (*func)(struct kthread_work *work))
244 {
245 	kthread_init_work(&work->base, func);
246 	INIT_LIST_HEAD(&work->node);
247 	work->vblank = &crtc->dev->vblank[drm_crtc_index(crtc)];
248 }
249 EXPORT_SYMBOL(drm_vblank_work_init);
250 
drm_vblank_worker_init(struct drm_vblank_crtc * vblank)251 int drm_vblank_worker_init(struct drm_vblank_crtc *vblank)
252 {
253 	struct kthread_worker *worker;
254 
255 	INIT_LIST_HEAD(&vblank->pending_work);
256 	init_waitqueue_head(&vblank->work_wait_queue);
257 	worker = kthread_create_worker(0, "card%d-crtc%d",
258 				       vblank->dev->primary->index,
259 				       vblank->pipe);
260 	if (IS_ERR(worker))
261 		return PTR_ERR(worker);
262 
263 	vblank->worker = worker;
264 
265 #ifdef notyet
266 	sched_set_fifo(worker->task);
267 #endif
268 	return 0;
269 }
270