xref: /dragonfly/sys/dev/drm/drm_sysctl.c (revision bc3d4063)
1 /*-
2  * Copyright 2003 Eric Anholt
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * ERIC ANHOLT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * $DragonFly: src/sys/dev/drm/drm_sysctl.c,v 1.1 2008/04/05 18:12:29 hasso Exp $
24  */
25 
26 /** @file drm_sysctl.c
27  * Implementation of various sysctls for controlling DRM behavior and reporting
28  * debug information.
29  */
30 
31 #include "drmP.h"
32 #include "drm.h"
33 
34 #include <sys/sysctl.h>
35 
36 static int	   drm_name_info DRM_SYSCTL_HANDLER_ARGS;
37 static int	   drm_vm_info DRM_SYSCTL_HANDLER_ARGS;
38 static int	   drm_clients_info DRM_SYSCTL_HANDLER_ARGS;
39 static int	   drm_bufs_info DRM_SYSCTL_HANDLER_ARGS;
40 
41 struct drm_sysctl_list {
42 	const char *name;
43 	int	   (*f) DRM_SYSCTL_HANDLER_ARGS;
44 } drm_sysctl_list[] = {
45 	{"name",    drm_name_info},
46 	{"vm",	    drm_vm_info},
47 	{"clients", drm_clients_info},
48 	{"bufs",    drm_bufs_info},
49 };
50 #define DRM_SYSCTL_ENTRIES (sizeof(drm_sysctl_list)/sizeof(drm_sysctl_list[0]))
51 
52 struct drm_sysctl_info {
53 	struct sysctl_ctx_list ctx;
54 	char		       name[2];
55 };
56 
57 int drm_sysctl_init(drm_device_t *dev)
58 {
59 	struct drm_sysctl_info *info;
60 	struct sysctl_oid *oid;
61 	struct sysctl_oid *top, *drioid;
62 	int		  i;
63 
64 	info = malloc(sizeof *info, M_DRM, M_WAITOK | M_ZERO);
65 	if ( !info )
66 		return 1;
67 	dev->sysctl = info;
68 
69 	/* Add the sysctl node for DRI if it doesn't already exist */
70 	drioid = SYSCTL_ADD_NODE( &info->ctx, &sysctl__hw_children, OID_AUTO, "dri", CTLFLAG_RW, NULL, "DRI Graphics");
71 	if (!drioid)
72 		return 1;
73 
74 	/* Find the next free slot under hw.dri */
75 	i = 0;
76 	SLIST_FOREACH(oid, SYSCTL_CHILDREN(drioid), oid_link) {
77 		if (i <= oid->oid_arg2)
78 			i = oid->oid_arg2 + 1;
79 	}
80 	if (i>9)
81 		return 1;
82 
83 	/* Add the hw.dri.x for our device */
84 	info->name[0] = '0' + i;
85 	info->name[1] = 0;
86 	top = SYSCTL_ADD_NODE( &info->ctx, SYSCTL_CHILDREN(drioid), OID_AUTO, info->name, CTLFLAG_RW, NULL, NULL);
87 	if (!top)
88 		return 1;
89 
90 	for (i = 0; i < DRM_SYSCTL_ENTRIES; i++) {
91 		oid = SYSCTL_ADD_OID(&info->ctx,
92 			SYSCTL_CHILDREN(top),
93 			OID_AUTO,
94 			drm_sysctl_list[i].name,
95 			CTLTYPE_INT | CTLFLAG_RD,
96 			dev,
97 			0,
98 			drm_sysctl_list[i].f,
99 			"A",
100 			NULL);
101 		if (!oid)
102 			return 1;
103 	}
104 	SYSCTL_ADD_INT(&info->ctx, SYSCTL_CHILDREN(top), OID_AUTO, "debug",
105 	    CTLFLAG_RW, &drm_debug_flag, sizeof(drm_debug_flag),
106 	    "Enable debugging output");
107 
108 	return 0;
109 }
110 
111 int drm_sysctl_cleanup(drm_device_t *dev)
112 {
113 	int error;
114 	error = sysctl_ctx_free( &dev->sysctl->ctx );
115 
116 	free(dev->sysctl, M_DRM);
117 	dev->sysctl = NULL;
118 
119 	return error;
120 }
121 
122 #define DRM_SYSCTL_PRINT(fmt, arg...)				\
123 do {								\
124 	snprintf(buf, sizeof(buf), fmt, ##arg);			\
125 	retcode = SYSCTL_OUT(req, buf, strlen(buf));		\
126 	if (retcode)						\
127 		goto done;					\
128 } while (0)
129 
130 static int drm_name_info DRM_SYSCTL_HANDLER_ARGS
131 {
132 	drm_device_t *dev = arg1;
133 	char buf[128];
134 	int retcode;
135 	int hasunique = 0;
136 
137 	DRM_SYSCTL_PRINT("%s 0x%x", dev->driver.name, dev2udev(dev->devnode));
138 
139 	DRM_LOCK();
140 	if (dev->unique) {
141 		snprintf(buf, sizeof(buf), " %s", dev->unique);
142 		hasunique = 1;
143 	}
144 	DRM_UNLOCK();
145 
146 	if (hasunique)
147 		SYSCTL_OUT(req, buf, strlen(buf));
148 
149 	SYSCTL_OUT(req, "", 1);
150 
151 done:
152 	return retcode;
153 }
154 
155 static int drm_vm_info DRM_SYSCTL_HANDLER_ARGS
156 {
157 	drm_device_t *dev = arg1;
158 	drm_local_map_t *map, *tempmaps;
159 	const char   *types[] = { "FB", "REG", "SHM", "AGP", "SG" };
160 	const char *type, *yesno;
161 	int i, mapcount;
162 	char buf[128];
163 	int retcode;
164 
165 	/* We can't hold the lock while doing SYSCTL_OUTs, so allocate a
166 	 * temporary copy of all the map entries and then SYSCTL_OUT that.
167 	 */
168 	DRM_LOCK();
169 
170 	mapcount = 0;
171 	TAILQ_FOREACH(map, &dev->maplist, link)
172 		mapcount++;
173 
174 	tempmaps = malloc(sizeof(drm_local_map_t) * mapcount, M_DRM, M_NOWAIT);
175 	if (tempmaps == NULL) {
176 		DRM_UNLOCK();
177 		return ENOMEM;
178 	}
179 
180 	i = 0;
181 	TAILQ_FOREACH(map, &dev->maplist, link)
182 		tempmaps[i++] = *map;
183 
184 	DRM_UNLOCK();
185 
186 	DRM_SYSCTL_PRINT("\nslot	 offset	      size type flags	 "
187 			 "address mtrr\n");
188 
189 	for (i = 0; i < mapcount; i++) {
190 		map = &tempmaps[i];
191 
192 		if (map->type < 0 || map->type > 4)
193 			type = "??";
194 		else
195 			type = types[map->type];
196 
197 		if (!map->mtrr)
198 			yesno = "no";
199 		else
200 			yesno = "yes";
201 
202 		DRM_SYSCTL_PRINT(
203 		    "%4d 0x%08lx 0x%08lx %4.4s  0x%02x 0x%08lx %s\n", i,
204 		    map->offset, map->size, type, map->flags,
205 		    (unsigned long)map->handle, yesno);
206 	}
207 	SYSCTL_OUT(req, "", 1);
208 
209 done:
210 	free(tempmaps, M_DRM);
211 	return retcode;
212 }
213 
214 static int drm_bufs_info DRM_SYSCTL_HANDLER_ARGS
215 {
216 	drm_device_t	 *dev = arg1;
217 	drm_device_dma_t *dma = dev->dma;
218 	drm_device_dma_t tempdma;
219 	int *templists;
220 	int i;
221 	char buf[128];
222 	int retcode;
223 
224 	/* We can't hold the locks around DRM_SYSCTL_PRINT, so make a temporary
225 	 * copy of the whole structure and the relevant data from buflist.
226 	 */
227 	DRM_LOCK();
228 	if (dma == NULL) {
229 		DRM_UNLOCK();
230 		return 0;
231 	}
232 	DRM_SPINLOCK(&dev->dma_lock);
233 	tempdma = *dma;
234 	templists = malloc(sizeof(int) * dma->buf_count, M_DRM, M_NOWAIT);
235 	for (i = 0; i < dma->buf_count; i++)
236 		templists[i] = dma->buflist[i]->list;
237 	dma = &tempdma;
238 	DRM_SPINUNLOCK(&dev->dma_lock);
239 	DRM_UNLOCK();
240 
241 	DRM_SYSCTL_PRINT("\n o     size count  free	 segs pages    kB\n");
242 	for (i = 0; i <= DRM_MAX_ORDER; i++) {
243 		if (dma->bufs[i].buf_count)
244 			DRM_SYSCTL_PRINT("%2d %8d %5d %5d %5d %5d %5d\n",
245 				       i,
246 				       dma->bufs[i].buf_size,
247 				       dma->bufs[i].buf_count,
248 				       atomic_read(&dma->bufs[i]
249 						   .freelist.count),
250 				       dma->bufs[i].seg_count,
251 				       dma->bufs[i].seg_count
252 				       *(1 << dma->bufs[i].page_order),
253 				       (dma->bufs[i].seg_count
254 					* (1 << dma->bufs[i].page_order))
255 				       * PAGE_SIZE / 1024);
256 	}
257 	DRM_SYSCTL_PRINT("\n");
258 	for (i = 0; i < dma->buf_count; i++) {
259 		if (i && !(i%32)) DRM_SYSCTL_PRINT("\n");
260 		DRM_SYSCTL_PRINT(" %d", templists[i]);
261 	}
262 	DRM_SYSCTL_PRINT("\n");
263 
264 	SYSCTL_OUT(req, "", 1);
265 done:
266 	free(templists, M_DRM);
267 	return retcode;
268 }
269 
270 static int drm_clients_info DRM_SYSCTL_HANDLER_ARGS
271 {
272 	drm_device_t *dev = arg1;
273 	drm_file_t *priv, *tempprivs;
274 	char buf[128];
275 	int retcode;
276 	int privcount, i;
277 
278 	DRM_LOCK();
279 
280 	privcount = 0;
281 	TAILQ_FOREACH(priv, &dev->files, link)
282 		privcount++;
283 
284 	tempprivs = malloc(sizeof(drm_file_t) * privcount, M_DRM, M_NOWAIT);
285 	if (tempprivs == NULL) {
286 		DRM_UNLOCK();
287 		return ENOMEM;
288 	}
289 	i = 0;
290 	TAILQ_FOREACH(priv, &dev->files, link)
291 		tempprivs[i++] = *priv;
292 
293 	DRM_UNLOCK();
294 
295 	DRM_SYSCTL_PRINT("\na dev	pid    uid	magic	  ioctls\n");
296 	for (i = 0; i < privcount; i++) {
297 		priv = &tempprivs[i];
298 		DRM_SYSCTL_PRINT("%c %3d %5d %5d %10u %10lu\n",
299 			       priv->authenticated ? 'y' : 'n',
300 			       priv->minor,
301 			       priv->pid,
302 			       priv->uid,
303 			       priv->magic,
304 			       priv->ioctl_count);
305 	}
306 
307 	SYSCTL_OUT(req, "", 1);
308 done:
309 	free(tempprivs, M_DRM);
310 	return retcode;
311 }
312