OSDN Git Service

Fix the possibility of sleeping with locks held in sysctls by copying the
[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 #ifdef __FreeBSD__
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 #ifdef DEBUG_MEMORY
40         { "mem",     DRM(mem_info)     },
41 #endif
42         { "vm",      DRM(vm_info)      },
43         { "clients", DRM(clients_info) },
44 #if __HAVE_DMA
45         { "bufs",    DRM(bufs_info)    },
46 #endif
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)(drm_device_t *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 = DRM(alloc)(sizeof *info, DRM_MEM_DRIVER);
63         if ( !info )
64                 return 1;
65         bzero(info, sizeof *info);
66         dev->sysctl = info;
67
68         /* Add the sysctl node for DRI if it doesn't already exist */
69         drioid = SYSCTL_ADD_NODE( &info->ctx, &sysctl__hw_children, OID_AUTO, "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         /* Add the hw.dri.x for our device */
83         info->name[0] = '0' + i;
84         info->name[1] = 0;
85         top = SYSCTL_ADD_NODE( &info->ctx, SYSCTL_CHILDREN(drioid), OID_AUTO, info->name, CTLFLAG_RW, NULL, NULL);
86         if (!top)
87                 return 1;
88         
89         for (i = 0; i < DRM_SYSCTL_ENTRIES; i++) {
90                 oid = sysctl_add_oid( &info->ctx, 
91                         SYSCTL_CHILDREN(top), 
92                         OID_AUTO, 
93                         DRM(sysctl_list)[i].name, 
94                         CTLTYPE_INT | CTLFLAG_RD, 
95                         dev, 
96                         0, 
97                         DRM(sysctl_list)[i].f, 
98                         "A", 
99                         NULL);
100                 if (!oid)
101                         return 1;
102         }
103         return 0;
104 }
105
106 int DRM(sysctl_cleanup)(drm_device_t *dev)
107 {
108         int error;
109         error = sysctl_ctx_free( &dev->sysctl->ctx );
110
111         DRM(free)(dev->sysctl, sizeof *dev->sysctl, DRM_MEM_DRIVER);
112         dev->sysctl = NULL;
113
114         return error;
115 }
116
117 #define DRM_SYSCTL_PRINT(fmt, arg...)                           \
118 do {                                                            \
119         snprintf(buf, sizeof(buf), fmt, ##arg);                 \
120         retcode = SYSCTL_OUT(req, buf, strlen(buf));            \
121         if (retcode)                                            \
122                 goto done;                                      \
123 } while (0)
124
125 static int DRM(name_info)DRM_SYSCTL_HANDLER_ARGS
126 {
127         drm_device_t *dev = arg1;
128         char buf[128];
129         int retcode;
130         int hasunique = 0;
131
132         DRM_SYSCTL_PRINT("%s 0x%x", dev->name, dev2udev(dev->devnode));
133         
134         DRM_LOCK();
135         if (dev->unique) {
136                 snprintf(buf, sizeof(buf), " %s", dev->unique);
137                 hasunique = 1;
138         }
139         DRM_UNLOCK();
140         
141         if (hasunique)
142                 SYSCTL_OUT(req, buf, strlen(buf));
143
144         SYSCTL_OUT(req, "", 1);
145
146 done:
147         return retcode;
148 }
149
150 static int DRM(vm_info)DRM_SYSCTL_HANDLER_ARGS
151 {
152         drm_device_t *dev = arg1;
153         drm_local_map_t *map, *tempmaps;
154         drm_map_list_entry_t    *listentry;
155         const char   *types[] = { "FB", "REG", "SHM", "AGP", "SG" };
156         const char *type, *yesno;
157         int i, mapcount;
158         char buf[128];
159         int retcode;
160
161         /* We can't hold the lock while doing SYSCTL_OUTs, so allocate a
162          * temporary copy of all the map entries and then SYSCTL_OUT that.
163          */
164         DRM_LOCK();
165
166         mapcount = 0;
167         TAILQ_FOREACH(listentry, dev->maplist, link)
168                 mapcount++;
169
170         tempmaps = DRM(alloc)(sizeof(drm_local_map_t) * mapcount, DRM_MEM_MAPS);
171         if (tempmaps == NULL) {
172                 DRM_UNLOCK();
173                 return ENOMEM;
174         }
175
176         i = 0;
177         TAILQ_FOREACH(listentry, dev->maplist, link)
178                 tempmaps[i++] = *listentry->map;
179
180         DRM_UNLOCK();
181
182         DRM_SYSCTL_PRINT("\nslot         offset       size type flags    "
183                          "address mtrr\n");
184
185         for (i = 0; i < mapcount; i++) {
186                 map = &tempmaps[i];
187
188                 if (map->type < 0 || map->type > 4)
189                         type = "??";
190                 else
191                         type = types[map->type];
192
193                 if (map->mtrr <= 0)
194                         yesno = "no";
195                 else
196                         yesno = "yes";
197
198                 DRM_SYSCTL_PRINT(
199                     "%4d 0x%08lx 0x%08lx %4.4s  0x%02x 0x%08lx %s\n", i,
200                     map->offset, map->size, type, map->flags,
201                     (unsigned long)map->handle, yesno);
202         }
203         SYSCTL_OUT(req, "", 1);
204
205 done:
206         DRM(free)(tempmaps, sizeof(drm_local_map_t) * mapcount, DRM_MEM_MAPS);
207         return retcode;
208 }
209
210 #if __HAVE_DMA
211 static int DRM(bufs_info) DRM_SYSCTL_HANDLER_ARGS
212 {
213         drm_device_t     *dev = arg1;
214         drm_device_dma_t *dma = dev->dma;
215         drm_device_dma_t tempdma;
216         int *templists;
217         int i;
218         char buf[128];
219         int retcode;
220
221         /* We can't hold the locks around DRM_SYSCTL_PRINT, so make a temporary
222          * copy of the whole structure and the relevant data from buflist.
223          */
224         DRM_LOCK();
225         DRM_SPINLOCK(&dev->dma_lock);
226         if (dma == NULL) {
227                 DRM_SPINUNLOCK(&dev->dma_lock);
228                 DRM_UNLOCK();
229                 return 0;
230         }
231         tempdma = *dma;
232         templists = DRM(alloc)(sizeof(int) * dma->buf_count, DRM_MEM_BUFS);
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         DRM(free)(templists, sizeof(int) * dma->buf_count, DRM_MEM_BUFS);
265         return retcode;
266 }
267 #endif
268
269 static int DRM(clients_info)DRM_SYSCTL_HANDLER_ARGS
270 {
271         drm_device_t *dev = arg1;
272         drm_file_t *priv, *tempprivs;
273         char buf[128];
274         int retcode;
275         int privcount, i;
276
277         DRM_LOCK();
278
279         privcount = 0;
280         TAILQ_FOREACH(priv, &dev->files, link)
281                 privcount++;
282
283         tempprivs = DRM(alloc)(sizeof(drm_file_t) * privcount, DRM_MEM_FILES);
284         if (tempprivs == NULL) {
285                 DRM_UNLOCK();
286                 return ENOMEM;
287         }
288         i = 0;
289         TAILQ_FOREACH(priv, &dev->files, link)
290                 tempprivs[i++] = *priv;
291
292         DRM_UNLOCK();
293
294         DRM_SYSCTL_PRINT("\na dev       pid    uid      magic     ioctls\n");
295         for (i = 0; i < privcount; i++) {
296                 priv = &tempprivs[i];
297                 DRM_SYSCTL_PRINT("%c %3d %5d %5d %10u %10lu\n",
298                                priv->authenticated ? 'y' : 'n',
299                                priv->minor,
300                                priv->pid,
301                                priv->uid,
302                                priv->magic,
303                                priv->ioctl_count);
304         }
305
306         SYSCTL_OUT(req, "", 1);
307 done:
308         DRM(free)(tempprivs, sizeof(drm_file_t) * privcount, DRM_MEM_FILES);
309         return retcode;
310 }
311
312 #elif defined(__NetBSD__)
313 /* stub it out for now, sysctl is only for debugging */
314 int DRM(sysctl_init)(drm_device_t *dev)
315 {
316         return 0;
317 }
318
319 int DRM(sysctl_cleanup)(drm_device_t *dev)
320 {
321         return 0;
322 }
323 #endif