1 /*- 2 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas. 3 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the next 14 * paragraph) shall be included in all copies or substantial portions of the 15 * Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 23 * OTHER DEALINGS IN THE SOFTWARE. 24 * 25 * Authors: 26 * Rickard E. (Rik) Faith <faith@valinux.com> 27 * Gareth Hughes <gareth@valinux.com> 28 * 29 * $DragonFly: src/sys/dev/drm/drm_lock.c,v 1.1 2008/04/05 18:12:29 hasso Exp $ 30 */ 31 32 /** @file drm_lock.c 33 * Implementation of the ioctls and other support code for dealing with the 34 * hardware lock. 35 * 36 * The DRM hardware lock is a shared structure between the kernel and userland. 37 * 38 * On uncontended access where the new context was the last context, the 39 * client may take the lock without dropping down into the kernel, using atomic 40 * compare-and-set. 41 * 42 * If the client finds during compare-and-set that it was not the last owner 43 * of the lock, it calls the DRM lock ioctl, which may sleep waiting for the 44 * lock, and may have side-effects of kernel-managed context switching. 45 * 46 * When the client releases the lock, if the lock is marked as being contended 47 * by another client, then the DRM unlock ioctl is called so that the 48 * contending client may be woken up. 49 */ 50 51 #include "drmP.h" 52 53 int drm_lock_take(__volatile__ unsigned int *lock, unsigned int context) 54 { 55 unsigned int old, new; 56 57 do { 58 old = *lock; 59 if (old & _DRM_LOCK_HELD) new = old | _DRM_LOCK_CONT; 60 else new = context | _DRM_LOCK_HELD; 61 } while (!atomic_cmpset_int(lock, old, new)); 62 63 if (_DRM_LOCKING_CONTEXT(old) == context) { 64 if (old & _DRM_LOCK_HELD) { 65 if (context != DRM_KERNEL_CONTEXT) { 66 DRM_ERROR("%d holds heavyweight lock\n", 67 context); 68 } 69 return 0; 70 } 71 } 72 if (new == (context | _DRM_LOCK_HELD)) { 73 /* Have lock */ 74 return 1; 75 } 76 return 0; 77 } 78 79 /* This takes a lock forcibly and hands it to context. Should ONLY be used 80 inside *_unlock to give lock to kernel before calling *_dma_schedule. */ 81 int drm_lock_transfer(drm_device_t *dev, 82 __volatile__ unsigned int *lock, unsigned int context) 83 { 84 unsigned int old, new; 85 86 dev->lock.file_priv = NULL; 87 do { 88 old = *lock; 89 new = context | _DRM_LOCK_HELD; 90 } while (!atomic_cmpset_int(lock, old, new)); 91 92 return 1; 93 } 94 95 int drm_lock_free(drm_device_t *dev, 96 __volatile__ unsigned int *lock, unsigned int context) 97 { 98 unsigned int old, new; 99 100 dev->lock.file_priv = NULL; 101 do { 102 old = *lock; 103 new = 0; 104 } while (!atomic_cmpset_int(lock, old, new)); 105 106 if (_DRM_LOCK_IS_HELD(old) && _DRM_LOCKING_CONTEXT(old) != context) { 107 DRM_ERROR("%d freed heavyweight lock held by %d\n", 108 context, _DRM_LOCKING_CONTEXT(old)); 109 return 1; 110 } 111 DRM_WAKEUP_INT((void *)&dev->lock.lock_queue); 112 return 0; 113 } 114 115 int drm_lock(drm_device_t *dev, void *data, struct drm_file *file_priv) 116 { 117 drm_lock_t *lock = data; 118 int ret = 0; 119 120 if (lock->context == DRM_KERNEL_CONTEXT) { 121 DRM_ERROR("Process %d using kernel context %d\n", 122 DRM_CURRENTPID, lock->context); 123 return EINVAL; 124 } 125 126 DRM_DEBUG("%d (pid %d) requests lock (0x%08x), flags = 0x%08x\n", 127 lock->context, DRM_CURRENTPID, dev->lock.hw_lock->lock, 128 lock->flags); 129 130 if (dev->driver.use_dma_queue && lock->context < 0) 131 return EINVAL; 132 133 DRM_LOCK(); 134 for (;;) { 135 if (drm_lock_take(&dev->lock.hw_lock->lock, lock->context)) { 136 dev->lock.file_priv = file_priv; 137 dev->lock.lock_time = jiffies; 138 atomic_inc(&dev->counts[_DRM_STAT_LOCKS]); 139 break; /* Got lock */ 140 } 141 142 /* Contention */ 143 #if defined(__FreeBSD__) && __FreeBSD_version > 500000 144 ret = mtx_sleep((void *)&dev->lock.lock_queue, &dev->dev_lock, 145 PZERO | PCATCH, "drmlk2", 0); 146 #elif defined(__DragonFly__) 147 crit_enter(); 148 tsleep_interlock((void *)&dev->lock.lock_queue); 149 DRM_UNLOCK(); 150 ret = tsleep((void *)&dev->lock.lock_queue, PCATCH, 151 "drmlk2", 0); 152 crit_exit(); 153 DRM_LOCK(); 154 #else 155 ret = tsleep((void *)&dev->lock.lock_queue, PZERO | PCATCH, 156 "drmlk2", 0); 157 #endif 158 if (ret != 0) 159 break; 160 } 161 DRM_UNLOCK(); 162 DRM_DEBUG("%d %s\n", lock->context, ret ? "interrupted" : "has lock"); 163 164 if (ret != 0) 165 return ret; 166 167 /* XXX: Add signal blocking here */ 168 169 if (dev->driver.dma_quiescent != NULL && 170 (lock->flags & _DRM_LOCK_QUIESCENT)) 171 dev->driver.dma_quiescent(dev); 172 173 return 0; 174 } 175 176 int drm_unlock(drm_device_t *dev, void *data, struct drm_file *file_priv) 177 { 178 drm_lock_t *lock = data; 179 180 if (lock->context == DRM_KERNEL_CONTEXT) { 181 DRM_ERROR("Process %d using kernel context %d\n", 182 DRM_CURRENTPID, lock->context); 183 return EINVAL; 184 } 185 /* Check that the context unlock being requested actually matches 186 * who currently holds the lock. 187 */ 188 if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) || 189 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock) != lock->context) 190 return EINVAL; 191 192 atomic_inc(&dev->counts[_DRM_STAT_UNLOCKS]); 193 194 DRM_LOCK(); 195 drm_lock_transfer(dev, &dev->lock.hw_lock->lock, DRM_KERNEL_CONTEXT); 196 197 if (drm_lock_free(dev, &dev->lock.hw_lock->lock, DRM_KERNEL_CONTEXT)) { 198 DRM_ERROR("\n"); 199 } 200 DRM_UNLOCK(); 201 202 return 0; 203 } 204