cam_mem_mgr.c 53 KB

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