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