msm_cvp_buf.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  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;
  266. struct task_struct *task;
  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,
  857. list) {
  858. print_internal_buffer(CVP_MEM, "remove dspbufs", inst, cbuf);
  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. msm_cvp_unmap_smem(inst, cbuf->smem, "unmap dsp");
  868. msm_cvp_smem_put_dma_buf(cbuf->smem->dma_buf);
  869. list_del(&cbuf->list);
  870. kmem_cache_free(cvp_driver->buf_cache, cbuf);
  871. }
  872. mutex_unlock(&inst->cvpdspbufs.lock);
  873. return rc;
  874. }
  875. void msm_cvp_print_inst_bufs(struct msm_cvp_inst *inst)
  876. {
  877. struct cvp_internal_buf *buf;
  878. int i;
  879. if (!inst) {
  880. dprintk(CVP_ERR, "%s - invalid param %pK\n",
  881. __func__, inst);
  882. return;
  883. }
  884. dprintk(CVP_ERR, "active session cmd %d\n", inst->cur_cmd_type);
  885. dprintk(CVP_ERR,
  886. "---Buffer details for inst: %pK of type: %d---\n",
  887. inst, inst->session_type);
  888. mutex_lock(&inst->dma_cache.lock);
  889. dprintk(CVP_ERR, "dma cache:\n");
  890. if (inst->dma_cache.nr <= MAX_DMABUF_NUMS)
  891. for (i = 0; i < inst->dma_cache.nr; i++)
  892. print_smem(CVP_ERR, "bufdump", inst,
  893. inst->dma_cache.entries[i]);
  894. mutex_unlock(&inst->dma_cache.lock);
  895. mutex_lock(&inst->cvpdspbufs.lock);
  896. dprintk(CVP_ERR, "dsp buffer list:\n");
  897. list_for_each_entry(buf, &inst->cvpdspbufs.list, list)
  898. print_cvp_buffer(CVP_ERR, "bufdump", inst, buf);
  899. mutex_unlock(&inst->cvpdspbufs.lock);
  900. mutex_lock(&inst->persistbufs.lock);
  901. dprintk(CVP_ERR, "persist buffer list:\n");
  902. list_for_each_entry(buf, &inst->persistbufs.list, list)
  903. print_cvp_buffer(CVP_ERR, "bufdump", inst, buf);
  904. mutex_unlock(&inst->persistbufs.lock);
  905. }
  906. struct cvp_internal_buf *cvp_allocate_arp_bufs(struct msm_cvp_inst *inst,
  907. u32 buffer_size)
  908. {
  909. struct cvp_internal_buf *buf;
  910. struct msm_cvp_list *buf_list;
  911. u32 smem_flags = SMEM_UNCACHED;
  912. int rc = 0;
  913. if (!inst) {
  914. dprintk(CVP_ERR, "%s Invalid input\n", __func__);
  915. return NULL;
  916. }
  917. buf_list = &inst->persistbufs;
  918. if (!buffer_size)
  919. return NULL;
  920. /* PERSIST buffer requires secure mapping */
  921. /* Disable and wait for hyp_assign available
  922. * smem_flags |= SMEM_SECURE | SMEM_NON_PIXEL;
  923. */
  924. buf = kmem_cache_zalloc(cvp_driver->buf_cache, GFP_KERNEL);
  925. if (!buf) {
  926. dprintk(CVP_ERR, "%s Out of memory\n", __func__);
  927. goto fail_kzalloc;
  928. }
  929. buf->smem = kmem_cache_zalloc(cvp_driver->smem_cache, GFP_KERNEL);
  930. if (!buf->smem) {
  931. dprintk(CVP_ERR, "%s Out of memory\n", __func__);
  932. goto fail_kzalloc;
  933. }
  934. rc = msm_cvp_smem_alloc(buffer_size, 1, smem_flags, 0,
  935. &(inst->core->resources), buf->smem);
  936. if (rc) {
  937. dprintk(CVP_ERR, "Failed to allocate ARP memory\n");
  938. goto err_no_mem;
  939. }
  940. buf->size = buf->smem->size;
  941. buf->type = HFI_BUFFER_INTERNAL_PERSIST_1;
  942. buf->ownership = DRIVER;
  943. mutex_lock(&buf_list->lock);
  944. list_add_tail(&buf->list, &buf_list->list);
  945. mutex_unlock(&buf_list->lock);
  946. return buf;
  947. err_no_mem:
  948. kmem_cache_free(cvp_driver->buf_cache, buf);
  949. fail_kzalloc:
  950. return NULL;
  951. }
  952. int cvp_release_arp_buffers(struct msm_cvp_inst *inst)
  953. {
  954. struct msm_cvp_smem *smem;
  955. struct list_head *ptr, *next;
  956. struct cvp_internal_buf *buf;
  957. int rc = 0;
  958. struct msm_cvp_core *core;
  959. struct cvp_hfi_device *hdev;
  960. if (!inst) {
  961. dprintk(CVP_ERR, "Invalid instance pointer = %pK\n", inst);
  962. return -EINVAL;
  963. }
  964. core = inst->core;
  965. if (!core) {
  966. dprintk(CVP_ERR, "Invalid core pointer = %pK\n", core);
  967. return -EINVAL;
  968. }
  969. hdev = core->device;
  970. if (!hdev) {
  971. dprintk(CVP_ERR, "Invalid device pointer = %pK\n", hdev);
  972. return -EINVAL;
  973. }
  974. dprintk(CVP_MEM, "release persist buffer!\n");
  975. mutex_lock(&inst->persistbufs.lock);
  976. /* Workaround for FW: release buffer means release all */
  977. if (inst->state <= MSM_CVP_CLOSE_DONE) {
  978. rc = call_hfi_op(hdev, session_release_buffers,
  979. (void *)inst->session);
  980. if (!rc) {
  981. mutex_unlock(&inst->persistbufs.lock);
  982. rc = wait_for_sess_signal_receipt(inst,
  983. HAL_SESSION_RELEASE_BUFFER_DONE);
  984. if (rc)
  985. dprintk(CVP_WARN,
  986. "%s: wait for signal failed, rc %d\n",
  987. __func__, rc);
  988. mutex_lock(&inst->persistbufs.lock);
  989. } else {
  990. dprintk(CVP_WARN, "Fail to send Rel prst buf\n");
  991. }
  992. }
  993. list_for_each_safe(ptr, next, &inst->persistbufs.list) {
  994. buf = list_entry(ptr, struct cvp_internal_buf, list);
  995. smem = buf->smem;
  996. if (!smem) {
  997. dprintk(CVP_ERR, "%s invalid smem\n", __func__);
  998. mutex_unlock(&inst->persistbufs.lock);
  999. return -EINVAL;
  1000. }
  1001. list_del(&buf->list);
  1002. if (buf->ownership == DRIVER) {
  1003. dprintk(CVP_MEM,
  1004. "%s: %x : fd %d %s size %d",
  1005. "free arp", hash32_ptr(inst->session), buf->fd,
  1006. smem->dma_buf->name, buf->size);
  1007. msm_cvp_smem_free(smem);
  1008. kmem_cache_free(cvp_driver->smem_cache, smem);
  1009. }
  1010. buf->smem = NULL;
  1011. kmem_cache_free(cvp_driver->buf_cache, buf);
  1012. }
  1013. mutex_unlock(&inst->persistbufs.lock);
  1014. return rc;
  1015. }
  1016. int cvp_allocate_dsp_bufs(struct msm_cvp_inst *inst,
  1017. struct cvp_internal_buf *buf,
  1018. u32 buffer_size,
  1019. u32 secure_type)
  1020. {
  1021. u32 smem_flags = SMEM_UNCACHED;
  1022. int rc = 0;
  1023. if (!inst) {
  1024. dprintk(CVP_ERR, "%s Invalid input\n", __func__);
  1025. return -EINVAL;
  1026. }
  1027. if (!buf)
  1028. return -EINVAL;
  1029. if (!buffer_size)
  1030. return -EINVAL;
  1031. switch (secure_type) {
  1032. case 0:
  1033. break;
  1034. case 1:
  1035. smem_flags |= SMEM_SECURE | SMEM_PIXEL;
  1036. break;
  1037. case 2:
  1038. smem_flags |= SMEM_SECURE | SMEM_NON_PIXEL;
  1039. break;
  1040. default:
  1041. dprintk(CVP_ERR, "%s Invalid secure_type %d\n",
  1042. __func__, secure_type);
  1043. return -EINVAL;
  1044. }
  1045. dprintk(CVP_ERR, "%s smem_flags 0x%x\n", __func__, smem_flags);
  1046. buf->smem = kmem_cache_zalloc(cvp_driver->smem_cache, GFP_KERNEL);
  1047. if (!buf->smem) {
  1048. dprintk(CVP_ERR, "%s Out of memory\n", __func__);
  1049. goto fail_kzalloc_smem_cache;
  1050. }
  1051. rc = msm_cvp_smem_alloc(buffer_size, 1, smem_flags, 0,
  1052. &(inst->core->resources), buf->smem);
  1053. if (rc) {
  1054. dprintk(CVP_ERR, "Failed to allocate ARP memory\n");
  1055. goto err_no_mem;
  1056. }
  1057. dprintk(CVP_ERR, "%s dma_buf %pK\n", __func__, buf->smem->dma_buf);
  1058. buf->size = buf->smem->size;
  1059. buf->type = HFI_BUFFER_INTERNAL_PERSIST_1;
  1060. buf->ownership = CLIENT;
  1061. return rc;
  1062. err_no_mem:
  1063. kmem_cache_free(cvp_driver->smem_cache, buf->smem);
  1064. fail_kzalloc_smem_cache:
  1065. return rc;
  1066. }
  1067. int cvp_release_dsp_buffers(struct msm_cvp_inst *inst,
  1068. struct cvp_internal_buf *buf)
  1069. {
  1070. struct msm_cvp_smem *smem;
  1071. int rc = 0;
  1072. if (!inst) {
  1073. dprintk(CVP_ERR, "Invalid instance pointer = %pK\n", inst);
  1074. return -EINVAL;
  1075. }
  1076. if (!buf) {
  1077. dprintk(CVP_ERR, "Invalid buffer pointer = %pK\n", inst);
  1078. return -EINVAL;
  1079. }
  1080. smem = buf->smem;
  1081. if (!smem) {
  1082. dprintk(CVP_ERR, "%s invalid smem\n", __func__);
  1083. return -EINVAL;
  1084. }
  1085. if (buf->ownership == CLIENT) {
  1086. dprintk(CVP_MEM,
  1087. "%s: %x : fd %x %s size %d",
  1088. "free dsp buf", hash32_ptr(inst->session), buf->fd,
  1089. smem->dma_buf->name, buf->size);
  1090. msm_cvp_smem_free(smem);
  1091. kmem_cache_free(cvp_driver->smem_cache, smem);
  1092. }
  1093. return rc;
  1094. }