blob: abd946569515c78d0ecd409dcca0d8e4ba111c65 [file] [log] [blame]
Robin Murphy0db2e5d2015-10-01 20:13:58 +01001/*
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 Roedel461a6942017-04-26 15:46:20 +020023#include <linux/dma-mapping.h>
Robin Murphy0db2e5d2015-10-01 20:13:58 +010024#include <linux/iommu.h>
Robin Murphy44bb7e22016-09-12 17:13:59 +010025#include <linux/msi.h>
Robin Murphy0db2e5d2015-10-01 20:13:58 +010026
27int iommu_dma_init(void);
28
29/* Domain management interface for IOMMU drivers */
30int iommu_get_dma_cookie(struct iommu_domain *domain);
Robin Murphyfdbe5742017-01-19 20:57:46 +000031int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base);
Robin Murphy0db2e5d2015-10-01 20:13:58 +010032void iommu_put_dma_cookie(struct iommu_domain *domain);
33
34/* Setup call for arch DMA mapping code */
Robin Murphyfade1ec2016-09-12 17:14:00 +010035int iommu_dma_init_domain(struct iommu_domain *domain, dma_addr_t base,
36 u64 size, struct device *dev);
Robin Murphy0db2e5d2015-10-01 20:13:58 +010037
38/* General helpers for DMA-API <-> IOMMU-API interaction */
Mitchel Humpherys737c85c2017-01-06 18:58:12 +053039int dma_info_to_prot(enum dma_data_direction dir, bool coherent,
40 unsigned long attrs);
Robin Murphy0db2e5d2015-10-01 20:13:58 +010041
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 Murphy3b6b7e12016-04-13 17:29:10 +010046struct page **iommu_dma_alloc(struct device *dev, size_t size, gfp_t gfp,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070047 unsigned long attrs, int prot, dma_addr_t *handle,
Robin Murphy0db2e5d2015-10-01 20:13:58 +010048 void (*flush_page)(struct device *, const void *, phys_addr_t));
49void iommu_dma_free(struct device *dev, struct page **pages, size_t size,
50 dma_addr_t *handle);
51
52int iommu_dma_mmap(struct page **pages, size_t size, struct vm_area_struct *vma);
53
54dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page,
55 unsigned long offset, size_t size, int prot);
56int 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 */
63void iommu_dma_unmap_page(struct device *dev, dma_addr_t handle, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070064 enum dma_data_direction dir, unsigned long attrs);
Robin Murphy0db2e5d2015-10-01 20:13:58 +010065void iommu_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070066 enum dma_data_direction dir, unsigned long attrs);
Robin Murphy51f8cc92016-11-14 12:16:26 +000067dma_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);
69void iommu_dma_unmap_resource(struct device *dev, dma_addr_t handle,
70 size_t size, enum dma_data_direction dir, unsigned long attrs);
Robin Murphy0db2e5d2015-10-01 20:13:58 +010071int iommu_dma_mapping_error(struct device *dev, dma_addr_t dma_addr);
72
Robin Murphy44bb7e22016-09-12 17:13:59 +010073/* The DMA API isn't _quite_ the whole story, though... */
74void iommu_dma_map_msi_msg(int irq, struct msi_msg *msg);
75
Robin Murphy0db2e5d2015-10-01 20:13:58 +010076#else
77
78struct iommu_domain;
Robin Murphy44bb7e22016-09-12 17:13:59 +010079struct msi_msg;
Robin Murphy0db2e5d2015-10-01 20:13:58 +010080
81static inline int iommu_dma_init(void)
82{
83 return 0;
84}
85
86static inline int iommu_get_dma_cookie(struct iommu_domain *domain)
87{
88 return -ENODEV;
89}
90
Robin Murphyfdbe5742017-01-19 20:57:46 +000091static inline int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base)
92{
93 return -ENODEV;
94}
95
Robin Murphy0db2e5d2015-10-01 20:13:58 +010096static inline void iommu_put_dma_cookie(struct iommu_domain *domain)
97{
98}
99
Robin Murphy44bb7e22016-09-12 17:13:59 +0100100static inline void iommu_dma_map_msi_msg(int irq, struct msi_msg *msg)
101{
102}
103
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100104#endif /* CONFIG_IOMMU_DMA */
105#endif /* __KERNEL__ */
106#endif /* __DMA_IOMMU_H */