video_memory_test.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (c) 2021-2022 Alibaba Group. All rights reserved.
  3. * License-Identifier: Apache-2.0
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. * not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <sys/mman.h>
  19. #include <sys/ioctl.h>
  20. #include <fcntl.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <unistd.h>
  24. #include <memory.h>
  25. #include "video_mem.h"
  26. typedef enum MemType
  27. {
  28. MEM_TYPE_CONTIGUOUS,
  29. MEM_TYPE_NONCONTIGUOUS,
  30. MEM_TYPE_CMA,
  31. MEM_TYPE_VI,
  32. MEM_TYPE_MAX
  33. } MemType;
  34. int alloc_flags[MEM_TYPE_MAX] =
  35. {
  36. VMEM_FLAG_CONTIGUOUS | VMEM_FLAG_4GB_ADDR,
  37. VMEM_FLAG_NON_CONTIGUOUS,
  38. VMEM_FLAG_CMA,
  39. VMEM_FLAG_VI
  40. };
  41. int alloc_num[MEM_TYPE_MAX] = {3, 3, 3, 3};
  42. void printUsage(char *name)
  43. {
  44. printf(" \
  45. Usage: %s [buf_size|-h]\n\
  46. buf_size: buffer size to be allocated, in unit of 4K pages\n\
  47. -h: print this message\n",
  48. name);
  49. }
  50. int main(int argc, char **argv)
  51. {
  52. int fd_alloc = -1;
  53. int size = 1920*1080*3/2;
  54. int pgsize = getpagesize();
  55. void *vmem = NULL;
  56. VmemParams *vmparams[MEM_TYPE_MAX] = {0};
  57. int err = 0;
  58. if (argc > 1)
  59. {
  60. if (strcmp(argv[1], "-h") == 0)
  61. {
  62. printUsage(argv[0]);
  63. return 0;
  64. }
  65. else
  66. size = atoi(argv[1]) * pgsize;
  67. }
  68. if (size <= 0)
  69. {
  70. printf("ERROR: invalid size: %d\n", size);
  71. printUsage(argv[0]);
  72. return -1;
  73. }
  74. do
  75. {
  76. if (VMEM_create(&vmem) != VMEM_STATUS_OK)
  77. break;
  78. VmemParams imp_params;
  79. for (int type = 0; type < MEM_TYPE_MAX; type++)
  80. {
  81. vmparams[type] = malloc(sizeof(VmemParams)*alloc_num[type]);
  82. if (vmparams[type] == NULL)
  83. {
  84. printf("ERROR: Failed to allocate VmemParams\n");
  85. err = 1;
  86. break;
  87. }
  88. for (int i = 0; i < alloc_num[type]; i++)
  89. {
  90. VmemParams *params = &vmparams[type][i];
  91. memset(params, 0, sizeof(*params));
  92. params->size = size;
  93. params->flags = alloc_flags[type];
  94. int vi_rsvmem_pool_region_id = i; // only for type == MEM_TYPE_VI
  95. if (type == MEM_TYPE_VI)
  96. {
  97. params->flags = SET_ALLOC_FLAG_REGION(params->flags, vi_rsvmem_pool_region_id);
  98. }
  99. if (VMEM_allocate(vmem, params) != VMEM_STATUS_OK)
  100. {
  101. if (type == MEM_TYPE_VI)
  102. printf("ERROR: Failed to allocate memory type %d, region_id=%d\n",
  103. type, vi_rsvmem_pool_region_id);
  104. else
  105. printf("ERROR: Failed to allocate memory type %d\n", type);
  106. break;
  107. }
  108. if (VMEM_mmap(vmem, params) != VMEM_STATUS_OK)
  109. {
  110. printf("ERROR: Failed to mmap busAddress: 0x%08x\n",
  111. params->phy_address);
  112. err = 1;
  113. break;
  114. }
  115. if (VMEM_export(vmem, params) != VMEM_STATUS_OK)
  116. {
  117. printf("ERROR: Failed to export buffer: 0x%08x\n",
  118. params->phy_address);
  119. err = 1;
  120. break;
  121. }
  122. printf("Allocated buffer %d of type %d at paddr 0x%08x vaddr %p size %d fd %d\n",
  123. i, type, params->phy_address, params->vir_address, size, params->fd);
  124. memset(&imp_params, 0, sizeof(imp_params));
  125. imp_params.fd = params->fd;
  126. if (VMEM_import(vmem, &imp_params) != VMEM_STATUS_OK)
  127. {
  128. printf("ERROR: Failed to import fd %d\n", params->fd);
  129. err = 1;
  130. break;
  131. }
  132. printf("Imported fd %d: paddr 0x%08x vaddr %p size %d\n",
  133. params->fd, params->phy_address, params->vir_address, size);
  134. VMEM_release(vmem, &imp_params);
  135. }
  136. if (err)
  137. break;
  138. }
  139. } while (0);
  140. for (int type = 0; type < MEM_TYPE_MAX; type++)
  141. {
  142. for (int i = 0; i < alloc_num[type]; i++)
  143. {
  144. VmemParams *params = &vmparams[type][i];
  145. VMEM_free(vmem, params);
  146. memset(params, 0, sizeof(*params));
  147. }
  148. if (vmparams[type])
  149. free(vmparams[type]);
  150. }
  151. VMEM_destroy(vmem);
  152. return 0;
  153. }