xref: /dflybsd-src/sys/dev/drm/drm_ioctl.c (revision 789731325bde747251c28a37e0a00ed4efb88c46)
16e29dde8SFrançois Tigeot /*
26e29dde8SFrançois Tigeot  * Created: Fri Jan  8 09:01:26 1999 by faith@valinux.com
36e29dde8SFrançois Tigeot  *
47f3c3d6fSHasso Tepper  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
57f3c3d6fSHasso Tepper  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
67f3c3d6fSHasso Tepper  * All Rights Reserved.
77f3c3d6fSHasso Tepper  *
824edb884SFrançois Tigeot  * Author Rickard E. (Rik) Faith <faith@valinux.com>
924edb884SFrançois Tigeot  * Author Gareth Hughes <gareth@valinux.com>
1024edb884SFrançois Tigeot  *
117f3c3d6fSHasso Tepper  * Permission is hereby granted, free of charge, to any person obtaining a
127f3c3d6fSHasso Tepper  * copy of this software and associated documentation files (the "Software"),
137f3c3d6fSHasso Tepper  * to deal in the Software without restriction, including without limitation
147f3c3d6fSHasso Tepper  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
157f3c3d6fSHasso Tepper  * and/or sell copies of the Software, and to permit persons to whom the
167f3c3d6fSHasso Tepper  * Software is furnished to do so, subject to the following conditions:
177f3c3d6fSHasso Tepper  *
187f3c3d6fSHasso Tepper  * The above copyright notice and this permission notice (including the next
197f3c3d6fSHasso Tepper  * paragraph) shall be included in all copies or substantial portions of the
207f3c3d6fSHasso Tepper  * Software.
217f3c3d6fSHasso Tepper  *
227f3c3d6fSHasso Tepper  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
237f3c3d6fSHasso Tepper  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
247f3c3d6fSHasso Tepper  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
257f3c3d6fSHasso Tepper  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
267f3c3d6fSHasso Tepper  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
277f3c3d6fSHasso Tepper  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
287f3c3d6fSHasso Tepper  * OTHER DEALINGS IN THE SOFTWARE.
297f3c3d6fSHasso Tepper  */
307f3c3d6fSHasso Tepper 
31a85cb24fSFrançois Tigeot #include <drm/drm_ioctl.h>
3218e26a6dSFrançois Tigeot #include <drm/drmP.h>
331dedbd3bSFrançois Tigeot #include <drm/drm_auth.h>
3424edb884SFrançois Tigeot #include "drm_legacy.h"
352c9916cdSFrançois Tigeot #include "drm_internal.h"
362c9916cdSFrançois Tigeot #include "drm_crtc_internal.h"
377f3c3d6fSHasso Tepper 
38c4bb8e01SFrançois Tigeot #include <linux/pci.h>
396e29dde8SFrançois Tigeot #include <linux/export.h>
406e29dde8SFrançois Tigeot 
411dedbd3bSFrançois Tigeot /**
421dedbd3bSFrançois Tigeot  * DOC: getunique and setversion story
431dedbd3bSFrançois Tigeot  *
441dedbd3bSFrançois Tigeot  * BEWARE THE DRAGONS! MIND THE TRAPDOORS!
451dedbd3bSFrançois Tigeot  *
461dedbd3bSFrançois Tigeot  * In an attempt to warn anyone else who's trying to figure out what's going
471dedbd3bSFrançois Tigeot  * on here, I'll try to summarize the story. First things first, let's clear up
481dedbd3bSFrançois Tigeot  * the names, because the kernel internals, libdrm and the ioctls are all named
491dedbd3bSFrançois Tigeot  * differently:
501dedbd3bSFrançois Tigeot  *
511dedbd3bSFrançois Tigeot  *  - GET_UNIQUE ioctl, implemented by drm_getunique is wrapped up in libdrm
521dedbd3bSFrançois Tigeot  *    through the drmGetBusid function.
531dedbd3bSFrançois Tigeot  *  - The libdrm drmSetBusid function is backed by the SET_UNIQUE ioctl. All
541dedbd3bSFrançois Tigeot  *    that code is nerved in the kernel with drm_invalid_op().
551dedbd3bSFrançois Tigeot  *  - The internal set_busid kernel functions and driver callbacks are
561dedbd3bSFrançois Tigeot  *    exclusively use by the SET_VERSION ioctl, because only drm 1.0 (which is
571dedbd3bSFrançois Tigeot  *    nerved) allowed userspace to set the busid through the above ioctl.
581dedbd3bSFrançois Tigeot  *  - Other ioctls and functions involved are named consistently.
591dedbd3bSFrançois Tigeot  *
601dedbd3bSFrançois Tigeot  * For anyone wondering what's the difference between drm 1.1 and 1.4: Correctly
611dedbd3bSFrançois Tigeot  * handling pci domains in the busid on ppc. Doing this correctly was only
621dedbd3bSFrançois Tigeot  * implemented in libdrm in 2010, hence can't be nerved yet. No one knows what's
631dedbd3bSFrançois Tigeot  * special with drm 1.2 and 1.3.
641dedbd3bSFrançois Tigeot  *
651dedbd3bSFrançois Tigeot  * Now the actual horror story of how device lookup in drm works. At large,
661dedbd3bSFrançois Tigeot  * there's 2 different ways, either by busid, or by device driver name.
671dedbd3bSFrançois Tigeot  *
681dedbd3bSFrançois Tigeot  * Opening by busid is fairly simple:
691dedbd3bSFrançois Tigeot  *
701dedbd3bSFrançois Tigeot  * 1. First call SET_VERSION to make sure pci domains are handled properly. As a
711dedbd3bSFrançois Tigeot  *    side-effect this fills out the unique name in the master structure.
721dedbd3bSFrançois Tigeot  * 2. Call GET_UNIQUE to read out the unique name from the master structure,
731dedbd3bSFrançois Tigeot  *    which matches the busid thanks to step 1. If it doesn't, proceed to try
741dedbd3bSFrançois Tigeot  *    the next device node.
751dedbd3bSFrançois Tigeot  *
761dedbd3bSFrançois Tigeot  * Opening by name is slightly different:
771dedbd3bSFrançois Tigeot  *
781dedbd3bSFrançois Tigeot  * 1. Directly call VERSION to get the version and to match against the driver
791dedbd3bSFrançois Tigeot  *    name returned by that ioctl. Note that SET_VERSION is not called, which
801dedbd3bSFrançois Tigeot  *    means the the unique name for the master node just opening is _not_ filled
811dedbd3bSFrançois Tigeot  *    out. This despite that with current drm device nodes are always bound to
821dedbd3bSFrançois Tigeot  *    one device, and can't be runtime assigned like with drm 1.0.
831dedbd3bSFrançois Tigeot  * 2. Match driver name. If it mismatches, proceed to the next device node.
841dedbd3bSFrançois Tigeot  * 3. Call GET_UNIQUE, and check whether the unique name has length zero (by
851dedbd3bSFrançois Tigeot  *    checking that the first byte in the string is 0). If that's not the case
861dedbd3bSFrançois Tigeot  *    libdrm skips and proceeds to the next device node. Probably this is just
871dedbd3bSFrançois Tigeot  *    copypasta from drm 1.0 times where a set unique name meant that the driver
881dedbd3bSFrançois Tigeot  *    was in use already, but that's just conjecture.
891dedbd3bSFrançois Tigeot  *
901dedbd3bSFrançois Tigeot  * Long story short: To keep the open by name logic working, GET_UNIQUE must
911dedbd3bSFrançois Tigeot  * _not_ return a unique string when SET_VERSION hasn't been called yet,
921dedbd3bSFrançois Tigeot  * otherwise libdrm breaks. Even when that unique string can't ever change, and
931dedbd3bSFrançois Tigeot  * is totally irrelevant for actually opening the device because runtime
941dedbd3bSFrançois Tigeot  * assignable device instances were only support in drm 1.0, which is long dead.
951dedbd3bSFrançois Tigeot  * But the libdrm code in drmOpenByName somehow survived, hence this can't be
961dedbd3bSFrançois Tigeot  * broken.
971dedbd3bSFrançois Tigeot  */
981dedbd3bSFrançois Tigeot 
99352ff8bdSFrançois Tigeot /*
1006e29dde8SFrançois Tigeot  * Get the bus id.
1016e29dde8SFrançois Tigeot  *
1026e29dde8SFrançois Tigeot  * \param inode device inode.
1036e29dde8SFrançois Tigeot  * \param file_priv DRM file private.
1046e29dde8SFrançois Tigeot  * \param cmd command.
1056e29dde8SFrançois Tigeot  * \param arg user argument, pointing to a drm_unique structure.
1066e29dde8SFrançois Tigeot  * \return zero on success or a negative number on failure.
1076e29dde8SFrançois Tigeot  *
1086e29dde8SFrançois Tigeot  * Copies the bus id from drm_device::unique into user space.
1097f3c3d6fSHasso Tepper  */
drm_getunique(struct drm_device * dev,void * data,struct drm_file * file_priv)1103f2dd94aSFrançois Tigeot int drm_getunique(struct drm_device *dev, void *data,
111b3705d71SHasso Tepper 		  struct drm_file *file_priv)
1127f3c3d6fSHasso Tepper {
113b3705d71SHasso Tepper 	struct drm_unique *u = data;
1143f2dd94aSFrançois Tigeot 	struct drm_master *master = file_priv->master;
1157f3c3d6fSHasso Tepper 
1163f2dd94aSFrançois Tigeot 	mutex_lock(&master->dev->master_mutex);
1173f2dd94aSFrançois Tigeot 	if (u->unique_len >= master->unique_len) {
1183f2dd94aSFrançois Tigeot 		if (copy_to_user(u->unique, master->unique, master->unique_len)) {
1193f2dd94aSFrançois Tigeot 			mutex_unlock(&master->dev->master_mutex);
120b922632fSImre Vadász 			return -EFAULT;
1217f3c3d6fSHasso Tepper 		}
122a85cb24fSFrançois Tigeot 	}
1233f2dd94aSFrançois Tigeot 	u->unique_len = master->unique_len;
1243f2dd94aSFrançois Tigeot 	mutex_unlock(&master->dev->master_mutex);
1257f3c3d6fSHasso Tepper 
1267f3c3d6fSHasso Tepper 	return 0;
1277f3c3d6fSHasso Tepper }
1287f3c3d6fSHasso Tepper 
1293f2dd94aSFrançois Tigeot static void
drm_unset_busid(struct drm_device * dev,struct drm_master * master)1303f2dd94aSFrançois Tigeot drm_unset_busid(struct drm_device *dev,
1313f2dd94aSFrançois Tigeot 		struct drm_master *master)
1327f3c3d6fSHasso Tepper {
1333f2dd94aSFrançois Tigeot 	kfree(master->unique);
1343f2dd94aSFrançois Tigeot 	master->unique = NULL;
1353f2dd94aSFrançois Tigeot 	master->unique_len = 0;
1367f3c3d6fSHasso Tepper }
1377f3c3d6fSHasso Tepper 
drm_set_busid(struct drm_device * dev,struct drm_file * file_priv)1383f2dd94aSFrançois Tigeot static int drm_set_busid(struct drm_device *dev, struct drm_file *file_priv)
1393f2dd94aSFrançois Tigeot {
1403f2dd94aSFrançois Tigeot 	struct drm_master *master = file_priv->master;
1413f2dd94aSFrançois Tigeot 	int ret;
1427f3c3d6fSHasso Tepper 
1433f2dd94aSFrançois Tigeot 	if (master->unique != NULL)
1443f2dd94aSFrançois Tigeot 		drm_unset_busid(dev, master);
1453f2dd94aSFrançois Tigeot 
1463f2dd94aSFrançois Tigeot 	if (dev->dev) {
1473f2dd94aSFrançois Tigeot 		ret = drm_pci_set_busid(dev, master);
1483f2dd94aSFrançois Tigeot 		if (ret) {
1493f2dd94aSFrançois Tigeot 			drm_unset_busid(dev, master);
1503f2dd94aSFrançois Tigeot 			return ret;
1513f2dd94aSFrançois Tigeot 		}
1523f2dd94aSFrançois Tigeot 	} else {
1533f2dd94aSFrançois Tigeot 		WARN_ON(!dev->unique);
1543f2dd94aSFrançois Tigeot 		master->unique = kstrdup(dev->unique, M_DRM);
1553f2dd94aSFrançois Tigeot 		if (master->unique)
1563f2dd94aSFrançois Tigeot 			master->unique_len = strlen(dev->unique);
1573f2dd94aSFrançois Tigeot 	}
1587f3c3d6fSHasso Tepper 
1597f3c3d6fSHasso Tepper 	return 0;
1607f3c3d6fSHasso Tepper }
1617f3c3d6fSHasso Tepper 
162352ff8bdSFrançois Tigeot /*
1636e29dde8SFrançois Tigeot  * Get client information.
1646e29dde8SFrançois Tigeot  *
1656e29dde8SFrançois Tigeot  * \param inode device inode.
1666e29dde8SFrançois Tigeot  * \param file_priv DRM file private.
1676e29dde8SFrançois Tigeot  * \param cmd command.
1686e29dde8SFrançois Tigeot  * \param arg user argument, pointing to a drm_client structure.
1696e29dde8SFrançois Tigeot  *
1706e29dde8SFrançois Tigeot  * \return zero on success or a negative number on failure.
1716e29dde8SFrançois Tigeot  *
1726e29dde8SFrançois Tigeot  * Searches for the client with the specified index and copies its information
1736e29dde8SFrançois Tigeot  * into userspace
1746e29dde8SFrançois Tigeot  */
drm_getclient(struct drm_device * dev,void * data,struct drm_file * file_priv)1753f2dd94aSFrançois Tigeot int drm_getclient(struct drm_device *dev, void *data,
176b3705d71SHasso Tepper 		  struct drm_file *file_priv)
1777f3c3d6fSHasso Tepper {
178b3705d71SHasso Tepper 	struct drm_client *client = data;
1797f3c3d6fSHasso Tepper 
1801dedbd3bSFrançois Tigeot 	/*
1811dedbd3bSFrançois Tigeot 	 * Hollowed-out getclient ioctl to keep some dead old drm tests/tools
1821dedbd3bSFrançois Tigeot 	 * not breaking completely. Userspace tools stop enumerating one they
1831dedbd3bSFrançois Tigeot 	 * get -EINVAL, hence this is the return value we need to hand back for
1841dedbd3bSFrançois Tigeot 	 * no clients tracked.
1851dedbd3bSFrançois Tigeot 	 *
1861dedbd3bSFrançois Tigeot 	 * Unfortunately some clients (*cough* libva *cough*) use this in a fun
1871dedbd3bSFrançois Tigeot 	 * attempt to figure out whether they're authenticated or not. Since
1881dedbd3bSFrançois Tigeot 	 * that's the only thing they care about, give it to the directly
1891dedbd3bSFrançois Tigeot 	 * instead of walking one giant list.
1901dedbd3bSFrançois Tigeot 	 */
1911dedbd3bSFrançois Tigeot 	if (client->idx == 0) {
1921dedbd3bSFrançois Tigeot 		client->auth = file_priv->authenticated;
1931dedbd3bSFrançois Tigeot 		client->pid = curproc->p_pid;
1941dedbd3bSFrançois Tigeot 		client->uid = -1;
1951dedbd3bSFrançois Tigeot 		client->magic = 0;
1961dedbd3bSFrançois Tigeot 		client->iocs = 0;
1971610a1a0SFrançois Tigeot 
1987f3c3d6fSHasso Tepper 		return 0;
1991dedbd3bSFrançois Tigeot 	} else {
200b922632fSImre Vadász 		return -EINVAL;
2017f3c3d6fSHasso Tepper 	}
2021dedbd3bSFrançois Tigeot }
2037f3c3d6fSHasso Tepper 
204352ff8bdSFrançois Tigeot /*
2056e29dde8SFrançois Tigeot  * Get statistics information.
2066e29dde8SFrançois Tigeot  *
2076e29dde8SFrançois Tigeot  * \param inode device inode.
2086e29dde8SFrançois Tigeot  * \param file_priv DRM file private.
2096e29dde8SFrançois Tigeot  * \param cmd command.
2106e29dde8SFrançois Tigeot  * \param arg user argument, pointing to a drm_stats structure.
2116e29dde8SFrançois Tigeot  *
2126e29dde8SFrançois Tigeot  * \return zero on success or a negative number on failure.
2136e29dde8SFrançois Tigeot  */
drm_getstats(struct drm_device * dev,void * data,struct drm_file * file_priv)2142c9916cdSFrançois Tigeot static int drm_getstats(struct drm_device *dev, void *data,
2152c9916cdSFrançois Tigeot 		 struct drm_file *file_priv)
2167f3c3d6fSHasso Tepper {
217b3705d71SHasso Tepper 	struct drm_stats *stats = data;
2187f3c3d6fSHasso Tepper 
219908aca88SFrançois Tigeot 	/* Clear stats to prevent userspace from eating its stack garbage. */
220908aca88SFrançois Tigeot 	memset(stats, 0, sizeof(*stats));
2217f3c3d6fSHasso Tepper 
2227f3c3d6fSHasso Tepper 	return 0;
2237f3c3d6fSHasso Tepper }
2247f3c3d6fSHasso Tepper 
225352ff8bdSFrançois Tigeot /*
2266e29dde8SFrançois Tigeot  * Get device/driver capabilities
2276e29dde8SFrançois Tigeot  */
drm_getcap(struct drm_device * dev,void * data,struct drm_file * file_priv)2282c9916cdSFrançois Tigeot static int drm_getcap(struct drm_device *dev, void *data, struct drm_file *file_priv)
2295718399fSFrançois Tigeot {
2305718399fSFrançois Tigeot 	struct drm_get_cap *req = data;
2311dedbd3bSFrançois Tigeot 	struct drm_crtc *crtc;
2325718399fSFrançois Tigeot 
2335718399fSFrançois Tigeot 	req->value = 0;
2344be47400SFrançois Tigeot 
2354be47400SFrançois Tigeot 	/* Only some caps make sense with UMS/render-only drivers. */
2364be47400SFrançois Tigeot 	switch (req->capability) {
2374be47400SFrançois Tigeot 	case DRM_CAP_TIMESTAMP_MONOTONIC:
2383f2dd94aSFrançois Tigeot 		req->value = 1;
2394be47400SFrançois Tigeot 		return 0;
2404be47400SFrançois Tigeot 	case DRM_CAP_PRIME:
2414be47400SFrançois Tigeot #ifdef __DragonFly__
2424be47400SFrançois Tigeot 		return -EINVAL;
2434be47400SFrançois Tigeot #else
2444be47400SFrançois Tigeot 		req->value |= dev->driver->prime_fd_to_handle ? DRM_PRIME_CAP_IMPORT : 0;
2454be47400SFrançois Tigeot 		req->value |= dev->driver->prime_handle_to_fd ? DRM_PRIME_CAP_EXPORT : 0;
2464be47400SFrançois Tigeot 		return 0;
2474be47400SFrançois Tigeot #endif
2484be47400SFrançois Tigeot 	}
2494be47400SFrançois Tigeot 
2504be47400SFrançois Tigeot 	/* Other caps only work with KMS drivers */
2514be47400SFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
2524be47400SFrançois Tigeot 		return -ENOTSUPP;
2534be47400SFrançois Tigeot 
2545718399fSFrançois Tigeot 	switch (req->capability) {
2555718399fSFrançois Tigeot 	case DRM_CAP_DUMB_BUFFER:
2565718399fSFrançois Tigeot 		if (dev->driver->dumb_create)
2575718399fSFrançois Tigeot 			req->value = 1;
2585718399fSFrançois Tigeot 		break;
2595718399fSFrançois Tigeot 	case DRM_CAP_VBLANK_HIGH_CRTC:
2605718399fSFrançois Tigeot 		req->value = 1;
2615718399fSFrançois Tigeot 		break;
2625718399fSFrançois Tigeot 	case DRM_CAP_DUMB_PREFERRED_DEPTH:
2635718399fSFrançois Tigeot 		req->value = dev->mode_config.preferred_depth;
2645718399fSFrançois Tigeot 		break;
2655718399fSFrançois Tigeot 	case DRM_CAP_DUMB_PREFER_SHADOW:
2665718399fSFrançois Tigeot 		req->value = dev->mode_config.prefer_shadow;
2675718399fSFrançois Tigeot 		break;
2682c9916cdSFrançois Tigeot 	case DRM_CAP_ASYNC_PAGE_FLIP:
2692c9916cdSFrançois Tigeot 		req->value = dev->mode_config.async_page_flip;
2702c9916cdSFrançois Tigeot 		break;
2711dedbd3bSFrançois Tigeot 	case DRM_CAP_PAGE_FLIP_TARGET:
2721dedbd3bSFrançois Tigeot 		req->value = 1;
2731dedbd3bSFrançois Tigeot 		drm_for_each_crtc(crtc, dev) {
2741dedbd3bSFrançois Tigeot 			if (!crtc->funcs->page_flip_target)
2751dedbd3bSFrançois Tigeot 				req->value = 0;
2761dedbd3bSFrançois Tigeot 		}
2771dedbd3bSFrançois Tigeot 		break;
278f30c41c3SImre Vadasz 	case DRM_CAP_CURSOR_WIDTH:
279ce7d1dc9SImre Vadasz 		if (dev->mode_config.cursor_width)
280f30c41c3SImre Vadasz 			req->value = dev->mode_config.cursor_width;
281f30c41c3SImre Vadasz 		else
282f30c41c3SImre Vadasz 			req->value = 64;
283f30c41c3SImre Vadasz 		break;
284f30c41c3SImre Vadasz 	case DRM_CAP_CURSOR_HEIGHT:
285ce7d1dc9SImre Vadasz 		if (dev->mode_config.cursor_height)
286f30c41c3SImre Vadasz 			req->value = dev->mode_config.cursor_height;
287f30c41c3SImre Vadasz 		else
288f30c41c3SImre Vadasz 			req->value = 64;
289f30c41c3SImre Vadasz 		break;
290908aca88SFrançois Tigeot 	case DRM_CAP_ADDFB2_MODIFIERS:
291908aca88SFrançois Tigeot 		req->value = dev->mode_config.allow_fb_modifiers;
292908aca88SFrançois Tigeot 		break;
2932949ab59SPeeter Must 	case DRM_CAP_CRTC_IN_VBLANK_EVENT:
2942949ab59SPeeter Must 		req->value = 1;
2952949ab59SPeeter Must 		break;
2965718399fSFrançois Tigeot 	default:
297b922632fSImre Vadász 		return -EINVAL;
2985718399fSFrançois Tigeot 	}
2995718399fSFrançois Tigeot 	return 0;
3005718399fSFrançois Tigeot }
3015718399fSFrançois Tigeot 
302352ff8bdSFrançois Tigeot /*
3039edbd4a0SFrançois Tigeot  * Set device/driver capabilities
3049edbd4a0SFrançois Tigeot  */
3052c9916cdSFrançois Tigeot static int
drm_setclientcap(struct drm_device * dev,void * data,struct drm_file * file_priv)3069edbd4a0SFrançois Tigeot drm_setclientcap(struct drm_device *dev, void *data, struct drm_file *file_priv)
3079edbd4a0SFrançois Tigeot {
3089edbd4a0SFrançois Tigeot 	struct drm_set_client_cap *req = data;
3099edbd4a0SFrançois Tigeot 
3109edbd4a0SFrançois Tigeot 	switch (req->capability) {
3119edbd4a0SFrançois Tigeot 	case DRM_CLIENT_CAP_STEREO_3D:
3129edbd4a0SFrançois Tigeot 		if (req->value > 1)
3139edbd4a0SFrançois Tigeot 			return -EINVAL;
3149edbd4a0SFrançois Tigeot 		file_priv->stereo_allowed = req->value;
3159edbd4a0SFrançois Tigeot 		break;
316ba55f2f5SFrançois Tigeot 	case DRM_CLIENT_CAP_UNIVERSAL_PLANES:
317ba55f2f5SFrançois Tigeot 		if (req->value > 1)
318ba55f2f5SFrançois Tigeot 			return -EINVAL;
319ba55f2f5SFrançois Tigeot 		file_priv->universal_planes = req->value;
320ba55f2f5SFrançois Tigeot 		break;
32119c468b4SFrançois Tigeot 	case DRM_CLIENT_CAP_ATOMIC:
32219c468b4SFrançois Tigeot 		if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
32319c468b4SFrançois Tigeot 			return -EINVAL;
32419c468b4SFrançois Tigeot 		if (req->value > 1)
32519c468b4SFrançois Tigeot 			return -EINVAL;
32619c468b4SFrançois Tigeot 		file_priv->atomic = req->value;
32719c468b4SFrançois Tigeot 		file_priv->universal_planes = req->value;
32819c468b4SFrançois Tigeot 		break;
3299edbd4a0SFrançois Tigeot 	default:
3309edbd4a0SFrançois Tigeot 		return -EINVAL;
3319edbd4a0SFrançois Tigeot 	}
3329edbd4a0SFrançois Tigeot 
3339edbd4a0SFrançois Tigeot 	return 0;
3349edbd4a0SFrançois Tigeot }
3359edbd4a0SFrançois Tigeot 
336352ff8bdSFrançois Tigeot /*
3376e29dde8SFrançois Tigeot  * Setversion ioctl.
3386e29dde8SFrançois Tigeot  *
3396e29dde8SFrançois Tigeot  * \param inode device inode.
3406e29dde8SFrançois Tigeot  * \param file_priv DRM file private.
3416e29dde8SFrançois Tigeot  * \param cmd command.
3426e29dde8SFrançois Tigeot  * \param arg user argument, pointing to a drm_lock structure.
3436e29dde8SFrançois Tigeot  * \return zero on success or negative number on failure.
3446e29dde8SFrançois Tigeot  *
3456e29dde8SFrançois Tigeot  * Sets the requested interface version
3466e29dde8SFrançois Tigeot  */
drm_setversion(struct drm_device * dev,void * data,struct drm_file * file_priv)34704b45e6fSzrj static int drm_setversion(struct drm_device *dev, void *data, struct drm_file *file_priv)
3487f3c3d6fSHasso Tepper {
349b3705d71SHasso Tepper 	struct drm_set_version *sv = data;
3506e29dde8SFrançois Tigeot 	int if_version, retcode = 0;
3517f3c3d6fSHasso Tepper 
352a85cb24fSFrançois Tigeot 	mutex_lock(&dev->master_mutex);
3531dedbd3bSFrançois Tigeot 	if (sv->drm_di_major != -1) {
3541dedbd3bSFrançois Tigeot 		if (sv->drm_di_major != DRM_IF_MAJOR ||
3551dedbd3bSFrançois Tigeot 		    sv->drm_di_minor < 0 || sv->drm_di_minor > DRM_IF_MINOR) {
3561dedbd3bSFrançois Tigeot 			retcode = -EINVAL;
3571dedbd3bSFrançois Tigeot 			goto done;
3587f3c3d6fSHasso Tepper 		}
3591dedbd3bSFrançois Tigeot 		if_version = DRM_IF_VERSION(sv->drm_di_major,
3601dedbd3bSFrançois Tigeot 					    sv->drm_di_minor);
3611dedbd3bSFrançois Tigeot 		dev->if_version = max(if_version, dev->if_version);
3621dedbd3bSFrançois Tigeot 		if (sv->drm_di_minor >= 1) {
3637f3c3d6fSHasso Tepper 			/*
3647f3c3d6fSHasso Tepper 			 * Version 1.1 includes tying of DRM to specific device
3656e29dde8SFrançois Tigeot 			 * Version 1.4 has proper PCI domain support
3667f3c3d6fSHasso Tepper 			 */
3676e29dde8SFrançois Tigeot 			retcode = drm_set_busid(dev, file_priv);
3686e29dde8SFrançois Tigeot 			if (retcode)
3691dedbd3bSFrançois Tigeot 				goto done;
3701dedbd3bSFrançois Tigeot 		}
3711dedbd3bSFrançois Tigeot 	}
3721dedbd3bSFrançois Tigeot 
3731dedbd3bSFrançois Tigeot 	if (sv->drm_dd_major != -1) {
3741dedbd3bSFrançois Tigeot 		if (sv->drm_dd_major != dev->driver->major ||
3751dedbd3bSFrançois Tigeot 		    sv->drm_dd_minor < 0 || sv->drm_dd_minor >
3761dedbd3bSFrançois Tigeot 		    dev->driver->minor) {
3771dedbd3bSFrançois Tigeot 			retcode = -EINVAL;
3781dedbd3bSFrançois Tigeot 			goto done;
3791dedbd3bSFrançois Tigeot 		}
3801dedbd3bSFrançois Tigeot 	}
3811dedbd3bSFrançois Tigeot 
3821dedbd3bSFrançois Tigeot done:
3831dedbd3bSFrançois Tigeot 	sv->drm_di_major = DRM_IF_MAJOR;
3841dedbd3bSFrançois Tigeot 	sv->drm_di_minor = DRM_IF_MINOR;
3851dedbd3bSFrançois Tigeot 	sv->drm_dd_major = dev->driver->major;
3861dedbd3bSFrançois Tigeot 	sv->drm_dd_minor = dev->driver->minor;
387a85cb24fSFrançois Tigeot 	mutex_unlock(&dev->master_mutex);
3881dedbd3bSFrançois Tigeot 
3896e29dde8SFrançois Tigeot 	return retcode;
3907f3c3d6fSHasso Tepper }
3917f3c3d6fSHasso Tepper 
392352ff8bdSFrançois Tigeot /**
393352ff8bdSFrançois Tigeot  * drm_noop - DRM no-op ioctl implemntation
394352ff8bdSFrançois Tigeot  * @dev: DRM device for the ioctl
395352ff8bdSFrançois Tigeot  * @data: data pointer for the ioctl
396352ff8bdSFrançois Tigeot  * @file_priv: DRM file for the ioctl call
397352ff8bdSFrançois Tigeot  *
398352ff8bdSFrançois Tigeot  * This no-op implementation for drm ioctls is useful for deprecated
399352ff8bdSFrançois Tigeot  * functionality where we can't return a failure code because existing userspace
400352ff8bdSFrançois Tigeot  * checks the result of the ioctl, but doesn't care about the action.
401352ff8bdSFrançois Tigeot  *
402352ff8bdSFrançois Tigeot  * Always returns successfully with 0.
403352ff8bdSFrançois Tigeot  */
drm_noop(struct drm_device * dev,void * data,struct drm_file * file_priv)4046e29dde8SFrançois Tigeot int drm_noop(struct drm_device *dev, void *data,
4056e29dde8SFrançois Tigeot 	     struct drm_file *file_priv)
4067f3c3d6fSHasso Tepper {
4077f3c3d6fSHasso Tepper 	DRM_DEBUG("\n");
4087f3c3d6fSHasso Tepper 	return 0;
4097f3c3d6fSHasso Tepper }
4106e29dde8SFrançois Tigeot EXPORT_SYMBOL(drm_noop);
41124edb884SFrançois Tigeot 
41224edb884SFrançois Tigeot /**
413352ff8bdSFrançois Tigeot  * drm_invalid_op - DRM invalid ioctl implemntation
414352ff8bdSFrançois Tigeot  * @dev: DRM device for the ioctl
415352ff8bdSFrançois Tigeot  * @data: data pointer for the ioctl
416352ff8bdSFrançois Tigeot  * @file_priv: DRM file for the ioctl call
417352ff8bdSFrançois Tigeot  *
418352ff8bdSFrançois Tigeot  * This no-op implementation for drm ioctls is useful for deprecated
419352ff8bdSFrançois Tigeot  * functionality where we really don't want to allow userspace to call the ioctl
420352ff8bdSFrançois Tigeot  * any more. This is the case for old ums interfaces for drivers that
421352ff8bdSFrançois Tigeot  * transitioned to kms gradually and so kept the old legacy tables around. This
422352ff8bdSFrançois Tigeot  * only applies to radeon and i915 kms drivers, other drivers shouldn't need to
423352ff8bdSFrançois Tigeot  * use this function.
424352ff8bdSFrançois Tigeot  *
425352ff8bdSFrançois Tigeot  * Always fails with a return value of -EINVAL.
426352ff8bdSFrançois Tigeot  */
drm_invalid_op(struct drm_device * dev,void * data,struct drm_file * file_priv)427352ff8bdSFrançois Tigeot int drm_invalid_op(struct drm_device *dev, void *data,
428352ff8bdSFrançois Tigeot 		   struct drm_file *file_priv)
429352ff8bdSFrançois Tigeot {
430352ff8bdSFrançois Tigeot 	return -EINVAL;
431352ff8bdSFrançois Tigeot }
432352ff8bdSFrançois Tigeot EXPORT_SYMBOL(drm_invalid_op);
433352ff8bdSFrançois Tigeot 
434352ff8bdSFrançois Tigeot /*
43524edb884SFrançois Tigeot  * Copy and IOCTL return string to user space
43624edb884SFrançois Tigeot  */
drm_copy_field(char __user * buf,size_t * buf_len,const char * value)43724edb884SFrançois Tigeot static int drm_copy_field(char __user *buf, size_t *buf_len, const char *value)
43824edb884SFrançois Tigeot {
43924edb884SFrançois Tigeot 	int len;
44024edb884SFrançois Tigeot 
44124edb884SFrançois Tigeot 	/* don't overflow userbuf */
44224edb884SFrançois Tigeot 	len = strlen(value);
44324edb884SFrançois Tigeot 	if (len > *buf_len)
44424edb884SFrançois Tigeot 		len = *buf_len;
44524edb884SFrançois Tigeot 
44624edb884SFrançois Tigeot 	/* let userspace know exact length of driver value (which could be
44724edb884SFrançois Tigeot 	 * larger than the userspace-supplied buffer) */
44824edb884SFrançois Tigeot 	*buf_len = strlen(value);
44924edb884SFrançois Tigeot 
45024edb884SFrançois Tigeot 	/* finally, try filling in the userbuf */
45124edb884SFrançois Tigeot 	if (len && buf)
45224edb884SFrançois Tigeot 		if (copy_to_user(buf, value, len))
45324edb884SFrançois Tigeot 			return -EFAULT;
45424edb884SFrançois Tigeot 	return 0;
45524edb884SFrançois Tigeot }
45624edb884SFrançois Tigeot 
457352ff8bdSFrançois Tigeot /*
45824edb884SFrançois Tigeot  * Get version information
45924edb884SFrançois Tigeot  *
46024edb884SFrançois Tigeot  * \param inode device inode.
46124edb884SFrançois Tigeot  * \param filp file pointer.
46224edb884SFrançois Tigeot  * \param cmd command.
46324edb884SFrançois Tigeot  * \param arg user argument, pointing to a drm_version structure.
46424edb884SFrançois Tigeot  * \return zero on success or negative number on failure.
46524edb884SFrançois Tigeot  *
46624edb884SFrançois Tigeot  * Fills in the version information in \p arg.
46724edb884SFrançois Tigeot  */
drm_version(struct drm_device * dev,void * data,struct drm_file * file_priv)4683f2dd94aSFrançois Tigeot int drm_version(struct drm_device *dev, void *data,
46924edb884SFrançois Tigeot 		       struct drm_file *file_priv)
47024edb884SFrançois Tigeot {
471*78973132SSergey Zigachev #if 0
47245a165c7SMatthew Dillon 	static int drm_version_initial;
473*78973132SSergey Zigachev #endif
47424edb884SFrançois Tigeot 	struct drm_version *version = data;
47524edb884SFrançois Tigeot 	int err;
47624edb884SFrançois Tigeot 
477*78973132SSergey Zigachev #if 0
47845a165c7SMatthew Dillon 	if (drm_version_initial == 0) {
47945a165c7SMatthew Dillon 		int dummy;
48045a165c7SMatthew Dillon 
48145a165c7SMatthew Dillon 		++drm_version_initial;
48245a165c7SMatthew Dillon 		tsleep(&dummy, 0, "SDELAY", hz*2);
48345a165c7SMatthew Dillon 	}
484*78973132SSergey Zigachev #endif
48545a165c7SMatthew Dillon 
48624edb884SFrançois Tigeot 	version->version_major = dev->driver->major;
48724edb884SFrançois Tigeot 	version->version_minor = dev->driver->minor;
48824edb884SFrançois Tigeot 	version->version_patchlevel = dev->driver->patchlevel;
48924edb884SFrançois Tigeot 	err = drm_copy_field(version->name, &version->name_len,
49024edb884SFrançois Tigeot 			dev->driver->name);
49124edb884SFrançois Tigeot 	if (!err)
49224edb884SFrançois Tigeot 		err = drm_copy_field(version->date, &version->date_len,
49324edb884SFrançois Tigeot 				dev->driver->date);
49424edb884SFrançois Tigeot 	if (!err)
49524edb884SFrançois Tigeot 		err = drm_copy_field(version->desc, &version->desc_len,
49624edb884SFrançois Tigeot 				dev->driver->desc);
49724edb884SFrançois Tigeot 
49824edb884SFrançois Tigeot 	return err;
49924edb884SFrançois Tigeot }
50024edb884SFrançois Tigeot 
501a85cb24fSFrançois Tigeot /**
50224edb884SFrançois Tigeot  * drm_ioctl_permit - Check ioctl permissions against caller
50324edb884SFrançois Tigeot  *
50424edb884SFrançois Tigeot  * @flags: ioctl permission flags.
50524edb884SFrançois Tigeot  * @file_priv: Pointer to struct drm_file identifying the caller.
50624edb884SFrançois Tigeot  *
50724edb884SFrançois Tigeot  * Checks whether the caller is allowed to run an ioctl with the
508a85cb24fSFrançois Tigeot  * indicated permissions.
509a85cb24fSFrançois Tigeot  *
510a85cb24fSFrançois Tigeot  * Returns:
511a85cb24fSFrançois Tigeot  * Zero if allowed, -EACCES otherwise.
51224edb884SFrançois Tigeot  */
drm_ioctl_permit(u32 flags,struct drm_file * file_priv)513a05eeebfSFrançois Tigeot int drm_ioctl_permit(u32 flags, struct drm_file *file_priv)
51424edb884SFrançois Tigeot {
51524edb884SFrançois Tigeot 	/* ROOT_ONLY is only for CAP_SYS_ADMIN */
51624edb884SFrançois Tigeot 	if (unlikely((flags & DRM_ROOT_ONLY) && !capable(CAP_SYS_ADMIN)))
51724edb884SFrançois Tigeot 		return -EACCES;
51824edb884SFrançois Tigeot 
51924edb884SFrançois Tigeot 	/* AUTH is only for authenticated or render client */
5203f2dd94aSFrançois Tigeot 	if (unlikely((flags & DRM_AUTH) && !drm_is_render_client(file_priv) &&
5213f2dd94aSFrançois Tigeot 		     !file_priv->authenticated))
52224edb884SFrançois Tigeot 		return -EACCES;
52324edb884SFrançois Tigeot 
52424edb884SFrançois Tigeot 	/* MASTER is only for master or control clients */
5251dedbd3bSFrançois Tigeot 	if (unlikely((flags & DRM_MASTER) &&
5261dedbd3bSFrançois Tigeot 		     !drm_is_current_master(file_priv) &&
5271dedbd3bSFrançois Tigeot 		     !drm_is_control_client(file_priv)))
52824edb884SFrançois Tigeot 		return -EACCES;
52924edb884SFrançois Tigeot 
5303f2dd94aSFrançois Tigeot 	/* Control clients must be explicitly allowed */
5313f2dd94aSFrançois Tigeot 	if (unlikely(!(flags & DRM_CONTROL_ALLOW) &&
5323f2dd94aSFrançois Tigeot 		     drm_is_control_client(file_priv)))
5333f2dd94aSFrançois Tigeot 		return -EACCES;
5343f2dd94aSFrançois Tigeot 
5353f2dd94aSFrançois Tigeot 	/* Render clients must be explicitly allowed */
5363f2dd94aSFrançois Tigeot 	if (unlikely(!(flags & DRM_RENDER_ALLOW) &&
5373f2dd94aSFrançois Tigeot 		     drm_is_render_client(file_priv)))
5383f2dd94aSFrançois Tigeot 		return -EACCES;
5393f2dd94aSFrançois Tigeot 
54024edb884SFrançois Tigeot 	return 0;
54124edb884SFrançois Tigeot }
542a05eeebfSFrançois Tigeot EXPORT_SYMBOL(drm_ioctl_permit);
54324edb884SFrançois Tigeot 
5442c9916cdSFrançois Tigeot #define DRM_IOCTL_DEF(ioctl, _func, _flags)	\
5457387c767Szrj 	[DRM_IOCTL_NR(ioctl)] = {		\
5467387c767Szrj 		.cmd = ioctl,			\
5477387c767Szrj 		.func = _func,			\
5487387c767Szrj 		.flags = _flags,		\
5497387c767Szrj 		.name = #ioctl			\
5507387c767Szrj 	}
5512c9916cdSFrançois Tigeot 
552352ff8bdSFrançois Tigeot /* Ioctl table */
5532c9916cdSFrançois Tigeot static const struct drm_ioctl_desc drm_ioctls[] = {
554a05eeebfSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_VERSION, drm_version,
555a05eeebfSFrançois Tigeot 		      DRM_UNLOCKED|DRM_RENDER_ALLOW|DRM_CONTROL_ALLOW),
556a85cb24fSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_GET_UNIQUE, drm_getunique, DRM_UNLOCKED),
5571dedbd3bSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_GET_MAGIC, drm_getmagic, DRM_UNLOCKED),
5582c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_IRQ_BUSID, drm_irq_by_busid, DRM_MASTER|DRM_ROOT_ONLY),
5598621f407SFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_GET_MAP, drm_legacy_getmap_ioctl, DRM_UNLOCKED),
5602c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_GET_CLIENT, drm_getclient, DRM_UNLOCKED),
5612c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_GET_STATS, drm_getstats, DRM_UNLOCKED),
5622c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_GET_CAP, drm_getcap, DRM_UNLOCKED|DRM_RENDER_ALLOW),
563a85cb24fSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_SET_CLIENT_CAP, drm_setclientcap, DRM_UNLOCKED),
564a85cb24fSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_SET_VERSION, drm_setversion, DRM_UNLOCKED | DRM_MASTER),
5652c9916cdSFrançois Tigeot 
5661dedbd3bSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_SET_UNIQUE, drm_invalid_op, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
5672c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_BLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
5682c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_UNBLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
5691dedbd3bSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_AUTH_MAGIC, drm_authmagic, DRM_AUTH|DRM_UNLOCKED|DRM_MASTER),
5702c9916cdSFrançois Tigeot 
5718a697a22SFrançois Tigeot #ifdef __DragonFly__
5728a697a22SFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_GET_PCIINFO, drm_getpciinfo, DRM_UNLOCKED|DRM_RENDER_ALLOW),
5738a697a22SFrançois Tigeot #else
5742c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_ADD_MAP, drm_legacy_addmap_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
5758a697a22SFrançois Tigeot #endif
5762c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_RM_MAP, drm_legacy_rmmap_ioctl, DRM_AUTH),
5772c9916cdSFrançois Tigeot 
5782c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_SET_SAREA_CTX, drm_legacy_setsareactx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
5792c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_GET_SAREA_CTX, drm_legacy_getsareactx, DRM_AUTH),
5802c9916cdSFrançois Tigeot 
5811dedbd3bSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_SET_MASTER, drm_setmaster_ioctl, DRM_UNLOCKED|DRM_ROOT_ONLY),
5821dedbd3bSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_DROP_MASTER, drm_dropmaster_ioctl, DRM_UNLOCKED|DRM_ROOT_ONLY),
5832c9916cdSFrançois Tigeot 
5842c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_ADD_CTX, drm_legacy_addctx, DRM_AUTH|DRM_ROOT_ONLY),
5852c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_RM_CTX, drm_legacy_rmctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
5862c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MOD_CTX, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
5872c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_GET_CTX, drm_legacy_getctx, DRM_AUTH),
5882c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_SWITCH_CTX, drm_legacy_switchctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
5892c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_NEW_CTX, drm_legacy_newctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
5902c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_RES_CTX, drm_legacy_resctx, DRM_AUTH),
5912c9916cdSFrançois Tigeot 
5922c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_ADD_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
5932c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_RM_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
5942c9916cdSFrançois Tigeot 
595a05eeebfSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_LOCK, drm_legacy_lock, DRM_AUTH),
596a05eeebfSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_UNLOCK, drm_legacy_unlock, DRM_AUTH),
5972c9916cdSFrançois Tigeot 
5982c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_FINISH, drm_noop, DRM_AUTH),
5992c9916cdSFrançois Tigeot 
6002c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_ADD_BUFS, drm_legacy_addbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6012c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MARK_BUFS, drm_legacy_markbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6022c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_INFO_BUFS, drm_legacy_infobufs, DRM_AUTH),
6032c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MAP_BUFS, drm_legacy_mapbufs, DRM_AUTH),
6042c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_FREE_BUFS, drm_legacy_freebufs, DRM_AUTH),
6052c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_DMA, drm_legacy_dma_ioctl, DRM_AUTH),
6062c9916cdSFrançois Tigeot 
607a85cb24fSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_CONTROL, drm_legacy_irq_control, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6082c9916cdSFrançois Tigeot 
6090e1ba51bSFrançois Tigeot #if IS_ENABLED(CONFIG_AGP)
6102c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_AGP_ACQUIRE, drm_agp_acquire_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6112c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_AGP_RELEASE, drm_agp_release_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6122c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_AGP_ENABLE, drm_agp_enable_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6132c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_AGP_INFO, drm_agp_info_ioctl, DRM_AUTH),
6142c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_AGP_ALLOC, drm_agp_alloc_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6152c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_AGP_FREE, drm_agp_free_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6162c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_AGP_BIND, drm_agp_bind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6172c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_AGP_UNBIND, drm_agp_unbind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6180e1ba51bSFrançois Tigeot #endif
6192c9916cdSFrançois Tigeot 
6202c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_SG_ALLOC, drm_legacy_sg_alloc, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6212c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_SG_FREE, drm_legacy_sg_free, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6222c9916cdSFrançois Tigeot 
6233f2dd94aSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_WAIT_VBLANK, drm_wait_vblank_ioctl, DRM_UNLOCKED),
6242c9916cdSFrançois Tigeot 
6253f2dd94aSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODESET_CTL, drm_legacy_modeset_ctl_ioctl, 0),
6262c9916cdSFrançois Tigeot 
6272c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_UPDATE_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
6282c9916cdSFrançois Tigeot 
6292c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_GEM_CLOSE, drm_gem_close_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
6302c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_GEM_FLINK, drm_gem_flink_ioctl, DRM_AUTH|DRM_UNLOCKED),
6312c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_GEM_OPEN, drm_gem_open_ioctl, DRM_AUTH|DRM_UNLOCKED),
6322c9916cdSFrançois Tigeot 
6332c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETRESOURCES, drm_mode_getresources, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
6342c9916cdSFrançois Tigeot 
6352c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_PRIME_HANDLE_TO_FD, drm_prime_handle_to_fd_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
6362c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_PRIME_FD_TO_HANDLE, drm_prime_fd_to_handle_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
6372c9916cdSFrançois Tigeot 
6382c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANERESOURCES, drm_mode_getplane_res, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
6392c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCRTC, drm_mode_getcrtc, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
6402c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETCRTC, drm_mode_setcrtc, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
6412c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANE, drm_mode_getplane, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
6422c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETPLANE, drm_mode_setplane, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
6432c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_CURSOR, drm_mode_cursor_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
6442c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETGAMMA, drm_mode_gamma_get_ioctl, DRM_UNLOCKED),
6452c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETGAMMA, drm_mode_gamma_set_ioctl, DRM_MASTER|DRM_UNLOCKED),
6462c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETENCODER, drm_mode_getencoder, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
6472c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCONNECTOR, drm_mode_getconnector, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
6482c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_ATTACHMODE, drm_noop, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
6492c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_DETACHMODE, drm_noop, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
6502c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPROPERTY, drm_mode_getproperty_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
6512c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETPROPERTY, drm_mode_connector_property_set_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
6522c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPROPBLOB, drm_mode_getblob_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
6532c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETFB, drm_mode_getfb, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
6542c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_ADDFB, drm_mode_addfb, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
6552c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_ADDFB2, drm_mode_addfb2, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
6562c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_RMFB, drm_mode_rmfb, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
6572c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_PAGE_FLIP, drm_mode_page_flip_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
6582c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_DIRTYFB, drm_mode_dirtyfb_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
6592c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_CREATE_DUMB, drm_mode_create_dumb_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
6602c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_MAP_DUMB, drm_mode_mmap_dumb_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
6612c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_DESTROY_DUMB, drm_mode_destroy_dumb_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
6622c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_OBJ_GETPROPERTIES, drm_mode_obj_get_properties_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
6632c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_OBJ_SETPROPERTY, drm_mode_obj_set_property_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
6642c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_CURSOR2, drm_mode_cursor2_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
6652c9916cdSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_ATOMIC, drm_mode_atomic_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
66619c468b4SFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_CREATEPROPBLOB, drm_mode_createblob_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
66719c468b4SFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_DESTROYPROPBLOB, drm_mode_destroyblob_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
6683f2dd94aSFrançois Tigeot 
6693f2dd94aSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_CREATE, drm_syncobj_create_ioctl,
6703f2dd94aSFrançois Tigeot 		      DRM_UNLOCKED|DRM_RENDER_ALLOW),
6713f2dd94aSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_DESTROY, drm_syncobj_destroy_ioctl,
6723f2dd94aSFrançois Tigeot 		      DRM_UNLOCKED|DRM_RENDER_ALLOW),
6733f2dd94aSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, drm_syncobj_handle_to_fd_ioctl,
6743f2dd94aSFrançois Tigeot 		      DRM_UNLOCKED|DRM_RENDER_ALLOW),
6753f2dd94aSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, drm_syncobj_fd_to_handle_ioctl,
6763f2dd94aSFrançois Tigeot 		      DRM_UNLOCKED|DRM_RENDER_ALLOW),
6773f2dd94aSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_WAIT, drm_syncobj_wait_ioctl,
6783f2dd94aSFrançois Tigeot 		      DRM_UNLOCKED|DRM_RENDER_ALLOW),
6793f2dd94aSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_RESET, drm_syncobj_reset_ioctl,
6803f2dd94aSFrançois Tigeot 		      DRM_UNLOCKED|DRM_RENDER_ALLOW),
6813f2dd94aSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_SIGNAL, drm_syncobj_signal_ioctl,
6823f2dd94aSFrançois Tigeot 		      DRM_UNLOCKED|DRM_RENDER_ALLOW),
6833f2dd94aSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_CRTC_GET_SEQUENCE, drm_crtc_get_sequence_ioctl, DRM_UNLOCKED),
6843f2dd94aSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_CRTC_QUEUE_SEQUENCE, drm_crtc_queue_sequence_ioctl, DRM_UNLOCKED),
6853f2dd94aSFrançois Tigeot #if 0
6863f2dd94aSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_CREATE_LEASE, drm_mode_create_lease_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
6873f2dd94aSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_LIST_LESSEES, drm_mode_list_lessees_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
6883f2dd94aSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GET_LEASE, drm_mode_get_lease_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
6893f2dd94aSFrançois Tigeot 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_REVOKE_LEASE, drm_mode_revoke_lease_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
6903f2dd94aSFrançois Tigeot #endif
6912c9916cdSFrançois Tigeot };
6922c9916cdSFrançois Tigeot 
6932c9916cdSFrançois Tigeot #define DRM_CORE_IOCTL_COUNT	ARRAY_SIZE( drm_ioctls )
6942c9916cdSFrançois Tigeot 
69524edb884SFrançois Tigeot /**
696a85cb24fSFrançois Tigeot  * DOC: driver specific ioctls
697a85cb24fSFrançois Tigeot  *
698a85cb24fSFrançois Tigeot  * First things first, driver private IOCTLs should only be needed for drivers
699a85cb24fSFrançois Tigeot  * supporting rendering. Kernel modesetting is all standardized, and extended
700a85cb24fSFrançois Tigeot  * through properties. There are a few exceptions in some existing drivers,
701a85cb24fSFrançois Tigeot  * which define IOCTL for use by the display DRM master, but they all predate
702a85cb24fSFrançois Tigeot  * properties.
703a85cb24fSFrançois Tigeot  *
704a85cb24fSFrançois Tigeot  * Now if you do have a render driver you always have to support it through
705a85cb24fSFrançois Tigeot  * driver private properties. There's a few steps needed to wire all the things
706a85cb24fSFrançois Tigeot  * up.
707a85cb24fSFrançois Tigeot  *
708a85cb24fSFrançois Tigeot  * First you need to define the structure for your IOCTL in your driver private
709a85cb24fSFrançois Tigeot  * UAPI header in ``include/uapi/drm/my_driver_drm.h``::
710a85cb24fSFrançois Tigeot  *
711a85cb24fSFrançois Tigeot  *     struct my_driver_operation {
712a85cb24fSFrançois Tigeot  *             u32 some_thing;
713a85cb24fSFrançois Tigeot  *             u32 another_thing;
714a85cb24fSFrançois Tigeot  *     };
715a85cb24fSFrançois Tigeot  *
716a85cb24fSFrançois Tigeot  * Please make sure that you follow all the best practices from
717a85cb24fSFrançois Tigeot  * ``Documentation/ioctl/botching-up-ioctls.txt``. Note that drm_ioctl()
718a85cb24fSFrançois Tigeot  * automatically zero-extends structures, hence make sure you can add more stuff
719a85cb24fSFrançois Tigeot  * at the end, i.e. don't put a variable sized array there.
720a85cb24fSFrançois Tigeot  *
721a85cb24fSFrançois Tigeot  * Then you need to define your IOCTL number, using one of DRM_IO(), DRM_IOR(),
722a85cb24fSFrançois Tigeot  * DRM_IOW() or DRM_IOWR(). It must start with the DRM_IOCTL\_ prefix::
723a85cb24fSFrançois Tigeot  *
724a85cb24fSFrançois Tigeot  *     ##define DRM_IOCTL_MY_DRIVER_OPERATION \
725a85cb24fSFrançois Tigeot  *         DRM_IOW(DRM_COMMAND_BASE, struct my_driver_operation)
726a85cb24fSFrançois Tigeot  *
727a85cb24fSFrançois Tigeot  * DRM driver private IOCTL must be in the range from DRM_COMMAND_BASE to
728a85cb24fSFrançois Tigeot  * DRM_COMMAND_END. Finally you need an array of &struct drm_ioctl_desc to wire
7293f2dd94aSFrançois Tigeot  * up the handlers and set the access rights::
730a85cb24fSFrançois Tigeot  *
731a85cb24fSFrançois Tigeot  *     static const struct drm_ioctl_desc my_driver_ioctls[] = {
732a85cb24fSFrançois Tigeot  *         DRM_IOCTL_DEF_DRV(MY_DRIVER_OPERATION, my_driver_operation,
733a85cb24fSFrançois Tigeot  *                 DRM_AUTH|DRM_RENDER_ALLOW),
734a85cb24fSFrançois Tigeot  *     };
735a85cb24fSFrançois Tigeot  *
736a85cb24fSFrançois Tigeot  * And then assign this to the &drm_driver.ioctls field in your driver
737a85cb24fSFrançois Tigeot  * structure.
7383f2dd94aSFrançois Tigeot  *
7393f2dd94aSFrançois Tigeot  * See the separate chapter on :ref:`file operations<drm_driver_fops>` for how
7403f2dd94aSFrançois Tigeot  * the driver-specific IOCTLs are wired up.
741a85cb24fSFrançois Tigeot  */
742a85cb24fSFrançois Tigeot 
7433f2dd94aSFrançois Tigeot #if 0
7443f2dd94aSFrançois Tigeot long drm_ioctl_kernel(struct file *file, drm_ioctl_t *func, void *kdata,
7453f2dd94aSFrançois Tigeot 		      u32 flags)
7463f2dd94aSFrançois Tigeot {
7473f2dd94aSFrançois Tigeot 	struct drm_file *file_priv = file->private_data;
7483f2dd94aSFrançois Tigeot 	struct drm_device *dev = file_priv->minor->dev;
7493f2dd94aSFrançois Tigeot 	int retcode;
7503f2dd94aSFrançois Tigeot 
7513f2dd94aSFrançois Tigeot 	if (drm_dev_is_unplugged(dev))
7523f2dd94aSFrançois Tigeot 		return -ENODEV;
7533f2dd94aSFrançois Tigeot 
7543f2dd94aSFrançois Tigeot 	retcode = drm_ioctl_permit(flags, file_priv);
7553f2dd94aSFrançois Tigeot 	if (unlikely(retcode))
7563f2dd94aSFrançois Tigeot 		return retcode;
7573f2dd94aSFrançois Tigeot 
7583f2dd94aSFrançois Tigeot 	/* Enforce sane locking for modern driver ioctls. */
7593f2dd94aSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_LEGACY) ||
7603f2dd94aSFrançois Tigeot 	    (flags & DRM_UNLOCKED))
7613f2dd94aSFrançois Tigeot 		retcode = func(dev, kdata, file_priv);
7623f2dd94aSFrançois Tigeot 	else {
7633f2dd94aSFrançois Tigeot 		mutex_lock(&drm_global_mutex);
7643f2dd94aSFrançois Tigeot 		retcode = func(dev, kdata, file_priv);
7653f2dd94aSFrançois Tigeot 		mutex_unlock(&drm_global_mutex);
7663f2dd94aSFrançois Tigeot 	}
7673f2dd94aSFrançois Tigeot 	return retcode;
7683f2dd94aSFrançois Tigeot }
7693f2dd94aSFrançois Tigeot EXPORT_SYMBOL(drm_ioctl_kernel);
7703f2dd94aSFrançois Tigeot #endif
7713f2dd94aSFrançois Tigeot 
772a85cb24fSFrançois Tigeot /**
773352ff8bdSFrançois Tigeot  * drm_ioctl - ioctl callback implementation for DRM drivers
774352ff8bdSFrançois Tigeot  * @filp: file this ioctl is called on
775352ff8bdSFrançois Tigeot  * @cmd: ioctl cmd number
776352ff8bdSFrançois Tigeot  * @arg: user argument
77724edb884SFrançois Tigeot  *
778a85cb24fSFrançois Tigeot  * Looks up the ioctl function in the DRM core and the driver dispatch table,
779a85cb24fSFrançois Tigeot  * stored in &drm_driver.ioctls. It checks for necessary permission by calling
780a85cb24fSFrançois Tigeot  * drm_ioctl_permit(), and dispatches to the respective function.
781352ff8bdSFrançois Tigeot  *
782352ff8bdSFrançois Tigeot  * Returns:
783352ff8bdSFrançois Tigeot  * Zero on success, negative error code on failure.
78424edb884SFrançois Tigeot  */
drm_ioctl(struct dev_ioctl_args * ap)78524edb884SFrançois Tigeot int drm_ioctl(struct dev_ioctl_args *ap)
78624edb884SFrançois Tigeot {
787c4bb8e01SFrançois Tigeot 	struct file *filp = ap->a_fp;
788c22b0374SFrançois Tigeot 	u_long cmd = ap->a_cmd;
789c4bb8e01SFrançois Tigeot 	struct drm_file *file_priv = filp->private_data;
79024edb884SFrançois Tigeot 	struct drm_device *dev;
79124edb884SFrançois Tigeot 	const struct drm_ioctl_desc *ioctl = NULL;
792908aca88SFrançois Tigeot 	drm_ioctl_t *func;
79324edb884SFrançois Tigeot 	unsigned int nr = DRM_IOCTL_NR(cmd);
7945fe27e74SFrançois Tigeot 	int retcode = -EINVAL;
79524edb884SFrançois Tigeot 	caddr_t data = ap->a_data;
796352ff8bdSFrançois Tigeot 	bool is_driver_ioctl;
79724edb884SFrançois Tigeot 
7983f2dd94aSFrançois Tigeot 	dev = file_priv->minor->dev;
79924edb884SFrançois Tigeot 
8003f2dd94aSFrançois Tigeot 	if (drm_dev_is_unplugged(dev))
801908aca88SFrançois Tigeot 		return -ENODEV;
80224edb884SFrançois Tigeot 
803352ff8bdSFrançois Tigeot 	is_driver_ioctl = nr >= DRM_COMMAND_BASE && nr < DRM_COMMAND_END;
804352ff8bdSFrançois Tigeot 
8055fe27e74SFrançois Tigeot 	if (is_driver_ioctl) {
8065fe27e74SFrançois Tigeot 		/* driver ioctl */
8075fe27e74SFrançois Tigeot 		if (nr - DRM_COMMAND_BASE >= dev->driver->num_ioctls)
8085fe27e74SFrançois Tigeot 			goto err_i1;
8095fe27e74SFrançois Tigeot 		ioctl = &dev->driver->ioctls[nr - DRM_COMMAND_BASE];
8105fe27e74SFrançois Tigeot 	} else {
8115fe27e74SFrançois Tigeot 		/* core ioctl */
8125fe27e74SFrançois Tigeot 		if (nr >= DRM_CORE_IOCTL_COUNT)
8135fe27e74SFrançois Tigeot 			goto err_i1;
81424edb884SFrançois Tigeot 		ioctl = &drm_ioctls[nr];
81524edb884SFrançois Tigeot 	}
81624edb884SFrançois Tigeot 
8177387c767Szrj 	DRM_DEBUG_IOCTL("pid=%d, dev=0x%lx, auth=%d, %s\n",
818908aca88SFrançois Tigeot 		  DRM_CURRENTPID,
819908aca88SFrançois Tigeot 		  (long)dev->dev,
8207387c767Szrj 		  file_priv->authenticated, ioctl->name);
82124edb884SFrançois Tigeot 
82224edb884SFrançois Tigeot 	/* Do not trust userspace, use our own definition */
82324edb884SFrançois Tigeot 	func = ioctl->func;
82424edb884SFrançois Tigeot 
825352ff8bdSFrançois Tigeot 	if (unlikely(!func)) {
826352ff8bdSFrançois Tigeot 		DRM_DEBUG("no function\n");
827352ff8bdSFrançois Tigeot 		retcode = -EINVAL;
828352ff8bdSFrançois Tigeot 		goto err_i1;
82924edb884SFrançois Tigeot 	}
83024edb884SFrançois Tigeot 
831908aca88SFrançois Tigeot 	retcode = drm_ioctl_permit(ioctl->flags, file_priv);
832908aca88SFrançois Tigeot 	if (unlikely(retcode))
833908aca88SFrançois Tigeot 		goto err_i1;
83424edb884SFrançois Tigeot 
835a85cb24fSFrançois Tigeot 	/* Enforce sane locking for modern driver ioctls. */
836a85cb24fSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_LEGACY) ||
837c22b0374SFrançois Tigeot 	    (ioctl->flags & DRM_UNLOCKED))
838247972abSFrançois Tigeot 		retcode = -func(dev, data, file_priv);
839c22b0374SFrançois Tigeot 	else {
84024edb884SFrançois Tigeot 		mutex_lock(&drm_global_mutex);
841247972abSFrançois Tigeot 		retcode = -func(dev, data, file_priv);
84224edb884SFrançois Tigeot 		mutex_unlock(&drm_global_mutex);
843908aca88SFrançois Tigeot 	}
8443f2dd94aSFrançois Tigeot 
845b922632fSImre Vadász 	if (retcode == ERESTARTSYS)
846b922632fSImre Vadász 			retcode = EINTR;
84724edb884SFrançois Tigeot 
848352ff8bdSFrançois Tigeot       err_i1:
84924edb884SFrançois Tigeot 	if (!ioctl)
85091fff950Szrj 		DRM_DEBUG_FIOCTL("invalid ioctl: pid=%d, dev=0x%lx, auth=%d, cmd=0x%02lx, nr=0x%02x\n",
85124edb884SFrançois Tigeot 			  DRM_CURRENTPID,
85224edb884SFrançois Tigeot 			  (long)dev->dev,
85324edb884SFrançois Tigeot 			  file_priv->authenticated, cmd, nr);
85424edb884SFrançois Tigeot 
85524edb884SFrançois Tigeot 	if (retcode)
85691fff950Szrj 		DRM_DEBUG_FIOCTL("ret = %d\n", retcode);
85724edb884SFrançois Tigeot 	return retcode;
85824edb884SFrançois Tigeot }
8592c9916cdSFrançois Tigeot EXPORT_SYMBOL(drm_ioctl);
86024edb884SFrançois Tigeot 
86124edb884SFrançois Tigeot /**
86224edb884SFrançois Tigeot  * drm_ioctl_flags - Check for core ioctl and return ioctl permission flags
863352ff8bdSFrançois Tigeot  * @nr: ioctl number
864352ff8bdSFrançois Tigeot  * @flags: where to return the ioctl permission flags
86524edb884SFrançois Tigeot  *
866352ff8bdSFrançois Tigeot  * This ioctl is only used by the vmwgfx driver to augment the access checks
867352ff8bdSFrançois Tigeot  * done by the drm core and insofar a pretty decent layering violation. This
868352ff8bdSFrançois Tigeot  * shouldn't be used by any drivers.
869352ff8bdSFrançois Tigeot  *
870352ff8bdSFrançois Tigeot  * Returns:
8711dedbd3bSFrançois Tigeot  * True if the @nr corresponds to a DRM core ioctl number, false otherwise.
87224edb884SFrançois Tigeot  */
drm_ioctl_flags(unsigned int nr,unsigned int * flags)87324edb884SFrançois Tigeot bool drm_ioctl_flags(unsigned int nr, unsigned int *flags)
87424edb884SFrançois Tigeot {
8754be47400SFrançois Tigeot 	if (nr >= DRM_COMMAND_BASE && nr < DRM_COMMAND_END)
8764be47400SFrançois Tigeot 		return false;
8774be47400SFrançois Tigeot 
8784be47400SFrançois Tigeot 	if (nr >= DRM_CORE_IOCTL_COUNT)
8794be47400SFrançois Tigeot 		return false;
8804be47400SFrançois Tigeot 
88124edb884SFrançois Tigeot 	*flags = drm_ioctls[nr].flags;
88224edb884SFrançois Tigeot 	return true;
88324edb884SFrançois Tigeot }
88424edb884SFrançois Tigeot EXPORT_SYMBOL(drm_ioctl_flags);
885