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