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