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 = drm_alloc(master->unique_size, DRM_MEM_DRIVER);
99 if (copy_from_user(master->unique, u->unique, master->unique_len))
102 master->unique[master->unique_len] = '\0';
105 drm_alloc(strlen(dev->driver->pci_driver.name) +
106 strlen(master->unique) + 2, DRM_MEM_DRIVER);
110 sprintf(dev->devname, "%s@%s", dev->driver->pci_driver.name,
113 /* Return error if the busid submitted doesn't match the device's actual
116 ret = sscanf(master->unique, "PCI:%d:%d:%d", &bus, &slot, &func);
122 if ((domain != drm_get_pci_domain(dev)) ||
123 (bus != dev->pdev->bus->number) ||
124 (slot != PCI_SLOT(dev->pdev->devfn)) ||
125 (func != PCI_FUNC(dev->pdev->devfn)))
131 static int drm_set_busid(struct drm_device *dev, struct drm_file *file_priv)
133 struct drm_master *master = file_priv->master;
136 if (master->unique != NULL)
139 master->unique_len = 40;
140 master->unique_size = master->unique_len;
141 master->unique = drm_alloc(master->unique_size, DRM_MEM_DRIVER);
142 if (master->unique == NULL)
145 len = snprintf(master->unique, master->unique_len, "pci:%04x:%02x:%02x.%d",
146 drm_get_pci_domain(dev),
147 dev->pdev->bus->number,
148 PCI_SLOT(dev->pdev->devfn),
149 PCI_FUNC(dev->pdev->devfn));
150 if (len >= master->unique_len)
151 DRM_ERROR("buffer overflow");
153 master->unique_len = len;
156 drm_alloc(strlen(dev->driver->pci_driver.name) + master->unique_len +
158 if (dev->devname == NULL)
161 sprintf(dev->devname, "%s@%s", dev->driver->pci_driver.name,
168 * Get a mapping information.
170 * \param inode device inode.
171 * \param file_priv DRM file private.
172 * \param cmd command.
173 * \param arg user argument, pointing to a drm_map structure.
175 * \return zero on success or a negative number on failure.
177 * Searches for the mapping with the specified offset and copies its information
180 int drm_getmap(struct drm_device *dev, void *data,
181 struct drm_file *file_priv)
183 struct drm_map *map = data;
184 struct drm_map_list *r_list = NULL;
185 struct list_head *list;
191 mutex_lock(&dev->struct_mutex);
193 mutex_unlock(&dev->struct_mutex);
198 list_for_each(list, &dev->maplist) {
200 r_list = list_entry(list, struct drm_map_list, head);
205 if (!r_list || !r_list->map) {
206 mutex_unlock(&dev->struct_mutex);
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;
216 mutex_unlock(&dev->struct_mutex);
222 * Get client information.
224 * \param inode device inode.
225 * \param file_priv DRM file private.
226 * \param cmd command.
227 * \param arg user argument, pointing to a drm_client structure.
229 * \return zero on success or a negative number on failure.
231 * Searches for the client with the specified index and copies its information
234 int drm_getclient(struct drm_device *dev, void *data,
235 struct drm_file *file_priv)
237 struct drm_client *client = data;
243 mutex_lock(&dev->struct_mutex);
246 list_for_each_entry(pt, &dev->filelist, lhead) {
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);
258 mutex_unlock(&dev->struct_mutex);
264 * Get statistics information.
266 * \param inode device inode.
267 * \param file_priv DRM file private.
268 * \param cmd command.
269 * \param arg user argument, pointing to a drm_stats structure.
271 * \return zero on success or a negative number on failure.
273 int drm_getstats(struct drm_device *dev, void *data,
274 struct drm_file *file_priv)
276 struct drm_stats *stats = data;
279 memset(stats, 0, sizeof(*stats));
281 mutex_lock(&dev->struct_mutex);
283 for (i = 0; i < dev->counters; i++) {
284 if (dev->types[i] == _DRM_STAT_LOCK)
285 stats->data[i].value =
286 (file_priv->master->lock.hw_lock ? file_priv->master->lock.hw_lock->lock : 0);
288 stats->data[i].value = atomic_read(&dev->counts[i]);
289 stats->data[i].type = dev->types[i];
292 stats->count = dev->counters;
294 mutex_unlock(&dev->struct_mutex);
302 * \param inode device inode.
303 * \param file_priv DRM file private.
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.
308 * Sets the requested interface version
310 int drm_setversion(struct drm_device *dev, void *data, struct drm_file *file_priv)
312 struct drm_set_version *sv = data;
313 int if_version, retcode = 0;
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) {
321 if_version = DRM_IF_VERSION(sv->drm_di_major,
323 dev->if_version = max(if_version, dev->if_version);
324 if (sv->drm_di_minor >= 1) {
326 * Version 1.1 includes tying of DRM to specific device
328 drm_set_busid(dev, file_priv);
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) {
340 if (dev->driver->set_version)
341 dev->driver->set_version(dev, sv);
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;
354 int drm_noop(struct drm_device *dev, void *data,
355 struct drm_file *file_priv)