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