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