msm_smem.c 12 KB

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