1695093e3SVarun Sethi /*
2695093e3SVarun Sethi  * This program is free software; you can redistribute it and/or modify
3695093e3SVarun Sethi  * it under the terms of the GNU General Public License, version 2, as
4695093e3SVarun Sethi  * published by the Free Software Foundation.
5695093e3SVarun Sethi  *
6695093e3SVarun Sethi  * This program is distributed in the hope that it will be useful,
7695093e3SVarun Sethi  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8695093e3SVarun Sethi  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9695093e3SVarun Sethi  * GNU General Public License for more details.
10695093e3SVarun Sethi  *
11695093e3SVarun Sethi  * You should have received a copy of the GNU General Public License
12695093e3SVarun Sethi  * along with this program; if not, write to the Free Software
13695093e3SVarun Sethi  * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
14695093e3SVarun Sethi  *
15695093e3SVarun Sethi  * Copyright (C) 2013 Freescale Semiconductor, Inc.
16695093e3SVarun Sethi  * Author: Varun Sethi <varun.sethi@freescale.com>
17695093e3SVarun Sethi  *
18695093e3SVarun Sethi  */
19695093e3SVarun Sethi 
20695093e3SVarun Sethi #define pr_fmt(fmt)    "fsl-pamu-domain: %s: " fmt, __func__
21695093e3SVarun Sethi 
22695093e3SVarun Sethi #include "fsl_pamu_domain.h"
23695093e3SVarun Sethi 
24cd70d465SEmil Medve #include <sysdev/fsl_pci.h>
25cd70d465SEmil Medve 
26695093e3SVarun Sethi /*
27695093e3SVarun Sethi  * Global spinlock that needs to be held while
28695093e3SVarun Sethi  * configuring PAMU.
29695093e3SVarun Sethi  */
30695093e3SVarun Sethi static DEFINE_SPINLOCK(iommu_lock);
31695093e3SVarun Sethi 
32695093e3SVarun Sethi static struct kmem_cache *fsl_pamu_domain_cache;
33695093e3SVarun Sethi static struct kmem_cache *iommu_devinfo_cache;
34695093e3SVarun Sethi static DEFINE_SPINLOCK(device_domain_lock);
35695093e3SVarun Sethi 
363ff2dcc0SJoerg Roedel struct iommu_device pamu_iommu;	/* IOMMU core code handle */
373ff2dcc0SJoerg Roedel 
388d4bfe40SJoerg Roedel static struct fsl_dma_domain *to_fsl_dma_domain(struct iommu_domain *dom)
398d4bfe40SJoerg Roedel {
408d4bfe40SJoerg Roedel 	return container_of(dom, struct fsl_dma_domain, iommu_domain);
418d4bfe40SJoerg Roedel }
428d4bfe40SJoerg Roedel 
43695093e3SVarun Sethi static int __init iommu_init_mempool(void)
44695093e3SVarun Sethi {
45695093e3SVarun Sethi 	fsl_pamu_domain_cache = kmem_cache_create("fsl_pamu_domain",
46695093e3SVarun Sethi 						  sizeof(struct fsl_dma_domain),
47695093e3SVarun Sethi 						  0,
48695093e3SVarun Sethi 						  SLAB_HWCACHE_ALIGN,
49695093e3SVarun Sethi 						  NULL);
50695093e3SVarun Sethi 	if (!fsl_pamu_domain_cache) {
51695093e3SVarun Sethi 		pr_debug("Couldn't create fsl iommu_domain cache\n");
52695093e3SVarun Sethi 		return -ENOMEM;
53695093e3SVarun Sethi 	}
54695093e3SVarun Sethi 
55695093e3SVarun Sethi 	iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
56695093e3SVarun Sethi 						sizeof(struct device_domain_info),
57695093e3SVarun Sethi 						0,
58695093e3SVarun Sethi 						SLAB_HWCACHE_ALIGN,
59695093e3SVarun Sethi 						NULL);
60695093e3SVarun Sethi 	if (!iommu_devinfo_cache) {
61695093e3SVarun Sethi 		pr_debug("Couldn't create devinfo cache\n");
62695093e3SVarun Sethi 		kmem_cache_destroy(fsl_pamu_domain_cache);
63695093e3SVarun Sethi 		return -ENOMEM;
64695093e3SVarun Sethi 	}
65695093e3SVarun Sethi 
66695093e3SVarun Sethi 	return 0;
67695093e3SVarun Sethi }
68695093e3SVarun Sethi 
69695093e3SVarun Sethi static phys_addr_t get_phys_addr(struct fsl_dma_domain *dma_domain, dma_addr_t iova)
70695093e3SVarun Sethi {
71695093e3SVarun Sethi 	u32 win_cnt = dma_domain->win_cnt;
72cd70d465SEmil Medve 	struct dma_window *win_ptr = &dma_domain->win_arr[0];
73695093e3SVarun Sethi 	struct iommu_domain_geometry *geom;
74695093e3SVarun Sethi 
758d4bfe40SJoerg Roedel 	geom = &dma_domain->iommu_domain.geometry;
76695093e3SVarun Sethi 
77695093e3SVarun Sethi 	if (!win_cnt || !dma_domain->geom_size) {
78695093e3SVarun Sethi 		pr_debug("Number of windows/geometry not configured for the domain\n");
79695093e3SVarun Sethi 		return 0;
80695093e3SVarun Sethi 	}
81695093e3SVarun Sethi 
82695093e3SVarun Sethi 	if (win_cnt > 1) {
83695093e3SVarun Sethi 		u64 subwin_size;
84695093e3SVarun Sethi 		dma_addr_t subwin_iova;
85695093e3SVarun Sethi 		u32 wnd;
86695093e3SVarun Sethi 
87695093e3SVarun Sethi 		subwin_size = dma_domain->geom_size >> ilog2(win_cnt);
88695093e3SVarun Sethi 		subwin_iova = iova & ~(subwin_size - 1);
89695093e3SVarun Sethi 		wnd = (subwin_iova - geom->aperture_start) >> ilog2(subwin_size);
90695093e3SVarun Sethi 		win_ptr = &dma_domain->win_arr[wnd];
91695093e3SVarun Sethi 	}
92695093e3SVarun Sethi 
93695093e3SVarun Sethi 	if (win_ptr->valid)
94cd70d465SEmil Medve 		return win_ptr->paddr + (iova & (win_ptr->size - 1));
95695093e3SVarun Sethi 
96695093e3SVarun Sethi 	return 0;
97695093e3SVarun Sethi }
98695093e3SVarun Sethi 
99695093e3SVarun Sethi static int map_subwins(int liodn, struct fsl_dma_domain *dma_domain)
100695093e3SVarun Sethi {
101cd70d465SEmil Medve 	struct dma_window *sub_win_ptr = &dma_domain->win_arr[0];
102695093e3SVarun Sethi 	int i, ret;
103695093e3SVarun Sethi 	unsigned long rpn, flags;
104695093e3SVarun Sethi 
105695093e3SVarun Sethi 	for (i = 0; i < dma_domain->win_cnt; i++) {
106695093e3SVarun Sethi 		if (sub_win_ptr[i].valid) {
107cd70d465SEmil Medve 			rpn = sub_win_ptr[i].paddr >> PAMU_PAGE_SHIFT;
108695093e3SVarun Sethi 			spin_lock_irqsave(&iommu_lock, flags);
109695093e3SVarun Sethi 			ret = pamu_config_spaace(liodn, dma_domain->win_cnt, i,
110695093e3SVarun Sethi 						 sub_win_ptr[i].size,
111695093e3SVarun Sethi 						 ~(u32)0,
112695093e3SVarun Sethi 						 rpn,
113695093e3SVarun Sethi 						 dma_domain->snoop_id,
114695093e3SVarun Sethi 						 dma_domain->stash_id,
115695093e3SVarun Sethi 						 (i > 0) ? 1 : 0,
116695093e3SVarun Sethi 						 sub_win_ptr[i].prot);
117695093e3SVarun Sethi 			spin_unlock_irqrestore(&iommu_lock, flags);
118695093e3SVarun Sethi 			if (ret) {
119cd70d465SEmil Medve 				pr_debug("SPAACE configuration failed for liodn %d\n",
120695093e3SVarun Sethi 					 liodn);
121695093e3SVarun Sethi 				return ret;
122695093e3SVarun Sethi 			}
123695093e3SVarun Sethi 		}
124695093e3SVarun Sethi 	}
125695093e3SVarun Sethi 
126695093e3SVarun Sethi 	return ret;
127695093e3SVarun Sethi }
128695093e3SVarun Sethi 
129695093e3SVarun Sethi static int map_win(int liodn, struct fsl_dma_domain *dma_domain)
130695093e3SVarun Sethi {
131695093e3SVarun Sethi 	int ret;
132695093e3SVarun Sethi 	struct dma_window *wnd = &dma_domain->win_arr[0];
1338d4bfe40SJoerg Roedel 	phys_addr_t wnd_addr = dma_domain->iommu_domain.geometry.aperture_start;
134695093e3SVarun Sethi 	unsigned long flags;
135695093e3SVarun Sethi 
136695093e3SVarun Sethi 	spin_lock_irqsave(&iommu_lock, flags);
137695093e3SVarun Sethi 	ret = pamu_config_ppaace(liodn, wnd_addr,
138695093e3SVarun Sethi 				 wnd->size,
139695093e3SVarun Sethi 				 ~(u32)0,
140695093e3SVarun Sethi 				 wnd->paddr >> PAMU_PAGE_SHIFT,
141695093e3SVarun Sethi 				 dma_domain->snoop_id, dma_domain->stash_id,
142695093e3SVarun Sethi 				 0, wnd->prot);
143695093e3SVarun Sethi 	spin_unlock_irqrestore(&iommu_lock, flags);
144695093e3SVarun Sethi 	if (ret)
145cd70d465SEmil Medve 		pr_debug("PAACE configuration failed for liodn %d\n", liodn);
146695093e3SVarun Sethi 
147695093e3SVarun Sethi 	return ret;
148695093e3SVarun Sethi }
149695093e3SVarun Sethi 
150695093e3SVarun Sethi /* Map the DMA window corresponding to the LIODN */
151695093e3SVarun Sethi static int map_liodn(int liodn, struct fsl_dma_domain *dma_domain)
152695093e3SVarun Sethi {
153695093e3SVarun Sethi 	if (dma_domain->win_cnt > 1)
154695093e3SVarun Sethi 		return map_subwins(liodn, dma_domain);
155695093e3SVarun Sethi 	else
156695093e3SVarun Sethi 		return map_win(liodn, dma_domain);
157695093e3SVarun Sethi }
158695093e3SVarun Sethi 
159695093e3SVarun Sethi /* Update window/subwindow mapping for the LIODN */
160695093e3SVarun Sethi static int update_liodn(int liodn, struct fsl_dma_domain *dma_domain, u32 wnd_nr)
161695093e3SVarun Sethi {
162695093e3SVarun Sethi 	int ret;
163695093e3SVarun Sethi 	struct dma_window *wnd = &dma_domain->win_arr[wnd_nr];
164695093e3SVarun Sethi 	unsigned long flags;
165695093e3SVarun Sethi 
166695093e3SVarun Sethi 	spin_lock_irqsave(&iommu_lock, flags);
167695093e3SVarun Sethi 	if (dma_domain->win_cnt > 1) {
168695093e3SVarun Sethi 		ret = pamu_config_spaace(liodn, dma_domain->win_cnt, wnd_nr,
169695093e3SVarun Sethi 					 wnd->size,
170695093e3SVarun Sethi 					 ~(u32)0,
171695093e3SVarun Sethi 					 wnd->paddr >> PAMU_PAGE_SHIFT,
172695093e3SVarun Sethi 					 dma_domain->snoop_id,
173695093e3SVarun Sethi 					 dma_domain->stash_id,
174695093e3SVarun Sethi 					 (wnd_nr > 0) ? 1 : 0,
175695093e3SVarun Sethi 					 wnd->prot);
176695093e3SVarun Sethi 		if (ret)
177cd70d465SEmil Medve 			pr_debug("Subwindow reconfiguration failed for liodn %d\n",
178cd70d465SEmil Medve 				 liodn);
179695093e3SVarun Sethi 	} else {
180695093e3SVarun Sethi 		phys_addr_t wnd_addr;
181695093e3SVarun Sethi 
1828d4bfe40SJoerg Roedel 		wnd_addr = dma_domain->iommu_domain.geometry.aperture_start;
183695093e3SVarun Sethi 
184695093e3SVarun Sethi 		ret = pamu_config_ppaace(liodn, wnd_addr,
185695093e3SVarun Sethi 					 wnd->size,
186695093e3SVarun Sethi 					 ~(u32)0,
187695093e3SVarun Sethi 					 wnd->paddr >> PAMU_PAGE_SHIFT,
188695093e3SVarun Sethi 					 dma_domain->snoop_id, dma_domain->stash_id,
189695093e3SVarun Sethi 					 0, wnd->prot);
190695093e3SVarun Sethi 		if (ret)
191cd70d465SEmil Medve 			pr_debug("Window reconfiguration failed for liodn %d\n",
192cd70d465SEmil Medve 				 liodn);
193695093e3SVarun Sethi 	}
194695093e3SVarun Sethi 
195695093e3SVarun Sethi 	spin_unlock_irqrestore(&iommu_lock, flags);
196695093e3SVarun Sethi 
197695093e3SVarun Sethi 	return ret;
198695093e3SVarun Sethi }
199695093e3SVarun Sethi 
200695093e3SVarun Sethi static int update_liodn_stash(int liodn, struct fsl_dma_domain *dma_domain,
201695093e3SVarun Sethi 			      u32 val)
202695093e3SVarun Sethi {
203695093e3SVarun Sethi 	int ret = 0, i;
204695093e3SVarun Sethi 	unsigned long flags;
205695093e3SVarun Sethi 
206695093e3SVarun Sethi 	spin_lock_irqsave(&iommu_lock, flags);
207695093e3SVarun Sethi 	if (!dma_domain->win_arr) {
208cd70d465SEmil Medve 		pr_debug("Windows not configured, stash destination update failed for liodn %d\n",
209cd70d465SEmil Medve 			 liodn);
210695093e3SVarun Sethi 		spin_unlock_irqrestore(&iommu_lock, flags);
211695093e3SVarun Sethi 		return -EINVAL;
212695093e3SVarun Sethi 	}
213695093e3SVarun Sethi 
214695093e3SVarun Sethi 	for (i = 0; i < dma_domain->win_cnt; i++) {
215695093e3SVarun Sethi 		ret = pamu_update_paace_stash(liodn, i, val);
216695093e3SVarun Sethi 		if (ret) {
217cd70d465SEmil Medve 			pr_debug("Failed to update SPAACE %d field for liodn %d\n ",
218cd70d465SEmil Medve 				 i, liodn);
219695093e3SVarun Sethi 			spin_unlock_irqrestore(&iommu_lock, flags);
220695093e3SVarun Sethi 			return ret;
221695093e3SVarun Sethi 		}
222695093e3SVarun Sethi 	}
223695093e3SVarun Sethi 
224695093e3SVarun Sethi 	spin_unlock_irqrestore(&iommu_lock, flags);
225695093e3SVarun Sethi 
226695093e3SVarun Sethi 	return ret;
227695093e3SVarun Sethi }
228695093e3SVarun Sethi 
229695093e3SVarun Sethi /* Set the geometry parameters for a LIODN */
230695093e3SVarun Sethi static int pamu_set_liodn(int liodn, struct device *dev,
231695093e3SVarun Sethi 			  struct fsl_dma_domain *dma_domain,
232695093e3SVarun Sethi 			  struct iommu_domain_geometry *geom_attr,
233695093e3SVarun Sethi 			  u32 win_cnt)
234695093e3SVarun Sethi {
235695093e3SVarun Sethi 	phys_addr_t window_addr, window_size;
236695093e3SVarun Sethi 	phys_addr_t subwin_size;
237695093e3SVarun Sethi 	int ret = 0, i;
238695093e3SVarun Sethi 	u32 omi_index = ~(u32)0;
239695093e3SVarun Sethi 	unsigned long flags;
240695093e3SVarun Sethi 
241695093e3SVarun Sethi 	/*
242695093e3SVarun Sethi 	 * Configure the omi_index at the geometry setup time.
243695093e3SVarun Sethi 	 * This is a static value which depends on the type of
244695093e3SVarun Sethi 	 * device and would not change thereafter.
245695093e3SVarun Sethi 	 */
246695093e3SVarun Sethi 	get_ome_index(&omi_index, dev);
247695093e3SVarun Sethi 
248695093e3SVarun Sethi 	window_addr = geom_attr->aperture_start;
249695093e3SVarun Sethi 	window_size = dma_domain->geom_size;
250695093e3SVarun Sethi 
251695093e3SVarun Sethi 	spin_lock_irqsave(&iommu_lock, flags);
252695093e3SVarun Sethi 	ret = pamu_disable_liodn(liodn);
253695093e3SVarun Sethi 	if (!ret)
254695093e3SVarun Sethi 		ret = pamu_config_ppaace(liodn, window_addr, window_size, omi_index,
255695093e3SVarun Sethi 					 0, dma_domain->snoop_id,
256695093e3SVarun Sethi 					 dma_domain->stash_id, win_cnt, 0);
257695093e3SVarun Sethi 	spin_unlock_irqrestore(&iommu_lock, flags);
258695093e3SVarun Sethi 	if (ret) {
259cd70d465SEmil Medve 		pr_debug("PAACE configuration failed for liodn %d, win_cnt =%d\n",
260cd70d465SEmil Medve 			 liodn, win_cnt);
261695093e3SVarun Sethi 		return ret;
262695093e3SVarun Sethi 	}
263695093e3SVarun Sethi 
264695093e3SVarun Sethi 	if (win_cnt > 1) {
265695093e3SVarun Sethi 		subwin_size = window_size >> ilog2(win_cnt);
266695093e3SVarun Sethi 		for (i = 0; i < win_cnt; i++) {
267695093e3SVarun Sethi 			spin_lock_irqsave(&iommu_lock, flags);
268695093e3SVarun Sethi 			ret = pamu_disable_spaace(liodn, i);
269695093e3SVarun Sethi 			if (!ret)
270695093e3SVarun Sethi 				ret = pamu_config_spaace(liodn, win_cnt, i,
271695093e3SVarun Sethi 							 subwin_size, omi_index,
272695093e3SVarun Sethi 							 0, dma_domain->snoop_id,
273695093e3SVarun Sethi 							 dma_domain->stash_id,
274695093e3SVarun Sethi 							 0, 0);
275695093e3SVarun Sethi 			spin_unlock_irqrestore(&iommu_lock, flags);
276695093e3SVarun Sethi 			if (ret) {
277cd70d465SEmil Medve 				pr_debug("SPAACE configuration failed for liodn %d\n",
278cd70d465SEmil Medve 					 liodn);
279695093e3SVarun Sethi 				return ret;
280695093e3SVarun Sethi 			}
281695093e3SVarun Sethi 		}
282695093e3SVarun Sethi 	}
283695093e3SVarun Sethi 
284695093e3SVarun Sethi 	return ret;
285695093e3SVarun Sethi }
286695093e3SVarun Sethi 
287695093e3SVarun Sethi static int check_size(u64 size, dma_addr_t iova)
288695093e3SVarun Sethi {
289695093e3SVarun Sethi 	/*
290695093e3SVarun Sethi 	 * Size must be a power of two and at least be equal
291695093e3SVarun Sethi 	 * to PAMU page size.
292695093e3SVarun Sethi 	 */
293d033f48fSVarun Sethi 	if ((size & (size - 1)) || size < PAMU_PAGE_SIZE) {
294cd70d465SEmil Medve 		pr_debug("Size too small or not a power of two\n");
295695093e3SVarun Sethi 		return -EINVAL;
296695093e3SVarun Sethi 	}
297695093e3SVarun Sethi 
298695093e3SVarun Sethi 	/* iova must be page size aligned */
299695093e3SVarun Sethi 	if (iova & (size - 1)) {
300cd70d465SEmil Medve 		pr_debug("Address is not aligned with window size\n");
301695093e3SVarun Sethi 		return -EINVAL;
302695093e3SVarun Sethi 	}
303695093e3SVarun Sethi 
304695093e3SVarun Sethi 	return 0;
305695093e3SVarun Sethi }
306695093e3SVarun Sethi 
307695093e3SVarun Sethi static struct fsl_dma_domain *iommu_alloc_dma_domain(void)
308695093e3SVarun Sethi {
309695093e3SVarun Sethi 	struct fsl_dma_domain *domain;
310695093e3SVarun Sethi 
311695093e3SVarun Sethi 	domain = kmem_cache_zalloc(fsl_pamu_domain_cache, GFP_KERNEL);
312695093e3SVarun Sethi 	if (!domain)
313695093e3SVarun Sethi 		return NULL;
314695093e3SVarun Sethi 
315695093e3SVarun Sethi 	domain->stash_id = ~(u32)0;
316695093e3SVarun Sethi 	domain->snoop_id = ~(u32)0;
317695093e3SVarun Sethi 	domain->win_cnt = pamu_get_max_subwin_cnt();
318695093e3SVarun Sethi 	domain->geom_size = 0;
319695093e3SVarun Sethi 
320695093e3SVarun Sethi 	INIT_LIST_HEAD(&domain->devices);
321695093e3SVarun Sethi 
322695093e3SVarun Sethi 	spin_lock_init(&domain->domain_lock);
323695093e3SVarun Sethi 
324695093e3SVarun Sethi 	return domain;
325695093e3SVarun Sethi }
326695093e3SVarun Sethi 
327695093e3SVarun Sethi static void remove_device_ref(struct device_domain_info *info, u32 win_cnt)
328695093e3SVarun Sethi {
329695093e3SVarun Sethi 	unsigned long flags;
330695093e3SVarun Sethi 
331695093e3SVarun Sethi 	list_del(&info->link);
332695093e3SVarun Sethi 	spin_lock_irqsave(&iommu_lock, flags);
333695093e3SVarun Sethi 	if (win_cnt > 1)
334695093e3SVarun Sethi 		pamu_free_subwins(info->liodn);
335695093e3SVarun Sethi 	pamu_disable_liodn(info->liodn);
336695093e3SVarun Sethi 	spin_unlock_irqrestore(&iommu_lock, flags);
337695093e3SVarun Sethi 	spin_lock_irqsave(&device_domain_lock, flags);
338695093e3SVarun Sethi 	info->dev->archdata.iommu_domain = NULL;
339695093e3SVarun Sethi 	kmem_cache_free(iommu_devinfo_cache, info);
340695093e3SVarun Sethi 	spin_unlock_irqrestore(&device_domain_lock, flags);
341695093e3SVarun Sethi }
342695093e3SVarun Sethi 
343695093e3SVarun Sethi static void detach_device(struct device *dev, struct fsl_dma_domain *dma_domain)
344695093e3SVarun Sethi {
345695093e3SVarun Sethi 	struct device_domain_info *info, *tmp;
346695093e3SVarun Sethi 	unsigned long flags;
347695093e3SVarun Sethi 
348695093e3SVarun Sethi 	spin_lock_irqsave(&dma_domain->domain_lock, flags);
349695093e3SVarun Sethi 	/* Remove the device from the domain device list */
350695093e3SVarun Sethi 	list_for_each_entry_safe(info, tmp, &dma_domain->devices, link) {
351695093e3SVarun Sethi 		if (!dev || (info->dev == dev))
352695093e3SVarun Sethi 			remove_device_ref(info, dma_domain->win_cnt);
353695093e3SVarun Sethi 	}
354695093e3SVarun Sethi 	spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
355695093e3SVarun Sethi }
356695093e3SVarun Sethi 
357695093e3SVarun Sethi static void attach_device(struct fsl_dma_domain *dma_domain, int liodn, struct device *dev)
358695093e3SVarun Sethi {
359695093e3SVarun Sethi 	struct device_domain_info *info, *old_domain_info;
360695093e3SVarun Sethi 	unsigned long flags;
361695093e3SVarun Sethi 
362695093e3SVarun Sethi 	spin_lock_irqsave(&device_domain_lock, flags);
363695093e3SVarun Sethi 	/*
364695093e3SVarun Sethi 	 * Check here if the device is already attached to domain or not.
365695093e3SVarun Sethi 	 * If the device is already attached to a domain detach it.
366695093e3SVarun Sethi 	 */
36775f0e461SVarun Sethi 	old_domain_info = dev->archdata.iommu_domain;
368695093e3SVarun Sethi 	if (old_domain_info && old_domain_info->domain != dma_domain) {
369695093e3SVarun Sethi 		spin_unlock_irqrestore(&device_domain_lock, flags);
370695093e3SVarun Sethi 		detach_device(dev, old_domain_info->domain);
371695093e3SVarun Sethi 		spin_lock_irqsave(&device_domain_lock, flags);
372695093e3SVarun Sethi 	}
373695093e3SVarun Sethi 
374695093e3SVarun Sethi 	info = kmem_cache_zalloc(iommu_devinfo_cache, GFP_ATOMIC);
375695093e3SVarun Sethi 
376695093e3SVarun Sethi 	info->dev = dev;
377695093e3SVarun Sethi 	info->liodn = liodn;
378695093e3SVarun Sethi 	info->domain = dma_domain;
379695093e3SVarun Sethi 
380695093e3SVarun Sethi 	list_add(&info->link, &dma_domain->devices);
381695093e3SVarun Sethi 	/*
382695093e3SVarun Sethi 	 * In case of devices with multiple LIODNs just store
383695093e3SVarun Sethi 	 * the info for the first LIODN as all
384695093e3SVarun Sethi 	 * LIODNs share the same domain
385695093e3SVarun Sethi 	 */
38675f0e461SVarun Sethi 	if (!dev->archdata.iommu_domain)
387695093e3SVarun Sethi 		dev->archdata.iommu_domain = info;
388695093e3SVarun Sethi 	spin_unlock_irqrestore(&device_domain_lock, flags);
389695093e3SVarun Sethi }
390695093e3SVarun Sethi 
391695093e3SVarun Sethi static phys_addr_t fsl_pamu_iova_to_phys(struct iommu_domain *domain,
392695093e3SVarun Sethi 					 dma_addr_t iova)
393695093e3SVarun Sethi {
3948d4bfe40SJoerg Roedel 	struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain);
395695093e3SVarun Sethi 
396cd70d465SEmil Medve 	if (iova < domain->geometry.aperture_start ||
397cd70d465SEmil Medve 	    iova > domain->geometry.aperture_end)
398695093e3SVarun Sethi 		return 0;
399695093e3SVarun Sethi 
400695093e3SVarun Sethi 	return get_phys_addr(dma_domain, iova);
401695093e3SVarun Sethi }
402695093e3SVarun Sethi 
403b7eb6785SJoerg Roedel static bool fsl_pamu_capable(enum iommu_cap cap)
404695093e3SVarun Sethi {
405695093e3SVarun Sethi 	return cap == IOMMU_CAP_CACHE_COHERENCY;
406695093e3SVarun Sethi }
407695093e3SVarun Sethi 
4088d4bfe40SJoerg Roedel static void fsl_pamu_domain_free(struct iommu_domain *domain)
409695093e3SVarun Sethi {
4108d4bfe40SJoerg Roedel 	struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain);
411695093e3SVarun Sethi 
412695093e3SVarun Sethi 	/* remove all the devices from the device list */
413695093e3SVarun Sethi 	detach_device(NULL, dma_domain);
414695093e3SVarun Sethi 
415695093e3SVarun Sethi 	dma_domain->enabled = 0;
416695093e3SVarun Sethi 	dma_domain->mapped = 0;
417695093e3SVarun Sethi 
418695093e3SVarun Sethi 	kmem_cache_free(fsl_pamu_domain_cache, dma_domain);
419695093e3SVarun Sethi }
420695093e3SVarun Sethi 
4218d4bfe40SJoerg Roedel static struct iommu_domain *fsl_pamu_domain_alloc(unsigned type)
422695093e3SVarun Sethi {
423695093e3SVarun Sethi 	struct fsl_dma_domain *dma_domain;
424695093e3SVarun Sethi 
4258d4bfe40SJoerg Roedel 	if (type != IOMMU_DOMAIN_UNMANAGED)
4268d4bfe40SJoerg Roedel 		return NULL;
4278d4bfe40SJoerg Roedel 
428695093e3SVarun Sethi 	dma_domain = iommu_alloc_dma_domain();
429695093e3SVarun Sethi 	if (!dma_domain) {
430695093e3SVarun Sethi 		pr_debug("dma_domain allocation failed\n");
4318d4bfe40SJoerg Roedel 		return NULL;
432695093e3SVarun Sethi 	}
433695093e3SVarun Sethi 	/* defaul geometry 64 GB i.e. maximum system address */
4348d4bfe40SJoerg Roedel 	dma_domain->iommu_domain. geometry.aperture_start = 0;
4358d4bfe40SJoerg Roedel 	dma_domain->iommu_domain.geometry.aperture_end = (1ULL << 36) - 1;
4368d4bfe40SJoerg Roedel 	dma_domain->iommu_domain.geometry.force_aperture = true;
437695093e3SVarun Sethi 
4388d4bfe40SJoerg Roedel 	return &dma_domain->iommu_domain;
439695093e3SVarun Sethi }
440695093e3SVarun Sethi 
441695093e3SVarun Sethi /* Configure geometry settings for all LIODNs associated with domain */
442695093e3SVarun Sethi static int pamu_set_domain_geometry(struct fsl_dma_domain *dma_domain,
443695093e3SVarun Sethi 				    struct iommu_domain_geometry *geom_attr,
444695093e3SVarun Sethi 				    u32 win_cnt)
445695093e3SVarun Sethi {
446695093e3SVarun Sethi 	struct device_domain_info *info;
447695093e3SVarun Sethi 	int ret = 0;
448695093e3SVarun Sethi 
449695093e3SVarun Sethi 	list_for_each_entry(info, &dma_domain->devices, link) {
450695093e3SVarun Sethi 		ret = pamu_set_liodn(info->liodn, info->dev, dma_domain,
451695093e3SVarun Sethi 				     geom_attr, win_cnt);
452695093e3SVarun Sethi 		if (ret)
453695093e3SVarun Sethi 			break;
454695093e3SVarun Sethi 	}
455695093e3SVarun Sethi 
456695093e3SVarun Sethi 	return ret;
457695093e3SVarun Sethi }
458695093e3SVarun Sethi 
459695093e3SVarun Sethi /* Update stash destination for all LIODNs associated with the domain */
460695093e3SVarun Sethi static int update_domain_stash(struct fsl_dma_domain *dma_domain, u32 val)
461695093e3SVarun Sethi {
462695093e3SVarun Sethi 	struct device_domain_info *info;
463695093e3SVarun Sethi 	int ret = 0;
464695093e3SVarun Sethi 
465695093e3SVarun Sethi 	list_for_each_entry(info, &dma_domain->devices, link) {
466695093e3SVarun Sethi 		ret = update_liodn_stash(info->liodn, dma_domain, val);
467695093e3SVarun Sethi 		if (ret)
468695093e3SVarun Sethi 			break;
469695093e3SVarun Sethi 	}
470695093e3SVarun Sethi 
471695093e3SVarun Sethi 	return ret;
472695093e3SVarun Sethi }
473695093e3SVarun Sethi 
474695093e3SVarun Sethi /* Update domain mappings for all LIODNs associated with the domain */
475695093e3SVarun Sethi static int update_domain_mapping(struct fsl_dma_domain *dma_domain, u32 wnd_nr)
476695093e3SVarun Sethi {
477695093e3SVarun Sethi 	struct device_domain_info *info;
478695093e3SVarun Sethi 	int ret = 0;
479695093e3SVarun Sethi 
480695093e3SVarun Sethi 	list_for_each_entry(info, &dma_domain->devices, link) {
481695093e3SVarun Sethi 		ret = update_liodn(info->liodn, dma_domain, wnd_nr);
482695093e3SVarun Sethi 		if (ret)
483695093e3SVarun Sethi 			break;
484695093e3SVarun Sethi 	}
485695093e3SVarun Sethi 	return ret;
486695093e3SVarun Sethi }
487695093e3SVarun Sethi 
488695093e3SVarun Sethi static int disable_domain_win(struct fsl_dma_domain *dma_domain, u32 wnd_nr)
489695093e3SVarun Sethi {
490695093e3SVarun Sethi 	struct device_domain_info *info;
491695093e3SVarun Sethi 	int ret = 0;
492695093e3SVarun Sethi 
493695093e3SVarun Sethi 	list_for_each_entry(info, &dma_domain->devices, link) {
494695093e3SVarun Sethi 		if (dma_domain->win_cnt == 1 && dma_domain->enabled) {
495695093e3SVarun Sethi 			ret = pamu_disable_liodn(info->liodn);
496695093e3SVarun Sethi 			if (!ret)
497695093e3SVarun Sethi 				dma_domain->enabled = 0;
498695093e3SVarun Sethi 		} else {
499695093e3SVarun Sethi 			ret = pamu_disable_spaace(info->liodn, wnd_nr);
500695093e3SVarun Sethi 		}
501695093e3SVarun Sethi 	}
502695093e3SVarun Sethi 
503695093e3SVarun Sethi 	return ret;
504695093e3SVarun Sethi }
505695093e3SVarun Sethi 
506695093e3SVarun Sethi static void fsl_pamu_window_disable(struct iommu_domain *domain, u32 wnd_nr)
507695093e3SVarun Sethi {
5088d4bfe40SJoerg Roedel 	struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain);
509695093e3SVarun Sethi 	unsigned long flags;
510695093e3SVarun Sethi 	int ret;
511695093e3SVarun Sethi 
512695093e3SVarun Sethi 	spin_lock_irqsave(&dma_domain->domain_lock, flags);
513695093e3SVarun Sethi 	if (!dma_domain->win_arr) {
514695093e3SVarun Sethi 		pr_debug("Number of windows not configured\n");
515695093e3SVarun Sethi 		spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
516695093e3SVarun Sethi 		return;
517695093e3SVarun Sethi 	}
518695093e3SVarun Sethi 
519695093e3SVarun Sethi 	if (wnd_nr >= dma_domain->win_cnt) {
520695093e3SVarun Sethi 		pr_debug("Invalid window index\n");
521695093e3SVarun Sethi 		spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
522695093e3SVarun Sethi 		return;
523695093e3SVarun Sethi 	}
524695093e3SVarun Sethi 
525695093e3SVarun Sethi 	if (dma_domain->win_arr[wnd_nr].valid) {
526695093e3SVarun Sethi 		ret = disable_domain_win(dma_domain, wnd_nr);
527695093e3SVarun Sethi 		if (!ret) {
528695093e3SVarun Sethi 			dma_domain->win_arr[wnd_nr].valid = 0;
529695093e3SVarun Sethi 			dma_domain->mapped--;
530695093e3SVarun Sethi 		}
531695093e3SVarun Sethi 	}
532695093e3SVarun Sethi 
533695093e3SVarun Sethi 	spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
534695093e3SVarun Sethi }
535695093e3SVarun Sethi 
536695093e3SVarun Sethi static int fsl_pamu_window_enable(struct iommu_domain *domain, u32 wnd_nr,
537695093e3SVarun Sethi 				  phys_addr_t paddr, u64 size, int prot)
538695093e3SVarun Sethi {
5398d4bfe40SJoerg Roedel 	struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain);
540695093e3SVarun Sethi 	struct dma_window *wnd;
541695093e3SVarun Sethi 	int pamu_prot = 0;
542695093e3SVarun Sethi 	int ret;
543695093e3SVarun Sethi 	unsigned long flags;
544695093e3SVarun Sethi 	u64 win_size;
545695093e3SVarun Sethi 
546695093e3SVarun Sethi 	if (prot & IOMMU_READ)
547695093e3SVarun Sethi 		pamu_prot |= PAACE_AP_PERMS_QUERY;
548695093e3SVarun Sethi 	if (prot & IOMMU_WRITE)
549695093e3SVarun Sethi 		pamu_prot |= PAACE_AP_PERMS_UPDATE;
550695093e3SVarun Sethi 
551695093e3SVarun Sethi 	spin_lock_irqsave(&dma_domain->domain_lock, flags);
552695093e3SVarun Sethi 	if (!dma_domain->win_arr) {
553695093e3SVarun Sethi 		pr_debug("Number of windows not configured\n");
554695093e3SVarun Sethi 		spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
555695093e3SVarun Sethi 		return -ENODEV;
556695093e3SVarun Sethi 	}
557695093e3SVarun Sethi 
558695093e3SVarun Sethi 	if (wnd_nr >= dma_domain->win_cnt) {
559695093e3SVarun Sethi 		pr_debug("Invalid window index\n");
560695093e3SVarun Sethi 		spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
561695093e3SVarun Sethi 		return -EINVAL;
562695093e3SVarun Sethi 	}
563695093e3SVarun Sethi 
564695093e3SVarun Sethi 	win_size = dma_domain->geom_size >> ilog2(dma_domain->win_cnt);
565695093e3SVarun Sethi 	if (size > win_size) {
566695093e3SVarun Sethi 		pr_debug("Invalid window size\n");
567695093e3SVarun Sethi 		spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
568695093e3SVarun Sethi 		return -EINVAL;
569695093e3SVarun Sethi 	}
570695093e3SVarun Sethi 
571695093e3SVarun Sethi 	if (dma_domain->win_cnt == 1) {
572695093e3SVarun Sethi 		if (dma_domain->enabled) {
573695093e3SVarun Sethi 			pr_debug("Disable the window before updating the mapping\n");
574695093e3SVarun Sethi 			spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
575695093e3SVarun Sethi 			return -EBUSY;
576695093e3SVarun Sethi 		}
577695093e3SVarun Sethi 
578695093e3SVarun Sethi 		ret = check_size(size, domain->geometry.aperture_start);
579695093e3SVarun Sethi 		if (ret) {
580695093e3SVarun Sethi 			pr_debug("Aperture start not aligned to the size\n");
581695093e3SVarun Sethi 			spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
582695093e3SVarun Sethi 			return -EINVAL;
583695093e3SVarun Sethi 		}
584695093e3SVarun Sethi 	}
585695093e3SVarun Sethi 
586695093e3SVarun Sethi 	wnd = &dma_domain->win_arr[wnd_nr];
587695093e3SVarun Sethi 	if (!wnd->valid) {
588695093e3SVarun Sethi 		wnd->paddr = paddr;
589695093e3SVarun Sethi 		wnd->size = size;
590695093e3SVarun Sethi 		wnd->prot = pamu_prot;
591695093e3SVarun Sethi 
592695093e3SVarun Sethi 		ret = update_domain_mapping(dma_domain, wnd_nr);
593695093e3SVarun Sethi 		if (!ret) {
594695093e3SVarun Sethi 			wnd->valid = 1;
595695093e3SVarun Sethi 			dma_domain->mapped++;
596695093e3SVarun Sethi 		}
597695093e3SVarun Sethi 	} else {
598695093e3SVarun Sethi 		pr_debug("Disable the window before updating the mapping\n");
599695093e3SVarun Sethi 		ret = -EBUSY;
600695093e3SVarun Sethi 	}
601695093e3SVarun Sethi 
602695093e3SVarun Sethi 	spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
603695093e3SVarun Sethi 
604695093e3SVarun Sethi 	return ret;
605695093e3SVarun Sethi }
606695093e3SVarun Sethi 
607695093e3SVarun Sethi /*
608695093e3SVarun Sethi  * Attach the LIODN to the DMA domain and configure the geometry
609695093e3SVarun Sethi  * and window mappings.
610695093e3SVarun Sethi  */
611695093e3SVarun Sethi static int handle_attach_device(struct fsl_dma_domain *dma_domain,
612695093e3SVarun Sethi 				struct device *dev, const u32 *liodn,
613695093e3SVarun Sethi 				int num)
614695093e3SVarun Sethi {
615695093e3SVarun Sethi 	unsigned long flags;
6168d4bfe40SJoerg Roedel 	struct iommu_domain *domain = &dma_domain->iommu_domain;
617695093e3SVarun Sethi 	int ret = 0;
618695093e3SVarun Sethi 	int i;
619695093e3SVarun Sethi 
620695093e3SVarun Sethi 	spin_lock_irqsave(&dma_domain->domain_lock, flags);
621695093e3SVarun Sethi 	for (i = 0; i < num; i++) {
622695093e3SVarun Sethi 		/* Ensure that LIODN value is valid */
623695093e3SVarun Sethi 		if (liodn[i] >= PAACE_NUMBER_ENTRIES) {
6246bd4f1c7SRob Herring 			pr_debug("Invalid liodn %d, attach device failed for %pOF\n",
6256bd4f1c7SRob Herring 				 liodn[i], dev->of_node);
626695093e3SVarun Sethi 			ret = -EINVAL;
627695093e3SVarun Sethi 			break;
628695093e3SVarun Sethi 		}
629695093e3SVarun Sethi 
630695093e3SVarun Sethi 		attach_device(dma_domain, liodn[i], dev);
631695093e3SVarun Sethi 		/*
632695093e3SVarun Sethi 		 * Check if geometry has already been configured
633695093e3SVarun Sethi 		 * for the domain. If yes, set the geometry for
634695093e3SVarun Sethi 		 * the LIODN.
635695093e3SVarun Sethi 		 */
636695093e3SVarun Sethi 		if (dma_domain->win_arr) {
637695093e3SVarun Sethi 			u32 win_cnt = dma_domain->win_cnt > 1 ? dma_domain->win_cnt : 0;
638cd70d465SEmil Medve 
639695093e3SVarun Sethi 			ret = pamu_set_liodn(liodn[i], dev, dma_domain,
640cd70d465SEmil Medve 					     &domain->geometry, win_cnt);
641695093e3SVarun Sethi 			if (ret)
642695093e3SVarun Sethi 				break;
643695093e3SVarun Sethi 			if (dma_domain->mapped) {
644695093e3SVarun Sethi 				/*
645695093e3SVarun Sethi 				 * Create window/subwindow mapping for
646695093e3SVarun Sethi 				 * the LIODN.
647695093e3SVarun Sethi 				 */
648695093e3SVarun Sethi 				ret = map_liodn(liodn[i], dma_domain);
649695093e3SVarun Sethi 				if (ret)
650695093e3SVarun Sethi 					break;
651695093e3SVarun Sethi 			}
652695093e3SVarun Sethi 		}
653695093e3SVarun Sethi 	}
654695093e3SVarun Sethi 	spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
655695093e3SVarun Sethi 
656695093e3SVarun Sethi 	return ret;
657695093e3SVarun Sethi }
658695093e3SVarun Sethi 
659695093e3SVarun Sethi static int fsl_pamu_attach_device(struct iommu_domain *domain,
660695093e3SVarun Sethi 				  struct device *dev)
661695093e3SVarun Sethi {
6628d4bfe40SJoerg Roedel 	struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain);
663695093e3SVarun Sethi 	const u32 *liodn;
664695093e3SVarun Sethi 	u32 liodn_cnt;
665695093e3SVarun Sethi 	int len, ret = 0;
666695093e3SVarun Sethi 	struct pci_dev *pdev = NULL;
667695093e3SVarun Sethi 	struct pci_controller *pci_ctl;
668695093e3SVarun Sethi 
669695093e3SVarun Sethi 	/*
670695093e3SVarun Sethi 	 * Use LIODN of the PCI controller while attaching a
671695093e3SVarun Sethi 	 * PCI device.
672695093e3SVarun Sethi 	 */
673b3eb76d1SYijing Wang 	if (dev_is_pci(dev)) {
674695093e3SVarun Sethi 		pdev = to_pci_dev(dev);
675695093e3SVarun Sethi 		pci_ctl = pci_bus_to_host(pdev->bus);
676695093e3SVarun Sethi 		/*
677695093e3SVarun Sethi 		 * make dev point to pci controller device
678695093e3SVarun Sethi 		 * so we can get the LIODN programmed by
679695093e3SVarun Sethi 		 * u-boot.
680695093e3SVarun Sethi 		 */
681695093e3SVarun Sethi 		dev = pci_ctl->parent;
682695093e3SVarun Sethi 	}
683695093e3SVarun Sethi 
684695093e3SVarun Sethi 	liodn = of_get_property(dev->of_node, "fsl,liodn", &len);
685695093e3SVarun Sethi 	if (liodn) {
686695093e3SVarun Sethi 		liodn_cnt = len / sizeof(u32);
687cd70d465SEmil Medve 		ret = handle_attach_device(dma_domain, dev, liodn, liodn_cnt);
688695093e3SVarun Sethi 	} else {
6896bd4f1c7SRob Herring 		pr_debug("missing fsl,liodn property at %pOF\n", dev->of_node);
690695093e3SVarun Sethi 		ret = -EINVAL;
691695093e3SVarun Sethi 	}
692695093e3SVarun Sethi 
693695093e3SVarun Sethi 	return ret;
694695093e3SVarun Sethi }
695695093e3SVarun Sethi 
696695093e3SVarun Sethi static void fsl_pamu_detach_device(struct iommu_domain *domain,
697695093e3SVarun Sethi 				   struct device *dev)
698695093e3SVarun Sethi {
6998d4bfe40SJoerg Roedel 	struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain);
700695093e3SVarun Sethi 	const u32 *prop;
701695093e3SVarun Sethi 	int len;
702695093e3SVarun Sethi 	struct pci_dev *pdev = NULL;
703695093e3SVarun Sethi 	struct pci_controller *pci_ctl;
704695093e3SVarun Sethi 
705695093e3SVarun Sethi 	/*
706695093e3SVarun Sethi 	 * Use LIODN of the PCI controller while detaching a
707695093e3SVarun Sethi 	 * PCI device.
708695093e3SVarun Sethi 	 */
709b3eb76d1SYijing Wang 	if (dev_is_pci(dev)) {
710695093e3SVarun Sethi 		pdev = to_pci_dev(dev);
711695093e3SVarun Sethi 		pci_ctl = pci_bus_to_host(pdev->bus);
712695093e3SVarun Sethi 		/*
713695093e3SVarun Sethi 		 * make dev point to pci controller device
714695093e3SVarun Sethi 		 * so we can get the LIODN programmed by
715695093e3SVarun Sethi 		 * u-boot.
716695093e3SVarun Sethi 		 */
717695093e3SVarun Sethi 		dev = pci_ctl->parent;
718695093e3SVarun Sethi 	}
719695093e3SVarun Sethi 
720695093e3SVarun Sethi 	prop = of_get_property(dev->of_node, "fsl,liodn", &len);
721695093e3SVarun Sethi 	if (prop)
722695093e3SVarun Sethi 		detach_device(dev, dma_domain);
723695093e3SVarun Sethi 	else
7246bd4f1c7SRob Herring 		pr_debug("missing fsl,liodn property at %pOF\n", dev->of_node);
725695093e3SVarun Sethi }
726695093e3SVarun Sethi 
727695093e3SVarun Sethi static  int configure_domain_geometry(struct iommu_domain *domain, void *data)
728695093e3SVarun Sethi {
729695093e3SVarun Sethi 	struct iommu_domain_geometry *geom_attr = data;
7308d4bfe40SJoerg Roedel 	struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain);
731695093e3SVarun Sethi 	dma_addr_t geom_size;
732695093e3SVarun Sethi 	unsigned long flags;
733695093e3SVarun Sethi 
734695093e3SVarun Sethi 	geom_size = geom_attr->aperture_end - geom_attr->aperture_start + 1;
735695093e3SVarun Sethi 	/*
736695093e3SVarun Sethi 	 * Sanity check the geometry size. Also, we do not support
737695093e3SVarun Sethi 	 * DMA outside of the geometry.
738695093e3SVarun Sethi 	 */
739695093e3SVarun Sethi 	if (check_size(geom_size, geom_attr->aperture_start) ||
740695093e3SVarun Sethi 	    !geom_attr->force_aperture) {
741695093e3SVarun Sethi 		pr_debug("Invalid PAMU geometry attributes\n");
742695093e3SVarun Sethi 		return -EINVAL;
743695093e3SVarun Sethi 	}
744695093e3SVarun Sethi 
745695093e3SVarun Sethi 	spin_lock_irqsave(&dma_domain->domain_lock, flags);
746695093e3SVarun Sethi 	if (dma_domain->enabled) {
747695093e3SVarun Sethi 		pr_debug("Can't set geometry attributes as domain is active\n");
748695093e3SVarun Sethi 		spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
749695093e3SVarun Sethi 		return  -EBUSY;
750695093e3SVarun Sethi 	}
751695093e3SVarun Sethi 
752695093e3SVarun Sethi 	/* Copy the domain geometry information */
753695093e3SVarun Sethi 	memcpy(&domain->geometry, geom_attr,
754695093e3SVarun Sethi 	       sizeof(struct iommu_domain_geometry));
755695093e3SVarun Sethi 	dma_domain->geom_size = geom_size;
756695093e3SVarun Sethi 
757695093e3SVarun Sethi 	spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
758695093e3SVarun Sethi 
759695093e3SVarun Sethi 	return 0;
760695093e3SVarun Sethi }
761695093e3SVarun Sethi 
762695093e3SVarun Sethi /* Set the domain stash attribute */
763695093e3SVarun Sethi static int configure_domain_stash(struct fsl_dma_domain *dma_domain, void *data)
764695093e3SVarun Sethi {
765695093e3SVarun Sethi 	struct pamu_stash_attribute *stash_attr = data;
766695093e3SVarun Sethi 	unsigned long flags;
767695093e3SVarun Sethi 	int ret;
768695093e3SVarun Sethi 
769695093e3SVarun Sethi 	spin_lock_irqsave(&dma_domain->domain_lock, flags);
770695093e3SVarun Sethi 
771695093e3SVarun Sethi 	memcpy(&dma_domain->dma_stash, stash_attr,
772695093e3SVarun Sethi 	       sizeof(struct pamu_stash_attribute));
773695093e3SVarun Sethi 
774695093e3SVarun Sethi 	dma_domain->stash_id = get_stash_id(stash_attr->cache,
775695093e3SVarun Sethi 					    stash_attr->cpu);
776695093e3SVarun Sethi 	if (dma_domain->stash_id == ~(u32)0) {
777695093e3SVarun Sethi 		pr_debug("Invalid stash attributes\n");
778695093e3SVarun Sethi 		spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
779695093e3SVarun Sethi 		return -EINVAL;
780695093e3SVarun Sethi 	}
781695093e3SVarun Sethi 
782695093e3SVarun Sethi 	ret = update_domain_stash(dma_domain, dma_domain->stash_id);
783695093e3SVarun Sethi 
784695093e3SVarun Sethi 	spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
785695093e3SVarun Sethi 
786695093e3SVarun Sethi 	return ret;
787695093e3SVarun Sethi }
788695093e3SVarun Sethi 
789695093e3SVarun Sethi /* Configure domain dma state i.e. enable/disable DMA */
790695093e3SVarun Sethi static int configure_domain_dma_state(struct fsl_dma_domain *dma_domain, bool enable)
791695093e3SVarun Sethi {
792695093e3SVarun Sethi 	struct device_domain_info *info;
793695093e3SVarun Sethi 	unsigned long flags;
794695093e3SVarun Sethi 	int ret;
795695093e3SVarun Sethi 
796695093e3SVarun Sethi 	spin_lock_irqsave(&dma_domain->domain_lock, flags);
797695093e3SVarun Sethi 
798695093e3SVarun Sethi 	if (enable && !dma_domain->mapped) {
799695093e3SVarun Sethi 		pr_debug("Can't enable DMA domain without valid mapping\n");
800695093e3SVarun Sethi 		spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
801695093e3SVarun Sethi 		return -ENODEV;
802695093e3SVarun Sethi 	}
803695093e3SVarun Sethi 
804695093e3SVarun Sethi 	dma_domain->enabled = enable;
805cd70d465SEmil Medve 	list_for_each_entry(info, &dma_domain->devices, link) {
806695093e3SVarun Sethi 		ret = (enable) ? pamu_enable_liodn(info->liodn) :
807695093e3SVarun Sethi 			pamu_disable_liodn(info->liodn);
808695093e3SVarun Sethi 		if (ret)
809695093e3SVarun Sethi 			pr_debug("Unable to set dma state for liodn %d",
810695093e3SVarun Sethi 				 info->liodn);
811695093e3SVarun Sethi 	}
812695093e3SVarun Sethi 	spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
813695093e3SVarun Sethi 
814695093e3SVarun Sethi 	return 0;
815695093e3SVarun Sethi }
816695093e3SVarun Sethi 
817695093e3SVarun Sethi static int fsl_pamu_set_domain_attr(struct iommu_domain *domain,
818695093e3SVarun Sethi 				    enum iommu_attr attr_type, void *data)
819695093e3SVarun Sethi {
8208d4bfe40SJoerg Roedel 	struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain);
821701d8a62SRobin Murphy 	u32 *count;
822695093e3SVarun Sethi 	int ret = 0;
823695093e3SVarun Sethi 
824695093e3SVarun Sethi 	switch (attr_type) {
825695093e3SVarun Sethi 	case DOMAIN_ATTR_GEOMETRY:
826695093e3SVarun Sethi 		ret = configure_domain_geometry(domain, data);
827695093e3SVarun Sethi 		break;
828695093e3SVarun Sethi 	case DOMAIN_ATTR_FSL_PAMU_STASH:
829695093e3SVarun Sethi 		ret = configure_domain_stash(dma_domain, data);
830695093e3SVarun Sethi 		break;
831695093e3SVarun Sethi 	case DOMAIN_ATTR_FSL_PAMU_ENABLE:
832695093e3SVarun Sethi 		ret = configure_domain_dma_state(dma_domain, *(int *)data);
833695093e3SVarun Sethi 		break;
834701d8a62SRobin Murphy 	case DOMAIN_ATTR_WINDOWS:
835701d8a62SRobin Murphy 		count = data;
836701d8a62SRobin Murphy 
837701d8a62SRobin Murphy 		if (domain->ops->domain_set_windows != NULL)
838701d8a62SRobin Murphy 			ret = domain->ops->domain_set_windows(domain, *count);
839701d8a62SRobin Murphy 		else
840701d8a62SRobin Murphy 			ret = -ENODEV;
841701d8a62SRobin Murphy 
842701d8a62SRobin Murphy 		break;
843695093e3SVarun Sethi 	default:
844695093e3SVarun Sethi 		pr_debug("Unsupported attribute type\n");
845695093e3SVarun Sethi 		ret = -EINVAL;
846695093e3SVarun Sethi 		break;
847cd70d465SEmil Medve 	}
848695093e3SVarun Sethi 
849695093e3SVarun Sethi 	return ret;
850695093e3SVarun Sethi }
851695093e3SVarun Sethi 
852695093e3SVarun Sethi static int fsl_pamu_get_domain_attr(struct iommu_domain *domain,
853695093e3SVarun Sethi 				    enum iommu_attr attr_type, void *data)
854695093e3SVarun Sethi {
8558d4bfe40SJoerg Roedel 	struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain);
856701d8a62SRobin Murphy 	u32 *count;
857695093e3SVarun Sethi 	int ret = 0;
858695093e3SVarun Sethi 
859695093e3SVarun Sethi 	switch (attr_type) {
860695093e3SVarun Sethi 	case DOMAIN_ATTR_FSL_PAMU_STASH:
861cd70d465SEmil Medve 		memcpy(data, &dma_domain->dma_stash,
862695093e3SVarun Sethi 		       sizeof(struct pamu_stash_attribute));
863695093e3SVarun Sethi 		break;
864695093e3SVarun Sethi 	case DOMAIN_ATTR_FSL_PAMU_ENABLE:
865695093e3SVarun Sethi 		*(int *)data = dma_domain->enabled;
866695093e3SVarun Sethi 		break;
867695093e3SVarun Sethi 	case DOMAIN_ATTR_FSL_PAMUV1:
868695093e3SVarun Sethi 		*(int *)data = DOMAIN_ATTR_FSL_PAMUV1;
869695093e3SVarun Sethi 		break;
870701d8a62SRobin Murphy 	case DOMAIN_ATTR_WINDOWS:
871701d8a62SRobin Murphy 		count = data;
872701d8a62SRobin Murphy 
873701d8a62SRobin Murphy 		if (domain->ops->domain_get_windows != NULL)
874701d8a62SRobin Murphy 			*count = domain->ops->domain_get_windows(domain);
875701d8a62SRobin Murphy 		else
876701d8a62SRobin Murphy 			ret = -ENODEV;
877701d8a62SRobin Murphy 
878701d8a62SRobin Murphy 		break;
879695093e3SVarun Sethi 	default:
880695093e3SVarun Sethi 		pr_debug("Unsupported attribute type\n");
881695093e3SVarun Sethi 		ret = -EINVAL;
882695093e3SVarun Sethi 		break;
883cd70d465SEmil Medve 	}
884695093e3SVarun Sethi 
885695093e3SVarun Sethi 	return ret;
886695093e3SVarun Sethi }
887695093e3SVarun Sethi 
888695093e3SVarun Sethi static struct iommu_group *get_device_iommu_group(struct device *dev)
889695093e3SVarun Sethi {
890695093e3SVarun Sethi 	struct iommu_group *group;
891695093e3SVarun Sethi 
892695093e3SVarun Sethi 	group = iommu_group_get(dev);
893695093e3SVarun Sethi 	if (!group)
894695093e3SVarun Sethi 		group = iommu_group_alloc();
895695093e3SVarun Sethi 
896695093e3SVarun Sethi 	return group;
897695093e3SVarun Sethi }
898695093e3SVarun Sethi 
899695093e3SVarun Sethi static  bool check_pci_ctl_endpt_part(struct pci_controller *pci_ctl)
900695093e3SVarun Sethi {
901695093e3SVarun Sethi 	u32 version;
902695093e3SVarun Sethi 
903695093e3SVarun Sethi 	/* Check the PCI controller version number by readding BRR1 register */
904695093e3SVarun Sethi 	version = in_be32(pci_ctl->cfg_addr + (PCI_FSL_BRR1 >> 2));
905695093e3SVarun Sethi 	version &= PCI_FSL_BRR1_VER;
906695093e3SVarun Sethi 	/* If PCI controller version is >= 0x204 we can partition endpoints */
907cd70d465SEmil Medve 	return version >= 0x204;
908695093e3SVarun Sethi }
909695093e3SVarun Sethi 
910695093e3SVarun Sethi /* Get iommu group information from peer devices or devices on the parent bus */
911695093e3SVarun Sethi static struct iommu_group *get_shared_pci_device_group(struct pci_dev *pdev)
912695093e3SVarun Sethi {
913695093e3SVarun Sethi 	struct pci_dev *tmp;
914695093e3SVarun Sethi 	struct iommu_group *group;
915695093e3SVarun Sethi 	struct pci_bus *bus = pdev->bus;
916695093e3SVarun Sethi 
917695093e3SVarun Sethi 	/*
918695093e3SVarun Sethi 	 * Traverese the pci bus device list to get
919695093e3SVarun Sethi 	 * the shared iommu group.
920695093e3SVarun Sethi 	 */
921695093e3SVarun Sethi 	while (bus) {
922695093e3SVarun Sethi 		list_for_each_entry(tmp, &bus->devices, bus_list) {
923695093e3SVarun Sethi 			if (tmp == pdev)
924695093e3SVarun Sethi 				continue;
925695093e3SVarun Sethi 			group = iommu_group_get(&tmp->dev);
926695093e3SVarun Sethi 			if (group)
927695093e3SVarun Sethi 				return group;
928695093e3SVarun Sethi 		}
929695093e3SVarun Sethi 
930695093e3SVarun Sethi 		bus = bus->parent;
931695093e3SVarun Sethi 	}
932695093e3SVarun Sethi 
933695093e3SVarun Sethi 	return NULL;
934695093e3SVarun Sethi }
935695093e3SVarun Sethi 
936695093e3SVarun Sethi static struct iommu_group *get_pci_device_group(struct pci_dev *pdev)
937695093e3SVarun Sethi {
938695093e3SVarun Sethi 	struct pci_controller *pci_ctl;
939695093e3SVarun Sethi 	bool pci_endpt_partioning;
940695093e3SVarun Sethi 	struct iommu_group *group = NULL;
941695093e3SVarun Sethi 
942695093e3SVarun Sethi 	pci_ctl = pci_bus_to_host(pdev->bus);
943695093e3SVarun Sethi 	pci_endpt_partioning = check_pci_ctl_endpt_part(pci_ctl);
944695093e3SVarun Sethi 	/* We can partition PCIe devices so assign device group to the device */
945695093e3SVarun Sethi 	if (pci_endpt_partioning) {
946d5e58297SJoerg Roedel 		group = pci_device_group(&pdev->dev);
947695093e3SVarun Sethi 
948695093e3SVarun Sethi 		/*
949695093e3SVarun Sethi 		 * PCIe controller is not a paritionable entity
950695093e3SVarun Sethi 		 * free the controller device iommu_group.
951695093e3SVarun Sethi 		 */
952695093e3SVarun Sethi 		if (pci_ctl->parent->iommu_group)
953695093e3SVarun Sethi 			iommu_group_remove_device(pci_ctl->parent);
954695093e3SVarun Sethi 	} else {
955695093e3SVarun Sethi 		/*
956695093e3SVarun Sethi 		 * All devices connected to the controller will share the
957695093e3SVarun Sethi 		 * PCI controllers device group. If this is the first
958695093e3SVarun Sethi 		 * device to be probed for the pci controller, copy the
959695093e3SVarun Sethi 		 * device group information from the PCI controller device
960695093e3SVarun Sethi 		 * node and remove the PCI controller iommu group.
961695093e3SVarun Sethi 		 * For subsequent devices, the iommu group information can
962695093e3SVarun Sethi 		 * be obtained from sibling devices (i.e. from the bus_devices
963695093e3SVarun Sethi 		 * link list).
964695093e3SVarun Sethi 		 */
965695093e3SVarun Sethi 		if (pci_ctl->parent->iommu_group) {
966695093e3SVarun Sethi 			group = get_device_iommu_group(pci_ctl->parent);
967695093e3SVarun Sethi 			iommu_group_remove_device(pci_ctl->parent);
968cd70d465SEmil Medve 		} else {
969695093e3SVarun Sethi 			group = get_shared_pci_device_group(pdev);
970695093e3SVarun Sethi 		}
971cd70d465SEmil Medve 	}
972695093e3SVarun Sethi 
9733170447cSVarun Sethi 	if (!group)
9743170447cSVarun Sethi 		group = ERR_PTR(-ENODEV);
9753170447cSVarun Sethi 
976695093e3SVarun Sethi 	return group;
977695093e3SVarun Sethi }
978695093e3SVarun Sethi 
979d5e58297SJoerg Roedel static struct iommu_group *fsl_pamu_device_group(struct device *dev)
980695093e3SVarun Sethi {
9813170447cSVarun Sethi 	struct iommu_group *group = ERR_PTR(-ENODEV);
982d5e58297SJoerg Roedel 	int len;
983695093e3SVarun Sethi 
984695093e3SVarun Sethi 	/*
985695093e3SVarun Sethi 	 * For platform devices we allocate a separate group for
986695093e3SVarun Sethi 	 * each of the devices.
987695093e3SVarun Sethi 	 */
988d5e58297SJoerg Roedel 	if (dev_is_pci(dev))
989d5e58297SJoerg Roedel 		group = get_pci_device_group(to_pci_dev(dev));
990d5e58297SJoerg Roedel 	else if (of_get_property(dev->of_node, "fsl,liodn", &len))
991695093e3SVarun Sethi 		group = get_device_iommu_group(dev);
992d5e58297SJoerg Roedel 
993d5e58297SJoerg Roedel 	return group;
994695093e3SVarun Sethi }
995695093e3SVarun Sethi 
996d5e58297SJoerg Roedel static int fsl_pamu_add_device(struct device *dev)
997d5e58297SJoerg Roedel {
998d5e58297SJoerg Roedel 	struct iommu_group *group;
999d5e58297SJoerg Roedel 
1000d5e58297SJoerg Roedel 	group = iommu_group_get_for_dev(dev);
10013170447cSVarun Sethi 	if (IS_ERR(group))
1002695093e3SVarun Sethi 		return PTR_ERR(group);
1003695093e3SVarun Sethi 
1004695093e3SVarun Sethi 	iommu_group_put(group);
1005d5e58297SJoerg Roedel 
100668a17f0bSJoerg Roedel 	iommu_device_link(&pamu_iommu, dev);
100768a17f0bSJoerg Roedel 
1008d5e58297SJoerg Roedel 	return 0;
1009695093e3SVarun Sethi }
1010695093e3SVarun Sethi 
1011695093e3SVarun Sethi static void fsl_pamu_remove_device(struct device *dev)
1012695093e3SVarun Sethi {
101368a17f0bSJoerg Roedel 	iommu_device_unlink(&pamu_iommu, dev);
1014695093e3SVarun Sethi 	iommu_group_remove_device(dev);
1015695093e3SVarun Sethi }
1016695093e3SVarun Sethi 
1017695093e3SVarun Sethi static int fsl_pamu_set_windows(struct iommu_domain *domain, u32 w_count)
1018695093e3SVarun Sethi {
10198d4bfe40SJoerg Roedel 	struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain);
1020695093e3SVarun Sethi 	unsigned long flags;
1021695093e3SVarun Sethi 	int ret;
1022695093e3SVarun Sethi 
1023695093e3SVarun Sethi 	spin_lock_irqsave(&dma_domain->domain_lock, flags);
1024695093e3SVarun Sethi 	/* Ensure domain is inactive i.e. DMA should be disabled for the domain */
1025695093e3SVarun Sethi 	if (dma_domain->enabled) {
1026695093e3SVarun Sethi 		pr_debug("Can't set geometry attributes as domain is active\n");
1027695093e3SVarun Sethi 		spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
1028695093e3SVarun Sethi 		return  -EBUSY;
1029695093e3SVarun Sethi 	}
1030695093e3SVarun Sethi 
1031695093e3SVarun Sethi 	/* Ensure that the geometry has been set for the domain */
1032695093e3SVarun Sethi 	if (!dma_domain->geom_size) {
1033695093e3SVarun Sethi 		pr_debug("Please configure geometry before setting the number of windows\n");
1034695093e3SVarun Sethi 		spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
1035695093e3SVarun Sethi 		return -EINVAL;
1036695093e3SVarun Sethi 	}
1037695093e3SVarun Sethi 
1038695093e3SVarun Sethi 	/*
1039695093e3SVarun Sethi 	 * Ensure we have valid window count i.e. it should be less than
1040695093e3SVarun Sethi 	 * maximum permissible limit and should be a power of two.
1041695093e3SVarun Sethi 	 */
1042695093e3SVarun Sethi 	if (w_count > pamu_get_max_subwin_cnt() || !is_power_of_2(w_count)) {
1043695093e3SVarun Sethi 		pr_debug("Invalid window count\n");
1044695093e3SVarun Sethi 		spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
1045695093e3SVarun Sethi 		return -EINVAL;
1046695093e3SVarun Sethi 	}
1047695093e3SVarun Sethi 
1048695093e3SVarun Sethi 	ret = pamu_set_domain_geometry(dma_domain, &domain->geometry,
1049cd70d465SEmil Medve 				       w_count > 1 ? w_count : 0);
1050695093e3SVarun Sethi 	if (!ret) {
1051695093e3SVarun Sethi 		kfree(dma_domain->win_arr);
1052cd70d465SEmil Medve 		dma_domain->win_arr = kcalloc(w_count,
1053cd70d465SEmil Medve 					      sizeof(*dma_domain->win_arr),
1054cd70d465SEmil Medve 					      GFP_ATOMIC);
1055695093e3SVarun Sethi 		if (!dma_domain->win_arr) {
1056695093e3SVarun Sethi 			spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
1057695093e3SVarun Sethi 			return -ENOMEM;
1058695093e3SVarun Sethi 		}
1059695093e3SVarun Sethi 		dma_domain->win_cnt = w_count;
1060695093e3SVarun Sethi 	}
1061695093e3SVarun Sethi 	spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
1062695093e3SVarun Sethi 
1063695093e3SVarun Sethi 	return ret;
1064695093e3SVarun Sethi }
1065695093e3SVarun Sethi 
1066695093e3SVarun Sethi static u32 fsl_pamu_get_windows(struct iommu_domain *domain)
1067695093e3SVarun Sethi {
10688d4bfe40SJoerg Roedel 	struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain);
1069695093e3SVarun Sethi 
1070695093e3SVarun Sethi 	return dma_domain->win_cnt;
1071695093e3SVarun Sethi }
1072695093e3SVarun Sethi 
1073b22f6434SThierry Reding static const struct iommu_ops fsl_pamu_ops = {
1074b7eb6785SJoerg Roedel 	.capable	= fsl_pamu_capable,
10758d4bfe40SJoerg Roedel 	.domain_alloc	= fsl_pamu_domain_alloc,
10768d4bfe40SJoerg Roedel 	.domain_free    = fsl_pamu_domain_free,
1077695093e3SVarun Sethi 	.attach_dev	= fsl_pamu_attach_device,
1078695093e3SVarun Sethi 	.detach_dev	= fsl_pamu_detach_device,
1079695093e3SVarun Sethi 	.domain_window_enable = fsl_pamu_window_enable,
1080695093e3SVarun Sethi 	.domain_window_disable = fsl_pamu_window_disable,
1081695093e3SVarun Sethi 	.domain_get_windows = fsl_pamu_get_windows,
1082695093e3SVarun Sethi 	.domain_set_windows = fsl_pamu_set_windows,
1083695093e3SVarun Sethi 	.iova_to_phys	= fsl_pamu_iova_to_phys,
1084695093e3SVarun Sethi 	.domain_set_attr = fsl_pamu_set_domain_attr,
1085695093e3SVarun Sethi 	.domain_get_attr = fsl_pamu_get_domain_attr,
1086695093e3SVarun Sethi 	.add_device	= fsl_pamu_add_device,
1087695093e3SVarun Sethi 	.remove_device	= fsl_pamu_remove_device,
1088d5e58297SJoerg Roedel 	.device_group   = fsl_pamu_device_group,
1089695093e3SVarun Sethi };
1090695093e3SVarun Sethi 
1091cd70d465SEmil Medve int __init pamu_domain_init(void)
1092695093e3SVarun Sethi {
1093695093e3SVarun Sethi 	int ret = 0;
1094695093e3SVarun Sethi 
1095695093e3SVarun Sethi 	ret = iommu_init_mempool();
1096695093e3SVarun Sethi 	if (ret)
1097695093e3SVarun Sethi 		return ret;
1098695093e3SVarun Sethi 
10993ff2dcc0SJoerg Roedel 	ret = iommu_device_sysfs_add(&pamu_iommu, NULL, NULL, "iommu0");
11003ff2dcc0SJoerg Roedel 	if (ret)
11013ff2dcc0SJoerg Roedel 		return ret;
11023ff2dcc0SJoerg Roedel 
11033ff2dcc0SJoerg Roedel 	iommu_device_set_ops(&pamu_iommu, &fsl_pamu_ops);
11043ff2dcc0SJoerg Roedel 
11053ff2dcc0SJoerg Roedel 	ret = iommu_device_register(&pamu_iommu);
11063ff2dcc0SJoerg Roedel 	if (ret) {
11073ff2dcc0SJoerg Roedel 		iommu_device_sysfs_remove(&pamu_iommu);
11083ff2dcc0SJoerg Roedel 		pr_err("Can't register iommu device\n");
11093ff2dcc0SJoerg Roedel 		return ret;
11103ff2dcc0SJoerg Roedel 	}
11113ff2dcc0SJoerg Roedel 
1112695093e3SVarun Sethi 	bus_set_iommu(&platform_bus_type, &fsl_pamu_ops);
1113695093e3SVarun Sethi 	bus_set_iommu(&pci_bus_type, &fsl_pamu_ops);
1114695093e3SVarun Sethi 
1115695093e3SVarun Sethi 	return ret;
1116695093e3SVarun Sethi }
1117