cvp_smem.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2018-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/dma-buf.h>
  6. #include <linux/dma-heap.h>
  7. #include <linux/dma-direction.h>
  8. #include <linux/iommu.h>
  9. #include <linux/msm_dma_iommu_mapping.h>
  10. #include <soc/qcom/secure_buffer.h>
  11. #include <linux/mem-buf.h>
  12. #include <linux/slab.h>
  13. #include <linux/types.h>
  14. #include <linux/qcom-dma-mapping.h>
  15. #include <linux/version.h>
  16. #include "msm_cvp_core.h"
  17. #include "msm_cvp_debug.h"
  18. #include "msm_cvp_resources.h"
  19. #include "cvp_core_hfi.h"
  20. #include "msm_cvp_dsp.h"
  21. static void * __cvp_dma_buf_vmap(struct dma_buf *dbuf)
  22. {
  23. #if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 13, 0))
  24. return dma_buf_vmap(dbuf);
  25. #else
  26. struct dma_buf_map map;
  27. void *dma_map;
  28. int err;
  29. err = dma_buf_vmap(dbuf, &map);
  30. dma_map = err ? NULL : map.vaddr;
  31. if (!dma_map)
  32. dprintk(CVP_ERR, "map to kvaddr failed\n");
  33. return dma_map;
  34. #endif
  35. }
  36. static void __cvp_dma_buf_vunmap(struct dma_buf *dbuf, void *vaddr)
  37. {
  38. #if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 13, 0))
  39. dma_buf_vunmap(dbuf, vaddr);
  40. #else
  41. struct dma_buf_map map = { \
  42. .vaddr = vaddr, \
  43. .is_iomem = false, \
  44. };
  45. if (vaddr)
  46. dma_buf_vunmap(dbuf, &map);
  47. #endif
  48. }
  49. static int msm_dma_get_device_address(struct dma_buf *dbuf, u32 align,
  50. dma_addr_t *iova, u32 flags, struct msm_cvp_platform_resources *res,
  51. struct cvp_dma_mapping_info *mapping_info)
  52. {
  53. int rc = 0;
  54. struct dma_buf_attachment *attach;
  55. struct sg_table *table = NULL;
  56. struct context_bank_info *cb = NULL;
  57. if (!dbuf || !iova || !mapping_info) {
  58. dprintk(CVP_ERR, "Invalid params: %pK, %pK, %pK\n",
  59. dbuf, iova, mapping_info);
  60. return -EINVAL;
  61. }
  62. if (is_iommu_present(res)) {
  63. cb = msm_cvp_smem_get_context_bank(res, flags);
  64. if (!cb) {
  65. dprintk(CVP_ERR,
  66. "%s: Failed to get context bank device\n",
  67. __func__);
  68. rc = -EIO;
  69. goto mem_map_failed;
  70. }
  71. /* Prepare a dma buf for dma on the given device */
  72. attach = dma_buf_attach(dbuf, cb->dev);
  73. if (IS_ERR_OR_NULL(attach)) {
  74. rc = PTR_ERR(attach) ?: -ENOMEM;
  75. dprintk(CVP_ERR, "Failed to attach dmabuf\n");
  76. goto mem_buf_attach_failed;
  77. }
  78. /*
  79. * Get the scatterlist for the given attachment
  80. * Mapping of sg is taken care by map attachment
  81. */
  82. attach->dma_map_attrs = DMA_ATTR_DELAYED_UNMAP;
  83. /*
  84. * We do not need dma_map function to perform cache operations
  85. * on the whole buffer size and hence pass skip sync flag.
  86. * We do the required cache operations separately for the
  87. * required buffer size
  88. */
  89. attach->dma_map_attrs |= DMA_ATTR_SKIP_CPU_SYNC;
  90. if (res->sys_cache_present)
  91. attach->dma_map_attrs |=
  92. DMA_ATTR_IOMMU_USE_UPSTREAM_HINT;
  93. table = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
  94. if (IS_ERR_OR_NULL(table)) {
  95. rc = PTR_ERR(table) ?: -ENOMEM;
  96. dprintk(CVP_ERR, "Failed to map table\n");
  97. goto mem_map_table_failed;
  98. }
  99. if (table->sgl) {
  100. if (flags & SMEM_CAMERA) {
  101. *iova = sg_phys(table->sgl);
  102. } else {
  103. *iova = table->sgl->dma_address;
  104. }
  105. } else {
  106. dprintk(CVP_ERR, "sgl is NULL\n");
  107. rc = -ENOMEM;
  108. goto mem_map_sg_failed;
  109. }
  110. mapping_info->dev = cb->dev;
  111. mapping_info->domain = cb->domain;
  112. mapping_info->table = table;
  113. mapping_info->attach = attach;
  114. mapping_info->buf = dbuf;
  115. mapping_info->cb_info = (void *)cb;
  116. } else {
  117. dprintk(CVP_MEM, "iommu not present, use phys mem addr\n");
  118. }
  119. return 0;
  120. mem_map_sg_failed:
  121. dma_buf_unmap_attachment(attach, table, DMA_BIDIRECTIONAL);
  122. mem_map_table_failed:
  123. dma_buf_detach(dbuf, attach);
  124. mem_buf_attach_failed:
  125. mem_map_failed:
  126. return rc;
  127. }
  128. static int msm_dma_put_device_address(u32 flags,
  129. struct cvp_dma_mapping_info *mapping_info)
  130. {
  131. int rc = 0;
  132. if (!mapping_info) {
  133. dprintk(CVP_WARN, "Invalid mapping_info\n");
  134. return -EINVAL;
  135. }
  136. if (!mapping_info->dev || !mapping_info->table ||
  137. !mapping_info->buf || !mapping_info->attach ||
  138. !mapping_info->cb_info) {
  139. dprintk(CVP_WARN, "Invalid params\n");
  140. return -EINVAL;
  141. }
  142. dma_buf_unmap_attachment(mapping_info->attach,
  143. mapping_info->table, DMA_BIDIRECTIONAL);
  144. dma_buf_detach(mapping_info->buf, mapping_info->attach);
  145. mapping_info->dev = NULL;
  146. mapping_info->domain = NULL;
  147. mapping_info->table = NULL;
  148. mapping_info->attach = NULL;
  149. mapping_info->buf = NULL;
  150. mapping_info->cb_info = NULL;
  151. return rc;
  152. }
  153. struct dma_buf *msm_cvp_smem_get_dma_buf(int fd)
  154. {
  155. struct dma_buf *dma_buf;
  156. dma_buf = dma_buf_get(fd);
  157. if (IS_ERR_OR_NULL(dma_buf)) {
  158. dprintk(CVP_ERR, "Failed to get dma_buf for %d, error %ld\n",
  159. fd, PTR_ERR(dma_buf));
  160. dma_buf = NULL;
  161. }
  162. return dma_buf;
  163. }
  164. void msm_cvp_smem_put_dma_buf(void *dma_buf)
  165. {
  166. if (!dma_buf) {
  167. dprintk(CVP_ERR, "%s: NULL dma_buf\n", __func__);
  168. return;
  169. }
  170. dma_heap_buffer_free((struct dma_buf *)dma_buf);
  171. }
  172. int msm_cvp_map_smem(struct msm_cvp_inst *inst,
  173. struct msm_cvp_smem *smem,
  174. const char *str)
  175. {
  176. int *vmid_list;
  177. int *perms_list;
  178. int nelems = 0;
  179. int rc = 0;
  180. dma_addr_t iova = 0;
  181. u32 temp = 0;
  182. u32 align = SZ_4K;
  183. struct dma_buf *dma_buf;
  184. if (!inst || !smem) {
  185. dprintk(CVP_ERR, "%s: Invalid params: %pK %pK\n",
  186. __func__, inst, smem);
  187. return -EINVAL;
  188. }
  189. dma_buf = smem->dma_buf;
  190. rc = mem_buf_dma_buf_copy_vmperm(dma_buf,
  191. &vmid_list, &perms_list, &nelems);
  192. if (rc) {
  193. dprintk(CVP_ERR, "%s fail to get vmid and perms %d\n",
  194. __func__, rc);
  195. return rc;
  196. }
  197. for (temp = 0; temp < nelems; temp++) {
  198. if (vmid_list[temp] == VMID_CP_PIXEL)
  199. smem->flags |= (SMEM_SECURE | SMEM_PIXEL);
  200. else if (vmid_list[temp] == VMID_CP_NON_PIXEL)
  201. smem->flags |= (SMEM_SECURE | SMEM_NON_PIXEL);
  202. else if (vmid_list[temp] == VMID_CP_CAMERA)
  203. smem->flags |= (SMEM_SECURE | SMEM_CAMERA);
  204. }
  205. rc = msm_dma_get_device_address(dma_buf, align, &iova, smem->flags,
  206. &(inst->core->resources), &smem->mapping_info);
  207. if (rc) {
  208. dprintk(CVP_ERR, "Failed to get device address: %d\n", rc);
  209. goto exit;
  210. }
  211. temp = (u32)iova;
  212. if ((dma_addr_t)temp != iova) {
  213. dprintk(CVP_ERR, "iova(%pa) truncated to %#x", &iova, temp);
  214. rc = -EINVAL;
  215. goto exit;
  216. }
  217. smem->size = dma_buf->size;
  218. smem->device_addr = (u32)iova;
  219. print_smem(CVP_MEM, str, inst, smem);
  220. goto success;
  221. exit:
  222. smem->device_addr = 0x0;
  223. success:
  224. kfree(vmid_list);
  225. kfree(perms_list);
  226. return rc;
  227. }
  228. int msm_cvp_unmap_smem(struct msm_cvp_inst *inst,
  229. struct msm_cvp_smem *smem,
  230. const char *str)
  231. {
  232. int rc = 0;
  233. if (!smem) {
  234. dprintk(CVP_ERR, "%s: Invalid params: %pK\n", __func__, smem);
  235. rc = -EINVAL;
  236. goto exit;
  237. }
  238. print_smem(CVP_MEM, str, inst, smem);
  239. rc = msm_dma_put_device_address(smem->flags, &smem->mapping_info);
  240. if (rc) {
  241. dprintk(CVP_ERR, "Failed to put device address: %d\n", rc);
  242. goto exit;
  243. }
  244. smem->device_addr = 0x0;
  245. exit:
  246. return rc;
  247. }
  248. static int alloc_dma_mem(size_t size, u32 align, int map_kernel,
  249. struct msm_cvp_platform_resources *res, struct msm_cvp_smem *mem)
  250. {
  251. dma_addr_t iova = 0;
  252. int rc = 0;
  253. struct dma_buf *dbuf = NULL;
  254. struct dma_heap *heap = NULL;
  255. struct mem_buf_lend_kernel_arg arg;
  256. int vmids[1];
  257. int perms[1];
  258. if (!res) {
  259. dprintk(CVP_ERR, "%s: NULL res\n", __func__);
  260. return -EINVAL;
  261. }
  262. align = ALIGN(align, SZ_4K);
  263. size = ALIGN(size, SZ_4K);
  264. if (is_iommu_present(res)) {
  265. heap = dma_heap_find("qcom,system");
  266. dprintk(CVP_MEM, "%s size %zx align %d flag %d\n",
  267. __func__, size, align, mem->flags);
  268. } else {
  269. dprintk(CVP_ERR,
  270. "No IOMMU CB: allocate shared memory heap size %zx align %d\n",
  271. size, align);
  272. }
  273. dbuf = dma_heap_buffer_alloc(heap, size, 0, 0);
  274. if (IS_ERR_OR_NULL(dbuf)) {
  275. dprintk(CVP_ERR,
  276. "Failed to allocate shared memory = %x bytes, %x %x\n",
  277. size, mem->flags, PTR_ERR(dbuf));
  278. rc = -ENOMEM;
  279. goto fail_shared_mem_alloc;
  280. }
  281. perms[0] = PERM_READ | PERM_WRITE;
  282. arg.nr_acl_entries = 1;
  283. arg.vmids = vmids;
  284. arg.perms = perms;
  285. if (mem->flags & SMEM_NON_PIXEL) {
  286. vmids[0] = VMID_CP_NON_PIXEL;
  287. rc = mem_buf_lend(dbuf, &arg);
  288. } else if (mem->flags & SMEM_PIXEL) {
  289. vmids[0] = VMID_CP_PIXEL;
  290. rc = mem_buf_lend(dbuf, &arg);
  291. }
  292. if (rc) {
  293. dprintk(CVP_ERR, "Failed to lend dmabuf %d, vmid %d\n",
  294. rc, vmids[0]);
  295. goto fail_device_address;
  296. }
  297. if (!gfa_cv.dmabuf_f_op)
  298. gfa_cv.dmabuf_f_op = (const struct file_operations *)dbuf->file->f_op;
  299. mem->size = size;
  300. mem->dma_buf = dbuf;
  301. mem->kvaddr = NULL;
  302. rc = msm_dma_get_device_address(dbuf, align, &iova, mem->flags,
  303. res, &mem->mapping_info);
  304. if (rc) {
  305. dprintk(CVP_ERR, "Failed to get device address: %d\n",
  306. rc);
  307. goto fail_device_address;
  308. }
  309. mem->device_addr = (u32)iova;
  310. if ((dma_addr_t)mem->device_addr != iova) {
  311. dprintk(CVP_ERR, "iova(%pa) truncated to %#x",
  312. &iova, mem->device_addr);
  313. goto fail_device_address;
  314. }
  315. if (map_kernel) {
  316. dma_buf_begin_cpu_access(dbuf, DMA_BIDIRECTIONAL);
  317. mem->kvaddr = __cvp_dma_buf_vmap(dbuf);
  318. if (!mem->kvaddr) {
  319. dprintk(CVP_ERR,
  320. "Failed to map shared mem in kernel\n");
  321. rc = -EIO;
  322. goto fail_map;
  323. }
  324. }
  325. dprintk(CVP_MEM,
  326. "%s: dma_buf=%pK,iova=%x,size=%d,kvaddr=%pK,flags=%#lx\n",
  327. __func__, mem->dma_buf, mem->device_addr, mem->size,
  328. mem->kvaddr, mem->flags);
  329. return rc;
  330. fail_map:
  331. if (map_kernel)
  332. dma_buf_end_cpu_access(dbuf, DMA_BIDIRECTIONAL);
  333. fail_device_address:
  334. dma_heap_buffer_free(dbuf);
  335. fail_shared_mem_alloc:
  336. return rc;
  337. }
  338. static int free_dma_mem(struct msm_cvp_smem *mem)
  339. {
  340. dprintk(CVP_MEM,
  341. "%s: dma_buf = %pK, device_addr = %x, size = %d, kvaddr = %pK\n",
  342. __func__, mem->dma_buf, mem->device_addr, mem->size, mem->kvaddr);
  343. if (mem->device_addr) {
  344. msm_dma_put_device_address(mem->flags, &mem->mapping_info);
  345. mem->device_addr = 0x0;
  346. }
  347. if (mem->kvaddr) {
  348. __cvp_dma_buf_vunmap(mem->dma_buf, mem->kvaddr);
  349. mem->kvaddr = NULL;
  350. dma_buf_end_cpu_access(mem->dma_buf, DMA_BIDIRECTIONAL);
  351. }
  352. if (mem->dma_buf) {
  353. dma_heap_buffer_free(mem->dma_buf);
  354. mem->dma_buf = NULL;
  355. }
  356. return 0;
  357. }
  358. int msm_cvp_smem_alloc(size_t size, u32 align, int map_kernel,
  359. void *res, struct msm_cvp_smem *smem)
  360. {
  361. int rc = 0;
  362. if (!smem || !size) {
  363. dprintk(CVP_ERR, "%s: NULL smem or %d size\n",
  364. __func__, (u32)size);
  365. return -EINVAL;
  366. }
  367. rc = alloc_dma_mem(size, align, map_kernel,
  368. (struct msm_cvp_platform_resources *)res, smem);
  369. return rc;
  370. }
  371. int msm_cvp_smem_free(struct msm_cvp_smem *smem)
  372. {
  373. int rc = 0;
  374. if (!smem) {
  375. dprintk(CVP_ERR, "NULL smem passed\n");
  376. return -EINVAL;
  377. }
  378. rc = free_dma_mem(smem);
  379. return rc;
  380. };
  381. int msm_cvp_smem_cache_operations(struct dma_buf *dbuf,
  382. enum smem_cache_ops cache_op, unsigned long offset, unsigned long size)
  383. {
  384. int rc = 0;
  385. if (!dbuf) {
  386. dprintk(CVP_ERR, "%s: Invalid params\n", __func__);
  387. return -EINVAL;
  388. }
  389. switch (cache_op) {
  390. case SMEM_CACHE_CLEAN:
  391. case SMEM_CACHE_CLEAN_INVALIDATE:
  392. rc = dma_buf_begin_cpu_access_partial(dbuf, DMA_BIDIRECTIONAL,
  393. offset, size);
  394. if (rc)
  395. break;
  396. rc = dma_buf_end_cpu_access_partial(dbuf, DMA_BIDIRECTIONAL,
  397. offset, size);
  398. break;
  399. case SMEM_CACHE_INVALIDATE:
  400. rc = dma_buf_begin_cpu_access_partial(dbuf, DMA_TO_DEVICE,
  401. offset, size);
  402. if (rc)
  403. break;
  404. rc = dma_buf_end_cpu_access_partial(dbuf, DMA_FROM_DEVICE,
  405. offset, size);
  406. break;
  407. default:
  408. dprintk(CVP_ERR, "%s: cache (%d) operation not supported\n",
  409. __func__, cache_op);
  410. rc = -EINVAL;
  411. break;
  412. }
  413. return rc;
  414. }
  415. struct context_bank_info *msm_cvp_smem_get_context_bank(
  416. struct msm_cvp_platform_resources *res,
  417. unsigned int flags)
  418. {
  419. struct context_bank_info *cb = NULL, *match = NULL;
  420. char *search_str;
  421. char *non_secure_cb = "cvp_hlos";
  422. char *secure_nonpixel_cb = "cvp_sec_nonpixel";
  423. char *secure_pixel_cb = "cvp_sec_pixel";
  424. bool is_secure = (flags & SMEM_SECURE) ? true : false;
  425. if (flags & SMEM_PIXEL)
  426. search_str = secure_pixel_cb;
  427. else if (flags & SMEM_NON_PIXEL)
  428. search_str = secure_nonpixel_cb;
  429. else if (flags & SMEM_CAMERA)
  430. search_str = secure_pixel_cb;
  431. else
  432. search_str = non_secure_cb;
  433. list_for_each_entry(cb, &res->context_banks, list) {
  434. if (cb->is_secure == is_secure &&
  435. !strcmp(search_str, cb->name)) {
  436. match = cb;
  437. break;
  438. }
  439. }
  440. if (!match)
  441. dprintk(CVP_ERR,
  442. "%s: cb not found for flags %x, is_secure %d\n",
  443. __func__, flags, is_secure);
  444. return match;
  445. }
  446. int msm_cvp_map_ipcc_regs(u32 *iova)
  447. {
  448. struct context_bank_info *cb;
  449. struct msm_cvp_core *core;
  450. struct cvp_hfi_device *hfi_ops;
  451. struct iris_hfi_device *dev = NULL;
  452. phys_addr_t paddr;
  453. u32 size;
  454. core = list_first_entry(&cvp_driver->cores, struct msm_cvp_core, list);
  455. if (core) {
  456. hfi_ops = core->device;
  457. if (hfi_ops)
  458. dev = hfi_ops->hfi_device_data;
  459. }
  460. if (!dev)
  461. return -EINVAL;
  462. paddr = dev->res->ipcc_reg_base;
  463. size = dev->res->ipcc_reg_size;
  464. if (!paddr || !size)
  465. return -EINVAL;
  466. cb = msm_cvp_smem_get_context_bank(dev->res, 0);
  467. if (!cb) {
  468. dprintk(CVP_ERR, "%s: fail to get context bank\n", __func__);
  469. return -EINVAL;
  470. }
  471. *iova = dma_map_resource(cb->dev, paddr, size, DMA_BIDIRECTIONAL, 0);
  472. if (*iova == DMA_MAPPING_ERROR) {
  473. dprintk(CVP_WARN, "%s: fail to map IPCC regs\n", __func__);
  474. return -EFAULT;
  475. }
  476. return 0;
  477. }