xref: /dflybsd-src/sys/dev/drm/drm_sysctl.c (revision 450f08dbfd98cded95c51be4079ef10f5adb3241)
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  * $FreeBSD: src/sys/dev/drm2/drm_sysctl.c,v 1.1 2012/05/22 11:07:44 kib 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 <sys/conf.h>
32 #include <sys/sysctl.h>
33 #include <sys/types.h>
34 
35 #include "dev/drm/drmP.h"
36 #include "dev/drm/drm.h"
37 
38 static int	   drm_name_info DRM_SYSCTL_HANDLER_ARGS;
39 static int	   drm_vm_info DRM_SYSCTL_HANDLER_ARGS;
40 static int	   drm_clients_info DRM_SYSCTL_HANDLER_ARGS;
41 static int	   drm_bufs_info DRM_SYSCTL_HANDLER_ARGS;
42 static int	   drm_vblank_info DRM_SYSCTL_HANDLER_ARGS;
43 
44 struct drm_sysctl_list {
45 	const char *name;
46 	int	   (*f) DRM_SYSCTL_HANDLER_ARGS;
47 } drm_sysctl_list[] = {
48 	{"name",    drm_name_info},
49 	{"vm",	    drm_vm_info},
50 	{"clients", drm_clients_info},
51 	{"bufs",    drm_bufs_info},
52 	{"vblank",    drm_vblank_info},
53 };
54 #define DRM_SYSCTL_ENTRIES NELEM(drm_sysctl_list)
55 
56 struct drm_sysctl_info {
57 	struct sysctl_ctx_list ctx;
58 	char		       name[2];
59 };
60 
61 int drm_sysctl_init(struct drm_device *dev)
62 {
63 	struct drm_sysctl_info *info;
64 	struct sysctl_oid *oid;
65 	struct sysctl_oid *top, *drioid;
66 	int		  i;
67 
68 	info = kmalloc(sizeof *info, DRM_MEM_DRIVER, M_WAITOK | M_ZERO);
69 	if ( !info )
70 		return 1;
71 	dev->sysctl = info;
72 
73 	/* Add the sysctl node for DRI if it doesn't already exist */
74 	drioid = SYSCTL_ADD_NODE(&info->ctx, &sysctl__hw_children, OID_AUTO,
75 	    "dri", CTLFLAG_RW, NULL, "DRI Graphics");
76 	if (!drioid)
77 		return 1;
78 
79 	/* Find the next free slot under hw.dri */
80 	i = 0;
81 	SLIST_FOREACH(oid, SYSCTL_CHILDREN(drioid), oid_link) {
82 		if (i <= oid->oid_arg2)
83 			i = oid->oid_arg2 + 1;
84 	}
85 	if (i>9)
86 		return 1;
87 
88 	dev->sysctl_node_idx = i;
89 	/* Add the hw.dri.x for our device */
90 	info->name[0] = '0' + i;
91 	info->name[1] = 0;
92 	top = SYSCTL_ADD_NODE(&info->ctx, SYSCTL_CHILDREN(drioid),
93 	    OID_AUTO, info->name, CTLFLAG_RW, NULL, NULL);
94 	if (!top)
95 		return 1;
96 
97 	for (i = 0; i < DRM_SYSCTL_ENTRIES; i++) {
98 		oid = SYSCTL_ADD_OID(&info->ctx,
99 			SYSCTL_CHILDREN(top),
100 			OID_AUTO,
101 			drm_sysctl_list[i].name,
102 			CTLTYPE_STRING | CTLFLAG_RD,
103 			dev,
104 			0,
105 			drm_sysctl_list[i].f,
106 			"A",
107 			NULL);
108 		if (!oid)
109 			return 1;
110 	}
111 	SYSCTL_ADD_INT(&info->ctx, SYSCTL_CHILDREN(drioid), OID_AUTO, "debug",
112 	    CTLFLAG_RW, &drm_debug_flag, sizeof(drm_debug_flag),
113 	    "Enable debugging output");
114 	SYSCTL_ADD_INT(&info->ctx, SYSCTL_CHILDREN(drioid), OID_AUTO, "notyet",
115 	    CTLFLAG_RW, &drm_notyet_flag, sizeof(drm_debug_flag),
116 	    "Enable notyet reminders");
117 
118 	if (dev->driver->sysctl_init != NULL)
119 		dev->driver->sysctl_init(dev, &info->ctx, top);
120 
121 	SYSCTL_ADD_INT(&info->ctx, SYSCTL_CHILDREN(drioid), OID_AUTO,
122 	    "vblank_offdelay", CTLFLAG_RW, &drm_vblank_offdelay,
123 	    sizeof(drm_vblank_offdelay),
124 	    "");
125 	SYSCTL_ADD_INT(&info->ctx, SYSCTL_CHILDREN(drioid), OID_AUTO,
126 	    "timestamp_precision", CTLFLAG_RW, &drm_timestamp_precision,
127 	    sizeof(drm_timestamp_precision),
128 	    "");
129 
130 	return (0);
131 }
132 
133 int drm_sysctl_cleanup(struct drm_device *dev)
134 {
135 	int error;
136 
137 	error = sysctl_ctx_free(&dev->sysctl->ctx);
138 	drm_free(dev->sysctl, DRM_MEM_DRIVER);
139 	dev->sysctl = NULL;
140 	if (dev->driver->sysctl_cleanup != NULL)
141 		dev->driver->sysctl_cleanup(dev);
142 
143 	return (error);
144 }
145 
146 #define DRM_SYSCTL_PRINT(fmt, arg...)				\
147 do {								\
148 	ksnprintf(buf, sizeof(buf), fmt, ##arg);			\
149 	retcode = SYSCTL_OUT(req, buf, strlen(buf));		\
150 	if (retcode)						\
151 		goto done;					\
152 } while (0)
153 
154 static int drm_name_info DRM_SYSCTL_HANDLER_ARGS
155 {
156 	struct drm_device *dev = arg1;
157 	char buf[128];
158 	int retcode;
159 	int hasunique = 0;
160 
161 	DRM_SYSCTL_PRINT("%s 0x%x", dev->driver->name, dev2udev(dev->devnode));
162 
163 	DRM_LOCK(dev);
164 	if (dev->unique) {
165 		ksnprintf(buf, sizeof(buf), " %s", dev->unique);
166 		hasunique = 1;
167 	}
168 	DRM_UNLOCK(dev);
169 
170 	if (hasunique)
171 		SYSCTL_OUT(req, buf, strlen(buf));
172 
173 	SYSCTL_OUT(req, "", 1);
174 
175 done:
176 	return retcode;
177 }
178 
179 static int drm_vm_info DRM_SYSCTL_HANDLER_ARGS
180 {
181 	struct drm_device *dev = arg1;
182 	drm_local_map_t *map, *tempmaps;
183 	const char *types[] = {
184 		[_DRM_FRAME_BUFFER] = "FB",
185 		[_DRM_REGISTERS] = "REG",
186 		[_DRM_SHM] = "SHM",
187 		[_DRM_AGP] = "AGP",
188 		[_DRM_SCATTER_GATHER] = "SG",
189 		[_DRM_CONSISTENT] = "CONS",
190 		[_DRM_GEM] = "GEM"
191 	};
192 	const char *type, *yesno;
193 	int i, mapcount;
194 	char buf[128];
195 	int retcode;
196 
197 	/* We can't hold the lock while doing SYSCTL_OUTs, so allocate a
198 	 * temporary copy of all the map entries and then SYSCTL_OUT that.
199 	 */
200 	DRM_LOCK(dev);
201 
202 	mapcount = 0;
203 	TAILQ_FOREACH(map, &dev->maplist, link)
204 		mapcount++;
205 
206 	tempmaps = kmalloc(sizeof(drm_local_map_t) * mapcount, DRM_MEM_DRIVER,
207 	    M_NOWAIT);
208 	if (tempmaps == NULL) {
209 		DRM_UNLOCK(dev);
210 		return ENOMEM;
211 	}
212 
213 	i = 0;
214 	TAILQ_FOREACH(map, &dev->maplist, link)
215 		tempmaps[i++] = *map;
216 
217 	DRM_UNLOCK(dev);
218 
219 	DRM_SYSCTL_PRINT("\nslot offset	        size       "
220 	    "type flags address            handle mtrr\n");
221 
222 	for (i = 0; i < mapcount; i++) {
223 		map = &tempmaps[i];
224 
225 		switch(map->type) {
226 		default:
227 			type = "??";
228 			break;
229 		case _DRM_FRAME_BUFFER:
230 		case _DRM_REGISTERS:
231 		case _DRM_SHM:
232 		case _DRM_AGP:
233 		case _DRM_SCATTER_GATHER:
234 		case _DRM_CONSISTENT:
235 		case _DRM_GEM:
236 			type = types[map->type];
237 			break;
238 		}
239 
240 		if (!map->mtrr)
241 			yesno = "no";
242 		else
243 			yesno = "yes";
244 
245 		DRM_SYSCTL_PRINT(
246 		    "%4d 0x%016lx 0x%08lx %4.4s  0x%02x 0x%016lx %6d %s\n",
247 		    i, map->offset, map->size, type, map->flags,
248 		    (unsigned long)map->virtual,
249 		    (unsigned int)((unsigned long)map->handle >>
250 		    DRM_MAP_HANDLE_SHIFT), yesno);
251 	}
252 	SYSCTL_OUT(req, "", 1);
253 
254 done:
255 	drm_free(tempmaps, DRM_MEM_DRIVER);
256 	return retcode;
257 }
258 
259 static int drm_bufs_info DRM_SYSCTL_HANDLER_ARGS
260 {
261 	struct drm_device	 *dev = arg1;
262 	drm_device_dma_t *dma = dev->dma;
263 	drm_device_dma_t tempdma;
264 	int *templists;
265 	int i;
266 	char buf[128];
267 	int retcode;
268 
269 	/* We can't hold the locks around DRM_SYSCTL_PRINT, so make a temporary
270 	 * copy of the whole structure and the relevant data from buflist.
271 	 */
272 	DRM_LOCK(dev);
273 	if (dma == NULL) {
274 		DRM_UNLOCK(dev);
275 		return 0;
276 	}
277 	spin_lock(&dev->dma_lock);
278 	tempdma = *dma;
279 	templists = kmalloc(sizeof(int) * dma->buf_count, DRM_MEM_DRIVER,
280 	    M_NOWAIT);
281 	for (i = 0; i < dma->buf_count; i++)
282 		templists[i] = dma->buflist[i]->list;
283 	dma = &tempdma;
284 	spin_unlock(&dev->dma_lock);
285 	DRM_UNLOCK(dev);
286 
287 	DRM_SYSCTL_PRINT("\n o     size count  free	 segs pages    kB\n");
288 	for (i = 0; i <= DRM_MAX_ORDER; i++) {
289 		if (dma->bufs[i].buf_count)
290 			DRM_SYSCTL_PRINT("%2d %8d %5d %5d %5d %5d %5d\n",
291 				       i,
292 				       dma->bufs[i].buf_size,
293 				       dma->bufs[i].buf_count,
294 				       atomic_read(&dma->bufs[i]
295 						   .freelist.count),
296 				       dma->bufs[i].seg_count,
297 				       dma->bufs[i].seg_count
298 				       *(1 << dma->bufs[i].page_order),
299 				       (dma->bufs[i].seg_count
300 					* (1 << dma->bufs[i].page_order))
301 				       * (int)PAGE_SIZE / 1024);
302 	}
303 	DRM_SYSCTL_PRINT("\n");
304 	for (i = 0; i < dma->buf_count; i++) {
305 		if (i && !(i%32)) DRM_SYSCTL_PRINT("\n");
306 		DRM_SYSCTL_PRINT(" %d", templists[i]);
307 	}
308 	DRM_SYSCTL_PRINT("\n");
309 
310 	SYSCTL_OUT(req, "", 1);
311 done:
312 	drm_free(templists, DRM_MEM_DRIVER);
313 	return retcode;
314 }
315 
316 static int drm_clients_info DRM_SYSCTL_HANDLER_ARGS
317 {
318 	struct drm_device *dev = arg1;
319 	struct drm_file *priv, *tempprivs;
320 	char buf[128];
321 	int retcode;
322 	int privcount, i;
323 
324 	DRM_LOCK(dev);
325 
326 	privcount = 0;
327 	TAILQ_FOREACH(priv, &dev->files, link)
328 		privcount++;
329 
330 	tempprivs = kmalloc(sizeof(struct drm_file) * privcount, DRM_MEM_DRIVER,
331 	    M_NOWAIT);
332 	if (tempprivs == NULL) {
333 		DRM_UNLOCK(dev);
334 		return ENOMEM;
335 	}
336 	i = 0;
337 	TAILQ_FOREACH(priv, &dev->files, link)
338 		tempprivs[i++] = *priv;
339 
340 	DRM_UNLOCK(dev);
341 
342 	DRM_SYSCTL_PRINT(
343 	    "\na dev            pid   uid      magic     ioctls\n");
344 	for (i = 0; i < privcount; i++) {
345 		priv = &tempprivs[i];
346 		DRM_SYSCTL_PRINT("%c %-12s %5d %5d %10u %10lu\n",
347 			       priv->authenticated ? 'y' : 'n',
348 			       devtoname(priv->dev->devnode),
349 			       priv->pid,
350 			       priv->uid,
351 			       priv->magic,
352 			       priv->ioctl_count);
353 	}
354 
355 	SYSCTL_OUT(req, "", 1);
356 done:
357 	drm_free(tempprivs, DRM_MEM_DRIVER);
358 	return retcode;
359 }
360 
361 static int drm_vblank_info DRM_SYSCTL_HANDLER_ARGS
362 {
363 	struct drm_device *dev = arg1;
364 	char buf[128];
365 	int retcode;
366 	int i;
367 
368 	DRM_SYSCTL_PRINT("\ncrtc ref count    last     enabled inmodeset\n");
369 	DRM_LOCK(dev);
370 	if (dev->_vblank_count == NULL)
371 		goto done;
372 	for (i = 0 ; i < dev->num_crtcs ; i++) {
373 		DRM_SYSCTL_PRINT("  %02d  %02d %08d %08d %02d      %02d\n",
374 		    i, dev->vblank_refcount[i],
375 		    dev->_vblank_count[i],
376 		    dev->last_vblank[i],
377 		    dev->vblank_enabled[i],
378 		    dev->vblank_inmodeset[i]);
379 	}
380 done:
381 	DRM_UNLOCK(dev);
382 
383 	SYSCTL_OUT(req, "", -1);
384 	return retcode;
385 }
386