1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved 4 * 5 * DMA operations that map physical memory through IOMMU. 6 */ 7 #ifndef _LINUX_IOMMU_DMA_H 8 #define _LINUX_IOMMU_DMA_H 9 10 #include <linux/dma-direction.h> 11 12 #ifdef CONFIG_IOMMU_DMA 13 static inline bool use_dma_iommu(struct device *dev) 14 { 15 return dev->dma_iommu; 16 } 17 dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page, 18 unsigned long offset, size_t size, enum dma_data_direction dir, 19 unsigned long attrs); 20 void iommu_dma_unmap_page(struct device *dev, dma_addr_t dma_handle, 21 size_t size, enum dma_data_direction dir, unsigned long attrs); 22 int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents, 23 enum dma_data_direction dir, unsigned long attrs); 24 void iommu_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, 25 enum dma_data_direction dir, unsigned long attrs); 26 void *iommu_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, 27 gfp_t gfp, unsigned long attrs); 28 int iommu_dma_mmap(struct device *dev, struct vm_area_struct *vma, 29 void *cpu_addr, dma_addr_t dma_addr, size_t size, 30 unsigned long attrs); 31 int iommu_dma_get_sgtable(struct device *dev, struct sg_table *sgt, 32 void *cpu_addr, dma_addr_t dma_addr, size_t size, 33 unsigned long attrs); 34 unsigned long iommu_dma_get_merge_boundary(struct device *dev); 35 size_t iommu_dma_opt_mapping_size(void); 36 size_t iommu_dma_max_mapping_size(struct device *dev); 37 void iommu_dma_free(struct device *dev, size_t size, void *cpu_addr, 38 dma_addr_t handle, unsigned long attrs); 39 dma_addr_t iommu_dma_map_resource(struct device *dev, phys_addr_t phys, 40 size_t size, enum dma_data_direction dir, unsigned long attrs); 41 void iommu_dma_unmap_resource(struct device *dev, dma_addr_t handle, 42 size_t size, enum dma_data_direction dir, unsigned long attrs); 43 struct sg_table *iommu_dma_alloc_noncontiguous(struct device *dev, size_t size, 44 enum dma_data_direction dir, gfp_t gfp, unsigned long attrs); 45 void iommu_dma_free_noncontiguous(struct device *dev, size_t size, 46 struct sg_table *sgt, enum dma_data_direction dir); 47 void iommu_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle, 48 size_t size, enum dma_data_direction dir); 49 void iommu_dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle, 50 size_t size, enum dma_data_direction dir); 51 void iommu_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sgl, 52 int nelems, enum dma_data_direction dir); 53 void iommu_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sgl, 54 int nelems, enum dma_data_direction dir); 55 #else 56 static inline bool use_dma_iommu(struct device *dev) 57 { 58 return false; 59 } 60 static inline dma_addr_t iommu_dma_map_page(struct device *dev, 61 struct page *page, unsigned long offset, size_t size, 62 enum dma_data_direction dir, unsigned long attrs) 63 { 64 return DMA_MAPPING_ERROR; 65 } 66 static inline void iommu_dma_unmap_page(struct device *dev, 67 dma_addr_t dma_handle, size_t size, enum dma_data_direction dir, 68 unsigned long attrs) 69 { 70 } 71 static inline int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg, 72 int nents, enum dma_data_direction dir, unsigned long attrs) 73 { 74 return -EINVAL; 75 } 76 static inline void iommu_dma_unmap_sg(struct device *dev, 77 struct scatterlist *sg, int nents, enum dma_data_direction dir, 78 unsigned long attrs) 79 { 80 } 81 static inline void *iommu_dma_alloc(struct device *dev, size_t size, 82 dma_addr_t *handle, gfp_t gfp, unsigned long attrs) 83 { 84 return NULL; 85 } 86 static inline int iommu_dma_mmap(struct device *dev, struct vm_area_struct *vma, 87 void *cpu_addr, dma_addr_t dma_addr, size_t size, 88 unsigned long attrs) 89 { 90 return -EINVAL; 91 } 92 static inline int iommu_dma_get_sgtable(struct device *dev, 93 struct sg_table *sgt, void *cpu_addr, dma_addr_t dma_addr, 94 size_t size, unsigned long attrs) 95 { 96 return -EINVAL; 97 } 98 static inline unsigned long iommu_dma_get_merge_boundary(struct device *dev) 99 { 100 return 0; 101 } 102 static inline size_t iommu_dma_opt_mapping_size(void) 103 { 104 return 0; 105 } 106 static inline size_t iommu_dma_max_mapping_size(struct device *dev) 107 { 108 return 0; 109 } 110 static inline void iommu_dma_free(struct device *dev, size_t size, 111 void *cpu_addr, dma_addr_t handle, unsigned long attrs) 112 { 113 } 114 static inline dma_addr_t iommu_dma_map_resource(struct device *dev, 115 phys_addr_t phys, size_t size, enum dma_data_direction dir, 116 unsigned long attrs) 117 { 118 return DMA_MAPPING_ERROR; 119 } 120 static inline void iommu_dma_unmap_resource(struct device *dev, 121 dma_addr_t handle, size_t size, enum dma_data_direction dir, 122 unsigned long attrs) 123 { 124 } 125 static inline struct sg_table * 126 iommu_dma_alloc_noncontiguous(struct device *dev, size_t size, 127 enum dma_data_direction dir, gfp_t gfp, unsigned long attrs) 128 { 129 return NULL; 130 } 131 static inline void iommu_dma_free_noncontiguous(struct device *dev, size_t size, 132 struct sg_table *sgt, enum dma_data_direction dir) 133 { 134 } 135 static inline void iommu_dma_sync_single_for_cpu(struct device *dev, 136 dma_addr_t dma_handle, size_t size, 137 enum dma_data_direction dir) 138 { 139 } 140 static inline void iommu_dma_sync_single_for_device(struct device *dev, 141 dma_addr_t dma_handle, size_t size, enum dma_data_direction dir) 142 { 143 } 144 static inline void iommu_dma_sync_sg_for_cpu(struct device *dev, 145 struct scatterlist *sgl, int nelems, 146 enum dma_data_direction dir) 147 { 148 } 149 static inline void iommu_dma_sync_sg_for_device(struct device *dev, 150 struct scatterlist *sgl, int nelems, 151 enum dma_data_direction dir) 152 { 153 } 154 #endif /* CONFIG_IOMMU_DMA */ 155 #endif /* _LINUX_IOMMU_DMA_H */ 156