cam_mem_mgr.c 53 KB

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