blob: b195e102e73797b2c6a316bdbe583091ced3dabe [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.
45 * \param filp file pointer.
46 * \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 */
52int drm_getunique(struct inode *inode, struct file *filp,
Dave Airlieb5e89ed2005-09-25 14:28:13 +100053 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Dave Airlie84b1fd12007-07-11 15:53:27 +100055 struct drm_file *priv = filp->private_data;
56 struct drm_device *dev = priv->head->dev;
Dave Airliec60ce622007-07-11 15:27:12 +100057 struct drm_unique __user *argp = (void __user *)arg;
58 struct drm_unique u;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 if (copy_from_user(&u, argp, sizeof(u)))
61 return -EFAULT;
62 if (u.unique_len >= dev->unique_len) {
63 if (copy_to_user(u.unique, dev->unique, dev->unique_len))
64 return -EFAULT;
65 }
66 u.unique_len = dev->unique_len;
67 if (copy_to_user(argp, &u, sizeof(u)))
68 return -EFAULT;
69 return 0;
70}
71
72/**
73 * Set the bus id.
Dave Airlieb5e89ed2005-09-25 14:28:13 +100074 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * \param inode device inode.
76 * \param filp file pointer.
77 * \param cmd command.
78 * \param arg user argument, pointing to a drm_unique structure.
79 * \return zero on success or a negative number on failure.
80 *
81 * Copies the bus id from userspace into drm_device::unique, and verifies that
82 * it matches the device this DRM is attached to (EINVAL otherwise). Deprecated
83 * in interface version 1.1 and will return EBUSY when setversion has requested
84 * version 1.1 or greater.
85 */
86int drm_setunique(struct inode *inode, struct file *filp,
Dave Airlieb5e89ed2005-09-25 14:28:13 +100087 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Dave Airlie84b1fd12007-07-11 15:53:27 +100089 struct drm_file *priv = filp->private_data;
90 struct drm_device *dev = priv->head->dev;
Dave Airliec60ce622007-07-11 15:27:12 +100091 struct drm_unique u;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100092 int domain, bus, slot, func, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Dave Airlieb5e89ed2005-09-25 14:28:13 +100094 if (dev->unique_len || dev->unique)
95 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Dave Airliec60ce622007-07-11 15:27:12 +100097 if (copy_from_user(&u, (struct drm_unique __user *) arg, sizeof(u)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return -EFAULT;
99
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000100 if (!u.unique_len || u.unique_len > 1024)
101 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 dev->unique_len = u.unique_len;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000104 dev->unique = drm_alloc(u.unique_len + 1, DRM_MEM_DRIVER);
105 if (!dev->unique)
106 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 if (copy_from_user(dev->unique, u.unique, dev->unique_len))
108 return -EFAULT;
109
110 dev->unique[dev->unique_len] = '\0';
111
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000112 dev->devname =
113 drm_alloc(strlen(dev->driver->pci_driver.name) +
114 strlen(dev->unique) + 2, DRM_MEM_DRIVER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 if (!dev->devname)
116 return -ENOMEM;
117
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000118 sprintf(dev->devname, "%s@%s", dev->driver->pci_driver.name,
119 dev->unique);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 /* Return error if the busid submitted doesn't match the device's actual
122 * busid.
123 */
124 ret = sscanf(dev->unique, "PCI:%d:%d:%d", &bus, &slot, &func);
125 if (ret != 3)
126 return DRM_ERR(EINVAL);
127 domain = bus >> 8;
128 bus &= 0xff;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000129
Dave Airlie332296012006-08-07 20:23:42 +1000130 if ((domain != drm_get_pci_domain(dev)) ||
Dave242ef0e2006-07-18 04:01:01 +1000131 (bus != dev->pdev->bus->number) ||
132 (slot != PCI_SLOT(dev->pdev->devfn)) ||
133 (func != PCI_FUNC(dev->pdev->devfn)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return -EINVAL;
135
136 return 0;
137}
138
Dave Airlie84b1fd12007-07-11 15:53:27 +1000139static int drm_set_busid(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Dave Airliefe347652006-01-02 21:19:39 +1100141 int len;
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 if (dev->unique != NULL)
Dave Airlie1f27ce62006-08-27 11:11:14 +1000144 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Dave Airliefe347652006-01-02 21:19:39 +1100146 dev->unique_len = 40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 dev->unique = drm_alloc(dev->unique_len + 1, DRM_MEM_DRIVER);
148 if (dev->unique == NULL)
Dave Airlie1f27ce62006-08-27 11:11:14 +1000149 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Dave Airliefe347652006-01-02 21:19:39 +1100151 len = snprintf(dev->unique, dev->unique_len, "pci:%04x:%02x:%02x.%d",
Dave Airlie332296012006-08-07 20:23:42 +1000152 drm_get_pci_domain(dev), dev->pdev->bus->number,
Dave242ef0e2006-07-18 04:01:01 +1000153 PCI_SLOT(dev->pdev->devfn),
154 PCI_FUNC(dev->pdev->devfn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Dave Airliefe347652006-01-02 21:19:39 +1100156 if (len > dev->unique_len)
157 DRM_ERROR("Unique buffer overflowed\n");
158
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000159 dev->devname =
160 drm_alloc(strlen(dev->driver->pci_driver.name) + dev->unique_len +
161 2, DRM_MEM_DRIVER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (dev->devname == NULL)
Dave Airlie1f27ce62006-08-27 11:11:14 +1000163 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000165 sprintf(dev->devname, "%s@%s", dev->driver->pci_driver.name,
166 dev->unique);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 return 0;
169}
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171/**
172 * Get a mapping information.
173 *
174 * \param inode device inode.
175 * \param filp file pointer.
176 * \param cmd command.
177 * \param arg user argument, pointing to a drm_map structure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000178 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 * \return zero on success or a negative number on failure.
180 *
181 * Searches for the mapping with the specified offset and copies its information
182 * into userspace
183 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000184int drm_getmap(struct inode *inode, struct file *filp,
185 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000187 struct drm_file *priv = filp->private_data;
188 struct drm_device *dev = priv->head->dev;
Dave Airliec60ce622007-07-11 15:27:12 +1000189 struct drm_map __user *argp = (void __user *)arg;
190 struct drm_map map;
Dave Airlie55910512007-07-11 16:53:40 +1000191 struct drm_map_list *r_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 struct list_head *list;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000193 int idx;
194 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 if (copy_from_user(&map, argp, sizeof(map)))
197 return -EFAULT;
198 idx = map.offset;
199
Dave Airlie30e2fb12006-02-02 19:37:46 +1100200 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (idx < 0) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100202 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return -EINVAL;
204 }
205
206 i = 0;
Dave Airliebd1b3312007-05-26 05:01:51 +1000207 list_for_each(list, &dev->maplist) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000208 if (i == idx) {
Dave Airlie55910512007-07-11 16:53:40 +1000209 r_list = list_entry(list, struct drm_map_list, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 break;
211 }
212 i++;
213 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000214 if (!r_list || !r_list->map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100215 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return -EINVAL;
217 }
218
219 map.offset = r_list->map->offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000220 map.size = r_list->map->size;
221 map.type = r_list->map->type;
222 map.flags = r_list->map->flags;
223 map.handle = (void *)(unsigned long)r_list->user_token;
224 map.mtrr = r_list->map->mtrr;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100225 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000227 if (copy_to_user(argp, &map, sizeof(map)))
228 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return 0;
230}
231
232/**
233 * Get client information.
234 *
235 * \param inode device inode.
236 * \param filp file pointer.
237 * \param cmd command.
238 * \param arg user argument, pointing to a drm_client 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 *
242 * Searches for the client with the specified index and copies its information
243 * into userspace
244 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000245int drm_getclient(struct inode *inode, struct file *filp,
246 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000248 struct drm_file *priv = filp->private_data;
249 struct drm_device *dev = priv->head->dev;
Dave Airliec60ce622007-07-11 15:27:12 +1000250 struct drm_client __user *argp = (struct drm_client __user *)arg;
251 struct drm_client client;
Dave Airlie84b1fd12007-07-11 15:53:27 +1000252 struct drm_file *pt;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000253 int idx;
254 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 if (copy_from_user(&client, argp, sizeof(client)))
257 return -EFAULT;
258 idx = client.idx;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100259 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000260
261 if (list_empty(&dev->filelist)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100262 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return -EINVAL;
264 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000265
266 i = 0;
267 list_for_each_entry(pt, &dev->filelist, lhead) {
268 if (i++ >= idx)
269 break;
270 }
271
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000272 client.auth = pt->authenticated;
273 client.pid = pt->pid;
274 client.uid = pt->uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 client.magic = pt->magic;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000276 client.iocs = pt->ioctl_count;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100277 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Dave Airliefe347652006-01-02 21:19:39 +1100279 if (copy_to_user(argp, &client, sizeof(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return -EFAULT;
281 return 0;
282}
283
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000284/**
285 * Get statistics information.
286 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 * \param inode device inode.
288 * \param filp file pointer.
289 * \param cmd command.
290 * \param arg user argument, pointing to a drm_stats structure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000291 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 * \return zero on success or a negative number on failure.
293 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000294int drm_getstats(struct inode *inode, struct file *filp,
295 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000297 struct drm_file *priv = filp->private_data;
298 struct drm_device *dev = priv->head->dev;
Dave Airliec60ce622007-07-11 15:27:12 +1000299 struct drm_stats stats;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000300 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 memset(&stats, 0, sizeof(stats));
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000303
Dave Airlie30e2fb12006-02-02 19:37:46 +1100304 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 for (i = 0; i < dev->counters; i++) {
307 if (dev->types[i] == _DRM_STAT_LOCK)
308 stats.data[i].value
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000309 = (dev->lock.hw_lock ? dev->lock.hw_lock->lock : 0);
310 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 stats.data[i].value = atomic_read(&dev->counts[i]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000312 stats.data[i].type = dev->types[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 stats.count = dev->counters;
316
Dave Airlie30e2fb12006-02-02 19:37:46 +1100317 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Dave Airliec60ce622007-07-11 15:27:12 +1000319 if (copy_to_user((struct drm_stats __user *) arg, &stats, sizeof(stats)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return -EFAULT;
321 return 0;
322}
323
324/**
325 * Setversion ioctl.
326 *
327 * \param inode device inode.
328 * \param filp file pointer.
329 * \param cmd command.
330 * \param arg user argument, pointing to a drm_lock structure.
331 * \return zero on success or negative number on failure.
332 *
333 * Sets the requested interface version
334 */
335int drm_setversion(DRM_IOCTL_ARGS)
336{
337 DRM_DEVICE;
Dave Airliec60ce622007-07-11 15:27:12 +1000338 struct drm_set_version sv;
339 struct drm_set_version retv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 int if_version;
Dave Airliec60ce622007-07-11 15:27:12 +1000341 struct drm_set_version __user *argp = (void __user *)data;
Dave Airlie1f27ce62006-08-27 11:11:14 +1000342 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Dave Airlie3d774612006-08-07 20:07:43 +1000344 if (copy_from_user(&sv, argp, sizeof(sv)))
345 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 retv.drm_di_major = DRM_IF_MAJOR;
348 retv.drm_di_minor = DRM_IF_MINOR;
Dave Airlie22eae942005-11-10 22:16:34 +1100349 retv.drm_dd_major = dev->driver->major;
350 retv.drm_dd_minor = dev->driver->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Dave Airlie1f27ce62006-08-27 11:11:14 +1000352 if (copy_to_user(argp, &retv, sizeof(retv)))
Dave Airlie3d774612006-08-07 20:07:43 +1000353 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 if (sv.drm_di_major != -1) {
356 if (sv.drm_di_major != DRM_IF_MAJOR ||
357 sv.drm_di_minor < 0 || sv.drm_di_minor > DRM_IF_MINOR)
Dave Airlie1f27ce62006-08-27 11:11:14 +1000358 return -EINVAL;
Dave Airliefe347652006-01-02 21:19:39 +1100359 if_version = DRM_IF_VERSION(sv.drm_di_major, sv.drm_di_minor);
Dave Airlie3d774612006-08-07 20:07:43 +1000360 dev->if_version = max(if_version, dev->if_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (sv.drm_di_minor >= 1) {
362 /*
363 * Version 1.1 includes tying of DRM to specific device
364 */
Dave Airlie1f27ce62006-08-27 11:11:14 +1000365 ret = drm_set_busid(dev);
366 if (ret)
367 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
369 }
370
371 if (sv.drm_dd_major != -1) {
Dave Airlie22eae942005-11-10 22:16:34 +1100372 if (sv.drm_dd_major != dev->driver->major ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000373 sv.drm_dd_minor < 0
Dave Airlie22eae942005-11-10 22:16:34 +1100374 || sv.drm_dd_minor > dev->driver->minor)
Dave Airlie1f27ce62006-08-27 11:11:14 +1000375 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 if (dev->driver->set_version)
378 dev->driver->set_version(dev, &sv);
379 }
380 return 0;
381}
382
383/** No-op ioctl. */
384int drm_noop(struct inode *inode, struct file *filp, unsigned int cmd,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000385 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
387 DRM_DEBUG("\n");
388 return 0;
389}