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