cam_mem_mgr.c 73 KB

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