media: ipu6: not override the dma_ops of device in driver
Subsystems (2)
PATCH v2 1/5 media: ipu6: not override the dma_ops of device in driver
From: Bingbu Cao <[email protected]>
DMA ops are a helper for architectures and not for drivers to override the DMA implementation. Driver should not override the DMA implementation.
This patch removes the dma_ops override from auxiliary device and sets up and expose its own IPU6 DMA mapping APIs, the calle use the mapping APIs to allocate/free DMA buffers, map/unmap the SG pages(likely allocated by IOMMU) to be accessible by IPU MMU and performs cache synchronization.
---
v2: reword the commit message to explain the patch actually does
--- drivers/media/pci/intel/ipu6/ipu6-bus.c | 6 - drivers/media/pci/intel/ipu6/ipu6-buttress.c | 21 +- drivers/media/pci/intel/ipu6/ipu6-cpd.c | 18 +- drivers/media/pci/intel/ipu6/ipu6-dma.c | 195 +++++++++---------- drivers/media/pci/intel/ipu6/ipu6-dma.h | 34 +++- drivers/media/pci/intel/ipu6/ipu6-fw-com.c | 14 +- 6 files changed, 156 insertions(+), 132 deletions(-)
drivers/media/pci/intel/ipu6/ipu6-bus.c +0 -6
@@ -94,8 +94,6 @@ ipu6_bus_initialize_device(struct pci_dev *pdev, struct device *parent,if (!adev)return ERR_PTR (-ENOMEM ); - adev->dma_mask =DMA_BIT_MASK (isp->secure_mode ?IPU6_MMU_ADDR_BITS : -IPU6_MMU_ADDR_BITS_NON_SECURE ); adev->isp = isp; adev->ctrl = ctrl; adev->pdata = pdata;
@@ -106,10 +104,6 @@ ipu6_bus_initialize_device(struct pci_dev *pdev, struct device *parent, auxdev->dev.parent = parent; auxdev->dev.release = ipu6_bus_release; - auxdev->dev.dma_ops = &ipu6_dma_ops; - auxdev->dev.dma_mask = &adev->dma_mask; - auxdev->dev.dma_parms = pdev->dev.dma_parms; - auxdev->dev.coherent_dma_mask = adev->dma_mask; ret =auxiliary_device_init (auxdev);if (ret <0 ) {
drivers/media/pci/intel/ipu6/ipu6-buttress.c +16 -5
@@ -24,6 +24,7 @@#include "ipu6.h" #include "ipu6-bus.h" +#include "ipu6-dma.h" #include "ipu6-buttress.h" #include "ipu6-platform-buttress-regs.h"
@@ -553,6 +554,7 @@ int ipu6_buttress_map_fw_image(struct ipu6_bus_device *sys, const struct firmware *fw, struct sg_table *sgt) { bool is_vmalloc = is_vmalloc_addr(fw->data); +struct pci_dev *pdev = sys->isp->pdev;struct page **pages;const void *addr;unsigned long n_pages;
@@ -588,14 +590,20 @@ int ipu6_buttress_map_fw_image(struct ipu6_bus_device *sys, goto out; } - ret =dma_map_sgtable (&sys->auxdev.dev, sgt,DMA_TO_DEVICE ,0 ); -if (ret <0 ) { - ret = -ENOMEM ; + ret =dma_map_sgtable (&pdev->dev, sgt,DMA_TO_DEVICE ,0 ); +if (ret) { +sg_free_table (sgt); + goto out; + } + + ret =ipu6_dma_map_sgtable (sys, sgt,DMA_TO_DEVICE ,0 ); +if (ret) { +dma_unmap_sgtable (&pdev->dev, sgt,DMA_TO_DEVICE ,0 );sg_free_table (sgt); goto out; } -dma_sync_sgtable_for_device (&sys->auxdev.dev, sgt,DMA_TO_DEVICE ); +ipu6_dma_sync_sgtable (sys, sgt); out:kfree (pages);
@@ -607,7 +615,10 @@ EXPORT_SYMBOL_NS_GPL(ipu6_buttress_map_fw_image, INTEL_IPU6);void ipu6_buttress_unmap_fw_image (struct ipu6_bus_device *sys,struct sg_table *sgt) { -dma_unmap_sgtable (&sys->auxdev.dev, sgt,DMA_TO_DEVICE ,0 ); +struct pci_dev *pdev = sys->isp->pdev; + +ipu6_dma_unmap_sgtable (sys, sgt,DMA_TO_DEVICE ,0 ); +dma_unmap_sgtable (&pdev->dev, sgt,DMA_TO_DEVICE ,0 );sg_free_table (sgt); }EXPORT_SYMBOL_NS_GPL (ipu6_buttress_unmap_fw_image,INTEL_IPU6 );
drivers/media/pci/intel/ipu6/ipu6-cpd.c +9 -9
@@ -15,6 +15,7 @@#include "ipu6.h" #include "ipu6-bus.h" #include "ipu6-cpd.h" +#include "ipu6-dma.h" /* 15 entries + header*/ #define MAX_PKG_DIR_ENT_CNT 16
@@ -162,7 +163,6 @@ int ipu6_cpd_create_pkg_dir(struct ipu6_bus_device *adev, const void *src) {dma_addr_t dma_addr_src =sg_dma_address (adev->fw_sgt.sgl);const struct ipu6_cpd_ent *ent, *man_ent, *met_ent; -struct device *dev = &adev->auxdev.dev;struct ipu6_device *isp = adev->isp;unsigned int man_sz, met_sz;void *pkg_dir_pos;
@@ -175,8 +175,8 @@ int ipu6_cpd_create_pkg_dir(struct ipu6_bus_device *adev, const void *src) met_sz = met_ent->len; adev->pkg_dir_size =PKG_DIR_SIZE + man_sz + met_sz; - adev->pkg_dir =dma_alloc_attrs (dev, adev->pkg_dir_size, - &adev->pkg_dir_dma_addr,GFP_KERNEL ,0 ); + adev->pkg_dir =ipu6_dma_alloc (adev, adev->pkg_dir_size, + &adev->pkg_dir_dma_addr,GFP_KERNEL ,0 );if (!adev->pkg_dir)return -ENOMEM ;
@@ -198,8 +198,8 @@ int ipu6_cpd_create_pkg_dir(struct ipu6_bus_device *adev, const void *src) met_ent->len);if (ret) {dev_err (&isp->pdev->dev,"Failed to parse module data\n" ); -dma_free_attrs (dev, adev->pkg_dir_size, - adev->pkg_dir, adev->pkg_dir_dma_addr,0 ); +ipu6_dma_free (adev, adev->pkg_dir_size, + adev->pkg_dir, adev->pkg_dir_dma_addr,0 );return ret; }
@@ -211,8 +211,8 @@ int ipu6_cpd_create_pkg_dir(struct ipu6_bus_device *adev, const void *src) pkg_dir_pos += man_sz;memcpy (pkg_dir_pos, src + met_ent->offset, met_sz); -dma_sync_single_range_for_device (dev, adev->pkg_dir_dma_addr, -0 , adev->pkg_dir_size,DMA_TO_DEVICE ); +ipu6_dma_sync_single (adev, adev->pkg_dir_dma_addr, + adev->pkg_dir_size);return 0 ; }
@@ -220,8 +220,8 @@ EXPORT_SYMBOL_NS_GPL(ipu6_cpd_create_pkg_dir, INTEL_IPU6);void ipu6_cpd_free_pkg_dir (struct ipu6_bus_device *adev) { -dma_free_attrs (&adev->auxdev.dev, adev->pkg_dir_size, adev->pkg_dir, - adev->pkg_dir_dma_addr,0 ); +ipu6_dma_free (adev, adev->pkg_dir_size, adev->pkg_dir, + adev->pkg_dir_dma_addr,0 ); }EXPORT_SYMBOL_NS_GPL (ipu6_cpd_free_pkg_dir,INTEL_IPU6 );
drivers/media/pci/intel/ipu6/ipu6-dma.c +92 -103
357 lines not on this page · raw message
drivers/media/pci/intel/ipu6/ipu6-dma.h +32 -2
@@ -5,7 +5,13 @@#define IPU6_DMA_H #include <linux/dma-map-ops.h> +#include <linux/dma-mapping.h> #include <linux/iova.h> +#include <linux/iova.h> +#include <linux/scatterlist.h> +#include <linux/types.h> + +#include "ipu6-bus.h" struct ipu6_mmu_info ;
@@ -14,6 +20,30 @@ struct ipu6_dma_mapping {struct iova_domain iovad; }; -extern const struct dma_map_ops ipu6_dma_ops; - +void ipu6_dma_sync_single (struct ipu6_bus_device *sys,dma_addr_t dma_handle, +size_t size); +void ipu6_dma_sync_sg (struct ipu6_bus_device *sys,struct scatterlist *sglist, +int nents); +void ipu6_dma_sync_sgtable (struct ipu6_bus_device *sys,struct sg_table *sgt); +void *ipu6_dma_alloc (struct ipu6_bus_device *sys,size_t size, +dma_addr_t *dma_handle,gfp_t gfp, +unsigned long attrs); +void ipu6_dma_free (struct ipu6_bus_device *sys,size_t size,void *vaddr, +dma_addr_t dma_handle,unsigned long attrs); +int ipu6_dma_mmap (struct ipu6_bus_device *sys,struct vm_area_struct *vma, +void *addr,dma_addr_t iova,size_t size, +unsigned long attrs); +int ipu6_dma_map_sg (struct ipu6_bus_device *sys,struct scatterlist *sglist, +int nents,enum dma_data_direction dir, +unsigned long attrs); +void ipu6_dma_unmap_sg (struct ipu6_bus_device *sys,struct scatterlist *sglist, +int nents,enum dma_data_direction dir, +unsigned long attrs); +int ipu6_dma_map_sgtable (struct ipu6_bus_device *sys,struct sg_table *sgt, +enum dma_data_direction dir,unsigned long attrs); +void ipu6_dma_unmap_sgtable (struct ipu6_bus_device *sys,struct sg_table *sgt, +enum dma_data_direction dir,unsigned long attrs); +int ipu6_dma_get_sgtable (struct ipu6_bus_device *sys,struct sg_table *sgt, +void *cpu_addr,dma_addr_t handle,size_t size, +unsigned long attrs);#endif /* IPU6_DMA_H */
drivers/media/pci/intel/ipu6/ipu6-fw-com.c +7 -7
@@ -12,6 +12,7 @@#include <linux/types.h> #include "ipu6-bus.h" +#include "ipu6-dma.h" #include "ipu6-fw-com.h" /*
@@ -88,7 +89,6 @@ struct ipu6_fw_com_context {void *dma_buffer;dma_addr_t dma_addr;unsigned int dma_size; -unsigned long attrs;struct ipu6_fw_sys_queue *input_queue;/* array of host to SP queues */ struct ipu6_fw_sys_queue *output_queue;/* array of SP to host */
@@ -164,7 +164,6 @@ void *ipu6_fw_com_prepare(struct ipu6_fw_com_cfg *cfg,struct ipu6_fw_com_context *ctx;struct device *dev = &adev->auxdev.dev;size_t sizeall, offset; -unsigned long attrs =0 ;void *specific_host_addr;unsigned int i;
@@ -206,9 +205,8 @@ void *ipu6_fw_com_prepare(struct ipu6_fw_com_cfg *cfg, sizeall += sizeinput + sizeoutput; - ctx->dma_buffer =dma_alloc_attrs (dev, sizeall, &ctx->dma_addr, -GFP_KERNEL , attrs); - ctx->attrs = attrs; + ctx->dma_buffer =ipu6_dma_alloc (adev, sizeall, &ctx->dma_addr, +GFP_KERNEL ,0 );if (!ctx->dma_buffer) {dev_err (dev,"failed to allocate dma memory\n" );kfree (ctx);
@@ -239,6 +237,8 @@ void *ipu6_fw_com_prepare(struct ipu6_fw_com_cfg *cfg,memcpy (specific_host_addr, cfg->specific_addr, cfg->specific_size); +ipu6_dma_sync_single (adev, ctx->config_vied_addr, sizeall); +/* initialize input queues */ offset += specific_size; res.reg =SYSCOM_QPR_BASE_REG ;
@@ -315,8 +315,8 @@ int ipu6_fw_com_release(struct ipu6_fw_com_context *ctx, unsigned int force)if (!force && !ctx->cell_ready (ctx->adev))return -EBUSY ; -dma_free_attrs (&ctx->adev->auxdev.dev, ctx->dma_size, - ctx->dma_buffer, ctx->dma_addr, ctx->attrs); +ipu6_dma_free (ctx->adev, ctx->dma_size, + ctx->dma_buffer, ctx->dma_addr,0 );kfree (ctx);return 0 ; }
Signature · 1 line
2.34.1
PATCH v2 2/5 media: ipu6: use the IPU6 DMA mapping APIs to do mapping
From: Bingbu Cao <[email protected]>
dma_ops is removed from the IPU6 auxiliary device, ISYS driver should use the IPU6 DMA mapping APIs directly instead of depending on the device callbacks.
ISYS driver switch from the videobuf2 DMA contig memory allocator to scatter/gather memory allocator.
--- drivers/media/pci/intel/ipu6/Kconfig | 2 +- .../media/pci/intel/ipu6/ipu6-isys-queue.c | 66 +++++++++++++++---- .../media/pci/intel/ipu6/ipu6-isys-queue.h | 1 + drivers/media/pci/intel/ipu6/ipu6-isys.c | 19 +++--- 4 files changed, 64 insertions(+), 24 deletions(-)
drivers/media/pci/intel/ipu6/Kconfig +1 -1
@@ -14,7 +14,7 @@ config VIDEO_INTEL_IPU6 select IOMMU_IOVA select VIDEO_V4L2_SUBDEV_API select MEDIA_CONTROLLER - select VIDEOBUF2_DMA_CONTIG + select VIDEOBUF2_DMA_SG select V4L2_FWNODE help This is the 6th Gen Intel Image Processing Unit, found in Intel SoCs
drivers/media/pci/intel/ipu6/ipu6-isys-queue.c +53 -13
130 lines not on this page · raw message
drivers/media/pci/intel/ipu6/ipu6-isys-queue.h +1 -0
@@ -38,6 +38,7 @@ struct ipu6_isys_buffer {struct ipu6_isys_video_buffer {struct vb2_v4l2_buffer vb_v4l2;struct ipu6_isys_buffer ib; +dma_addr_t dma_addr; };#define IPU6_ISYS_BUFFER_LIST_FL_INCOMING BIT(0)
drivers/media/pci/intel/ipu6/ipu6-isys.c +9 -10
@@ -34,6 +34,7 @@#include "ipu6-bus.h" #include "ipu6-cpd.h" +#include "ipu6-dma.h" #include "ipu6-isys.h" #include "ipu6-isys-csi2.h" #include "ipu6-mmu.h"
@@ -933,29 +934,27 @@ static const struct dev_pm_ops isys_pm_ops = {static void free_fw_msg_bufs (struct ipu6_isys *isys) { -struct device *dev = &isys->adev->auxdev.dev;struct isys_fw_msgs *fwmsg, *safe;list_for_each_entry_safe (fwmsg, safe, &isys->framebuflist, head) -dma_free_attrs (dev,sizeof (struct isys_fw_msgs ), fwmsg, - fwmsg->dma_addr,0 ); +ipu6_dma_free (isys->adev,sizeof (struct isys_fw_msgs ), fwmsg, + fwmsg->dma_addr,0 );list_for_each_entry_safe (fwmsg, safe, &isys->framebuflist_fw, head) -dma_free_attrs (dev,sizeof (struct isys_fw_msgs ), fwmsg, - fwmsg->dma_addr,0 ); +ipu6_dma_free (isys->adev,sizeof (struct isys_fw_msgs ), fwmsg, + fwmsg->dma_addr,0 ); }static int alloc_fw_msg_bufs (struct ipu6_isys *isys,int amount) { -struct device *dev = &isys->adev->auxdev.dev;struct isys_fw_msgs *addr;dma_addr_t dma_addr;unsigned long flags;unsigned int i;for (i =0 ; i < amount; i++) { - addr =dma_alloc_attrs (dev,sizeof (struct isys_fw_msgs ), - &dma_addr,GFP_KERNEL ,0 ); + addr =ipu6_dma_alloc (isys->adev,sizeof (*addr), + &dma_addr,GFP_KERNEL ,0 );if (!addr)break ; addr->dma_addr = dma_addr;
@@ -974,8 +973,8 @@ static int alloc_fw_msg_bufs(struct ipu6_isys *isys, int amount) struct isys_fw_msgs, head);list_del (&addr->head);spin_unlock_irqrestore (&isys->listlock, flags); -dma_free_attrs (dev,sizeof (struct isys_fw_msgs ), addr, - addr->dma_addr,0 ); +ipu6_dma_free (isys->adev,sizeof (struct isys_fw_msgs ), addr, + addr->dma_addr,0 );spin_lock_irqsave (&isys->listlock, flags); }spin_unlock_irqrestore (&isys->listlock, flags);
Signature · 1 line
2.34.1
PATCH v2 3/5 media: ipu6: remove architecture DMA ops dependency in Kconfig
In reply to [email protected]From: Bingbu Cao <[email protected]>
IPU6 driver doesn't override the dma_ops of device now, it doesn't depends on the ARCH_HAS_DMA_OPS, so remove the dependency in Kconfig.
--- drivers/media/pci/intel/ipu6/Kconfig | 6 ------ 1 file changed, 6 deletions(-)
drivers/media/pci/intel/ipu6/Kconfig +0 -6
@@ -4,12 +4,6 @@ config VIDEO_INTEL_IPU6 depends on VIDEO_DEV depends on X86 && X86_64 && HAS_DMA depends on IPU_BRIDGE || !IPU_BRIDGE - # - # This driver incorrectly tries to override the dma_ops. It should - # never have done that, but for now keep it working on architectures - # that use dma ops - # - depends on ARCH_HAS_DMA_OPS select AUXILIARY_BUS select IOMMU_IOVA select VIDEO_V4L2_SUBDEV_API
Signature · 1 line
2.34.1
PATCH v2 4/5 Documentation: ipu6: remove the dma_ops part from the doc
In reply to [email protected]From: Bingbu Cao <[email protected]>
dma_ops override is not in code anymore, so remove it.
--- Documentation/driver-api/media/drivers/ipu6.rst | 15 --------------- 1 file changed, 15 deletions(-)
Documentation/driver-api/media/drivers/ipu6.rst +0 -15
@@ -98,21 +98,6 @@ The IPU6 driver exports its own DMA operations. The IPU6 driver will update the page table entries for each DMA operation and invalidate the MMU TLB after each unmap and free. -.. code-block:: none - - const struct dma_map_ops ipu6_dma_ops = { - .alloc = ipu6_dma_alloc, - .free = ipu6_dma_free, - .mmap = ipu6_dma_mmap, - .map_sg = ipu6_dma_map_sg, - .unmap_sg = ipu6_dma_unmap_sg, - ... - }; - -.. Note:: IPU6 MMU works behind IOMMU so for each IPU6 DMA ops, driver will call - generic PCI DMA ops to ask IOMMU to do the additional mapping if VT-d - enabled. - Firmware file format ====================
Signature · 1 line
2.34.1
PATCH v2 5/5 media: ipu6: use PFN_UP() and sg_virt() for code simplicity
In reply to [email protected]From: Bingbu Cao <[email protected]>
Use PFN_UP() and sg_virt() can be used to simplify the code.
--- drivers/media/pci/intel/ipu6/ipu6-buttress.c | 2 +- drivers/media/pci/intel/ipu6/ipu6-dma.c | 8 ++++---- drivers/media/pci/intel/ipu6/ipu6-mmu.c | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-)
drivers/media/pci/intel/ipu6/ipu6-buttress.c +1 -1
@@ -564,7 +564,7 @@ int ipu6_buttress_map_fw_image(struct ipu6_bus_device *sys,if (!is_vmalloc && !virt_addr_valid (fw->data))return -EDOM ; - n_pages =PHYS_PFN (PAGE_ALIGN (fw->size)); + n_pages =PFN_UP (fw->size); pages =kmalloc_array (n_pages,sizeof (*pages),GFP_KERNEL );if (!pages)
drivers/media/pci/intel/ipu6/ipu6-dma.c +4 -4
@@ -139,7 +139,7 @@ void ipu6_dma_sync_sg(struct ipu6_bus_device *sys, struct scatterlist *sglist,int i;for_each_sg (sglist, sg, nents, i) -clflush_cache_range (page_to_virt (sg_page (sg)), sg->length); +clflush_cache_range (sg_virt (sg), sg->length); }EXPORT_SYMBOL_NS_GPL (ipu6_dma_sync_sg,INTEL_IPU6 );
@@ -392,7 +392,7 @@ int ipu6_dma_map_sg(struct ipu6_bus_device *sys, struct scatterlist *sglist, }for_each_sg (sglist, sg, nents, i) - npages +=PHYS_PFN (PAGE_ALIGN (sg_dma_len (sg))); + npages +=PFN_UP (sg_dma_len (sg));dev_dbg (dev,"dmamap trying to map %d ents %zu pages\n" , nents, npages);
@@ -421,7 +421,7 @@ int ipu6_dma_map_sg(struct ipu6_bus_device *sys, struct scatterlist *sglist,sg_dma_address (sg) =PFN_PHYS (iova_addr); - iova_addr +=PHYS_PFN (PAGE_ALIGN (sg_dma_len (sg))); + iova_addr +=PFN_UP (sg_dma_len (sg)); }dev_dbg (dev,"dmamap %d ents %zu pages mapped\n" , nents, npages);
@@ -480,7 +480,7 @@ int ipu6_dma_get_sgtable(struct ipu6_bus_device *sys, struct sg_table *sgt,if (WARN_ON (!info->pages))return -ENOMEM ; - n_pages =PHYS_PFN (PAGE_ALIGN (size)); + n_pages =PFN_UP (size); ret =sg_alloc_table_from_pages (sgt, info->pages, n_pages,0 , size,GFP_KERNEL );
drivers/media/pci/intel/ipu6/ipu6-mmu.c +1 -1
@@ -409,7 +409,7 @@ static void __ipu6_mmu_unmap(struct ipu6_mmu_info *mmu_info,static int allocate_trash_buffer (struct ipu6_mmu *mmu) { -unsigned int n_pages =PHYS_PFN (PAGE_ALIGN (IPU6_MMUV2_TRASH_RANGE )); +unsigned int n_pages =PFN_UP (IPU6_MMUV2_TRASH_RANGE );struct iova *iova;unsigned int i;dma_addr_t dma;
Signature · 1 line
2.34.1
Thanks a lot for doing this work!