OSDN Git Service

Remove the vestiges of the memory-debug code.
[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  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24
25 #include "drmP.h"
26 #include "drm.h"
27
28 #ifdef __FreeBSD__
29
30 #include <sys/sysctl.h>
31
32 static int         drm_name_info DRM_SYSCTL_HANDLER_ARGS;
33 static int         drm_vm_info DRM_SYSCTL_HANDLER_ARGS;
34 static int         drm_clients_info DRM_SYSCTL_HANDLER_ARGS;
35 static int         drm_bufs_info DRM_SYSCTL_HANDLER_ARGS;
36
37 struct drm_sysctl_list {
38         const char *name;
39         int        (*f) DRM_SYSCTL_HANDLER_ARGS;
40 } drm_sysctl_list[] = {
41         {"name",    drm_name_info},
42         {"vm",      drm_vm_info},
43         {"clients", drm_clients_info},
44         {"bufs",    drm_bufs_info},
45 };
46 #define DRM_SYSCTL_ENTRIES (sizeof(drm_sysctl_list)/sizeof(drm_sysctl_list[0]))
47
48 struct drm_sysctl_info {
49         struct sysctl_ctx_list ctx;
50         char                   name[2];
51 };
52
53 int drm_sysctl_init(drm_device_t *dev)
54 {
55         struct drm_sysctl_info *info;
56         struct sysctl_oid *oid;
57         struct sysctl_oid *top, *drioid;
58         int               i;
59
60         info = drm_alloc(sizeof *info, DRM_MEM_DRIVER);
61         if ( !info )
62                 return 1;
63         bzero(info, sizeof *info);
64         dev->sysctl = info;
65
66         /* Add the sysctl node for DRI if it doesn't already exist */
67         drioid = SYSCTL_ADD_NODE( &info->ctx, &sysctl__hw_children, OID_AUTO, "dri", CTLFLAG_RW, NULL, "DRI Graphics");
68         if (!drioid)
69                 return 1;
70
71         /* Find the next free slot under hw.dri */
72         i = 0;
73         SLIST_FOREACH(oid, SYSCTL_CHILDREN(drioid), oid_link) {
74                 if (i <= oid->oid_arg2)
75                         i = oid->oid_arg2 + 1;
76         }
77         if (i>9)
78                 return 1;
79         
80         /* Add the hw.dri.x for our device */
81         info->name[0] = '0' + i;
82         info->name[1] = 0;
83         top = SYSCTL_ADD_NODE( &info->ctx, SYSCTL_CHILDREN(drioid), OID_AUTO, info->name, CTLFLAG_RW, NULL, NULL);
84         if (!top)
85                 return 1;
86         
87         for (i = 0; i < DRM_SYSCTL_ENTRIES; i++) {
88                 oid = sysctl_add_oid( &info->ctx, 
89                         SYSCTL_CHILDREN(top), 
90                         OID_AUTO, 
91                         drm_sysctl_list[i].name, 
92                         CTLTYPE_INT | CTLFLAG_RD, 
93                         dev, 
94                         0, 
95                         drm_sysctl_list[i].f, 
96                         "A", 
97                         NULL);
98                 if (!oid)
99                         return 1;
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         drm_free(dev->sysctl, sizeof *dev->sysctl, DRM_MEM_DRIVER);
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         drm_map_list_entry_t    *listentry;
153         const char   *types[] = { "FB", "REG", "SHM", "AGP", "SG" };
154         const char *type, *yesno;
155         int i, mapcount;
156         char buf[128];
157         int retcode;
158
159         /* We can't hold the lock while doing SYSCTL_OUTs, so allocate a
160          * temporary copy of all the map entries and then SYSCTL_OUT that.
161          */
162         DRM_LOCK();
163
164         mapcount = 0;
165         TAILQ_FOREACH(listentry, dev->maplist, link)
166                 mapcount++;
167
168         tempmaps = drm_alloc(sizeof(drm_local_map_t) * mapcount, DRM_MEM_MAPS);
169         if (tempmaps == NULL) {
170                 DRM_UNLOCK();
171                 return ENOMEM;
172         }
173
174         i = 0;
175         TAILQ_FOREACH(listentry, dev->maplist, link)
176                 tempmaps[i++] = *listentry->map;
177
178         DRM_UNLOCK();
179
180         DRM_SYSCTL_PRINT("\nslot         offset       size type flags    "
181                          "address mtrr\n");
182
183         for (i = 0; i < mapcount; i++) {
184                 map = &tempmaps[i];
185
186                 if (map->type < 0 || map->type > 4)
187                         type = "??";
188                 else
189                         type = types[map->type];
190
191                 if (!map->mtrr)
192                         yesno = "no";
193                 else
194                         yesno = "yes";
195
196                 DRM_SYSCTL_PRINT(
197                     "%4d 0x%08lx 0x%08lx %4.4s  0x%02x 0x%08lx %s\n", i,
198                     map->offset, map->size, type, map->flags,
199                     (unsigned long)map->handle, yesno);
200         }
201         SYSCTL_OUT(req, "", 1);
202
203 done:
204         drm_free(tempmaps, sizeof(drm_local_map_t) * mapcount, DRM_MEM_MAPS);
205         return retcode;
206 }
207
208 static int drm_bufs_info DRM_SYSCTL_HANDLER_ARGS
209 {
210         drm_device_t     *dev = arg1;
211         drm_device_dma_t *dma = dev->dma;
212         drm_device_dma_t tempdma;
213         int *templists;
214         int i;
215         char buf[128];
216         int retcode;
217
218         /* We can't hold the locks around DRM_SYSCTL_PRINT, so make a temporary
219          * copy of the whole structure and the relevant data from buflist.
220          */
221         DRM_LOCK();
222         if (dma == NULL) {
223                 DRM_UNLOCK();
224                 return 0;
225         }
226         DRM_SPINLOCK(&dev->dma_lock);
227         tempdma = *dma;
228         templists = drm_alloc(sizeof(int) * dma->buf_count, DRM_MEM_BUFS);
229         for (i = 0; i < dma->buf_count; i++)
230                 templists[i] = dma->buflist[i]->list;
231         dma = &tempdma;
232         DRM_SPINUNLOCK(&dev->dma_lock);
233         DRM_UNLOCK();
234
235         DRM_SYSCTL_PRINT("\n o     size count  free      segs pages    kB\n");
236         for (i = 0; i <= DRM_MAX_ORDER; i++) {
237                 if (dma->bufs[i].buf_count)
238                         DRM_SYSCTL_PRINT("%2d %8d %5d %5d %5d %5d %5d\n",
239                                        i,
240                                        dma->bufs[i].buf_size,
241                                        dma->bufs[i].buf_count,
242                                        atomic_read(&dma->bufs[i]
243                                                    .freelist.count),
244                                        dma->bufs[i].seg_count,
245                                        dma->bufs[i].seg_count
246                                        *(1 << dma->bufs[i].page_order),
247                                        (dma->bufs[i].seg_count
248                                         * (1 << dma->bufs[i].page_order))
249                                        * PAGE_SIZE / 1024);
250         }
251         DRM_SYSCTL_PRINT("\n");
252         for (i = 0; i < dma->buf_count; i++) {
253                 if (i && !(i%32)) DRM_SYSCTL_PRINT("\n");
254                 DRM_SYSCTL_PRINT(" %d", templists[i]);
255         }
256         DRM_SYSCTL_PRINT("\n");
257
258         SYSCTL_OUT(req, "", 1);
259 done:
260         drm_free(templists, sizeof(int) * dma->buf_count, DRM_MEM_BUFS);
261         return retcode;
262 }
263
264 static int drm_clients_info DRM_SYSCTL_HANDLER_ARGS
265 {
266         drm_device_t *dev = arg1;
267         drm_file_t *priv, *tempprivs;
268         char buf[128];
269         int retcode;
270         int privcount, i;
271
272         DRM_LOCK();
273
274         privcount = 0;
275         TAILQ_FOREACH(priv, &dev->files, link)
276                 privcount++;
277
278         tempprivs = drm_alloc(sizeof(drm_file_t) * privcount, DRM_MEM_FILES);
279         if (tempprivs == NULL) {
280                 DRM_UNLOCK();
281                 return ENOMEM;
282         }
283         i = 0;
284         TAILQ_FOREACH(priv, &dev->files, link)
285                 tempprivs[i++] = *priv;
286
287         DRM_UNLOCK();
288
289         DRM_SYSCTL_PRINT("\na dev       pid    uid      magic     ioctls\n");
290         for (i = 0; i < privcount; i++) {
291                 priv = &tempprivs[i];
292                 DRM_SYSCTL_PRINT("%c %3d %5d %5d %10u %10lu\n",
293                                priv->authenticated ? 'y' : 'n',
294                                priv->minor,
295                                priv->pid,
296                                priv->uid,
297                                priv->magic,
298                                priv->ioctl_count);
299         }
300
301         SYSCTL_OUT(req, "", 1);
302 done:
303         drm_free(tempprivs, sizeof(drm_file_t) * privcount, DRM_MEM_FILES);
304         return retcode;
305 }
306
307 #elif defined(__NetBSD__)
308 /* stub it out for now, sysctl is only for debugging */
309 int drm_sysctl_init(drm_device_t *dev)
310 {
311         return 0;
312 }
313
314 int drm_sysctl_cleanup(drm_device_t *dev)
315 {
316         return 0;
317 }
318 #endif