xref: /freebsd/sys/dev/drm2/drm_ioctl.c (revision a9e8641da961bcf3d24afc85fd657f2083a872a2)
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 #include <dev/drm2/drm_core.h>
41 
42 /*
43  * Beginning in revision 1.1 of the DRM interface, getunique will return
44  * a unique in the form pci:oooo:bb:dd.f (o=domain, b=bus, d=device, f=function)
45  * before setunique has been called.  The format for the bus-specific part of
46  * the unique is not defined for any other bus.
47  */
48 int drm_getunique(struct drm_device *dev, void *data,
49 		  struct drm_file *file_priv)
50 {
51 	struct drm_unique *u = data;
52 
53 	if (u->unique_len >= dev->unique_len) {
54 		if (DRM_COPY_TO_USER(u->unique, dev->unique, dev->unique_len))
55 			return EFAULT;
56 	}
57 	u->unique_len = dev->unique_len;
58 
59 	return 0;
60 }
61 
62 /* Deprecated in DRM version 1.1, and will return EBUSY when setversion has
63  * requested version 1.1 or greater.
64  */
65 int drm_setunique(struct drm_device *dev, void *data,
66 		  struct drm_file *file_priv)
67 {
68 	struct drm_unique *u = data;
69 	int domain, bus, slot, func, ret;
70 	char *busid;
71 
72 	/* Check and copy in the submitted Bus ID */
73 	if (!u->unique_len || u->unique_len > 1024)
74 		return EINVAL;
75 
76 	busid = malloc(u->unique_len + 1, DRM_MEM_DRIVER, M_WAITOK);
77 	if (busid == NULL)
78 		return ENOMEM;
79 
80 	if (DRM_COPY_FROM_USER(busid, u->unique, u->unique_len)) {
81 		free(busid, DRM_MEM_DRIVER);
82 		return EFAULT;
83 	}
84 	busid[u->unique_len] = '\0';
85 
86 	/* Return error if the busid submitted doesn't match the device's actual
87 	 * busid.
88 	 */
89 	ret = sscanf(busid, "PCI:%d:%d:%d", &bus, &slot, &func);
90 	if (ret != 3) {
91 		free(busid, DRM_MEM_DRIVER);
92 		return EINVAL;
93 	}
94 	domain = bus >> 8;
95 	bus &= 0xff;
96 
97 	if ((domain != dev->pci_domain) ||
98 	    (bus != dev->pci_bus) ||
99 	    (slot != dev->pci_slot) ||
100 	    (func != dev->pci_func)) {
101 		free(busid, DRM_MEM_DRIVER);
102 		return EINVAL;
103 	}
104 
105 	/* Actually set the device's busid now. */
106 	DRM_LOCK(dev);
107 	if (dev->unique_len || dev->unique) {
108 		DRM_UNLOCK(dev);
109 		return EBUSY;
110 	}
111 
112 	dev->unique_len = u->unique_len;
113 	dev->unique = busid;
114 	DRM_UNLOCK(dev);
115 
116 	return 0;
117 }
118 
119 
120 static int
121 drm_set_busid(struct drm_device *dev)
122 {
123 
124 	DRM_LOCK(dev);
125 
126 	if (dev->unique != NULL) {
127 		DRM_UNLOCK(dev);
128 		return EBUSY;
129 	}
130 
131 	dev->unique_len = 20;
132 	dev->unique = malloc(dev->unique_len + 1, DRM_MEM_DRIVER, M_NOWAIT);
133 	if (dev->unique == NULL) {
134 		DRM_UNLOCK(dev);
135 		return ENOMEM;
136 	}
137 
138 	snprintf(dev->unique, dev->unique_len, "pci:%04x:%02x:%02x.%1x",
139 	    dev->pci_domain, dev->pci_bus, dev->pci_slot, dev->pci_func);
140 
141 	DRM_UNLOCK(dev);
142 
143 	return 0;
144 }
145 
146 int drm_getmap(struct drm_device *dev, void *data, struct drm_file *file_priv)
147 {
148 	struct drm_map     *map = data;
149 	drm_local_map_t    *mapinlist;
150 	int          idx;
151 	int	     i = 0;
152 
153 	idx = map->offset;
154 
155 	DRM_LOCK(dev);
156 	if (idx < 0) {
157 		DRM_UNLOCK(dev);
158 		return EINVAL;
159 	}
160 
161 	TAILQ_FOREACH(mapinlist, &dev->maplist, link) {
162 		if (i == idx) {
163 			map->offset = mapinlist->offset;
164 			map->size   = mapinlist->size;
165 			map->type   = mapinlist->type;
166 			map->flags  = mapinlist->flags;
167 			map->handle = mapinlist->handle;
168 			map->mtrr   = mapinlist->mtrr;
169 			break;
170 		}
171 		i++;
172 	}
173 
174 	DRM_UNLOCK(dev);
175 
176  	if (mapinlist == NULL)
177 		return EINVAL;
178 
179 	return 0;
180 }
181 
182 int drm_getclient(struct drm_device *dev, void *data,
183 		  struct drm_file *file_priv)
184 {
185 	struct drm_client *client = data;
186 	struct drm_file *pt;
187 	int idx;
188 	int i = 0;
189 
190 	idx = client->idx;
191 	DRM_LOCK(dev);
192 	TAILQ_FOREACH(pt, &dev->files, link) {
193 		if (i == idx) {
194 			client->auth  = pt->authenticated;
195 			client->pid   = pt->pid;
196 			client->uid   = pt->uid;
197 			client->magic = pt->magic;
198 			client->iocs  = pt->ioctl_count;
199 			DRM_UNLOCK(dev);
200 			return 0;
201 		}
202 		i++;
203 	}
204 	DRM_UNLOCK(dev);
205 
206 	return EINVAL;
207 }
208 
209 int drm_getstats(struct drm_device *dev, void *data, struct drm_file *file_priv)
210 {
211 	struct drm_stats *stats = data;
212 	int          i;
213 
214 	memset(stats, 0, sizeof(struct drm_stats));
215 
216 	DRM_LOCK(dev);
217 
218 	for (i = 0; i < dev->counters; i++) {
219 		if (dev->types[i] == _DRM_STAT_LOCK)
220 			stats->data[i].value =
221 			    (dev->lock.hw_lock ? dev->lock.hw_lock->lock : 0);
222 		else
223 			stats->data[i].value = atomic_read(&dev->counts[i]);
224 		stats->data[i].type = dev->types[i];
225 	}
226 
227 	stats->count = dev->counters;
228 
229 	DRM_UNLOCK(dev);
230 
231 	return 0;
232 }
233 
234 int drm_getcap(struct drm_device *dev, void *data, struct drm_file *file_priv)
235 {
236 	struct drm_get_cap *req = data;
237 
238 	req->value = 0;
239 	switch (req->capability) {
240 	case DRM_CAP_DUMB_BUFFER:
241 		if (dev->driver->dumb_create)
242 			req->value = 1;
243 		break;
244 	case DRM_CAP_VBLANK_HIGH_CRTC:
245 		req->value = 1;
246 		break;
247 	case DRM_CAP_DUMB_PREFERRED_DEPTH:
248 		req->value = dev->mode_config.preferred_depth;
249 		break;
250 	case DRM_CAP_DUMB_PREFER_SHADOW:
251 		req->value = dev->mode_config.prefer_shadow;
252 		break;
253 	case DRM_CAP_TIMESTAMP_MONOTONIC:
254 		req->value = drm_timestamp_monotonic;
255 		break;
256 	default:
257 		return EINVAL;
258 	}
259 	return 0;
260 }
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