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