cam_mem_mgr.c 53 KB

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