From f79070bcf817a63603203629b55f92dd341e1260 Mon Sep 17 00:00:00 2001 From: Chris Goldsworthy Date: Wed, 27 Apr 2022 19:21:47 -0700 Subject: [PATCH 1/2] qcom-dma-mapping: Take a snapshot of qcom-dma-mapping.h from msm-5.15 Take a snapshot of the qcom-dma-mapping.h header as of commit f843a80d2c45 ("Merge "defconfig: gen3auto: Separated if_changed"") on msm-5.15. Additionally, add the stubbed versions of the DMA_ATTR_SYS_CACHE_ONLY and DMA_ATTR_SYS_CACHE_ONLY_NWA attributes, as these are yet to be merged upstream. Change-Id: I3267f809622239ff13f0599d3a32c00b1dbf9f30 Signed-off-by: Chris Goldsworthy --- include/linux/qcom-dma-mapping.h | 47 ++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 include/linux/qcom-dma-mapping.h diff --git a/include/linux/qcom-dma-mapping.h b/include/linux/qcom-dma-mapping.h new file mode 100644 index 000000000000..32c14d19ae6a --- /dev/null +++ b/include/linux/qcom-dma-mapping.h @@ -0,0 +1,47 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (c) 2021, The Linux Foundation. All rights reserved. + */ + +/* + * DMA_ATTR_NO_DELAYED_UNMAP: Used by msm specific lazy mapping to indicate + * that the mapping can be freed on unmap, rather than when the ion_buffer + * is freed. + */ +#define DMA_ATTR_NO_DELAYED_UNMAP (1UL << 13) +/* + * When passed to a DMA map call the DMA_ATTR_FORCE_COHERENT DMA + * attribute can be used to force a buffer to be mapped as IO coherent. + */ +#define DMA_ATTR_FORCE_COHERENT (1UL << 15) +/* + * When passed to a DMA map call the DMA_ATTR_FORCE_NON_COHERENT DMA + * attribute can be used to force a buffer to not be mapped as IO + * coherent. + */ +#define DMA_ATTR_FORCE_NON_COHERENT (1UL << 16) +/* + * DMA_ATTR_DELAYED_UNMAP: Used by ION, it will ensure that mappings are not + * removed on unmap but instead are removed when the ion_buffer is freed. + */ +#define DMA_ATTR_DELAYED_UNMAP (1UL << 17) + +#ifndef DMA_ATTR_SYS_CACHE_ONLY +/* Attributes are not supported, so render them ineffective. */ +#define DMA_ATTR_SYS_CACHE_ONLY (0UL) +#define DMA_ATTR_SYS_CACHE_ONLY_NWA (0UL) +#endif + +/* + * DMA_ATTR_IOMMU_USE_UPSTREAM_HINT: Normally an smmu will override any bus + * attributes (i.e cacheablilty) provided by the client device. Some hardware + * may be designed to use the original attributes instead. + */ +#define DMA_ATTR_IOMMU_USE_UPSTREAM_HINT (DMA_ATTR_SYS_CACHE_ONLY) + +/* + * DMA_ATTR_IOMMU_USE_LLC_NWA: Overrides the bus attributes to use the System + * Cache(LLC) with allocation policy as Inner Non-Cacheable, Outer Cacheable: + * Write-Back, Read-Allocate, No Write-Allocate policy. + */ +#define DMA_ATTR_IOMMU_USE_LLC_NWA (DMA_ATTR_SYS_CACHE_ONLY_NWA) From e117a3efa448b3999e5c53acd8f18ed5512ba41c Mon Sep 17 00:00:00 2001 From: Chris Goldsworthy Date: Wed, 27 Apr 2022 20:35:23 -0700 Subject: [PATCH 2/2] mem-buf: Take a snapshot of mem-buf-exporter.h from msm-5.15 Take a snapshot of the qcom-dma-mapping.h header as of commit f843a80d2c45 ("Merge "defconfig: gen3auto: Separated if_changed"") on msm-5.15, excluding any Gunyah references (as the Gunyah driver has yet to be ported). Change-Id: I0549feb28eedb80668f1233576c365aefc3a8e6e Signed-off-by: Chris Goldsworthy --- include/linux/mem-buf-exporter.h | 111 +++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 include/linux/mem-buf-exporter.h diff --git a/include/linux/mem-buf-exporter.h b/include/linux/mem-buf-exporter.h new file mode 100644 index 000000000000..d1b86d36691d --- /dev/null +++ b/include/linux/mem-buf-exporter.h @@ -0,0 +1,111 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved. + */ + +#ifndef _MEM_BUF_EXPORTER_H +#define _MEM_BUF_EXPORTER_H + +#include +#include + +int mem_buf_dma_buf_attach(struct dma_buf *dmabuf, + struct dma_buf_attachment *attachment); + +/* Private data for managing a dmabuf's Virtual Machine permissions. */ +struct mem_buf_vmperm; + +/* + * @lookup: Returns the mem_buf_vmperm data structure contained somewhere + * in the exporter's private_data, or a negative number on error. + * @attach: The exporter's normal dma_buf_attach callback + * @dma_ops: The exporter's standard dma_buf callbacks, except for + * attach which must be NULL. + */ +struct mem_buf_dma_buf_ops { + struct mem_buf_vmperm *(*lookup)(struct dma_buf *dmabuf); + int (*attach)(struct dma_buf *dmabuf, struct dma_buf_attachment *a); + struct dma_buf_ops dma_ops; +}; + +struct dma_buf * +mem_buf_dma_buf_export(struct dma_buf_export_info *exp_info, + struct mem_buf_dma_buf_ops *ops); + + +#define MEM_BUF_WRAPPER_FLAG_STATIC_VM BIT(0) +#define MEM_BUF_WRAPPER_FLAG_LENDSHARE BIT(1) +#define MEM_BUF_WRAPPER_FLAG_ACCEPT BIT(2) +#define MEM_BUF_WRAPPER_FLAG_ERR BIT(3) + +/* + * A dmabuf owned by the current VM with RWX permissions. + * All variants should be free'd via mem_buf_vmperm_free(). + * + * @dmabuf: value returned from dma_buf_export() + * @sgt: Reference to the exporter's internal memory descriptor. + * Will not be freed by mem_buf_vmperm_free(). + */ +struct mem_buf_vmperm *mem_buf_vmperm_alloc(struct sg_table *sgt); + +/* + * A dmabuf which permantently belongs to the given VMs & permissions. + */ +struct mem_buf_vmperm *mem_buf_vmperm_alloc_staticvm(struct sg_table *sgt, int *vmids, int *perms, + u32 nr_acl_entries); + +/* + * Performs the expected close step based on whether the dmabuf + * is of the "STATICVM" "MEMACCEPT" or "DEFAULT" type. + * Exporters should call this from dma_buf_release. If this function + * Returns an error, exporters should consider the underlying memory + * to have undefined permissions. + */ +int mem_buf_vmperm_release(struct mem_buf_vmperm *vmperm); + +/* + * Pins ths permissions of the dmabuf. + * The exporter should call this from dma_buf_mmap, vm_ops->open(), and + * dma_buf_map_attachment. It must not be called from + * dma_buf_begin_cpu_access as userspace may call this function unsafely + * via DMA_BUF_IOCTL_SYNC. + */ +void mem_buf_vmperm_pin(struct mem_buf_vmperm *vmperm); + +/* + * Unpins ths permissions of the dmabuf. + * The exporter should call this from vm_ops->close(), and + * dma_buf_unmap_attachment. It must not be called from + * dma_buf_end_cpu_access as userspace may call this function unsafely + * via DMA_BUF_IOCTL_SYNC. + */ +void mem_buf_vmperm_unpin(struct mem_buf_vmperm *vmperm); + +/* + * Check whether the current permissions of the dmabuf allow CMO. + * Requires RW. + * + * The exporter should call this during dma_buf_map/unmap_attachment, + * and dma_buf_begin/end_cpu_access. + */ +bool mem_buf_vmperm_can_cmo(struct mem_buf_vmperm *vmperm); + +/* + * Check whether the current permissions of the dmabuf allow mmap. + * Requires at minimum the permissions specified in vma. + * + * The exporter should call mem_buf_vmperm_pin() from mmap. + * The exporter should implement the open() and close() operations of + * vma->vm_ops, and call mem_buf_vmperm_pin()/unpin() respectively. + */ +bool mem_buf_vmperm_can_mmap(struct mem_buf_vmperm *vmperm, struct vm_area_struct *vma); + +/* + * Check whether the current permissions of the dmabuf allow vmap. + * Requires RW. + * + * The exporter should call this during dma_buf_vmap, + */ +bool mem_buf_vmperm_can_vmap(struct mem_buf_vmperm *vmperm); + +#endif /*_MEM_BUF_EXPORTER_H */