1 /* Copyright (c) 2011-2015, The Linux Foundation. All rights reserved.
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.
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.
13 #ifndef _LINUX_MSM_IOMMU_DOMAINS_H
14 #define _LINUX_MSM_IOMMU_DOMAINS_H
16 #include <linux/errno.h>
17 #include <linux/mutex.h>
18 #include <linux/rbtree.h>
19 #include <linux/msm_ion.h>
21 #define MSM_IOMMU_DOMAIN_SECURE 0x1
24 struct mutex pool_mutex;
25 unsigned long *bitmap;
26 unsigned long nr_pages;
49 struct msm_iommu_domain_name {
54 struct msm_iommu_domain {
55 /* iommu domain to map in */
56 struct iommu_domain *domain;
57 /* total number of allocations from this domain */
58 atomic_t allocation_cnt;
59 /* number of iova pools */
62 * array of gen_pools for allocating iovas.
63 * behavior is undefined if these overlap
65 struct mem_pool *iova_pools;
68 struct iommu_domains_pdata {
69 struct msm_iommu_domain *domains;
71 struct msm_iommu_domain_name *domain_names;
73 unsigned int domain_alloc_flags;
77 struct msm_iova_partition {
82 struct msm_iova_layout {
83 struct msm_iova_partition *partitions;
85 const char *client_name;
86 unsigned int domain_flags;
87 unsigned int is_secure;
90 #if defined(CONFIG_MSM_IOMMU)
92 * ion_map_iommu - map the given handle into an iommu
94 * @client - client who allocated the handle
95 * @handle - handle to map
96 * @domain_num - domain number to map to
97 * @partition_num - partition number to allocate iova from
98 * @align - alignment for the iova
99 * @iova_length - length of iova to map. If the iova length is
100 * greater than the handle length, the remaining
101 * address space will be mapped to a dummy buffer.
102 * @iova - pointer to store the iova address
103 * @buffer_size - pointer to store the size of the buffer
104 * @flags - flags for options to map
105 * @iommu_flags - flags specific to the iommu.
107 * Maps the handle into the iova space specified via domain number. Iova
108 * will be allocated from the partition specified via partition_num.
109 * Returns 0 on success, negative value on error.
111 int ion_map_iommu(struct ion_client *client, struct ion_handle *handle,
112 int domain_num, int partition_num, unsigned long align,
113 unsigned long iova_length, ion_phys_addr_t *iova,
114 unsigned long *buffer_size,
115 unsigned long flags, unsigned long iommu_flags);
118 * ion_unmap_iommu - unmap the handle from an iommu
120 * @client - client who allocated the handle
121 * @handle - handle to unmap
122 * @domain_num - domain to unmap from
123 * @partition_num - partition to unmap from
125 * Decrement the reference count on the iommu mapping. If the count is
126 * 0, the mapping will be removed from the iommu.
128 void ion_unmap_iommu(struct ion_client *client, struct ion_handle *handle,
129 int domain_num, int partition_num);
131 extern void msm_iommu_set_client_name(struct iommu_domain *domain,
133 extern struct iommu_domain *msm_get_iommu_domain(int domain_num);
134 extern int msm_find_domain_no(const struct iommu_domain *domain);
135 extern struct iommu_domain *msm_iommu_domain_find(const char *name);
136 extern int msm_iommu_domain_no_find(const char *name);
139 extern int msm_allocate_iova_address(unsigned int iommu_domain,
140 unsigned int partition_no,
143 unsigned long *iova);
145 extern void msm_free_iova_address(unsigned long iova,
146 unsigned int iommu_domain,
147 unsigned int partition_no,
150 extern int msm_use_iommu(void);
152 extern int msm_iommu_map_extra(struct iommu_domain *domain,
153 unsigned long start_iova,
154 phys_addr_t phys_addr,
156 unsigned long page_size,
159 extern void msm_iommu_unmap_extra(struct iommu_domain *domain,
160 unsigned long start_iova,
162 unsigned long page_size);
164 extern int msm_iommu_map_contig_buffer(phys_addr_t phys,
165 unsigned int domain_no,
166 unsigned int partition_no,
169 unsigned long cached,
170 dma_addr_t *iova_val);
173 extern void msm_iommu_unmap_contig_buffer(dma_addr_t iova,
174 unsigned int domain_no,
175 unsigned int partition_no,
178 extern int msm_register_domain(struct msm_iova_layout *layout);
179 extern int msm_unregister_domain(struct iommu_domain *domain);
181 int msm_map_dma_buf(struct dma_buf *dma_buf, struct sg_table *table,
182 int domain_num, int partition_num, unsigned long align,
183 unsigned long iova_length, ion_phys_addr_t *iova,
184 unsigned long *buffer_size,
185 unsigned long flags, unsigned long iommu_flags);
187 void msm_unmap_dma_buf(struct sg_table *table, int domain_num,
190 static inline int ion_map_iommu(struct ion_client *client,
191 struct ion_handle *handle, int domain_num,
192 int partition_num, unsigned long align,
193 unsigned long iova_length, ion_phys_addr_t *iova,
194 unsigned long *buffer_size,
196 unsigned long iommu_flags)
201 static inline void ion_unmap_iommu(struct ion_client *client,
202 struct ion_handle *handle, int domain_num,
207 static inline void msm_iommu_set_client_name(struct iommu_domain *domain,
212 static inline struct iommu_domain
213 *msm_get_iommu_domain(int subsys_id) { return NULL; }
216 static inline int msm_find_domain_no(const struct iommu_domain *domain)
221 static inline int msm_allocate_iova_address(unsigned int iommu_domain,
222 unsigned int partition_no,
225 unsigned long *iova) { return -ENOMEM; }
227 static inline void msm_free_iova_address(unsigned long iova,
228 unsigned int iommu_domain,
229 unsigned int partition_no,
230 unsigned long size) { }
232 static inline int msm_use_iommu(void)
237 static inline int msm_iommu_map_extra(struct iommu_domain *domain,
238 unsigned long start_iova,
239 phys_addr_t phys_addr,
241 unsigned long page_size,
247 static inline void msm_iommu_unmap_extra(struct iommu_domain *domain,
248 unsigned long start_iova,
250 unsigned long page_size)
254 static inline int msm_iommu_map_contig_buffer(phys_addr_t phys,
255 unsigned int domain_no,
256 unsigned int partition_no,
259 unsigned long cached,
260 dma_addr_t *iova_val)
266 static inline void msm_iommu_unmap_contig_buffer(dma_addr_t iova,
267 unsigned int domain_no,
268 unsigned int partition_no,
273 static inline int msm_register_domain(struct msm_iova_layout *layout)
278 static inline int msm_unregister_domain(struct iommu_domain *domain)
283 static inline struct iommu_domain *msm_iommu_domain_find(const char *name)
288 static inline int msm_iommu_domain_no_find(const char *name)
293 static inline int msm_map_dma_buf(struct dma_buf *dma_buf,
294 struct sg_table *table,
295 int domain_num, int partition_num, unsigned long align,
296 unsigned long iova_length, ion_phys_addr_t *iova,
297 unsigned long *buffer_size,
298 unsigned long flags, unsigned long iommu_flags)
303 static inline void msm_unmap_dma_buf(struct sg_table *table, int domain_num,