cam_mem_mgr.c 61 KB

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