hc
2024-08-16 a24a44ff9ca902811b99aa9663d697cf452e08ef
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
// SPDX-License-Identifier: GPL-2.0
/*
 * Framework for userspace DMA-BUF allocations
 *
 * Copyright (C) 2011 Google, Inc.
 * Copyright (C) 2019 Linaro Ltd.
 * Copyright (C) 2022 Rockchip Electronics Co. Ltd.
 * Author: Simon Xue <xxm@rock-chips.com>
 */
 
#include <linux/cma.h>
#include <linux/cdev.h>
#include <linux/debugfs.h>
#include <linux/device.h>
#include <linux/dma-buf.h>
#include <linux/dma-resv.h>
#include <linux/dma-map-ops.h>
#include <linux/err.h>
#include <linux/xarray.h>
#include <linux/list.h>
#include <linux/proc_fs.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/syscalls.h>
#include <uapi/linux/rk-dma-heap.h>
 
#include "rk-dma-heap.h"
 
#define DEVNAME "rk_dma_heap"
 
#define NUM_HEAP_MINORS 128
 
static LIST_HEAD(rk_heap_list);
static DEFINE_MUTEX(rk_heap_list_lock);
static dev_t rk_dma_heap_devt;
static struct class *rk_dma_heap_class;
static DEFINE_XARRAY_ALLOC(rk_dma_heap_minors);
struct proc_dir_entry *proc_rk_dma_heap_dir;
 
#define K(size) ((unsigned long)((size) >> 10))
 
static int rk_vmap_pfn_apply(pte_t *pte, unsigned long addr, void *private)
{
   struct rk_vmap_pfn_data *data = private;
 
   *pte = pte_mkspecial(pfn_pte(data->pfn++, data->prot));
   return 0;
}
 
void *rk_vmap_contig_pfn(unsigned long pfn, unsigned int count, pgprot_t prot)
{
   struct rk_vmap_pfn_data data = { .pfn = pfn, .prot = pgprot_nx(prot) };
   struct vm_struct *area;
 
   area = get_vm_area_caller(count * PAGE_SIZE, VM_MAP,
           __builtin_return_address(0));
   if (!area)
       return NULL;
   if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
           count * PAGE_SIZE, rk_vmap_pfn_apply, &data)) {
       free_vm_area(area);
       return NULL;
   }
   return area->addr;
}
 
int rk_dma_heap_set_dev(struct device *heap_dev)
{
   int err = 0;
 
   if (!heap_dev)
       return -EINVAL;
 
   dma_coerce_mask_and_coherent(heap_dev, DMA_BIT_MASK(64));
 
   if (!heap_dev->dma_parms) {
       heap_dev->dma_parms = devm_kzalloc(heap_dev,
                          sizeof(*heap_dev->dma_parms),
                          GFP_KERNEL);
       if (!heap_dev->dma_parms)
           return -ENOMEM;
 
       err = dma_set_max_seg_size(heap_dev, (unsigned int)DMA_BIT_MASK(64));
       if (err) {
           devm_kfree(heap_dev, heap_dev->dma_parms);
           dev_err(heap_dev, "Failed to set DMA segment size, err:%d\n", err);
           return err;
       }
   }
 
   return 0;
}
EXPORT_SYMBOL_GPL(rk_dma_heap_set_dev);
 
struct rk_dma_heap *rk_dma_heap_find(const char *name)
{
   struct rk_dma_heap *h;
 
   mutex_lock(&rk_heap_list_lock);
   list_for_each_entry(h, &rk_heap_list, list) {
       if (!strcmp(h->name, name)) {
           kref_get(&h->refcount);
           mutex_unlock(&rk_heap_list_lock);
           return h;
       }
   }
   mutex_unlock(&rk_heap_list_lock);
   return NULL;
}
EXPORT_SYMBOL_GPL(rk_dma_heap_find);
 
void rk_dma_heap_buffer_free(struct dma_buf *dmabuf)
{
   dma_buf_put(dmabuf);
}
EXPORT_SYMBOL_GPL(rk_dma_heap_buffer_free);
 
