drm_ioctl.c revision 1.23 1 1.23 riastrad /* $NetBSD: drm_ioctl.c,v 1.23 2022/08/27 21:24:15 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.23 riastrad __KERNEL_RCSID(0, "$NetBSD: drm_ioctl.c,v 1.23 2022/08/27 21:24:15 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.21 riastrad if (dev->dev && dev_is_pci(dev->dev)) {
165 1.21 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.22 riastrad #ifdef __NetBSD__
357 1.22 riastrad if (current->p_comm[0] == 'X' && req->value == 1)
358 1.19 riastrad #else
359 1.22 riastrad if (current->comm[0] == 'X' && req->value == 1)
360 1.19 riastrad #endif
361 1.22 riastrad {
362 1.13 riastrad pr_info("broken atomic modeset userspace detected, disabling atomic\n");
363 1.13 riastrad return -EOPNOTSUPP;
364 1.13 riastrad }
365 1.13 riastrad if (req->value > 2)
366 1.13 riastrad return -EINVAL;
367 1.13 riastrad file_priv->atomic = req->value;
368 1.13 riastrad file_priv->universal_planes = req->value;
369 1.13 riastrad /*
370 1.13 riastrad * No atomic user-space blows up on aspect ratio mode bits.
371 1.13 riastrad */
372 1.13 riastrad file_priv->aspect_ratio_allowed = req->value;
373 1.13 riastrad break;
374 1.13 riastrad case DRM_CLIENT_CAP_ASPECT_RATIO:
375 1.13 riastrad if (req->value > 1)
376 1.13 riastrad return -EINVAL;
377 1.13 riastrad file_priv->aspect_ratio_allowed = req->value;
378 1.13 riastrad break;
379 1.13 riastrad case DRM_CLIENT_CAP_WRITEBACK_CONNECTORS:
380 1.13 riastrad if (!file_priv->atomic)
381 1.3 riastrad return -EINVAL;
382 1.3 riastrad if (req->value > 1)
383 1.3 riastrad return -EINVAL;
384 1.13 riastrad file_priv->writeback_connectors = req->value;
385 1.3 riastrad break;
386 1.1 riastrad default:
387 1.1 riastrad return -EINVAL;
388 1.1 riastrad }
389 1.3 riastrad
390 1.1 riastrad return 0;
391 1.1 riastrad }
392 1.1 riastrad
393 1.5 riastrad /*
394 1.1 riastrad * Setversion ioctl.
395 1.1 riastrad *
396 1.1 riastrad * \param inode device inode.
397 1.1 riastrad * \param file_priv DRM file private.
398 1.1 riastrad * \param cmd command.
399 1.1 riastrad * \param arg user argument, pointing to a drm_lock structure.
400 1.1 riastrad * \return zero on success or negative number on failure.
401 1.1 riastrad *
402 1.1 riastrad * Sets the requested interface version
403 1.1 riastrad */
404 1.5 riastrad static int drm_setversion(struct drm_device *dev, void *data, struct drm_file *file_priv)
405 1.1 riastrad {
406 1.1 riastrad struct drm_set_version *sv = data;
407 1.1 riastrad int if_version, retcode = 0;
408 1.1 riastrad
409 1.13 riastrad mutex_lock(&dev->master_mutex);
410 1.1 riastrad if (sv->drm_di_major != -1) {
411 1.1 riastrad if (sv->drm_di_major != DRM_IF_MAJOR ||
412 1.1 riastrad sv->drm_di_minor < 0 || sv->drm_di_minor > DRM_IF_MINOR) {
413 1.1 riastrad retcode = -EINVAL;
414 1.1 riastrad goto done;
415 1.1 riastrad }
416 1.1 riastrad if_version = DRM_IF_VERSION(sv->drm_di_major,
417 1.1 riastrad sv->drm_di_minor);
418 1.1 riastrad dev->if_version = max(if_version, dev->if_version);
419 1.1 riastrad if (sv->drm_di_minor >= 1) {
420 1.1 riastrad /*
421 1.1 riastrad * Version 1.1 includes tying of DRM to specific device
422 1.1 riastrad * Version 1.4 has proper PCI domain support
423 1.1 riastrad */
424 1.1 riastrad retcode = drm_set_busid(dev, file_priv);
425 1.1 riastrad if (retcode)
426 1.1 riastrad goto done;
427 1.1 riastrad }
428 1.1 riastrad }
429 1.1 riastrad
430 1.1 riastrad if (sv->drm_dd_major != -1) {
431 1.1 riastrad if (sv->drm_dd_major != dev->driver->major ||
432 1.1 riastrad sv->drm_dd_minor < 0 || sv->drm_dd_minor >
433 1.1 riastrad dev->driver->minor) {
434 1.1 riastrad retcode = -EINVAL;
435 1.1 riastrad goto done;
436 1.1 riastrad }
437 1.1 riastrad }
438 1.1 riastrad
439 1.1 riastrad done:
440 1.1 riastrad sv->drm_di_major = DRM_IF_MAJOR;
441 1.1 riastrad sv->drm_di_minor = DRM_IF_MINOR;
442 1.1 riastrad sv->drm_dd_major = dev->driver->major;
443 1.1 riastrad sv->drm_dd_minor = dev->driver->minor;
444 1.13 riastrad mutex_unlock(&dev->master_mutex);
445 1.1 riastrad
446 1.1 riastrad return retcode;
447 1.1 riastrad }
448 1.1 riastrad
449 1.5 riastrad /**
450 1.5 riastrad * drm_noop - DRM no-op ioctl implemntation
451 1.5 riastrad * @dev: DRM device for the ioctl
452 1.5 riastrad * @data: data pointer for the ioctl
453 1.5 riastrad * @file_priv: DRM file for the ioctl call
454 1.5 riastrad *
455 1.5 riastrad * This no-op implementation for drm ioctls is useful for deprecated
456 1.5 riastrad * functionality where we can't return a failure code because existing userspace
457 1.5 riastrad * checks the result of the ioctl, but doesn't care about the action.
458 1.5 riastrad *
459 1.5 riastrad * Always returns successfully with 0.
460 1.5 riastrad */
461 1.1 riastrad int drm_noop(struct drm_device *dev, void *data,
462 1.1 riastrad struct drm_file *file_priv)
463 1.1 riastrad {
464 1.1 riastrad DRM_DEBUG("\n");
465 1.1 riastrad return 0;
466 1.1 riastrad }
467 1.1 riastrad EXPORT_SYMBOL(drm_noop);
468 1.5 riastrad
469 1.5 riastrad /**
470 1.5 riastrad * drm_invalid_op - DRM invalid ioctl implemntation
471 1.5 riastrad * @dev: DRM device for the ioctl
472 1.5 riastrad * @data: data pointer for the ioctl
473 1.5 riastrad * @file_priv: DRM file for the ioctl call
474 1.5 riastrad *
475 1.5 riastrad * This no-op implementation for drm ioctls is useful for deprecated
476 1.5 riastrad * functionality where we really don't want to allow userspace to call the ioctl
477 1.5 riastrad * any more. This is the case for old ums interfaces for drivers that
478 1.5 riastrad * transitioned to kms gradually and so kept the old legacy tables around. This
479 1.5 riastrad * only applies to radeon and i915 kms drivers, other drivers shouldn't need to
480 1.5 riastrad * use this function.
481 1.5 riastrad *
482 1.5 riastrad * Always fails with a return value of -EINVAL.
483 1.5 riastrad */
484 1.5 riastrad int drm_invalid_op(struct drm_device *dev, void *data,
485 1.5 riastrad struct drm_file *file_priv)
486 1.5 riastrad {
487 1.5 riastrad return -EINVAL;
488 1.5 riastrad }
489 1.5 riastrad EXPORT_SYMBOL(drm_invalid_op);
490 1.5 riastrad
491 1.5 riastrad /*
492 1.5 riastrad * Copy and IOCTL return string to user space
493 1.5 riastrad */
494 1.5 riastrad static int drm_copy_field(char __user *buf, size_t *buf_len, const char *value)
495 1.5 riastrad {
496 1.5 riastrad int len;
497 1.5 riastrad
498 1.5 riastrad /* don't overflow userbuf */
499 1.5 riastrad len = strlen(value);
500 1.5 riastrad if (len > *buf_len)
501 1.5 riastrad len = *buf_len;
502 1.5 riastrad
503 1.5 riastrad /* let userspace know exact length of driver value (which could be
504 1.5 riastrad * larger than the userspace-supplied buffer) */
505 1.5 riastrad *buf_len = strlen(value);
506 1.5 riastrad
507 1.5 riastrad /* finally, try filling in the userbuf */
508 1.5 riastrad if (len && buf)
509 1.5 riastrad if (copy_to_user(buf, value, len))
510 1.5 riastrad return -EFAULT;
511 1.5 riastrad return 0;
512 1.5 riastrad }
513 1.5 riastrad
514 1.5 riastrad /*
515 1.5 riastrad * Get version information
516 1.5 riastrad *
517 1.5 riastrad * \param inode device inode.
518 1.5 riastrad * \param filp file pointer.
519 1.5 riastrad * \param cmd command.
520 1.5 riastrad * \param arg user argument, pointing to a drm_version structure.
521 1.5 riastrad * \return zero on success or negative number on failure.
522 1.5 riastrad *
523 1.5 riastrad * Fills in the version information in \p arg.
524 1.5 riastrad */
525 1.13 riastrad int drm_version(struct drm_device *dev, void *data,
526 1.5 riastrad struct drm_file *file_priv)
527 1.5 riastrad {
528 1.5 riastrad struct drm_version *version = data;
529 1.5 riastrad int err;
530 1.5 riastrad
531 1.5 riastrad version->version_major = dev->driver->major;
532 1.5 riastrad version->version_minor = dev->driver->minor;
533 1.5 riastrad version->version_patchlevel = dev->driver->patchlevel;
534 1.5 riastrad err = drm_copy_field(version->name, &version->name_len,
535 1.5 riastrad dev->driver->name);
536 1.5 riastrad if (!err)
537 1.5 riastrad err = drm_copy_field(version->date, &version->date_len,
538 1.5 riastrad dev->driver->date);
539 1.5 riastrad if (!err)
540 1.5 riastrad err = drm_copy_field(version->desc, &version->desc_len,
541 1.5 riastrad dev->driver->desc);
542 1.5 riastrad
543 1.5 riastrad return err;
544 1.5 riastrad }
545 1.5 riastrad
546 1.13 riastrad /**
547 1.5 riastrad * drm_ioctl_permit - Check ioctl permissions against caller
548 1.5 riastrad *
549 1.5 riastrad * @flags: ioctl permission flags.
550 1.5 riastrad * @file_priv: Pointer to struct drm_file identifying the caller.
551 1.5 riastrad *
552 1.5 riastrad * Checks whether the caller is allowed to run an ioctl with the
553 1.13 riastrad * indicated permissions.
554 1.13 riastrad *
555 1.13 riastrad * Returns:
556 1.13 riastrad * Zero if allowed, -EACCES otherwise.
557 1.5 riastrad */
558 1.5 riastrad int drm_ioctl_permit(u32 flags, struct drm_file *file_priv)
559 1.5 riastrad {
560 1.5 riastrad /* ROOT_ONLY is only for CAP_SYS_ADMIN */
561 1.5 riastrad if (unlikely((flags & DRM_ROOT_ONLY) && !capable(CAP_SYS_ADMIN)))
562 1.5 riastrad return -EACCES;
563 1.5 riastrad
564 1.5 riastrad /* AUTH is only for authenticated or render client */
565 1.5 riastrad if (unlikely((flags & DRM_AUTH) && !drm_is_render_client(file_priv) &&
566 1.5 riastrad !file_priv->authenticated))
567 1.5 riastrad return -EACCES;
568 1.5 riastrad
569 1.5 riastrad /* MASTER is only for master or control clients */
570 1.13 riastrad if (unlikely((flags & DRM_MASTER) &&
571 1.13 riastrad !drm_is_current_master(file_priv)))
572 1.5 riastrad return -EACCES;
573 1.5 riastrad
574 1.5 riastrad /* Render clients must be explicitly allowed */
575 1.5 riastrad if (unlikely(!(flags & DRM_RENDER_ALLOW) &&
576 1.5 riastrad drm_is_render_client(file_priv)))
577 1.5 riastrad return -EACCES;
578 1.5 riastrad
579 1.5 riastrad return 0;
580 1.5 riastrad }
581 1.5 riastrad EXPORT_SYMBOL(drm_ioctl_permit);
582 1.5 riastrad
583 1.5 riastrad #define DRM_IOCTL_DEF(ioctl, _func, _flags) \
584 1.5 riastrad [DRM_IOCTL_NR(ioctl)] = { \
585 1.5 riastrad .cmd = ioctl, \
586 1.5 riastrad .func = _func, \
587 1.5 riastrad .flags = _flags, \
588 1.5 riastrad .name = #ioctl \
589 1.5 riastrad }
590 1.5 riastrad
591 1.13 riastrad #if IS_ENABLED(CONFIG_DRM_LEGACY)
592 1.13 riastrad #define DRM_LEGACY_IOCTL_DEF(ioctl, _func, _flags) DRM_IOCTL_DEF(ioctl, _func, _flags)
593 1.13 riastrad #else
594 1.13 riastrad #define DRM_LEGACY_IOCTL_DEF(ioctl, _func, _flags) DRM_IOCTL_DEF(ioctl, drm_invalid_op, _flags)
595 1.13 riastrad #endif
596 1.13 riastrad
597 1.5 riastrad /* Ioctl table */
598 1.5 riastrad static const struct drm_ioctl_desc drm_ioctls[] = {
599 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_VERSION, drm_version, DRM_RENDER_ALLOW),
600 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GET_UNIQUE, drm_getunique, 0),
601 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GET_MAGIC, drm_getmagic, 0),
602 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_IRQ_BUSID, drm_irq_by_busid, DRM_MASTER|DRM_ROOT_ONLY),
603 1.13 riastrad
604 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_GET_MAP, drm_legacy_getmap_ioctl, 0),
605 1.13 riastrad
606 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GET_CLIENT, drm_getclient, 0),
607 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GET_STATS, drm_getstats, 0),
608 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GET_CAP, drm_getcap, DRM_RENDER_ALLOW),
609 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SET_CLIENT_CAP, drm_setclientcap, 0),
610 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SET_VERSION, drm_setversion, DRM_MASTER),
611 1.5 riastrad
612 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SET_UNIQUE, drm_invalid_op, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
613 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_BLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
614 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_UNBLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
615 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AUTH_MAGIC, drm_authmagic, DRM_MASTER),
616 1.5 riastrad
617 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_ADD_MAP, drm_legacy_addmap_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
618 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_RM_MAP, drm_legacy_rmmap_ioctl, DRM_AUTH),
619 1.5 riastrad
620 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_SET_SAREA_CTX, drm_legacy_setsareactx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
621 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_GET_SAREA_CTX, drm_legacy_getsareactx, DRM_AUTH),
622 1.5 riastrad
623 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SET_MASTER, drm_setmaster_ioctl, DRM_ROOT_ONLY),
624 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_DROP_MASTER, drm_dropmaster_ioctl, DRM_ROOT_ONLY),
625 1.5 riastrad
626 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_ADD_CTX, drm_legacy_addctx, DRM_AUTH|DRM_ROOT_ONLY),
627 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_RM_CTX, drm_legacy_rmctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
628 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_MOD_CTX, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
629 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_GET_CTX, drm_legacy_getctx, DRM_AUTH),
630 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_SWITCH_CTX, drm_legacy_switchctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
631 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_NEW_CTX, drm_legacy_newctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
632 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_RES_CTX, drm_legacy_resctx, DRM_AUTH),
633 1.5 riastrad
634 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_ADD_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
635 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_RM_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
636 1.5 riastrad
637 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_LOCK, drm_legacy_lock, DRM_AUTH),
638 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_UNLOCK, drm_legacy_unlock, DRM_AUTH),
639 1.5 riastrad
640 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_FINISH, drm_noop, DRM_AUTH),
641 1.5 riastrad
642 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_ADD_BUFS, drm_legacy_addbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
643 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_MARK_BUFS, drm_legacy_markbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
644 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_INFO_BUFS, drm_legacy_infobufs, DRM_AUTH),
645 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_MAP_BUFS, drm_legacy_mapbufs, DRM_AUTH),
646 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_FREE_BUFS, drm_legacy_freebufs, DRM_AUTH),
647 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_DMA, drm_legacy_dma_ioctl, DRM_AUTH),
648 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_CONTROL, drm_legacy_irq_control, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
649 1.5 riastrad
650 1.5 riastrad #if IS_ENABLED(CONFIG_AGP)
651 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AGP_ACQUIRE, drm_agp_acquire_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
652 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AGP_RELEASE, drm_agp_release_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
653 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AGP_ENABLE, drm_agp_enable_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
654 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AGP_INFO, drm_agp_info_ioctl, DRM_AUTH),
655 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AGP_ALLOC, drm_agp_alloc_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
656 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AGP_FREE, drm_agp_free_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
657 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AGP_BIND, drm_agp_bind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
658 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AGP_UNBIND, drm_agp_unbind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
659 1.5 riastrad #endif
660 1.5 riastrad
661 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_SG_ALLOC, drm_legacy_sg_alloc, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
662 1.13 riastrad DRM_LEGACY_IOCTL_DEF(DRM_IOCTL_SG_FREE, drm_legacy_sg_free, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
663 1.5 riastrad
664 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_WAIT_VBLANK, drm_wait_vblank_ioctl, DRM_UNLOCKED),
665 1.5 riastrad
666 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODESET_CTL, drm_legacy_modeset_ctl_ioctl, 0),
667 1.5 riastrad
668 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_UPDATE_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
669 1.5 riastrad
670 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GEM_CLOSE, drm_gem_close_ioctl, DRM_RENDER_ALLOW),
671 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GEM_FLINK, drm_gem_flink_ioctl, DRM_AUTH),
672 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GEM_OPEN, drm_gem_open_ioctl, DRM_AUTH),
673 1.13 riastrad
674 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETRESOURCES, drm_mode_getresources, 0),
675 1.13 riastrad
676 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_PRIME_HANDLE_TO_FD, drm_prime_handle_to_fd_ioctl, DRM_RENDER_ALLOW),
677 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_PRIME_FD_TO_HANDLE, drm_prime_fd_to_handle_ioctl, DRM_RENDER_ALLOW),
678 1.13 riastrad
679 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANERESOURCES, drm_mode_getplane_res, 0),
680 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCRTC, drm_mode_getcrtc, 0),
681 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETCRTC, drm_mode_setcrtc, DRM_MASTER),
682 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANE, drm_mode_getplane, 0),
683 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETPLANE, drm_mode_setplane, DRM_MASTER),
684 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_CURSOR, drm_mode_cursor_ioctl, DRM_MASTER),
685 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETGAMMA, drm_mode_gamma_get_ioctl, 0),
686 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETGAMMA, drm_mode_gamma_set_ioctl, DRM_MASTER),
687 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETENCODER, drm_mode_getencoder, 0),
688 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCONNECTOR, drm_mode_getconnector, 0),
689 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_ATTACHMODE, drm_noop, DRM_MASTER),
690 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_DETACHMODE, drm_noop, DRM_MASTER),
691 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPROPERTY, drm_mode_getproperty_ioctl, 0),
692 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETPROPERTY, drm_connector_property_set_ioctl, DRM_MASTER),
693 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPROPBLOB, drm_mode_getblob_ioctl, 0),
694 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETFB, drm_mode_getfb, 0),
695 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_ADDFB, drm_mode_addfb_ioctl, 0),
696 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_ADDFB2, drm_mode_addfb2_ioctl, 0),
697 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_RMFB, drm_mode_rmfb_ioctl, 0),
698 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_PAGE_FLIP, drm_mode_page_flip_ioctl, DRM_MASTER),
699 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_DIRTYFB, drm_mode_dirtyfb_ioctl, DRM_MASTER),
700 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_CREATE_DUMB, drm_mode_create_dumb_ioctl, 0),
701 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_MAP_DUMB, drm_mode_mmap_dumb_ioctl, 0),
702 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_DESTROY_DUMB, drm_mode_destroy_dumb_ioctl, 0),
703 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_OBJ_GETPROPERTIES, drm_mode_obj_get_properties_ioctl, 0),
704 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_OBJ_SETPROPERTY, drm_mode_obj_set_property_ioctl, DRM_MASTER),
705 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_CURSOR2, drm_mode_cursor2_ioctl, DRM_MASTER),
706 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_ATOMIC, drm_mode_atomic_ioctl, DRM_MASTER),
707 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_CREATEPROPBLOB, drm_mode_createblob_ioctl, 0),
708 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_DESTROYPROPBLOB, drm_mode_destroyblob_ioctl, 0),
709 1.13 riastrad
710 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_CREATE, drm_syncobj_create_ioctl,
711 1.13 riastrad DRM_RENDER_ALLOW),
712 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_DESTROY, drm_syncobj_destroy_ioctl,
713 1.13 riastrad DRM_RENDER_ALLOW),
714 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, drm_syncobj_handle_to_fd_ioctl,
715 1.13 riastrad DRM_RENDER_ALLOW),
716 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, drm_syncobj_fd_to_handle_ioctl,
717 1.13 riastrad DRM_RENDER_ALLOW),
718 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_TRANSFER, drm_syncobj_transfer_ioctl,
719 1.13 riastrad DRM_RENDER_ALLOW),
720 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_WAIT, drm_syncobj_wait_ioctl,
721 1.13 riastrad DRM_RENDER_ALLOW),
722 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_TIMELINE_WAIT, drm_syncobj_timeline_wait_ioctl,
723 1.13 riastrad DRM_RENDER_ALLOW),
724 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_RESET, drm_syncobj_reset_ioctl,
725 1.13 riastrad DRM_RENDER_ALLOW),
726 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_SIGNAL, drm_syncobj_signal_ioctl,
727 1.13 riastrad DRM_RENDER_ALLOW),
728 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_TIMELINE_SIGNAL, drm_syncobj_timeline_signal_ioctl,
729 1.13 riastrad DRM_RENDER_ALLOW),
730 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_QUERY, drm_syncobj_query_ioctl,
731 1.13 riastrad DRM_RENDER_ALLOW),
732 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_CRTC_GET_SEQUENCE, drm_crtc_get_sequence_ioctl, 0),
733 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_CRTC_QUEUE_SEQUENCE, drm_crtc_queue_sequence_ioctl, 0),
734 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_CREATE_LEASE, drm_mode_create_lease_ioctl, DRM_MASTER),
735 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_LIST_LESSEES, drm_mode_list_lessees_ioctl, DRM_MASTER),
736 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GET_LEASE, drm_mode_get_lease_ioctl, DRM_MASTER),
737 1.13 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_REVOKE_LEASE, drm_mode_revoke_lease_ioctl, DRM_MASTER),
738 1.5 riastrad };
739 1.5 riastrad
740 1.5 riastrad #define DRM_CORE_IOCTL_COUNT ARRAY_SIZE( drm_ioctls )
741 1.5 riastrad
742 1.5 riastrad /**
743 1.13 riastrad * DOC: driver specific ioctls
744 1.13 riastrad *
745 1.13 riastrad * First things first, driver private IOCTLs should only be needed for drivers
746 1.13 riastrad * supporting rendering. Kernel modesetting is all standardized, and extended
747 1.13 riastrad * through properties. There are a few exceptions in some existing drivers,
748 1.13 riastrad * which define IOCTL for use by the display DRM master, but they all predate
749 1.13 riastrad * properties.
750 1.13 riastrad *
751 1.13 riastrad * Now if you do have a render driver you always have to support it through
752 1.13 riastrad * driver private properties. There's a few steps needed to wire all the things
753 1.13 riastrad * up.
754 1.13 riastrad *
755 1.13 riastrad * First you need to define the structure for your IOCTL in your driver private
756 1.13 riastrad * UAPI header in ``include/uapi/drm/my_driver_drm.h``::
757 1.13 riastrad *
758 1.13 riastrad * struct my_driver_operation {
759 1.13 riastrad * u32 some_thing;
760 1.13 riastrad * u32 another_thing;
761 1.13 riastrad * };
762 1.13 riastrad *
763 1.13 riastrad * Please make sure that you follow all the best practices from
764 1.13 riastrad * ``Documentation/ioctl/botching-up-ioctls.rst``. Note that drm_ioctl()
765 1.13 riastrad * automatically zero-extends structures, hence make sure you can add more stuff
766 1.13 riastrad * at the end, i.e. don't put a variable sized array there.
767 1.13 riastrad *
768 1.13 riastrad * Then you need to define your IOCTL number, using one of DRM_IO(), DRM_IOR(),
769 1.13 riastrad * DRM_IOW() or DRM_IOWR(). It must start with the DRM_IOCTL\_ prefix::
770 1.13 riastrad *
771 1.13 riastrad * ##define DRM_IOCTL_MY_DRIVER_OPERATION \
772 1.13 riastrad * DRM_IOW(DRM_COMMAND_BASE, struct my_driver_operation)
773 1.13 riastrad *
774 1.13 riastrad * DRM driver private IOCTL must be in the range from DRM_COMMAND_BASE to
775 1.13 riastrad * DRM_COMMAND_END. Finally you need an array of &struct drm_ioctl_desc to wire
776 1.13 riastrad * up the handlers and set the access rights::
777 1.13 riastrad *
778 1.13 riastrad * static const struct drm_ioctl_desc my_driver_ioctls[] = {
779 1.13 riastrad * DRM_IOCTL_DEF_DRV(MY_DRIVER_OPERATION, my_driver_operation,
780 1.13 riastrad * DRM_AUTH|DRM_RENDER_ALLOW),
781 1.13 riastrad * };
782 1.13 riastrad *
783 1.13 riastrad * And then assign this to the &drm_driver.ioctls field in your driver
784 1.13 riastrad * structure.
785 1.13 riastrad *
786 1.13 riastrad * See the separate chapter on :ref:`file operations<drm_driver_fops>` for how
787 1.13 riastrad * the driver-specific IOCTLs are wired up.
788 1.13 riastrad */
789 1.13 riastrad
790 1.13 riastrad long drm_ioctl_kernel(struct file *file, drm_ioctl_t *func, void *kdata,
791 1.13 riastrad u32 flags)
792 1.13 riastrad {
793 1.15 riastrad #ifdef __NetBSD__
794 1.15 riastrad struct drm_file *file_priv = file->f_data;
795 1.15 riastrad #else
796 1.13 riastrad struct drm_file *file_priv = file->private_data;
797 1.15 riastrad #endif
798 1.13 riastrad struct drm_device *dev = file_priv->minor->dev;
799 1.13 riastrad int retcode;
800 1.13 riastrad
801 1.13 riastrad if (drm_dev_is_unplugged(dev))
802 1.13 riastrad return -ENODEV;
803 1.13 riastrad
804 1.13 riastrad retcode = drm_ioctl_permit(flags, file_priv);
805 1.13 riastrad if (unlikely(retcode))
806 1.13 riastrad return retcode;
807 1.13 riastrad
808 1.13 riastrad /* Enforce sane locking for modern driver ioctls. */
809 1.13 riastrad if (likely(!drm_core_check_feature(dev, DRIVER_LEGACY)) ||
810 1.13 riastrad (flags & DRM_UNLOCKED))
811 1.13 riastrad retcode = func(dev, kdata, file_priv);
812 1.13 riastrad else {
813 1.13 riastrad mutex_lock(&drm_global_mutex);
814 1.13 riastrad retcode = func(dev, kdata, file_priv);
815 1.13 riastrad mutex_unlock(&drm_global_mutex);
816 1.13 riastrad }
817 1.13 riastrad return retcode;
818 1.13 riastrad }
819 1.13 riastrad EXPORT_SYMBOL(drm_ioctl_kernel);
820 1.13 riastrad
821 1.13 riastrad /**
822 1.5 riastrad * drm_ioctl - ioctl callback implementation for DRM drivers
823 1.5 riastrad * @filp: file this ioctl is called on
824 1.5 riastrad * @cmd: ioctl cmd number
825 1.5 riastrad * @arg: user argument
826 1.5 riastrad *
827 1.13 riastrad * Looks up the ioctl function in the DRM core and the driver dispatch table,
828 1.13 riastrad * stored in &drm_driver.ioctls. It checks for necessary permission by calling
829 1.13 riastrad * drm_ioctl_permit(), and dispatches to the respective function.
830 1.5 riastrad *
831 1.5 riastrad * Returns:
832 1.5 riastrad * Zero on success, negative error code on failure.
833 1.5 riastrad */
834 1.6 riastrad #ifdef __NetBSD__
835 1.6 riastrad #include <sys/file.h>
836 1.6 riastrad int
837 1.6 riastrad drm_ioctl(struct file *fp, unsigned long cmd, void *data)
838 1.6 riastrad {
839 1.23 riastrad char stackbuf[128] __aligned(ALIGNBYTES + 1);
840 1.8 riastrad char *buf = stackbuf;
841 1.11 maya void *data0 = data;
842 1.6 riastrad struct drm_file *const file = fp->f_data;
843 1.6 riastrad const unsigned int nr = DRM_IOCTL_NR(cmd);
844 1.6 riastrad int error;
845 1.6 riastrad
846 1.6 riastrad switch (cmd) {
847 1.6 riastrad case FIONBIO:
848 1.6 riastrad case FIOASYNC:
849 1.6 riastrad return 0;
850 1.6 riastrad default:
851 1.6 riastrad break;
852 1.6 riastrad }
853 1.6 riastrad
854 1.6 riastrad if (IOCGROUP(cmd) != DRM_IOCTL_BASE)
855 1.6 riastrad return EINVAL;
856 1.6 riastrad
857 1.6 riastrad KASSERT(file != NULL);
858 1.6 riastrad KASSERT(file->minor != NULL);
859 1.6 riastrad KASSERT(file->minor->dev != NULL);
860 1.6 riastrad struct drm_device *const dev = file->minor->dev;
861 1.6 riastrad const struct drm_ioctl_desc *ioctl;
862 1.6 riastrad
863 1.18 riastrad if (drm_dev_is_unplugged(dev))
864 1.6 riastrad return ENXIO;
865 1.6 riastrad
866 1.6 riastrad const bool is_driver_ioctl =
867 1.6 riastrad (DRM_COMMAND_BASE <= nr) && (nr < DRM_COMMAND_END);
868 1.6 riastrad
869 1.6 riastrad if (is_driver_ioctl) {
870 1.6 riastrad const unsigned int driver_nr = nr - DRM_COMMAND_BASE;
871 1.6 riastrad if (driver_nr >= dev->driver->num_ioctls)
872 1.6 riastrad return EINVAL;
873 1.6 riastrad ioctl = &dev->driver->ioctls[driver_nr];
874 1.6 riastrad } else if (nr < __arraycount(drm_ioctls)) {
875 1.6 riastrad ioctl = &drm_ioctls[nr];
876 1.6 riastrad } else {
877 1.6 riastrad ioctl = NULL;
878 1.6 riastrad }
879 1.6 riastrad
880 1.6 riastrad if ((ioctl == NULL) || (ioctl->func == NULL))
881 1.6 riastrad return EINVAL;
882 1.6 riastrad
883 1.6 riastrad /* XXX errno Linux->NetBSD */
884 1.6 riastrad error = -drm_ioctl_permit(ioctl->flags, file);
885 1.6 riastrad if (error)
886 1.6 riastrad return error;
887 1.6 riastrad
888 1.8 riastrad /* If userland passed in too few bytes, zero-pad them. */
889 1.8 riastrad if (IOCPARM_LEN(cmd) < IOCPARM_LEN(ioctl->cmd)) {
890 1.8 riastrad /* 12-bit quantity, according to <sys/ioccom.h> */
891 1.8 riastrad KASSERT(IOCPARM_LEN(ioctl->cmd) <= 4096);
892 1.8 riastrad if (IOCPARM_LEN(ioctl->cmd) > sizeof stackbuf) {
893 1.8 riastrad buf = kmem_alloc(IOCPARM_LEN(ioctl->cmd), KM_NOSLEEP);
894 1.8 riastrad if (buf == NULL)
895 1.8 riastrad return ENOMEM;
896 1.8 riastrad }
897 1.8 riastrad memcpy(buf, data, IOCPARM_LEN(cmd));
898 1.8 riastrad memset(buf + IOCPARM_LEN(cmd), 0,
899 1.8 riastrad IOCPARM_LEN(ioctl->cmd) - IOCPARM_LEN(cmd));
900 1.11 maya data0 = buf;
901 1.8 riastrad }
902 1.8 riastrad
903 1.6 riastrad if ((drm_core_check_feature(dev, DRIVER_MODESET) && is_driver_ioctl) ||
904 1.6 riastrad ISSET(ioctl->flags, DRM_UNLOCKED)) {
905 1.6 riastrad /* XXX errno Linux->NetBSD */
906 1.11 maya error = -(*ioctl->func)(dev, data0, file);
907 1.6 riastrad } else {
908 1.6 riastrad mutex_lock(&drm_global_mutex);
909 1.6 riastrad /* XXX errno Linux->NetBSD */
910 1.11 maya error = -(*ioctl->func)(dev, data0, file);
911 1.6 riastrad mutex_unlock(&drm_global_mutex);
912 1.6 riastrad }
913 1.6 riastrad
914 1.11 maya /* If we used a temporary buffer, copy it back out. */
915 1.11 maya if (data != data0)
916 1.11 maya memcpy(data, data0, IOCPARM_LEN(cmd));
917 1.11 maya
918 1.8 riastrad /* If we had to allocate a heap buffer, free it. */
919 1.8 riastrad if (buf != stackbuf)
920 1.8 riastrad kmem_free(buf, IOCPARM_LEN(ioctl->cmd));
921 1.8 riastrad
922 1.6 riastrad return error;
923 1.6 riastrad }
924 1.6 riastrad #else
925 1.5 riastrad long drm_ioctl(struct file *filp,
926 1.5 riastrad unsigned int cmd, unsigned long arg)
927 1.5 riastrad {
928 1.5 riastrad struct drm_file *file_priv = filp->private_data;
929 1.5 riastrad struct drm_device *dev;
930 1.5 riastrad const struct drm_ioctl_desc *ioctl = NULL;
931 1.5 riastrad drm_ioctl_t *func;
932 1.5 riastrad unsigned int nr = DRM_IOCTL_NR(cmd);
933 1.5 riastrad int retcode = -EINVAL;
934 1.5 riastrad char stack_kdata[128];
935 1.5 riastrad char *kdata = NULL;
936 1.13 riastrad unsigned int in_size, out_size, drv_size, ksize;
937 1.5 riastrad bool is_driver_ioctl;
938 1.5 riastrad
939 1.5 riastrad dev = file_priv->minor->dev;
940 1.5 riastrad
941 1.13 riastrad if (drm_dev_is_unplugged(dev))
942 1.5 riastrad return -ENODEV;
943 1.5 riastrad
944 1.5 riastrad is_driver_ioctl = nr >= DRM_COMMAND_BASE && nr < DRM_COMMAND_END;
945 1.5 riastrad
946 1.5 riastrad if (is_driver_ioctl) {
947 1.5 riastrad /* driver ioctl */
948 1.13 riastrad unsigned int index = nr - DRM_COMMAND_BASE;
949 1.13 riastrad
950 1.13 riastrad if (index >= dev->driver->num_ioctls)
951 1.5 riastrad goto err_i1;
952 1.13 riastrad index = array_index_nospec(index, dev->driver->num_ioctls);
953 1.13 riastrad ioctl = &dev->driver->ioctls[index];
954 1.5 riastrad } else {
955 1.5 riastrad /* core ioctl */
956 1.5 riastrad if (nr >= DRM_CORE_IOCTL_COUNT)
957 1.5 riastrad goto err_i1;
958 1.13 riastrad nr = array_index_nospec(nr, DRM_CORE_IOCTL_COUNT);
959 1.5 riastrad ioctl = &drm_ioctls[nr];
960 1.5 riastrad }
961 1.5 riastrad
962 1.5 riastrad drv_size = _IOC_SIZE(ioctl->cmd);
963 1.13 riastrad out_size = in_size = _IOC_SIZE(cmd);
964 1.13 riastrad if ((cmd & ioctl->cmd & IOC_IN) == 0)
965 1.13 riastrad in_size = 0;
966 1.13 riastrad if ((cmd & ioctl->cmd & IOC_OUT) == 0)
967 1.13 riastrad out_size = 0;
968 1.13 riastrad ksize = max(max(in_size, out_size), drv_size);
969 1.5 riastrad
970 1.5 riastrad DRM_DEBUG("pid=%d, dev=0x%lx, auth=%d, %s\n",
971 1.5 riastrad task_pid_nr(current),
972 1.5 riastrad (long)old_encode_dev(file_priv->minor->kdev->devt),
973 1.5 riastrad file_priv->authenticated, ioctl->name);
974 1.5 riastrad
975 1.5 riastrad /* Do not trust userspace, use our own definition */
976 1.5 riastrad func = ioctl->func;
977 1.5 riastrad
978 1.5 riastrad if (unlikely(!func)) {
979 1.5 riastrad DRM_DEBUG("no function\n");
980 1.5 riastrad retcode = -EINVAL;
981 1.5 riastrad goto err_i1;
982 1.5 riastrad }
983 1.5 riastrad
984 1.13 riastrad if (ksize <= sizeof(stack_kdata)) {
985 1.13 riastrad kdata = stack_kdata;
986 1.13 riastrad } else {
987 1.13 riastrad kdata = kmalloc(ksize, GFP_KERNEL);
988 1.13 riastrad if (!kdata) {
989 1.13 riastrad retcode = -ENOMEM;
990 1.13 riastrad goto err_i1;
991 1.5 riastrad }
992 1.5 riastrad }
993 1.5 riastrad
994 1.13 riastrad if (copy_from_user(kdata, (void __user *)arg, in_size) != 0) {
995 1.13 riastrad retcode = -EFAULT;
996 1.13 riastrad goto err_i1;
997 1.5 riastrad }
998 1.5 riastrad
999 1.13 riastrad if (ksize > in_size)
1000 1.13 riastrad memset(kdata + in_size, 0, ksize - in_size);
1001 1.5 riastrad
1002 1.13 riastrad retcode = drm_ioctl_kernel(filp, func, kdata, ioctl->flags);
1003 1.13 riastrad if (copy_to_user((void __user *)arg, kdata, out_size) != 0)
1004 1.13 riastrad retcode = -EFAULT;
1005 1.5 riastrad
1006 1.5 riastrad err_i1:
1007 1.5 riastrad if (!ioctl)
1008 1.5 riastrad DRM_DEBUG("invalid ioctl: pid=%d, dev=0x%lx, auth=%d, cmd=0x%02x, nr=0x%02x\n",
1009 1.5 riastrad task_pid_nr(current),
1010 1.5 riastrad (long)old_encode_dev(file_priv->minor->kdev->devt),
1011 1.5 riastrad file_priv->authenticated, cmd, nr);
1012 1.5 riastrad
1013 1.5 riastrad if (kdata != stack_kdata)
1014 1.5 riastrad kfree(kdata);
1015 1.5 riastrad if (retcode)
1016 1.13 riastrad DRM_DEBUG("pid=%d, ret = %d\n", task_pid_nr(current), retcode);
1017 1.5 riastrad return retcode;
1018 1.5 riastrad }
1019 1.6 riastrad #endif
1020 1.5 riastrad EXPORT_SYMBOL(drm_ioctl);
1021 1.5 riastrad
1022 1.5 riastrad /**
1023 1.5 riastrad * drm_ioctl_flags - Check for core ioctl and return ioctl permission flags
1024 1.5 riastrad * @nr: ioctl number
1025 1.5 riastrad * @flags: where to return the ioctl permission flags
1026 1.5 riastrad *
1027 1.5 riastrad * This ioctl is only used by the vmwgfx driver to augment the access checks
1028 1.5 riastrad * done by the drm core and insofar a pretty decent layering violation. This
1029 1.5 riastrad * shouldn't be used by any drivers.
1030 1.5 riastrad *
1031 1.5 riastrad * Returns:
1032 1.13 riastrad * True if the @nr corresponds to a DRM core ioctl number, false otherwise.
1033 1.5 riastrad */
1034 1.5 riastrad bool drm_ioctl_flags(unsigned int nr, unsigned int *flags)
1035 1.5 riastrad {
1036 1.5 riastrad if (nr >= DRM_COMMAND_BASE && nr < DRM_COMMAND_END)
1037 1.5 riastrad return false;
1038 1.5 riastrad
1039 1.5 riastrad if (nr >= DRM_CORE_IOCTL_COUNT)
1040 1.5 riastrad return false;
1041 1.13 riastrad nr = array_index_nospec(nr, DRM_CORE_IOCTL_COUNT);
1042 1.5 riastrad
1043 1.5 riastrad *flags = drm_ioctls[nr].flags;
1044 1.5 riastrad return true;
1045 1.5 riastrad }
1046 1.5 riastrad EXPORT_SYMBOL(drm_ioctl_flags);
1047