1494N/A/*
1683N/A * Copyright (c) 2006, 2016, Oracle and/or its affiliates. All rights reserved.
1494N/A */
1494N/A
1494N/A/*
1494N/A * drm_drv.h -- Generic driver template -*- linux-c -*-
1494N/A * Created: Thu Nov 23 03:10:50 2000 by gareth@valinux.com
1494N/A */
1494N/A/*
1494N/A * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
1494N/A * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
1494N/A * Copyright (c) 2009, 2013, Intel Corporation.
1494N/A * All Rights Reserved.
1494N/A *
1494N/A * Permission is hereby granted, free of charge, to any person obtaining a
1494N/A * copy of this software and associated documentation files (the "Software"),
1494N/A * to deal in the Software without restriction, including without limitation
1494N/A * the rights to use, copy, modify, merge, publish, distribute, sublicense,
1494N/A * and/or sell copies of the Software, and to permit persons to whom the
1494N/A * Software is furnished to do so, subject to the following conditions:
1494N/A *
1494N/A * The above copyright notice and this permission notice (including the next
1494N/A * paragraph) shall be included in all copies or substantial portions of the
1494N/A * Software.
1494N/A *
1494N/A * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1494N/A * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1494N/A * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1494N/A * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
1494N/A * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1494N/A * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
1494N/A * OTHER DEALINGS IN THE SOFTWARE.
1494N/A *
1494N/A * Authors:
1494N/A * Rickard E. (Rik) Faith <faith@valinux.com>
1494N/A * Gareth Hughes <gareth@valinux.com>
1494N/A *
1494N/A */
1494N/A
1494N/A#include "drm.h"
1494N/A#include "drmP.h"
1494N/A#include "drm_core.h"
1494N/A#include "drm_io32.h"
1494N/A#include "drm_sarea.h"
1494N/A#include "drm_linux_list.h"
1494N/A
1494N/Astatic int drm_version(DRM_IOCTL_ARGS);
1494N/A
1494N/A/** Ioctl table */
1494N/Astatic struct drm_ioctl_desc drm_ioctls[] = {
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_VERSION, drm_version, DRM_UNLOCKED, copyin32_drm_version, copyout32_drm_version),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_GET_UNIQUE, drm_getunique, 0, copyin32_drm_unique, copyout32_drm_unique),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_GET_MAGIC, drm_getmagic, 0, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_IRQ_BUSID, drm_irq_by_busid, DRM_MASTER|DRM_ROOT_ONLY, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_GET_MAP, drm_getmap, DRM_UNLOCKED, copyin32_drm_map, copyout32_drm_map),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_GET_CLIENT, drm_getclient, DRM_UNLOCKED, copyin32_drm_client, copyout32_drm_client),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_GET_STATS, drm_getstats, DRM_UNLOCKED, NULL, copyout32_drm_stats),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_GET_CAP, drm_getcap, DRM_UNLOCKED, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_SET_VERSION, drm_setversion, DRM_MASTER, NULL, NULL),
1494N/A
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_SET_UNIQUE, drm_setunique, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_BLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_UNBLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_AUTH_MAGIC, drm_authmagic, DRM_AUTH|DRM_MASTER, NULL, NULL),
1494N/A
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_ADD_MAP, drm_addmap_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY, copyin32_drm_map, copyout32_drm_map),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_RM_MAP, drm_rmmap_ioctl, DRM_AUTH, copyin32_drm_map, copyout32_drm_map),
1494N/A
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_SET_SAREA_CTX, drm_setsareactx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY, copyin32_drm_ctx_priv_map, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_GET_SAREA_CTX, drm_getsareactx, DRM_AUTH, copyin32_drm_ctx_priv_map, copyout32_drm_ctx_priv_map),
1494N/A
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_SET_MASTER, drm_setmaster_ioctl, DRM_ROOT_ONLY, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_DROP_MASTER, drm_dropmaster_ioctl, DRM_ROOT_ONLY, NULL, NULL),
1494N/A
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_ADD_CTX, drm_addctx, DRM_AUTH|DRM_ROOT_ONLY, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_RM_CTX, drm_rmctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_MOD_CTX, drm_modctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_GET_CTX, drm_getctx, DRM_AUTH, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_SWITCH_CTX, drm_switchctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_NEW_CTX, drm_newctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_RES_CTX, drm_resctx, DRM_AUTH, copyin32_drm_ctx_res, copyout32_drm_ctx_res),
1494N/A
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_ADD_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_RM_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY, NULL, NULL),
1494N/A
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_LOCK, drm_lock, DRM_AUTH, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_UNLOCK, drm_unlock, DRM_AUTH, NULL, NULL),
1494N/A
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_FINISH, drm_noop, DRM_AUTH, NULL, NULL),
1494N/A
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_ADD_BUFS, drm_addbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY, copyin32_drm_buf_desc, copyout32_drm_buf_desc),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_MARK_BUFS, drm_markbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_INFO_BUFS, drm_infobufs, DRM_AUTH, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_MAP_BUFS, drm_mapbufs, DRM_AUTH, copyin32_drm_buf_map, copyout32_drm_buf_map),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_FREE_BUFS, drm_freebufs, DRM_AUTH, copyin32_drm_buf_free, NULL),
1494N/A /* The DRM_IOCTL_DMA ioctl should be defined by the driver. */
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_DMA, NULL, DRM_AUTH, NULL, NULL),
1494N/A
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_CONTROL, drm_control, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY, NULL, NULL),
1494N/A
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_AGP_ACQUIRE, drm_agp_acquire_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_AGP_RELEASE, drm_agp_release_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_AGP_ENABLE, drm_agp_enable_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_AGP_INFO, drm_agp_info_ioctl, DRM_AUTH, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_AGP_ALLOC, drm_agp_alloc_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_AGP_FREE, drm_agp_free_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_AGP_BIND, drm_agp_bind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_AGP_UNBIND, drm_agp_unbind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY, NULL, NULL),
1494N/A
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_SG_ALLOC, drm_sg_alloc_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY, copyin32_drm_scatter_gather, copyout32_drm_scatter_gather),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_SG_FREE, drm_sg_free, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY, copyin32_drm_scatter_gather, NULL),
1494N/A
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_WAIT_VBLANK, drm_wait_vblank, DRM_UNLOCKED, copyin32_drm_wait_vblank, copyout32_drm_wait_vblank),
1494N/A
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_MODESET_CTL, drm_modeset_ctl, 0, NULL, NULL),
1494N/A
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_UPDATE_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY, NULL, NULL),
1494N/A
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_GEM_CLOSE, drm_gem_close_ioctl, DRM_UNLOCKED, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_GEM_FLINK, drm_gem_flink_ioctl, DRM_AUTH|DRM_UNLOCKED, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_GEM_OPEN, drm_gem_open_ioctl, DRM_AUTH|DRM_UNLOCKED, NULL, NULL),
1494N/A
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETRESOURCES, drm_mode_getresources, DRM_CONTROL_ALLOW|DRM_UNLOCKED, NULL, NULL),
1494N/A
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANERESOURCES, drm_mode_getplane_res, DRM_CONTROL_ALLOW|DRM_UNLOCKED, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCRTC, drm_mode_getcrtc, DRM_CONTROL_ALLOW|DRM_UNLOCKED, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETCRTC, drm_mode_setcrtc, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANE, drm_mode_getplane, DRM_CONTROL_ALLOW|DRM_UNLOCKED, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETPLANE, drm_mode_setplane, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_MODE_CURSOR, drm_mode_cursor_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETGAMMA, drm_mode_gamma_get_ioctl, DRM_UNLOCKED, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETGAMMA, drm_mode_gamma_set_ioctl, DRM_MASTER|DRM_UNLOCKED, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETENCODER, drm_mode_getencoder, DRM_CONTROL_ALLOW|DRM_UNLOCKED, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCONNECTOR, drm_mode_getconnector, DRM_CONTROL_ALLOW|DRM_UNLOCKED, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_MODE_ATTACHMODE, drm_noop, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_MODE_DETACHMODE, drm_noop, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPROPERTY, drm_mode_getproperty_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETPROPERTY, drm_mode_connector_property_set_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPROPBLOB, drm_mode_getblob_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETFB, drm_mode_getfb, DRM_CONTROL_ALLOW|DRM_UNLOCKED, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_MODE_ADDFB, drm_mode_addfb, DRM_CONTROL_ALLOW|DRM_UNLOCKED, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_MODE_ADDFB2, drm_mode_addfb2, DRM_CONTROL_ALLOW|DRM_UNLOCKED, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_MODE_RMFB, drm_mode_rmfb, DRM_CONTROL_ALLOW|DRM_UNLOCKED, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_MODE_PAGE_FLIP, drm_mode_page_flip_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_MODE_DIRTYFB, drm_mode_dirtyfb_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_MODE_CREATE_DUMB, drm_mode_create_dumb_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_MODE_MAP_DUMB, drm_mode_mmap_dumb_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_MODE_DESTROY_DUMB, drm_mode_destroy_dumb_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_MODE_OBJ_GETPROPERTIES, drm_mode_obj_get_properties_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_MODE_OBJ_SETPROPERTY, drm_mode_obj_set_property_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED, NULL, NULL),
1494N/A DRM_IOCTL_DEF(DRM_IOCTL_MODE_CURSOR2, drm_mode_cursor2_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED, NULL, NULL)
1494N/A};
1494N/A
1494N/A#define DRM_CORE_IOCTL_COUNT ARRAY_SIZE( drm_ioctls )
1494N/A
1494N/A/**
1494N/A * Take down the DRM device.
1494N/A *
1494N/A * \param dev DRM device structure.
1494N/A *
1494N/A * Frees every resource in \p dev.
1494N/A *
1494N/A * \sa drm_device
1494N/A */
1494N/Aint drm_lastclose(struct drm_device * dev)
1494N/A{
1494N/A DRM_DEBUG("\n");
1494N/A
1494N/A if (dev->driver->lastclose)
1494N/A dev->driver->lastclose(dev);
1494N/A DRM_DEBUG("driver lastclose completed\n");
1494N/A
1494N/A if (dev->irq_enabled && !drm_core_check_feature(dev, DRIVER_MODESET))
1494N/A (void) drm_irq_uninstall(dev);
1494N/A
1494N/A mutex_lock(&dev->struct_mutex);
1494N/A
1494N/A /* Clear AGP information */
1494N/A if (drm_core_has_AGP(dev) && dev->agp &&
1494N/A !drm_core_check_feature(dev, DRIVER_MODESET)) {
1494N/A struct drm_agp_mem *entry, *tempe;
1494N/A
1494N/A /* Remove AGP resources, but leave dev->agp
1494N/A intact until drv_cleanup is called. */
1494N/A list_for_each_entry_safe(entry, tempe, struct drm_agp_mem, &dev->agp->memory, head) {
1494N/A if (entry->bound)
1494N/A (void) drm_agp_unbind_memory(entry->handle, dev);
1494N/A kfree(entry, sizeof (*entry));
1494N/A }
1494N/A INIT_LIST_HEAD(&dev->agp->memory);
1494N/A
1494N/A if (dev->agp->acquired)
1494N/A (void) drm_agp_release(dev);
1494N/A
1494N/A dev->agp->acquired = 0;
1494N/A dev->agp->enabled = 0;
1494N/A }
1494N/A if (drm_core_check_feature(dev, DRIVER_SG) && dev->sg &&
1494N/A !drm_core_check_feature(dev, DRIVER_MODESET)) {
1494N/A drm_sg_cleanup(dev->sg);
1494N/A dev->sg = NULL;
1494N/A }
1494N/A
1494N/A if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
1494N/A !drm_core_check_feature(dev, DRIVER_MODESET))
1494N/A drm_dma_takedown(dev);
1494N/A
1494N/A mutex_unlock(&dev->struct_mutex);
1494N/A
1494N/A DRM_DEBUG("lastclose completed\n");
1494N/A return 0;
1494N/A}
1494N/A
1494N/A/**
1494N/A * Module initialization. Called via init_module at module load time, or via
1494N/A * linux/init/main.c (this is not currently supported).
1494N/A *
1494N/A * \return zero on success or a negative number on failure.
1494N/A *
1494N/A * Initializes an array of drm_device structures, and attempts to
1494N/A * initialize all available devices, using consecutive minors, registering the
1494N/A * stubs and initializing the AGP device.
1494N/A *
1494N/A * Expands the \c DRIVER_PREINIT and \c DRIVER_POST_INIT macros before and
1494N/A * after the initialization for driver customization.
1494N/A */
1494N/Aint drm_init(struct drm_device *dev, struct drm_driver *driver)
1494N/A{
1494N/A dev_info_t *dip = dev->devinfo;
1494N/A struct pci_dev *pdev = NULL;
1494N/A struct dev_ops *devop;
1494N/A int ret, i;
1494N/A
1494N/A DRM_DEBUG("\n");
1494N/A
1494N/A pdev = pci_dev_create(dev);
1494N/A if (!pdev)
1494N/A return -EFAULT;
1494N/A
1494N/A for (i = 0; driver->id_table[i].vendor != 0; i++) {
1494N/A if ((driver->id_table[i].vendor == pdev->vendor) &&
1494N/A (driver->id_table[i].device == pdev->device)) {
1494N/A ret = drm_get_dev(dev, pdev, driver, driver->id_table[i].driver_data);
1494N/A if (ret) {
1494N/A pci_dev_destroy(pdev);
1494N/A return ret;
1494N/A }
1494N/A
1494N/A /*
1494N/A * DRM drivers are required to use common cb_ops
1494N/A */
1494N/A devop = ddi_get_driver(dev->devinfo);
1494N/A if (devop->devo_cb_ops != &drm_cb_ops) {
1494N/A devop->devo_cb_ops = &drm_cb_ops;
1494N/A }
1494N/A /*
1494N/A * Initialize for fma support
1494N/A */
1494N/A dev->drm_fm_cap = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1494N/A DDI_PROP_DONTPASS, "fm-capable",
1494N/A DDI_FM_EREPORT_CAPABLE | DDI_FM_DMACHK_CAPABLE |
1494N/A DDI_FM_ACCCHK_CAPABLE | DDI_FM_ERRCB_CAPABLE);
1494N/A
1494N/A drm_fm_init(dev);
1494N/A return 0;
1494N/A }
1494N/A }
1494N/A
1494N/A pci_dev_destroy(pdev);
1494N/A return -ENODEV;
1494N/A}
1494N/A
1494N/Avoid drm_exit(struct drm_device *dev)
1494N/A{
1494N/A drm_put_dev(dev);
1494N/A pci_dev_destroy(dev->pdev);
1494N/A drm_fm_fini(dev);
1494N/A}
1494N/A
1494N/Aint __init drm_core_init(void)
1494N/A{
1494N/A INIT_LIST_HEAD(&drm_iomem_list);
1494N/A
1494N/A idr_init(&drm_minors_idr);
1494N/A
1494N/A DRM_INFO("Initialized %s %d.%d.%d %s\n",
1494N/A CORE_NAME, CORE_MAJOR, CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
1494N/A return 0;
1494N/A}
1494N/A
1494N/Avoid __exit drm_core_exit(void)
1494N/A{
1494N/A idr_remove_all(&drm_minors_idr);
1494N/A idr_destroy(&drm_minors_idr);
1494N/A}
1494N/A
1494N/A/**
1494N/A * Copy and IOCTL return string to user space
1494N/A */
1494N/Astatic int drm_copy_field(char *buf, size_t *buf_len, const char *value)
1494N/A{
1494N/A int len;
1494N/A
1494N/A /* don't overflow userbuf */
1494N/A len = strlen(value);
1494N/A if (len > *buf_len)
1494N/A len = *buf_len;
1494N/A
1494N/A /* let userspace know exact length of driver value (which could be
1494N/A * larger than the userspace-supplied buffer) */
1494N/A *buf_len = strlen(value);
1494N/A
1494N/A /* finally, try filling in the userbuf */
1494N/A if (len && buf)
1494N/A if (copy_to_user(buf, value, len))
1494N/A return -EFAULT;
1494N/A return 0;
1494N/A}
1494N/A
1494N/A/**
1494N/A * Get version information
1494N/A *
1494N/A * \return zero on success or negative number on failure.
1494N/A *
1494N/A * Fills in the version information in \p arg.
1494N/A */
1494N/A/* LINTED */
1494N/Astatic int drm_version(DRM_IOCTL_ARGS)
1494N/A{
1494N/A struct drm_version *version = data;
1494N/A int err;
1494N/A
1494N/A version->version_major = dev->driver->major;
1494N/A version->version_minor = dev->driver->minor;
1494N/A version->version_patchlevel = dev->driver->patchlevel;
1494N/A err = drm_copy_field(version->name, &version->name_len,
1494N/A dev->driver->name);
1494N/A if (!err)
1494N/A err = drm_copy_field(version->date, &version->date_len,
1494N/A dev->driver->date);
1494N/A if (!err)
1494N/A err = drm_copy_field(version->desc, &version->desc_len,
1494N/A dev->driver->desc);
1494N/A
1494N/A return err;
1494N/A}
1494N/A
1494N/A/**
1494N/A * Called whenever a process performs an ioctl on /dev/drm.
1494N/A *
1494N/A * \return zero on success or negative number on failure.
1494N/A *
1494N/A * Looks up the ioctl function in the ::ioctls table, checking for root
1494N/A * previleges if so required, and dispatches to the respective function.
1494N/A */
1494N/Aint drm_ioctl(dev_t dev_id, struct drm_file *file_priv,
1494N/A int cmd, intptr_t arg, int mode, cred_t *credp)
1494N/A{
1494N/A struct drm_device *dev = file_priv->minor->dev;
1494N/A struct drm_ioctl_desc *ioctl;
1494N/A drm_ioctl_t *func;
1494N/A unsigned int nr = DRM_IOCTL_NR(cmd);
1494N/A int retcode = -EINVAL;
1494N/A char stack_kdata[128];
1494N/A char *kdata = NULL;
1494N/A unsigned int usize, asize;
1683N/A#ifdef DEBUG
1683N/A const char *ioc_name = NULL;
1683N/A#endif
1494N/A
1494N/A atomic_inc(&dev->ioctl_count);
1494N/A atomic_inc(&dev->counts[_DRM_STAT_IOCTLS]);
1494N/A ++file_priv->ioctl_count;
1494N/A
1494N/A if ((nr >= DRM_CORE_IOCTL_COUNT) &&
1494N/A ((nr < DRM_COMMAND_BASE) || (nr >= DRM_COMMAND_END)))
1494N/A goto err_i1;
1494N/A if ((nr >= DRM_COMMAND_BASE) && (nr < DRM_COMMAND_END) &&
1494N/A (nr < DRM_COMMAND_BASE + dev->driver->num_ioctls)) {
1494N/A ioctl = &dev->driver->ioctls[nr - DRM_COMMAND_BASE];
1494N/A usize = asize = _IOC_SIZE(cmd);
1494N/A cmd = ioctl->cmd;
1494N/A }
1494N/A else if ((nr >= DRM_COMMAND_END) || (nr < DRM_COMMAND_BASE)) {
1494N/A ioctl = &drm_ioctls[nr];
1494N/A cmd = ioctl->cmd;
1494N/A usize = asize = _IOC_SIZE(cmd);
1494N/A } else
1494N/A goto err_i1;
1494N/A
1494N/A /* Do not trust userspace, use our own definition */
1494N/A func = ioctl->func;
1494N/A /* is there a local override? */
1494N/A if ((nr == DRM_IOCTL_NR(DRM_IOCTL_DMA)) && dev->driver->dma_ioctl)
1494N/A func = dev->driver->dma_ioctl;
1494N/A
1683N/A#ifdef DEBUG
1683N/A if (ioctl->name != NULL)
1683N/A ioc_name = ioctl->name;
1683N/A#endif
1683N/A
1494N/A if (!func) {
1494N/A DRM_DEBUG("no function\n");
1494N/A retcode = -EINVAL;
1494N/A } else if (((ioctl->flags & DRM_ROOT_ONLY) && !DRM_SUSER(credp)) ||
1494N/A ((ioctl->flags & DRM_AUTH) && !file_priv->authenticated) ||
1494N/A ((ioctl->flags & DRM_MASTER) && !file_priv->is_master) ||
1494N/A (!(ioctl->flags & DRM_CONTROL_ALLOW) && (file_priv->minor->type == DRM_MINOR_CONTROL))) {
1494N/A retcode = -EACCES;
1494N/A } else {
1494N/A if (cmd & (IOC_IN | IOC_OUT)) {
1494N/A if (asize <= sizeof(stack_kdata)) {
1494N/A kdata = stack_kdata;
1494N/A } else {
1494N/A kdata = kmalloc(asize, GFP_KERNEL);
1494N/A if (!kdata) {
1494N/A retcode = -ENOMEM;
1494N/A goto err_i1;
1494N/A }
1494N/A }
1494N/A if (asize > usize)
1494N/A (void) memset(kdata + usize, 0, asize - usize);
1494N/A }
1494N/A
1494N/A if (cmd & IOC_IN) {
1494N/A#ifdef _MULTI_DATAMODEL
1494N/A if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32 && ioctl->copyin32) {
1494N/A if (ioctl->copyin32((void*)kdata, (void*)arg)) {
1494N/A retcode = -EFAULT;
1494N/A goto err_i1;
1494N/A }
1494N/A } else {
1494N/A#endif
1494N/A if (DRM_COPY_FROM_USER(kdata, (void __user *)arg,
1494N/A _IOC_SIZE(cmd)) != 0) {
1494N/A retcode = -EFAULT;
1494N/A goto err_i1;
1494N/A }
1494N/A#ifdef _MULTI_DATAMODEL
1494N/A }
1494N/A#endif
1494N/A }
1494N/A retcode = func(dev_id, dev, kdata, file_priv, mode, credp);
1494N/A
1494N/A if (cmd & IOC_OUT) {
1494N/A#ifdef _MULTI_DATAMODEL
1494N/A if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32 && ioctl->copyout32) {
1494N/A if (ioctl->copyout32((void*)arg, (void*)kdata)) {
1494N/A retcode = -EFAULT;
1494N/A goto err_i1;
1494N/A }
1494N/A } else {
1494N/A#endif
1494N/A if (DRM_COPY_TO_USER((void __user *)arg, kdata,
1494N/A _IOC_SIZE(cmd)) != 0)
1494N/A retcode = -EFAULT;
1494N/A#ifdef _MULTI_DATAMODEL
1494N/A }
1494N/A#endif
1494N/A }
1494N/A }
1494N/A
1494N/Aerr_i1:
1494N/A if (kdata && (kdata != stack_kdata))
1494N/A kfree(kdata, asize);
1494N/A atomic_dec(&dev->ioctl_count);
1683N/A#ifdef DEBUG
1494N/A if (retcode)
1683N/A if (ioc_name == NULL)
1683N/A DRM_DEBUG("ioccmd: 0x%x, ret = %d\n", cmd, -retcode);
1683N/A else
1683N/A DRM_DEBUG("IOC %s: ret = %d\n", ioc_name, -retcode);
1683N/A#endif
1494N/A return retcode;
1494N/A}
1494N/A
1494N/Astruct drm_local_map *drm_getsarea(struct drm_device *dev)
1494N/A{
1494N/A struct drm_map_list *entry;
1494N/A
1494N/A list_for_each_entry(entry, struct drm_map_list, &dev->maplist, head) {
1494N/A if (entry->map && entry->map->type == _DRM_SHM &&
1494N/A (entry->map->flags & _DRM_CONTAINS_LOCK)) {
1494N/A return entry->map;
1494N/A }
1494N/A }
1494N/A return NULL;
1494N/A}
1494N/A
1494N/Avoid
1494N/Adrm_gem_object_reference(struct drm_gem_object *obj)
1494N/A{
1494N/A kref_get(&obj->refcount);
1494N/A}
1494N/A
1494N/Avoid
1494N/Adrm_gem_object_unreference(struct drm_gem_object *obj)
1494N/A{
1494N/A if (obj != NULL)
1494N/A kref_put(&obj->refcount, drm_gem_object_free);
1494N/A}
1494N/A
1494N/Avoid
1494N/Adrm_gem_object_unreference_unlocked(struct drm_gem_object *obj)
1494N/A{
1494N/A if (obj != NULL) {
1494N/A struct drm_device *dev = obj->dev;
1494N/A mutex_lock(&dev->struct_mutex);
1494N/A kref_put(&obj->refcount, drm_gem_object_free);
1494N/A mutex_unlock(&dev->struct_mutex);
1494N/A }
1494N/A}
1494N/A
1494N/Avoid
1494N/Adrm_gem_object_handle_reference(struct drm_gem_object *obj)
1494N/A{
1494N/A drm_gem_object_reference(obj);
1494N/A atomic_inc(&obj->handle_count);
1494N/A}
1494N/A
1494N/Avoid
1494N/Adrm_gem_object_handle_unreference(struct drm_gem_object *obj)
1494N/A{
1494N/A if (obj == NULL)
1494N/A return;
1494N/A
1494N/A if (atomic_read(&obj->handle_count) == 0)
1494N/A return;
1494N/A /*
1494N/A * Must bump handle count first as this may be the last
1494N/A * ref, in which case the object would disappear before we
1494N/A * checked for a name
1494N/A */
1494N/A if (atomic_dec_and_test(&obj->handle_count))
1494N/A drm_gem_object_handle_free(obj);
1494N/A drm_gem_object_unreference(obj);
1494N/A}
1494N/A
1494N/Avoid
1494N/Adrm_gem_object_handle_unreference_unlocked(struct drm_gem_object *obj)
1494N/A{
1494N/A if (obj == NULL)
1494N/A return;
1494N/A
1494N/A if (atomic_read(&obj->handle_count) == 0)
1494N/A return;
1494N/A
1494N/A /*
1494N/A * Must bump handle count first as this may be the last
1494N/A * ref, in which case the object would disappear before we
1494N/A * checked for a name
1494N/A */
1494N/A
1494N/A if (atomic_dec_and_test(&obj->handle_count))
1494N/A drm_gem_object_handle_free(obj);
1494N/A drm_gem_object_unreference_unlocked(obj);
1494N/A}
1494N/A
1494N/A
1494N/A/*
1494N/A * PCI bus specific error callback
1494N/A */
1494N/A/* ARGSUSED */
1494N/Aint
1494N/Adrm_fm_error_cb(
1494N/A dev_info_t *dip,
1494N/A ddi_fm_error_t *err,
1494N/A const void *impl)
1494N/A{
1494N/A
1494N/A pci_ereport_post(dip, err, NULL);
1494N/A
1494N/A return (err->fme_status);
1494N/A}
1494N/A
1494N/Avoid
1494N/Adrm_fm_init(struct drm_device *dev)
1494N/A{
1494N/A dev_info_t *dip = dev->devinfo;
1494N/A if (dev->drm_fm_cap) {
1494N/A /* we do not care the iblk cookie */
1494N/A ddi_fm_init(dip, &dev->drm_fm_cap, NULL);
1494N/A
1494N/A /*
1494N/A * Initialize pci ereport capabilities if ereport capable
1494N/A */
1494N/A if (DDI_FM_EREPORT_CAP(dev->drm_fm_cap) ||
1494N/A DDI_FM_ERRCB_CAP(dev->drm_fm_cap)) {
1494N/A pci_ereport_setup(dip);
1494N/A }
1494N/A
1494N/A /*
1494N/A * Register error callback if error callback capable
1494N/A */
1494N/A if (DDI_FM_ERRCB_CAP(dev->drm_fm_cap)) {
1494N/A ddi_fm_handler_register(dip,
1494N/A drm_fm_error_cb, (void*) dev);
1494N/A }
1494N/A }
1494N/A}
1494N/A
1494N/Avoid
1494N/Adrm_fm_fini(struct drm_device *dev)
1494N/A{
1494N/A dev_info_t *dip = dev->devinfo;
1494N/A if (dev->drm_fm_cap) {
1494N/A if (DDI_FM_EREPORT_CAP(dev->drm_fm_cap) ||
1494N/A DDI_FM_ERRCB_CAP(dev->drm_fm_cap)) {
1494N/A pci_ereport_teardown(dip);
1494N/A }
1494N/A
1494N/A if (DDI_FM_ERRCB_CAP(dev->drm_fm_cap)) {
1494N/A ddi_fm_handler_unregister(dip);
1494N/A }
1494N/A
1494N/A ddi_fm_fini(dip);
1494N/A }
1494N/A}
1494N/A
1494N/A/*
1494N/A * report a device error.
1494N/A * detail - DDI_FM_DEVICE_*
1494N/A */
1494N/Avoid
1494N/Adrm_fm_ereport(
1494N/A struct drm_device *dev,
1494N/A char *detail)
1494N/A{
1494N/A uint64_t ena;
1494N/A char buf[FM_MAX_CLASS];
1494N/A dev_info_t *dip = dev->devinfo;
1494N/A
1494N/A if (DDI_FM_EREPORT_CAP(dev->drm_fm_cap)) {
1494N/A
1494N/A (void) snprintf(buf, FM_MAX_CLASS, "%s.%s", DDI_FM_DEVICE,
1494N/A detail);
1494N/A
1494N/A ena = fm_ena_generate(0, FM_ENA_FMT1);
1494N/A
1494N/A ddi_fm_ereport_post(dip, buf, ena, DDI_NOSLEEP,
1494N/A FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, NULL);
1494N/A }
1494N/A}
1494N/A
1494N/A/*
1494N/A * Check PIO Access error, like register access.
1494N/A */
1494N/Aint
1494N/Adrm_check_acc_handle(
1494N/A struct drm_device *dev,
1494N/A ddi_acc_handle_t handle)
1494N/A{
1494N/A ddi_fm_error_t de;
1494N/A
1494N/A if (!DDI_FM_ACC_ERR_CAP(dev->drm_fm_cap)) {
1494N/A
1494N/A return (DDI_FM_OK);
1494N/A
1494N/A }
1494N/A
1494N/A ddi_fm_acc_err_get(handle, &de, DDI_FME_VERSION);
1494N/A ddi_fm_acc_err_clear(handle, DDI_FME_VERSION);
1494N/A
1494N/A return (de.fme_status);
1494N/A
1494N/A}
1494N/A
1494N/A/*
1494N/A * Check DMA Access error.
1494N/A */
1494N/Aint
1494N/Adrm_check_dma_handle(
1494N/A struct drm_device *dev,
1494N/A ddi_dma_handle_t handle)
1494N/A{
1494N/A ddi_fm_error_t de;
1494N/A
1494N/A if (!DDI_FM_DMA_ERR_CAP(dev->drm_fm_cap)) {
1494N/A
1494N/A return (DDI_FM_OK);
1494N/A
1494N/A }
1494N/A
1494N/A ddi_fm_dma_err_get(handle, &de, DDI_FME_VERSION);
1494N/A ddi_fm_dma_err_clear(handle, DDI_FME_VERSION);
1494N/A
1494N/A return (de.fme_status);
1494N/A
1494N/A}