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 */
73267992Shselasky	drioid = SYSCTL_ADD_NODE(&info->ctx, SYSCTL_CHILDREN(&sysctl___hw), 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
140275856Sgleb	DRM_SYSCTL_PRINT("%s 0x%jx", dev->driver->name,
141275856Sgleb	    (uintmax_t)dev2udev(dev->devnode));
142275856Sgleb
143145132Sanholt	DRM_LOCK();
144145132Sanholt	if (dev->unique) {
145145132Sanholt		snprintf(buf, sizeof(buf), " %s", dev->unique);
146145132Sanholt		hasunique = 1;
147145132Sanholt	}
148145132Sanholt	DRM_UNLOCK();
149145132Sanholt
150145132Sanholt	if (hasunique)
151145132Sanholt		SYSCTL_OUT(req, buf, strlen(buf));
152145132Sanholt
153145132Sanholt	SYSCTL_OUT(req, "", 1);
154145132Sanholt
155145132Sanholtdone:
156145132Sanholt	return retcode;
157145132Sanholt}
158145132Sanholt
159145132Sanholtstatic int drm_vm_info DRM_SYSCTL_HANDLER_ARGS
160145132Sanholt{
161182080Srnoland	struct drm_device *dev = arg1;
162145132Sanholt	drm_local_map_t *map, *tempmaps;
163145132Sanholt	const char   *types[] = { "FB", "REG", "SHM", "AGP", "SG" };
164145132Sanholt	const char *type, *yesno;
165145132Sanholt	int i, mapcount;
166145132Sanholt	char buf[128];
167145132Sanholt	int retcode;
168145132Sanholt
169145132Sanholt	/* We can't hold the lock while doing SYSCTL_OUTs, so allocate a
170145132Sanholt	 * temporary copy of all the map entries and then SYSCTL_OUT that.
171145132Sanholt	 */
172145132Sanholt	DRM_LOCK();
173145132Sanholt
174145132Sanholt	mapcount = 0;
175145132Sanholt	TAILQ_FOREACH(map, &dev->maplist, link)
176145132Sanholt		mapcount++;
177145132Sanholt
178183833Srnoland	tempmaps = malloc(sizeof(drm_local_map_t) * mapcount, DRM_MEM_DRIVER,
179183833Srnoland	    M_NOWAIT);
180145132Sanholt	if (tempmaps == NULL) {
181145132Sanholt		DRM_UNLOCK();
182145132Sanholt		return ENOMEM;
183145132Sanholt	}
184145132Sanholt
185145132Sanholt	i = 0;
186145132Sanholt	TAILQ_FOREACH(map, &dev->maplist, link)
187145132Sanholt		tempmaps[i++] = *map;
188145132Sanholt
189145132Sanholt	DRM_UNLOCK();
190145132Sanholt
191189562Srnoland	DRM_SYSCTL_PRINT("\nslot offset	        size       "
192207066Srnoland	    "type flags address            handle mtrr\n");
193145132Sanholt
194145132Sanholt	for (i = 0; i < mapcount; i++) {
195145132Sanholt		map = &tempmaps[i];
196145132Sanholt
197271013Sglebius		if (map->type > 4)
198145132Sanholt			type = "??";
199145132Sanholt		else
200145132Sanholt			type = types[map->type];
201145132Sanholt
202145132Sanholt		if (!map->mtrr)
203145132Sanholt			yesno = "no";
204145132Sanholt		else
205145132Sanholt			yesno = "yes";
206145132Sanholt
207145132Sanholt		DRM_SYSCTL_PRINT(
208207066Srnoland		    "%4d 0x%016lx 0x%08lx %4.4s  0x%02x 0x%016lx %6d %s\n",
209207066Srnoland		    i, map->offset, map->size, type, map->flags,
210207066Srnoland		    (unsigned long)map->virtual,
211207066Srnoland		    (unsigned int)((unsigned long)map->handle >>
212207066Srnoland		    DRM_MAP_HANDLE_SHIFT), yesno);
213145132Sanholt	}
214145132Sanholt	SYSCTL_OUT(req, "", 1);
215145132Sanholt
216145132Sanholtdone:
217183833Srnoland	free(tempmaps, DRM_MEM_DRIVER);
218145132Sanholt	return retcode;
219145132Sanholt}
220145132Sanholt
221145132Sanholtstatic int drm_bufs_info DRM_SYSCTL_HANDLER_ARGS
222145132Sanholt{
223182080Srnoland	struct drm_device	 *dev = arg1;
224145132Sanholt	drm_device_dma_t *dma = dev->dma;
225145132Sanholt	drm_device_dma_t tempdma;
226145132Sanholt	int *templists;
227145132Sanholt	int i;
228145132Sanholt	char buf[128];
229145132Sanholt	int retcode;
230145132Sanholt
231145132Sanholt	/* We can't hold the locks around DRM_SYSCTL_PRINT, so make a temporary
232145132Sanholt	 * copy of the whole structure and the relevant data from buflist.
233145132Sanholt	 */
234145132Sanholt	DRM_LOCK();
235145132Sanholt	if (dma == NULL) {
236145132Sanholt		DRM_UNLOCK();
237145132Sanholt		return 0;
238145132Sanholt	}
239145132Sanholt	DRM_SPINLOCK(&dev->dma_lock);
240145132Sanholt	tempdma = *dma;
241183833Srnoland	templists = malloc(sizeof(int) * dma->buf_count, DRM_MEM_DRIVER,
242183833Srnoland	    M_NOWAIT);
243145132Sanholt	for (i = 0; i < dma->buf_count; i++)
244145132Sanholt		templists[i] = dma->buflist[i]->list;
245145132Sanholt	dma = &tempdma;
246145132Sanholt	DRM_SPINUNLOCK(&dev->dma_lock);
247145132Sanholt	DRM_UNLOCK();
248145132Sanholt
249145132Sanholt	DRM_SYSCTL_PRINT("\n o     size count  free	 segs pages    kB\n");
250145132Sanholt	for (i = 0; i <= DRM_MAX_ORDER; i++) {
251145132Sanholt		if (dma->bufs[i].buf_count)
252145132Sanholt			DRM_SYSCTL_PRINT("%2d %8d %5d %5d %5d %5d %5d\n",
253145132Sanholt				       i,
254145132Sanholt				       dma->bufs[i].buf_size,
255145132Sanholt				       dma->bufs[i].buf_count,
256145132Sanholt				       atomic_read(&dma->bufs[i]
257145132Sanholt						   .freelist.count),
258145132Sanholt				       dma->bufs[i].seg_count,
259145132Sanholt				       dma->bufs[i].seg_count
260145132Sanholt				       *(1 << dma->bufs[i].page_order),
261145132Sanholt				       (dma->bufs[i].seg_count
262145132Sanholt					* (1 << dma->bufs[i].page_order))
263215367Snwhitehorn				       * (int)PAGE_SIZE / 1024);
264145132Sanholt	}
265145132Sanholt	DRM_SYSCTL_PRINT("\n");
266145132Sanholt	for (i = 0; i < dma->buf_count; i++) {
267145132Sanholt		if (i && !(i%32)) DRM_SYSCTL_PRINT("\n");
268145132Sanholt		DRM_SYSCTL_PRINT(" %d", templists[i]);
269145132Sanholt	}
270145132Sanholt	DRM_SYSCTL_PRINT("\n");
271145132Sanholt
272145132Sanholt	SYSCTL_OUT(req, "", 1);
273145132Sanholtdone:
274183833Srnoland	free(templists, DRM_MEM_DRIVER);
275145132Sanholt	return retcode;
276145132Sanholt}
277145132Sanholt
278145132Sanholtstatic int drm_clients_info DRM_SYSCTL_HANDLER_ARGS
279145132Sanholt{
280182080Srnoland	struct drm_device *dev = arg1;
281183573Srnoland	struct drm_file *priv, *tempprivs;
282145132Sanholt	char buf[128];
283145132Sanholt	int retcode;
284145132Sanholt	int privcount, i;
285145132Sanholt
286145132Sanholt	DRM_LOCK();
287145132Sanholt
288145132Sanholt	privcount = 0;
289145132Sanholt	TAILQ_FOREACH(priv, &dev->files, link)
290145132Sanholt		privcount++;
291145132Sanholt
292183833Srnoland	tempprivs = malloc(sizeof(struct drm_file) * privcount, DRM_MEM_DRIVER,
293183833Srnoland	    M_NOWAIT);
294145132Sanholt	if (tempprivs == NULL) {
295145132Sanholt		DRM_UNLOCK();
296145132Sanholt		return ENOMEM;
297145132Sanholt	}
298145132Sanholt	i = 0;
299145132Sanholt	TAILQ_FOREACH(priv, &dev->files, link)
300145132Sanholt		tempprivs[i++] = *priv;
301145132Sanholt
302145132Sanholt	DRM_UNLOCK();
303145132Sanholt
304196465Srnoland	DRM_SYSCTL_PRINT(
305196465Srnoland	    "\na dev            pid   uid      magic     ioctls\n");
306145132Sanholt	for (i = 0; i < privcount; i++) {
307145132Sanholt		priv = &tempprivs[i];
308196465Srnoland		DRM_SYSCTL_PRINT("%c %-12s %5d %5d %10u %10lu\n",
309145132Sanholt			       priv->authenticated ? 'y' : 'n',
310196465Srnoland			       devtoname(priv->dev->devnode),
311145132Sanholt			       priv->pid,
312145132Sanholt			       priv->uid,
313145132Sanholt			       priv->magic,
314145132Sanholt			       priv->ioctl_count);
315145132Sanholt	}
316145132Sanholt
317145132Sanholt	SYSCTL_OUT(req, "", 1);
318145132Sanholtdone:
319183833Srnoland	free(tempprivs, DRM_MEM_DRIVER);
320145132Sanholt	return retcode;
321145132Sanholt}
322194759Srnoland
323194759Srnolandstatic int drm_vblank_info DRM_SYSCTL_HANDLER_ARGS
324194759Srnoland{
325194759Srnoland	struct drm_device *dev = arg1;
326194759Srnoland	char buf[128];
327194759Srnoland	int retcode;
328194759Srnoland	int i;
329194759Srnoland
330194759Srnoland	DRM_SYSCTL_PRINT("\ncrtc ref count    last     enabled inmodeset\n");
331194759Srnoland	for(i = 0 ; i < dev->num_crtcs ; i++) {
332194759Srnoland		DRM_SYSCTL_PRINT("  %02d  %02d %08d %08d %02d      %02d\n",
333194759Srnoland		    i, atomic_load_acq_32(&dev->vblank[i].refcount),
334194759Srnoland		    atomic_load_acq_32(&dev->vblank[i].count),
335194759Srnoland		    atomic_load_acq_32(&dev->vblank[i].last),
336194759Srnoland		    atomic_load_acq_int(&dev->vblank[i].enabled),
337194759Srnoland		    atomic_load_acq_int(&dev->vblank[i].inmodeset));
338194759Srnoland	}
339194759Srnoland
340194759Srnoland	SYSCTL_OUT(req, "", -1);
341194759Srnolanddone:
342194759Srnoland	return retcode;
343194759Srnoland}
344