xref: /freebsd/sys/dev/drm2/drm_ioctl.c (revision a10cee30c94cf5944826d2a495e9cdf339dfbcc8)
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  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 /** @file drm_ioctl.c
35  * Varios minor DRM ioctls not applicable to other files, such as versioning
36  * information and reporting DRM information to userland.
37  */
38 
39 #include <dev/drm2/drmP.h>
40 
41 /*
42  * Beginning in revision 1.1 of the DRM interface, getunique will return
43  * a unique in the form pci:oooo:bb:dd.f (o=domain, b=bus, d=device, f=function)
44  * before setunique has been called.  The format for the bus-specific part of
45  * the unique is not defined for any other bus.
46  */
47 int drm_getunique(struct drm_device *dev, void *data,
48 		  struct drm_file *file_priv)
49 {
50 	struct drm_unique *u = data;
51 
52 	if (u->unique_len >= dev->unique_len) {
53 		if (DRM_COPY_TO_USER(u->unique, dev->unique, dev->unique_len))
54 			return EFAULT;
55 	}
56 	u->unique_len = dev->unique_len;
57 
58 	return 0;
59 }
60 
61 /* Deprecated in DRM version 1.1, and will return EBUSY when setversion has
62  * requested version 1.1 or greater.
63  */
64 int drm_setunique(struct drm_device *dev, void *data,
65 		  struct drm_file *file_priv)
66 {
67 	struct drm_unique *u = data;
68 	int domain, bus, slot, func, ret;
69 	char *busid;
70 
71 	/* Check and copy in the submitted Bus ID */
72 	if (!u->unique_len || u->unique_len > 1024)
73 		return EINVAL;
74 
75 	busid = malloc(u->unique_len + 1, DRM_MEM_DRIVER, M_WAITOK);
76 	if (busid == NULL)
77 		return ENOMEM;
78 
79 	if (DRM_COPY_FROM_USER(busid, u->unique, u->unique_len)) {
80 		free(busid, DRM_MEM_DRIVER);
81 		return EFAULT;
82 	}
83 	busid[u->unique_len] = '\0';
84 
85 	/* Return error if the busid submitted doesn't match the device's actual
86 	 * busid.
87 	 */
88 	ret = sscanf(busid, "PCI:%d:%d:%d", &bus, &slot, &func);
89 	if (ret != 3) {
90 		free(busid, DRM_MEM_DRIVER);
91 		return EINVAL;
92 	}
93 	domain = bus >> 8;
94 	bus &= 0xff;
95 
96 	if ((domain != dev->pci_domain) ||
97 	    (bus != dev->pci_bus) ||
98 	    (slot != dev->pci_slot) ||
99 	    (func != dev->pci_func)) {
100 		free(busid, DRM_MEM_DRIVER);
101 		return EINVAL;
102 	}
103 
104 	/* Actually set the device's busid now. */
105 	DRM_LOCK(dev);
106 	if (dev->unique_len || dev->unique) {
107 		DRM_UNLOCK(dev);
108 		return EBUSY;
109 	}
110 
111 	dev->unique_len = u->unique_len;
112 	dev->unique = busid;
113 	DRM_UNLOCK(dev);
114 
115 	return 0;
116 }
117 
118 
119 static int
120 drm_set_busid(struct drm_device *dev)
121 {
122 
123 	DRM_LOCK(dev);
124 
125 	if (dev->unique != NULL) {
126 		DRM_UNLOCK(dev);
127 		return EBUSY;
128 	}
129 
130 	dev->unique_len = 20;
131 	dev->unique = malloc(dev->unique_len + 1, DRM_MEM_DRIVER, M_NOWAIT);
132 	if (dev->unique == NULL) {
133 		DRM_UNLOCK(dev);
134 		return ENOMEM;
135 	}
136 
137 	snprintf(dev->unique, dev->unique_len, "pci:%04x:%02x:%02x.%1x",
138 	    dev->pci_domain, dev->pci_bus, dev->pci_slot, dev->pci_func);
139 
140 	DRM_UNLOCK(dev);
141 
142 	return 0;
143 }
144 
145 int drm_getmap(struct drm_device *dev, void *data, struct drm_file *file_priv)
146 {
147 	struct drm_map     *map = data;
148 	drm_local_map_t    *mapinlist;
149 	int          idx;
150 	int	     i = 0;
151 
152 	idx = map->offset;
153 
154 	DRM_LOCK(dev);
155 	if (idx < 0) {
156 		DRM_UNLOCK(dev);
157 		return EINVAL;
158 	}
159 
160 	TAILQ_FOREACH(mapinlist, &dev->maplist, link) {
161 		if (i == idx) {
162 			map->offset = mapinlist->offset;
163 			map->size   = mapinlist->size;
164 			map->type   = mapinlist->type;
165 			map->flags  = mapinlist->flags;
166 			map->handle = mapinlist->handle;
167 			map->mtrr   = mapinlist->mtrr;
168 			break;
169 		}
170 		i++;
171 	}
172 
173 	DRM_UNLOCK(dev);
174 
175  	if (mapinlist == NULL)
176 		return EINVAL;
177 
178 	return 0;
179 }
180 
181 int drm_getclient(struct drm_device *dev, void *data,
182 		  struct drm_file *file_priv)
183 {
184 	struct drm_client *client = data;
185 	struct drm_file *pt;
186 	int idx;
187 	int i = 0;
188 
189 	idx = client->idx;
190 	DRM_LOCK(dev);
191 	TAILQ_FOREACH(pt, &dev->files, link) {
192 		if (i == idx) {
193 			client->auth  = pt->authenticated;
194 			client->pid   = pt->pid;
195 			client->uid   = pt->uid;
196 			client->magic = pt->magic;
197 			client->iocs  = pt->ioctl_count;
198 			DRM_UNLOCK(dev);
199 			return 0;
200 		}
201 		i++;
202 	}
203 	DRM_UNLOCK(dev);
204 
205 	return EINVAL;
206 }
207 
208 int drm_getstats(struct drm_device *dev, void *data, struct drm_file *file_priv)
209 {
210 	struct drm_stats *stats = data;
211 	int          i;
212 
213 	memset(stats, 0, sizeof(struct drm_stats));
214 
215 	DRM_LOCK(dev);
216 
217 	for (i = 0; i < dev->counters; i++) {
218 		if (dev->types[i] == _DRM_STAT_LOCK)
219 			stats->data[i].value =
220 			    (dev->lock.hw_lock ? dev->lock.hw_lock->lock : 0);
221 		else
222 			stats->data[i].value = atomic_read(&dev->counts[i]);
223 		stats->data[i].type = dev->types[i];
224 	}
225 
226 	stats->count = dev->counters;
227 
228 	DRM_UNLOCK(dev);
229 
230 	return 0;
231 }
232 
233 int drm_getcap(struct drm_device *dev, void *data, struct drm_file *file_priv)
234 {
235 	struct drm_get_cap *req = data;
236 
237 	req->value = 0;
238 	switch (req->capability) {
239 	case DRM_CAP_DUMB_BUFFER:
240 		if (dev->driver->dumb_create)
241 			req->value = 1;
242 		break;
243 	case DRM_CAP_VBLANK_HIGH_CRTC:
244 		req->value = 1;
245 		break;
246 	case DRM_CAP_DUMB_PREFERRED_DEPTH:
247 		req->value = dev->mode_config.preferred_depth;
248 		break;
249 	case DRM_CAP_DUMB_PREFER_SHADOW:
250 		req->value = dev->mode_config.prefer_shadow;
251 		break;
252 	default:
253 		return EINVAL;
254 	}
255 	return 0;
256 }
257 
258 
259 #define DRM_IF_MAJOR	1
260 #define DRM_IF_MINOR	2
261 
262 int drm_setversion(struct drm_device *dev, void *data,
263 		   struct drm_file *file_priv)
264 {
265 	struct drm_set_version *sv = data;
266 	struct drm_set_version ver;
267 	int if_version;
268 
269 	/* Save the incoming data, and set the response before continuing
270 	 * any further.
271 	 */
272 	ver = *sv;
273 	sv->drm_di_major = DRM_IF_MAJOR;
274 	sv->drm_di_minor = DRM_IF_MINOR;
275 	sv->drm_dd_major = dev->driver->major;
276 	sv->drm_dd_minor = dev->driver->minor;
277 
278 	DRM_DEBUG("ver.drm_di_major %d ver.drm_di_minor %d "
279 	    "ver.drm_dd_major %d ver.drm_dd_minor %d\n",
280 	    ver.drm_di_major, ver.drm_di_minor, ver.drm_dd_major,
281 	    ver.drm_dd_minor);
282 	DRM_DEBUG("sv->drm_di_major %d sv->drm_di_minor %d "
283 	    "sv->drm_dd_major %d sv->drm_dd_minor %d\n",
284 	    sv->drm_di_major, sv->drm_di_minor, sv->drm_dd_major,
285 	    sv->drm_dd_minor);
286 
287 	if (ver.drm_di_major != -1) {
288 		if (ver.drm_di_major != DRM_IF_MAJOR ||
289 		    ver.drm_di_minor < 0 || ver.drm_di_minor > DRM_IF_MINOR) {
290 			return EINVAL;
291 		}
292 		if_version = DRM_IF_VERSION(ver.drm_di_major,
293 		    ver.drm_dd_minor);
294 		dev->if_version = DRM_MAX(if_version, dev->if_version);
295 		if (ver.drm_di_minor >= 1) {
296 			/*
297 			 * Version 1.1 includes tying of DRM to specific device
298 			 */
299 			drm_set_busid(dev);
300 		}
301 	}
302 
303 	if (ver.drm_dd_major != -1) {
304 		if (ver.drm_dd_major != dev->driver->major ||
305 		    ver.drm_dd_minor < 0 ||
306 		    ver.drm_dd_minor > dev->driver->minor)
307 		{
308 			return EINVAL;
309 		}
310 	}
311 
312 	return 0;
313 }
314 
315 
316 int drm_noop(struct drm_device *dev, void *data, struct drm_file *file_priv)
317 {
318 	DRM_DEBUG("\n");
319 	return 0;
320 }
321