From e10502002f0ebb2b56b19384b2f2eae7a7a84512 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Tue, 27 May 2008 17:50:39 -0700 Subject: [intel-gem] Replace idlelock usage with real lock acquisition. --- linux-core/drm_lock.c | 74 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 61 insertions(+), 13 deletions(-) (limited to 'linux-core/drm_lock.c') diff --git a/linux-core/drm_lock.c b/linux-core/drm_lock.c index db1d646b..ad7c1679 100644 --- a/linux-core/drm_lock.c +++ b/linux-core/drm_lock.c @@ -215,22 +215,16 @@ int drm_lock_take(struct drm_lock_data *lock_data, } while (prev != old); spin_unlock_irqrestore(&lock_data->spinlock, irqflags); - if (_DRM_LOCKING_CONTEXT(old) == context) { - if (old & _DRM_LOCK_HELD) { - if (context != DRM_KERNEL_CONTEXT) { - DRM_ERROR("%d holds heavyweight lock\n", - context); - } - return 0; + /* Warn on recursive locking of user contexts. */ + if (_DRM_LOCKING_CONTEXT(old) == context && _DRM_LOCK_IS_HELD(old)) { + if (context != DRM_KERNEL_CONTEXT) { + DRM_ERROR("%d holds heavyweight lock\n", + context); } + return 0; } - if ((_DRM_LOCKING_CONTEXT(new)) == context && (new & _DRM_LOCK_HELD)) { - /* Have lock */ - - return 1; - } - return 0; + return !_DRM_LOCK_IS_HELD(old); } /** @@ -386,6 +380,60 @@ void drm_idlelock_release(struct drm_lock_data *lock_data) } EXPORT_SYMBOL(drm_idlelock_release); +/** + * Takes the lock on behalf of the client if needed, using the kernel context. + * + * This allows us to hide the hardware lock when it's required for protection + * of data structures (such as command ringbuffer) shared with the X Server, and + * a way for us to transition to lockless for those requests when the X Server + * stops accessing the ringbuffer directly, without having to update the + * other userland clients. + */ +int drm_client_lock_take(struct drm_device *dev, struct drm_file *file_priv) +{ + int ret; + unsigned long irqflags; + + /* If the client has the lock, we're already done. */ + if (drm_i_have_hw_lock(dev, file_priv)) + return 0; + + /* Client doesn't hold the lock. Block taking the lock with the kernel + * context on behalf of the client, and return whether we were + * successful. + */ + spin_lock_irqsave(&dev->lock.spinlock, irqflags); + dev->lock.user_waiters++; + spin_unlock_irqrestore(&dev->lock.spinlock, irqflags); + ret = wait_event_interruptible(dev->lock.lock_queue, + drm_lock_take(&dev->lock, + DRM_KERNEL_CONTEXT)); + spin_lock_irqsave(&dev->lock.spinlock, irqflags); + dev->lock.user_waiters--; + if (ret != 0) { + spin_unlock_irqrestore(&dev->lock.spinlock, irqflags); + return ret; + } else { + dev->lock.file_priv = file_priv; + dev->lock.lock_time = jiffies; + dev->lock.kernel_held = 1; + file_priv->lock_count++; + spin_unlock_irqrestore(&dev->lock.spinlock, irqflags); + return 0; + } +} +EXPORT_SYMBOL(drm_client_lock_take); + +void drm_client_lock_release(struct drm_device *dev) +{ + if (dev->lock.kernel_held) { + dev->lock.kernel_held = 0; + dev->lock.file_priv = NULL; + drm_lock_free(&dev->lock, DRM_KERNEL_CONTEXT); + } +} +EXPORT_SYMBOL(drm_client_lock_release); + int drm_i_have_hw_lock(struct drm_device *dev, struct drm_file *file_priv) { -- cgit v1.2.3 From 0903de0c8f7d2566c1bd65600142a71572eec07e Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Tue, 3 Jun 2008 21:49:51 -0700 Subject: Drop struct_mutex while waiting in drm_client_lock_take struct_mutex cannot be held while blocking on DRM lock. --- linux-core/drm_lock.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'linux-core/drm_lock.c') diff --git a/linux-core/drm_lock.c b/linux-core/drm_lock.c index 58c5f08d..e6eae42b 100644 --- a/linux-core/drm_lock.c +++ b/linux-core/drm_lock.c @@ -393,6 +393,7 @@ int drm_client_lock_take(struct drm_device *dev, struct drm_file *file_priv) if (drm_i_have_hw_lock(dev, file_priv)) return 0; + mutex_unlock (&dev->struct_mutex); /* Client doesn't hold the lock. Block taking the lock with the kernel * context on behalf of the client, and return whether we were * successful. @@ -407,15 +408,15 @@ int drm_client_lock_take(struct drm_device *dev, struct drm_file *file_priv) dev->lock.user_waiters--; if (ret != 0) { spin_unlock_irqrestore(&dev->lock.spinlock, irqflags); - return ret; } else { dev->lock.file_priv = file_priv; dev->lock.lock_time = jiffies; dev->lock.kernel_held = 1; file_priv->lock_count++; spin_unlock_irqrestore(&dev->lock.spinlock, irqflags); - return 0; } + mutex_lock (&dev->struct_mutex); + return ret; } EXPORT_SYMBOL(drm_client_lock_take); -- cgit v1.2.3