1207753Smm/*-
2207753Smm * Copyright 2003 Eric Anholt
3207753Smm * All Rights Reserved.
4207753Smm *
5207753Smm * Permission is hereby granted, free of charge, to any person obtaining a
6207753Smm * copy of this software and associated documentation files (the "Software"),
7207753Smm * to deal in the Software without restriction, including without limitation
8207753Smm * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9207753Smm * and/or sell copies of the Software, and to permit persons to whom the
10207753Smm * Software is furnished to do so, subject to the following conditions:
11207753Smm *
12207753Smm * The above copyright notice and this permission notice (including the next
13207753Smm * paragraph) shall be included in all copies or substantial portions of the
14207753Smm * Software.
15207753Smm *
16207753Smm * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17207753Smm * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18207753Smm * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19207753Smm * ERIC ANHOLT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20207753Smm * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21207753Smm * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22219001Smm */
23223935Smm
24207753Smm#include <sys/cdefs.h>
25207753Smm__FBSDID("$FreeBSD$");
26207753Smm
27223935Smm/** @file drm_sysctl.c
28207753Smm * Implementation of various sysctls for controlling DRM behavior and reporting
29207753Smm * debug information.
30207753Smm */
31207753Smm
32207753Smm#include "dev/drm/drmP.h"
33207753Smm#include "dev/drm/drm.h"
34207753Smm
35207753Smm#include <sys/sysctl.h>
36207753Smm
37207753Smmstatic int	   drm_name_info DRM_SYSCTL_HANDLER_ARGS;
38207753Smmstatic int	   drm_vm_info DRM_SYSCTL_HANDLER_ARGS;
39207753Smmstatic int	   drm_clients_info DRM_SYSCTL_HANDLER_ARGS;
40207753Smmstatic int	   drm_bufs_info DRM_SYSCTL_HANDLER_ARGS;
41207753Smmstatic int	   drm_vblank_info DRM_SYSCTL_HANDLER_ARGS;
42207753Smm
43207753Smmstruct drm_sysctl_list {
44207753Smm	const char *name;
45207753Smm	int	   (*f) DRM_SYSCTL_HANDLER_ARGS;
46207753Smm} drm_sysctl_list[] = {
47207753Smm	{"name",    drm_name_info},
48207753Smm	{"vm",	    drm_vm_info},
49207753Smm	{"clients", drm_clients_info},
50207753Smm	{"bufs",    drm_bufs_info},
51207753Smm	{"vblank",    drm_vblank_info},
52207753Smm};
53207753Smm#define DRM_SYSCTL_ENTRIES (sizeof(drm_sysctl_list)/sizeof(drm_sysctl_list[0]))
54207753Smm
55207753Smmstruct drm_sysctl_info {
56207753Smm	struct sysctl_ctx_list ctx;
57207753Smm	char		       name[2];
58207753Smm};
59207753Smm
60207753Smmint drm_sysctl_init(struct drm_device *dev)
61207753Smm{
62207753Smm	struct drm_sysctl_info *info;
63207753Smm	struct sysctl_oid *oid;
64207753Smm	struct sysctl_oid *top, *drioid;
65207753Smm	int		  i;
66207753Smm
67207753Smm	info = malloc(sizeof *info, DRM_MEM_DRIVER, M_WAITOK | M_ZERO);
68207753Smm	if ( !info )
69207753Smm		return 1;
70207753Smm	dev->sysctl = info;
71207753Smm
72207753Smm	/* Add the sysctl node for DRI if it doesn't already exist */
73207753Smm	drioid = SYSCTL_ADD_NODE( &info->ctx, &sysctl__hw_children, OID_AUTO, "dri", CTLFLAG_RW, NULL, "DRI Graphics");
74207753Smm	if (!drioid)
75207753Smm		return 1;
76207753Smm
77207753Smm	/* Find the next free slot under hw.dri */
78207753Smm	i = 0;
79207753Smm	SLIST_FOREACH(oid, SYSCTL_CHILDREN(drioid), oid_link) {
80207753Smm		if (i <= oid->oid_arg2)
81207753Smm			i = oid->oid_arg2 + 1;
82207753Smm	}
83207753Smm	if (i>9)
84207753Smm		return 1;
85207753Smm
86207753Smm	/* Add the hw.dri.x for our device */
87207753Smm	info->name[0] = '0' + i;
88207753Smm	info->name[1] = 0;
89207753Smm	top = SYSCTL_ADD_NODE( &info->ctx, SYSCTL_CHILDREN(drioid), OID_AUTO, info->name, CTLFLAG_RW, NULL, NULL);
90207753Smm	if (!top)
91207753Smm		return 1;
92207753Smm
93207753Smm	for (i = 0; i < DRM_SYSCTL_ENTRIES; i++) {
94207753Smm		oid = SYSCTL_ADD_OID(&info->ctx,
95207753Smm			SYSCTL_CHILDREN(top),
96207753Smm			OID_AUTO,
97207753Smm			drm_sysctl_list[i].name,
98207753Smm			CTLTYPE_STRING | CTLFLAG_RD,
99207753Smm			dev,
100207753Smm			0,
101207753Smm			drm_sysctl_list[i].f,
102207753Smm			"A",
103207753Smm			NULL);
104207753Smm		if (!oid)
105223935Smm			return 1;
106207753Smm	}
107207753Smm	SYSCTL_ADD_INT(&info->ctx, SYSCTL_CHILDREN(top), OID_AUTO, "debug",
108207753Smm	    CTLFLAG_RW, &drm_debug_flag, sizeof(drm_debug_flag),
109207753Smm	    "Enable debugging output");
110207753Smm
111207753Smm	return 0;
112207753Smm}
113207753Smm
114207753Smmint drm_sysctl_cleanup(struct drm_device *dev)
115207753Smm{
116207753Smm	int error;
117207753Smm	error = sysctl_ctx_free( &dev->sysctl->ctx );
118207753Smm
119207753Smm	free(dev->sysctl, DRM_MEM_DRIVER);
120207753Smm	dev->sysctl = NULL;
121207753Smm
122207753Smm	return error;
123207753Smm}
124
125#define DRM_SYSCTL_PRINT(fmt, arg...)				\
126do {								\
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
133static 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
154done:
155	return retcode;
156}
157
158static 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            handle 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 %6d %s\n",
208		    i, map->offset, map->size, type, map->flags,
209		    (unsigned long)map->virtual,
210		    (unsigned int)((unsigned long)map->handle >>
211		    DRM_MAP_HANDLE_SHIFT), yesno);
212	}
213	SYSCTL_OUT(req, "", 1);
214
215done:
216	free(tempmaps, DRM_MEM_DRIVER);
217	return retcode;
218}
219
220static int drm_bufs_info DRM_SYSCTL_HANDLER_ARGS
221{
222	struct drm_device	 *dev = arg1;
223	drm_device_dma_t *dma = dev->dma;
224	drm_device_dma_t tempdma;
225	int *templists;
226	int i;
227	char buf[128];
228	int retcode;
229
230	/* We can't hold the locks around DRM_SYSCTL_PRINT, so make a temporary
231	 * copy of the whole structure and the relevant data from buflist.
232	 */
233	DRM_LOCK();
234	if (dma == NULL) {
235		DRM_UNLOCK();
236		return 0;
237	}
238	DRM_SPINLOCK(&dev->dma_lock);
239	tempdma = *dma;
240	templists = malloc(sizeof(int) * dma->buf_count, DRM_MEM_DRIVER,
241	    M_NOWAIT);
242	for (i = 0; i < dma->buf_count; i++)
243		templists[i] = dma->buflist[i]->list;
244	dma = &tempdma;
245	DRM_SPINUNLOCK(&dev->dma_lock);
246	DRM_UNLOCK();
247
248	DRM_SYSCTL_PRINT("\n o     size count  free	 segs pages    kB\n");
249	for (i = 0; i <= DRM_MAX_ORDER; i++) {
250		if (dma->bufs[i].buf_count)
251			DRM_SYSCTL_PRINT("%2d %8d %5d %5d %5d %5d %5d\n",
252				       i,
253				       dma->bufs[i].buf_size,
254				       dma->bufs[i].buf_count,
255				       atomic_read(&dma->bufs[i]
256						   .freelist.count),
257				       dma->bufs[i].seg_count,
258				       dma->bufs[i].seg_count
259				       *(1 << dma->bufs[i].page_order),
260				       (dma->bufs[i].seg_count
261					* (1 << dma->bufs[i].page_order))
262				       * (int)PAGE_SIZE / 1024);
263	}
264	DRM_SYSCTL_PRINT("\n");
265	for (i = 0; i < dma->buf_count; i++) {
266		if (i && !(i%32)) DRM_SYSCTL_PRINT("\n");
267		DRM_SYSCTL_PRINT(" %d", templists[i]);
268	}
269	DRM_SYSCTL_PRINT("\n");
270
271	SYSCTL_OUT(req, "", 1);
272done:
273	free(templists, DRM_MEM_DRIVER);
274	return retcode;
275}
276
277static int drm_clients_info DRM_SYSCTL_HANDLER_ARGS
278{
279	struct drm_device *dev = arg1;
280	struct drm_file *priv, *tempprivs;
281	char buf[128];
282	int retcode;
283	int privcount, i;
284
285	DRM_LOCK();
286
287	privcount = 0;
288	TAILQ_FOREACH(priv, &dev->files, link)
289		privcount++;
290
291	tempprivs = malloc(sizeof(struct drm_file) * privcount, DRM_MEM_DRIVER,
292	    M_NOWAIT);
293	if (tempprivs == NULL) {
294		DRM_UNLOCK();
295		return ENOMEM;
296	}
297	i = 0;
298	TAILQ_FOREACH(priv, &dev->files, link)
299		tempprivs[i++] = *priv;
300
301	DRM_UNLOCK();
302
303	DRM_SYSCTL_PRINT(
304	    "\na dev            pid   uid      magic     ioctls\n");
305	for (i = 0; i < privcount; i++) {
306		priv = &tempprivs[i];
307		DRM_SYSCTL_PRINT("%c %-12s %5d %5d %10u %10lu\n",
308			       priv->authenticated ? 'y' : 'n',
309			       devtoname(priv->dev->devnode),
310			       priv->pid,
311			       priv->uid,
312			       priv->magic,
313			       priv->ioctl_count);
314	}
315
316	SYSCTL_OUT(req, "", 1);
317done:
318	free(tempprivs, DRM_MEM_DRIVER);
319	return retcode;
320}
321
322static int drm_vblank_info DRM_SYSCTL_HANDLER_ARGS
323{
324	struct drm_device *dev = arg1;
325	char buf[128];
326	int retcode;
327	int i;
328
329	DRM_SYSCTL_PRINT("\ncrtc ref count    last     enabled inmodeset\n");
330	for(i = 0 ; i < dev->num_crtcs ; i++) {
331		DRM_SYSCTL_PRINT("  %02d  %02d %08d %08d %02d      %02d\n",
332		    i, atomic_load_acq_32(&dev->vblank[i].refcount),
333		    atomic_load_acq_32(&dev->vblank[i].count),
334		    atomic_load_acq_32(&dev->vblank[i].last),
335		    atomic_load_acq_int(&dev->vblank[i].enabled),
336		    atomic_load_acq_int(&dev->vblank[i].inmodeset));
337	}
338
339	SYSCTL_OUT(req, "", -1);
340done:
341	return retcode;
342}
343