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