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