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