OSDN Git Service

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