]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Created: Tue Feb 2 08:37:54 1999 by [email protected] | |
3 | * | |
4 | * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas. | |
5 | * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. | |
6 | * All Rights Reserved. | |
7 | * | |
32e7b94a DR |
8 | * Author Rickard E. (Rik) Faith <[email protected]> |
9 | * Author Gareth Hughes <[email protected]> | |
10 | * | |
1da177e4 LT |
11 | * Permission is hereby granted, free of charge, to any person obtaining a |
12 | * copy of this software and associated documentation files (the "Software"), | |
13 | * to deal in the Software without restriction, including without limitation | |
14 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
15 | * and/or sell copies of the Software, and to permit persons to whom the | |
16 | * Software is furnished to do so, subject to the following conditions: | |
17 | * | |
18 | * The above copyright notice and this permission notice (including the next | |
19 | * paragraph) shall be included in all copies or substantial portions of the | |
20 | * Software. | |
21 | * | |
22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
23 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
24 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
25 | * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
26 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
27 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
28 | * OTHER DEALINGS IN THE SOFTWARE. | |
29 | */ | |
30 | ||
760285e7 | 31 | #include <drm/drmP.h> |
67d0ec4e | 32 | #include "drm_internal.h" |
6548f4e7 | 33 | #include "drm_legacy.h" |
1da177e4 | 34 | |
1da177e4 | 35 | /** |
32e7b94a DR |
36 | * drm_getmagic - Get unique magic of a client |
37 | * @dev: DRM device to operate on | |
38 | * @data: ioctl data containing the drm_auth object | |
39 | * @file_priv: DRM file that performs the operation | |
1da177e4 | 40 | * |
32e7b94a DR |
41 | * This looks up the unique magic of the passed client and returns it. If the |
42 | * client did not have a magic assigned, yet, a new one is registered. The magic | |
43 | * is stored in the passed drm_auth object. | |
1da177e4 | 44 | * |
32e7b94a | 45 | * Returns: 0 on success, negative error code on failure. |
1da177e4 | 46 | */ |
c153f45f | 47 | int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv) |
1da177e4 | 48 | { |
c153f45f | 49 | struct drm_auth *auth = data; |
32e7b94a | 50 | int ret = 0; |
1da177e4 | 51 | |
d2b34ee6 | 52 | mutex_lock(&dev->master_mutex); |
32e7b94a DR |
53 | if (!file_priv->magic) { |
54 | ret = idr_alloc(&file_priv->master->magic_map, file_priv, | |
55 | 1, 0, GFP_KERNEL); | |
56 | if (ret >= 0) | |
57 | file_priv->magic = ret; | |
1da177e4 | 58 | } |
32e7b94a | 59 | auth->magic = file_priv->magic; |
d2b34ee6 | 60 | mutex_unlock(&dev->master_mutex); |
1da177e4 | 61 | |
c153f45f EA |
62 | DRM_DEBUG("%u\n", auth->magic); |
63 | ||
32e7b94a | 64 | return ret < 0 ? ret : 0; |
1da177e4 LT |
65 | } |
66 | ||
67 | /** | |
32e7b94a DR |
68 | * drm_authmagic - Authenticate client with a magic |
69 | * @dev: DRM device to operate on | |
70 | * @data: ioctl data containing the drm_auth object | |
71 | * @file_priv: DRM file that performs the operation | |
1da177e4 | 72 | * |
32e7b94a | 73 | * This looks up a DRM client by the passed magic and authenticates it. |
1da177e4 | 74 | * |
32e7b94a | 75 | * Returns: 0 on success, negative error code on failure. |
1da177e4 | 76 | */ |
c153f45f EA |
77 | int drm_authmagic(struct drm_device *dev, void *data, |
78 | struct drm_file *file_priv) | |
1da177e4 | 79 | { |
c153f45f | 80 | struct drm_auth *auth = data; |
84b1fd10 | 81 | struct drm_file *file; |
1da177e4 | 82 | |
c153f45f | 83 | DRM_DEBUG("%u\n", auth->magic); |
32e7b94a | 84 | |
d2b34ee6 | 85 | mutex_lock(&dev->master_mutex); |
32e7b94a DR |
86 | file = idr_find(&file_priv->master->magic_map, auth->magic); |
87 | if (file) { | |
1da177e4 | 88 | file->authenticated = 1; |
32e7b94a | 89 | idr_replace(&file_priv->master->magic_map, NULL, auth->magic); |
1da177e4 | 90 | } |
d2b34ee6 | 91 | mutex_unlock(&dev->master_mutex); |
32e7b94a DR |
92 | |
93 | return file ? 0 : -EINVAL; | |
1da177e4 | 94 | } |
6548f4e7 SV |
95 | |
96 | static struct drm_master *drm_master_create(struct drm_device *dev) | |
97 | { | |
98 | struct drm_master *master; | |
99 | ||
100 | master = kzalloc(sizeof(*master), GFP_KERNEL); | |
101 | if (!master) | |
102 | return NULL; | |
103 | ||
104 | kref_init(&master->refcount); | |
105 | spin_lock_init(&master->lock.spinlock); | |
106 | init_waitqueue_head(&master->lock.lock_queue); | |
107 | idr_init(&master->magic_map); | |
108 | master->dev = dev; | |
109 | ||
110 | return master; | |
111 | } | |
112 | ||
d6ed682e SV |
113 | static int drm_set_master(struct drm_device *dev, struct drm_file *fpriv, |
114 | bool new_master) | |
115 | { | |
116 | int ret = 0; | |
117 | ||
118 | dev->master = drm_master_get(fpriv->master); | |
119 | fpriv->is_master = 1; | |
120 | if (dev->driver->master_set) { | |
121 | ret = dev->driver->master_set(dev, fpriv, new_master); | |
122 | if (unlikely(ret != 0)) { | |
123 | fpriv->is_master = 0; | |
124 | drm_master_put(&dev->master); | |
125 | } | |
126 | } | |
127 | ||
128 | return ret; | |
129 | } | |
130 | ||
6548f4e7 SV |
131 | /* |
132 | * drm_new_set_master - Allocate a new master object and become master for the | |
133 | * associated master realm. | |
134 | * | |
135 | * @dev: The associated device. | |
136 | * @fpriv: File private identifying the client. | |
137 | * | |
d2b34ee6 | 138 | * This function must be called with dev::master_mutex held. |
6548f4e7 SV |
139 | * Returns negative error code on failure. Zero on success. |
140 | */ | |
2cbae7e6 | 141 | static int drm_new_set_master(struct drm_device *dev, struct drm_file *fpriv) |
6548f4e7 SV |
142 | { |
143 | struct drm_master *old_master; | |
144 | int ret; | |
145 | ||
146 | lockdep_assert_held_once(&dev->master_mutex); | |
147 | ||
6548f4e7 | 148 | old_master = fpriv->master; |
d6ed682e SV |
149 | fpriv->master = drm_master_create(dev); |
150 | if (!fpriv->master) { | |
151 | fpriv->master = old_master; | |
152 | return -ENOMEM; | |
153 | } | |
6548f4e7 SV |
154 | |
155 | if (dev->driver->master_create) { | |
156 | ret = dev->driver->master_create(dev, fpriv->master); | |
157 | if (ret) | |
158 | goto out_err; | |
159 | } | |
6548f4e7 SV |
160 | fpriv->allowed_master = 1; |
161 | fpriv->authenticated = 1; | |
d6ed682e SV |
162 | |
163 | ret = drm_set_master(dev, fpriv, true); | |
164 | if (ret) | |
165 | goto out_err; | |
166 | ||
6548f4e7 SV |
167 | if (old_master) |
168 | drm_master_put(&old_master); | |
169 | ||
170 | return 0; | |
171 | ||
172 | out_err: | |
d6ed682e | 173 | /* drop references and restore old master on failure */ |
6548f4e7 SV |
174 | drm_master_put(&fpriv->master); |
175 | fpriv->master = old_master; | |
176 | ||
177 | return ret; | |
178 | } | |
179 | ||
180 | int drm_setmaster_ioctl(struct drm_device *dev, void *data, | |
181 | struct drm_file *file_priv) | |
182 | { | |
183 | int ret = 0; | |
184 | ||
185 | mutex_lock(&dev->master_mutex); | |
b3ac9f25 | 186 | if (drm_is_current_master(file_priv)) |
6548f4e7 SV |
187 | goto out_unlock; |
188 | ||
95c081c1 | 189 | if (dev->master) { |
6548f4e7 SV |
190 | ret = -EINVAL; |
191 | goto out_unlock; | |
192 | } | |
193 | ||
194 | if (!file_priv->master) { | |
195 | ret = -EINVAL; | |
196 | goto out_unlock; | |
197 | } | |
198 | ||
199 | if (!file_priv->allowed_master) { | |
200 | ret = drm_new_set_master(dev, file_priv); | |
201 | goto out_unlock; | |
202 | } | |
203 | ||
d6ed682e | 204 | ret = drm_set_master(dev, file_priv, false); |
6548f4e7 SV |
205 | out_unlock: |
206 | mutex_unlock(&dev->master_mutex); | |
207 | return ret; | |
208 | } | |
209 | ||
d6ed682e SV |
210 | static void drm_drop_master(struct drm_device *dev, |
211 | struct drm_file *fpriv) | |
212 | { | |
213 | if (dev->driver->master_drop) | |
214 | dev->driver->master_drop(dev, fpriv); | |
215 | drm_master_put(&dev->master); | |
216 | fpriv->is_master = 0; | |
217 | } | |
218 | ||
6548f4e7 SV |
219 | int drm_dropmaster_ioctl(struct drm_device *dev, void *data, |
220 | struct drm_file *file_priv) | |
221 | { | |
222 | int ret = -EINVAL; | |
223 | ||
224 | mutex_lock(&dev->master_mutex); | |
b3ac9f25 | 225 | if (!drm_is_current_master(file_priv)) |
6548f4e7 SV |
226 | goto out_unlock; |
227 | ||
95c081c1 | 228 | if (!dev->master) |
6548f4e7 SV |
229 | goto out_unlock; |
230 | ||
231 | ret = 0; | |
d6ed682e | 232 | drm_drop_master(dev, file_priv); |
6548f4e7 SV |
233 | out_unlock: |
234 | mutex_unlock(&dev->master_mutex); | |
235 | return ret; | |
236 | } | |
237 | ||
2cbae7e6 SV |
238 | int drm_master_open(struct drm_file *file_priv) |
239 | { | |
240 | struct drm_device *dev = file_priv->minor->dev; | |
241 | int ret = 0; | |
242 | ||
243 | /* if there is no current master make this fd it, but do not create | |
244 | * any master object for render clients */ | |
245 | mutex_lock(&dev->master_mutex); | |
95c081c1 | 246 | if (!dev->master) |
2cbae7e6 SV |
247 | ret = drm_new_set_master(dev, file_priv); |
248 | else | |
95c081c1 | 249 | file_priv->master = drm_master_get(dev->master); |
2cbae7e6 SV |
250 | mutex_unlock(&dev->master_mutex); |
251 | ||
252 | return ret; | |
253 | } | |
254 | ||
14d71ebd SV |
255 | void drm_master_release(struct drm_file *file_priv) |
256 | { | |
257 | struct drm_device *dev = file_priv->minor->dev; | |
0de4cc99 | 258 | struct drm_master *master = file_priv->master; |
14d71ebd | 259 | |
d2b34ee6 | 260 | mutex_lock(&dev->master_mutex); |
a77316bf SV |
261 | if (file_priv->magic) |
262 | idr_remove(&file_priv->master->magic_map, file_priv->magic); | |
a77316bf | 263 | |
b3ac9f25 | 264 | if (!drm_is_current_master(file_priv)) |
0de4cc99 | 265 | goto out; |
14d71ebd | 266 | |
0de4cc99 | 267 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) { |
14d71ebd SV |
268 | /* |
269 | * Since the master is disappearing, so is the | |
270 | * possibility to lock. | |
271 | */ | |
272 | mutex_lock(&dev->struct_mutex); | |
273 | if (master->lock.hw_lock) { | |
274 | if (dev->sigdata.lock == master->lock.hw_lock) | |
275 | dev->sigdata.lock = NULL; | |
276 | master->lock.hw_lock = NULL; | |
277 | master->lock.file_priv = NULL; | |
278 | wake_up_interruptible_all(&master->lock.lock_queue); | |
279 | } | |
280 | mutex_unlock(&dev->struct_mutex); | |
14d71ebd SV |
281 | } |
282 | ||
d6ed682e SV |
283 | if (dev->master == file_priv->master) |
284 | drm_drop_master(dev, file_priv); | |
0de4cc99 | 285 | out: |
14d71ebd SV |
286 | /* drop the master reference held by the file priv */ |
287 | if (file_priv->master) | |
288 | drm_master_put(&file_priv->master); | |
14d71ebd SV |
289 | mutex_unlock(&dev->master_mutex); |
290 | } | |
291 | ||
b3ac9f25 SV |
292 | bool drm_is_current_master(struct drm_file *fpriv) |
293 | { | |
294 | return fpriv->is_master; | |
295 | } | |
296 | EXPORT_SYMBOL(drm_is_current_master); | |
297 | ||
6548f4e7 SV |
298 | struct drm_master *drm_master_get(struct drm_master *master) |
299 | { | |
300 | kref_get(&master->refcount); | |
301 | return master; | |
302 | } | |
303 | EXPORT_SYMBOL(drm_master_get); | |
304 | ||
305 | static void drm_master_destroy(struct kref *kref) | |
306 | { | |
307 | struct drm_master *master = container_of(kref, struct drm_master, refcount); | |
308 | struct drm_device *dev = master->dev; | |
309 | ||
310 | if (dev->driver->master_destroy) | |
311 | dev->driver->master_destroy(dev, master); | |
312 | ||
313 | drm_legacy_master_rmmaps(dev, master); | |
314 | ||
315 | idr_destroy(&master->magic_map); | |
316 | kfree(master->unique); | |
317 | kfree(master); | |
318 | } | |
319 | ||
320 | void drm_master_put(struct drm_master **master) | |
321 | { | |
322 | kref_put(&(*master)->refcount, drm_master_destroy); | |
323 | *master = NULL; | |
324 | } | |
325 | EXPORT_SYMBOL(drm_master_put); |