]>
Commit | Line | Data |
---|---|---|
bcb877e4 | 1 | /* |
1da177e4 LT |
2 | * \author Rickard E. (Rik) Faith <faith@valinux.com> |
3 | * \author Daryll Strauss <daryll@valinux.com> | |
4 | * \author Gareth Hughes <gareth@valinux.com> | |
5 | */ | |
6 | ||
7 | /* | |
8 | * Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com | |
9 | * | |
10 | * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas. | |
11 | * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. | |
12 | * All Rights Reserved. | |
13 | * | |
14 | * Permission is hereby granted, free of charge, to any person obtaining a | |
15 | * copy of this software and associated documentation files (the "Software"), | |
16 | * to deal in the Software without restriction, including without limitation | |
17 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
18 | * and/or sell copies of the Software, and to permit persons to whom the | |
19 | * Software is furnished to do so, subject to the following conditions: | |
20 | * | |
21 | * The above copyright notice and this permission notice (including the next | |
22 | * paragraph) shall be included in all copies or substantial portions of the | |
23 | * Software. | |
24 | * | |
25 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
26 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
27 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
28 | * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
29 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
30 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
31 | * OTHER DEALINGS IN THE SOFTWARE. | |
32 | */ | |
33 | ||
760285e7 | 34 | #include <drm/drmP.h> |
1da177e4 | 35 | #include <linux/poll.h> |
5a0e3ad6 | 36 | #include <linux/slab.h> |
e0cd3608 | 37 | #include <linux/module.h> |
e7b96070 | 38 | #include "drm_legacy.h" |
67d0ec4e | 39 | #include "drm_internal.h" |
81065548 | 40 | #include "drm_crtc_internal.h" |
1da177e4 | 41 | |
0d639883 | 42 | /* from BKL pushdown */ |
58374713 AB |
43 | DEFINE_MUTEX(drm_global_mutex); |
44 | ||
bcb877e4 DV |
45 | /** |
46 | * DOC: file operations | |
47 | * | |
48 | * Drivers must define the file operations structure that forms the DRM | |
49 | * userspace API entry point, even though most of those operations are | |
50 | * implemented in the DRM core. The mandatory functions are drm_open(), | |
55edf41b JN |
51 | * drm_read(), drm_ioctl() and drm_compat_ioctl() if CONFIG_COMPAT is enabled |
52 | * (note that drm_compat_ioctl will be NULL if CONFIG_COMPAT=n). Drivers which | |
53 | * implement private ioctls that require 32/64 bit compatibility support must | |
54 | * provide their own .compat_ioctl() handler that processes private ioctls and | |
55 | * calls drm_compat_ioctl() for core ioctls. | |
bcb877e4 DV |
56 | * |
57 | * In addition drm_read() and drm_poll() provide support for DRM events. DRM | |
58 | * events are a generic and extensible means to send asynchronous events to | |
59 | * userspace through the file descriptor. They are used to send vblank event and | |
60 | * page flip completions by the KMS API. But drivers can also use it for their | |
61 | * own needs, e.g. to signal completion of rendering. | |
62 | * | |
63 | * The memory mapping implementation will vary depending on how the driver | |
64 | * manages memory. Legacy drivers will use the deprecated drm_legacy_mmap() | |
65 | * function, modern drivers should use one of the provided memory-manager | |
66 | * specific implementations. For GEM-based drivers this is drm_gem_mmap(). | |
67 | * | |
68 | * No other file operations are supported by the DRM userspace API. Overall the | |
da5335b8 | 69 | * following is an example #file_operations structure:: |
bcb877e4 DV |
70 | * |
71 | * static const example_drm_fops = { | |
72 | * .owner = THIS_MODULE, | |
73 | * .open = drm_open, | |
74 | * .release = drm_release, | |
75 | * .unlocked_ioctl = drm_ioctl, | |
55edf41b | 76 | * .compat_ioctl = drm_compat_ioctl, // NULL if CONFIG_COMPAT=n |
bcb877e4 DV |
77 | * .poll = drm_poll, |
78 | * .read = drm_read, | |
79 | * .llseek = no_llseek, | |
80 | * .mmap = drm_gem_mmap, | |
81 | * }; | |
82 | */ | |
83 | ||
1dcc0ceb | 84 | static int drm_open_helper(struct file *filp, struct drm_minor *minor); |
c94f7029 | 85 | |
84b1fd10 | 86 | static int drm_setup(struct drm_device * dev) |
1da177e4 | 87 | { |
1da177e4 LT |
88 | int ret; |
89 | ||
7d14bb6b | 90 | if (dev->driver->firstopen && |
fa538645 | 91 | drm_core_check_feature(dev, DRIVER_LEGACY)) { |
22eae947 | 92 | ret = dev->driver->firstopen(dev); |
b5e89ed5 | 93 | if (ret != 0) |
1da177e4 LT |
94 | return ret; |
95 | } | |
96 | ||
f336ab76 DV |
97 | ret = drm_legacy_dma_setup(dev); |
98 | if (ret < 0) | |
99 | return ret; | |
1da177e4 | 100 | |
1da177e4 | 101 | |
b5e89ed5 | 102 | DRM_DEBUG("\n"); |
1da177e4 LT |
103 | return 0; |
104 | } | |
105 | ||
106 | /** | |
bcb877e4 DV |
107 | * drm_open - open method for DRM file |
108 | * @inode: device inode | |
109 | * @filp: file pointer. | |
b5e89ed5 | 110 | * |
bcb877e4 DV |
111 | * This function must be used by drivers as their .open() #file_operations |
112 | * method. It looks up the correct DRM device and instantiates all the per-file | |
113 | * resources for it. | |
114 | * | |
115 | * RETURNS: | |
1da177e4 | 116 | * |
bcb877e4 | 117 | * 0 on success or negative errno value on falure. |
1da177e4 | 118 | */ |
b5e89ed5 | 119 | int drm_open(struct inode *inode, struct file *filp) |
1da177e4 | 120 | { |
1616c525 | 121 | struct drm_device *dev; |
2c14f28b | 122 | struct drm_minor *minor; |
1616c525 | 123 | int retcode; |
fdb40a08 | 124 | int need_setup = 0; |
b5e89ed5 | 125 | |
1616c525 DH |
126 | minor = drm_minor_acquire(iminor(inode)); |
127 | if (IS_ERR(minor)) | |
128 | return PTR_ERR(minor); | |
2c07a21d | 129 | |
1616c525 | 130 | dev = minor->dev; |
fdb40a08 IH |
131 | if (!dev->open_count++) |
132 | need_setup = 1; | |
fdb40a08 | 133 | |
6796cb16 DH |
134 | /* share address_space across all char-devs of a single device */ |
135 | filp->f_mapping = dev->anon_inode->i_mapping; | |
fdb40a08 | 136 | |
1dcc0ceb | 137 | retcode = drm_open_helper(filp, minor); |
fdb40a08 IH |
138 | if (retcode) |
139 | goto err_undo; | |
fdb40a08 IH |
140 | if (need_setup) { |
141 | retcode = drm_setup(dev); | |
142 | if (retcode) | |
143 | goto err_undo; | |
f453ba04 | 144 | } |
fdb40a08 | 145 | return 0; |
a2c0a97b | 146 | |
fdb40a08 | 147 | err_undo: |
fdb40a08 | 148 | dev->open_count--; |
1616c525 | 149 | drm_minor_release(minor); |
1da177e4 LT |
150 | return retcode; |
151 | } | |
152 | EXPORT_SYMBOL(drm_open); | |
153 | ||
bcb877e4 | 154 | /* |
d985c108 DA |
155 | * Check whether DRI will run on this CPU. |
156 | * | |
157 | * \return non-zero if the DRI will run on this CPU, or zero otherwise. | |
158 | */ | |
159 | static int drm_cpu_valid(void) | |
160 | { | |
d985c108 DA |
161 | #if defined(__sparc__) && !defined(__sparc_v9__) |
162 | return 0; /* No cmpxchg before v9 sparc. */ | |
163 | #endif | |
164 | return 1; | |
165 | } | |
166 | ||
bcb877e4 | 167 | /* |
d985c108 DA |
168 | * Called whenever a process opens /dev/drm. |
169 | * | |
d985c108 | 170 | * \param filp file pointer. |
f4aede2e | 171 | * \param minor acquired minor-object. |
d985c108 DA |
172 | * \return zero on success or a negative number on failure. |
173 | * | |
174 | * Creates and initializes a drm_file structure for the file private data in \p | |
175 | * filp and add it into the double linked list in \p dev. | |
176 | */ | |
1dcc0ceb | 177 | static int drm_open_helper(struct file *filp, struct drm_minor *minor) |
d985c108 | 178 | { |
f4aede2e | 179 | struct drm_device *dev = minor->dev; |
84b1fd10 | 180 | struct drm_file *priv; |
d985c108 DA |
181 | int ret; |
182 | ||
183 | if (filp->f_flags & O_EXCL) | |
184 | return -EBUSY; /* No exclusive opens */ | |
185 | if (!drm_cpu_valid()) | |
186 | return -EINVAL; | |
13bb9cc8 | 187 | if (dev->switch_power_state != DRM_SWITCH_POWER_ON && dev->switch_power_state != DRM_SWITCH_POWER_DYNAMIC_OFF) |
5bcf719b | 188 | return -EINVAL; |
d985c108 | 189 | |
f4aede2e | 190 | DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor->index); |
d985c108 | 191 | |
6ebc22e6 | 192 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
d985c108 DA |
193 | if (!priv) |
194 | return -ENOMEM; | |
195 | ||
d985c108 | 196 | filp->private_data = priv; |
6c340eac | 197 | priv->filp = filp; |
5fce5e0b | 198 | priv->pid = get_pid(task_pid(current)); |
f4aede2e | 199 | priv->minor = minor; |
df9b6a9c | 200 | |
d985c108 | 201 | /* for compatibility root is always authenticated */ |
3cb01a98 | 202 | priv->authenticated = capable(CAP_SYS_ADMIN); |
d985c108 DA |
203 | priv->lock_count = 0; |
204 | ||
bd1b331f | 205 | INIT_LIST_HEAD(&priv->lhead); |
f453ba04 | 206 | INIT_LIST_HEAD(&priv->fbs); |
4b096ac1 | 207 | mutex_init(&priv->fbs_lock); |
e2f5d2ea | 208 | INIT_LIST_HEAD(&priv->blobs); |
681047b4 | 209 | INIT_LIST_HEAD(&priv->pending_event_list); |
c9a9c5e0 KH |
210 | INIT_LIST_HEAD(&priv->event_list); |
211 | init_waitqueue_head(&priv->event_wait); | |
212 | priv->event_space = 4096; /* set aside 4k for event buffer */ | |
bd1b331f | 213 | |
9b2c0b7f CW |
214 | mutex_init(&priv->event_read_lock); |
215 | ||
1bcecfac | 216 | if (drm_core_check_feature(dev, DRIVER_GEM)) |
673a394b EA |
217 | drm_gem_open(dev, priv); |
218 | ||
3248877e DA |
219 | if (drm_core_check_feature(dev, DRIVER_PRIME)) |
220 | drm_prime_init_file_private(&priv->prime); | |
221 | ||
d985c108 DA |
222 | if (dev->driver->open) { |
223 | ret = dev->driver->open(dev, priv); | |
224 | if (ret < 0) | |
df9b6a9c | 225 | goto out_prime_destroy; |
d985c108 DA |
226 | } |
227 | ||
2cbae7e6 DV |
228 | if (drm_is_primary_client(priv)) { |
229 | ret = drm_master_open(priv); | |
a0af2e53 | 230 | if (ret) |
df9b6a9c | 231 | goto out_close; |
7c1c2871 | 232 | } |
bd1b331f | 233 | |
1d2ac403 | 234 | mutex_lock(&dev->filelist_mutex); |
bd1b331f | 235 | list_add(&priv->lhead, &dev->filelist); |
1d2ac403 | 236 | mutex_unlock(&dev->filelist_mutex); |
d985c108 DA |
237 | |
238 | #ifdef __alpha__ | |
239 | /* | |
240 | * Default the hose | |
241 | */ | |
242 | if (!dev->hose) { | |
243 | struct pci_dev *pci_dev; | |
244 | pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL); | |
245 | if (pci_dev) { | |
246 | dev->hose = pci_dev->sysdata; | |
247 | pci_dev_put(pci_dev); | |
248 | } | |
249 | if (!dev->hose) { | |
59c1ad3b YW |
250 | struct pci_bus *b = list_entry(pci_root_buses.next, |
251 | struct pci_bus, node); | |
d985c108 DA |
252 | if (b) |
253 | dev->hose = b->sysdata; | |
254 | } | |
255 | } | |
256 | #endif | |
257 | ||
258 | return 0; | |
df9b6a9c SWK |
259 | |
260 | out_close: | |
261 | if (dev->driver->postclose) | |
262 | dev->driver->postclose(dev, priv); | |
263 | out_prime_destroy: | |
264 | if (drm_core_check_feature(dev, DRIVER_PRIME)) | |
265 | drm_prime_destroy_file_private(&priv->prime); | |
1bcecfac | 266 | if (drm_core_check_feature(dev, DRIVER_GEM)) |
df9b6a9c | 267 | drm_gem_release(dev, priv); |
df9b6a9c | 268 | put_pid(priv->pid); |
9a298b2a | 269 | kfree(priv); |
d985c108 DA |
270 | filp->private_data = NULL; |
271 | return ret; | |
272 | } | |
273 | ||
c9a9c5e0 KH |
274 | static void drm_events_release(struct drm_file *file_priv) |
275 | { | |
276 | struct drm_device *dev = file_priv->minor->dev; | |
277 | struct drm_pending_event *e, *et; | |
c9a9c5e0 KH |
278 | unsigned long flags; |
279 | ||
280 | spin_lock_irqsave(&dev->event_lock, flags); | |
281 | ||
681047b4 DV |
282 | /* Unlink pending events */ |
283 | list_for_each_entry_safe(e, et, &file_priv->pending_event_list, | |
284 | pending_link) { | |
285 | list_del(&e->pending_link); | |
286 | e->file_priv = NULL; | |
287 | } | |
288 | ||
c9a9c5e0 | 289 | /* Remove unconsumed events */ |
1dda6805 YC |
290 | list_for_each_entry_safe(e, et, &file_priv->event_list, link) { |
291 | list_del(&e->link); | |
1b47aaf9 | 292 | kfree(e); |
1dda6805 | 293 | } |
c9a9c5e0 KH |
294 | |
295 | spin_unlock_irqrestore(&dev->event_lock, flags); | |
296 | } | |
297 | ||
bcb877e4 | 298 | /* |
1c8887dd DH |
299 | * drm_legacy_dev_reinit |
300 | * | |
301 | * Reinitializes a legacy/ums drm device in it's lastclose function. | |
302 | */ | |
303 | static void drm_legacy_dev_reinit(struct drm_device *dev) | |
304 | { | |
68dfbeba DV |
305 | if (dev->irq_enabled) |
306 | drm_irq_uninstall(dev); | |
307 | ||
308 | mutex_lock(&dev->struct_mutex); | |
309 | ||
310 | drm_legacy_agp_clear(dev); | |
311 | ||
312 | drm_legacy_sg_cleanup(dev); | |
313 | drm_legacy_vma_flush(dev); | |
314 | drm_legacy_dma_takedown(dev); | |
315 | ||
316 | mutex_unlock(&dev->struct_mutex); | |
1c8887dd | 317 | |
1c8887dd DH |
318 | dev->sigdata.lock = NULL; |
319 | ||
320 | dev->context_flag = 0; | |
321 | dev->last_context = 0; | |
322 | dev->if_version = 0; | |
68dfbeba DV |
323 | |
324 | DRM_DEBUG("lastclose completed\n"); | |
1c8887dd DH |
325 | } |
326 | ||
bcb877e4 | 327 | /* |
1c8887dd DH |
328 | * Take down the DRM device. |
329 | * | |
330 | * \param dev DRM device structure. | |
331 | * | |
332 | * Frees every resource in \p dev. | |
333 | * | |
334 | * \sa drm_device | |
335 | */ | |
68dfbeba | 336 | void drm_lastclose(struct drm_device * dev) |
1c8887dd | 337 | { |
1c8887dd DH |
338 | DRM_DEBUG("\n"); |
339 | ||
340 | if (dev->driver->lastclose) | |
341 | dev->driver->lastclose(dev); | |
342 | DRM_DEBUG("driver lastclose completed\n"); | |
343 | ||
fa538645 | 344 | if (drm_core_check_feature(dev, DRIVER_LEGACY)) |
68dfbeba | 345 | drm_legacy_dev_reinit(dev); |
1c8887dd DH |
346 | } |
347 | ||
1da177e4 | 348 | /** |
bcb877e4 DV |
349 | * drm_release - release method for DRM file |
350 | * @inode: device inode | |
351 | * @filp: file pointer. | |
1da177e4 | 352 | * |
bcb877e4 DV |
353 | * This function must be used by drivers as their .release() #file_operations |
354 | * method. It frees any resources associated with the open file, and if this is | |
355 | * the last open file for the DRM device also proceeds to call drm_lastclose(). | |
1da177e4 | 356 | * |
bcb877e4 DV |
357 | * RETURNS: |
358 | * | |
359 | * Always succeeds and returns 0. | |
1da177e4 | 360 | */ |
b5e89ed5 | 361 | int drm_release(struct inode *inode, struct file *filp) |
1da177e4 | 362 | { |
6c340eac | 363 | struct drm_file *file_priv = filp->private_data; |
1616c525 DH |
364 | struct drm_minor *minor = file_priv->minor; |
365 | struct drm_device *dev = minor->dev; | |
1da177e4 | 366 | |
58374713 | 367 | mutex_lock(&drm_global_mutex); |
1da177e4 | 368 | |
b5e89ed5 | 369 | DRM_DEBUG("open_count = %d\n", dev->open_count); |
1da177e4 | 370 | |
1d2ac403 | 371 | mutex_lock(&dev->filelist_mutex); |
dff01de1 | 372 | list_del(&file_priv->lhead); |
1d2ac403 DV |
373 | mutex_unlock(&dev->filelist_mutex); |
374 | ||
22eae947 | 375 | if (dev->driver->preclose) |
6c340eac | 376 | dev->driver->preclose(dev, file_priv); |
1da177e4 LT |
377 | |
378 | /* ======================================================== | |
379 | * Begin inline drm_release | |
380 | */ | |
381 | ||
b5e89ed5 | 382 | DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n", |
ba25f9dc | 383 | task_pid_nr(current), |
5817878c | 384 | (long)old_encode_dev(file_priv->minor->kdev->devt), |
b5e89ed5 DA |
385 | dev->open_count); |
386 | ||
fa538645 | 387 | if (drm_core_check_feature(dev, DRIVER_LEGACY)) |
1a75a222 | 388 | drm_legacy_lock_release(dev, filp); |
1da177e4 | 389 | |
67cb4b4d | 390 | if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) |
a266162a | 391 | drm_legacy_reclaim_buffers(dev, file_priv); |
67cb4b4d | 392 | |
c9a9c5e0 KH |
393 | drm_events_release(file_priv); |
394 | ||
e2f5d2ea | 395 | if (drm_core_check_feature(dev, DRIVER_MODESET)) { |
ea39f835 | 396 | drm_fb_release(file_priv); |
e2f5d2ea DS |
397 | drm_property_destroy_user_blobs(dev, file_priv); |
398 | } | |
ea39f835 | 399 | |
1bcecfac | 400 | if (drm_core_check_feature(dev, DRIVER_GEM)) |
4e47e02d P |
401 | drm_gem_release(dev, file_priv); |
402 | ||
e7b96070 | 403 | drm_legacy_ctxbitmap_flush(dev, file_priv); |
1da177e4 | 404 | |
14d71ebd DV |
405 | if (drm_is_primary_client(file_priv)) |
406 | drm_master_release(file_priv); | |
c996fd0b | 407 | |
22eae947 | 408 | if (dev->driver->postclose) |
6c340eac | 409 | dev->driver->postclose(dev, file_priv); |
3248877e DA |
410 | |
411 | if (drm_core_check_feature(dev, DRIVER_PRIME)) | |
412 | drm_prime_destroy_file_private(&file_priv->prime); | |
413 | ||
ddde4371 VS |
414 | WARN_ON(!list_empty(&file_priv->event_list)); |
415 | ||
5fce5e0b | 416 | put_pid(file_priv->pid); |
9a298b2a | 417 | kfree(file_priv); |
1da177e4 LT |
418 | |
419 | /* ======================================================== | |
420 | * End inline drm_release | |
421 | */ | |
422 | ||
b5e89ed5 | 423 | if (!--dev->open_count) { |
68dfbeba | 424 | drm_lastclose(dev); |
2c07a21d DA |
425 | if (drm_device_is_unplugged(dev)) |
426 | drm_put_dev(dev); | |
1da177e4 | 427 | } |
58374713 | 428 | mutex_unlock(&drm_global_mutex); |
1da177e4 | 429 | |
1616c525 DH |
430 | drm_minor_release(minor); |
431 | ||
68dfbeba | 432 | return 0; |
1da177e4 LT |
433 | } |
434 | EXPORT_SYMBOL(drm_release); | |
435 | ||
bcb877e4 DV |
436 | /** |
437 | * drm_read - read method for DRM file | |
438 | * @filp: file pointer | |
439 | * @buffer: userspace destination pointer for the read | |
440 | * @count: count in bytes to read | |
441 | * @offset: offset to read | |
442 | * | |
443 | * This function must be used by drivers as their .read() #file_operations | |
444 | * method iff they use DRM events for asynchronous signalling to userspace. | |
445 | * Since events are used by the KMS API for vblank and page flip completion this | |
446 | * means all modern display drivers must use it. | |
447 | * | |
448 | * @offset is ignore, DRM events are read like a pipe. Therefore drivers also | |
449 | * must set the .llseek() #file_operation to no_llseek(). Polling support is | |
450 | * provided by drm_poll(). | |
451 | * | |
452 | * This function will only ever read a full event. Therefore userspace must | |
453 | * supply a big enough buffer to fit any event to ensure forward progress. Since | |
454 | * the maximum event space is currently 4K it's recommended to just use that for | |
455 | * safety. | |
456 | * | |
457 | * RETURNS: | |
458 | * | |
459 | * Number of bytes read (always aligned to full events, and can be 0) or a | |
460 | * negative error code on failure. | |
461 | */ | |
cdd1cf79 CW |
462 | ssize_t drm_read(struct file *filp, char __user *buffer, |
463 | size_t count, loff_t *offset) | |
c9a9c5e0 | 464 | { |
cdd1cf79 | 465 | struct drm_file *file_priv = filp->private_data; |
c9a9c5e0 | 466 | struct drm_device *dev = file_priv->minor->dev; |
9b2c0b7f | 467 | ssize_t ret; |
c9a9c5e0 | 468 | |
cdd1cf79 CW |
469 | if (!access_ok(VERIFY_WRITE, buffer, count)) |
470 | return -EFAULT; | |
c9a9c5e0 | 471 | |
9b2c0b7f CW |
472 | ret = mutex_lock_interruptible(&file_priv->event_read_lock); |
473 | if (ret) | |
474 | return ret; | |
475 | ||
cdd1cf79 | 476 | for (;;) { |
83eb64c8 CW |
477 | struct drm_pending_event *e = NULL; |
478 | ||
479 | spin_lock_irq(&dev->event_lock); | |
480 | if (!list_empty(&file_priv->event_list)) { | |
481 | e = list_first_entry(&file_priv->event_list, | |
482 | struct drm_pending_event, link); | |
483 | file_priv->event_space += e->event->length; | |
484 | list_del(&e->link); | |
485 | } | |
486 | spin_unlock_irq(&dev->event_lock); | |
487 | ||
488 | if (e == NULL) { | |
cdd1cf79 CW |
489 | if (ret) |
490 | break; | |
c9a9c5e0 | 491 | |
cdd1cf79 CW |
492 | if (filp->f_flags & O_NONBLOCK) { |
493 | ret = -EAGAIN; | |
494 | break; | |
495 | } | |
c9a9c5e0 | 496 | |
9b2c0b7f | 497 | mutex_unlock(&file_priv->event_read_lock); |
cdd1cf79 CW |
498 | ret = wait_event_interruptible(file_priv->event_wait, |
499 | !list_empty(&file_priv->event_list)); | |
9b2c0b7f CW |
500 | if (ret >= 0) |
501 | ret = mutex_lock_interruptible(&file_priv->event_read_lock); | |
502 | if (ret) | |
503 | return ret; | |
cdd1cf79 | 504 | } else { |
83eb64c8 CW |
505 | unsigned length = e->event->length; |
506 | ||
507 | if (length > count - ret) { | |
508 | put_back_event: | |
509 | spin_lock_irq(&dev->event_lock); | |
510 | file_priv->event_space -= length; | |
511 | list_add(&e->link, &file_priv->event_list); | |
512 | spin_unlock_irq(&dev->event_lock); | |
cdd1cf79 | 513 | break; |
83eb64c8 | 514 | } |
cdd1cf79 | 515 | |
83eb64c8 | 516 | if (copy_to_user(buffer + ret, e->event, length)) { |
cdd1cf79 CW |
517 | if (ret == 0) |
518 | ret = -EFAULT; | |
83eb64c8 | 519 | goto put_back_event; |
cdd1cf79 | 520 | } |
c9a9c5e0 | 521 | |
83eb64c8 | 522 | ret += length; |
1b47aaf9 | 523 | kfree(e); |
c9a9c5e0 | 524 | } |
c9a9c5e0 | 525 | } |
9b2c0b7f | 526 | mutex_unlock(&file_priv->event_read_lock); |
c9a9c5e0 | 527 | |
cdd1cf79 | 528 | return ret; |
c9a9c5e0 KH |
529 | } |
530 | EXPORT_SYMBOL(drm_read); | |
531 | ||
bcb877e4 DV |
532 | /** |
533 | * drm_poll - poll method for DRM file | |
534 | * @filp: file pointer | |
535 | * @wait: poll waiter table | |
536 | * | |
537 | * This function must be used by drivers as their .read() #file_operations | |
538 | * method iff they use DRM events for asynchronous signalling to userspace. | |
539 | * Since events are used by the KMS API for vblank and page flip completion this | |
540 | * means all modern display drivers must use it. | |
541 | * | |
542 | * See also drm_read(). | |
543 | * | |
544 | * RETURNS: | |
545 | * | |
546 | * Mask of POLL flags indicating the current status of the file. | |
547 | */ | |
1da177e4 LT |
548 | unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait) |
549 | { | |
c9a9c5e0 KH |
550 | struct drm_file *file_priv = filp->private_data; |
551 | unsigned int mask = 0; | |
552 | ||
553 | poll_wait(filp, &file_priv->event_wait, wait); | |
554 | ||
555 | if (!list_empty(&file_priv->event_list)) | |
556 | mask |= POLLIN | POLLRDNORM; | |
557 | ||
558 | return mask; | |
1da177e4 | 559 | } |
b5e89ed5 | 560 | EXPORT_SYMBOL(drm_poll); |
2dd500f1 DV |
561 | |
562 | /** | |
4020b220 | 563 | * drm_event_reserve_init_locked - init a DRM event and reserve space for it |
2dd500f1 DV |
564 | * @dev: DRM device |
565 | * @file_priv: DRM file private data | |
566 | * @p: tracking structure for the pending event | |
567 | * @e: actual event data to deliver to userspace | |
568 | * | |
569 | * This function prepares the passed in event for eventual delivery. If the event | |
570 | * doesn't get delivered (because the IOCTL fails later on, before queuing up | |
571 | * anything) then the even must be cancelled and freed using | |
fb740cf2 DV |
572 | * drm_event_cancel_free(). Successfully initialized events should be sent out |
573 | * using drm_send_event() or drm_send_event_locked() to signal completion of the | |
574 | * asynchronous event to userspace. | |
2dd500f1 DV |
575 | * |
576 | * If callers embedded @p into a larger structure it must be allocated with | |
577 | * kmalloc and @p must be the first member element. | |
578 | * | |
4020b220 | 579 | * This is the locked version of drm_event_reserve_init() for callers which |
ef40cbf9 | 580 | * already hold &drm_device.event_lock. |
4020b220 | 581 | * |
2dd500f1 DV |
582 | * RETURNS: |
583 | * | |
584 | * 0 on success or a negative error code on failure. | |
585 | */ | |
4020b220 DV |
586 | int drm_event_reserve_init_locked(struct drm_device *dev, |
587 | struct drm_file *file_priv, | |
588 | struct drm_pending_event *p, | |
589 | struct drm_event *e) | |
2dd500f1 | 590 | { |
4020b220 DV |
591 | if (file_priv->event_space < e->length) |
592 | return -ENOMEM; | |
2dd500f1 DV |
593 | |
594 | file_priv->event_space -= e->length; | |
595 | ||
596 | p->event = e; | |
681047b4 | 597 | list_add(&p->pending_link, &file_priv->pending_event_list); |
2dd500f1 DV |
598 | p->file_priv = file_priv; |
599 | ||
4020b220 DV |
600 | return 0; |
601 | } | |
602 | EXPORT_SYMBOL(drm_event_reserve_init_locked); | |
603 | ||
604 | /** | |
605 | * drm_event_reserve_init - init a DRM event and reserve space for it | |
606 | * @dev: DRM device | |
607 | * @file_priv: DRM file private data | |
608 | * @p: tracking structure for the pending event | |
609 | * @e: actual event data to deliver to userspace | |
610 | * | |
611 | * This function prepares the passed in event for eventual delivery. If the event | |
612 | * doesn't get delivered (because the IOCTL fails later on, before queuing up | |
613 | * anything) then the even must be cancelled and freed using | |
614 | * drm_event_cancel_free(). Successfully initialized events should be sent out | |
615 | * using drm_send_event() or drm_send_event_locked() to signal completion of the | |
616 | * asynchronous event to userspace. | |
617 | * | |
618 | * If callers embedded @p into a larger structure it must be allocated with | |
619 | * kmalloc and @p must be the first member element. | |
620 | * | |
ef40cbf9 | 621 | * Callers which already hold &drm_device.event_lock should use |
20c9ca4f | 622 | * drm_event_reserve_init_locked() instead. |
4020b220 DV |
623 | * |
624 | * RETURNS: | |
625 | * | |
626 | * 0 on success or a negative error code on failure. | |
627 | */ | |
628 | int drm_event_reserve_init(struct drm_device *dev, | |
629 | struct drm_file *file_priv, | |
630 | struct drm_pending_event *p, | |
631 | struct drm_event *e) | |
632 | { | |
633 | unsigned long flags; | |
634 | int ret; | |
635 | ||
636 | spin_lock_irqsave(&dev->event_lock, flags); | |
637 | ret = drm_event_reserve_init_locked(dev, file_priv, p, e); | |
2dd500f1 | 638 | spin_unlock_irqrestore(&dev->event_lock, flags); |
4020b220 | 639 | |
2dd500f1 DV |
640 | return ret; |
641 | } | |
642 | EXPORT_SYMBOL(drm_event_reserve_init); | |
643 | ||
644 | /** | |
645 | * drm_event_cancel_free - free a DRM event and release it's space | |
646 | * @dev: DRM device | |
647 | * @p: tracking structure for the pending event | |
648 | * | |
649 | * This function frees the event @p initialized with drm_event_reserve_init() | |
650 | * and releases any allocated space. | |
651 | */ | |
652 | void drm_event_cancel_free(struct drm_device *dev, | |
653 | struct drm_pending_event *p) | |
654 | { | |
655 | unsigned long flags; | |
656 | spin_lock_irqsave(&dev->event_lock, flags); | |
681047b4 DV |
657 | if (p->file_priv) { |
658 | p->file_priv->event_space += p->event->length; | |
659 | list_del(&p->pending_link); | |
660 | } | |
2dd500f1 | 661 | spin_unlock_irqrestore(&dev->event_lock, flags); |
838de39f GP |
662 | |
663 | if (p->fence) | |
f54d1867 | 664 | dma_fence_put(p->fence); |
838de39f | 665 | |
1b47aaf9 | 666 | kfree(p); |
2dd500f1 DV |
667 | } |
668 | EXPORT_SYMBOL(drm_event_cancel_free); | |
fb740cf2 DV |
669 | |
670 | /** | |
671 | * drm_send_event_locked - send DRM event to file descriptor | |
672 | * @dev: DRM device | |
673 | * @e: DRM event to deliver | |
674 | * | |
675 | * This function sends the event @e, initialized with drm_event_reserve_init(), | |
676 | * to its associated userspace DRM file. Callers must already hold | |
ef40cbf9 | 677 | * &drm_device.event_lock, see drm_send_event() for the unlocked version. |
681047b4 DV |
678 | * |
679 | * Note that the core will take care of unlinking and disarming events when the | |
680 | * corresponding DRM file is closed. Drivers need not worry about whether the | |
681 | * DRM file for this event still exists and can call this function upon | |
682 | * completion of the asynchronous work unconditionally. | |
fb740cf2 DV |
683 | */ |
684 | void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e) | |
685 | { | |
686 | assert_spin_locked(&dev->event_lock); | |
687 | ||
3b24f7d6 | 688 | if (e->completion) { |
3b24f7d6 | 689 | complete_all(e->completion); |
24835e44 | 690 | e->completion_release(e->completion); |
3b24f7d6 DV |
691 | e->completion = NULL; |
692 | } | |
693 | ||
1b47aaf9 | 694 | if (e->fence) { |
f54d1867 CW |
695 | dma_fence_signal(e->fence); |
696 | dma_fence_put(e->fence); | |
1b47aaf9 GP |
697 | } |
698 | ||
681047b4 | 699 | if (!e->file_priv) { |
1b47aaf9 | 700 | kfree(e); |
681047b4 DV |
701 | return; |
702 | } | |
703 | ||
704 | list_del(&e->pending_link); | |
fb740cf2 DV |
705 | list_add_tail(&e->link, |
706 | &e->file_priv->event_list); | |
707 | wake_up_interruptible(&e->file_priv->event_wait); | |
708 | } | |
709 | EXPORT_SYMBOL(drm_send_event_locked); | |
710 | ||
711 | /** | |
712 | * drm_send_event - send DRM event to file descriptor | |
713 | * @dev: DRM device | |
714 | * @e: DRM event to deliver | |
715 | * | |
716 | * This function sends the event @e, initialized with drm_event_reserve_init(), | |
ef40cbf9 DV |
717 | * to its associated userspace DRM file. This function acquires |
718 | * &drm_device.event_lock, see drm_send_event_locked() for callers which already | |
719 | * hold this lock. | |
681047b4 DV |
720 | * |
721 | * Note that the core will take care of unlinking and disarming events when the | |
722 | * corresponding DRM file is closed. Drivers need not worry about whether the | |
723 | * DRM file for this event still exists and can call this function upon | |
724 | * completion of the asynchronous work unconditionally. | |
fb740cf2 DV |
725 | */ |
726 | void drm_send_event(struct drm_device *dev, struct drm_pending_event *e) | |
727 | { | |
728 | unsigned long irqflags; | |
729 | ||
730 | spin_lock_irqsave(&dev->event_lock, irqflags); | |
731 | drm_send_event_locked(dev, e); | |
732 | spin_unlock_irqrestore(&dev->event_lock, irqflags); | |
733 | } | |
734 | EXPORT_SYMBOL(drm_send_event); |