allocator_ext_dma.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright 2018 Rockchip Electronics Co. LTD
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stdio.h>
  17. #include <sys/mman.h>
  18. #include "mpp_mem.h"
  19. #include "mpp_debug.h"
  20. #include "mpp_common.h"
  21. #include "allocator_ext_dma.h"
  22. typedef struct {
  23. size_t alignment;
  24. MppAllocFlagType flags;
  25. } allocator_ctx;
  26. static MPP_RET allocator_ext_dma_open(void **ctx, size_t alignment, MppAllocFlagType flags)
  27. {
  28. MPP_RET ret = MPP_OK;
  29. allocator_ctx *p = NULL;
  30. if (NULL == ctx) {
  31. mpp_err_f("do not accept NULL input\n");
  32. return MPP_ERR_NULL_PTR;
  33. }
  34. p = mpp_malloc(allocator_ctx, 1);
  35. if (NULL == p) {
  36. mpp_err_f("failed to allocate context\n");
  37. ret = MPP_ERR_MALLOC;
  38. } else {
  39. p->alignment = alignment;
  40. p->flags = flags;
  41. }
  42. *ctx = p;
  43. return ret;
  44. }
  45. static MPP_RET allocator_ext_dma_alloc(void *ctx, MppBufferInfo *info)
  46. {
  47. if (!ctx || !info) {
  48. mpp_err_f("found NULL context input\n");
  49. return MPP_ERR_VALUE;
  50. }
  51. return MPP_ERR_PERM;
  52. }
  53. static MPP_RET allocator_ext_dma_free(void *ctx, MppBufferInfo *info)
  54. {
  55. if (!ctx || !info) {
  56. mpp_err_f("found NULL context input\n");
  57. return MPP_ERR_VALUE;
  58. }
  59. return MPP_ERR_PERM;
  60. }
  61. static MPP_RET allocator_ext_dma_import(void *ctx, MppBufferInfo *info)
  62. {
  63. allocator_ctx *p = (allocator_ctx *)ctx;
  64. mpp_assert(p);
  65. mpp_assert(info->size);
  66. if (info->ptr) {
  67. mpp_err_f("The ext_dma is not used for userptr\n");
  68. return MPP_ERR_VALUE;
  69. }
  70. return ((info->fd < 0) ? MPP_ERR_VALUE : MPP_OK);
  71. }
  72. static MPP_RET allocator_ext_dma_mmap(void *ctx, MppBufferInfo *info)
  73. {
  74. void *ptr = NULL;
  75. int flags = 0;
  76. unsigned long offset = 0L;
  77. mpp_assert(ctx);
  78. mpp_assert(info->size);
  79. mpp_assert(info->fd >= 0);
  80. if (info->ptr)
  81. return MPP_OK;
  82. /*
  83. * It is insecure to access the first memory page,
  84. * usually system doesn't allow this behavior.
  85. */
  86. flags = PROT_READ;
  87. if (fcntl(info->fd, F_GETFL) & O_RDWR)
  88. flags |= PROT_WRITE;
  89. ptr = mmap(NULL, info->size, flags, MAP_SHARED, info->fd, offset);
  90. if (ptr == MAP_FAILED)
  91. return MPP_ERR_NULL_PTR;
  92. info->ptr = ptr;
  93. return MPP_OK;
  94. }
  95. static MPP_RET allocator_ext_dma_release(void *ctx, MppBufferInfo *info)
  96. {
  97. mpp_assert(ctx);
  98. mpp_assert(info->size);
  99. if (info->ptr)
  100. munmap(info->ptr, info->size);
  101. info->ptr = NULL;
  102. info->hnd = NULL;
  103. info->fd = -1;
  104. info->size = 0;
  105. return MPP_OK;
  106. }
  107. static MPP_RET allocator_ext_dma_close(void *ctx)
  108. {
  109. if (ctx) {
  110. mpp_free(ctx);
  111. return MPP_OK;
  112. }
  113. mpp_err_f("found NULL context input\n");
  114. return MPP_ERR_VALUE;
  115. }
  116. static MppAllocFlagType os_allocator_ext_dma_flags(void *ctx)
  117. {
  118. allocator_ctx *p = (allocator_ctx *)ctx;
  119. return p ? (MppAllocFlagType)p->flags : MPP_ALLOC_FLAG_NONE;
  120. }
  121. os_allocator allocator_ext_dma = {
  122. .type = MPP_BUFFER_TYPE_EXT_DMA,
  123. .open = allocator_ext_dma_open,
  124. .close = allocator_ext_dma_close,
  125. .alloc = allocator_ext_dma_alloc,
  126. .free = allocator_ext_dma_free,
  127. .import = allocator_ext_dma_import,
  128. .release = allocator_ext_dma_release,
  129. .mmap = allocator_ext_dma_mmap,
  130. .flags = os_allocator_ext_dma_flags,
  131. };