OSDN Git Service

Fix proc formatting broken by last commit.
[android-x86/external-libdrm.git] / linux-core / drm_proc.c
1 /**
2  * \file drm_proc.c
3  * /proc support for DRM
4  *
5  * \author Rickard E. (Rik) Faith <faith@valinux.com>
6  * \author Gareth Hughes <gareth@valinux.com>
7  *
8  * \par Acknowledgements:
9  *    Matthew J Sottek <matthew.j.sottek@intel.com> sent in a patch to fix
10  *    the problem with the proc files not outputting all their information.
11  */
12
13 /*
14  * Created: Mon Jan 11 09:48:47 1999 by faith@valinux.com
15  *
16  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
17  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
18  * All Rights Reserved.
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining a
21  * copy of this software and associated documentation files (the "Software"),
22  * to deal in the Software without restriction, including without limitation
23  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
24  * and/or sell copies of the Software, and to permit persons to whom the
25  * Software is furnished to do so, subject to the following conditions:
26  *
27  * The above copyright notice and this permission notice (including the next
28  * paragraph) shall be included in all copies or substantial portions of the
29  * Software.
30  *
31  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
34  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
35  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
36  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
37  * OTHER DEALINGS IN THE SOFTWARE.
38  */
39
40 #include "drmP.h"
41
42 static int drm_name_info(char *buf, char **start, off_t offset,
43                          int request, int *eof, void *data);
44 static int drm_vm_info(char *buf, char **start, off_t offset,
45                        int request, int *eof, void *data);
46 static int drm_clients_info(char *buf, char **start, off_t offset,
47                             int request, int *eof, void *data);
48 static int drm_queues_info(char *buf, char **start, off_t offset,
49                            int request, int *eof, void *data);
50 static int drm_bufs_info(char *buf, char **start, off_t offset,
51                          int request, int *eof, void *data);
52 #if DRM_DEBUG_CODE
53 static int drm_vma_info(char *buf, char **start, off_t offset,
54                         int request, int *eof, void *data);
55 #endif
56
57 /**
58  * Proc file list.
59  */
60 static struct drm_proc_list {
61         const char *name;       /**< file name */
62         int (*f) (char *, char **, off_t, int, int *, void *);          /**< proc callback*/
63 } drm_proc_list[] = {
64         {"name", drm_name_info},
65         {"mem", drm_mem_info},
66         {"vm", drm_vm_info},
67         {"clients", drm_clients_info},
68         {"queues", drm_queues_info},
69         {"bufs", drm_bufs_info},
70 #if DRM_DEBUG_CODE
71         {"vma", drm_vma_info},
72 #endif
73 };
74
75 #define DRM_PROC_ENTRIES (sizeof(drm_proc_list)/sizeof(drm_proc_list[0]))
76
77 /**
78  * Initialize the DRI proc filesystem for a device.
79  *
80  * \param dev DRM device.
81  * \param minor device minor number.
82  * \param root DRI proc dir entry.
83  * \param dev_root resulting DRI device proc dir entry.
84  * \return root entry pointer on success, or NULL on failure.
85  *
86  * Create the DRI proc root entry "/proc/dri", the device proc root entry
87  * "/proc/dri/%minor%/", and each entry in proc_list as
88  * "/proc/dri/%minor%/%name%".
89  */
90 int drm_proc_init(drm_device_t * dev, int minor,
91                   struct proc_dir_entry *root, struct proc_dir_entry **dev_root)
92 {
93         struct proc_dir_entry *ent;
94         int i, j;
95         char name[64];
96
97         sprintf(name, "%d", minor);
98         *dev_root = proc_mkdir(name, root);
99         if (!*dev_root) {
100                 DRM_ERROR("Cannot create /proc/dri/%s\n", name);
101                 return -1;
102         }
103
104         for (i = 0; i < DRM_PROC_ENTRIES; i++) {
105                 ent = create_proc_entry(drm_proc_list[i].name,
106                                         S_IFREG | S_IRUGO, *dev_root);
107                 if (!ent) {
108                         DRM_ERROR("Cannot create /proc/dri/%s/%s\n",
109                                   name, drm_proc_list[i].name);
110                         for (j = 0; j < i; j++)
111                                 remove_proc_entry(drm_proc_list[i].name,
112                                                   *dev_root);
113                         remove_proc_entry(name, root);
114                         return -1;
115                 }
116                 ent->read_proc = drm_proc_list[i].f;
117                 ent->data = dev;
118         }
119         return 0;
120 }
121
122 /**
123  * Cleanup the proc filesystem resources.
124  *
125  * \param minor device minor number.
126  * \param root DRI proc dir entry.
127  * \param dev_root DRI device proc dir entry.
128  * \return always zero.
129  *
130  * Remove all proc entries created by proc_init().
131  */
132 int drm_proc_cleanup(int minor, struct proc_dir_entry *root,
133                      struct proc_dir_entry *dev_root)
134 {
135         int i;
136         char name[64];
137
138         if (!root || !dev_root)
139                 return 0;
140
141         for (i = 0; i < DRM_PROC_ENTRIES; i++)
142                 remove_proc_entry(drm_proc_list[i].name, dev_root);
143         sprintf(name, "%d", minor);
144         remove_proc_entry(name, root);
145
146         return 0;
147 }
148
149 /**
150  * Called when "/proc/dri/.../name" is read.
151  *
152  * \param buf output buffer.
153  * \param start start of output data.
154  * \param offset requested start offset.
155  * \param request requested number of bytes.
156  * \param eof whether there is no more data to return.
157  * \param data private data.
158  * \return number of written bytes.
159  *
160  * Prints the device name together with the bus id if available.
161  */
162 static int drm_name_info(char *buf, char **start, off_t offset, int request,
163                          int *eof, void *data)
164 {
165         drm_device_t *dev = (drm_device_t *) data;
166         int len = 0;
167
168         if (offset > DRM_PROC_LIMIT) {
169                 *eof = 1;
170                 return 0;
171         }
172
173         *start = &buf[offset];
174         *eof = 0;
175
176         if (dev->unique) {
177                 DRM_PROC_PRINT("%s %s %s\n",
178                                dev->driver->pci_driver.name,
179                                pci_name(dev->pdev), dev->unique);
180         } else {
181                 DRM_PROC_PRINT("%s %s\n", dev->driver->pci_driver.name,
182                                pci_name(dev->pdev));
183         }
184
185         if (len > request + offset)
186                 return request;
187         *eof = 1;
188         return len - offset;
189 }
190
191 /**
192  * Called when "/proc/dri/.../vm" is read.
193  *
194  * \param buf output buffer.
195  * \param start start of output data.
196  * \param offset requested start offset.
197  * \param request requested number of bytes.
198  * \param eof whether there is no more data to return.
199  * \param data private data.
200  * \return number of written bytes.
201  *
202  * Prints information about all mappings in drm_device::maplist.
203  */
204 static int drm__vm_info(char *buf, char **start, off_t offset, int request,
205                         int *eof, void *data)
206 {
207         drm_device_t *dev = (drm_device_t *) data;
208         int len = 0;
209         drm_map_t *map;
210         drm_map_list_t *r_list;
211         struct list_head *list;
212
213         /* Hardcoded from _DRM_FRAME_BUFFER,
214            _DRM_REGISTERS, _DRM_SHM, _DRM_AGP,
215            _DRM_SCATTER_GATHER, and _DRM_CONSISTENT. */
216         const char *types[] = { "FB", "REG", "SHM", "AGP", "SG", "PCI" };
217         const char *type;
218         int i;
219
220         if (offset > DRM_PROC_LIMIT) {
221                 *eof = 1;
222                 return 0;
223         }
224
225         *start = &buf[offset];
226         *eof = 0;
227
228         DRM_PROC_PRINT("slot     offset       size type flags    "
229                        "address mtrr\n\n");
230         i = 0;
231         if (dev->maplist != NULL)
232                 list_for_each(list, &dev->maplist->head) {
233                 r_list = list_entry(list, drm_map_list_t, head);
234                 map = r_list->map;
235                 if (!map)
236                         continue;
237                 if (map->type < 0 || map->type > 5)
238                         type = "??";
239                 else
240                         type = types[map->type];
241                 DRM_PROC_PRINT("%4d 0x%08lx 0x%08lx %4.4s  0x%02x 0x%08lx ",
242                                i,
243                                map->offset,
244                                map->size, type, map->flags, 
245                                (unsigned long) r_list->user_token);
246
247                 if (map->mtrr < 0) {
248                         DRM_PROC_PRINT("none\n");
249                 } else {
250                         DRM_PROC_PRINT("%4d\n", map->mtrr);
251                 }
252                 i++;
253                 }
254
255         if (len > request + offset)
256                 return request;
257         *eof = 1;
258         return len - offset;
259 }
260
261 /**
262  * Simply calls _vm_info() while holding the drm_device::struct_mutex lock.
263  */
264 static int drm_vm_info(char *buf, char **start, off_t offset, int request,
265                        int *eof, void *data)
266 {
267         drm_device_t *dev = (drm_device_t *) data;
268         int ret;
269
270         mutex_lock(&dev->struct_mutex);
271         ret = drm__vm_info(buf, start, offset, request, eof, data);
272         mutex_unlock(&dev->struct_mutex);
273         return ret;
274 }
275
276 /**
277  * Called when "/proc/dri/.../queues" is read.
278  *
279  * \param buf output buffer.
280  * \param start start of output data.
281  * \param offset requested start offset.
282  * \param request requested number of bytes.
283  * \param eof whether there is no more data to return.
284  * \param data private data.
285  * \return number of written bytes.
286  */
287 static int drm__queues_info(char *buf, char **start, off_t offset,
288                             int request, int *eof, void *data)
289 {
290         drm_device_t *dev = (drm_device_t *) data;
291         int len = 0;
292         int i;
293         drm_queue_t *q;
294
295         if (offset > DRM_PROC_LIMIT) {
296                 *eof = 1;
297                 return 0;
298         }
299
300         *start = &buf[offset];
301         *eof = 0;
302
303         DRM_PROC_PRINT("  ctx/flags   use   fin"
304                        "   blk/rw/rwf  wait    flushed     queued"
305                        "      locks\n\n");
306         for (i = 0; i < dev->queue_count; i++) {
307                 q = dev->queuelist[i];
308                 atomic_inc(&q->use_count);
309                 DRM_PROC_PRINT_RET(atomic_dec(&q->use_count),
310                                    "%5d/0x%03x %5d %5d"
311                                    " %5d/%c%c/%c%c%c %5Zd\n",
312                                    i,
313                                    q->flags,
314                                    atomic_read(&q->use_count),
315                                    atomic_read(&q->finalization),
316                                    atomic_read(&q->block_count),
317                                    atomic_read(&q->block_read) ? 'r' : '-',
318                                    atomic_read(&q->block_write) ? 'w' : '-',
319                                    waitqueue_active(&q->read_queue) ? 'r' : '-',
320                                    waitqueue_active(&q->
321                                                     write_queue) ? 'w' : '-',
322                                    waitqueue_active(&q->
323                                                     flush_queue) ? 'f' : '-',
324                                    DRM_BUFCOUNT(&q->waitlist));
325                 atomic_dec(&q->use_count);
326         }
327
328         if (len > request + offset)
329                 return request;
330         *eof = 1;
331         return len - offset;
332 }
333
334 /**
335  * Simply calls _queues_info() while holding the drm_device::struct_mutex lock.
336  */
337 static int drm_queues_info(char *buf, char **start, off_t offset, int request,
338                            int *eof, void *data)
339 {
340         drm_device_t *dev = (drm_device_t *) data;
341         int ret;
342
343         mutex_lock(&dev->struct_mutex);
344         ret = drm__queues_info(buf, start, offset, request, eof, data);
345         mutex_unlock(&dev->struct_mutex);
346         return ret;
347 }
348
349 /**
350  * Called when "/proc/dri/.../bufs" is read.
351  *
352  * \param buf output buffer.
353  * \param start start of output data.
354  * \param offset requested start offset.
355  * \param request requested number of bytes.
356  * \param eof whether there is no more data to return.
357  * \param data private data.
358  * \return number of written bytes.
359  */
360 static int drm__bufs_info(char *buf, char **start, off_t offset, int request,
361                           int *eof, void *data)
362 {
363         drm_device_t *dev = (drm_device_t *) data;
364         int len = 0;
365         drm_device_dma_t *dma = dev->dma;
366         int i;
367
368         if (!dma || offset > DRM_PROC_LIMIT) {
369                 *eof = 1;
370                 return 0;
371         }
372
373         *start = &buf[offset];
374         *eof = 0;
375
376         DRM_PROC_PRINT(" o     size count  free  segs pages    kB\n\n");
377         for (i = 0; i <= DRM_MAX_ORDER; i++) {
378                 if (dma->bufs[i].buf_count)
379                         DRM_PROC_PRINT("%2d %8d %5d %5d %5d %5d %5ld\n",
380                                        i,
381                                        dma->bufs[i].buf_size,
382                                        dma->bufs[i].buf_count,
383                                        atomic_read(&dma->bufs[i]
384                                                    .freelist.count),
385                                        dma->bufs[i].seg_count,
386                                        dma->bufs[i].seg_count
387                                        * (1 << dma->bufs[i].page_order),
388                                        (dma->bufs[i].seg_count
389                                         * (1 << dma->bufs[i].page_order))
390                                        * PAGE_SIZE / 1024);
391         }
392         DRM_PROC_PRINT("\n");
393         for (i = 0; i < dma->buf_count; i++) {
394                 if (i && !(i % 32))
395                         DRM_PROC_PRINT("\n");
396                 DRM_PROC_PRINT(" %d", dma->buflist[i]->list);
397         }
398         DRM_PROC_PRINT("\n");
399
400         if (len > request + offset)
401                 return request;
402         *eof = 1;
403         return len - offset;
404 }
405
406 /**
407  * Simply calls _bufs_info() while holding the drm_device::struct_mutex lock.
408  */
409 static int drm_bufs_info(char *buf, char **start, off_t offset, int request,
410                          int *eof, void *data)
411 {
412         drm_device_t *dev = (drm_device_t *) data;
413         int ret;
414
415         mutex_lock(&dev->struct_mutex);
416         ret = drm__bufs_info(buf, start, offset, request, eof, data);
417         mutex_unlock(&dev->struct_mutex);
418         return ret;
419 }
420
421 /**
422  * Called when "/proc/dri/.../clients" is read.
423  *
424  * \param buf output buffer.
425  * \param start start of output data.
426  * \param offset requested start offset.
427  * \param request requested number of bytes.
428  * \param eof whether there is no more data to return.
429  * \param data private data.
430  * \return number of written bytes.
431  */
432 static int drm__clients_info(char *buf, char **start, off_t offset,
433                              int request, int *eof, void *data)
434 {
435         drm_device_t *dev = (drm_device_t *) data;
436         int len = 0;
437         drm_file_t *priv;
438
439         if (offset > DRM_PROC_LIMIT) {
440                 *eof = 1;
441                 return 0;
442         }
443
444         *start = &buf[offset];
445         *eof = 0;
446
447         DRM_PROC_PRINT("a dev   pid    uid      magic     ioctls\n\n");
448         for (priv = dev->file_first; priv; priv = priv->next) {
449                 DRM_PROC_PRINT("%c %3d %5d %5d %10u %10lu\n",
450                                priv->authenticated ? 'y' : 'n',
451                                priv->minor,
452                                priv->pid,
453                                priv->uid, priv->magic, priv->ioctl_count);
454         }
455
456         if (len > request + offset)
457                 return request;
458         *eof = 1;
459         return len - offset;
460 }
461
462 /**
463  * Simply calls _clients_info() while holding the drm_device::struct_mutex lock.
464  */
465 static int drm_clients_info(char *buf, char **start, off_t offset,
466                             int request, int *eof, void *data)
467 {
468         drm_device_t *dev = (drm_device_t *) data;
469         int ret;
470
471         mutex_lock(&dev->struct_mutex);
472         ret = drm__clients_info(buf, start, offset, request, eof, data);
473         mutex_unlock(&dev->struct_mutex);
474         return ret;
475 }
476
477 #if DRM_DEBUG_CODE
478
479 static int drm__vma_info(char *buf, char **start, off_t offset, int request,
480                          int *eof, void *data)
481 {
482         drm_device_t *dev = (drm_device_t *) data;
483         int len = 0;
484         drm_vma_entry_t *pt;
485         struct vm_area_struct *vma;
486 #if defined(__i386__)
487         unsigned int pgprot;
488 #endif
489
490         if (offset > DRM_PROC_LIMIT) {
491                 *eof = 1;
492                 return 0;
493         }
494
495         *start = &buf[offset];
496         *eof = 0;
497
498         DRM_PROC_PRINT("vma use count: %d, high_memory = %p, 0x%08lx\n",
499                        atomic_read(&dev->vma_count),
500                        high_memory, virt_to_phys(high_memory));
501         for (pt = dev->vmalist; pt; pt = pt->next) {
502                 if (!(vma = pt->vma))
503                         continue;
504                 DRM_PROC_PRINT("\n%5d 0x%08lx-0x%08lx %c%c%c%c%c%c 0x%08lx",
505                                pt->pid,
506                                vma->vm_start,
507                                vma->vm_end,
508                                vma->vm_flags & VM_READ ? 'r' : '-',
509                                vma->vm_flags & VM_WRITE ? 'w' : '-',
510                                vma->vm_flags & VM_EXEC ? 'x' : '-',
511                                vma->vm_flags & VM_MAYSHARE ? 's' : 'p',
512                                vma->vm_flags & VM_LOCKED ? 'l' : '-',
513                                vma->vm_flags & VM_IO ? 'i' : '-',
514                                VM_OFFSET(vma));
515
516 #if defined(__i386__)
517                 pgprot = pgprot_val(vma->vm_page_prot);
518                 DRM_PROC_PRINT(" %c%c%c%c%c%c%c%c%c",
519                                pgprot & _PAGE_PRESENT ? 'p' : '-',
520                                pgprot & _PAGE_RW ? 'w' : 'r',
521                                pgprot & _PAGE_USER ? 'u' : 's',
522                                pgprot & _PAGE_PWT ? 't' : 'b',
523                                pgprot & _PAGE_PCD ? 'u' : 'c',
524                                pgprot & _PAGE_ACCESSED ? 'a' : '-',
525                                pgprot & _PAGE_DIRTY ? 'd' : '-',
526                                pgprot & _PAGE_PSE ? 'm' : 'k',
527                                pgprot & _PAGE_GLOBAL ? 'g' : 'l');
528 #endif
529                 DRM_PROC_PRINT("\n");
530         }
531
532         if (len > request + offset)
533                 return request;
534         *eof = 1;
535         return len - offset;
536 }
537
538 static int drm_vma_info(char *buf, char **start, off_t offset, int request,
539                         int *eof, void *data)
540 {
541         drm_device_t *dev = (drm_device_t *) data;
542         int ret;
543
544         mutex_lock(&dev->struct_mutex);
545         ret = drm__vma_info(buf, start, offset, request, eof, data);
546         mutex_unlock(&dev->struct_mutex);
547         return ret;
548 }
549 #endif