18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Created: Fri Jan  8 09:01:26 1999 by faith@valinux.com
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
58c2ecf20Sopenharmony_ci * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
68c2ecf20Sopenharmony_ci * All Rights Reserved.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Author Rickard E. (Rik) Faith <faith@valinux.com>
98c2ecf20Sopenharmony_ci * Author Gareth Hughes <gareth@valinux.com>
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
128c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
138c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation
148c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
158c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
168c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice (including the next
198c2ecf20Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
208c2ecf20Sopenharmony_ci * Software.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
238c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
248c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
258c2ecf20Sopenharmony_ci * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
268c2ecf20Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
278c2ecf20Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
288c2ecf20Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
298c2ecf20Sopenharmony_ci */
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#include <linux/export.h>
328c2ecf20Sopenharmony_ci#include <linux/nospec.h>
338c2ecf20Sopenharmony_ci#include <linux/pci.h>
348c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#include <drm/drm_agpsupport.h>
378c2ecf20Sopenharmony_ci#include <drm/drm_auth.h>
388c2ecf20Sopenharmony_ci#include <drm/drm_crtc.h>
398c2ecf20Sopenharmony_ci#include <drm/drm_drv.h>
408c2ecf20Sopenharmony_ci#include <drm/drm_file.h>
418c2ecf20Sopenharmony_ci#include <drm/drm_ioctl.h>
428c2ecf20Sopenharmony_ci#include <drm/drm_print.h>
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#include "drm_crtc_internal.h"
458c2ecf20Sopenharmony_ci#include "drm_internal.h"
468c2ecf20Sopenharmony_ci#include "drm_legacy.h"
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci/**
498c2ecf20Sopenharmony_ci * DOC: getunique and setversion story
508c2ecf20Sopenharmony_ci *
518c2ecf20Sopenharmony_ci * BEWARE THE DRAGONS! MIND THE TRAPDOORS!
528c2ecf20Sopenharmony_ci *
538c2ecf20Sopenharmony_ci * In an attempt to warn anyone else who's trying to figure out what's going
548c2ecf20Sopenharmony_ci * on here, I'll try to summarize the story. First things first, let's clear up
558c2ecf20Sopenharmony_ci * the names, because the kernel internals, libdrm and the ioctls are all named
568c2ecf20Sopenharmony_ci * differently:
578c2ecf20Sopenharmony_ci *
588c2ecf20Sopenharmony_ci *  - GET_UNIQUE ioctl, implemented by drm_getunique is wrapped up in libdrm
598c2ecf20Sopenharmony_ci *    through the drmGetBusid function.
608c2ecf20Sopenharmony_ci *  - The libdrm drmSetBusid function is backed by the SET_UNIQUE ioctl. All
618c2ecf20Sopenharmony_ci *    that code is nerved in the kernel with drm_invalid_op().
628c2ecf20Sopenharmony_ci *  - The internal set_busid kernel functions and driver callbacks are
638c2ecf20Sopenharmony_ci *    exclusively use by the SET_VERSION ioctl, because only drm 1.0 (which is
648c2ecf20Sopenharmony_ci *    nerved) allowed userspace to set the busid through the above ioctl.
658c2ecf20Sopenharmony_ci *  - Other ioctls and functions involved are named consistently.
668c2ecf20Sopenharmony_ci *
678c2ecf20Sopenharmony_ci * For anyone wondering what's the difference between drm 1.1 and 1.4: Correctly
688c2ecf20Sopenharmony_ci * handling pci domains in the busid on ppc. Doing this correctly was only
698c2ecf20Sopenharmony_ci * implemented in libdrm in 2010, hence can't be nerved yet. No one knows what's
708c2ecf20Sopenharmony_ci * special with drm 1.2 and 1.3.
718c2ecf20Sopenharmony_ci *
728c2ecf20Sopenharmony_ci * Now the actual horror story of how device lookup in drm works. At large,
738c2ecf20Sopenharmony_ci * there's 2 different ways, either by busid, or by device driver name.
748c2ecf20Sopenharmony_ci *
758c2ecf20Sopenharmony_ci * Opening by busid is fairly simple:
768c2ecf20Sopenharmony_ci *
778c2ecf20Sopenharmony_ci * 1. First call SET_VERSION to make sure pci domains are handled properly. As a
788c2ecf20Sopenharmony_ci *    side-effect this fills out the unique name in the master structure.
798c2ecf20Sopenharmony_ci * 2. Call GET_UNIQUE to read out the unique name from the master structure,
808c2ecf20Sopenharmony_ci *    which matches the busid thanks to step 1. If it doesn't, proceed to try
818c2ecf20Sopenharmony_ci *    the next device node.
828c2ecf20Sopenharmony_ci *
838c2ecf20Sopenharmony_ci * Opening by name is slightly different:
848c2ecf20Sopenharmony_ci *
858c2ecf20Sopenharmony_ci * 1. Directly call VERSION to get the version and to match against the driver
868c2ecf20Sopenharmony_ci *    name returned by that ioctl. Note that SET_VERSION is not called, which
878c2ecf20Sopenharmony_ci *    means the the unique name for the master node just opening is _not_ filled
888c2ecf20Sopenharmony_ci *    out. This despite that with current drm device nodes are always bound to
898c2ecf20Sopenharmony_ci *    one device, and can't be runtime assigned like with drm 1.0.
908c2ecf20Sopenharmony_ci * 2. Match driver name. If it mismatches, proceed to the next device node.
918c2ecf20Sopenharmony_ci * 3. Call GET_UNIQUE, and check whether the unique name has length zero (by
928c2ecf20Sopenharmony_ci *    checking that the first byte in the string is 0). If that's not the case
938c2ecf20Sopenharmony_ci *    libdrm skips and proceeds to the next device node. Probably this is just
948c2ecf20Sopenharmony_ci *    copypasta from drm 1.0 times where a set unique name meant that the driver
958c2ecf20Sopenharmony_ci *    was in use already, but that's just conjecture.
968c2ecf20Sopenharmony_ci *
978c2ecf20Sopenharmony_ci * Long story short: To keep the open by name logic working, GET_UNIQUE must
988c2ecf20Sopenharmony_ci * _not_ return a unique string when SET_VERSION hasn't been called yet,
998c2ecf20Sopenharmony_ci * otherwise libdrm breaks. Even when that unique string can't ever change, and
1008c2ecf20Sopenharmony_ci * is totally irrelevant for actually opening the device because runtime
1018c2ecf20Sopenharmony_ci * assignable device instances were only support in drm 1.0, which is long dead.
1028c2ecf20Sopenharmony_ci * But the libdrm code in drmOpenByName somehow survived, hence this can't be
1038c2ecf20Sopenharmony_ci * broken.
1048c2ecf20Sopenharmony_ci */
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci/*
1078c2ecf20Sopenharmony_ci * Get the bus id.
1088c2ecf20Sopenharmony_ci *
1098c2ecf20Sopenharmony_ci * \param inode device inode.
1108c2ecf20Sopenharmony_ci * \param file_priv DRM file private.
1118c2ecf20Sopenharmony_ci * \param cmd command.
1128c2ecf20Sopenharmony_ci * \param arg user argument, pointing to a drm_unique structure.
1138c2ecf20Sopenharmony_ci * \return zero on success or a negative number on failure.
1148c2ecf20Sopenharmony_ci *
1158c2ecf20Sopenharmony_ci * Copies the bus id from drm_device::unique into user space.
1168c2ecf20Sopenharmony_ci */
1178c2ecf20Sopenharmony_ciint drm_getunique(struct drm_device *dev, void *data,
1188c2ecf20Sopenharmony_ci		  struct drm_file *file_priv)
1198c2ecf20Sopenharmony_ci{
1208c2ecf20Sopenharmony_ci	struct drm_unique *u = data;
1218c2ecf20Sopenharmony_ci	struct drm_master *master;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	mutex_lock(&dev->master_mutex);
1248c2ecf20Sopenharmony_ci	master = file_priv->master;
1258c2ecf20Sopenharmony_ci	if (u->unique_len >= master->unique_len) {
1268c2ecf20Sopenharmony_ci		if (copy_to_user(u->unique, master->unique, master->unique_len)) {
1278c2ecf20Sopenharmony_ci			mutex_unlock(&dev->master_mutex);
1288c2ecf20Sopenharmony_ci			return -EFAULT;
1298c2ecf20Sopenharmony_ci		}
1308c2ecf20Sopenharmony_ci	}
1318c2ecf20Sopenharmony_ci	u->unique_len = master->unique_len;
1328c2ecf20Sopenharmony_ci	mutex_unlock(&dev->master_mutex);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	return 0;
1358c2ecf20Sopenharmony_ci}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_cistatic void
1388c2ecf20Sopenharmony_cidrm_unset_busid(struct drm_device *dev,
1398c2ecf20Sopenharmony_ci		struct drm_master *master)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	kfree(master->unique);
1428c2ecf20Sopenharmony_ci	master->unique = NULL;
1438c2ecf20Sopenharmony_ci	master->unique_len = 0;
1448c2ecf20Sopenharmony_ci}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cistatic int drm_set_busid(struct drm_device *dev, struct drm_file *file_priv)
1478c2ecf20Sopenharmony_ci{
1488c2ecf20Sopenharmony_ci	struct drm_master *master = file_priv->master;
1498c2ecf20Sopenharmony_ci	int ret;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	if (master->unique != NULL)
1528c2ecf20Sopenharmony_ci		drm_unset_busid(dev, master);
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	if (dev->dev && dev_is_pci(dev->dev)) {
1558c2ecf20Sopenharmony_ci		ret = drm_pci_set_busid(dev, master);
1568c2ecf20Sopenharmony_ci		if (ret) {
1578c2ecf20Sopenharmony_ci			drm_unset_busid(dev, master);
1588c2ecf20Sopenharmony_ci			return ret;
1598c2ecf20Sopenharmony_ci		}
1608c2ecf20Sopenharmony_ci	} else {
1618c2ecf20Sopenharmony_ci		WARN_ON(!dev->unique);
1628c2ecf20Sopenharmony_ci		master->unique = kstrdup(dev->unique, GFP_KERNEL);
1638c2ecf20Sopenharmony_ci		if (master->unique)
1648c2ecf20Sopenharmony_ci			master->unique_len = strlen(dev->unique);
1658c2ecf20Sopenharmony_ci	}
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	return 0;
1688c2ecf20Sopenharmony_ci}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci/*
1718c2ecf20Sopenharmony_ci * Get client information.
1728c2ecf20Sopenharmony_ci *
1738c2ecf20Sopenharmony_ci * \param inode device inode.
1748c2ecf20Sopenharmony_ci * \param file_priv DRM file private.
1758c2ecf20Sopenharmony_ci * \param cmd command.
1768c2ecf20Sopenharmony_ci * \param arg user argument, pointing to a drm_client structure.
1778c2ecf20Sopenharmony_ci *
1788c2ecf20Sopenharmony_ci * \return zero on success or a negative number on failure.
1798c2ecf20Sopenharmony_ci *
1808c2ecf20Sopenharmony_ci * Searches for the client with the specified index and copies its information
1818c2ecf20Sopenharmony_ci * into userspace
1828c2ecf20Sopenharmony_ci */
1838c2ecf20Sopenharmony_ciint drm_getclient(struct drm_device *dev, void *data,
1848c2ecf20Sopenharmony_ci		  struct drm_file *file_priv)
1858c2ecf20Sopenharmony_ci{
1868c2ecf20Sopenharmony_ci	struct drm_client *client = data;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	/*
1898c2ecf20Sopenharmony_ci	 * Hollowed-out getclient ioctl to keep some dead old drm tests/tools
1908c2ecf20Sopenharmony_ci	 * not breaking completely. Userspace tools stop enumerating one they
1918c2ecf20Sopenharmony_ci	 * get -EINVAL, hence this is the return value we need to hand back for
1928c2ecf20Sopenharmony_ci	 * no clients tracked.
1938c2ecf20Sopenharmony_ci	 *
1948c2ecf20Sopenharmony_ci	 * Unfortunately some clients (*cough* libva *cough*) use this in a fun
1958c2ecf20Sopenharmony_ci	 * attempt to figure out whether they're authenticated or not. Since
1968c2ecf20Sopenharmony_ci	 * that's the only thing they care about, give it to the directly
1978c2ecf20Sopenharmony_ci	 * instead of walking one giant list.
1988c2ecf20Sopenharmony_ci	 */
1998c2ecf20Sopenharmony_ci	if (client->idx == 0) {
2008c2ecf20Sopenharmony_ci		client->auth = file_priv->authenticated;
2018c2ecf20Sopenharmony_ci		client->pid = task_pid_vnr(current);
2028c2ecf20Sopenharmony_ci		client->uid = overflowuid;
2038c2ecf20Sopenharmony_ci		client->magic = 0;
2048c2ecf20Sopenharmony_ci		client->iocs = 0;
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci		return 0;
2078c2ecf20Sopenharmony_ci	} else {
2088c2ecf20Sopenharmony_ci		return -EINVAL;
2098c2ecf20Sopenharmony_ci	}
2108c2ecf20Sopenharmony_ci}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci/*
2138c2ecf20Sopenharmony_ci * Get statistics information.
2148c2ecf20Sopenharmony_ci *
2158c2ecf20Sopenharmony_ci * \param inode device inode.
2168c2ecf20Sopenharmony_ci * \param file_priv DRM file private.
2178c2ecf20Sopenharmony_ci * \param cmd command.
2188c2ecf20Sopenharmony_ci * \param arg user argument, pointing to a drm_stats structure.
2198c2ecf20Sopenharmony_ci *
2208c2ecf20Sopenharmony_ci * \return zero on success or a negative number on failure.
2218c2ecf20Sopenharmony_ci */
2228c2ecf20Sopenharmony_cistatic int drm_getstats(struct drm_device *dev, void *data,
2238c2ecf20Sopenharmony_ci		 struct drm_file *file_priv)
2248c2ecf20Sopenharmony_ci{
2258c2ecf20Sopenharmony_ci	struct drm_stats *stats = data;
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	/* Clear stats to prevent userspace from eating its stack garbage. */
2288c2ecf20Sopenharmony_ci	memset(stats, 0, sizeof(*stats));
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	return 0;
2318c2ecf20Sopenharmony_ci}
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci/*
2348c2ecf20Sopenharmony_ci * Get device/driver capabilities
2358c2ecf20Sopenharmony_ci */
2368c2ecf20Sopenharmony_cistatic int drm_getcap(struct drm_device *dev, void *data, struct drm_file *file_priv)
2378c2ecf20Sopenharmony_ci{
2388c2ecf20Sopenharmony_ci	struct drm_get_cap *req = data;
2398c2ecf20Sopenharmony_ci	struct drm_crtc *crtc;
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	req->value = 0;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	/* Only some caps make sense with UMS/render-only drivers. */
2448c2ecf20Sopenharmony_ci	switch (req->capability) {
2458c2ecf20Sopenharmony_ci	case DRM_CAP_TIMESTAMP_MONOTONIC:
2468c2ecf20Sopenharmony_ci		req->value = 1;
2478c2ecf20Sopenharmony_ci		return 0;
2488c2ecf20Sopenharmony_ci	case DRM_CAP_PRIME:
2498c2ecf20Sopenharmony_ci		req->value |= dev->driver->prime_fd_to_handle ? DRM_PRIME_CAP_IMPORT : 0;
2508c2ecf20Sopenharmony_ci		req->value |= dev->driver->prime_handle_to_fd ? DRM_PRIME_CAP_EXPORT : 0;
2518c2ecf20Sopenharmony_ci		return 0;
2528c2ecf20Sopenharmony_ci	case DRM_CAP_SYNCOBJ:
2538c2ecf20Sopenharmony_ci		req->value = drm_core_check_feature(dev, DRIVER_SYNCOBJ);
2548c2ecf20Sopenharmony_ci		return 0;
2558c2ecf20Sopenharmony_ci	case DRM_CAP_SYNCOBJ_TIMELINE:
2568c2ecf20Sopenharmony_ci		req->value = drm_core_check_feature(dev, DRIVER_SYNCOBJ_TIMELINE);
2578c2ecf20Sopenharmony_ci		return 0;
2588c2ecf20Sopenharmony_ci	}
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	/* Other caps only work with KMS drivers */
2618c2ecf20Sopenharmony_ci	if (!drm_core_check_feature(dev, DRIVER_MODESET))
2628c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	switch (req->capability) {
2658c2ecf20Sopenharmony_ci	case DRM_CAP_DUMB_BUFFER:
2668c2ecf20Sopenharmony_ci		if (dev->driver->dumb_create)
2678c2ecf20Sopenharmony_ci			req->value = 1;
2688c2ecf20Sopenharmony_ci		break;
2698c2ecf20Sopenharmony_ci	case DRM_CAP_VBLANK_HIGH_CRTC:
2708c2ecf20Sopenharmony_ci		req->value = 1;
2718c2ecf20Sopenharmony_ci		break;
2728c2ecf20Sopenharmony_ci	case DRM_CAP_DUMB_PREFERRED_DEPTH:
2738c2ecf20Sopenharmony_ci		req->value = dev->mode_config.preferred_depth;
2748c2ecf20Sopenharmony_ci		break;
2758c2ecf20Sopenharmony_ci	case DRM_CAP_DUMB_PREFER_SHADOW:
2768c2ecf20Sopenharmony_ci		req->value = dev->mode_config.prefer_shadow;
2778c2ecf20Sopenharmony_ci		break;
2788c2ecf20Sopenharmony_ci	case DRM_CAP_ASYNC_PAGE_FLIP:
2798c2ecf20Sopenharmony_ci		req->value = dev->mode_config.async_page_flip;
2808c2ecf20Sopenharmony_ci		break;
2818c2ecf20Sopenharmony_ci	case DRM_CAP_PAGE_FLIP_TARGET:
2828c2ecf20Sopenharmony_ci		req->value = 1;
2838c2ecf20Sopenharmony_ci		drm_for_each_crtc(crtc, dev) {
2848c2ecf20Sopenharmony_ci			if (!crtc->funcs->page_flip_target)
2858c2ecf20Sopenharmony_ci				req->value = 0;
2868c2ecf20Sopenharmony_ci		}
2878c2ecf20Sopenharmony_ci		break;
2888c2ecf20Sopenharmony_ci	case DRM_CAP_CURSOR_WIDTH:
2898c2ecf20Sopenharmony_ci		if (dev->mode_config.cursor_width)
2908c2ecf20Sopenharmony_ci			req->value = dev->mode_config.cursor_width;
2918c2ecf20Sopenharmony_ci		else
2928c2ecf20Sopenharmony_ci			req->value = 64;
2938c2ecf20Sopenharmony_ci		break;
2948c2ecf20Sopenharmony_ci	case DRM_CAP_CURSOR_HEIGHT:
2958c2ecf20Sopenharmony_ci		if (dev->mode_config.cursor_height)
2968c2ecf20Sopenharmony_ci			req->value = dev->mode_config.cursor_height;
2978c2ecf20Sopenharmony_ci		else
2988c2ecf20Sopenharmony_ci			req->value = 64;
2998c2ecf20Sopenharmony_ci		break;
3008c2ecf20Sopenharmony_ci	case DRM_CAP_ADDFB2_MODIFIERS:
3018c2ecf20Sopenharmony_ci		req->value = dev->mode_config.allow_fb_modifiers;
3028c2ecf20Sopenharmony_ci		break;
3038c2ecf20Sopenharmony_ci	case DRM_CAP_CRTC_IN_VBLANK_EVENT:
3048c2ecf20Sopenharmony_ci		req->value = 1;
3058c2ecf20Sopenharmony_ci		break;
3068c2ecf20Sopenharmony_ci	default:
3078c2ecf20Sopenharmony_ci		return -EINVAL;
3088c2ecf20Sopenharmony_ci	}
3098c2ecf20Sopenharmony_ci	return 0;
3108c2ecf20Sopenharmony_ci}
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci/*
3138c2ecf20Sopenharmony_ci * Set device/driver capabilities
3148c2ecf20Sopenharmony_ci */
3158c2ecf20Sopenharmony_cistatic int
3168c2ecf20Sopenharmony_cidrm_setclientcap(struct drm_device *dev, void *data, struct drm_file *file_priv)
3178c2ecf20Sopenharmony_ci{
3188c2ecf20Sopenharmony_ci	struct drm_set_client_cap *req = data;
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	/* No render-only settable capabilities for now */
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	/* Below caps that only works with KMS drivers */
3238c2ecf20Sopenharmony_ci	if (!drm_core_check_feature(dev, DRIVER_MODESET))
3248c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	switch (req->capability) {
3278c2ecf20Sopenharmony_ci	case DRM_CLIENT_CAP_STEREO_3D:
3288c2ecf20Sopenharmony_ci		if (req->value > 1)
3298c2ecf20Sopenharmony_ci			return -EINVAL;
3308c2ecf20Sopenharmony_ci		file_priv->stereo_allowed = req->value;
3318c2ecf20Sopenharmony_ci		break;
3328c2ecf20Sopenharmony_ci	case DRM_CLIENT_CAP_UNIVERSAL_PLANES:
3338c2ecf20Sopenharmony_ci		if (req->value > 1)
3348c2ecf20Sopenharmony_ci			return -EINVAL;
3358c2ecf20Sopenharmony_ci		file_priv->universal_planes = req->value;
3368c2ecf20Sopenharmony_ci		break;
3378c2ecf20Sopenharmony_ci	case DRM_CLIENT_CAP_ATOMIC:
3388c2ecf20Sopenharmony_ci		if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
3398c2ecf20Sopenharmony_ci			return -EOPNOTSUPP;
3408c2ecf20Sopenharmony_ci		/* The modesetting DDX has a totally broken idea of atomic. */
3418c2ecf20Sopenharmony_ci		if (current->comm[0] == 'X' && req->value == 1) {
3428c2ecf20Sopenharmony_ci			pr_info("broken atomic modeset userspace detected, disabling atomic\n");
3438c2ecf20Sopenharmony_ci			return -EOPNOTSUPP;
3448c2ecf20Sopenharmony_ci		}
3458c2ecf20Sopenharmony_ci		if (req->value > 2)
3468c2ecf20Sopenharmony_ci			return -EINVAL;
3478c2ecf20Sopenharmony_ci		file_priv->atomic = req->value;
3488c2ecf20Sopenharmony_ci		file_priv->universal_planes = req->value;
3498c2ecf20Sopenharmony_ci		/*
3508c2ecf20Sopenharmony_ci		 * No atomic user-space blows up on aspect ratio mode bits.
3518c2ecf20Sopenharmony_ci		 */
3528c2ecf20Sopenharmony_ci		file_priv->aspect_ratio_allowed = req->value;
3538c2ecf20Sopenharmony_ci		break;
3548c2ecf20Sopenharmony_ci	case DRM_CLIENT_CAP_ASPECT_RATIO:
3558c2ecf20Sopenharmony_ci		if (req->value > 1)
3568c2ecf20Sopenharmony_ci			return -EINVAL;
3578c2ecf20Sopenharmony_ci		file_priv->aspect_ratio_allowed = req->value;
3588c2ecf20Sopenharmony_ci		break;
3598c2ecf20Sopenharmony_ci	case DRM_CLIENT_CAP_WRITEBACK_CONNECTORS:
3608c2ecf20Sopenharmony_ci		if (!file_priv->atomic)
3618c2ecf20Sopenharmony_ci			return -EINVAL;
3628c2ecf20Sopenharmony_ci		if (req->value > 1)
3638c2ecf20Sopenharmony_ci			return -EINVAL;
3648c2ecf20Sopenharmony_ci		file_priv->writeback_connectors = req->value;
3658c2ecf20Sopenharmony_ci		break;
3668c2ecf20Sopenharmony_ci	default:
3678c2ecf20Sopenharmony_ci		return -EINVAL;
3688c2ecf20Sopenharmony_ci	}
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	return 0;
3718c2ecf20Sopenharmony_ci}
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci/*
3748c2ecf20Sopenharmony_ci * Setversion ioctl.
3758c2ecf20Sopenharmony_ci *
3768c2ecf20Sopenharmony_ci * \param inode device inode.
3778c2ecf20Sopenharmony_ci * \param file_priv DRM file private.
3788c2ecf20Sopenharmony_ci * \param cmd command.
3798c2ecf20Sopenharmony_ci * \param arg user argument, pointing to a drm_lock structure.
3808c2ecf20Sopenharmony_ci * \return zero on success or negative number on failure.
3818c2ecf20Sopenharmony_ci *
3828c2ecf20Sopenharmony_ci * Sets the requested interface version
3838c2ecf20Sopenharmony_ci */
3848c2ecf20Sopenharmony_cistatic int drm_setversion(struct drm_device *dev, void *data, struct drm_file *file_priv)
3858c2ecf20Sopenharmony_ci{
3868c2ecf20Sopenharmony_ci	struct drm_set_version *sv = data;
3878c2ecf20Sopenharmony_ci	int if_version, retcode = 0;
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	mutex_lock(&dev->master_mutex);
3908c2ecf20Sopenharmony_ci	if (sv->drm_di_major != -1) {
3918c2ecf20Sopenharmony_ci		if (sv->drm_di_major != DRM_IF_MAJOR ||
3928c2ecf20Sopenharmony_ci		    sv->drm_di_minor < 0 || sv->drm_di_minor > DRM_IF_MINOR) {
3938c2ecf20Sopenharmony_ci			retcode = -EINVAL;
3948c2ecf20Sopenharmony_ci			goto done;
3958c2ecf20Sopenharmony_ci		}
3968c2ecf20Sopenharmony_ci		if_version = DRM_IF_VERSION(sv->drm_di_major,
3978c2ecf20Sopenharmony_ci					    sv->drm_di_minor);
3988c2ecf20Sopenharmony_ci		dev->if_version = max(if_version, dev->if_version);
3998c2ecf20Sopenharmony_ci		if (sv->drm_di_minor >= 1) {
4008c2ecf20Sopenharmony_ci			/*
4018c2ecf20Sopenharmony_ci			 * Version 1.1 includes tying of DRM to specific device
4028c2ecf20Sopenharmony_ci			 * Version 1.4 has proper PCI domain support
4038c2ecf20Sopenharmony_ci			 */
4048c2ecf20Sopenharmony_ci			retcode = drm_set_busid(dev, file_priv);
4058c2ecf20Sopenharmony_ci			if (retcode)
4068c2ecf20Sopenharmony_ci				goto done;
4078c2ecf20Sopenharmony_ci		}
4088c2ecf20Sopenharmony_ci	}
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	if (sv->drm_dd_major != -1) {
4118c2ecf20Sopenharmony_ci		if (sv->drm_dd_major != dev->driver->major ||
4128c2ecf20Sopenharmony_ci		    sv->drm_dd_minor < 0 || sv->drm_dd_minor >
4138c2ecf20Sopenharmony_ci		    dev->driver->minor) {
4148c2ecf20Sopenharmony_ci			retcode = -EINVAL;
4158c2ecf20Sopenharmony_ci			goto done;
4168c2ecf20Sopenharmony_ci		}
4178c2ecf20Sopenharmony_ci	}
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_cidone:
4208c2ecf20Sopenharmony_ci	sv->drm_di_major = DRM_IF_MAJOR;
4218c2ecf20Sopenharmony_ci	sv->drm_di_minor = DRM_IF_MINOR;
4228c2ecf20Sopenharmony_ci	sv->drm_dd_major = dev->driver->major;
4238c2ecf20Sopenharmony_ci	sv->drm_dd_minor = dev->driver->minor;
4248c2ecf20Sopenharmony_ci	mutex_unlock(&dev->master_mutex);
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	return retcode;
4278c2ecf20Sopenharmony_ci}
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci/**
4308c2ecf20Sopenharmony_ci * drm_noop - DRM no-op ioctl implemntation
4318c2ecf20Sopenharmony_ci * @dev: DRM device for the ioctl
4328c2ecf20Sopenharmony_ci * @data: data pointer for the ioctl
4338c2ecf20Sopenharmony_ci * @file_priv: DRM file for the ioctl call
4348c2ecf20Sopenharmony_ci *
4358c2ecf20Sopenharmony_ci * This no-op implementation for drm ioctls is useful for deprecated
4368c2ecf20Sopenharmony_ci * functionality where we can't return a failure code because existing userspace
4378c2ecf20Sopenharmony_ci * checks the result of the ioctl, but doesn't care about the action.
4388c2ecf20Sopenharmony_ci *
4398c2ecf20Sopenharmony_ci * Always returns successfully with 0.
4408c2ecf20Sopenharmony_ci */
4418c2ecf20Sopenharmony_ciint drm_noop(struct drm_device *dev, void *data,
4428c2ecf20Sopenharmony_ci	     struct drm_file *file_priv)
4438c2ecf20Sopenharmony_ci{
4448c2ecf20Sopenharmony_ci	DRM_DEBUG("\n");
4458c2ecf20Sopenharmony_ci	return 0;
4468c2ecf20Sopenharmony_ci}
4478c2ecf20Sopenharmony_ciEXPORT_SYMBOL(drm_noop);
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci/**
4508c2ecf20Sopenharmony_ci * drm_invalid_op - DRM invalid ioctl implemntation
4518c2ecf20Sopenharmony_ci * @dev: DRM device for the ioctl
4528c2ecf20Sopenharmony_ci * @data: data pointer for the ioctl
4538c2ecf20Sopenharmony_ci * @file_priv: DRM file for the ioctl call
4548c2ecf20Sopenharmony_ci *
4558c2ecf20Sopenharmony_ci * This no-op implementation for drm ioctls is useful for deprecated
4568c2ecf20Sopenharmony_ci * functionality where we really don't want to allow userspace to call the ioctl
4578c2ecf20Sopenharmony_ci * any more. This is the case for old ums interfaces for drivers that
4588c2ecf20Sopenharmony_ci * transitioned to kms gradually and so kept the old legacy tables around. This
4598c2ecf20Sopenharmony_ci * only applies to radeon and i915 kms drivers, other drivers shouldn't need to
4608c2ecf20Sopenharmony_ci * use this function.
4618c2ecf20Sopenharmony_ci *
4628c2ecf20Sopenharmony_ci * Always fails with a return value of -EINVAL.
4638c2ecf20Sopenharmony_ci */
4648c2ecf20Sopenharmony_ciint drm_invalid_op(struct drm_device *dev, void *data,
4658c2ecf20Sopenharmony_ci		   struct drm_file *file_priv)
4668c2ecf20Sopenharmony_ci{
4678c2ecf20Sopenharmony_ci	return -EINVAL;
4688c2ecf20Sopenharmony_ci}
4698c2ecf20Sopenharmony_ciEXPORT_SYMBOL(drm_invalid_op);
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci/*
4728c2ecf20Sopenharmony_ci * Copy and IOCTL return string to user space
4738c2ecf20Sopenharmony_ci */
4748c2ecf20Sopenharmony_cistatic int drm_copy_field(char __user *buf, size_t *buf_len, const char *value)
4758c2ecf20Sopenharmony_ci{
4768c2ecf20Sopenharmony_ci	size_t len;
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	/* don't attempt to copy a NULL pointer */
4798c2ecf20Sopenharmony_ci	if (WARN_ONCE(!value, "BUG: the value to copy was not set!")) {
4808c2ecf20Sopenharmony_ci		*buf_len = 0;
4818c2ecf20Sopenharmony_ci		return 0;
4828c2ecf20Sopenharmony_ci	}
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci	/* don't overflow userbuf */
4858c2ecf20Sopenharmony_ci	len = strlen(value);
4868c2ecf20Sopenharmony_ci	if (len > *buf_len)
4878c2ecf20Sopenharmony_ci		len = *buf_len;
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	/* let userspace know exact length of driver value (which could be
4908c2ecf20Sopenharmony_ci	 * larger than the userspace-supplied buffer) */
4918c2ecf20Sopenharmony_ci	*buf_len = strlen(value);
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	/* finally, try filling in the userbuf */
4948c2ecf20Sopenharmony_ci	if (len && buf)
4958c2ecf20Sopenharmony_ci		if (copy_to_user(buf, value, len))
4968c2ecf20Sopenharmony_ci			return -EFAULT;
4978c2ecf20Sopenharmony_ci	return 0;
4988c2ecf20Sopenharmony_ci}
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci/*
5018c2ecf20Sopenharmony_ci * Get version information
5028c2ecf20Sopenharmony_ci *
5038c2ecf20Sopenharmony_ci * \param inode device inode.
5048c2ecf20Sopenharmony_ci * \param filp file pointer.
5058c2ecf20Sopenharmony_ci * \param cmd command.
5068c2ecf20Sopenharmony_ci * \param arg user argument, pointing to a drm_version structure.
5078c2ecf20Sopenharmony_ci * \return zero on success or negative number on failure.
5088c2ecf20Sopenharmony_ci *
5098c2ecf20Sopenharmony_ci * Fills in the version information in \p arg.
5108c2ecf20Sopenharmony_ci */
5118c2ecf20Sopenharmony_ciint drm_version(struct drm_device *dev, void *data,
5128c2ecf20Sopenharmony_ci		       struct drm_file *file_priv)
5138c2ecf20Sopenharmony_ci{
5148c2ecf20Sopenharmony_ci	struct drm_version *version = data;
5158c2ecf20Sopenharmony_ci	int err;
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci	version->version_major = dev->driver->major;
5188c2ecf20Sopenharmony_ci	version->version_minor = dev->driver->minor;
5198c2ecf20Sopenharmony_ci	version->version_patchlevel = dev->driver->patchlevel;
5208c2ecf20Sopenharmony_ci	err = drm_copy_field(version->name, &version->name_len,
5218c2ecf20Sopenharmony_ci			dev->driver->name);
5228c2ecf20Sopenharmony_ci	if (!err)
5238c2ecf20Sopenharmony_ci		err = drm_copy_field(version->date, &version->date_len,
5248c2ecf20Sopenharmony_ci				dev->driver->date);
5258c2ecf20Sopenharmony_ci	if (!err)
5268c2ecf20Sopenharmony_ci		err = drm_copy_field(version->desc, &version->desc_len,
5278c2ecf20Sopenharmony_ci				dev->driver->desc);
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	return err;
5308c2ecf20Sopenharmony_ci}
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci/**
5338c2ecf20Sopenharmony_ci * drm_ioctl_permit - Check ioctl permissions against caller
5348c2ecf20Sopenharmony_ci *
5358c2ecf20Sopenharmony_ci * @flags: ioctl permission flags.
5368c2ecf20Sopenharmony_ci * @file_priv: Pointer to struct drm_file identifying the caller.
5378c2ecf20Sopenharmony_ci *
5388c2ecf20Sopenharmony_ci * Checks whether the caller is allowed to run an ioctl with the
5398c2ecf20Sopenharmony_ci * indicated permissions.
5408c2ecf20Sopenharmony_ci *
5418c2ecf20Sopenharmony_ci * Returns:
5428c2ecf20Sopenharmony_ci * Zero if allowed, -EACCES otherwise.
5438c2ecf20Sopenharmony_ci */
5448c2ecf20Sopenharmony_ciint drm_ioctl_permit(u32 flags, struct drm_file *file_priv)
5458c2ecf20Sopenharmony_ci{
5468c2ecf20Sopenharmony_ci	/* ROOT_ONLY is only for CAP_SYS_ADMIN */
5478c2ecf20Sopenharmony_ci	if (unlikely((flags & DRM_ROOT_ONLY) && !capable(CAP_SYS_ADMIN)))
5488c2ecf20Sopenharmony_ci		return -EACCES;
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci	/* AUTH is only for authenticated or render client */
5518c2ecf20Sopenharmony_ci	if (unlikely((flags & DRM_AUTH) && !drm_is_render_client(file_priv) &&
5528c2ecf20Sopenharmony_ci		     !file_priv->authenticated))
5538c2ecf20Sopenharmony_ci		return -EACCES;
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci	/* MASTER is only for master or control clients */
5568c2ecf20Sopenharmony_ci	if (unlikely((flags & DRM_MASTER) &&
5578c2ecf20Sopenharmony_ci		     !drm_is_current_master(file_priv)))
5588c2ecf20Sopenharmony_ci		return -EACCES;
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	/* Render clients must be explicitly allowed */
5618c2ecf20Sopenharmony_ci	if (unlikely(!(flags & DRM_RENDER_ALLOW) &&
5628c2ecf20Sopenharmony_ci		     drm_is_render_client(file_priv)))
5638c2ecf20Sopenharmony_ci		return -EACCES;
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci	return 0;
5668c2ecf20Sopenharmony_ci}
5678c2ecf20Sopenharmony_ciEXPORT_SYMBOL(drm_ioctl_permit);
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci#define DRM_IOCTL_DEF(ioctl, _func, _flags)	\
5708c2ecf20Sopenharmony_ci	[DRM_IOCTL_NR(ioctl)] = {		\
5718c2ecf20Sopenharmony_ci		.cmd = ioctl,			\
5728c2ecf20Sopenharmony_ci		.func = _func,			\
5738c2ecf20Sopenharmony_ci		.flags = _flags,		\
5748c2ecf20Sopenharmony_ci		.name = #ioctl			\
5758c2ecf20Sopenharmony_ci	}
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_DRM_LEGACY)
5788c2ecf20Sopenharmony_ci#define DRM_LEGACY_IOCTL_DEF(ioctl, _func, _flags)  DRM_IOCTL_DEF(ioctl, _func, _flags)
5798c2ecf20Sopenharmony_ci#else
5808c2ecf20Sopenharmony_ci#define DRM_LEGACY_IOCTL_DEF(ioctl, _func, _flags) DRM_IOCTL_DEF(ioctl, drm_invalid_op, _flags)
5818c2ecf20Sopenharmony_ci#endif
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci/* Ioctl table */
5848c2ecf20Sopenharmony_cistatic const struct drm_ioctl_desc drm_ioctls[] = {
5858c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_VERSION, drm_version, DRM_RENDER_ALLOW),
5868c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_GET_UNIQUE, drm_getunique, 0),
5878c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_GET_MAGIC, drm_getmagic, 0),
5888c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_IRQ_BUSID, drm_irq_by_busid, DRM_MASTER|DRM_ROOT_ONLY),
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_GET_MAP, drm_legacy_getmap_ioctl, 0),
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_GET_CLIENT, drm_getclient, 0),
5938c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_GET_STATS, drm_getstats, 0),
5948c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_GET_CAP, drm_getcap, DRM_RENDER_ALLOW),
5958c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_SET_CLIENT_CAP, drm_setclientcap, 0),
5968c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_SET_VERSION, drm_setversion, DRM_MASTER),
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_SET_UNIQUE, drm_invalid_op, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
5998c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_BLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6008c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_UNBLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6018c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_AUTH_MAGIC, drm_authmagic, DRM_MASTER),
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_ci	DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_ADD_MAP, drm_legacy_addmap_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6048c2ecf20Sopenharmony_ci	DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_RM_MAP, drm_legacy_rmmap_ioctl, DRM_AUTH),
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci	DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_SET_SAREA_CTX, drm_legacy_setsareactx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6078c2ecf20Sopenharmony_ci	DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_GET_SAREA_CTX, drm_legacy_getsareactx, DRM_AUTH),
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_SET_MASTER, drm_setmaster_ioctl, 0),
6108c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_DROP_MASTER, drm_dropmaster_ioctl, 0),
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci	DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_ADD_CTX, drm_legacy_addctx, DRM_AUTH|DRM_ROOT_ONLY),
6138c2ecf20Sopenharmony_ci	DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_RM_CTX, drm_legacy_rmctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6148c2ecf20Sopenharmony_ci	DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_MOD_CTX, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6158c2ecf20Sopenharmony_ci	DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_GET_CTX, drm_legacy_getctx, DRM_AUTH),
6168c2ecf20Sopenharmony_ci	DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_SWITCH_CTX, drm_legacy_switchctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6178c2ecf20Sopenharmony_ci	DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_NEW_CTX, drm_legacy_newctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6188c2ecf20Sopenharmony_ci	DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_RES_CTX, drm_legacy_resctx, DRM_AUTH),
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_ADD_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6218c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_RM_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ci	DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_LOCK, drm_legacy_lock, DRM_AUTH),
6248c2ecf20Sopenharmony_ci	DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_UNLOCK, drm_legacy_unlock, DRM_AUTH),
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_FINISH, drm_noop, DRM_AUTH),
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_ADD_BUFS, drm_legacy_addbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6298c2ecf20Sopenharmony_ci	DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_MARK_BUFS, drm_legacy_markbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6308c2ecf20Sopenharmony_ci	DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_INFO_BUFS, drm_legacy_infobufs, DRM_AUTH),
6318c2ecf20Sopenharmony_ci	DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_MAP_BUFS, drm_legacy_mapbufs, DRM_AUTH),
6328c2ecf20Sopenharmony_ci	DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_FREE_BUFS, drm_legacy_freebufs, DRM_AUTH),
6338c2ecf20Sopenharmony_ci	DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_DMA, drm_legacy_dma_ioctl, DRM_AUTH),
6348c2ecf20Sopenharmony_ci	DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_CONTROL, drm_legacy_irq_control, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_AGP)
6378c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_AGP_ACQUIRE, drm_agp_acquire_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6388c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_AGP_RELEASE, drm_agp_release_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6398c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_AGP_ENABLE, drm_agp_enable_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6408c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_AGP_INFO, drm_agp_info_ioctl, DRM_AUTH),
6418c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_AGP_ALLOC, drm_agp_alloc_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6428c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_AGP_FREE, drm_agp_free_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6438c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_AGP_BIND, drm_agp_bind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6448c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_AGP_UNBIND, drm_agp_unbind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6458c2ecf20Sopenharmony_ci#endif
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_ci	DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_SG_ALLOC, drm_legacy_sg_alloc, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6488c2ecf20Sopenharmony_ci	DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_SG_FREE, drm_legacy_sg_free, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_WAIT_VBLANK, drm_wait_vblank_ioctl, DRM_UNLOCKED),
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODESET_CTL, drm_legacy_modeset_ctl_ioctl, 0),
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_UPDATE_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_GEM_CLOSE, drm_gem_close_ioctl, DRM_RENDER_ALLOW),
6578c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_GEM_FLINK, drm_gem_flink_ioctl, DRM_AUTH),
6588c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_GEM_OPEN, drm_gem_open_ioctl, DRM_AUTH),
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETRESOURCES, drm_mode_getresources, 0),
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_PRIME_HANDLE_TO_FD, drm_prime_handle_to_fd_ioctl, DRM_RENDER_ALLOW),
6638c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_PRIME_FD_TO_HANDLE, drm_prime_fd_to_handle_ioctl, DRM_RENDER_ALLOW),
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANERESOURCES, drm_mode_getplane_res, 0),
6668c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCRTC, drm_mode_getcrtc, 0),
6678c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETCRTC, drm_mode_setcrtc, DRM_MASTER),
6688c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANE, drm_mode_getplane, 0),
6698c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETPLANE, drm_mode_setplane, DRM_MASTER),
6708c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_CURSOR, drm_mode_cursor_ioctl, DRM_MASTER),
6718c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETGAMMA, drm_mode_gamma_get_ioctl, 0),
6728c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETGAMMA, drm_mode_gamma_set_ioctl, DRM_MASTER),
6738c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETENCODER, drm_mode_getencoder, 0),
6748c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCONNECTOR, drm_mode_getconnector, 0),
6758c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_ATTACHMODE, drm_noop, DRM_MASTER),
6768c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_DETACHMODE, drm_noop, DRM_MASTER),
6778c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPROPERTY, drm_mode_getproperty_ioctl, 0),
6788c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETPROPERTY, drm_connector_property_set_ioctl, DRM_MASTER),
6798c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPROPBLOB, drm_mode_getblob_ioctl, 0),
6808c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETFB, drm_mode_getfb, 0),
6818c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETFB2, drm_mode_getfb2_ioctl, 0),
6828c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_ADDFB, drm_mode_addfb_ioctl, 0),
6838c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_ADDFB2, drm_mode_addfb2_ioctl, 0),
6848c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_RMFB, drm_mode_rmfb_ioctl, 0),
6858c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_PAGE_FLIP, drm_mode_page_flip_ioctl, DRM_MASTER),
6868c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_DIRTYFB, drm_mode_dirtyfb_ioctl, DRM_MASTER),
6878c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_CREATE_DUMB, drm_mode_create_dumb_ioctl, 0),
6888c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_MAP_DUMB, drm_mode_mmap_dumb_ioctl, 0),
6898c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_DESTROY_DUMB, drm_mode_destroy_dumb_ioctl, 0),
6908c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_OBJ_GETPROPERTIES, drm_mode_obj_get_properties_ioctl, 0),
6918c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_OBJ_SETPROPERTY, drm_mode_obj_set_property_ioctl, DRM_MASTER),
6928c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_CURSOR2, drm_mode_cursor2_ioctl, DRM_MASTER),
6938c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_ATOMIC, drm_mode_atomic_ioctl, DRM_MASTER),
6948c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_CREATEPROPBLOB, drm_mode_createblob_ioctl, 0),
6958c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_DESTROYPROPBLOB, drm_mode_destroyblob_ioctl, 0),
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_CREATE, drm_syncobj_create_ioctl,
6988c2ecf20Sopenharmony_ci		      DRM_RENDER_ALLOW),
6998c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_DESTROY, drm_syncobj_destroy_ioctl,
7008c2ecf20Sopenharmony_ci		      DRM_RENDER_ALLOW),
7018c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, drm_syncobj_handle_to_fd_ioctl,
7028c2ecf20Sopenharmony_ci		      DRM_RENDER_ALLOW),
7038c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, drm_syncobj_fd_to_handle_ioctl,
7048c2ecf20Sopenharmony_ci		      DRM_RENDER_ALLOW),
7058c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_TRANSFER, drm_syncobj_transfer_ioctl,
7068c2ecf20Sopenharmony_ci		      DRM_RENDER_ALLOW),
7078c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_WAIT, drm_syncobj_wait_ioctl,
7088c2ecf20Sopenharmony_ci		      DRM_RENDER_ALLOW),
7098c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_TIMELINE_WAIT, drm_syncobj_timeline_wait_ioctl,
7108c2ecf20Sopenharmony_ci		      DRM_RENDER_ALLOW),
7118c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_RESET, drm_syncobj_reset_ioctl,
7128c2ecf20Sopenharmony_ci		      DRM_RENDER_ALLOW),
7138c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_SIGNAL, drm_syncobj_signal_ioctl,
7148c2ecf20Sopenharmony_ci		      DRM_RENDER_ALLOW),
7158c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_TIMELINE_SIGNAL, drm_syncobj_timeline_signal_ioctl,
7168c2ecf20Sopenharmony_ci		      DRM_RENDER_ALLOW),
7178c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_QUERY, drm_syncobj_query_ioctl,
7188c2ecf20Sopenharmony_ci		      DRM_RENDER_ALLOW),
7198c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_CRTC_GET_SEQUENCE, drm_crtc_get_sequence_ioctl, 0),
7208c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_CRTC_QUEUE_SEQUENCE, drm_crtc_queue_sequence_ioctl, 0),
7218c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_CREATE_LEASE, drm_mode_create_lease_ioctl, DRM_MASTER),
7228c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_LIST_LESSEES, drm_mode_list_lessees_ioctl, DRM_MASTER),
7238c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GET_LEASE, drm_mode_get_lease_ioctl, DRM_MASTER),
7248c2ecf20Sopenharmony_ci	DRM_IOCTL_DEF(DRM_IOCTL_MODE_REVOKE_LEASE, drm_mode_revoke_lease_ioctl, DRM_MASTER),
7258c2ecf20Sopenharmony_ci};
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci#define DRM_CORE_IOCTL_COUNT	ARRAY_SIZE( drm_ioctls )
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci/**
7308c2ecf20Sopenharmony_ci * DOC: driver specific ioctls
7318c2ecf20Sopenharmony_ci *
7328c2ecf20Sopenharmony_ci * First things first, driver private IOCTLs should only be needed for drivers
7338c2ecf20Sopenharmony_ci * supporting rendering. Kernel modesetting is all standardized, and extended
7348c2ecf20Sopenharmony_ci * through properties. There are a few exceptions in some existing drivers,
7358c2ecf20Sopenharmony_ci * which define IOCTL for use by the display DRM master, but they all predate
7368c2ecf20Sopenharmony_ci * properties.
7378c2ecf20Sopenharmony_ci *
7388c2ecf20Sopenharmony_ci * Now if you do have a render driver you always have to support it through
7398c2ecf20Sopenharmony_ci * driver private properties. There's a few steps needed to wire all the things
7408c2ecf20Sopenharmony_ci * up.
7418c2ecf20Sopenharmony_ci *
7428c2ecf20Sopenharmony_ci * First you need to define the structure for your IOCTL in your driver private
7438c2ecf20Sopenharmony_ci * UAPI header in ``include/uapi/drm/my_driver_drm.h``::
7448c2ecf20Sopenharmony_ci *
7458c2ecf20Sopenharmony_ci *     struct my_driver_operation {
7468c2ecf20Sopenharmony_ci *             u32 some_thing;
7478c2ecf20Sopenharmony_ci *             u32 another_thing;
7488c2ecf20Sopenharmony_ci *     };
7498c2ecf20Sopenharmony_ci *
7508c2ecf20Sopenharmony_ci * Please make sure that you follow all the best practices from
7518c2ecf20Sopenharmony_ci * ``Documentation/process/botching-up-ioctls.rst``. Note that drm_ioctl()
7528c2ecf20Sopenharmony_ci * automatically zero-extends structures, hence make sure you can add more stuff
7538c2ecf20Sopenharmony_ci * at the end, i.e. don't put a variable sized array there.
7548c2ecf20Sopenharmony_ci *
7558c2ecf20Sopenharmony_ci * Then you need to define your IOCTL number, using one of DRM_IO(), DRM_IOR(),
7568c2ecf20Sopenharmony_ci * DRM_IOW() or DRM_IOWR(). It must start with the DRM_IOCTL\_ prefix::
7578c2ecf20Sopenharmony_ci *
7588c2ecf20Sopenharmony_ci *     ##define DRM_IOCTL_MY_DRIVER_OPERATION \
7598c2ecf20Sopenharmony_ci *         DRM_IOW(DRM_COMMAND_BASE, struct my_driver_operation)
7608c2ecf20Sopenharmony_ci *
7618c2ecf20Sopenharmony_ci * DRM driver private IOCTL must be in the range from DRM_COMMAND_BASE to
7628c2ecf20Sopenharmony_ci * DRM_COMMAND_END. Finally you need an array of &struct drm_ioctl_desc to wire
7638c2ecf20Sopenharmony_ci * up the handlers and set the access rights::
7648c2ecf20Sopenharmony_ci *
7658c2ecf20Sopenharmony_ci *     static const struct drm_ioctl_desc my_driver_ioctls[] = {
7668c2ecf20Sopenharmony_ci *         DRM_IOCTL_DEF_DRV(MY_DRIVER_OPERATION, my_driver_operation,
7678c2ecf20Sopenharmony_ci *                 DRM_AUTH|DRM_RENDER_ALLOW),
7688c2ecf20Sopenharmony_ci *     };
7698c2ecf20Sopenharmony_ci *
7708c2ecf20Sopenharmony_ci * And then assign this to the &drm_driver.ioctls field in your driver
7718c2ecf20Sopenharmony_ci * structure.
7728c2ecf20Sopenharmony_ci *
7738c2ecf20Sopenharmony_ci * See the separate chapter on :ref:`file operations<drm_driver_fops>` for how
7748c2ecf20Sopenharmony_ci * the driver-specific IOCTLs are wired up.
7758c2ecf20Sopenharmony_ci */
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_cilong drm_ioctl_kernel(struct file *file, drm_ioctl_t *func, void *kdata,
7788c2ecf20Sopenharmony_ci		      u32 flags)
7798c2ecf20Sopenharmony_ci{
7808c2ecf20Sopenharmony_ci	struct drm_file *file_priv = file->private_data;
7818c2ecf20Sopenharmony_ci	struct drm_device *dev = file_priv->minor->dev;
7828c2ecf20Sopenharmony_ci	int retcode;
7838c2ecf20Sopenharmony_ci
7848c2ecf20Sopenharmony_ci	if (drm_dev_is_unplugged(dev))
7858c2ecf20Sopenharmony_ci		return -ENODEV;
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_ci	retcode = drm_ioctl_permit(flags, file_priv);
7888c2ecf20Sopenharmony_ci	if (unlikely(retcode))
7898c2ecf20Sopenharmony_ci		return retcode;
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_ci	/* Enforce sane locking for modern driver ioctls. */
7928c2ecf20Sopenharmony_ci	if (likely(!drm_core_check_feature(dev, DRIVER_LEGACY)) ||
7938c2ecf20Sopenharmony_ci	    (flags & DRM_UNLOCKED))
7948c2ecf20Sopenharmony_ci		retcode = func(dev, kdata, file_priv);
7958c2ecf20Sopenharmony_ci	else {
7968c2ecf20Sopenharmony_ci		mutex_lock(&drm_global_mutex);
7978c2ecf20Sopenharmony_ci		retcode = func(dev, kdata, file_priv);
7988c2ecf20Sopenharmony_ci		mutex_unlock(&drm_global_mutex);
7998c2ecf20Sopenharmony_ci	}
8008c2ecf20Sopenharmony_ci	return retcode;
8018c2ecf20Sopenharmony_ci}
8028c2ecf20Sopenharmony_ciEXPORT_SYMBOL(drm_ioctl_kernel);
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci/**
8058c2ecf20Sopenharmony_ci * drm_ioctl - ioctl callback implementation for DRM drivers
8068c2ecf20Sopenharmony_ci * @filp: file this ioctl is called on
8078c2ecf20Sopenharmony_ci * @cmd: ioctl cmd number
8088c2ecf20Sopenharmony_ci * @arg: user argument
8098c2ecf20Sopenharmony_ci *
8108c2ecf20Sopenharmony_ci * Looks up the ioctl function in the DRM core and the driver dispatch table,
8118c2ecf20Sopenharmony_ci * stored in &drm_driver.ioctls. It checks for necessary permission by calling
8128c2ecf20Sopenharmony_ci * drm_ioctl_permit(), and dispatches to the respective function.
8138c2ecf20Sopenharmony_ci *
8148c2ecf20Sopenharmony_ci * Returns:
8158c2ecf20Sopenharmony_ci * Zero on success, negative error code on failure.
8168c2ecf20Sopenharmony_ci */
8178c2ecf20Sopenharmony_cilong drm_ioctl(struct file *filp,
8188c2ecf20Sopenharmony_ci	      unsigned int cmd, unsigned long arg)
8198c2ecf20Sopenharmony_ci{
8208c2ecf20Sopenharmony_ci	struct drm_file *file_priv = filp->private_data;
8218c2ecf20Sopenharmony_ci	struct drm_device *dev;
8228c2ecf20Sopenharmony_ci	const struct drm_ioctl_desc *ioctl = NULL;
8238c2ecf20Sopenharmony_ci	drm_ioctl_t *func;
8248c2ecf20Sopenharmony_ci	unsigned int nr = DRM_IOCTL_NR(cmd);
8258c2ecf20Sopenharmony_ci	int retcode = -EINVAL;
8268c2ecf20Sopenharmony_ci	char stack_kdata[128];
8278c2ecf20Sopenharmony_ci	char *kdata = NULL;
8288c2ecf20Sopenharmony_ci	unsigned int in_size, out_size, drv_size, ksize;
8298c2ecf20Sopenharmony_ci	bool is_driver_ioctl;
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci	dev = file_priv->minor->dev;
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_ci	if (drm_dev_is_unplugged(dev))
8348c2ecf20Sopenharmony_ci		return -ENODEV;
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ci       if (DRM_IOCTL_TYPE(cmd) != DRM_IOCTL_BASE)
8378c2ecf20Sopenharmony_ci               return -ENOTTY;
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci	is_driver_ioctl = nr >= DRM_COMMAND_BASE && nr < DRM_COMMAND_END;
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_ci	if (is_driver_ioctl) {
8428c2ecf20Sopenharmony_ci		/* driver ioctl */
8438c2ecf20Sopenharmony_ci		unsigned int index = nr - DRM_COMMAND_BASE;
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci		if (index >= dev->driver->num_ioctls)
8468c2ecf20Sopenharmony_ci			goto err_i1;
8478c2ecf20Sopenharmony_ci		index = array_index_nospec(index, dev->driver->num_ioctls);
8488c2ecf20Sopenharmony_ci		ioctl = &dev->driver->ioctls[index];
8498c2ecf20Sopenharmony_ci	} else {
8508c2ecf20Sopenharmony_ci		/* core ioctl */
8518c2ecf20Sopenharmony_ci		if (nr >= DRM_CORE_IOCTL_COUNT)
8528c2ecf20Sopenharmony_ci			goto err_i1;
8538c2ecf20Sopenharmony_ci		nr = array_index_nospec(nr, DRM_CORE_IOCTL_COUNT);
8548c2ecf20Sopenharmony_ci		ioctl = &drm_ioctls[nr];
8558c2ecf20Sopenharmony_ci	}
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_ci	drv_size = _IOC_SIZE(ioctl->cmd);
8588c2ecf20Sopenharmony_ci	out_size = in_size = _IOC_SIZE(cmd);
8598c2ecf20Sopenharmony_ci	if ((cmd & ioctl->cmd & IOC_IN) == 0)
8608c2ecf20Sopenharmony_ci		in_size = 0;
8618c2ecf20Sopenharmony_ci	if ((cmd & ioctl->cmd & IOC_OUT) == 0)
8628c2ecf20Sopenharmony_ci		out_size = 0;
8638c2ecf20Sopenharmony_ci	ksize = max(max(in_size, out_size), drv_size);
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ci	DRM_DEBUG("comm=\"%s\" pid=%d, dev=0x%lx, auth=%d, %s\n",
8668c2ecf20Sopenharmony_ci		  current->comm, task_pid_nr(current),
8678c2ecf20Sopenharmony_ci		  (long)old_encode_dev(file_priv->minor->kdev->devt),
8688c2ecf20Sopenharmony_ci		  file_priv->authenticated, ioctl->name);
8698c2ecf20Sopenharmony_ci
8708c2ecf20Sopenharmony_ci	/* Do not trust userspace, use our own definition */
8718c2ecf20Sopenharmony_ci	func = ioctl->func;
8728c2ecf20Sopenharmony_ci
8738c2ecf20Sopenharmony_ci	if (unlikely(!func)) {
8748c2ecf20Sopenharmony_ci		DRM_DEBUG("no function\n");
8758c2ecf20Sopenharmony_ci		retcode = -EINVAL;
8768c2ecf20Sopenharmony_ci		goto err_i1;
8778c2ecf20Sopenharmony_ci	}
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ci	if (ksize <= sizeof(stack_kdata)) {
8808c2ecf20Sopenharmony_ci		kdata = stack_kdata;
8818c2ecf20Sopenharmony_ci	} else {
8828c2ecf20Sopenharmony_ci		kdata = kmalloc(ksize, GFP_KERNEL);
8838c2ecf20Sopenharmony_ci		if (!kdata) {
8848c2ecf20Sopenharmony_ci			retcode = -ENOMEM;
8858c2ecf20Sopenharmony_ci			goto err_i1;
8868c2ecf20Sopenharmony_ci		}
8878c2ecf20Sopenharmony_ci	}
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_ci	if (copy_from_user(kdata, (void __user *)arg, in_size) != 0) {
8908c2ecf20Sopenharmony_ci		retcode = -EFAULT;
8918c2ecf20Sopenharmony_ci		goto err_i1;
8928c2ecf20Sopenharmony_ci	}
8938c2ecf20Sopenharmony_ci
8948c2ecf20Sopenharmony_ci	if (ksize > in_size)
8958c2ecf20Sopenharmony_ci		memset(kdata + in_size, 0, ksize - in_size);
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_ci	retcode = drm_ioctl_kernel(filp, func, kdata, ioctl->flags);
8988c2ecf20Sopenharmony_ci	if (copy_to_user((void __user *)arg, kdata, out_size) != 0)
8998c2ecf20Sopenharmony_ci		retcode = -EFAULT;
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_ci      err_i1:
9028c2ecf20Sopenharmony_ci	if (!ioctl)
9038c2ecf20Sopenharmony_ci		DRM_DEBUG("invalid ioctl: comm=\"%s\", pid=%d, dev=0x%lx, auth=%d, cmd=0x%02x, nr=0x%02x\n",
9048c2ecf20Sopenharmony_ci			  current->comm, task_pid_nr(current),
9058c2ecf20Sopenharmony_ci			  (long)old_encode_dev(file_priv->minor->kdev->devt),
9068c2ecf20Sopenharmony_ci			  file_priv->authenticated, cmd, nr);
9078c2ecf20Sopenharmony_ci
9088c2ecf20Sopenharmony_ci	if (kdata != stack_kdata)
9098c2ecf20Sopenharmony_ci		kfree(kdata);
9108c2ecf20Sopenharmony_ci	if (retcode)
9118c2ecf20Sopenharmony_ci		DRM_DEBUG("comm=\"%s\", pid=%d, ret=%d\n", current->comm,
9128c2ecf20Sopenharmony_ci			  task_pid_nr(current), retcode);
9138c2ecf20Sopenharmony_ci	return retcode;
9148c2ecf20Sopenharmony_ci}
9158c2ecf20Sopenharmony_ciEXPORT_SYMBOL(drm_ioctl);
9168c2ecf20Sopenharmony_ci
9178c2ecf20Sopenharmony_ci/**
9188c2ecf20Sopenharmony_ci * drm_ioctl_flags - Check for core ioctl and return ioctl permission flags
9198c2ecf20Sopenharmony_ci * @nr: ioctl number
9208c2ecf20Sopenharmony_ci * @flags: where to return the ioctl permission flags
9218c2ecf20Sopenharmony_ci *
9228c2ecf20Sopenharmony_ci * This ioctl is only used by the vmwgfx driver to augment the access checks
9238c2ecf20Sopenharmony_ci * done by the drm core and insofar a pretty decent layering violation. This
9248c2ecf20Sopenharmony_ci * shouldn't be used by any drivers.
9258c2ecf20Sopenharmony_ci *
9268c2ecf20Sopenharmony_ci * Returns:
9278c2ecf20Sopenharmony_ci * True if the @nr corresponds to a DRM core ioctl number, false otherwise.
9288c2ecf20Sopenharmony_ci */
9298c2ecf20Sopenharmony_cibool drm_ioctl_flags(unsigned int nr, unsigned int *flags)
9308c2ecf20Sopenharmony_ci{
9318c2ecf20Sopenharmony_ci	if (nr >= DRM_COMMAND_BASE && nr < DRM_COMMAND_END)
9328c2ecf20Sopenharmony_ci		return false;
9338c2ecf20Sopenharmony_ci
9348c2ecf20Sopenharmony_ci	if (nr >= DRM_CORE_IOCTL_COUNT)
9358c2ecf20Sopenharmony_ci		return false;
9368c2ecf20Sopenharmony_ci	nr = array_index_nospec(nr, DRM_CORE_IOCTL_COUNT);
9378c2ecf20Sopenharmony_ci
9388c2ecf20Sopenharmony_ci	*flags = drm_ioctls[nr].flags;
9398c2ecf20Sopenharmony_ci	return true;
9408c2ecf20Sopenharmony_ci}
9418c2ecf20Sopenharmony_ciEXPORT_SYMBOL(drm_ioctl_flags);
942