OSDN Git Service

drm: convert drawable handling to use Linux idr
[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(drm_device_t * dev, int minor,
94                   struct proc_dir_entry *root, struct proc_dir_entry **dev_root)
95 {
96         struct proc_dir_entry *ent;
97         int i, j;
98         char name[64];
99
100         sprintf(name, "%d", minor);
101         *dev_root = proc_mkdir(name, root);
102         if (!*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, *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                                                   *dev_root);
116                         remove_proc_entry(name, root);
117                         return -1;
118                 }
119                 ent->read_proc = drm_proc_list[i].f;
120                 ent->data = dev;
121         }
122         return 0;
123 }
124
125 /**
126  * Cleanup the proc filesystem resources.
127  *
128  * \param minor device minor number.
129  * \param root DRI proc dir entry.
130  * \param dev_root DRI device proc dir entry.
131  * \return always zero.
132  *
133  * Remove all proc entries created by proc_init().
134  */
135 int drm_proc_cleanup(int minor, struct proc_dir_entry *root,
136                      struct proc_dir_entry *dev_root)
137 {
138         int i;
139         char name[64];
140
141         if (!root || !dev_root)
142                 return 0;
143
144         for (i = 0; i < DRM_PROC_ENTRIES; i++)
145                 remove_proc_entry(drm_proc_list[i].name, dev_root);
146         sprintf(name, "%d", minor);
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         drm_device_t *dev = (drm_device_t *) data;
169         int len = 0;
170
171         if (offset > DRM_PROC_LIMIT) {
172                 *eof = 1;
173                 return 0;
174         }
175
176         *start = &buf[offset];
177         *eof = 0;
178
179         if (dev->unique) {
180                 DRM_PROC_PRINT("%s %s %s\n",
181                                dev->driver->pci_driver.name,
182                                pci_name(dev->pdev), dev->unique);
183         } else {
184                 DRM_PROC_PRINT("%s %s\n", dev->driver->pci_driver.name,
185                                pci_name(dev->pdev));
186         }
187
188         if (len > request + offset)
189                 return request;
190         *eof = 1;
191         return len - offset;
192 }
193
194 /**
195  * Called when "/proc/dri/.../vm" is read.
196  *
197  * \param buf output buffer.
198  * \param start start of output data.
199  * \param offset requested start offset.
200  * \param request requested number of bytes.
201  * \param eof whether there is no more data to return.
202  * \param data private data.
203  * \return number of written bytes.
204  *
205  * Prints information about all mappings in drm_device::maplist.
206  */
207 static int drm__vm_info(char *buf, char **start, off_t offset, int request,
208                         int *eof, void *data)
209 {
210         drm_device_t *dev = (drm_device_t *) data;
211         int len = 0;
212         drm_map_t *map;
213         drm_map_list_t *r_list;
214
215         /* Hardcoded from _DRM_FRAME_BUFFER,
216            _DRM_REGISTERS, _DRM_SHM, _DRM_AGP,
217            _DRM_SCATTER_GATHER, and _DRM_CONSISTENT. */
218         const char *types[] = { "FB", "REG", "SHM", "AGP", "SG", "PCI" };
219         const char *type;
220         int i;
221
222         if (offset > DRM_PROC_LIMIT) {
223                 *eof = 1;
224                 return 0;
225         }
226
227         *start = &buf[offset];
228         *eof = 0;
229
230         DRM_PROC_PRINT("slot     offset       size type flags    "
231                        "address mtrr\n\n");
232         i = 0;
233         list_for_each_entry(r_list, &dev->maplist, 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/.../objects" 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__objects_info(char *buf, char **start, off_t offset, int request,
433                           int *eof, void *data)
434 {
435         drm_device_t *dev = (drm_device_t *) data;
436         int len = 0;
437         drm_buffer_manager_t *bm = &dev->bm;
438         drm_fence_manager_t *fm = &dev->fm;
439         drm_u64_t used_mem;
440         drm_u64_t low_mem;
441         drm_u64_t high_mem;
442
443
444         if (offset > DRM_PROC_LIMIT) {
445                 *eof = 1;
446                 return 0;
447         }
448
449         *start = &buf[offset];
450         *eof = 0;
451
452         DRM_PROC_PRINT("Object accounting:\n\n");
453         if (fm->initialized) {
454                 DRM_PROC_PRINT("Number of active fence objects: %d.\n",
455                                atomic_read(&fm->count));
456         } else {
457                 DRM_PROC_PRINT("Fence objects are not supported by this driver\n");
458         }
459
460         if (bm->initialized) {
461                 DRM_PROC_PRINT("Number of active buffer objects: %d.\n\n",
462                                atomic_read(&bm->count));
463         }
464         DRM_PROC_PRINT("Memory accounting:\n\n");
465         if (bm->initialized) {
466                 DRM_PROC_PRINT("Number of locked GATT pages: %lu.\n", bm->cur_pages);
467         } else {
468                 DRM_PROC_PRINT("Buffer objects are not supported by this driver.\n");
469         }
470
471         drm_query_memctl(&used_mem, &low_mem, &high_mem);
472
473         if (used_mem > 16*PAGE_SIZE) {
474                 DRM_PROC_PRINT("Used object memory is %lu pages.\n",
475                                (unsigned long) (used_mem >> PAGE_SHIFT));
476         } else {
477                 DRM_PROC_PRINT("Used object memory is %lu bytes.\n",
478                                (unsigned long) used_mem);
479         }
480         DRM_PROC_PRINT("Soft object memory usage threshold is %lu pages.\n",
481                        (unsigned long) (low_mem >> PAGE_SHIFT));
482         DRM_PROC_PRINT("Hard object memory usage threshold is %lu pages.\n",
483                        (unsigned long) (high_mem >> PAGE_SHIFT));
484
485         DRM_PROC_PRINT("\n");
486
487         if (len > request + offset)
488                 return request;
489         *eof = 1;
490         return len - offset;
491 }
492
493 /**
494  * Simply calls _objects_info() while holding the drm_device::struct_mutex lock.
495  */
496 static int drm_objects_info(char *buf, char **start, off_t offset, int request,
497                          int *eof, void *data)
498 {
499         drm_device_t *dev = (drm_device_t *) data;
500         int ret;
501
502         mutex_lock(&dev->struct_mutex);
503         ret = drm__objects_info(buf, start, offset, request, eof, data);
504         mutex_unlock(&dev->struct_mutex);
505         return ret;
506 }
507
508 /**
509  * Called when "/proc/dri/.../clients" is read.
510  *
511  * \param buf output buffer.
512  * \param start start of output data.
513  * \param offset requested start offset.
514  * \param request requested number of bytes.
515  * \param eof whether there is no more data to return.
516  * \param data private data.
517  * \return number of written bytes.
518  */
519 static int drm__clients_info(char *buf, char **start, off_t offset,
520                              int request, int *eof, void *data)
521 {
522         drm_device_t *dev = (drm_device_t *) data;
523         int len = 0;
524         drm_file_t *priv;
525
526         if (offset > DRM_PROC_LIMIT) {
527                 *eof = 1;
528                 return 0;
529         }
530
531         *start = &buf[offset];
532         *eof = 0;
533
534         DRM_PROC_PRINT("a dev   pid    uid      magic     ioctls\n\n");
535         list_for_each_entry(priv, &dev->filelist, lhead) {
536                 DRM_PROC_PRINT("%c %3d %5d %5d %10u %10lu\n",
537                                priv->authenticated ? 'y' : 'n',
538                                priv->minor,
539                                priv->pid,
540                                priv->uid, priv->magic, priv->ioctl_count);
541         }
542
543         if (len > request + offset)
544                 return request;
545         *eof = 1;
546         return len - offset;
547 }
548
549 /**
550  * Simply calls _clients_info() while holding the drm_device::struct_mutex lock.
551  */
552 static int drm_clients_info(char *buf, char **start, off_t offset,
553                             int request, int *eof, void *data)
554 {
555         drm_device_t *dev = (drm_device_t *) data;
556         int ret;
557
558         mutex_lock(&dev->struct_mutex);
559         ret = drm__clients_info(buf, start, offset, request, eof, data);
560         mutex_unlock(&dev->struct_mutex);
561         return ret;
562 }
563
564 #if DRM_DEBUG_CODE
565
566 static int drm__vma_info(char *buf, char **start, off_t offset, int request,
567                          int *eof, void *data)
568 {
569         drm_device_t *dev = (drm_device_t *) data;
570         int len = 0;
571         drm_vma_entry_t *pt;
572         struct vm_area_struct *vma;
573 #if defined(__i386__)
574         unsigned int pgprot;
575 #endif
576
577         if (offset > DRM_PROC_LIMIT) {
578                 *eof = 1;
579                 return 0;
580         }
581
582         *start = &buf[offset];
583         *eof = 0;
584
585         DRM_PROC_PRINT("vma use count: %d, high_memory = %p, 0x%08lx\n",
586                        atomic_read(&dev->vma_count),
587                        high_memory, virt_to_phys(high_memory));
588         list_for_each_entry(pt, &dev->vmalist, head) {
589                 if (!(vma = pt->vma))
590                         continue;
591                 DRM_PROC_PRINT("\n%5d 0x%08lx-0x%08lx %c%c%c%c%c%c 0x%08lx000",
592                                pt->pid,
593                                vma->vm_start,
594                                vma->vm_end,
595                                vma->vm_flags & VM_READ ? 'r' : '-',
596                                vma->vm_flags & VM_WRITE ? 'w' : '-',
597                                vma->vm_flags & VM_EXEC ? 'x' : '-',
598                                vma->vm_flags & VM_MAYSHARE ? 's' : 'p',
599                                vma->vm_flags & VM_LOCKED ? 'l' : '-',
600                                vma->vm_flags & VM_IO ? 'i' : '-',
601                                vma->vm_pgoff);
602
603 #if defined(__i386__)
604                 pgprot = pgprot_val(vma->vm_page_prot);
605                 DRM_PROC_PRINT(" %c%c%c%c%c%c%c%c%c",
606                                pgprot & _PAGE_PRESENT ? 'p' : '-',
607                                pgprot & _PAGE_RW ? 'w' : 'r',
608                                pgprot & _PAGE_USER ? 'u' : 's',
609                                pgprot & _PAGE_PWT ? 't' : 'b',
610                                pgprot & _PAGE_PCD ? 'u' : 'c',
611                                pgprot & _PAGE_ACCESSED ? 'a' : '-',
612                                pgprot & _PAGE_DIRTY ? 'd' : '-',
613                                pgprot & _PAGE_PSE ? 'm' : 'k',
614                                pgprot & _PAGE_GLOBAL ? 'g' : 'l');
615 #endif
616                 DRM_PROC_PRINT("\n");
617         }
618
619         if (len > request + offset)
620                 return request;
621         *eof = 1;
622         return len - offset;
623 }
624
625 static int drm_vma_info(char *buf, char **start, off_t offset, int request,
626                         int *eof, void *data)
627 {
628         drm_device_t *dev = (drm_device_t *) data;
629         int ret;
630
631         mutex_lock(&dev->struct_mutex);
632         ret = drm__vma_info(buf, start, offset, request, eof, data);
633         mutex_unlock(&dev->struct_mutex);
634         return ret;
635 }
636 #endif