ark_memalloc.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <sys/types.h>
  6. #include <linux/ioctl.h>
  7. #include <sys/ioctl.h>
  8. #include <errno.h>
  9. #include <string.h>
  10. #include <sys/mman.h>
  11. #include <sys/syscall.h>
  12. #include <signal.h>
  13. #include <semaphore.h>
  14. #include <sys/shm.h>
  15. #include <sys/ipc.h>
  16. #include "ark_common.h"
  17. #include "ark_memalloc.h"
  18. void arkapi_memalloc_free(memalloc_handle *handle, reqbuf_info *pbuf)
  19. {
  20. MemallocParams params;
  21. int ret = SUCCESS, i;
  22. if (!handle || handle->fd <= 0 || !pbuf) {
  23. printf("%s: handle=%p, handle->fd=%d, pbuf=%p, error.\n", __func__, handle, handle->fd, pbuf);
  24. return;
  25. }
  26. if (!pbuf->count || !pbuf->size) {
  27. printf("%s: reqbuf=%p, count=%d, size=%d, error.\n", \
  28. __func__, pbuf, pbuf->count, pbuf->size);
  29. return;
  30. }
  31. for (i = 0; i < pbuf->count; i++) {
  32. if (pbuf->phy_addr[i] != 0) {
  33. params.busAddress = pbuf->phy_addr[i];
  34. params.size = pbuf->size;
  35. ioctl(handle->fd, MEMALLOC_IOCSFREEBUFFER, &params);
  36. if (pbuf->virt_addr[i] != NULL)
  37. arkapi_mem_unmap(pbuf->virt_addr[i], pbuf->size);
  38. ark_dbg("%s: phy_addr[%d]=%p, virt_addr[%d]=%p, size=%d.\n", \
  39. __func__,i, pbuf->phy_addr[i], i, pbuf->virt_addr[i], pbuf->size);
  40. }
  41. }
  42. list_del(&pbuf->list);
  43. free(pbuf);
  44. --handle->reqbuf_cnt;
  45. ark_dbg("%s: ret=%d.\n",__func__, ret);
  46. }
  47. void* arkapi_mem_map(unsigned int phy_addr, unsigned int size)
  48. {
  49. int mem_fd;
  50. void *virt_addr = NULL;
  51. if(!size || !phy_addr)
  52. return NULL;
  53. mem_fd = open(MEM_PATH, O_RDWR | O_SYNC);
  54. if (mem_fd < 0) {
  55. printf("open %s failure.\n",MEM_PATH);
  56. return virt_addr;
  57. }
  58. virt_addr = (unsigned char*)mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, mem_fd, phy_addr);
  59. ark_dbg("%s: phy_addr=%p ==> addr_virt=%p, size=%d .\n",__func__, phy_addr, virt_addr, size);
  60. close(mem_fd);
  61. return virt_addr;
  62. }
  63. int arkapi_mem_unmap(void* virt_addr, unsigned int size)
  64. {
  65. int ret;
  66. if(!size || !virt_addr)
  67. return -EINVAL;
  68. ret = munmap((void *)virt_addr, size);
  69. ark_dbg("%s: virt_addr=%p, size=%d .\n",__func__, virt_addr, size);
  70. return ret;
  71. }
  72. memalloc_handle *arkapi_memalloc_init(void)
  73. {
  74. memalloc_handle *handle = NULL;
  75. reqbuf_info *pbuf;
  76. int fd;
  77. fd = open(MEMALLOC_PATH, O_RDWR | O_SYNC);
  78. if (fd < 0) {
  79. printf("open %s fail.\n", MEMALLOC_PATH);
  80. return NULL;
  81. }
  82. handle = (memalloc_handle *)malloc(sizeof(memalloc_handle));
  83. if(!handle){
  84. printf("%s: malloc handle error.\n", __func__);
  85. return NULL;
  86. }
  87. memset(handle, 0 ,sizeof(memalloc_handle));
  88. handle->fd = fd;
  89. INIT_LIST_HEAD(&handle->reqbuf_list);
  90. ark_dbg("%s: <---success.\n",__func__);
  91. return handle;
  92. }
  93. reqbuf_info *arkapi_memalloc_reqbuf(memalloc_handle *handle, int size, int count)
  94. {
  95. reqbuf_info *pbuf = NULL;
  96. MemallocParams params;
  97. int ret, i;
  98. if (!handle || handle->fd <= 0) {
  99. printf("%s: handle=%p, handle->fd=%d, error.\n", __func__, handle, handle->fd);
  100. return NULL;
  101. }
  102. if (!size || !count || count > FRAME_MAX) {
  103. printf("%s: count=%d(max=5), size=%d, error.\n", __func__, count, size);
  104. return NULL;
  105. }
  106. pbuf = (reqbuf_info *)malloc(sizeof(reqbuf_info));
  107. if (!pbuf) {
  108. printf("%s: pbuf malloc error.\n", __func__);
  109. return NULL;
  110. }
  111. memset(pbuf, 0, sizeof(reqbuf_info));
  112. pbuf->count = count;
  113. pbuf->size = params.size = size;
  114. for (i = 0; i < pbuf->count; i++) {
  115. ret = ioctl(handle->fd, MEMALLOC_IOCXGETBUFFER, &params);
  116. if (ret != 0) {
  117. printf("%s: ioctl fail, ret=%d.\n",__func__, ret);
  118. goto err;
  119. }
  120. pbuf->phy_addr[i] = params.busAddress;
  121. pbuf->virt_addr[i] = arkapi_mem_map(params.busAddress, params.size);
  122. if (pbuf->virt_addr[i] == NULL) {
  123. ret = -EINVAL;
  124. goto err;
  125. }
  126. ark_dbg("%s: reqbuf.phy_addr[%d]=%p, reqbuf.virt_addr[%d]=%p, size=%d.\n",\
  127. __func__,i,pbuf->phy_addr[i],i,pbuf->virt_addr[i], size);
  128. }
  129. list_add(&pbuf->list, &handle->reqbuf_list);
  130. ++handle->reqbuf_cnt;
  131. ark_dbg("%s: success. reqbuf_cnt=%d.\n",__func__, handle->reqbuf_cnt);
  132. return pbuf;
  133. err:
  134. for (i = 0; i < pbuf->count; i++) {
  135. if (pbuf->phy_addr[i] != 0) {
  136. params.busAddress = pbuf->phy_addr[i];
  137. params.size = pbuf->size;
  138. ioctl(handle->fd, MEMALLOC_IOCSFREEBUFFER, &params);
  139. if (pbuf->virt_addr[i] != NULL)
  140. arkapi_mem_unmap(&pbuf->virt_addr[i], pbuf->size);
  141. }
  142. }
  143. free(pbuf);
  144. return NULL;
  145. }
  146. void arkapi_memalloc_release(memalloc_handle *handle)
  147. {
  148. int ret, i, user_id;
  149. struct list_head *pos;
  150. struct list_head *del_tmp;
  151. reqbuf_info *pbuf;
  152. if(!handle || handle->fd <= 0){
  153. printf("%s: handle=%p, handle->fd=%d.\n", __func__, handle, handle->fd);
  154. return;
  155. }
  156. list_for_each_safe(pos, del_tmp, &handle->reqbuf_list) {
  157. pbuf = list_entry(pos, reqbuf_info, list);
  158. if (pbuf && pbuf->count > 0 && pbuf->size > 0) {
  159. arkapi_memalloc_free(handle, pbuf);
  160. ark_dbg("%s: reqbuf_cnt=%d.\n",__func__, handle->reqbuf_cnt);
  161. }
  162. }
  163. if(handle->fd)
  164. close(handle->fd);
  165. if(handle)
  166. free(handle);
  167. ark_dbg("%s: <---success.\n",__func__);
  168. }
  169. #if 0
  170. int ark1668_memalloc_flush(int memalloc_fd, unsigned int phy_start, unsigned int size)
  171. {
  172. MemallocParams param;
  173. int ret = SUCCESS;
  174. if (memalloc_fd < 0 || !phy_start || !size)
  175. return -EINVAL;
  176. param.busAddress = phy_start;
  177. param.size = size;
  178. ret = ioctl(memalloc_fd, MEMALLOC_IOCSFLUSHRAMBUFFER, &param);
  179. if (ret != 0) {
  180. printf("%s ioctl fail.\n", __func__);
  181. return ret;
  182. }
  183. }
  184. void ark1668_cache_flush(unsigned int virt_start, unsigned int size)
  185. {
  186. syscall(__ARM_NR_cacheflush, virt_start, virt_start+size, 0);
  187. }
  188. int arkapi_memalloc_flush(memalloc_handle *handle, reqbuf_info *reqbuf, int buf_id)
  189. {
  190. reqbuf_info *pbuf;
  191. if(!handle || !reqbuf || buf_id >= FRAME_MAX || buf_id < 0){
  192. printf("%s: handle=%p, reqbuf=%p, buf_id=%d(max=5)error.\n", \
  193. __func__, handle, reqbuf, buf_id);
  194. return -EINVAL;
  195. }
  196. if(handle->fd <= 0){
  197. printf("%s: fd <= 0, error.\n", __func__);
  198. return -EINVAL;
  199. }
  200. ark1668_memalloc_flush(handle->fd, pbuf->phy_addr[buf_id], pbuf->size);
  201. ark1668_cache_flush((unsigned int)pbuf->virt_addr[buf_id], pbuf->size);
  202. //ark_dbg("%s: success.\n",__func__);
  203. return SUCCESS;
  204. }
  205. #endif