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$");
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;
41194759Srnolandstatic int	   drm_vblank_info DRM_SYSCTL_HANDLER_ARGS;
42145132Sanholt
43145132Sanholtstruct drm_sysctl_list {
44145132Sanholt	const char *name;
45145132Sanholt	int	   (*f) DRM_SYSCTL_HANDLER_ARGS;
46145132Sanholt} drm_sysctl_list[] = {
47145132Sanholt	{"name",    drm_name_info},
48145132Sanholt	{"vm",	    drm_vm_info},
49145132Sanholt	{"clients", drm_clients_info},
50145132Sanholt	{"bufs",    drm_bufs_info},
51194759Srnoland	{"vblank",    drm_vblank_info},
52145132Sanholt};
53145132Sanholt#define DRM_SYSCTL_ENTRIES (sizeof(drm_sysctl_list)/sizeof(drm_sysctl_list[0]))
54145132Sanholt
55145132Sanholtstruct drm_sysctl_info {
56145132Sanholt	struct sysctl_ctx_list ctx;
57145132Sanholt	char		       name[2];
58145132Sanholt};
59145132Sanholt
60182080Srnolandint drm_sysctl_init(struct drm_device *dev)
61145132Sanholt{
62145132Sanholt	struct drm_sysctl_info *info;
63145132Sanholt	struct sysctl_oid *oid;
64145132Sanholt	struct sysctl_oid *top, *drioid;
65145132Sanholt	int		  i;
66145132Sanholt
67183833Srnoland	info = malloc(sizeof *info, DRM_MEM_DRIVER, M_WAITOK | M_ZERO);
68145132Sanholt	if ( !info )
69145132Sanholt		return 1;
70145132Sanholt	dev->sysctl = info;
71145132Sanholt
72145132Sanholt	/* Add the sysctl node for DRI if it doesn't already exist */
73145132Sanholt	drioid = SYSCTL_ADD_NODE( &info->ctx, &sysctl__hw_children, OID_AUTO, "dri", CTLFLAG_RW, NULL, "DRI Graphics");
74145132Sanholt	if (!drioid)
75145132Sanholt		return 1;
76145132Sanholt
77145132Sanholt	/* Find the next free slot under hw.dri */
78145132Sanholt	i = 0;
79145132Sanholt	SLIST_FOREACH(oid, SYSCTL_CHILDREN(drioid), oid_link) {
80145132Sanholt		if (i <= oid->oid_arg2)
81145132Sanholt			i = oid->oid_arg2 + 1;
82145132Sanholt	}
83145132Sanholt	if (i>9)
84145132Sanholt		return 1;
85145132Sanholt
86145132Sanholt	/* Add the hw.dri.x for our device */
87145132Sanholt	info->name[0] = '0' + i;
88145132Sanholt	info->name[1] = 0;
89145132Sanholt	top = SYSCTL_ADD_NODE( &info->ctx, SYSCTL_CHILDREN(drioid), OID_AUTO, info->name, CTLFLAG_RW, NULL, NULL);
90145132Sanholt	if (!top)
91145132Sanholt		return 1;
92145132Sanholt
93145132Sanholt	for (i = 0; i < DRM_SYSCTL_ENTRIES; i++) {
94145132Sanholt		oid = SYSCTL_ADD_OID(&info->ctx,
95145132Sanholt			SYSCTL_CHILDREN(top),
96145132Sanholt			OID_AUTO,
97145132Sanholt			drm_sysctl_list[i].name,
98220979Skib			CTLTYPE_STRING | CTLFLAG_RD,
99145132Sanholt			dev,
100145132Sanholt			0,
101145132Sanholt			drm_sysctl_list[i].f,
102145132Sanholt			"A",
103145132Sanholt			NULL);
104145132Sanholt		if (!oid)
105145132Sanholt			return 1;
106145132Sanholt	}
107145132Sanholt	SYSCTL_ADD_INT(&info->ctx, SYSCTL_CHILDREN(top), OID_AUTO, "debug",
108145132Sanholt	    CTLFLAG_RW, &drm_debug_flag, sizeof(drm_debug_flag),
109145132Sanholt	    "Enable debugging output");
110145132Sanholt
111145132Sanholt	return 0;
112145132Sanholt}
113145132Sanholt
114182080Srnolandint drm_sysctl_cleanup(struct drm_device *dev)
115145132Sanholt{
116145132Sanholt	int error;
117145132Sanholt	error = sysctl_ctx_free( &dev->sysctl->ctx );
118145132Sanholt
119183833Srnoland	free(dev->sysctl, DRM_MEM_DRIVER);
120145132Sanholt	dev->sysctl = NULL;
121145132Sanholt
122145132Sanholt	return error;
123145132Sanholt}
124145132Sanholt
125145132Sanholt#define DRM_SYSCTL_PRINT(fmt, arg...)				\
126145132Sanholtdo {								\
127145132Sanholt	snprintf(buf, sizeof(buf), fmt, ##arg);			\
128145132Sanholt	retcode = SYSCTL_OUT(req, buf, strlen(buf));		\
129145132Sanholt	if (retcode)						\
130145132Sanholt		goto done;					\
131145132Sanholt} while (0)
132145132Sanholt
133145132Sanholtstatic int drm_name_info DRM_SYSCTL_HANDLER_ARGS
134145132Sanholt{
135182080Srnoland	struct drm_device *dev = arg1;
136145132Sanholt	char buf[128];
137145132Sanholt	int retcode;
138145132Sanholt	int hasunique = 0;
139145132Sanholt
140183573Srnoland	DRM_SYSCTL_PRINT("%s 0x%x", dev->driver->name, dev2udev(dev->devnode));
141145132Sanholt
142145132Sanholt	DRM_LOCK();
143145132Sanholt	if (dev->unique) {
144145132Sanholt		snprintf(buf, sizeof(buf), " %s", dev->unique);
145145132Sanholt		hasunique = 1;
146145132Sanholt	}
147145132Sanholt	DRM_UNLOCK();
148145132Sanholt
149145132Sanholt	if (hasunique)
150145132Sanholt		SYSCTL_OUT(req, buf, strlen(buf));
151145132Sanholt
152145132Sanholt	SYSCTL_OUT(req, "", 1);
153145132Sanholt
154145132Sanholtdone:
155145132Sanholt	return retcode;
156145132Sanholt}
157145132Sanholt
158145132Sanholtstatic int drm_vm_info DRM_SYSCTL_HANDLER_ARGS
159145132Sanholt{
160182080Srnoland	struct drm_device *dev = arg1;
161145132Sanholt	drm_local_map_t *map, *tempmaps;
162145132Sanholt	const char   *types[] = { "FB", "REG", "SHM", "AGP", "SG" };
163145132Sanholt	const char *type, *yesno;
164145132Sanholt	int i, mapcount;
165145132Sanholt	char buf[128];
166145132Sanholt	int retcode;
167145132Sanholt
168145132Sanholt	/* We can't hold the lock while doing SYSCTL_OUTs, so allocate a
169145132Sanholt	 * temporary copy of all the map entries and then SYSCTL_OUT that.
170145132Sanholt	 */
171145132Sanholt	DRM_LOCK();
172145132Sanholt
173145132Sanholt	mapcount = 0;
174145132Sanholt	TAILQ_FOREACH(map, &dev->maplist, link)
175145132Sanholt		mapcount++;
176145132Sanholt
177183833Srnoland	tempmaps = malloc(sizeof(drm_local_map_t) * mapcount, DRM_MEM_DRIVER,
178183833Srnoland	    M_NOWAIT);
179145132Sanholt	if (tempmaps == NULL) {
180145132Sanholt		DRM_UNLOCK();
181145132Sanholt		return ENOMEM;
182145132Sanholt	}
183145132Sanholt
184145132Sanholt	i = 0;
185145132Sanholt	TAILQ_FOREACH(map, &dev->maplist, link)
186145132Sanholt		tempmaps[i++] = *map;
187145132Sanholt
188145132Sanholt	DRM_UNLOCK();
189145132Sanholt
190189562Srnoland	DRM_SYSCTL_PRINT("\nslot offset	        size       "
191207066Srnoland	    "type flags address            handle mtrr\n");
192145132Sanholt
193145132Sanholt	for (i = 0; i < mapcount; i++) {
194145132Sanholt		map = &tempmaps[i];
195145132Sanholt
196145132Sanholt		if (map->type < 0 || map->type > 4)
197145132Sanholt			type = "??";
198145132Sanholt		else
199145132Sanholt			type = types[map->type];
200145132Sanholt
201145132Sanholt		if (!map->mtrr)
202145132Sanholt			yesno = "no";
203145132Sanholt		else
204145132Sanholt			yesno = "yes";
205145132Sanholt
206145132Sanholt		DRM_SYSCTL_PRINT(
207207066Srnoland		    "%4d 0x%016lx 0x%08lx %4.4s  0x%02x 0x%016lx %6d %s\n",
208207066Srnoland		    i, map->offset, map->size, type, map->flags,
209207066Srnoland		    (unsigned long)map->virtual,
210207066Srnoland		    (unsigned int)((unsigned long)map->handle >>
211207066Srnoland		    DRM_MAP_HANDLE_SHIFT), yesno);
212145132Sanholt	}
213145132Sanholt	SYSCTL_OUT(req, "", 1);
214145132Sanholt
215145132Sanholtdone:
216183833Srnoland	free(tempmaps, DRM_MEM_DRIVER);
217145132Sanholt	return retcode;
218145132Sanholt}
219145132Sanholt
220145132Sanholtstatic int drm_bufs_info DRM_SYSCTL_HANDLER_ARGS
221145132Sanholt{
222182080Srnoland	struct drm_device	 *dev = arg1;
223145132Sanholt	drm_device_dma_t *dma = dev->dma;
224145132Sanholt	drm_device_dma_t tempdma;
225145132Sanholt	int *templists;
226145132Sanholt	int i;
227145132Sanholt	char buf[128];
228145132Sanholt	int retcode;
229145132Sanholt
230145132Sanholt	/* We can't hold the locks around DRM_SYSCTL_PRINT, so make a temporary
231145132Sanholt	 * copy of the whole structure and the relevant data from buflist.
232145132Sanholt	 */
233145132Sanholt	DRM_LOCK();
234145132Sanholt	if (dma == NULL) {
235145132Sanholt		DRM_UNLOCK();
236145132Sanholt		return 0;
237145132Sanholt	}
238145132Sanholt	DRM_SPINLOCK(&dev->dma_lock);
239145132Sanholt	tempdma = *dma;
240183833Srnoland	templists = malloc(sizeof(int) * dma->buf_count, DRM_MEM_DRIVER,
241183833Srnoland	    M_NOWAIT);
242145132Sanholt	for (i = 0; i < dma->buf_count; i++)
243145132Sanholt		templists[i] = dma->buflist[i]->list;
244145132Sanholt	dma = &tempdma;
245145132Sanholt	DRM_SPINUNLOCK(&dev->dma_lock);
246145132Sanholt	DRM_UNLOCK();
247145132Sanholt
248145132Sanholt	DRM_SYSCTL_PRINT("\n o     size count  free	 segs pages    kB\n");
249145132Sanholt	for (i = 0; i <= DRM_MAX_ORDER; i++) {
250145132Sanholt		if (dma->bufs[i].buf_count)
251145132Sanholt			DRM_SYSCTL_PRINT("%2d %8d %5d %5d %5d %5d %5d\n",
252145132Sanholt				       i,
253145132Sanholt				       dma->bufs[i].buf_size,
254145132Sanholt				       dma->bufs[i].buf_count,
255145132Sanholt				       atomic_read(&dma->bufs[i]
256145132Sanholt						   .freelist.count),
257145132Sanholt				       dma->bufs[i].seg_count,
258145132Sanholt				       dma->bufs[i].seg_count
259145132Sanholt				       *(1 << dma->bufs[i].page_order),
260145132Sanholt				       (dma->bufs[i].seg_count
261145132Sanholt					* (1 << dma->bufs[i].page_order))
262215367Snwhitehorn				       * (int)PAGE_SIZE / 1024);
263145132Sanholt	}
264145132Sanholt	DRM_SYSCTL_PRINT("\n");
265145132Sanholt	for (i = 0; i < dma->buf_count; i++) {
266145132Sanholt		if (i && !(i%32)) DRM_SYSCTL_PRINT("\n");
267145132Sanholt		DRM_SYSCTL_PRINT(" %d", templists[i]);
268145132Sanholt	}
269145132Sanholt	DRM_SYSCTL_PRINT("\n");
270145132Sanholt
271145132Sanholt	SYSCTL_OUT(req, "", 1);
272145132Sanholtdone:
273183833Srnoland	free(templists, DRM_MEM_DRIVER);
274145132Sanholt	return retcode;
275145132Sanholt}
276145132Sanholt
277145132Sanholtstatic int drm_clients_info DRM_SYSCTL_HANDLER_ARGS
278145132Sanholt{
279182080Srnoland	struct drm_device *dev = arg1;
280183573Srnoland	struct drm_file *priv, *tempprivs;
281145132Sanholt	char buf[128];
282145132Sanholt	int retcode;
283145132Sanholt	int privcount, i;
284145132Sanholt
285145132Sanholt	DRM_LOCK();
286145132Sanholt
287145132Sanholt	privcount = 0;
288145132Sanholt	TAILQ_FOREACH(priv, &dev->files, link)
289145132Sanholt		privcount++;
290145132Sanholt
291183833Srnoland	tempprivs = malloc(sizeof(struct drm_file) * privcount, DRM_MEM_DRIVER,
292183833Srnoland	    M_NOWAIT);
293145132Sanholt	if (tempprivs == NULL) {
294145132Sanholt		DRM_UNLOCK();
295145132Sanholt		return ENOMEM;
296145132Sanholt	}
297145132Sanholt	i = 0;
298145132Sanholt	TAILQ_FOREACH(priv, &dev->files, link)
299145132Sanholt		tempprivs[i++] = *priv;
300145132Sanholt
301145132Sanholt	DRM_UNLOCK();
302145132Sanholt
303196465Srnoland	DRM_SYSCTL_PRINT(
304196465Srnoland	    "\na dev            pid   uid      magic     ioctls\n");
305145132Sanholt	for (i = 0; i < privcount; i++) {
306145132Sanholt		priv = &tempprivs[i];
307196465Srnoland		DRM_SYSCTL_PRINT("%c %-12s %5d %5d %10u %10lu\n",
308145132Sanholt			       priv->authenticated ? 'y' : 'n',
309196465Srnoland			       devtoname(priv->dev->devnode),
310145132Sanholt			       priv->pid,
311145132Sanholt			       priv->uid,
312145132Sanholt			       priv->magic,
313145132Sanholt			       priv->ioctl_count);
314145132Sanholt	}
315145132Sanholt
316145132Sanholt	SYSCTL_OUT(req, "", 1);
317145132Sanholtdone:
318183833Srnoland	free(tempprivs, DRM_MEM_DRIVER);
319145132Sanholt	return retcode;
320145132Sanholt}
321194759Srnoland
322194759Srnolandstatic int drm_vblank_info DRM_SYSCTL_HANDLER_ARGS
323194759Srnoland{
324194759Srnoland	struct drm_device *dev = arg1;
325194759Srnoland	char buf[128];
326194759Srnoland	int retcode;
327194759Srnoland	int i;
328194759Srnoland
329194759Srnoland	DRM_SYSCTL_PRINT("\ncrtc ref count    last     enabled inmodeset\n");
330194759Srnoland	for(i = 0 ; i < dev->num_crtcs ; i++) {
331194759Srnoland		DRM_SYSCTL_PRINT("  %02d  %02d %08d %08d %02d      %02d\n",
332194759Srnoland		    i, atomic_load_acq_32(&dev->vblank[i].refcount),
333194759Srnoland		    atomic_load_acq_32(&dev->vblank[i].count),
334194759Srnoland		    atomic_load_acq_32(&dev->vblank[i].last),
335194759Srnoland		    atomic_load_acq_int(&dev->vblank[i].enabled),
336194759Srnoland		    atomic_load_acq_int(&dev->vblank[i].inmodeset));
337194759Srnoland	}
338194759Srnoland
339194759Srnoland	SYSCTL_OUT(req, "", -1);
340194759Srnolanddone:
341194759Srnoland	return retcode;
342194759Srnoland}
343