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