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