]>
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 | ||
0500c04e SR |
34 | #include <linux/dma-fence.h> |
35 | #include <linux/module.h> | |
36 | #include <linux/pci.h> | |
1da177e4 | 37 | #include <linux/poll.h> |
5a0e3ad6 | 38 | #include <linux/slab.h> |
a8f8b1d9 | 39 | |
c76f0f7c | 40 | #include <drm/drm_client.h> |
0500c04e | 41 | #include <drm/drm_drv.h> |
a8f8b1d9 | 42 | #include <drm/drm_file.h> |
0500c04e | 43 | #include <drm/drm_print.h> |
a8f8b1d9 | 44 | |
81065548 | 45 | #include "drm_crtc_internal.h" |
0500c04e SR |
46 | #include "drm_internal.h" |
47 | #include "drm_legacy.h" | |
1da177e4 | 48 | |
0d639883 | 49 | /* from BKL pushdown */ |
58374713 AB |
50 | DEFINE_MUTEX(drm_global_mutex); |
51 | ||
bcb877e4 DV |
52 | /** |
53 | * DOC: file operations | |
54 | * | |
55 | * Drivers must define the file operations structure that forms the DRM | |
56 | * userspace API entry point, even though most of those operations are | |
b93658f8 DV |
57 | * implemented in the DRM core. The resulting &struct file_operations must be |
58 | * stored in the &drm_driver.fops field. The mandatory functions are drm_open(), | |
55edf41b | 59 | * drm_read(), drm_ioctl() and drm_compat_ioctl() if CONFIG_COMPAT is enabled |
b93658f8 DV |
60 | * Note that drm_compat_ioctl will be NULL if CONFIG_COMPAT=n, so there's no |
61 | * need to sprinkle #ifdef into the code. Drivers which implement private ioctls | |
62 | * that require 32/64 bit compatibility support must provide their own | |
63 | * &file_operations.compat_ioctl handler that processes private ioctls and calls | |
64 | * drm_compat_ioctl() for core ioctls. | |
bcb877e4 DV |
65 | * |
66 | * In addition drm_read() and drm_poll() provide support for DRM events. DRM | |
67 | * events are a generic and extensible means to send asynchronous events to | |
68 | * userspace through the file descriptor. They are used to send vblank event and | |
69 | * page flip completions by the KMS API. But drivers can also use it for their | |
70 | * own needs, e.g. to signal completion of rendering. | |
71 | * | |
b93658f8 DV |
72 | * For the driver-side event interface see drm_event_reserve_init() and |
73 | * drm_send_event() as the main starting points. | |
74 | * | |
bcb877e4 DV |
75 | * The memory mapping implementation will vary depending on how the driver |
76 | * manages memory. Legacy drivers will use the deprecated drm_legacy_mmap() | |
77 | * function, modern drivers should use one of the provided memory-manager | |
b93658f8 DV |
78 | * specific implementations. For GEM-based drivers this is drm_gem_mmap(), and |
79 | * for drivers which use the CMA GEM helpers it's drm_gem_cma_mmap(). | |
bcb877e4 DV |
80 | * |
81 | * No other file operations are supported by the DRM userspace API. Overall the | |
bb2eaba6 | 82 | * following is an example &file_operations structure:: |
bcb877e4 DV |
83 | * |
84 | * static const example_drm_fops = { | |
85 | * .owner = THIS_MODULE, | |
86 | * .open = drm_open, | |
87 | * .release = drm_release, | |
88 | * .unlocked_ioctl = drm_ioctl, | |
55edf41b | 89 | * .compat_ioctl = drm_compat_ioctl, // NULL if CONFIG_COMPAT=n |
bcb877e4 DV |
90 | * .poll = drm_poll, |
91 | * .read = drm_read, | |
92 | * .llseek = no_llseek, | |
93 | * .mmap = drm_gem_mmap, | |
94 | * }; | |
b93658f8 | 95 | * |
f42e1819 DV |
96 | * For plain GEM based drivers there is the DEFINE_DRM_GEM_FOPS() macro, and for |
97 | * CMA based drivers there is the DEFINE_DRM_GEM_CMA_FOPS() macro to make this | |
98 | * simpler. | |
bb2eaba6 DV |
99 | * |
100 | * The driver's &file_operations must be stored in &drm_driver.fops. | |
101 | * | |
102 | * For driver-private IOCTL handling see the more detailed discussion in | |
103 | * :ref:`IOCTL support in the userland interfaces chapter<drm_driver_ioctl>`. | |
bcb877e4 DV |
104 | */ |
105 | ||
1572042a DH |
106 | /** |
107 | * drm_file_alloc - allocate file context | |
108 | * @minor: minor to allocate on | |
109 | * | |
110 | * This allocates a new DRM file context. It is not linked into any context and | |
111 | * can be used by the caller freely. Note that the context keeps a pointer to | |
112 | * @minor, so it must be freed before @minor is. | |
113 | * | |
114 | * RETURNS: | |
115 | * Pointer to newly allocated context, ERR_PTR on failure. | |
116 | */ | |
117 | struct drm_file *drm_file_alloc(struct drm_minor *minor) | |
118 | { | |
119 | struct drm_device *dev = minor->dev; | |
120 | struct drm_file *file; | |
121 | int ret; | |
122 | ||
123 | file = kzalloc(sizeof(*file), GFP_KERNEL); | |
124 | if (!file) | |
125 | return ERR_PTR(-ENOMEM); | |
126 | ||
127 | file->pid = get_pid(task_pid(current)); | |
128 | file->minor = minor; | |
129 | ||
130 | /* for compatibility root is always authenticated */ | |
131 | file->authenticated = capable(CAP_SYS_ADMIN); | |
1572042a DH |
132 | |
133 | INIT_LIST_HEAD(&file->lhead); | |
134 | INIT_LIST_HEAD(&file->fbs); | |
135 | mutex_init(&file->fbs_lock); | |
136 | INIT_LIST_HEAD(&file->blobs); | |
137 | INIT_LIST_HEAD(&file->pending_event_list); | |
138 | INIT_LIST_HEAD(&file->event_list); | |
139 | init_waitqueue_head(&file->event_wait); | |
140 | file->event_space = 4096; /* set aside 4k for event buffer */ | |
141 | ||
142 | mutex_init(&file->event_read_lock); | |
143 | ||
144 | if (drm_core_check_feature(dev, DRIVER_GEM)) | |
145 | drm_gem_open(dev, file); | |
146 | ||
147 | if (drm_core_check_feature(dev, DRIVER_SYNCOBJ)) | |
148 | drm_syncobj_open(file); | |
149 | ||
150 | if (drm_core_check_feature(dev, DRIVER_PRIME)) | |
151 | drm_prime_init_file_private(&file->prime); | |
152 | ||
153 | if (dev->driver->open) { | |
154 | ret = dev->driver->open(dev, file); | |
155 | if (ret < 0) | |
156 | goto out_prime_destroy; | |
157 | } | |
158 | ||
1572042a DH |
159 | return file; |
160 | ||
1572042a DH |
161 | out_prime_destroy: |
162 | if (drm_core_check_feature(dev, DRIVER_PRIME)) | |
163 | drm_prime_destroy_file_private(&file->prime); | |
164 | if (drm_core_check_feature(dev, DRIVER_SYNCOBJ)) | |
165 | drm_syncobj_release(file); | |
166 | if (drm_core_check_feature(dev, DRIVER_GEM)) | |
167 | drm_gem_release(dev, file); | |
168 | put_pid(file->pid); | |
169 | kfree(file); | |
170 | ||
171 | return ERR_PTR(ret); | |
172 | } | |
173 | ||
174 | static void drm_events_release(struct drm_file *file_priv) | |
175 | { | |
176 | struct drm_device *dev = file_priv->minor->dev; | |
177 | struct drm_pending_event *e, *et; | |
178 | unsigned long flags; | |
179 | ||
180 | spin_lock_irqsave(&dev->event_lock, flags); | |
181 | ||
182 | /* Unlink pending events */ | |
183 | list_for_each_entry_safe(e, et, &file_priv->pending_event_list, | |
184 | pending_link) { | |
185 | list_del(&e->pending_link); | |
186 | e->file_priv = NULL; | |
187 | } | |
188 | ||
189 | /* Remove unconsumed events */ | |
190 | list_for_each_entry_safe(e, et, &file_priv->event_list, link) { | |
191 | list_del(&e->link); | |
192 | kfree(e); | |
193 | } | |
194 | ||
195 | spin_unlock_irqrestore(&dev->event_lock, flags); | |
196 | } | |
197 | ||
198 | /** | |
199 | * drm_file_free - free file context | |
200 | * @file: context to free, or NULL | |
201 | * | |
202 | * This destroys and deallocates a DRM file context previously allocated via | |
203 | * drm_file_alloc(). The caller must make sure to unlink it from any contexts | |
204 | * before calling this. | |
205 | * | |
206 | * If NULL is passed, this is a no-op. | |
207 | * | |
208 | * RETURNS: | |
209 | * 0 on success, or error code on failure. | |
210 | */ | |
211 | void drm_file_free(struct drm_file *file) | |
212 | { | |
213 | struct drm_device *dev; | |
214 | ||
215 | if (!file) | |
216 | return; | |
217 | ||
218 | dev = file->minor->dev; | |
219 | ||
220 | DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n", | |
221 | task_pid_nr(current), | |
222 | (long)old_encode_dev(file->minor->kdev->devt), | |
223 | dev->open_count); | |
224 | ||
225 | if (drm_core_check_feature(dev, DRIVER_LEGACY) && | |
226 | dev->driver->preclose) | |
227 | dev->driver->preclose(dev, file); | |
228 | ||
229 | if (drm_core_check_feature(dev, DRIVER_LEGACY)) | |
230 | drm_legacy_lock_release(dev, file->filp); | |
231 | ||
232 | if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) | |
233 | drm_legacy_reclaim_buffers(dev, file); | |
234 | ||
235 | drm_events_release(file); | |
236 | ||
237 | if (drm_core_check_feature(dev, DRIVER_MODESET)) { | |
238 | drm_fb_release(file); | |
239 | drm_property_destroy_user_blobs(dev, file); | |
240 | } | |
241 | ||
242 | if (drm_core_check_feature(dev, DRIVER_SYNCOBJ)) | |
243 | drm_syncobj_release(file); | |
244 | ||
245 | if (drm_core_check_feature(dev, DRIVER_GEM)) | |
246 | drm_gem_release(dev, file); | |
247 | ||
248 | drm_legacy_ctxbitmap_flush(dev, file); | |
249 | ||
250 | if (drm_is_primary_client(file)) | |
251 | drm_master_release(file); | |
252 | ||
253 | if (dev->driver->postclose) | |
254 | dev->driver->postclose(dev, file); | |
255 | ||
256 | if (drm_core_check_feature(dev, DRIVER_PRIME)) | |
257 | drm_prime_destroy_file_private(&file->prime); | |
258 | ||
259 | WARN_ON(!list_empty(&file->event_list)); | |
260 | ||
261 | put_pid(file->pid); | |
262 | kfree(file); | |
263 | } | |
264 | ||
e21710a8 EV |
265 | static void drm_close_helper(struct file *filp) |
266 | { | |
267 | struct drm_file *file_priv = filp->private_data; | |
268 | struct drm_device *dev = file_priv->minor->dev; | |
269 | ||
270 | mutex_lock(&dev->filelist_mutex); | |
271 | list_del(&file_priv->lhead); | |
272 | mutex_unlock(&dev->filelist_mutex); | |
273 | ||
274 | drm_file_free(file_priv); | |
275 | } | |
276 | ||
bcb877e4 | 277 | /* |
d985c108 DA |
278 | * Check whether DRI will run on this CPU. |
279 | * | |
280 | * \return non-zero if the DRI will run on this CPU, or zero otherwise. | |
281 | */ | |
282 | static int drm_cpu_valid(void) | |
283 | { | |
d985c108 DA |
284 | #if defined(__sparc__) && !defined(__sparc_v9__) |
285 | return 0; /* No cmpxchg before v9 sparc. */ | |
286 | #endif | |
287 | return 1; | |
288 | } | |
289 | ||
bcb877e4 | 290 | /* |
d985c108 DA |
291 | * Called whenever a process opens /dev/drm. |
292 | * | |
d985c108 | 293 | * \param filp file pointer. |
f4aede2e | 294 | * \param minor acquired minor-object. |
d985c108 DA |
295 | * \return zero on success or a negative number on failure. |
296 | * | |
297 | * Creates and initializes a drm_file structure for the file private data in \p | |
298 | * filp and add it into the double linked list in \p dev. | |
299 | */ | |
1dcc0ceb | 300 | static int drm_open_helper(struct file *filp, struct drm_minor *minor) |
d985c108 | 301 | { |
f4aede2e | 302 | struct drm_device *dev = minor->dev; |
84b1fd10 | 303 | struct drm_file *priv; |
7eeaeb90 | 304 | int ret; |
d985c108 DA |
305 | |
306 | if (filp->f_flags & O_EXCL) | |
307 | return -EBUSY; /* No exclusive opens */ | |
308 | if (!drm_cpu_valid()) | |
309 | return -EINVAL; | |
13bb9cc8 | 310 | if (dev->switch_power_state != DRM_SWITCH_POWER_ON && dev->switch_power_state != DRM_SWITCH_POWER_DYNAMIC_OFF) |
5bcf719b | 311 | return -EINVAL; |
d985c108 | 312 | |
f4aede2e | 313 | DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor->index); |
d985c108 | 314 | |
1572042a DH |
315 | priv = drm_file_alloc(minor); |
316 | if (IS_ERR(priv)) | |
317 | return PTR_ERR(priv); | |
d985c108 | 318 | |
7eeaeb90 NT |
319 | if (drm_is_primary_client(priv)) { |
320 | ret = drm_master_open(priv); | |
321 | if (ret) { | |
322 | drm_file_free(priv); | |
323 | return ret; | |
324 | } | |
325 | } | |
326 | ||
d985c108 | 327 | filp->private_data = priv; |
76ef6b28 | 328 | filp->f_mode |= FMODE_UNSIGNED_OFFSET; |
6c340eac | 329 | priv->filp = filp; |
bd1b331f | 330 | |
1d2ac403 | 331 | mutex_lock(&dev->filelist_mutex); |
bd1b331f | 332 | list_add(&priv->lhead, &dev->filelist); |
1d2ac403 | 333 | mutex_unlock(&dev->filelist_mutex); |
d985c108 DA |
334 | |
335 | #ifdef __alpha__ | |
336 | /* | |
337 | * Default the hose | |
338 | */ | |
339 | if (!dev->hose) { | |
340 | struct pci_dev *pci_dev; | |
341 | pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL); | |
342 | if (pci_dev) { | |
343 | dev->hose = pci_dev->sysdata; | |
344 | pci_dev_put(pci_dev); | |
345 | } | |
346 | if (!dev->hose) { | |
59c1ad3b YW |
347 | struct pci_bus *b = list_entry(pci_root_buses.next, |
348 | struct pci_bus, node); | |
d985c108 DA |
349 | if (b) |
350 | dev->hose = b->sysdata; | |
351 | } | |
352 | } | |
353 | #endif | |
354 | ||
355 | return 0; | |
c9a9c5e0 KH |
356 | } |
357 | ||
094aa54f DV |
358 | /** |
359 | * drm_open - open method for DRM file | |
360 | * @inode: device inode | |
361 | * @filp: file pointer. | |
362 | * | |
363 | * This function must be used by drivers as their &file_operations.open method. | |
364 | * It looks up the correct DRM device and instantiates all the per-file | |
365 | * resources for it. It also calls the &drm_driver.open driver callback. | |
366 | * | |
367 | * RETURNS: | |
368 | * | |
369 | * 0 on success or negative errno value on falure. | |
370 | */ | |
371 | int drm_open(struct inode *inode, struct file *filp) | |
372 | { | |
373 | struct drm_device *dev; | |
374 | struct drm_minor *minor; | |
375 | int retcode; | |
376 | int need_setup = 0; | |
377 | ||
378 | minor = drm_minor_acquire(iminor(inode)); | |
379 | if (IS_ERR(minor)) | |
380 | return PTR_ERR(minor); | |
381 | ||
382 | dev = minor->dev; | |
383 | if (!dev->open_count++) | |
384 | need_setup = 1; | |
385 | ||
386 | /* share address_space across all char-devs of a single device */ | |
387 | filp->f_mapping = dev->anon_inode->i_mapping; | |
388 | ||
389 | retcode = drm_open_helper(filp, minor); | |
390 | if (retcode) | |
391 | goto err_undo; | |
392 | if (need_setup) { | |
393 | retcode = drm_legacy_setup(dev); | |
394 | if (retcode) { | |
395 | drm_close_helper(filp); | |
396 | goto err_undo; | |
397 | } | |
398 | } | |
399 | return 0; | |
400 | ||
401 | err_undo: | |
402 | dev->open_count--; | |
403 | drm_minor_release(minor); | |
404 | return retcode; | |
405 | } | |
406 | EXPORT_SYMBOL(drm_open); | |
407 | ||
68dfbeba | 408 | void drm_lastclose(struct drm_device * dev) |
1c8887dd | 409 | { |
1c8887dd DH |
410 | DRM_DEBUG("\n"); |
411 | ||
412 | if (dev->driver->lastclose) | |
413 | dev->driver->lastclose(dev); | |
414 | DRM_DEBUG("driver lastclose completed\n"); | |
415 | ||
fa538645 | 416 | if (drm_core_check_feature(dev, DRIVER_LEGACY)) |
68dfbeba | 417 | drm_legacy_dev_reinit(dev); |
c76f0f7c NT |
418 | |
419 | drm_client_dev_restore(dev); | |
1c8887dd DH |
420 | } |
421 | ||
1da177e4 | 422 | /** |
bcb877e4 DV |
423 | * drm_release - release method for DRM file |
424 | * @inode: device inode | |
425 | * @filp: file pointer. | |
1da177e4 | 426 | * |
b93658f8 DV |
427 | * This function must be used by drivers as their &file_operations.release |
428 | * method. It frees any resources associated with the open file, and calls the | |
45c3d213 DV |
429 | * &drm_driver.postclose driver callback. If this is the last open file for the |
430 | * DRM device also proceeds to call the &drm_driver.lastclose driver callback. | |
1da177e4 | 431 | * |
bcb877e4 DV |
432 | * RETURNS: |
433 | * | |
434 | * Always succeeds and returns 0. | |
1da177e4 | 435 | */ |
b5e89ed5 | 436 | int drm_release(struct inode *inode, struct file *filp) |
1da177e4 | 437 | { |
6c340eac | 438 | struct drm_file *file_priv = filp->private_data; |
1616c525 DH |
439 | struct drm_minor *minor = file_priv->minor; |
440 | struct drm_device *dev = minor->dev; | |
1da177e4 | 441 | |
58374713 | 442 | mutex_lock(&drm_global_mutex); |
1da177e4 | 443 | |
b5e89ed5 | 444 | DRM_DEBUG("open_count = %d\n", dev->open_count); |
1da177e4 | 445 | |
e21710a8 | 446 | drm_close_helper(filp); |
1da177e4 | 447 | |
1ee57d4d | 448 | if (!--dev->open_count) |
68dfbeba | 449 | drm_lastclose(dev); |
1ee57d4d | 450 | |
58374713 | 451 | mutex_unlock(&drm_global_mutex); |
1da177e4 | 452 | |
1616c525 DH |
453 | drm_minor_release(minor); |
454 | ||
68dfbeba | 455 | return 0; |
1da177e4 LT |
456 | } |
457 | EXPORT_SYMBOL(drm_release); | |
458 | ||
bcb877e4 DV |
459 | /** |
460 | * drm_read - read method for DRM file | |
461 | * @filp: file pointer | |
462 | * @buffer: userspace destination pointer for the read | |
463 | * @count: count in bytes to read | |
464 | * @offset: offset to read | |
465 | * | |
b93658f8 | 466 | * This function must be used by drivers as their &file_operations.read |
bcb877e4 DV |
467 | * method iff they use DRM events for asynchronous signalling to userspace. |
468 | * Since events are used by the KMS API for vblank and page flip completion this | |
469 | * means all modern display drivers must use it. | |
470 | * | |
b93658f8 DV |
471 | * @offset is ignored, DRM events are read like a pipe. Therefore drivers also |
472 | * must set the &file_operation.llseek to no_llseek(). Polling support is | |
bcb877e4 DV |
473 | * provided by drm_poll(). |
474 | * | |
475 | * This function will only ever read a full event. Therefore userspace must | |
476 | * supply a big enough buffer to fit any event to ensure forward progress. Since | |
477 | * the maximum event space is currently 4K it's recommended to just use that for | |
478 | * safety. | |
479 | * | |
480 | * RETURNS: | |
481 | * | |
482 | * Number of bytes read (always aligned to full events, and can be 0) or a | |
483 | * negative error code on failure. | |
484 | */ | |
cdd1cf79 CW |
485 | ssize_t drm_read(struct file *filp, char __user *buffer, |
486 | size_t count, loff_t *offset) | |
c9a9c5e0 | 487 | { |
cdd1cf79 | 488 | struct drm_file *file_priv = filp->private_data; |
c9a9c5e0 | 489 | struct drm_device *dev = file_priv->minor->dev; |
9b2c0b7f | 490 | ssize_t ret; |
c9a9c5e0 | 491 | |
96d4f267 | 492 | if (!access_ok(buffer, count)) |
cdd1cf79 | 493 | return -EFAULT; |
c9a9c5e0 | 494 | |
9b2c0b7f CW |
495 | ret = mutex_lock_interruptible(&file_priv->event_read_lock); |
496 | if (ret) | |
497 | return ret; | |
498 | ||
cdd1cf79 | 499 | for (;;) { |
83eb64c8 CW |
500 | struct drm_pending_event *e = NULL; |
501 | ||
502 | spin_lock_irq(&dev->event_lock); | |
503 | if (!list_empty(&file_priv->event_list)) { | |
504 | e = list_first_entry(&file_priv->event_list, | |
505 | struct drm_pending_event, link); | |
506 | file_priv->event_space += e->event->length; | |
507 | list_del(&e->link); | |
508 | } | |
509 | spin_unlock_irq(&dev->event_lock); | |
510 | ||
511 | if (e == NULL) { | |
cdd1cf79 CW |
512 | if (ret) |
513 | break; | |
c9a9c5e0 | 514 | |
cdd1cf79 CW |
515 | if (filp->f_flags & O_NONBLOCK) { |
516 | ret = -EAGAIN; | |
517 | break; | |
518 | } | |
c9a9c5e0 | 519 | |
9b2c0b7f | 520 | mutex_unlock(&file_priv->event_read_lock); |
cdd1cf79 CW |
521 | ret = wait_event_interruptible(file_priv->event_wait, |
522 | !list_empty(&file_priv->event_list)); | |
9b2c0b7f CW |
523 | if (ret >= 0) |
524 | ret = mutex_lock_interruptible(&file_priv->event_read_lock); | |
525 | if (ret) | |
526 | return ret; | |
cdd1cf79 | 527 | } else { |
83eb64c8 CW |
528 | unsigned length = e->event->length; |
529 | ||
530 | if (length > count - ret) { | |
531 | put_back_event: | |
532 | spin_lock_irq(&dev->event_lock); | |
533 | file_priv->event_space -= length; | |
534 | list_add(&e->link, &file_priv->event_list); | |
535 | spin_unlock_irq(&dev->event_lock); | |
60b80199 | 536 | wake_up_interruptible(&file_priv->event_wait); |
cdd1cf79 | 537 | break; |
83eb64c8 | 538 | } |
cdd1cf79 | 539 | |
83eb64c8 | 540 | if (copy_to_user(buffer + ret, e->event, length)) { |
cdd1cf79 CW |
541 | if (ret == 0) |
542 | ret = -EFAULT; | |
83eb64c8 | 543 | goto put_back_event; |
cdd1cf79 | 544 | } |
c9a9c5e0 | 545 | |
83eb64c8 | 546 | ret += length; |
1b47aaf9 | 547 | kfree(e); |
c9a9c5e0 | 548 | } |
c9a9c5e0 | 549 | } |
9b2c0b7f | 550 | mutex_unlock(&file_priv->event_read_lock); |
c9a9c5e0 | 551 | |
cdd1cf79 | 552 | return ret; |
c9a9c5e0 KH |
553 | } |
554 | EXPORT_SYMBOL(drm_read); | |
555 | ||
bcb877e4 DV |
556 | /** |
557 | * drm_poll - poll method for DRM file | |
558 | * @filp: file pointer | |
559 | * @wait: poll waiter table | |
560 | * | |
b93658f8 DV |
561 | * This function must be used by drivers as their &file_operations.read method |
562 | * iff they use DRM events for asynchronous signalling to userspace. Since | |
563 | * events are used by the KMS API for vblank and page flip completion this means | |
564 | * all modern display drivers must use it. | |
bcb877e4 DV |
565 | * |
566 | * See also drm_read(). | |
567 | * | |
568 | * RETURNS: | |
569 | * | |
570 | * Mask of POLL flags indicating the current status of the file. | |
571 | */ | |
afc9a42b | 572 | __poll_t drm_poll(struct file *filp, struct poll_table_struct *wait) |
1da177e4 | 573 | { |
c9a9c5e0 | 574 | struct drm_file *file_priv = filp->private_data; |
afc9a42b | 575 | __poll_t mask = 0; |
c9a9c5e0 KH |
576 | |
577 | poll_wait(filp, &file_priv->event_wait, wait); | |
578 | ||
579 | if (!list_empty(&file_priv->event_list)) | |
a9a08845 | 580 | mask |= EPOLLIN | EPOLLRDNORM; |
c9a9c5e0 KH |
581 | |
582 | return mask; | |
1da177e4 | 583 | } |
b5e89ed5 | 584 | EXPORT_SYMBOL(drm_poll); |
2dd500f1 DV |
585 | |
586 | /** | |
4020b220 | 587 | * drm_event_reserve_init_locked - init a DRM event and reserve space for it |
2dd500f1 DV |
588 | * @dev: DRM device |
589 | * @file_priv: DRM file private data | |
590 | * @p: tracking structure for the pending event | |
591 | * @e: actual event data to deliver to userspace | |
592 | * | |
593 | * This function prepares the passed in event for eventual delivery. If the event | |
594 | * doesn't get delivered (because the IOCTL fails later on, before queuing up | |
595 | * anything) then the even must be cancelled and freed using | |
fb740cf2 DV |
596 | * drm_event_cancel_free(). Successfully initialized events should be sent out |
597 | * using drm_send_event() or drm_send_event_locked() to signal completion of the | |
598 | * asynchronous event to userspace. | |
2dd500f1 DV |
599 | * |
600 | * If callers embedded @p into a larger structure it must be allocated with | |
601 | * kmalloc and @p must be the first member element. | |
602 | * | |
4020b220 | 603 | * This is the locked version of drm_event_reserve_init() for callers which |
ef40cbf9 | 604 | * already hold &drm_device.event_lock. |
4020b220 | 605 | * |
2dd500f1 DV |
606 | * RETURNS: |
607 | * | |
608 | * 0 on success or a negative error code on failure. | |
609 | */ | |
4020b220 DV |
610 | int drm_event_reserve_init_locked(struct drm_device *dev, |
611 | struct drm_file *file_priv, | |
612 | struct drm_pending_event *p, | |
613 | struct drm_event *e) | |
2dd500f1 | 614 | { |
4020b220 DV |
615 | if (file_priv->event_space < e->length) |
616 | return -ENOMEM; | |
2dd500f1 DV |
617 | |
618 | file_priv->event_space -= e->length; | |
619 | ||
620 | p->event = e; | |
681047b4 | 621 | list_add(&p->pending_link, &file_priv->pending_event_list); |
2dd500f1 DV |
622 | p->file_priv = file_priv; |
623 | ||
4020b220 DV |
624 | return 0; |
625 | } | |
626 | EXPORT_SYMBOL(drm_event_reserve_init_locked); | |
627 | ||
628 | /** | |
629 | * drm_event_reserve_init - init a DRM event and reserve space for it | |
630 | * @dev: DRM device | |
631 | * @file_priv: DRM file private data | |
632 | * @p: tracking structure for the pending event | |
633 | * @e: actual event data to deliver to userspace | |
634 | * | |
635 | * This function prepares the passed in event for eventual delivery. If the event | |
636 | * doesn't get delivered (because the IOCTL fails later on, before queuing up | |
637 | * anything) then the even must be cancelled and freed using | |
638 | * drm_event_cancel_free(). Successfully initialized events should be sent out | |
639 | * using drm_send_event() or drm_send_event_locked() to signal completion of the | |
640 | * asynchronous event to userspace. | |
641 | * | |
642 | * If callers embedded @p into a larger structure it must be allocated with | |
643 | * kmalloc and @p must be the first member element. | |
644 | * | |
ef40cbf9 | 645 | * Callers which already hold &drm_device.event_lock should use |
20c9ca4f | 646 | * drm_event_reserve_init_locked() instead. |
4020b220 DV |
647 | * |
648 | * RETURNS: | |
649 | * | |
650 | * 0 on success or a negative error code on failure. | |
651 | */ | |
652 | int drm_event_reserve_init(struct drm_device *dev, | |
653 | struct drm_file *file_priv, | |
654 | struct drm_pending_event *p, | |
655 | struct drm_event *e) | |
656 | { | |
657 | unsigned long flags; | |
658 | int ret; | |
659 | ||
660 | spin_lock_irqsave(&dev->event_lock, flags); | |
661 | ret = drm_event_reserve_init_locked(dev, file_priv, p, e); | |
2dd500f1 | 662 | spin_unlock_irqrestore(&dev->event_lock, flags); |
4020b220 | 663 | |
2dd500f1 DV |
664 | return ret; |
665 | } | |
666 | EXPORT_SYMBOL(drm_event_reserve_init); | |
667 | ||
668 | /** | |
1e55a53a | 669 | * drm_event_cancel_free - free a DRM event and release its space |
2dd500f1 DV |
670 | * @dev: DRM device |
671 | * @p: tracking structure for the pending event | |
672 | * | |
673 | * This function frees the event @p initialized with drm_event_reserve_init() | |
b93658f8 DV |
674 | * and releases any allocated space. It is used to cancel an event when the |
675 | * nonblocking operation could not be submitted and needed to be aborted. | |
2dd500f1 DV |
676 | */ |
677 | void drm_event_cancel_free(struct drm_device *dev, | |
678 | struct drm_pending_event *p) | |
679 | { | |
680 | unsigned long flags; | |
681 | spin_lock_irqsave(&dev->event_lock, flags); | |
681047b4 DV |
682 | if (p->file_priv) { |
683 | p->file_priv->event_space += p->event->length; | |
684 | list_del(&p->pending_link); | |
685 | } | |
2dd500f1 | 686 | spin_unlock_irqrestore(&dev->event_lock, flags); |
838de39f GP |
687 | |
688 | if (p->fence) | |
f54d1867 | 689 | dma_fence_put(p->fence); |
838de39f | 690 | |
1b47aaf9 | 691 | kfree(p); |
2dd500f1 DV |
692 | } |
693 | EXPORT_SYMBOL(drm_event_cancel_free); | |
fb740cf2 DV |
694 | |
695 | /** | |
696 | * drm_send_event_locked - send DRM event to file descriptor | |
697 | * @dev: DRM device | |
698 | * @e: DRM event to deliver | |
699 | * | |
700 | * This function sends the event @e, initialized with drm_event_reserve_init(), | |
701 | * to its associated userspace DRM file. Callers must already hold | |
ef40cbf9 | 702 | * &drm_device.event_lock, see drm_send_event() for the unlocked version. |
681047b4 DV |
703 | * |
704 | * Note that the core will take care of unlinking and disarming events when the | |
705 | * corresponding DRM file is closed. Drivers need not worry about whether the | |
706 | * DRM file for this event still exists and can call this function upon | |
707 | * completion of the asynchronous work unconditionally. | |
fb740cf2 DV |
708 | */ |
709 | void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e) | |
710 | { | |
711 | assert_spin_locked(&dev->event_lock); | |
712 | ||
3b24f7d6 | 713 | if (e->completion) { |
3b24f7d6 | 714 | complete_all(e->completion); |
24835e44 | 715 | e->completion_release(e->completion); |
3b24f7d6 DV |
716 | e->completion = NULL; |
717 | } | |
718 | ||
1b47aaf9 | 719 | if (e->fence) { |
f54d1867 CW |
720 | dma_fence_signal(e->fence); |
721 | dma_fence_put(e->fence); | |
1b47aaf9 GP |
722 | } |
723 | ||
681047b4 | 724 | if (!e->file_priv) { |
1b47aaf9 | 725 | kfree(e); |
681047b4 DV |
726 | return; |
727 | } | |
728 | ||
729 | list_del(&e->pending_link); | |
fb740cf2 DV |
730 | list_add_tail(&e->link, |
731 | &e->file_priv->event_list); | |
732 | wake_up_interruptible(&e->file_priv->event_wait); | |
733 | } | |
734 | EXPORT_SYMBOL(drm_send_event_locked); | |
735 | ||
736 | /** | |
737 | * drm_send_event - send DRM event to file descriptor | |
738 | * @dev: DRM device | |
739 | * @e: DRM event to deliver | |
740 | * | |
741 | * This function sends the event @e, initialized with drm_event_reserve_init(), | |
ef40cbf9 DV |
742 | * to its associated userspace DRM file. This function acquires |
743 | * &drm_device.event_lock, see drm_send_event_locked() for callers which already | |
744 | * hold this lock. | |
681047b4 DV |
745 | * |
746 | * Note that the core will take care of unlinking and disarming events when the | |
747 | * corresponding DRM file is closed. Drivers need not worry about whether the | |
748 | * DRM file for this event still exists and can call this function upon | |
749 | * completion of the asynchronous work unconditionally. | |
fb740cf2 DV |
750 | */ |
751 | void drm_send_event(struct drm_device *dev, struct drm_pending_event *e) | |
752 | { | |
753 | unsigned long irqflags; | |
754 | ||
755 | spin_lock_irqsave(&dev->event_lock, irqflags); | |
756 | drm_send_event_locked(dev, e); | |
757 | spin_unlock_irqrestore(&dev->event_lock, irqflags); | |
758 | } | |
759 | EXPORT_SYMBOL(drm_send_event); |