msm_cvp_buf.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/pid.h>
  6. #include <linux/fdtable.h>
  7. #include <linux/rcupdate.h>
  8. #include <linux/fs.h>
  9. #include <linux/dma-buf.h>
  10. #include <linux/sched/task.h>
  11. #include "msm_cvp_common.h"
  12. #include "cvp_hfi_api.h"
  13. #include "msm_cvp_debug.h"
  14. #include "msm_cvp_core.h"
  15. #include "msm_cvp_dsp.h"
  16. #define CLEAR_USE_BITMAP(idx, inst) \
  17. do { \
  18. clear_bit(idx, &inst->dma_cache.usage_bitmap); \
  19. dprintk(CVP_MEM, "clear %x bit %d dma_cache bitmap 0x%llx\n", \
  20. hash32_ptr(inst->session), smem->bitmap_index, \
  21. inst->dma_cache.usage_bitmap); \
  22. } while (0)
  23. #define SET_USE_BITMAP(idx, inst) \
  24. do { \
  25. set_bit(idx, &inst->dma_cache.usage_bitmap); \
  26. dprintk(CVP_MEM, "Set %x bit %d dma_cache bitmap 0x%llx\n", \
  27. hash32_ptr(inst->session), idx, \
  28. inst->dma_cache.usage_bitmap); \
  29. } while (0)
  30. void print_smem(u32 tag, const char *str, struct msm_cvp_inst *inst,
  31. struct msm_cvp_smem *smem)
  32. {
  33. if (!(tag & msm_cvp_debug) || !inst || !smem)
  34. return;
  35. if (smem->dma_buf) {
  36. dprintk(tag,
  37. "%s: %x : %s size %d flags %#x iova %#x idx %d ref %d",
  38. str, hash32_ptr(inst->session), smem->dma_buf->name,
  39. smem->size, smem->flags, smem->device_addr,
  40. smem->bitmap_index, smem->refcount);
  41. }
  42. }
  43. static void print_internal_buffer(u32 tag, const char *str,
  44. struct msm_cvp_inst *inst, struct cvp_internal_buf *cbuf)
  45. {
  46. if (!(tag & msm_cvp_debug) || !inst || !cbuf)
  47. return;
  48. if (cbuf->smem->dma_buf) {
  49. dprintk(tag,
  50. "%s: %x : fd %d off %d %s size %d iova %#x",
  51. str, hash32_ptr(inst->session), cbuf->fd,
  52. cbuf->offset, cbuf->smem->dma_buf->name, cbuf->size,
  53. cbuf->smem->device_addr);
  54. } else {
  55. dprintk(tag,
  56. "%s: %x : idx %2d fd %d off %d size %d iova %#x",
  57. str, hash32_ptr(inst->session), cbuf->fd,
  58. cbuf->offset, cbuf->size, cbuf->smem->device_addr);
  59. }
  60. }
  61. void print_cvp_buffer(u32 tag, const char *str, struct msm_cvp_inst *inst,
  62. struct cvp_internal_buf *cbuf)
  63. {
  64. dprintk(tag, "%s addr: %x size %u\n", str,
  65. cbuf->smem->device_addr, cbuf->size);
  66. }
  67. void print_client_buffer(u32 tag, const char *str,
  68. struct msm_cvp_inst *inst, struct eva_kmd_buffer *cbuf)
  69. {
  70. if (!(tag & msm_cvp_debug) || !inst || !cbuf)
  71. return;
  72. dprintk(tag,
  73. "%s: %x : idx %2d fd %d off %d size %d type %d flags 0x%x\n",
  74. str, hash32_ptr(inst->session), cbuf->index, cbuf->fd,
  75. cbuf->offset, cbuf->size, cbuf->type, cbuf->flags);
  76. }
  77. int msm_cvp_map_buf_dsp(struct msm_cvp_inst *inst, struct eva_kmd_buffer *buf)
  78. {
  79. int rc = 0;
  80. bool found = false;
  81. struct cvp_internal_buf *cbuf;
  82. struct msm_cvp_smem *smem = NULL;
  83. struct cvp_hal_session *session;
  84. struct dma_buf *dma_buf = NULL;
  85. if (!inst || !inst->core || !buf) {
  86. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  87. return -EINVAL;
  88. }
  89. if (buf->fd < 0) {
  90. dprintk(CVP_ERR, "%s: Invalid fd = %d", __func__, buf->fd);
  91. return 0;
  92. }
  93. if (buf->offset) {
  94. dprintk(CVP_ERR,
  95. "%s: offset is deprecated, set to 0.\n",
  96. __func__);
  97. return -EINVAL;
  98. }
  99. session = (struct cvp_hal_session *)inst->session;
  100. mutex_lock(&inst->cvpdspbufs.lock);
  101. list_for_each_entry(cbuf, &inst->cvpdspbufs.list, list) {
  102. if (cbuf->fd == buf->fd) {
  103. if (cbuf->size != buf->size) {
  104. dprintk(CVP_ERR, "%s: buf size mismatch\n",
  105. __func__);
  106. mutex_unlock(&inst->cvpdspbufs.lock);
  107. return -EINVAL;
  108. }
  109. found = true;
  110. break;
  111. }
  112. }
  113. mutex_unlock(&inst->cvpdspbufs.lock);
  114. if (found) {
  115. print_internal_buffer(CVP_ERR, "duplicate", inst, cbuf);
  116. return -EINVAL;
  117. }
  118. dma_buf = msm_cvp_smem_get_dma_buf(buf->fd);
  119. if (!dma_buf) {
  120. dprintk(CVP_ERR, "%s: Invalid fd = %d", __func__, buf->fd);
  121. return 0;
  122. }
  123. cbuf = kmem_cache_zalloc(cvp_driver->buf_cache, GFP_KERNEL);
  124. if (!cbuf)
  125. return -ENOMEM;
  126. smem = kmem_cache_zalloc(cvp_driver->smem_cache, GFP_KERNEL);
  127. if (!smem) {
  128. kmem_cache_free(cvp_driver->buf_cache, cbuf);
  129. return -ENOMEM;
  130. }
  131. smem->dma_buf = dma_buf;
  132. smem->bitmap_index = MAX_DMABUF_NUMS;
  133. dprintk(CVP_MEM, "%s: dma_buf = %llx\n", __func__, dma_buf);
  134. rc = msm_cvp_map_smem(inst, smem, "map dsp");
  135. if (rc) {
  136. print_client_buffer(CVP_ERR, "map failed", inst, buf);
  137. goto exit;
  138. }
  139. if (buf->index) {
  140. rc = cvp_dsp_register_buffer(hash32_ptr(session), buf->fd,
  141. smem->dma_buf->size, buf->size, buf->offset,
  142. buf->index, (uint32_t)smem->device_addr);
  143. if (rc) {
  144. dprintk(CVP_ERR,
  145. "%s: failed dsp registration for fd=%d rc=%d",
  146. __func__, buf->fd, rc);
  147. goto exit;
  148. }
  149. } else {
  150. dprintk(CVP_ERR, "%s: buf index is 0 fd=%d", __func__, buf->fd);
  151. rc = -EINVAL;
  152. goto exit;
  153. }
  154. cbuf->smem = smem;
  155. cbuf->fd = buf->fd;
  156. cbuf->size = buf->size;
  157. cbuf->offset = buf->offset;
  158. cbuf->ownership = CLIENT;
  159. cbuf->index = buf->index;
  160. mutex_lock(&inst->cvpdspbufs.lock);
  161. list_add_tail(&cbuf->list, &inst->cvpdspbufs.list);
  162. mutex_unlock(&inst->cvpdspbufs.lock);
  163. return rc;
  164. exit:
  165. if (smem->device_addr) {
  166. msm_cvp_unmap_smem(inst, smem, "unmap dsp");
  167. msm_cvp_smem_put_dma_buf(smem->dma_buf);
  168. }
  169. kmem_cache_free(cvp_driver->buf_cache, cbuf);
  170. cbuf = NULL;
  171. kmem_cache_free(cvp_driver->smem_cache, smem);
  172. smem = NULL;
  173. return rc;
  174. }
  175. int msm_cvp_unmap_buf_dsp(struct msm_cvp_inst *inst, struct eva_kmd_buffer *buf)
  176. {
  177. int rc = 0;
  178. bool found;
  179. struct cvp_internal_buf *cbuf;
  180. struct cvp_hal_session *session;
  181. if (!inst || !inst->core || !buf) {
  182. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  183. return -EINVAL;
  184. }
  185. session = (struct cvp_hal_session *)inst->session;
  186. if (!session) {
  187. dprintk(CVP_ERR, "%s: invalid session\n", __func__);
  188. return -EINVAL;
  189. }
  190. mutex_lock(&inst->cvpdspbufs.lock);
  191. found = false;
  192. list_for_each_entry(cbuf, &inst->cvpdspbufs.list, list) {
  193. if (cbuf->fd == buf->fd) {
  194. found = true;
  195. break;
  196. }
  197. }
  198. mutex_unlock(&inst->cvpdspbufs.lock);
  199. if (!found) {
  200. print_client_buffer(CVP_ERR, "invalid", inst, buf);
  201. return -EINVAL;
  202. }
  203. if (buf->index) {
  204. rc = cvp_dsp_deregister_buffer(hash32_ptr(session), buf->fd,
  205. cbuf->smem->dma_buf->size, buf->size, buf->offset,
  206. buf->index, (uint32_t)cbuf->smem->device_addr);
  207. if (rc) {
  208. dprintk(CVP_ERR,
  209. "%s: failed dsp deregistration fd=%d rc=%d",
  210. __func__, buf->fd, rc);
  211. return rc;
  212. }
  213. }
  214. if (cbuf->smem->device_addr) {
  215. msm_cvp_unmap_smem(inst, cbuf->smem, "unmap dsp");
  216. msm_cvp_smem_put_dma_buf(cbuf->smem->dma_buf);
  217. }
  218. mutex_lock(&inst->cvpdspbufs.lock);
  219. list_del(&cbuf->list);
  220. mutex_unlock(&inst->cvpdspbufs.lock);
  221. kmem_cache_free(cvp_driver->smem_cache, cbuf->smem);
  222. kmem_cache_free(cvp_driver->buf_cache, cbuf);
  223. return rc;
  224. }
  225. static struct file *msm_cvp_fget(unsigned int fd, struct task_struct *task,
  226. fmode_t mask, unsigned int refs)
  227. {
  228. struct files_struct *files = task->files;
  229. struct file *file;
  230. rcu_read_lock();
  231. loop:
  232. file = fcheck_files(files, fd);
  233. if (file) {
  234. /* File object ref couldn't be taken.
  235. * dup2() atomicity guarantee is the reason
  236. * we loop to catch the new file (or NULL pointer)
  237. */
  238. if (file->f_mode & mask)
  239. file = NULL;
  240. else if (!get_file_rcu_many(file, refs))
  241. goto loop;
  242. }
  243. rcu_read_unlock();
  244. return file;
  245. }
  246. static struct dma_buf *cvp_dma_buf_get(struct file *file, int fd,
  247. struct task_struct *task)
  248. {
  249. if (file->f_op != gfa_cv.dmabuf_f_op) {
  250. dprintk(CVP_WARN, "fd doesn't refer to dma_buf\n");
  251. return ERR_PTR(-EINVAL);
  252. }
  253. return file->private_data;
  254. }
  255. int msm_cvp_map_buf_dsp_new(struct msm_cvp_inst *inst,
  256. struct eva_kmd_buffer *buf,
  257. int32_t pid, uint32_t *iova)
  258. {
  259. int rc = 0;
  260. bool found = false;
  261. struct cvp_internal_buf *cbuf;
  262. struct msm_cvp_smem *smem = NULL;
  263. struct cvp_hal_session *session;
  264. struct dma_buf *dma_buf = NULL;
  265. struct pid *pid_s = NULL;
  266. struct task_struct *task = NULL;
  267. struct file *file;
  268. if (!inst || !inst->core || !buf) {
  269. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  270. return -EINVAL;
  271. }
  272. if (buf->fd < 0) {
  273. dprintk(CVP_ERR, "%s: Invalid fd = %d", __func__, buf->fd);
  274. return 0;
  275. }
  276. if (buf->offset) {
  277. dprintk(CVP_ERR,
  278. "%s: offset is deprecated, set to 0.\n",
  279. __func__);
  280. return -EINVAL;
  281. }
  282. session = (struct cvp_hal_session *)inst->session;
  283. mutex_lock(&inst->cvpdspbufs.lock);
  284. list_for_each_entry(cbuf, &inst->cvpdspbufs.list, list) {
  285. if (cbuf->fd == buf->fd) {
  286. if (cbuf->size != buf->size) {
  287. dprintk(CVP_ERR, "%s: buf size mismatch\n",
  288. __func__);
  289. mutex_unlock(&inst->cvpdspbufs.lock);
  290. return -EINVAL;
  291. }
  292. found = true;
  293. break;
  294. }
  295. }
  296. mutex_unlock(&inst->cvpdspbufs.lock);
  297. if (found) {
  298. print_internal_buffer(CVP_ERR, "duplicate", inst, cbuf);
  299. return -EINVAL;
  300. }
  301. // Temp workaround : commented for passing compilation due to missing dependency in AU whitelist
  302. // pid_s = find_get_pid(pid);
  303. if (pid_s == NULL) {
  304. dprintk(CVP_WARN, "%s incorrect pid\n", __func__);
  305. return -EINVAL;
  306. }
  307. dprintk(CVP_WARN, "%s get pid_s 0x%x from pidA 0x%x\n", __func__, pid_s, pid);
  308. /* task = get_pid_task(pid, PIDTYPE_PID); */
  309. // Temp workaround : commented for passing compilation due to missing dependency in AU whitelist
  310. // task = get_pid_task(pid_s, PIDTYPE_TGID);
  311. if (!task)
  312. dprintk(CVP_WARN, "%s task doesn't exist\n", __func__);
  313. file = msm_cvp_fget(buf->fd, task, FMODE_PATH, 1);
  314. if (file == NULL) {
  315. dprintk(CVP_WARN, "%s fail to get file from fd\n", __func__);
  316. put_task_struct(task);
  317. return -EINVAL;
  318. }
  319. //entry->file = file;
  320. dma_buf = cvp_dma_buf_get(
  321. file,
  322. buf->fd,
  323. task);
  324. if (dma_buf == ERR_PTR(-EINVAL)) {
  325. dprintk(CVP_ERR, "%s: Invalid fd = %d", __func__, buf->fd);
  326. fput(file);
  327. put_task_struct(task);
  328. return -EINVAL;
  329. }
  330. dprintk(CVP_WARN, "dma_buf from internal %llu\n", dma_buf);
  331. /* to unmap dsp buf, below sequence is required
  332. * fput(file);
  333. * dma_buf_put(dma_buf);
  334. * put_task_struct(task);
  335. */
  336. if (!dma_buf) {
  337. dprintk(CVP_ERR, "%s: Invalid fd = %d", __func__, buf->fd);
  338. return 0;
  339. }
  340. cbuf = kmem_cache_zalloc(cvp_driver->buf_cache, GFP_KERNEL);
  341. if (!cbuf)
  342. return -ENOMEM;
  343. smem = kmem_cache_zalloc(cvp_driver->smem_cache, GFP_KERNEL);
  344. if (!smem) {
  345. kmem_cache_free(cvp_driver->buf_cache, cbuf);
  346. return -ENOMEM;
  347. }
  348. smem->dma_buf = dma_buf;
  349. smem->bitmap_index = MAX_DMABUF_NUMS;
  350. dprintk(CVP_DSP, "%s: dma_buf = %llx\n", __func__, dma_buf);
  351. rc = msm_cvp_map_smem(inst, smem, "map dsp");
  352. if (rc) {
  353. print_client_buffer(CVP_ERR, "map failed", inst, buf);
  354. goto exit;
  355. }
  356. cbuf->smem = smem;
  357. cbuf->fd = buf->fd;
  358. cbuf->size = buf->size;
  359. cbuf->offset = buf->offset;
  360. cbuf->ownership = CLIENT;
  361. cbuf->index = buf->index;
  362. *iova = (uint32_t)smem->device_addr;
  363. dprintk(CVP_DSP, "%s: buf->fd %d, device_addr = %llx\n",
  364. __func__, buf->fd, (uint32_t)smem->device_addr);
  365. mutex_lock(&inst->cvpdspbufs.lock);
  366. list_add_tail(&cbuf->list, &inst->cvpdspbufs.list);
  367. mutex_unlock(&inst->cvpdspbufs.lock);
  368. return rc;
  369. exit:
  370. if (smem->device_addr) {
  371. msm_cvp_unmap_smem(inst, smem, "unmap dsp");
  372. msm_cvp_smem_put_dma_buf(smem->dma_buf);
  373. }
  374. kmem_cache_free(cvp_driver->buf_cache, cbuf);
  375. cbuf = NULL;
  376. kmem_cache_free(cvp_driver->smem_cache, smem);
  377. smem = NULL;
  378. return rc;
  379. }
  380. int msm_cvp_unmap_buf_dsp_new(struct msm_cvp_inst *inst,
  381. struct eva_kmd_buffer *buf)
  382. {
  383. int rc = 0;
  384. bool found;
  385. struct cvp_internal_buf *cbuf;
  386. struct cvp_hal_session *session;
  387. if (!inst || !inst->core || !buf) {
  388. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  389. return -EINVAL;
  390. }
  391. session = (struct cvp_hal_session *)inst->session;
  392. if (!session) {
  393. dprintk(CVP_ERR, "%s: invalid session\n", __func__);
  394. return -EINVAL;
  395. }
  396. mutex_lock(&inst->cvpdspbufs.lock);
  397. found = false;
  398. list_for_each_entry(cbuf, &inst->cvpdspbufs.list, list) {
  399. if (cbuf->fd == buf->fd) {
  400. found = true;
  401. break;
  402. }
  403. }
  404. mutex_unlock(&inst->cvpdspbufs.lock);
  405. if (!found) {
  406. print_client_buffer(CVP_ERR, "invalid", inst, buf);
  407. return -EINVAL;
  408. }
  409. if (cbuf->smem->device_addr) {
  410. msm_cvp_unmap_smem(inst, cbuf->smem, "unmap dsp");
  411. msm_cvp_smem_put_dma_buf(cbuf->smem->dma_buf);
  412. }
  413. mutex_lock(&inst->cvpdspbufs.lock);
  414. list_del(&cbuf->list);
  415. mutex_unlock(&inst->cvpdspbufs.lock);
  416. kmem_cache_free(cvp_driver->smem_cache, cbuf->smem);
  417. kmem_cache_free(cvp_driver->buf_cache, cbuf);
  418. return rc;
  419. }
  420. void msm_cvp_cache_operations(struct msm_cvp_smem *smem, u32 type,
  421. u32 offset, u32 size)
  422. {
  423. enum smem_cache_ops cache_op;
  424. if (msm_cvp_cacheop_disabled)
  425. return;
  426. if (!smem) {
  427. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  428. return;
  429. }
  430. switch (type) {
  431. case EVA_KMD_BUFTYPE_INPUT:
  432. cache_op = SMEM_CACHE_CLEAN;
  433. break;
  434. case EVA_KMD_BUFTYPE_OUTPUT:
  435. cache_op = SMEM_CACHE_INVALIDATE;
  436. break;
  437. default:
  438. cache_op = SMEM_CACHE_CLEAN_INVALIDATE;
  439. }
  440. dprintk(CVP_MEM,
  441. "%s: cache operation enabled for dma_buf: %llx, cache_op: %d, offset: %d, size: %d\n",
  442. __func__, smem->dma_buf, cache_op, offset, size);
  443. msm_cvp_smem_cache_operations(smem->dma_buf, cache_op, offset, size);
  444. }
  445. static struct msm_cvp_smem *msm_cvp_session_find_smem(struct msm_cvp_inst *inst,
  446. struct dma_buf *dma_buf)
  447. {
  448. struct msm_cvp_smem *smem;
  449. int i;
  450. if (inst->dma_cache.nr > MAX_DMABUF_NUMS)
  451. return NULL;
  452. mutex_lock(&inst->dma_cache.lock);
  453. for (i = 0; i < inst->dma_cache.nr; i++)
  454. if (inst->dma_cache.entries[i]->dma_buf == dma_buf) {
  455. SET_USE_BITMAP(i, inst);
  456. smem = inst->dma_cache.entries[i];
  457. smem->bitmap_index = i;
  458. atomic_inc(&smem->refcount);
  459. /*
  460. * If we find it, it means we already increased
  461. * refcount before, so we put it to avoid double
  462. * incremental.
  463. */
  464. msm_cvp_smem_put_dma_buf(smem->dma_buf);
  465. mutex_unlock(&inst->dma_cache.lock);
  466. print_smem(CVP_MEM, "found", inst, smem);
  467. return smem;
  468. }
  469. mutex_unlock(&inst->dma_cache.lock);
  470. return NULL;
  471. }
  472. static int msm_cvp_session_add_smem(struct msm_cvp_inst *inst,
  473. struct msm_cvp_smem *smem)
  474. {
  475. unsigned int i;
  476. struct msm_cvp_smem *smem2;
  477. mutex_lock(&inst->dma_cache.lock);
  478. if (inst->dma_cache.nr < MAX_DMABUF_NUMS) {
  479. inst->dma_cache.entries[inst->dma_cache.nr] = smem;
  480. SET_USE_BITMAP(inst->dma_cache.nr, inst);
  481. smem->bitmap_index = inst->dma_cache.nr;
  482. inst->dma_cache.nr++;
  483. i = smem->bitmap_index;
  484. } else {
  485. i = find_first_zero_bit(&inst->dma_cache.usage_bitmap,
  486. MAX_DMABUF_NUMS);
  487. if (i < MAX_DMABUF_NUMS) {
  488. smem2 = inst->dma_cache.entries[i];
  489. msm_cvp_unmap_smem(inst, smem2, "unmap cpu");
  490. msm_cvp_smem_put_dma_buf(smem2->dma_buf);
  491. kmem_cache_free(cvp_driver->smem_cache, smem2);
  492. inst->dma_cache.entries[i] = smem;
  493. smem->bitmap_index = i;
  494. SET_USE_BITMAP(i, inst);
  495. } else {
  496. dprintk(CVP_WARN, "%s: not enough memory\n", __func__);
  497. mutex_unlock(&inst->dma_cache.lock);
  498. return -ENOMEM;
  499. }
  500. }
  501. atomic_inc(&smem->refcount);
  502. mutex_unlock(&inst->dma_cache.lock);
  503. dprintk(CVP_MEM, "Add entry %d into cache\n", i);
  504. return 0;
  505. }
  506. static struct msm_cvp_smem *msm_cvp_session_get_smem(struct msm_cvp_inst *inst,
  507. struct cvp_buf_type *buf)
  508. {
  509. int rc = 0, found = 1;
  510. struct msm_cvp_smem *smem = NULL;
  511. struct dma_buf *dma_buf = NULL;
  512. if (buf->fd < 0) {
  513. dprintk(CVP_ERR, "%s: Invalid fd = %d", __func__, buf->fd);
  514. return NULL;
  515. }
  516. dma_buf = msm_cvp_smem_get_dma_buf(buf->fd);
  517. if (!dma_buf) {
  518. dprintk(CVP_ERR, "%s: Invalid fd = %d", __func__, buf->fd);
  519. return NULL;
  520. }
  521. smem = msm_cvp_session_find_smem(inst, dma_buf);
  522. if (!smem) {
  523. found = 0;
  524. smem = kmem_cache_zalloc(cvp_driver->smem_cache, GFP_KERNEL);
  525. if (!smem)
  526. return NULL;
  527. smem->dma_buf = dma_buf;
  528. smem->bitmap_index = MAX_DMABUF_NUMS;
  529. rc = msm_cvp_map_smem(inst, smem, "map cpu");
  530. if (rc)
  531. goto exit;
  532. rc = msm_cvp_session_add_smem(inst, smem);
  533. if (rc && rc != -ENOMEM)
  534. goto exit2;
  535. }
  536. if (buf->size > smem->size || buf->size > smem->size - buf->offset) {
  537. dprintk(CVP_ERR, "%s: invalid offset %d or size %d\n",
  538. __func__, buf->offset, buf->size);
  539. if (found) {
  540. mutex_lock(&inst->dma_cache.lock);
  541. atomic_dec(&smem->refcount);
  542. mutex_unlock(&inst->dma_cache.lock);
  543. return NULL;
  544. }
  545. goto exit2;
  546. }
  547. return smem;
  548. exit2:
  549. msm_cvp_unmap_smem(inst, smem, "unmap cpu");
  550. exit:
  551. msm_cvp_smem_put_dma_buf(dma_buf);
  552. kmem_cache_free(cvp_driver->smem_cache, smem);
  553. smem = NULL;
  554. return smem;
  555. }
  556. static u32 msm_cvp_map_user_persist_buf(struct msm_cvp_inst *inst,
  557. struct cvp_buf_type *buf)
  558. {
  559. u32 iova = 0;
  560. struct msm_cvp_smem *smem = NULL;
  561. struct cvp_internal_buf *pbuf;
  562. if (!inst) {
  563. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  564. return -EINVAL;
  565. }
  566. pbuf = kmem_cache_zalloc(cvp_driver->buf_cache, GFP_KERNEL);
  567. if (!pbuf)
  568. return 0;
  569. smem = msm_cvp_session_get_smem(inst, buf);
  570. if (!smem)
  571. goto exit;
  572. pbuf->smem = smem;
  573. pbuf->fd = buf->fd;
  574. pbuf->size = buf->size;
  575. pbuf->offset = buf->offset;
  576. pbuf->ownership = CLIENT;
  577. mutex_lock(&inst->persistbufs.lock);
  578. list_add_tail(&pbuf->list, &inst->persistbufs.list);
  579. mutex_unlock(&inst->persistbufs.lock);
  580. print_internal_buffer(CVP_MEM, "map persist", inst, pbuf);
  581. iova = smem->device_addr + buf->offset;
  582. return iova;
  583. exit:
  584. kmem_cache_free(cvp_driver->buf_cache, pbuf);
  585. return 0;
  586. }
  587. u32 msm_cvp_map_frame_buf(struct msm_cvp_inst *inst,
  588. struct cvp_buf_type *buf,
  589. struct msm_cvp_frame *frame)
  590. {
  591. u32 iova = 0;
  592. struct msm_cvp_smem *smem = NULL;
  593. u32 nr;
  594. u32 type;
  595. if (!inst || !frame) {
  596. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  597. return 0;
  598. }
  599. nr = frame->nr;
  600. if (nr == MAX_FRAME_BUFFER_NUMS) {
  601. dprintk(CVP_ERR, "%s: max frame buffer reached\n", __func__);
  602. return 0;
  603. }
  604. smem = msm_cvp_session_get_smem(inst, buf);
  605. if (!smem)
  606. return 0;
  607. frame->bufs[nr].fd = buf->fd;
  608. frame->bufs[nr].smem = smem;
  609. frame->bufs[nr].size = buf->size;
  610. frame->bufs[nr].offset = buf->offset;
  611. print_internal_buffer(CVP_MEM, "map cpu", inst, &frame->bufs[nr]);
  612. frame->nr++;
  613. type = EVA_KMD_BUFTYPE_INPUT | EVA_KMD_BUFTYPE_OUTPUT;
  614. msm_cvp_cache_operations(smem, type, buf->offset, buf->size);
  615. iova = smem->device_addr + buf->offset;
  616. return iova;
  617. }
  618. static void msm_cvp_unmap_frame_buf(struct msm_cvp_inst *inst,
  619. struct msm_cvp_frame *frame)
  620. {
  621. u32 i;
  622. u32 type;
  623. struct msm_cvp_smem *smem = NULL;
  624. struct cvp_internal_buf *buf;
  625. type = EVA_KMD_BUFTYPE_OUTPUT;
  626. for (i = 0; i < frame->nr; ++i) {
  627. buf = &frame->bufs[i];
  628. smem = buf->smem;
  629. msm_cvp_cache_operations(smem, type, buf->offset, buf->size);
  630. if (smem->bitmap_index >= MAX_DMABUF_NUMS) {
  631. /* smem not in dmamap cache */
  632. msm_cvp_unmap_smem(inst, smem, "unmap cpu");
  633. dma_heap_buffer_free(smem->dma_buf);
  634. kmem_cache_free(cvp_driver->smem_cache, smem);
  635. buf->smem = NULL;
  636. } else {
  637. mutex_lock(&inst->dma_cache.lock);
  638. if (atomic_dec_and_test(&smem->refcount)) {
  639. CLEAR_USE_BITMAP(smem->bitmap_index, inst);
  640. print_smem(CVP_MEM, "Map dereference",
  641. inst, smem);
  642. }
  643. mutex_unlock(&inst->dma_cache.lock);
  644. }
  645. }
  646. kmem_cache_free(cvp_driver->frame_cache, frame);
  647. }
  648. void msm_cvp_unmap_frame(struct msm_cvp_inst *inst, u64 ktid)
  649. {
  650. struct msm_cvp_frame *frame, *dummy1;
  651. bool found;
  652. if (!inst) {
  653. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  654. return;
  655. }
  656. ktid &= (FENCE_BIT - 1);
  657. dprintk(CVP_MEM, "%s: (%#x) unmap frame %llu\n",
  658. __func__, hash32_ptr(inst->session), ktid);
  659. found = false;
  660. mutex_lock(&inst->frames.lock);
  661. list_for_each_entry_safe(frame, dummy1, &inst->frames.list, list) {
  662. if (frame->ktid == ktid) {
  663. found = true;
  664. list_del(&frame->list);
  665. break;
  666. }
  667. }
  668. mutex_unlock(&inst->frames.lock);
  669. if (found)
  670. msm_cvp_unmap_frame_buf(inst, frame);
  671. else
  672. dprintk(CVP_WARN, "%s frame %llu not found!\n", __func__, ktid);
  673. }
  674. int msm_cvp_unmap_user_persist(struct msm_cvp_inst *inst,
  675. struct eva_kmd_hfi_packet *in_pkt,
  676. unsigned int offset, unsigned int buf_num)
  677. {
  678. struct cvp_hfi_cmd_session_hdr *cmd_hdr;
  679. struct cvp_internal_buf *pbuf, *dummy;
  680. u64 ktid;
  681. int rc = 0;
  682. struct msm_cvp_smem *smem = NULL;
  683. if (!offset || !buf_num)
  684. return rc;
  685. cmd_hdr = (struct cvp_hfi_cmd_session_hdr *)in_pkt;
  686. ktid = cmd_hdr->client_data.kdata & (FENCE_BIT - 1);
  687. mutex_lock(&inst->persistbufs.lock);
  688. list_for_each_entry_safe(pbuf, dummy, &inst->persistbufs.list, list) {
  689. if (pbuf->ktid == ktid && pbuf->ownership == CLIENT) {
  690. list_del(&pbuf->list);
  691. smem = pbuf->smem;
  692. dprintk(CVP_MEM, "unmap persist: %x %d %d %#x",
  693. hash32_ptr(inst->session), pbuf->fd,
  694. pbuf->size, smem->device_addr);
  695. if (smem->bitmap_index >= MAX_DMABUF_NUMS) {
  696. /* smem not in dmamap cache */
  697. msm_cvp_unmap_smem(inst, smem,
  698. "unmap cpu");
  699. dma_heap_buffer_free(smem->dma_buf);
  700. kmem_cache_free(
  701. cvp_driver->smem_cache,
  702. smem);
  703. pbuf->smem = NULL;
  704. } else {
  705. mutex_lock(&inst->dma_cache.lock);
  706. if (atomic_dec_and_test(&smem->refcount))
  707. CLEAR_USE_BITMAP(
  708. smem->bitmap_index,
  709. inst);
  710. mutex_unlock(&inst->dma_cache.lock);
  711. }
  712. kmem_cache_free(cvp_driver->buf_cache, pbuf);
  713. }
  714. }
  715. mutex_unlock(&inst->persistbufs.lock);
  716. return rc;
  717. }
  718. int msm_cvp_mark_user_persist(struct msm_cvp_inst *inst,
  719. struct eva_kmd_hfi_packet *in_pkt,
  720. unsigned int offset, unsigned int buf_num)
  721. {
  722. struct cvp_hfi_cmd_session_hdr *cmd_hdr;
  723. struct cvp_internal_buf *pbuf, *dummy;
  724. u64 ktid;
  725. struct cvp_buf_type *buf;
  726. int i, rc = 0;
  727. if (!offset || !buf_num)
  728. return 0;
  729. cmd_hdr = (struct cvp_hfi_cmd_session_hdr *)in_pkt;
  730. ktid = atomic64_inc_return(&inst->core->kernel_trans_id);
  731. ktid &= (FENCE_BIT - 1);
  732. cmd_hdr->client_data.kdata = ktid;
  733. for (i = 0; i < buf_num; i++) {
  734. buf = (struct cvp_buf_type *)&in_pkt->pkt_data[offset];
  735. offset += sizeof(*buf) >> 2;
  736. if (buf->fd < 0 || !buf->size)
  737. continue;
  738. mutex_lock(&inst->persistbufs.lock);
  739. list_for_each_entry_safe(pbuf, dummy, &inst->persistbufs.list,
  740. list) {
  741. if (pbuf->ownership == CLIENT) {
  742. if (pbuf->fd == buf->fd &&
  743. pbuf->size == buf->size)
  744. buf->fd = pbuf->smem->device_addr;
  745. rc = 1;
  746. break;
  747. }
  748. }
  749. mutex_unlock(&inst->persistbufs.lock);
  750. if (!rc) {
  751. dprintk(CVP_ERR, "%s No persist buf %d found\n",
  752. __func__, buf->fd);
  753. rc = -EFAULT;
  754. break;
  755. }
  756. pbuf->ktid = ktid;
  757. rc = 0;
  758. }
  759. return rc;
  760. }
  761. int msm_cvp_map_user_persist(struct msm_cvp_inst *inst,
  762. struct eva_kmd_hfi_packet *in_pkt,
  763. unsigned int offset, unsigned int buf_num)
  764. {
  765. struct cvp_buf_type *buf;
  766. int i;
  767. u32 iova;
  768. if (!offset || !buf_num)
  769. return 0;
  770. for (i = 0; i < buf_num; i++) {
  771. buf = (struct cvp_buf_type *)&in_pkt->pkt_data[offset];
  772. offset += sizeof(*buf) >> 2;
  773. if (buf->fd < 0 || !buf->size)
  774. continue;
  775. iova = msm_cvp_map_user_persist_buf(inst, buf);
  776. if (!iova) {
  777. dprintk(CVP_ERR,
  778. "%s: buf %d register failed.\n",
  779. __func__, i);
  780. return -EINVAL;
  781. }
  782. buf->fd = iova;
  783. }
  784. return 0;
  785. }
  786. int msm_cvp_map_frame(struct msm_cvp_inst *inst,
  787. struct eva_kmd_hfi_packet *in_pkt,
  788. unsigned int offset, unsigned int buf_num)
  789. {
  790. struct cvp_buf_type *buf;
  791. int i;
  792. u32 iova;
  793. u64 ktid;
  794. struct msm_cvp_frame *frame;
  795. struct cvp_hfi_cmd_session_hdr *cmd_hdr;
  796. if (!offset || !buf_num)
  797. return 0;
  798. cmd_hdr = (struct cvp_hfi_cmd_session_hdr *)in_pkt;
  799. ktid = atomic64_inc_return(&inst->core->kernel_trans_id);
  800. ktid &= (FENCE_BIT - 1);
  801. cmd_hdr->client_data.kdata = ktid;
  802. frame = kmem_cache_zalloc(cvp_driver->frame_cache, GFP_KERNEL);
  803. if (!frame)
  804. return -ENOMEM;
  805. frame->ktid = ktid;
  806. frame->nr = 0;
  807. frame->pkt_type = cmd_hdr->packet_type;
  808. for (i = 0; i < buf_num; i++) {
  809. buf = (struct cvp_buf_type *)&in_pkt->pkt_data[offset];
  810. offset += sizeof(*buf) >> 2;
  811. if (buf->fd < 0 || !buf->size)
  812. continue;
  813. iova = msm_cvp_map_frame_buf(inst, buf, frame);
  814. if (!iova) {
  815. dprintk(CVP_ERR,
  816. "%s: buf %d register failed.\n",
  817. __func__, i);
  818. msm_cvp_unmap_frame_buf(inst, frame);
  819. return -EINVAL;
  820. }
  821. buf->fd = iova;
  822. }
  823. mutex_lock(&inst->frames.lock);
  824. list_add_tail(&frame->list, &inst->frames.list);
  825. mutex_unlock(&inst->frames.lock);
  826. dprintk(CVP_MEM, "%s: map frame %llu\n", __func__, ktid);
  827. return 0;
  828. }
  829. int msm_cvp_session_deinit_buffers(struct msm_cvp_inst *inst)
  830. {
  831. int rc = 0, i;
  832. struct cvp_internal_buf *cbuf, *dummy;
  833. struct msm_cvp_frame *frame, *dummy1;
  834. struct msm_cvp_smem *smem;
  835. struct cvp_hal_session *session;
  836. session = (struct cvp_hal_session *)inst->session;
  837. mutex_lock(&inst->frames.lock);
  838. list_for_each_entry_safe(frame, dummy1, &inst->frames.list, list) {
  839. list_del(&frame->list);
  840. msm_cvp_unmap_frame_buf(inst, frame);
  841. }
  842. mutex_unlock(&inst->frames.lock);
  843. mutex_lock(&inst->dma_cache.lock);
  844. for (i = 0; i < inst->dma_cache.nr; i++) {
  845. smem = inst->dma_cache.entries[i];
  846. if (atomic_read(&smem->refcount) == 0) {
  847. print_smem(CVP_MEM, "free", inst, smem);
  848. } else {
  849. print_smem(CVP_WARN, "in use", inst, smem);
  850. }
  851. msm_cvp_unmap_smem(inst, smem, "unmap cpu");
  852. msm_cvp_smem_put_dma_buf(smem->dma_buf);
  853. kmem_cache_free(cvp_driver->smem_cache, smem);
  854. inst->dma_cache.entries[i] = NULL;
  855. }
  856. mutex_unlock(&inst->dma_cache.lock);
  857. mutex_lock(&inst->cvpdspbufs.lock);
  858. list_for_each_entry_safe(cbuf, dummy, &inst->cvpdspbufs.list,
  859. list) {
  860. print_internal_buffer(CVP_MEM, "remove dspbufs", inst, cbuf);
  861. rc = cvp_dsp_deregister_buffer(hash32_ptr(session),
  862. cbuf->fd, cbuf->smem->dma_buf->size, cbuf->size,
  863. cbuf->offset, cbuf->index,
  864. (uint32_t)cbuf->smem->device_addr);
  865. if (rc)
  866. dprintk(CVP_ERR,
  867. "%s: failed dsp deregistration fd=%d rc=%d",
  868. __func__, cbuf->fd, rc);
  869. msm_cvp_unmap_smem(inst, cbuf->smem, "unmap dsp");
  870. msm_cvp_smem_put_dma_buf(cbuf->smem->dma_buf);
  871. list_del(&cbuf->list);
  872. kmem_cache_free(cvp_driver->buf_cache, cbuf);
  873. }
  874. mutex_unlock(&inst->cvpdspbufs.lock);
  875. return rc;
  876. }
  877. void msm_cvp_print_inst_bufs(struct msm_cvp_inst *inst)
  878. {
  879. struct cvp_internal_buf *buf;
  880. int i;
  881. if (!inst) {
  882. dprintk(CVP_ERR, "%s - invalid param %pK\n",
  883. __func__, inst);
  884. return;
  885. }
  886. dprintk(CVP_ERR, "active session cmd %d\n", inst->cur_cmd_type);
  887. dprintk(CVP_ERR,
  888. "---Buffer details for inst: %pK of type: %d---\n",
  889. inst, inst->session_type);
  890. mutex_lock(&inst->dma_cache.lock);
  891. dprintk(CVP_ERR, "dma cache:\n");
  892. if (inst->dma_cache.nr <= MAX_DMABUF_NUMS)
  893. for (i = 0; i < inst->dma_cache.nr; i++)
  894. print_smem(CVP_ERR, "bufdump", inst,
  895. inst->dma_cache.entries[i]);
  896. mutex_unlock(&inst->dma_cache.lock);
  897. mutex_lock(&inst->cvpdspbufs.lock);
  898. dprintk(CVP_ERR, "dsp buffer list:\n");
  899. list_for_each_entry(buf, &inst->cvpdspbufs.list, list)
  900. print_cvp_buffer(CVP_ERR, "bufdump", inst, buf);
  901. mutex_unlock(&inst->cvpdspbufs.lock);
  902. mutex_lock(&inst->persistbufs.lock);
  903. dprintk(CVP_ERR, "persist buffer list:\n");
  904. list_for_each_entry(buf, &inst->persistbufs.list, list)
  905. print_cvp_buffer(CVP_ERR, "bufdump", inst, buf);
  906. mutex_unlock(&inst->persistbufs.lock);
  907. }
  908. struct cvp_internal_buf *cvp_allocate_arp_bufs(struct msm_cvp_inst *inst,
  909. u32 buffer_size)
  910. {
  911. struct cvp_internal_buf *buf;
  912. struct msm_cvp_list *buf_list;
  913. u32 smem_flags = SMEM_UNCACHED;
  914. int rc = 0;
  915. if (!inst) {
  916. dprintk(CVP_ERR, "%s Invalid input\n", __func__);
  917. return NULL;
  918. }
  919. buf_list = &inst->persistbufs;
  920. if (!buffer_size)
  921. return NULL;
  922. /* PERSIST buffer requires secure mapping */
  923. /* Disable and wait for hyp_assign available
  924. * smem_flags |= SMEM_SECURE | SMEM_NON_PIXEL;
  925. */
  926. buf = kmem_cache_zalloc(cvp_driver->buf_cache, GFP_KERNEL);
  927. if (!buf) {
  928. dprintk(CVP_ERR, "%s Out of memory\n", __func__);
  929. goto fail_kzalloc;
  930. }
  931. buf->smem = kmem_cache_zalloc(cvp_driver->smem_cache, GFP_KERNEL);
  932. if (!buf->smem) {
  933. dprintk(CVP_ERR, "%s Out of memory\n", __func__);
  934. goto fail_kzalloc;
  935. }
  936. rc = msm_cvp_smem_alloc(buffer_size, 1, smem_flags, 0,
  937. &(inst->core->resources), buf->smem);
  938. if (rc) {
  939. dprintk(CVP_ERR, "Failed to allocate ARP memory\n");
  940. goto err_no_mem;
  941. }
  942. buf->size = buf->smem->size;
  943. buf->type = HFI_BUFFER_INTERNAL_PERSIST_1;
  944. buf->ownership = DRIVER;
  945. mutex_lock(&buf_list->lock);
  946. list_add_tail(&buf->list, &buf_list->list);
  947. mutex_unlock(&buf_list->lock);
  948. return buf;
  949. err_no_mem:
  950. kmem_cache_free(cvp_driver->buf_cache, buf);
  951. fail_kzalloc:
  952. return NULL;
  953. }
  954. int cvp_release_arp_buffers(struct msm_cvp_inst *inst)
  955. {
  956. struct msm_cvp_smem *smem;
  957. struct list_head *ptr, *next;
  958. struct cvp_internal_buf *buf;
  959. int rc = 0;
  960. struct msm_cvp_core *core;
  961. struct cvp_hfi_device *hdev;
  962. if (!inst) {
  963. dprintk(CVP_ERR, "Invalid instance pointer = %pK\n", inst);
  964. return -EINVAL;
  965. }
  966. core = inst->core;
  967. if (!core) {
  968. dprintk(CVP_ERR, "Invalid core pointer = %pK\n", core);
  969. return -EINVAL;
  970. }
  971. hdev = core->device;
  972. if (!hdev) {
  973. dprintk(CVP_ERR, "Invalid device pointer = %pK\n", hdev);
  974. return -EINVAL;
  975. }
  976. dprintk(CVP_MEM, "release persist buffer!\n");
  977. mutex_lock(&inst->persistbufs.lock);
  978. /* Workaround for FW: release buffer means release all */
  979. if (inst->state <= MSM_CVP_CLOSE_DONE) {
  980. rc = call_hfi_op(hdev, session_release_buffers,
  981. (void *)inst->session);
  982. if (!rc) {
  983. mutex_unlock(&inst->persistbufs.lock);
  984. rc = wait_for_sess_signal_receipt(inst,
  985. HAL_SESSION_RELEASE_BUFFER_DONE);
  986. if (rc)
  987. dprintk(CVP_WARN,
  988. "%s: wait for signal failed, rc %d\n",
  989. __func__, rc);
  990. mutex_lock(&inst->persistbufs.lock);
  991. } else {
  992. dprintk(CVP_WARN, "Fail to send Rel prst buf\n");
  993. }
  994. }
  995. list_for_each_safe(ptr, next, &inst->persistbufs.list) {
  996. buf = list_entry(ptr, struct cvp_internal_buf, list);
  997. smem = buf->smem;
  998. if (!smem) {
  999. dprintk(CVP_ERR, "%s invalid smem\n", __func__);
  1000. mutex_unlock(&inst->persistbufs.lock);
  1001. return -EINVAL;
  1002. }
  1003. list_del(&buf->list);
  1004. if (buf->ownership == DRIVER) {
  1005. dprintk(CVP_MEM,
  1006. "%s: %x : fd %d %s size %d",
  1007. "free arp", hash32_ptr(inst->session), buf->fd,
  1008. smem->dma_buf->name, buf->size);
  1009. msm_cvp_smem_free(smem);
  1010. kmem_cache_free(cvp_driver->smem_cache, smem);
  1011. }
  1012. buf->smem = NULL;
  1013. kmem_cache_free(cvp_driver->buf_cache, buf);
  1014. }
  1015. mutex_unlock(&inst->persistbufs.lock);
  1016. return rc;
  1017. }
  1018. int cvp_allocate_dsp_bufs(struct msm_cvp_inst *inst,
  1019. struct cvp_internal_buf *buf,
  1020. u32 buffer_size,
  1021. u32 secure_type)
  1022. {
  1023. u32 smem_flags = SMEM_UNCACHED;
  1024. int rc = 0;
  1025. if (!inst) {
  1026. dprintk(CVP_ERR, "%s Invalid input\n", __func__);
  1027. return -EINVAL;
  1028. }
  1029. if (!buf)
  1030. return -EINVAL;
  1031. if (!buffer_size)
  1032. return -EINVAL;
  1033. switch (secure_type) {
  1034. case 0:
  1035. break;
  1036. case 1:
  1037. smem_flags |= SMEM_SECURE | SMEM_PIXEL;
  1038. break;
  1039. case 2:
  1040. smem_flags |= SMEM_SECURE | SMEM_NON_PIXEL;
  1041. break;
  1042. default:
  1043. dprintk(CVP_ERR, "%s Invalid secure_type %d\n",
  1044. __func__, secure_type);
  1045. return -EINVAL;
  1046. }
  1047. dprintk(CVP_ERR, "%s smem_flags 0x%x\n", __func__, smem_flags);
  1048. buf->smem = kmem_cache_zalloc(cvp_driver->smem_cache, GFP_KERNEL);
  1049. if (!buf->smem) {
  1050. dprintk(CVP_ERR, "%s Out of memory\n", __func__);
  1051. goto fail_kzalloc_smem_cache;
  1052. }
  1053. rc = msm_cvp_smem_alloc(buffer_size, 1, smem_flags, 0,
  1054. &(inst->core->resources), buf->smem);
  1055. if (rc) {
  1056. dprintk(CVP_ERR, "Failed to allocate ARP memory\n");
  1057. goto err_no_mem;
  1058. }
  1059. dprintk(CVP_ERR, "%s dma_buf %pK\n", __func__, buf->smem->dma_buf);
  1060. buf->size = buf->smem->size;
  1061. buf->type = HFI_BUFFER_INTERNAL_PERSIST_1;
  1062. buf->ownership = CLIENT;
  1063. return rc;
  1064. err_no_mem:
  1065. kmem_cache_free(cvp_driver->smem_cache, buf->smem);
  1066. fail_kzalloc_smem_cache:
  1067. return rc;
  1068. }
  1069. int cvp_release_dsp_buffers(struct msm_cvp_inst *inst,
  1070. struct cvp_internal_buf *buf)
  1071. {
  1072. struct msm_cvp_smem *smem;
  1073. int rc = 0;
  1074. if (!inst) {
  1075. dprintk(CVP_ERR, "Invalid instance pointer = %pK\n", inst);
  1076. return -EINVAL;
  1077. }
  1078. if (!buf) {
  1079. dprintk(CVP_ERR, "Invalid buffer pointer = %pK\n", inst);
  1080. return -EINVAL;
  1081. }
  1082. smem = buf->smem;
  1083. if (!smem) {
  1084. dprintk(CVP_ERR, "%s invalid smem\n", __func__);
  1085. return -EINVAL;
  1086. }
  1087. if (buf->ownership == CLIENT) {
  1088. dprintk(CVP_MEM,
  1089. "%s: %x : fd %x %s size %d",
  1090. "free dsp buf", hash32_ptr(inst->session), buf->fd,
  1091. smem->dma_buf->name, buf->size);
  1092. msm_cvp_smem_free(smem);
  1093. kmem_cache_free(cvp_driver->smem_cache, smem);
  1094. }
  1095. return rc;
  1096. }