cam_mem_mgr.c 64 KB

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