cam_mem_mgr.c 53 KB

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