xref: /openbsd-src/sys/dev/pci/drm/drm_ioctl.c (revision 850e275390052b330d93020bf619a739a3c277ac)
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 /** @file drm_ioctl.c
32  * Varios minor DRM ioctls not applicable to other files, such as versioning
33  * information and reporting DRM information to userland.
34  */
35 
36 #include "drmP.h"
37 
38 int	drm_set_busid(struct drm_device *);
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
47 drm_getunique(struct drm_device *dev, void *data, 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
64 drm_setunique(struct drm_device *dev, void *data, struct drm_file *file_priv)
65 {
66 	struct drm_unique	*u = data;
67 	char			*busid;
68 	int			 domain, bus, slot, func, ret;
69 #if defined (__NetBSD__)
70 	return EOPNOTSUPP;
71 #endif
72 
73 	/* Check and copy in the submitted Bus ID */
74 	if (!u->unique_len || u->unique_len > 1024)
75 		return EINVAL;
76 
77 	busid = drm_alloc(u->unique_len + 1, DRM_MEM_DRIVER);
78 	if (busid == NULL)
79 		return ENOMEM;
80 
81 	if (DRM_COPY_FROM_USER(busid, u->unique, u->unique_len)) {
82 		drm_free(busid, u->unique_len + 1, DRM_MEM_DRIVER);
83 		return EFAULT;
84 	}
85 	busid[u->unique_len] = '\0';
86 
87 	/* Return error if the busid submitted doesn't match the device's actual
88 	 * busid.
89 	 */
90 #ifdef __FreeBSD__
91 	ret = sscanf(busid, "PCI:%d:%d:%d", &bus, &slot, &func);
92 #endif /* Net and Openbsd don't have sscanf in the kernel this is deprecated anyway. */
93 
94 	if (ret != 3) {
95 		drm_free(busid, u->unique_len + 1, DRM_MEM_DRIVER);
96 		return EINVAL;
97 	}
98 	domain = bus >> 8;
99 	bus &= 0xff;
100 
101 	if ((domain != dev->pci_domain) || (bus != dev->pci_bus) ||
102 	    (slot != dev->pci_slot) || (func != dev->pci_func)) {
103 		drm_free(busid, u->unique_len + 1, DRM_MEM_DRIVER);
104 		return EINVAL;
105 	}
106 
107 	/* Actually set the device's busid now. */
108 	DRM_LOCK();
109 	if (dev->unique_len || dev->unique) {
110 		DRM_UNLOCK();
111 		return EBUSY;
112 	}
113 
114 	dev->unique_len = u->unique_len;
115 	dev->unique = busid;
116 	DRM_UNLOCK();
117 
118 	return 0;
119 }
120 
121 
122 int
123 drm_set_busid(struct drm_device *dev)
124 {
125 
126 	DRM_LOCK();
127 
128 	if (dev->unique != NULL) {
129 		DRM_UNLOCK();
130 		return EBUSY;
131 	}
132 
133 	dev->unique_len = 20;
134 	dev->unique = drm_alloc(dev->unique_len + 1, DRM_MEM_DRIVER);
135 	if (dev->unique == NULL) {
136 		DRM_UNLOCK();
137 		return ENOMEM;
138 	}
139 
140 	snprintf(dev->unique, dev->unique_len, "pci:%04x:%02x:%02x.%1x",
141 	    dev->pci_domain, dev->pci_bus, dev->pci_slot, dev->pci_func);
142 
143 	DRM_UNLOCK();
144 
145 	return 0;
146 }
147 
148 int
149 drm_getmap(struct drm_device *dev, void *data, struct drm_file *file_priv)
150 {
151 	struct drm_map	*map = data;
152 	drm_local_map_t	*mapinlist;
153 	int		 idx, i = 0;
154 
155 	idx = map->offset;
156 
157 	DRM_LOCK();
158 	if (idx < 0) {
159 		DRM_UNLOCK();
160 		return EINVAL;
161 	}
162 
163 	TAILQ_FOREACH(mapinlist, &dev->maplist, link) {
164 		if (i == idx) {
165 			map->offset = mapinlist->offset;
166 			map->size = mapinlist->size;
167 			map->type = mapinlist->type;
168 			map->flags = mapinlist->flags;
169 			map->handle = mapinlist->handle;
170 			map->mtrr = mapinlist->mtrr;
171 			break;
172 		}
173 		i++;
174 	}
175 
176 	DRM_UNLOCK();
177 
178  	if (mapinlist == NULL)
179 		return EINVAL;
180 
181 	return 0;
182 }
183 
184 int
185 drm_getclient(struct drm_device *dev, void *data, struct drm_file *file_priv)
186 {
187 	return (EINVAL);
188 }
189 
190 int
191 drm_getstats(struct drm_device *dev, void *data, struct drm_file *file_priv)
192 {
193 	return (EINVAL);
194 }
195 
196 #define DRM_IF_MAJOR	1
197 #define DRM_IF_MINOR	2
198 
199 int
200 drm_setversion(struct drm_device *dev, void *data, struct drm_file *file_priv)
201 {
202 	struct drm_set_version	ver, *sv = data;
203 	int			if_version;
204 
205 	/* Save the incoming data, and set the response before continuing
206 	 * any further.
207 	 */
208 	ver = *sv;
209 	sv->drm_di_major = DRM_IF_MAJOR;
210 	sv->drm_di_minor = DRM_IF_MINOR;
211 	sv->drm_dd_major = dev->driver.major;
212 	sv->drm_dd_minor = dev->driver.minor;
213 
214 	if (ver.drm_di_major != -1) {
215 		if (ver.drm_di_major != DRM_IF_MAJOR || ver.drm_di_minor < 0 ||
216 		    ver.drm_di_minor > DRM_IF_MINOR) {
217 			return EINVAL;
218 		}
219 		if_version = DRM_IF_VERSION(ver.drm_di_major, ver.drm_dd_minor);
220 		dev->if_version = DRM_MAX(if_version, dev->if_version);
221 		if (ver.drm_di_minor >= 1) {
222 			/*
223 			 * Version 1.1 includes tying of DRM to specific device
224 			 */
225 			drm_set_busid(dev);
226 		}
227 	}
228 
229 	if (ver.drm_dd_major != -1) {
230 		if (ver.drm_dd_major != dev->driver.major ||
231 		    ver.drm_dd_minor < 0 ||
232 		    ver.drm_dd_minor > dev->driver.minor)
233 		{
234 			return EINVAL;
235 		}
236 	}
237 
238 	return 0;
239 }
240 
241 
242 int
243 drm_noop(struct drm_device *dev, void *data, struct drm_file *file_priv)
244 {
245 	DRM_DEBUG("\n");
246 	return 0;
247 }
248