struct dma_buf *rk_dma_heap_buffer_alloc(struct rk_dma_heap *heap, size_t len,
                    unsigned int fd_flags,
                    unsigned int heap_flags,
                    const char *name)
{
   struct dma_buf *dmabuf;
 
   if (fd_flags & ~RK_DMA_HEAP_VALID_FD_FLAGS)
       return ERR_PTR(-EINVAL);
 
   if (heap_flags & ~RK_DMA_HEAP_VALID_HEAP_FLAGS)
       return ERR_PTR(-EINVAL);
   /*
    * Allocations from all heaps have to begin
    * and end on page boundaries.
    */
   len = PAGE_ALIGN(len);
   if (!len)
       return ERR_PTR(-EINVAL);
 
   dmabuf = heap->ops->allocate(heap, len, fd_flags, heap_flags, name);
 
   if (IS_ENABLED(CONFIG_DMABUF_RK_HEAPS_DEBUG) && !IS_ERR(dmabuf))
       dma_buf_set_name(dmabuf, name);
 
   return dmabuf;
}
EXPORT_SYMBOL_GPL(rk_dma_heap_buffer_alloc);
 
int rk_dma_heap_bufferfd_alloc(struct rk_dma_heap *heap, size_t len,
                  unsigned int fd_flags,
                  unsigned int heap_flags,
                  const char *name)
{
   struct dma_buf *dmabuf;
   int fd;
 
   dmabuf = rk_dma_heap_buffer_alloc(heap, len, fd_flags, heap_flags,
                     name);
 
   if (IS_ERR(dmabuf))
       return PTR_ERR(dmabuf);
 
   fd = dma_buf_fd(dmabuf, fd_flags);
   if (fd < 0) {
       dma_buf_put(dmabuf);
       /* just return, as put will call release and that will free */
   }
 
   return fd;
 
}
EXPORT_SYMBOL_GPL(rk_dma_heap_bufferfd_alloc);
 
struct page *rk_dma_heap_alloc_contig_pages(struct rk_dma_heap *heap,
                       size_t len, const char *name)
{
   if (!heap->support_cma) {
       WARN_ON(!heap->support_cma);
       return ERR_PTR(-EINVAL);
   }
 
   len = PAGE_ALIGN(len);
   if (!len)
       return ERR_PTR(-EINVAL);
 
   return heap->ops->alloc_contig_pages(heap, len, name);
}
EXPORT_SYMBOL_GPL(rk_dma_heap_alloc_contig_pages);
 
void rk_dma_heap_free_contig_pages(struct rk_dma_heap *heap,
                  struct page *pages, size_t len,
                  const char *name)
{
   if (!heap->support_cma) {
       WARN_ON(!heap->support_cma);
       return;
   }
 
   return heap->ops->free_contig_pages(heap, pages, len, name);
}
EXPORT_SYMBOL_GPL(rk_dma_heap_free_contig_pages);
 
void rk_dma_heap_total_inc(struct rk_dma_heap *heap, size_t len)
{
   mutex_lock(&rk_heap_list_lock);
   heap->total_size += len;
   mutex_unlock(&rk_heap_list_lock);
}
 
void rk_dma_heap_total_dec(struct rk_dma_heap *heap, size_t len)
{
   mutex_lock(&rk_heap_list_lock);
   if (WARN_ON(heap->total_size < len))
       heap->total_size = 0;
   else
       heap->total_size -= len;
   mutex_unlock(&rk_heap_list_lock);
}
 
static int rk_dma_heap_open(struct inode *inode, struct file *file)
{
   struct rk_dma_heap *heap;
 
   heap = xa_load(&rk_dma_heap_minors, iminor(inode));
   if (!heap) {
       pr_err("dma_heap: minor %d unknown.\n", iminor(inode));
       return -ENODEV;
   }
 
   /* instance data as context */
   file->private_data = heap;
   nonseekable_open(inode, file);
 
   return 0;
}
 
static long rk_dma_heap_ioctl_allocate(struct file *file, void *data)
{
   struct rk_dma_heap_allocation_data *heap_allocation = data;
   struct rk_dma_heap *heap = file->private_data;
   int fd;
 
   if (heap_allocation->fd)
       return -EINVAL;
 
   fd = rk_dma_heap_bufferfd_alloc(heap, heap_allocation->len,
                   heap_allocation->fd_flags,
                   heap_allocation->heap_flags, NULL);
   if (fd < 0)
       return fd;
 
   heap_allocation->fd = fd;
 
   return 0;
}
 
