msm_cvp_buf.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  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. pid_s = find_get_pid(pid);
  302. if (pid_s == NULL) {
  303. dprintk(CVP_WARN, "%s incorrect pid\n", __func__);
  304. return -EINVAL;
  305. }
  306. dprintk(CVP_WARN, "%s get pid_s 0x%x from pidA 0x%x\n", __func__, pid_s, pid);
  307. /* task = get_pid_task(pid, PIDTYPE_PID); */
  308. task = get_pid_task(pid_s, PIDTYPE_TGID);
  309. if (!task)
  310. dprintk(CVP_WARN, "%s task doesn't exist\n", __func__);
  311. file = msm_cvp_fget(buf->fd, task, FMODE_PATH, 1);
  312. if (file == NULL) {
  313. dprintk(CVP_WARN, "%s fail to get file from fd\n", __func__);
  314. put_task_struct(task);
  315. return -EINVAL;
  316. }
  317. //entry->file = file;
  318. dma_buf = cvp_dma_buf_get(
  319. file,
  320. buf->fd,
  321. task);
  322. if (dma_buf == ERR_PTR(-EINVAL)) {
  323. dprintk(CVP_ERR, "%s: Invalid fd = %d", __func__, buf->fd);
  324. fput(file);
  325. put_task_struct(task);
  326. return -EINVAL;
  327. }
  328. dprintk(CVP_WARN, "dma_buf from internal %llu\n", dma_buf);
  329. /* to unmap dsp buf, below sequence is required
  330. * fput(file);
  331. * dma_buf_put(dma_buf);
  332. * put_task_struct(task);
  333. */
  334. if (!dma_buf) {
  335. dprintk(CVP_ERR, "%s: Invalid fd = %d", __func__, buf->fd);
  336. return 0;
  337. }
  338. cbuf = kmem_cache_zalloc(cvp_driver->buf_cache, GFP_KERNEL);
  339. if (!cbuf)
  340. return -ENOMEM;
  341. smem = kmem_cache_zalloc(cvp_driver->smem_cache, GFP_KERNEL);
  342. if (!smem) {
  343. kmem_cache_free(cvp_driver->buf_cache, cbuf);
  344. return -ENOMEM;
  345. }
  346. smem->dma_buf = dma_buf;
  347. smem->bitmap_index = MAX_DMABUF_NUMS;
  348. dprintk(CVP_DSP, "%s: dma_buf = %llx\n", __func__, dma_buf);
  349. rc = msm_cvp_map_smem(inst, smem, "map dsp");
  350. if (rc) {
  351. print_client_buffer(CVP_ERR, "map failed", inst, buf);
  352. goto exit;
  353. }
  354. cbuf->smem = smem;
  355. cbuf->fd = buf->fd;
  356. cbuf->size = buf->size;
  357. cbuf->offset = buf->offset;
  358. cbuf->ownership = CLIENT;
  359. cbuf->index = buf->index;
  360. *iova = (uint32_t)smem->device_addr;
  361. dprintk(CVP_DSP, "%s: buf->fd %d, device_addr = %llx\n",
  362. __func__, buf->fd, (uint32_t)smem->device_addr);
  363. mutex_lock(&inst->cvpdspbufs.lock);
  364. list_add_tail(&cbuf->list, &inst->cvpdspbufs.list);
  365. mutex_unlock(&inst->cvpdspbufs.lock);
  366. return rc;
  367. exit:
  368. if (smem->device_addr) {
  369. msm_cvp_unmap_smem(inst, smem, "unmap dsp");
  370. msm_cvp_smem_put_dma_buf(smem->dma_buf);
  371. }
  372. kmem_cache_free(cvp_driver->buf_cache, cbuf);
  373. cbuf = NULL;
  374. kmem_cache_free(cvp_driver->smem_cache, smem);
  375. smem = NULL;
  376. return rc;
  377. }
  378. int msm_cvp_unmap_buf_dsp_new(struct msm_cvp_inst *inst,
  379. struct eva_kmd_buffer *buf)
  380. {
  381. int rc = 0;
  382. bool found;
  383. struct cvp_internal_buf *cbuf;
  384. struct cvp_hal_session *session;
  385. if (!inst || !inst->core || !buf) {
  386. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  387. return -EINVAL;
  388. }
  389. session = (struct cvp_hal_session *)inst->session;
  390. if (!session) {
  391. dprintk(CVP_ERR, "%s: invalid session\n", __func__);
  392. return -EINVAL;
  393. }
  394. mutex_lock(&inst->cvpdspbufs.lock);
  395. found = false;
  396. list_for_each_entry(cbuf, &inst->cvpdspbufs.list, list) {
  397. if (cbuf->fd == buf->fd) {
  398. found = true;
  399. break;
  400. }
  401. }
  402. mutex_unlock(&inst->cvpdspbufs.lock);
  403. if (!found) {
  404. print_client_buffer(CVP_ERR, "invalid", inst, buf);
  405. return -EINVAL;
  406. }
  407. if (cbuf->smem->device_addr) {
  408. msm_cvp_unmap_smem(inst, cbuf->smem, "unmap dsp");
  409. msm_cvp_smem_put_dma_buf(cbuf->smem->dma_buf);
  410. }
  411. mutex_lock(&inst->cvpdspbufs.lock);
  412. list_del(&cbuf->list);
  413. mutex_unlock(&inst->cvpdspbufs.lock);
  414. kmem_cache_free(cvp_driver->smem_cache, cbuf->smem);
  415. kmem_cache_free(cvp_driver->buf_cache, cbuf);
  416. return rc;
  417. }
  418. void msm_cvp_cache_operations(struct msm_cvp_smem *smem, u32 type,
  419. u32 offset, u32 size)
  420. {
  421. enum smem_cache_ops cache_op;
  422. if (msm_cvp_cacheop_disabled)
  423. return;
  424. if (!smem) {
  425. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  426. return;
  427. }
  428. switch (type) {
  429. case EVA_KMD_BUFTYPE_INPUT:
  430. cache_op = SMEM_CACHE_CLEAN;
  431. break;
  432. case EVA_KMD_BUFTYPE_OUTPUT:
  433. cache_op = SMEM_CACHE_INVALIDATE;
  434. break;
  435. default:
  436. cache_op = SMEM_CACHE_CLEAN_INVALIDATE;
  437. }
  438. dprintk(CVP_MEM,
  439. "%s: cache operation enabled for dma_buf: %llx, cache_op: %d, offset: %d, size: %d\n",
  440. __func__, smem->dma_buf, cache_op, offset, size);
  441. msm_cvp_smem_cache_operations(smem->dma_buf, cache_op, offset, size);
  442. }
  443. static struct msm_cvp_smem *msm_cvp_session_find_smem(struct msm_cvp_inst *inst,
  444. struct dma_buf *dma_buf)
  445. {
  446. struct msm_cvp_smem *smem;
  447. int i;
  448. if (inst->dma_cache.nr > MAX_DMABUF_NUMS)
  449. return NULL;
  450. mutex_lock(&inst->dma_cache.lock);
  451. for (i = 0; i < inst->dma_cache.nr; i++)
  452. if (inst->dma_cache.entries[i]->dma_buf == dma_buf) {
  453. SET_USE_BITMAP(i, inst);
  454. smem = inst->dma_cache.entries[i];
  455. smem->bitmap_index = i;
  456. atomic_inc(&smem->refcount);
  457. /*
  458. * If we find it, it means we already increased
  459. * refcount before, so we put it to avoid double
  460. * incremental.
  461. */
  462. msm_cvp_smem_put_dma_buf(smem->dma_buf);
  463. mutex_unlock(&inst->dma_cache.lock);
  464. print_smem(CVP_MEM, "found", inst, smem);
  465. return smem;
  466. }
  467. mutex_unlock(&inst->dma_cache.lock);
  468. return NULL;
  469. }
  470. static int msm_cvp_session_add_smem(struct msm_cvp_inst *inst,
  471. struct msm_cvp_smem *smem)
  472. {
  473. unsigned int i;
  474. struct msm_cvp_smem *smem2;
  475. mutex_lock(&inst->dma_cache.lock);
  476. if (inst->dma_cache.nr < MAX_DMABUF_NUMS) {
  477. inst->dma_cache.entries[inst->dma_cache.nr] = smem;
  478. SET_USE_BITMAP(inst->dma_cache.nr, inst);
  479. smem->bitmap_index = inst->dma_cache.nr;
  480. inst->dma_cache.nr++;
  481. i = smem->bitmap_index;
  482. } else {
  483. i = find_first_zero_bit(&inst->dma_cache.usage_bitmap,
  484. MAX_DMABUF_NUMS);
  485. if (i < MAX_DMABUF_NUMS) {
  486. smem2 = inst->dma_cache.entries[i];
  487. msm_cvp_unmap_smem(inst, smem2, "unmap cpu");
  488. msm_cvp_smem_put_dma_buf(smem2->dma_buf);
  489. kmem_cache_free(cvp_driver->smem_cache, smem2);
  490. inst->dma_cache.entries[i] = smem;
  491. smem->bitmap_index = i;
  492. SET_USE_BITMAP(i, inst);
  493. } else {
  494. dprintk(CVP_WARN, "%s: not enough memory\n", __func__);
  495. mutex_unlock(&inst->dma_cache.lock);
  496. return -ENOMEM;
  497. }
  498. }
  499. atomic_inc(&smem->refcount);
  500. mutex_unlock(&inst->dma_cache.lock);
  501. dprintk(CVP_MEM, "Add entry %d into cache\n", i);
  502. return 0;
  503. }
  504. static struct msm_cvp_smem *msm_cvp_session_get_smem(struct msm_cvp_inst *inst,
  505. struct cvp_buf_type *buf)
  506. {
  507. int rc = 0, found = 1;
  508. struct msm_cvp_smem *smem = NULL;
  509. struct dma_buf *dma_buf = NULL;
  510. if (buf->fd < 0) {
  511. dprintk(CVP_ERR, "%s: Invalid fd = %d", __func__, buf->fd);
  512. return NULL;
  513. }
  514. dma_buf = msm_cvp_smem_get_dma_buf(buf->fd);
  515. if (!dma_buf) {
  516. dprintk(CVP_ERR, "%s: Invalid fd = %d", __func__, buf->fd);
  517. return NULL;
  518. }
  519. smem = msm_cvp_session_find_smem(inst, dma_buf);
  520. if (!smem) {
  521. found = 0;
  522. smem = kmem_cache_zalloc(cvp_driver->smem_cache, GFP_KERNEL);
  523. if (!smem)
  524. return NULL;
  525. smem->dma_buf = dma_buf;
  526. smem->bitmap_index = MAX_DMABUF_NUMS;
  527. rc = msm_cvp_map_smem(inst, smem, "map cpu");
  528. if (rc)
  529. goto exit;
  530. rc = msm_cvp_session_add_smem(inst, smem);
  531. if (rc && rc != -ENOMEM)
  532. goto exit2;
  533. }
  534. if (buf->size > smem->size || buf->size > smem->size - buf->offset) {
  535. dprintk(CVP_ERR, "%s: invalid offset %d or size %d\n",
  536. __func__, buf->offset, buf->size);
  537. if (found) {
  538. mutex_lock(&inst->dma_cache.lock);
  539. atomic_dec(&smem->refcount);
  540. mutex_unlock(&inst->dma_cache.lock);
  541. return NULL;
  542. }
  543. goto exit2;
  544. }
  545. return smem;
  546. exit2:
  547. msm_cvp_unmap_smem(inst, smem, "unmap cpu");
  548. exit:
  549. msm_cvp_smem_put_dma_buf(dma_buf);
  550. kmem_cache_free(cvp_driver->smem_cache, smem);
  551. smem = NULL;
  552. return smem;
  553. }
  554. static u32 msm_cvp_map_user_persist_buf(struct msm_cvp_inst *inst,
  555. struct cvp_buf_type *buf)
  556. {
  557. u32 iova = 0;
  558. struct msm_cvp_smem *smem = NULL;
  559. struct cvp_internal_buf *pbuf;
  560. if (!inst) {
  561. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  562. return -EINVAL;
  563. }
  564. pbuf = kmem_cache_zalloc(cvp_driver->buf_cache, GFP_KERNEL);
  565. if (!pbuf)
  566. return 0;
  567. smem = msm_cvp_session_get_smem(inst, buf);
  568. if (!smem)
  569. goto exit;
  570. pbuf->smem = smem;
  571. pbuf->fd = buf->fd;
  572. pbuf->size = buf->size;
  573. pbuf->offset = buf->offset;
  574. pbuf->ownership = CLIENT;
  575. mutex_lock(&inst->persistbufs.lock);
  576. list_add_tail(&pbuf->list, &inst->persistbufs.list);
  577. mutex_unlock(&inst->persistbufs.lock);
  578. print_internal_buffer(CVP_MEM, "map persist", inst, pbuf);
  579. iova = smem->device_addr + buf->offset;
  580. return iova;
  581. exit:
  582. kmem_cache_free(cvp_driver->buf_cache, pbuf);
  583. return 0;
  584. }
  585. u32 msm_cvp_map_frame_buf(struct msm_cvp_inst *inst,
  586. struct cvp_buf_type *buf,
  587. struct msm_cvp_frame *frame)
  588. {
  589. u32 iova = 0;
  590. struct msm_cvp_smem *smem = NULL;
  591. u32 nr;
  592. u32 type;
  593. if (!inst || !frame) {
  594. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  595. return 0;
  596. }
  597. nr = frame->nr;
  598. if (nr == MAX_FRAME_BUFFER_NUMS) {
  599. dprintk(CVP_ERR, "%s: max frame buffer reached\n", __func__);
  600. return 0;
  601. }
  602. smem = msm_cvp_session_get_smem(inst, buf);
  603. if (!smem)
  604. return 0;
  605. frame->bufs[nr].fd = buf->fd;
  606. frame->bufs[nr].smem = smem;
  607. frame->bufs[nr].size = buf->size;
  608. frame->bufs[nr].offset = buf->offset;
  609. print_internal_buffer(CVP_MEM, "map cpu", inst, &frame->bufs[nr]);
  610. frame->nr++;
  611. type = EVA_KMD_BUFTYPE_INPUT | EVA_KMD_BUFTYPE_OUTPUT;
  612. msm_cvp_cache_operations(smem, type, buf->offset, buf->size);
  613. iova = smem->device_addr + buf->offset;
  614. return iova;
  615. }
  616. static void msm_cvp_unmap_frame_buf(struct msm_cvp_inst *inst,
  617. struct msm_cvp_frame *frame)
  618. {
  619. u32 i;
  620. u32 type;
  621. struct msm_cvp_smem *smem = NULL;
  622. struct cvp_internal_buf *buf;
  623. type = EVA_KMD_BUFTYPE_OUTPUT;
  624. for (i = 0; i < frame->nr; ++i) {
  625. buf = &frame->bufs[i];
  626. smem = buf->smem;
  627. msm_cvp_cache_operations(smem, type, buf->offset, buf->size);
  628. if (smem->bitmap_index >= MAX_DMABUF_NUMS) {
  629. /* smem not in dmamap cache */
  630. msm_cvp_unmap_smem(inst, smem, "unmap cpu");
  631. dma_heap_buffer_free(smem->dma_buf);
  632. kmem_cache_free(cvp_driver->smem_cache, smem);
  633. buf->smem = NULL;
  634. } else {
  635. mutex_lock(&inst->dma_cache.lock);
  636. if (atomic_dec_and_test(&smem->refcount)) {
  637. CLEAR_USE_BITMAP(smem->bitmap_index, inst);
  638. print_smem(CVP_MEM, "Map dereference",
  639. inst, smem);
  640. }
  641. mutex_unlock(&inst->dma_cache.lock);
  642. }
  643. }
  644. kmem_cache_free(cvp_driver->frame_cache, frame);
  645. }
  646. void msm_cvp_unmap_frame(struct msm_cvp_inst *inst, u64 ktid)
  647. {
  648. struct msm_cvp_frame *frame, *dummy1;
  649. bool found;
  650. if (!inst) {
  651. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  652. return;
  653. }
  654. ktid &= (FENCE_BIT - 1);
  655. dprintk(CVP_MEM, "%s: (%#x) unmap frame %llu\n",
  656. __func__, hash32_ptr(inst->session), ktid);
  657. found = false;
  658. mutex_lock(&inst->frames.lock);
  659. list_for_each_entry_safe(frame, dummy1, &inst->frames.list, list) {
  660. if (frame->ktid == ktid) {
  661. found = true;
  662. list_del(&frame->list);
  663. break;
  664. }
  665. }
  666. mutex_unlock(&inst->frames.lock);
  667. if (found)
  668. msm_cvp_unmap_frame_buf(inst, frame);
  669. else
  670. dprintk(CVP_WARN, "%s frame %llu not found!\n", __func__, ktid);
  671. }
  672. int msm_cvp_unmap_user_persist(struct msm_cvp_inst *inst,
  673. struct eva_kmd_hfi_packet *in_pkt,
  674. unsigned int offset, unsigned int buf_num)
  675. {
  676. struct cvp_hfi_cmd_session_hdr *cmd_hdr;
  677. struct cvp_internal_buf *pbuf, *dummy;
  678. u64 ktid;
  679. int rc = 0;
  680. struct msm_cvp_smem *smem = NULL;
  681. if (!offset || !buf_num)
  682. return rc;
  683. cmd_hdr = (struct cvp_hfi_cmd_session_hdr *)in_pkt;
  684. ktid = cmd_hdr->client_data.kdata & (FENCE_BIT - 1);
  685. mutex_lock(&inst->persistbufs.lock);
  686. list_for_each_entry_safe(pbuf, dummy, &inst->persistbufs.list, list) {
  687. if (pbuf->ktid == ktid && pbuf->ownership == CLIENT) {
  688. list_del(&pbuf->list);
  689. smem = pbuf->smem;
  690. dprintk(CVP_MEM, "unmap persist: %x %d %d %#x",
  691. hash32_ptr(inst->session), pbuf->fd,
  692. pbuf->size, smem->device_addr);
  693. if (smem->bitmap_index >= MAX_DMABUF_NUMS) {
  694. /* smem not in dmamap cache */
  695. msm_cvp_unmap_smem(inst, smem,
  696. "unmap cpu");
  697. dma_heap_buffer_free(smem->dma_buf);
  698. kmem_cache_free(
  699. cvp_driver->smem_cache,
  700. smem);
  701. pbuf->smem = NULL;
  702. } else {
  703. mutex_lock(&inst->dma_cache.lock);
  704. if (atomic_dec_and_test(&smem->refcount))
  705. CLEAR_USE_BITMAP(
  706. smem->bitmap_index,
  707. inst);
  708. mutex_unlock(&inst->dma_cache.lock);
  709. }
  710. kmem_cache_free(cvp_driver->buf_cache, pbuf);
  711. }
  712. }
  713. mutex_unlock(&inst->persistbufs.lock);
  714. return rc;
  715. }
  716. int msm_cvp_mark_user_persist(struct msm_cvp_inst *inst,
  717. struct eva_kmd_hfi_packet *in_pkt,
  718. unsigned int offset, unsigned int buf_num)
  719. {
  720. struct cvp_hfi_cmd_session_hdr *cmd_hdr;
  721. struct cvp_internal_buf *pbuf, *dummy;
  722. u64 ktid;
  723. struct cvp_buf_type *buf;
  724. int i, rc = 0;
  725. if (!offset || !buf_num)
  726. return 0;
  727. cmd_hdr = (struct cvp_hfi_cmd_session_hdr *)in_pkt;
  728. ktid = atomic64_inc_return(&inst->core->kernel_trans_id);
  729. ktid &= (FENCE_BIT - 1);
  730. cmd_hdr->client_data.kdata = ktid;
  731. for (i = 0; i < buf_num; i++) {
  732. buf = (struct cvp_buf_type *)&in_pkt->pkt_data[offset];
  733. offset += sizeof(*buf) >> 2;
  734. if (buf->fd < 0 || !buf->size)
  735. continue;
  736. mutex_lock(&inst->persistbufs.lock);
  737. list_for_each_entry_safe(pbuf, dummy, &inst->persistbufs.list,
  738. list) {
  739. if (pbuf->ownership == CLIENT) {
  740. if (pbuf->fd == buf->fd &&
  741. pbuf->size == buf->size)
  742. buf->fd = pbuf->smem->device_addr;
  743. rc = 1;
  744. break;
  745. }
  746. }
  747. mutex_unlock(&inst->persistbufs.lock);
  748. if (!rc) {
  749. dprintk(CVP_ERR, "%s No persist buf %d found\n",
  750. __func__, buf->fd);
  751. rc = -EFAULT;
  752. break;
  753. }
  754. pbuf->ktid = ktid;
  755. rc = 0;
  756. }
  757. return rc;
  758. }
  759. int msm_cvp_map_user_persist(struct msm_cvp_inst *inst,
  760. struct eva_kmd_hfi_packet *in_pkt,
  761. unsigned int offset, unsigned int buf_num)
  762. {
  763. struct cvp_buf_type *buf;
  764. int i;
  765. u32 iova;
  766. if (!offset || !buf_num)
  767. return 0;
  768. for (i = 0; i < buf_num; i++) {
  769. buf = (struct cvp_buf_type *)&in_pkt->pkt_data[offset];
  770. offset += sizeof(*buf) >> 2;
  771. if (buf->fd < 0 || !buf->size)
  772. continue;
  773. iova = msm_cvp_map_user_persist_buf(inst, buf);
  774. if (!iova) {
  775. dprintk(CVP_ERR,
  776. "%s: buf %d register failed.\n",
  777. __func__, i);
  778. return -EINVAL;
  779. }
  780. buf->fd = iova;
  781. }
  782. return 0;
  783. }
  784. int msm_cvp_map_frame(struct msm_cvp_inst *inst,
  785. struct eva_kmd_hfi_packet *in_pkt,
  786. unsigned int offset, unsigned int buf_num)
  787. {
  788. struct cvp_buf_type *buf;
  789. int i;
  790. u32 iova;
  791. u64 ktid;
  792. struct msm_cvp_frame *frame;
  793. struct cvp_hfi_cmd_session_hdr *cmd_hdr;
  794. if (!offset || !buf_num)
  795. return 0;
  796. cmd_hdr = (struct cvp_hfi_cmd_session_hdr *)in_pkt;
  797. ktid = atomic64_inc_return(&inst->core->kernel_trans_id);
  798. ktid &= (FENCE_BIT - 1);
  799. cmd_hdr->client_data.kdata = ktid;
  800. frame = kmem_cache_zalloc(cvp_driver->frame_cache, GFP_KERNEL);
  801. if (!frame)
  802. return -ENOMEM;
  803. frame->ktid = ktid;
  804. frame->nr = 0;
  805. frame->pkt_type = cmd_hdr->packet_type;
  806. for (i = 0; i < buf_num; i++) {
  807. buf = (struct cvp_buf_type *)&in_pkt->pkt_data[offset];
  808. offset += sizeof(*buf) >> 2;
  809. if (buf->fd < 0 || !buf->size)
  810. continue;
  811. iova = msm_cvp_map_frame_buf(inst, buf, frame);
  812. if (!iova) {
  813. dprintk(CVP_ERR,
  814. "%s: buf %d register failed.\n",
  815. __func__, i);
  816. msm_cvp_unmap_frame_buf(inst, frame);
  817. return -EINVAL;
  818. }
  819. buf->fd = iova;
  820. }
  821. mutex_lock(&inst->frames.lock);
  822. list_add_tail(&frame->list, &inst->frames.list);
  823. mutex_unlock(&inst->frames.lock);
  824. dprintk(CVP_MEM, "%s: map frame %llu\n", __func__, ktid);
  825. return 0;
  826. }
  827. int msm_cvp_session_deinit_buffers(struct msm_cvp_inst *inst)
  828. {
  829. int rc = 0, i;
  830. struct cvp_internal_buf *cbuf, *dummy;
  831. struct msm_cvp_frame *frame, *dummy1;
  832. struct msm_cvp_smem *smem;
  833. struct cvp_hal_session *session;
  834. session = (struct cvp_hal_session *)inst->session;
  835. mutex_lock(&inst->frames.lock);
  836. list_for_each_entry_safe(frame, dummy1, &inst->frames.list, list) {
  837. list_del(&frame->list);
  838. msm_cvp_unmap_frame_buf(inst, frame);
  839. }
  840. mutex_unlock(&inst->frames.lock);
  841. mutex_lock(&inst->dma_cache.lock);
  842. for (i = 0; i < inst->dma_cache.nr; i++) {
  843. smem = inst->dma_cache.entries[i];
  844. if (atomic_read(&smem->refcount) == 0) {
  845. print_smem(CVP_MEM, "free", inst, smem);
  846. } else {
  847. print_smem(CVP_WARN, "in use", inst, smem);
  848. }
  849. msm_cvp_unmap_smem(inst, smem, "unmap cpu");
  850. msm_cvp_smem_put_dma_buf(smem->dma_buf);
  851. kmem_cache_free(cvp_driver->smem_cache, smem);
  852. inst->dma_cache.entries[i] = NULL;
  853. }
  854. mutex_unlock(&inst->dma_cache.lock);
  855. mutex_lock(&inst->cvpdspbufs.lock);
  856. list_for_each_entry_safe(cbuf, dummy, &inst->cvpdspbufs.list, list) {
  857. print_internal_buffer(CVP_MEM, "remove dspbufs", inst, cbuf);
  858. if (cbuf->ownership == CLIENT) {
  859. rc = cvp_dsp_deregister_buffer(hash32_ptr(session),
  860. cbuf->fd, cbuf->smem->dma_buf->size, cbuf->size,
  861. cbuf->offset, cbuf->index,
  862. (uint32_t)cbuf->smem->device_addr);
  863. if (rc)
  864. dprintk(CVP_ERR,
  865. "%s: failed dsp deregistration fd=%d rc=%d",
  866. __func__, cbuf->fd, rc);
  867. } else if (cbuf->ownership == DSP) {
  868. rc = cvp_dsp_fastrpc_unmap(inst->process_id, cbuf);
  869. if (rc)
  870. dprintk(CVP_ERR,
  871. "%s: failed to unmap buf from DSP\n",
  872. __func__);
  873. rc = cvp_release_dsp_buffers(inst, cbuf);
  874. if (rc)
  875. dprintk(CVP_ERR,
  876. "%s Fail to free buffer 0x%x\n",
  877. __func__, rc);
  878. }
  879. msm_cvp_unmap_smem(inst, cbuf->smem, "unmap dsp");
  880. msm_cvp_smem_put_dma_buf(cbuf->smem->dma_buf);
  881. list_del(&cbuf->list);
  882. kmem_cache_free(cvp_driver->buf_cache, cbuf);
  883. }
  884. mutex_unlock(&inst->cvpdspbufs.lock);
  885. return rc;
  886. }
  887. void msm_cvp_print_inst_bufs(struct msm_cvp_inst *inst)
  888. {
  889. struct cvp_internal_buf *buf;
  890. int i;
  891. if (!inst) {
  892. dprintk(CVP_ERR, "%s - invalid param %pK\n",
  893. __func__, inst);
  894. return;
  895. }
  896. dprintk(CVP_ERR, "active session cmd %d\n", inst->cur_cmd_type);
  897. dprintk(CVP_ERR,
  898. "---Buffer details for inst: %pK of type: %d---\n",
  899. inst, inst->session_type);
  900. mutex_lock(&inst->dma_cache.lock);
  901. dprintk(CVP_ERR, "dma cache:\n");
  902. if (inst->dma_cache.nr <= MAX_DMABUF_NUMS)
  903. for (i = 0; i < inst->dma_cache.nr; i++)
  904. print_smem(CVP_ERR, "bufdump", inst,
  905. inst->dma_cache.entries[i]);
  906. mutex_unlock(&inst->dma_cache.lock);
  907. mutex_lock(&inst->cvpdspbufs.lock);
  908. dprintk(CVP_ERR, "dsp buffer list:\n");
  909. list_for_each_entry(buf, &inst->cvpdspbufs.list, list)
  910. print_cvp_buffer(CVP_ERR, "bufdump", inst, buf);
  911. mutex_unlock(&inst->cvpdspbufs.lock);
  912. mutex_lock(&inst->persistbufs.lock);
  913. dprintk(CVP_ERR, "persist buffer list:\n");
  914. list_for_each_entry(buf, &inst->persistbufs.list, list)
  915. print_cvp_buffer(CVP_ERR, "bufdump", inst, buf);
  916. mutex_unlock(&inst->persistbufs.lock);
  917. }
  918. struct cvp_internal_buf *cvp_allocate_arp_bufs(struct msm_cvp_inst *inst,
  919. u32 buffer_size)
  920. {
  921. struct cvp_internal_buf *buf;
  922. struct msm_cvp_list *buf_list;
  923. u32 smem_flags = SMEM_UNCACHED;
  924. int rc = 0;
  925. if (!inst) {
  926. dprintk(CVP_ERR, "%s Invalid input\n", __func__);
  927. return NULL;
  928. }
  929. buf_list = &inst->persistbufs;
  930. if (!buffer_size)
  931. return NULL;
  932. /* PERSIST buffer requires secure mapping
  933. * Disable and wait for hyp_assign available
  934. */
  935. smem_flags |= SMEM_SECURE | SMEM_NON_PIXEL;
  936. buf = kmem_cache_zalloc(cvp_driver->buf_cache, GFP_KERNEL);
  937. if (!buf) {
  938. dprintk(CVP_ERR, "%s Out of memory\n", __func__);
  939. goto fail_kzalloc;
  940. }
  941. buf->smem = kmem_cache_zalloc(cvp_driver->smem_cache, GFP_KERNEL);
  942. if (!buf->smem) {
  943. dprintk(CVP_ERR, "%s Out of memory\n", __func__);
  944. goto fail_kzalloc;
  945. }
  946. buf->smem->flags = smem_flags;
  947. rc = msm_cvp_smem_alloc(buffer_size, 1, 0,
  948. &(inst->core->resources), buf->smem);
  949. if (rc) {
  950. dprintk(CVP_ERR, "Failed to allocate ARP memory\n");
  951. goto err_no_mem;
  952. }
  953. buf->size = buf->smem->size;
  954. buf->type = HFI_BUFFER_INTERNAL_PERSIST_1;
  955. buf->ownership = DRIVER;
  956. mutex_lock(&buf_list->lock);
  957. list_add_tail(&buf->list, &buf_list->list);
  958. mutex_unlock(&buf_list->lock);
  959. return buf;
  960. err_no_mem:
  961. kmem_cache_free(cvp_driver->buf_cache, buf);
  962. fail_kzalloc:
  963. return NULL;
  964. }
  965. int cvp_release_arp_buffers(struct msm_cvp_inst *inst)
  966. {
  967. struct msm_cvp_smem *smem;
  968. struct list_head *ptr, *next;
  969. struct cvp_internal_buf *buf;
  970. int rc = 0;
  971. struct msm_cvp_core *core;
  972. struct cvp_hfi_device *hdev;
  973. if (!inst) {
  974. dprintk(CVP_ERR, "Invalid instance pointer = %pK\n", inst);
  975. return -EINVAL;
  976. }
  977. core = inst->core;
  978. if (!core) {
  979. dprintk(CVP_ERR, "Invalid core pointer = %pK\n", core);
  980. return -EINVAL;
  981. }
  982. hdev = core->device;
  983. if (!hdev) {
  984. dprintk(CVP_ERR, "Invalid device pointer = %pK\n", hdev);
  985. return -EINVAL;
  986. }
  987. dprintk(CVP_MEM, "release persist buffer!\n");
  988. mutex_lock(&inst->persistbufs.lock);
  989. /* Workaround for FW: release buffer means release all */
  990. if (inst->state <= MSM_CVP_CLOSE_DONE) {
  991. rc = call_hfi_op(hdev, session_release_buffers,
  992. (void *)inst->session);
  993. if (!rc) {
  994. mutex_unlock(&inst->persistbufs.lock);
  995. rc = wait_for_sess_signal_receipt(inst,
  996. HAL_SESSION_RELEASE_BUFFER_DONE);
  997. if (rc)
  998. dprintk(CVP_WARN,
  999. "%s: wait for signal failed, rc %d\n",
  1000. __func__, rc);
  1001. mutex_lock(&inst->persistbufs.lock);
  1002. } else {
  1003. dprintk(CVP_WARN, "Fail to send Rel prst buf\n");
  1004. }
  1005. }
  1006. list_for_each_safe(ptr, next, &inst->persistbufs.list) {
  1007. buf = list_entry(ptr, struct cvp_internal_buf, list);
  1008. smem = buf->smem;
  1009. if (!smem) {
  1010. dprintk(CVP_ERR, "%s invalid smem\n", __func__);
  1011. mutex_unlock(&inst->persistbufs.lock);
  1012. return -EINVAL;
  1013. }
  1014. list_del(&buf->list);
  1015. if (buf->ownership == DRIVER) {
  1016. dprintk(CVP_MEM,
  1017. "%s: %x : fd %d %s size %d",
  1018. "free arp", hash32_ptr(inst->session), buf->fd,
  1019. smem->dma_buf->name, buf->size);
  1020. msm_cvp_smem_free(smem);
  1021. kmem_cache_free(cvp_driver->smem_cache, smem);
  1022. }
  1023. buf->smem = NULL;
  1024. kmem_cache_free(cvp_driver->buf_cache, buf);
  1025. }
  1026. mutex_unlock(&inst->persistbufs.lock);
  1027. return rc;
  1028. }
  1029. int cvp_allocate_dsp_bufs(struct msm_cvp_inst *inst,
  1030. struct cvp_internal_buf *buf,
  1031. u32 buffer_size,
  1032. u32 secure_type)
  1033. {
  1034. u32 smem_flags = SMEM_UNCACHED;
  1035. int rc = 0;
  1036. if (!inst) {
  1037. dprintk(CVP_ERR, "%s Invalid input\n", __func__);
  1038. return -EINVAL;
  1039. }
  1040. if (!buf)
  1041. return -EINVAL;
  1042. if (!buffer_size)
  1043. return -EINVAL;
  1044. switch (secure_type) {
  1045. case 0:
  1046. break;
  1047. case 1:
  1048. smem_flags |= SMEM_SECURE | SMEM_PIXEL;
  1049. break;
  1050. case 2:
  1051. smem_flags |= SMEM_SECURE | SMEM_NON_PIXEL;
  1052. break;
  1053. default:
  1054. dprintk(CVP_ERR, "%s Invalid secure_type %d\n",
  1055. __func__, secure_type);
  1056. return -EINVAL;
  1057. }
  1058. dprintk(CVP_MEM, "%s smem_flags 0x%x\n", __func__, smem_flags);
  1059. buf->smem = kmem_cache_zalloc(cvp_driver->smem_cache, GFP_KERNEL);
  1060. if (!buf->smem) {
  1061. dprintk(CVP_ERR, "%s Out of memory\n", __func__);
  1062. goto fail_kzalloc_smem_cache;
  1063. }
  1064. buf->smem->flags = smem_flags;
  1065. rc = msm_cvp_smem_alloc(buffer_size, 1, 0,
  1066. &(inst->core->resources), buf->smem);
  1067. if (rc) {
  1068. dprintk(CVP_ERR, "Failed to allocate ARP memory\n");
  1069. goto err_no_mem;
  1070. }
  1071. dprintk(CVP_MEM, "%s dma_buf %pK\n", __func__, buf->smem->dma_buf);
  1072. buf->size = buf->smem->size;
  1073. buf->type = HFI_BUFFER_INTERNAL_PERSIST_1;
  1074. buf->ownership = DSP;
  1075. return rc;
  1076. err_no_mem:
  1077. kmem_cache_free(cvp_driver->smem_cache, buf->smem);
  1078. fail_kzalloc_smem_cache:
  1079. return rc;
  1080. }
  1081. int cvp_release_dsp_buffers(struct msm_cvp_inst *inst,
  1082. struct cvp_internal_buf *buf)
  1083. {
  1084. struct msm_cvp_smem *smem;
  1085. int rc = 0;
  1086. if (!inst) {
  1087. dprintk(CVP_ERR, "Invalid instance pointer = %pK\n", inst);
  1088. return -EINVAL;
  1089. }
  1090. if (!buf) {
  1091. dprintk(CVP_ERR, "Invalid buffer pointer = %pK\n", inst);
  1092. return -EINVAL;
  1093. }
  1094. smem = buf->smem;
  1095. if (!smem) {
  1096. dprintk(CVP_ERR, "%s invalid smem\n", __func__);
  1097. return -EINVAL;
  1098. }
  1099. if (buf->ownership == DSP) {
  1100. dprintk(CVP_MEM,
  1101. "%s: %x : fd %x %s size %d",
  1102. __func__, hash32_ptr(inst->session), buf->fd,
  1103. smem->dma_buf->name, buf->size);
  1104. msm_cvp_smem_free(smem);
  1105. kmem_cache_free(cvp_driver->smem_cache, smem);
  1106. } else {
  1107. dprintk(CVP_ERR,
  1108. "%s: wrong owner %d %x : fd %x %s size %d",
  1109. __func__, buf->ownership, hash32_ptr(inst->session),
  1110. buf->fd, smem->dma_buf->name, buf->size);
  1111. }
  1112. return rc;
  1113. }