xref: /freebsd/sys/dev/drm2/drm_sysctl.c (revision 09a53ad8f1318c5daae6cfb19d97f4f6459f0013)
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 		free(dev->sysctl, DRM_MEM_DRIVER);
75 		dev->sysctl = NULL;
76 		return (-ENOMEM);
77 	}
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 		drm_sysctl_cleanup(dev);
87 		return (-ENOSPC);
88 	}
89 
90 	dev->sysctl_node_idx = i;
91 	/* Add the hw.dri.x for our device */
92 	info->name[0] = '0' + i;
93 	info->name[1] = 0;
94 	top = SYSCTL_ADD_NODE(&info->ctx, SYSCTL_CHILDREN(drioid),
95 	    OID_AUTO, info->name, CTLFLAG_RW, NULL, NULL);
96 	if (!top) {
97 		drm_sysctl_cleanup(dev);
98 		return (-ENOMEM);
99 	}
100 
101 	for (i = 0; i < DRM_SYSCTL_ENTRIES; i++) {
102 		oid = SYSCTL_ADD_OID(&info->ctx,
103 			SYSCTL_CHILDREN(top),
104 			OID_AUTO,
105 			drm_sysctl_list[i].name,
106 			CTLTYPE_STRING | CTLFLAG_RD,
107 			dev,
108 			0,
109 			drm_sysctl_list[i].f,
110 			"A",
111 			NULL);
112 		if (!oid) {
113 			drm_sysctl_cleanup(dev);
114 			return (-ENOMEM);
115 		}
116 	}
117 	SYSCTL_ADD_INT(&info->ctx, SYSCTL_CHILDREN(drioid), OID_AUTO, "debug",
118 	    CTLFLAG_RW, &drm_debug, sizeof(drm_debug),
119 	    "Enable debugging output");
120 	SYSCTL_ADD_INT(&info->ctx, SYSCTL_CHILDREN(drioid), OID_AUTO, "notyet",
121 	    CTLFLAG_RW, &drm_notyet, sizeof(drm_debug),
122 	    "Enable notyet reminders");
123 
124 	if (dev->driver->sysctl_init != NULL)
125 		dev->driver->sysctl_init(dev, &info->ctx, top);
126 
127 	SYSCTL_ADD_INT(&info->ctx, SYSCTL_CHILDREN(drioid), OID_AUTO,
128 	    "vblank_offdelay", CTLFLAG_RW, &drm_vblank_offdelay,
129 	    sizeof(drm_vblank_offdelay),
130 	    "");
131 	SYSCTL_ADD_INT(&info->ctx, SYSCTL_CHILDREN(drioid), OID_AUTO,
132 	    "timestamp_precision", CTLFLAG_RW, &drm_timestamp_precision,
133 	    sizeof(drm_timestamp_precision),
134 	    "");
135 
136 	return (0);
137 }
138 
139 int drm_sysctl_cleanup(struct drm_device *dev)
140 {
141 	int error;
142 
143 	if (dev->sysctl == NULL)
144 		return (0);
145 
146 	error = sysctl_ctx_free(&dev->sysctl->ctx);
147 	free(dev->sysctl, DRM_MEM_DRIVER);
148 	dev->sysctl = NULL;
149 	if (dev->driver->sysctl_cleanup != NULL)
150 		dev->driver->sysctl_cleanup(dev);
151 
152 	return (-error);
153 }
154 
155 #define DRM_SYSCTL_PRINT(fmt, arg...)				\
156 do {								\
157 	snprintf(buf, sizeof(buf), fmt, ##arg);			\
158 	retcode = SYSCTL_OUT(req, buf, strlen(buf));		\
159 	if (retcode)						\
160 		goto done;					\
161 } while (0)
162 
163 static int drm_name_info DRM_SYSCTL_HANDLER_ARGS
164 {
165 	struct drm_device *dev = arg1;
166 	struct drm_minor *minor;
167 	struct drm_master *master;
168 	char buf[128];
169 	int retcode;
170 	int hasunique = 0;
171 
172 	/* FIXME: This still uses primary minor. */
173 	minor = dev->primary;
174 	DRM_SYSCTL_PRINT("%s 0x%jx", dev->driver->name,
175 	    (uintmax_t)dev2udev(minor->device));
176 
177 	DRM_LOCK(dev);
178 	master = minor->master;
179 	if (master != NULL && master->unique) {
180 		snprintf(buf, sizeof(buf), " %s", master->unique);
181 		hasunique = 1;
182 	}
183 	DRM_UNLOCK(dev);
184 
185 	if (hasunique)
186 		SYSCTL_OUT(req, buf, strlen(buf));
187 
188 	SYSCTL_OUT(req, "", 1);
189 
190 done:
191 	return retcode;
192 }
193 
194 static int drm_vm_info DRM_SYSCTL_HANDLER_ARGS
195 {
196 	struct drm_device *dev = arg1;
197 	struct drm_map_list *entry;
198 	struct drm_local_map *map, *tempmaps;
199 	const char *types[] = {
200 		[_DRM_FRAME_BUFFER] = "FB",
201 		[_DRM_REGISTERS] = "REG",
202 		[_DRM_SHM] = "SHM",
203 		[_DRM_AGP] = "AGP",
204 		[_DRM_SCATTER_GATHER] = "SG",
205 		[_DRM_CONSISTENT] = "CONS",
206 		[_DRM_GEM] = "GEM"
207 	};
208 	const char *type, *yesno;
209 	int i, mapcount;
210 	char buf[128];
211 	int retcode;
212 
213 	/* We can't hold the lock while doing SYSCTL_OUTs, so allocate a
214 	 * temporary copy of all the map entries and then SYSCTL_OUT that.
215 	 */
216 	DRM_LOCK(dev);
217 
218 	mapcount = 0;
219 	list_for_each_entry(entry, &dev->maplist, head) {
220 		if (entry->map != NULL)
221 			mapcount++;
222 	}
223 
224 	tempmaps = malloc(sizeof(*tempmaps) * mapcount, DRM_MEM_DRIVER,
225 	    M_NOWAIT);
226 	if (tempmaps == NULL) {
227 		DRM_UNLOCK(dev);
228 		return ENOMEM;
229 	}
230 
231 	i = 0;
232 	list_for_each_entry(entry, &dev->maplist, head) {
233 		if (entry->map != NULL)
234 			tempmaps[i++] = *entry->map;
235 	}
236 
237 	DRM_UNLOCK(dev);
238 
239 	DRM_SYSCTL_PRINT("\nslot offset	        size       "
240 	    "type flags address            mtrr\n");
241 
242 	for (i = 0; i < mapcount; i++) {
243 		map = &tempmaps[i];
244 
245 		switch(map->type) {
246 		default:
247 			type = "??";
248 			break;
249 		case _DRM_FRAME_BUFFER:
250 		case _DRM_REGISTERS:
251 		case _DRM_SHM:
252 		case _DRM_AGP:
253 		case _DRM_SCATTER_GATHER:
254 		case _DRM_CONSISTENT:
255 		case _DRM_GEM:
256 			type = types[map->type];
257 			break;
258 		}
259 
260 		if (map->mtrr < 0)
261 			yesno = "no";
262 		else
263 			yesno = "yes";
264 
265 		DRM_SYSCTL_PRINT(
266 		    "%4d 0x%016llx 0x%08lx %4.4s  0x%02x 0x%016lx %s\n",
267 		    i, (unsigned long long)map->offset, map->size, type,
268 		    map->flags, (unsigned long)map->handle, yesno);
269 	}
270 	SYSCTL_OUT(req, "", 1);
271 
272 done:
273 	free(tempmaps, DRM_MEM_DRIVER);
274 	return retcode;
275 }
276 
277 static int drm_bufs_info DRM_SYSCTL_HANDLER_ARGS
278 {
279 	struct drm_device	 *dev = arg1;
280 	struct drm_device_dma *dma = dev->dma;
281 	struct drm_device_dma tempdma;
282 	int *templists;
283 	int i;
284 	char buf[128];
285 	int retcode;
286 
287 	/* We can't hold the locks around DRM_SYSCTL_PRINT, so make a temporary
288 	 * copy of the whole structure and the relevant data from buflist.
289 	 */
290 	DRM_LOCK(dev);
291 	if (dma == NULL) {
292 		DRM_UNLOCK(dev);
293 		return 0;
294 	}
295 	DRM_SPINLOCK(&dev->dma_lock);
296 	tempdma = *dma;
297 	templists = malloc(sizeof(int) * dma->buf_count, DRM_MEM_DRIVER,
298 	    M_NOWAIT);
299 	for (i = 0; i < dma->buf_count; i++)
300 		templists[i] = dma->buflist[i]->list;
301 	dma = &tempdma;
302 	DRM_SPINUNLOCK(&dev->dma_lock);
303 	DRM_UNLOCK(dev);
304 
305 	DRM_SYSCTL_PRINT("\n o     size count  free	 segs pages    kB\n");
306 	for (i = 0; i <= DRM_MAX_ORDER; i++) {
307 		if (dma->bufs[i].buf_count)
308 			DRM_SYSCTL_PRINT("%2d %8d %5d %5d %5d %5d %5d\n",
309 				       i,
310 				       dma->bufs[i].buf_size,
311 				       dma->bufs[i].buf_count,
312 				       atomic_read(&dma->bufs[i]
313 						   .freelist.count),
314 				       dma->bufs[i].seg_count,
315 				       dma->bufs[i].seg_count
316 				       *(1 << dma->bufs[i].page_order),
317 				       (dma->bufs[i].seg_count
318 					* (1 << dma->bufs[i].page_order))
319 				       * (int)PAGE_SIZE / 1024);
320 	}
321 	DRM_SYSCTL_PRINT("\n");
322 	for (i = 0; i < dma->buf_count; i++) {
323 		if (i && !(i%32)) DRM_SYSCTL_PRINT("\n");
324 		DRM_SYSCTL_PRINT(" %d", templists[i]);
325 	}
326 	DRM_SYSCTL_PRINT("\n");
327 
328 	SYSCTL_OUT(req, "", 1);
329 done:
330 	free(templists, DRM_MEM_DRIVER);
331 	return retcode;
332 }
333 
334 static int drm_clients_info DRM_SYSCTL_HANDLER_ARGS
335 {
336 	struct drm_device *dev = arg1;
337 	struct drm_file *priv, *tempprivs;
338 	char buf[128];
339 	int retcode;
340 	int privcount, i;
341 
342 	DRM_LOCK(dev);
343 
344 	privcount = 0;
345 	list_for_each_entry(priv, &dev->filelist, lhead)
346 		privcount++;
347 
348 	tempprivs = malloc(sizeof(struct drm_file) * privcount, DRM_MEM_DRIVER,
349 	    M_NOWAIT);
350 	if (tempprivs == NULL) {
351 		DRM_UNLOCK(dev);
352 		return ENOMEM;
353 	}
354 	i = 0;
355 	list_for_each_entry(priv, &dev->filelist, lhead)
356 		tempprivs[i++] = *priv;
357 
358 	DRM_UNLOCK(dev);
359 
360 	DRM_SYSCTL_PRINT(
361 	    "\na dev            pid   uid      magic     ioctls\n");
362 	for (i = 0; i < privcount; i++) {
363 		priv = &tempprivs[i];
364 		DRM_SYSCTL_PRINT("%c %-12s %5d %5d %10u %10lu\n",
365 			       priv->authenticated ? 'y' : 'n',
366 			       devtoname(priv->minor->device),
367 			       priv->pid,
368 			       priv->uid,
369 			       priv->magic,
370 			       priv->ioctl_count);
371 	}
372 
373 	SYSCTL_OUT(req, "", 1);
374 done:
375 	free(tempprivs, DRM_MEM_DRIVER);
376 	return retcode;
377 }
378 
379 static int drm_vblank_info DRM_SYSCTL_HANDLER_ARGS
380 {
381 	struct drm_device *dev = arg1;
382 	char buf[128];
383 	int retcode;
384 	int i;
385 
386 	DRM_SYSCTL_PRINT("\ncrtc ref count    last     enabled inmodeset\n");
387 	DRM_LOCK(dev);
388 	if (dev->_vblank_count == NULL)
389 		goto done;
390 	for (i = 0 ; i < dev->num_crtcs ; i++) {
391 		DRM_SYSCTL_PRINT("  %02d  %02d %08d %08d %02d      %02d\n",
392 		    i, dev->vblank_refcount[i],
393 		    dev->_vblank_count[i],
394 		    dev->last_vblank[i],
395 		    dev->vblank_enabled[i],
396 		    dev->vblank_inmodeset[i]);
397 	}
398 done:
399 	DRM_UNLOCK(dev);
400 
401 	SYSCTL_OUT(req, "", -1);
402 	return retcode;
403 }
404