static unsigned int rk_dma_heap_ioctl_cmds[] = {
   RK_DMA_HEAP_IOCTL_ALLOC,
};
 
static long rk_dma_heap_ioctl(struct file *file, unsigned int ucmd,
                 unsigned long arg)
{
   char stack_kdata[128];
   char *kdata = stack_kdata;
   unsigned int kcmd;
   unsigned int in_size, out_size, drv_size, ksize;
   int nr = _IOC_NR(ucmd);
   int ret = 0;
 
   if (nr >= ARRAY_SIZE(rk_dma_heap_ioctl_cmds))
       return -EINVAL;
 
   /* Get the kernel ioctl cmd that matches */
   kcmd = rk_dma_heap_ioctl_cmds[nr];
 
   /* Figure out the delta between user cmd size and kernel cmd size */
   drv_size = _IOC_SIZE(kcmd);
   out_size = _IOC_SIZE(ucmd);
   in_size = out_size;
   if ((ucmd & kcmd & IOC_IN) == 0)
       in_size = 0;
   if ((ucmd & kcmd & IOC_OUT) == 0)
       out_size = 0;
   ksize = max(max(in_size, out_size), drv_size);
 
   /* If necessary, allocate buffer for ioctl argument */
   if (ksize > sizeof(stack_kdata)) {
       kdata = kmalloc(ksize, GFP_KERNEL);
       if (!kdata)
           return -ENOMEM;
   }
 
   if (copy_from_user(kdata, (void __user *)arg, in_size) != 0) {
       ret = -EFAULT;
       goto err;
   }
 
   /* zero out any difference between the kernel/user structure size */
   if (ksize > in_size)
       memset(kdata + in_size, 0, ksize - in_size);
 
   switch (kcmd) {
   case RK_DMA_HEAP_IOCTL_ALLOC:
       ret = rk_dma_heap_ioctl_allocate(file, kdata);
       break;
   default:
       ret = -ENOTTY;
       goto err;
   }
 
   if (copy_to_user((void __user *)arg, kdata, out_size) != 0)
       ret = -EFAULT;
err:
   if (kdata != stack_kdata)
       kfree(kdata);
   return ret;
}
 
static const struct file_operations rk_dma_heap_fops = {
   .owner          = THIS_MODULE,
   .open        = rk_dma_heap_open,
   .unlocked_ioctl = rk_dma_heap_ioctl,
#ifdef CONFIG_COMPAT
   .compat_ioctl    = rk_dma_heap_ioctl,
#endif
};
 
/**
 * rk_dma_heap_get_drvdata() - get per-subdriver data for the heap
 * @heap: DMA-Heap to retrieve private data for
 *
 * Returns:
 * The per-subdriver data for the heap.
 */
void *rk_dma_heap_get_drvdata(struct rk_dma_heap *heap)
{
   return heap->priv;
}
 
static void rk_dma_heap_release(struct kref *ref)
{
   struct rk_dma_heap *heap = container_of(ref, struct rk_dma_heap, refcount);
   int minor = MINOR(heap->heap_devt);
 
   /* Note, we already holding the rk_heap_list_lock here */
   list_del(&heap->list);
 
   device_destroy(rk_dma_heap_class, heap->heap_devt);
   cdev_del(&heap->heap_cdev);
   xa_erase(&rk_dma_heap_minors, minor);
 
   kfree(heap);
}
 
void rk_dma_heap_put(struct rk_dma_heap *h)
{
   /*
    * Take the rk_heap_list_lock now to avoid racing with code
    * scanning the list and then taking a kref.
    */
   mutex_lock(&rk_heap_list_lock);
   kref_put(&h->refcount, rk_dma_heap_release);
   mutex_unlock(&rk_heap_list_lock);
}
 
/**
 * rk_dma_heap_get_dev() - get device struct for the heap
 * @heap: DMA-Heap to retrieve device struct from
 *
 * Returns:
 * The device struct for the heap.
 */
