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