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 inode
*inode
, struct drm_file
*file_priv
,
53 unsigned int cmd
, unsigned long arg
)
55 struct drm_device
*dev
= file_priv
->head
->dev
;
56 struct drm_unique __user
*argp
= (void __user
*)arg
;
59 if (copy_from_user(&u
, argp
, sizeof(u
)))
61 if (u
.unique_len
>= dev
->unique_len
) {
62 if (copy_to_user(u
.unique
, dev
->unique
, dev
->unique_len
))
65 u
.unique_len
= dev
->unique_len
;
66 if (copy_to_user(argp
, &u
, sizeof(u
)))
74 * \param inode device inode.
75 * \param file_priv DRM file private.
77 * \param arg user argument, pointing to a drm_unique structure.
78 * \return zero on success or a negative number on failure.
80 * Copies the bus id from userspace into drm_device::unique, and verifies that
81 * it matches the device this DRM is attached to (EINVAL otherwise). Deprecated
82 * in interface version 1.1 and will return EBUSY when setversion has requested
83 * version 1.1 or greater.
85 int drm_setunique(struct inode
*inode
, struct drm_file
*file_priv
,
86 unsigned int cmd
, unsigned long arg
)
88 struct drm_device
*dev
= file_priv
->head
->dev
;
90 int domain
, bus
, slot
, func
, ret
;
92 if (dev
->unique_len
|| dev
->unique
)
95 if (copy_from_user(&u
, (struct drm_unique __user
*) arg
, sizeof(u
)))
98 if (!u
.unique_len
|| u
.unique_len
> 1024)
101 dev
->unique_len
= u
.unique_len
;
102 dev
->unique
= drm_alloc(u
.unique_len
+ 1, DRM_MEM_DRIVER
);
105 if (copy_from_user(dev
->unique
, u
.unique
, dev
->unique_len
))
108 dev
->unique
[dev
->unique_len
] = '\0';
111 drm_alloc(strlen(dev
->driver
->pci_driver
.name
) +
112 strlen(dev
->unique
) + 2, DRM_MEM_DRIVER
);
116 sprintf(dev
->devname
, "%s@%s", dev
->driver
->pci_driver
.name
,
119 /* Return error if the busid submitted doesn't match the device's actual
122 ret
= sscanf(dev
->unique
, "PCI:%d:%d:%d", &bus
, &slot
, &func
);
128 if ((domain
!= drm_get_pci_domain(dev
)) ||
129 (bus
!= dev
->pdev
->bus
->number
) ||
130 (slot
!= PCI_SLOT(dev
->pdev
->devfn
)) ||
131 (func
!= PCI_FUNC(dev
->pdev
->devfn
)))
137 static int drm_set_busid(struct drm_device
* dev
)
141 if (dev
->unique
!= NULL
)
144 dev
->unique_len
= 40;
145 dev
->unique
= drm_alloc(dev
->unique_len
+ 1, DRM_MEM_DRIVER
);
146 if (dev
->unique
== NULL
)
149 len
= snprintf(dev
->unique
, dev
->unique_len
, "pci:%04x:%02x:%02x.%d",
150 drm_get_pci_domain(dev
), dev
->pdev
->bus
->number
,
151 PCI_SLOT(dev
->pdev
->devfn
),
152 PCI_FUNC(dev
->pdev
->devfn
));
154 if (len
> dev
->unique_len
)
155 DRM_ERROR("Unique buffer overflowed\n");
158 drm_alloc(strlen(dev
->driver
->pci_driver
.name
) + dev
->unique_len
+
160 if (dev
->devname
== NULL
)
163 sprintf(dev
->devname
, "%s@%s", dev
->driver
->pci_driver
.name
,
170 * Get a mapping information.
172 * \param inode device inode.
173 * \param file_priv DRM file private.
174 * \param cmd command.
175 * \param arg user argument, pointing to a drm_map structure.
177 * \return zero on success or a negative number on failure.
179 * Searches for the mapping with the specified offset and copies its information
182 int drm_getmap(struct inode
*inode
, struct drm_file
*file_priv
,
183 unsigned int cmd
, unsigned long arg
)
185 struct drm_device
*dev
= file_priv
->head
->dev
;
186 struct drm_map __user
*argp
= (void __user
*)arg
;
188 struct drm_map_list
*r_list
= NULL
;
189 struct list_head
*list
;
193 if (copy_from_user(&map
, argp
, sizeof(map
)))
197 mutex_lock(&dev
->struct_mutex
);
199 mutex_unlock(&dev
->struct_mutex
);
204 list_for_each(list
, &dev
->maplist
) {
206 r_list
= list_entry(list
, struct drm_map_list
, head
);
211 if (!r_list
|| !r_list
->map
) {
212 mutex_unlock(&dev
->struct_mutex
);
216 map
.offset
= r_list
->map
->offset
;
217 map
.size
= r_list
->map
->size
;
218 map
.type
= r_list
->map
->type
;
219 map
.flags
= r_list
->map
->flags
;
220 map
.handle
= (void *)(unsigned long)r_list
->user_token
;
221 map
.mtrr
= r_list
->map
->mtrr
;
222 mutex_unlock(&dev
->struct_mutex
);
224 if (copy_to_user(argp
, &map
, sizeof(map
)))
230 * Get client information.
232 * \param inode device inode.
233 * \param file_priv DRM file private.
234 * \param cmd command.
235 * \param arg user argument, pointing to a drm_client structure.
237 * \return zero on success or a negative number on failure.
239 * Searches for the client with the specified index and copies its information
242 int drm_getclient(struct inode
*inode
, struct drm_file
*file_priv
,
243 unsigned int cmd
, unsigned long arg
)
245 struct drm_device
*dev
= file_priv
->head
->dev
;
246 struct drm_client __user
*argp
= (struct drm_client __user
*)arg
;
247 struct drm_client client
;
252 if (copy_from_user(&client
, argp
, sizeof(client
)))
255 mutex_lock(&dev
->struct_mutex
);
257 if (list_empty(&dev
->filelist
)) {
258 mutex_unlock(&dev
->struct_mutex
);
263 list_for_each_entry(pt
, &dev
->filelist
, lhead
) {
268 client
.auth
= pt
->authenticated
;
269 client
.pid
= pt
->pid
;
270 client
.uid
= pt
->uid
;
271 client
.magic
= pt
->magic
;
272 client
.iocs
= pt
->ioctl_count
;
273 mutex_unlock(&dev
->struct_mutex
);
275 if (copy_to_user(argp
, &client
, sizeof(client
)))
281 * Get statistics information.
283 * \param inode device inode.
284 * \param file_priv DRM file private.
285 * \param cmd command.
286 * \param arg user argument, pointing to a drm_stats structure.
288 * \return zero on success or a negative number on failure.
290 int drm_getstats(struct inode
*inode
, struct drm_file
*file_priv
,
291 unsigned int cmd
, unsigned long arg
)
293 struct drm_device
*dev
= file_priv
->head
->dev
;
294 struct drm_stats stats
;
297 memset(&stats
, 0, sizeof(stats
));
299 mutex_lock(&dev
->struct_mutex
);
301 for (i
= 0; i
< dev
->counters
; i
++) {
302 if (dev
->types
[i
] == _DRM_STAT_LOCK
)
304 = (dev
->lock
.hw_lock
? dev
->lock
.hw_lock
->lock
: 0);
306 stats
.data
[i
].value
= atomic_read(&dev
->counts
[i
]);
307 stats
.data
[i
].type
= dev
->types
[i
];
310 stats
.count
= dev
->counters
;
312 mutex_unlock(&dev
->struct_mutex
);
314 if (copy_to_user((struct drm_stats __user
*) arg
, &stats
, sizeof(stats
)))
322 * \param inode device inode.
323 * \param file_priv DRM file private.
324 * \param cmd command.
325 * \param arg user argument, pointing to a drm_lock structure.
326 * \return zero on success or negative number on failure.
328 * Sets the requested interface version
330 int drm_setversion(DRM_IOCTL_ARGS
)
333 struct drm_set_version sv
;
334 struct drm_set_version retv
;
336 struct drm_set_version __user
*argp
= (void __user
*)data
;
339 if (copy_from_user(&sv
, argp
, sizeof(sv
)))
342 retv
.drm_di_major
= DRM_IF_MAJOR
;
343 retv
.drm_di_minor
= DRM_IF_MINOR
;
344 retv
.drm_dd_major
= dev
->driver
->major
;
345 retv
.drm_dd_minor
= dev
->driver
->minor
;
347 if (copy_to_user(argp
, &retv
, sizeof(retv
)))
350 if (sv
.drm_di_major
!= -1) {
351 if (sv
.drm_di_major
!= DRM_IF_MAJOR
||
352 sv
.drm_di_minor
< 0 || sv
.drm_di_minor
> DRM_IF_MINOR
)
354 if_version
= DRM_IF_VERSION(sv
.drm_di_major
, sv
.drm_di_minor
);
355 dev
->if_version
= max(if_version
, dev
->if_version
);
356 if (sv
.drm_di_minor
>= 1) {
358 * Version 1.1 includes tying of DRM to specific device
360 ret
= drm_set_busid(dev
);
366 if (sv
.drm_dd_major
!= -1) {
367 if (sv
.drm_dd_major
!= dev
->driver
->major
||
369 || sv
.drm_dd_minor
> dev
->driver
->minor
)
372 if (dev
->driver
->set_version
)
373 dev
->driver
->set_version(dev
, &sv
);
379 int drm_noop(struct inode
*inode
, struct drm_file
*file_priv
, unsigned int cmd
,