blob: 44dabb1809f22b8dba8a5301ef8e22d2d5c427da [file] [log] [blame]
Liam Marka67eb072017-11-30 16:59:25 -08001/* Copyright (c) 2015-2017, The Linux Foundation. All rights reserved.
Olav Hauganff0116e2015-05-28 17:21:45 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#ifndef _LINUX_MSM_DMA_IOMMU_MAPPING_H
14#define _LINUX_MSM_DMA_IOMMU_MAPPING_H
15
16#include <linux/device.h>
17#include <linux/dma-buf.h>
18#include <linux/scatterlist.h>
19#include <linux/dma-mapping.h>
20
Laura Abbott29defcc2014-08-01 16:13:40 -070021#ifdef CONFIG_QCOM_LAZY_MAPPING
Olav Hauganff0116e2015-05-28 17:21:45 -070022/*
23 * This function is not taking a reference to the dma_buf here. It is expected
24 * that clients hold reference to the dma_buf until they are done with mapping
25 * and unmapping.
26 */
27int msm_dma_map_sg_attrs(struct device *dev, struct scatterlist *sg, int nents,
28 enum dma_data_direction dir, struct dma_buf *dma_buf,
Laura Abbott29defcc2014-08-01 16:13:40 -070029 unsigned long attrs);
Olav Hauganff0116e2015-05-28 17:21:45 -070030
Liam Marka67eb072017-11-30 16:59:25 -080031/*
32 * This function takes an extra reference to the dma_buf.
33 * What this means is that calling msm_dma_unmap_sg will not result in buffer's
34 * iommu mapping being removed, which means that subsequent calls to lazy map
35 * will simply re-use the existing iommu mapping.
36 * The iommu unmapping of the buffer will occur when the ION buffer is
37 * destroyed.
38 * Using lazy mapping can provide a performance benefit because subsequent
39 * mappings are faster.
40 *
41 * The limitation of using this API are that all subsequent iommu mappings
42 * must be the same as the original mapping, ie they must map the same part of
43 * the buffer with the same dma data direction. Also there can't be multiple
44 * mappings of different parts of the buffer.
45 */
Olav Hauganff0116e2015-05-28 17:21:45 -070046static inline int msm_dma_map_sg_lazy(struct device *dev,
47 struct scatterlist *sg, int nents,
48 enum dma_data_direction dir,
49 struct dma_buf *dma_buf)
50{
51 return msm_dma_map_sg_attrs(dev, sg, nents, dir, dma_buf, 0);
52}
53
54static inline int msm_dma_map_sg(struct device *dev, struct scatterlist *sg,
55 int nents, enum dma_data_direction dir,
56 struct dma_buf *dma_buf)
57{
Laura Abbott29defcc2014-08-01 16:13:40 -070058 unsigned long attrs;
59
60 attrs = DMA_ATTR_NO_DELAYED_UNMAP;
61 return msm_dma_map_sg_attrs(dev, sg, nents, dir, dma_buf, attrs);
Olav Hauganff0116e2015-05-28 17:21:45 -070062}
63
64void msm_dma_unmap_sg(struct device *dev, struct scatterlist *sgl, int nents,
65 enum dma_data_direction dir, struct dma_buf *dma_buf);
66
Laura Abbott29defcc2014-08-01 16:13:40 -070067int msm_dma_unmap_all_for_dev(struct device *dev);
Olav Hauganff0116e2015-05-28 17:21:45 -070068
69/*
70 * Below is private function only to be called by framework (ION) and not by
71 * clients.
72 */
Olav Hauganff0116e2015-05-28 17:21:45 -070073void msm_dma_buf_freed(void *buffer);
74
Laura Abbott29defcc2014-08-01 16:13:40 -070075#else /*CONFIG_QCOM_LAZY_MAPPING*/
76
77static inline int msm_dma_map_sg_attrs(struct device *dev,
78 struct scatterlist *sg, int nents,
79 enum dma_data_direction dir, struct dma_buf *dma_buf,
80 unsigned long attrs)
81{
82 return -EINVAL;
83}
84
85static inline int msm_dma_map_sg_lazy(struct device *dev,
86 struct scatterlist *sg, int nents,
87 enum dma_data_direction dir,
88 struct dma_buf *dma_buf)
89{
90 return -EINVAL;
91}
92
93static inline int msm_dma_map_sg(struct device *dev, struct scatterlist *sg,
94 int nents, enum dma_data_direction dir,
95 struct dma_buf *dma_buf)
96{
97 return -EINVAL;
98}
99
100static inline void msm_dma_unmap_sg(struct device *dev,
101 struct scatterlist *sgl, int nents,
102 enum dma_data_direction dir,
103 struct dma_buf *dma_buf)
104{
105}
106
Patrick Daly2d9b2f62016-09-19 18:25:00 -0700107static inline int msm_dma_unmap_all_for_dev(struct device *dev)
Laura Abbott29defcc2014-08-01 16:13:40 -0700108{
109 return 0;
110}
111
112static inline void msm_dma_buf_freed(void *buffer) {}
113#endif /*CONFIG_QCOM_LAZY_MAPPING*/
114
Olav Hauganff0116e2015-05-28 17:21:45 -0700115#endif