cam_mem_mgr.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/types.h>
  7. #include <linux/mutex.h>
  8. #include <linux/slab.h>
  9. #include <linux/dma-buf.h>
  10. #include <linux/version.h>
  11. #include <linux/debugfs.h>
  12. #if IS_REACHABLE(CONFIG_DMABUF_HEAPS)
  13. #include <linux/mem-buf.h>
  14. #include <soc/qcom/secure_buffer.h>
  15. #endif
  16. #include "cam_compat.h"
  17. #include "cam_req_mgr_util.h"
  18. #include "cam_mem_mgr.h"
  19. #include "cam_smmu_api.h"
  20. #include "cam_debug_util.h"
  21. #include "cam_trace.h"
  22. #include "cam_common_util.h"
  23. #include "cam_presil_hw_access.h"
  24. #include "cam_compat.h"
  25. #define CAM_MEM_SHARED_BUFFER_PAD_4K (4 * 1024)
  26. static struct cam_mem_table tbl;
  27. static atomic_t cam_mem_mgr_state = ATOMIC_INIT(CAM_MEM_MGR_UNINITIALIZED);
  28. #if IS_REACHABLE(CONFIG_DMABUF_HEAPS)
  29. static void cam_mem_mgr_put_dma_heaps(void);
  30. static int cam_mem_mgr_get_dma_heaps(void);
  31. #endif
  32. #ifdef CONFIG_CAM_PRESIL
  33. static inline void cam_mem_mgr_reset_presil_params(int idx)
  34. {
  35. tbl.bufq[idx].presil_params.fd_for_umd_daemon = -1;
  36. tbl.bufq[idx].presil_params.refcount = 0;
  37. }
  38. #else
  39. static inline void cam_mem_mgr_reset_presil_params(int idx)
  40. {
  41. return;
  42. }
  43. #endif
  44. static unsigned long cam_mem_mgr_mini_dump_cb(void *dst, unsigned long len)
  45. {
  46. struct cam_mem_table_mini_dump *md;
  47. if (!dst) {
  48. CAM_ERR(CAM_MEM, "Invalid params");
  49. return 0;
  50. }
  51. if (len < sizeof(*md)) {
  52. CAM_ERR(CAM_MEM, "Insufficient length %u", len);
  53. return 0;
  54. }
  55. md = (struct cam_mem_table_mini_dump *)dst;
  56. memcpy(md->bufq, tbl.bufq, CAM_MEM_BUFQ_MAX * sizeof(struct cam_mem_buf_queue));
  57. md->dbg_buf_idx = tbl.dbg_buf_idx;
  58. md->alloc_profile_enable = tbl.alloc_profile_enable;
  59. md->force_cache_allocs = tbl.force_cache_allocs;
  60. md->need_shared_buffer_padding = tbl.need_shared_buffer_padding;
  61. return sizeof(*md);
  62. }
  63. static void cam_mem_mgr_print_tbl(void)
  64. {
  65. int i;
  66. uint64_t ms, hrs, min, sec;
  67. struct timespec64 current_ts;
  68. CAM_GET_TIMESTAMP(current_ts);
  69. CAM_CONVERT_TIMESTAMP_FORMAT(current_ts, hrs, min, sec, ms);
  70. CAM_INFO(CAM_MEM, "***%llu:%llu:%llu:%llu Mem mgr table dump***",
  71. hrs, min, sec, ms);
  72. for (i = 1; i < CAM_MEM_BUFQ_MAX; i++) {
  73. if (tbl.bufq[i].active) {
  74. CAM_CONVERT_TIMESTAMP_FORMAT((tbl.bufq[i].timestamp), hrs, min, sec, ms);
  75. CAM_INFO(CAM_MEM,
  76. "%llu:%llu:%llu:%llu idx %d fd %d i_ino %lu size %llu",
  77. hrs, min, sec, ms, i, tbl.bufq[i].fd, tbl.bufq[i].i_ino,
  78. tbl.bufq[i].len);
  79. }
  80. }
  81. }
  82. static int cam_mem_util_get_dma_dir(uint32_t flags)
  83. {
  84. int rc = -EINVAL;
  85. if (flags & CAM_MEM_FLAG_HW_READ_ONLY)
  86. rc = DMA_TO_DEVICE;
  87. else if (flags & CAM_MEM_FLAG_HW_WRITE_ONLY)
  88. rc = DMA_FROM_DEVICE;
  89. else if (flags & CAM_MEM_FLAG_HW_READ_WRITE)
  90. rc = DMA_BIDIRECTIONAL;
  91. else if (flags & CAM_MEM_FLAG_PROTECTED_MODE)
  92. rc = DMA_BIDIRECTIONAL;
  93. return rc;
  94. }
  95. static int cam_mem_util_map_cpu_va(struct dma_buf *dmabuf, uintptr_t *vaddr, size_t *len)
  96. {
  97. int rc = 0;
  98. /*
  99. * dma_buf_begin_cpu_access() and dma_buf_end_cpu_access()
  100. * need to be called in pair to avoid stability issue.
  101. */
  102. rc = dma_buf_begin_cpu_access(dmabuf, DMA_BIDIRECTIONAL);
  103. if (rc) {
  104. CAM_ERR(CAM_MEM, "dma begin access failed rc=%d", rc);
  105. return rc;
  106. }
  107. rc = cam_compat_util_get_dmabuf_va(dmabuf, vaddr);
  108. if (rc) {
  109. CAM_ERR(CAM_MEM, "kernel vmap failed: rc = %d", rc);
  110. *len = 0;
  111. dma_buf_end_cpu_access(dmabuf, DMA_BIDIRECTIONAL);
  112. }
  113. else {
  114. *len = dmabuf->size;
  115. CAM_DBG(CAM_MEM, "vaddr = %llu, len = %zu", *vaddr, *len);
  116. }
  117. return rc;
  118. }
  119. static int cam_mem_util_unmap_cpu_va(struct dma_buf *dmabuf,
  120. uint64_t vaddr)
  121. {
  122. int rc = 0;
  123. if (!dmabuf || !vaddr) {
  124. CAM_ERR(CAM_MEM, "Invalid input args %pK %llX", dmabuf, vaddr);
  125. return -EINVAL;
  126. }
  127. cam_compat_util_put_dmabuf_va(dmabuf, (void *)vaddr);
  128. /*
  129. * dma_buf_begin_cpu_access() and
  130. * dma_buf_end_cpu_access() need to be called in pair
  131. * to avoid stability issue.
  132. */
  133. rc = dma_buf_end_cpu_access(dmabuf, DMA_BIDIRECTIONAL);
  134. if (rc) {
  135. CAM_ERR(CAM_MEM, "Failed in end cpu access, dmabuf=%pK",
  136. dmabuf);
  137. return rc;
  138. }
  139. return rc;
  140. }
  141. static int cam_mem_mgr_create_debug_fs(void)
  142. {
  143. int rc = 0;
  144. struct dentry *dbgfileptr = NULL;
  145. dbgfileptr = debugfs_create_dir("camera_memmgr", NULL);
  146. if (!dbgfileptr) {
  147. CAM_ERR(CAM_MEM,"DebugFS could not create directory!");
  148. rc = -ENOENT;
  149. goto end;
  150. }
  151. /* Store parent inode for cleanup in caller */
  152. tbl.dentry = dbgfileptr;
  153. debugfs_create_bool("alloc_profile_enable", 0644,
  154. tbl.dentry, &tbl.alloc_profile_enable);
  155. end:
  156. return rc;
  157. }
  158. int cam_mem_mgr_init(void)
  159. {
  160. int i;
  161. int bitmap_size;
  162. int rc = 0;
  163. if (atomic_read(&cam_mem_mgr_state))
  164. return 0;
  165. memset(tbl.bufq, 0, sizeof(tbl.bufq));
  166. if (cam_smmu_need_force_alloc_cached(&tbl.force_cache_allocs)) {
  167. CAM_ERR(CAM_MEM, "Error in getting force cache alloc flag");
  168. return -EINVAL;
  169. }
  170. tbl.need_shared_buffer_padding = cam_smmu_need_shared_buffer_padding();
  171. #if IS_REACHABLE(CONFIG_DMABUF_HEAPS)
  172. rc = cam_mem_mgr_get_dma_heaps();
  173. if (rc) {
  174. CAM_ERR(CAM_MEM, "Failed in getting dma heaps rc=%d", rc);
  175. return rc;
  176. }
  177. #endif
  178. bitmap_size = BITS_TO_LONGS(CAM_MEM_BUFQ_MAX) * sizeof(long);
  179. tbl.bitmap = kzalloc(bitmap_size, GFP_KERNEL);
  180. if (!tbl.bitmap) {
  181. rc = -ENOMEM;
  182. goto put_heaps;
  183. }
  184. tbl.bits = bitmap_size * BITS_PER_BYTE;
  185. bitmap_zero(tbl.bitmap, tbl.bits);
  186. /* We need to reserve slot 0 because 0 is invalid */
  187. set_bit(0, tbl.bitmap);
  188. for (i = 1; i < CAM_MEM_BUFQ_MAX; i++) {
  189. tbl.bufq[i].fd = -1;
  190. tbl.bufq[i].buf_handle = -1;
  191. cam_mem_mgr_reset_presil_params(i);
  192. }
  193. mutex_init(&tbl.m_lock);
  194. atomic_set(&cam_mem_mgr_state, CAM_MEM_MGR_INITIALIZED);
  195. cam_mem_mgr_create_debug_fs();
  196. cam_common_register_mini_dump_cb(cam_mem_mgr_mini_dump_cb,
  197. "cam_mem");
  198. return 0;
  199. put_heaps:
  200. #if IS_REACHABLE(CONFIG_DMABUF_HEAPS)
  201. cam_mem_mgr_put_dma_heaps();
  202. #endif
  203. return rc;
  204. }
  205. static int32_t cam_mem_get_slot(void)
  206. {
  207. int32_t idx;
  208. mutex_lock(&tbl.m_lock);
  209. idx = find_first_zero_bit(tbl.bitmap, tbl.bits);
  210. if (idx >= CAM_MEM_BUFQ_MAX || idx <= 0) {
  211. mutex_unlock(&tbl.m_lock);
  212. return -ENOMEM;
  213. }
  214. set_bit(idx, tbl.bitmap);
  215. tbl.bufq[idx].active = true;
  216. CAM_GET_TIMESTAMP((tbl.bufq[idx].timestamp));
  217. mutex_init(&tbl.bufq[idx].q_lock);
  218. mutex_unlock(&tbl.m_lock);
  219. return idx;
  220. }
  221. static void cam_mem_put_slot(int32_t idx)
  222. {
  223. mutex_lock(&tbl.m_lock);
  224. mutex_lock(&tbl.bufq[idx].q_lock);
  225. tbl.bufq[idx].active = false;
  226. tbl.bufq[idx].is_internal = false;
  227. memset(&tbl.bufq[idx].timestamp, 0, sizeof(struct timespec64));
  228. mutex_unlock(&tbl.bufq[idx].q_lock);
  229. mutex_destroy(&tbl.bufq[idx].q_lock);
  230. clear_bit(idx, tbl.bitmap);
  231. mutex_unlock(&tbl.m_lock);
  232. }
  233. int cam_mem_get_io_buf(int32_t buf_handle, int32_t mmu_handle,
  234. dma_addr_t *iova_ptr, size_t *len_ptr, uint32_t *flags)
  235. {
  236. int rc = 0, idx;
  237. *len_ptr = 0;
  238. if (!atomic_read(&cam_mem_mgr_state)) {
  239. CAM_ERR(CAM_MEM, "failed. mem_mgr not initialized");
  240. return -EINVAL;
  241. }
  242. idx = CAM_MEM_MGR_GET_HDL_IDX(buf_handle);
  243. if (idx >= CAM_MEM_BUFQ_MAX || idx <= 0)
  244. return -ENOENT;
  245. if (!tbl.bufq[idx].active) {
  246. CAM_ERR(CAM_MEM, "Buffer at idx=%d is already unmapped,",
  247. idx);
  248. return -EAGAIN;
  249. }
  250. mutex_lock(&tbl.bufq[idx].q_lock);
  251. if (buf_handle != tbl.bufq[idx].buf_handle) {
  252. rc = -EINVAL;
  253. goto handle_mismatch;
  254. }
  255. if (CAM_MEM_MGR_IS_SECURE_HDL(buf_handle))
  256. rc = cam_smmu_get_stage2_iova(mmu_handle, tbl.bufq[idx].fd, tbl.bufq[idx].dma_buf,
  257. iova_ptr, len_ptr);
  258. else
  259. rc = cam_smmu_get_iova(mmu_handle, tbl.bufq[idx].fd, tbl.bufq[idx].dma_buf,
  260. iova_ptr, len_ptr);
  261. if (rc) {
  262. CAM_ERR(CAM_MEM,
  263. "fail to map buf_hdl:0x%x, mmu_hdl: 0x%x for fd:%d i_ino:%lu",
  264. buf_handle, mmu_handle, tbl.bufq[idx].fd, tbl.bufq[idx].i_ino);
  265. goto handle_mismatch;
  266. }
  267. if (flags)
  268. *flags = tbl.bufq[idx].flags;
  269. CAM_DBG(CAM_MEM,
  270. "handle:0x%x fd:%d i_ino:%lu iova_ptr:0x%llx len_ptr:%llu",
  271. mmu_handle, tbl.bufq[idx].fd, tbl.bufq[idx].i_ino, iova_ptr, *len_ptr);
  272. handle_mismatch:
  273. mutex_unlock(&tbl.bufq[idx].q_lock);
  274. return rc;
  275. }
  276. EXPORT_SYMBOL(cam_mem_get_io_buf);
  277. int cam_mem_get_cpu_buf(int32_t buf_handle, uintptr_t *vaddr_ptr, size_t *len)
  278. {
  279. int idx;
  280. if (!atomic_read(&cam_mem_mgr_state)) {
  281. CAM_ERR(CAM_MEM, "failed. mem_mgr not initialized");
  282. return -EINVAL;
  283. }
  284. if (!buf_handle || !vaddr_ptr || !len)
  285. return -EINVAL;
  286. idx = CAM_MEM_MGR_GET_HDL_IDX(buf_handle);
  287. if (idx >= CAM_MEM_BUFQ_MAX || idx <= 0)
  288. return -EINVAL;
  289. if (!tbl.bufq[idx].active) {
  290. CAM_ERR(CAM_MEM, "Buffer at idx=%d is already unmapped,",
  291. idx);
  292. return -EPERM;
  293. }
  294. if (buf_handle != tbl.bufq[idx].buf_handle)
  295. return -EINVAL;
  296. if (!(tbl.bufq[idx].flags & CAM_MEM_FLAG_KMD_ACCESS))
  297. return -EINVAL;
  298. if (tbl.bufq[idx].kmdvaddr) {
  299. *vaddr_ptr = tbl.bufq[idx].kmdvaddr;
  300. *len = tbl.bufq[idx].len;
  301. } else {
  302. CAM_ERR(CAM_MEM, "No KMD access was requested for 0x%x handle",
  303. buf_handle);
  304. return -EINVAL;
  305. }
  306. return 0;
  307. }
  308. EXPORT_SYMBOL(cam_mem_get_cpu_buf);
  309. int cam_mem_mgr_cache_ops(struct cam_mem_cache_ops_cmd *cmd)
  310. {
  311. int rc = 0, idx;
  312. uint32_t cache_dir;
  313. unsigned long dmabuf_flag = 0;
  314. if (!atomic_read(&cam_mem_mgr_state)) {
  315. CAM_ERR(CAM_MEM, "failed. mem_mgr not initialized");
  316. return -EINVAL;
  317. }
  318. if (!cmd)
  319. return -EINVAL;
  320. idx = CAM_MEM_MGR_GET_HDL_IDX(cmd->buf_handle);
  321. if (idx >= CAM_MEM_BUFQ_MAX || idx <= 0)
  322. return -EINVAL;
  323. mutex_lock(&tbl.bufq[idx].q_lock);
  324. if (!tbl.bufq[idx].active) {
  325. CAM_ERR(CAM_MEM, "Buffer at idx=%d is already unmapped,",
  326. idx);
  327. rc = -EINVAL;
  328. goto end;
  329. }
  330. if (cmd->buf_handle != tbl.bufq[idx].buf_handle) {
  331. rc = -EINVAL;
  332. goto end;
  333. }
  334. rc = dma_buf_get_flags(tbl.bufq[idx].dma_buf, &dmabuf_flag);
  335. if (rc) {
  336. CAM_ERR(CAM_MEM, "cache get flags failed %d", rc);
  337. goto end;
  338. }
  339. #if IS_REACHABLE(CONFIG_DMABUF_HEAPS)
  340. CAM_DBG(CAM_MEM, "Calling dmap buf APIs for cache operations");
  341. cache_dir = DMA_BIDIRECTIONAL;
  342. #else
  343. if (dmabuf_flag & ION_FLAG_CACHED) {
  344. switch (cmd->mem_cache_ops) {
  345. case CAM_MEM_CLEAN_CACHE:
  346. cache_dir = DMA_TO_DEVICE;
  347. break;
  348. case CAM_MEM_INV_CACHE:
  349. cache_dir = DMA_FROM_DEVICE;
  350. break;
  351. case CAM_MEM_CLEAN_INV_CACHE:
  352. cache_dir = DMA_BIDIRECTIONAL;
  353. break;
  354. default:
  355. CAM_ERR(CAM_MEM,
  356. "invalid cache ops :%d", cmd->mem_cache_ops);
  357. rc = -EINVAL;
  358. goto end;
  359. }
  360. } else {
  361. CAM_DBG(CAM_MEM, "BUF is not cached");
  362. goto end;
  363. }
  364. #endif
  365. rc = dma_buf_begin_cpu_access(tbl.bufq[idx].dma_buf,
  366. (cmd->mem_cache_ops == CAM_MEM_CLEAN_INV_CACHE) ?
  367. DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  368. if (rc) {
  369. CAM_ERR(CAM_MEM, "dma begin access failed rc=%d", rc);
  370. goto end;
  371. }
  372. rc = dma_buf_end_cpu_access(tbl.bufq[idx].dma_buf,
  373. cache_dir);
  374. if (rc) {
  375. CAM_ERR(CAM_MEM, "dma end access failed rc=%d", rc);
  376. goto end;
  377. }
  378. end:
  379. mutex_unlock(&tbl.bufq[idx].q_lock);
  380. return rc;
  381. }
  382. EXPORT_SYMBOL(cam_mem_mgr_cache_ops);
  383. #if IS_REACHABLE(CONFIG_DMABUF_HEAPS)
  384. #define CAM_MAX_VMIDS 4
  385. static void cam_mem_mgr_put_dma_heaps(void)
  386. {
  387. CAM_DBG(CAM_MEM, "Releasing DMA Buf heaps usage");
  388. }
  389. static int cam_mem_mgr_get_dma_heaps(void)
  390. {
  391. int rc = 0;
  392. tbl.system_heap = NULL;
  393. tbl.system_uncached_heap = NULL;
  394. tbl.camera_heap = NULL;
  395. tbl.camera_uncached_heap = NULL;
  396. tbl.secure_display_heap = NULL;
  397. tbl.system_heap = dma_heap_find("qcom,system");
  398. if (IS_ERR_OR_NULL(tbl.system_heap)) {
  399. rc = PTR_ERR(tbl.system_heap);
  400. CAM_ERR(CAM_MEM, "qcom system heap not found, rc=%d", rc);
  401. tbl.system_heap = NULL;
  402. goto put_heaps;
  403. }
  404. tbl.system_uncached_heap = dma_heap_find("qcom,system-uncached");
  405. if (IS_ERR_OR_NULL(tbl.system_uncached_heap)) {
  406. if (tbl.force_cache_allocs) {
  407. /* optional, we anyway do not use uncached */
  408. CAM_DBG(CAM_MEM,
  409. "qcom system-uncached heap not found, err=%d",
  410. PTR_ERR(tbl.system_uncached_heap));
  411. tbl.system_uncached_heap = NULL;
  412. } else {
  413. /* fatal, must need uncached heaps */
  414. rc = PTR_ERR(tbl.system_uncached_heap);
  415. CAM_ERR(CAM_MEM,
  416. "qcom system-uncached heap not found, rc=%d",
  417. rc);
  418. tbl.system_uncached_heap = NULL;
  419. goto put_heaps;
  420. }
  421. }
  422. tbl.secure_display_heap = dma_heap_find("qcom,display");
  423. if (IS_ERR_OR_NULL(tbl.secure_display_heap)) {
  424. rc = PTR_ERR(tbl.secure_display_heap);
  425. CAM_ERR(CAM_MEM, "qcom,display heap not found, rc=%d",
  426. rc);
  427. tbl.secure_display_heap = NULL;
  428. goto put_heaps;
  429. }
  430. tbl.camera_heap = dma_heap_find("qcom,camera");
  431. if (IS_ERR_OR_NULL(tbl.camera_heap)) {
  432. /* optional heap, not a fatal error */
  433. CAM_DBG(CAM_MEM, "qcom camera heap not found, err=%d",
  434. PTR_ERR(tbl.camera_heap));
  435. tbl.camera_heap = NULL;
  436. }
  437. tbl.camera_uncached_heap = dma_heap_find("qcom,camera-uncached");
  438. if (IS_ERR_OR_NULL(tbl.camera_uncached_heap)) {
  439. /* optional heap, not a fatal error */
  440. CAM_DBG(CAM_MEM, "qcom camera heap not found, err=%d",
  441. PTR_ERR(tbl.camera_uncached_heap));
  442. tbl.camera_uncached_heap = NULL;
  443. }
  444. CAM_INFO(CAM_MEM,
  445. "Heaps : system=%pK, system_uncached=%pK, camera=%pK, camera-uncached=%pK, secure_display=%pK",
  446. tbl.system_heap, tbl.system_uncached_heap,
  447. tbl.camera_heap, tbl.camera_uncached_heap,
  448. tbl.secure_display_heap);
  449. return 0;
  450. put_heaps:
  451. cam_mem_mgr_put_dma_heaps();
  452. return rc;
  453. }
  454. static int cam_mem_util_get_dma_buf(size_t len,
  455. unsigned int cam_flags,
  456. struct dma_buf **buf,
  457. unsigned long *i_ino)
  458. {
  459. int rc = 0;
  460. struct dma_heap *heap;
  461. struct dma_heap *try_heap = NULL;
  462. struct timespec64 ts1, ts2;
  463. long microsec = 0;
  464. bool use_cached_heap = false;
  465. struct mem_buf_lend_kernel_arg arg;
  466. int vmids[CAM_MAX_VMIDS];
  467. int perms[CAM_MAX_VMIDS];
  468. int num_vmids = 0;
  469. if (!buf) {
  470. CAM_ERR(CAM_MEM, "Invalid params");
  471. return -EINVAL;
  472. }
  473. if (tbl.alloc_profile_enable)
  474. CAM_GET_TIMESTAMP(ts1);
  475. if ((cam_flags & CAM_MEM_FLAG_CACHE) ||
  476. (tbl.force_cache_allocs &&
  477. (!(cam_flags & CAM_MEM_FLAG_PROTECTED_MODE)))) {
  478. CAM_DBG(CAM_MEM,
  479. "Using CACHED heap, cam_flags=0x%x, force_cache_allocs=%d",
  480. cam_flags, tbl.force_cache_allocs);
  481. use_cached_heap = true;
  482. } else if (cam_flags & CAM_MEM_FLAG_PROTECTED_MODE) {
  483. use_cached_heap = true;
  484. CAM_DBG(CAM_MEM,
  485. "Using CACHED heap for secure, cam_flags=0x%x, force_cache_allocs=%d",
  486. cam_flags, tbl.force_cache_allocs);
  487. } else {
  488. use_cached_heap = false;
  489. CAM_ERR(CAM_MEM,
  490. "Using UNCACHED heap not supported, cam_flags=0x%x, force_cache_allocs=%d",
  491. cam_flags, tbl.force_cache_allocs);
  492. /*
  493. * Need a better handling based on whether dma-buf-heaps support
  494. * uncached heaps or not. For now, assume not supported.
  495. */
  496. return -EINVAL;
  497. }
  498. if (cam_flags & CAM_MEM_FLAG_PROTECTED_MODE) {
  499. heap = tbl.secure_display_heap;
  500. vmids[num_vmids] = VMID_CP_CAMERA;
  501. perms[num_vmids] = PERM_READ | PERM_WRITE;
  502. num_vmids++;
  503. if (cam_flags & CAM_MEM_FLAG_CDSP_OUTPUT) {
  504. CAM_DBG(CAM_MEM, "Secure mode CDSP flags");
  505. vmids[num_vmids] = VMID_CP_CDSP;
  506. perms[num_vmids] = PERM_READ | PERM_WRITE;
  507. num_vmids++;
  508. }
  509. } else if (cam_flags & CAM_MEM_FLAG_EVA_NOPIXEL) {
  510. heap = tbl.secure_display_heap;
  511. vmids[num_vmids] = VMID_CP_NON_PIXEL;
  512. perms[num_vmids] = PERM_READ | PERM_WRITE;
  513. num_vmids++;
  514. } else if (use_cached_heap) {
  515. try_heap = tbl.camera_heap;
  516. heap = tbl.system_heap;
  517. } else {
  518. try_heap = tbl.camera_uncached_heap;
  519. heap = tbl.system_uncached_heap;
  520. }
  521. CAM_DBG(CAM_MEM, "Using heaps : try=%pK, heap=%pK", try_heap, heap);
  522. *buf = NULL;
  523. if (!try_heap && !heap) {
  524. CAM_ERR(CAM_MEM,
  525. "No heap available for allocation, cant allocate");
  526. return -EINVAL;
  527. }
  528. if (try_heap) {
  529. *buf = dma_heap_buffer_alloc(try_heap, len, O_RDWR, 0);
  530. if (IS_ERR(*buf)) {
  531. CAM_WARN(CAM_MEM,
  532. "Failed in allocating from try heap, heap=%pK, len=%zu, err=%d",
  533. try_heap, len, PTR_ERR(*buf));
  534. *buf = NULL;
  535. }
  536. }
  537. if (*buf == NULL) {
  538. *buf = dma_heap_buffer_alloc(heap, len, O_RDWR, 0);
  539. if (IS_ERR(*buf)) {
  540. rc = PTR_ERR(*buf);
  541. CAM_ERR(CAM_MEM,
  542. "Failed in allocating from heap, heap=%pK, len=%zu, err=%d",
  543. heap, len, rc);
  544. *buf = NULL;
  545. return rc;
  546. }
  547. }
  548. *i_ino = file_inode((*buf)->file)->i_ino;
  549. if ((cam_flags & CAM_MEM_FLAG_PROTECTED_MODE) ||
  550. (cam_flags & CAM_MEM_FLAG_EVA_NOPIXEL)) {
  551. if (num_vmids >= CAM_MAX_VMIDS) {
  552. CAM_ERR(CAM_MEM, "Insufficient array size for vmids %d", num_vmids);
  553. rc = -EINVAL;
  554. goto end;
  555. }
  556. arg.nr_acl_entries = num_vmids;
  557. arg.vmids = vmids;
  558. arg.perms = perms;
  559. rc = mem_buf_lend(*buf, &arg);
  560. if (rc) {
  561. CAM_ERR(CAM_MEM,
  562. "Failed in buf lend rc=%d, buf=%pK, vmids [0]=0x%x, [1]=0x%x, [2]=0x%x",
  563. rc, *buf, vmids[0], vmids[1], vmids[2]);
  564. goto end;
  565. }
  566. }
  567. CAM_DBG(CAM_MEM, "Allocate success, len=%zu, *buf=%pK, i_ino=%lu", len, *buf, *i_ino);
  568. if (tbl.alloc_profile_enable) {
  569. CAM_GET_TIMESTAMP(ts2);
  570. CAM_GET_TIMESTAMP_DIFF_IN_MICRO(ts1, ts2, microsec);
  571. trace_cam_log_event("IONAllocProfile", "size and time in micro",
  572. len, microsec);
  573. }
  574. return rc;
  575. end:
  576. dma_buf_put(*buf);
  577. return rc;
  578. }
  579. #else
  580. static int cam_mem_util_get_dma_buf(size_t len,
  581. unsigned int cam_flags,
  582. struct dma_buf **buf,
  583. unsigned long *i_ino)
  584. {
  585. int rc = 0;
  586. unsigned int heap_id;
  587. int32_t ion_flag = 0;
  588. struct timespec64 ts1, ts2;
  589. long microsec = 0;
  590. if (!buf) {
  591. CAM_ERR(CAM_MEM, "Invalid params");
  592. return -EINVAL;
  593. }
  594. if (tbl.alloc_profile_enable)
  595. CAM_GET_TIMESTAMP(ts1);
  596. if ((cam_flags & CAM_MEM_FLAG_PROTECTED_MODE) &&
  597. (cam_flags & CAM_MEM_FLAG_CDSP_OUTPUT)) {
  598. heap_id = ION_HEAP(ION_SECURE_DISPLAY_HEAP_ID);
  599. ion_flag |=
  600. ION_FLAG_SECURE | ION_FLAG_CP_CAMERA | ION_FLAG_CP_CDSP;
  601. } else if (cam_flags & CAM_MEM_FLAG_PROTECTED_MODE) {
  602. heap_id = ION_HEAP(ION_SECURE_DISPLAY_HEAP_ID);
  603. ion_flag |= ION_FLAG_SECURE | ION_FLAG_CP_CAMERA;
  604. } else {
  605. heap_id = ION_HEAP(ION_SYSTEM_HEAP_ID) |
  606. ION_HEAP(ION_CAMERA_HEAP_ID);
  607. }
  608. if (cam_flags & CAM_MEM_FLAG_CACHE)
  609. ion_flag |= ION_FLAG_CACHED;
  610. else
  611. ion_flag &= ~ION_FLAG_CACHED;
  612. if (tbl.force_cache_allocs && (!(ion_flag & ION_FLAG_SECURE)))
  613. ion_flag |= ION_FLAG_CACHED;
  614. *buf = ion_alloc(len, heap_id, ion_flag);
  615. if (IS_ERR_OR_NULL(*buf))
  616. return -ENOMEM;
  617. *i_ino = file_inode((*buf)->file)->i_ino;
  618. if (tbl.alloc_profile_enable) {
  619. CAM_GET_TIMESTAMP(ts2);
  620. CAM_GET_TIMESTAMP_DIFF_IN_MICRO(ts1, ts2, microsec);
  621. trace_cam_log_event("IONAllocProfile", "size and time in micro",
  622. len, microsec);
  623. }
  624. return rc;
  625. }
  626. #endif
  627. static int cam_mem_util_buffer_alloc(size_t len, uint32_t flags,
  628. struct dma_buf **dmabuf,
  629. int *fd,
  630. unsigned long *i_ino)
  631. {
  632. int rc;
  633. struct dma_buf *temp_dmabuf = NULL;
  634. rc = cam_mem_util_get_dma_buf(len, flags, dmabuf, i_ino);
  635. if (rc) {
  636. CAM_ERR(CAM_MEM,
  637. "Error allocating dma buf : len=%llu, flags=0x%x",
  638. len, flags);
  639. return rc;
  640. }
  641. *fd = dma_buf_fd(*dmabuf, O_CLOEXEC);
  642. if (*fd < 0) {
  643. CAM_ERR(CAM_MEM, "get fd fail, *fd=%d", *fd);
  644. rc = -EINVAL;
  645. goto put_buf;
  646. }
  647. CAM_DBG(CAM_MEM, "Alloc success : len=%zu, *dmabuf=%pK, fd=%d, i_ino=%lu",
  648. len, *dmabuf, *fd, *i_ino);
  649. /*
  650. * increment the ref count so that ref count becomes 2 here
  651. * when we close fd, refcount becomes 1 and when we do
  652. * dmap_put_buf, ref count becomes 0 and memory will be freed.
  653. */
  654. temp_dmabuf = dma_buf_get(*fd);
  655. if (IS_ERR_OR_NULL(temp_dmabuf)) {
  656. rc = PTR_ERR(temp_dmabuf);
  657. CAM_ERR(CAM_MEM, "dma_buf_get failed, *fd=%d, i_ino=%lu, rc=%d", *fd, *i_ino, rc);
  658. goto put_buf;
  659. }
  660. return rc;
  661. put_buf:
  662. dma_buf_put(*dmabuf);
  663. return rc;
  664. }
  665. static int cam_mem_util_check_alloc_flags(struct cam_mem_mgr_alloc_cmd *cmd)
  666. {
  667. if (cmd->num_hdl > CAM_MEM_MMU_MAX_HANDLE) {
  668. CAM_ERR(CAM_MEM, "Num of mmu hdl exceeded maximum(%d)",
  669. CAM_MEM_MMU_MAX_HANDLE);
  670. return -EINVAL;
  671. }
  672. if (cmd->flags & CAM_MEM_FLAG_PROTECTED_MODE &&
  673. cmd->flags & CAM_MEM_FLAG_KMD_ACCESS) {
  674. CAM_ERR(CAM_MEM, "Kernel mapping in secure mode not allowed");
  675. return -EINVAL;
  676. }
  677. if ((cmd->flags & CAM_MEM_FLAG_EVA_NOPIXEL) &&
  678. (cmd->flags & CAM_MEM_FLAG_PROTECTED_MODE ||
  679. cmd->flags & CAM_MEM_FLAG_KMD_ACCESS)){
  680. CAM_ERR(CAM_MEM,
  681. "Kernel mapping and secure mode not allowed in no pixel mode");
  682. return -EINVAL;
  683. }
  684. return 0;
  685. }
  686. static int cam_mem_util_check_map_flags(struct cam_mem_mgr_map_cmd *cmd)
  687. {
  688. if (!cmd->flags) {
  689. CAM_ERR(CAM_MEM, "Invalid flags");
  690. return -EINVAL;
  691. }
  692. if (cmd->num_hdl > CAM_MEM_MMU_MAX_HANDLE) {
  693. CAM_ERR(CAM_MEM, "Num of mmu hdl %d exceeded maximum(%d)",
  694. cmd->num_hdl, CAM_MEM_MMU_MAX_HANDLE);
  695. return -EINVAL;
  696. }
  697. if (cmd->flags & CAM_MEM_FLAG_PROTECTED_MODE &&
  698. cmd->flags & CAM_MEM_FLAG_KMD_ACCESS) {
  699. CAM_ERR(CAM_MEM,
  700. "Kernel mapping in secure mode not allowed, flags=0x%x",
  701. cmd->flags);
  702. return -EINVAL;
  703. }
  704. if (cmd->flags & CAM_MEM_FLAG_HW_SHARED_ACCESS) {
  705. CAM_ERR(CAM_MEM,
  706. "Shared memory buffers are not allowed to be mapped");
  707. return -EINVAL;
  708. }
  709. return 0;
  710. }
  711. static int cam_mem_util_map_hw_va(uint32_t flags,
  712. int32_t *mmu_hdls,
  713. int32_t num_hdls,
  714. int fd,
  715. struct dma_buf *dmabuf,
  716. dma_addr_t *hw_vaddr,
  717. size_t *len,
  718. enum cam_smmu_region_id region,
  719. bool is_internal)
  720. {
  721. int i;
  722. int rc = -1;
  723. int dir = cam_mem_util_get_dma_dir(flags);
  724. bool dis_delayed_unmap = false;
  725. if (dir < 0) {
  726. CAM_ERR(CAM_MEM, "fail to map DMA direction, dir=%d", dir);
  727. return dir;
  728. }
  729. if (flags & CAM_MEM_FLAG_DISABLE_DELAYED_UNMAP)
  730. dis_delayed_unmap = true;
  731. CAM_DBG(CAM_MEM,
  732. "map_hw_va : fd = %d, flags = 0x%x, dir=%d, num_hdls=%d",
  733. fd, flags, dir, num_hdls);
  734. for (i = 0; i < num_hdls; i++) {
  735. /* If 36-bit enabled, check for ICP cmd buffers and map them within the shared region */
  736. if (cam_smmu_is_expanded_memory() &&
  737. cam_smmu_supports_shared_region(mmu_hdls[i]) &&
  738. (flags & CAM_MEM_FLAG_CMD_BUF_TYPE))
  739. region = CAM_SMMU_REGION_SHARED;
  740. if (flags & CAM_MEM_FLAG_PROTECTED_MODE)
  741. rc = cam_smmu_map_stage2_iova(mmu_hdls[i], fd, dmabuf, dir, hw_vaddr, len);
  742. else
  743. rc = cam_smmu_map_user_iova(mmu_hdls[i], fd, dmabuf, dis_delayed_unmap, dir,
  744. hw_vaddr, len, region, is_internal);
  745. if (rc) {
  746. CAM_ERR(CAM_MEM,
  747. "Failed %s map to smmu, i=%d, fd=%d, dir=%d, mmu_hdl=%d, rc=%d",
  748. (flags & CAM_MEM_FLAG_PROTECTED_MODE) ? "" : "secured",
  749. i, fd, dir, mmu_hdls[i], rc);
  750. goto multi_map_fail;
  751. }
  752. }
  753. return rc;
  754. multi_map_fail:
  755. for (--i; i>= 0; i--) {
  756. if (flags & CAM_MEM_FLAG_PROTECTED_MODE)
  757. cam_smmu_unmap_stage2_iova(mmu_hdls[i], fd, dmabuf);
  758. else
  759. cam_smmu_unmap_user_iova(mmu_hdls[i], fd, dmabuf, CAM_SMMU_REGION_IO);
  760. }
  761. return rc;
  762. }
  763. int cam_mem_mgr_alloc_and_map(struct cam_mem_mgr_alloc_cmd *cmd)
  764. {
  765. int rc;
  766. int32_t idx;
  767. struct dma_buf *dmabuf = NULL;
  768. int fd = -1;
  769. dma_addr_t hw_vaddr = 0;
  770. size_t len;
  771. uintptr_t kvaddr = 0;
  772. size_t klen;
  773. unsigned long i_ino = 0;
  774. if (!atomic_read(&cam_mem_mgr_state)) {
  775. CAM_ERR(CAM_MEM, "failed. mem_mgr not initialized");
  776. return -EINVAL;
  777. }
  778. if (!cmd) {
  779. CAM_ERR(CAM_MEM, " Invalid argument");
  780. return -EINVAL;
  781. }
  782. len = cmd->len;
  783. if (tbl.need_shared_buffer_padding &&
  784. (cmd->flags & CAM_MEM_FLAG_HW_SHARED_ACCESS)) {
  785. len += CAM_MEM_SHARED_BUFFER_PAD_4K;
  786. CAM_DBG(CAM_MEM, "Pad 4k size, actual %llu, allocating %zu",
  787. cmd->len, len);
  788. }
  789. rc = cam_mem_util_check_alloc_flags(cmd);
  790. if (rc) {
  791. CAM_ERR(CAM_MEM, "Invalid flags: flags = 0x%X, rc=%d",
  792. cmd->flags, rc);
  793. return rc;
  794. }
  795. rc = cam_mem_util_buffer_alloc(len, cmd->flags, &dmabuf, &fd, &i_ino);
  796. if (rc) {
  797. CAM_ERR(CAM_MEM,
  798. "Ion Alloc failed, len=%llu, align=%llu, flags=0x%x, num_hdl=%d",
  799. len, cmd->align, cmd->flags, cmd->num_hdl);
  800. cam_mem_mgr_print_tbl();
  801. return rc;
  802. }
  803. if (!dmabuf) {
  804. CAM_ERR(CAM_MEM,
  805. "Ion Alloc return NULL dmabuf! fd=%d, i_ino=%lu, len=%d", fd, i_ino, len);
  806. cam_mem_mgr_print_tbl();
  807. return rc;
  808. }
  809. idx = cam_mem_get_slot();
  810. if (idx < 0) {
  811. CAM_ERR(CAM_MEM, "Failed in getting mem slot, idx=%d", idx);
  812. rc = -ENOMEM;
  813. goto slot_fail;
  814. }
  815. if ((cmd->flags & CAM_MEM_FLAG_HW_READ_WRITE) ||
  816. (cmd->flags & CAM_MEM_FLAG_HW_SHARED_ACCESS) ||
  817. (cmd->flags & CAM_MEM_FLAG_PROTECTED_MODE)) {
  818. enum cam_smmu_region_id region;
  819. if (cmd->flags & CAM_MEM_FLAG_HW_READ_WRITE)
  820. region = CAM_SMMU_REGION_IO;
  821. if (cmd->flags & CAM_MEM_FLAG_HW_SHARED_ACCESS)
  822. region = CAM_SMMU_REGION_SHARED;
  823. if (cmd->flags & CAM_MEM_FLAG_PROTECTED_MODE)
  824. region = CAM_SMMU_REGION_IO;
  825. rc = cam_mem_util_map_hw_va(cmd->flags,
  826. cmd->mmu_hdls,
  827. cmd->num_hdl,
  828. fd,
  829. dmabuf,
  830. &hw_vaddr,
  831. &len,
  832. region,
  833. true);
  834. if (rc) {
  835. CAM_ERR(CAM_MEM,
  836. "Failed in map_hw_va len=%llu, flags=0x%x, fd=%d, region=%d, num_hdl=%d, rc=%d",
  837. len, cmd->flags,
  838. fd, region, cmd->num_hdl, rc);
  839. if (rc == -EALREADY) {
  840. if ((size_t)dmabuf->size != len)
  841. rc = -EBADR;
  842. cam_mem_mgr_print_tbl();
  843. }
  844. goto map_hw_fail;
  845. }
  846. }
  847. mutex_lock(&tbl.bufq[idx].q_lock);
  848. tbl.bufq[idx].fd = fd;
  849. tbl.bufq[idx].i_ino = i_ino;
  850. tbl.bufq[idx].dma_buf = NULL;
  851. tbl.bufq[idx].flags = cmd->flags;
  852. tbl.bufq[idx].buf_handle = GET_MEM_HANDLE(idx, fd);
  853. tbl.bufq[idx].is_internal = true;
  854. if (cmd->flags & CAM_MEM_FLAG_PROTECTED_MODE)
  855. CAM_MEM_MGR_SET_SECURE_HDL(tbl.bufq[idx].buf_handle, true);
  856. if (cmd->flags & CAM_MEM_FLAG_KMD_ACCESS) {
  857. rc = cam_mem_util_map_cpu_va(dmabuf, &kvaddr, &klen);
  858. if (rc) {
  859. CAM_ERR(CAM_MEM, "dmabuf: %pK mapping failed: %d",
  860. dmabuf, rc);
  861. goto map_kernel_fail;
  862. }
  863. }
  864. if (cmd->flags & CAM_MEM_FLAG_KMD_DEBUG_FLAG)
  865. tbl.dbg_buf_idx = idx;
  866. tbl.bufq[idx].kmdvaddr = kvaddr;
  867. tbl.bufq[idx].vaddr = hw_vaddr;
  868. tbl.bufq[idx].dma_buf = dmabuf;
  869. tbl.bufq[idx].len = len;
  870. tbl.bufq[idx].num_hdl = cmd->num_hdl;
  871. cam_mem_mgr_reset_presil_params(idx);
  872. memcpy(tbl.bufq[idx].hdls, cmd->mmu_hdls,
  873. sizeof(int32_t) * cmd->num_hdl);
  874. tbl.bufq[idx].is_imported = false;
  875. mutex_unlock(&tbl.bufq[idx].q_lock);
  876. cmd->out.buf_handle = tbl.bufq[idx].buf_handle;
  877. cmd->out.fd = tbl.bufq[idx].fd;
  878. cmd->out.vaddr = 0;
  879. CAM_DBG(CAM_MEM,
  880. "fd=%d, flags=0x%x, num_hdl=%d, idx=%d, buf handle=%x, len=%zu, i_ino=%lu",
  881. cmd->out.fd, cmd->flags, cmd->num_hdl, idx, cmd->out.buf_handle,
  882. tbl.bufq[idx].len, tbl.bufq[idx].i_ino);
  883. return rc;
  884. map_kernel_fail:
  885. mutex_unlock(&tbl.bufq[idx].q_lock);
  886. map_hw_fail:
  887. cam_mem_put_slot(idx);
  888. slot_fail:
  889. dma_buf_put(dmabuf);
  890. return rc;
  891. }
  892. static bool cam_mem_util_is_map_internal(int32_t fd, unsigned i_ino)
  893. {
  894. uint32_t i;
  895. bool is_internal = false;
  896. mutex_lock(&tbl.m_lock);
  897. for_each_set_bit(i, tbl.bitmap, tbl.bits) {
  898. if ((tbl.bufq[i].fd == fd) && (tbl.bufq[i].i_ino == i_ino)) {
  899. is_internal = tbl.bufq[i].is_internal;
  900. break;
  901. }
  902. }
  903. mutex_unlock(&tbl.m_lock);
  904. return is_internal;
  905. }
  906. int cam_mem_mgr_map(struct cam_mem_mgr_map_cmd *cmd)
  907. {
  908. int32_t idx;
  909. int rc;
  910. struct dma_buf *dmabuf;
  911. dma_addr_t hw_vaddr = 0;
  912. size_t len = 0;
  913. bool is_internal = false;
  914. unsigned long i_ino;
  915. if (!atomic_read(&cam_mem_mgr_state)) {
  916. CAM_ERR(CAM_MEM, "failed. mem_mgr not initialized");
  917. return -EINVAL;
  918. }
  919. if (!cmd || (cmd->fd < 0)) {
  920. CAM_ERR(CAM_MEM, "Invalid argument");
  921. return -EINVAL;
  922. }
  923. if (cmd->num_hdl > CAM_MEM_MMU_MAX_HANDLE) {
  924. CAM_ERR(CAM_MEM, "Num of mmu hdl %d exceeded maximum(%d)",
  925. cmd->num_hdl, CAM_MEM_MMU_MAX_HANDLE);
  926. return -EINVAL;
  927. }
  928. rc = cam_mem_util_check_map_flags(cmd);
  929. if (rc) {
  930. CAM_ERR(CAM_MEM, "Invalid flags: flags = %X", cmd->flags);
  931. return rc;
  932. }
  933. dmabuf = dma_buf_get(cmd->fd);
  934. if (IS_ERR_OR_NULL((void *)(dmabuf))) {
  935. CAM_ERR(CAM_MEM, "Failed to import dma_buf fd");
  936. return -EINVAL;
  937. }
  938. i_ino = file_inode(dmabuf->file)->i_ino;
  939. is_internal = cam_mem_util_is_map_internal(cmd->fd, i_ino);
  940. idx = cam_mem_get_slot();
  941. if (idx < 0) {
  942. CAM_ERR(CAM_MEM, "Failed in getting mem slot, idx=%d, fd=%d",
  943. idx, cmd->fd);
  944. rc = -ENOMEM;
  945. goto slot_fail;
  946. }
  947. if ((cmd->flags & CAM_MEM_FLAG_HW_READ_WRITE) ||
  948. (cmd->flags & CAM_MEM_FLAG_PROTECTED_MODE)) {
  949. rc = cam_mem_util_map_hw_va(cmd->flags,
  950. cmd->mmu_hdls,
  951. cmd->num_hdl,
  952. cmd->fd,
  953. dmabuf,
  954. &hw_vaddr,
  955. &len,
  956. CAM_SMMU_REGION_IO,
  957. is_internal);
  958. if (rc) {
  959. CAM_ERR(CAM_MEM,
  960. "Failed in map_hw_va, flags=0x%x, fd=%d, len=%llu, region=%d, num_hdl=%d, rc=%d",
  961. cmd->flags, cmd->fd, len,
  962. CAM_SMMU_REGION_IO, cmd->num_hdl, rc);
  963. if (rc == -EALREADY) {
  964. if ((size_t)dmabuf->size != len) {
  965. rc = -EBADR;
  966. cam_mem_mgr_print_tbl();
  967. }
  968. }
  969. goto map_fail;
  970. }
  971. }
  972. mutex_lock(&tbl.bufq[idx].q_lock);
  973. tbl.bufq[idx].fd = cmd->fd;
  974. tbl.bufq[idx].i_ino = i_ino;
  975. tbl.bufq[idx].dma_buf = NULL;
  976. tbl.bufq[idx].flags = cmd->flags;
  977. tbl.bufq[idx].buf_handle = GET_MEM_HANDLE(idx, cmd->fd);
  978. if (cmd->flags & CAM_MEM_FLAG_PROTECTED_MODE)
  979. CAM_MEM_MGR_SET_SECURE_HDL(tbl.bufq[idx].buf_handle, true);
  980. tbl.bufq[idx].kmdvaddr = 0;
  981. if (cmd->num_hdl > 0)
  982. tbl.bufq[idx].vaddr = hw_vaddr;
  983. else
  984. tbl.bufq[idx].vaddr = 0;
  985. tbl.bufq[idx].dma_buf = dmabuf;
  986. tbl.bufq[idx].len = len;
  987. tbl.bufq[idx].num_hdl = cmd->num_hdl;
  988. memcpy(tbl.bufq[idx].hdls, cmd->mmu_hdls,
  989. sizeof(int32_t) * cmd->num_hdl);
  990. tbl.bufq[idx].is_imported = true;
  991. tbl.bufq[idx].is_internal = is_internal;
  992. mutex_unlock(&tbl.bufq[idx].q_lock);
  993. cmd->out.buf_handle = tbl.bufq[idx].buf_handle;
  994. cmd->out.vaddr = 0;
  995. cmd->out.size = (uint32_t)len;
  996. CAM_DBG(CAM_MEM,
  997. "fd=%d, flags=0x%x, num_hdl=%d, idx=%d, buf handle=%x, len=%zu, i_ino=%lu",
  998. cmd->fd, cmd->flags, cmd->num_hdl, idx, cmd->out.buf_handle,
  999. tbl.bufq[idx].len, tbl.bufq[idx].i_ino);
  1000. return rc;
  1001. map_fail:
  1002. cam_mem_put_slot(idx);
  1003. slot_fail:
  1004. dma_buf_put(dmabuf);
  1005. return rc;
  1006. }
  1007. static int cam_mem_util_unmap_hw_va(int32_t idx,
  1008. enum cam_smmu_region_id region,
  1009. enum cam_smmu_mapping_client client)
  1010. {
  1011. int i;
  1012. uint32_t flags;
  1013. int32_t *mmu_hdls;
  1014. int num_hdls;
  1015. int fd;
  1016. struct dma_buf *dma_buf;
  1017. unsigned long i_ino;
  1018. int rc = 0;
  1019. if (idx >= CAM_MEM_BUFQ_MAX || idx <= 0) {
  1020. CAM_ERR(CAM_MEM, "Incorrect index");
  1021. return -EINVAL;
  1022. }
  1023. flags = tbl.bufq[idx].flags;
  1024. mmu_hdls = tbl.bufq[idx].hdls;
  1025. num_hdls = tbl.bufq[idx].num_hdl;
  1026. fd = tbl.bufq[idx].fd;
  1027. dma_buf = tbl.bufq[idx].dma_buf;
  1028. i_ino = tbl.bufq[idx].i_ino;
  1029. CAM_DBG(CAM_MEM,
  1030. "unmap_hw_va : idx=%d, fd=%x, i_ino=%lu flags=0x%x, num_hdls=%d, client=%d",
  1031. idx, fd, i_ino, flags, num_hdls, client);
  1032. if (flags & CAM_MEM_FLAG_PROTECTED_MODE) {
  1033. for (i = 0; i < num_hdls; i++) {
  1034. rc = cam_smmu_unmap_stage2_iova(mmu_hdls[i], fd, dma_buf);
  1035. if (rc < 0) {
  1036. CAM_ERR(CAM_MEM,
  1037. "Failed in secure unmap, i=%d, fd=%d, i_ino=%lu, mmu_hdl=%d, rc=%d",
  1038. i, fd, i_ino, mmu_hdls[i], rc);
  1039. goto unmap_end;
  1040. }
  1041. }
  1042. } else {
  1043. for (i = 0; i < num_hdls; i++) {
  1044. if (client == CAM_SMMU_MAPPING_USER) {
  1045. rc = cam_smmu_unmap_user_iova(mmu_hdls[i],
  1046. fd, dma_buf, region);
  1047. } else if (client == CAM_SMMU_MAPPING_KERNEL) {
  1048. rc = cam_smmu_unmap_kernel_iova(mmu_hdls[i],
  1049. tbl.bufq[idx].dma_buf, region);
  1050. } else {
  1051. CAM_ERR(CAM_MEM,
  1052. "invalid caller for unmapping : %d",
  1053. client);
  1054. rc = -EINVAL;
  1055. }
  1056. if (rc < 0) {
  1057. CAM_ERR(CAM_MEM,
  1058. "Failed in unmap, i=%d, fd=%d, i_ino=%lu, mmu_hdl=%d, region=%d, rc=%d",
  1059. i, fd, i_ino, mmu_hdls[i], region, rc);
  1060. goto unmap_end;
  1061. }
  1062. }
  1063. }
  1064. return rc;
  1065. unmap_end:
  1066. CAM_ERR(CAM_MEM, "unmapping failed");
  1067. return rc;
  1068. }
  1069. static void cam_mem_mgr_unmap_active_buf(int idx)
  1070. {
  1071. enum cam_smmu_region_id region = CAM_SMMU_REGION_SHARED;
  1072. if (tbl.bufq[idx].flags & CAM_MEM_FLAG_HW_SHARED_ACCESS)
  1073. region = CAM_SMMU_REGION_SHARED;
  1074. else if (tbl.bufq[idx].flags & CAM_MEM_FLAG_HW_READ_WRITE)
  1075. region = CAM_SMMU_REGION_IO;
  1076. cam_mem_util_unmap_hw_va(idx, region, CAM_SMMU_MAPPING_USER);
  1077. if (tbl.bufq[idx].flags & CAM_MEM_FLAG_KMD_ACCESS)
  1078. cam_mem_util_unmap_cpu_va(tbl.bufq[idx].dma_buf,
  1079. tbl.bufq[idx].kmdvaddr);
  1080. }
  1081. static int cam_mem_mgr_cleanup_table(void)
  1082. {
  1083. int i;
  1084. mutex_lock(&tbl.m_lock);
  1085. for (i = 1; i < CAM_MEM_BUFQ_MAX; i++) {
  1086. if (!tbl.bufq[i].active) {
  1087. CAM_DBG(CAM_MEM,
  1088. "Buffer inactive at idx=%d, continuing", i);
  1089. continue;
  1090. } else {
  1091. CAM_DBG(CAM_MEM,
  1092. "Active buffer at idx=%d, possible leak needs unmapping",
  1093. i);
  1094. cam_mem_mgr_unmap_active_buf(i);
  1095. }
  1096. mutex_lock(&tbl.bufq[i].q_lock);
  1097. if (tbl.bufq[i].dma_buf) {
  1098. dma_buf_put(tbl.bufq[i].dma_buf);
  1099. tbl.bufq[i].dma_buf = NULL;
  1100. }
  1101. tbl.bufq[i].fd = -1;
  1102. tbl.bufq[i].i_ino = 0;
  1103. tbl.bufq[i].flags = 0;
  1104. tbl.bufq[i].buf_handle = -1;
  1105. tbl.bufq[i].vaddr = 0;
  1106. tbl.bufq[i].len = 0;
  1107. memset(tbl.bufq[i].hdls, 0,
  1108. sizeof(int32_t) * tbl.bufq[i].num_hdl);
  1109. tbl.bufq[i].num_hdl = 0;
  1110. tbl.bufq[i].dma_buf = NULL;
  1111. tbl.bufq[i].active = false;
  1112. tbl.bufq[i].is_internal = false;
  1113. cam_mem_mgr_reset_presil_params(i);
  1114. mutex_unlock(&tbl.bufq[i].q_lock);
  1115. mutex_destroy(&tbl.bufq[i].q_lock);
  1116. }
  1117. bitmap_zero(tbl.bitmap, tbl.bits);
  1118. /* We need to reserve slot 0 because 0 is invalid */
  1119. set_bit(0, tbl.bitmap);
  1120. mutex_unlock(&tbl.m_lock);
  1121. return 0;
  1122. }
  1123. void cam_mem_mgr_deinit(void)
  1124. {
  1125. if (!atomic_read(&cam_mem_mgr_state))
  1126. return;
  1127. atomic_set(&cam_mem_mgr_state, CAM_MEM_MGR_UNINITIALIZED);
  1128. cam_mem_mgr_cleanup_table();
  1129. debugfs_remove_recursive(tbl.dentry);
  1130. mutex_lock(&tbl.m_lock);
  1131. bitmap_zero(tbl.bitmap, tbl.bits);
  1132. kfree(tbl.bitmap);
  1133. tbl.bitmap = NULL;
  1134. tbl.dbg_buf_idx = -1;
  1135. mutex_unlock(&tbl.m_lock);
  1136. mutex_destroy(&tbl.m_lock);
  1137. }
  1138. static int cam_mem_util_unmap(int32_t idx,
  1139. enum cam_smmu_mapping_client client)
  1140. {
  1141. int rc = 0;
  1142. enum cam_smmu_region_id region = CAM_SMMU_REGION_SHARED;
  1143. if (idx >= CAM_MEM_BUFQ_MAX || idx <= 0) {
  1144. CAM_ERR(CAM_MEM, "Incorrect index");
  1145. return -EINVAL;
  1146. }
  1147. CAM_DBG(CAM_MEM, "Flags = %X idx %d", tbl.bufq[idx].flags, idx);
  1148. mutex_lock(&tbl.m_lock);
  1149. if ((!tbl.bufq[idx].active) &&
  1150. (tbl.bufq[idx].vaddr) == 0) {
  1151. CAM_WARN(CAM_MEM, "Buffer at idx=%d is already unmapped,",
  1152. idx);
  1153. mutex_unlock(&tbl.m_lock);
  1154. return 0;
  1155. }
  1156. /* Deactivate the buffer queue to prevent multiple unmap */
  1157. mutex_lock(&tbl.bufq[idx].q_lock);
  1158. tbl.bufq[idx].active = false;
  1159. tbl.bufq[idx].vaddr = 0;
  1160. mutex_unlock(&tbl.bufq[idx].q_lock);
  1161. mutex_unlock(&tbl.m_lock);
  1162. if (tbl.bufq[idx].flags & CAM_MEM_FLAG_KMD_ACCESS) {
  1163. if (tbl.bufq[idx].dma_buf && tbl.bufq[idx].kmdvaddr) {
  1164. rc = cam_mem_util_unmap_cpu_va(tbl.bufq[idx].dma_buf,
  1165. tbl.bufq[idx].kmdvaddr);
  1166. if (rc)
  1167. CAM_ERR(CAM_MEM,
  1168. "Failed, dmabuf=%pK, kmdvaddr=%pK",
  1169. tbl.bufq[idx].dma_buf,
  1170. (void *) tbl.bufq[idx].kmdvaddr);
  1171. }
  1172. }
  1173. /* SHARED flag gets precedence, all other flags after it */
  1174. if (tbl.bufq[idx].flags & CAM_MEM_FLAG_HW_SHARED_ACCESS) {
  1175. region = CAM_SMMU_REGION_SHARED;
  1176. } else {
  1177. if (tbl.bufq[idx].flags & CAM_MEM_FLAG_HW_READ_WRITE)
  1178. region = CAM_SMMU_REGION_IO;
  1179. }
  1180. if ((tbl.bufq[idx].flags & CAM_MEM_FLAG_HW_READ_WRITE) ||
  1181. (tbl.bufq[idx].flags & CAM_MEM_FLAG_HW_SHARED_ACCESS) ||
  1182. (tbl.bufq[idx].flags & CAM_MEM_FLAG_PROTECTED_MODE)) {
  1183. if (cam_mem_util_unmap_hw_va(idx, region, client))
  1184. CAM_ERR(CAM_MEM, "Failed, dmabuf=%pK",
  1185. tbl.bufq[idx].dma_buf);
  1186. /*
  1187. * Workaround as smmu driver doing put_buf without get_buf for kernel mappings
  1188. * Setting NULL here so that we dont call dma_buf_pt again below
  1189. */
  1190. if (client == CAM_SMMU_MAPPING_KERNEL)
  1191. tbl.bufq[idx].dma_buf = NULL;
  1192. }
  1193. mutex_lock(&tbl.m_lock);
  1194. mutex_lock(&tbl.bufq[idx].q_lock);
  1195. tbl.bufq[idx].flags = 0;
  1196. tbl.bufq[idx].buf_handle = -1;
  1197. memset(tbl.bufq[idx].hdls, 0,
  1198. sizeof(int32_t) * CAM_MEM_MMU_MAX_HANDLE);
  1199. CAM_DBG(CAM_MEM,
  1200. "Ion buf at idx = %d freeing fd = %d, imported %d, dma_buf %pK, i_ino %lu",
  1201. idx, tbl.bufq[idx].fd, tbl.bufq[idx].is_imported, tbl.bufq[idx].dma_buf,
  1202. tbl.bufq[idx].i_ino);
  1203. if (tbl.bufq[idx].dma_buf)
  1204. dma_buf_put(tbl.bufq[idx].dma_buf);
  1205. tbl.bufq[idx].fd = -1;
  1206. tbl.bufq[idx].i_ino = 0;
  1207. tbl.bufq[idx].dma_buf = NULL;
  1208. tbl.bufq[idx].is_imported = false;
  1209. tbl.bufq[idx].is_internal = false;
  1210. tbl.bufq[idx].len = 0;
  1211. tbl.bufq[idx].num_hdl = 0;
  1212. cam_mem_mgr_reset_presil_params(idx);
  1213. memset(&tbl.bufq[idx].timestamp, 0, sizeof(struct timespec64));
  1214. mutex_unlock(&tbl.bufq[idx].q_lock);
  1215. mutex_destroy(&tbl.bufq[idx].q_lock);
  1216. clear_bit(idx, tbl.bitmap);
  1217. mutex_unlock(&tbl.m_lock);
  1218. return rc;
  1219. }
  1220. int cam_mem_mgr_release(struct cam_mem_mgr_release_cmd *cmd)
  1221. {
  1222. int idx;
  1223. int rc;
  1224. if (!atomic_read(&cam_mem_mgr_state)) {
  1225. CAM_ERR(CAM_MEM, "failed. mem_mgr not initialized");
  1226. return -EINVAL;
  1227. }
  1228. if (!cmd) {
  1229. CAM_ERR(CAM_MEM, "Invalid argument");
  1230. return -EINVAL;
  1231. }
  1232. idx = CAM_MEM_MGR_GET_HDL_IDX(cmd->buf_handle);
  1233. if (idx >= CAM_MEM_BUFQ_MAX || idx <= 0) {
  1234. CAM_ERR(CAM_MEM, "Incorrect index %d extracted from mem handle",
  1235. idx);
  1236. return -EINVAL;
  1237. }
  1238. if (!tbl.bufq[idx].active) {
  1239. CAM_ERR(CAM_MEM, "Released buffer state should be active");
  1240. return -EINVAL;
  1241. }
  1242. if (tbl.bufq[idx].buf_handle != cmd->buf_handle) {
  1243. CAM_ERR(CAM_MEM,
  1244. "Released buf handle %d not matching within table %d, idx=%d",
  1245. cmd->buf_handle, tbl.bufq[idx].buf_handle, idx);
  1246. return -EINVAL;
  1247. }
  1248. CAM_DBG(CAM_MEM, "Releasing hdl = %x, idx = %d", cmd->buf_handle, idx);
  1249. rc = cam_mem_util_unmap(idx, CAM_SMMU_MAPPING_USER);
  1250. return rc;
  1251. }
  1252. int cam_mem_mgr_request_mem(struct cam_mem_mgr_request_desc *inp,
  1253. struct cam_mem_mgr_memory_desc *out)
  1254. {
  1255. struct dma_buf *buf = NULL;
  1256. int ion_fd = -1;
  1257. int rc = 0;
  1258. uintptr_t kvaddr;
  1259. dma_addr_t iova = 0;
  1260. size_t request_len = 0;
  1261. uint32_t mem_handle;
  1262. int32_t idx;
  1263. int32_t smmu_hdl = 0;
  1264. int32_t num_hdl = 0;
  1265. unsigned long i_ino = 0;
  1266. enum cam_smmu_region_id region = CAM_SMMU_REGION_SHARED;
  1267. if (!atomic_read(&cam_mem_mgr_state)) {
  1268. CAM_ERR(CAM_MEM, "failed. mem_mgr not initialized");
  1269. return -EINVAL;
  1270. }
  1271. if (!inp || !out) {
  1272. CAM_ERR(CAM_MEM, "Invalid params");
  1273. return -EINVAL;
  1274. }
  1275. if (!(inp->flags & CAM_MEM_FLAG_HW_READ_WRITE ||
  1276. inp->flags & CAM_MEM_FLAG_HW_SHARED_ACCESS ||
  1277. inp->flags & CAM_MEM_FLAG_CACHE)) {
  1278. CAM_ERR(CAM_MEM, "Invalid flags for request mem");
  1279. return -EINVAL;
  1280. }
  1281. rc = cam_mem_util_get_dma_buf(inp->size, inp->flags, &buf, &i_ino);
  1282. if (rc) {
  1283. CAM_ERR(CAM_MEM, "ION alloc failed for shared buffer");
  1284. goto ion_fail;
  1285. } else if (!buf) {
  1286. CAM_ERR(CAM_MEM, "ION alloc returned NULL buffer");
  1287. goto ion_fail;
  1288. } else {
  1289. CAM_DBG(CAM_MEM, "Got dma_buf = %pK", buf);
  1290. }
  1291. /*
  1292. * we are mapping kva always here,
  1293. * update flags so that we do unmap properly
  1294. */
  1295. inp->flags |= CAM_MEM_FLAG_KMD_ACCESS;
  1296. rc = cam_mem_util_map_cpu_va(buf, &kvaddr, &request_len);
  1297. if (rc) {
  1298. CAM_ERR(CAM_MEM, "Failed to get kernel vaddr");
  1299. goto map_fail;
  1300. }
  1301. if (!inp->smmu_hdl) {
  1302. CAM_ERR(CAM_MEM, "Invalid SMMU handle");
  1303. rc = -EINVAL;
  1304. goto smmu_fail;
  1305. }
  1306. /* SHARED flag gets precedence, all other flags after it */
  1307. if (inp->flags & CAM_MEM_FLAG_HW_SHARED_ACCESS) {
  1308. region = CAM_SMMU_REGION_SHARED;
  1309. } else {
  1310. if (inp->flags & CAM_MEM_FLAG_HW_READ_WRITE)
  1311. region = CAM_SMMU_REGION_IO;
  1312. }
  1313. rc = cam_smmu_map_kernel_iova(inp->smmu_hdl,
  1314. buf,
  1315. CAM_SMMU_MAP_RW,
  1316. &iova,
  1317. &request_len,
  1318. region);
  1319. if (rc < 0) {
  1320. CAM_ERR(CAM_MEM, "SMMU mapping failed");
  1321. goto smmu_fail;
  1322. }
  1323. smmu_hdl = inp->smmu_hdl;
  1324. num_hdl = 1;
  1325. idx = cam_mem_get_slot();
  1326. if (idx < 0) {
  1327. CAM_ERR(CAM_MEM, "Failed in getting mem slot, idx=%d", idx);
  1328. rc = -ENOMEM;
  1329. goto slot_fail;
  1330. }
  1331. mutex_lock(&tbl.bufq[idx].q_lock);
  1332. mem_handle = GET_MEM_HANDLE(idx, ion_fd);
  1333. tbl.bufq[idx].dma_buf = buf;
  1334. tbl.bufq[idx].fd = -1;
  1335. tbl.bufq[idx].i_ino = i_ino;
  1336. tbl.bufq[idx].flags = inp->flags;
  1337. tbl.bufq[idx].buf_handle = mem_handle;
  1338. tbl.bufq[idx].kmdvaddr = kvaddr;
  1339. tbl.bufq[idx].vaddr = iova;
  1340. tbl.bufq[idx].len = inp->size;
  1341. tbl.bufq[idx].num_hdl = num_hdl;
  1342. memcpy(tbl.bufq[idx].hdls, &smmu_hdl,
  1343. sizeof(int32_t));
  1344. tbl.bufq[idx].is_imported = false;
  1345. mutex_unlock(&tbl.bufq[idx].q_lock);
  1346. out->kva = kvaddr;
  1347. out->iova = (uint32_t)iova;
  1348. out->smmu_hdl = smmu_hdl;
  1349. out->mem_handle = mem_handle;
  1350. out->len = inp->size;
  1351. out->region = region;
  1352. CAM_DBG(CAM_MEM, "idx=%d, dmabuf=%pK, i_ino=%lu, flags=0x%x, mem_handle=0x%x",
  1353. idx, buf, i_ino, inp->flags, mem_handle);
  1354. return rc;
  1355. slot_fail:
  1356. cam_smmu_unmap_kernel_iova(inp->smmu_hdl,
  1357. buf, region);
  1358. smmu_fail:
  1359. cam_mem_util_unmap_cpu_va(buf, kvaddr);
  1360. map_fail:
  1361. dma_buf_put(buf);
  1362. ion_fail:
  1363. return rc;
  1364. }
  1365. EXPORT_SYMBOL(cam_mem_mgr_request_mem);
  1366. int cam_mem_mgr_release_mem(struct cam_mem_mgr_memory_desc *inp)
  1367. {
  1368. int32_t idx;
  1369. int rc;
  1370. if (!atomic_read(&cam_mem_mgr_state)) {
  1371. CAM_ERR(CAM_MEM, "failed. mem_mgr not initialized");
  1372. return -EINVAL;
  1373. }
  1374. if (!inp) {
  1375. CAM_ERR(CAM_MEM, "Invalid argument");
  1376. return -EINVAL;
  1377. }
  1378. idx = CAM_MEM_MGR_GET_HDL_IDX(inp->mem_handle);
  1379. if (idx >= CAM_MEM_BUFQ_MAX || idx <= 0) {
  1380. CAM_ERR(CAM_MEM, "Incorrect index extracted from mem handle");
  1381. return -EINVAL;
  1382. }
  1383. if (!tbl.bufq[idx].active) {
  1384. if (tbl.bufq[idx].vaddr == 0) {
  1385. CAM_ERR(CAM_MEM, "buffer is released already");
  1386. return 0;
  1387. }
  1388. CAM_ERR(CAM_MEM, "Released buffer state should be active");
  1389. return -EINVAL;
  1390. }
  1391. if (tbl.bufq[idx].buf_handle != inp->mem_handle) {
  1392. CAM_ERR(CAM_MEM,
  1393. "Released buf handle not matching within table");
  1394. return -EINVAL;
  1395. }
  1396. CAM_DBG(CAM_MEM, "Releasing hdl = %X", inp->mem_handle);
  1397. rc = cam_mem_util_unmap(idx, CAM_SMMU_MAPPING_KERNEL);
  1398. return rc;
  1399. }
  1400. EXPORT_SYMBOL(cam_mem_mgr_release_mem);
  1401. int cam_mem_mgr_reserve_memory_region(struct cam_mem_mgr_request_desc *inp,
  1402. enum cam_smmu_region_id region,
  1403. struct cam_mem_mgr_memory_desc *out)
  1404. {
  1405. struct dma_buf *buf = NULL;
  1406. int rc = 0;
  1407. int ion_fd = -1;
  1408. dma_addr_t iova = 0;
  1409. size_t request_len = 0;
  1410. uint32_t mem_handle;
  1411. int32_t idx;
  1412. int32_t smmu_hdl = 0;
  1413. int32_t num_hdl = 0;
  1414. uintptr_t kvaddr = 0;
  1415. unsigned long i_ino = 0;
  1416. if (!atomic_read(&cam_mem_mgr_state)) {
  1417. CAM_ERR(CAM_MEM, "failed. mem_mgr not initialized");
  1418. return -EINVAL;
  1419. }
  1420. if (!inp || !out) {
  1421. CAM_ERR(CAM_MEM, "Invalid param(s)");
  1422. return -EINVAL;
  1423. }
  1424. if (!inp->smmu_hdl) {
  1425. CAM_ERR(CAM_MEM, "Invalid SMMU handle");
  1426. return -EINVAL;
  1427. }
  1428. if ((region != CAM_SMMU_REGION_SECHEAP) &&
  1429. (region != CAM_SMMU_REGION_FWUNCACHED)) {
  1430. CAM_ERR(CAM_MEM, "Only secondary heap supported");
  1431. return -EINVAL;
  1432. }
  1433. rc = cam_mem_util_get_dma_buf(inp->size, 0, &buf, &i_ino);
  1434. if (rc) {
  1435. CAM_ERR(CAM_MEM, "ION alloc failed for sec heap buffer");
  1436. goto ion_fail;
  1437. } else if (!buf) {
  1438. CAM_ERR(CAM_MEM, "ION alloc returned NULL buffer");
  1439. goto ion_fail;
  1440. } else {
  1441. CAM_DBG(CAM_MEM, "Got dma_buf = %pK", buf);
  1442. }
  1443. if (inp->flags & CAM_MEM_FLAG_KMD_ACCESS) {
  1444. rc = cam_mem_util_map_cpu_va(buf, &kvaddr, &request_len);
  1445. if (rc) {
  1446. CAM_ERR(CAM_MEM, "Failed to get kernel vaddr");
  1447. goto kmap_fail;
  1448. }
  1449. }
  1450. rc = cam_smmu_reserve_buf_region(region,
  1451. inp->smmu_hdl, buf, &iova, &request_len);
  1452. if (rc) {
  1453. CAM_ERR(CAM_MEM, "Reserving secondary heap failed");
  1454. goto smmu_fail;
  1455. }
  1456. smmu_hdl = inp->smmu_hdl;
  1457. num_hdl = 1;
  1458. idx = cam_mem_get_slot();
  1459. if (idx < 0) {
  1460. CAM_ERR(CAM_MEM, "Failed in getting mem slot, idx=%d", idx);
  1461. rc = -ENOMEM;
  1462. goto slot_fail;
  1463. }
  1464. mutex_lock(&tbl.bufq[idx].q_lock);
  1465. mem_handle = GET_MEM_HANDLE(idx, ion_fd);
  1466. tbl.bufq[idx].fd = -1;
  1467. tbl.bufq[idx].i_ino = i_ino;
  1468. tbl.bufq[idx].dma_buf = buf;
  1469. tbl.bufq[idx].flags = inp->flags;
  1470. tbl.bufq[idx].buf_handle = mem_handle;
  1471. tbl.bufq[idx].kmdvaddr = kvaddr;
  1472. tbl.bufq[idx].vaddr = iova;
  1473. tbl.bufq[idx].len = request_len;
  1474. tbl.bufq[idx].num_hdl = num_hdl;
  1475. memcpy(tbl.bufq[idx].hdls, &smmu_hdl,
  1476. sizeof(int32_t));
  1477. tbl.bufq[idx].is_imported = false;
  1478. mutex_unlock(&tbl.bufq[idx].q_lock);
  1479. out->kva = kvaddr;
  1480. out->iova = (uint32_t)iova;
  1481. out->smmu_hdl = smmu_hdl;
  1482. out->mem_handle = mem_handle;
  1483. out->len = request_len;
  1484. out->region = region;
  1485. return rc;
  1486. slot_fail:
  1487. cam_smmu_release_buf_region(region, smmu_hdl);
  1488. smmu_fail:
  1489. if (region == CAM_SMMU_REGION_FWUNCACHED)
  1490. cam_mem_util_unmap_cpu_va(buf, kvaddr);
  1491. kmap_fail:
  1492. dma_buf_put(buf);
  1493. ion_fail:
  1494. return rc;
  1495. }
  1496. EXPORT_SYMBOL(cam_mem_mgr_reserve_memory_region);
  1497. int cam_mem_mgr_free_memory_region(struct cam_mem_mgr_memory_desc *inp)
  1498. {
  1499. int32_t idx;
  1500. int rc;
  1501. int32_t smmu_hdl;
  1502. if (!atomic_read(&cam_mem_mgr_state)) {
  1503. CAM_ERR(CAM_MEM, "failed. mem_mgr not initialized");
  1504. return -EINVAL;
  1505. }
  1506. if (!inp) {
  1507. CAM_ERR(CAM_MEM, "Invalid argument");
  1508. return -EINVAL;
  1509. }
  1510. if ((inp->region != CAM_SMMU_REGION_SECHEAP) &&
  1511. (inp->region != CAM_SMMU_REGION_FWUNCACHED)) {
  1512. CAM_ERR(CAM_MEM, "Only secondary heap supported");
  1513. return -EINVAL;
  1514. }
  1515. idx = CAM_MEM_MGR_GET_HDL_IDX(inp->mem_handle);
  1516. if (idx >= CAM_MEM_BUFQ_MAX || idx <= 0) {
  1517. CAM_ERR(CAM_MEM, "Incorrect index extracted from mem handle");
  1518. return -EINVAL;
  1519. }
  1520. if (!tbl.bufq[idx].active) {
  1521. if (tbl.bufq[idx].vaddr == 0) {
  1522. CAM_ERR(CAM_MEM, "buffer is released already");
  1523. return 0;
  1524. }
  1525. CAM_ERR(CAM_MEM, "Released buffer state should be active");
  1526. return -EINVAL;
  1527. }
  1528. if (tbl.bufq[idx].buf_handle != inp->mem_handle) {
  1529. CAM_ERR(CAM_MEM,
  1530. "Released buf handle not matching within table");
  1531. return -EINVAL;
  1532. }
  1533. if (tbl.bufq[idx].num_hdl != 1) {
  1534. CAM_ERR(CAM_MEM,
  1535. "Sec heap region should have only one smmu hdl");
  1536. return -ENODEV;
  1537. }
  1538. memcpy(&smmu_hdl, tbl.bufq[idx].hdls,
  1539. sizeof(int32_t));
  1540. if (inp->smmu_hdl != smmu_hdl) {
  1541. CAM_ERR(CAM_MEM,
  1542. "Passed SMMU handle doesn't match with internal hdl");
  1543. return -ENODEV;
  1544. }
  1545. rc = cam_smmu_release_buf_region(inp->region, inp->smmu_hdl);
  1546. if (rc) {
  1547. CAM_ERR(CAM_MEM,
  1548. "Sec heap region release failed");
  1549. return -ENODEV;
  1550. }
  1551. CAM_DBG(CAM_MEM, "Releasing hdl = %X", inp->mem_handle);
  1552. rc = cam_mem_util_unmap(idx, CAM_SMMU_MAPPING_KERNEL);
  1553. if (rc)
  1554. CAM_ERR(CAM_MEM, "unmapping secondary heap failed");
  1555. return rc;
  1556. }
  1557. EXPORT_SYMBOL(cam_mem_mgr_free_memory_region);
  1558. #ifdef CONFIG_CAM_PRESIL
  1559. struct dma_buf *cam_mem_mgr_get_dma_buf(int fd)
  1560. {
  1561. struct dma_buf *dmabuf = NULL;
  1562. dmabuf = dma_buf_get(fd);
  1563. if (IS_ERR_OR_NULL((void *)(dmabuf))) {
  1564. CAM_ERR(CAM_MEM, "Failed to import dma_buf for fd");
  1565. return NULL;
  1566. }
  1567. CAM_INFO(CAM_PRESIL, "Received DMA Buf* %pK", dmabuf);
  1568. return dmabuf;
  1569. }
  1570. int cam_presil_put_dmabuf_from_fd(uint64_t input_dmabuf)
  1571. {
  1572. struct dma_buf *dmabuf = (struct dma_buf *)(uint64_t)input_dmabuf;
  1573. int idx = 0;
  1574. CAM_INFO(CAM_PRESIL, "Received dma_buf :%pK", dmabuf);
  1575. if (!dmabuf) {
  1576. CAM_ERR(CAM_PRESIL, "NULL to import dma_buf fd");
  1577. return -EINVAL;
  1578. }
  1579. for (idx = 0; idx < CAM_MEM_BUFQ_MAX; idx++) {
  1580. if ((tbl.bufq[idx].dma_buf != NULL) && (tbl.bufq[idx].dma_buf == dmabuf)) {
  1581. if (tbl.bufq[idx].presil_params.refcount)
  1582. tbl.bufq[idx].presil_params.refcount--;
  1583. else
  1584. CAM_ERR(CAM_PRESIL, "Unbalanced dmabuf put: %pK", dmabuf);
  1585. if (!tbl.bufq[idx].presil_params.refcount) {
  1586. dma_buf_put(dmabuf);
  1587. cam_mem_mgr_reset_presil_params(idx);
  1588. CAM_DBG(CAM_PRESIL, "Done dma_buf_put for %pK", dmabuf);
  1589. }
  1590. }
  1591. }
  1592. return 0;
  1593. }
  1594. EXPORT_SYMBOL(cam_presil_put_dmabuf_from_fd);
  1595. int cam_presil_get_fd_from_dmabuf(uint64_t input_dmabuf)
  1596. {
  1597. int fd_for_dmabuf = -1;
  1598. struct dma_buf *dmabuf = (struct dma_buf *)(uint64_t)input_dmabuf;
  1599. int idx = 0;
  1600. CAM_DBG(CAM_PRESIL, "Received dma_buf :%pK", dmabuf);
  1601. if (!dmabuf) {
  1602. CAM_ERR(CAM_PRESIL, "NULL to import dma_buf fd");
  1603. return -EINVAL;
  1604. }
  1605. for (idx = 0; idx < CAM_MEM_BUFQ_MAX; idx++) {
  1606. if ((tbl.bufq[idx].dma_buf != NULL) && (tbl.bufq[idx].dma_buf == dmabuf)) {
  1607. CAM_DBG(CAM_PRESIL,
  1608. "Found entry for request from Presil UMD Daemon at %d, dmabuf %pK fd_for_umd_daemon %d refcount: %d",
  1609. idx, tbl.bufq[idx].dma_buf,
  1610. tbl.bufq[idx].presil_params.fd_for_umd_daemon,
  1611. tbl.bufq[idx].presil_params.refcount);
  1612. if (tbl.bufq[idx].presil_params.fd_for_umd_daemon < 0) {
  1613. fd_for_dmabuf = dma_buf_fd(dmabuf, O_CLOEXEC);
  1614. if (fd_for_dmabuf < 0) {
  1615. CAM_ERR(CAM_PRESIL, "get fd fail, fd_for_dmabuf=%d",
  1616. fd_for_dmabuf);
  1617. return -EINVAL;
  1618. }
  1619. tbl.bufq[idx].presil_params.fd_for_umd_daemon = fd_for_dmabuf;
  1620. CAM_INFO(CAM_PRESIL,
  1621. "Received generated idx %d fd_for_dmabuf Buf* %lld", idx,
  1622. fd_for_dmabuf);
  1623. } else {
  1624. fd_for_dmabuf = tbl.bufq[idx].presil_params.fd_for_umd_daemon;
  1625. CAM_INFO(CAM_PRESIL,
  1626. "Received existing at idx %d fd_for_dmabuf Buf* %lld", idx,
  1627. fd_for_dmabuf);
  1628. }
  1629. tbl.bufq[idx].presil_params.refcount++;
  1630. } else {
  1631. CAM_DBG(CAM_MEM,
  1632. "Not found dmabuf at idx=%d, dma_buf %pK handle 0x%0x active %d ",
  1633. idx, tbl.bufq[idx].dma_buf, tbl.bufq[idx].buf_handle,
  1634. tbl.bufq[idx].active);
  1635. }
  1636. }
  1637. return (int)fd_for_dmabuf;
  1638. }
  1639. EXPORT_SYMBOL(cam_presil_get_fd_from_dmabuf);
  1640. int cam_mem_mgr_send_buffer_to_presil(int32_t iommu_hdl, int32_t buf_handle)
  1641. {
  1642. int rc = 0;
  1643. /* Sending Presil IO Buf to PC side ( as iova start address indicates) */
  1644. uint64_t io_buf_addr;
  1645. size_t io_buf_size;
  1646. int i, j, fd = -1, idx = 0;
  1647. uint8_t *iova_ptr = NULL;
  1648. uint64_t dmabuf = 0;
  1649. bool is_mapped_in_cb = false;
  1650. CAM_DBG(CAM_PRESIL, "buf handle 0x%0x", buf_handle);
  1651. idx = CAM_MEM_MGR_GET_HDL_IDX(buf_handle);
  1652. for (i = 0; i < tbl.bufq[idx].num_hdl; i++) {
  1653. if (tbl.bufq[idx].hdls[i] == iommu_hdl)
  1654. is_mapped_in_cb = true;
  1655. }
  1656. if (!is_mapped_in_cb) {
  1657. for (j = 0; j < CAM_MEM_BUFQ_MAX; j++) {
  1658. if (tbl.bufq[j].i_ino == tbl.bufq[idx].i_ino) {
  1659. for (i = 0; i < tbl.bufq[j].num_hdl; i++) {
  1660. if (tbl.bufq[j].hdls[i] == iommu_hdl)
  1661. is_mapped_in_cb = true;
  1662. }
  1663. }
  1664. }
  1665. if (!is_mapped_in_cb) {
  1666. CAM_DBG(CAM_PRESIL,
  1667. "Still Could not find idx=%d, FD %d buf_handle 0x%0x",
  1668. idx, GET_FD_FROM_HANDLE(buf_handle), buf_handle);
  1669. /*
  1670. * Okay to return 0, since this function also gets called for buffers that
  1671. * are shared only between umd/kmd, these may not be mapped with smmu
  1672. */
  1673. return 0;
  1674. }
  1675. }
  1676. if ((tbl.bufq[idx].buf_handle != 0) && (tbl.bufq[idx].active) &&
  1677. (tbl.bufq[idx].buf_handle == buf_handle)) {
  1678. CAM_DBG(CAM_PRESIL,
  1679. "Found dmabuf in bufq idx %d, FD %d handle 0x%0x dmabuf %pK",
  1680. idx, tbl.bufq[idx].fd, tbl.bufq[idx].buf_handle, tbl.bufq[idx].dma_buf);
  1681. dmabuf = (uint64_t)tbl.bufq[idx].dma_buf;
  1682. fd = tbl.bufq[idx].fd;
  1683. } else {
  1684. CAM_ERR(CAM_PRESIL,
  1685. "Could not find dmabuf Invalid Mem idx=%d, FD %d handle 0x%0x active %d",
  1686. idx, tbl.bufq[idx].fd, tbl.bufq[idx].buf_handle, tbl.bufq[idx].active);
  1687. return -EINVAL;
  1688. }
  1689. rc = cam_mem_get_io_buf(buf_handle, iommu_hdl, &io_buf_addr, &io_buf_size, NULL);
  1690. if (rc || NULL == (void *)io_buf_addr) {
  1691. CAM_DBG(CAM_PRESIL, "Invalid ioaddr : 0x%x, fd = %d, dmabuf = %pK",
  1692. io_buf_addr, fd, dmabuf);
  1693. return -EINVAL;
  1694. }
  1695. iova_ptr = (uint8_t *)io_buf_addr;
  1696. CAM_INFO(CAM_PRESIL, "Sending buffer with ioaddr : 0x%x, fd = %d, dmabuf = %pK",
  1697. io_buf_addr, fd, dmabuf);
  1698. rc = cam_presil_send_buffer(dmabuf, 0, 0, (uint32_t)io_buf_size, (uint64_t)iova_ptr);
  1699. return rc;
  1700. }
  1701. int cam_mem_mgr_send_all_buffers_to_presil(int32_t iommu_hdl)
  1702. {
  1703. int idx = 0;
  1704. int rc = 0;
  1705. int32_t fd_already_sent[128];
  1706. int fd_already_sent_count = 0;
  1707. int fd_already_index = 0;
  1708. int fd_already_sent_found = 0;
  1709. memset(&fd_already_sent, 0x0, sizeof(fd_already_sent));
  1710. for (idx = 0; idx < CAM_MEM_BUFQ_MAX; idx++) {
  1711. if ((tbl.bufq[idx].buf_handle != 0) && (tbl.bufq[idx].active)) {
  1712. CAM_DBG(CAM_PRESIL, "Sending %d, FD %d handle 0x%0x", idx, tbl.bufq[idx].fd,
  1713. tbl.bufq[idx].buf_handle);
  1714. fd_already_sent_found = 0;
  1715. for (fd_already_index = 0; fd_already_index < fd_already_sent_count;
  1716. fd_already_index++) {
  1717. if (fd_already_sent[fd_already_index] == tbl.bufq[idx].fd) {
  1718. fd_already_sent_found = 1;
  1719. CAM_DBG(CAM_PRESIL,
  1720. "fd_already_sent %d, FD %d handle 0x%0x flags=0x%0x",
  1721. idx, tbl.bufq[idx].fd, tbl.bufq[idx].buf_handle,
  1722. tbl.bufq[idx].flags);
  1723. }
  1724. }
  1725. if (fd_already_sent_found)
  1726. continue;
  1727. CAM_DBG(CAM_PRESIL, "Sending %d, FD %d handle 0x%0x flags=0x%0x", idx,
  1728. tbl.bufq[idx].fd, tbl.bufq[idx].buf_handle, tbl.bufq[idx].flags);
  1729. rc = cam_mem_mgr_send_buffer_to_presil(iommu_hdl, tbl.bufq[idx].buf_handle);
  1730. fd_already_sent[fd_already_sent_count++] = tbl.bufq[idx].fd;
  1731. } else {
  1732. CAM_DBG(CAM_PRESIL, "Invalid Mem idx=%d, FD %d handle 0x%0x active %d",
  1733. idx, tbl.bufq[idx].fd, tbl.bufq[idx].buf_handle,
  1734. tbl.bufq[idx].active);
  1735. }
  1736. }
  1737. return rc;
  1738. }
  1739. EXPORT_SYMBOL(cam_mem_mgr_send_all_buffers_to_presil);
  1740. int cam_mem_mgr_retrieve_buffer_from_presil(int32_t buf_handle, uint32_t buf_size,
  1741. uint32_t offset, int32_t iommu_hdl)
  1742. {
  1743. int rc = 0;
  1744. /* Receive output buffer from Presil IO Buf to PC side (as iova start address indicates) */
  1745. uint64_t io_buf_addr;
  1746. size_t io_buf_size;
  1747. uint64_t dmabuf = 0;
  1748. int fd = 0;
  1749. uint8_t *iova_ptr = NULL;
  1750. int idx = 0;
  1751. CAM_DBG(CAM_PRESIL, "buf handle 0x%0x ", buf_handle);
  1752. rc = cam_mem_get_io_buf(buf_handle, iommu_hdl, &io_buf_addr, &io_buf_size, NULL);
  1753. if (rc) {
  1754. CAM_ERR(CAM_PRESIL, "Unable to get IOVA for buffer buf_hdl: 0x%0x iommu_hdl: 0x%0x",
  1755. buf_handle, iommu_hdl);
  1756. return -EINVAL;
  1757. }
  1758. iova_ptr = (uint8_t *)io_buf_addr;
  1759. iova_ptr += offset; // correct target address to start writing buffer to.
  1760. if (!buf_size) {
  1761. buf_size = io_buf_size;
  1762. CAM_DBG(CAM_PRESIL, "Updated buf_size from Zero to 0x%0x", buf_size);
  1763. }
  1764. fd = GET_FD_FROM_HANDLE(buf_handle);
  1765. idx = CAM_MEM_MGR_GET_HDL_IDX(buf_handle);
  1766. if ((tbl.bufq[idx].buf_handle != 0) && (tbl.bufq[idx].active) &&
  1767. (tbl.bufq[idx].buf_handle == buf_handle)) {
  1768. CAM_DBG(CAM_PRESIL, "Found dmabuf in bufq idx %d, FD %d handle 0x%0x dmabuf %pK",
  1769. idx, tbl.bufq[idx].fd, tbl.bufq[idx].buf_handle, tbl.bufq[idx].dma_buf);
  1770. dmabuf = (uint64_t)tbl.bufq[idx].dma_buf;
  1771. } else {
  1772. CAM_ERR(CAM_PRESIL,
  1773. "Could not find dmabuf Invalid Mem idx=%d, FD %d handle 0x%0x active %d ",
  1774. idx, tbl.bufq[idx].fd, tbl.bufq[idx].buf_handle, tbl.bufq[idx].active);
  1775. }
  1776. CAM_DBG(CAM_PRESIL,
  1777. "Retrieving buffer with ioaddr : 0x%x, offset = %d, size = %d, fd = %d, dmabuf = %pK",
  1778. io_buf_addr, offset, buf_size, fd, dmabuf);
  1779. rc = cam_presil_retrieve_buffer(dmabuf, 0, 0, (uint32_t)buf_size, (uint64_t)io_buf_addr);
  1780. CAM_INFO(CAM_PRESIL,
  1781. "Retrieved buffer with ioaddr : 0x%x, offset = %d, size = %d, fd = %d, dmabuf = %pK",
  1782. io_buf_addr, 0, buf_size, fd, dmabuf);
  1783. return rc;
  1784. }
  1785. #else /* ifdef CONFIG_CAM_PRESIL */
  1786. struct dma_buf * cam_mem_mgr_get_dma_buf(int fd)
  1787. {
  1788. return NULL;
  1789. }
  1790. int cam_mem_mgr_send_all_buffers_to_presil(int32_t iommu_hdl)
  1791. {
  1792. return 0;
  1793. }
  1794. int cam_mem_mgr_send_buffer_to_presil(int32_t iommu_hdl, int32_t buf_handle)
  1795. {
  1796. return 0;
  1797. }
  1798. int cam_mem_mgr_retrieve_buffer_from_presil(int32_t buf_handle,
  1799. uint32_t buf_size,
  1800. uint32_t offset,
  1801. int32_t iommu_hdl)
  1802. {
  1803. return 0;
  1804. }
  1805. #endif /* ifdef CONFIG_CAM_PRESIL */