OSDN Git Service

Merge branch 'master' of ../../drm into modesetting-101
[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 static int drm_objects_info(char *buf, char **start, off_t offset,
53                          int request, int *eof, void *data);
54 #if DRM_DEBUG_CODE
55 static int drm_vma_info(char *buf, char **start, off_t offset,
56                         int request, int *eof, void *data);
57 #endif
58
59 /**
60  * Proc file list.
61  */
62 static struct drm_proc_list {
63         const char *name;       /**< file name */
64         int (*f) (char *, char **, off_t, int, int *, void *);          /**< proc callback*/
65 } drm_proc_list[] = {
66         {"name", drm_name_info},
67         {"mem", drm_mem_info},
68         {"vm", drm_vm_info},
69         {"clients", drm_clients_info},
70         {"queues", drm_queues_info},
71         {"bufs", drm_bufs_info},
72         {"objects", drm_objects_info},
73 #if DRM_DEBUG_CODE
74         {"vma", drm_vma_info},
75 #endif
76 };
77
78 #define DRM_PROC_ENTRIES ARRAY_SIZE(drm_proc_list)
79
80 /**
81  * Initialize the DRI proc filesystem for a device.
82  *
83  * \param dev DRM device.
84  * \param minor device minor number.
85  * \param root DRI proc dir entry.
86  * \param dev_root resulting DRI device proc dir entry.
87  * \return root entry pointer on success, or NULL on failure.
88  *
89  * Create the DRI proc root entry "/proc/dri", the device proc root entry
90  * "/proc/dri/%minor%/", and each entry in proc_list as
91  * "/proc/dri/%minor%/%name%".
92  */
93 int drm_proc_init(struct drm_minor *minor, int minor_id,
94                   struct proc_dir_entry *root)
95 {
96         struct proc_dir_entry *ent;
97         int i, j;
98         char name[64];
99
100         sprintf(name, "%d", minor_id);
101         minor->dev_root = proc_mkdir(name, root);
102         if (!minor->dev_root) {
103                 DRM_ERROR("Cannot create /proc/dri/%s\n", name);
104                 return -1;
105         }
106
107         for (i = 0; i < DRM_PROC_ENTRIES; i++) {
108                 ent = create_proc_entry(drm_proc_list[i].name,
109                                         S_IFREG | S_IRUGO, minor->dev_root);
110                 if (!ent) {
111                         DRM_ERROR("Cannot create /proc/dri/%s/%s\n",
112                                   name, drm_proc_list[i].name);
113                         for (j = 0; j < i; j++)
114                                 remove_proc_entry(drm_proc_list[i].name,
115                                                   minor->dev_root);
116                         remove_proc_entry(name, root);
117                         minor->dev_root = NULL;
118                         return -1;
119                 }
120                 ent->read_proc = drm_proc_list[i].f;
121                 ent->data = minor;
122         }
123         return 0;
124 }
125
126 /**
127  * Cleanup the proc filesystem resources.
128  *
129  * \param minor device minor number.
130  * \param root DRI proc dir entry.
131  * \param dev_root DRI device proc dir entry.
132  * \return always zero.
133  *
134  * Remove all proc entries created by proc_init().
135  */
136 int drm_proc_cleanup(struct drm_minor *minor, struct proc_dir_entry *root)
137 {
138         int i;
139         char name[64];
140
141         if (!root || !minor->dev_root)
142                 return 0;
143
144         for (i = 0; i < DRM_PROC_ENTRIES; i++)
145                 remove_proc_entry(drm_proc_list[i].name, minor->dev_root);
146         sprintf(name, "%d", minor->index);
147         remove_proc_entry(name, root);
148
149         return 0;
150 }
151
152 /**
153  * Called when "/proc/dri/.../name" is read.
154  *
155  * \param buf output buffer.
156  * \param start start of output data.
157  * \param offset requested start offset.
158  * \param request requested number of bytes.
159  * \param eof whether there is no more data to return.
160  * \param data private data.
161  * \return number of written bytes.
162  *
163  * Prints the device name together with the bus id if available.
164  */
165 static int drm_name_info(char *buf, char **start, off_t offset, int request,
166                          int *eof, void *data)
167 {
168         struct drm_minor *minor = (struct drm_minor *) data; 
169         struct drm_master *master = minor->master;
170         struct drm_device *dev = minor->dev;
171         int len = 0;
172
173         if (offset > DRM_PROC_LIMIT) {
174                 *eof = 1;
175                 return 0;
176         }
177
178         *start = &buf[offset];
179         *eof = 0;
180
181         if (master->unique) {
182                 DRM_PROC_PRINT("%s %s %s\n",
183                                dev->driver->pci_driver.name,
184                                pci_name(dev->pdev), master->unique);
185         } else {
186                 DRM_PROC_PRINT("%s %s\n", dev->driver->pci_driver.name,
187                                pci_name(dev->pdev));
188         }
189
190         if (len > request + offset)
191                 return request;
192         *eof = 1;
193         return len - offset;
194 }
195
196 /**
197  * Called when "/proc/dri/.../vm" is read.
198  *
199  * \param buf output buffer.
200  * \param start start of output data.
201  * \param offset requested start offset.
202  * \param request requested number of bytes.
203  * \param eof whether there is no more data to return.
204  * \param data private data.
205  * \return number of written bytes.
206  *
207  * Prints information about all mappings in drm_device::maplist.
208  */
209 static int drm__vm_info(char *buf, char **start, off_t offset, int request,
210                         int *eof, void *data)
211 {
212         struct drm_minor *minor = (struct drm_minor *) data; 
213         struct drm_device *dev = minor->dev;
214         int len = 0;
215         struct drm_map *map;
216         struct drm_map_list *r_list;
217
218         /* Hardcoded from _DRM_FRAME_BUFFER,
219            _DRM_REGISTERS, _DRM_SHM, _DRM_AGP,
220            _DRM_SCATTER_GATHER, and _DRM_CONSISTENT. */
221         const char *types[] = { "FB", "REG", "SHM", "AGP", "SG", "PCI" };
222         const char *type;
223         int i;
224
225         if (offset > DRM_PROC_LIMIT) {
226                 *eof = 1;
227                 return 0;
228         }
229
230         *start = &buf[offset];
231         *eof = 0;
232
233         DRM_PROC_PRINT("slot     offset       size type flags    "
234                        "address mtrr\n\n");
235         i = 0;
236         list_for_each_entry(r_list, &dev->maplist, head) {
237                 map = r_list->map;
238                 if (!map)
239                         continue;
240                 if (map->type < 0 || map->type > 5)
241                         type = "??";
242                 else
243                         type = types[map->type];
244                 DRM_PROC_PRINT("%4d 0x%08lx 0x%08lx %4.4s  0x%02x 0x%08lx ",
245                                i,
246                                map->offset,
247                                map->size, type, map->flags,
248                                (unsigned long) r_list->user_token);
249
250                 if (map->mtrr < 0) {
251                         DRM_PROC_PRINT("none\n");
252                 } else {
253                         DRM_PROC_PRINT("%4d\n", map->mtrr);
254                 }
255                 i++;
256         }
257
258         if (len > request + offset)
259                 return request;
260         *eof = 1;
261         return len - offset;
262 }
263
264 /**
265  * Simply calls _vm_info() while holding the drm_device::struct_mutex lock.
266  */
267 static int drm_vm_info(char *buf, char **start, off_t offset, int request,
268                        int *eof, void *data)
269 {
270         struct drm_minor *minor = (struct drm_minor *) data; 
271         struct drm_device *dev = minor->dev;
272         int ret;
273
274         mutex_lock(&dev->struct_mutex);
275         ret = drm__vm_info(buf, start, offset, request, eof, data);
276         mutex_unlock(&dev->struct_mutex);
277         return ret;
278 }
279
280 /**
281  * Called when "/proc/dri/.../queues" is read.
282  *
283  * \param buf output buffer.
284  * \param start start of output data.
285  * \param offset requested start offset.
286  * \param request requested number of bytes.
287  * \param eof whether there is no more data to return.
288  * \param data private data.
289  * \return number of written bytes.
290  */
291 static int drm__queues_info(char *buf, char **start, off_t offset,
292                             int request, int *eof, void *data)
293 {
294         struct drm_minor *minor = (struct drm_minor *) data; 
295         struct drm_device *dev = minor->dev;
296         int len = 0;
297         int i;
298         struct drm_queue *q;
299
300         if (offset > DRM_PROC_LIMIT) {
301                 *eof = 1;
302                 return 0;
303         }
304
305         *start = &buf[offset];
306         *eof = 0;
307
308         DRM_PROC_PRINT("  ctx/flags   use   fin"
309                        "   blk/rw/rwf  wait    flushed     queued"
310                        "      locks\n\n");
311         for (i = 0; i < dev->queue_count; i++) {
312                 q = dev->queuelist[i];
313                 atomic_inc(&q->use_count);
314                 DRM_PROC_PRINT_RET(atomic_dec(&q->use_count),
315                                    "%5d/0x%03x %5d %5d"
316                                    " %5d/%c%c/%c%c%c %5Zd\n",
317                                    i,
318                                    q->flags,
319                                    atomic_read(&q->use_count),
320                                    atomic_read(&q->finalization),
321                                    atomic_read(&q->block_count),
322                                    atomic_read(&q->block_read) ? 'r' : '-',
323                                    atomic_read(&q->block_write) ? 'w' : '-',
324                                    waitqueue_active(&q->read_queue) ? 'r' : '-',
325                                    waitqueue_active(&q->
326                                                     write_queue) ? 'w' : '-',
327                                    waitqueue_active(&q->
328                                                     flush_queue) ? 'f' : '-',
329                                    DRM_BUFCOUNT(&q->waitlist));
330                 atomic_dec(&q->use_count);
331         }
332
333         if (len > request + offset)
334                 return request;
335         *eof = 1;
336         return len - offset;
337 }
338
339 /**
340  * Simply calls _queues_info() while holding the drm_device::struct_mutex lock.
341  */
342 static int drm_queues_info(char *buf, char **start, off_t offset, int request,
343                            int *eof, void *data)
344 {
345         struct drm_minor *minor = (struct drm_minor *) data; 
346         struct drm_device *dev = minor->dev;
347         int ret;
348
349         mutex_lock(&dev->struct_mutex);
350         ret = drm__queues_info(buf, start, offset, request, eof, data);
351         mutex_unlock(&dev->struct_mutex);
352         return ret;
353 }
354
355 /**
356  * Called when "/proc/dri/.../bufs" is read.
357  *
358  * \param buf output buffer.
359  * \param start start of output data.
360  * \param offset requested start offset.
361  * \param request requested number of bytes.
362  * \param eof whether there is no more data to return.
363  * \param data private data.
364  * \return number of written bytes.
365  */
366 static int drm__bufs_info(char *buf, char **start, off_t offset, int request,
367                           int *eof, void *data)
368 {
369         struct drm_minor *minor = (struct drm_minor *) data; 
370         struct drm_device *dev = minor->dev;
371         int len = 0;
372         struct drm_device_dma *dma = dev->dma;
373         int i;
374
375         if (!dma || offset > DRM_PROC_LIMIT) {
376                 *eof = 1;
377                 return 0;
378         }
379
380         *start = &buf[offset];
381         *eof = 0;
382
383         DRM_PROC_PRINT(" o     size count  free  segs pages    kB\n\n");
384         for (i = 0; i <= DRM_MAX_ORDER; i++) {
385                 if (dma->bufs[i].buf_count)
386                         DRM_PROC_PRINT("%2d %8d %5d %5d %5d %5d %5ld\n",
387                                        i,
388                                        dma->bufs[i].buf_size,
389                                        dma->bufs[i].buf_count,
390                                        atomic_read(&dma->bufs[i]
391                                                    .freelist.count),
392                                        dma->bufs[i].seg_count,
393                                        dma->bufs[i].seg_count
394                                        * (1 << dma->bufs[i].page_order),
395                                        (dma->bufs[i].seg_count
396                                         * (1 << dma->bufs[i].page_order))
397                                        * PAGE_SIZE / 1024);
398         }
399         DRM_PROC_PRINT("\n");
400         for (i = 0; i < dma->buf_count; i++) {
401                 if (i && !(i % 32))
402                         DRM_PROC_PRINT("\n");
403                 DRM_PROC_PRINT(" %d", dma->buflist[i]->list);
404         }
405         DRM_PROC_PRINT("\n");
406
407         if (len > request + offset)
408                 return request;
409         *eof = 1;
410         return len - offset;
411 }
412
413 /**
414  * Simply calls _bufs_info() while holding the drm_device::struct_mutex lock.
415  */
416 static int drm_bufs_info(char *buf, char **start, off_t offset, int request,
417                          int *eof, void *data)
418 {
419         struct drm_minor *minor = (struct drm_minor *) data; 
420         struct drm_device *dev = minor->dev;
421         int ret;
422
423         mutex_lock(&dev->struct_mutex);
424         ret = drm__bufs_info(buf, start, offset, request, eof, data);
425         mutex_unlock(&dev->struct_mutex);
426         return ret;
427 }
428
429 /**
430  * Called when "/proc/dri/.../objects" is read.
431  *
432  * \param buf output buffer.
433  * \param start start of output data.
434  * \param offset requested start offset.
435  * \param request requested number of bytes.
436  * \param eof whether there is no more data to return.
437  * \param data private data.
438  * \return number of written bytes.
439  */
440 static int drm__objects_info(char *buf, char **start, off_t offset, int request,
441                           int *eof, void *data)
442 {
443         struct drm_minor *minor = (struct drm_minor *) data; 
444         struct drm_device *dev = minor->dev;
445         int len = 0;
446         struct drm_buffer_manager *bm = &dev->bm;
447         struct drm_fence_manager *fm = &dev->fm;
448         uint64_t used_mem;
449         uint64_t low_mem;
450         uint64_t high_mem;
451
452
453         if (offset > DRM_PROC_LIMIT) {
454                 *eof = 1;
455                 return 0;
456         }
457
458         *start = &buf[offset];
459         *eof = 0;
460
461         DRM_PROC_PRINT("Object accounting:\n\n");
462         if (fm->initialized) {
463                 DRM_PROC_PRINT("Number of active fence objects: %d.\n",
464                                atomic_read(&fm->count));
465         } else {
466                 DRM_PROC_PRINT("Fence objects are not supported by this driver\n");
467         }
468
469         if (bm->initialized) {
470                 DRM_PROC_PRINT("Number of active buffer objects: %d.\n\n",
471                                atomic_read(&bm->count));
472         }
473         DRM_PROC_PRINT("Memory accounting:\n\n");
474         if (bm->initialized) {
475                 DRM_PROC_PRINT("Number of locked GATT pages: %lu.\n", bm->cur_pages);
476         } else {
477                 DRM_PROC_PRINT("Buffer objects are not supported by this driver.\n");
478         }
479
480         drm_query_memctl(&used_mem, &low_mem, &high_mem);
481
482         if (used_mem > 16*PAGE_SIZE) {
483                 DRM_PROC_PRINT("Used object memory is %lu pages.\n",
484                                (unsigned long) (used_mem >> PAGE_SHIFT));
485         } else {
486                 DRM_PROC_PRINT("Used object memory is %lu bytes.\n",
487                                (unsigned long) used_mem);
488         }
489         DRM_PROC_PRINT("Soft object memory usage threshold is %lu pages.\n",
490                        (unsigned long) (low_mem >> PAGE_SHIFT));
491         DRM_PROC_PRINT("Hard object memory usage threshold is %lu pages.\n",
492                        (unsigned long) (high_mem >> PAGE_SHIFT));
493
494         DRM_PROC_PRINT("\n");
495
496         if (len > request + offset)
497                 return request;
498         *eof = 1;
499         return len - offset;
500 }
501
502 /**
503  * Simply calls _objects_info() while holding the drm_device::struct_mutex lock.
504  */
505 static int drm_objects_info(char *buf, char **start, off_t offset, int request,
506                          int *eof, void *data)
507 {
508         struct drm_minor *minor = (struct drm_minor *) data; 
509         struct drm_device *dev = minor->dev;
510         int ret;
511
512         mutex_lock(&dev->struct_mutex);
513         ret = drm__objects_info(buf, start, offset, request, eof, data);
514         mutex_unlock(&dev->struct_mutex);
515         return ret;
516 }
517
518 /**
519  * Called when "/proc/dri/.../clients" is read.
520  *
521  * \param buf output buffer.
522  * \param start start of output data.
523  * \param offset requested start offset.
524  * \param request requested number of bytes.
525  * \param eof whether there is no more data to return.
526  * \param data private data.
527  * \return number of written bytes.
528  */
529 static int drm__clients_info(char *buf, char **start, off_t offset,
530                              int request, int *eof, void *data)
531 {
532         struct drm_minor *minor = (struct drm_minor *) data; 
533         struct drm_device *dev = minor->dev;
534         int len = 0;
535         struct drm_file *priv;
536
537         if (offset > DRM_PROC_LIMIT) {
538                 *eof = 1;
539                 return 0;
540         }
541
542         *start = &buf[offset];
543         *eof = 0;
544
545         DRM_PROC_PRINT("a dev   pid    uid      magic     ioctls\n\n");
546         list_for_each_entry(priv, &dev->filelist, lhead) {
547                 DRM_PROC_PRINT("%c %3d %5d %5d %10u %10lu\n",
548                                priv->authenticated ? 'y' : 'n',
549                                priv->minor->index,
550                                priv->pid,
551                                priv->uid, priv->magic, priv->ioctl_count);
552         }
553
554         if (len > request + offset)
555                 return request;
556         *eof = 1;
557         return len - offset;
558 }
559
560 /**
561  * Simply calls _clients_info() while holding the drm_device::struct_mutex lock.
562  */
563 static int drm_clients_info(char *buf, char **start, off_t offset,
564                             int request, int *eof, void *data)
565 {
566         struct drm_minor *minor = (struct drm_minor *) data; 
567         struct drm_device *dev = minor->dev;
568         int ret;
569
570         mutex_lock(&dev->struct_mutex);
571         ret = drm__clients_info(buf, start, offset, request, eof, data);
572         mutex_unlock(&dev->struct_mutex);
573         return ret;
574 }
575
576 #if DRM_DEBUG_CODE
577
578 static int drm__vma_info(char *buf, char **start, off_t offset, int request,
579                          int *eof, void *data)
580 {
581         struct drm_minor *minor = (struct drm_minor *) data; 
582         struct drm_device *dev = minor->dev;
583         int len = 0;
584         struct drm_vma_entry *pt;
585         struct vm_area_struct *vma;
586 #if defined(__i386__)
587         unsigned int pgprot;
588 #endif
589
590         if (offset > DRM_PROC_LIMIT) {
591                 *eof = 1;
592                 return 0;
593         }
594
595         *start = &buf[offset];
596         *eof = 0;
597
598         DRM_PROC_PRINT("vma use count: %d, high_memory = %p, 0x%08lx\n",
599                        atomic_read(&dev->vma_count),
600                        high_memory, virt_to_phys(high_memory));
601         list_for_each_entry(pt, &dev->vmalist, head) {
602                 if (!(vma = pt->vma))
603                         continue;
604                 DRM_PROC_PRINT("\n%5d 0x%08lx-0x%08lx %c%c%c%c%c%c 0x%08lx000",
605                                pt->pid,
606                                vma->vm_start,
607                                vma->vm_end,
608                                vma->vm_flags & VM_READ ? 'r' : '-',
609                                vma->vm_flags & VM_WRITE ? 'w' : '-',
610                                vma->vm_flags & VM_EXEC ? 'x' : '-',
611                                vma->vm_flags & VM_MAYSHARE ? 's' : 'p',
612                                vma->vm_flags & VM_LOCKED ? 'l' : '-',
613                                vma->vm_flags & VM_IO ? 'i' : '-',
614                                vma->vm_pgoff);
615
616 #if defined(__i386__)
617                 pgprot = pgprot_val(vma->vm_page_prot);
618                 DRM_PROC_PRINT(" %c%c%c%c%c%c%c%c%c",
619                                pgprot & _PAGE_PRESENT ? 'p' : '-',
620                                pgprot & _PAGE_RW ? 'w' : 'r',
621                                pgprot & _PAGE_USER ? 'u' : 's',
622                                pgprot & _PAGE_PWT ? 't' : 'b',
623                                pgprot & _PAGE_PCD ? 'u' : 'c',
624                                pgprot & _PAGE_ACCESSED ? 'a' : '-',
625                                pgprot & _PAGE_DIRTY ? 'd' : '-',
626                                pgprot & _PAGE_PSE ? 'm' : 'k',
627                                pgprot & _PAGE_GLOBAL ? 'g' : 'l');
628 #endif
629                 DRM_PROC_PRINT("\n");
630         }
631
632         if (len > request + offset)
633                 return request;
634         *eof = 1;
635         return len - offset;
636 }
637
638 static int drm_vma_info(char *buf, char **start, off_t offset, int request,
639                         int *eof, void *data)
640 {
641         struct drm_minor *minor = (struct drm_minor *) data; 
642         struct drm_device *dev = minor->dev;
643         int ret;
644
645         mutex_lock(&dev->struct_mutex);
646         ret = drm__vma_info(buf, start, offset, request, eof, data);
647         mutex_unlock(&dev->struct_mutex);
648         return ret;
649 }
650 #endif