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
;
68 drm_unset_busid(struct drm_device
*dev
,
69 struct drm_master
*master
)
74 kfree(master
->unique
);
75 master
->unique
= NULL
;
76 master
->unique_len
= 0;
77 master
->unique_size
= 0;
83 * \param inode device inode.
84 * \param file_priv DRM file private.
86 * \param arg user argument, pointing to a drm_unique structure.
87 * \return zero on success or a negative number on failure.
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.
94 int drm_setunique(struct drm_device
*dev
, void *data
,
95 struct drm_file
*file_priv
)
97 struct drm_unique
*u
= data
;
98 struct drm_master
*master
= file_priv
->master
;
99 int domain
, bus
, slot
, func
, ret
;
101 if (master
->unique_len
|| master
->unique
)
104 if (!u
->unique_len
|| u
->unique_len
> 1024)
107 master
->unique_len
= u
->unique_len
;
108 master
->unique_size
= u
->unique_len
+ 1;
109 master
->unique
= kmalloc(master
->unique_size
, GFP_KERNEL
);
110 if (!master
->unique
) {
115 if (copy_from_user(master
->unique
, u
->unique
, master
->unique_len
)) {
120 master
->unique
[master
->unique_len
] = '\0';
122 dev
->devname
= kmalloc(strlen(dev
->driver
->pci_driver
.name
) +
123 strlen(master
->unique
) + 2, GFP_KERNEL
);
129 sprintf(dev
->devname
, "%s@%s", dev
->driver
->pci_driver
.name
,
132 /* Return error if the busid submitted doesn't match the device's actual
135 ret
= sscanf(master
->unique
, "PCI:%d:%d:%d", &bus
, &slot
, &func
);
144 if ((domain
!= drm_get_pci_domain(dev
)) ||
145 (bus
!= dev
->pdev
->bus
->number
) ||
146 (slot
!= PCI_SLOT(dev
->pdev
->devfn
)) ||
147 (func
!= PCI_FUNC(dev
->pdev
->devfn
))) {
155 drm_unset_busid(dev
, master
);
159 static int drm_set_busid(struct drm_device
*dev
, struct drm_file
*file_priv
)
161 struct drm_master
*master
= file_priv
->master
;
164 if (master
->unique
!= NULL
)
165 drm_unset_busid(dev
, master
);
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
);
171 if (master
->unique
== NULL
)
174 len
= snprintf(master
->unique
, master
->unique_len
,
175 "platform:%s", dev
->platformdev
->name
);
177 if (len
> master
->unique_len
) {
178 DRM_ERROR("Unique buffer overflowed\n");
184 kmalloc(strlen(dev
->platformdev
->name
) +
185 master
->unique_len
+ 2, GFP_KERNEL
);
187 if (dev
->devname
== NULL
) {
192 sprintf(dev
->devname
, "%s@%s", dev
->platformdev
->name
,
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
)
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
));
208 if (len
>= master
->unique_len
) {
209 DRM_ERROR("buffer overflow");
213 master
->unique_len
= len
;
216 kmalloc(strlen(dev
->driver
->pci_driver
.name
) +
217 master
->unique_len
+ 2, GFP_KERNEL
);
219 if (dev
->devname
== NULL
) {
224 sprintf(dev
->devname
, "%s@%s", dev
->driver
->pci_driver
.name
,
231 drm_unset_busid(dev
, master
);
236 * Get a mapping information.
238 * \param inode device inode.
239 * \param file_priv DRM file private.
240 * \param cmd command.
241 * \param arg user argument, pointing to a drm_map structure.
243 * \return zero on success or a negative number on failure.
245 * Searches for the mapping with the specified offset and copies its information
248 int drm_getmap(struct drm_device
*dev
, void *data
,
249 struct drm_file
*file_priv
)
251 struct drm_map
*map
= data
;
252 struct drm_map_list
*r_list
= NULL
;
253 struct list_head
*list
;
259 mutex_lock(&dev
->struct_mutex
);
261 mutex_unlock(&dev
->struct_mutex
);
266 list_for_each(list
, &dev
->maplist
) {
268 r_list
= list_entry(list
, struct drm_map_list
, head
);
273 if (!r_list
|| !r_list
->map
) {
274 mutex_unlock(&dev
->struct_mutex
);
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
;
284 mutex_unlock(&dev
->struct_mutex
);
290 * Get client information.
292 * \param inode device inode.
293 * \param file_priv DRM file private.
294 * \param cmd command.
295 * \param arg user argument, pointing to a drm_client structure.
297 * \return zero on success or a negative number on failure.
299 * Searches for the client with the specified index and copies its information
302 int drm_getclient(struct drm_device
*dev
, void *data
,
303 struct drm_file
*file_priv
)
305 struct drm_client
*client
= data
;
311 mutex_lock(&dev
->struct_mutex
);
314 list_for_each_entry(pt
, &dev
->filelist
, lhead
) {
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
);
326 mutex_unlock(&dev
->struct_mutex
);
332 * Get statistics information.
334 * \param inode device inode.
335 * \param file_priv DRM file private.
336 * \param cmd command.
337 * \param arg user argument, pointing to a drm_stats structure.
339 * \return zero on success or a negative number on failure.
341 int drm_getstats(struct drm_device
*dev
, void *data
,
342 struct drm_file
*file_priv
)
344 struct drm_stats
*stats
= data
;
347 memset(stats
, 0, sizeof(*stats
));
349 mutex_lock(&dev
->struct_mutex
);
351 for (i
= 0; i
< dev
->counters
; i
++) {
352 if (dev
->types
[i
] == _DRM_STAT_LOCK
)
353 stats
->data
[i
].value
=
354 (file_priv
->master
->lock
.hw_lock
? file_priv
->master
->lock
.hw_lock
->lock
: 0);
356 stats
->data
[i
].value
= atomic_read(&dev
->counts
[i
]);
357 stats
->data
[i
].type
= dev
->types
[i
];
360 stats
->count
= dev
->counters
;
362 mutex_unlock(&dev
->struct_mutex
);
370 * \param inode device inode.
371 * \param file_priv DRM file private.
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.
376 * Sets the requested interface version
378 int drm_setversion(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
380 struct drm_set_version
*sv
= data
;
381 int if_version
, retcode
= 0;
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
) {
389 if_version
= DRM_IF_VERSION(sv
->drm_di_major
,
391 dev
->if_version
= max(if_version
, dev
->if_version
);
392 if (sv
->drm_di_minor
>= 1) {
394 * Version 1.1 includes tying of DRM to specific device
395 * Version 1.4 has proper PCI domain support
397 retcode
= drm_set_busid(dev
, file_priv
);
403 if (sv
->drm_dd_major
!= -1) {
404 if (sv
->drm_dd_major
!= dev
->driver
->major
||
405 sv
->drm_dd_minor
< 0 || sv
->drm_dd_minor
>
406 dev
->driver
->minor
) {
411 if (dev
->driver
->set_version
)
412 dev
->driver
->set_version(dev
, sv
);
416 sv
->drm_di_major
= DRM_IF_MAJOR
;
417 sv
->drm_di_minor
= DRM_IF_MINOR
;
418 sv
->drm_dd_major
= dev
->driver
->major
;
419 sv
->drm_dd_minor
= dev
->driver
->minor
;
425 int drm_noop(struct drm_device
*dev
, void *data
,
426 struct drm_file
*file_priv
)