msm_cvp_buf.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020, The Linux Foundation. All rights reserved.
  4. */
  5. #include "msm_cvp_common.h"
  6. #include "cvp_hfi_api.h"
  7. #include "msm_cvp_debug.h"
  8. #include "msm_cvp_core.h"
  9. #include "msm_cvp_dsp.h"
  10. #define CLEAR_USE_BITMAP(idx, inst) \
  11. do { \
  12. clear_bit(idx, &inst->dma_cache.usage_bitmap); \
  13. dprintk(CVP_MEM, "clear %x bit %d dma_cache bitmap 0x%llx\n", \
  14. hash32_ptr(inst->session), smem->bitmap_index, \
  15. inst->dma_cache.usage_bitmap); \
  16. } while (0)
  17. #define SET_USE_BITMAP(idx, inst) \
  18. do { \
  19. set_bit(idx, &inst->dma_cache.usage_bitmap); \
  20. dprintk(CVP_MEM, "Set %x bit %d dma_cache bitmap 0x%llx\n", \
  21. hash32_ptr(inst->session), idx, \
  22. inst->dma_cache.usage_bitmap); \
  23. } while (0)
  24. void print_smem(u32 tag, const char *str, struct msm_cvp_inst *inst,
  25. struct msm_cvp_smem *smem)
  26. {
  27. if (!(tag & msm_cvp_debug) || !inst || !smem)
  28. return;
  29. if (smem->dma_buf) {
  30. dprintk(tag,
  31. "%s: %x : %s size %d flags %#x iova %#x idx %d ref %d",
  32. str, hash32_ptr(inst->session), smem->dma_buf->name,
  33. smem->size, smem->flags, smem->device_addr,
  34. smem->bitmap_index, smem->refcount);
  35. }
  36. }
  37. static void print_internal_buffer(u32 tag, const char *str,
  38. struct msm_cvp_inst *inst, struct cvp_internal_buf *cbuf)
  39. {
  40. if (!(tag & msm_cvp_debug) || !inst || !cbuf)
  41. return;
  42. if (cbuf->smem->dma_buf) {
  43. dprintk(tag,
  44. "%s: %x : fd %d off %d %s size %d iova %#x",
  45. str, hash32_ptr(inst->session), cbuf->fd,
  46. cbuf->offset, cbuf->smem->dma_buf->name, cbuf->size,
  47. cbuf->smem->device_addr);
  48. } else {
  49. dprintk(tag,
  50. "%s: %x : idx %2d fd %d off %d size %d iova %#x",
  51. str, hash32_ptr(inst->session), cbuf->fd,
  52. cbuf->offset, cbuf->size, cbuf->smem->device_addr);
  53. }
  54. }
  55. void print_cvp_buffer(u32 tag, const char *str, struct msm_cvp_inst *inst,
  56. struct cvp_internal_buf *cbuf)
  57. {
  58. dprintk(tag, "%s addr: %x size %u\n", str,
  59. cbuf->smem->device_addr, cbuf->size);
  60. }
  61. void print_client_buffer(u32 tag, const char *str,
  62. struct msm_cvp_inst *inst, struct eva_kmd_buffer *cbuf)
  63. {
  64. if (!(tag & msm_cvp_debug) || !inst || !cbuf)
  65. return;
  66. dprintk(tag,
  67. "%s: %x : idx %2d fd %d off %d size %d type %d flags 0x%x\n",
  68. str, hash32_ptr(inst->session), cbuf->index, cbuf->fd,
  69. cbuf->offset, cbuf->size, cbuf->type, cbuf->flags);
  70. }
  71. int msm_cvp_map_buf_dsp(struct msm_cvp_inst *inst, struct eva_kmd_buffer *buf)
  72. {
  73. int rc = 0;
  74. bool found = false;
  75. struct cvp_internal_buf *cbuf;
  76. struct msm_cvp_smem *smem = NULL;
  77. struct cvp_hal_session *session;
  78. struct dma_buf *dma_buf = NULL;
  79. if (!inst || !inst->core || !buf) {
  80. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  81. return -EINVAL;
  82. }
  83. if (buf->fd < 0) {
  84. dprintk(CVP_ERR, "%s: Invalid fd = %d", __func__, buf->fd);
  85. return 0;
  86. }
  87. if (buf->offset) {
  88. dprintk(CVP_ERR,
  89. "%s: offset is deprecated, set to 0.\n",
  90. __func__);
  91. return -EINVAL;
  92. }
  93. session = (struct cvp_hal_session *)inst->session;
  94. mutex_lock(&inst->cvpdspbufs.lock);
  95. list_for_each_entry(cbuf, &inst->cvpdspbufs.list, list) {
  96. if (cbuf->fd == buf->fd) {
  97. if (cbuf->size != buf->size) {
  98. dprintk(CVP_ERR, "%s: buf size mismatch\n",
  99. __func__);
  100. mutex_unlock(&inst->cvpdspbufs.lock);
  101. return -EINVAL;
  102. }
  103. found = true;
  104. break;
  105. }
  106. }
  107. mutex_unlock(&inst->cvpdspbufs.lock);
  108. if (found) {
  109. print_internal_buffer(CVP_ERR, "duplicate", inst, cbuf);
  110. return -EINVAL;
  111. }
  112. dma_buf = msm_cvp_smem_get_dma_buf(buf->fd);
  113. if (!dma_buf) {
  114. dprintk(CVP_ERR, "%s: Invalid fd = %d", __func__, buf->fd);
  115. return 0;
  116. }
  117. cbuf = kmem_cache_zalloc(cvp_driver->buf_cache, GFP_KERNEL);
  118. if (!cbuf)
  119. return -ENOMEM;
  120. smem = kmem_cache_zalloc(cvp_driver->smem_cache, GFP_KERNEL);
  121. if (!smem) {
  122. kmem_cache_free(cvp_driver->buf_cache, cbuf);
  123. return -ENOMEM;
  124. }
  125. smem->dma_buf = dma_buf;
  126. smem->bitmap_index = MAX_DMABUF_NUMS;
  127. dprintk(CVP_MEM, "%s: dma_buf = %llx\n", __func__, dma_buf);
  128. rc = msm_cvp_map_smem(inst, smem, "map dsp");
  129. if (rc) {
  130. print_client_buffer(CVP_ERR, "map failed", inst, buf);
  131. goto exit;
  132. }
  133. if (buf->index) {
  134. rc = cvp_dsp_register_buffer(hash32_ptr(session), buf->fd,
  135. smem->dma_buf->size, buf->size, buf->offset,
  136. buf->index, (uint32_t)smem->device_addr);
  137. if (rc) {
  138. dprintk(CVP_ERR,
  139. "%s: failed dsp registration for fd=%d rc=%d",
  140. __func__, buf->fd, rc);
  141. goto exit;
  142. }
  143. } else {
  144. dprintk(CVP_ERR, "%s: buf index is 0 fd=%d", __func__, buf->fd);
  145. rc = -EINVAL;
  146. goto exit;
  147. }
  148. cbuf->smem = smem;
  149. cbuf->fd = buf->fd;
  150. cbuf->size = buf->size;
  151. cbuf->offset = buf->offset;
  152. cbuf->ownership = CLIENT;
  153. cbuf->index = buf->index;
  154. mutex_lock(&inst->cvpdspbufs.lock);
  155. list_add_tail(&cbuf->list, &inst->cvpdspbufs.list);
  156. mutex_unlock(&inst->cvpdspbufs.lock);
  157. return rc;
  158. exit:
  159. if (smem->device_addr) {
  160. msm_cvp_unmap_smem(inst, smem, "unmap dsp");
  161. msm_cvp_smem_put_dma_buf(smem->dma_buf);
  162. }
  163. kmem_cache_free(cvp_driver->buf_cache, cbuf);
  164. cbuf = NULL;
  165. kmem_cache_free(cvp_driver->smem_cache, smem);
  166. smem = NULL;
  167. return rc;
  168. }
  169. int msm_cvp_unmap_buf_dsp(struct msm_cvp_inst *inst, struct eva_kmd_buffer *buf)
  170. {
  171. int rc = 0;
  172. bool found;
  173. struct cvp_internal_buf *cbuf;
  174. struct cvp_hal_session *session;
  175. if (!inst || !inst->core || !buf) {
  176. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  177. return -EINVAL;
  178. }
  179. session = (struct cvp_hal_session *)inst->session;
  180. if (!session) {
  181. dprintk(CVP_ERR, "%s: invalid session\n", __func__);
  182. return -EINVAL;
  183. }
  184. mutex_lock(&inst->cvpdspbufs.lock);
  185. found = false;
  186. list_for_each_entry(cbuf, &inst->cvpdspbufs.list, list) {
  187. if (cbuf->fd == buf->fd) {
  188. found = true;
  189. break;
  190. }
  191. }
  192. mutex_unlock(&inst->cvpdspbufs.lock);
  193. if (!found) {
  194. print_client_buffer(CVP_ERR, "invalid", inst, buf);
  195. return -EINVAL;
  196. }
  197. if (buf->index) {
  198. rc = cvp_dsp_deregister_buffer(hash32_ptr(session), buf->fd,
  199. cbuf->smem->dma_buf->size, buf->size, buf->offset,
  200. buf->index, (uint32_t)cbuf->smem->device_addr);
  201. if (rc) {
  202. dprintk(CVP_ERR,
  203. "%s: failed dsp deregistration fd=%d rc=%d",
  204. __func__, buf->fd, rc);
  205. return rc;
  206. }
  207. }
  208. if (cbuf->smem->device_addr) {
  209. msm_cvp_unmap_smem(inst, cbuf->smem, "unmap dsp");
  210. msm_cvp_smem_put_dma_buf(cbuf->smem->dma_buf);
  211. }
  212. mutex_lock(&inst->cvpdspbufs.lock);
  213. list_del(&cbuf->list);
  214. mutex_unlock(&inst->cvpdspbufs.lock);
  215. kmem_cache_free(cvp_driver->smem_cache, cbuf->smem);
  216. kmem_cache_free(cvp_driver->buf_cache, cbuf);
  217. return rc;
  218. }
  219. void msm_cvp_cache_operations(struct msm_cvp_smem *smem, u32 type,
  220. u32 offset, u32 size)
  221. {
  222. enum smem_cache_ops cache_op;
  223. if (msm_cvp_cacheop_disabled)
  224. return;
  225. if (!smem) {
  226. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  227. return;
  228. }
  229. switch (type) {
  230. case EVA_KMD_BUFTYPE_INPUT:
  231. cache_op = SMEM_CACHE_CLEAN;
  232. break;
  233. case EVA_KMD_BUFTYPE_OUTPUT:
  234. cache_op = SMEM_CACHE_INVALIDATE;
  235. break;
  236. default:
  237. cache_op = SMEM_CACHE_CLEAN_INVALIDATE;
  238. }
  239. dprintk(CVP_MEM,
  240. "%s: cache operation enabled for dma_buf: %llx, cache_op: %d, offset: %d, size: %d\n",
  241. __func__, smem->dma_buf, cache_op, offset, size);
  242. msm_cvp_smem_cache_operations(smem->dma_buf, cache_op, offset, size);
  243. }
  244. static struct msm_cvp_smem *msm_cvp_session_find_smem(struct msm_cvp_inst *inst,
  245. struct dma_buf *dma_buf)
  246. {
  247. struct msm_cvp_smem *smem;
  248. int i;
  249. if (inst->dma_cache.nr > MAX_DMABUF_NUMS)
  250. return NULL;
  251. mutex_lock(&inst->dma_cache.lock);
  252. for (i = 0; i < inst->dma_cache.nr; i++)
  253. if (inst->dma_cache.entries[i]->dma_buf == dma_buf) {
  254. SET_USE_BITMAP(i, inst);
  255. smem = inst->dma_cache.entries[i];
  256. smem->bitmap_index = i;
  257. atomic_inc(&smem->refcount);
  258. /*
  259. * If we find it, it means we already increased
  260. * refcount before, so we put it to avoid double
  261. * incremental.
  262. */
  263. msm_cvp_smem_put_dma_buf(smem->dma_buf);
  264. mutex_unlock(&inst->dma_cache.lock);
  265. print_smem(CVP_MEM, "found", inst, smem);
  266. return smem;
  267. }
  268. mutex_unlock(&inst->dma_cache.lock);
  269. return NULL;
  270. }
  271. static int msm_cvp_session_add_smem(struct msm_cvp_inst *inst,
  272. struct msm_cvp_smem *smem)
  273. {
  274. unsigned int i;
  275. struct msm_cvp_smem *smem2;
  276. mutex_lock(&inst->dma_cache.lock);
  277. if (inst->dma_cache.nr < MAX_DMABUF_NUMS) {
  278. inst->dma_cache.entries[inst->dma_cache.nr] = smem;
  279. SET_USE_BITMAP(inst->dma_cache.nr, inst);
  280. smem->bitmap_index = inst->dma_cache.nr;
  281. inst->dma_cache.nr++;
  282. i = smem->bitmap_index;
  283. } else {
  284. i = find_first_zero_bit(&inst->dma_cache.usage_bitmap,
  285. MAX_DMABUF_NUMS);
  286. if (i < MAX_DMABUF_NUMS) {
  287. smem2 = inst->dma_cache.entries[i];
  288. msm_cvp_unmap_smem(inst, smem2, "unmap cpu");
  289. msm_cvp_smem_put_dma_buf(smem2->dma_buf);
  290. kmem_cache_free(cvp_driver->smem_cache, smem2);
  291. inst->dma_cache.entries[i] = smem;
  292. smem->bitmap_index = i;
  293. SET_USE_BITMAP(i, inst);
  294. } else {
  295. dprintk(CVP_WARN, "%s: not enough memory\n", __func__);
  296. mutex_unlock(&inst->dma_cache.lock);
  297. return -ENOMEM;
  298. }
  299. }
  300. atomic_inc(&smem->refcount);
  301. mutex_unlock(&inst->dma_cache.lock);
  302. dprintk(CVP_MEM, "Add entry %d into cache\n", i);
  303. return 0;
  304. }
  305. static struct msm_cvp_smem *msm_cvp_session_get_smem(struct msm_cvp_inst *inst,
  306. struct cvp_buf_type *buf)
  307. {
  308. int rc = 0, found = 1;
  309. struct msm_cvp_smem *smem = NULL;
  310. struct dma_buf *dma_buf = NULL;
  311. if (buf->fd < 0) {
  312. dprintk(CVP_ERR, "%s: Invalid fd = %d", __func__, buf->fd);
  313. return NULL;
  314. }
  315. dma_buf = msm_cvp_smem_get_dma_buf(buf->fd);
  316. if (!dma_buf) {
  317. dprintk(CVP_ERR, "%s: Invalid fd = %d", __func__, buf->fd);
  318. return NULL;
  319. }
  320. smem = msm_cvp_session_find_smem(inst, dma_buf);
  321. if (!smem) {
  322. found = 0;
  323. smem = kmem_cache_zalloc(cvp_driver->smem_cache, GFP_KERNEL);
  324. if (!smem)
  325. return NULL;
  326. smem->dma_buf = dma_buf;
  327. smem->bitmap_index = MAX_DMABUF_NUMS;
  328. rc = msm_cvp_map_smem(inst, smem, "map cpu");
  329. if (rc)
  330. goto exit;
  331. rc = msm_cvp_session_add_smem(inst, smem);
  332. if (rc && rc != -ENOMEM)
  333. goto exit2;
  334. }
  335. if (buf->size > smem->size || buf->size > smem->size - buf->offset) {
  336. dprintk(CVP_ERR, "%s: invalid offset %d or size %d\n",
  337. __func__, buf->offset, buf->size);
  338. if (found) {
  339. mutex_lock(&inst->dma_cache.lock);
  340. atomic_dec(&smem->refcount);
  341. mutex_unlock(&inst->dma_cache.lock);
  342. return NULL;
  343. }
  344. goto exit2;
  345. }
  346. return smem;
  347. exit2:
  348. msm_cvp_unmap_smem(inst, smem, "unmap cpu");
  349. exit:
  350. msm_cvp_smem_put_dma_buf(dma_buf);
  351. kmem_cache_free(cvp_driver->smem_cache, smem);
  352. smem = NULL;
  353. return smem;
  354. }
  355. static u32 msm_cvp_map_user_persist_buf(struct msm_cvp_inst *inst,
  356. struct cvp_buf_type *buf)
  357. {
  358. u32 iova = 0;
  359. struct msm_cvp_smem *smem = NULL;
  360. struct cvp_internal_buf *pbuf;
  361. if (!inst) {
  362. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  363. return -EINVAL;
  364. }
  365. pbuf = kmem_cache_zalloc(cvp_driver->buf_cache, GFP_KERNEL);
  366. if (!pbuf)
  367. return 0;
  368. smem = msm_cvp_session_get_smem(inst, buf);
  369. if (!smem)
  370. goto exit;
  371. pbuf->smem = smem;
  372. pbuf->fd = buf->fd;
  373. pbuf->size = buf->size;
  374. pbuf->offset = buf->offset;
  375. pbuf->ownership = CLIENT;
  376. mutex_lock(&inst->persistbufs.lock);
  377. list_add_tail(&pbuf->list, &inst->persistbufs.list);
  378. mutex_unlock(&inst->persistbufs.lock);
  379. print_internal_buffer(CVP_MEM, "map persist", inst, pbuf);
  380. iova = smem->device_addr + buf->offset;
  381. return iova;
  382. exit:
  383. kmem_cache_free(cvp_driver->buf_cache, pbuf);
  384. return 0;
  385. }
  386. u32 msm_cvp_map_frame_buf(struct msm_cvp_inst *inst,
  387. struct cvp_buf_type *buf,
  388. struct msm_cvp_frame *frame)
  389. {
  390. u32 iova = 0;
  391. struct msm_cvp_smem *smem = NULL;
  392. u32 nr;
  393. u32 type;
  394. if (!inst || !frame) {
  395. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  396. return 0;
  397. }
  398. nr = frame->nr;
  399. if (nr == MAX_FRAME_BUFFER_NUMS) {
  400. dprintk(CVP_ERR, "%s: max frame buffer reached\n", __func__);
  401. return 0;
  402. }
  403. smem = msm_cvp_session_get_smem(inst, buf);
  404. if (!smem)
  405. return 0;
  406. frame->bufs[nr].fd = buf->fd;
  407. frame->bufs[nr].smem = smem;
  408. frame->bufs[nr].size = buf->size;
  409. frame->bufs[nr].offset = buf->offset;
  410. print_internal_buffer(CVP_MEM, "map cpu", inst, &frame->bufs[nr]);
  411. frame->nr++;
  412. type = EVA_KMD_BUFTYPE_INPUT | EVA_KMD_BUFTYPE_OUTPUT;
  413. msm_cvp_cache_operations(smem, type, buf->offset, buf->size);
  414. iova = smem->device_addr + buf->offset;
  415. return iova;
  416. }
  417. static void msm_cvp_unmap_frame_buf(struct msm_cvp_inst *inst,
  418. struct msm_cvp_frame *frame)
  419. {
  420. u32 i;
  421. u32 type;
  422. struct msm_cvp_smem *smem = NULL;
  423. struct cvp_internal_buf *buf;
  424. type = EVA_KMD_BUFTYPE_OUTPUT;
  425. for (i = 0; i < frame->nr; ++i) {
  426. buf = &frame->bufs[i];
  427. smem = buf->smem;
  428. msm_cvp_cache_operations(smem, type, buf->offset, buf->size);
  429. if (smem->bitmap_index >= MAX_DMABUF_NUMS) {
  430. /* smem not in dmamap cache */
  431. msm_cvp_unmap_smem(inst, smem, "unmap cpu");
  432. dma_heap_buffer_free(smem->dma_buf);
  433. kmem_cache_free(cvp_driver->smem_cache, smem);
  434. buf->smem = NULL;
  435. } else {
  436. mutex_lock(&inst->dma_cache.lock);
  437. if (atomic_dec_and_test(&smem->refcount)) {
  438. CLEAR_USE_BITMAP(smem->bitmap_index, inst);
  439. print_smem(CVP_MEM, "Map dereference",
  440. inst, smem);
  441. }
  442. mutex_unlock(&inst->dma_cache.lock);
  443. }
  444. }
  445. kmem_cache_free(cvp_driver->frame_cache, frame);
  446. }
  447. void msm_cvp_unmap_frame(struct msm_cvp_inst *inst, u64 ktid)
  448. {
  449. struct msm_cvp_frame *frame, *dummy1;
  450. bool found;
  451. if (!inst) {
  452. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  453. return;
  454. }
  455. ktid &= (FENCE_BIT - 1);
  456. dprintk(CVP_MEM, "%s: (%#x) unmap frame %llu\n",
  457. __func__, hash32_ptr(inst->session), ktid);
  458. found = false;
  459. mutex_lock(&inst->frames.lock);
  460. list_for_each_entry_safe(frame, dummy1, &inst->frames.list, list) {
  461. if (frame->ktid == ktid) {
  462. found = true;
  463. list_del(&frame->list);
  464. break;
  465. }
  466. }
  467. mutex_unlock(&inst->frames.lock);
  468. if (found)
  469. msm_cvp_unmap_frame_buf(inst, frame);
  470. else
  471. dprintk(CVP_WARN, "%s frame %llu not found!\n", __func__, ktid);
  472. }
  473. int msm_cvp_unmap_user_persist(struct msm_cvp_inst *inst,
  474. struct eva_kmd_hfi_packet *in_pkt,
  475. unsigned int offset, unsigned int buf_num)
  476. {
  477. struct cvp_hfi_cmd_session_hdr *cmd_hdr;
  478. struct cvp_internal_buf *pbuf, *dummy;
  479. u64 ktid;
  480. int rc = 0;
  481. struct msm_cvp_smem *smem = NULL;
  482. if (!offset || !buf_num)
  483. return rc;
  484. cmd_hdr = (struct cvp_hfi_cmd_session_hdr *)in_pkt;
  485. ktid = cmd_hdr->client_data.kdata & (FENCE_BIT - 1);
  486. mutex_lock(&inst->persistbufs.lock);
  487. list_for_each_entry_safe(pbuf, dummy, &inst->persistbufs.list, list) {
  488. if (pbuf->ktid == ktid && pbuf->ownership == CLIENT) {
  489. list_del(&pbuf->list);
  490. smem = pbuf->smem;
  491. dprintk(CVP_MEM, "unmap persist: %x %d %d %#x",
  492. hash32_ptr(inst->session), pbuf->fd,
  493. pbuf->size, smem->device_addr);
  494. if (smem->bitmap_index >= MAX_DMABUF_NUMS) {
  495. /* smem not in dmamap cache */
  496. msm_cvp_unmap_smem(inst, smem,
  497. "unmap cpu");
  498. dma_heap_buffer_free(smem->dma_buf);
  499. kmem_cache_free(
  500. cvp_driver->smem_cache,
  501. smem);
  502. pbuf->smem = NULL;
  503. } else {
  504. mutex_lock(&inst->dma_cache.lock);
  505. if (atomic_dec_and_test(&smem->refcount))
  506. CLEAR_USE_BITMAP(
  507. smem->bitmap_index,
  508. inst);
  509. mutex_unlock(&inst->dma_cache.lock);
  510. }
  511. kmem_cache_free(cvp_driver->buf_cache, pbuf);
  512. }
  513. }
  514. mutex_unlock(&inst->persistbufs.lock);
  515. return rc;
  516. }
  517. int msm_cvp_mark_user_persist(struct msm_cvp_inst *inst,
  518. struct eva_kmd_hfi_packet *in_pkt,
  519. unsigned int offset, unsigned int buf_num)
  520. {
  521. struct cvp_hfi_cmd_session_hdr *cmd_hdr;
  522. struct cvp_internal_buf *pbuf, *dummy;
  523. u64 ktid;
  524. struct cvp_buf_type *buf;
  525. int i, rc = 0;
  526. if (!offset || !buf_num)
  527. return 0;
  528. cmd_hdr = (struct cvp_hfi_cmd_session_hdr *)in_pkt;
  529. ktid = atomic64_inc_return(&inst->core->kernel_trans_id);
  530. ktid &= (FENCE_BIT - 1);
  531. cmd_hdr->client_data.kdata = ktid;
  532. for (i = 0; i < buf_num; i++) {
  533. buf = (struct cvp_buf_type *)&in_pkt->pkt_data[offset];
  534. offset += sizeof(*buf) >> 2;
  535. if (buf->fd < 0 || !buf->size)
  536. continue;
  537. mutex_lock(&inst->persistbufs.lock);
  538. list_for_each_entry_safe(pbuf, dummy, &inst->persistbufs.list,
  539. list) {
  540. if (pbuf->ownership == CLIENT) {
  541. if (pbuf->fd == buf->fd &&
  542. pbuf->size == buf->size)
  543. buf->fd = pbuf->smem->device_addr;
  544. rc = 1;
  545. break;
  546. }
  547. }
  548. mutex_unlock(&inst->persistbufs.lock);
  549. if (!rc) {
  550. dprintk(CVP_ERR, "%s No persist buf %d found\n",
  551. __func__, buf->fd);
  552. rc = -EFAULT;
  553. break;
  554. }
  555. pbuf->ktid = ktid;
  556. rc = 0;
  557. }
  558. return rc;
  559. }
  560. int msm_cvp_map_user_persist(struct msm_cvp_inst *inst,
  561. struct eva_kmd_hfi_packet *in_pkt,
  562. unsigned int offset, unsigned int buf_num)
  563. {
  564. struct cvp_buf_type *buf;
  565. int i;
  566. u32 iova;
  567. if (!offset || !buf_num)
  568. return 0;
  569. for (i = 0; i < buf_num; i++) {
  570. buf = (struct cvp_buf_type *)&in_pkt->pkt_data[offset];
  571. offset += sizeof(*buf) >> 2;
  572. if (buf->fd < 0 || !buf->size)
  573. continue;
  574. iova = msm_cvp_map_user_persist_buf(inst, buf);
  575. if (!iova) {
  576. dprintk(CVP_ERR,
  577. "%s: buf %d register failed.\n",
  578. __func__, i);
  579. return -EINVAL;
  580. }
  581. buf->fd = iova;
  582. }
  583. return 0;
  584. }
  585. int msm_cvp_map_frame(struct msm_cvp_inst *inst,
  586. struct eva_kmd_hfi_packet *in_pkt,
  587. unsigned int offset, unsigned int buf_num)
  588. {
  589. struct cvp_buf_type *buf;
  590. int i;
  591. u32 iova;
  592. u64 ktid;
  593. struct msm_cvp_frame *frame;
  594. struct cvp_hfi_cmd_session_hdr *cmd_hdr;
  595. if (!offset || !buf_num)
  596. return 0;
  597. cmd_hdr = (struct cvp_hfi_cmd_session_hdr *)in_pkt;
  598. ktid = atomic64_inc_return(&inst->core->kernel_trans_id);
  599. ktid &= (FENCE_BIT - 1);
  600. cmd_hdr->client_data.kdata = ktid;
  601. frame = kmem_cache_zalloc(cvp_driver->frame_cache, GFP_KERNEL);
  602. if (!frame)
  603. return -ENOMEM;
  604. frame->ktid = ktid;
  605. frame->nr = 0;
  606. frame->pkt_type = cmd_hdr->packet_type;
  607. for (i = 0; i < buf_num; i++) {
  608. buf = (struct cvp_buf_type *)&in_pkt->pkt_data[offset];
  609. offset += sizeof(*buf) >> 2;
  610. if (buf->fd < 0 || !buf->size)
  611. continue;
  612. iova = msm_cvp_map_frame_buf(inst, buf, frame);
  613. if (!iova) {
  614. dprintk(CVP_ERR,
  615. "%s: buf %d register failed.\n",
  616. __func__, i);
  617. msm_cvp_unmap_frame_buf(inst, frame);
  618. return -EINVAL;
  619. }
  620. buf->fd = iova;
  621. }
  622. mutex_lock(&inst->frames.lock);
  623. list_add_tail(&frame->list, &inst->frames.list);
  624. mutex_unlock(&inst->frames.lock);
  625. dprintk(CVP_MEM, "%s: map frame %llu\n", __func__, ktid);
  626. return 0;
  627. }
  628. int msm_cvp_session_deinit_buffers(struct msm_cvp_inst *inst)
  629. {
  630. int rc = 0, i;
  631. struct cvp_internal_buf *cbuf, *dummy;
  632. struct msm_cvp_frame *frame, *dummy1;
  633. struct msm_cvp_smem *smem;
  634. struct cvp_hal_session *session;
  635. session = (struct cvp_hal_session *)inst->session;
  636. mutex_lock(&inst->frames.lock);
  637. list_for_each_entry_safe(frame, dummy1, &inst->frames.list, list) {
  638. list_del(&frame->list);
  639. msm_cvp_unmap_frame_buf(inst, frame);
  640. }
  641. mutex_unlock(&inst->frames.lock);
  642. mutex_lock(&inst->dma_cache.lock);
  643. for (i = 0; i < inst->dma_cache.nr; i++) {
  644. smem = inst->dma_cache.entries[i];
  645. if (atomic_read(&smem->refcount) == 0) {
  646. print_smem(CVP_MEM, "free", inst, smem);
  647. } else {
  648. print_smem(CVP_WARN, "in use", inst, smem);
  649. }
  650. msm_cvp_unmap_smem(inst, smem, "unmap cpu");
  651. msm_cvp_smem_put_dma_buf(smem->dma_buf);
  652. kmem_cache_free(cvp_driver->smem_cache, smem);
  653. inst->dma_cache.entries[i] = NULL;
  654. }
  655. mutex_unlock(&inst->dma_cache.lock);
  656. mutex_lock(&inst->cvpdspbufs.lock);
  657. list_for_each_entry_safe(cbuf, dummy, &inst->cvpdspbufs.list,
  658. list) {
  659. print_internal_buffer(CVP_MEM, "remove dspbufs", inst, cbuf);
  660. rc = cvp_dsp_deregister_buffer(hash32_ptr(session),
  661. cbuf->fd, cbuf->smem->dma_buf->size, cbuf->size,
  662. cbuf->offset, cbuf->index,
  663. (uint32_t)cbuf->smem->device_addr);
  664. if (rc)
  665. dprintk(CVP_ERR,
  666. "%s: failed dsp deregistration fd=%d rc=%d",
  667. __func__, cbuf->fd, rc);
  668. msm_cvp_unmap_smem(inst, cbuf->smem, "unmap dsp");
  669. msm_cvp_smem_put_dma_buf(cbuf->smem->dma_buf);
  670. list_del(&cbuf->list);
  671. kmem_cache_free(cvp_driver->buf_cache, cbuf);
  672. }
  673. mutex_unlock(&inst->cvpdspbufs.lock);
  674. return rc;
  675. }
  676. void msm_cvp_print_inst_bufs(struct msm_cvp_inst *inst)
  677. {
  678. struct cvp_internal_buf *buf;
  679. int i;
  680. if (!inst) {
  681. dprintk(CVP_ERR, "%s - invalid param %pK\n",
  682. __func__, inst);
  683. return;
  684. }
  685. dprintk(CVP_ERR, "active session cmd %d\n", inst->cur_cmd_type);
  686. dprintk(CVP_ERR,
  687. "---Buffer details for inst: %pK of type: %d---\n",
  688. inst, inst->session_type);
  689. mutex_lock(&inst->dma_cache.lock);
  690. dprintk(CVP_ERR, "dma cache:\n");
  691. if (inst->dma_cache.nr <= MAX_DMABUF_NUMS)
  692. for (i = 0; i < inst->dma_cache.nr; i++)
  693. print_smem(CVP_ERR, "bufdump", inst,
  694. inst->dma_cache.entries[i]);
  695. mutex_unlock(&inst->dma_cache.lock);
  696. mutex_lock(&inst->cvpdspbufs.lock);
  697. dprintk(CVP_ERR, "dsp buffer list:\n");
  698. list_for_each_entry(buf, &inst->cvpdspbufs.list, list)
  699. print_cvp_buffer(CVP_ERR, "bufdump", inst, buf);
  700. mutex_unlock(&inst->cvpdspbufs.lock);
  701. mutex_lock(&inst->persistbufs.lock);
  702. dprintk(CVP_ERR, "persist buffer list:\n");
  703. list_for_each_entry(buf, &inst->persistbufs.list, list)
  704. print_cvp_buffer(CVP_ERR, "bufdump", inst, buf);
  705. mutex_unlock(&inst->persistbufs.lock);
  706. }
  707. struct cvp_internal_buf *cvp_allocate_arp_bufs(struct msm_cvp_inst *inst,
  708. u32 buffer_size)
  709. {
  710. struct cvp_internal_buf *buf;
  711. struct msm_cvp_list *buf_list;
  712. u32 smem_flags = SMEM_UNCACHED;
  713. int rc = 0;
  714. if (!inst) {
  715. dprintk(CVP_ERR, "%s Invalid input\n", __func__);
  716. return NULL;
  717. }
  718. buf_list = &inst->persistbufs;
  719. if (!buffer_size)
  720. return NULL;
  721. /* PERSIST buffer requires secure mapping */
  722. /* Disable and wait for hyp_assign available
  723. * smem_flags |= SMEM_SECURE | SMEM_NON_PIXEL;
  724. */
  725. buf = kmem_cache_zalloc(cvp_driver->buf_cache, GFP_KERNEL);
  726. if (!buf) {
  727. dprintk(CVP_ERR, "%s Out of memory\n", __func__);
  728. goto fail_kzalloc;
  729. }
  730. buf->smem = kmem_cache_zalloc(cvp_driver->smem_cache, GFP_KERNEL);
  731. if (!buf->smem) {
  732. dprintk(CVP_ERR, "%s Out of memory\n", __func__);
  733. goto fail_kzalloc;
  734. }
  735. rc = msm_cvp_smem_alloc(buffer_size, 1, smem_flags, 0,
  736. &(inst->core->resources), buf->smem);
  737. if (rc) {
  738. dprintk(CVP_ERR, "Failed to allocate ARP memory\n");
  739. goto err_no_mem;
  740. }
  741. buf->size = buf->smem->size;
  742. buf->type = HFI_BUFFER_INTERNAL_PERSIST_1;
  743. buf->ownership = DRIVER;
  744. mutex_lock(&buf_list->lock);
  745. list_add_tail(&buf->list, &buf_list->list);
  746. mutex_unlock(&buf_list->lock);
  747. return buf;
  748. err_no_mem:
  749. kmem_cache_free(cvp_driver->buf_cache, buf);
  750. fail_kzalloc:
  751. return NULL;
  752. }
  753. int cvp_release_arp_buffers(struct msm_cvp_inst *inst)
  754. {
  755. struct msm_cvp_smem *smem;
  756. struct list_head *ptr, *next;
  757. struct cvp_internal_buf *buf;
  758. int rc = 0;
  759. struct msm_cvp_core *core;
  760. struct cvp_hfi_device *hdev;
  761. if (!inst) {
  762. dprintk(CVP_ERR, "Invalid instance pointer = %pK\n", inst);
  763. return -EINVAL;
  764. }
  765. core = inst->core;
  766. if (!core) {
  767. dprintk(CVP_ERR, "Invalid core pointer = %pK\n", core);
  768. return -EINVAL;
  769. }
  770. hdev = core->device;
  771. if (!hdev) {
  772. dprintk(CVP_ERR, "Invalid device pointer = %pK\n", hdev);
  773. return -EINVAL;
  774. }
  775. dprintk(CVP_MEM, "release persist buffer!\n");
  776. mutex_lock(&inst->persistbufs.lock);
  777. /* Workaround for FW: release buffer means release all */
  778. if (inst->state <= MSM_CVP_CLOSE_DONE) {
  779. rc = call_hfi_op(hdev, session_release_buffers,
  780. (void *)inst->session);
  781. if (!rc) {
  782. mutex_unlock(&inst->persistbufs.lock);
  783. rc = wait_for_sess_signal_receipt(inst,
  784. HAL_SESSION_RELEASE_BUFFER_DONE);
  785. if (rc)
  786. dprintk(CVP_WARN,
  787. "%s: wait for signal failed, rc %d\n",
  788. __func__, rc);
  789. mutex_lock(&inst->persistbufs.lock);
  790. } else {
  791. dprintk(CVP_WARN, "Fail to send Rel prst buf\n");
  792. }
  793. }
  794. list_for_each_safe(ptr, next, &inst->persistbufs.list) {
  795. buf = list_entry(ptr, struct cvp_internal_buf, list);
  796. smem = buf->smem;
  797. if (!smem) {
  798. dprintk(CVP_ERR, "%s invalid smem\n", __func__);
  799. mutex_unlock(&inst->persistbufs.lock);
  800. return -EINVAL;
  801. }
  802. list_del(&buf->list);
  803. if (buf->ownership == DRIVER) {
  804. dprintk(CVP_MEM,
  805. "%s: %x : fd %d %s size %d",
  806. "free arp", hash32_ptr(inst->session), buf->fd,
  807. smem->dma_buf->name, buf->size);
  808. msm_cvp_smem_free(smem);
  809. kmem_cache_free(cvp_driver->smem_cache, smem);
  810. }
  811. buf->smem = NULL;
  812. kmem_cache_free(cvp_driver->buf_cache, buf);
  813. }
  814. mutex_unlock(&inst->persistbufs.lock);
  815. return rc;
  816. }