xref: /openbsd-src/sys/dev/pci/drm/drm_ioctl.c (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /*
2  * Created: Fri Jan  8 09:01:26 1999 by faith@valinux.com
3  *
4  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6  * All Rights Reserved.
7  *
8  * Author Rickard E. (Rik) Faith <faith@valinux.com>
9  * Author Gareth Hughes <gareth@valinux.com>
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice (including the next
19  * paragraph) shall be included in all copies or substantial portions of the
20  * Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
25  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28  * OTHER DEALINGS IN THE SOFTWARE.
29  */
30 
31 #include <sys/filio.h>
32 
33 #include <drm/drm_ioctl.h>
34 #include <drm/drmP.h>
35 #ifdef __linux__
36 #include <drm/drm_auth.h>
37 #include "drm_legacy.h"
38 #endif
39 #include "drm_internal.h"
40 #include "drm_crtc_internal.h"
41 
42 #include <linux/pci.h>
43 #include <linux/export.h>
44 #include <linux/nospec.h>
45 
46 int	drm_setunique(struct drm_device *, void *, struct drm_file *);
47 
48 /**
49  * DOC: getunique and setversion story
50  *
51  * BEWARE THE DRAGONS! MIND THE TRAPDOORS!
52  *
53  * In an attempt to warn anyone else who's trying to figure out what's going
54  * on here, I'll try to summarize the story. First things first, let's clear up
55  * the names, because the kernel internals, libdrm and the ioctls are all named
56  * differently:
57  *
58  *  - GET_UNIQUE ioctl, implemented by drm_getunique is wrapped up in libdrm
59  *    through the drmGetBusid function.
60  *  - The libdrm drmSetBusid function is backed by the SET_UNIQUE ioctl. All
61  *    that code is nerved in the kernel with drm_invalid_op().
62  *  - The internal set_busid kernel functions and driver callbacks are
63  *    exclusively use by the SET_VERSION ioctl, because only drm 1.0 (which is
64  *    nerved) allowed userspace to set the busid through the above ioctl.
65  *  - Other ioctls and functions involved are named consistently.
66  *
67  * For anyone wondering what's the difference between drm 1.1 and 1.4: Correctly
68  * handling pci domains in the busid on ppc. Doing this correctly was only
69  * implemented in libdrm in 2010, hence can't be nerved yet. No one knows what's
70  * special with drm 1.2 and 1.3.
71  *
72  * Now the actual horror story of how device lookup in drm works. At large,
73  * there's 2 different ways, either by busid, or by device driver name.
74  *
75  * Opening by busid is fairly simple:
76  *
77  * 1. First call SET_VERSION to make sure pci domains are handled properly. As a
78  *    side-effect this fills out the unique name in the master structure.
79  * 2. Call GET_UNIQUE to read out the unique name from the master structure,
80  *    which matches the busid thanks to step 1. If it doesn't, proceed to try
81  *    the next device node.
82  *
83  * Opening by name is slightly different:
84  *
85  * 1. Directly call VERSION to get the version and to match against the driver
86  *    name returned by that ioctl. Note that SET_VERSION is not called, which
87  *    means the the unique name for the master node just opening is _not_ filled
88  *    out. This despite that with current drm device nodes are always bound to
89  *    one device, and can't be runtime assigned like with drm 1.0.
90  * 2. Match driver name. If it mismatches, proceed to the next device node.
91  * 3. Call GET_UNIQUE, and check whether the unique name has length zero (by
92  *    checking that the first byte in the string is 0). If that's not the case
93  *    libdrm skips and proceeds to the next device node. Probably this is just
94  *    copypasta from drm 1.0 times where a set unique name meant that the driver
95  *    was in use already, but that's just conjecture.
96  *
97  * Long story short: To keep the open by name logic working, GET_UNIQUE must
98  * _not_ return a unique string when SET_VERSION hasn't been called yet,
99  * otherwise libdrm breaks. Even when that unique string can't ever change, and
100  * is totally irrelevant for actually opening the device because runtime
101  * assignable device instances were only support in drm 1.0, which is long dead.
102  * But the libdrm code in drmOpenByName somehow survived, hence this can't be
103  * broken.
104  */
105 
106 /*
107  * Get the bus id.
108  *
109  * \param inode device inode.
110  * \param file_priv DRM file private.
111  * \param cmd command.
112  * \param arg user argument, pointing to a drm_unique structure.
113  * \return zero on success or a negative number on failure.
114  *
115  * Copies the bus id from drm_device::unique into user space.
116  */
117 int drm_getunique(struct drm_device *dev, void *data,
118 		  struct drm_file *file_priv)
119 {
120 	struct drm_unique	 *u = data;
121 
122 	if (u->unique_len >= dev->unique_len) {
123 		if (DRM_COPY_TO_USER(u->unique, dev->unique, dev->unique_len))
124 			return -EFAULT;
125 	}
126 	u->unique_len = dev->unique_len;
127 
128 	return 0;
129 }
130 
131 /*
132  * Get client information.
133  *
134  * \param inode device inode.
135  * \param file_priv DRM file private.
136  * \param cmd command.
137  * \param arg user argument, pointing to a drm_client structure.
138  *
139  * \return zero on success or a negative number on failure.
140  *
141  * Searches for the client with the specified index and copies its information
142  * into userspace
143  */
144 int drm_getclient(struct drm_device *dev, void *data,
145 		  struct drm_file *file_priv)
146 {
147 	struct drm_client *client = data;
148 
149 	/*
150 	 * Hollowed-out getclient ioctl to keep some dead old drm tests/tools
151 	 * not breaking completely. Userspace tools stop enumerating one they
152 	 * get -EINVAL, hence this is the return value we need to hand back for
153 	 * no clients tracked.
154 	 *
155 	 * Unfortunately some clients (*cough* libva *cough*) use this in a fun
156 	 * attempt to figure out whether they're authenticated or not. Since
157 	 * that's the only thing they care about, give it to the directly
158 	 * instead of walking one giant list.
159 	 */
160 	if (client->idx == 0) {
161 		client->auth = file_priv->authenticated;
162 #ifdef __linux__
163 		client->pid = task_pid_vnr(current);
164 		client->uid = overflowuid;
165 #else
166 		client->pid = curproc->p_p->ps_pid;
167 		client->uid = 0xfffe;
168 #endif
169 		client->magic = 0;
170 		client->iocs = 0;
171 
172 		return 0;
173 	} else {
174 		return -EINVAL;
175 	}
176 }
177 
178 /*
179  * Get statistics information.
180  *
181  * \param inode device inode.
182  * \param file_priv DRM file private.
183  * \param cmd command.
184  * \param arg user argument, pointing to a drm_stats structure.
185  *
186  * \return zero on success or a negative number on failure.
187  */
188 static int drm_getstats(struct drm_device *dev, void *data,
189 		 struct drm_file *file_priv)
190 {
191 	struct drm_stats *stats = data;
192 
193 	/* Clear stats to prevent userspace from eating its stack garbage. */
194 	memset(stats, 0, sizeof(*stats));
195 
196 	return 0;
197 }
198 
199 /*
200  * Get device/driver capabilities
201  */
202 static int drm_getcap(struct drm_device *dev, void *data, struct drm_file *file_priv)
203 {
204 	struct drm_get_cap *req = data;
205 	struct drm_crtc *crtc;
206 
207 	req->value = 0;
208 
209 	/* Only some caps make sense with UMS/render-only drivers. */
210 	switch (req->capability) {
211 	case DRM_CAP_TIMESTAMP_MONOTONIC:
212 		req->value = 1;
213 		return 0;
214 	case DRM_CAP_PRIME:
215 		req->value |= dev->driver->prime_fd_to_handle ? DRM_PRIME_CAP_IMPORT : 0;
216 		req->value |= dev->driver->prime_handle_to_fd ? DRM_PRIME_CAP_EXPORT : 0;
217 		return 0;
218 	case DRM_CAP_SYNCOBJ:
219 		req->value = drm_core_check_feature(dev, DRIVER_SYNCOBJ);
220 		return 0;
221 	}
222 
223 	/* Other caps only work with KMS drivers */
224 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
225 		return -ENOTSUPP;
226 
227 	switch (req->capability) {
228 	case DRM_CAP_DUMB_BUFFER:
229 		if (dev->driver->dumb_create)
230 			req->value = 1;
231 		break;
232 	case DRM_CAP_VBLANK_HIGH_CRTC:
233 		req->value = 1;
234 		break;
235 	case DRM_CAP_DUMB_PREFERRED_DEPTH:
236 		req->value = dev->mode_config.preferred_depth;
237 		break;
238 	case DRM_CAP_DUMB_PREFER_SHADOW:
239 		req->value = dev->mode_config.prefer_shadow;
240 		break;
241 	case DRM_CAP_ASYNC_PAGE_FLIP:
242 		req->value = dev->mode_config.async_page_flip;
243 		break;
244 	case DRM_CAP_PAGE_FLIP_TARGET:
245 		req->value = 1;
246 		drm_for_each_crtc(crtc, dev) {
247 			if (!crtc->funcs->page_flip_target)
248 				req->value = 0;
249 		}
250 		break;
251 	case DRM_CAP_CURSOR_WIDTH:
252 		if (dev->mode_config.cursor_width)
253 			req->value = dev->mode_config.cursor_width;
254 		else
255 			req->value = 64;
256 		break;
257 	case DRM_CAP_CURSOR_HEIGHT:
258 		if (dev->mode_config.cursor_height)
259 			req->value = dev->mode_config.cursor_height;
260 		else
261 			req->value = 64;
262 		break;
263 	case DRM_CAP_ADDFB2_MODIFIERS:
264 		req->value = dev->mode_config.allow_fb_modifiers;
265 		break;
266 	case DRM_CAP_CRTC_IN_VBLANK_EVENT:
267 		req->value = 1;
268 		break;
269 	default:
270 		return -EINVAL;
271 	}
272 	return 0;
273 }
274 
275 /*
276  * Set device/driver capabilities
277  */
278 static int
279 drm_setclientcap(struct drm_device *dev, void *data, struct drm_file *file_priv)
280 {
281 	struct drm_set_client_cap *req = data;
282 
283 	switch (req->capability) {
284 	case DRM_CLIENT_CAP_STEREO_3D:
285 		if (req->value > 1)
286 			return -EINVAL;
287 		file_priv->stereo_allowed = req->value;
288 		break;
289 	case DRM_CLIENT_CAP_UNIVERSAL_PLANES:
290 		if (req->value > 1)
291 			return -EINVAL;
292 		file_priv->universal_planes = req->value;
293 		break;
294 	case DRM_CLIENT_CAP_ATOMIC:
295 		if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
296 			return -EINVAL;
297 		if (req->value > 1)
298 			return -EINVAL;
299 		file_priv->atomic = req->value;
300 		file_priv->universal_planes = req->value;
301 		/*
302 		 * No atomic user-space blows up on aspect ratio mode bits.
303 		 */
304 		file_priv->aspect_ratio_allowed = req->value;
305 		break;
306 	case DRM_CLIENT_CAP_ASPECT_RATIO:
307 		if (req->value > 1)
308 			return -EINVAL;
309 		file_priv->aspect_ratio_allowed = req->value;
310 		break;
311 #ifdef notyet
312 	case DRM_CLIENT_CAP_WRITEBACK_CONNECTORS:
313 		if (!file_priv->atomic)
314 			return -EINVAL;
315 		if (req->value > 1)
316 			return -EINVAL;
317 		file_priv->writeback_connectors = req->value;
318 		break;
319 #endif
320 	default:
321 		return -EINVAL;
322 	}
323 
324 	return 0;
325 }
326 
327 /*
328  * Setversion ioctl.
329  *
330  * \param inode device inode.
331  * \param file_priv DRM file private.
332  * \param cmd command.
333  * \param arg user argument, pointing to a drm_lock structure.
334  * \return zero on success or negative number on failure.
335  *
336  * Sets the requested interface version
337  */
338 static int drm_setversion(struct drm_device *dev, void *data, struct drm_file *file_priv)
339 {
340 	struct drm_set_version	ver, *sv = data;
341 	int			if_version;
342 
343 	/* Save the incoming data, and set the response before continuing
344 	 * any further.
345 	 */
346 	ver = *sv;
347 	sv->drm_di_major = DRM_IF_MAJOR;
348 	sv->drm_di_minor = DRM_IF_MINOR;
349 	sv->drm_dd_major = dev->driver->major;
350 	sv->drm_dd_minor = dev->driver->minor;
351 
352 	/*
353 	 * We no longer support interface versions less than 1.1, so error
354 	 * out if the xserver is too old. 1.1 always ties the drm to a
355 	 * certain busid, this was done on attach
356 	 */
357 	if (ver.drm_di_major != -1) {
358 		if (ver.drm_di_major != DRM_IF_MAJOR || ver.drm_di_minor < 1 ||
359 		    ver.drm_di_minor > DRM_IF_MINOR) {
360 			return -EINVAL;
361 		}
362 		if_version = DRM_IF_VERSION(ver.drm_di_major, ver.drm_dd_minor);
363 		dev->if_version = imax(if_version, dev->if_version);
364 	}
365 
366 	if (ver.drm_dd_major != -1) {
367 		if (ver.drm_dd_major != dev->driver->major ||
368 		    ver.drm_dd_minor < 0 ||
369 		    ver.drm_dd_minor > dev->driver->minor)
370 			return -EINVAL;
371 	}
372 
373 	return 0;
374 }
375 
376 /**
377  * drm_noop - DRM no-op ioctl implemntation
378  * @dev: DRM device for the ioctl
379  * @data: data pointer for the ioctl
380  * @file_priv: DRM file for the ioctl call
381  *
382  * This no-op implementation for drm ioctls is useful for deprecated
383  * functionality where we can't return a failure code because existing userspace
384  * checks the result of the ioctl, but doesn't care about the action.
385  *
386  * Always returns successfully with 0.
387  */
388 int drm_noop(struct drm_device *dev, void *data,
389 	     struct drm_file *file_priv)
390 {
391 	return 0;
392 }
393 EXPORT_SYMBOL(drm_noop);
394 
395 /**
396  * drm_invalid_op - DRM invalid ioctl implemntation
397  * @dev: DRM device for the ioctl
398  * @data: data pointer for the ioctl
399  * @file_priv: DRM file for the ioctl call
400  *
401  * This no-op implementation for drm ioctls is useful for deprecated
402  * functionality where we really don't want to allow userspace to call the ioctl
403  * any more. This is the case for old ums interfaces for drivers that
404  * transitioned to kms gradually and so kept the old legacy tables around. This
405  * only applies to radeon and i915 kms drivers, other drivers shouldn't need to
406  * use this function.
407  *
408  * Always fails with a return value of -EINVAL.
409  */
410 int drm_invalid_op(struct drm_device *dev, void *data,
411 		   struct drm_file *file_priv)
412 {
413 	return -EINVAL;
414 }
415 EXPORT_SYMBOL(drm_invalid_op);
416 
417 /*
418  * Get version information
419  *
420  * \param inode device inode.
421  * \param filp file pointer.
422  * \param cmd command.
423  * \param arg user argument, pointing to a drm_version structure.
424  * \return zero on success or negative number on failure.
425  *
426  * Fills in the version information in \p arg.
427  */
428 int drm_version(struct drm_device *dev, void *data,
429 		       struct drm_file *file_priv)
430 {
431 	struct drm_version	*version = data;
432 	int			 len;
433 
434 #define DRM_COPY(name, value)						\
435 	len = strlen( value );						\
436 	if ( len > name##_len ) len = name##_len;			\
437 	name##_len = strlen( value );					\
438 	if ( len && name ) {						\
439 		if ( DRM_COPY_TO_USER( name, value, len ) )		\
440 			return -EFAULT;				\
441 	}
442 
443 	version->version_major = dev->driver->major;
444 	version->version_minor = dev->driver->minor;
445 	version->version_patchlevel = dev->driver->patchlevel;
446 
447 	DRM_COPY(version->name, dev->driver->name);
448 	DRM_COPY(version->date, dev->driver->date);
449 	DRM_COPY(version->desc, dev->driver->desc);
450 
451 	return 0;
452 }
453 
454 #define DRM_IOCTL_DEF(ioctl, _func, _flags) \
455 	[DRM_IOCTL_NR(ioctl)] = {.cmd = ioctl, .func = _func, .flags = _flags, .cmd_drv = 0}
456 
457 /* Ioctl table */
458 static const struct drm_ioctl_desc drm_ioctls[] = {
459 	DRM_IOCTL_DEF(DRM_IOCTL_VERSION, drm_version,
460 		      DRM_UNLOCKED|DRM_RENDER_ALLOW),
461 	DRM_IOCTL_DEF(DRM_IOCTL_GET_UNIQUE, drm_getunique, DRM_UNLOCKED),
462 	DRM_IOCTL_DEF(DRM_IOCTL_GET_MAGIC, drm_getmagic, DRM_UNLOCKED),
463 #ifdef __linux__
464 	DRM_IOCTL_DEF(DRM_IOCTL_IRQ_BUSID, drm_irq_by_busid, DRM_MASTER|DRM_ROOT_ONLY),
465 	DRM_IOCTL_DEF(DRM_IOCTL_GET_MAP, drm_legacy_getmap_ioctl, DRM_UNLOCKED),
466 #endif
467 	DRM_IOCTL_DEF(DRM_IOCTL_GET_CLIENT, drm_getclient, DRM_UNLOCKED),
468 	DRM_IOCTL_DEF(DRM_IOCTL_GET_STATS, drm_getstats, DRM_UNLOCKED),
469 	DRM_IOCTL_DEF(DRM_IOCTL_GET_CAP, drm_getcap, DRM_UNLOCKED|DRM_RENDER_ALLOW),
470 	DRM_IOCTL_DEF(DRM_IOCTL_SET_CLIENT_CAP, drm_setclientcap, DRM_UNLOCKED),
471 	DRM_IOCTL_DEF(DRM_IOCTL_SET_VERSION, drm_setversion, DRM_UNLOCKED | DRM_MASTER),
472 
473 	DRM_IOCTL_DEF(DRM_IOCTL_SET_UNIQUE, drm_invalid_op, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
474 	DRM_IOCTL_DEF(DRM_IOCTL_BLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
475 	DRM_IOCTL_DEF(DRM_IOCTL_UNBLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
476 	DRM_IOCTL_DEF(DRM_IOCTL_AUTH_MAGIC, drm_authmagic, DRM_AUTH|DRM_UNLOCKED|DRM_MASTER),
477 
478 #ifdef __linux__
479 	DRM_IOCTL_DEF(DRM_IOCTL_ADD_MAP, drm_legacy_addmap_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
480 	DRM_IOCTL_DEF(DRM_IOCTL_RM_MAP, drm_legacy_rmmap_ioctl, DRM_AUTH),
481 
482 	DRM_IOCTL_DEF(DRM_IOCTL_SET_SAREA_CTX, drm_legacy_setsareactx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
483 	DRM_IOCTL_DEF(DRM_IOCTL_GET_SAREA_CTX, drm_legacy_getsareactx, DRM_AUTH),
484 #else
485 	DRM_IOCTL_DEF(DRM_IOCTL_GET_PCIINFO, drm_getpciinfo, DRM_UNLOCKED|DRM_RENDER_ALLOW),
486 
487 	DRM_IOCTL_DEF(DRM_IOCTL_SET_SAREA_CTX, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
488 	DRM_IOCTL_DEF(DRM_IOCTL_GET_SAREA_CTX, drm_noop, DRM_AUTH),
489 #endif
490 
491 #ifdef __linux__
492 	DRM_IOCTL_DEF(DRM_IOCTL_SET_MASTER, drm_setmaster_ioctl, DRM_UNLOCKED|DRM_ROOT_ONLY),
493 	DRM_IOCTL_DEF(DRM_IOCTL_DROP_MASTER, drm_dropmaster_ioctl, DRM_UNLOCKED|DRM_ROOT_ONLY),
494 #else
495 	/* On OpenBSD xorg privdrop has already occurred before this point */
496 	DRM_IOCTL_DEF(DRM_IOCTL_SET_MASTER, drm_noop, DRM_UNLOCKED|DRM_RENDER_ALLOW),
497 	DRM_IOCTL_DEF(DRM_IOCTL_DROP_MASTER, drm_noop, DRM_UNLOCKED|DRM_RENDER_ALLOW),
498 #endif
499 
500 #ifdef __linux__
501 	DRM_IOCTL_DEF(DRM_IOCTL_ADD_CTX, drm_legacy_addctx, DRM_AUTH|DRM_ROOT_ONLY),
502 	DRM_IOCTL_DEF(DRM_IOCTL_RM_CTX, drm_legacy_rmctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
503 #endif
504 	DRM_IOCTL_DEF(DRM_IOCTL_MOD_CTX, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
505 #ifdef __linux__
506 	DRM_IOCTL_DEF(DRM_IOCTL_GET_CTX, drm_legacy_getctx, DRM_AUTH),
507 	DRM_IOCTL_DEF(DRM_IOCTL_SWITCH_CTX, drm_legacy_switchctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
508 	DRM_IOCTL_DEF(DRM_IOCTL_NEW_CTX, drm_legacy_newctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
509 #else
510 	DRM_IOCTL_DEF(DRM_IOCTL_SWITCH_CTX, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
511 	DRM_IOCTL_DEF(DRM_IOCTL_NEW_CTX, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
512 #endif
513 #ifdef __linux__
514 	DRM_IOCTL_DEF(DRM_IOCTL_RES_CTX, drm_legacy_resctx, DRM_AUTH),
515 #endif
516 
517 	DRM_IOCTL_DEF(DRM_IOCTL_ADD_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
518 	DRM_IOCTL_DEF(DRM_IOCTL_RM_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
519 
520 #ifdef __linux__
521 	DRM_IOCTL_DEF(DRM_IOCTL_LOCK, drm_legacy_lock, DRM_AUTH),
522 	DRM_IOCTL_DEF(DRM_IOCTL_UNLOCK, drm_legacy_unlock, DRM_AUTH),
523 #endif
524 
525 	DRM_IOCTL_DEF(DRM_IOCTL_FINISH, drm_noop, DRM_AUTH),
526 
527 #ifdef __linux__
528 	DRM_IOCTL_DEF(DRM_IOCTL_ADD_BUFS, drm_legacy_addbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
529 	DRM_IOCTL_DEF(DRM_IOCTL_MARK_BUFS, drm_legacy_markbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
530 	DRM_IOCTL_DEF(DRM_IOCTL_INFO_BUFS, drm_legacy_infobufs, DRM_AUTH),
531 #else
532 	DRM_IOCTL_DEF(DRM_IOCTL_MARK_BUFS, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
533 	DRM_IOCTL_DEF(DRM_IOCTL_INFO_BUFS, drm_noop, DRM_AUTH),
534 #endif
535 #ifdef __linux__
536 	DRM_IOCTL_DEF(DRM_IOCTL_MAP_BUFS, drm_legacy_mapbufs, DRM_AUTH),
537 	DRM_IOCTL_DEF(DRM_IOCTL_FREE_BUFS, drm_legacy_freebufs, DRM_AUTH),
538 	DRM_IOCTL_DEF(DRM_IOCTL_DMA, drm_legacy_dma_ioctl, DRM_AUTH),
539 
540 	DRM_IOCTL_DEF(DRM_IOCTL_CONTROL, drm_legacy_irq_control, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
541 
542 #if IS_ENABLED(CONFIG_AGP)
543 	DRM_IOCTL_DEF(DRM_IOCTL_AGP_ACQUIRE, drm_agp_acquire_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
544 	DRM_IOCTL_DEF(DRM_IOCTL_AGP_RELEASE, drm_agp_release_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
545 	DRM_IOCTL_DEF(DRM_IOCTL_AGP_ENABLE, drm_agp_enable_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
546 	DRM_IOCTL_DEF(DRM_IOCTL_AGP_INFO, drm_agp_info_ioctl, DRM_AUTH),
547 	DRM_IOCTL_DEF(DRM_IOCTL_AGP_ALLOC, drm_agp_alloc_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
548 	DRM_IOCTL_DEF(DRM_IOCTL_AGP_FREE, drm_agp_free_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
549 	DRM_IOCTL_DEF(DRM_IOCTL_AGP_BIND, drm_agp_bind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
550 	DRM_IOCTL_DEF(DRM_IOCTL_AGP_UNBIND, drm_agp_unbind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
551 #endif
552 
553 	DRM_IOCTL_DEF(DRM_IOCTL_SG_ALLOC, drm_legacy_sg_alloc, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
554 	DRM_IOCTL_DEF(DRM_IOCTL_SG_FREE, drm_legacy_sg_free, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
555 #endif
556 
557 	DRM_IOCTL_DEF(DRM_IOCTL_WAIT_VBLANK, drm_wait_vblank_ioctl, DRM_UNLOCKED),
558 
559 	DRM_IOCTL_DEF(DRM_IOCTL_MODESET_CTL, drm_legacy_modeset_ctl_ioctl, 0),
560 
561 	DRM_IOCTL_DEF(DRM_IOCTL_UPDATE_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
562 
563 	DRM_IOCTL_DEF(DRM_IOCTL_GEM_CLOSE, drm_gem_close_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
564 	DRM_IOCTL_DEF(DRM_IOCTL_GEM_FLINK, drm_gem_flink_ioctl, DRM_AUTH|DRM_UNLOCKED),
565 	DRM_IOCTL_DEF(DRM_IOCTL_GEM_OPEN, drm_gem_open_ioctl, DRM_AUTH|DRM_UNLOCKED),
566 
567 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETRESOURCES, drm_mode_getresources, DRM_UNLOCKED),
568 
569 	DRM_IOCTL_DEF(DRM_IOCTL_PRIME_HANDLE_TO_FD, drm_prime_handle_to_fd_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
570 	DRM_IOCTL_DEF(DRM_IOCTL_PRIME_FD_TO_HANDLE, drm_prime_fd_to_handle_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
571 
572 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANERESOURCES, drm_mode_getplane_res, DRM_UNLOCKED),
573 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCRTC, drm_mode_getcrtc, DRM_UNLOCKED),
574 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETCRTC, drm_mode_setcrtc, DRM_MASTER|DRM_UNLOCKED),
575 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANE, drm_mode_getplane, DRM_UNLOCKED),
576 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETPLANE, drm_mode_setplane, DRM_MASTER|DRM_UNLOCKED),
577 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_CURSOR, drm_mode_cursor_ioctl, DRM_MASTER|DRM_UNLOCKED),
578 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETGAMMA, drm_mode_gamma_get_ioctl, DRM_UNLOCKED),
579 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETGAMMA, drm_mode_gamma_set_ioctl, DRM_MASTER|DRM_UNLOCKED),
580 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETENCODER, drm_mode_getencoder, DRM_UNLOCKED),
581 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCONNECTOR, drm_mode_getconnector, DRM_UNLOCKED),
582 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_ATTACHMODE, drm_noop, DRM_MASTER|DRM_UNLOCKED),
583 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_DETACHMODE, drm_noop, DRM_MASTER|DRM_UNLOCKED),
584 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPROPERTY, drm_mode_getproperty_ioctl, DRM_UNLOCKED),
585 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETPROPERTY, drm_connector_property_set_ioctl, DRM_MASTER|DRM_UNLOCKED),
586 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPROPBLOB, drm_mode_getblob_ioctl, DRM_UNLOCKED),
587 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETFB, drm_mode_getfb, DRM_UNLOCKED),
588 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_ADDFB, drm_mode_addfb_ioctl, DRM_UNLOCKED),
589 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_ADDFB2, drm_mode_addfb2, DRM_UNLOCKED),
590 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_RMFB, drm_mode_rmfb_ioctl, DRM_UNLOCKED),
591 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_PAGE_FLIP, drm_mode_page_flip_ioctl, DRM_MASTER|DRM_UNLOCKED),
592 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_DIRTYFB, drm_mode_dirtyfb_ioctl, DRM_MASTER|DRM_UNLOCKED),
593 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_CREATE_DUMB, drm_mode_create_dumb_ioctl, DRM_UNLOCKED),
594 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_MAP_DUMB, drm_mode_mmap_dumb_ioctl, DRM_UNLOCKED),
595 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_DESTROY_DUMB, drm_mode_destroy_dumb_ioctl, DRM_UNLOCKED),
596 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_OBJ_GETPROPERTIES, drm_mode_obj_get_properties_ioctl, DRM_UNLOCKED),
597 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_OBJ_SETPROPERTY, drm_mode_obj_set_property_ioctl, DRM_MASTER|DRM_UNLOCKED),
598 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_CURSOR2, drm_mode_cursor2_ioctl, DRM_MASTER|DRM_UNLOCKED),
599 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_ATOMIC, drm_mode_atomic_ioctl, DRM_MASTER|DRM_UNLOCKED),
600 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_CREATEPROPBLOB, drm_mode_createblob_ioctl, DRM_UNLOCKED),
601 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_DESTROYPROPBLOB, drm_mode_destroyblob_ioctl, DRM_UNLOCKED),
602 
603 	DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_CREATE, drm_syncobj_create_ioctl,
604 		      DRM_UNLOCKED|DRM_RENDER_ALLOW),
605 	DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_DESTROY, drm_syncobj_destroy_ioctl,
606 		      DRM_UNLOCKED|DRM_RENDER_ALLOW),
607 	DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, drm_syncobj_handle_to_fd_ioctl,
608 		      DRM_UNLOCKED|DRM_RENDER_ALLOW),
609 	DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, drm_syncobj_fd_to_handle_ioctl,
610 		      DRM_UNLOCKED|DRM_RENDER_ALLOW),
611 	DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_WAIT, drm_syncobj_wait_ioctl,
612 		      DRM_UNLOCKED|DRM_RENDER_ALLOW),
613 	DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_RESET, drm_syncobj_reset_ioctl,
614 		      DRM_UNLOCKED|DRM_RENDER_ALLOW),
615 	DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_SIGNAL, drm_syncobj_signal_ioctl,
616 		      DRM_UNLOCKED|DRM_RENDER_ALLOW),
617 	DRM_IOCTL_DEF(DRM_IOCTL_CRTC_GET_SEQUENCE, drm_crtc_get_sequence_ioctl, DRM_UNLOCKED),
618 	DRM_IOCTL_DEF(DRM_IOCTL_CRTC_QUEUE_SEQUENCE, drm_crtc_queue_sequence_ioctl, DRM_UNLOCKED),
619 #ifdef __linux__
620 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_CREATE_LEASE, drm_mode_create_lease_ioctl, DRM_MASTER|DRM_UNLOCKED),
621 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_LIST_LESSEES, drm_mode_list_lessees_ioctl, DRM_MASTER|DRM_UNLOCKED),
622 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GET_LEASE, drm_mode_get_lease_ioctl, DRM_MASTER|DRM_UNLOCKED),
623 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_REVOKE_LEASE, drm_mode_revoke_lease_ioctl, DRM_MASTER|DRM_UNLOCKED),
624 #endif
625 };
626 
627 #define DRM_CORE_IOCTL_COUNT	ARRAY_SIZE( drm_ioctls )
628 
629 int
630 pledge_ioctl_drm(struct proc *p, long com, dev_t device)
631 {
632 	struct drm_device *dev = drm_get_device_from_kdev(device);
633 	unsigned int nr = DRM_IOCTL_NR(com);
634 	const struct drm_ioctl_desc *ioctl;
635 
636 	if (dev == NULL)
637 		return EPERM;
638 
639 	if (nr < DRM_CORE_IOCTL_COUNT &&
640 	    ((nr < DRM_COMMAND_BASE || nr >= DRM_COMMAND_END)))
641 		ioctl = &drm_ioctls[nr];
642 	else if (nr >= DRM_COMMAND_BASE && nr < DRM_COMMAND_END &&
643 	    nr < DRM_COMMAND_BASE + dev->driver->num_ioctls)
644 		ioctl = &dev->driver->ioctls[nr - DRM_COMMAND_BASE];
645 	else
646 		return EPERM;
647 
648 	if (ioctl->flags & DRM_RENDER_ALLOW)
649 		return 0;
650 
651 	/*
652 	 * These are dangerous, but we have to allow them until we
653 	 * have prime/dma-buf support.
654 	 */
655 	switch (com) {
656 	case DRM_IOCTL_GET_MAGIC:
657 	case DRM_IOCTL_GEM_OPEN:
658 		return 0;
659 	}
660 
661 	return EPERM;
662 }
663 
664 int
665 drm_setunique(struct drm_device *dev, void *data,
666     struct drm_file *file_priv)
667 {
668 	/*
669 	 * Deprecated in DRM version 1.1, and will return EBUSY
670 	 * when setversion has
671 	 * requested version 1.1 or greater.
672 	 */
673 	return (-EBUSY);
674 }
675 
676 int
677 drm_do_ioctl(struct drm_device *dev, int minor, u_long cmd, caddr_t data)
678 {
679 	struct drm_file *file_priv;
680 	const struct drm_ioctl_desc *ioctl;
681 	drm_ioctl_t *func;
682 	unsigned int nr = DRM_IOCTL_NR(cmd);
683 	int retcode = -EINVAL;
684 	unsigned int usize, asize;
685 
686 	mutex_lock(&dev->struct_mutex);
687 	file_priv = drm_find_file_by_minor(dev, minor);
688 	mutex_unlock(&dev->struct_mutex);
689 	if (file_priv == NULL) {
690 		DRM_ERROR("can't find authenticator\n");
691 		return -EINVAL;
692 	}
693 
694 	DRM_DEBUG("pid=%d, cmd=0x%02lx, nr=0x%02x, dev 0x%lx, auth=%d\n",
695 	    DRM_CURRENTPID, cmd, (u_int)DRM_IOCTL_NR(cmd), (long)&dev->dev,
696 	    file_priv->authenticated);
697 
698 	switch (cmd) {
699 	case FIONBIO:
700 	case FIOASYNC:
701 		return 0;
702 	}
703 
704 	if ((nr >= DRM_CORE_IOCTL_COUNT) &&
705 	    ((nr < DRM_COMMAND_BASE) || (nr >= DRM_COMMAND_END)))
706 		return (-EINVAL);
707 	if ((nr >= DRM_COMMAND_BASE) && (nr < DRM_COMMAND_END) &&
708 	    (nr < DRM_COMMAND_BASE + dev->driver->num_ioctls)) {
709 		uint32_t drv_size;
710 		ioctl = &dev->driver->ioctls[nr - DRM_COMMAND_BASE];
711 		drv_size = IOCPARM_LEN(ioctl->cmd_drv);
712 		usize = asize = IOCPARM_LEN(cmd);
713 		if (drv_size > asize)
714 			asize = drv_size;
715 	} else if ((nr >= DRM_COMMAND_END) || (nr < DRM_COMMAND_BASE)) {
716 		uint32_t drv_size;
717 		ioctl = &drm_ioctls[nr];
718 
719 		drv_size = IOCPARM_LEN(ioctl->cmd_drv);
720 		usize = asize = IOCPARM_LEN(cmd);
721 		if (drv_size > asize)
722 			asize = drv_size;
723 		cmd = ioctl->cmd;
724 	} else
725 		return (-EINVAL);
726 
727 	func = ioctl->func;
728 	if (!func) {
729 		DRM_DEBUG("no function\n");
730 		return (-EINVAL);
731 	}
732 
733 	/* ROOT_ONLY is only for CAP_SYS_ADMIN */
734 	if ((ioctl->flags & DRM_ROOT_ONLY) && !capable(CAP_SYS_ADMIN))
735 		return -EACCES;
736 
737 	/* AUTH is only for authenticated or render client */
738 	if ((ioctl->flags & DRM_AUTH) && !drm_is_render_client(file_priv) &&
739 	    !file_priv->authenticated)
740 		return -EACCES;
741 
742 	/* MASTER is only for master or control clients */
743 	if ((ioctl->flags & DRM_MASTER) && !file_priv->is_master)
744 		return -EACCES;
745 
746 	/* Render clients must be explicitly allowed */
747 	if (!(ioctl->flags & DRM_RENDER_ALLOW) &&
748 	    drm_is_render_client(file_priv))
749 		return -EACCES;
750 
751 	if (ioctl->flags & DRM_UNLOCKED)
752 		retcode = func(dev, data, file_priv);
753 	else {
754 		/* XXX lock */
755 		retcode = func(dev, data, file_priv);
756 		/* XXX unlock */
757 	}
758 
759 	return (retcode);
760 }
761 
762 /* drmioctl is called whenever a process performs an ioctl on /dev/drm.
763  */
764 int
765 drmioctl(dev_t kdev, u_long cmd, caddr_t data, int flags, struct proc *p)
766 {
767 	struct drm_device *dev = drm_get_device_from_kdev(kdev);
768 	int error;
769 
770 	if (dev == NULL)
771 		return ENODEV;
772 
773 	mtx_enter(&dev->quiesce_mtx);
774 	while (dev->quiesce)
775 		msleep_nsec(&dev->quiesce, &dev->quiesce_mtx, PZERO, "drmioc",
776 		    INFSLP);
777 	dev->quiesce_count++;
778 	mtx_leave(&dev->quiesce_mtx);
779 
780 	error = -drm_do_ioctl(dev, minor(kdev), cmd, data);
781 	if (error < 0 && error != ERESTART && error != EJUSTRETURN)
782 		printf("%s: cmd 0x%lx errno %d\n", __func__, cmd, error);
783 
784 	mtx_enter(&dev->quiesce_mtx);
785 	dev->quiesce_count--;
786 	if (dev->quiesce)
787 		wakeup(&dev->quiesce_count);
788 	mtx_leave(&dev->quiesce_mtx);
789 
790 	return (error);
791 }
792