struct device *rk_dma_heap_get_dev(struct rk_dma_heap *heap)
{
   return heap->heap_dev;
}
 
/**
 * rk_dma_heap_get_name() - get heap name
 * @heap: DMA-Heap to retrieve private data for
 *
 * Returns:
 * The char* for the heap name.
 */
const char *rk_dma_heap_get_name(struct rk_dma_heap *heap)
{
   return heap->name;
}
 
struct rk_dma_heap *rk_dma_heap_add(const struct rk_dma_heap_export_info *exp_info)
{
   struct rk_dma_heap *heap, *err_ret;
   unsigned int minor;
   int ret;
 
   if (!exp_info->name || !strcmp(exp_info->name, "")) {
       pr_err("rk_dma_heap: Cannot add heap without a name\n");
       return ERR_PTR(-EINVAL);
   }
 
   if (!exp_info->ops || !exp_info->ops->allocate) {
       pr_err("rk_dma_heap: Cannot add heap with invalid ops struct\n");
       return ERR_PTR(-EINVAL);
   }
 
   /* check the name is unique */
   heap = rk_dma_heap_find(exp_info->name);
   if (heap) {
       pr_err("rk_dma_heap: Already registered heap named %s\n",
              exp_info->name);
       rk_dma_heap_put(heap);
       return ERR_PTR(-EINVAL);
   }
 
   heap = kzalloc(sizeof(*heap), GFP_KERNEL);
   if (!heap)
       return ERR_PTR(-ENOMEM);
 
   kref_init(&heap->refcount);
   heap->name = exp_info->name;
   heap->ops = exp_info->ops;
   heap->priv = exp_info->priv;
   heap->support_cma = exp_info->support_cma;
   INIT_LIST_HEAD(&heap->dmabuf_list);
   INIT_LIST_HEAD(&heap->contig_list);
   mutex_init(&heap->dmabuf_lock);
   mutex_init(&heap->contig_lock);
 
   /* Find unused minor number */
   ret = xa_alloc(&rk_dma_heap_minors, &minor, heap,
              XA_LIMIT(0, NUM_HEAP_MINORS - 1), GFP_KERNEL);
   if (ret < 0) {
       pr_err("rk_dma_heap: Unable to get minor number for heap\n");
       err_ret = ERR_PTR(ret);
       goto err0;
   }
 
   /* Create device */
   heap->heap_devt = MKDEV(MAJOR(rk_dma_heap_devt), minor);
 
   cdev_init(&heap->heap_cdev, &rk_dma_heap_fops);
   ret = cdev_add(&heap->heap_cdev, heap->heap_devt, 1);
   if (ret < 0) {
       pr_err("dma_heap: Unable to add char device\n");
       err_ret = ERR_PTR(ret);
       goto err1;
   }
 
   heap->heap_dev = device_create(rk_dma_heap_class,
                      NULL,
                      heap->heap_devt,
                      NULL,
                      heap->name);
   if (IS_ERR(heap->heap_dev)) {
       pr_err("rk_dma_heap: Unable to create device\n");
       err_ret = ERR_CAST(heap->heap_dev);
       goto err2;
   }
 
   heap->procfs = proc_rk_dma_heap_dir;
 
   /* Make sure it doesn't disappear on us */
   heap->heap_dev = get_device(heap->heap_dev);
 
   /* Add heap to the list */
   mutex_lock(&rk_heap_list_lock);
   list_add(&heap->list, &rk_heap_list);
   mutex_unlock(&rk_heap_list_lock);
 
   return heap;
 
err2:
   cdev_del(&heap->heap_cdev);
err1:
   xa_erase(&rk_dma_heap_minors, minor);
err0:
   kfree(heap);
   return err_ret;
}
 
static char *rk_dma_heap_devnode(struct device *dev, umode_t *mode)
{
   return kasprintf(GFP_KERNEL, "rk_dma_heap/%s", dev_name(dev));
}
 
