drm: Use an include directory hierarchy similar to the Linux one
[dragonfly.git] / sys / dev / drm / drm_sysctl.c
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  * $FreeBSD: src/sys/dev/drm2/drm_sysctl.c,v 1.1 2012/05/22 11:07:44 kib Exp $
24  */
25
26 /** @file drm_sysctl.c
27  * Implementation of various sysctls for controlling DRM behavior and reporting
28  * debug information.
29  */
30
31 #include <sys/conf.h>
32 #include <sys/sysctl.h>
33 #include <sys/types.h>
34
35 #include <drm/drmP.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 static int         drm_vblank_info DRM_SYSCTL_HANDLER_ARGS;
42
43 struct drm_sysctl_list {
44         const char *name;
45         int        (*f) DRM_SYSCTL_HANDLER_ARGS;
46 } drm_sysctl_list[] = {
47         {"name",    drm_name_info},
48         {"vm",      drm_vm_info},
49         {"clients", drm_clients_info},
50         {"bufs",    drm_bufs_info},
51         {"vblank",    drm_vblank_info},
52 };
53 #define DRM_SYSCTL_ENTRIES NELEM(drm_sysctl_list)
54
55 struct drm_sysctl_info {
56         struct sysctl_ctx_list ctx;
57         char                   name[2];
58 };
59
60 int drm_sysctl_init(struct drm_device *dev)
61 {
62         struct drm_sysctl_info *info;
63         struct sysctl_oid *oid;
64         struct sysctl_oid *top, *drioid;
65         int               i;
66
67         info = kmalloc(sizeof *info, DRM_MEM_DRIVER, M_WAITOK | M_ZERO);
68         if ( !info )
69                 return 1;
70         dev->sysctl = info;
71
72         /* Add the sysctl node for DRI if it doesn't already exist */
73         drioid = SYSCTL_ADD_NODE(&info->ctx, &sysctl__hw_children, OID_AUTO,
74             "dri", CTLFLAG_RW, NULL, "DRI Graphics");
75         if (!drioid)
76                 return 1;
77
78         /* Find the next free slot under hw.dri */
79         i = 0;
80         SLIST_FOREACH(oid, SYSCTL_CHILDREN(drioid), oid_link) {
81                 if (i <= oid->oid_arg2)
82                         i = oid->oid_arg2 + 1;
83         }
84         if (i>9)
85                 return 1;
86         
87         dev->sysctl_node_idx = i;
88         /* Add the hw.dri.x for our device */
89         info->name[0] = '0' + i;
90         info->name[1] = 0;
91         top = SYSCTL_ADD_NODE(&info->ctx, SYSCTL_CHILDREN(drioid),
92             OID_AUTO, info->name, CTLFLAG_RW, NULL, NULL);
93         if (!top)
94                 return 1;
95         
96         for (i = 0; i < DRM_SYSCTL_ENTRIES; i++) {
97                 oid = SYSCTL_ADD_OID(&info->ctx,
98                         SYSCTL_CHILDREN(top),
99                         OID_AUTO,
100                         drm_sysctl_list[i].name,
101                         CTLTYPE_STRING | CTLFLAG_RD,
102                         dev,
103                         0,
104                         drm_sysctl_list[i].f,
105                         "A",
106                         NULL);
107                 if (!oid)
108                         return 1;
109         }
110         SYSCTL_ADD_INT(&info->ctx, SYSCTL_CHILDREN(drioid), OID_AUTO, "debug",
111             CTLFLAG_RW, &drm_debug_flag, sizeof(drm_debug_flag),
112             "Enable debugging output");
113         SYSCTL_ADD_INT(&info->ctx, SYSCTL_CHILDREN(drioid), OID_AUTO, "notyet",
114             CTLFLAG_RW, &drm_notyet_flag, sizeof(drm_debug_flag),
115             "Enable notyet reminders");
116
117         if (dev->driver->sysctl_init != NULL)
118                 dev->driver->sysctl_init(dev, &info->ctx, top);
119
120         SYSCTL_ADD_INT(&info->ctx, SYSCTL_CHILDREN(drioid), OID_AUTO,
121             "vblank_offdelay", CTLFLAG_RW, &drm_vblank_offdelay,
122             sizeof(drm_vblank_offdelay),
123             "");
124         SYSCTL_ADD_INT(&info->ctx, SYSCTL_CHILDREN(drioid), OID_AUTO,
125             "timestamp_precision", CTLFLAG_RW, &drm_timestamp_precision,
126             sizeof(drm_timestamp_precision),
127             "");
128
129         return (0);
130 }
131
132 int drm_sysctl_cleanup(struct drm_device *dev)
133 {
134         int error;
135
136         error = sysctl_ctx_free(&dev->sysctl->ctx);
137         drm_free(dev->sysctl, DRM_MEM_DRIVER);
138         dev->sysctl = NULL;
139         if (dev->driver->sysctl_cleanup != NULL)
140                 dev->driver->sysctl_cleanup(dev);
141
142         return (error);
143 }
144
145 #define DRM_SYSCTL_PRINT(fmt, arg...)                           \
146 do {                                                            \
147         ksnprintf(buf, sizeof(buf), fmt, ##arg);                        \
148         retcode = SYSCTL_OUT(req, buf, strlen(buf));            \
149         if (retcode)                                            \
150                 goto done;                                      \
151 } while (0)
152
153 static int drm_name_info DRM_SYSCTL_HANDLER_ARGS
154 {
155         struct drm_device *dev = arg1;
156         char buf[128];
157         int retcode;
158         int hasunique = 0;
159
160         DRM_SYSCTL_PRINT("%s 0x%x", dev->driver->name, dev2udev(dev->devnode));
161         
162         DRM_LOCK(dev);
163         if (dev->unique) {
164                 ksnprintf(buf, sizeof(buf), " %s", dev->unique);
165                 hasunique = 1;
166         }
167         DRM_UNLOCK(dev);
168         
169         if (hasunique)
170                 SYSCTL_OUT(req, buf, strlen(buf));
171
172         SYSCTL_OUT(req, "", 1);
173
174 done:
175         return retcode;
176 }
177
178 static int drm_vm_info DRM_SYSCTL_HANDLER_ARGS
179 {
180         struct drm_device *dev = arg1;
181         drm_local_map_t *map, *tempmaps;
182         const char *types[] = {
183                 [_DRM_FRAME_BUFFER] = "FB",
184                 [_DRM_REGISTERS] = "REG",
185                 [_DRM_SHM] = "SHM",
186                 [_DRM_AGP] = "AGP",
187                 [_DRM_SCATTER_GATHER] = "SG",
188                 [_DRM_CONSISTENT] = "CONS",
189                 [_DRM_GEM] = "GEM"
190         };
191         const char *type, *yesno;
192         int i, mapcount;
193         char buf[128];
194         int retcode;
195
196         /* We can't hold the lock while doing SYSCTL_OUTs, so allocate a
197          * temporary copy of all the map entries and then SYSCTL_OUT that.
198          */
199         DRM_LOCK(dev);
200
201         mapcount = 0;
202         TAILQ_FOREACH(map, &dev->maplist, link)
203                 mapcount++;
204
205         tempmaps = kmalloc(sizeof(drm_local_map_t) * mapcount, DRM_MEM_DRIVER,
206             M_NOWAIT);
207         if (tempmaps == NULL) {
208                 DRM_UNLOCK(dev);
209                 return ENOMEM;
210         }
211
212         i = 0;
213         TAILQ_FOREACH(map, &dev->maplist, link)
214                 tempmaps[i++] = *map;
215
216         DRM_UNLOCK(dev);
217
218         DRM_SYSCTL_PRINT("\nslot offset         size       "
219             "type flags address            handle mtrr\n");
220
221         for (i = 0; i < mapcount; i++) {
222                 map = &tempmaps[i];
223
224                 switch(map->type) {
225                 default:
226                         type = "??";
227                         break;
228                 case _DRM_FRAME_BUFFER:
229                 case _DRM_REGISTERS:
230                 case _DRM_SHM:
231                 case _DRM_AGP:
232                 case _DRM_SCATTER_GATHER:
233                 case _DRM_CONSISTENT:
234                 case _DRM_GEM:
235                         type = types[map->type];
236                         break;
237                 }
238
239                 if (!map->mtrr)
240                         yesno = "no";
241                 else
242                         yesno = "yes";
243
244                 DRM_SYSCTL_PRINT(
245                     "%4d 0x%016lx 0x%08lx %4.4s  0x%02x 0x%016lx %6d %s\n",
246                     i, map->offset, map->size, type, map->flags,
247                     (unsigned long)map->virtual,
248                     (unsigned int)((unsigned long)map->handle >>
249                     DRM_MAP_HANDLE_SHIFT), yesno);
250         }
251         SYSCTL_OUT(req, "", 1);
252
253 done:
254         drm_free(tempmaps, DRM_MEM_DRIVER);
255         return retcode;
256 }
257
258 static int drm_bufs_info DRM_SYSCTL_HANDLER_ARGS
259 {
260         struct drm_device        *dev = arg1;
261         drm_device_dma_t *dma = dev->dma;
262         drm_device_dma_t tempdma;
263         int *templists;
264         int i;
265         char buf[128];
266         int retcode;
267
268         /* We can't hold the locks around DRM_SYSCTL_PRINT, so make a temporary
269          * copy of the whole structure and the relevant data from buflist.
270          */
271         DRM_LOCK(dev);
272         if (dma == NULL) {
273                 DRM_UNLOCK(dev);
274                 return 0;
275         }
276         spin_lock(&dev->dma_lock);
277         tempdma = *dma;
278         templists = kmalloc(sizeof(int) * dma->buf_count, DRM_MEM_DRIVER,
279             M_NOWAIT);
280         for (i = 0; i < dma->buf_count; i++)
281                 templists[i] = dma->buflist[i]->list;
282         dma = &tempdma;
283         spin_unlock(&dev->dma_lock);
284         DRM_UNLOCK(dev);
285
286         DRM_SYSCTL_PRINT("\n o     size count  free      segs pages    kB\n");
287         for (i = 0; i <= DRM_MAX_ORDER; i++) {
288                 if (dma->bufs[i].buf_count)
289                         DRM_SYSCTL_PRINT("%2d %8d %5d %5d %5d %5d %5d\n",
290                                        i,
291                                        dma->bufs[i].buf_size,
292                                        dma->bufs[i].buf_count,
293                                        atomic_read(&dma->bufs[i]
294                                                    .freelist.count),
295                                        dma->bufs[i].seg_count,
296                                        dma->bufs[i].seg_count
297                                        *(1 << dma->bufs[i].page_order),
298                                        (dma->bufs[i].seg_count
299                                         * (1 << dma->bufs[i].page_order))
300                                        * (int)PAGE_SIZE / 1024);
301         }
302         DRM_SYSCTL_PRINT("\n");
303         for (i = 0; i < dma->buf_count; i++) {
304                 if (i && !(i%32)) DRM_SYSCTL_PRINT("\n");
305                 DRM_SYSCTL_PRINT(" %d", templists[i]);
306         }
307         DRM_SYSCTL_PRINT("\n");
308
309         SYSCTL_OUT(req, "", 1);
310 done:
311         drm_free(templists, DRM_MEM_DRIVER);
312         return retcode;
313 }
314
315 static int drm_clients_info DRM_SYSCTL_HANDLER_ARGS
316 {
317         struct drm_device *dev = arg1;
318         struct drm_file *priv, *tempprivs;
319         char buf[128];
320         int retcode;
321         int privcount, i;
322
323         DRM_LOCK(dev);
324
325         privcount = 0;
326         TAILQ_FOREACH(priv, &dev->files, link)
327                 privcount++;
328
329         tempprivs = kmalloc(sizeof(struct drm_file) * privcount, DRM_MEM_DRIVER,
330             M_NOWAIT);
331         if (tempprivs == NULL) {
332                 DRM_UNLOCK(dev);
333                 return ENOMEM;
334         }
335         i = 0;
336         TAILQ_FOREACH(priv, &dev->files, link)
337                 tempprivs[i++] = *priv;
338
339         DRM_UNLOCK(dev);
340
341         DRM_SYSCTL_PRINT(
342             "\na dev            pid   uid      magic     ioctls\n");
343         for (i = 0; i < privcount; i++) {
344                 priv = &tempprivs[i];
345                 DRM_SYSCTL_PRINT("%c %-12s %5d %5d %10u %10lu\n",
346                                priv->authenticated ? 'y' : 'n',
347                                devtoname(priv->dev->devnode),
348                                priv->pid,
349                                priv->uid,
350                                priv->magic,
351                                priv->ioctl_count);
352         }
353
354         SYSCTL_OUT(req, "", 1);
355 done:
356         drm_free(tempprivs, DRM_MEM_DRIVER);
357         return retcode;
358 }
359
360 static int drm_vblank_info DRM_SYSCTL_HANDLER_ARGS
361 {
362         struct drm_device *dev = arg1;
363         char buf[128];
364         int retcode;
365         int i;
366
367         DRM_SYSCTL_PRINT("\ncrtc ref count    last     enabled inmodeset\n");
368         DRM_LOCK(dev);
369         if (dev->_vblank_count == NULL)
370                 goto done;
371         for (i = 0 ; i < dev->num_crtcs ; i++) {
372                 DRM_SYSCTL_PRINT("  %02d  %02d %08d %08d %02d      %02d\n",
373                     i, dev->vblank_refcount[i],
374                     dev->_vblank_count[i],
375                     dev->last_vblank[i],
376                     dev->vblank_enabled[i],
377                     dev->vblank_inmodeset[i]);
378         }
379 done:
380         DRM_UNLOCK(dev);
381
382         SYSCTL_OUT(req, "", -1);
383         return retcode;
384 }