Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014-2015 ARM Ltd. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | #ifndef __DMA_IOMMU_H |
| 17 | #define __DMA_IOMMU_H |
| 18 | |
| 19 | #ifdef __KERNEL__ |
| 20 | #include <asm/errno.h> |
| 21 | |
| 22 | #ifdef CONFIG_IOMMU_DMA |
Joerg Roedel | 461a694 | 2017-04-26 15:46:20 +0200 | [diff] [blame] | 23 | #include <linux/dma-mapping.h> |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 24 | #include <linux/iommu.h> |
Robin Murphy | 44bb7e2 | 2016-09-12 17:13:59 +0100 | [diff] [blame] | 25 | #include <linux/msi.h> |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 26 | |
| 27 | int iommu_dma_init(void); |
| 28 | |
| 29 | /* Domain management interface for IOMMU drivers */ |
| 30 | int iommu_get_dma_cookie(struct iommu_domain *domain); |
Robin Murphy | fdbe574 | 2017-01-19 20:57:46 +0000 | [diff] [blame] | 31 | int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base); |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 32 | void iommu_put_dma_cookie(struct iommu_domain *domain); |
| 33 | |
| 34 | /* Setup call for arch DMA mapping code */ |
Robin Murphy | fade1ec | 2016-09-12 17:14:00 +0100 | [diff] [blame] | 35 | int iommu_dma_init_domain(struct iommu_domain *domain, dma_addr_t base, |
| 36 | u64 size, struct device *dev); |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 37 | |
| 38 | /* General helpers for DMA-API <-> IOMMU-API interaction */ |
Mitchel Humpherys | 737c85c | 2017-01-06 18:58:12 +0530 | [diff] [blame] | 39 | int dma_info_to_prot(enum dma_data_direction dir, bool coherent, |
| 40 | unsigned long attrs); |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 41 | |
| 42 | /* |
| 43 | * These implement the bulk of the relevant DMA mapping callbacks, but require |
| 44 | * the arch code to take care of attributes and cache maintenance |
| 45 | */ |
Robin Murphy | 3b6b7e1 | 2016-04-13 17:29:10 +0100 | [diff] [blame] | 46 | struct page **iommu_dma_alloc(struct device *dev, size_t size, gfp_t gfp, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 47 | unsigned long attrs, int prot, dma_addr_t *handle, |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 48 | void (*flush_page)(struct device *, const void *, phys_addr_t)); |
| 49 | void iommu_dma_free(struct device *dev, struct page **pages, size_t size, |
| 50 | dma_addr_t *handle); |
| 51 | |
| 52 | int iommu_dma_mmap(struct page **pages, size_t size, struct vm_area_struct *vma); |
| 53 | |
| 54 | dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page, |
| 55 | unsigned long offset, size_t size, int prot); |
| 56 | int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg, |
| 57 | int nents, int prot); |
| 58 | |
| 59 | /* |
| 60 | * Arch code with no special attribute handling may use these |
| 61 | * directly as DMA mapping callbacks for simplicity |
| 62 | */ |
| 63 | void iommu_dma_unmap_page(struct device *dev, dma_addr_t handle, size_t size, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 64 | enum dma_data_direction dir, unsigned long attrs); |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 65 | void iommu_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 66 | enum dma_data_direction dir, unsigned long attrs); |
Robin Murphy | 51f8cc9 | 2016-11-14 12:16:26 +0000 | [diff] [blame] | 67 | dma_addr_t iommu_dma_map_resource(struct device *dev, phys_addr_t phys, |
| 68 | size_t size, enum dma_data_direction dir, unsigned long attrs); |
| 69 | void iommu_dma_unmap_resource(struct device *dev, dma_addr_t handle, |
| 70 | size_t size, enum dma_data_direction dir, unsigned long attrs); |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 71 | int iommu_dma_mapping_error(struct device *dev, dma_addr_t dma_addr); |
| 72 | |
Robin Murphy | 44bb7e2 | 2016-09-12 17:13:59 +0100 | [diff] [blame] | 73 | /* The DMA API isn't _quite_ the whole story, though... */ |
| 74 | void iommu_dma_map_msi_msg(int irq, struct msi_msg *msg); |
Robin Murphy | 273df96 | 2017-03-16 17:00:19 +0000 | [diff] [blame] | 75 | void iommu_dma_get_resv_regions(struct device *dev, struct list_head *list); |
Robin Murphy | 44bb7e2 | 2016-09-12 17:13:59 +0100 | [diff] [blame] | 76 | |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 77 | #else |
| 78 | |
| 79 | struct iommu_domain; |
Robin Murphy | 44bb7e2 | 2016-09-12 17:13:59 +0100 | [diff] [blame] | 80 | struct msi_msg; |
Arnd Bergmann | 4b1c889 | 2017-05-18 15:13:57 +0200 | [diff] [blame] | 81 | struct device; |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 82 | |
| 83 | static inline int iommu_dma_init(void) |
| 84 | { |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static inline int iommu_get_dma_cookie(struct iommu_domain *domain) |
| 89 | { |
| 90 | return -ENODEV; |
| 91 | } |
| 92 | |
Robin Murphy | fdbe574 | 2017-01-19 20:57:46 +0000 | [diff] [blame] | 93 | static inline int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base) |
| 94 | { |
| 95 | return -ENODEV; |
| 96 | } |
| 97 | |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 98 | static inline void iommu_put_dma_cookie(struct iommu_domain *domain) |
| 99 | { |
| 100 | } |
| 101 | |
Robin Murphy | 44bb7e2 | 2016-09-12 17:13:59 +0100 | [diff] [blame] | 102 | static inline void iommu_dma_map_msi_msg(int irq, struct msi_msg *msg) |
| 103 | { |
| 104 | } |
| 105 | |
Robin Murphy | 273df96 | 2017-03-16 17:00:19 +0000 | [diff] [blame] | 106 | static inline void iommu_dma_get_resv_regions(struct device *dev, struct list_head *list) |
| 107 | { |
| 108 | } |
| 109 | |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 110 | #endif /* CONFIG_IOMMU_DMA */ |
| 111 | #endif /* __KERNEL__ */ |
| 112 | #endif /* __DMA_IOMMU_H */ |