msm_cvp_buf.c 23 KB

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