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