UAPI: (Scripted) Convert #include "..." to #include <path/...> in drivers/gpu/
[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
760285e7
DH
36#include <drm/drmP.h>
37#include <drm/drm_core.h>
1da177e4 38
760285e7
DH
39#include <linux/pci.h>
40#include <linux/export.h>
1da177e4
LT
41
42/**
43 * Get the bus id.
b5e89ed5 44 *
1da177e4 45 * \param inode device inode.
6c340eac 46 * \param file_priv DRM file private.
1da177e4
LT
47 * \param cmd command.
48 * \param arg user argument, pointing to a drm_unique structure.
49 * \return zero on success or a negative number on failure.
50 *
51 * Copies the bus id from drm_device::unique into user space.
52 */
c153f45f
EA
53int drm_getunique(struct drm_device *dev, void *data,
54 struct drm_file *file_priv)
1da177e4 55{
c153f45f 56 struct drm_unique *u = data;
7c1c2871 57 struct drm_master *master = file_priv->master;
1da177e4 58
7c1c2871
DA
59 if (u->unique_len >= master->unique_len) {
60 if (copy_to_user(u->unique, master->unique, master->unique_len))
1da177e4
LT
61 return -EFAULT;
62 }
7c1c2871 63 u->unique_len = master->unique_len;
c153f45f 64
1da177e4
LT
65 return 0;
66}
67
3fb688fd
CW
68static void
69drm_unset_busid(struct drm_device *dev,
70 struct drm_master *master)
71{
72 kfree(dev->devname);
73 dev->devname = NULL;
74
75 kfree(master->unique);
76 master->unique = NULL;
77 master->unique_len = 0;
78 master->unique_size = 0;
79}
80
1da177e4
LT
81/**
82 * Set the bus id.
b5e89ed5 83 *
1da177e4 84 * \param inode device inode.
6c340eac 85 * \param file_priv DRM file private.
1da177e4
LT
86 * \param cmd command.
87 * \param arg user argument, pointing to a drm_unique structure.
88 * \return zero on success or a negative number on failure.
89 *
90 * Copies the bus id from userspace into drm_device::unique, and verifies that
91 * it matches the device this DRM is attached to (EINVAL otherwise). Deprecated
92 * in interface version 1.1 and will return EBUSY when setversion has requested
93 * version 1.1 or greater.
94 */
c153f45f
EA
95int drm_setunique(struct drm_device *dev, void *data,
96 struct drm_file *file_priv)
1da177e4 97{
c153f45f 98 struct drm_unique *u = data;
7c1c2871 99 struct drm_master *master = file_priv->master;
8410ea3b 100 int ret;
1da177e4 101
7c1c2871 102 if (master->unique_len || master->unique)
b5e89ed5 103 return -EBUSY;
1da177e4 104
c153f45f 105 if (!u->unique_len || u->unique_len > 1024)
b5e89ed5 106 return -EINVAL;
1da177e4 107
8410ea3b
DA
108 if (!dev->driver->bus->set_unique)
109 return -EINVAL;
b5e89ed5 110
8410ea3b
DA
111 ret = dev->driver->bus->set_unique(dev, master, u);
112 if (ret)
3fb688fd 113 goto err;
1da177e4
LT
114
115 return 0;
3fb688fd
CW
116
117err:
118 drm_unset_busid(dev, master);
119 return ret;
1da177e4
LT
120}
121
7c1c2871 122static int drm_set_busid(struct drm_device *dev, struct drm_file *file_priv)
1da177e4 123{
7c1c2871 124 struct drm_master *master = file_priv->master;
8410ea3b 125 int ret;
3fb688fd
CW
126
127 if (master->unique != NULL)
128 drm_unset_busid(dev, master);
fe34765b 129
8410ea3b
DA
130 ret = dev->driver->bus->set_busid(dev, master);
131 if (ret)
132 goto err;
1da177e4 133 return 0;
3fb688fd
CW
134err:
135 drm_unset_busid(dev, master);
136 return ret;
1da177e4
LT
137}
138
1da177e4
LT
139/**
140 * Get a mapping information.
141 *
142 * \param inode device inode.
6c340eac 143 * \param file_priv DRM file private.
1da177e4
LT
144 * \param cmd command.
145 * \param arg user argument, pointing to a drm_map structure.
b5e89ed5 146 *
1da177e4
LT
147 * \return zero on success or a negative number on failure.
148 *
149 * Searches for the mapping with the specified offset and copies its information
150 * into userspace
151 */
c153f45f
EA
152int drm_getmap(struct drm_device *dev, void *data,
153 struct drm_file *file_priv)
1da177e4 154{
c153f45f 155 struct drm_map *map = data;
55910517 156 struct drm_map_list *r_list = NULL;
1da177e4 157 struct list_head *list;
b5e89ed5
DA
158 int idx;
159 int i;
1da177e4 160
c153f45f 161 idx = map->offset;
09b4ea47 162 if (idx < 0)
1da177e4 163 return -EINVAL;
1da177e4
LT
164
165 i = 0;
09b4ea47 166 mutex_lock(&dev->struct_mutex);
bd1b331f 167 list_for_each(list, &dev->maplist) {
b5e89ed5 168 if (i == idx) {
55910517 169 r_list = list_entry(list, struct drm_map_list, head);
1da177e4
LT
170 break;
171 }
172 i++;
173 }
b5e89ed5 174 if (!r_list || !r_list->map) {
30e2fb18 175 mutex_unlock(&dev->struct_mutex);
1da177e4
LT
176 return -EINVAL;
177 }
178
c153f45f
EA
179 map->offset = r_list->map->offset;
180 map->size = r_list->map->size;
181 map->type = r_list->map->type;
182 map->flags = r_list->map->flags;
183 map->handle = (void *)(unsigned long) r_list->user_token;
184 map->mtrr = r_list->map->mtrr;
30e2fb18 185 mutex_unlock(&dev->struct_mutex);
1da177e4 186
1da177e4
LT
187 return 0;
188}
189
190/**
191 * Get client 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_client structure.
b5e89ed5 197 *
1da177e4
LT
198 * \return zero on success or a negative number on failure.
199 *
200 * Searches for the client with the specified index and copies its information
201 * into userspace
202 */
c153f45f
EA
203int drm_getclient(struct drm_device *dev, void *data,
204 struct drm_file *file_priv)
1da177e4 205{
c153f45f 206 struct drm_client *client = data;
84b1fd10 207 struct drm_file *pt;
b5e89ed5
DA
208 int idx;
209 int i;
1da177e4 210
c153f45f 211 idx = client->idx;
bd1b331f 212 i = 0;
09b4ea47
IH
213
214 mutex_lock(&dev->struct_mutex);
bd1b331f 215 list_for_each_entry(pt, &dev->filelist, lhead) {
b018fcda
EA
216 if (i++ >= idx) {
217 client->auth = pt->authenticated;
218 client->pid = pt->pid;
219 client->uid = pt->uid;
220 client->magic = pt->magic;
221 client->iocs = pt->ioctl_count;
222 mutex_unlock(&dev->struct_mutex);
223
224 return 0;
225 }
bd1b331f 226 }
30e2fb18 227 mutex_unlock(&dev->struct_mutex);
1da177e4 228
b018fcda 229 return -EINVAL;
1da177e4
LT
230}
231
b5e89ed5
DA
232/**
233 * Get statistics information.
234 *
1da177e4 235 * \param inode device inode.
6c340eac 236 * \param file_priv DRM file private.
1da177e4
LT
237 * \param cmd command.
238 * \param arg user argument, pointing to a drm_stats structure.
b5e89ed5 239 *
1da177e4
LT
240 * \return zero on success or a negative number on failure.
241 */
c153f45f
EA
242int drm_getstats(struct drm_device *dev, void *data,
243 struct drm_file *file_priv)
1da177e4 244{
c153f45f 245 struct drm_stats *stats = data;
b5e89ed5 246 int i;
1da177e4 247
246a3d18 248 memset(stats, 0, sizeof(*stats));
b5e89ed5 249
1da177e4
LT
250 for (i = 0; i < dev->counters; i++) {
251 if (dev->types[i] == _DRM_STAT_LOCK)
c153f45f 252 stats->data[i].value =
7c1c2871 253 (file_priv->master->lock.hw_lock ? file_priv->master->lock.hw_lock->lock : 0);
b5e89ed5 254 else
c153f45f
EA
255 stats->data[i].value = atomic_read(&dev->counts[i]);
256 stats->data[i].type = dev->types[i];
1da177e4 257 }
b5e89ed5 258
c153f45f 259 stats->count = dev->counters;
1da177e4 260
1da177e4
LT
261 return 0;
262}
263
9f35421e
BS
264/**
265 * Get device/driver capabilities
266 */
267int drm_getcap(struct drm_device *dev, void *data, struct drm_file *file_priv)
268{
269 struct drm_get_cap *req = data;
270
271 req->value = 0;
e73f88af
DA
272 switch (req->capability) {
273 case DRM_CAP_DUMB_BUFFER:
274 if (dev->driver->dumb_create)
275 req->value = 1;
276 break;
51eab416 277 case DRM_CAP_VBLANK_HIGH_CRTC:
19b01b5f
IH
278 req->value = 1;
279 break;
019d96cb
DA
280 case DRM_CAP_DUMB_PREFERRED_DEPTH:
281 req->value = dev->mode_config.preferred_depth;
282 break;
283 case DRM_CAP_DUMB_PREFER_SHADOW:
284 req->value = dev->mode_config.prefer_shadow;
285 break;
4271a409
DA
286 case DRM_CAP_PRIME:
287 req->value |= dev->driver->prime_fd_to_handle ? DRM_PRIME_CAP_IMPORT : 0;
288 req->value |= dev->driver->prime_handle_to_fd ? DRM_PRIME_CAP_EXPORT : 0;
289 break;
e73f88af
DA
290 default:
291 return -EINVAL;
292 }
9f35421e
BS
293 return 0;
294}
295
1da177e4
LT
296/**
297 * Setversion ioctl.
298 *
299 * \param inode device inode.
6c340eac 300 * \param file_priv DRM file private.
1da177e4
LT
301 * \param cmd command.
302 * \param arg user argument, pointing to a drm_lock structure.
303 * \return zero on success or negative number on failure.
304 *
305 * Sets the requested interface version
306 */
c153f45f 307int drm_setversion(struct drm_device *dev, void *data, struct drm_file *file_priv)
1da177e4 308{
c153f45f
EA
309 struct drm_set_version *sv = data;
310 int if_version, retcode = 0;
311
312 if (sv->drm_di_major != -1) {
313 if (sv->drm_di_major != DRM_IF_MAJOR ||
314 sv->drm_di_minor < 0 || sv->drm_di_minor > DRM_IF_MINOR) {
315 retcode = -EINVAL;
316 goto done;
317 }
318 if_version = DRM_IF_VERSION(sv->drm_di_major,
319 sv->drm_di_minor);
3d77461e 320 dev->if_version = max(if_version, dev->if_version);
c153f45f 321 if (sv->drm_di_minor >= 1) {
1da177e4
LT
322 /*
323 * Version 1.1 includes tying of DRM to specific device
c17c2f89 324 * Version 1.4 has proper PCI domain support
1da177e4 325 */
3fb688fd
CW
326 retcode = drm_set_busid(dev, file_priv);
327 if (retcode)
328 goto done;
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}
b2c606fe 360EXPORT_SYMBOL(drm_noop);