cam_mem_mgr.c 57 KB

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