blob: e77bd8b57df2acd666a2c3dc8d7cacf1540c5c74 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002 * \file drm_ioctl.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * IOCTL processing for DRM
4 *
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
7 */
8
9/*
10 * Created: Fri Jan 8 09:01:26 1999 by faith@valinux.com
11 *
12 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
13 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 * All Rights Reserved.
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a
17 * copy of this software and associated documentation files (the "Software"),
18 * to deal in the Software without restriction, including without limitation
19 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 * and/or sell copies of the Software, and to permit persons to whom the
21 * Software is furnished to do so, subject to the following conditions:
22 *
23 * The above copyright notice and this permission notice (including the next
24 * paragraph) shall be included in all copies or substantial portions of the
25 * Software.
26 *
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33 * OTHER DEALINGS IN THE SOFTWARE.
34 */
35
David Howells760285e2012-10-02 18:01:07 +010036#include <drm/drmP.h>
37#include <drm/drm_core.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
David Howells760285e2012-10-02 18:01:07 +010039#include <linux/pci.h>
40#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/**
43 * Get the bus id.
Dave Airlieb5e89ed2005-09-25 14:28:13 +100044 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +100046 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * \param cmd command.
48 * \param arg user argument, pointing to a drm_unique structure.
49 * \return zero on success or a negative number on failure.
50 *
51 * Copies the bus id from drm_device::unique into user space.
52 */
Eric Anholtc153f452007-09-03 12:06:45 +100053int drm_getunique(struct drm_device *dev, void *data,
54 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Eric Anholtc153f452007-09-03 12:06:45 +100056 struct drm_unique *u = data;
Dave Airlie7c1c2872008-11-28 14:22:24 +100057 struct drm_master *master = file_priv->master;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Dave Airlie7c1c2872008-11-28 14:22:24 +100059 if (u->unique_len >= master->unique_len) {
60 if (copy_to_user(u->unique, master->unique, master->unique_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 return -EFAULT;
62 }
Dave Airlie7c1c2872008-11-28 14:22:24 +100063 u->unique_len = master->unique_len;
Eric Anholtc153f452007-09-03 12:06:45 +100064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 return 0;
66}
67
Chris Wilson3fb688f2010-08-04 11:09:42 +010068static void
69drm_unset_busid(struct drm_device *dev,
70 struct drm_master *master)
71{
72 kfree(dev->devname);
73 dev->devname = NULL;
74
75 kfree(master->unique);
76 master->unique = NULL;
77 master->unique_len = 0;
78 master->unique_size = 0;
79}
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/**
82 * Set the bus id.
Dave Airlieb5e89ed2005-09-25 14:28:13 +100083 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +100085 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * \param cmd command.
87 * \param arg user argument, pointing to a drm_unique structure.
88 * \return zero on success or a negative number on failure.
89 *
90 * Copies the bus id from userspace into drm_device::unique, and verifies that
91 * it matches the device this DRM is attached to (EINVAL otherwise). Deprecated
92 * in interface version 1.1 and will return EBUSY when setversion has requested
93 * version 1.1 or greater.
94 */
Eric Anholtc153f452007-09-03 12:06:45 +100095int drm_setunique(struct drm_device *dev, void *data,
96 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Eric Anholtc153f452007-09-03 12:06:45 +100098 struct drm_unique *u = data;
Dave Airlie7c1c2872008-11-28 14:22:24 +100099 struct drm_master *master = file_priv->master;
Dave Airlie8410ea32010-12-15 03:16:38 +1000100 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Dave Airlie7c1c2872008-11-28 14:22:24 +1000102 if (master->unique_len || master->unique)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000103 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Eric Anholtc153f452007-09-03 12:06:45 +1000105 if (!u->unique_len || u->unique_len > 1024)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000106 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Dave Airlie8410ea32010-12-15 03:16:38 +1000108 if (!dev->driver->bus->set_unique)
109 return -EINVAL;
110
111 ret = dev->driver->bus->set_unique(dev, master, u);
112 if (ret)
Chris Wilson3fb688f2010-08-04 11:09:42 +0100113 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 return 0;
Chris Wilson3fb688f2010-08-04 11:09:42 +0100116
117err:
118 drm_unset_busid(dev, master);
119 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Dave Airlie7c1c2872008-11-28 14:22:24 +1000122static int drm_set_busid(struct drm_device *dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Dave Airlie7c1c2872008-11-28 14:22:24 +1000124 struct drm_master *master = file_priv->master;
Dave Airlie8410ea32010-12-15 03:16:38 +1000125 int ret;
Chris Wilson3fb688f2010-08-04 11:09:42 +0100126
127 if (master->unique != NULL)
128 drm_unset_busid(dev, master);
Dave Airliefe347652006-01-02 21:19:39 +1100129
Dave Airlie8410ea32010-12-15 03:16:38 +1000130 ret = dev->driver->bus->set_busid(dev, master);
131 if (ret)
132 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return 0;
Chris Wilson3fb688f2010-08-04 11:09:42 +0100134err:
135 drm_unset_busid(dev, master);
136 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139/**
140 * Get a mapping information.
141 *
142 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000143 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 * \param cmd command.
145 * \param arg user argument, pointing to a drm_map structure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000146 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 * \return zero on success or a negative number on failure.
148 *
149 * Searches for the mapping with the specified offset and copies its information
150 * into userspace
151 */
Eric Anholtc153f452007-09-03 12:06:45 +1000152int drm_getmap(struct drm_device *dev, void *data,
153 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Eric Anholtc153f452007-09-03 12:06:45 +1000155 struct drm_map *map = data;
Dave Airlie55910512007-07-11 16:53:40 +1000156 struct drm_map_list *r_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 struct list_head *list;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000158 int idx;
159 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Eric Anholtc153f452007-09-03 12:06:45 +1000161 idx = map->offset;
Ilija Hadzic09b4ea42011-10-28 17:43:28 -0400162 if (idx < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 i = 0;
Ilija Hadzic09b4ea42011-10-28 17:43:28 -0400166 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000167 list_for_each(list, &dev->maplist) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000168 if (i == idx) {
Dave Airlie55910512007-07-11 16:53:40 +1000169 r_list = list_entry(list, struct drm_map_list, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 break;
171 }
172 i++;
173 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000174 if (!r_list || !r_list->map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100175 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return -EINVAL;
177 }
178
Eric Anholtc153f452007-09-03 12:06:45 +1000179 map->offset = r_list->map->offset;
180 map->size = r_list->map->size;
181 map->type = r_list->map->type;
182 map->flags = r_list->map->flags;
183 map->handle = (void *)(unsigned long) r_list->user_token;
184 map->mtrr = r_list->map->mtrr;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100185 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return 0;
188}
189
190/**
191 * Get client information.
192 *
193 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000194 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 * \param cmd command.
196 * \param arg user argument, pointing to a drm_client structure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000197 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 * \return zero on success or a negative number on failure.
199 *
200 * Searches for the client with the specified index and copies its information
201 * into userspace
202 */
Eric Anholtc153f452007-09-03 12:06:45 +1000203int drm_getclient(struct drm_device *dev, void *data,
204 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Eric Anholtc153f452007-09-03 12:06:45 +1000206 struct drm_client *client = data;
Dave Airlie84b1fd12007-07-11 15:53:27 +1000207 struct drm_file *pt;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000208 int idx;
209 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Eric Anholtc153f452007-09-03 12:06:45 +1000211 idx = client->idx;
Dave Airliebd1b3312007-05-26 05:01:51 +1000212 i = 0;
Ilija Hadzic09b4ea42011-10-28 17:43:28 -0400213
214 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000215 list_for_each_entry(pt, &dev->filelist, lhead) {
Eric Anholtb018fcd2007-11-22 18:46:54 +1000216 if (i++ >= idx) {
217 client->auth = pt->authenticated;
Eric W. Biederman5fce5e02012-02-07 16:47:26 -0800218 client->pid = pid_vnr(pt->pid);
219 client->uid = from_kuid_munged(current_user_ns(), pt->uid);
Eric Anholtb018fcd2007-11-22 18:46:54 +1000220 client->magic = pt->magic;
221 client->iocs = pt->ioctl_count;
222 mutex_unlock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000223
Eric Anholtb018fcd2007-11-22 18:46:54 +1000224 return 0;
225 }
226 }
Dave Airlie30e2fb12006-02-02 19:37:46 +1100227 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Eric Anholtb018fcd2007-11-22 18:46:54 +1000229 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000232/**
233 * Get statistics information.
234 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000236 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 * \param cmd command.
238 * \param arg user argument, pointing to a drm_stats structure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000239 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 * \return zero on success or a negative number on failure.
241 */
Eric Anholtc153f452007-09-03 12:06:45 +1000242int drm_getstats(struct drm_device *dev, void *data,
243 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Eric Anholtc153f452007-09-03 12:06:45 +1000245 struct drm_stats *stats = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000246 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Li Zefan246a3d12007-11-05 12:53:09 +1000248 memset(stats, 0, sizeof(*stats));
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 for (i = 0; i < dev->counters; i++) {
251 if (dev->types[i] == _DRM_STAT_LOCK)
Eric Anholtc153f452007-09-03 12:06:45 +1000252 stats->data[i].value =
Dave Airlie7c1c2872008-11-28 14:22:24 +1000253 (file_priv->master->lock.hw_lock ? file_priv->master->lock.hw_lock->lock : 0);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000254 else
Eric Anholtc153f452007-09-03 12:06:45 +1000255 stats->data[i].value = atomic_read(&dev->counts[i]);
256 stats->data[i].type = dev->types[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000258
Eric Anholtc153f452007-09-03 12:06:45 +1000259 stats->count = dev->counters;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return 0;
262}
263
264/**
Ben Skeggs9f354212011-02-21 11:17:35 +1000265 * Get device/driver capabilities
266 */
267int drm_getcap(struct drm_device *dev, void *data, struct drm_file *file_priv)
268{
269 struct drm_get_cap *req = data;
270
271 req->value = 0;
Dave Airliee73f88a2011-03-04 14:50:28 +1000272 switch (req->capability) {
273 case DRM_CAP_DUMB_BUFFER:
274 if (dev->driver->dumb_create)
275 req->value = 1;
276 break;
Dave Airlie51eab412011-03-24 20:54:35 +1000277 case DRM_CAP_VBLANK_HIGH_CRTC:
Ilija Hadzic19b01b52011-03-18 16:58:04 -0500278 req->value = 1;
279 break;
Dave Airlie019d96c2011-09-29 16:20:42 +0100280 case DRM_CAP_DUMB_PREFERRED_DEPTH:
281 req->value = dev->mode_config.preferred_depth;
282 break;
283 case DRM_CAP_DUMB_PREFER_SHADOW:
284 req->value = dev->mode_config.prefer_shadow;
285 break;
Dave Airlie4271a402012-04-02 14:11:50 +0100286 case DRM_CAP_PRIME:
287 req->value |= dev->driver->prime_fd_to_handle ? DRM_PRIME_CAP_IMPORT : 0;
288 req->value |= dev->driver->prime_handle_to_fd ? DRM_PRIME_CAP_EXPORT : 0;
289 break;
Imre Deakc61eef72012-10-23 18:53:26 +0000290 case DRM_CAP_TIMESTAMP_MONOTONIC:
291 req->value = drm_timestamp_monotonic;
292 break;
Dave Airliee73f88a2011-03-04 14:50:28 +1000293 default:
294 return -EINVAL;
295 }
Ben Skeggs9f354212011-02-21 11:17:35 +1000296 return 0;
297}
298
299/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 * Setversion ioctl.
301 *
302 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000303 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 * \param cmd command.
305 * \param arg user argument, pointing to a drm_lock structure.
306 * \return zero on success or negative number on failure.
307 *
308 * Sets the requested interface version
309 */
Eric Anholtc153f452007-09-03 12:06:45 +1000310int drm_setversion(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Eric Anholtc153f452007-09-03 12:06:45 +1000312 struct drm_set_version *sv = data;
313 int if_version, retcode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Eric Anholtc153f452007-09-03 12:06:45 +1000315 if (sv->drm_di_major != -1) {
316 if (sv->drm_di_major != DRM_IF_MAJOR ||
317 sv->drm_di_minor < 0 || sv->drm_di_minor > DRM_IF_MINOR) {
318 retcode = -EINVAL;
319 goto done;
320 }
321 if_version = DRM_IF_VERSION(sv->drm_di_major,
322 sv->drm_di_minor);
Dave Airlie3d774612006-08-07 20:07:43 +1000323 dev->if_version = max(if_version, dev->if_version);
Eric Anholtc153f452007-09-03 12:06:45 +1000324 if (sv->drm_di_minor >= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 /*
326 * Version 1.1 includes tying of DRM to specific device
Benjamin Herrenschmidtc17c2f82010-08-06 13:55:10 +1000327 * Version 1.4 has proper PCI domain support
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 */
Chris Wilson3fb688f2010-08-04 11:09:42 +0100329 retcode = drm_set_busid(dev, file_priv);
330 if (retcode)
331 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
333 }
334
Eric Anholtc153f452007-09-03 12:06:45 +1000335 if (sv->drm_dd_major != -1) {
336 if (sv->drm_dd_major != dev->driver->major ||
337 sv->drm_dd_minor < 0 || sv->drm_dd_minor >
338 dev->driver->minor) {
339 retcode = -EINVAL;
340 goto done;
341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 if (dev->driver->set_version)
Eric Anholtc153f452007-09-03 12:06:45 +1000344 dev->driver->set_version(dev, sv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
Eric Anholtc153f452007-09-03 12:06:45 +1000346
347done:
348 sv->drm_di_major = DRM_IF_MAJOR;
349 sv->drm_di_minor = DRM_IF_MINOR;
350 sv->drm_dd_major = dev->driver->major;
351 sv->drm_dd_minor = dev->driver->minor;
352
353 return retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
356/** No-op ioctl. */
Eric Anholtc153f452007-09-03 12:06:45 +1000357int drm_noop(struct drm_device *dev, void *data,
358 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 DRM_DEBUG("\n");
361 return 0;
362}
Daniel Vetterb2c606f2012-01-17 12:50:12 +0100363EXPORT_SYMBOL(drm_noop);