cam_mem_mgr.c 60 KB

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