xref: /freebsd/sys/dev/drm2/drm_sysctl.c (revision d184218c)
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__hw_children, 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%x", dev->driver->name, dev2udev(dev->devnode));
159 
160 	DRM_LOCK(dev);
161 	if (dev->unique) {
162 		snprintf(buf, sizeof(buf), " %s", dev->unique);
163 		hasunique = 1;
164 	}
165 	DRM_UNLOCK(dev);
166 
167 	if (hasunique)
168 		SYSCTL_OUT(req, buf, strlen(buf));
169 
170 	SYSCTL_OUT(req, "", 1);
171 
172 done:
173 	return retcode;
174 }
175 
176 static int drm_vm_info DRM_SYSCTL_HANDLER_ARGS
177 {
178 	struct drm_device *dev = arg1;
179 	drm_local_map_t *map, *tempmaps;
180 	const char   *types[] = { "FB", "REG", "SHM", "AGP", "SG" };
181 	const char *type, *yesno;
182 	int i, mapcount;
183 	char buf[128];
184 	int retcode;
185 
186 	/* We can't hold the lock while doing SYSCTL_OUTs, so allocate a
187 	 * temporary copy of all the map entries and then SYSCTL_OUT that.
188 	 */
189 	DRM_LOCK(dev);
190 
191 	mapcount = 0;
192 	TAILQ_FOREACH(map, &dev->maplist, link)
193 		mapcount++;
194 
195 	tempmaps = malloc(sizeof(drm_local_map_t) * mapcount, DRM_MEM_DRIVER,
196 	    M_NOWAIT);
197 	if (tempmaps == NULL) {
198 		DRM_UNLOCK(dev);
199 		return ENOMEM;
200 	}
201 
202 	i = 0;
203 	TAILQ_FOREACH(map, &dev->maplist, link)
204 		tempmaps[i++] = *map;
205 
206 	DRM_UNLOCK(dev);
207 
208 	DRM_SYSCTL_PRINT("\nslot offset	        size       "
209 	    "type flags address            handle mtrr\n");
210 
211 	for (i = 0; i < mapcount; i++) {
212 		map = &tempmaps[i];
213 
214 		if (map->type < 0 || map->type > 4)
215 			type = "??";
216 		else
217 			type = types[map->type];
218 
219 		if (!map->mtrr)
220 			yesno = "no";
221 		else
222 			yesno = "yes";
223 
224 		DRM_SYSCTL_PRINT(
225 		    "%4d 0x%016lx 0x%08lx %4.4s  0x%02x 0x%016lx %6d %s\n",
226 		    i, map->offset, map->size, type, map->flags,
227 		    (unsigned long)map->virtual,
228 		    (unsigned int)((unsigned long)map->handle >>
229 		    DRM_MAP_HANDLE_SHIFT), yesno);
230 	}
231 	SYSCTL_OUT(req, "", 1);
232 
233 done:
234 	free(tempmaps, DRM_MEM_DRIVER);
235 	return retcode;
236 }
237 
238 static int drm_bufs_info DRM_SYSCTL_HANDLER_ARGS
239 {
240 	struct drm_device	 *dev = arg1;
241 	drm_device_dma_t *dma = dev->dma;
242 	drm_device_dma_t tempdma;
243 	int *templists;
244 	int i;
245 	char buf[128];
246 	int retcode;
247 
248 	/* We can't hold the locks around DRM_SYSCTL_PRINT, so make a temporary
249 	 * copy of the whole structure and the relevant data from buflist.
250 	 */
251 	DRM_LOCK(dev);
252 	if (dma == NULL) {
253 		DRM_UNLOCK(dev);
254 		return 0;
255 	}
256 	DRM_SPINLOCK(&dev->dma_lock);
257 	tempdma = *dma;
258 	templists = malloc(sizeof(int) * dma->buf_count, DRM_MEM_DRIVER,
259 	    M_NOWAIT);
260 	for (i = 0; i < dma->buf_count; i++)
261 		templists[i] = dma->buflist[i]->list;
262 	dma = &tempdma;
263 	DRM_SPINUNLOCK(&dev->dma_lock);
264 	DRM_UNLOCK(dev);
265 
266 	DRM_SYSCTL_PRINT("\n o     size count  free	 segs pages    kB\n");
267 	for (i = 0; i <= DRM_MAX_ORDER; i++) {
268 		if (dma->bufs[i].buf_count)
269 			DRM_SYSCTL_PRINT("%2d %8d %5d %5d %5d %5d %5d\n",
270 				       i,
271 				       dma->bufs[i].buf_size,
272 				       dma->bufs[i].buf_count,
273 				       atomic_read(&dma->bufs[i]
274 						   .freelist.count),
275 				       dma->bufs[i].seg_count,
276 				       dma->bufs[i].seg_count
277 				       *(1 << dma->bufs[i].page_order),
278 				       (dma->bufs[i].seg_count
279 					* (1 << dma->bufs[i].page_order))
280 				       * (int)PAGE_SIZE / 1024);
281 	}
282 	DRM_SYSCTL_PRINT("\n");
283 	for (i = 0; i < dma->buf_count; i++) {
284 		if (i && !(i%32)) DRM_SYSCTL_PRINT("\n");
285 		DRM_SYSCTL_PRINT(" %d", templists[i]);
286 	}
287 	DRM_SYSCTL_PRINT("\n");
288 
289 	SYSCTL_OUT(req, "", 1);
290 done:
291 	free(templists, DRM_MEM_DRIVER);
292 	return retcode;
293 }
294 
295 static int drm_clients_info DRM_SYSCTL_HANDLER_ARGS
296 {
297 	struct drm_device *dev = arg1;
298 	struct drm_file *priv, *tempprivs;
299 	char buf[128];
300 	int retcode;
301 	int privcount, i;
302 
303 	DRM_LOCK(dev);
304 
305 	privcount = 0;
306 	TAILQ_FOREACH(priv, &dev->files, link)
307 		privcount++;
308 
309 	tempprivs = malloc(sizeof(struct drm_file) * privcount, DRM_MEM_DRIVER,
310 	    M_NOWAIT);
311 	if (tempprivs == NULL) {
312 		DRM_UNLOCK(dev);
313 		return ENOMEM;
314 	}
315 	i = 0;
316 	TAILQ_FOREACH(priv, &dev->files, link)
317 		tempprivs[i++] = *priv;
318 
319 	DRM_UNLOCK(dev);
320 
321 	DRM_SYSCTL_PRINT(
322 	    "\na dev            pid   uid      magic     ioctls\n");
323 	for (i = 0; i < privcount; i++) {
324 		priv = &tempprivs[i];
325 		DRM_SYSCTL_PRINT("%c %-12s %5d %5d %10u %10lu\n",
326 			       priv->authenticated ? 'y' : 'n',
327 			       devtoname(priv->dev->devnode),
328 			       priv->pid,
329 			       priv->uid,
330 			       priv->magic,
331 			       priv->ioctl_count);
332 	}
333 
334 	SYSCTL_OUT(req, "", 1);
335 done:
336 	free(tempprivs, DRM_MEM_DRIVER);
337 	return retcode;
338 }
339 
340 static int drm_vblank_info DRM_SYSCTL_HANDLER_ARGS
341 {
342 	struct drm_device *dev = arg1;
343 	char buf[128];
344 	int retcode;
345 	int i;
346 
347 	DRM_SYSCTL_PRINT("\ncrtc ref count    last     enabled inmodeset\n");
348 	DRM_LOCK(dev);
349 	if (dev->_vblank_count == NULL)
350 		goto done;
351 	for (i = 0 ; i < dev->num_crtcs ; i++) {
352 		DRM_SYSCTL_PRINT("  %02d  %02d %08d %08d %02d      %02d\n",
353 		    i, dev->vblank_refcount[i],
354 		    dev->_vblank_count[i],
355 		    dev->last_vblank[i],
356 		    dev->vblank_enabled[i],
357 		    dev->vblank_inmodeset[i]);
358 	}
359 done:
360 	DRM_UNLOCK(dev);
361 
362 	SYSCTL_OUT(req, "", -1);
363 	return retcode;
364 }
365