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