15e6c2b4fSLyude Paul // SPDX-License-Identifier: MIT
25e6c2b4fSLyude Paul 
35e6c2b4fSLyude Paul #include <uapi/linux/sched/types.h>
45e6c2b4fSLyude Paul 
55e6c2b4fSLyude Paul #include <drm/drm_print.h>
65e6c2b4fSLyude Paul #include <drm/drm_vblank.h>
75e6c2b4fSLyude Paul #include <drm/drm_vblank_work.h>
85e6c2b4fSLyude Paul #include <drm/drm_crtc.h>
95e6c2b4fSLyude Paul 
105e6c2b4fSLyude Paul #include "drm_internal.h"
115e6c2b4fSLyude Paul 
125e6c2b4fSLyude Paul /**
135e6c2b4fSLyude Paul  * DOC: vblank works
145e6c2b4fSLyude Paul  *
155e6c2b4fSLyude Paul  * Many DRM drivers need to program hardware in a time-sensitive manner, many
165e6c2b4fSLyude Paul  * times with a deadline of starting and finishing within a certain region of
175e6c2b4fSLyude Paul  * the scanout. Most of the time the safest way to accomplish this is to
185e6c2b4fSLyude Paul  * simply do said time-sensitive programming in the driver's IRQ handler,
195e6c2b4fSLyude Paul  * which allows drivers to avoid being preempted during these critical
205e6c2b4fSLyude Paul  * regions. Or even better, the hardware may even handle applying such
215e6c2b4fSLyude Paul  * time-critical programming independently of the CPU.
225e6c2b4fSLyude Paul  *
235e6c2b4fSLyude Paul  * While there's a decent amount of hardware that's designed so that the CPU
245e6c2b4fSLyude Paul  * doesn't need to be concerned with extremely time-sensitive programming,
255e6c2b4fSLyude Paul  * there's a few situations where it can't be helped. Some unforgiving
265e6c2b4fSLyude Paul  * hardware may require that certain time-sensitive programming be handled
275e6c2b4fSLyude Paul  * completely by the CPU, and said programming may even take too long to
285e6c2b4fSLyude Paul  * handle in an IRQ handler. Another such situation would be where the driver
295e6c2b4fSLyude Paul  * needs to perform a task that needs to complete within a specific scanout
305e6c2b4fSLyude Paul  * period, but might possibly block and thus cannot be handled in an IRQ
315e6c2b4fSLyude Paul  * context. Both of these situations can't be solved perfectly in Linux since
325e6c2b4fSLyude Paul  * we're not a realtime kernel, and thus the scheduler may cause us to miss
335e6c2b4fSLyude Paul  * our deadline if it decides to preempt us. But for some drivers, it's good
345e6c2b4fSLyude Paul  * enough if we can lower our chance of being preempted to an absolute
355e6c2b4fSLyude Paul  * minimum.
365e6c2b4fSLyude Paul  *
375e6c2b4fSLyude Paul  * This is where &drm_vblank_work comes in. &drm_vblank_work provides a simple
385e6c2b4fSLyude Paul  * generic delayed work implementation which delays work execution until a
395e6c2b4fSLyude Paul  * particular vblank has passed, and then executes the work at realtime
405e6c2b4fSLyude Paul  * priority. This provides the best possible chance at performing
415e6c2b4fSLyude Paul  * time-sensitive hardware programming on time, even when the system is under
425e6c2b4fSLyude Paul  * heavy load. &drm_vblank_work also supports rescheduling, so that self
435e6c2b4fSLyude Paul  * re-arming work items can be easily implemented.
445e6c2b4fSLyude Paul  */
455e6c2b4fSLyude Paul 
drm_handle_vblank_works(struct drm_vblank_crtc * vblank)465e6c2b4fSLyude Paul void drm_handle_vblank_works(struct drm_vblank_crtc *vblank)
475e6c2b4fSLyude Paul {
485e6c2b4fSLyude Paul 	struct drm_vblank_work *work, *next;
495e6c2b4fSLyude Paul 	u64 count = atomic64_read(&vblank->count);
505e6c2b4fSLyude Paul 	bool wake = false;
515e6c2b4fSLyude Paul 
525e6c2b4fSLyude Paul 	assert_spin_locked(&vblank->dev->event_lock);
535e6c2b4fSLyude Paul 
545e6c2b4fSLyude Paul 	list_for_each_entry_safe(work, next, &vblank->pending_work, node) {
555e6c2b4fSLyude Paul 		if (!drm_vblank_passed(count, work->count))
565e6c2b4fSLyude Paul 			continue;
575e6c2b4fSLyude Paul 
585e6c2b4fSLyude Paul 		list_del_init(&work->node);
595e6c2b4fSLyude Paul 		drm_vblank_put(vblank->dev, vblank->pipe);
605e6c2b4fSLyude Paul 		kthread_queue_work(vblank->worker, &work->base);
615e6c2b4fSLyude Paul 		wake = true;
625e6c2b4fSLyude Paul 	}
635e6c2b4fSLyude Paul 	if (wake)
645e6c2b4fSLyude Paul 		wake_up_all(&vblank->work_wait_queue);
655e6c2b4fSLyude Paul }
665e6c2b4fSLyude Paul 
675e6c2b4fSLyude Paul /* Handle cancelling any pending vblank work items and drop respective vblank
685e6c2b4fSLyude Paul  * references in response to vblank interrupts being disabled.
695e6c2b4fSLyude Paul  */
drm_vblank_cancel_pending_works(struct drm_vblank_crtc * vblank)705e6c2b4fSLyude Paul void drm_vblank_cancel_pending_works(struct drm_vblank_crtc *vblank)
715e6c2b4fSLyude Paul {
725e6c2b4fSLyude Paul 	struct drm_vblank_work *work, *next;
735e6c2b4fSLyude Paul 
745e6c2b4fSLyude Paul 	assert_spin_locked(&vblank->dev->event_lock);
755e6c2b4fSLyude Paul 
765e6c2b4fSLyude Paul 	list_for_each_entry_safe(work, next, &vblank->pending_work, node) {
775e6c2b4fSLyude Paul 		list_del_init(&work->node);
785e6c2b4fSLyude Paul 		drm_vblank_put(vblank->dev, vblank->pipe);
795e6c2b4fSLyude Paul 	}
805e6c2b4fSLyude Paul 
815e6c2b4fSLyude Paul 	wake_up_all(&vblank->work_wait_queue);
825e6c2b4fSLyude Paul }
835e6c2b4fSLyude Paul 
845e6c2b4fSLyude Paul /**
855e6c2b4fSLyude Paul  * drm_vblank_work_schedule - schedule a vblank work
865e6c2b4fSLyude Paul  * @work: vblank work to schedule
875e6c2b4fSLyude Paul  * @count: target vblank count
885e6c2b4fSLyude Paul  * @nextonmiss: defer until the next vblank if target vblank was missed
895e6c2b4fSLyude Paul  *
905e6c2b4fSLyude Paul  * Schedule @work for execution once the crtc vblank count reaches @count.
915e6c2b4fSLyude Paul  *
925e6c2b4fSLyude Paul  * If the crtc vblank count has already reached @count and @nextonmiss is
935e6c2b4fSLyude Paul  * %false the work starts to execute immediately.
945e6c2b4fSLyude Paul  *
955e6c2b4fSLyude Paul  * If the crtc vblank count has already reached @count and @nextonmiss is
965e6c2b4fSLyude Paul  * %true the work is deferred until the next vblank (as if @count has been
975e6c2b4fSLyude Paul  * specified as crtc vblank count + 1).
985e6c2b4fSLyude Paul  *
995e6c2b4fSLyude Paul  * If @work is already scheduled, this function will reschedule said work
1005e6c2b4fSLyude Paul  * using the new @count. This can be used for self-rearming work items.
1015e6c2b4fSLyude Paul  *
1025e6c2b4fSLyude Paul  * Returns:
1035e6c2b4fSLyude Paul  * %1 if @work was successfully (re)scheduled, %0 if it was either already
1045e6c2b4fSLyude Paul  * scheduled or cancelled, or a negative error code on failure.
1055e6c2b4fSLyude Paul  */
drm_vblank_work_schedule(struct drm_vblank_work * work,u64 count,bool nextonmiss)1065e6c2b4fSLyude Paul int drm_vblank_work_schedule(struct drm_vblank_work *work,
1075e6c2b4fSLyude Paul 			     u64 count, bool nextonmiss)
1085e6c2b4fSLyude Paul {
1095e6c2b4fSLyude Paul 	struct drm_vblank_crtc *vblank = work->vblank;
1105e6c2b4fSLyude Paul 	struct drm_device *dev = vblank->dev;
1115e6c2b4fSLyude Paul 	u64 cur_vbl;
1125e6c2b4fSLyude Paul 	unsigned long irqflags;
1135e6c2b4fSLyude Paul 	bool passed, inmodeset, rescheduling = false, wake = false;
1145e6c2b4fSLyude Paul 	int ret = 0;
1155e6c2b4fSLyude Paul 
1165e6c2b4fSLyude Paul 	spin_lock_irqsave(&dev->event_lock, irqflags);
1175e6c2b4fSLyude Paul 	if (work->cancelling)
1185e6c2b4fSLyude Paul 		goto out;
1195e6c2b4fSLyude Paul 
1205e6c2b4fSLyude Paul 	spin_lock(&dev->vbl_lock);
1215e6c2b4fSLyude Paul 	inmodeset = vblank->inmodeset;
1225e6c2b4fSLyude Paul 	spin_unlock(&dev->vbl_lock);
1235e6c2b4fSLyude Paul 	if (inmodeset)
1245e6c2b4fSLyude Paul 		goto out;
1255e6c2b4fSLyude Paul 
1265e6c2b4fSLyude Paul 	if (list_empty(&work->node)) {
1275e6c2b4fSLyude Paul 		ret = drm_vblank_get(dev, vblank->pipe);
1285e6c2b4fSLyude Paul 		if (ret < 0)
1295e6c2b4fSLyude Paul 			goto out;
1305e6c2b4fSLyude Paul 	} else if (work->count == count) {
1315e6c2b4fSLyude Paul 		/* Already scheduled w/ same vbl count */
1325e6c2b4fSLyude Paul 		goto out;
1335e6c2b4fSLyude Paul 	} else {
1345e6c2b4fSLyude Paul 		rescheduling = true;
1355e6c2b4fSLyude Paul 	}
1365e6c2b4fSLyude Paul 
1375e6c2b4fSLyude Paul 	work->count = count;
1385e6c2b4fSLyude Paul 	cur_vbl = drm_vblank_count(dev, vblank->pipe);
1395e6c2b4fSLyude Paul 	passed = drm_vblank_passed(cur_vbl, count);
1405e6c2b4fSLyude Paul 	if (passed)
1415e6c2b4fSLyude Paul 		drm_dbg_core(dev,
1425e6c2b4fSLyude Paul 			     "crtc %d vblank %llu already passed (current %llu)\n",
1435e6c2b4fSLyude Paul 			     vblank->pipe, count, cur_vbl);
1445e6c2b4fSLyude Paul 
1455e6c2b4fSLyude Paul 	if (!nextonmiss && passed) {
1465e6c2b4fSLyude Paul 		drm_vblank_put(dev, vblank->pipe);
1475e6c2b4fSLyude Paul 		ret = kthread_queue_work(vblank->worker, &work->base);
1485e6c2b4fSLyude Paul 
1495e6c2b4fSLyude Paul 		if (rescheduling) {
1505e6c2b4fSLyude Paul 			list_del_init(&work->node);
1515e6c2b4fSLyude Paul 			wake = true;
1525e6c2b4fSLyude Paul 		}
1535e6c2b4fSLyude Paul 	} else {
1545e6c2b4fSLyude Paul 		if (!rescheduling)
1555e6c2b4fSLyude Paul 			list_add_tail(&work->node, &vblank->pending_work);
1565e6c2b4fSLyude Paul 		ret = true;
1575e6c2b4fSLyude Paul 	}
1585e6c2b4fSLyude Paul 
1595e6c2b4fSLyude Paul out:
1605e6c2b4fSLyude Paul 	spin_unlock_irqrestore(&dev->event_lock, irqflags);
1615e6c2b4fSLyude Paul 	if (wake)
1625e6c2b4fSLyude Paul 		wake_up_all(&vblank->work_wait_queue);
1635e6c2b4fSLyude Paul 	return ret;
1645e6c2b4fSLyude Paul }
1655e6c2b4fSLyude Paul EXPORT_SYMBOL(drm_vblank_work_schedule);
1665e6c2b4fSLyude Paul 
1675e6c2b4fSLyude Paul /**
1685e6c2b4fSLyude Paul  * drm_vblank_work_cancel_sync - cancel a vblank work and wait for it to
1695e6c2b4fSLyude Paul  * finish executing
1705e6c2b4fSLyude Paul  * @work: vblank work to cancel
1715e6c2b4fSLyude Paul  *
1725e6c2b4fSLyude Paul  * Cancel an already scheduled vblank work and wait for its
1735e6c2b4fSLyude Paul  * execution to finish.
1745e6c2b4fSLyude Paul  *
1755e6c2b4fSLyude Paul  * On return, @work is guaranteed to no longer be scheduled or running, even
1765e6c2b4fSLyude Paul  * if it's self-arming.
1775e6c2b4fSLyude Paul  *
1785e6c2b4fSLyude Paul  * Returns:
1795e6c2b4fSLyude Paul  * %True if the work was cancelled before it started to execute, %false
1805e6c2b4fSLyude Paul  * otherwise.
1815e6c2b4fSLyude Paul  */
drm_vblank_work_cancel_sync(struct drm_vblank_work * work)1825e6c2b4fSLyude Paul bool drm_vblank_work_cancel_sync(struct drm_vblank_work *work)
1835e6c2b4fSLyude Paul {
1845e6c2b4fSLyude Paul 	struct drm_vblank_crtc *vblank = work->vblank;
1855e6c2b4fSLyude Paul 	struct drm_device *dev = vblank->dev;
1865e6c2b4fSLyude Paul 	bool ret = false;
1875e6c2b4fSLyude Paul 
1885e6c2b4fSLyude Paul 	spin_lock_irq(&dev->event_lock);
1895e6c2b4fSLyude Paul 	if (!list_empty(&work->node)) {
1905e6c2b4fSLyude Paul 		list_del_init(&work->node);
1915e6c2b4fSLyude Paul 		drm_vblank_put(vblank->dev, vblank->pipe);
1925e6c2b4fSLyude Paul 		ret = true;
1935e6c2b4fSLyude Paul 	}
1945e6c2b4fSLyude Paul 
1955e6c2b4fSLyude Paul 	work->cancelling++;
1965e6c2b4fSLyude Paul 	spin_unlock_irq(&dev->event_lock);
1975e6c2b4fSLyude Paul 
1985e6c2b4fSLyude Paul 	wake_up_all(&vblank->work_wait_queue);
1995e6c2b4fSLyude Paul 
2005e6c2b4fSLyude Paul 	if (kthread_cancel_work_sync(&work->base))
2015e6c2b4fSLyude Paul 		ret = true;
2025e6c2b4fSLyude Paul 
2035e6c2b4fSLyude Paul 	spin_lock_irq(&dev->event_lock);
2045e6c2b4fSLyude Paul 	work->cancelling--;
2055e6c2b4fSLyude Paul 	spin_unlock_irq(&dev->event_lock);
2065e6c2b4fSLyude Paul 
2075e6c2b4fSLyude Paul 	return ret;
2085e6c2b4fSLyude Paul }
2095e6c2b4fSLyude Paul EXPORT_SYMBOL(drm_vblank_work_cancel_sync);
2105e6c2b4fSLyude Paul 
2115e6c2b4fSLyude Paul /**
2125e6c2b4fSLyude Paul  * drm_vblank_work_flush - wait for a scheduled vblank work to finish
2135e6c2b4fSLyude Paul  * executing
2145e6c2b4fSLyude Paul  * @work: vblank work to flush
2155e6c2b4fSLyude Paul  *
2165e6c2b4fSLyude Paul  * Wait until @work has finished executing once.
2175e6c2b4fSLyude Paul  */
drm_vblank_work_flush(struct drm_vblank_work * work)2185e6c2b4fSLyude Paul void drm_vblank_work_flush(struct drm_vblank_work *work)
2195e6c2b4fSLyude Paul {
2205e6c2b4fSLyude Paul 	struct drm_vblank_crtc *vblank = work->vblank;
2215e6c2b4fSLyude Paul 	struct drm_device *dev = vblank->dev;
2225e6c2b4fSLyude Paul 
2235e6c2b4fSLyude Paul 	spin_lock_irq(&dev->event_lock);
2245e6c2b4fSLyude Paul 	wait_event_lock_irq(vblank->work_wait_queue, list_empty(&work->node),
2255e6c2b4fSLyude Paul 			    dev->event_lock);
2265e6c2b4fSLyude Paul 	spin_unlock_irq(&dev->event_lock);
2275e6c2b4fSLyude Paul 
2285e6c2b4fSLyude Paul 	kthread_flush_work(&work->base);
2295e6c2b4fSLyude Paul }
2305e6c2b4fSLyude Paul EXPORT_SYMBOL(drm_vblank_work_flush);
2315e6c2b4fSLyude Paul 
2325e6c2b4fSLyude Paul /**
2335e6c2b4fSLyude Paul  * drm_vblank_work_init - initialize a vblank work item
2345e6c2b4fSLyude Paul  * @work: vblank work item
2355e6c2b4fSLyude Paul  * @crtc: CRTC whose vblank will trigger the work execution
2365e6c2b4fSLyude Paul  * @func: work function to be executed
2375e6c2b4fSLyude Paul  *
2385e6c2b4fSLyude Paul  * Initialize a vblank work item for a specific crtc.
2395e6c2b4fSLyude Paul  */
drm_vblank_work_init(struct drm_vblank_work * work,struct drm_crtc * crtc,void (* func)(struct kthread_work * work))2405e6c2b4fSLyude Paul void drm_vblank_work_init(struct drm_vblank_work *work, struct drm_crtc *crtc,
2415e6c2b4fSLyude Paul 			  void (*func)(struct kthread_work *work))
2425e6c2b4fSLyude Paul {
2435e6c2b4fSLyude Paul 	kthread_init_work(&work->base, func);
2445e6c2b4fSLyude Paul 	INIT_LIST_HEAD(&work->node);
2455e6c2b4fSLyude Paul 	work->vblank = &crtc->dev->vblank[drm_crtc_index(crtc)];
2465e6c2b4fSLyude Paul }
2475e6c2b4fSLyude Paul EXPORT_SYMBOL(drm_vblank_work_init);
2485e6c2b4fSLyude Paul 
drm_vblank_worker_init(struct drm_vblank_crtc * vblank)2495e6c2b4fSLyude Paul int drm_vblank_worker_init(struct drm_vblank_crtc *vblank)
2505e6c2b4fSLyude Paul {
2515e6c2b4fSLyude Paul 	struct kthread_worker *worker;
2525e6c2b4fSLyude Paul 
2535e6c2b4fSLyude Paul 	INIT_LIST_HEAD(&vblank->pending_work);
2545e6c2b4fSLyude Paul 	init_waitqueue_head(&vblank->work_wait_queue);
2555e6c2b4fSLyude Paul 	worker = kthread_create_worker(0, "card%d-crtc%d",
2565e6c2b4fSLyude Paul 				       vblank->dev->primary->index,
2575e6c2b4fSLyude Paul 				       vblank->pipe);
2585e6c2b4fSLyude Paul 	if (IS_ERR(worker))
2595e6c2b4fSLyude Paul 		return PTR_ERR(worker);
2605e6c2b4fSLyude Paul 
2615e6c2b4fSLyude Paul 	vblank->worker = worker;
2625e6c2b4fSLyude Paul 
2635e6c2b4fSLyude Paul 	sched_set_fifo(worker->task);
2645e6c2b4fSLyude Paul 	return 0;
2655e6c2b4fSLyude Paul }
2665e6c2b4fSLyude Paul