1 /** 2 * \file drm_lock.c 3 * IOCTLs for locking 4 * 5 * \author Rickard E. (Rik) Faith <faith@valinux.com> 6 * \author Gareth Hughes <gareth@valinux.com> 7 */ 8 9 /* 10 * Created: Tue Feb 2 08:37:54 1999 by faith@valinux.com 11 * 12 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas. 13 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. 14 * All Rights Reserved. 15 * 16 * Permission is hereby granted, free of charge, to any person obtaining a 17 * copy of this software and associated documentation files (the "Software"), 18 * to deal in the Software without restriction, including without limitation 19 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 20 * and/or sell copies of the Software, and to permit persons to whom the 21 * Software is furnished to do so, subject to the following conditions: 22 * 23 * The above copyright notice and this permission notice (including the next 24 * paragraph) shall be included in all copies or substantial portions of the 25 * Software. 26 * 27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 30 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 31 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 32 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 33 * OTHER DEALINGS IN THE SOFTWARE. 34 */ 35 36 #include <linux/export.h> 37 #include <drm/drmP.h> 38 #include "drm_legacy.h" 39 #include "drm_internal.h" 40 41 #if 0 42 static int drm_notifier(void *priv); 43 44 static int drm_lock_take(struct drm_lock_data *lock_data, unsigned int context); 45 #endif 46 47 /** 48 * Lock ioctl. 49 * 50 * \param inode device inode. 51 * \param file_priv DRM file private. 52 * \param cmd command. 53 * \param arg user argument, pointing to a drm_lock structure. 54 * \return zero on success or negative number on failure. 55 * 56 * Add the current task to the lock wait queue, and attempt to take to lock. 57 */ 58 int drm_legacy_lock(struct drm_device *dev, void *data, 59 struct drm_file *file_priv) 60 { 61 #if 0 62 DECLARE_WAITQUEUE(entry, current); 63 struct drm_lock *lock = data; 64 struct drm_master *master = file_priv->master; 65 int ret = 0; 66 67 if (drm_core_check_feature(dev, DRIVER_MODESET)) 68 return -EINVAL; 69 70 ++file_priv->lock_count; 71 72 if (lock->context == DRM_KERNEL_CONTEXT) { 73 DRM_ERROR("Process %d using kernel context %d\n", 74 task_pid_nr(current), lock->context); 75 return -EINVAL; 76 } 77 78 DRM_DEBUG("%d (pid %d) requests lock (0x%08x), flags = 0x%08x\n", 79 lock->context, task_pid_nr(current), 80 master->lock.hw_lock->lock, lock->flags); 81 82 add_wait_queue(&master->lock.lock_queue, &entry); 83 spin_lock_bh(&master->lock.spinlock); 84 master->lock.user_waiters++; 85 spin_unlock_bh(&master->lock.spinlock); 86 87 for (;;) { 88 __set_current_state(TASK_INTERRUPTIBLE); 89 if (!master->lock.hw_lock) { 90 /* Device has been unregistered */ 91 send_sig(SIGTERM, current, 0); 92 ret = -EINTR; 93 break; 94 } 95 if (drm_lock_take(&master->lock, lock->context)) { 96 master->lock.file_priv = file_priv; 97 master->lock.lock_time = jiffies; 98 break; /* Got lock */ 99 } 100 101 /* Contention */ 102 mutex_unlock(&drm_global_mutex); 103 schedule(); 104 mutex_lock(&drm_global_mutex); 105 if (signal_pending(current)) { 106 ret = -EINTR; 107 break; 108 } 109 } 110 spin_lock_bh(&master->lock.spinlock); 111 master->lock.user_waiters--; 112 spin_unlock_bh(&master->lock.spinlock); 113 __set_current_state(TASK_RUNNING); 114 remove_wait_queue(&master->lock.lock_queue, &entry); 115 116 DRM_DEBUG("%d %s\n", lock->context, 117 ret ? "interrupted" : "has lock"); 118 if (ret) return ret; 119 120 /* don't set the block all signals on the master process for now 121 * really probably not the correct answer but lets us debug xkb 122 * xserver for now */ 123 if (!file_priv->is_master) { 124 dev->sigdata.context = lock->context; 125 dev->sigdata.lock = master->lock.hw_lock; 126 } 127 128 if (dev->driver->dma_quiescent && (lock->flags & _DRM_LOCK_QUIESCENT)) 129 { 130 if (dev->driver->dma_quiescent(dev)) { 131 DRM_DEBUG("%d waiting for DMA quiescent\n", 132 lock->context); 133 return -EBUSY; 134 } 135 } 136 #endif 137 138 return 0; 139 } 140 141 /** 142 * Unlock ioctl. 143 * 144 * \param inode device inode. 145 * \param file_priv DRM file private. 146 * \param cmd command. 147 * \param arg user argument, pointing to a drm_lock structure. 148 * \return zero on success or negative number on failure. 149 * 150 * Transfer and free the lock. 151 */ 152 int drm_legacy_unlock(struct drm_device *dev, void *data, struct drm_file *file_priv) 153 { 154 #if 0 155 struct drm_lock *lock = data; 156 struct drm_master *master = file_priv->master; 157 158 if (drm_core_check_feature(dev, DRIVER_MODESET)) 159 return -EINVAL; 160 161 if (lock->context == DRM_KERNEL_CONTEXT) { 162 DRM_ERROR("Process %d using kernel context %d\n", 163 task_pid_nr(current), lock->context); 164 return -EINVAL; 165 } 166 167 if (drm_legacy_lock_free(&master->lock, lock->context)) { 168 /* FIXME: Should really bail out here. */ 169 } 170 171 unblock_all_signals(); 172 #endif 173 return 0; 174 } 175 176 #if 0 177 /** 178 * Take the heavyweight lock. 179 * 180 * \param lock lock pointer. 181 * \param context locking context. 182 * \return one if the lock is held, or zero otherwise. 183 * 184 * Attempt to mark the lock as held by the given context, via the \p cmpxchg instruction. 185 */ 186 static 187 int drm_lock_take(struct drm_lock_data *lock_data, 188 unsigned int context) 189 { 190 unsigned int old, new, prev; 191 volatile unsigned int *lock = &lock_data->hw_lock->lock; 192 193 spin_lock_bh(&lock_data->spinlock); 194 do { 195 old = *lock; 196 if (old & _DRM_LOCK_HELD) 197 new = old | _DRM_LOCK_CONT; 198 else { 199 new = context | _DRM_LOCK_HELD | 200 ((lock_data->user_waiters + lock_data->kernel_waiters > 1) ? 201 _DRM_LOCK_CONT : 0); 202 } 203 prev = cmpxchg(lock, old, new); 204 } while (prev != old); 205 spin_unlock_bh(&lock_data->spinlock); 206 207 if (_DRM_LOCKING_CONTEXT(old) == context) { 208 if (old & _DRM_LOCK_HELD) { 209 if (context != DRM_KERNEL_CONTEXT) { 210 DRM_ERROR("%d holds heavyweight lock\n", 211 context); 212 } 213 return 0; 214 } 215 } 216 217 if ((_DRM_LOCKING_CONTEXT(new)) == context && (new & _DRM_LOCK_HELD)) { 218 /* Have lock */ 219 return 1; 220 } 221 return 0; 222 } 223 224 /** 225 * This takes a lock forcibly and hands it to context. Should ONLY be used 226 * inside *_unlock to give lock to kernel before calling *_dma_schedule. 227 * 228 * \param dev DRM device. 229 * \param lock lock pointer. 230 * \param context locking context. 231 * \return always one. 232 * 233 * Resets the lock file pointer. 234 * Marks the lock as held by the given context, via the \p cmpxchg instruction. 235 */ 236 static int drm_lock_transfer(struct drm_lock_data *lock_data, 237 unsigned int context) 238 { 239 unsigned int old, new, prev; 240 volatile unsigned int *lock = &lock_data->hw_lock->lock; 241 242 lock_data->file_priv = NULL; 243 do { 244 old = *lock; 245 new = context | _DRM_LOCK_HELD; 246 prev = cmpxchg(lock, old, new); 247 } while (prev != old); 248 return 1; 249 } 250 #endif 251 252 /** 253 * Free lock. 254 * 255 * \param dev DRM device. 256 * \param lock lock. 257 * \param context context. 258 * 259 * Resets the lock file pointer. 260 * Marks the lock as not held, via the \p cmpxchg instruction. Wakes any task 261 * waiting on the lock queue. 262 */ 263 int drm_legacy_lock_free(struct drm_lock_data *lock_data, unsigned int context) 264 { 265 #if 0 266 unsigned int old, new, prev; 267 volatile unsigned int *lock = &lock_data->hw_lock->lock; 268 269 spin_lock_bh(&lock_data->spinlock); 270 if (lock_data->kernel_waiters != 0) { 271 drm_lock_transfer(lock_data, 0); 272 lock_data->idle_has_lock = 1; 273 spin_unlock_bh(&lock_data->spinlock); 274 return 1; 275 } 276 spin_unlock_bh(&lock_data->spinlock); 277 278 do { 279 old = *lock; 280 new = _DRM_LOCKING_CONTEXT(old); 281 prev = cmpxchg(lock, old, new); 282 } while (prev != old); 283 284 if (_DRM_LOCK_IS_HELD(old) && _DRM_LOCKING_CONTEXT(old) != context) { 285 DRM_ERROR("%d freed heavyweight lock held by %d\n", 286 context, _DRM_LOCKING_CONTEXT(old)); 287 return 1; 288 } 289 wake_up_interruptible(&lock_data->lock_queue); 290 #endif 291 return 0; 292 } 293 294 #if 0 295 /** 296 * If we get here, it means that the process has called DRM_IOCTL_LOCK 297 * without calling DRM_IOCTL_UNLOCK. 298 * 299 * If the lock is not held, then let the signal proceed as usual. If the lock 300 * is held, then set the contended flag and keep the signal blocked. 301 * 302 * \param priv pointer to a drm_device structure. 303 * \return one if the signal should be delivered normally, or zero if the 304 * signal should be blocked. 305 */ 306 static int drm_notifier(void *priv) 307 { 308 struct drm_device *dev = priv; 309 struct drm_hw_lock *lock = dev->sigdata.lock; 310 unsigned int old, new, prev; 311 312 /* Allow signal delivery if lock isn't held */ 313 if (!lock || !_DRM_LOCK_IS_HELD(lock->lock) 314 || _DRM_LOCKING_CONTEXT(lock->lock) != dev->sigdata.context) 315 return 1; 316 317 /* Otherwise, set flag to force call to 318 drmUnlock */ 319 do { 320 old = lock->lock; 321 new = old | _DRM_LOCK_CONT; 322 prev = cmpxchg(&lock->lock, old, new); 323 } while (prev != old); 324 return 0; 325 } 326 #endif 327 328 /** 329 * This function returns immediately and takes the hw lock 330 * with the kernel context if it is free, otherwise it gets the highest priority when and if 331 * it is eventually released. 332 * 333 * This guarantees that the kernel will _eventually_ have the lock _unless_ it is held 334 * by a blocked process. (In the latter case an explicit wait for the hardware lock would cause 335 * a deadlock, which is why the "idlelock" was invented). 336 * 337 * This should be sufficient to wait for GPU idle without 338 * having to worry about starvation. 339 */ 340 341 void drm_legacy_idlelock_take(struct drm_lock_data *lock_data) 342 { 343 #if 0 344 int ret; 345 346 spin_lock_bh(&lock_data->spinlock); 347 lock_data->kernel_waiters++; 348 if (!lock_data->idle_has_lock) { 349 350 spin_unlock_bh(&lock_data->spinlock); 351 ret = drm_lock_take(lock_data, DRM_KERNEL_CONTEXT); 352 spin_lock_bh(&lock_data->spinlock); 353 354 if (ret == 1) 355 lock_data->idle_has_lock = 1; 356 } 357 spin_unlock_bh(&lock_data->spinlock); 358 #endif 359 } 360 EXPORT_SYMBOL(drm_legacy_idlelock_take); 361 362 void drm_legacy_idlelock_release(struct drm_lock_data *lock_data) 363 { 364 #if 0 365 unsigned int old, prev; 366 volatile unsigned int *lock = &lock_data->hw_lock->lock; 367 368 spin_lock_bh(&lock_data->spinlock); 369 if (--lock_data->kernel_waiters == 0) { 370 if (lock_data->idle_has_lock) { 371 do { 372 old = *lock; 373 prev = cmpxchg(lock, old, DRM_KERNEL_CONTEXT); 374 } while (prev != old); 375 wake_up_interruptible(&lock_data->lock_queue); 376 lock_data->idle_has_lock = 0; 377 } 378 } 379 spin_unlock_bh(&lock_data->spinlock); 380 #endif 381 } 382 EXPORT_SYMBOL(drm_legacy_idlelock_release); 383 384 int drm_legacy_i_have_hw_lock(struct drm_device *dev, 385 struct drm_file *file_priv) 386 { 387 #if 0 388 struct drm_master *master = file_priv->master; 389 return (file_priv->lock_count && master->lock.hw_lock && 390 _DRM_LOCK_IS_HELD(master->lock.hw_lock->lock) && 391 master->lock.file_priv == file_priv); 392 #endif 393 return 0; 394 } 395