cam_mem_mgr.c 57 KB

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