drm_ioctl.c revision 152909
1/* drm_ioctl.h -- IOCTL processing for DRM -*- linux-c -*-
2 * Created: Fri Jan  8 09:01:26 1999 by faith@valinux.com
3 */
4/*-
5 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
6 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
7 * All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the next
17 * paragraph) shall be included in all copies or substantial portions of the
18 * Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 * OTHER DEALINGS IN THE SOFTWARE.
27 *
28 * Authors:
29 *    Rickard E. (Rik) Faith <faith@valinux.com>
30 *    Gareth Hughes <gareth@valinux.com>
31 *
32 */
33
34#include <sys/cdefs.h>
35__FBSDID("$FreeBSD: head/sys/dev/drm/drm_ioctl.c 152909 2005-11-28 23:13:57Z anholt $");
36
37#include "dev/drm/drmP.h"
38
39/*
40 * Beginning in revision 1.1 of the DRM interface, getunique will return
41 * a unique in the form pci:oooo:bb:dd.f (o=domain, b=bus, d=device, f=function)
42 * before setunique has been called.  The format for the bus-specific part of
43 * the unique is not defined for any other bus.
44 */
45int drm_getunique(DRM_IOCTL_ARGS)
46{
47	DRM_DEVICE;
48	drm_unique_t	 u;
49
50	DRM_COPY_FROM_USER_IOCTL( u, (drm_unique_t *)data, sizeof(u) );
51
52	if (u.unique_len >= dev->unique_len) {
53		if (DRM_COPY_TO_USER(u.unique, dev->unique, dev->unique_len))
54			return DRM_ERR(EFAULT);
55	}
56	u.unique_len = dev->unique_len;
57
58	DRM_COPY_TO_USER_IOCTL( (drm_unique_t *)data, u, sizeof(u) );
59
60	return 0;
61}
62
63/* Deprecated in DRM version 1.1, and will return EBUSY when setversion has
64 * requested version 1.1 or greater.
65 */
66int drm_setunique(DRM_IOCTL_ARGS)
67{
68	DRM_DEVICE;
69	drm_unique_t u;
70	int domain, bus, slot, func, ret;
71	char *busid;
72
73	DRM_COPY_FROM_USER_IOCTL( u, (drm_unique_t *)data, sizeof(u) );
74
75	/* Check and copy in the submitted Bus ID */
76	if (!u.unique_len || u.unique_len > 1024)
77		return DRM_ERR(EINVAL);
78
79	busid = malloc(u.unique_len + 1, M_DRM, M_WAITOK);
80	if (busid == NULL)
81		return DRM_ERR(ENOMEM);
82
83	if (DRM_COPY_FROM_USER(busid, u.unique, u.unique_len)) {
84		free(busid, M_DRM);
85		return DRM_ERR(EFAULT);
86	}
87	busid[u.unique_len] = '\0';
88
89	/* Return error if the busid submitted doesn't match the device's actual
90	 * busid.
91	 */
92	ret = sscanf(busid, "PCI:%d:%d:%d", &bus, &slot, &func);
93	if (ret != 3) {
94		free(busid, M_DRM);
95		return DRM_ERR(EINVAL);
96	}
97	domain = bus >> 8;
98	bus &= 0xff;
99
100	if ((domain != dev->pci_domain) ||
101	    (bus != dev->pci_bus) ||
102	    (slot != dev->pci_slot) ||
103	    (func != dev->pci_func)) {
104		free(busid, M_DRM);
105		return DRM_ERR(EINVAL);
106	}
107
108	/* Actually set the device's busid now. */
109	DRM_LOCK();
110	if (dev->unique_len || dev->unique) {
111		DRM_UNLOCK();
112		return DRM_ERR(EBUSY);
113	}
114
115	dev->unique_len = u.unique_len;
116	dev->unique = busid;
117	DRM_UNLOCK();
118
119	return 0;
120}
121
122
123static int
124drm_set_busid(drm_device_t *dev)
125{
126
127	DRM_LOCK();
128
129	if (dev->unique != NULL) {
130		DRM_UNLOCK();
131		return EBUSY;
132	}
133
134	dev->unique_len = 20;
135	dev->unique = malloc(dev->unique_len + 1, M_DRM, M_NOWAIT);
136	if (dev->unique == NULL) {
137		DRM_UNLOCK();
138		return ENOMEM;
139	}
140
141	snprintf(dev->unique, dev->unique_len, "pci:%04x:%02x:%02x.%1x",
142	    dev->pci_domain, dev->pci_bus, dev->pci_slot, dev->pci_func);
143
144	DRM_UNLOCK();
145
146	return 0;
147}
148
149int drm_getmap(DRM_IOCTL_ARGS)
150{
151	DRM_DEVICE;
152	drm_map_t    map;
153	drm_local_map_t    *mapinlist;
154	int          idx;
155	int	     i = 0;
156
157	DRM_COPY_FROM_USER_IOCTL( map, (drm_map_t *)data, sizeof(map) );
158
159	idx = map.offset;
160
161	DRM_LOCK();
162	if (idx < 0) {
163		DRM_UNLOCK();
164		return DRM_ERR(EINVAL);
165	}
166
167	TAILQ_FOREACH(mapinlist, &dev->maplist, link) {
168		if (i==idx) {
169			map.offset = mapinlist->offset;
170			map.size   = mapinlist->size;
171			map.type   = mapinlist->type;
172			map.flags  = mapinlist->flags;
173			map.handle = mapinlist->handle;
174			map.mtrr   = mapinlist->mtrr;
175			break;
176		}
177		i++;
178	}
179
180	DRM_UNLOCK();
181
182 	if (mapinlist == NULL)
183		return EINVAL;
184
185	DRM_COPY_TO_USER_IOCTL( (drm_map_t *)data, map, sizeof(map) );
186
187	return 0;
188}
189
190int drm_getclient(DRM_IOCTL_ARGS)
191{
192	DRM_DEVICE;
193	drm_client_t client;
194	drm_file_t   *pt;
195	int          idx;
196	int          i = 0;
197
198	DRM_COPY_FROM_USER_IOCTL( client, (drm_client_t *)data, sizeof(client) );
199
200	idx = client.idx;
201	DRM_LOCK();
202	TAILQ_FOREACH(pt, &dev->files, link) {
203		if (i==idx)
204		{
205			client.auth  = pt->authenticated;
206			client.pid   = pt->pid;
207			client.uid   = pt->uid;
208			client.magic = pt->magic;
209			client.iocs  = pt->ioctl_count;
210			DRM_UNLOCK();
211
212			*(drm_client_t *)data = client;
213			return 0;
214		}
215		i++;
216	}
217	DRM_UNLOCK();
218
219	DRM_COPY_TO_USER_IOCTL( (drm_client_t *)data, client, sizeof(client) );
220
221	return 0;
222}
223
224int drm_getstats(DRM_IOCTL_ARGS)
225{
226	DRM_DEVICE;
227	drm_stats_t  stats;
228	int          i;
229
230	memset(&stats, 0, sizeof(stats));
231
232	DRM_LOCK();
233
234	for (i = 0; i < dev->counters; i++) {
235		if (dev->types[i] == _DRM_STAT_LOCK)
236			stats.data[i].value
237				= (dev->lock.hw_lock
238				   ? dev->lock.hw_lock->lock : 0);
239		else
240			stats.data[i].value = atomic_read(&dev->counts[i]);
241		stats.data[i].type  = dev->types[i];
242	}
243
244	stats.count = dev->counters;
245
246	DRM_UNLOCK();
247
248	DRM_COPY_TO_USER_IOCTL( (drm_stats_t *)data, stats, sizeof(stats) );
249
250	return 0;
251}
252
253#define DRM_IF_MAJOR	1
254#define DRM_IF_MINOR	2
255
256int drm_setversion(DRM_IOCTL_ARGS)
257{
258	DRM_DEVICE;
259	drm_set_version_t sv;
260	drm_set_version_t retv;
261	int if_version;
262
263	DRM_COPY_FROM_USER_IOCTL(sv, (drm_set_version_t *)data, sizeof(sv));
264
265	retv.drm_di_major = DRM_IF_MAJOR;
266	retv.drm_di_minor = DRM_IF_MINOR;
267	retv.drm_dd_major = dev->driver.major;
268	retv.drm_dd_minor = dev->driver.minor;
269
270	DRM_COPY_TO_USER_IOCTL((drm_set_version_t *)data, retv, sizeof(sv));
271
272	if (sv.drm_di_major != -1) {
273		if (sv.drm_di_major != DRM_IF_MAJOR ||
274		    sv.drm_di_minor < 0 || sv.drm_di_minor > DRM_IF_MINOR)
275			return EINVAL;
276		if_version = DRM_IF_VERSION(sv.drm_di_major, sv.drm_dd_minor);
277		dev->if_version = DRM_MAX(if_version, dev->if_version);
278		if (sv.drm_di_minor >= 1) {
279			/*
280			 * Version 1.1 includes tying of DRM to specific device
281			 */
282			drm_set_busid(dev);
283		}
284	}
285
286	if (sv.drm_dd_major != -1) {
287		if (sv.drm_dd_major != dev->driver.major ||
288		    sv.drm_dd_minor < 0 || sv.drm_dd_minor > dev->driver.minor)
289			return EINVAL;
290	}
291	return 0;
292}
293
294
295int drm_noop(DRM_IOCTL_ARGS)
296{
297	DRM_DEBUG("\n");
298	return 0;
299}
300