static int rk_dma_heap_dump_dmabuf(const struct dma_buf *dmabuf, void *data)
{
   struct rk_dma_heap *heap = (struct rk_dma_heap *)data;
   struct rk_dma_heap_dmabuf *buf;
   struct dma_buf_attachment *a;
   phys_addr_t size;
   int attach_count;
   int ret;
 
   if (!strcmp(dmabuf->exp_name, heap->name)) {
       seq_printf(heap->s, "dma-heap:<%s> -dmabuf", heap->name);
       mutex_lock(&heap->dmabuf_lock);
       list_for_each_entry(buf, &heap->dmabuf_list, node) {
           if (buf->dmabuf->file->f_inode->i_ino ==
               dmabuf->file->f_inode->i_ino) {
               seq_printf(heap->s,
                      "\ti_ino = %ld\n",
                      dmabuf->file->f_inode->i_ino);
               size = buf->end - buf->start + 1;
               seq_printf(heap->s,
                      "\tAlloc by (%-20s)\t[%pa-%pa]\t%pa (%lu KiB)\n",
                      dmabuf->name, &buf->start,
                      &buf->end, &size, K(size));
               seq_puts(heap->s, "\t\tAttached Devices:\n");
               attach_count = 0;
               ret = dma_resv_lock_interruptible(dmabuf->resv,
                                 NULL);
               if (ret)
                   goto error_unlock;
               list_for_each_entry(a, &dmabuf->attachments,
                           node) {
                   seq_printf(heap->s, "\t\t%s\n",
                          dev_name(a->dev));
                   attach_count++;
               }
               dma_resv_unlock(dmabuf->resv);
               seq_printf(heap->s,
                      "Total %d devices attached\n\n",
                      attach_count);
           }
       }
       mutex_unlock(&heap->dmabuf_lock);
   }
 
   return 0;
error_unlock:
   mutex_unlock(&heap->dmabuf_lock);
   return ret;
}
 
static int rk_dma_heap_dump_contig(void *data)
{
   struct rk_dma_heap *heap = (struct rk_dma_heap *)data;
   struct rk_dma_heap_contig_buf *buf;
   phys_addr_t size;
 
   mutex_lock(&heap->contig_lock);
   list_for_each_entry(buf, &heap->contig_list, node) {
       size = buf->end - buf->start + 1;
       seq_printf(heap->s, "dma-heap:<%s> -non dmabuf\n", heap->name);
       seq_printf(heap->s, "\tAlloc by (%-20s)\t[%pa-%pa]\t%pa (%lu KiB)\n",
              buf->orig_alloc, &buf->start, &buf->end, &size, K(size));
   }
   mutex_unlock(&heap->contig_lock);
 
   return 0;
}
 
static ssize_t rk_total_pools_kb_show(struct kobject *kobj,
                     struct kobj_attribute *attr, char *buf)
{
   struct rk_dma_heap *heap;
   u64 total_pool_size = 0;
 
   mutex_lock(&rk_heap_list_lock);
   list_for_each_entry(heap, &rk_heap_list, list)
       if (heap->ops->get_pool_size)
           total_pool_size += heap->ops->get_pool_size(heap);
   mutex_unlock(&rk_heap_list_lock);
 
   return sysfs_emit(buf, "%llu\n", total_pool_size / 1024);
}
 
static struct kobj_attribute rk_total_pools_kb_attr =
   __ATTR_RO(rk_total_pools_kb);
 
static struct attribute *rk_dma_heap_sysfs_attrs[] = {
   &rk_total_pools_kb_attr.attr,
   NULL,
};
 
ATTRIBUTE_GROUPS(rk_dma_heap_sysfs);
 
static struct kobject *rk_dma_heap_kobject;
 
static int rk_dma_heap_sysfs_setup(void)
{
   int ret;
 
   rk_dma_heap_kobject = kobject_create_and_add("rk_dma_heap",
                            kernel_kobj);
   if (!rk_dma_heap_kobject)
       return -ENOMEM;
 
   ret = sysfs_create_groups(rk_dma_heap_kobject,
                 rk_dma_heap_sysfs_groups);
   if (ret) {
       kobject_put(rk_dma_heap_kobject);
       return ret;
   }
 
   return 0;
}
 
static void rk_dma_heap_sysfs_teardown(void)
{
   kobject_put(rk_dma_heap_kobject);
}
 
#ifdef CONFIG_DEBUG_FS
 
static struct dentry *rk_dma_heap_debugfs_dir;
 
