msm_cvp_buf.c 28 KB

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