cam_mem_mgr.c 70 KB

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