Chinaunix首页 | 论坛 | 博客
  • 博客访问: 291426
  • 博文数量: 65
  • 博客积分: 185
  • 博客等级: 入伍新兵
  • 技术积分: 609
  • 用 户 组: 普通用户
  • 注册时间: 2012-11-06 21:41
个人简介

好好学习,天天向上

文章分类

全部博文(65)

文章存档

2022年(3)

2021年(25)

2020年(1)

2019年(3)

2016年(2)

2015年(3)

2014年(14)

2013年(7)

2012年(7)

我的朋友

分类: 嵌入式

2021-04-12 20:49:27


点击(此处)折叠或打开

  1. #include <linux/module.h>
  2. #include <linux/debugfs.h>
  3. #include <linux/miscdevice.h>
  4. #include <linux/dma-mapping.h>
  5. #include <linux/of_device.h>
  6. #include <linux/slab.h>

  7. /*
  8.  *
  9. / # insmod hd_test.ko
  10. / # dmesg | grep dma
  11. [ 0.000000] cma: dma_contiguous_reserve(limit 80000000)
  12. [ 0.000000] cma: dma_contiguous_reserve: reserving 16 MiB for global area
  13. [ 126.888985] cl_added:dma_alloc_attrs:254, ops=swiotlb_dma_ops+0x0/0x80
  14. [ 126.945438] cl_added:dma_alloc_from_coherent:163
  15. [ 126.958159] cl_added:dma_alloc_attrs:257, ops=swiotlb_dma_ops+0x0/0x80
  16. [ 126.974582] cl_added:dma_alloc_attrs:263
  17. [ 127.027068] dma_alloc ok, dmabuf=ffff000019601000, addr=7f100000
  18. / # cat /proc/meminfo | grep -i cma
  19. CmaTotal: 16384 kB
  20. CmaFree: 5888 kB
  21. / # rmmod hd_test.ko
  22. / # dmesg | tail -n 5
  23. [ 126.982036] cma: cma_alloc(cma ffff800000b90c90, count 2560, align 8)
  24. [ 127.002513] cma: cma_alloc(): returned ffff7bffc1fc4000
  25. [ 127.027068] dma_alloc ok, dmabuf=ffff000019601000, addr=7f100000
  26. [ 204.600526] cma: cma_release(page ffff7bffc1fc4000)
  27. [ 204.618475] test_module_exit
  28. / #
  29. / # cat /proc/meminfo | grep -i cma
  30. CmaTotal: 16384 kB
  31. CmaFree: 15532 kB

  32.  *
  33. / #


  34. */

  35. MODULE_LICENSE("GPL");
  36. MODULE_DESCRIPTION("Test module");


  37. static struct miscdevice dummy_test_miscdev = {
  38.     .minor        = MISC_DYNAMIC_MINOR,
  39.     .name        = "dummy_1",
  40. //    .fops        = &dummy_test_fops,
  41. };


  42. static char* dmabuf;
  43. static dma_addr_t addr;
  44. static u64 test_dma_mask = 0x4fffffff;
  45. #define TEST_SIZE 1024*10*1024
  46. static int test_module_init(void)
  47. {
  48.     int rc;

  49.     rc = misc_register(&dummy_test_miscdev);
  50.     if (rc) {
  51.         pr_err("unable to register misc device\n");
  52.     }
  53.     /* configure dev->archdata.dma_ops = &swiotlb_dma_ops;
  54.      * or this dev's dma_ops is NULL, dma_alloc_coherent will fail */
  55.     of_dma_configure(dummy_test_miscdev.this_device, NULL);
  56.      //arch_setup_dma_ops(dummy_test_miscdev.this_device, 0, 0, NULL,1);
  57.     dummy_test_miscdev.this_device->dma_mask = &test_dma_mask;
  58.     dummy_test_miscdev.this_device->coherent_dma_mask = test_dma_mask;
  59.     dmabuf = dma_alloc_coherent(dummy_test_miscdev.this_device, TEST_SIZE,
  60.                  &addr, GFP_KERNEL);

  61.     if (! dmabuf) {
  62.          pr_err("dma_alloc fail\n");
  63.     } else {
  64.         pr_err("dma_alloc ok, dmabuf=%p, addr=%llx\n", dmabuf, addr);
  65.     }


  66.     return 0;
  67. }

  68. static void test_module_exit(void)
  69. {
  70.     dma_free_coherent(dummy_test_miscdev.this_device, TEST_SIZE, dmabuf, addr);
  71.     misc_deregister(&dummy_test_miscdev);
  72.     pr_err("test_module_exit\n");
  73.     return;
  74. }

  75. module_init(test_module_init);
  76. module_exit(test_module_exit);

阅读(1671) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~