blob: 9b9ff46c2378b8601661e8dacbd176f7e40d979d [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
36#include "drmP.h"
37#include "drm_core.h"
38
39#include "linux/pci.h"
40
41/**
42 * Get the bus id.
Dave Airlieb5e89ed2005-09-25 14:28:13 +100043 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +100045 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * \param cmd command.
47 * \param arg user argument, pointing to a drm_unique structure.
48 * \return zero on success or a negative number on failure.
49 *
50 * Copies the bus id from drm_device::unique into user space.
51 */
Eric Anholtc153f452007-09-03 12:06:45 +100052int drm_getunique(struct drm_device *dev, void *data,
53 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Eric Anholtc153f452007-09-03 12:06:45 +100055 struct drm_unique *u = data;
Dave Airlie7c1c2872008-11-28 14:22:24 +100056 struct drm_master *master = file_priv->master;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Dave Airlie7c1c2872008-11-28 14:22:24 +100058 if (u->unique_len >= master->unique_len) {
59 if (copy_to_user(u->unique, master->unique, master->unique_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 return -EFAULT;
61 }
Dave Airlie7c1c2872008-11-28 14:22:24 +100062 u->unique_len = master->unique_len;
Eric Anholtc153f452007-09-03 12:06:45 +100063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 return 0;
65}
66
67/**
68 * Set the bus id.
Dave Airlieb5e89ed2005-09-25 14:28:13 +100069 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +100071 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * \param cmd command.
73 * \param arg user argument, pointing to a drm_unique structure.
74 * \return zero on success or a negative number on failure.
75 *
76 * Copies the bus id from userspace into drm_device::unique, and verifies that
77 * it matches the device this DRM is attached to (EINVAL otherwise). Deprecated
78 * in interface version 1.1 and will return EBUSY when setversion has requested
79 * version 1.1 or greater.
80 */
Eric Anholtc153f452007-09-03 12:06:45 +100081int drm_setunique(struct drm_device *dev, void *data,
82 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Eric Anholtc153f452007-09-03 12:06:45 +100084 struct drm_unique *u = data;
Dave Airlie7c1c2872008-11-28 14:22:24 +100085 struct drm_master *master = file_priv->master;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100086 int domain, bus, slot, func, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Dave Airlie7c1c2872008-11-28 14:22:24 +100088 if (master->unique_len || master->unique)
Dave Airlieb5e89ed2005-09-25 14:28:13 +100089 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Eric Anholtc153f452007-09-03 12:06:45 +100091 if (!u->unique_len || u->unique_len > 1024)
Dave Airlieb5e89ed2005-09-25 14:28:13 +100092 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Dave Airlie7c1c2872008-11-28 14:22:24 +100094 master->unique_len = u->unique_len;
Vegard Nossum1147c9c2008-12-02 13:38:47 +100095 master->unique_size = u->unique_len + 1;
Eric Anholt9a298b22009-03-24 12:23:04 -070096 master->unique = kmalloc(master->unique_size, GFP_KERNEL);
Dave Airlie7c1c2872008-11-28 14:22:24 +100097 if (!master->unique)
Dave Airlieb5e89ed2005-09-25 14:28:13 +100098 return -ENOMEM;
Dave Airlie7c1c2872008-11-28 14:22:24 +100099 if (copy_from_user(master->unique, u->unique, master->unique_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return -EFAULT;
101
Dave Airlie7c1c2872008-11-28 14:22:24 +1000102 master->unique[master->unique_len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Eric Anholt9a298b22009-03-24 12:23:04 -0700104 dev->devname = kmalloc(strlen(dev->driver->pci_driver.name) +
105 strlen(master->unique) + 2, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 if (!dev->devname)
107 return -ENOMEM;
108
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000109 sprintf(dev->devname, "%s@%s", dev->driver->pci_driver.name,
Dave Airlie7c1c2872008-11-28 14:22:24 +1000110 master->unique);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 /* Return error if the busid submitted doesn't match the device's actual
113 * busid.
114 */
Dave Airlie7c1c2872008-11-28 14:22:24 +1000115 ret = sscanf(master->unique, "PCI:%d:%d:%d", &bus, &slot, &func);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (ret != 3)
Eric Anholt20caafa2007-08-25 19:22:43 +1000117 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 domain = bus >> 8;
119 bus &= 0xff;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000120
Dave Airlie332296012006-08-07 20:23:42 +1000121 if ((domain != drm_get_pci_domain(dev)) ||
Dave242ef0e2006-07-18 04:01:01 +1000122 (bus != dev->pdev->bus->number) ||
123 (slot != PCI_SLOT(dev->pdev->devfn)) ||
124 (func != PCI_FUNC(dev->pdev->devfn)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return -EINVAL;
126
127 return 0;
128}
129
Dave Airlie7c1c2872008-11-28 14:22:24 +1000130static int drm_set_busid(struct drm_device *dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Dave Airlie7c1c2872008-11-28 14:22:24 +1000132 struct drm_master *master = file_priv->master;
Dave Airliefe347652006-01-02 21:19:39 +1100133 int len;
134
Dave Airlie7c1c2872008-11-28 14:22:24 +1000135 if (master->unique != NULL)
136 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Dave Airlie7c1c2872008-11-28 14:22:24 +1000138 master->unique_len = 40;
Vegard Nossum1147c9c2008-12-02 13:38:47 +1000139 master->unique_size = master->unique_len;
Eric Anholt9a298b22009-03-24 12:23:04 -0700140 master->unique = kmalloc(master->unique_size, GFP_KERNEL);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000141 if (master->unique == NULL)
Dave Airlie1f27ce62006-08-27 11:11:14 +1000142 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Dave Airlie7c1c2872008-11-28 14:22:24 +1000144 len = snprintf(master->unique, master->unique_len, "pci:%04x:%02x:%02x.%d",
145 drm_get_pci_domain(dev),
146 dev->pdev->bus->number,
Dave242ef0e2006-07-18 04:01:01 +1000147 PCI_SLOT(dev->pdev->devfn),
148 PCI_FUNC(dev->pdev->devfn));
Vegard Nossum1147c9c2008-12-02 13:38:47 +1000149 if (len >= master->unique_len)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000150 DRM_ERROR("buffer overflow");
Vegard Nossum1147c9c2008-12-02 13:38:47 +1000151 else
152 master->unique_len = len;
Dave Airliefe347652006-01-02 21:19:39 +1100153
Eric Anholt9a298b22009-03-24 12:23:04 -0700154 dev->devname = kmalloc(strlen(dev->driver->pci_driver.name) +
155 master->unique_len + 2, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 if (dev->devname == NULL)
Dave Airlie1f27ce62006-08-27 11:11:14 +1000157 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000159 sprintf(dev->devname, "%s@%s", dev->driver->pci_driver.name,
Dave Airlie7c1c2872008-11-28 14:22:24 +1000160 master->unique);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 return 0;
163}
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165/**
166 * Get a mapping information.
167 *
168 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000169 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 * \param cmd command.
171 * \param arg user argument, pointing to a drm_map structure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000172 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 * \return zero on success or a negative number on failure.
174 *
175 * Searches for the mapping with the specified offset and copies its information
176 * into userspace
177 */
Eric Anholtc153f452007-09-03 12:06:45 +1000178int drm_getmap(struct drm_device *dev, void *data,
179 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Eric Anholtc153f452007-09-03 12:06:45 +1000181 struct drm_map *map = data;
Dave Airlie55910512007-07-11 16:53:40 +1000182 struct drm_map_list *r_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 struct list_head *list;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000184 int idx;
185 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Eric Anholtc153f452007-09-03 12:06:45 +1000187 idx = map->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Dave Airlie30e2fb12006-02-02 19:37:46 +1100189 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (idx < 0) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100191 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return -EINVAL;
193 }
194
195 i = 0;
Dave Airliebd1b3312007-05-26 05:01:51 +1000196 list_for_each(list, &dev->maplist) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000197 if (i == idx) {
Dave Airlie55910512007-07-11 16:53:40 +1000198 r_list = list_entry(list, struct drm_map_list, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 break;
200 }
201 i++;
202 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000203 if (!r_list || !r_list->map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100204 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return -EINVAL;
206 }
207
Eric Anholtc153f452007-09-03 12:06:45 +1000208 map->offset = r_list->map->offset;
209 map->size = r_list->map->size;
210 map->type = r_list->map->type;
211 map->flags = r_list->map->flags;
212 map->handle = (void *)(unsigned long) r_list->user_token;
213 map->mtrr = r_list->map->mtrr;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100214 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return 0;
217}
218
219/**
220 * Get client information.
221 *
222 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000223 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 * \param cmd command.
225 * \param arg user argument, pointing to a drm_client structure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000226 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 * \return zero on success or a negative number on failure.
228 *
229 * Searches for the client with the specified index and copies its information
230 * into userspace
231 */
Eric Anholtc153f452007-09-03 12:06:45 +1000232int drm_getclient(struct drm_device *dev, void *data,
233 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Eric Anholtc153f452007-09-03 12:06:45 +1000235 struct drm_client *client = data;
Dave Airlie84b1fd12007-07-11 15:53:27 +1000236 struct drm_file *pt;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000237 int idx;
238 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Eric Anholtc153f452007-09-03 12:06:45 +1000240 idx = client->idx;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100241 mutex_lock(&dev->struct_mutex);
Dave Airliebc5f4522007-11-05 12:50:58 +1000242
Dave Airliebd1b3312007-05-26 05:01:51 +1000243 i = 0;
244 list_for_each_entry(pt, &dev->filelist, lhead) {
Eric Anholtb018fcd2007-11-22 18:46:54 +1000245 if (i++ >= idx) {
246 client->auth = pt->authenticated;
247 client->pid = pt->pid;
248 client->uid = pt->uid;
249 client->magic = pt->magic;
250 client->iocs = pt->ioctl_count;
251 mutex_unlock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000252
Eric Anholtb018fcd2007-11-22 18:46:54 +1000253 return 0;
254 }
255 }
Dave Airlie30e2fb12006-02-02 19:37:46 +1100256 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Eric Anholtb018fcd2007-11-22 18:46:54 +1000258 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000261/**
262 * Get statistics information.
263 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000265 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 * \param cmd command.
267 * \param arg user argument, pointing to a drm_stats structure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000268 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 * \return zero on success or a negative number on failure.
270 */
Eric Anholtc153f452007-09-03 12:06:45 +1000271int drm_getstats(struct drm_device *dev, void *data,
272 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Eric Anholtc153f452007-09-03 12:06:45 +1000274 struct drm_stats *stats = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000275 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Li Zefan246a3d12007-11-05 12:53:09 +1000277 memset(stats, 0, sizeof(*stats));
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000278
Dave Airlie30e2fb12006-02-02 19:37:46 +1100279 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 for (i = 0; i < dev->counters; i++) {
282 if (dev->types[i] == _DRM_STAT_LOCK)
Eric Anholtc153f452007-09-03 12:06:45 +1000283 stats->data[i].value =
Dave Airlie7c1c2872008-11-28 14:22:24 +1000284 (file_priv->master->lock.hw_lock ? file_priv->master->lock.hw_lock->lock : 0);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000285 else
Eric Anholtc153f452007-09-03 12:06:45 +1000286 stats->data[i].value = atomic_read(&dev->counts[i]);
287 stats->data[i].type = dev->types[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000289
Eric Anholtc153f452007-09-03 12:06:45 +1000290 stats->count = dev->counters;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Dave Airlie30e2fb12006-02-02 19:37:46 +1100292 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return 0;
295}
296
297/**
298 * Setversion ioctl.
299 *
300 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000301 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 * \param cmd command.
303 * \param arg user argument, pointing to a drm_lock structure.
304 * \return zero on success or negative number on failure.
305 *
306 * Sets the requested interface version
307 */
Eric Anholtc153f452007-09-03 12:06:45 +1000308int drm_setversion(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Eric Anholtc153f452007-09-03 12:06:45 +1000310 struct drm_set_version *sv = data;
311 int if_version, retcode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Eric Anholtc153f452007-09-03 12:06:45 +1000313 if (sv->drm_di_major != -1) {
314 if (sv->drm_di_major != DRM_IF_MAJOR ||
315 sv->drm_di_minor < 0 || sv->drm_di_minor > DRM_IF_MINOR) {
316 retcode = -EINVAL;
317 goto done;
318 }
319 if_version = DRM_IF_VERSION(sv->drm_di_major,
320 sv->drm_di_minor);
Dave Airlie3d774612006-08-07 20:07:43 +1000321 dev->if_version = max(if_version, dev->if_version);
Eric Anholtc153f452007-09-03 12:06:45 +1000322 if (sv->drm_di_minor >= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 /*
324 * Version 1.1 includes tying of DRM to specific device
325 */
Dave Airlie7c1c2872008-11-28 14:22:24 +1000326 drm_set_busid(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328 }
329
Eric Anholtc153f452007-09-03 12:06:45 +1000330 if (sv->drm_dd_major != -1) {
331 if (sv->drm_dd_major != dev->driver->major ||
332 sv->drm_dd_minor < 0 || sv->drm_dd_minor >
333 dev->driver->minor) {
334 retcode = -EINVAL;
335 goto done;
336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 if (dev->driver->set_version)
Eric Anholtc153f452007-09-03 12:06:45 +1000339 dev->driver->set_version(dev, sv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
Eric Anholtc153f452007-09-03 12:06:45 +1000341
342done:
343 sv->drm_di_major = DRM_IF_MAJOR;
344 sv->drm_di_minor = DRM_IF_MINOR;
345 sv->drm_dd_major = dev->driver->major;
346 sv->drm_dd_minor = dev->driver->minor;
347
348 return retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
351/** No-op ioctl. */
Eric Anholtc153f452007-09-03 12:06:45 +1000352int drm_noop(struct drm_device *dev, void *data,
353 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 DRM_DEBUG("\n");
356 return 0;
357}