Module Name:    src
Committed By:   riastradh
Date:           Sat Apr  2 22:40:43 UTC 2016

Modified Files:
        src/sys/external/bsd/drm2/drm: drm_lock.c

Log Message:
Take a stab at implementing drm_idlelock_take/release.

Evidently needed by VIA DRM/UMS.  Noted and tested by medfly/coypu.


To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 src/sys/external/bsd/drm2/drm/drm_lock.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/external/bsd/drm2/drm/drm_lock.c
diff -u src/sys/external/bsd/drm2/drm/drm_lock.c:1.3 src/sys/external/bsd/drm2/drm/drm_lock.c:1.4
--- src/sys/external/bsd/drm2/drm/drm_lock.c:1.3	Wed Jul 16 20:56:25 2014
+++ src/sys/external/bsd/drm2/drm/drm_lock.c	Sat Apr  2 22:40:43 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: drm_lock.c,v 1.3 2014/07/16 20:56:25 riastradh Exp $	*/
+/*	$NetBSD: drm_lock.c,v 1.4 2016/04/02 22:40:43 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2013 The NetBSD Foundation, Inc.
@@ -46,7 +46,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: drm_lock.c,v 1.3 2014/07/16 20:56:25 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: drm_lock.c,v 1.4 2016/04/02 22:40:43 riastradh Exp $");
 
 #include <sys/types.h>
 #include <sys/errno.h>
@@ -232,28 +232,47 @@ drm_lock_free(struct drm_lock_data *lock
 }
 
 /*
- * Take the lock for the kernel's use.
- *
- * XXX This is unimplemented because it's not clear that the Linux code
- * makes sense at all.  Linux's drm_idlelock_take never blocks, but it
- * doesn't guarantee that the kernel holds the lock on return!  For
- * now, I'll hope that the code paths relying on this don't matter yet.
+ * Try to acquire the lock.  Whether or not we acquire it, guarantee
+ * that whoever next releases it relinquishes it to the kernel, not to
+ * anyone else.
  */
 void
-drm_idlelock_take(struct drm_lock_data *lock_data __unused)
+drm_idlelock_take(struct drm_lock_data *lock_data)
 {
-	KASSERT(mutex_is_locked(&drm_global_mutex));
-	panic("drm_idlelock_take is not yet implemented"); /* XXX */
+
+	spin_lock(&lock_data->spinlock);
+	KASSERT(!lock_data->idle_has_lock);
+	KASSERT(lock_data->kernel_waiters < UINT32_MAX);
+	lock_data->kernel_waiters++;
+	/* Try to acquire the lock.  */
+	if (drm_lock_acquire(lock_data, DRM_KERNEL_CONTEXT)) {
+		lock_data->idle_has_lock = 1;
+	} else {
+		/*
+		 * Recording that there are kernel waiters will prevent
+		 * userland from acquiring the lock again when it is
+		 * next released.
+		 */
+	}
+	spin_unlock(&lock_data->spinlock);
 }
 
 /*
- * Release the lock from the kernel.
+ * Release whatever drm_idlelock_take managed to acquire.
  */
 void
-drm_idlelock_release(struct drm_lock_data *lock_data __unused)
+drm_idlelock_release(struct drm_lock_data *lock_data)
 {
-	KASSERT(mutex_is_locked(&drm_global_mutex));
-	panic("drm_idlelock_release is not yet implemented"); /* XXX */
+
+	spin_lock(&lock_data->spinlock);
+	KASSERT(0 < lock_data->kernel_waiters);
+	if (--lock_data->kernel_waiters == 0) {
+		if (lock_data->idle_has_lock) {
+			/* We did acquire it.  Release it.  */
+			drm_lock_release(lock_data, DRM_KERNEL_CONTEXT);
+		}
+	}
+	spin_unlock(&lock_data->spinlock);
 }
 
 /*

Reply via email to