cam_mem_mgr.c 53 KB

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