drm_ioctl.c revision 1.3.2.1 1 1.1 riastrad /**
2 1.1 riastrad * \file drm_ioctl.c
3 1.1 riastrad * IOCTL processing for DRM
4 1.1 riastrad *
5 1.1 riastrad * \author Rickard E. (Rik) Faith <faith (at) valinux.com>
6 1.1 riastrad * \author Gareth Hughes <gareth (at) valinux.com>
7 1.1 riastrad */
8 1.1 riastrad
9 1.1 riastrad /*
10 1.1 riastrad * Created: Fri Jan 8 09:01:26 1999 by faith (at) valinux.com
11 1.1 riastrad *
12 1.1 riastrad * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
13 1.1 riastrad * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 1.1 riastrad * All Rights Reserved.
15 1.1 riastrad *
16 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a
17 1.1 riastrad * copy of this software and associated documentation files (the "Software"),
18 1.1 riastrad * to deal in the Software without restriction, including without limitation
19 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the
21 1.1 riastrad * Software is furnished to do so, subject to the following conditions:
22 1.1 riastrad *
23 1.1 riastrad * The above copyright notice and this permission notice (including the next
24 1.1 riastrad * paragraph) shall be included in all copies or substantial portions of the
25 1.1 riastrad * Software.
26 1.1 riastrad *
27 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 1.1 riastrad * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31 1.1 riastrad * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32 1.1 riastrad * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33 1.1 riastrad * OTHER DEALINGS IN THE SOFTWARE.
34 1.1 riastrad */
35 1.1 riastrad
36 1.1 riastrad #include <drm/drmP.h>
37 1.1 riastrad #include <drm/drm_core.h>
38 1.1 riastrad
39 1.1 riastrad #include <linux/pci.h>
40 1.1 riastrad #include <linux/export.h>
41 1.3 riastrad #ifdef CONFIG_X86
42 1.3 riastrad #include <asm/mtrr.h>
43 1.3 riastrad #endif
44 1.1 riastrad
45 1.1 riastrad /**
46 1.1 riastrad * Get the bus id.
47 1.1 riastrad *
48 1.1 riastrad * \param inode device inode.
49 1.1 riastrad * \param file_priv DRM file private.
50 1.1 riastrad * \param cmd command.
51 1.1 riastrad * \param arg user argument, pointing to a drm_unique structure.
52 1.1 riastrad * \return zero on success or a negative number on failure.
53 1.1 riastrad *
54 1.1 riastrad * Copies the bus id from drm_device::unique into user space.
55 1.1 riastrad */
56 1.1 riastrad int drm_getunique(struct drm_device *dev, void *data,
57 1.1 riastrad struct drm_file *file_priv)
58 1.1 riastrad {
59 1.1 riastrad struct drm_unique *u = data;
60 1.1 riastrad struct drm_master *master = file_priv->master;
61 1.1 riastrad
62 1.1 riastrad if (u->unique_len >= master->unique_len) {
63 1.1 riastrad if (copy_to_user(u->unique, master->unique, master->unique_len))
64 1.1 riastrad return -EFAULT;
65 1.1 riastrad }
66 1.1 riastrad u->unique_len = master->unique_len;
67 1.1 riastrad
68 1.1 riastrad return 0;
69 1.1 riastrad }
70 1.1 riastrad
71 1.1 riastrad static void
72 1.1 riastrad drm_unset_busid(struct drm_device *dev,
73 1.1 riastrad struct drm_master *master)
74 1.1 riastrad {
75 1.1 riastrad kfree(dev->devname);
76 1.1 riastrad dev->devname = NULL;
77 1.1 riastrad
78 1.1 riastrad kfree(master->unique);
79 1.1 riastrad master->unique = NULL;
80 1.1 riastrad master->unique_len = 0;
81 1.1 riastrad master->unique_size = 0;
82 1.1 riastrad }
83 1.1 riastrad
84 1.1 riastrad /**
85 1.1 riastrad * Set the bus id.
86 1.1 riastrad *
87 1.1 riastrad * \param inode device inode.
88 1.1 riastrad * \param file_priv DRM file private.
89 1.1 riastrad * \param cmd command.
90 1.1 riastrad * \param arg user argument, pointing to a drm_unique structure.
91 1.1 riastrad * \return zero on success or a negative number on failure.
92 1.1 riastrad *
93 1.1 riastrad * Copies the bus id from userspace into drm_device::unique, and verifies that
94 1.1 riastrad * it matches the device this DRM is attached to (EINVAL otherwise). Deprecated
95 1.1 riastrad * in interface version 1.1 and will return EBUSY when setversion has requested
96 1.1 riastrad * version 1.1 or greater.
97 1.1 riastrad */
98 1.1 riastrad int drm_setunique(struct drm_device *dev, void *data,
99 1.1 riastrad struct drm_file *file_priv)
100 1.1 riastrad {
101 1.1 riastrad struct drm_unique *u = data;
102 1.1 riastrad struct drm_master *master = file_priv->master;
103 1.1 riastrad int ret;
104 1.1 riastrad
105 1.1 riastrad if (master->unique_len || master->unique)
106 1.1 riastrad return -EBUSY;
107 1.1 riastrad
108 1.1 riastrad if (!u->unique_len || u->unique_len > 1024)
109 1.1 riastrad return -EINVAL;
110 1.1 riastrad
111 1.1 riastrad if (!dev->driver->bus->set_unique)
112 1.1 riastrad return -EINVAL;
113 1.1 riastrad
114 1.1 riastrad ret = dev->driver->bus->set_unique(dev, master, u);
115 1.1 riastrad if (ret)
116 1.1 riastrad goto err;
117 1.1 riastrad
118 1.1 riastrad return 0;
119 1.1 riastrad
120 1.1 riastrad err:
121 1.1 riastrad drm_unset_busid(dev, master);
122 1.1 riastrad return ret;
123 1.1 riastrad }
124 1.1 riastrad
125 1.1 riastrad static int drm_set_busid(struct drm_device *dev, struct drm_file *file_priv)
126 1.1 riastrad {
127 1.1 riastrad struct drm_master *master = file_priv->master;
128 1.1 riastrad int ret;
129 1.1 riastrad
130 1.1 riastrad if (master->unique != NULL)
131 1.1 riastrad drm_unset_busid(dev, master);
132 1.1 riastrad
133 1.1 riastrad ret = dev->driver->bus->set_busid(dev, master);
134 1.1 riastrad if (ret)
135 1.1 riastrad goto err;
136 1.1 riastrad return 0;
137 1.1 riastrad err:
138 1.1 riastrad drm_unset_busid(dev, master);
139 1.1 riastrad return ret;
140 1.1 riastrad }
141 1.1 riastrad
142 1.1 riastrad /**
143 1.1 riastrad * Get a mapping information.
144 1.1 riastrad *
145 1.1 riastrad * \param inode device inode.
146 1.1 riastrad * \param file_priv DRM file private.
147 1.1 riastrad * \param cmd command.
148 1.1 riastrad * \param arg user argument, pointing to a drm_map structure.
149 1.1 riastrad *
150 1.1 riastrad * \return zero on success or a negative number on failure.
151 1.1 riastrad *
152 1.1 riastrad * Searches for the mapping with the specified offset and copies its information
153 1.1 riastrad * into userspace
154 1.1 riastrad */
155 1.1 riastrad int drm_getmap(struct drm_device *dev, void *data,
156 1.1 riastrad struct drm_file *file_priv)
157 1.1 riastrad {
158 1.1 riastrad struct drm_map *map = data;
159 1.1 riastrad struct drm_map_list *r_list = NULL;
160 1.1 riastrad struct list_head *list;
161 1.1 riastrad int idx;
162 1.1 riastrad int i;
163 1.1 riastrad
164 1.1 riastrad idx = map->offset;
165 1.1 riastrad if (idx < 0)
166 1.1 riastrad return -EINVAL;
167 1.1 riastrad
168 1.1 riastrad i = 0;
169 1.1 riastrad mutex_lock(&dev->struct_mutex);
170 1.1 riastrad list_for_each(list, &dev->maplist) {
171 1.1 riastrad if (i == idx) {
172 1.1 riastrad r_list = list_entry(list, struct drm_map_list, head);
173 1.1 riastrad break;
174 1.1 riastrad }
175 1.1 riastrad i++;
176 1.1 riastrad }
177 1.1 riastrad if (!r_list || !r_list->map) {
178 1.1 riastrad mutex_unlock(&dev->struct_mutex);
179 1.1 riastrad return -EINVAL;
180 1.1 riastrad }
181 1.1 riastrad
182 1.1 riastrad map->offset = r_list->map->offset;
183 1.1 riastrad map->size = r_list->map->size;
184 1.1 riastrad map->type = r_list->map->type;
185 1.1 riastrad map->flags = r_list->map->flags;
186 1.1 riastrad map->handle = (void *)(unsigned long) r_list->user_token;
187 1.3 riastrad
188 1.3 riastrad #ifdef CONFIG_X86
189 1.3 riastrad /*
190 1.3 riastrad * There appears to be exactly one user of the mtrr index: dritest.
191 1.3 riastrad * It's easy enough to keep it working on non-PAT systems.
192 1.3 riastrad */
193 1.3 riastrad map->mtrr = phys_wc_to_mtrr_index(r_list->map->mtrr);
194 1.3 riastrad #else
195 1.3 riastrad map->mtrr = -1;
196 1.3 riastrad #endif
197 1.3 riastrad
198 1.1 riastrad mutex_unlock(&dev->struct_mutex);
199 1.1 riastrad
200 1.1 riastrad return 0;
201 1.1 riastrad }
202 1.1 riastrad
203 1.1 riastrad /**
204 1.1 riastrad * Get client information.
205 1.1 riastrad *
206 1.1 riastrad * \param inode device inode.
207 1.1 riastrad * \param file_priv DRM file private.
208 1.1 riastrad * \param cmd command.
209 1.1 riastrad * \param arg user argument, pointing to a drm_client structure.
210 1.1 riastrad *
211 1.1 riastrad * \return zero on success or a negative number on failure.
212 1.1 riastrad *
213 1.1 riastrad * Searches for the client with the specified index and copies its information
214 1.1 riastrad * into userspace
215 1.1 riastrad */
216 1.1 riastrad int drm_getclient(struct drm_device *dev, void *data,
217 1.1 riastrad struct drm_file *file_priv)
218 1.1 riastrad {
219 1.1 riastrad struct drm_client *client = data;
220 1.1 riastrad
221 1.3 riastrad /*
222 1.3 riastrad * Hollowed-out getclient ioctl to keep some dead old drm tests/tools
223 1.3 riastrad * not breaking completely. Userspace tools stop enumerating one they
224 1.3 riastrad * get -EINVAL, hence this is the return value we need to hand back for
225 1.3 riastrad * no clients tracked.
226 1.3 riastrad *
227 1.3 riastrad * Unfortunately some clients (*cough* libva *cough*) use this in a fun
228 1.3 riastrad * attempt to figure out whether they're authenticated or not. Since
229 1.3 riastrad * that's the only thing they care about, give it to the directly
230 1.3 riastrad * instead of walking one giant list.
231 1.3 riastrad */
232 1.3 riastrad if (client->idx == 0) {
233 1.3 riastrad client->auth = file_priv->authenticated;
234 1.2 riastrad #ifdef __NetBSD__ /* XXX Too scary to contemplate. */
235 1.3.2.1 snj client->pid = curproc->p_pid;
236 1.3.2.1 snj client->uid = kauth_cred_geteuid(curproc->p_cred);
237 1.2 riastrad #else
238 1.3 riastrad client->pid = pid_vnr(file_priv->pid);
239 1.3 riastrad client->uid = from_kuid_munged(current_user_ns(),
240 1.3 riastrad file_priv->uid);
241 1.2 riastrad #endif
242 1.3 riastrad client->magic = 0;
243 1.3 riastrad client->iocs = 0;
244 1.1 riastrad
245 1.3 riastrad return 0;
246 1.3 riastrad } else {
247 1.3 riastrad return -EINVAL;
248 1.1 riastrad }
249 1.1 riastrad }
250 1.1 riastrad
251 1.1 riastrad /**
252 1.1 riastrad * Get statistics information.
253 1.1 riastrad *
254 1.1 riastrad * \param inode device inode.
255 1.1 riastrad * \param file_priv DRM file private.
256 1.1 riastrad * \param cmd command.
257 1.1 riastrad * \param arg user argument, pointing to a drm_stats structure.
258 1.1 riastrad *
259 1.1 riastrad * \return zero on success or a negative number on failure.
260 1.1 riastrad */
261 1.1 riastrad int drm_getstats(struct drm_device *dev, void *data,
262 1.1 riastrad struct drm_file *file_priv)
263 1.1 riastrad {
264 1.1 riastrad struct drm_stats *stats = data;
265 1.1 riastrad
266 1.3 riastrad /* Clear stats to prevent userspace from eating its stack garbage. */
267 1.1 riastrad memset(stats, 0, sizeof(*stats));
268 1.1 riastrad
269 1.1 riastrad return 0;
270 1.1 riastrad }
271 1.1 riastrad
272 1.1 riastrad /**
273 1.1 riastrad * Get device/driver capabilities
274 1.1 riastrad */
275 1.1 riastrad int drm_getcap(struct drm_device *dev, void *data, struct drm_file *file_priv)
276 1.1 riastrad {
277 1.1 riastrad struct drm_get_cap *req = data;
278 1.1 riastrad
279 1.1 riastrad req->value = 0;
280 1.1 riastrad switch (req->capability) {
281 1.1 riastrad case DRM_CAP_DUMB_BUFFER:
282 1.1 riastrad if (dev->driver->dumb_create)
283 1.1 riastrad req->value = 1;
284 1.1 riastrad break;
285 1.1 riastrad case DRM_CAP_VBLANK_HIGH_CRTC:
286 1.1 riastrad req->value = 1;
287 1.1 riastrad break;
288 1.1 riastrad case DRM_CAP_DUMB_PREFERRED_DEPTH:
289 1.1 riastrad req->value = dev->mode_config.preferred_depth;
290 1.1 riastrad break;
291 1.1 riastrad case DRM_CAP_DUMB_PREFER_SHADOW:
292 1.1 riastrad req->value = dev->mode_config.prefer_shadow;
293 1.1 riastrad break;
294 1.1 riastrad case DRM_CAP_PRIME:
295 1.1 riastrad req->value |= dev->driver->prime_fd_to_handle ? DRM_PRIME_CAP_IMPORT : 0;
296 1.1 riastrad req->value |= dev->driver->prime_handle_to_fd ? DRM_PRIME_CAP_EXPORT : 0;
297 1.1 riastrad break;
298 1.1 riastrad case DRM_CAP_TIMESTAMP_MONOTONIC:
299 1.1 riastrad req->value = drm_timestamp_monotonic;
300 1.1 riastrad break;
301 1.3 riastrad case DRM_CAP_ASYNC_PAGE_FLIP:
302 1.3 riastrad req->value = dev->mode_config.async_page_flip;
303 1.3 riastrad break;
304 1.3 riastrad case DRM_CAP_CURSOR_WIDTH:
305 1.3 riastrad if (dev->mode_config.cursor_width)
306 1.3 riastrad req->value = dev->mode_config.cursor_width;
307 1.3 riastrad else
308 1.3 riastrad req->value = 64;
309 1.3 riastrad break;
310 1.3 riastrad case DRM_CAP_CURSOR_HEIGHT:
311 1.3 riastrad if (dev->mode_config.cursor_height)
312 1.3 riastrad req->value = dev->mode_config.cursor_height;
313 1.3 riastrad else
314 1.3 riastrad req->value = 64;
315 1.3 riastrad break;
316 1.3 riastrad default:
317 1.3 riastrad return -EINVAL;
318 1.3 riastrad }
319 1.3 riastrad return 0;
320 1.3 riastrad }
321 1.3 riastrad
322 1.3 riastrad /**
323 1.3 riastrad * Set device/driver capabilities
324 1.3 riastrad */
325 1.3 riastrad int
326 1.3 riastrad drm_setclientcap(struct drm_device *dev, void *data, struct drm_file *file_priv)
327 1.3 riastrad {
328 1.3 riastrad struct drm_set_client_cap *req = data;
329 1.3 riastrad
330 1.3 riastrad switch (req->capability) {
331 1.3 riastrad case DRM_CLIENT_CAP_STEREO_3D:
332 1.3 riastrad if (req->value > 1)
333 1.3 riastrad return -EINVAL;
334 1.3 riastrad file_priv->stereo_allowed = req->value;
335 1.3 riastrad break;
336 1.3 riastrad case DRM_CLIENT_CAP_UNIVERSAL_PLANES:
337 1.3 riastrad if (!drm_universal_planes)
338 1.3 riastrad return -EINVAL;
339 1.3 riastrad if (req->value > 1)
340 1.3 riastrad return -EINVAL;
341 1.3 riastrad file_priv->universal_planes = req->value;
342 1.3 riastrad break;
343 1.1 riastrad default:
344 1.1 riastrad return -EINVAL;
345 1.1 riastrad }
346 1.3 riastrad
347 1.1 riastrad return 0;
348 1.1 riastrad }
349 1.1 riastrad
350 1.1 riastrad /**
351 1.1 riastrad * Setversion ioctl.
352 1.1 riastrad *
353 1.1 riastrad * \param inode device inode.
354 1.1 riastrad * \param file_priv DRM file private.
355 1.1 riastrad * \param cmd command.
356 1.1 riastrad * \param arg user argument, pointing to a drm_lock structure.
357 1.1 riastrad * \return zero on success or negative number on failure.
358 1.1 riastrad *
359 1.1 riastrad * Sets the requested interface version
360 1.1 riastrad */
361 1.1 riastrad int drm_setversion(struct drm_device *dev, void *data, struct drm_file *file_priv)
362 1.1 riastrad {
363 1.1 riastrad struct drm_set_version *sv = data;
364 1.1 riastrad int if_version, retcode = 0;
365 1.1 riastrad
366 1.1 riastrad if (sv->drm_di_major != -1) {
367 1.1 riastrad if (sv->drm_di_major != DRM_IF_MAJOR ||
368 1.1 riastrad sv->drm_di_minor < 0 || sv->drm_di_minor > DRM_IF_MINOR) {
369 1.1 riastrad retcode = -EINVAL;
370 1.1 riastrad goto done;
371 1.1 riastrad }
372 1.1 riastrad if_version = DRM_IF_VERSION(sv->drm_di_major,
373 1.1 riastrad sv->drm_di_minor);
374 1.1 riastrad dev->if_version = max(if_version, dev->if_version);
375 1.1 riastrad if (sv->drm_di_minor >= 1) {
376 1.1 riastrad /*
377 1.1 riastrad * Version 1.1 includes tying of DRM to specific device
378 1.1 riastrad * Version 1.4 has proper PCI domain support
379 1.1 riastrad */
380 1.1 riastrad retcode = drm_set_busid(dev, file_priv);
381 1.1 riastrad if (retcode)
382 1.1 riastrad goto done;
383 1.1 riastrad }
384 1.1 riastrad }
385 1.1 riastrad
386 1.1 riastrad if (sv->drm_dd_major != -1) {
387 1.1 riastrad if (sv->drm_dd_major != dev->driver->major ||
388 1.1 riastrad sv->drm_dd_minor < 0 || sv->drm_dd_minor >
389 1.1 riastrad dev->driver->minor) {
390 1.1 riastrad retcode = -EINVAL;
391 1.1 riastrad goto done;
392 1.1 riastrad }
393 1.1 riastrad }
394 1.1 riastrad
395 1.1 riastrad done:
396 1.1 riastrad sv->drm_di_major = DRM_IF_MAJOR;
397 1.1 riastrad sv->drm_di_minor = DRM_IF_MINOR;
398 1.1 riastrad sv->drm_dd_major = dev->driver->major;
399 1.1 riastrad sv->drm_dd_minor = dev->driver->minor;
400 1.1 riastrad
401 1.1 riastrad return retcode;
402 1.1 riastrad }
403 1.1 riastrad
404 1.1 riastrad /** No-op ioctl. */
405 1.1 riastrad int drm_noop(struct drm_device *dev, void *data,
406 1.1 riastrad struct drm_file *file_priv)
407 1.1 riastrad {
408 1.1 riastrad DRM_DEBUG("\n");
409 1.1 riastrad return 0;
410 1.1 riastrad }
411 1.1 riastrad EXPORT_SYMBOL(drm_noop);
412