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