xref: /freebsd-12.1/sys/dev/drm/drm_sysctl.c (revision 7850fa71)
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/drm/drmP.h"
33 #include "dev/drm/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 	if ( !info )
69 		return 1;
70 	dev->sysctl = info;
71 
72 	/* Add the sysctl node for DRI if it doesn't already exist */
73 	drioid = SYSCTL_ADD_NODE( &info->ctx, &sysctl__hw_children, OID_AUTO, "dri", CTLFLAG_RW, NULL, "DRI Graphics");
74 	if (!drioid)
75 		return 1;
76 
77 	/* Find the next free slot under hw.dri */
78 	i = 0;
79 	SLIST_FOREACH(oid, SYSCTL_CHILDREN(drioid), oid_link) {
80 		if (i <= oid->oid_arg2)
81 			i = oid->oid_arg2 + 1;
82 	}
83 	if (i>9)
84 		return 1;
85 
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), OID_AUTO, info->name, CTLFLAG_RW, NULL, NULL);
90 	if (!top)
91 		return 1;
92 
93 	for (i = 0; i < DRM_SYSCTL_ENTRIES; i++) {
94 		oid = SYSCTL_ADD_OID(&info->ctx,
95 			SYSCTL_CHILDREN(top),
96 			OID_AUTO,
97 			drm_sysctl_list[i].name,
98 			CTLTYPE_INT | CTLFLAG_RD,
99 			dev,
100 			0,
101 			drm_sysctl_list[i].f,
102 			"A",
103 			NULL);
104 		if (!oid)
105 			return 1;
106 	}
107 	SYSCTL_ADD_INT(&info->ctx, SYSCTL_CHILDREN(top), OID_AUTO, "debug",
108 	    CTLFLAG_RW, &drm_debug_flag, sizeof(drm_debug_flag),
109 	    "Enable debugging output");
110 
111 	return 0;
112 }
113 
114 int drm_sysctl_cleanup(struct drm_device *dev)
115 {
116 	int error;
117 	error = sysctl_ctx_free( &dev->sysctl->ctx );
118 
119 	free(dev->sysctl, DRM_MEM_DRIVER);
120 	dev->sysctl = NULL;
121 
122 	return error;
123 }
124 
125 #define DRM_SYSCTL_PRINT(fmt, arg...)				\
126 do {								\
127 	snprintf(buf, sizeof(buf), fmt, ##arg);			\
128 	retcode = SYSCTL_OUT(req, buf, strlen(buf));		\
129 	if (retcode)						\
130 		goto done;					\
131 } while (0)
132 
133 static int drm_name_info DRM_SYSCTL_HANDLER_ARGS
134 {
135 	struct drm_device *dev = arg1;
136 	char buf[128];
137 	int retcode;
138 	int hasunique = 0;
139 
140 	DRM_SYSCTL_PRINT("%s 0x%x", dev->driver->name, dev2udev(dev->devnode));
141 
142 	DRM_LOCK();
143 	if (dev->unique) {
144 		snprintf(buf, sizeof(buf), " %s", dev->unique);
145 		hasunique = 1;
146 	}
147 	DRM_UNLOCK();
148 
149 	if (hasunique)
150 		SYSCTL_OUT(req, buf, strlen(buf));
151 
152 	SYSCTL_OUT(req, "", 1);
153 
154 done:
155 	return retcode;
156 }
157 
158 static int drm_vm_info DRM_SYSCTL_HANDLER_ARGS
159 {
160 	struct drm_device *dev = arg1;
161 	drm_local_map_t *map, *tempmaps;
162 	const char   *types[] = { "FB", "REG", "SHM", "AGP", "SG" };
163 	const char *type, *yesno;
164 	int i, mapcount;
165 	char buf[128];
166 	int retcode;
167 
168 	/* We can't hold the lock while doing SYSCTL_OUTs, so allocate a
169 	 * temporary copy of all the map entries and then SYSCTL_OUT that.
170 	 */
171 	DRM_LOCK();
172 
173 	mapcount = 0;
174 	TAILQ_FOREACH(map, &dev->maplist, link)
175 		mapcount++;
176 
177 	tempmaps = malloc(sizeof(drm_local_map_t) * mapcount, DRM_MEM_DRIVER,
178 	    M_NOWAIT);
179 	if (tempmaps == NULL) {
180 		DRM_UNLOCK();
181 		return ENOMEM;
182 	}
183 
184 	i = 0;
185 	TAILQ_FOREACH(map, &dev->maplist, link)
186 		tempmaps[i++] = *map;
187 
188 	DRM_UNLOCK();
189 
190 	DRM_SYSCTL_PRINT("\nslot offset	        size       "
191 	    "type flags address            mtrr\n");
192 
193 	for (i = 0; i < mapcount; i++) {
194 		map = &tempmaps[i];
195 
196 		if (map->type < 0 || map->type > 4)
197 			type = "??";
198 		else
199 			type = types[map->type];
200 
201 		if (!map->mtrr)
202 			yesno = "no";
203 		else
204 			yesno = "yes";
205 
206 		DRM_SYSCTL_PRINT(
207 		    "%4d 0x%016lx 0x%08lx %4.4s  0x%02x 0x%016lx %s\n", i,
208 		    map->offset, map->size, type, map->flags,
209 		    (unsigned long)map->handle, yesno);
210 	}
211 	SYSCTL_OUT(req, "", 1);
212 
213 done:
214 	free(tempmaps, DRM_MEM_DRIVER);
215 	return retcode;
216 }
217 
218 static int drm_bufs_info DRM_SYSCTL_HANDLER_ARGS
219 {
220 	struct drm_device	 *dev = arg1;
221 	drm_device_dma_t *dma = dev->dma;
222 	drm_device_dma_t tempdma;
223 	int *templists;
224 	int i;
225 	char buf[128];
226 	int retcode;
227 
228 	/* We can't hold the locks around DRM_SYSCTL_PRINT, so make a temporary
229 	 * copy of the whole structure and the relevant data from buflist.
230 	 */
231 	DRM_LOCK();
232 	if (dma == NULL) {
233 		DRM_UNLOCK();
234 		return 0;
235 	}
236 	DRM_SPINLOCK(&dev->dma_lock);
237 	tempdma = *dma;
238 	templists = malloc(sizeof(int) * dma->buf_count, DRM_MEM_DRIVER,
239 	    M_NOWAIT);
240 	for (i = 0; i < dma->buf_count; i++)
241 		templists[i] = dma->buflist[i]->list;
242 	dma = &tempdma;
243 	DRM_SPINUNLOCK(&dev->dma_lock);
244 	DRM_UNLOCK();
245 
246 	DRM_SYSCTL_PRINT("\n o     size count  free	 segs pages    kB\n");
247 	for (i = 0; i <= DRM_MAX_ORDER; i++) {
248 		if (dma->bufs[i].buf_count)
249 			DRM_SYSCTL_PRINT("%2d %8d %5d %5d %5d %5d %5d\n",
250 				       i,
251 				       dma->bufs[i].buf_size,
252 				       dma->bufs[i].buf_count,
253 				       atomic_read(&dma->bufs[i]
254 						   .freelist.count),
255 				       dma->bufs[i].seg_count,
256 				       dma->bufs[i].seg_count
257 				       *(1 << dma->bufs[i].page_order),
258 				       (dma->bufs[i].seg_count
259 					* (1 << dma->bufs[i].page_order))
260 				       * PAGE_SIZE / 1024);
261 	}
262 	DRM_SYSCTL_PRINT("\n");
263 	for (i = 0; i < dma->buf_count; i++) {
264 		if (i && !(i%32)) DRM_SYSCTL_PRINT("\n");
265 		DRM_SYSCTL_PRINT(" %d", templists[i]);
266 	}
267 	DRM_SYSCTL_PRINT("\n");
268 
269 	SYSCTL_OUT(req, "", 1);
270 done:
271 	free(templists, DRM_MEM_DRIVER);
272 	return retcode;
273 }
274 
275 static int drm_clients_info DRM_SYSCTL_HANDLER_ARGS
276 {
277 	struct drm_device *dev = arg1;
278 	struct drm_file *priv, *tempprivs;
279 	char buf[128];
280 	int retcode;
281 	int privcount, i;
282 
283 	DRM_LOCK();
284 
285 	privcount = 0;
286 	TAILQ_FOREACH(priv, &dev->files, link)
287 		privcount++;
288 
289 	tempprivs = malloc(sizeof(struct drm_file) * privcount, DRM_MEM_DRIVER,
290 	    M_NOWAIT);
291 	if (tempprivs == NULL) {
292 		DRM_UNLOCK();
293 		return ENOMEM;
294 	}
295 	i = 0;
296 	TAILQ_FOREACH(priv, &dev->files, link)
297 		tempprivs[i++] = *priv;
298 
299 	DRM_UNLOCK();
300 
301 	DRM_SYSCTL_PRINT("\na dev	pid    uid	magic	  ioctls\n");
302 	for (i = 0; i < privcount; i++) {
303 		priv = &tempprivs[i];
304 		DRM_SYSCTL_PRINT("%c %3d %5d %5d %10u %10lu\n",
305 			       priv->authenticated ? 'y' : 'n',
306 			       priv->minor,
307 			       priv->pid,
308 			       priv->uid,
309 			       priv->magic,
310 			       priv->ioctl_count);
311 	}
312 
313 	SYSCTL_OUT(req, "", 1);
314 done:
315 	free(tempprivs, DRM_MEM_DRIVER);
316 	return retcode;
317 }
318 
319 static int drm_vblank_info DRM_SYSCTL_HANDLER_ARGS
320 {
321 	struct drm_device *dev = arg1;
322 	char buf[128];
323 	int retcode;
324 	int i;
325 
326 	DRM_SYSCTL_PRINT("\ncrtc ref count    last     enabled inmodeset\n");
327 	for(i = 0 ; i < dev->num_crtcs ; i++) {
328 		DRM_SYSCTL_PRINT("  %02d  %02d %08d %08d %02d      %02d\n",
329 		    i, atomic_load_acq_32(&dev->vblank[i].refcount),
330 		    atomic_load_acq_32(&dev->vblank[i].count),
331 		    atomic_load_acq_32(&dev->vblank[i].last),
332 		    atomic_load_acq_int(&dev->vblank[i].enabled),
333 		    atomic_load_acq_int(&dev->vblank[i].inmodeset));
334 	}
335 
336 	SYSCTL_OUT(req, "", -1);
337 done:
338 	return retcode;
339 }
340