From e099f8b1fd9b2de05e7fb7932cfc7508f4eda775 Mon Sep 17 00:00:00 2001 From: Sagar Kamble Date: Thu, 3 Jun 2021 18:17:27 +0530 Subject: [PATCH] gpu: nvgpu: specify DMA_TO_DEVICE direction to map_attachment for RO maps nvgpu does map attachment with DMA_BIDIRECTIONAL direction for buffers irrespective of the GPU mapping type. nvmap will allow map attachment with only DMA_TO_DEVICE direction for RO buffers for secure buffer access. nvgpu does RO GPU mapping if the buffer is RO for CPU or user requests to map as RO. In both cases the dma_buf map attachment should be done with DMA_TO_DEVICE direction as the intent for accessing the SGT is reading from GPU. Also, map the gpfifo buffer as read_only as it is intended to be read only. The userd buffer is accessed by the GPU through iova and is not GMMU mapped. Bug 200731819 Change-Id: Ifc60973f298f7cacab16c5dedecbb40c5f33ed1d Signed-off-by: Sagar Kamble Reviewed-on: https://git-master.nvidia.com/r/c/linux-nvgpu/+/2539312 Tested-by: mobile promotions Reviewed-by: Deepak Nibade Reviewed-by: Vijayakumar Subbu Reviewed-by: mobile promotions --- drivers/gpu/nvgpu/os/linux/cde.c | 2 +- drivers/gpu/nvgpu/os/linux/dmabuf_priv.c | 7 ++++--- drivers/gpu/nvgpu/os/linux/dmabuf_priv.h | 5 +++-- drivers/gpu/nvgpu/os/linux/linux-channel.c | 6 ++++-- drivers/gpu/nvgpu/os/linux/vm.c | 6 +++++- 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/drivers/gpu/nvgpu/os/linux/cde.c b/drivers/gpu/nvgpu/os/linux/cde.c index 3865857c7..c1a4961d1 100644 --- a/drivers/gpu/nvgpu/os/linux/cde.c +++ b/drivers/gpu/nvgpu/os/linux/cde.c @@ -1149,7 +1149,7 @@ __releases(&l->cde_app->mutex) nvgpu_log(g, gpu_dbg_cde, "surface=0x%p scatterBuffer=0x%p", surface, scatter_buffer); sgt = nvgpu_mm_pin(dev_from_gk20a(g), compbits_scatter_buf, - &attachment); + &attachment, DMA_BIDIRECTIONAL); if (IS_ERR(sgt)) { nvgpu_err(g, "mm_pin failed"); diff --git a/drivers/gpu/nvgpu/os/linux/dmabuf_priv.c b/drivers/gpu/nvgpu/os/linux/dmabuf_priv.c index 7daa52601..1c42c919e 100644 --- a/drivers/gpu/nvgpu/os/linux/dmabuf_priv.c +++ b/drivers/gpu/nvgpu/os/linux/dmabuf_priv.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2020, NVIDIA CORPORATION. All rights reserved. + * Copyright (c) 2017-2021, NVIDIA CORPORATION. All rights reserved. * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU General Public License, @@ -135,7 +135,8 @@ struct gk20a_dmabuf_priv *gk20a_dma_buf_get_drvdata( } struct sg_table *nvgpu_mm_pin(struct device *dev, - struct dma_buf *dmabuf, struct dma_buf_attachment **attachment) + struct dma_buf *dmabuf, struct dma_buf_attachment **attachment, + enum dma_data_direction direction) { struct gk20a *g = get_gk20a(dev); struct dma_buf_attachment *attach = NULL; @@ -148,7 +149,7 @@ struct sg_table *nvgpu_mm_pin(struct device *dev, return ERR_CAST(attach); } - sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL); + sgt = dma_buf_map_attachment(attach, direction); if (IS_ERR(sgt)) { dma_buf_detach(dmabuf, attach); nvgpu_err(g, "Failed to map attachment (err = %ld)!", diff --git a/drivers/gpu/nvgpu/os/linux/dmabuf_priv.h b/drivers/gpu/nvgpu/os/linux/dmabuf_priv.h index b79c56870..3df5b5d44 100644 --- a/drivers/gpu/nvgpu/os/linux/dmabuf_priv.h +++ b/drivers/gpu/nvgpu/os/linux/dmabuf_priv.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2020, NVIDIA CORPORATION. All rights reserved. + * Copyright (c) 2017-2021, NVIDIA CORPORATION. All rights reserved. * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU General Public License, @@ -99,7 +99,8 @@ struct gk20a_dmabuf_priv { struct sg_table *nvgpu_mm_pin(struct device *dev, struct dma_buf *dmabuf, - struct dma_buf_attachment **attachment); + struct dma_buf_attachment **attachment, + enum dma_data_direction direction); void nvgpu_mm_unpin(struct device *dev, struct dma_buf *dmabuf, diff --git a/drivers/gpu/nvgpu/os/linux/linux-channel.c b/drivers/gpu/nvgpu/os/linux/linux-channel.c index 732cbafb0..ad0d54366 100644 --- a/drivers/gpu/nvgpu/os/linux/linux-channel.c +++ b/drivers/gpu/nvgpu/os/linux/linux-channel.c @@ -41,6 +41,8 @@ #include #include +#include + #include #include @@ -439,7 +441,7 @@ int nvgpu_usermode_buf_from_dmabuf(struct gk20a *g, int dmabuf_fd, goto put_dmabuf; } - sgt = nvgpu_mm_pin(dev, dmabuf, &attachment); + sgt = nvgpu_mm_pin(dev, dmabuf, &attachment, DMA_TO_DEVICE); if (IS_ERR(sgt)) { nvgpu_err(g, "Failed to pin dma_buf!"); err = PTR_ERR(sgt); @@ -528,7 +530,7 @@ static int nvgpu_channel_alloc_usermode_buffers(struct nvgpu_channel *c, } c->usermode_gpfifo.gpu_va = nvgpu_gmmu_map(c->vm, &c->usermode_gpfifo, - c->usermode_gpfifo.size, 0, gk20a_mem_flag_none, + c->usermode_gpfifo.size, 0, gk20a_mem_flag_read_only, false, c->usermode_gpfifo.aperture); if (c->usermode_gpfifo.gpu_va == 0) { diff --git a/drivers/gpu/nvgpu/os/linux/vm.c b/drivers/gpu/nvgpu/os/linux/vm.c index 49dc5433e..cb71600ed 100644 --- a/drivers/gpu/nvgpu/os/linux/vm.c +++ b/drivers/gpu/nvgpu/os/linux/vm.c @@ -15,6 +15,7 @@ */ #include +#include #include #include @@ -265,7 +266,10 @@ int nvgpu_vm_map_linux(struct vm_gk20a *vm, return err; } - sgt = nvgpu_mm_pin(dev, dmabuf, &attachment); + sgt = nvgpu_mm_pin(dev, dmabuf, &attachment, + (buffer_rw_mode == gk20a_mem_flag_read_only) || + (map_access_requested == NVGPU_VM_MAP_ACCESS_READ_ONLY) ? + DMA_TO_DEVICE : DMA_BIDIRECTIONAL); if (IS_ERR(sgt)) { nvgpu_warn(g, "Failed to pin dma_buf!"); return PTR_ERR(sgt);