drm_sysctl.c revision 182080
1145132Sanholt/*-
2145132Sanholt * Copyright 2003 Eric Anholt
3145132Sanholt * All Rights Reserved.
4145132Sanholt *
5145132Sanholt * Permission is hereby granted, free of charge, to any person obtaining a
6145132Sanholt * copy of this software and associated documentation files (the "Software"),
7145132Sanholt * to deal in the Software without restriction, including without limitation
8145132Sanholt * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9145132Sanholt * and/or sell copies of the Software, and to permit persons to whom the
10145132Sanholt * Software is furnished to do so, subject to the following conditions:
11145132Sanholt *
12145132Sanholt * The above copyright notice and this permission notice (including the next
13145132Sanholt * paragraph) shall be included in all copies or substantial portions of the
14145132Sanholt * Software.
15145132Sanholt *
16145132Sanholt * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17145132Sanholt * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18145132Sanholt * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19145132Sanholt * ERIC ANHOLT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20145132Sanholt * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21145132Sanholt * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22145132Sanholt */
23145132Sanholt
24152909Sanholt#include <sys/cdefs.h>
25152909Sanholt__FBSDID("$FreeBSD: head/sys/dev/drm/drm_sysctl.c 182080 2008-08-23 20:59:12Z rnoland $");
26152909Sanholt
27182080Srnoland/** @file drm_sysctl.c
28182080Srnoland * Implementation of various sysctls for controlling DRM behavior and reporting
29182080Srnoland * debug information.
30182080Srnoland */
31182080Srnoland
32145132Sanholt#include "dev/drm/drmP.h"
33145132Sanholt#include "dev/drm/drm.h"
34145132Sanholt
35145132Sanholt#include <sys/sysctl.h>
36145132Sanholt
37145132Sanholtstatic int	   drm_name_info DRM_SYSCTL_HANDLER_ARGS;
38145132Sanholtstatic int	   drm_vm_info DRM_SYSCTL_HANDLER_ARGS;
39145132Sanholtstatic int	   drm_clients_info DRM_SYSCTL_HANDLER_ARGS;
40145132Sanholtstatic int	   drm_bufs_info DRM_SYSCTL_HANDLER_ARGS;
41145132Sanholt
42145132Sanholtstruct drm_sysctl_list {
43145132Sanholt	const char *name;
44145132Sanholt	int	   (*f) DRM_SYSCTL_HANDLER_ARGS;
45145132Sanholt} drm_sysctl_list[] = {
46145132Sanholt	{"name",    drm_name_info},
47145132Sanholt	{"vm",	    drm_vm_info},
48145132Sanholt	{"clients", drm_clients_info},
49145132Sanholt	{"bufs",    drm_bufs_info},
50145132Sanholt};
51145132Sanholt#define DRM_SYSCTL_ENTRIES (sizeof(drm_sysctl_list)/sizeof(drm_sysctl_list[0]))
52145132Sanholt
53145132Sanholtstruct drm_sysctl_info {
54145132Sanholt	struct sysctl_ctx_list ctx;
55145132Sanholt	char		       name[2];
56145132Sanholt};
57145132Sanholt
58182080Srnolandint drm_sysctl_init(struct drm_device *dev)
59145132Sanholt{
60145132Sanholt	struct drm_sysctl_info *info;
61145132Sanholt	struct sysctl_oid *oid;
62145132Sanholt	struct sysctl_oid *top, *drioid;
63145132Sanholt	int		  i;
64145132Sanholt
65145132Sanholt	info = malloc(sizeof *info, M_DRM, M_WAITOK | M_ZERO);
66145132Sanholt	if ( !info )
67145132Sanholt		return 1;
68145132Sanholt	dev->sysctl = info;
69145132Sanholt
70145132Sanholt	/* Add the sysctl node for DRI if it doesn't already exist */
71145132Sanholt	drioid = SYSCTL_ADD_NODE( &info->ctx, &sysctl__hw_children, OID_AUTO, "dri", CTLFLAG_RW, NULL, "DRI Graphics");
72145132Sanholt	if (!drioid)
73145132Sanholt		return 1;
74145132Sanholt
75145132Sanholt	/* Find the next free slot under hw.dri */
76145132Sanholt	i = 0;
77145132Sanholt	SLIST_FOREACH(oid, SYSCTL_CHILDREN(drioid), oid_link) {
78145132Sanholt		if (i <= oid->oid_arg2)
79145132Sanholt			i = oid->oid_arg2 + 1;
80145132Sanholt	}
81145132Sanholt	if (i>9)
82145132Sanholt		return 1;
83145132Sanholt
84145132Sanholt	/* Add the hw.dri.x for our device */
85145132Sanholt	info->name[0] = '0' + i;
86145132Sanholt	info->name[1] = 0;
87145132Sanholt	top = SYSCTL_ADD_NODE( &info->ctx, SYSCTL_CHILDREN(drioid), OID_AUTO, info->name, CTLFLAG_RW, NULL, NULL);
88145132Sanholt	if (!top)
89145132Sanholt		return 1;
90145132Sanholt
91145132Sanholt	for (i = 0; i < DRM_SYSCTL_ENTRIES; i++) {
92145132Sanholt		oid = SYSCTL_ADD_OID(&info->ctx,
93145132Sanholt			SYSCTL_CHILDREN(top),
94145132Sanholt			OID_AUTO,
95145132Sanholt			drm_sysctl_list[i].name,
96145132Sanholt			CTLTYPE_INT | CTLFLAG_RD,
97145132Sanholt			dev,
98145132Sanholt			0,
99145132Sanholt			drm_sysctl_list[i].f,
100145132Sanholt			"A",
101145132Sanholt			NULL);
102145132Sanholt		if (!oid)
103145132Sanholt			return 1;
104145132Sanholt	}
105145132Sanholt	SYSCTL_ADD_INT(&info->ctx, SYSCTL_CHILDREN(top), OID_AUTO, "debug",
106145132Sanholt	    CTLFLAG_RW, &drm_debug_flag, sizeof(drm_debug_flag),
107145132Sanholt	    "Enable debugging output");
108145132Sanholt
109145132Sanholt	return 0;
110145132Sanholt}
111145132Sanholt
112182080Srnolandint drm_sysctl_cleanup(struct drm_device *dev)
113145132Sanholt{
114145132Sanholt	int error;
115145132Sanholt	error = sysctl_ctx_free( &dev->sysctl->ctx );
116145132Sanholt
117145132Sanholt	free(dev->sysctl, M_DRM);
118145132Sanholt	dev->sysctl = NULL;
119145132Sanholt
120145132Sanholt	return error;
121145132Sanholt}
122145132Sanholt
123145132Sanholt#define DRM_SYSCTL_PRINT(fmt, arg...)				\
124145132Sanholtdo {								\
125145132Sanholt	snprintf(buf, sizeof(buf), fmt, ##arg);			\
126145132Sanholt	retcode = SYSCTL_OUT(req, buf, strlen(buf));		\
127145132Sanholt	if (retcode)						\
128145132Sanholt		goto done;					\
129145132Sanholt} while (0)
130145132Sanholt
131145132Sanholtstatic int drm_name_info DRM_SYSCTL_HANDLER_ARGS
132145132Sanholt{
133182080Srnoland	struct drm_device *dev = arg1;
134145132Sanholt	char buf[128];
135145132Sanholt	int retcode;
136145132Sanholt	int hasunique = 0;
137145132Sanholt
138152909Sanholt	DRM_SYSCTL_PRINT("%s 0x%x", dev->driver.name, dev2udev(dev->devnode));
139145132Sanholt
140145132Sanholt	DRM_LOCK();
141145132Sanholt	if (dev->unique) {
142145132Sanholt		snprintf(buf, sizeof(buf), " %s", dev->unique);
143145132Sanholt		hasunique = 1;
144145132Sanholt	}
145145132Sanholt	DRM_UNLOCK();
146145132Sanholt
147145132Sanholt	if (hasunique)
148145132Sanholt		SYSCTL_OUT(req, buf, strlen(buf));
149145132Sanholt
150145132Sanholt	SYSCTL_OUT(req, "", 1);
151145132Sanholt
152145132Sanholtdone:
153145132Sanholt	return retcode;
154145132Sanholt}
155145132Sanholt
156145132Sanholtstatic int drm_vm_info DRM_SYSCTL_HANDLER_ARGS
157145132Sanholt{
158182080Srnoland	struct drm_device *dev = arg1;
159145132Sanholt	drm_local_map_t *map, *tempmaps;
160145132Sanholt	const char   *types[] = { "FB", "REG", "SHM", "AGP", "SG" };
161145132Sanholt	const char *type, *yesno;
162145132Sanholt	int i, mapcount;
163145132Sanholt	char buf[128];
164145132Sanholt	int retcode;
165145132Sanholt
166145132Sanholt	/* We can't hold the lock while doing SYSCTL_OUTs, so allocate a
167145132Sanholt	 * temporary copy of all the map entries and then SYSCTL_OUT that.
168145132Sanholt	 */
169145132Sanholt	DRM_LOCK();
170145132Sanholt
171145132Sanholt	mapcount = 0;
172145132Sanholt	TAILQ_FOREACH(map, &dev->maplist, link)
173145132Sanholt		mapcount++;
174145132Sanholt
175145132Sanholt	tempmaps = malloc(sizeof(drm_local_map_t) * mapcount, M_DRM, M_NOWAIT);
176145132Sanholt	if (tempmaps == NULL) {
177145132Sanholt		DRM_UNLOCK();
178145132Sanholt		return ENOMEM;
179145132Sanholt	}
180145132Sanholt
181145132Sanholt	i = 0;
182145132Sanholt	TAILQ_FOREACH(map, &dev->maplist, link)
183145132Sanholt		tempmaps[i++] = *map;
184145132Sanholt
185145132Sanholt	DRM_UNLOCK();
186145132Sanholt
187145132Sanholt	DRM_SYSCTL_PRINT("\nslot	 offset	      size type flags	 "
188145132Sanholt			 "address mtrr\n");
189145132Sanholt
190145132Sanholt	for (i = 0; i < mapcount; i++) {
191145132Sanholt		map = &tempmaps[i];
192145132Sanholt
193145132Sanholt		if (map->type < 0 || map->type > 4)
194145132Sanholt			type = "??";
195145132Sanholt		else
196145132Sanholt			type = types[map->type];
197145132Sanholt
198145132Sanholt		if (!map->mtrr)
199145132Sanholt			yesno = "no";
200145132Sanholt		else
201145132Sanholt			yesno = "yes";
202145132Sanholt
203145132Sanholt		DRM_SYSCTL_PRINT(
204145132Sanholt		    "%4d 0x%08lx 0x%08lx %4.4s  0x%02x 0x%08lx %s\n", i,
205145132Sanholt		    map->offset, map->size, type, map->flags,
206145132Sanholt		    (unsigned long)map->handle, yesno);
207145132Sanholt	}
208145132Sanholt	SYSCTL_OUT(req, "", 1);
209145132Sanholt
210145132Sanholtdone:
211145132Sanholt	free(tempmaps, M_DRM);
212145132Sanholt	return retcode;
213145132Sanholt}
214145132Sanholt
215145132Sanholtstatic int drm_bufs_info DRM_SYSCTL_HANDLER_ARGS
216145132Sanholt{
217182080Srnoland	struct drm_device	 *dev = arg1;
218145132Sanholt	drm_device_dma_t *dma = dev->dma;
219145132Sanholt	drm_device_dma_t tempdma;
220145132Sanholt	int *templists;
221145132Sanholt	int i;
222145132Sanholt	char buf[128];
223145132Sanholt	int retcode;
224145132Sanholt
225145132Sanholt	/* We can't hold the locks around DRM_SYSCTL_PRINT, so make a temporary
226145132Sanholt	 * copy of the whole structure and the relevant data from buflist.
227145132Sanholt	 */
228145132Sanholt	DRM_LOCK();
229145132Sanholt	if (dma == NULL) {
230145132Sanholt		DRM_UNLOCK();
231145132Sanholt		return 0;
232145132Sanholt	}
233145132Sanholt	DRM_SPINLOCK(&dev->dma_lock);
234145132Sanholt	tempdma = *dma;
235145132Sanholt	templists = malloc(sizeof(int) * dma->buf_count, M_DRM, M_NOWAIT);
236145132Sanholt	for (i = 0; i < dma->buf_count; i++)
237145132Sanholt		templists[i] = dma->buflist[i]->list;
238145132Sanholt	dma = &tempdma;
239145132Sanholt	DRM_SPINUNLOCK(&dev->dma_lock);
240145132Sanholt	DRM_UNLOCK();
241145132Sanholt
242145132Sanholt	DRM_SYSCTL_PRINT("\n o     size count  free	 segs pages    kB\n");
243145132Sanholt	for (i = 0; i <= DRM_MAX_ORDER; i++) {
244145132Sanholt		if (dma->bufs[i].buf_count)
245145132Sanholt			DRM_SYSCTL_PRINT("%2d %8d %5d %5d %5d %5d %5d\n",
246145132Sanholt				       i,
247145132Sanholt				       dma->bufs[i].buf_size,
248145132Sanholt				       dma->bufs[i].buf_count,
249145132Sanholt				       atomic_read(&dma->bufs[i]
250145132Sanholt						   .freelist.count),
251145132Sanholt				       dma->bufs[i].seg_count,
252145132Sanholt				       dma->bufs[i].seg_count
253145132Sanholt				       *(1 << dma->bufs[i].page_order),
254145132Sanholt				       (dma->bufs[i].seg_count
255145132Sanholt					* (1 << dma->bufs[i].page_order))
256145132Sanholt				       * PAGE_SIZE / 1024);
257145132Sanholt	}
258145132Sanholt	DRM_SYSCTL_PRINT("\n");
259145132Sanholt	for (i = 0; i < dma->buf_count; i++) {
260145132Sanholt		if (i && !(i%32)) DRM_SYSCTL_PRINT("\n");
261145132Sanholt		DRM_SYSCTL_PRINT(" %d", templists[i]);
262145132Sanholt	}
263145132Sanholt	DRM_SYSCTL_PRINT("\n");
264145132Sanholt
265145132Sanholt	SYSCTL_OUT(req, "", 1);
266145132Sanholtdone:
267145132Sanholt	free(templists, M_DRM);
268145132Sanholt	return retcode;
269145132Sanholt}
270145132Sanholt
271145132Sanholtstatic int drm_clients_info DRM_SYSCTL_HANDLER_ARGS
272145132Sanholt{
273182080Srnoland	struct drm_device *dev = arg1;
274145132Sanholt	drm_file_t *priv, *tempprivs;
275145132Sanholt	char buf[128];
276145132Sanholt	int retcode;
277145132Sanholt	int privcount, i;
278145132Sanholt
279145132Sanholt	DRM_LOCK();
280145132Sanholt
281145132Sanholt	privcount = 0;
282145132Sanholt	TAILQ_FOREACH(priv, &dev->files, link)
283145132Sanholt		privcount++;
284145132Sanholt
285145132Sanholt	tempprivs = malloc(sizeof(drm_file_t) * privcount, M_DRM, M_NOWAIT);
286145132Sanholt	if (tempprivs == NULL) {
287145132Sanholt		DRM_UNLOCK();
288145132Sanholt		return ENOMEM;
289145132Sanholt	}
290145132Sanholt	i = 0;
291145132Sanholt	TAILQ_FOREACH(priv, &dev->files, link)
292145132Sanholt		tempprivs[i++] = *priv;
293145132Sanholt
294145132Sanholt	DRM_UNLOCK();
295145132Sanholt
296145132Sanholt	DRM_SYSCTL_PRINT("\na dev	pid    uid	magic	  ioctls\n");
297145132Sanholt	for (i = 0; i < privcount; i++) {
298145132Sanholt		priv = &tempprivs[i];
299145132Sanholt		DRM_SYSCTL_PRINT("%c %3d %5d %5d %10u %10lu\n",
300145132Sanholt			       priv->authenticated ? 'y' : 'n',
301145132Sanholt			       priv->minor,
302145132Sanholt			       priv->pid,
303145132Sanholt			       priv->uid,
304145132Sanholt			       priv->magic,
305145132Sanholt			       priv->ioctl_count);
306145132Sanholt	}
307145132Sanholt
308145132Sanholt	SYSCTL_OUT(req, "", 1);
309145132Sanholtdone:
310145132Sanholt	free(tempprivs, M_DRM);
311145132Sanholt	return retcode;
312145132Sanholt}
313