1 1.3 riastrad /* $NetBSD: ttm_lock.h,v 1.3 2022/02/17 01:21:02 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /************************************************************************** 4 1.1 riastrad * 5 1.1 riastrad * Copyright (c) 2007-2009 VMware, Inc., Palo Alto, CA., USA 6 1.1 riastrad * All Rights Reserved. 7 1.1 riastrad * 8 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a 9 1.1 riastrad * copy of this software and associated documentation files (the 10 1.1 riastrad * "Software"), to deal in the Software without restriction, including 11 1.1 riastrad * without limitation the rights to use, copy, modify, merge, publish, 12 1.1 riastrad * distribute, sub license, and/or sell copies of the Software, and to 13 1.1 riastrad * permit persons to whom the Software is furnished to do so, subject to 14 1.1 riastrad * the following conditions: 15 1.1 riastrad * 16 1.1 riastrad * The above copyright notice and this permission notice (including the 17 1.1 riastrad * next paragraph) shall be included in all copies or substantial portions 18 1.1 riastrad * of the Software. 19 1.1 riastrad * 20 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 23 1.1 riastrad * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 24 1.1 riastrad * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 25 1.1 riastrad * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 26 1.1 riastrad * USE OR OTHER DEALINGS IN THE SOFTWARE. 27 1.1 riastrad * 28 1.1 riastrad **************************************************************************/ 29 1.1 riastrad /* 30 1.1 riastrad * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com> 31 1.1 riastrad */ 32 1.1 riastrad 33 1.1 riastrad /** @file ttm_lock.h 34 1.1 riastrad * This file implements a simple replacement for the buffer manager use 35 1.1 riastrad * of the DRM heavyweight hardware lock. 36 1.1 riastrad * The lock is a read-write lock. Taking it in read mode and write mode 37 1.1 riastrad * is relatively fast, and intended for in-kernel use only. 38 1.1 riastrad * 39 1.1 riastrad * The vt mode is used only when there is a need to block all 40 1.1 riastrad * user-space processes from validating buffers. 41 1.1 riastrad * It's allowed to leave kernel space with the vt lock held. 42 1.1 riastrad * If a user-space process dies while having the vt-lock, 43 1.1 riastrad * it will be released during the file descriptor release. The vt lock 44 1.1 riastrad * excludes write lock and read lock. 45 1.1 riastrad * 46 1.1 riastrad * The suspend mode is used to lock out all TTM users when preparing for 47 1.1 riastrad * and executing suspend operations. 48 1.1 riastrad * 49 1.1 riastrad */ 50 1.1 riastrad 51 1.1 riastrad #ifndef _TTM_LOCK_H_ 52 1.1 riastrad #define _TTM_LOCK_H_ 53 1.1 riastrad 54 1.1 riastrad #include <linux/atomic.h> 55 1.1 riastrad #include <linux/wait.h> 56 1.1 riastrad 57 1.1 riastrad #include "ttm_object.h" 58 1.1 riastrad 59 1.3 riastrad #ifdef __NetBSD__ 60 1.3 riastrad #include "drm_wait_netbsd.h" /* XXX */ 61 1.3 riastrad #endif 62 1.3 riastrad 63 1.1 riastrad /** 64 1.1 riastrad * struct ttm_lock 65 1.1 riastrad * 66 1.1 riastrad * @base: ttm base object used solely to release the lock if the client 67 1.1 riastrad * holding the lock dies. 68 1.1 riastrad * @queue: Queue for processes waiting for lock change-of-status. 69 1.1 riastrad * @lock: Spinlock protecting some lock members. 70 1.1 riastrad * @rw: Read-write lock counter. Protected by @lock. 71 1.1 riastrad * @flags: Lock state. Protected by @lock. 72 1.1 riastrad */ 73 1.1 riastrad 74 1.1 riastrad struct ttm_lock { 75 1.1 riastrad struct ttm_base_object base; 76 1.3 riastrad drm_waitqueue_t queue; 77 1.1 riastrad spinlock_t lock; 78 1.1 riastrad int32_t rw; 79 1.1 riastrad uint32_t flags; 80 1.1 riastrad }; 81 1.1 riastrad 82 1.1 riastrad 83 1.1 riastrad /** 84 1.1 riastrad * ttm_lock_init 85 1.1 riastrad * 86 1.1 riastrad * @lock: Pointer to a struct ttm_lock 87 1.1 riastrad * Initializes the lock. 88 1.1 riastrad */ 89 1.1 riastrad extern void ttm_lock_init(struct ttm_lock *lock); 90 1.1 riastrad 91 1.1 riastrad /** 92 1.1 riastrad * ttm_read_unlock 93 1.1 riastrad * 94 1.1 riastrad * @lock: Pointer to a struct ttm_lock 95 1.1 riastrad * 96 1.1 riastrad * Releases a read lock. 97 1.1 riastrad */ 98 1.1 riastrad extern void ttm_read_unlock(struct ttm_lock *lock); 99 1.1 riastrad 100 1.1 riastrad /** 101 1.1 riastrad * ttm_read_lock 102 1.1 riastrad * 103 1.1 riastrad * @lock: Pointer to a struct ttm_lock 104 1.1 riastrad * @interruptible: Interruptible sleeping while waiting for a lock. 105 1.1 riastrad * 106 1.1 riastrad * Takes the lock in read mode. 107 1.1 riastrad * Returns: 108 1.1 riastrad * -ERESTARTSYS If interrupted by a signal and interruptible is true. 109 1.1 riastrad */ 110 1.1 riastrad extern int ttm_read_lock(struct ttm_lock *lock, bool interruptible); 111 1.1 riastrad 112 1.1 riastrad /** 113 1.1 riastrad * ttm_read_trylock 114 1.1 riastrad * 115 1.1 riastrad * @lock: Pointer to a struct ttm_lock 116 1.1 riastrad * @interruptible: Interruptible sleeping while waiting for a lock. 117 1.1 riastrad * 118 1.1 riastrad * Tries to take the lock in read mode. If the lock is already held 119 1.1 riastrad * in write mode, the function will return -EBUSY. If the lock is held 120 1.1 riastrad * in vt or suspend mode, the function will sleep until these modes 121 1.1 riastrad * are unlocked. 122 1.1 riastrad * 123 1.1 riastrad * Returns: 124 1.1 riastrad * -EBUSY The lock was already held in write mode. 125 1.1 riastrad * -ERESTARTSYS If interrupted by a signal and interruptible is true. 126 1.1 riastrad */ 127 1.1 riastrad extern int ttm_read_trylock(struct ttm_lock *lock, bool interruptible); 128 1.1 riastrad 129 1.1 riastrad /** 130 1.1 riastrad * ttm_write_unlock 131 1.1 riastrad * 132 1.1 riastrad * @lock: Pointer to a struct ttm_lock 133 1.1 riastrad * 134 1.1 riastrad * Releases a write lock. 135 1.1 riastrad */ 136 1.1 riastrad extern void ttm_write_unlock(struct ttm_lock *lock); 137 1.1 riastrad 138 1.1 riastrad /** 139 1.1 riastrad * ttm_write_lock 140 1.1 riastrad * 141 1.1 riastrad * @lock: Pointer to a struct ttm_lock 142 1.1 riastrad * @interruptible: Interruptible sleeping while waiting for a lock. 143 1.1 riastrad * 144 1.1 riastrad * Takes the lock in write mode. 145 1.1 riastrad * Returns: 146 1.1 riastrad * -ERESTARTSYS If interrupted by a signal and interruptible is true. 147 1.1 riastrad */ 148 1.1 riastrad extern int ttm_write_lock(struct ttm_lock *lock, bool interruptible); 149 1.1 riastrad 150 1.1 riastrad /** 151 1.1 riastrad * ttm_lock_downgrade 152 1.1 riastrad * 153 1.1 riastrad * @lock: Pointer to a struct ttm_lock 154 1.1 riastrad * 155 1.1 riastrad * Downgrades a write lock to a read lock. 156 1.1 riastrad */ 157 1.1 riastrad extern void ttm_lock_downgrade(struct ttm_lock *lock); 158 1.1 riastrad 159 1.1 riastrad /** 160 1.1 riastrad * ttm_suspend_lock 161 1.1 riastrad * 162 1.1 riastrad * @lock: Pointer to a struct ttm_lock 163 1.1 riastrad * 164 1.1 riastrad * Takes the lock in suspend mode. Excludes read and write mode. 165 1.1 riastrad */ 166 1.1 riastrad extern void ttm_suspend_lock(struct ttm_lock *lock); 167 1.1 riastrad 168 1.1 riastrad /** 169 1.1 riastrad * ttm_suspend_unlock 170 1.1 riastrad * 171 1.1 riastrad * @lock: Pointer to a struct ttm_lock 172 1.1 riastrad * 173 1.1 riastrad * Releases a suspend lock 174 1.1 riastrad */ 175 1.1 riastrad extern void ttm_suspend_unlock(struct ttm_lock *lock); 176 1.1 riastrad 177 1.1 riastrad /** 178 1.1 riastrad * ttm_vt_lock 179 1.1 riastrad * 180 1.1 riastrad * @lock: Pointer to a struct ttm_lock 181 1.1 riastrad * @interruptible: Interruptible sleeping while waiting for a lock. 182 1.1 riastrad * @tfile: Pointer to a struct ttm_object_file to register the lock with. 183 1.1 riastrad * 184 1.1 riastrad * Takes the lock in vt mode. 185 1.1 riastrad * Returns: 186 1.1 riastrad * -ERESTARTSYS If interrupted by a signal and interruptible is true. 187 1.1 riastrad * -ENOMEM: Out of memory when locking. 188 1.1 riastrad */ 189 1.1 riastrad extern int ttm_vt_lock(struct ttm_lock *lock, bool interruptible, 190 1.1 riastrad struct ttm_object_file *tfile); 191 1.1 riastrad 192 1.1 riastrad /** 193 1.1 riastrad * ttm_vt_unlock 194 1.1 riastrad * 195 1.1 riastrad * @lock: Pointer to a struct ttm_lock 196 1.1 riastrad * 197 1.1 riastrad * Releases a vt lock. 198 1.1 riastrad * Returns: 199 1.1 riastrad * -EINVAL If the lock was not held. 200 1.1 riastrad */ 201 1.1 riastrad extern int ttm_vt_unlock(struct ttm_lock *lock); 202 1.1 riastrad 203 1.1 riastrad /** 204 1.1 riastrad * ttm_write_unlock 205 1.1 riastrad * 206 1.1 riastrad * @lock: Pointer to a struct ttm_lock 207 1.1 riastrad * 208 1.1 riastrad * Releases a write lock. 209 1.1 riastrad */ 210 1.1 riastrad extern void ttm_write_unlock(struct ttm_lock *lock); 211 1.1 riastrad 212 1.1 riastrad /** 213 1.1 riastrad * ttm_write_lock 214 1.1 riastrad * 215 1.1 riastrad * @lock: Pointer to a struct ttm_lock 216 1.1 riastrad * @interruptible: Interruptible sleeping while waiting for a lock. 217 1.1 riastrad * 218 1.1 riastrad * Takes the lock in write mode. 219 1.1 riastrad * Returns: 220 1.1 riastrad * -ERESTARTSYS If interrupted by a signal and interruptible is true. 221 1.1 riastrad */ 222 1.1 riastrad extern int ttm_write_lock(struct ttm_lock *lock, bool interruptible); 223 1.1 riastrad 224 1.1 riastrad #endif 225