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