video_mem.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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_memory.h"
  26. #include "video_mem.h"
  27. #define VMEM_PRINT(level, ...) \
  28. { \
  29. if (log_level >= VMEM_LOG_##level) \
  30. { \
  31. printf("VMEM[%d] %s: ", pid, #level); \
  32. printf(__VA_ARGS__); \
  33. } \
  34. }
  35. #define VMEM_LOGE(...) VMEM_PRINT(ERROR, __VA_ARGS__)
  36. #define VMEM_LOGW(...) VMEM_PRINT(WARNING, __VA_ARGS__)
  37. #define VMEM_LOGI(...) VMEM_PRINT(INFO, __VA_ARGS__)
  38. #define VMEM_LOGD(...) VMEM_PRINT(DEBUG, __VA_ARGS__)
  39. #define VMEM_LOGT(...) VMEM_PRINT(TRACE, __VA_ARGS__)
  40. typedef enum _VmemLogLevel
  41. {
  42. VMEM_LOG_QUIET = 0,
  43. VMEM_LOG_ERROR,
  44. VMEM_LOG_WARNING,
  45. VMEM_LOG_INFO,
  46. VMEM_LOG_DEBUG,
  47. VMEM_LOG_TRACE,
  48. VMEM_LOG_MAX
  49. } VmemLogLevel;
  50. typedef struct _VmemContext
  51. {
  52. int fd_alloc;
  53. } VmemContext;
  54. int log_level = VMEM_LOG_ERROR;
  55. int pid = 0;
  56. static int getLogLevel();
  57. VmemStatus
  58. VMEM_create(void **vmem)
  59. {
  60. VmemContext *ctx = NULL;
  61. log_level = getLogLevel();
  62. pid = getpid();
  63. if (vmem == NULL)
  64. return VMEM_STATUS_ERROR;
  65. ctx = (VmemContext *)malloc(sizeof(*ctx));
  66. if (ctx == NULL)
  67. return VMEM_STATUS_NO_MEMORY;
  68. *vmem = (void *)ctx;
  69. ctx->fd_alloc = open("/dev/vidmem", O_RDWR);
  70. if (ctx->fd_alloc == -1)
  71. {
  72. VMEM_LOGE("Failed to open /dev/vidmem\n");
  73. return VMEM_STATUS_ERROR;
  74. }
  75. return VMEM_STATUS_OK;
  76. }
  77. VmemStatus
  78. VMEM_allocate(void *vmem, VmemParams *params)
  79. {
  80. VmemContext *ctx = NULL;
  81. VidmemParams p;
  82. if (vmem == NULL || params == NULL)
  83. return VMEM_STATUS_ERROR;
  84. ctx = (VmemContext *)vmem;
  85. params->phy_address = 0;
  86. params->vir_address = NULL;
  87. params->fd = -1;
  88. memset(&p, 0, sizeof(p));
  89. p.size = params->size;
  90. p.flags = params->flags;
  91. ioctl(ctx->fd_alloc, MEMORY_IOC_ALLOCATE, &p);
  92. if (p.bus_address == 0)
  93. {
  94. VMEM_LOGE("Failed to allocate memory\n");
  95. return VMEM_STATUS_NO_MEMORY;
  96. }
  97. params->phy_address = p.bus_address;
  98. VMEM_LOGI("Allocated %d bytes, phy addr 0x%08x\n",
  99. params->size, params->phy_address);
  100. return VMEM_STATUS_OK;
  101. }
  102. VmemStatus
  103. VMEM_mmap(void *vmem, VmemParams *params)
  104. {
  105. VmemContext *ctx = NULL;
  106. void *vir_addr;
  107. if (vmem == NULL || params == NULL)
  108. return VMEM_STATUS_ERROR;
  109. if (params->vir_address != NULL)
  110. return VMEM_STATUS_OK;
  111. ctx = (VmemContext *)vmem;
  112. int fd = params->fd > 0 ? params->fd : ctx->fd_alloc;
  113. unsigned int offset = params->fd > 0 ? 0 : params->phy_address;
  114. vir_addr = mmap(0, params->size, PROT_READ | PROT_WRITE,
  115. MAP_SHARED, fd, offset);
  116. if (vir_addr == MAP_FAILED)
  117. {
  118. VMEM_LOGE("Failed to mmap physical address: 0x%08x, using fd %d\n",
  119. params->phy_address, fd);
  120. return VMEM_STATUS_ERROR;
  121. }
  122. params->vir_address = vir_addr;
  123. VMEM_LOGI("Mapped phy addr 0x%08x to vir addr %p, size %d\n",
  124. params->phy_address, params->vir_address, params->size);
  125. return VMEM_STATUS_OK;
  126. }
  127. VmemStatus
  128. VMEM_free(void *vmem, VmemParams *params)
  129. {
  130. VmemContext *ctx = NULL;
  131. VidmemParams p;
  132. if (vmem == NULL || params == NULL)
  133. return VMEM_STATUS_ERROR;
  134. ctx = (VmemContext *)vmem;
  135. VMEM_LOGI("Free virt addr %p, phy addr 0x%08x, size %d\n",
  136. params->vir_address, params->phy_address, params->size);
  137. if (params->vir_address != MAP_FAILED && params->vir_address != NULL)
  138. munmap(params->vir_address, params->size);
  139. params->vir_address = NULL;
  140. if (params->phy_address != 0)
  141. {
  142. memset(&p, 0, sizeof(p));
  143. p.bus_address = params->phy_address;
  144. ioctl(ctx->fd_alloc, MEMORY_IOC_FREE, &p);
  145. params->phy_address = 0;
  146. }
  147. return VMEM_STATUS_OK;
  148. }
  149. VmemStatus
  150. VMEM_destroy(void *vmem)
  151. {
  152. if (vmem != NULL)
  153. {
  154. VmemContext *ctx = (VmemContext *)vmem;
  155. if (ctx->fd_alloc != -1)
  156. close(ctx->fd_alloc);
  157. free(vmem);
  158. }
  159. return VMEM_STATUS_OK;
  160. }
  161. VmemStatus
  162. VMEM_export(void *vmem, VmemParams *params)
  163. {
  164. VmemContext *ctx = NULL;
  165. VidmemParams p;
  166. if (vmem == NULL || params == NULL)
  167. return VMEM_STATUS_ERROR;
  168. ctx = (VmemContext *)vmem;
  169. memset(&p, 0, sizeof(p));
  170. p.bus_address = params->phy_address;
  171. p.flags = O_RDWR;
  172. ioctl(ctx->fd_alloc, MEMORY_IOC_DMABUF_EXPORT, &p);
  173. if (p.fd < 0) {
  174. VMEM_LOGE("Failed to export memory\n");
  175. return VMEM_STATUS_ERROR;
  176. }
  177. params->fd = p.fd;
  178. VMEM_LOGI("Exported phy addr 0x%08x to fd %d, size %d\n",
  179. params->phy_address, params->fd, params->size);
  180. return VMEM_STATUS_OK;
  181. }
  182. VmemStatus
  183. VMEM_import(void *vmem, VmemParams *params)
  184. {
  185. VmemContext *ctx = NULL;
  186. VidmemParams p;
  187. if (vmem == NULL || params == NULL)
  188. return VMEM_STATUS_ERROR;
  189. ctx = (VmemContext *)vmem;
  190. memset(&p, 0, sizeof(p));
  191. p.fd = params->fd;
  192. ioctl(ctx->fd_alloc, MEMORY_IOC_DMABUF_IMPORT, &p);
  193. if (p.bus_address == 0 || p.size == 0) {
  194. VMEM_LOGE("Failed to import memory\n");
  195. return VMEM_STATUS_ERROR;
  196. }
  197. params->phy_address = p.bus_address;
  198. params->size = p.size;
  199. VMEM_LOGI("Imported fd %d to phy addr 0x%08x, size %d\n",
  200. params->fd, params->phy_address, params->size);
  201. return VMEM_STATUS_OK;
  202. }
  203. VmemStatus
  204. VMEM_release(void *vmem, VmemParams *params)
  205. {
  206. VmemContext *ctx = NULL;
  207. if (vmem == NULL || params == NULL)
  208. return VMEM_STATUS_ERROR;
  209. ctx = (VmemContext *)vmem;
  210. VMEM_LOGI("Released imported phy addr 0x%08x, fd %d, size %d\n",
  211. params->phy_address, params->fd, params->size);
  212. if (params->vir_address != MAP_FAILED && params->vir_address != NULL)
  213. munmap(params->vir_address, params->size);
  214. params->vir_address = NULL;
  215. if (params->phy_address != 0) {
  216. VidmemParams p;
  217. memset(&p, 0, sizeof(p));
  218. p.bus_address = params->phy_address;
  219. ioctl(ctx->fd_alloc, MEMORY_IOC_DMABUF_RELEASE, &p);
  220. params->phy_address = 0;
  221. }
  222. return VMEM_STATUS_OK;
  223. }
  224. static int getLogLevel()
  225. {
  226. char *env = getenv("VMEM_LOG_LEVEL");
  227. if (env == NULL)
  228. return VMEM_LOG_ERROR;
  229. else
  230. {
  231. int level = atoi(env);
  232. if (level >= VMEM_LOG_MAX || level < VMEM_LOG_QUIET)
  233. return VMEM_LOG_ERROR;
  234. else
  235. return level;
  236. }
  237. }