static int rk_dma_heap_debug_show(struct seq_file *s, void *unused)
{
   struct rk_dma_heap *heap;
   unsigned long total = 0;
 
   mutex_lock(&rk_heap_list_lock);
   list_for_each_entry(heap, &rk_heap_list, list) {
       heap->s = s;
       get_each_dmabuf(rk_dma_heap_dump_dmabuf, heap);
       rk_dma_heap_dump_contig(heap);
       total += heap->total_size;
   }
   seq_printf(s, "\nTotal : 0x%lx (%lu KiB)\n", total, K(total));
   mutex_unlock(&rk_heap_list_lock);
 
   return 0;
}
DEFINE_SHOW_ATTRIBUTE(rk_dma_heap_debug);
 
static int rk_dma_heap_init_debugfs(void)
{
   struct dentry *d;
   int err = 0;
 
   d = debugfs_create_dir("rk_dma_heap", NULL);
   if (IS_ERR(d))
       return PTR_ERR(d);
 
   rk_dma_heap_debugfs_dir = d;
 
   d = debugfs_create_file("dma_heap_info", 0444,
               rk_dma_heap_debugfs_dir, NULL,
               &rk_dma_heap_debug_fops);
   if (IS_ERR(d)) {
       dma_heap_print("rk_dma_heap : debugfs: failed to create node bufinfo\n");
       debugfs_remove_recursive(rk_dma_heap_debugfs_dir);
       rk_dma_heap_debugfs_dir = NULL;
       err = PTR_ERR(d);
   }
 
   return err;
}
#else
static inline int rk_dma_heap_init_debugfs(void)
{
   return 0;
}
#endif
 
static int rk_dma_heap_proc_show(struct seq_file *s, void *unused)
{
   struct rk_dma_heap *heap;
   unsigned long total = 0;
 
   mutex_lock(&rk_heap_list_lock);
   list_for_each_entry(heap, &rk_heap_list, list) {
       heap->s = s;
       get_each_dmabuf(rk_dma_heap_dump_dmabuf, heap);
       rk_dma_heap_dump_contig(heap);
       total += heap->total_size;
   }
   seq_printf(s, "\nTotal : 0x%lx (%lu KiB)\n", total, K(total));
   mutex_unlock(&rk_heap_list_lock);
 
   return 0;
}
 
static int rk_dma_heap_info_proc_open(struct inode *inode,
                         struct file *file)
{
   return single_open(file, rk_dma_heap_proc_show, NULL);
}
 
static const struct proc_ops rk_dma_heap_info_proc_fops = {
   .proc_open    = rk_dma_heap_info_proc_open,
   .proc_read    = seq_read,
   .proc_lseek    = seq_lseek,
   .proc_release    = single_release,
};
 
static int rk_dma_heap_init_proc(void)
{
   proc_rk_dma_heap_dir = proc_mkdir("rk_dma_heap", NULL);
   if (!proc_rk_dma_heap_dir) {
       pr_err("create rk_dma_heap proc dir error\n");
       return -ENOENT;
   }
 
   proc_create("dma_heap_info", 0644, proc_rk_dma_heap_dir,
           &rk_dma_heap_info_proc_fops);
 
   return 0;
}
 
static int rk_dma_heap_init(void)
{
   int ret;
 
   ret = rk_dma_heap_sysfs_setup();
   if (ret)
       return ret;
 
   ret = alloc_chrdev_region(&rk_dma_heap_devt, 0, NUM_HEAP_MINORS,
                 DEVNAME);
   if (ret)
       goto err_chrdev;
 
   rk_dma_heap_class = class_create(THIS_MODULE, DEVNAME);
   if (IS_ERR(rk_dma_heap_class)) {
       ret = PTR_ERR(rk_dma_heap_class);
       goto err_class;
   }
   rk_dma_heap_class->devnode = rk_dma_heap_devnode;
 
   rk_dma_heap_init_debugfs();
   rk_dma_heap_init_proc();
 
   return 0;
 
err_class:
   unregister_chrdev_region(rk_dma_heap_devt, NUM_HEAP_MINORS);
err_chrdev:
   rk_dma_heap_sysfs_teardown();
   return ret;
}
subsys_initcall(rk_dma_heap_init);