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