cam_mem_mgr.c 67 KB

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