drm_ioctl.c revision 1.17 1 1.17 riastrad /* $NetBSD: drm_ioctl.c,v 1.17 2021/12/19 09:48:45 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*
4 1.1 riastrad * Created: Fri Jan 8 09:01:26 1999 by faith (at) valinux.com
5 1.1 riastrad *
6 1.1 riastrad * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
7 1.1 riastrad * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
8 1.1 riastrad * All Rights Reserved.
9 1.1 riastrad *
10 1.5 riastrad * Author Rickard E. (Rik) Faith <faith (at) valinux.com>
11 1.5 riastrad * Author Gareth Hughes <gareth (at) valinux.com>
12 1.5 riastrad *
13 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a
14 1.1 riastrad * copy of this software and associated documentation files (the "Software"),
15 1.1 riastrad * to deal in the Software without restriction, including without limitation
16 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the
18 1.1 riastrad * Software is furnished to do so, subject to the following conditions:
19 1.1 riastrad *
20 1.1 riastrad * The above copyright notice and this permission notice (including the next
21 1.1 riastrad * paragraph) shall be included in all copies or substantial portions of the
22 1.1 riastrad * Software.
23 1.1 riastrad *
24 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 1.1 riastrad * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
28 1.1 riastrad * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
29 1.1 riastrad * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
30 1.1 riastrad * OTHER DEALINGS IN THE SOFTWARE.
31 1.1 riastrad */
32 1.1 riastrad
33 1.5 riastrad #include <sys/cdefs.h>
34 1.17 riastrad __KERNEL_RCSID(0, "$NetBSD: drm_ioctl.c,v 1.17 2021/12/19 09:48:45 riastradh Exp $");
35 1.5 riastrad
36 1.13 riastrad #include <linux/export.h>
37 1.13 riastrad #include <linux/nospec.h>
38 1.13 riastrad #include <linux/pci.h>
39 1.13 riastrad #include <linux/uaccess.h>
40 1.16 riastrad #include <linux/capability.h>
41 1.13 riastrad
42 1.15 riastrad #ifdef __NetBSD__
43 1.15 riastrad #include <sys/types.h>
44 1.15 riastrad #include <sys/file.h>
45 1.15 riastrad #endif
46 1.15 riastrad
47 1.13 riastrad #include <drm/drm_agpsupport.h>
48 1.13 riastrad #include <drm/drm_auth.h>
49 1.13 riastrad #include <drm/drm_crtc.h>
50 1.13 riastrad #include <drm/drm_drv.h>
51 1.13 riastrad #include <drm/drm_file.h>
52 1.13 riastrad #include <drm/drm_ioctl.h>
53 1.13 riastrad #include <drm/drm_print.h>
54 1.13 riastrad
55 1.13 riastrad #include "drm_crtc_internal.h"
56 1.13 riastrad #include "drm_internal.h"
57 1.5 riastrad #include "drm_legacy.h"
58 1.1 riastrad
59 1.13 riastrad /**
60 1.13 riastrad * DOC: getunique and setversion story
61 1.13 riastrad *
62 1.13 riastrad * BEWARE THE DRAGONS! MIND THE TRAPDOORS!
63 1.13 riastrad *
64 1.13 riastrad * In an attempt to warn anyone else who's trying to figure out what's going
65 1.13 riastrad * on here, I'll try to summarize the story. First things first, let's clear up
66 1.13 riastrad * the names, because the kernel internals, libdrm and the ioctls are all named
67 1.13 riastrad * differently:
68 1.13 riastrad *
69 1.13 riastrad * - GET_UNIQUE ioctl, implemented by drm_getunique is wrapped up in libdrm
70 1.13 riastrad * through the drmGetBusid function.
71 1.13 riastrad * - The libdrm drmSetBusid function is backed by the SET_UNIQUE ioctl. All
72 1.13 riastrad * that code is nerved in the kernel with drm_invalid_op().
73 1.13 riastrad * - The internal set_busid kernel functions and driver callbacks are
74 1.13 riastrad * exclusively use by the SET_VERSION ioctl, because only drm 1.0 (which is
75 1.13 riastrad * nerved) allowed userspace to set the busid through the above ioctl.
76 1.13 riastrad * - Other ioctls and functions involved are named consistently.
77 1.13 riastrad *
78 1.13 riastrad * For anyone wondering what's the difference between drm 1.1 and 1.4: Correctly
79 1.13 riastrad * handling pci domains in the busid on ppc. Doing this correctly was only
80 1.13 riastrad * implemented in libdrm in 2010, hence can't be nerved yet. No one knows what's
81 1.13 riastrad * special with drm 1.2 and 1.3.
82 1.13 riastrad *
83 1.13 riastrad * Now the actual horror story of how device lookup in drm works. At large,
84 1.13 riastrad * there's 2 different ways, either by busid, or by device driver name.
85 1.13 riastrad *
86 1.13 riastrad * Opening by busid is fairly simple:
87 1.13 riastrad *
88 1.13 riastrad * 1. First call SET_VERSION to make sure pci domains are handled properly. As a
89 1.13 riastrad * side-effect this fills out the unique name in the master structure.
90 1.13 riastrad * 2. Call GET_UNIQUE to read out the unique name from the master structure,
91 1.13 riastrad * which matches the busid thanks to step 1. If it doesn't, proceed to try
92 1.13 riastrad * the next device node.
93 1.13 riastrad *
94 1.13 riastrad * Opening by name is slightly different:
95 1.13 riastrad *
96 1.13 riastrad * 1. Directly call VERSION to get the version and to match against the driver
97 1.13 riastrad * name returned by that ioctl. Note that SET_VERSION is not called, which
98 1.13 riastrad * means the the unique name for the master node just opening is _not_ filled
99 1.13 riastrad * out. This despite that with current drm device nodes are always bound to
100 1.13 riastrad * one device, and can't be runtime assigned like with drm 1.0.
101 1.13 riastrad * 2. Match driver name. If it mismatches, proceed to the next device node.
102 1.13 riastrad * 3. Call GET_UNIQUE, and check whether the unique name has length zero (by
103 1.13 riastrad * checking that the first byte in the string is 0). If that's not the case
104 1.13 riastrad * libdrm skips and proceeds to the next device node. Probably this is just
105 1.13 riastrad * copypasta from drm 1.0 times where a set unique name meant that the driver
106 1.13 riastrad * was in use already, but that's just conjecture.
107 1.13 riastrad *
108 1.13 riastrad * Long story short: To keep the open by name logic working, GET_UNIQUE must
109 1.13 riastrad * _not_ return a unique string when SET_VERSION hasn't been called yet,
110 1.13 riastrad * otherwise libdrm breaks. Even when that unique string can't ever change, and
111 1.13 riastrad * is totally irrelevant for actually opening the device because runtime
112 1.13 riastrad * assignable device instances were only support in drm 1.0, which is long dead.
113 1.13 riastrad * But the libdrm code in drmOpenByName somehow survived, hence this can't be
114 1.13 riastrad * broken.
115 1.13 riastrad */
116 1.5 riastrad
117 1.5 riastrad /*
118 1.1 riastrad * Get the bus id.
119 1.1 riastrad *
120 1.1 riastrad * \param inode device inode.
121 1.1 riastrad * \param file_priv DRM file private.
122 1.1 riastrad * \param cmd command.
123 1.1 riastrad * \param arg user argument, pointing to a drm_unique structure.
124 1.1 riastrad * \return zero on success or a negative number on failure.
125 1.1 riastrad *
126 1.1 riastrad * Copies the bus id from drm_device::unique into user space.
127 1.1 riastrad */
128 1.13 riastrad int drm_getunique(struct drm_device *dev, void *data,
129 1.1 riastrad struct drm_file *file_priv)
130 1.1 riastrad {
131 1.1 riastrad struct drm_unique *u = data;
132 1.1 riastrad struct drm_master *master = file_priv->master;
133 1.1 riastrad
134 1.13 riastrad mutex_lock(&master->dev->master_mutex);
135 1.1 riastrad if (u->unique_len >= master->unique_len) {
136 1.13 riastrad if (copy_to_user(u->unique, master->unique, master->unique_len)) {
137 1.13 riastrad mutex_unlock(&master->dev->master_mutex);
138 1.1 riastrad return -EFAULT;
139 1.13 riastrad }
140 1.1 riastrad }
141 1.1 riastrad u->unique_len = master->unique_len;
142 1.13 riastrad mutex_unlock(&master->dev->master_mutex);
143 1.1 riastrad
144 1.1 riastrad return 0;
145 1.1 riastrad }
146 1.1 riastrad
147 1.1 riastrad static void
148 1.1 riastrad drm_unset_busid(struct drm_device *dev,
149 1.1 riastrad struct drm_master *master)
150 1.1 riastrad {
151 1.1 riastrad kfree(master->unique);
152 1.1 riastrad master->unique = NULL;
153 1.1 riastrad master->unique_len = 0;
154 1.1 riastrad }
155 1.1 riastrad
156 1.1 riastrad static int drm_set_busid(struct drm_device *dev, struct drm_file *file_priv)
157 1.1 riastrad {
158 1.1 riastrad struct drm_master *master = file_priv->master;
159 1.1 riastrad int ret;
160 1.1 riastrad
161 1.1 riastrad if (master->unique != NULL)
162 1.1 riastrad drm_unset_busid(dev, master);
163 1.1 riastrad
164 1.17 riastrad if (dev->dev && dev_is_pci(dev->pdev)) {
165 1.17 riastrad ret = drm_pci_set_busid(dev, master);
166 1.5 riastrad if (ret) {
167 1.5 riastrad drm_unset_busid(dev, master);
168 1.5 riastrad return ret;
169 1.5 riastrad }
170 1.5 riastrad } else {
171 1.13 riastrad WARN_ON(!dev->unique);
172 1.5 riastrad master->unique = kstrdup(dev->unique, GFP_KERNEL);
173 1.5 riastrad if (master->unique)
174 1.5 riastrad master->unique_len = strlen(dev->unique);
175 1.5 riastrad }
176 1.5 riastrad
177 1.1 riastrad return 0;
178 1.1 riastrad }
179 1.1 riastrad
180 1.5 riastrad /*
181 1.1 riastrad * Get client information.
182 1.1 riastrad *
183 1.1 riastrad * \param inode device inode.
184 1.1 riastrad * \param file_priv DRM file private.
185 1.1 riastrad * \param cmd command.
186 1.1 riastrad * \param arg user argument, pointing to a drm_client structure.
187 1.1 riastrad *
188 1.1 riastrad * \return zero on success or a negative number on failure.
189 1.1 riastrad *
190 1.1 riastrad * Searches for the client with the specified index and copies its information
191 1.1 riastrad * into userspace
192 1.1 riastrad */
193 1.13 riastrad int drm_getclient(struct drm_device *dev, void *data,
194 1.1 riastrad struct drm_file *file_priv)
195 1.1 riastrad {
196 1.1 riastrad struct drm_client *client = data;
197 1.1 riastrad
198 1.3 riastrad /*
199 1.3 riastrad * Hollowed-out getclient ioctl to keep some dead old drm tests/tools
200 1.3 riastrad * not breaking completely. Userspace tools stop enumerating one they
201 1.3 riastrad * get -EINVAL, hence this is the return value we need to hand back for
202 1.3 riastrad * no clients tracked.
203 1.3 riastrad *
204 1.3 riastrad * Unfortunately some clients (*cough* libva *cough*) use this in a fun
205 1.3 riastrad * attempt to figure out whether they're authenticated or not. Since
206 1.3 riastrad * that's the only thing they care about, give it to the directly
207 1.3 riastrad * instead of walking one giant list.
208 1.3 riastrad */
209 1.3 riastrad if (client->idx == 0) {
210 1.3 riastrad client->auth = file_priv->authenticated;
211 1.2 riastrad #ifdef __NetBSD__ /* XXX Too scary to contemplate. */
212 1.4 riastrad client->pid = curproc->p_pid;
213 1.4 riastrad client->uid = kauth_cred_geteuid(curproc->p_cred);
214 1.2 riastrad #else
215 1.13 riastrad client->pid = task_pid_vnr(current);
216 1.13 riastrad client->uid = overflowuid;
217 1.2 riastrad #endif
218 1.3 riastrad client->magic = 0;
219 1.3 riastrad client->iocs = 0;
220 1.1 riastrad
221 1.3 riastrad return 0;
222 1.3 riastrad } else {
223 1.3 riastrad return -EINVAL;
224 1.1 riastrad }
225 1.1 riastrad }
226 1.1 riastrad
227 1.5 riastrad /*
228 1.1 riastrad * Get statistics information.
229 1.1 riastrad *
230 1.1 riastrad * \param inode device inode.
231 1.1 riastrad * \param file_priv DRM file private.
232 1.1 riastrad * \param cmd command.
233 1.1 riastrad * \param arg user argument, pointing to a drm_stats structure.
234 1.1 riastrad *
235 1.1 riastrad * \return zero on success or a negative number on failure.
236 1.1 riastrad */
237 1.5 riastrad static int drm_getstats(struct drm_device *dev, void *data,
238 1.1 riastrad struct drm_file *file_priv)
239 1.1 riastrad {
240 1.1 riastrad struct drm_stats *stats = data;
241 1.1 riastrad
242 1.3 riastrad /* Clear stats to prevent userspace from eating its stack garbage. */
243 1.1 riastrad memset(stats, 0, sizeof(*stats));
244 1.1 riastrad
245 1.1 riastrad return 0;
246 1.1 riastrad }
247 1.1 riastrad
248 1.5 riastrad /*
249 1.1 riastrad * Get device/driver capabilities
250 1.1 riastrad */
251 1.5 riastrad static int drm_getcap(struct drm_device *dev, void *data, struct drm_file *file_priv)
252 1.1 riastrad {
253 1.1 riastrad struct drm_get_cap *req = data;
254 1.13 riastrad struct drm_crtc *crtc;
255 1.1 riastrad
256 1.1 riastrad req->value = 0;
257 1.13 riastrad
258 1.13 riastrad /* Only some caps make sense with UMS/render-only drivers. */
259 1.13 riastrad switch (req->capability) {
260 1.13 riastrad case DRM_CAP_TIMESTAMP_MONOTONIC:
261 1.13 riastrad req->value = 1;
262 1.13 riastrad return 0;
263 1.13 riastrad case DRM_CAP_PRIME:
264 1.13 riastrad req->value |= dev->driver->prime_fd_to_handle ? DRM_PRIME_CAP_IMPORT : 0;
265 1.13 riastrad req->value |= dev->driver->prime_handle_to_fd ? DRM_PRIME_CAP_EXPORT : 0;
266 1.13 riastrad return 0;
267 1.13 riastrad case DRM_CAP_SYNCOBJ:
268 1.13 riastrad req->value = drm_core_check_feature(dev, DRIVER_SYNCOBJ);
269 1.13 riastrad return 0;
270 1.13 riastrad case DRM_CAP_SYNCOBJ_TIMELINE:
271 1.13 riastrad req->value = drm_core_check_feature(dev, DRIVER_SYNCOBJ_TIMELINE);
272 1.13 riastrad return 0;
273 1.13 riastrad }
274 1.13 riastrad
275 1.13 riastrad /* Other caps only work with KMS drivers */
276 1.13 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
277 1.13 riastrad return -EOPNOTSUPP;
278 1.13 riastrad
279 1.1 riastrad switch (req->capability) {
280 1.1 riastrad case DRM_CAP_DUMB_BUFFER:
281 1.1 riastrad if (dev->driver->dumb_create)
282 1.1 riastrad req->value = 1;
283 1.1 riastrad break;
284 1.1 riastrad case DRM_CAP_VBLANK_HIGH_CRTC:
285 1.1 riastrad req->value = 1;
286 1.1 riastrad break;
287 1.1 riastrad case DRM_CAP_DUMB_PREFERRED_DEPTH:
288 1.1 riastrad req->value = dev->mode_config.preferred_depth;
289 1.1 riastrad break;
290 1.1 riastrad case DRM_CAP_DUMB_PREFER_SHADOW:
291 1.1 riastrad req->value = dev->mode_config.prefer_shadow;
292 1.1 riastrad break;
293 1.3 riastrad case DRM_CAP_ASYNC_PAGE_FLIP:
294 1.3 riastrad req->value = dev->mode_config.async_page_flip;
295 1.3 riastrad break;
296 1.13 riastrad case DRM_CAP_PAGE_FLIP_TARGET:
297 1.13 riastrad req->value = 1;
298 1.13 riastrad drm_for_each_crtc(crtc, dev) {
299 1.13 riastrad if (!crtc->funcs->page_flip_target)
300 1.13 riastrad req->value = 0;
301 1.13 riastrad }
302 1.13 riastrad break;
303 1.3 riastrad case DRM_CAP_CURSOR_WIDTH:
304 1.3 riastrad if (dev->mode_config.cursor_width)
305 1.3 riastrad req->value = dev->mode_config.cursor_width;
306 1.3 riastrad else
307 1.3 riastrad req->value = 64;
308 1.3 riastrad break;
309 1.3 riastrad case DRM_CAP_CURSOR_HEIGHT:
310 1.3 riastrad if (dev->mode_config.cursor_height)
311 1.3 riastrad req->value = dev->mode_config.cursor_height;
312 1.3 riastrad else
313 1.3 riastrad req->value = 64;
314 1.3 riastrad break;
315 1.5 riastrad case DRM_CAP_ADDFB2_MODIFIERS:
316 1.5 riastrad req->value = dev->mode_config.allow_fb_modifiers;
317 1.5 riastrad break;
318 1.13 riastrad case DRM_CAP_CRTC_IN_VBLANK_EVENT:
319 1.13 riastrad req->value = 1;
320 1.13 riastrad break;
321 1.3 riastrad default:
322 1.3 riastrad return -EINVAL;
323 1.3 riastrad }
324 1.3 riastrad return 0;
325 1.3 riastrad }
326 1.3 riastrad
327 1.5 riastrad /*
328 1.3 riastrad * Set device/driver capabilities
329 1.3 riastrad */
330 1.5 riastrad static int
331 1.3 riastrad drm_setclientcap(struct drm_device *dev, void *data, struct drm_file *file_priv)
332 1.3 riastrad {
333 1.3 riastrad struct drm_set_client_cap *req = data;
334 1.3 riastrad
335 1.13 riastrad /* No render-only settable capabilities for now */
336 1.13 riastrad
337 1.13 riastrad /* Below caps that only works with KMS drivers */
338 1.13 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
339 1.13 riastrad return -EOPNOTSUPP;
340 1.13 riastrad
341 1.3 riastrad switch (req->capability) {
342 1.3 riastrad case DRM_CLIENT_CAP_STEREO_3D:
343 1.3 riastrad if (req->value > 1)
344 1.3 riastrad return -EINVAL;
345 1.3 riastrad file_priv->stereo_allowed = req->value;
346 1.3 riastrad break;
347 1.3 riastrad case DRM_CLIENT_CAP_UNIVERSAL_PLANES:
348 1.5 riastrad if (req->value > 1)
349 1.5 riastrad return -EINVAL;
350 1.5 riastrad file_priv->universal_planes = req->value;
351 1.5 riastrad break;
352 1.5 riastrad case DRM_CLIENT_CAP_ATOMIC:
353 1.5 riastrad if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
354 1.13 riastrad return -EOPNOTSUPP;
355 1.13 riastrad /* The modesetting DDX has a totally broken idea of atomic. */
356 1.13 riastrad if (current->comm[0] == 'X' && req->value == 1) {
357 1.13 riastrad pr_info("broken atomic modeset userspace detected, disabling atomic\n");
358 1.13 riastrad return -EOPNOTSUPP;
359 1.13 riastrad }
360 1.13 riastrad if (req->value > 2)
361 1.13 riastrad return -EINVAL;
362 1.13 riastrad file_priv->atomic = req->value;
363 1.13 riastrad file_priv->universal_planes = req->value;
364 1.13 riastrad /*
365 1.13 riastrad * No atomic user-space blows up on aspect ratio mode bits.
366 1.13 riastrad */
367 1.13 riastrad file_priv->aspect_ratio_allowed = req->value;
368 1.13 riastrad break;
369 1.13 riastrad case DRM_CLIENT_CAP_ASPECT_RATIO:
370 1.13 riastrad if (req->value > 1)
371 1.13 riastrad return -EINVAL;
372 1.13 riastrad file_priv->aspect_ratio_allowed = req->value;
373 1.13 riastrad break;
374 1.13 riastrad case DRM_CLIENT_CAP_WRITEBACK_CONNECTORS:
375 1.13 riastrad if (!file_priv->atomic)
376 1.3 riastrad return -EINVAL;
377 1.3 riastrad if (req->value > 1)
378 1.3 riastrad return -EINVAL;
379 1.13 riastrad file_priv->writeback_connectors = req->value;
380 1.3 riastrad break;
381 1.1 riastrad default:
382 1.1 riastrad return -EINVAL;
383 1.1 riastrad }
384 1.3 riastrad
385 1.1 riastrad return 0;
386 1.1 riastrad }
387 1.1 riastrad
388 1.5 riastrad /*
389 1.1 riastrad * Setversion ioctl.
390 1.1 riastrad *
391 1.1 riastrad * \param inode device inode.
392 1.1 riastrad * \param file_priv DRM file private.
393 1.1 riastrad * \param cmd command.
394 1.1 riastrad * \param arg user argument, pointing to a drm_lock structure.
395 1.1 riastrad * \return zero on success or negative number on failure.
396 1.1 riastrad *
397 1.1 riastrad * Sets the requested interface version
398 1.1 riastrad */
399 1.5 riastrad static int drm_setversion(struct drm_device *dev, void *data, struct drm_file *file_priv)
400 1.1 riastrad {
401 1.1 riastrad struct drm_set_version *sv = data;
402 1.1 riastrad int if_version, retcode = 0;
403 1.1 riastrad
404 1.13 riastrad mutex_lock(&dev->master_mutex);
405 1.1 riastrad if (sv->drm_di_major != -1) {
406 1.1 riastrad if (sv->drm_di_major != DRM_IF_MAJOR ||
407 1.1 riastrad sv->drm_di_minor < 0 || sv->drm_di_minor > DRM_IF_MINOR) {
408 1.1 riastrad retcode = -EINVAL;
409 1.1 riastrad goto done;
410 1.1 riastrad }
411 1.1 riastrad if_version = DRM_IF_VERSION(sv->drm_di_major,
412 1.1 riastrad sv->drm_di_minor);
413 1.1 riastrad dev->if_version = max(if_version, dev->if_version);
414 1.1 riastrad if (sv->drm_di_minor >= 1) {
415 1.1 riastrad /*
416 1.1 riastrad * Version 1.1 includes tying of DRM to specific device
417 1.1 riastrad * Version 1.4 has proper PCI domain support
418 1.1 riastrad */
419 1.1 riastrad retcode = drm_set_busid(dev, file_priv);
420 1.1 riastrad if (retcode)
421 1.1 riastrad goto done;
422 1.1 riastrad }
423 1.1 riastrad }
424 1.1 riastrad
425 1.1 riastrad if (sv->drm_dd_major != -1) {
426 1.1 riastrad if (sv->drm_dd_major != dev->driver->major ||
427 1.1 riastrad sv->drm_dd_minor < 0 || sv->drm_dd_minor >
428 1.1 riastrad dev->driver->minor) {
429 1.1 riastrad retcode = -EINVAL;
430 1.1 riastrad goto done;
431 1.1 riastrad }
432 1.1 riastrad }
433 1.1 riastrad
434 1.1 riastrad done:
435 1.1 riastrad sv->drm_di_major = DRM_IF_MAJOR;
436 1.1 riastrad sv->drm_di_minor = DRM_IF_MINOR;
437 1.1 riastrad sv->drm_dd_major = dev->driver->major;
438 1.1 riastrad sv->drm_dd_minor = dev->driver->minor;
439 1.13 riastrad mutex_unlock(&dev->master_mutex);
440 1.1 riastrad
441 1.1 riastrad return retcode;
442 1.1 riastrad }
443 1.1 riastrad
444 1.5 riastrad /**
445 1.5 riastrad * drm_noop - DRM no-op ioctl implemntation
446 1.5 riastrad * @dev: DRM device for the ioctl
447 1.5 riastrad * @data: data pointer for the ioctl
448 1.5 riastrad * @file_priv: DRM file for the ioctl call
449 1.5 riastrad *
450 1.5 riastrad * This no-op implementation for drm ioctls is useful for deprecated
451 1.5 riastrad * functionality where we can't return a failure code because existing userspace
452 1.5 riastrad * checks the result of the ioctl, but doesn't care about the action.
453 1.5 riastrad *
454 1.5 riastrad * Always returns successfully with 0.
455 1.5 riastrad */
456 1.1 riastrad int drm_noop(struct drm_device *dev, void *data,
457 1.1 riastrad struct drm_file *file_priv)
458 1.1 riastrad {
459 1.1 riastrad DRM_DEBUG("\n");
460 1.1 riastrad return 0;
461 1.1 riastrad }
462 1.1 riastrad EXPORT_SYMBOL(drm_noop);
463 1.5 riastrad
464 1.5 riastrad /**
465 1.5 riastrad * drm_invalid_op - DRM invalid ioctl implemntation
466 1.5 riastrad * @dev: DRM device for the ioctl
467 1.5 riastrad * @data: data pointer for the ioctl
468 1.5 riastrad * @file_priv: DRM file for the ioctl call
469 1.5 riastrad *
470 1.5 riastrad * This no-op implementation for drm ioctls is useful for deprecated
471 1.5 riastrad * functionality where we really don't want to allow userspace to call the ioctl
472 1.5 riastrad * any more. This is the case for old ums interfaces for drivers that
473 1.5 riastrad * transitioned to kms gradually and so kept the old legacy tables around. This
474 1.5 riastrad * only applies to radeon and i915 kms drivers, other drivers shouldn't need to
475 1.5 riastrad * use this function.
476 1.5 riastrad *
477 1.5 riastrad * Always fails with a return value of -EINVAL.
478 1.5 riastrad */
479 1.5 riastrad int drm_invalid_op(struct drm_device *dev, void *data,
480 1.5 riastrad struct drm_file *file_priv)
481 1.5 riastrad {
482 1.5 riastrad return -EINVAL;
483 1.5 riastrad }
484 1.5 riastrad EXPORT_SYMBOL(drm_invalid_op);
485 1.5 riastrad
486 1.5 riastrad /*
487 1.5 riastrad * Copy and IOCTL return string to user space
488 1.5 riastrad */
489 1.5 riastrad static int drm_copy_field(char __user *buf, size_t *buf_len, const char *value)
490 1.5 riastrad {
491 1.5 riastrad int len;
492 1.5 riastrad
493 1.5 riastrad /* don't overflow userbuf */
494 1.5 riastrad len = strlen(value);
495 1.5 riastrad if (len > *buf_len)
496 1.5 riastrad len = *buf_len;
497 1.5 riastrad
498 1.5 riastrad /* let userspace know exact length of driver value (which could be
499 1.5 riastrad * larger than the userspace-supplied buffer) */
500 1.5 riastrad *buf_len = strlen(value);
501 1.5 riastrad
502 1.5 riastrad /* finally, try filling in the userbuf */
503 1.5 riastrad if (len && buf)
504 1.5 riastrad if (copy_to_user(buf, value, len))
505 1.5 riastrad return -EFAULT;
506 1.5 riastrad return 0;
507 1.5 riastrad }
508 1.5 riastrad
509 1.5 riastrad /*
510 1.5 riastrad * Get version information
511 1.5 riastrad *
512 1.5 riastrad * \param inode device inode.
513 1.5 riastrad * \param filp file pointer.
514 1.5 riastrad * \param cmd command.
515 1.5 riastrad * \param arg user argument, pointing to a drm_version structure.
516 1.5 riastrad * \return zero on success or negative number on failure.
517 1.5 riastrad *
518 1.5 riastrad * Fills in the version information in \p arg.
519 1.5 riastrad */
520 1.13 riastrad int drm_version(struct drm_device *dev, void *data,
521 1.5 riastrad struct drm_file *file_priv)
522 1.5 riastrad {
523 1.5 riastrad struct drm_version *version = data;
524 1.5 riastrad int err;
525 1.5 riastrad
526 1.5 riastrad version->version_major = dev->driver->major;
527 1.5 riastrad version->version_minor = dev->driver->minor;
528 1.5 riastrad version->version_patchlevel = dev->driver->patchlevel;
529 1.5 riastrad err = drm_copy_field(version->name, &version->name_len,
530 1.5 riastrad dev->driver->name);
531 1.5 riastrad if (!err)
532 1.5 riastrad err = drm_copy_field(version->date, &version->date_len,
533 1.5 riastrad dev->driver->date);
534 1.5 riastrad if (!err)
535 1.5 riastrad err = drm_copy_field(version->desc, &version->desc_len,
536 1.5 riastrad dev->driver->desc);
537 1.5 riastrad
538 1.5 riastrad return err;
539 1.5 riastrad }
540 1.5 riastrad
541 1.13 riastrad /**
542 1.5 riastrad * drm_ioctl_permit - Check ioctl permissions against caller
543 1.5 riastrad *
544 1.5 riastrad * @flags: ioctl permission flags.
545 1.5 riastrad * @file_priv: Pointer to struct drm_file identifying the caller.
546 1.5 riastrad *
547 1.5 riastrad * Checks whether the caller is allowed to run an ioctl with the
548 1.13 riastrad * indicated permissions.
549 1.13 riastrad *
550 1.13 riastrad * Returns:
551 1.13 riastrad * Zero if allowed, -EACCES otherwise.
552 1.5 riastrad */
553 1.5 riastrad int drm_ioctl_permit(u32 flags, struct drm_file *file_priv)
554 1.5 riastrad {
555 1.5 riastrad /* ROOT_ONLY is only for CAP_SYS_ADMIN */
556 1.5 riastrad if (unlikely((flags & DRM_ROOT_ONLY) && !capable(CAP_SYS_ADMIN)))
557 1.5 riastrad return -EACCES;
558 1.5 riastrad
559 1.5 riastrad /* AUTH is only for authenticated or render client */
560 1.5 riastrad if (unlikely((flags & DRM_AUTH) && !drm_is_render_client(file_priv) &&
561 1.5 riastrad !file_priv->authenticated))
562 1.5 riastrad return -EACCES;
563 1.5 riastrad
564 1.5 riastrad /* MASTER is only for master or control clients */
565 1.13 riastrad if (unlikely((flags & DRM_MASTER) &&
566 1.13 riastrad !drm_is_current_master(file_priv)))
567 1.5 riastrad return -EACCES;
568 1.5 riastrad
569 1.5 riastrad /* Render clients must be explicitly allowed */
570 1.5 riastrad if (unlikely(!(flags & DRM_RENDER_ALLOW) &&
571 1.5 riastrad drm_is_render_client(file_priv)))
572 1.5 riastrad return -EACCES;
573 1.5 riastrad
574 1.5 riastrad return 0;
575 1.5 riastrad }
576 1.5 riastrad EXPORT_SYMBOL(drm_ioctl_permit);
577 1.5 riastrad
578 1.5 riastrad #define DRM_IOCTL_DEF(ioctl, _func, _flags) \
579 1.5 riastrad [DRM_IOCTL_NR(ioctl)] = { \
580 1.5 riastrad .cmd = ioctl, \
581 1.5 riastrad .func = _func, \
582 1.5 riastrad .flags = _flags, \
583 1.5 riastrad .name = #ioctl \
584 1.5 riastrad }
585 1.5 riastrad
586 1.13 riastrad #if IS_ENABLED(CONFIG_DRM_LEGACY)
587 1.13 riastrad #define DRM_LEGACY_IOCTL_DEF(ioctl, _func, _flags) DRM_IOCTL_DEF(ioctl, _func, _flags)
588 1.13 riastrad #else
589 1.13 riastrad #define DRM_LEGACY_IOCTL_DEF(ioctl, _func, _flags) DRM_IOCTL_DEF(ioctl, drm_invalid_op, _flags)
590 1.13 riastrad #endif
591 1.13 riastrad
592 1.5 riastrad /* Ioctl table */
593 1.5 riastrad static const struct drm_ioctl_desc drm_ioctls[] = {
594 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_VERSION, drm_version, DRM_RENDER_ALLOW),
595 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GET_UNIQUE, drm_getunique, 0),
596 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GET_MAGIC, drm_getmagic, 0),
597 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_IRQ_BUSID, drm_irq_by_busid, DRM_MASTER|DRM_ROOT_ONLY),
598 1.13 riastrad
599 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_GET_MAP, drm_legacy_getmap_ioctl, 0),
600 1.13 riastrad
601 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GET_CLIENT, drm_getclient, 0),
602 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GET_STATS, drm_getstats, 0),
603 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GET_CAP, drm_getcap, DRM_RENDER_ALLOW),
604 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SET_CLIENT_CAP, drm_setclientcap, 0),
605 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SET_VERSION, drm_setversion, DRM_MASTER),
606 1.5 riastrad
607 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SET_UNIQUE, drm_invalid_op, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
608 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_BLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
609 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_UNBLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
610 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AUTH_MAGIC, drm_authmagic, DRM_MASTER),
611 1.5 riastrad
612 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_ADD_MAP, drm_legacy_addmap_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
613 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_RM_MAP, drm_legacy_rmmap_ioctl, DRM_AUTH),
614 1.5 riastrad
615 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_SET_SAREA_CTX, drm_legacy_setsareactx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
616 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_GET_SAREA_CTX, drm_legacy_getsareactx, DRM_AUTH),
617 1.5 riastrad
618 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SET_MASTER, drm_setmaster_ioctl, DRM_ROOT_ONLY),
619 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_DROP_MASTER, drm_dropmaster_ioctl, DRM_ROOT_ONLY),
620 1.5 riastrad
621 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_ADD_CTX, drm_legacy_addctx, DRM_AUTH|DRM_ROOT_ONLY),
622 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_RM_CTX, drm_legacy_rmctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
623 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_MOD_CTX, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
624 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_GET_CTX, drm_legacy_getctx, DRM_AUTH),
625 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_SWITCH_CTX, drm_legacy_switchctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
626 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_NEW_CTX, drm_legacy_newctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
627 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_RES_CTX, drm_legacy_resctx, DRM_AUTH),
628 1.5 riastrad
629 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_ADD_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
630 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_RM_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
631 1.5 riastrad
632 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_LOCK, drm_legacy_lock, DRM_AUTH),
633 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_UNLOCK, drm_legacy_unlock, DRM_AUTH),
634 1.5 riastrad
635 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_FINISH, drm_noop, DRM_AUTH),
636 1.5 riastrad
637 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_ADD_BUFS, drm_legacy_addbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
638 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_MARK_BUFS, drm_legacy_markbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
639 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_INFO_BUFS, drm_legacy_infobufs, DRM_AUTH),
640 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_MAP_BUFS, drm_legacy_mapbufs, DRM_AUTH),
641 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_FREE_BUFS, drm_legacy_freebufs, DRM_AUTH),
642 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_DMA, drm_legacy_dma_ioctl, DRM_AUTH),
643 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_CONTROL, drm_legacy_irq_control, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
644 1.5 riastrad
645 1.5 riastrad #if IS_ENABLED(CONFIG_AGP)
646 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AGP_ACQUIRE, drm_agp_acquire_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
647 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AGP_RELEASE, drm_agp_release_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
648 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AGP_ENABLE, drm_agp_enable_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
649 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AGP_INFO, drm_agp_info_ioctl, DRM_AUTH),
650 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AGP_ALLOC, drm_agp_alloc_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
651 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AGP_FREE, drm_agp_free_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
652 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AGP_BIND, drm_agp_bind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
653 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AGP_UNBIND, drm_agp_unbind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
654 1.5 riastrad #endif
655 1.5 riastrad
656 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_SG_ALLOC, drm_legacy_sg_alloc, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
657 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_SG_FREE, drm_legacy_sg_free, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
658 1.5 riastrad
659 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_WAIT_VBLANK, drm_wait_vblank_ioctl, DRM_UNLOCKED),
660 1.5 riastrad
661 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODESET_CTL, drm_legacy_modeset_ctl_ioctl, 0),
662 1.5 riastrad
663 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_UPDATE_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
664 1.5 riastrad
665 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GEM_CLOSE, drm_gem_close_ioctl, DRM_RENDER_ALLOW),
666 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GEM_FLINK, drm_gem_flink_ioctl, DRM_AUTH),
667 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GEM_OPEN, drm_gem_open_ioctl, DRM_AUTH),
668 1.13 riastrad
669 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETRESOURCES, drm_mode_getresources, 0),
670 1.13 riastrad
671 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_PRIME_HANDLE_TO_FD, drm_prime_handle_to_fd_ioctl, DRM_RENDER_ALLOW),
672 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_PRIME_FD_TO_HANDLE, drm_prime_fd_to_handle_ioctl, DRM_RENDER_ALLOW),
673 1.13 riastrad
674 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANERESOURCES, drm_mode_getplane_res, 0),
675 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCRTC, drm_mode_getcrtc, 0),
676 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETCRTC, drm_mode_setcrtc, DRM_MASTER),
677 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANE, drm_mode_getplane, 0),
678 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETPLANE, drm_mode_setplane, DRM_MASTER),
679 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_CURSOR, drm_mode_cursor_ioctl, DRM_MASTER),
680 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETGAMMA, drm_mode_gamma_get_ioctl, 0),
681 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETGAMMA, drm_mode_gamma_set_ioctl, DRM_MASTER),
682 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETENCODER, drm_mode_getencoder, 0),
683 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCONNECTOR, drm_mode_getconnector, 0),
684 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_ATTACHMODE, drm_noop, DRM_MASTER),
685 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_DETACHMODE, drm_noop, DRM_MASTER),
686 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPROPERTY, drm_mode_getproperty_ioctl, 0),
687 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETPROPERTY, drm_connector_property_set_ioctl, DRM_MASTER),
688 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPROPBLOB, drm_mode_getblob_ioctl, 0),
689 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETFB, drm_mode_getfb, 0),
690 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_ADDFB, drm_mode_addfb_ioctl, 0),
691 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_ADDFB2, drm_mode_addfb2_ioctl, 0),
692 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_RMFB, drm_mode_rmfb_ioctl, 0),
693 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_PAGE_FLIP, drm_mode_page_flip_ioctl, DRM_MASTER),
694 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_DIRTYFB, drm_mode_dirtyfb_ioctl, DRM_MASTER),
695 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_CREATE_DUMB, drm_mode_create_dumb_ioctl, 0),
696 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_MAP_DUMB, drm_mode_mmap_dumb_ioctl, 0),
697 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_DESTROY_DUMB, drm_mode_destroy_dumb_ioctl, 0),
698 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_OBJ_GETPROPERTIES, drm_mode_obj_get_properties_ioctl, 0),
699 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_OBJ_SETPROPERTY, drm_mode_obj_set_property_ioctl, DRM_MASTER),
700 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_CURSOR2, drm_mode_cursor2_ioctl, DRM_MASTER),
701 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_ATOMIC, drm_mode_atomic_ioctl, DRM_MASTER),
702 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_CREATEPROPBLOB, drm_mode_createblob_ioctl, 0),
703 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_DESTROYPROPBLOB, drm_mode_destroyblob_ioctl, 0),
704 1.13 riastrad
705 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_CREATE, drm_syncobj_create_ioctl,
706 1.13 riastrad DRM_RENDER_ALLOW),
707 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_DESTROY, drm_syncobj_destroy_ioctl,
708 1.13 riastrad DRM_RENDER_ALLOW),
709 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, drm_syncobj_handle_to_fd_ioctl,
710 1.13 riastrad DRM_RENDER_ALLOW),
711 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, drm_syncobj_fd_to_handle_ioctl,
712 1.13 riastrad DRM_RENDER_ALLOW),
713 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_TRANSFER, drm_syncobj_transfer_ioctl,
714 1.13 riastrad DRM_RENDER_ALLOW),
715 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_WAIT, drm_syncobj_wait_ioctl,
716 1.13 riastrad DRM_RENDER_ALLOW),
717 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_TIMELINE_WAIT, drm_syncobj_timeline_wait_ioctl,
718 1.13 riastrad DRM_RENDER_ALLOW),
719 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_RESET, drm_syncobj_reset_ioctl,
720 1.13 riastrad DRM_RENDER_ALLOW),
721 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_SIGNAL, drm_syncobj_signal_ioctl,
722 1.13 riastrad DRM_RENDER_ALLOW),
723 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_TIMELINE_SIGNAL, drm_syncobj_timeline_signal_ioctl,
724 1.13 riastrad DRM_RENDER_ALLOW),
725 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_QUERY, drm_syncobj_query_ioctl,
726 1.13 riastrad DRM_RENDER_ALLOW),
727 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_CRTC_GET_SEQUENCE, drm_crtc_get_sequence_ioctl, 0),
728 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_CRTC_QUEUE_SEQUENCE, drm_crtc_queue_sequence_ioctl, 0),
729 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_CREATE_LEASE, drm_mode_create_lease_ioctl, DRM_MASTER),
730 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_LIST_LESSEES, drm_mode_list_lessees_ioctl, DRM_MASTER),
731 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GET_LEASE, drm_mode_get_lease_ioctl, DRM_MASTER),
732 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_REVOKE_LEASE, drm_mode_revoke_lease_ioctl, DRM_MASTER),
733 1.5 riastrad };
734 1.5 riastrad
735 1.5 riastrad #define DRM_CORE_IOCTL_COUNT ARRAY_SIZE( drm_ioctls )
736 1.5 riastrad
737 1.5 riastrad /**
738 1.13 riastrad * DOC: driver specific ioctls
739 1.13 riastrad *
740 1.13 riastrad * First things first, driver private IOCTLs should only be needed for drivers
741 1.13 riastrad * supporting rendering. Kernel modesetting is all standardized, and extended
742 1.13 riastrad * through properties. There are a few exceptions in some existing drivers,
743 1.13 riastrad * which define IOCTL for use by the display DRM master, but they all predate
744 1.13 riastrad * properties.
745 1.13 riastrad *
746 1.13 riastrad * Now if you do have a render driver you always have to support it through
747 1.13 riastrad * driver private properties. There's a few steps needed to wire all the things
748 1.13 riastrad * up.
749 1.13 riastrad *
750 1.13 riastrad * First you need to define the structure for your IOCTL in your driver private
751 1.13 riastrad * UAPI header in ``include/uapi/drm/my_driver_drm.h``::
752 1.13 riastrad *
753 1.13 riastrad * struct my_driver_operation {
754 1.13 riastrad * u32 some_thing;
755 1.13 riastrad * u32 another_thing;
756 1.13 riastrad * };
757 1.13 riastrad *
758 1.13 riastrad * Please make sure that you follow all the best practices from
759 1.13 riastrad * ``Documentation/ioctl/botching-up-ioctls.rst``. Note that drm_ioctl()
760 1.13 riastrad * automatically zero-extends structures, hence make sure you can add more stuff
761 1.13 riastrad * at the end, i.e. don't put a variable sized array there.
762 1.13 riastrad *
763 1.13 riastrad * Then you need to define your IOCTL number, using one of DRM_IO(), DRM_IOR(),
764 1.13 riastrad * DRM_IOW() or DRM_IOWR(). It must start with the DRM_IOCTL\_ prefix::
765 1.13 riastrad *
766 1.13 riastrad * ##define DRM_IOCTL_MY_DRIVER_OPERATION \
767 1.13 riastrad * DRM_IOW(DRM_COMMAND_BASE, struct my_driver_operation)
768 1.13 riastrad *
769 1.13 riastrad * DRM driver private IOCTL must be in the range from DRM_COMMAND_BASE to
770 1.13 riastrad * DRM_COMMAND_END. Finally you need an array of &struct drm_ioctl_desc to wire
771 1.13 riastrad * up the handlers and set the access rights::
772 1.13 riastrad *
773 1.13 riastrad * static const struct drm_ioctl_desc my_driver_ioctls[] = {
774 1.13 riastrad * DRM_IOCTL_DEF_DRV(MY_DRIVER_OPERATION, my_driver_operation,
775 1.13 riastrad * DRM_AUTH|DRM_RENDER_ALLOW),
776 1.13 riastrad * };
777 1.13 riastrad *
778 1.13 riastrad * And then assign this to the &drm_driver.ioctls field in your driver
779 1.13 riastrad * structure.
780 1.13 riastrad *
781 1.13 riastrad * See the separate chapter on :ref:`file operations<drm_driver_fops>` for how
782 1.13 riastrad * the driver-specific IOCTLs are wired up.
783 1.13 riastrad */
784 1.13 riastrad
785 1.13 riastrad long drm_ioctl_kernel(struct file *file, drm_ioctl_t *func, void *kdata,
786 1.13 riastrad u32 flags)
787 1.13 riastrad {
788 1.15 riastrad #ifdef __NetBSD__
789 1.15 riastrad struct drm_file *file_priv = file->f_data;
790 1.15 riastrad #else
791 1.13 riastrad struct drm_file *file_priv = file->private_data;
792 1.15 riastrad #endif
793 1.13 riastrad struct drm_device *dev = file_priv->minor->dev;
794 1.13 riastrad int retcode;
795 1.13 riastrad
796 1.13 riastrad if (drm_dev_is_unplugged(dev))
797 1.13 riastrad return -ENODEV;
798 1.13 riastrad
799 1.13 riastrad retcode = drm_ioctl_permit(flags, file_priv);
800 1.13 riastrad if (unlikely(retcode))
801 1.13 riastrad return retcode;
802 1.13 riastrad
803 1.13 riastrad /* Enforce sane locking for modern driver ioctls. */
804 1.13 riastrad if (likely(!drm_core_check_feature(dev, DRIVER_LEGACY)) ||
805 1.13 riastrad (flags & DRM_UNLOCKED))
806 1.13 riastrad retcode = func(dev, kdata, file_priv);
807 1.13 riastrad else {
808 1.13 riastrad mutex_lock(&drm_global_mutex);
809 1.13 riastrad retcode = func(dev, kdata, file_priv);
810 1.13 riastrad mutex_unlock(&drm_global_mutex);
811 1.13 riastrad }
812 1.13 riastrad return retcode;
813 1.13 riastrad }
814 1.13 riastrad EXPORT_SYMBOL(drm_ioctl_kernel);
815 1.13 riastrad
816 1.13 riastrad /**
817 1.5 riastrad * drm_ioctl - ioctl callback implementation for DRM drivers
818 1.5 riastrad * @filp: file this ioctl is called on
819 1.5 riastrad * @cmd: ioctl cmd number
820 1.5 riastrad * @arg: user argument
821 1.5 riastrad *
822 1.13 riastrad * Looks up the ioctl function in the DRM core and the driver dispatch table,
823 1.13 riastrad * stored in &drm_driver.ioctls. It checks for necessary permission by calling
824 1.13 riastrad * drm_ioctl_permit(), and dispatches to the respective function.
825 1.5 riastrad *
826 1.5 riastrad * Returns:
827 1.5 riastrad * Zero on success, negative error code on failure.
828 1.5 riastrad */
829 1.6 riastrad #ifdef __NetBSD__
830 1.6 riastrad #include <sys/file.h>
831 1.6 riastrad int
832 1.6 riastrad drm_ioctl(struct file *fp, unsigned long cmd, void *data)
833 1.6 riastrad {
834 1.8 riastrad char stackbuf[128];
835 1.8 riastrad char *buf = stackbuf;
836 1.11 maya void *data0 = data;
837 1.6 riastrad struct drm_file *const file = fp->f_data;
838 1.6 riastrad const unsigned int nr = DRM_IOCTL_NR(cmd);
839 1.6 riastrad int error;
840 1.6 riastrad
841 1.6 riastrad switch (cmd) {
842 1.6 riastrad case FIONBIO:
843 1.6 riastrad case FIOASYNC:
844 1.6 riastrad return 0;
845 1.6 riastrad default:
846 1.6 riastrad break;
847 1.6 riastrad }
848 1.6 riastrad
849 1.6 riastrad if (IOCGROUP(cmd) != DRM_IOCTL_BASE)
850 1.6 riastrad return EINVAL;
851 1.6 riastrad
852 1.6 riastrad KASSERT(file != NULL);
853 1.6 riastrad KASSERT(file->minor != NULL);
854 1.6 riastrad KASSERT(file->minor->dev != NULL);
855 1.6 riastrad struct drm_device *const dev = file->minor->dev;
856 1.6 riastrad const struct drm_ioctl_desc *ioctl;
857 1.6 riastrad
858 1.6 riastrad if (drm_device_is_unplugged(dev))
859 1.6 riastrad return ENXIO;
860 1.6 riastrad
861 1.6 riastrad const bool is_driver_ioctl =
862 1.6 riastrad (DRM_COMMAND_BASE <= nr) && (nr < DRM_COMMAND_END);
863 1.6 riastrad
864 1.6 riastrad if (is_driver_ioctl) {
865 1.6 riastrad const unsigned int driver_nr = nr - DRM_COMMAND_BASE;
866 1.6 riastrad if (driver_nr >= dev->driver->num_ioctls)
867 1.6 riastrad return EINVAL;
868 1.6 riastrad ioctl = &dev->driver->ioctls[driver_nr];
869 1.6 riastrad } else if (nr < __arraycount(drm_ioctls)) {
870 1.6 riastrad ioctl = &drm_ioctls[nr];
871 1.6 riastrad } else {
872 1.6 riastrad ioctl = NULL;
873 1.6 riastrad }
874 1.6 riastrad
875 1.6 riastrad if ((ioctl == NULL) || (ioctl->func == NULL))
876 1.6 riastrad return EINVAL;
877 1.6 riastrad
878 1.6 riastrad /* XXX errno Linux->NetBSD */
879 1.6 riastrad error = -drm_ioctl_permit(ioctl->flags, file);
880 1.6 riastrad if (error)
881 1.6 riastrad return error;
882 1.6 riastrad
883 1.8 riastrad /* If userland passed in too few bytes, zero-pad them. */
884 1.8 riastrad if (IOCPARM_LEN(cmd) < IOCPARM_LEN(ioctl->cmd)) {
885 1.8 riastrad /* 12-bit quantity, according to <sys/ioccom.h> */
886 1.8 riastrad KASSERT(IOCPARM_LEN(ioctl->cmd) <= 4096);
887 1.8 riastrad if (IOCPARM_LEN(ioctl->cmd) > sizeof stackbuf) {
888 1.8 riastrad buf = kmem_alloc(IOCPARM_LEN(ioctl->cmd), KM_NOSLEEP);
889 1.8 riastrad if (buf == NULL)
890 1.8 riastrad return ENOMEM;
891 1.8 riastrad }
892 1.8 riastrad memcpy(buf, data, IOCPARM_LEN(cmd));
893 1.8 riastrad memset(buf + IOCPARM_LEN(cmd), 0,
894 1.8 riastrad IOCPARM_LEN(ioctl->cmd) - IOCPARM_LEN(cmd));
895 1.11 maya data0 = buf;
896 1.8 riastrad }
897 1.8 riastrad
898 1.6 riastrad if ((drm_core_check_feature(dev, DRIVER_MODESET) && is_driver_ioctl) ||
899 1.6 riastrad ISSET(ioctl->flags, DRM_UNLOCKED)) {
900 1.6 riastrad /* XXX errno Linux->NetBSD */
901 1.11 maya error = -(*ioctl->func)(dev, data0, file);
902 1.6 riastrad } else {
903 1.6 riastrad mutex_lock(&drm_global_mutex);
904 1.6 riastrad /* XXX errno Linux->NetBSD */
905 1.11 maya error = -(*ioctl->func)(dev, data0, file);
906 1.6 riastrad mutex_unlock(&drm_global_mutex);
907 1.6 riastrad }
908 1.6 riastrad
909 1.11 maya /* If we used a temporary buffer, copy it back out. */
910 1.11 maya if (data != data0)
911 1.11 maya memcpy(data, data0, IOCPARM_LEN(cmd));
912 1.11 maya
913 1.8 riastrad /* If we had to allocate a heap buffer, free it. */
914 1.8 riastrad if (buf != stackbuf)
915 1.8 riastrad kmem_free(buf, IOCPARM_LEN(ioctl->cmd));
916 1.8 riastrad
917 1.6 riastrad return error;
918 1.6 riastrad }
919 1.6 riastrad #else
920 1.5 riastrad long drm_ioctl(struct file *filp,
921 1.5 riastrad unsigned int cmd, unsigned long arg)
922 1.5 riastrad {
923 1.5 riastrad struct drm_file *file_priv = filp->private_data;
924 1.5 riastrad struct drm_device *dev;
925 1.5 riastrad const struct drm_ioctl_desc *ioctl = NULL;
926 1.5 riastrad drm_ioctl_t *func;
927 1.5 riastrad unsigned int nr = DRM_IOCTL_NR(cmd);
928 1.5 riastrad int retcode = -EINVAL;
929 1.5 riastrad char stack_kdata[128];
930 1.5 riastrad char *kdata = NULL;
931 1.13 riastrad unsigned int in_size, out_size, drv_size, ksize;
932 1.5 riastrad bool is_driver_ioctl;
933 1.5 riastrad
934 1.5 riastrad dev = file_priv->minor->dev;
935 1.5 riastrad
936 1.13 riastrad if (drm_dev_is_unplugged(dev))
937 1.5 riastrad return -ENODEV;
938 1.5 riastrad
939 1.5 riastrad is_driver_ioctl = nr >= DRM_COMMAND_BASE && nr < DRM_COMMAND_END;
940 1.5 riastrad
941 1.5 riastrad if (is_driver_ioctl) {
942 1.5 riastrad /* driver ioctl */
943 1.13 riastrad unsigned int index = nr - DRM_COMMAND_BASE;
944 1.13 riastrad
945 1.13 riastrad if (index >= dev->driver->num_ioctls)
946 1.5 riastrad goto err_i1;
947 1.13 riastrad index = array_index_nospec(index, dev->driver->num_ioctls);
948 1.13 riastrad ioctl = &dev->driver->ioctls[index];
949 1.5 riastrad } else {
950 1.5 riastrad /* core ioctl */
951 1.5 riastrad if (nr >= DRM_CORE_IOCTL_COUNT)
952 1.5 riastrad goto err_i1;
953 1.13 riastrad nr = array_index_nospec(nr, DRM_CORE_IOCTL_COUNT);
954 1.5 riastrad ioctl = &drm_ioctls[nr];
955 1.5 riastrad }
956 1.5 riastrad
957 1.5 riastrad drv_size = _IOC_SIZE(ioctl->cmd);
958 1.13 riastrad out_size = in_size = _IOC_SIZE(cmd);
959 1.13 riastrad if ((cmd & ioctl->cmd & IOC_IN) == 0)
960 1.13 riastrad in_size = 0;
961 1.13 riastrad if ((cmd & ioctl->cmd & IOC_OUT) == 0)
962 1.13 riastrad out_size = 0;
963 1.13 riastrad ksize = max(max(in_size, out_size), drv_size);
964 1.5 riastrad
965 1.5 riastrad DRM_DEBUG("pid=%d, dev=0x%lx, auth=%d, %s\n",
966 1.5 riastrad task_pid_nr(current),
967 1.5 riastrad (long)old_encode_dev(file_priv->minor->kdev->devt),
968 1.5 riastrad file_priv->authenticated, ioctl->name);
969 1.5 riastrad
970 1.5 riastrad /* Do not trust userspace, use our own definition */
971 1.5 riastrad func = ioctl->func;
972 1.5 riastrad
973 1.5 riastrad if (unlikely(!func)) {
974 1.5 riastrad DRM_DEBUG("no function\n");
975 1.5 riastrad retcode = -EINVAL;
976 1.5 riastrad goto err_i1;
977 1.5 riastrad }
978 1.5 riastrad
979 1.13 riastrad if (ksize <= sizeof(stack_kdata)) {
980 1.13 riastrad kdata = stack_kdata;
981 1.13 riastrad } else {
982 1.13 riastrad kdata = kmalloc(ksize, GFP_KERNEL);
983 1.13 riastrad if (!kdata) {
984 1.13 riastrad retcode = -ENOMEM;
985 1.13 riastrad goto err_i1;
986 1.5 riastrad }
987 1.5 riastrad }
988 1.5 riastrad
989 1.13 riastrad if (copy_from_user(kdata, (void __user *)arg, in_size) != 0) {
990 1.13 riastrad retcode = -EFAULT;
991 1.13 riastrad goto err_i1;
992 1.5 riastrad }
993 1.5 riastrad
994 1.13 riastrad if (ksize > in_size)
995 1.13 riastrad memset(kdata + in_size, 0, ksize - in_size);
996 1.5 riastrad
997 1.13 riastrad retcode = drm_ioctl_kernel(filp, func, kdata, ioctl->flags);
998 1.13 riastrad if (copy_to_user((void __user *)arg, kdata, out_size) != 0)
999 1.13 riastrad retcode = -EFAULT;
1000 1.5 riastrad
1001 1.5 riastrad err_i1:
1002 1.5 riastrad if (!ioctl)
1003 1.5 riastrad DRM_DEBUG("invalid ioctl: pid=%d, dev=0x%lx, auth=%d, cmd=0x%02x, nr=0x%02x\n",
1004 1.5 riastrad task_pid_nr(current),
1005 1.5 riastrad (long)old_encode_dev(file_priv->minor->kdev->devt),
1006 1.5 riastrad file_priv->authenticated, cmd, nr);
1007 1.5 riastrad
1008 1.5 riastrad if (kdata != stack_kdata)
1009 1.5 riastrad kfree(kdata);
1010 1.5 riastrad if (retcode)
1011 1.13 riastrad DRM_DEBUG("pid=%d, ret = %d\n", task_pid_nr(current), retcode);
1012 1.5 riastrad return retcode;
1013 1.5 riastrad }
1014 1.6 riastrad #endif
1015 1.5 riastrad EXPORT_SYMBOL(drm_ioctl);
1016 1.5 riastrad
1017 1.5 riastrad /**
1018 1.5 riastrad * drm_ioctl_flags - Check for core ioctl and return ioctl permission flags
1019 1.5 riastrad * @nr: ioctl number
1020 1.5 riastrad * @flags: where to return the ioctl permission flags
1021 1.5 riastrad *
1022 1.5 riastrad * This ioctl is only used by the vmwgfx driver to augment the access checks
1023 1.5 riastrad * done by the drm core and insofar a pretty decent layering violation. This
1024 1.5 riastrad * shouldn't be used by any drivers.
1025 1.5 riastrad *
1026 1.5 riastrad * Returns:
1027 1.13 riastrad * True if the @nr corresponds to a DRM core ioctl number, false otherwise.
1028 1.5 riastrad */
1029 1.5 riastrad bool drm_ioctl_flags(unsigned int nr, unsigned int *flags)
1030 1.5 riastrad {
1031 1.5 riastrad if (nr >= DRM_COMMAND_BASE && nr < DRM_COMMAND_END)
1032 1.5 riastrad return false;
1033 1.5 riastrad
1034 1.5 riastrad if (nr >= DRM_CORE_IOCTL_COUNT)
1035 1.5 riastrad return false;
1036 1.13 riastrad nr = array_index_nospec(nr, DRM_CORE_IOCTL_COUNT);
1037 1.5 riastrad
1038 1.5 riastrad *flags = drm_ioctls[nr].flags;
1039 1.5 riastrad return true;
1040 1.5 riastrad }
1041 1.5 riastrad EXPORT_SYMBOL(drm_ioctl_flags);
1042