cam_mem_mgr.c 72 KB

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