1450N/A/*
1450N/A * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
1450N/A */
1450N/A
1450N/A/**
1450N/A * \file drm_fops.c
1450N/A * File operations for DRM
1450N/A *
1450N/A * \author Rickard E. (Rik) Faith <faith@valinux.com>
1450N/A * \author Daryll Strauss <daryll@valinux.com>
1450N/A * \author Gareth Hughes <gareth@valinux.com>
1450N/A */
1450N/A
1450N/A/*
1450N/A * Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com
1450N/A *
1450N/A * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
1450N/A * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
1450N/A * Copyright (c) 2009, 2013, Intel Corporation.
1450N/A * All Rights Reserved.
1450N/A *
1450N/A * Permission is hereby granted, free of charge, to any person obtaining a
1450N/A * copy of this software and associated documentation files (the "Software"),
1450N/A * to deal in the Software without restriction, including without limitation
1450N/A * the rights to use, copy, modify, merge, publish, distribute, sublicense,
1450N/A * and/or sell copies of the Software, and to permit persons to whom the
1450N/A * Software is furnished to do so, subject to the following conditions:
1450N/A *
1450N/A * The above copyright notice and this permission notice (including the next
1450N/A * paragraph) shall be included in all copies or substantial portions of the
1450N/A * Software.
1450N/A *
1450N/A * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1450N/A * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1450N/A * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1450N/A * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
1450N/A * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1450N/A * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
1450N/A * OTHER DEALINGS IN THE SOFTWARE.
1450N/A */
1450N/A
1450N/A#include "drmP.h"
1450N/A
1450N/Astatic int drm_open_helper(struct drm_minor *minor,
1450N/A int clone_id, int flags, cred_t *credp);
1450N/A
1450N/Astatic __inline__ int drm_device_is_agp(struct drm_device *dev)
1450N/A{
1450N/A if (drm_core_check_feature(dev, DRIVER_USE_PLATFORM_DEVICE))
1450N/A return 0;
1450N/A if (dev->driver->device_is_agp != NULL) {
1450N/A int err = (*dev->driver->device_is_agp) (dev);
1450N/A
1450N/A if (err != 2) {
1450N/A return err;
1450N/A }
1450N/A }
1450N/A
1450N/A return pci_find_capability(dev->pdev, PCI_CAP_ID_AGP);
1450N/A}
1450N/A
1450N/Astatic int drm_setup(struct drm_device * dev)
1450N/A{
1450N/A int i;
1450N/A int ret;
1450N/A static bool first_call = true;
1450N/A
1450N/A if (first_call) {
1450N/A /* OSOL_drm: moved from drm_fill_in_dev */
1450N/A if (drm_core_has_AGP(dev)) {
1450N/A if (drm_device_is_agp(dev))
1450N/A dev->agp = drm_agp_init(dev);
1450N/A if (drm_core_check_feature(dev, DRIVER_REQUIRE_AGP)
1450N/A && (dev->agp == NULL)) {
1450N/A DRM_ERROR("Cannot initialize the agpgart module.\n");
1450N/A return -EINVAL;
1450N/A }
1450N/A }
1450N/A }
1450N/A
1450N/A if (dev->driver->firstopen) {
1450N/A ret = dev->driver->firstopen(dev);
1450N/A if (ret != 0)
1450N/A return ret;
1450N/A }
1450N/A
1450N/A if (first_call) {
1450N/A /* OSOL_drm: moved from drm_get_dev */
1450N/A /* setup the grouping for the legacy output */
1450N/A if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1450N/A ret = drm_mode_group_init_legacy_group(dev, &dev->primary->mode_group);
1450N/A if (ret)
1450N/A return ret;
1450N/A }
1450N/A }
1450N/A
1450N/A atomic_set(&dev->ioctl_count, 0);
1450N/A
1450N/A if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
1450N/A !drm_core_check_feature(dev, DRIVER_MODESET)) {
1450N/A dev->buf_use = 0;
1450N/A atomic_set(&dev->buf_alloc, 0);
1450N/A
1450N/A i = drm_dma_setup(dev);
1450N/A if (i < 0)
1450N/A return i;
1450N/A }
1450N/A
1450N/A for (i = 0; i < DRM_ARRAY_SIZE(dev->counts); i++)
1450N/A atomic_set(&dev->counts[i], 0);
1450N/A
1450N/A dev->context_flag = 0;
1450N/A dev->last_context = 0;
1450N/A dev->if_version = 0;
1450N/A
1450N/A
1450N/A DRM_DEBUG("\n");
1450N/A
1450N/A /*
1450N/A * The kernel's context could be created here, but is now created
1450N/A * in drm_dma_enqueue. This is more resource-efficient for
1450N/A * hardware that does not do DMA, but may mean that
1450N/A * drm_select_queue fails between the time the interrupt is
1450N/A * initialized and the time the queues are initialized.
1450N/A */
1450N/A
1450N/A first_call = false;
1450N/A return 0;
1450N/A}
1450N/A
1450N/A/**
1450N/A * Open file.
1450N/A *
1450N/A * \return zero on success or a negative number on failure.
1450N/A *
1450N/A * Searches the DRM device with the same minor number, calls open_helper(), and
1450N/A * increments the device open count. If the open count was previous at zero,
1450N/A * i.e., it's the first that the device is open, then calls setup().
1450N/A */
1450N/Aint drm_open(struct drm_minor *minor, int clone_id, int flags, cred_t *credp)
1450N/A{
1450N/A struct drm_device *dev = minor->dev;
1450N/A int retcode = 0;
1450N/A
1450N/A DRM_DEBUG("minor->index=%d, clone_id=%d", minor->index, clone_id);
1450N/A
1450N/A retcode = drm_open_helper(minor, clone_id, flags, credp);
1450N/A if (!retcode) {
1450N/A atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
1450N/A spin_lock(&dev->count_lock);
1450N/A if (!dev->open_count++) {
1450N/A spin_unlock(&dev->count_lock);
1450N/A retcode = drm_setup(dev);
1450N/A goto out;
1450N/A }
1450N/A spin_unlock(&dev->count_lock);
1450N/A }
1450N/Aout:
1450N/A return retcode;
1450N/A}
1450N/A
1450N/A/**
1450N/A * Called whenever a process opens /dev/drm.
1450N/A *
1450N/A * Creates and initializes a drm_file structure for the file private data in \p
1450N/A * filp and add it into the double linked list in \p dev.
1450N/A */
1450N/Astatic int drm_open_helper(struct drm_minor *minor,
1450N/A int clone_id, int flags, cred_t *credp)
1450N/A{
1450N/A struct drm_device *dev = minor->dev;
1450N/A struct drm_file *priv;
1450N/A int minor_id = minor->index;
1450N/A int ret;
1450N/A
1450N/A if (flags & FEXCL)
1450N/A return -EBUSY; /* No exclusive opens */
1450N/A
1450N/A if (dev->switch_power_state != DRM_SWITCH_POWER_ON)
1450N/A return -EINVAL;
1450N/A
1450N/A DRM_DEBUG("pid = %d, minor = %d\n", ddi_get_pid(), minor_id);
1450N/A
1450N/A priv = kmalloc(sizeof(*priv), GFP_KERNEL);
1450N/A if (!priv)
1450N/A return -ENOMEM;
1450N/A
1450N/A (void) memset(priv, 0, sizeof(*priv));
1450N/A (void) idr_replace(&minor->clone_idr, priv, clone_id); /* OSOL_drm */
1450N/A priv->uid = crgetsuid(credp);
1450N/A priv->pid = ddi_get_pid();
1450N/A priv->minor = minor;
1450N/A priv->ioctl_count = 0;
1450N/A /* for compatibility root is always authenticated */
1450N/A priv->authenticated = DRM_SUSER(credp);
1450N/A priv->lock_count = 0;
1450N/A
1450N/A INIT_LIST_HEAD(&priv->lhead);
1450N/A INIT_LIST_HEAD(&priv->fbs);
1450N/A mutex_init(&priv->fbs_lock, NULL, MUTEX_DRIVER, NULL);
1450N/A INIT_LIST_HEAD(&priv->event_list);
1450N/A DRM_INIT_WAITQUEUE(&priv->event_wait, DRM_INTR_PRI(dev));
1450N/A priv->event_space = 4096; /* set aside 4k for event buffer */
1450N/A
1450N/A if (dev->driver->driver_features & DRIVER_GEM)
1450N/A drm_gem_open(dev, priv);
1450N/A
1450N/A if (dev->driver->open) {
1450N/A ret = dev->driver->open(dev, priv);
1450N/A if (ret < 0)
1450N/A goto out_free;
1450N/A }
1450N/A
1450N/A
1450N/A /* if there is no current master make this fd it */
1450N/A mutex_lock(&dev->struct_mutex);
1450N/A if (!priv->minor->master) {
1450N/A /* create a new master */
1450N/A priv->minor->master = drm_master_create(priv->minor);
1450N/A if (!priv->minor->master) {
1450N/A mutex_unlock(&dev->struct_mutex);
1450N/A ret = -ENOMEM;
1450N/A goto out_free;
1450N/A }
1450N/A
1450N/A priv->is_master = 1;
1450N/A /* take another reference for the copy in the local file priv */
1450N/A priv->master = drm_master_get(priv->minor->master);
1450N/A
1450N/A priv->authenticated = 1;
1450N/A
1450N/A mutex_unlock(&dev->struct_mutex);
1450N/A if (dev->driver->master_create) {
1450N/A ret = dev->driver->master_create(dev, priv->master);
1450N/A if (ret) {
1450N/A mutex_lock(&dev->struct_mutex);
1450N/A /* drop both references if this fails */
1450N/A drm_master_put(&priv->minor->master);
1450N/A drm_master_put(&priv->master);
1450N/A mutex_unlock(&dev->struct_mutex);
1450N/A goto out_free;
1450N/A }
1450N/A }
1450N/A mutex_lock(&dev->struct_mutex);
1450N/A if (dev->driver->master_set) {
1450N/A ret = dev->driver->master_set(dev, priv, true);
1450N/A if (ret) {
1450N/A /* drop both references if this fails */
1450N/A drm_master_put(&priv->minor->master);
1450N/A drm_master_put(&priv->master);
1450N/A mutex_unlock(&dev->struct_mutex);
1450N/A goto out_free;
1450N/A }
1450N/A }
1450N/A mutex_unlock(&dev->struct_mutex);
1450N/A } else {
1450N/A /* get a reference to the master */
1450N/A priv->master = drm_master_get(priv->minor->master);
1450N/A mutex_unlock(&dev->struct_mutex);
1450N/A }
1450N/A
1450N/A mutex_lock(&dev->struct_mutex);
1450N/A list_add(&priv->lhead, &dev->filelist, (caddr_t)priv);
1450N/A mutex_unlock(&dev->struct_mutex);
1450N/A
1450N/A return 0;
1450N/Aout_free:
1450N/A kfree(priv, sizeof (*priv));
1450N/A return ret;
1450N/A}
1450N/A
1450N/Avoid drm_master_release(struct drm_device *dev, struct drm_file *fpriv)
1450N/A{
1450N/A struct drm_master *master = fpriv->master;
1450N/A
1450N/A if (drm_i_have_hw_lock(dev, fpriv)) {
1450N/A DRM_DEBUG("Process %d dead, freeing lock for context %d",
1450N/A DRM_CURRENTPID, _DRM_LOCKING_CONTEXT(master->lock.hw_lock->lock));
1450N/A (void) drm_lock_free(&master->lock,
1450N/A _DRM_LOCKING_CONTEXT(master->lock.hw_lock->lock));
1450N/A }
1450N/A
1450N/A}
1450N/A
1450N/Astatic void drm_events_release(struct drm_file *file_priv)
1450N/A{
1450N/A struct drm_device *dev = file_priv->minor->dev;
1450N/A struct drm_pending_event *e, *et;
1450N/A struct drm_pending_vblank_event *v, *vt;
1450N/A unsigned long flags;
1450N/A
1450N/A spin_lock_irqsave(&dev->event_lock, flags);
1450N/A
1450N/A /* Remove pending flips */
1450N/A list_for_each_entry_safe(v, vt, struct drm_pending_vblank_event, &dev->vblank_event_list, base.link)
1450N/A if (v->base.file_priv == file_priv) {
1450N/A list_del(&v->base.link);
1450N/A drm_vblank_put(dev, v->pipe);
1450N/A v->base.destroy(&v->base, sizeof(struct drm_pending_vblank_event));
1450N/A }
1450N/A
1450N/A /* Remove unconsumed events */
1450N/A list_for_each_entry_safe(e, et, struct drm_pending_event, &file_priv->event_list, link)
1450N/A e->destroy(e, sizeof(struct drm_pending_vblank_event));
1450N/A
1450N/A spin_unlock_irqrestore(&dev->event_lock, flags);
1450N/A}
1450N/A
1450N/A/**
1450N/A * Release file.
1450N/A *
1450N/A * \return zero on success or a negative number on failure.
1450N/A *
1450N/A * If the hardware lock is held then free it, and take it again for the kernel
1450N/A * context since it's necessary to reclaim buffers. Unlink the file private
1450N/A * data from its list and free it. Decreases the open count and if it reaches
1450N/A * zero calls drm_lastclose().
1450N/A */
1450N/Aint
1450N/Adrm_release(struct drm_file *file_priv)
1450N/A{
1450N/A struct drm_device *dev = file_priv->minor->dev;
1450N/A int retcode = 0;
1450N/A
1450N/A
1450N/A
1450N/A DRM_DEBUG("open_count = %d\n", dev->open_count);
1450N/A
1450N/A if (dev->driver->preclose)
1450N/A dev->driver->preclose(dev, file_priv);
1450N/A
1450N/A /* ========================================================
1450N/A * Begin inline drm_release
1450N/A */
1450N/A
1450N/A /* Release any auth tokens that might point to this file_priv,
1450N/A (do that under the drm_global_mutex) */
1450N/A if (file_priv->magic)
1450N/A (void) drm_remove_magic(file_priv->master, file_priv->magic);
1450N/A
1450N/A /* if the master has gone away we can't do anything with the lock */
1450N/A if (file_priv->minor->master)
1450N/A drm_master_release(dev, file_priv);
1450N/A
1450N/A if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1450N/A drm_core_reclaim_buffers(dev, file_priv);
1450N/A
1450N/A drm_events_release(file_priv);
1450N/A
1450N/A if (dev->driver->driver_features & DRIVER_MODESET)
1450N/A drm_fb_release(file_priv);
1450N/A
1450N/A if (dev->driver->driver_features & DRIVER_GEM)
1450N/A drm_gem_release(dev, file_priv);
1450N/A
1450N/A mutex_lock(&dev->ctxlist_mutex);
1450N/A if (!list_empty(&dev->ctxlist)) {
1450N/A struct drm_ctx_list *pos, *n;
1450N/A
1450N/A list_for_each_entry_safe(pos, n, struct drm_ctx_list, &dev->ctxlist, head) {
1450N/A if (pos->tag == file_priv &&
1450N/A pos->handle != DRM_KERNEL_CONTEXT) {
1450N/A if (dev->driver->context_dtor)
1450N/A dev->driver->context_dtor(dev,
1450N/A pos->handle);
1450N/A
1450N/A drm_ctxbitmap_free(dev, pos->handle);
1450N/A
1450N/A list_del(&pos->head);
1450N/A kfree(pos, sizeof (*pos));
1450N/A --dev->ctx_count;
1450N/A }
1450N/A }
1450N/A }
1450N/A mutex_unlock(&dev->ctxlist_mutex);
1450N/A
1450N/A mutex_lock(&dev->struct_mutex);
1450N/A
1450N/A if (file_priv->is_master) {
1450N/A struct drm_master *master = file_priv->master;
1450N/A struct drm_file *temp;
1450N/A list_for_each_entry(temp, struct drm_file, &dev->filelist, lhead) {
1450N/A if ((temp->master == file_priv->master) &&
1450N/A (temp != file_priv))
1450N/A temp->authenticated = 0;
1450N/A }
1450N/A
1450N/A /**
1450N/A * Since the master is disappearing, so is the
1450N/A * possibility to lock.
1450N/A */
1450N/A
1450N/A if (master->lock.hw_lock) {
1450N/A master->lock.hw_lock = NULL;
1450N/A master->lock.file_priv = NULL;
1450N/A }
1450N/A
1450N/A if (file_priv->minor->master == file_priv->master) {
1450N/A /* drop the reference held my the minor */
1450N/A if (dev->driver->master_drop)
1450N/A dev->driver->master_drop(dev, file_priv, true);
1450N/A drm_master_put(&file_priv->minor->master);
1450N/A }
1450N/A }
1450N/A
1450N/A /* drop the reference held my the file priv */
1450N/A drm_master_put(&file_priv->master);
1450N/A file_priv->is_master = 0;
1450N/A list_del(&file_priv->lhead);
1450N/A mutex_unlock(&dev->struct_mutex);
1450N/A
1450N/A if (dev->driver->postclose)
1450N/A dev->driver->postclose(dev, file_priv);
1450N/A kfree(file_priv, sizeof (*file_priv));
1450N/A
1450N/A /* ========================================================
1450N/A * End inline drm_release
1450N/A */
1450N/A
1450N/A atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
1450N/A spin_lock(&dev->count_lock);
1450N/A if (!--dev->open_count) {
1450N/A if (atomic_read(&dev->ioctl_count)) {
1450N/A DRM_ERROR("Device busy: %d\n",
1450N/A atomic_read(&dev->ioctl_count));
1450N/A spin_unlock(&dev->count_lock);
1450N/A return -EBUSY;
1450N/A }
1450N/A spin_unlock(&dev->count_lock);
1450N/A return drm_lastclose(dev);
1450N/A }
1450N/A spin_unlock(&dev->count_lock);
1450N/A
1450N/A return retcode;
1450N/A}
1450N/A
1450N/Astatic bool
1450N/Adrm_dequeue_event(struct drm_file *file_priv,
1450N/A size_t total, size_t max, struct drm_pending_event **out)
1450N/A{
1450N/A struct drm_device *dev = file_priv->minor->dev;
1450N/A struct drm_pending_event *e;
1450N/A unsigned long flags;
1450N/A bool ret = false;
1450N/A
1450N/A spin_lock_irqsave(&dev->event_lock, flags);
1450N/A
1450N/A *out = NULL;
1450N/A if (list_empty(&file_priv->event_list))
1450N/A goto out;
1450N/A e = list_first_entry(&file_priv->event_list,
1450N/A struct drm_pending_event, link);
1450N/A if (e->event->length + total > max)
1450N/A goto out;
1450N/A
1450N/A file_priv->event_space += e->event->length;
1450N/A list_del(&e->link);
1450N/A *out = e;
1450N/A ret = true;
1450N/A
1450N/Aout:
1450N/A spin_unlock_irqrestore(&dev->event_lock, flags);
1450N/A return ret;
1450N/A}
1450N/A
1450N/Assize_t drm_read(struct drm_file *file_priv, struct uio *uiop)
1450N/A{
1450N/A struct drm_pending_event *e;
1450N/A size_t total;
1450N/A int ret = 0;
1450N/A
1450N/A DRM_WAIT(ret, &file_priv->event_wait,
1450N/A !list_empty(&file_priv->event_list));
1450N/A if (ret < 0) {
1450N/A DRM_ERROR("returns %d event_list is %d", ret, list_empty(&file_priv->event_list));
1450N/A return ret;
1450N/A }
1450N/A
1450N/A total = 0;
1450N/A while (drm_dequeue_event(file_priv, total, uiop->uio_iov->iov_len, &e)) {
1450N/A ret = uiomove((caddr_t)e->event, e->event->length, UIO_READ, uiop);
1450N/A if (ret) {
1450N/A DRM_ERROR("Failed to copy to user: %d", ret);
1450N/A return (0);
1450N/A }
1450N/A total += e->event->length;
1450N/A e->destroy(e, sizeof(struct drm_pending_vblank_event));
1450N/A }
1450N/A
1450N/A return total;
1450N/A}
1450N/A
1450N/Ashort drm_poll(struct drm_file *file_priv, short events)
1450N/A{
1450N/A short revent = 0;
1450N/A
1450N/A if (!list_empty(&file_priv->event_list)) {
1450N/A if (events & POLLIN)
1450N/A revent |= POLLIN;
1450N/A if (events & POLLRDNORM)
1450N/A revent |= POLLRDNORM;
1450N/A }
1450N/A
1450N/A return revent;
1450N/A}
1450N/A
1450N/A