blob: 476e0c54de2db8c08520a3fc1caa5021139d1c09 [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__
Marc Zyngier8a22a3e2018-05-08 13:14:33 +010020#include <linux/types.h>
Robin Murphy0db2e5d2015-10-01 20:13:58 +010021#include <asm/errno.h>
22
23#ifdef CONFIG_IOMMU_DMA
Joerg Roedel461a6942017-04-26 15:46:20 +020024#include <linux/dma-mapping.h>
Robin Murphy0db2e5d2015-10-01 20:13:58 +010025#include <linux/iommu.h>
Robin Murphy44bb7e22016-09-12 17:13:59 +010026#include <linux/msi.h>
Robin Murphy0db2e5d2015-10-01 20:13:58 +010027
28int iommu_dma_init(void);
29
30/* Domain management interface for IOMMU drivers */
31int iommu_get_dma_cookie(struct iommu_domain *domain);
Robin Murphyfdbe5742017-01-19 20:57:46 +000032int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base);
Robin Murphy0db2e5d2015-10-01 20:13:58 +010033void iommu_put_dma_cookie(struct iommu_domain *domain);
34
35/* Setup call for arch DMA mapping code */
Robin Murphyfade1ec2016-09-12 17:14:00 +010036int iommu_dma_init_domain(struct iommu_domain *domain, dma_addr_t base,
37 u64 size, struct device *dev);
Robin Murphy0db2e5d2015-10-01 20:13:58 +010038
39/* General helpers for DMA-API <-> IOMMU-API interaction */
Mitchel Humpherys737c85c2017-01-06 18:58:12 +053040int dma_info_to_prot(enum dma_data_direction dir, bool coherent,
41 unsigned long attrs);
Robin Murphy0db2e5d2015-10-01 20:13:58 +010042
43/*
44 * These implement the bulk of the relevant DMA mapping callbacks, but require
45 * the arch code to take care of attributes and cache maintenance
46 */
Robin Murphy3b6b7e12016-04-13 17:29:10 +010047struct page **iommu_dma_alloc(struct device *dev, size_t size, gfp_t gfp,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070048 unsigned long attrs, int prot, dma_addr_t *handle,
Robin Murphy0db2e5d2015-10-01 20:13:58 +010049 void (*flush_page)(struct device *, const void *, phys_addr_t));
50void iommu_dma_free(struct device *dev, struct page **pages, size_t size,
51 dma_addr_t *handle);
52
53int iommu_dma_mmap(struct page **pages, size_t size, struct vm_area_struct *vma);
54
55dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page,
56 unsigned long offset, size_t size, int prot);
57int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg,
58 int nents, int prot);
59
60/*
61 * Arch code with no special attribute handling may use these
62 * directly as DMA mapping callbacks for simplicity
63 */
64void iommu_dma_unmap_page(struct device *dev, dma_addr_t handle, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070065 enum dma_data_direction dir, unsigned long attrs);
Robin Murphy0db2e5d2015-10-01 20:13:58 +010066void iommu_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070067 enum dma_data_direction dir, unsigned long attrs);
Robin Murphy51f8cc92016-11-14 12:16:26 +000068dma_addr_t iommu_dma_map_resource(struct device *dev, phys_addr_t phys,
69 size_t size, enum dma_data_direction dir, unsigned long attrs);
70void iommu_dma_unmap_resource(struct device *dev, dma_addr_t handle,
71 size_t size, enum dma_data_direction dir, unsigned long attrs);
Robin Murphy0db2e5d2015-10-01 20:13:58 +010072
Robin Murphy44bb7e22016-09-12 17:13:59 +010073/* The DMA API isn't _quite_ the whole story, though... */
Julien Grallece6e6f2019-05-01 14:58:19 +010074/*
75 * iommu_dma_prepare_msi() - Map the MSI page in the IOMMU device
76 *
77 * The MSI page will be stored in @desc.
78 *
79 * Return: 0 on success otherwise an error describing the failure.
80 */
81int iommu_dma_prepare_msi(struct msi_desc *desc, phys_addr_t msi_addr);
82
83/* Update the MSI message if required. */
84void iommu_dma_compose_msi_msg(struct msi_desc *desc,
85 struct msi_msg *msg);
86
Robin Murphy273df962017-03-16 17:00:19 +000087void iommu_dma_get_resv_regions(struct device *dev, struct list_head *list);
Robin Murphy44bb7e22016-09-12 17:13:59 +010088
Robin Murphy0db2e5d2015-10-01 20:13:58 +010089#else
90
91struct iommu_domain;
Julien Grallece6e6f2019-05-01 14:58:19 +010092struct msi_desc;
Robin Murphy44bb7e22016-09-12 17:13:59 +010093struct msi_msg;
Arnd Bergmann4b1c8892017-05-18 15:13:57 +020094struct device;
Robin Murphy0db2e5d2015-10-01 20:13:58 +010095
96static inline int iommu_dma_init(void)
97{
98 return 0;
99}
100
101static inline int iommu_get_dma_cookie(struct iommu_domain *domain)
102{
103 return -ENODEV;
104}
105
Robin Murphyfdbe5742017-01-19 20:57:46 +0000106static inline int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base)
107{
108 return -ENODEV;
109}
110
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100111static inline void iommu_put_dma_cookie(struct iommu_domain *domain)
112{
113}
114
Julien Grallece6e6f2019-05-01 14:58:19 +0100115static inline int iommu_dma_prepare_msi(struct msi_desc *desc,
116 phys_addr_t msi_addr)
117{
118 return 0;
119}
120
121static inline void iommu_dma_compose_msi_msg(struct msi_desc *desc,
122 struct msi_msg *msg)
123{
124}
125
Robin Murphy273df962017-03-16 17:00:19 +0000126static inline void iommu_dma_get_resv_regions(struct device *dev, struct list_head *list)
127{
128}
129
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100130#endif /* CONFIG_IOMMU_DMA */
131#endif /* __KERNEL__ */
132#endif /* __DMA_IOMMU_H */