[intel] Quirk away MSI support on 945G/GM.
[platform/upstream/libdrm.git] / linux-core / 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         if (dev->unique != NULL)
132                 return -EBUSY;
133
134         dev->unique_len = 40;
135         dev->unique = drm_alloc(dev->unique_len + 1, DRM_MEM_DRIVER);
136         if (dev->unique == NULL)
137                 return -ENOMEM;
138
139         len = snprintf(dev->unique, dev->unique_len, "pci:%04x:%02x:%02x.%d",
140                        drm_get_pci_domain(dev),
141                        dev->pdev->bus->number,
142                        PCI_SLOT(dev->pdev->devfn),
143                        PCI_FUNC(dev->pdev->devfn));
144         if (len > dev->unique_len)
145                 DRM_ERROR("buffer overflow");
146
147         dev->devname =
148             drm_alloc(strlen(dev->driver->pci_driver.name) + dev->unique_len +
149                       2, DRM_MEM_DRIVER);
150         if (dev->devname == NULL)
151                 return -ENOMEM;
152
153         sprintf(dev->devname, "%s@%s", dev->driver->pci_driver.name,
154                 dev->unique);
155
156         return 0;
157 }
158
159 /**
160  * Get a mapping information.
161  *
162  * \param inode device inode.
163  * \param file_priv DRM file private.
164  * \param cmd command.
165  * \param arg user argument, pointing to a drm_map structure.
166  *
167  * \return zero on success or a negative number on failure.
168  *
169  * Searches for the mapping with the specified offset and copies its information
170  * into userspace
171  */
172 int drm_getmap(struct drm_device *dev, void *data,
173                struct drm_file *file_priv)
174 {
175         struct drm_map *map = data;
176         struct drm_map_list *r_list = NULL;
177         struct list_head *list;
178         int idx;
179         int i;
180
181         idx = map->offset;
182
183         mutex_lock(&dev->struct_mutex);
184         if (idx < 0) {
185                 mutex_unlock(&dev->struct_mutex);
186                 return -EINVAL;
187         }
188
189         i = 0;
190         list_for_each(list, &dev->maplist) {
191                 if (i == idx) {
192                         r_list = list_entry(list, struct drm_map_list, head);
193                         break;
194                 }
195                 i++;
196         }
197         if (!r_list || !r_list->map) {
198                 mutex_unlock(&dev->struct_mutex);
199                 return -EINVAL;
200         }
201
202         map->offset = r_list->map->offset;
203         map->size = r_list->map->size;
204         map->type = r_list->map->type;
205         map->flags = r_list->map->flags;
206         map->handle = (void *)(unsigned long) r_list->user_token;
207         map->mtrr = r_list->map->mtrr;
208         mutex_unlock(&dev->struct_mutex);
209
210         return 0;
211 }
212
213 /**
214  * Get client information.
215  *
216  * \param inode device inode.
217  * \param file_priv DRM file private.
218  * \param cmd command.
219  * \param arg user argument, pointing to a drm_client structure.
220  *
221  * \return zero on success or a negative number on failure.
222  *
223  * Searches for the client with the specified index and copies its information
224  * into userspace
225  */
226 int drm_getclient(struct drm_device *dev, void *data,
227                   struct drm_file *file_priv)
228 {
229         struct drm_client *client = data;
230         struct drm_file *pt;
231         int idx;
232         int i;
233
234         idx = client->idx;
235         mutex_lock(&dev->struct_mutex);
236
237         i = 0;
238         list_for_each_entry(pt, &dev->filelist, lhead) {
239                 if (i++ >= idx) {
240                         client->auth = pt->authenticated;
241                         client->pid = pt->pid;
242                         client->uid = pt->uid;
243                         client->magic = pt->magic;
244                         client->iocs = pt->ioctl_count;
245                         mutex_unlock(&dev->struct_mutex);
246
247                         return 0;
248                 }
249         }
250         mutex_unlock(&dev->struct_mutex);
251
252         return -EINVAL;
253 }
254
255 /**
256  * Get statistics information.
257  *
258  * \param inode device inode.
259  * \param file_priv DRM file private.
260  * \param cmd command.
261  * \param arg user argument, pointing to a drm_stats structure.
262  *
263  * \return zero on success or a negative number on failure.
264  */
265 int drm_getstats(struct drm_device *dev, void *data,
266                  struct drm_file *file_priv)
267 {
268         struct drm_stats *stats = data;
269         int i;
270
271         memset(stats, 0, sizeof(*stats));
272
273         mutex_lock(&dev->struct_mutex);
274
275         for (i = 0; i < dev->counters; i++) {
276                 if (dev->types[i] == _DRM_STAT_LOCK)
277                         stats->data[i].value =
278                             (dev->lock.hw_lock ? dev->lock.hw_lock->lock : 0);
279                 else
280                         stats->data[i].value = atomic_read(&dev->counts[i]);
281                 stats->data[i].type = dev->types[i];
282         }
283
284         stats->count = dev->counters;
285
286         mutex_unlock(&dev->struct_mutex);
287
288         return 0;
289 }
290
291 /**
292  * Setversion ioctl.
293  *
294  * \param inode device inode.
295  * \param file_priv DRM file private.
296  * \param cmd command.
297  * \param arg user argument, pointing to a drm_lock structure.
298  * \return zero on success or negative number on failure.
299  *
300  * Sets the requested interface version
301  */
302 int drm_setversion(struct drm_device *dev, void *data, struct drm_file *file_priv)
303 {
304         struct drm_set_version *sv = data;
305         int if_version, retcode = 0;
306
307         if (sv->drm_di_major != -1) {
308                 if (sv->drm_di_major != DRM_IF_MAJOR ||
309                     sv->drm_di_minor < 0 || sv->drm_di_minor > DRM_IF_MINOR) {
310                         retcode = -EINVAL;
311                         goto done;
312                 }
313                 if_version = DRM_IF_VERSION(sv->drm_di_major,
314                                             sv->drm_di_minor);
315                 dev->if_version = max(if_version, dev->if_version);
316                 if (sv->drm_di_minor >= 1) {
317                         /*
318                          * Version 1.1 includes tying of DRM to specific device
319                          */
320                         drm_set_busid(dev);
321                 }
322         }
323
324         if (sv->drm_dd_major != -1) {
325                 if (sv->drm_dd_major != dev->driver->major ||
326                     sv->drm_dd_minor < 0 || sv->drm_dd_minor >
327                     dev->driver->minor) {
328                         retcode = -EINVAL;
329                         goto done;
330                 }
331
332                 if (dev->driver->set_version)
333                         dev->driver->set_version(dev, sv);
334         }
335
336 done:
337         sv->drm_di_major = DRM_IF_MAJOR;
338         sv->drm_di_minor = DRM_IF_MINOR;
339         sv->drm_dd_major = dev->driver->major;
340         sv->drm_dd_minor = dev->driver->minor;
341
342         return retcode;
343 }
344
345 /** No-op ioctl. */
346 int drm_noop(struct drm_device *dev, void *data,
347              struct drm_file *file_priv)
348 {
349         DRM_DEBUG("\n");
350         return 0;
351 }