3 * IOCTL processing for DRM
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
10 * Created: Fri Jan 8 09:01:26 1999 by faith@valinux.com
12 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
13 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 * All Rights Reserved.
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:
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
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.
39 #include "linux/pci.h"
44 * \param inode device inode.
45 * \param file_priv DRM file private.
47 * \param arg user argument, pointing to a drm_unique structure.
48 * \return zero on success or a negative number on failure.
50 * Copies the bus id from drm_device::unique into user space.
52 int drm_getunique(struct drm_device *dev, void *data,
53 struct drm_file *file_priv)
55 struct drm_unique *u = data;
56 struct drm_master *master = file_priv->master;
58 if (u->unique_len >= master->unique_len) {
59 if (copy_to_user(u->unique, master->unique, master->unique_len))
62 u->unique_len = master->unique_len;
70 * \param inode device inode.
71 * \param file_priv DRM file private.
73 * \param arg user argument, pointing to a drm_unique structure.
74 * \return zero on success or a negative number on failure.
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.
81 int drm_setunique(struct drm_device *dev, void *data,
82 struct drm_file *file_priv)
84 struct drm_unique *u = data;
85 struct drm_master *master = file_priv->master;
86 int domain, bus, slot, func, ret;
88 if (master->unique_len || master->unique)
91 if (!u->unique_len || u->unique_len > 1024)
94 master->unique_len = u->unique_len;
95 master->unique_size = u->unique_len + 1;
96 master->unique = kmalloc(master->unique_size, GFP_KERNEL);
99 if (copy_from_user(master->unique, u->unique, master->unique_len))
102 master->unique[master->unique_len] = '\0';
104 dev->devname = kmalloc(strlen(dev->driver->pci_driver.name) +
105 strlen(master->unique) + 2, GFP_KERNEL);
109 sprintf(dev->devname, "%s@%s", dev->driver->pci_driver.name,
112 /* Return error if the busid submitted doesn't match the device's actual
115 ret = sscanf(master->unique, "PCI:%d:%d:%d", &bus, &slot, &func);
121 if ((domain != drm_get_pci_domain(dev)) ||
122 (bus != dev->pdev->bus->number) ||
123 (slot != PCI_SLOT(dev->pdev->devfn)) ||
124 (func != PCI_FUNC(dev->pdev->devfn)))
130 static int drm_set_busid(struct drm_device *dev, struct drm_file *file_priv)
132 struct drm_master *master = file_priv->master;
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);
139 if (master->unique == NULL)
142 len = snprintf(master->unique, master->unique_len,
143 "platform:%s", dev->platformdev->name);
145 if (len > master->unique_len)
146 DRM_ERROR("Unique buffer overflowed\n");
149 kmalloc(strlen(dev->platformdev->name) +
150 master->unique_len + 2, GFP_KERNEL);
152 if (dev->devname == NULL)
155 sprintf(dev->devname, "%s@%s", dev->platformdev->name,
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)
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");
174 master->unique_len = len;
177 kmalloc(strlen(dev->driver->pci_driver.name) +
178 master->unique_len + 2, GFP_KERNEL);
180 if (dev->devname == NULL)
183 sprintf(dev->devname, "%s@%s", dev->driver->pci_driver.name,
191 * Get a mapping information.
193 * \param inode device inode.
194 * \param file_priv DRM file private.
195 * \param cmd command.
196 * \param arg user argument, pointing to a drm_map structure.
198 * \return zero on success or a negative number on failure.
200 * Searches for the mapping with the specified offset and copies its information
203 int drm_getmap(struct drm_device *dev, void *data,
204 struct drm_file *file_priv)
206 struct drm_map *map = data;
207 struct drm_map_list *r_list = NULL;
208 struct list_head *list;
214 mutex_lock(&dev->struct_mutex);
216 mutex_unlock(&dev->struct_mutex);
221 list_for_each(list, &dev->maplist) {
223 r_list = list_entry(list, struct drm_map_list, head);
228 if (!r_list || !r_list->map) {
229 mutex_unlock(&dev->struct_mutex);
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;
239 mutex_unlock(&dev->struct_mutex);
245 * Get client information.
247 * \param inode device inode.
248 * \param file_priv DRM file private.
249 * \param cmd command.
250 * \param arg user argument, pointing to a drm_client structure.
252 * \return zero on success or a negative number on failure.
254 * Searches for the client with the specified index and copies its information
257 int drm_getclient(struct drm_device *dev, void *data,
258 struct drm_file *file_priv)
260 struct drm_client *client = data;
266 mutex_lock(&dev->struct_mutex);
269 list_for_each_entry(pt, &dev->filelist, lhead) {
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);
281 mutex_unlock(&dev->struct_mutex);
287 * Get statistics information.
289 * \param inode device inode.
290 * \param file_priv DRM file private.
291 * \param cmd command.
292 * \param arg user argument, pointing to a drm_stats structure.
294 * \return zero on success or a negative number on failure.
296 int drm_getstats(struct drm_device *dev, void *data,
297 struct drm_file *file_priv)
299 struct drm_stats *stats = data;
302 memset(stats, 0, sizeof(*stats));
304 mutex_lock(&dev->struct_mutex);
306 for (i = 0; i < dev->counters; i++) {
307 if (dev->types[i] == _DRM_STAT_LOCK)
308 stats->data[i].value =
309 (file_priv->master->lock.hw_lock ? file_priv->master->lock.hw_lock->lock : 0);
311 stats->data[i].value = atomic_read(&dev->counts[i]);
312 stats->data[i].type = dev->types[i];
315 stats->count = dev->counters;
317 mutex_unlock(&dev->struct_mutex);
325 * \param inode device inode.
326 * \param file_priv DRM file private.
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.
331 * Sets the requested interface version
333 int drm_setversion(struct drm_device *dev, void *data, struct drm_file *file_priv)
335 struct drm_set_version *sv = data;
336 int if_version, retcode = 0;
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) {
344 if_version = DRM_IF_VERSION(sv->drm_di_major,
346 dev->if_version = max(if_version, dev->if_version);
347 if (sv->drm_di_minor >= 1) {
349 * Version 1.1 includes tying of DRM to specific device
351 drm_set_busid(dev, file_priv);
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) {
363 if (dev->driver->set_version)
364 dev->driver->set_version(dev, sv);
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;
377 int drm_noop(struct drm_device *dev, void *data,
378 struct drm_file *file_priv)