Linux v2.6.15
[safe/jmp/linux-2.6] / drivers / char / drm / 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         {
65         "name", drm_name_info}, {
66         "mem", drm_mem_info}, {
67         "vm", drm_vm_info}, {
68         "clients", drm_clients_info}, {
69         "queues", drm_queues_info}, {
70         "bufs", drm_bufs_info},
71 #if DRM_DEBUG_CODE
72         {
73         "vma", drm_vma_info},
74 #endif
75 };
76
77 #define DRM_PROC_ENTRIES (sizeof(drm_proc_list)/sizeof(drm_proc_list[0]))
78
79 /**
80  * Initialize the DRI proc filesystem for a device.
81  *
82  * \param dev DRM device.
83  * \param minor device minor number.
84  * \param root DRI proc dir entry.
85  * \param dev_root resulting DRI device proc dir entry.
86  * \return root entry pointer on success, or NULL on failure.
87  *
88  * Create the DRI proc root entry "/proc/dri", the device proc root entry
89  * "/proc/dri/%minor%/", and each entry in proc_list as
90  * "/proc/dri/%minor%/%name%".
91  */
92 int drm_proc_init(drm_device_t * dev, int minor,
93                   struct proc_dir_entry *root, struct proc_dir_entry **dev_root)
94 {
95         struct proc_dir_entry *ent;
96         int i, j;
97         char name[64];
98
99         sprintf(name, "%d", minor);
100         *dev_root = proc_mkdir(name, root);
101         if (!*dev_root) {
102                 DRM_ERROR("Cannot create /proc/dri/%s\n", name);
103                 return -1;
104         }
105
106         for (i = 0; i < DRM_PROC_ENTRIES; i++) {
107                 ent = create_proc_entry(drm_proc_list[i].name,
108                                         S_IFREG | S_IRUGO, *dev_root);
109                 if (!ent) {
110                         DRM_ERROR("Cannot create /proc/dri/%s/%s\n",
111                                   name, drm_proc_list[i].name);
112                         for (j = 0; j < i; j++)
113                                 remove_proc_entry(drm_proc_list[i].name,
114                                                   *dev_root);
115                         remove_proc_entry(name, root);
116                         return -1;
117                 }
118                 ent->read_proc = drm_proc_list[i].f;
119                 ent->data = dev;
120         }
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         struct list_head *list;
215
216         /* Hardcoded from _DRM_FRAME_BUFFER,
217            _DRM_REGISTERS, _DRM_SHM, _DRM_AGP, and
218            _DRM_SCATTER_GATHER and _DRM_CONSISTENT */
219         const char *types[] = { "FB", "REG", "SHM", "AGP", "SG", "PCI" };
220         const char *type;
221         int i;
222
223         if (offset > DRM_PROC_LIMIT) {
224                 *eof = 1;
225                 return 0;
226         }
227
228         *start = &buf[offset];
229         *eof = 0;
230
231         DRM_PROC_PRINT("slot     offset       size type flags    "
232                        "address mtrr\n\n");
233         i = 0;
234         if (dev->maplist != NULL)
235                 list_for_each(list, &dev->maplist->head) {
236                 r_list = list_entry(list, drm_map_list_t, 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%08x ",
245                                i,
246                                map->offset,
247                                map->size, type, map->flags, r_list->user_token);
248                 if (map->mtrr < 0) {
249                         DRM_PROC_PRINT("none\n");
250                 } else {
251                         DRM_PROC_PRINT("%4d\n", map->mtrr);
252                 }
253                 i++;
254                 }
255
256         if (len > request + offset)
257                 return request;
258         *eof = 1;
259         return len - offset;
260 }
261
262 /**
263  * Simply calls _vm_info() while holding the drm_device::struct_sem lock.
264  */
265 static int drm_vm_info(char *buf, char **start, off_t offset, int request,
266                        int *eof, void *data)
267 {
268         drm_device_t *dev = (drm_device_t *) data;
269         int ret;
270
271         down(&dev->struct_sem);
272         ret = drm__vm_info(buf, start, offset, request, eof, data);
273         up(&dev->struct_sem);
274         return ret;
275 }
276
277 /**
278  * Called when "/proc/dri/.../queues" is read.
279  *
280  * \param buf output buffer.
281  * \param start start of output data.
282  * \param offset requested start offset.
283  * \param request requested number of bytes.
284  * \param eof whether there is no more data to return.
285  * \param data private data.
286  * \return number of written bytes.
287  */
288 static int drm__queues_info(char *buf, char **start, off_t offset,
289                             int request, int *eof, void *data)
290 {
291         drm_device_t *dev = (drm_device_t *) data;
292         int len = 0;
293         int i;
294         drm_queue_t *q;
295
296         if (offset > DRM_PROC_LIMIT) {
297                 *eof = 1;
298                 return 0;
299         }
300
301         *start = &buf[offset];
302         *eof = 0;
303
304         DRM_PROC_PRINT("  ctx/flags   use   fin"
305                        "   blk/rw/rwf  wait    flushed     queued"
306                        "      locks\n\n");
307         for (i = 0; i < dev->queue_count; i++) {
308                 q = dev->queuelist[i];
309                 atomic_inc(&q->use_count);
310                 DRM_PROC_PRINT_RET(atomic_dec(&q->use_count),
311                                    "%5d/0x%03x %5d %5d"
312                                    " %5d/%c%c/%c%c%c %5Zd\n",
313                                    i,
314                                    q->flags,
315                                    atomic_read(&q->use_count),
316                                    atomic_read(&q->finalization),
317                                    atomic_read(&q->block_count),
318                                    atomic_read(&q->block_read) ? 'r' : '-',
319                                    atomic_read(&q->block_write) ? 'w' : '-',
320                                    waitqueue_active(&q->read_queue) ? 'r' : '-',
321                                    waitqueue_active(&q->
322                                                     write_queue) ? 'w' : '-',
323                                    waitqueue_active(&q->
324                                                     flush_queue) ? 'f' : '-',
325                                    DRM_BUFCOUNT(&q->waitlist));
326                 atomic_dec(&q->use_count);
327         }
328
329         if (len > request + offset)
330                 return request;
331         *eof = 1;
332         return len - offset;
333 }
334
335 /**
336  * Simply calls _queues_info() while holding the drm_device::struct_sem lock.
337  */
338 static int drm_queues_info(char *buf, char **start, off_t offset, int request,
339                            int *eof, void *data)
340 {
341         drm_device_t *dev = (drm_device_t *) data;
342         int ret;
343
344         down(&dev->struct_sem);
345         ret = drm__queues_info(buf, start, offset, request, eof, data);
346         up(&dev->struct_sem);
347         return ret;
348 }
349
350 /**
351  * Called when "/proc/dri/.../bufs" is read.
352  *
353  * \param buf output buffer.
354  * \param start start of output data.
355  * \param offset requested start offset.
356  * \param request requested number of bytes.
357  * \param eof whether there is no more data to return.
358  * \param data private data.
359  * \return number of written bytes.
360  */
361 static int drm__bufs_info(char *buf, char **start, off_t offset, int request,
362                           int *eof, void *data)
363 {
364         drm_device_t *dev = (drm_device_t *) data;
365         int len = 0;
366         drm_device_dma_t *dma = dev->dma;
367         int i;
368
369         if (!dma || offset > DRM_PROC_LIMIT) {
370                 *eof = 1;
371                 return 0;
372         }
373
374         *start = &buf[offset];
375         *eof = 0;
376
377         DRM_PROC_PRINT(" o     size count  free  segs pages    kB\n\n");
378         for (i = 0; i <= DRM_MAX_ORDER; i++) {
379                 if (dma->bufs[i].buf_count)
380                         DRM_PROC_PRINT("%2d %8d %5d %5d %5d %5d %5ld\n",
381                                        i,
382                                        dma->bufs[i].buf_size,
383                                        dma->bufs[i].buf_count,
384                                        atomic_read(&dma->bufs[i]
385                                                    .freelist.count),
386                                        dma->bufs[i].seg_count,
387                                        dma->bufs[i].seg_count
388                                        * (1 << dma->bufs[i].page_order),
389                                        (dma->bufs[i].seg_count
390                                         * (1 << dma->bufs[i].page_order))
391                                        * PAGE_SIZE / 1024);
392         }
393         DRM_PROC_PRINT("\n");
394         for (i = 0; i < dma->buf_count; i++) {
395                 if (i && !(i % 32))
396                         DRM_PROC_PRINT("\n");
397                 DRM_PROC_PRINT(" %d", dma->buflist[i]->list);
398         }
399         DRM_PROC_PRINT("\n");
400
401         if (len > request + offset)
402                 return request;
403         *eof = 1;
404         return len - offset;
405 }
406
407 /**
408  * Simply calls _bufs_info() while holding the drm_device::struct_sem lock.
409  */
410 static int drm_bufs_info(char *buf, char **start, off_t offset, int request,
411                          int *eof, void *data)
412 {
413         drm_device_t *dev = (drm_device_t *) data;
414         int ret;
415
416         down(&dev->struct_sem);
417         ret = drm__bufs_info(buf, start, offset, request, eof, data);
418         up(&dev->struct_sem);
419         return ret;
420 }
421
422 /**
423  * Called when "/proc/dri/.../clients" is read.
424  *
425  * \param buf output buffer.
426  * \param start start of output data.
427  * \param offset requested start offset.
428  * \param request requested number of bytes.
429  * \param eof whether there is no more data to return.
430  * \param data private data.
431  * \return number of written bytes.
432  */
433 static int drm__clients_info(char *buf, char **start, off_t offset,
434                              int request, int *eof, void *data)
435 {
436         drm_device_t *dev = (drm_device_t *) data;
437         int len = 0;
438         drm_file_t *priv;
439
440         if (offset > DRM_PROC_LIMIT) {
441                 *eof = 1;
442                 return 0;
443         }
444
445         *start = &buf[offset];
446         *eof = 0;
447
448         DRM_PROC_PRINT("a dev   pid    uid      magic     ioctls\n\n");
449         for (priv = dev->file_first; priv; priv = priv->next) {
450                 DRM_PROC_PRINT("%c %3d %5d %5d %10u %10lu\n",
451                                priv->authenticated ? 'y' : 'n',
452                                priv->minor,
453                                priv->pid,
454                                priv->uid, priv->magic, priv->ioctl_count);
455         }
456
457         if (len > request + offset)
458                 return request;
459         *eof = 1;
460         return len - offset;
461 }
462
463 /**
464  * Simply calls _clients_info() while holding the drm_device::struct_sem lock.
465  */
466 static int drm_clients_info(char *buf, char **start, off_t offset,
467                             int request, int *eof, void *data)
468 {
469         drm_device_t *dev = (drm_device_t *) data;
470         int ret;
471
472         down(&dev->struct_sem);
473         ret = drm__clients_info(buf, start, offset, request, eof, data);
474         up(&dev->struct_sem);
475         return ret;
476 }
477
478 #if DRM_DEBUG_CODE
479
480 static int drm__vma_info(char *buf, char **start, off_t offset, int request,
481                          int *eof, void *data)
482 {
483         drm_device_t *dev = (drm_device_t *) data;
484         int len = 0;
485         drm_vma_entry_t *pt;
486         struct vm_area_struct *vma;
487 #if defined(__i386__)
488         unsigned int pgprot;
489 #endif
490
491         if (offset > DRM_PROC_LIMIT) {
492                 *eof = 1;
493                 return 0;
494         }
495
496         *start = &buf[offset];
497         *eof = 0;
498
499         DRM_PROC_PRINT("vma use count: %d, high_memory = %p, 0x%08lx\n",
500                        atomic_read(&dev->vma_count),
501                        high_memory, virt_to_phys(high_memory));
502         for (pt = dev->vmalist; pt; pt = pt->next) {
503                 if (!(vma = pt->vma))
504                         continue;
505                 DRM_PROC_PRINT("\n%5d 0x%08lx-0x%08lx %c%c%c%c%c%c 0x%08lx",
506                                pt->pid,
507                                vma->vm_start,
508                                vma->vm_end,
509                                vma->vm_flags & VM_READ ? 'r' : '-',
510                                vma->vm_flags & VM_WRITE ? 'w' : '-',
511                                vma->vm_flags & VM_EXEC ? 'x' : '-',
512                                vma->vm_flags & VM_MAYSHARE ? 's' : 'p',
513                                vma->vm_flags & VM_LOCKED ? 'l' : '-',
514                                vma->vm_flags & VM_IO ? 'i' : '-',
515                                VM_OFFSET(vma));
516
517 #if defined(__i386__)
518                 pgprot = pgprot_val(vma->vm_page_prot);
519                 DRM_PROC_PRINT(" %c%c%c%c%c%c%c%c%c",
520                                pgprot & _PAGE_PRESENT ? 'p' : '-',
521                                pgprot & _PAGE_RW ? 'w' : 'r',
522                                pgprot & _PAGE_USER ? 'u' : 's',
523                                pgprot & _PAGE_PWT ? 't' : 'b',
524                                pgprot & _PAGE_PCD ? 'u' : 'c',
525                                pgprot & _PAGE_ACCESSED ? 'a' : '-',
526                                pgprot & _PAGE_DIRTY ? 'd' : '-',
527                                pgprot & _PAGE_PSE ? 'm' : 'k',
528                                pgprot & _PAGE_GLOBAL ? 'g' : 'l');
529 #endif
530                 DRM_PROC_PRINT("\n");
531         }
532
533         if (len > request + offset)
534                 return request;
535         *eof = 1;
536         return len - offset;
537 }
538
539 static int drm_vma_info(char *buf, char **start, off_t offset, int request,
540                         int *eof, void *data)
541 {
542         drm_device_t *dev = (drm_device_t *) data;
543         int ret;
544
545         down(&dev->struct_sem);
546         ret = drm__vma_info(buf, start, offset, request, eof, data);
547         up(&dev->struct_sem);
548         return ret;
549 }
550 #endif