Merge commit 'v2.6.26' into sched/devel
[pandora-kernel.git] / drivers / gpu / drm / drm_ioctl.c
1 /**
2  * \file drm_ioctl.c
3  * 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.
43  *
44  * \param inode device inode.
45  * \param file_priv DRM file private.
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  */
52 int drm_getunique(struct drm_device *dev, void *data,
53                   struct drm_file *file_priv)
54 {
55         struct drm_unique *u = data;
56
57         if (u->unique_len >= dev->unique_len) {
58                 if (copy_to_user(u->unique, dev->unique, dev->unique_len))
59                         return -EFAULT;
60         }
61         u->unique_len = dev->unique_len;
62
63         return 0;
64 }
65
66 /**
67  * Set the bus id.
68  *
69  * \param inode device inode.
70  * \param file_priv DRM file private.
71  * \param cmd command.
72  * \param arg user argument, pointing to a drm_unique structure.
73  * \return zero on success or a negative number on failure.
74  *
75  * Copies the bus id from userspace into drm_device::unique, and verifies that
76  * it matches the device this DRM is attached to (EINVAL otherwise).  Deprecated
77  * in interface version 1.1 and will return EBUSY when setversion has requested
78  * version 1.1 or greater.
79  */
80 int drm_setunique(struct drm_device *dev, void *data,
81                   struct drm_file *file_priv)
82 {
83         struct drm_unique *u = data;
84         int domain, bus, slot, func, ret;
85
86         if (dev->unique_len || dev->unique)
87                 return -EBUSY;
88
89         if (!u->unique_len || u->unique_len > 1024)
90                 return -EINVAL;
91
92         dev->unique_len = u->unique_len;
93         dev->unique = drm_alloc(u->unique_len + 1, DRM_MEM_DRIVER);
94         if (!dev->unique)
95                 return -ENOMEM;
96         if (copy_from_user(dev->unique, u->unique, dev->unique_len))
97                 return -EFAULT;
98
99         dev->unique[dev->unique_len] = '\0';
100
101         dev->devname =
102             drm_alloc(strlen(dev->driver->pci_driver.name) +
103                       strlen(dev->unique) + 2, DRM_MEM_DRIVER);
104         if (!dev->devname)
105                 return -ENOMEM;
106
107         sprintf(dev->devname, "%s@%s", dev->driver->pci_driver.name,
108                 dev->unique);
109
110         /* Return error if the busid submitted doesn't match the device's actual
111          * busid.
112          */
113         ret = sscanf(dev->unique, "PCI:%d:%d:%d", &bus, &slot, &func);
114         if (ret != 3)
115                 return -EINVAL;
116         domain = bus >> 8;
117         bus &= 0xff;
118
119         if ((domain != drm_get_pci_domain(dev)) ||
120             (bus != dev->pdev->bus->number) ||
121             (slot != PCI_SLOT(dev->pdev->devfn)) ||
122             (func != PCI_FUNC(dev->pdev->devfn)))
123                 return -EINVAL;
124
125         return 0;
126 }
127
128 static int drm_set_busid(struct drm_device * dev)
129 {
130         int len;
131
132         if (dev->unique != NULL)
133                 return 0;
134
135         dev->unique_len = 40;
136         dev->unique = drm_alloc(dev->unique_len + 1, DRM_MEM_DRIVER);
137         if (dev->unique == NULL)
138                 return -ENOMEM;
139
140         len = snprintf(dev->unique, dev->unique_len, "pci:%04x:%02x:%02x.%d",
141                        drm_get_pci_domain(dev), dev->pdev->bus->number,
142                        PCI_SLOT(dev->pdev->devfn),
143                        PCI_FUNC(dev->pdev->devfn));
144
145         if (len > dev->unique_len)
146                 DRM_ERROR("Unique buffer overflowed\n");
147
148         dev->devname =
149             drm_alloc(strlen(dev->driver->pci_driver.name) + dev->unique_len +
150                       2, DRM_MEM_DRIVER);
151         if (dev->devname == NULL)
152                 return -ENOMEM;
153
154         sprintf(dev->devname, "%s@%s", dev->driver->pci_driver.name,
155                 dev->unique);
156
157         return 0;
158 }
159
160 /**
161  * Get a mapping information.
162  *
163  * \param inode device inode.
164  * \param file_priv DRM file private.
165  * \param cmd command.
166  * \param arg user argument, pointing to a drm_map structure.
167  *
168  * \return zero on success or a negative number on failure.
169  *
170  * Searches for the mapping with the specified offset and copies its information
171  * into userspace
172  */
173 int drm_getmap(struct drm_device *dev, void *data,
174                struct drm_file *file_priv)
175 {
176         struct drm_map *map = data;
177         struct drm_map_list *r_list = NULL;
178         struct list_head *list;
179         int idx;
180         int i;
181
182         idx = map->offset;
183
184         mutex_lock(&dev->struct_mutex);
185         if (idx < 0) {
186                 mutex_unlock(&dev->struct_mutex);
187                 return -EINVAL;
188         }
189
190         i = 0;
191         list_for_each(list, &dev->maplist) {
192                 if (i == idx) {
193                         r_list = list_entry(list, struct drm_map_list, head);
194                         break;
195                 }
196                 i++;
197         }
198         if (!r_list || !r_list->map) {
199                 mutex_unlock(&dev->struct_mutex);
200                 return -EINVAL;
201         }
202
203         map->offset = r_list->map->offset;
204         map->size = r_list->map->size;
205         map->type = r_list->map->type;
206         map->flags = r_list->map->flags;
207         map->handle = (void *)(unsigned long) r_list->user_token;
208         map->mtrr = r_list->map->mtrr;
209         mutex_unlock(&dev->struct_mutex);
210
211         return 0;
212 }
213
214 /**
215  * Get client information.
216  *
217  * \param inode device inode.
218  * \param file_priv DRM file private.
219  * \param cmd command.
220  * \param arg user argument, pointing to a drm_client structure.
221  *
222  * \return zero on success or a negative number on failure.
223  *
224  * Searches for the client with the specified index and copies its information
225  * into userspace
226  */
227 int drm_getclient(struct drm_device *dev, void *data,
228                   struct drm_file *file_priv)
229 {
230         struct drm_client *client = data;
231         struct drm_file *pt;
232         int idx;
233         int i;
234
235         idx = client->idx;
236         mutex_lock(&dev->struct_mutex);
237
238         i = 0;
239         list_for_each_entry(pt, &dev->filelist, lhead) {
240                 if (i++ >= idx) {
241                         client->auth = pt->authenticated;
242                         client->pid = pt->pid;
243                         client->uid = pt->uid;
244                         client->magic = pt->magic;
245                         client->iocs = pt->ioctl_count;
246                         mutex_unlock(&dev->struct_mutex);
247
248                         return 0;
249                 }
250         }
251         mutex_unlock(&dev->struct_mutex);
252
253         return -EINVAL;
254 }
255
256 /**
257  * Get statistics information.
258  *
259  * \param inode device inode.
260  * \param file_priv DRM file private.
261  * \param cmd command.
262  * \param arg user argument, pointing to a drm_stats structure.
263  *
264  * \return zero on success or a negative number on failure.
265  */
266 int drm_getstats(struct drm_device *dev, void *data,
267                  struct drm_file *file_priv)
268 {
269         struct drm_stats *stats = data;
270         int i;
271
272         memset(stats, 0, sizeof(*stats));
273
274         mutex_lock(&dev->struct_mutex);
275
276         for (i = 0; i < dev->counters; i++) {
277                 if (dev->types[i] == _DRM_STAT_LOCK)
278                         stats->data[i].value =
279                             (dev->lock.hw_lock ? dev->lock.hw_lock->lock : 0);
280                 else
281                         stats->data[i].value = atomic_read(&dev->counts[i]);
282                 stats->data[i].type = dev->types[i];
283         }
284
285         stats->count = dev->counters;
286
287         mutex_unlock(&dev->struct_mutex);
288
289         return 0;
290 }
291
292 /**
293  * Setversion ioctl.
294  *
295  * \param inode device inode.
296  * \param file_priv DRM file private.
297  * \param cmd command.
298  * \param arg user argument, pointing to a drm_lock structure.
299  * \return zero on success or negative number on failure.
300  *
301  * Sets the requested interface version
302  */
303 int drm_setversion(struct drm_device *dev, void *data, struct drm_file *file_priv)
304 {
305         struct drm_set_version *sv = data;
306         int if_version, retcode = 0;
307
308         if (sv->drm_di_major != -1) {
309                 if (sv->drm_di_major != DRM_IF_MAJOR ||
310                     sv->drm_di_minor < 0 || sv->drm_di_minor > DRM_IF_MINOR) {
311                         retcode = -EINVAL;
312                         goto done;
313                 }
314                 if_version = DRM_IF_VERSION(sv->drm_di_major,
315                                             sv->drm_di_minor);
316                 dev->if_version = max(if_version, dev->if_version);
317                 if (sv->drm_di_minor >= 1) {
318                         /*
319                          * Version 1.1 includes tying of DRM to specific device
320                          */
321                         drm_set_busid(dev);
322                 }
323         }
324
325         if (sv->drm_dd_major != -1) {
326                 if (sv->drm_dd_major != dev->driver->major ||
327                     sv->drm_dd_minor < 0 || sv->drm_dd_minor >
328                     dev->driver->minor) {
329                         retcode = -EINVAL;
330                         goto done;
331                 }
332
333                 if (dev->driver->set_version)
334                         dev->driver->set_version(dev, sv);
335         }
336
337 done:
338         sv->drm_di_major = DRM_IF_MAJOR;
339         sv->drm_di_minor = DRM_IF_MINOR;
340         sv->drm_dd_major = dev->driver->major;
341         sv->drm_dd_minor = dev->driver->minor;
342
343         return retcode;
344 }
345
346 /** No-op ioctl. */
347 int drm_noop(struct drm_device *dev, void *data,
348              struct drm_file *file_priv)
349 {
350         DRM_DEBUG("\n");
351         return 0;
352 }