[PATCH 1/2] [v2] drm/nouveau: retain device pointer in nvkm_gsp_mem object

Danilo Krummrich dakr at redhat.com
Mon Jun 17 17:48:43 UTC 2024


On Wed, Jun 12, 2024 at 06:52:52PM -0500, Timur Tabi wrote:
> Store the struct device pointer used to allocate the DMA buffer in
> the nvkm_gsp_mem object.  This allows nvkm_gsp_mem_dtor() to release
> the buffer without needing the nvkm_gsp.  This is needed so that
> we can retain DMA buffers even after the nvkm_gsp object is deleted.
> 
> Signed-off-by: Timur Tabi <ttabi at nvidia.com>
> ---
> v2: rebased to drm-misc-next
> 
>  .../gpu/drm/nouveau/include/nvkm/subdev/gsp.h |  1 +
>  .../gpu/drm/nouveau/nvkm/subdev/gsp/r535.c    | 36 ++++++++++---------
>  2 files changed, 20 insertions(+), 17 deletions(-)
> 
> diff --git a/drivers/gpu/drm/nouveau/include/nvkm/subdev/gsp.h b/drivers/gpu/drm/nouveau/include/nvkm/subdev/gsp.h
> index 9e6f39912368..a45a4ad843b9 100644
> --- a/drivers/gpu/drm/nouveau/include/nvkm/subdev/gsp.h
> +++ b/drivers/gpu/drm/nouveau/include/nvkm/subdev/gsp.h
> @@ -9,6 +9,7 @@
>  #define GSP_PAGE_SIZE  BIT(GSP_PAGE_SHIFT)
>  
>  struct nvkm_gsp_mem {
> +	struct device *dev;
>  	size_t size;
>  	void *data;
>  	dma_addr_t addr;
> diff --git a/drivers/gpu/drm/nouveau/nvkm/subdev/gsp/r535.c b/drivers/gpu/drm/nouveau/nvkm/subdev/gsp/r535.c
> index cf58f9da9139..bbab6d452aa2 100644
> --- a/drivers/gpu/drm/nouveau/nvkm/subdev/gsp/r535.c
> +++ b/drivers/gpu/drm/nouveau/nvkm/subdev/gsp/r535.c
> @@ -1000,7 +1000,7 @@ r535_gsp_rpc_get_gsp_static_info(struct nvkm_gsp *gsp)
>  }
>  
>  static void
> -nvkm_gsp_mem_dtor(struct nvkm_gsp *gsp, struct nvkm_gsp_mem *mem)
> +nvkm_gsp_mem_dtor(struct nvkm_gsp_mem *mem)
>  {
>  	if (mem->data) {
>  		/*
> @@ -1009,7 +1009,7 @@ nvkm_gsp_mem_dtor(struct nvkm_gsp *gsp, struct nvkm_gsp_mem *mem)
>  		 */
>  		memset(mem->data, 0xFF, mem->size);
>  
> -		dma_free_coherent(gsp->subdev.device->dev, mem->size, mem->data, mem->addr);
> +		dma_free_coherent(mem->dev, mem->size, mem->data, mem->addr);
>  		memset(mem, 0, sizeof(*mem));
>  	}
>  }
> @@ -1017,11 +1017,13 @@ nvkm_gsp_mem_dtor(struct nvkm_gsp *gsp, struct nvkm_gsp_mem *mem)
>  static int
>  nvkm_gsp_mem_ctor(struct nvkm_gsp *gsp, size_t size, struct nvkm_gsp_mem *mem)
>  {
> -	mem->size = size;
>  	mem->data = dma_alloc_coherent(gsp->subdev.device->dev, size, &mem->addr, GFP_KERNEL);
>  	if (WARN_ON(!mem->data))
>  		return -ENOMEM;
>  
> +	mem->size = size;
> +	mem->dev = gsp->subdev.device->dev;

If this can potentially out-live the drivers remove() callback, we have to take
a device reference here and drop it in nvkm_gsp_mem_dtor().

	mem->dev = get_device(gsp->subdev.device->dev);

> +
>  	return 0;
>  }
>  
> @@ -1054,8 +1056,8 @@ r535_gsp_postinit(struct nvkm_gsp *gsp)
>  	nvkm_wr32(device, 0x110004, 0x00000040);
>  
>  	/* Release the DMA buffers that were needed only for boot and init */
> -	nvkm_gsp_mem_dtor(gsp, &gsp->boot.fw);
> -	nvkm_gsp_mem_dtor(gsp, &gsp->libos);
> +	nvkm_gsp_mem_dtor(&gsp->boot.fw);
> +	nvkm_gsp_mem_dtor(&gsp->libos);
>  
>  	return ret;
>  }
> @@ -2234,8 +2236,8 @@ static void
>  nvkm_gsp_radix3_dtor(struct nvkm_gsp *gsp, struct nvkm_gsp_radix3 *rx3)
>  {
>  	nvkm_gsp_sg_free(gsp->subdev.device, &rx3->lvl2);
> -	nvkm_gsp_mem_dtor(gsp, &rx3->lvl1);
> -	nvkm_gsp_mem_dtor(gsp, &rx3->lvl0);
> +	nvkm_gsp_mem_dtor(&rx3->lvl1);
> +	nvkm_gsp_mem_dtor(&rx3->lvl0);
>  }
>  
>  /**
> @@ -2323,9 +2325,9 @@ nvkm_gsp_radix3_sg(struct nvkm_gsp *gsp, struct sg_table *sgt, u64 size,
>  
>  	if (ret) {
>  lvl2_fail:
> -		nvkm_gsp_mem_dtor(gsp, &rx3->lvl1);
> +		nvkm_gsp_mem_dtor(&rx3->lvl1);
>  lvl1_fail:
> -		nvkm_gsp_mem_dtor(gsp, &rx3->lvl0);
> +		nvkm_gsp_mem_dtor(&rx3->lvl0);
>  	}
>  
>  	return ret;
> @@ -2417,7 +2419,7 @@ r535_gsp_init(struct nvkm_gsp *gsp)
>  
>  done:
>  	if (gsp->sr.meta.data) {
> -		nvkm_gsp_mem_dtor(gsp, &gsp->sr.meta);
> +		nvkm_gsp_mem_dtor(&gsp->sr.meta);
>  		nvkm_gsp_radix3_dtor(gsp, &gsp->sr.radix3);
>  		nvkm_gsp_sg_free(gsp->subdev.device, &gsp->sr.sgt);
>  		return ret;
> @@ -2498,7 +2500,7 @@ r535_gsp_dtor(struct nvkm_gsp *gsp)
>  	mutex_destroy(&gsp->client_id.mutex);
>  
>  	nvkm_gsp_radix3_dtor(gsp, &gsp->radix3);
> -	nvkm_gsp_mem_dtor(gsp, &gsp->sig);
> +	nvkm_gsp_mem_dtor(&gsp->sig);
>  	nvkm_firmware_dtor(&gsp->fw);
>  
>  	nvkm_falcon_fw_dtor(&gsp->booter.unload);
> @@ -2509,12 +2511,12 @@ r535_gsp_dtor(struct nvkm_gsp *gsp)
>  
>  	r535_gsp_dtor_fws(gsp);
>  
> -	nvkm_gsp_mem_dtor(gsp, &gsp->rmargs);
> -	nvkm_gsp_mem_dtor(gsp, &gsp->wpr_meta);
> -	nvkm_gsp_mem_dtor(gsp, &gsp->shm.mem);
> -	nvkm_gsp_mem_dtor(gsp, &gsp->loginit);
> -	nvkm_gsp_mem_dtor(gsp, &gsp->logintr);
> -	nvkm_gsp_mem_dtor(gsp, &gsp->logrm);
> +	nvkm_gsp_mem_dtor(&gsp->rmargs);
> +	nvkm_gsp_mem_dtor(&gsp->wpr_meta);
> +	nvkm_gsp_mem_dtor(&gsp->shm.mem);
> +	nvkm_gsp_mem_dtor(&gsp->loginit);
> +	nvkm_gsp_mem_dtor(&gsp->logintr);
> +	nvkm_gsp_mem_dtor(&gsp->logrm);
>  }
>  
>  int
> 
> base-commit: a13aaf157467e694a3824d81304106b58d4c20d6
> prerequisite-patch-id: 1428f57d0b137672ec09da08e76c5d3069b35432
> -- 
> 2.34.1
> 



More information about the Nouveau mailing list