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 * $FreeBSD: src/sys/dev/drm2/drm_lock.c,v 1.1 2012/05/22 11:07:44 kib 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 <drm/drmP.h> 52 53 int drm_lock(struct drm_device *dev, void *data, struct drm_file *file_priv) 54 { 55 struct drm_lock *lock = data; 56 int ret = 0; 57 58 if (lock->context == DRM_KERNEL_CONTEXT) { 59 DRM_ERROR("Process %d using kernel context %d\n", 60 DRM_CURRENTPID, lock->context); 61 return EINVAL; 62 } 63 64 DRM_DEBUG("%d (pid %d) requests lock (0x%08x), flags = 0x%08x\n", 65 lock->context, DRM_CURRENTPID, dev->lock.hw_lock->lock, 66 lock->flags); 67 68 if (drm_core_check_feature(dev, DRIVER_DMA_QUEUE) && 69 lock->context < 0) 70 return EINVAL; 71 72 DRM_LOCK(dev); 73 for (;;) { 74 if (drm_lock_take(&dev->lock, lock->context)) { 75 dev->lock.file_priv = file_priv; 76 dev->lock.lock_time = jiffies; 77 atomic_inc(&dev->counts[_DRM_STAT_LOCKS]); 78 break; /* Got lock */ 79 } 80 81 /* Contention */ 82 ret = DRM_LOCK_SLEEP(dev, &dev->lock.lock_queue, 83 PCATCH, "drmlk2", 0); 84 if (ret != 0) 85 break; 86 } 87 DRM_UNLOCK(dev); 88 89 if (ret == ERESTART) 90 DRM_DEBUG("restarting syscall\n"); 91 else 92 DRM_DEBUG("%d %s\n", lock->context, 93 ret ? "interrupted" : "has lock"); 94 95 if (ret != 0) 96 return ret; 97 98 /* XXX: Add signal blocking here */ 99 100 if (dev->driver->dma_quiescent != NULL && 101 (lock->flags & _DRM_LOCK_QUIESCENT)) 102 dev->driver->dma_quiescent(dev); 103 104 return 0; 105 } 106 107 int drm_unlock(struct drm_device *dev, void *data, struct drm_file *file_priv) 108 { 109 struct drm_lock *lock = data; 110 111 DRM_DEBUG("%d (pid %d) requests unlock (0x%08x), flags = 0x%08x\n", 112 lock->context, DRM_CURRENTPID, dev->lock.hw_lock->lock, 113 lock->flags); 114 115 if (lock->context == DRM_KERNEL_CONTEXT) { 116 DRM_ERROR("Process %d using kernel context %d\n", 117 DRM_CURRENTPID, lock->context); 118 return EINVAL; 119 } 120 121 atomic_inc(&dev->counts[_DRM_STAT_UNLOCKS]); 122 123 DRM_LOCK(dev); 124 drm_lock_transfer(&dev->lock, DRM_KERNEL_CONTEXT); 125 126 if (drm_lock_free(&dev->lock, DRM_KERNEL_CONTEXT)) { 127 DRM_ERROR("\n"); 128 } 129 DRM_UNLOCK(dev); 130 131 return 0; 132 } 133 134 int drm_lock_take(struct drm_lock_data *lock_data, unsigned int context) 135 { 136 volatile unsigned int *lock = &lock_data->hw_lock->lock; 137 unsigned int old, new; 138 139 do { 140 old = *lock; 141 if (old & _DRM_LOCK_HELD) 142 new = old | _DRM_LOCK_CONT; 143 else 144 new = context | _DRM_LOCK_HELD; 145 } while (!atomic_cmpset_int(lock, old, new)); 146 147 if (_DRM_LOCKING_CONTEXT(old) == context) { 148 if (old & _DRM_LOCK_HELD) { 149 if (context != DRM_KERNEL_CONTEXT) { 150 DRM_ERROR("%d holds heavyweight lock\n", 151 context); 152 } 153 return 0; 154 } 155 } 156 if (new == (context | _DRM_LOCK_HELD)) { 157 /* Have lock */ 158 return 1; 159 } 160 return 0; 161 } 162 163 /* This takes a lock forcibly and hands it to context. Should ONLY be used 164 inside *_unlock to give lock to kernel before calling *_dma_schedule. */ 165 int drm_lock_transfer(struct drm_lock_data *lock_data, unsigned int context) 166 { 167 volatile unsigned int *lock = &lock_data->hw_lock->lock; 168 unsigned int old, new; 169 170 lock_data->file_priv = NULL; 171 do { 172 old = *lock; 173 new = context | _DRM_LOCK_HELD; 174 } while (!atomic_cmpset_int(lock, old, new)); 175 176 return 1; 177 } 178 179 int drm_lock_free(struct drm_lock_data *lock_data, unsigned int context) 180 { 181 volatile unsigned int *lock = &lock_data->hw_lock->lock; 182 unsigned int old, new; 183 184 lock_data->file_priv = NULL; 185 do { 186 old = *lock; 187 new = 0; 188 } while (!atomic_cmpset_int(lock, old, new)); 189 190 if (_DRM_LOCK_IS_HELD(old) && _DRM_LOCKING_CONTEXT(old) != context) { 191 DRM_ERROR("%d freed heavyweight lock held by %d\n", 192 context, _DRM_LOCKING_CONTEXT(old)); 193 return 1; 194 } 195 DRM_WAKEUP_INT((void *)&lock_data->lock_queue); 196 return 0; 197 } 198