OSDN Git Service

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