msm_vidc_memory.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/dma-buf.h>
  6. #include <linux/dma-heap.h>
  7. #include <linux/dma-mapping.h>
  8. #include <linux/qcom-dma-mapping.h>
  9. #include <linux/mem-buf.h>
  10. #include <soc/qcom/secure_buffer.h>
  11. #include "msm_vidc_memory.h"
  12. #include "msm_vidc_debug.h"
  13. #include "msm_vidc_internal.h"
  14. #include "msm_vidc_driver.h"
  15. #include "msm_vidc_core.h"
  16. #include "msm_vidc_events.h"
  17. #include "msm_vidc_platform.h"
  18. #include "venus_hfi.h"
  19. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,16,0))
  20. MODULE_IMPORT_NS(DMA_BUF);
  21. #endif
  22. struct msm_vidc_buf_region_name {
  23. enum msm_vidc_buffer_region region;
  24. char *name;
  25. };
  26. struct context_bank_info *msm_vidc_get_context_bank(struct msm_vidc_core *core,
  27. enum msm_vidc_buffer_region region)
  28. {
  29. struct context_bank_info *cb = NULL, *match = NULL;
  30. if (!region || region >= MSM_VIDC_REGION_MAX) {
  31. d_vpr_e("Invalid region %#x\n", region);
  32. return NULL;
  33. }
  34. venus_hfi_for_each_context_bank(core, cb) {
  35. if (cb->region == region) {
  36. match = cb;
  37. break;
  38. }
  39. }
  40. if (!match)
  41. d_vpr_e("cb not found for region %#x\n", region);
  42. return match;
  43. }
  44. struct dma_buf *msm_vidc_memory_get_dmabuf(struct msm_vidc_inst *inst, int fd)
  45. {
  46. struct msm_memory_dmabuf *buf = NULL;
  47. struct dma_buf *dmabuf = NULL;
  48. bool found = false;
  49. if (!inst) {
  50. d_vpr_e("%s: invalid params\n", __func__);
  51. return NULL;
  52. }
  53. /* get local dmabuf ref for tracking */
  54. dmabuf = dma_buf_get(fd);
  55. if (IS_ERR_OR_NULL(dmabuf)) {
  56. d_vpr_e("Failed to get dmabuf for %d, error %ld\n",
  57. fd, PTR_ERR(dmabuf));
  58. return NULL;
  59. }
  60. /* track dmabuf - inc refcount if already present */
  61. list_for_each_entry(buf, &inst->dmabuf_tracker, list) {
  62. if (buf->dmabuf == dmabuf) {
  63. buf->refcount++;
  64. found = true;
  65. break;
  66. }
  67. }
  68. if (found) {
  69. /* put local dmabuf ref */
  70. dma_buf_put(dmabuf);
  71. return dmabuf;
  72. }
  73. /* get tracker instance from pool */
  74. buf = msm_memory_pool_alloc(inst, MSM_MEM_POOL_DMABUF);
  75. if (!buf) {
  76. i_vpr_e(inst, "%s: dmabuf alloc failed\n", __func__);
  77. dma_buf_put(dmabuf);
  78. return NULL;
  79. }
  80. /* hold dmabuf strong ref in tracker */
  81. buf->dmabuf = dmabuf;
  82. buf->refcount = 1;
  83. INIT_LIST_HEAD(&buf->list);
  84. /* add new dmabuf entry to tracker */
  85. list_add_tail(&buf->list, &inst->dmabuf_tracker);
  86. return dmabuf;
  87. }
  88. void msm_vidc_memory_put_dmabuf(struct msm_vidc_inst *inst, struct dma_buf *dmabuf)
  89. {
  90. struct msm_memory_dmabuf *buf = NULL;
  91. bool found = false;
  92. if (!inst || !dmabuf) {
  93. d_vpr_e("%s: invalid params\n", __func__);
  94. return;
  95. }
  96. /* track dmabuf - dec refcount if already present */
  97. list_for_each_entry(buf, &inst->dmabuf_tracker, list) {
  98. if (buf->dmabuf == dmabuf) {
  99. buf->refcount--;
  100. found = true;
  101. break;
  102. }
  103. }
  104. if (!found) {
  105. i_vpr_e(inst, "%s: invalid dmabuf %#x\n", __func__, dmabuf);
  106. return;
  107. }
  108. /* non-zero refcount - do nothing */
  109. if (buf->refcount)
  110. return;
  111. /* remove dmabuf entry from tracker */
  112. list_del(&buf->list);
  113. /* release dmabuf strong ref from tracker */
  114. dma_buf_put(buf->dmabuf);
  115. /* put tracker instance back to pool */
  116. msm_memory_pool_free(inst, buf);
  117. }
  118. void msm_vidc_memory_put_dmabuf_completely(struct msm_vidc_inst *inst,
  119. struct msm_memory_dmabuf *buf)
  120. {
  121. if (!inst || !buf) {
  122. d_vpr_e("%s: invalid params\n", __func__);
  123. return;
  124. }
  125. while (buf->refcount) {
  126. buf->refcount--;
  127. if (!buf->refcount) {
  128. /* remove dmabuf entry from tracker */
  129. list_del(&buf->list);
  130. /* release dmabuf strong ref from tracker */
  131. dma_buf_put(buf->dmabuf);
  132. /* put tracker instance back to pool */
  133. msm_memory_pool_free(inst, buf);
  134. break;
  135. }
  136. }
  137. }
  138. static bool is_non_secure_buffer(struct dma_buf *dmabuf)
  139. {
  140. return mem_buf_dma_buf_exclusive_owner(dmabuf);
  141. }
  142. int msm_vidc_memory_map(struct msm_vidc_core *core, struct msm_vidc_map *map)
  143. {
  144. int rc = 0;
  145. struct dma_buf_attachment *attach = NULL;
  146. struct sg_table *table = NULL;
  147. struct context_bank_info *cb = NULL;
  148. if (!core || !map) {
  149. d_vpr_e("%s: invalid params\n", __func__);
  150. return -EINVAL;
  151. }
  152. if (map->refcount) {
  153. map->refcount++;
  154. goto exit;
  155. }
  156. /* reject non-secure mapping request for a secure buffer(or vice versa) */
  157. if (map->region == MSM_VIDC_NON_SECURE || map->region == MSM_VIDC_NON_SECURE_PIXEL) {
  158. if (!is_non_secure_buffer(map->dmabuf)) {
  159. d_vpr_e("%s: secure buffer mapping to non-secure region %d not allowed\n",
  160. __func__, map->region);
  161. return -EINVAL;
  162. }
  163. } else {
  164. if (is_non_secure_buffer(map->dmabuf)) {
  165. d_vpr_e("%s: non-secure buffer mapping to secure region %d not allowed\n",
  166. __func__, map->region);
  167. return -EINVAL;
  168. }
  169. }
  170. cb = msm_vidc_get_context_bank(core, map->region);
  171. if (!cb) {
  172. d_vpr_e("%s: Failed to get context bank device\n",
  173. __func__);
  174. rc = -EIO;
  175. goto error_cb;
  176. }
  177. /* Prepare a dma buf for dma on the given device */
  178. attach = dma_buf_attach(map->dmabuf, cb->dev);
  179. if (IS_ERR_OR_NULL(attach)) {
  180. rc = PTR_ERR(attach) ? PTR_ERR(attach) : -ENOMEM;
  181. d_vpr_e("Failed to attach dmabuf\n");
  182. goto error_attach;
  183. }
  184. /*
  185. * Get the scatterlist for the given attachment
  186. * Mapping of sg is taken care by map attachment
  187. */
  188. attach->dma_map_attrs |= DMA_ATTR_DELAYED_UNMAP;
  189. /*
  190. * We do not need dma_map function to perform cache operations
  191. * on the whole buffer size and hence pass skip sync flag.
  192. * We do the required cache operations separately for the
  193. * required buffer size
  194. */
  195. attach->dma_map_attrs |= DMA_ATTR_SKIP_CPU_SYNC;
  196. if (is_sys_cache_present(core))
  197. attach->dma_map_attrs |=
  198. DMA_ATTR_IOMMU_USE_UPSTREAM_HINT;
  199. table = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
  200. if (IS_ERR_OR_NULL(table)) {
  201. rc = PTR_ERR(table) ? PTR_ERR(table) : -ENOMEM;
  202. d_vpr_e("Failed to map table\n");
  203. goto error_table;
  204. }
  205. if (!table->sgl) {
  206. d_vpr_e("sgl is NULL\n");
  207. rc = -ENOMEM;
  208. goto error_sg;
  209. }
  210. map->device_addr = table->sgl->dma_address;
  211. map->table = table;
  212. map->attach = attach;
  213. map->refcount++;
  214. exit:
  215. d_vpr_l(
  216. "%s: type %11s, device_addr %#llx, refcount %d, region %d\n",
  217. __func__, buf_name(map->type), map->device_addr, map->refcount, map->region);
  218. return 0;
  219. error_sg:
  220. dma_buf_unmap_attachment(attach, table, DMA_BIDIRECTIONAL);
  221. error_table:
  222. dma_buf_detach(map->dmabuf, attach);
  223. error_attach:
  224. error_cb:
  225. return rc;
  226. }
  227. int msm_vidc_memory_unmap(struct msm_vidc_core *core,
  228. struct msm_vidc_map *map)
  229. {
  230. int rc = 0;
  231. if (!core || !map) {
  232. d_vpr_e("%s: invalid params\n", __func__);
  233. return -EINVAL;
  234. }
  235. if (map->refcount) {
  236. map->refcount--;
  237. } else {
  238. d_vpr_e("unmap called while refcount is zero already\n");
  239. return -EINVAL;
  240. }
  241. d_vpr_l(
  242. "%s: type %11s, device_addr %#llx, refcount %d, region %d\n",
  243. __func__, buf_name(map->type), map->device_addr, map->refcount, map->region);
  244. if (map->refcount)
  245. goto exit;
  246. dma_buf_unmap_attachment(map->attach, map->table, DMA_BIDIRECTIONAL);
  247. dma_buf_detach(map->dmabuf, map->attach);
  248. map->device_addr = 0x0;
  249. map->attach = NULL;
  250. map->table = NULL;
  251. exit:
  252. return rc;
  253. }
  254. struct dma_buf_attachment *msm_vidc_dma_buf_attach(struct dma_buf *dbuf,
  255. struct device *dev)
  256. {
  257. int rc = 0;
  258. struct dma_buf_attachment *attach = NULL;
  259. if (!dbuf || !dev) {
  260. d_vpr_e("%s: invalid params\n", __func__);
  261. return NULL;
  262. }
  263. attach = dma_buf_attach(dbuf, dev);
  264. if (IS_ERR_OR_NULL(attach)) {
  265. rc = PTR_ERR(attach) ? PTR_ERR(attach) : -1;
  266. d_vpr_e("Failed to attach dmabuf, error %d\n", rc);
  267. return NULL;;
  268. }
  269. return attach;
  270. }
  271. int msm_vidc_dma_buf_detach(struct dma_buf *dbuf,
  272. struct dma_buf_attachment *attach)
  273. {
  274. int rc = 0;
  275. if (!dbuf || !attach) {
  276. d_vpr_e("%s: invalid params\n", __func__);
  277. return -EINVAL;
  278. }
  279. dma_buf_detach(dbuf, attach);
  280. return rc;
  281. }
  282. struct sg_table *msm_vidc_dma_buf_map_attachment(
  283. struct dma_buf_attachment *attach)
  284. {
  285. int rc = 0;
  286. struct sg_table *table = NULL;
  287. if (!attach) {
  288. d_vpr_e("%s: invalid params\n", __func__);
  289. return NULL;
  290. }
  291. table = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
  292. if (IS_ERR_OR_NULL(table)) {
  293. rc = PTR_ERR(table) ? PTR_ERR(table) : -1;
  294. d_vpr_e("Failed to map table, error %d\n", rc);
  295. return NULL;
  296. }
  297. return table;
  298. }
  299. int msm_vidc_dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
  300. struct sg_table *table)
  301. {
  302. int rc = 0;
  303. if (!attach || !table) {
  304. d_vpr_e("%s: invalid params\n", __func__);
  305. return -EINVAL;
  306. }
  307. dma_buf_unmap_attachment(attach, table, DMA_BIDIRECTIONAL);
  308. return rc;
  309. }
  310. int msm_vidc_vmem_alloc(unsigned long size, void **mem, const char *msg)
  311. {
  312. int rc = 0;
  313. if (*mem) {
  314. d_vpr_e("%s: error: double alloc\n", msg);
  315. rc = -EINVAL;
  316. }
  317. *mem = vzalloc(size);
  318. if (!*mem) {
  319. d_vpr_e("allocation failed for %s\n", msg);
  320. rc = -ENOMEM;
  321. }
  322. return rc;
  323. }
  324. void msm_vidc_vmem_free(void **addr)
  325. {
  326. if (addr && *addr) {
  327. vfree(*addr);
  328. *addr = NULL;
  329. }
  330. }
  331. int msm_vidc_memory_alloc(struct msm_vidc_core *core, struct msm_vidc_alloc *mem)
  332. {
  333. int rc = 0;
  334. int size = 0;
  335. struct dma_heap *heap;
  336. char *heap_name = NULL;
  337. struct mem_buf_lend_kernel_arg lend_arg;
  338. int vmids[1];
  339. int perms[1];
  340. if (!mem) {
  341. d_vpr_e("%s: invalid params\n", __func__);
  342. return -EINVAL;
  343. }
  344. size = ALIGN(mem->size, SZ_4K);
  345. if (mem->secure) {
  346. switch (mem->region) {
  347. case MSM_VIDC_SECURE_PIXEL:
  348. heap_name = "qcom,secure-pixel";
  349. break;
  350. case MSM_VIDC_SECURE_NONPIXEL:
  351. heap_name = "qcom,secure-non-pixel";
  352. break;
  353. case MSM_VIDC_SECURE_BITSTREAM:
  354. heap_name = "qcom,system";
  355. break;
  356. default:
  357. d_vpr_e("invalid secure region : %#x\n", mem->region);
  358. return -EINVAL;
  359. }
  360. } else {
  361. heap_name = "qcom,system";
  362. }
  363. heap = dma_heap_find(heap_name);
  364. mem->dmabuf = dma_heap_buffer_alloc(heap, size, 0, 0);
  365. if (IS_ERR_OR_NULL(mem->dmabuf)) {
  366. d_vpr_e("%s: dma heap %s alloc failed\n", __func__, heap_name);
  367. mem->dmabuf = NULL;
  368. rc = -ENOMEM;
  369. goto error;
  370. }
  371. if (mem->secure && mem->type == MSM_VIDC_BUF_BIN)
  372. {
  373. vmids[0] = VMID_CP_BITSTREAM;
  374. perms[0] = PERM_READ | PERM_WRITE;
  375. lend_arg.nr_acl_entries = ARRAY_SIZE(vmids);
  376. lend_arg.vmids = vmids;
  377. lend_arg.perms = perms;
  378. rc = mem_buf_lend(mem->dmabuf, &lend_arg);
  379. if (rc) {
  380. d_vpr_e("%s: BIN dmabuf %pK LEND failed, rc %d heap %s\n",
  381. __func__, mem->dmabuf, rc, heap_name);
  382. goto error;
  383. }
  384. }
  385. if (mem->map_kernel) {
  386. dma_buf_begin_cpu_access(mem->dmabuf, DMA_BIDIRECTIONAL);
  387. /*
  388. * Waipio uses Kernel version 5.10.x,
  389. * Kalama uses Kernel Version 5.15.x,
  390. * Pineapple uses Kernel Version 5.18.x
  391. */
  392. #if (LINUX_VERSION_CODE < KERNEL_VERSION(5,15,0))
  393. mem->kvaddr = dma_buf_vmap(mem->dmabuf);
  394. if (!mem->kvaddr) {
  395. d_vpr_e("%s: kernel map failed\n", __func__);
  396. rc = -EIO;
  397. goto error;
  398. }
  399. #elif (LINUX_VERSION_CODE < KERNEL_VERSION(5,16,0))
  400. rc = dma_buf_vmap(mem->dmabuf, &mem->dmabuf_map);
  401. if (rc) {
  402. d_vpr_e("%s: kernel map failed\n", __func__);
  403. rc = -EIO;
  404. goto error;
  405. }
  406. mem->kvaddr = mem->dmabuf_map.vaddr;
  407. #else
  408. rc = dma_buf_vmap(mem->dmabuf, &mem->dmabuf_map);
  409. if (rc) {
  410. d_vpr_e("%s: kernel map failed\n", __func__);
  411. rc = -EIO;
  412. goto error;
  413. }
  414. mem->kvaddr = mem->dmabuf_map.vaddr;
  415. #endif
  416. }
  417. d_vpr_h(
  418. "%s: dmabuf %pK, size %d, kvaddr %pK, buffer_type %s, secure %d, region %d\n",
  419. __func__, mem->dmabuf, mem->size, mem->kvaddr, buf_name(mem->type),
  420. mem->secure, mem->region);
  421. trace_msm_vidc_dma_buffer("ALLOC", mem->dmabuf, mem->size, mem->kvaddr,
  422. buf_name(mem->type), mem->secure, mem->region);
  423. return 0;
  424. error:
  425. msm_vidc_memory_free(core, mem);
  426. return rc;
  427. }
  428. int msm_vidc_memory_free(struct msm_vidc_core *core, struct msm_vidc_alloc *mem)
  429. {
  430. int rc = 0;
  431. if (!mem || !mem->dmabuf) {
  432. d_vpr_e("%s: invalid params\n", __func__);
  433. return -EINVAL;
  434. }
  435. d_vpr_h(
  436. "%s: dmabuf %pK, size %d, kvaddr %pK, buffer_type %s, secure %d, region %d\n",
  437. __func__, mem->dmabuf, mem->size, mem->kvaddr, buf_name(mem->type),
  438. mem->secure, mem->region);
  439. trace_msm_vidc_dma_buffer("FREE", mem->dmabuf, mem->size, mem->kvaddr,
  440. buf_name(mem->type), mem->secure, mem->region);
  441. if (mem->kvaddr) {
  442. #if (LINUX_VERSION_CODE < KERNEL_VERSION(5,15,0))
  443. dma_buf_vunmap(mem->dmabuf, mem->kvaddr);
  444. #else
  445. dma_buf_vunmap(mem->dmabuf, &mem->dmabuf_map);
  446. #endif
  447. mem->kvaddr = NULL;
  448. dma_buf_end_cpu_access(mem->dmabuf, DMA_BIDIRECTIONAL);
  449. }
  450. if (mem->dmabuf) {
  451. dma_heap_buffer_free(mem->dmabuf);
  452. mem->dmabuf = NULL;
  453. }
  454. return rc;
  455. };
  456. void *msm_memory_pool_alloc(struct msm_vidc_inst *inst, enum msm_memory_pool_type type)
  457. {
  458. struct msm_memory_alloc_header *hdr = NULL;
  459. struct msm_memory_pool *pool;
  460. if (!inst || type < 0 || type >= MSM_MEM_POOL_MAX) {
  461. d_vpr_e("%s: Invalid params\n", __func__);
  462. return NULL;
  463. }
  464. pool = &inst->pool[type];
  465. if (!list_empty(&pool->free_pool)) {
  466. /* get 1st node from free pool */
  467. hdr = list_first_entry(&pool->free_pool,
  468. struct msm_memory_alloc_header, list);
  469. list_del_init(&hdr->list);
  470. /* reset existing data */
  471. memset((char *)hdr->buf, 0, pool->size);
  472. /* add to busy pool */
  473. list_add_tail(&hdr->list, &pool->busy_pool);
  474. /* set busy flag to true. This is to catch double free request */
  475. hdr->busy = true;
  476. return hdr->buf;
  477. }
  478. if (msm_vidc_vmem_alloc(pool->size + sizeof(struct msm_memory_alloc_header),
  479. (void **)&hdr, __func__))
  480. return NULL;
  481. INIT_LIST_HEAD(&hdr->list);
  482. hdr->type = type;
  483. hdr->busy = true;
  484. hdr->buf = (void *)(hdr + 1);
  485. list_add_tail(&hdr->list, &pool->busy_pool);
  486. return hdr->buf;
  487. }
  488. void msm_memory_pool_free(struct msm_vidc_inst *inst, void *vidc_buf)
  489. {
  490. struct msm_memory_alloc_header *hdr;
  491. struct msm_memory_pool *pool;
  492. if (!inst || !vidc_buf) {
  493. d_vpr_e("%s: Invalid params\n", __func__);
  494. return;
  495. }
  496. hdr = (struct msm_memory_alloc_header *)vidc_buf - 1;
  497. /* sanitize buffer addr */
  498. if (hdr->buf != vidc_buf) {
  499. i_vpr_e(inst, "%s: invalid buf addr %#x\n", __func__, vidc_buf);
  500. return;
  501. }
  502. /* sanitize pool type */
  503. if (hdr->type < 0 || hdr->type >= MSM_MEM_POOL_MAX) {
  504. i_vpr_e(inst, "%s: invalid pool type %#x\n", __func__, hdr->type);
  505. return;
  506. }
  507. pool = &inst->pool[hdr->type];
  508. /* catch double-free request */
  509. if (!hdr->busy) {
  510. i_vpr_e(inst, "%s: double free request. type %s, addr %#x\n", __func__,
  511. pool->name, vidc_buf);
  512. return;
  513. }
  514. hdr->busy = false;
  515. /* remove from busy pool */
  516. list_del_init(&hdr->list);
  517. /* add to free pool */
  518. list_add_tail(&hdr->list, &pool->free_pool);
  519. }
  520. static void msm_vidc_destroy_pool_buffers(struct msm_vidc_inst *inst,
  521. enum msm_memory_pool_type type)
  522. {
  523. struct msm_memory_alloc_header *hdr, *dummy;
  524. struct msm_memory_pool *pool;
  525. u32 fcount = 0, bcount = 0;
  526. if (!inst || type < 0 || type >= MSM_MEM_POOL_MAX) {
  527. d_vpr_e("%s: Invalid params\n", __func__);
  528. return;
  529. }
  530. pool = &inst->pool[type];
  531. /* detect memleak: busy pool is expected to be empty here */
  532. if (!list_empty(&pool->busy_pool))
  533. i_vpr_e(inst, "%s: destroy request on active buffer. type %s\n",
  534. __func__, pool->name);
  535. /* destroy all free buffers */
  536. list_for_each_entry_safe(hdr, dummy, &pool->free_pool, list) {
  537. list_del(&hdr->list);
  538. msm_vidc_vmem_free((void **)&hdr);
  539. fcount++;
  540. }
  541. /* destroy all busy buffers */
  542. list_for_each_entry_safe(hdr, dummy, &pool->busy_pool, list) {
  543. list_del(&hdr->list);
  544. msm_vidc_vmem_free((void **)&hdr);
  545. bcount++;
  546. }
  547. i_vpr_h(inst, "%s: type: %23s, count: free %2u, busy %2u\n",
  548. __func__, pool->name, fcount, bcount);
  549. }
  550. void msm_memory_pools_deinit(struct msm_vidc_inst *inst)
  551. {
  552. u32 i = 0;
  553. if (!inst) {
  554. d_vpr_e("%s: Invalid params\n", __func__);
  555. return;
  556. }
  557. /* destroy all buffers from all pool types */
  558. for (i = 0; i < MSM_MEM_POOL_MAX; i++)
  559. msm_vidc_destroy_pool_buffers(inst, i);
  560. }
  561. struct msm_vidc_type_size_name {
  562. enum msm_memory_pool_type type;
  563. u32 size;
  564. char *name;
  565. };
  566. static const struct msm_vidc_type_size_name buftype_size_name_arr[] = {
  567. {MSM_MEM_POOL_BUFFER, sizeof(struct msm_vidc_buffer), "MSM_MEM_POOL_BUFFER" },
  568. {MSM_MEM_POOL_MAP, sizeof(struct msm_vidc_map), "MSM_MEM_POOL_MAP" },
  569. {MSM_MEM_POOL_ALLOC, sizeof(struct msm_vidc_alloc), "MSM_MEM_POOL_ALLOC" },
  570. {MSM_MEM_POOL_TIMESTAMP, sizeof(struct msm_vidc_timestamp), "MSM_MEM_POOL_TIMESTAMP" },
  571. {MSM_MEM_POOL_DMABUF, sizeof(struct msm_memory_dmabuf), "MSM_MEM_POOL_DMABUF" },
  572. {MSM_MEM_POOL_PACKET, sizeof(struct hfi_pending_packet) + MSM_MEM_POOL_PACKET_SIZE,
  573. "MSM_MEM_POOL_PACKET"},
  574. {MSM_MEM_POOL_BUF_TIMER, sizeof(struct msm_vidc_input_timer), "MSM_MEM_POOL_BUF_TIMER" },
  575. {MSM_MEM_POOL_BUF_STATS, sizeof(struct msm_vidc_buffer_stats), "MSM_MEM_POOL_BUF_STATS"},
  576. };
  577. int msm_memory_pools_init(struct msm_vidc_inst *inst)
  578. {
  579. u32 i;
  580. if (!inst) {
  581. d_vpr_e("%s: Invalid params\n", __func__);
  582. return -EINVAL;
  583. }
  584. if (ARRAY_SIZE(buftype_size_name_arr) != MSM_MEM_POOL_MAX) {
  585. i_vpr_e(inst, "%s: num elements mismatch %lu %u\n", __func__,
  586. ARRAY_SIZE(buftype_size_name_arr), MSM_MEM_POOL_MAX);
  587. return -EINVAL;
  588. }
  589. for (i = 0; i < MSM_MEM_POOL_MAX; i++) {
  590. if (i != buftype_size_name_arr[i].type) {
  591. i_vpr_e(inst, "%s: type mismatch %u %u\n", __func__,
  592. i, buftype_size_name_arr[i].type);
  593. return -EINVAL;
  594. }
  595. inst->pool[i].size = buftype_size_name_arr[i].size;
  596. inst->pool[i].name = buftype_size_name_arr[i].name;
  597. INIT_LIST_HEAD(&inst->pool[i].free_pool);
  598. INIT_LIST_HEAD(&inst->pool[i].busy_pool);
  599. }
  600. return 0;
  601. }
  602. /*
  603. int msm_memory_cache_operations(struct msm_vidc_inst *inst,
  604. struct dma_buf *dbuf, enum smem_cache_ops cache_op,
  605. unsigned long offset, unsigned long size, u32 sid)
  606. {
  607. int rc = 0;
  608. unsigned long flags = 0;
  609. if (!inst) {
  610. d_vpr_e("%s: invalid parameters\n", __func__);
  611. return -EINVAL;
  612. }
  613. if (!dbuf) {
  614. i_vpr_e(inst, "%s: invalid params\n", __func__);
  615. return -EINVAL;
  616. }
  617. rc = dma_buf_get_flags(dbuf, &flags);
  618. if (rc) {
  619. i_vpr_e(inst, "%s: dma_buf_get_flags failed, err %d\n",
  620. __func__, rc);
  621. return rc;
  622. } else if (!(flags & ION_FLAG_CACHED)) {
  623. return rc;
  624. }
  625. switch (cache_op) {
  626. case SMEM_CACHE_CLEAN:
  627. case SMEM_CACHE_CLEAN_INVALIDATE:
  628. rc = dma_buf_begin_cpu_access_partial(dbuf, DMA_TO_DEVICE,
  629. offset, size);
  630. if (rc)
  631. break;
  632. rc = dma_buf_end_cpu_access_partial(dbuf, DMA_TO_DEVICE,
  633. offset, size);
  634. break;
  635. case SMEM_CACHE_INVALIDATE:
  636. rc = dma_buf_begin_cpu_access_partial(dbuf, DMA_TO_DEVICE,
  637. offset, size);
  638. if (rc)
  639. break;
  640. rc = dma_buf_end_cpu_access_partial(dbuf, DMA_FROM_DEVICE,
  641. offset, size);
  642. break;
  643. default:
  644. i_vpr_e(inst, "%s: cache (%d) operation not supported\n",
  645. __func__, cache_op);
  646. rc = -EINVAL;
  647. break;
  648. }
  649. return rc;
  650. }
  651. int msm_smem_memory_prefetch(struct msm_vidc_inst *inst)
  652. {
  653. int i, rc = 0;
  654. struct memory_regions *vidc_regions = NULL;
  655. struct ion_prefetch_region ion_region[MEMORY_REGIONS_MAX];
  656. if (!inst) {
  657. d_vpr_e("%s: invalid parameters\n", __func__);
  658. return -EINVAL;
  659. }
  660. vidc_regions = &inst->regions;
  661. if (vidc_regions->num_regions > MEMORY_REGIONS_MAX) {
  662. i_vpr_e(inst, "%s: invalid num_regions %d, max %d\n",
  663. __func__, vidc_regions->num_regions,
  664. MEMORY_REGIONS_MAX);
  665. return -EINVAL;
  666. }
  667. memset(ion_region, 0, sizeof(ion_region));
  668. for (i = 0; i < vidc_regions->num_regions; i++) {
  669. ion_region[i].size = vidc_regions->region[i].size;
  670. ion_region[i].vmid = vidc_regions->region[i].vmid;
  671. }
  672. rc = msm_ion_heap_prefetch(ION_SECURE_HEAP_ID, ion_region,
  673. vidc_regions->num_regions);
  674. if (rc)
  675. i_vpr_e(inst, "%s: prefetch failed, ret: %d\n",
  676. __func__, rc);
  677. else
  678. i_vpr_l(inst, "%s: prefetch succeeded\n", __func__);
  679. return rc;
  680. }
  681. int msm_smem_memory_drain(struct msm_vidc_inst *inst)
  682. {
  683. int i, rc = 0;
  684. struct memory_regions *vidc_regions = NULL;
  685. struct ion_prefetch_region ion_region[MEMORY_REGIONS_MAX];
  686. if (!inst) {
  687. d_vpr_e("%s: invalid parameters\n", __func__);
  688. return -EINVAL;
  689. }
  690. vidc_regions = &inst->regions;
  691. if (vidc_regions->num_regions > MEMORY_REGIONS_MAX) {
  692. i_vpr_e(inst, "%s: invalid num_regions %d, max %d\n",
  693. __func__, vidc_regions->num_regions,
  694. MEMORY_REGIONS_MAX);
  695. return -EINVAL;
  696. }
  697. memset(ion_region, 0, sizeof(ion_region));
  698. for (i = 0; i < vidc_regions->num_regions; i++) {
  699. ion_region[i].size = vidc_regions->region[i].size;
  700. ion_region[i].vmid = vidc_regions->region[i].vmid;
  701. }
  702. rc = msm_ion_heap_drain(ION_SECURE_HEAP_ID, ion_region,
  703. vidc_regions->num_regions);
  704. if (rc)
  705. i_vpr_e(inst, "%s: drain failed, ret: %d\n", __func__, rc);
  706. else
  707. i_vpr_l(inst, "%s: drain succeeded\n", __func__);
  708. return rc;
  709. }
  710. */