blob: 37258c8b2063f13e69c20be3fcb9baa48c05384e [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Robin Murphy0db2e5d2015-10-01 20:13:58 +01002/*
3 * Copyright (C) 2014-2015 ARM Ltd.
Robin Murphy0db2e5d2015-10-01 20:13:58 +01004 */
5#ifndef __DMA_IOMMU_H
6#define __DMA_IOMMU_H
7
8#ifdef __KERNEL__
Marc Zyngier8a22a3e2018-05-08 13:14:33 +01009#include <linux/types.h>
Robin Murphy0db2e5d2015-10-01 20:13:58 +010010#include <asm/errno.h>
11
12#ifdef CONFIG_IOMMU_DMA
Joerg Roedel461a6942017-04-26 15:46:20 +020013#include <linux/dma-mapping.h>
Robin Murphy0db2e5d2015-10-01 20:13:58 +010014#include <linux/iommu.h>
Robin Murphy44bb7e22016-09-12 17:13:59 +010015#include <linux/msi.h>
Robin Murphy0db2e5d2015-10-01 20:13:58 +010016
17int iommu_dma_init(void);
18
19/* Domain management interface for IOMMU drivers */
20int iommu_get_dma_cookie(struct iommu_domain *domain);
Robin Murphyfdbe5742017-01-19 20:57:46 +000021int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base);
Robin Murphy0db2e5d2015-10-01 20:13:58 +010022void iommu_put_dma_cookie(struct iommu_domain *domain);
23
24/* Setup call for arch DMA mapping code */
Robin Murphyfade1ec2016-09-12 17:14:00 +010025int iommu_dma_init_domain(struct iommu_domain *domain, dma_addr_t base,
26 u64 size, struct device *dev);
Robin Murphy0db2e5d2015-10-01 20:13:58 +010027
28/* General helpers for DMA-API <-> IOMMU-API interaction */
Mitchel Humpherys737c85c2017-01-06 18:58:12 +053029int dma_info_to_prot(enum dma_data_direction dir, bool coherent,
30 unsigned long attrs);
Robin Murphy0db2e5d2015-10-01 20:13:58 +010031
32/*
33 * These implement the bulk of the relevant DMA mapping callbacks, but require
34 * the arch code to take care of attributes and cache maintenance
35 */
Robin Murphy3b6b7e12016-04-13 17:29:10 +010036struct page **iommu_dma_alloc(struct device *dev, size_t size, gfp_t gfp,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070037 unsigned long attrs, int prot, dma_addr_t *handle,
Robin Murphy0db2e5d2015-10-01 20:13:58 +010038 void (*flush_page)(struct device *, const void *, phys_addr_t));
39void iommu_dma_free(struct device *dev, struct page **pages, size_t size,
40 dma_addr_t *handle);
41
42int iommu_dma_mmap(struct page **pages, size_t size, struct vm_area_struct *vma);
43
44dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page,
45 unsigned long offset, size_t size, int prot);
46int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg,
47 int nents, int prot);
48
49/*
50 * Arch code with no special attribute handling may use these
51 * directly as DMA mapping callbacks for simplicity
52 */
53void iommu_dma_unmap_page(struct device *dev, dma_addr_t handle, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070054 enum dma_data_direction dir, unsigned long attrs);
Robin Murphy0db2e5d2015-10-01 20:13:58 +010055void iommu_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070056 enum dma_data_direction dir, unsigned long attrs);
Robin Murphy51f8cc92016-11-14 12:16:26 +000057dma_addr_t iommu_dma_map_resource(struct device *dev, phys_addr_t phys,
58 size_t size, enum dma_data_direction dir, unsigned long attrs);
59void iommu_dma_unmap_resource(struct device *dev, dma_addr_t handle,
60 size_t size, enum dma_data_direction dir, unsigned long attrs);
Robin Murphy0db2e5d2015-10-01 20:13:58 +010061
Robin Murphy44bb7e22016-09-12 17:13:59 +010062/* The DMA API isn't _quite_ the whole story, though... */
Julien Grallece6e6f2019-05-01 14:58:19 +010063/*
64 * iommu_dma_prepare_msi() - Map the MSI page in the IOMMU device
65 *
66 * The MSI page will be stored in @desc.
67 *
68 * Return: 0 on success otherwise an error describing the failure.
69 */
70int iommu_dma_prepare_msi(struct msi_desc *desc, phys_addr_t msi_addr);
71
72/* Update the MSI message if required. */
73void iommu_dma_compose_msi_msg(struct msi_desc *desc,
74 struct msi_msg *msg);
75
Robin Murphy273df962017-03-16 17:00:19 +000076void iommu_dma_get_resv_regions(struct device *dev, struct list_head *list);
Robin Murphy44bb7e22016-09-12 17:13:59 +010077
Robin Murphy0db2e5d2015-10-01 20:13:58 +010078#else
79
80struct iommu_domain;
Julien Grallece6e6f2019-05-01 14:58:19 +010081struct msi_desc;
Robin Murphy44bb7e22016-09-12 17:13:59 +010082struct msi_msg;
Arnd Bergmann4b1c8892017-05-18 15:13:57 +020083struct device;
Robin Murphy0db2e5d2015-10-01 20:13:58 +010084
85static inline int iommu_dma_init(void)
86{
87 return 0;
88}
89
90static inline int iommu_get_dma_cookie(struct iommu_domain *domain)
91{
92 return -ENODEV;
93}
94
Robin Murphyfdbe5742017-01-19 20:57:46 +000095static inline int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base)
96{
97 return -ENODEV;
98}
99
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100100static inline void iommu_put_dma_cookie(struct iommu_domain *domain)
101{
102}
103
Julien Grallece6e6f2019-05-01 14:58:19 +0100104static inline int iommu_dma_prepare_msi(struct msi_desc *desc,
105 phys_addr_t msi_addr)
106{
107 return 0;
108}
109
110static inline void iommu_dma_compose_msi_msg(struct msi_desc *desc,
111 struct msi_msg *msg)
112{
113}
114
Robin Murphy273df962017-03-16 17:00:19 +0000115static inline void iommu_dma_get_resv_regions(struct device *dev, struct list_head *list)
116{
117}
118
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100119#endif /* CONFIG_IOMMU_DMA */
120#endif /* __KERNEL__ */
121#endif /* __DMA_IOMMU_H */