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