3 * File operations for DRM
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Daryll Strauss <daryll@valinux.com>
7 * \author Gareth Hughes <gareth@valinux.com>
11 * Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com
13 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
14 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
15 * All Rights Reserved.
17 * Permission is hereby granted, free of charge, to any person obtaining a
18 * copy of this software and associated documentation files (the "Software"),
19 * to deal in the Software without restriction, including without limitation
20 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
21 * and/or sell copies of the Software, and to permit persons to whom the
22 * Software is furnished to do so, subject to the following conditions:
24 * The above copyright notice and this permission notice (including the next
25 * paragraph) shall be included in all copies or substantial portions of the
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
31 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
32 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
33 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
34 * OTHER DEALINGS IN THE SOFTWARE.
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <dev/drm2/drmP.h>
42 static int drm_open_helper(struct cdev *kdev, int flags, int fmt,
43 DRM_STRUCTPROC *p, struct drm_device *dev);
45 static int drm_setup(struct drm_device * dev)
50 if (dev->driver->firstopen) {
51 ret = dev->driver->firstopen(dev);
56 atomic_set(&dev->ioctl_count, 0);
57 atomic_set(&dev->vma_count, 0);
59 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
60 !drm_core_check_feature(dev, DRIVER_MODESET)) {
62 atomic_set(&dev->buf_alloc, 0);
64 i = drm_dma_setup(dev);
70 * FIXME Linux<->FreeBSD: counter incremented in drm_open() and
74 for (i = 0; i < ARRAY_SIZE(dev->counts); i++)
75 atomic_set(&dev->counts[i], 0);
78 dev->sigdata.lock = NULL;
80 dev->context_flag = 0;
81 dev->interrupt_flag = 0;
83 dev->last_context = 0;
85 dev->last_checked = 0;
86 DRM_INIT_WAITQUEUE(&dev->context_wait);
93 dev->buf_async = NULL;
94 DRM_INIT_WAITQUEUE(&dev->buf_readers);
95 DRM_INIT_WAITQUEUE(&dev->buf_writers);
96 #endif /* FREEBSD_NOTYET */
101 * The kernel's context could be created here, but is now created
102 * in drm_dma_enqueue. This is more resource-efficient for
103 * hardware that does not do DMA, but may mean that
104 * drm_select_queue fails between the time the interrupt is
105 * initialized and the time the queues are initialized.
114 * \param inode device inode
115 * \param filp file pointer.
116 * \return zero on success or a negative number on failure.
118 * Searches the DRM device with the same minor number, calls open_helper(), and
119 * increments the device open count. If the open count was previous at zero,
120 * i.e., it's the first that the device is open, then calls setup().
122 int drm_open(struct cdev *kdev, int flags, int fmt, DRM_STRUCTPROC *p)
124 struct drm_device *dev = NULL;
125 struct drm_minor *minor;
129 minor = kdev->si_drv1;
133 if (!(dev = minor->dev))
136 sx_xlock(&drm_global_mutex);
139 * FIXME Linux<->FreeBSD: On Linux, counter updated outside
142 if (!dev->open_count++)
145 retcode = drm_open_helper(kdev, flags, fmt, p, dev);
147 sx_xunlock(&drm_global_mutex);
150 atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
152 retcode = drm_setup(dev);
156 sx_xunlock(&drm_global_mutex);
160 mtx_lock(&Giant); /* FIXME: Giant required? */
161 device_unbusy(dev->dev);
164 sx_xunlock(&drm_global_mutex);
167 EXPORT_SYMBOL(drm_open);
170 * Called whenever a process opens /dev/drm.
172 * \param inode device inode.
173 * \param filp file pointer.
175 * \return zero on success or a negative number on failure.
177 * Creates and initializes a drm_file structure for the file private data in \p
178 * filp and add it into the double linked list in \p dev.
180 static int drm_open_helper(struct cdev *kdev, int flags, int fmt,
181 DRM_STRUCTPROC *p, struct drm_device *dev)
183 struct drm_file *priv;
187 return -EBUSY; /* No exclusive opens */
188 if (dev->switch_power_state != DRM_SWITCH_POWER_ON)
191 DRM_DEBUG("pid = %d, device = %s\n", DRM_CURRENTPID, devtoname(kdev));
193 priv = malloc(sizeof(*priv), DRM_MEM_FILES, M_NOWAIT | M_ZERO);
197 priv->uid = p->td_ucred->cr_svuid;
198 priv->pid = p->td_proc->p_pid;
199 priv->minor = kdev->si_drv1;
200 priv->ioctl_count = 0;
201 /* for compatibility root is always authenticated */
202 priv->authenticated = DRM_SUSER(p);
203 priv->lock_count = 0;
205 INIT_LIST_HEAD(&priv->lhead);
206 INIT_LIST_HEAD(&priv->fbs);
207 INIT_LIST_HEAD(&priv->event_list);
208 priv->event_space = 4096; /* set aside 4k for event buffer */
210 if (dev->driver->driver_features & DRIVER_GEM)
211 drm_gem_open(dev, priv);
213 #ifdef FREEBSD_NOTYET
214 if (drm_core_check_feature(dev, DRIVER_PRIME))
215 drm_prime_init_file_private(&priv->prime);
216 #endif /* FREEBSD_NOTYET */
218 if (dev->driver->open) {
219 ret = dev->driver->open(dev, priv);
225 /* if there is no current master make this fd it */
227 if (!priv->minor->master) {
228 /* create a new master */
229 priv->minor->master = drm_master_create(priv->minor);
230 if (!priv->minor->master) {
237 /* take another reference for the copy in the local file priv */
238 priv->master = drm_master_get(priv->minor->master);
240 priv->authenticated = 1;
243 if (dev->driver->master_create) {
244 ret = dev->driver->master_create(dev, priv->master);
247 /* drop both references if this fails */
248 drm_master_put(&priv->minor->master);
249 drm_master_put(&priv->master);
255 if (dev->driver->master_set) {
256 ret = dev->driver->master_set(dev, priv, true);
258 /* drop both references if this fails */
259 drm_master_put(&priv->minor->master);
260 drm_master_put(&priv->master);
267 /* get a reference to the master */
268 priv->master = drm_master_get(priv->minor->master);
273 list_add(&priv->lhead, &dev->filelist);
276 mtx_lock(&Giant); /* FIXME: Giant required? */
277 device_busy(dev->dev);
280 ret = devfs_set_cdevpriv(priv, drm_release);
286 free(priv, DRM_MEM_FILES);
290 static void drm_master_release(struct drm_device *dev, struct drm_file *file_priv)
293 if (drm_i_have_hw_lock(dev, file_priv)) {
294 DRM_DEBUG("File %p released, freeing lock for context %d\n",
295 file_priv, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
296 drm_lock_free(&file_priv->master->lock,
297 _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
301 static void drm_events_release(struct drm_file *file_priv)
303 struct drm_device *dev = file_priv->minor->dev;
304 struct drm_pending_event *e, *et;
305 struct drm_pending_vblank_event *v, *vt;
308 DRM_SPINLOCK_IRQSAVE(&dev->event_lock, flags);
310 /* Remove pending flips */
311 list_for_each_entry_safe(v, vt, &dev->vblank_event_list, base.link)
312 if (v->base.file_priv == file_priv) {
313 list_del(&v->base.link);
314 drm_vblank_put(dev, v->pipe);
315 v->base.destroy(&v->base);
318 /* Remove unconsumed events */
319 list_for_each_entry_safe(e, et, &file_priv->event_list, link)
322 DRM_SPINUNLOCK_IRQRESTORE(&dev->event_lock, flags);
328 * \param inode device inode
329 * \param file_priv DRM file private.
330 * \return zero on success or a negative number on failure.
332 * If the hardware lock is held then free it, and take it again for the kernel
333 * context since it's necessary to reclaim buffers. Unlink the file private
334 * data from its list and free it. Decreases the open count and if it reaches
335 * zero calls drm_lastclose().
337 void drm_release(void *data)
339 struct drm_file *file_priv = data;
340 struct drm_device *dev = file_priv->minor->dev;
342 sx_xlock(&drm_global_mutex);
344 DRM_DEBUG("open_count = %d\n", dev->open_count);
346 if (dev->driver->preclose)
347 dev->driver->preclose(dev, file_priv);
349 /* ========================================================
350 * Begin inline drm_release
353 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
355 (long)file_priv->minor->device,
358 /* Release any auth tokens that might point to this file_priv,
359 (do that under the drm_global_mutex) */
360 if (file_priv->magic)
361 (void) drm_remove_magic(file_priv->master, file_priv->magic);
363 /* if the master has gone away we can't do anything with the lock */
364 if (file_priv->minor->master)
365 drm_master_release(dev, file_priv);
367 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
368 drm_core_reclaim_buffers(dev, file_priv);
370 drm_events_release(file_priv);
372 seldrain(&file_priv->event_poll);
374 if (dev->driver->driver_features & DRIVER_MODESET)
375 drm_fb_release(file_priv);
377 if (dev->driver->driver_features & DRIVER_GEM)
378 drm_gem_release(dev, file_priv);
380 #ifdef FREEBSD_NOTYET
381 mutex_lock(&dev->ctxlist_mutex);
382 if (!list_empty(&dev->ctxlist)) {
383 struct drm_ctx_list *pos, *n;
385 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
386 if (pos->tag == file_priv &&
387 pos->handle != DRM_KERNEL_CONTEXT) {
388 if (dev->driver->context_dtor)
389 dev->driver->context_dtor(dev,
392 drm_ctxbitmap_free(dev, pos->handle);
394 list_del(&pos->head);
400 mutex_unlock(&dev->ctxlist_mutex);
401 #endif /* FREEBSD_NOTYET */
405 if (file_priv->is_master) {
406 struct drm_master *master = file_priv->master;
407 struct drm_file *temp;
408 list_for_each_entry(temp, &dev->filelist, lhead) {
409 if ((temp->master == file_priv->master) &&
411 temp->authenticated = 0;
415 * Since the master is disappearing, so is the
416 * possibility to lock.
419 if (master->lock.hw_lock) {
420 if (dev->sigdata.lock == master->lock.hw_lock)
421 dev->sigdata.lock = NULL;
422 master->lock.hw_lock = NULL;
423 master->lock.file_priv = NULL;
424 DRM_WAKEUP_INT(&master->lock.lock_queue);
427 if (file_priv->minor->master == file_priv->master) {
428 /* drop the reference held my the minor */
429 if (dev->driver->master_drop)
430 dev->driver->master_drop(dev, file_priv, true);
431 drm_master_put(&file_priv->minor->master);
435 /* drop the reference held my the file priv */
436 drm_master_put(&file_priv->master);
437 file_priv->is_master = 0;
438 list_del(&file_priv->lhead);
441 if (dev->driver->postclose)
442 dev->driver->postclose(dev, file_priv);
444 #ifdef FREEBSD_NOTYET
445 if (drm_core_check_feature(dev, DRIVER_PRIME))
446 drm_prime_destroy_file_private(&file_priv->prime);
447 #endif /* FREEBSD_NOTYET */
449 free(file_priv, DRM_MEM_FILES);
451 /* ========================================================
452 * End inline drm_release
455 atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
457 device_unbusy(dev->dev);
459 if (!--dev->open_count) {
460 if (atomic_read(&dev->ioctl_count)) {
461 DRM_ERROR("Device busy: %d\n",
462 atomic_read(&dev->ioctl_count));
466 sx_xunlock(&drm_global_mutex);
468 EXPORT_SYMBOL(drm_release);
471 drm_dequeue_event(struct drm_file *file_priv, struct uio *uio,
472 struct drm_pending_event **out)
474 struct drm_pending_event *e;
477 /* Already locked in drm_read(). */
478 /* DRM_SPINLOCK_IRQSAVE(&dev->event_lock, flags); */
481 if (list_empty(&file_priv->event_list))
483 e = list_first_entry(&file_priv->event_list,
484 struct drm_pending_event, link);
485 if (e->event->length > uio->uio_resid)
488 file_priv->event_space += e->event->length;
494 /* DRM_SPINUNLOCK_IRQRESTORE(&dev->event_lock, flags); */
499 drm_read(struct cdev *kdev, struct uio *uio, int ioflag)
501 struct drm_file *file_priv;
502 struct drm_device *dev;
503 struct drm_pending_event *e;
506 error = devfs_get_cdevpriv((void **)&file_priv);
508 DRM_ERROR("can't find authenticator\n");
512 dev = drm_get_device_from_kdev(kdev);
513 mtx_lock(&dev->event_lock);
514 while (list_empty(&file_priv->event_list)) {
515 if ((ioflag & O_NONBLOCK) != 0) {
519 error = msleep(&file_priv->event_space, &dev->event_lock,
520 PCATCH, "drmrea", 0);
525 while (drm_dequeue_event(file_priv, uio, &e)) {
526 mtx_unlock(&dev->event_lock);
527 error = uiomove(e->event, e->event->length, uio);
528 CTR3(KTR_DRM, "drm_event_dequeued %d %d %d", curproc->p_pid,
529 e->event->type, e->event->length);
534 mtx_lock(&dev->event_lock);
538 mtx_unlock(&dev->event_lock);
541 EXPORT_SYMBOL(drm_read);
544 drm_event_wakeup(struct drm_pending_event *e)
546 struct drm_file *file_priv;
547 struct drm_device *dev;
549 file_priv = e->file_priv;
550 dev = file_priv->minor->dev;
551 mtx_assert(&dev->event_lock, MA_OWNED);
553 wakeup(&file_priv->event_space);
554 selwakeup(&file_priv->event_poll);
558 drm_poll(struct cdev *kdev, int events, struct thread *td)
560 struct drm_file *file_priv;
561 struct drm_device *dev;
564 error = devfs_get_cdevpriv((void **)&file_priv);
566 DRM_ERROR("can't find authenticator\n");
570 dev = drm_get_device_from_kdev(kdev);
573 mtx_lock(&dev->event_lock);
574 if ((events & (POLLIN | POLLRDNORM)) != 0) {
575 if (list_empty(&file_priv->event_list)) {
576 CTR0(KTR_DRM, "drm_poll empty list");
577 selrecord(td, &file_priv->event_poll);
579 revents |= events & (POLLIN | POLLRDNORM);
580 CTR1(KTR_DRM, "drm_poll revents %x", revents);
583 mtx_unlock(&dev->event_lock);
586 EXPORT_SYMBOL(drm_poll);
589 drm_mmap_single(struct cdev *kdev, vm_ooffset_t *offset, vm_size_t size,
590 struct vm_object **obj_res, int nprot)
592 struct drm_device *dev;
594 dev = drm_get_device_from_kdev(kdev);
595 if (dev->drm_ttm_bdev != NULL) {
596 return (-ttm_bo_mmap_single(dev->drm_ttm_bdev, offset, size,
598 } else if ((dev->driver->driver_features & DRIVER_GEM) != 0) {
599 return (-drm_gem_mmap_single(dev, offset, size, obj_res, nprot));