Merge branch 'vendor/GCC50'
[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 int drm_sysctl_init(struct drm_device *dev)
56 {
57         struct drm_sysctl_info *info;
58         struct sysctl_oid *oid;
59         struct sysctl_oid *top, *drioid;
60         int               i;
61
62         info = kmalloc(sizeof *info, M_DRM, M_WAITOK | M_ZERO);
63         if ( !info )
64                 return 1;
65         dev->sysctl = info;
66
67         /* Add the sysctl node for DRI if it doesn't already exist */
68         drioid = SYSCTL_ADD_NODE(&info->ctx, &sysctl__hw_children, OID_AUTO,
69             "dri", CTLFLAG_RW, NULL, "DRI Graphics");
70         if (!drioid)
71                 return 1;
72
73         /* Find the next free slot under hw.dri */
74         i = 0;
75         SLIST_FOREACH(oid, SYSCTL_CHILDREN(drioid), oid_link) {
76                 if (i <= oid->oid_arg2)
77                         i = oid->oid_arg2 + 1;
78         }
79         if (i>9)
80                 return 1;
81         
82         dev->sysctl_node_idx = i;
83         /* Add the hw.dri.x for our device */
84         info->name[0] = '0' + i;
85         info->name[1] = 0;
86         top = SYSCTL_ADD_NODE(&info->ctx, SYSCTL_CHILDREN(drioid),
87             OID_AUTO, info->name, CTLFLAG_RW, NULL, NULL);
88         if (!top)
89                 return 1;
90         
91         for (i = 0; i < DRM_SYSCTL_ENTRIES; i++) {
92                 oid = SYSCTL_ADD_OID(&info->ctx,
93                         SYSCTL_CHILDREN(top),
94                         OID_AUTO,
95                         drm_sysctl_list[i].name,
96                         CTLTYPE_STRING | CTLFLAG_RD,
97                         dev,
98                         0,
99                         drm_sysctl_list[i].f,
100                         "A",
101                         NULL);
102                 if (!oid)
103                         return 1;
104         }
105         SYSCTL_ADD_INT(&info->ctx, SYSCTL_CHILDREN(drioid), OID_AUTO, "debug",
106             CTLFLAG_RW, &drm_debug, sizeof(drm_debug),
107             "Enable debugging output");
108         SYSCTL_ADD_INT(&info->ctx, SYSCTL_CHILDREN(drioid), OID_AUTO, "notyet",
109             CTLFLAG_RW, &drm_notyet_flag, sizeof(drm_debug),
110             "Enable notyet reminders");
111
112         if (dev->driver->sysctl_init != NULL)
113                 dev->driver->sysctl_init(dev, &info->ctx, top);
114
115         SYSCTL_ADD_INT(&info->ctx, SYSCTL_CHILDREN(drioid), OID_AUTO,
116             "vblank_offdelay", CTLFLAG_RW, &drm_vblank_offdelay,
117             sizeof(drm_vblank_offdelay),
118             "");
119         SYSCTL_ADD_INT(&info->ctx, SYSCTL_CHILDREN(drioid), OID_AUTO,
120             "timestamp_precision", CTLFLAG_RW, &drm_timestamp_precision,
121             sizeof(drm_timestamp_precision),
122             "");
123
124         return (0);
125 }
126
127 int drm_sysctl_cleanup(struct drm_device *dev)
128 {
129         int error;
130
131         error = sysctl_ctx_free(&dev->sysctl->ctx);
132         drm_free(dev->sysctl, M_DRM);
133         dev->sysctl = NULL;
134         if (dev->driver->sysctl_cleanup != NULL)
135                 dev->driver->sysctl_cleanup(dev);
136
137         return (error);
138 }
139
140 #define DRM_SYSCTL_PRINT(fmt, arg...)                           \
141 do {                                                            \
142         ksnprintf(buf, sizeof(buf), fmt, ##arg);                        \
143         retcode = SYSCTL_OUT(req, buf, strlen(buf));            \
144         if (retcode)                                            \
145                 goto done;                                      \
146 } while (0)
147
148 static int drm_name_info DRM_SYSCTL_HANDLER_ARGS
149 {
150         struct drm_device *dev = arg1;
151         char buf[128];
152         int retcode;
153         int hasunique = 0;
154
155         DRM_SYSCTL_PRINT("%s 0x%x", dev->driver->name, dev2udev(dev->devnode));
156         
157         DRM_LOCK(dev);
158         if (dev->unique) {
159                 ksnprintf(buf, sizeof(buf), " %s", dev->unique);
160                 hasunique = 1;
161         }
162         DRM_UNLOCK(dev);
163         
164         if (hasunique)
165                 SYSCTL_OUT(req, buf, strlen(buf));
166
167         SYSCTL_OUT(req, "", 1);
168
169 done:
170         return retcode;
171 }
172
173 /**
174  * Called when "/proc/dri/.../vm" is read.
175  *
176  * Prints information about all mappings in drm_device::maplist.
177  */
178 static int drm_vm_info DRM_SYSCTL_HANDLER_ARGS
179 {
180         char buf[128];
181         int retcode;
182         struct drm_device *dev = arg1;
183         struct drm_local_map *map;
184         struct drm_map_list *r_list;
185
186         /* Hardcoded from _DRM_FRAME_BUFFER,
187            _DRM_REGISTERS, _DRM_SHM, _DRM_AGP, and
188            _DRM_SCATTER_GATHER and _DRM_CONSISTENT */
189         const char *types[] = { "FB", "REG", "SHM", "AGP", "SG", "PCI" };
190         const char *type;
191         int i;
192
193         DRM_LOCK(dev);
194         DRM_SYSCTL_PRINT("\nslot offset         size       "
195             "type flags address            handle mtrr\n");
196         i = 0;
197         list_for_each_entry(r_list, &dev->maplist, head) {
198                 map = r_list->map;
199                 if (!map)
200                         continue;
201                 if (map->type < 0 || map->type > 5)
202                         type = "??";
203                 else
204                         type = types[map->type];
205
206                 DRM_SYSCTL_PRINT("%4d 0x%016llx 0x%08lx %4.4s  0x%02x 0x%08lx ",
207                            i,
208                            (unsigned long long)map->offset,
209                            map->size, type, map->flags,
210                            (unsigned long) r_list->user_token);
211                 if (map->mtrr < 0)
212                         DRM_SYSCTL_PRINT("none\n");
213                 else
214                         DRM_SYSCTL_PRINT("%4d\n", map->mtrr);
215                 i++;
216
217         }
218         SYSCTL_OUT(req, "", 1);
219         DRM_UNLOCK(dev);
220
221 done:
222         return 0;
223 }
224
225 static int drm_bufs_info DRM_SYSCTL_HANDLER_ARGS
226 {
227         struct drm_device        *dev = arg1;
228         drm_device_dma_t *dma = dev->dma;
229         drm_device_dma_t tempdma;
230         int *templists;
231         int i;
232         char buf[128];
233         int retcode;
234
235         /* We can't hold the locks around DRM_SYSCTL_PRINT, so make a temporary
236          * copy of the whole structure and the relevant data from buflist.
237          */
238         DRM_LOCK(dev);
239         if (dma == NULL) {
240                 DRM_UNLOCK(dev);
241                 return 0;
242         }
243         spin_lock(&dev->dma_lock);
244         tempdma = *dma;
245         templists = kmalloc(sizeof(int) * dma->buf_count, M_DRM,
246                             M_WAITOK | M_NULLOK);
247         for (i = 0; i < dma->buf_count; i++)
248                 templists[i] = dma->buflist[i]->list;
249         dma = &tempdma;
250         spin_unlock(&dev->dma_lock);
251         DRM_UNLOCK(dev);
252
253         DRM_SYSCTL_PRINT("\n o     size count  free      segs pages    kB\n");
254         for (i = 0; i <= DRM_MAX_ORDER; i++) {
255                 if (dma->bufs[i].buf_count)
256                         DRM_SYSCTL_PRINT("%2d %8d %5d %5d %5d %5d %5d\n",
257                                        i,
258                                        dma->bufs[i].buf_size,
259                                        dma->bufs[i].buf_count,
260                                        atomic_read(&dma->bufs[i]
261                                                    .freelist.count),
262                                        dma->bufs[i].seg_count,
263                                        dma->bufs[i].seg_count
264                                        *(1 << dma->bufs[i].page_order),
265                                        (dma->bufs[i].seg_count
266                                         * (1 << dma->bufs[i].page_order))
267                                        * (int)PAGE_SIZE / 1024);
268         }
269         DRM_SYSCTL_PRINT("\n");
270         for (i = 0; i < dma->buf_count; i++) {
271                 if (i && !(i%32)) DRM_SYSCTL_PRINT("\n");
272                 DRM_SYSCTL_PRINT(" %d", templists[i]);
273         }
274         DRM_SYSCTL_PRINT("\n");
275
276         SYSCTL_OUT(req, "", 1);
277 done:
278         drm_free(templists, M_DRM);
279         return retcode;
280 }
281
282 static int drm_clients_info DRM_SYSCTL_HANDLER_ARGS
283 {
284         struct drm_device *dev = arg1;
285         struct drm_file *priv, *tempprivs;
286         char buf[128];
287         int retcode;
288         int privcount, i;
289
290         DRM_LOCK(dev);
291
292         privcount = 0;
293         list_for_each_entry(priv, &dev->filelist, lhead)
294                 privcount++;
295
296         tempprivs = kmalloc(sizeof(struct drm_file) * privcount, M_DRM,
297                             M_WAITOK | M_NULLOK);
298         if (tempprivs == NULL) {
299                 DRM_UNLOCK(dev);
300                 return ENOMEM;
301         }
302         i = 0;
303         list_for_each_entry(priv, &dev->filelist, lhead)
304                 tempprivs[i++] = *priv;
305
306         DRM_UNLOCK(dev);
307
308         DRM_SYSCTL_PRINT(
309             "\na dev            pid   uid      magic     ioctls\n");
310         for (i = 0; i < privcount; i++) {
311                 priv = &tempprivs[i];
312                 DRM_SYSCTL_PRINT("%c %-12s %5d %5d %10u %10lu\n",
313                                priv->authenticated ? 'y' : 'n',
314                                devtoname(priv->dev->devnode),
315                                priv->pid,
316                                priv->uid,
317                                priv->magic,
318                                priv->ioctl_count);
319         }
320
321         SYSCTL_OUT(req, "", 1);
322 done:
323         drm_free(tempprivs, M_DRM);
324         return retcode;
325 }
326
327 static int drm_vblank_info DRM_SYSCTL_HANDLER_ARGS
328 {
329         struct drm_device *dev = arg1;
330         char buf[128];
331         int retcode;
332         int i;
333
334         DRM_SYSCTL_PRINT("\ncrtc ref count    last     enabled inmodeset\n");
335         DRM_LOCK(dev);
336         if (dev->_vblank_count == NULL)
337                 goto done;
338         for (i = 0 ; i < dev->num_crtcs ; i++) {
339                 DRM_SYSCTL_PRINT("  %02d  %02d %08d %08d %02d      %02d\n",
340                     i, dev->vblank_refcount[i].counter,
341                     dev->_vblank_count[i].counter,
342                     dev->last_vblank[i],
343                     dev->vblank_enabled[i],
344                     dev->vblank_inmodeset[i]);
345         }
346 done:
347         DRM_UNLOCK(dev);
348
349         SYSCTL_OUT(req, "", -1);
350         return retcode;
351 }