Liam Mark | a67eb07 | 2017-11-30 16:59:25 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2015-2017, The Linux Foundation. All rights reserved. |
Olav Haugan | ff0116e | 2015-05-28 17:21:45 -0700 | [diff] [blame] | 2 | * |
| 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 Abbott | 29defcc | 2014-08-01 16:13:40 -0700 | [diff] [blame] | 21 | #ifdef CONFIG_QCOM_LAZY_MAPPING |
Olav Haugan | ff0116e | 2015-05-28 17:21:45 -0700 | [diff] [blame] | 22 | /* |
| 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 | */ |
| 27 | int 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 Abbott | 29defcc | 2014-08-01 16:13:40 -0700 | [diff] [blame] | 29 | unsigned long attrs); |
Olav Haugan | ff0116e | 2015-05-28 17:21:45 -0700 | [diff] [blame] | 30 | |
Liam Mark | a67eb07 | 2017-11-30 16:59:25 -0800 | [diff] [blame] | 31 | /* |
| 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 Haugan | ff0116e | 2015-05-28 17:21:45 -0700 | [diff] [blame] | 46 | static 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 | |
| 54 | static 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 Abbott | 29defcc | 2014-08-01 16:13:40 -0700 | [diff] [blame] | 58 | 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 Haugan | ff0116e | 2015-05-28 17:21:45 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void 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 Abbott | 29defcc | 2014-08-01 16:13:40 -0700 | [diff] [blame] | 67 | int msm_dma_unmap_all_for_dev(struct device *dev); |
Olav Haugan | ff0116e | 2015-05-28 17:21:45 -0700 | [diff] [blame] | 68 | |
| 69 | /* |
| 70 | * Below is private function only to be called by framework (ION) and not by |
| 71 | * clients. |
| 72 | */ |
Olav Haugan | ff0116e | 2015-05-28 17:21:45 -0700 | [diff] [blame] | 73 | void msm_dma_buf_freed(void *buffer); |
| 74 | |
Laura Abbott | 29defcc | 2014-08-01 16:13:40 -0700 | [diff] [blame] | 75 | #else /*CONFIG_QCOM_LAZY_MAPPING*/ |
| 76 | |
| 77 | static 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 | |
| 85 | static 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 | |
| 93 | static 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 | |
| 100 | static 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 Daly | 2d9b2f6 | 2016-09-19 18:25:00 -0700 | [diff] [blame] | 107 | static inline int msm_dma_unmap_all_for_dev(struct device *dev) |
Laura Abbott | 29defcc | 2014-08-01 16:13:40 -0700 | [diff] [blame] | 108 | { |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static inline void msm_dma_buf_freed(void *buffer) {} |
| 113 | #endif /*CONFIG_QCOM_LAZY_MAPPING*/ |
| 114 | |
Olav Haugan | ff0116e | 2015-05-28 17:21:45 -0700 | [diff] [blame] | 115 | #endif |