blob: 9b0feba6b063e4ba949119f0981a6e600835feaa [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 Airlieb5e89ed2005-09-25 14:28:13 +100055 drm_file_t *priv = filp->private_data;
56 drm_device_t *dev = priv->head->dev;
57 drm_unique_t __user *argp = (void __user *)arg;
58 drm_unique_t 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 Airlieb5e89ed2005-09-25 14:28:13 +100089 drm_file_t *priv = filp->private_data;
90 drm_device_t *dev = priv->head->dev;
91 drm_unique_t u;
92 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 Airlieb5e89ed2005-09-25 14:28:13 +100097 if (copy_from_user(&u, (drm_unique_t __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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 if ((domain != dev->pci_domain) ||
131 (bus != dev->pci_bus) ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000132 (slot != dev->pci_slot) || (func != dev->pci_func))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return -EINVAL;
134
135 return 0;
136}
137
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000138static int drm_set_busid(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 if (dev->unique != NULL)
141 return EBUSY;
142
143 dev->unique_len = 20;
144 dev->unique = drm_alloc(dev->unique_len + 1, DRM_MEM_DRIVER);
145 if (dev->unique == NULL)
146 return ENOMEM;
147
148 snprintf(dev->unique, dev->unique_len, "pci:%04x:%02x:%02x.%d",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000149 dev->pci_domain, dev->pci_bus, dev->pci_slot, dev->pci_func);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000151 dev->devname =
152 drm_alloc(strlen(dev->driver->pci_driver.name) + dev->unique_len +
153 2, DRM_MEM_DRIVER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if (dev->devname == NULL)
155 return ENOMEM;
156
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000157 sprintf(dev->devname, "%s@%s", dev->driver->pci_driver.name,
158 dev->unique);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 return 0;
161}
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/**
164 * Get a mapping information.
165 *
166 * \param inode device inode.
167 * \param filp file pointer.
168 * \param cmd command.
169 * \param arg user argument, pointing to a drm_map structure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000170 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 * \return zero on success or a negative number on failure.
172 *
173 * Searches for the mapping with the specified offset and copies its information
174 * into userspace
175 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000176int drm_getmap(struct inode *inode, struct file *filp,
177 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000179 drm_file_t *priv = filp->private_data;
180 drm_device_t *dev = priv->head->dev;
181 drm_map_t __user *argp = (void __user *)arg;
182 drm_map_t map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 drm_map_list_t *r_list = NULL;
184 struct list_head *list;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000185 int idx;
186 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 if (copy_from_user(&map, argp, sizeof(map)))
189 return -EFAULT;
190 idx = map.offset;
191
192 down(&dev->struct_sem);
193 if (idx < 0) {
194 up(&dev->struct_sem);
195 return -EINVAL;
196 }
197
198 i = 0;
199 list_for_each(list, &dev->maplist->head) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000200 if (i == idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 r_list = list_entry(list, drm_map_list_t, head);
202 break;
203 }
204 i++;
205 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000206 if (!r_list || !r_list->map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 up(&dev->struct_sem);
208 return -EINVAL;
209 }
210
211 map.offset = r_list->map->offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000212 map.size = r_list->map->size;
213 map.type = r_list->map->type;
214 map.flags = r_list->map->flags;
215 map.handle = (void *)(unsigned long)r_list->user_token;
216 map.mtrr = r_list->map->mtrr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 up(&dev->struct_sem);
218
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000219 if (copy_to_user(argp, &map, sizeof(map)))
220 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return 0;
222}
223
224/**
225 * Get client information.
226 *
227 * \param inode device inode.
228 * \param filp file pointer.
229 * \param cmd command.
230 * \param arg user argument, pointing to a drm_client structure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000231 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 * \return zero on success or a negative number on failure.
233 *
234 * Searches for the client with the specified index and copies its information
235 * into userspace
236 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000237int drm_getclient(struct inode *inode, struct file *filp,
238 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000240 drm_file_t *priv = filp->private_data;
241 drm_device_t *dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 drm_client_t __user *argp = (void __user *)arg;
243 drm_client_t client;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000244 drm_file_t *pt;
245 int idx;
246 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 if (copy_from_user(&client, argp, sizeof(client)))
249 return -EFAULT;
250 idx = client.idx;
251 down(&dev->struct_sem);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000252 for (i = 0, pt = dev->file_first; i < idx && pt; i++, pt = pt->next) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 if (!pt) {
255 up(&dev->struct_sem);
256 return -EINVAL;
257 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000258 client.auth = pt->authenticated;
259 client.pid = pt->pid;
260 client.uid = pt->uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 client.magic = pt->magic;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000262 client.iocs = pt->ioctl_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 up(&dev->struct_sem);
264
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000265 if (copy_to_user((drm_client_t __user *) arg, &client, sizeof(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return -EFAULT;
267 return 0;
268}
269
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000270/**
271 * Get statistics information.
272 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 * \param inode device inode.
274 * \param filp file pointer.
275 * \param cmd command.
276 * \param arg user argument, pointing to a drm_stats structure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000277 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 * \return zero on success or a negative number on failure.
279 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000280int drm_getstats(struct inode *inode, struct file *filp,
281 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000283 drm_file_t *priv = filp->private_data;
284 drm_device_t *dev = priv->head->dev;
285 drm_stats_t stats;
286 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 memset(&stats, 0, sizeof(stats));
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 down(&dev->struct_sem);
291
292 for (i = 0; i < dev->counters; i++) {
293 if (dev->types[i] == _DRM_STAT_LOCK)
294 stats.data[i].value
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000295 = (dev->lock.hw_lock ? dev->lock.hw_lock->lock : 0);
296 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 stats.data[i].value = atomic_read(&dev->counts[i]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000298 stats.data[i].type = dev->types[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 stats.count = dev->counters;
302
303 up(&dev->struct_sem);
304
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000305 if (copy_to_user((drm_stats_t __user *) arg, &stats, sizeof(stats)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return -EFAULT;
307 return 0;
308}
309
310/**
311 * Setversion ioctl.
312 *
313 * \param inode device inode.
314 * \param filp file pointer.
315 * \param cmd command.
316 * \param arg user argument, pointing to a drm_lock structure.
317 * \return zero on success or negative number on failure.
318 *
319 * Sets the requested interface version
320 */
321int drm_setversion(DRM_IOCTL_ARGS)
322{
323 DRM_DEVICE;
324 drm_set_version_t sv;
325 drm_set_version_t retv;
326 int if_version;
327 drm_set_version_t __user *argp = (void __user *)data;
328 drm_version_t version;
329
330 DRM_COPY_FROM_USER_IOCTL(sv, argp, sizeof(sv));
331
332 memset(&version, 0, sizeof(version));
333
334 dev->driver->version(&version);
335 retv.drm_di_major = DRM_IF_MAJOR;
336 retv.drm_di_minor = DRM_IF_MINOR;
337 retv.drm_dd_major = version.version_major;
338 retv.drm_dd_minor = version.version_minor;
339
340 DRM_COPY_TO_USER_IOCTL(argp, retv, sizeof(sv));
341
342 if (sv.drm_di_major != -1) {
343 if (sv.drm_di_major != DRM_IF_MAJOR ||
344 sv.drm_di_minor < 0 || sv.drm_di_minor > DRM_IF_MINOR)
345 return EINVAL;
346 if_version = DRM_IF_VERSION(sv.drm_di_major, sv.drm_dd_minor);
347 dev->if_version = DRM_MAX(if_version, dev->if_version);
348 if (sv.drm_di_minor >= 1) {
349 /*
350 * Version 1.1 includes tying of DRM to specific device
351 */
352 drm_set_busid(dev);
353 }
354 }
355
356 if (sv.drm_dd_major != -1) {
357 if (sv.drm_dd_major != version.version_major ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000358 sv.drm_dd_minor < 0
359 || sv.drm_dd_minor > version.version_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return EINVAL;
361
362 if (dev->driver->set_version)
363 dev->driver->set_version(dev, &sv);
364 }
365 return 0;
366}
367
368/** No-op ioctl. */
369int drm_noop(struct inode *inode, struct file *filp, unsigned int cmd,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000370 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 DRM_DEBUG("\n");
373 return 0;
374}