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 
36695093e3SVarun Sethi static int __init iommu_init_mempool(void)
37695093e3SVarun Sethi {
38695093e3SVarun Sethi 	fsl_pamu_domain_cache = kmem_cache_create("fsl_pamu_domain",
39695093e3SVarun Sethi 						  sizeof(struct fsl_dma_domain),
40695093e3SVarun Sethi 						  0,
41695093e3SVarun Sethi 						  SLAB_HWCACHE_ALIGN,
42695093e3SVarun Sethi 						  NULL);
43695093e3SVarun Sethi 	if (!fsl_pamu_domain_cache) {
44695093e3SVarun Sethi 		pr_debug("Couldn't create fsl iommu_domain cache\n");
45695093e3SVarun Sethi 		return -ENOMEM;
46695093e3SVarun Sethi 	}
47695093e3SVarun Sethi 
48695093e3SVarun Sethi 	iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
49695093e3SVarun Sethi 						sizeof(struct device_domain_info),
50695093e3SVarun Sethi 						0,
51695093e3SVarun Sethi 						SLAB_HWCACHE_ALIGN,
52695093e3SVarun Sethi 						NULL);
53695093e3SVarun Sethi 	if (!iommu_devinfo_cache) {
54695093e3SVarun Sethi 		pr_debug("Couldn't create devinfo cache\n");
55695093e3SVarun Sethi 		kmem_cache_destroy(fsl_pamu_domain_cache);
56695093e3SVarun Sethi 		return -ENOMEM;
57695093e3SVarun Sethi 	}
58695093e3SVarun Sethi 
59695093e3SVarun Sethi 	return 0;
60695093e3SVarun Sethi }
61695093e3SVarun Sethi 
62695093e3SVarun Sethi static phys_addr_t get_phys_addr(struct fsl_dma_domain *dma_domain, dma_addr_t iova)
63695093e3SVarun Sethi {
64695093e3SVarun Sethi 	u32 win_cnt = dma_domain->win_cnt;
65cd70d465SEmil Medve 	struct dma_window *win_ptr = &dma_domain->win_arr[0];
66695093e3SVarun Sethi 	struct iommu_domain_geometry *geom;
67695093e3SVarun Sethi 
68695093e3SVarun Sethi 	geom = &dma_domain->iommu_domain->geometry;
69695093e3SVarun Sethi 
70695093e3SVarun Sethi 	if (!win_cnt || !dma_domain->geom_size) {
71695093e3SVarun Sethi 		pr_debug("Number of windows/geometry not configured for the domain\n");
72695093e3SVarun Sethi 		return 0;
73695093e3SVarun Sethi 	}
74695093e3SVarun Sethi 
75695093e3SVarun Sethi 	if (win_cnt > 1) {
76695093e3SVarun Sethi 		u64 subwin_size;
77695093e3SVarun Sethi 		dma_addr_t subwin_iova;
78695093e3SVarun Sethi 		u32 wnd;
79695093e3SVarun Sethi 
80695093e3SVarun Sethi 		subwin_size = dma_domain->geom_size >> ilog2(win_cnt);
81695093e3SVarun Sethi 		subwin_iova = iova & ~(subwin_size - 1);
82695093e3SVarun Sethi 		wnd = (subwin_iova - geom->aperture_start) >> ilog2(subwin_size);
83695093e3SVarun Sethi 		win_ptr = &dma_domain->win_arr[wnd];
84695093e3SVarun Sethi 	}
85695093e3SVarun Sethi 
86695093e3SVarun Sethi 	if (win_ptr->valid)
87cd70d465SEmil Medve 		return win_ptr->paddr + (iova & (win_ptr->size - 1));
88695093e3SVarun Sethi 
89695093e3SVarun Sethi 	return 0;
90695093e3SVarun Sethi }
91695093e3SVarun Sethi 
92695093e3SVarun Sethi static int map_subwins(int liodn, struct fsl_dma_domain *dma_domain)
93695093e3SVarun Sethi {
94cd70d465SEmil Medve 	struct dma_window *sub_win_ptr = &dma_domain->win_arr[0];
95695093e3SVarun Sethi 	int i, ret;
96695093e3SVarun Sethi 	unsigned long rpn, flags;
97695093e3SVarun Sethi 
98695093e3SVarun Sethi 	for (i = 0; i < dma_domain->win_cnt; i++) {
99695093e3SVarun Sethi 		if (sub_win_ptr[i].valid) {
100cd70d465SEmil Medve 			rpn = sub_win_ptr[i].paddr >> PAMU_PAGE_SHIFT;
101695093e3SVarun Sethi 			spin_lock_irqsave(&iommu_lock, flags);
102695093e3SVarun Sethi 			ret = pamu_config_spaace(liodn, dma_domain->win_cnt, i,
103695093e3SVarun Sethi 						 sub_win_ptr[i].size,
104695093e3SVarun Sethi 						 ~(u32)0,
105695093e3SVarun Sethi 						 rpn,
106695093e3SVarun Sethi 						 dma_domain->snoop_id,
107695093e3SVarun Sethi 						 dma_domain->stash_id,
108695093e3SVarun Sethi 						 (i > 0) ? 1 : 0,
109695093e3SVarun Sethi 						 sub_win_ptr[i].prot);
110695093e3SVarun Sethi 			spin_unlock_irqrestore(&iommu_lock, flags);
111695093e3SVarun Sethi 			if (ret) {
112cd70d465SEmil Medve 				pr_debug("SPAACE configuration failed for liodn %d\n",
113695093e3SVarun Sethi 					 liodn);
114695093e3SVarun Sethi 				return ret;
115695093e3SVarun Sethi 			}
116695093e3SVarun Sethi 		}
117695093e3SVarun Sethi 	}
118695093e3SVarun Sethi 
119695093e3SVarun Sethi 	return ret;
120695093e3SVarun Sethi }
121695093e3SVarun Sethi 
122695093e3SVarun Sethi static int map_win(int liodn, struct fsl_dma_domain *dma_domain)
123695093e3SVarun Sethi {
124695093e3SVarun Sethi 	int ret;
125695093e3SVarun Sethi 	struct dma_window *wnd = &dma_domain->win_arr[0];
126695093e3SVarun Sethi 	phys_addr_t wnd_addr = dma_domain->iommu_domain->geometry.aperture_start;
127695093e3SVarun Sethi 	unsigned long flags;
128695093e3SVarun Sethi 
129695093e3SVarun Sethi 	spin_lock_irqsave(&iommu_lock, flags);
130695093e3SVarun Sethi 	ret = pamu_config_ppaace(liodn, wnd_addr,
131695093e3SVarun Sethi 				 wnd->size,
132695093e3SVarun Sethi 				 ~(u32)0,
133695093e3SVarun Sethi 				 wnd->paddr >> PAMU_PAGE_SHIFT,
134695093e3SVarun Sethi 				 dma_domain->snoop_id, dma_domain->stash_id,
135695093e3SVarun Sethi 				 0, wnd->prot);
136695093e3SVarun Sethi 	spin_unlock_irqrestore(&iommu_lock, flags);
137695093e3SVarun Sethi 	if (ret)
138cd70d465SEmil Medve 		pr_debug("PAACE configuration failed for liodn %d\n", liodn);
139695093e3SVarun Sethi 
140695093e3SVarun Sethi 	return ret;
141695093e3SVarun Sethi }
142695093e3SVarun Sethi 
143695093e3SVarun Sethi /* Map the DMA window corresponding to the LIODN */
144695093e3SVarun Sethi static int map_liodn(int liodn, struct fsl_dma_domain *dma_domain)
145695093e3SVarun Sethi {
146695093e3SVarun Sethi 	if (dma_domain->win_cnt > 1)
147695093e3SVarun Sethi 		return map_subwins(liodn, dma_domain);
148695093e3SVarun Sethi 	else
149695093e3SVarun Sethi 		return map_win(liodn, dma_domain);
150695093e3SVarun Sethi }
151695093e3SVarun Sethi 
152695093e3SVarun Sethi /* Update window/subwindow mapping for the LIODN */
153695093e3SVarun Sethi static int update_liodn(int liodn, struct fsl_dma_domain *dma_domain, u32 wnd_nr)
154695093e3SVarun Sethi {
155695093e3SVarun Sethi 	int ret;
156695093e3SVarun Sethi 	struct dma_window *wnd = &dma_domain->win_arr[wnd_nr];
157695093e3SVarun Sethi 	unsigned long flags;
158695093e3SVarun Sethi 
159695093e3SVarun Sethi 	spin_lock_irqsave(&iommu_lock, flags);
160695093e3SVarun Sethi 	if (dma_domain->win_cnt > 1) {
161695093e3SVarun Sethi 		ret = pamu_config_spaace(liodn, dma_domain->win_cnt, wnd_nr,
162695093e3SVarun Sethi 					 wnd->size,
163695093e3SVarun Sethi 					 ~(u32)0,
164695093e3SVarun Sethi 					 wnd->paddr >> PAMU_PAGE_SHIFT,
165695093e3SVarun Sethi 					 dma_domain->snoop_id,
166695093e3SVarun Sethi 					 dma_domain->stash_id,
167695093e3SVarun Sethi 					 (wnd_nr > 0) ? 1 : 0,
168695093e3SVarun Sethi 					 wnd->prot);
169695093e3SVarun Sethi 		if (ret)
170cd70d465SEmil Medve 			pr_debug("Subwindow reconfiguration failed for liodn %d\n",
171cd70d465SEmil Medve 				 liodn);
172695093e3SVarun Sethi 	} else {
173695093e3SVarun Sethi 		phys_addr_t wnd_addr;
174695093e3SVarun Sethi 
175695093e3SVarun Sethi 		wnd_addr = dma_domain->iommu_domain->geometry.aperture_start;
176695093e3SVarun Sethi 
177695093e3SVarun Sethi 		ret = pamu_config_ppaace(liodn, wnd_addr,
178695093e3SVarun Sethi 					 wnd->size,
179695093e3SVarun Sethi 					 ~(u32)0,
180695093e3SVarun Sethi 					 wnd->paddr >> PAMU_PAGE_SHIFT,
181695093e3SVarun Sethi 					 dma_domain->snoop_id, dma_domain->stash_id,
182695093e3SVarun Sethi 					 0, wnd->prot);
183695093e3SVarun Sethi 		if (ret)
184cd70d465SEmil Medve 			pr_debug("Window reconfiguration failed for liodn %d\n",
185cd70d465SEmil Medve 				 liodn);
186695093e3SVarun Sethi 	}
187695093e3SVarun Sethi 
188695093e3SVarun Sethi 	spin_unlock_irqrestore(&iommu_lock, flags);
189695093e3SVarun Sethi 
190695093e3SVarun Sethi 	return ret;
191695093e3SVarun Sethi }
192695093e3SVarun Sethi 
193695093e3SVarun Sethi static int update_liodn_stash(int liodn, struct fsl_dma_domain *dma_domain,
194695093e3SVarun Sethi 			      u32 val)
195695093e3SVarun Sethi {
196695093e3SVarun Sethi 	int ret = 0, i;
197695093e3SVarun Sethi 	unsigned long flags;
198695093e3SVarun Sethi 
199695093e3SVarun Sethi 	spin_lock_irqsave(&iommu_lock, flags);
200695093e3SVarun Sethi 	if (!dma_domain->win_arr) {
201cd70d465SEmil Medve 		pr_debug("Windows not configured, stash destination update failed for liodn %d\n",
202cd70d465SEmil Medve 			 liodn);
203695093e3SVarun Sethi 		spin_unlock_irqrestore(&iommu_lock, flags);
204695093e3SVarun Sethi 		return -EINVAL;
205695093e3SVarun Sethi 	}
206695093e3SVarun Sethi 
207695093e3SVarun Sethi 	for (i = 0; i < dma_domain->win_cnt; i++) {
208695093e3SVarun Sethi 		ret = pamu_update_paace_stash(liodn, i, val);
209695093e3SVarun Sethi 		if (ret) {
210cd70d465SEmil Medve 			pr_debug("Failed to update SPAACE %d field for liodn %d\n ",
211cd70d465SEmil Medve 				 i, liodn);
212695093e3SVarun Sethi 			spin_unlock_irqrestore(&iommu_lock, flags);
213695093e3SVarun Sethi 			return ret;
214695093e3SVarun Sethi 		}
215695093e3SVarun Sethi 	}
216695093e3SVarun Sethi 
217695093e3SVarun Sethi 	spin_unlock_irqrestore(&iommu_lock, flags);
218695093e3SVarun Sethi 
219695093e3SVarun Sethi 	return ret;
220695093e3SVarun Sethi }
221695093e3SVarun Sethi 
222695093e3SVarun Sethi /* Set the geometry parameters for a LIODN */
223695093e3SVarun Sethi static int pamu_set_liodn(int liodn, struct device *dev,
224695093e3SVarun Sethi 			  struct fsl_dma_domain *dma_domain,
225695093e3SVarun Sethi 			  struct iommu_domain_geometry *geom_attr,
226695093e3SVarun Sethi 			  u32 win_cnt)
227695093e3SVarun Sethi {
228695093e3SVarun Sethi 	phys_addr_t window_addr, window_size;
229695093e3SVarun Sethi 	phys_addr_t subwin_size;
230695093e3SVarun Sethi 	int ret = 0, i;
231695093e3SVarun Sethi 	u32 omi_index = ~(u32)0;
232695093e3SVarun Sethi 	unsigned long flags;
233695093e3SVarun Sethi 
234695093e3SVarun Sethi 	/*
235695093e3SVarun Sethi 	 * Configure the omi_index at the geometry setup time.
236695093e3SVarun Sethi 	 * This is a static value which depends on the type of
237695093e3SVarun Sethi 	 * device and would not change thereafter.
238695093e3SVarun Sethi 	 */
239695093e3SVarun Sethi 	get_ome_index(&omi_index, dev);
240695093e3SVarun Sethi 
241695093e3SVarun Sethi 	window_addr = geom_attr->aperture_start;
242695093e3SVarun Sethi 	window_size = dma_domain->geom_size;
243695093e3SVarun Sethi 
244695093e3SVarun Sethi 	spin_lock_irqsave(&iommu_lock, flags);
245695093e3SVarun Sethi 	ret = pamu_disable_liodn(liodn);
246695093e3SVarun Sethi 	if (!ret)
247695093e3SVarun Sethi 		ret = pamu_config_ppaace(liodn, window_addr, window_size, omi_index,
248695093e3SVarun Sethi 					 0, dma_domain->snoop_id,
249695093e3SVarun Sethi 					 dma_domain->stash_id, win_cnt, 0);
250695093e3SVarun Sethi 	spin_unlock_irqrestore(&iommu_lock, flags);
251695093e3SVarun Sethi 	if (ret) {
252cd70d465SEmil Medve 		pr_debug("PAACE configuration failed for liodn %d, win_cnt =%d\n",
253cd70d465SEmil Medve 			 liodn, win_cnt);
254695093e3SVarun Sethi 		return ret;
255695093e3SVarun Sethi 	}
256695093e3SVarun Sethi 
257695093e3SVarun Sethi 	if (win_cnt > 1) {
258695093e3SVarun Sethi 		subwin_size = window_size >> ilog2(win_cnt);
259695093e3SVarun Sethi 		for (i = 0; i < win_cnt; i++) {
260695093e3SVarun Sethi 			spin_lock_irqsave(&iommu_lock, flags);
261695093e3SVarun Sethi 			ret = pamu_disable_spaace(liodn, i);
262695093e3SVarun Sethi 			if (!ret)
263695093e3SVarun Sethi 				ret = pamu_config_spaace(liodn, win_cnt, i,
264695093e3SVarun Sethi 							 subwin_size, omi_index,
265695093e3SVarun Sethi 							 0, dma_domain->snoop_id,
266695093e3SVarun Sethi 							 dma_domain->stash_id,
267695093e3SVarun Sethi 							 0, 0);
268695093e3SVarun Sethi 			spin_unlock_irqrestore(&iommu_lock, flags);
269695093e3SVarun Sethi 			if (ret) {
270cd70d465SEmil Medve 				pr_debug("SPAACE configuration failed for liodn %d\n",
271cd70d465SEmil Medve 					 liodn);
272695093e3SVarun Sethi 				return ret;
273695093e3SVarun Sethi 			}
274695093e3SVarun Sethi 		}
275695093e3SVarun Sethi 	}
276695093e3SVarun Sethi 
277695093e3SVarun Sethi 	return ret;
278695093e3SVarun Sethi }
279695093e3SVarun Sethi 
280695093e3SVarun Sethi static int check_size(u64 size, dma_addr_t iova)
281695093e3SVarun Sethi {
282695093e3SVarun Sethi 	/*
283695093e3SVarun Sethi 	 * Size must be a power of two and at least be equal
284695093e3SVarun Sethi 	 * to PAMU page size.
285695093e3SVarun Sethi 	 */
286d033f48fSVarun Sethi 	if ((size & (size - 1)) || size < PAMU_PAGE_SIZE) {
287cd70d465SEmil Medve 		pr_debug("Size too small or not a power of two\n");
288695093e3SVarun Sethi 		return -EINVAL;
289695093e3SVarun Sethi 	}
290695093e3SVarun Sethi 
291695093e3SVarun Sethi 	/* iova must be page size aligned */
292695093e3SVarun Sethi 	if (iova & (size - 1)) {
293cd70d465SEmil Medve 		pr_debug("Address is not aligned with window size\n");
294695093e3SVarun Sethi 		return -EINVAL;
295695093e3SVarun Sethi 	}
296695093e3SVarun Sethi 
297695093e3SVarun Sethi 	return 0;
298695093e3SVarun Sethi }
299695093e3SVarun Sethi 
300695093e3SVarun Sethi static struct fsl_dma_domain *iommu_alloc_dma_domain(void)
301695093e3SVarun Sethi {
302695093e3SVarun Sethi 	struct fsl_dma_domain *domain;
303695093e3SVarun Sethi 
304695093e3SVarun Sethi 	domain = kmem_cache_zalloc(fsl_pamu_domain_cache, GFP_KERNEL);
305695093e3SVarun Sethi 	if (!domain)
306695093e3SVarun Sethi 		return NULL;
307695093e3SVarun Sethi 
308695093e3SVarun Sethi 	domain->stash_id = ~(u32)0;
309695093e3SVarun Sethi 	domain->snoop_id = ~(u32)0;
310695093e3SVarun Sethi 	domain->win_cnt = pamu_get_max_subwin_cnt();
311695093e3SVarun Sethi 	domain->geom_size = 0;
312695093e3SVarun Sethi 
313695093e3SVarun Sethi 	INIT_LIST_HEAD(&domain->devices);
314695093e3SVarun Sethi 
315695093e3SVarun Sethi 	spin_lock_init(&domain->domain_lock);
316695093e3SVarun Sethi 
317695093e3SVarun Sethi 	return domain;
318695093e3SVarun Sethi }
319695093e3SVarun Sethi 
320695093e3SVarun Sethi static void remove_device_ref(struct device_domain_info *info, u32 win_cnt)
321695093e3SVarun Sethi {
322695093e3SVarun Sethi 	unsigned long flags;
323695093e3SVarun Sethi 
324695093e3SVarun Sethi 	list_del(&info->link);
325695093e3SVarun Sethi 	spin_lock_irqsave(&iommu_lock, flags);
326695093e3SVarun Sethi 	if (win_cnt > 1)
327695093e3SVarun Sethi 		pamu_free_subwins(info->liodn);
328695093e3SVarun Sethi 	pamu_disable_liodn(info->liodn);
329695093e3SVarun Sethi 	spin_unlock_irqrestore(&iommu_lock, flags);
330695093e3SVarun Sethi 	spin_lock_irqsave(&device_domain_lock, flags);
331695093e3SVarun Sethi 	info->dev->archdata.iommu_domain = NULL;
332695093e3SVarun Sethi 	kmem_cache_free(iommu_devinfo_cache, info);
333695093e3SVarun Sethi 	spin_unlock_irqrestore(&device_domain_lock, flags);
334695093e3SVarun Sethi }
335695093e3SVarun Sethi 
336695093e3SVarun Sethi static void detach_device(struct device *dev, struct fsl_dma_domain *dma_domain)
337695093e3SVarun Sethi {
338695093e3SVarun Sethi 	struct device_domain_info *info, *tmp;
339695093e3SVarun Sethi 	unsigned long flags;
340695093e3SVarun Sethi 
341695093e3SVarun Sethi 	spin_lock_irqsave(&dma_domain->domain_lock, flags);
342695093e3SVarun Sethi 	/* Remove the device from the domain device list */
343695093e3SVarun Sethi 	list_for_each_entry_safe(info, tmp, &dma_domain->devices, link) {
344695093e3SVarun Sethi 		if (!dev || (info->dev == dev))
345695093e3SVarun Sethi 			remove_device_ref(info, dma_domain->win_cnt);
346695093e3SVarun Sethi 	}
347695093e3SVarun Sethi 	spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
348695093e3SVarun Sethi }
349695093e3SVarun Sethi 
350695093e3SVarun Sethi static void attach_device(struct fsl_dma_domain *dma_domain, int liodn, struct device *dev)
351695093e3SVarun Sethi {
352695093e3SVarun Sethi 	struct device_domain_info *info, *old_domain_info;
353695093e3SVarun Sethi 	unsigned long flags;
354695093e3SVarun Sethi 
355695093e3SVarun Sethi 	spin_lock_irqsave(&device_domain_lock, flags);
356695093e3SVarun Sethi 	/*
357695093e3SVarun Sethi 	 * Check here if the device is already attached to domain or not.
358695093e3SVarun Sethi 	 * If the device is already attached to a domain detach it.
359695093e3SVarun Sethi 	 */
36075f0e461SVarun Sethi 	old_domain_info = dev->archdata.iommu_domain;
361695093e3SVarun Sethi 	if (old_domain_info && old_domain_info->domain != dma_domain) {
362695093e3SVarun Sethi 		spin_unlock_irqrestore(&device_domain_lock, flags);
363695093e3SVarun Sethi 		detach_device(dev, old_domain_info->domain);
364695093e3SVarun Sethi 		spin_lock_irqsave(&device_domain_lock, flags);
365695093e3SVarun Sethi 	}
366695093e3SVarun Sethi 
367695093e3SVarun Sethi 	info = kmem_cache_zalloc(iommu_devinfo_cache, GFP_ATOMIC);
368695093e3SVarun Sethi 
369695093e3SVarun Sethi 	info->dev = dev;
370695093e3SVarun Sethi 	info->liodn = liodn;
371695093e3SVarun Sethi 	info->domain = dma_domain;
372695093e3SVarun Sethi 
373695093e3SVarun Sethi 	list_add(&info->link, &dma_domain->devices);
374695093e3SVarun Sethi 	/*
375695093e3SVarun Sethi 	 * In case of devices with multiple LIODNs just store
376695093e3SVarun Sethi 	 * the info for the first LIODN as all
377695093e3SVarun Sethi 	 * LIODNs share the same domain
378695093e3SVarun Sethi 	 */
37975f0e461SVarun Sethi 	if (!dev->archdata.iommu_domain)
380695093e3SVarun Sethi 		dev->archdata.iommu_domain = info;
381695093e3SVarun Sethi 	spin_unlock_irqrestore(&device_domain_lock, flags);
382695093e3SVarun Sethi }
383695093e3SVarun Sethi 
384695093e3SVarun Sethi static phys_addr_t fsl_pamu_iova_to_phys(struct iommu_domain *domain,
385695093e3SVarun Sethi 					 dma_addr_t iova)
386695093e3SVarun Sethi {
387695093e3SVarun Sethi 	struct fsl_dma_domain *dma_domain = domain->priv;
388695093e3SVarun Sethi 
389cd70d465SEmil Medve 	if (iova < domain->geometry.aperture_start ||
390cd70d465SEmil Medve 	    iova > domain->geometry.aperture_end)
391695093e3SVarun Sethi 		return 0;
392695093e3SVarun Sethi 
393695093e3SVarun Sethi 	return get_phys_addr(dma_domain, iova);
394695093e3SVarun Sethi }
395695093e3SVarun Sethi 
396b7eb6785SJoerg Roedel static bool fsl_pamu_capable(enum iommu_cap cap)
397695093e3SVarun Sethi {
398695093e3SVarun Sethi 	return cap == IOMMU_CAP_CACHE_COHERENCY;
399695093e3SVarun Sethi }
400695093e3SVarun Sethi 
401695093e3SVarun Sethi static void fsl_pamu_domain_destroy(struct iommu_domain *domain)
402695093e3SVarun Sethi {
403695093e3SVarun Sethi 	struct fsl_dma_domain *dma_domain = domain->priv;
404695093e3SVarun Sethi 
405695093e3SVarun Sethi 	domain->priv = NULL;
406695093e3SVarun Sethi 
407695093e3SVarun Sethi 	/* remove all the devices from the device list */
408695093e3SVarun Sethi 	detach_device(NULL, dma_domain);
409695093e3SVarun Sethi 
410695093e3SVarun Sethi 	dma_domain->enabled = 0;
411695093e3SVarun Sethi 	dma_domain->mapped = 0;
412695093e3SVarun Sethi 
413695093e3SVarun Sethi 	kmem_cache_free(fsl_pamu_domain_cache, dma_domain);
414695093e3SVarun Sethi }
415695093e3SVarun Sethi 
416695093e3SVarun Sethi static int fsl_pamu_domain_init(struct iommu_domain *domain)
417695093e3SVarun Sethi {
418695093e3SVarun Sethi 	struct fsl_dma_domain *dma_domain;
419695093e3SVarun Sethi 
420695093e3SVarun Sethi 	dma_domain = iommu_alloc_dma_domain();
421695093e3SVarun Sethi 	if (!dma_domain) {
422695093e3SVarun Sethi 		pr_debug("dma_domain allocation failed\n");
423695093e3SVarun Sethi 		return -ENOMEM;
424695093e3SVarun Sethi 	}
425695093e3SVarun Sethi 	domain->priv = dma_domain;
426695093e3SVarun Sethi 	dma_domain->iommu_domain = domain;
427695093e3SVarun Sethi 	/* defaul geometry 64 GB i.e. maximum system address */
428695093e3SVarun Sethi 	domain->geometry.aperture_start = 0;
429695093e3SVarun Sethi 	domain->geometry.aperture_end = (1ULL << 36) - 1;
430695093e3SVarun Sethi 	domain->geometry.force_aperture = true;
431695093e3SVarun Sethi 
432695093e3SVarun Sethi 	return 0;
433695093e3SVarun Sethi }
434695093e3SVarun Sethi 
435695093e3SVarun Sethi /* Configure geometry settings for all LIODNs associated with domain */
436695093e3SVarun Sethi static int pamu_set_domain_geometry(struct fsl_dma_domain *dma_domain,
437695093e3SVarun Sethi 				    struct iommu_domain_geometry *geom_attr,
438695093e3SVarun Sethi 				    u32 win_cnt)
439695093e3SVarun Sethi {
440695093e3SVarun Sethi 	struct device_domain_info *info;
441695093e3SVarun Sethi 	int ret = 0;
442695093e3SVarun Sethi 
443695093e3SVarun Sethi 	list_for_each_entry(info, &dma_domain->devices, link) {
444695093e3SVarun Sethi 		ret = pamu_set_liodn(info->liodn, info->dev, dma_domain,
445695093e3SVarun Sethi 				     geom_attr, win_cnt);
446695093e3SVarun Sethi 		if (ret)
447695093e3SVarun Sethi 			break;
448695093e3SVarun Sethi 	}
449695093e3SVarun Sethi 
450695093e3SVarun Sethi 	return ret;
451695093e3SVarun Sethi }
452695093e3SVarun Sethi 
453695093e3SVarun Sethi /* Update stash destination for all LIODNs associated with the domain */
454695093e3SVarun Sethi static int update_domain_stash(struct fsl_dma_domain *dma_domain, u32 val)
455695093e3SVarun Sethi {
456695093e3SVarun Sethi 	struct device_domain_info *info;
457695093e3SVarun Sethi 	int ret = 0;
458695093e3SVarun Sethi 
459695093e3SVarun Sethi 	list_for_each_entry(info, &dma_domain->devices, link) {
460695093e3SVarun Sethi 		ret = update_liodn_stash(info->liodn, dma_domain, val);
461695093e3SVarun Sethi 		if (ret)
462695093e3SVarun Sethi 			break;
463695093e3SVarun Sethi 	}
464695093e3SVarun Sethi 
465695093e3SVarun Sethi 	return ret;
466695093e3SVarun Sethi }
467695093e3SVarun Sethi 
468695093e3SVarun Sethi /* Update domain mappings for all LIODNs associated with the domain */
469695093e3SVarun Sethi static int update_domain_mapping(struct fsl_dma_domain *dma_domain, u32 wnd_nr)
470695093e3SVarun Sethi {
471695093e3SVarun Sethi 	struct device_domain_info *info;
472695093e3SVarun Sethi 	int ret = 0;
473695093e3SVarun Sethi 
474695093e3SVarun Sethi 	list_for_each_entry(info, &dma_domain->devices, link) {
475695093e3SVarun Sethi 		ret = update_liodn(info->liodn, dma_domain, wnd_nr);
476695093e3SVarun Sethi 		if (ret)
477695093e3SVarun Sethi 			break;
478695093e3SVarun Sethi 	}
479695093e3SVarun Sethi 	return ret;
480695093e3SVarun Sethi }
481695093e3SVarun Sethi 
482695093e3SVarun Sethi static int disable_domain_win(struct fsl_dma_domain *dma_domain, u32 wnd_nr)
483695093e3SVarun Sethi {
484695093e3SVarun Sethi 	struct device_domain_info *info;
485695093e3SVarun Sethi 	int ret = 0;
486695093e3SVarun Sethi 
487695093e3SVarun Sethi 	list_for_each_entry(info, &dma_domain->devices, link) {
488695093e3SVarun Sethi 		if (dma_domain->win_cnt == 1 && dma_domain->enabled) {
489695093e3SVarun Sethi 			ret = pamu_disable_liodn(info->liodn);
490695093e3SVarun Sethi 			if (!ret)
491695093e3SVarun Sethi 				dma_domain->enabled = 0;
492695093e3SVarun Sethi 		} else {
493695093e3SVarun Sethi 			ret = pamu_disable_spaace(info->liodn, wnd_nr);
494695093e3SVarun Sethi 		}
495695093e3SVarun Sethi 	}
496695093e3SVarun Sethi 
497695093e3SVarun Sethi 	return ret;
498695093e3SVarun Sethi }
499695093e3SVarun Sethi 
500695093e3SVarun Sethi static void fsl_pamu_window_disable(struct iommu_domain *domain, u32 wnd_nr)
501695093e3SVarun Sethi {
502695093e3SVarun Sethi 	struct fsl_dma_domain *dma_domain = domain->priv;
503695093e3SVarun Sethi 	unsigned long flags;
504695093e3SVarun Sethi 	int ret;
505695093e3SVarun Sethi 
506695093e3SVarun Sethi 	spin_lock_irqsave(&dma_domain->domain_lock, flags);
507695093e3SVarun Sethi 	if (!dma_domain->win_arr) {
508695093e3SVarun Sethi 		pr_debug("Number of windows not configured\n");
509695093e3SVarun Sethi 		spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
510695093e3SVarun Sethi 		return;
511695093e3SVarun Sethi 	}
512695093e3SVarun Sethi 
513695093e3SVarun Sethi 	if (wnd_nr >= dma_domain->win_cnt) {
514695093e3SVarun Sethi 		pr_debug("Invalid window index\n");
515695093e3SVarun Sethi 		spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
516695093e3SVarun Sethi 		return;
517695093e3SVarun Sethi 	}
518695093e3SVarun Sethi 
519695093e3SVarun Sethi 	if (dma_domain->win_arr[wnd_nr].valid) {
520695093e3SVarun Sethi 		ret = disable_domain_win(dma_domain, wnd_nr);
521695093e3SVarun Sethi 		if (!ret) {
522695093e3SVarun Sethi 			dma_domain->win_arr[wnd_nr].valid = 0;
523695093e3SVarun Sethi 			dma_domain->mapped--;
524695093e3SVarun Sethi 		}
525695093e3SVarun Sethi 	}
526695093e3SVarun Sethi 
527695093e3SVarun Sethi 	spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
528695093e3SVarun Sethi }
529695093e3SVarun Sethi 
530695093e3SVarun Sethi static int fsl_pamu_window_enable(struct iommu_domain *domain, u32 wnd_nr,
531695093e3SVarun Sethi 				  phys_addr_t paddr, u64 size, int prot)
532695093e3SVarun Sethi {
533695093e3SVarun Sethi 	struct fsl_dma_domain *dma_domain = domain->priv;
534695093e3SVarun Sethi 	struct dma_window *wnd;
535695093e3SVarun Sethi 	int pamu_prot = 0;
536695093e3SVarun Sethi 	int ret;
537695093e3SVarun Sethi 	unsigned long flags;
538695093e3SVarun Sethi 	u64 win_size;
539695093e3SVarun Sethi 
540695093e3SVarun Sethi 	if (prot & IOMMU_READ)
541695093e3SVarun Sethi 		pamu_prot |= PAACE_AP_PERMS_QUERY;
542695093e3SVarun Sethi 	if (prot & IOMMU_WRITE)
543695093e3SVarun Sethi 		pamu_prot |= PAACE_AP_PERMS_UPDATE;
544695093e3SVarun Sethi 
545695093e3SVarun Sethi 	spin_lock_irqsave(&dma_domain->domain_lock, flags);
546695093e3SVarun Sethi 	if (!dma_domain->win_arr) {
547695093e3SVarun Sethi 		pr_debug("Number of windows not configured\n");
548695093e3SVarun Sethi 		spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
549695093e3SVarun Sethi 		return -ENODEV;
550695093e3SVarun Sethi 	}
551695093e3SVarun Sethi 
552695093e3SVarun Sethi 	if (wnd_nr >= dma_domain->win_cnt) {
553695093e3SVarun Sethi 		pr_debug("Invalid window index\n");
554695093e3SVarun Sethi 		spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
555695093e3SVarun Sethi 		return -EINVAL;
556695093e3SVarun Sethi 	}
557695093e3SVarun Sethi 
558695093e3SVarun Sethi 	win_size = dma_domain->geom_size >> ilog2(dma_domain->win_cnt);
559695093e3SVarun Sethi 	if (size > win_size) {
560695093e3SVarun Sethi 		pr_debug("Invalid window size\n");
561695093e3SVarun Sethi 		spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
562695093e3SVarun Sethi 		return -EINVAL;
563695093e3SVarun Sethi 	}
564695093e3SVarun Sethi 
565695093e3SVarun Sethi 	if (dma_domain->win_cnt == 1) {
566695093e3SVarun Sethi 		if (dma_domain->enabled) {
567695093e3SVarun Sethi 			pr_debug("Disable the window before updating the mapping\n");
568695093e3SVarun Sethi 			spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
569695093e3SVarun Sethi 			return -EBUSY;
570695093e3SVarun Sethi 		}
571695093e3SVarun Sethi 
572695093e3SVarun Sethi 		ret = check_size(size, domain->geometry.aperture_start);
573695093e3SVarun Sethi 		if (ret) {
574695093e3SVarun Sethi 			pr_debug("Aperture start not aligned to the size\n");
575695093e3SVarun Sethi 			spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
576695093e3SVarun Sethi 			return -EINVAL;
577695093e3SVarun Sethi 		}
578695093e3SVarun Sethi 	}
579695093e3SVarun Sethi 
580695093e3SVarun Sethi 	wnd = &dma_domain->win_arr[wnd_nr];
581695093e3SVarun Sethi 	if (!wnd->valid) {
582695093e3SVarun Sethi 		wnd->paddr = paddr;
583695093e3SVarun Sethi 		wnd->size = size;
584695093e3SVarun Sethi 		wnd->prot = pamu_prot;
585695093e3SVarun Sethi 
586695093e3SVarun Sethi 		ret = update_domain_mapping(dma_domain, wnd_nr);
587695093e3SVarun Sethi 		if (!ret) {
588695093e3SVarun Sethi 			wnd->valid = 1;
589695093e3SVarun Sethi 			dma_domain->mapped++;
590695093e3SVarun Sethi 		}
591695093e3SVarun Sethi 	} else {
592695093e3SVarun Sethi 		pr_debug("Disable the window before updating the mapping\n");
593695093e3SVarun Sethi 		ret = -EBUSY;
594695093e3SVarun Sethi 	}
595695093e3SVarun Sethi 
596695093e3SVarun Sethi 	spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
597695093e3SVarun Sethi 
598695093e3SVarun Sethi 	return ret;
599695093e3SVarun Sethi }
600695093e3SVarun Sethi 
601695093e3SVarun Sethi /*
602695093e3SVarun Sethi  * Attach the LIODN to the DMA domain and configure the geometry
603695093e3SVarun Sethi  * and window mappings.
604695093e3SVarun Sethi  */
605695093e3SVarun Sethi static int handle_attach_device(struct fsl_dma_domain *dma_domain,
606695093e3SVarun Sethi 				struct device *dev, const u32 *liodn,
607695093e3SVarun Sethi 				int num)
608695093e3SVarun Sethi {
609695093e3SVarun Sethi 	unsigned long flags;
610695093e3SVarun Sethi 	struct iommu_domain *domain = dma_domain->iommu_domain;
611695093e3SVarun Sethi 	int ret = 0;
612695093e3SVarun Sethi 	int i;
613695093e3SVarun Sethi 
614695093e3SVarun Sethi 	spin_lock_irqsave(&dma_domain->domain_lock, flags);
615695093e3SVarun Sethi 	for (i = 0; i < num; i++) {
616695093e3SVarun Sethi 		/* Ensure that LIODN value is valid */
617695093e3SVarun Sethi 		if (liodn[i] >= PAACE_NUMBER_ENTRIES) {
618695093e3SVarun Sethi 			pr_debug("Invalid liodn %d, attach device failed for %s\n",
619695093e3SVarun Sethi 				 liodn[i], dev->of_node->full_name);
620695093e3SVarun Sethi 			ret = -EINVAL;
621695093e3SVarun Sethi 			break;
622695093e3SVarun Sethi 		}
623695093e3SVarun Sethi 
624695093e3SVarun Sethi 		attach_device(dma_domain, liodn[i], dev);
625695093e3SVarun Sethi 		/*
626695093e3SVarun Sethi 		 * Check if geometry has already been configured
627695093e3SVarun Sethi 		 * for the domain. If yes, set the geometry for
628695093e3SVarun Sethi 		 * the LIODN.
629695093e3SVarun Sethi 		 */
630695093e3SVarun Sethi 		if (dma_domain->win_arr) {
631695093e3SVarun Sethi 			u32 win_cnt = dma_domain->win_cnt > 1 ? dma_domain->win_cnt : 0;
632cd70d465SEmil Medve 
633695093e3SVarun Sethi 			ret = pamu_set_liodn(liodn[i], dev, dma_domain,
634cd70d465SEmil Medve 					     &domain->geometry, win_cnt);
635695093e3SVarun Sethi 			if (ret)
636695093e3SVarun Sethi 				break;
637695093e3SVarun Sethi 			if (dma_domain->mapped) {
638695093e3SVarun Sethi 				/*
639695093e3SVarun Sethi 				 * Create window/subwindow mapping for
640695093e3SVarun Sethi 				 * the LIODN.
641695093e3SVarun Sethi 				 */
642695093e3SVarun Sethi 				ret = map_liodn(liodn[i], dma_domain);
643695093e3SVarun Sethi 				if (ret)
644695093e3SVarun Sethi 					break;
645695093e3SVarun Sethi 			}
646695093e3SVarun Sethi 		}
647695093e3SVarun Sethi 	}
648695093e3SVarun Sethi 	spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
649695093e3SVarun Sethi 
650695093e3SVarun Sethi 	return ret;
651695093e3SVarun Sethi }
652695093e3SVarun Sethi 
653695093e3SVarun Sethi static int fsl_pamu_attach_device(struct iommu_domain *domain,
654695093e3SVarun Sethi 				  struct device *dev)
655695093e3SVarun Sethi {
656695093e3SVarun Sethi 	struct fsl_dma_domain *dma_domain = domain->priv;
657695093e3SVarun Sethi 	const u32 *liodn;
658695093e3SVarun Sethi 	u32 liodn_cnt;
659695093e3SVarun Sethi 	int len, ret = 0;
660695093e3SVarun Sethi 	struct pci_dev *pdev = NULL;
661695093e3SVarun Sethi 	struct pci_controller *pci_ctl;
662695093e3SVarun Sethi 
663695093e3SVarun Sethi 	/*
664695093e3SVarun Sethi 	 * Use LIODN of the PCI controller while attaching a
665695093e3SVarun Sethi 	 * PCI device.
666695093e3SVarun Sethi 	 */
667b3eb76d1SYijing Wang 	if (dev_is_pci(dev)) {
668695093e3SVarun Sethi 		pdev = to_pci_dev(dev);
669695093e3SVarun Sethi 		pci_ctl = pci_bus_to_host(pdev->bus);
670695093e3SVarun Sethi 		/*
671695093e3SVarun Sethi 		 * make dev point to pci controller device
672695093e3SVarun Sethi 		 * so we can get the LIODN programmed by
673695093e3SVarun Sethi 		 * u-boot.
674695093e3SVarun Sethi 		 */
675695093e3SVarun Sethi 		dev = pci_ctl->parent;
676695093e3SVarun Sethi 	}
677695093e3SVarun Sethi 
678695093e3SVarun Sethi 	liodn = of_get_property(dev->of_node, "fsl,liodn", &len);
679695093e3SVarun Sethi 	if (liodn) {
680695093e3SVarun Sethi 		liodn_cnt = len / sizeof(u32);
681cd70d465SEmil Medve 		ret = handle_attach_device(dma_domain, dev, liodn, liodn_cnt);
682695093e3SVarun Sethi 	} else {
683695093e3SVarun Sethi 		pr_debug("missing fsl,liodn property at %s\n",
684695093e3SVarun Sethi 			 dev->of_node->full_name);
685695093e3SVarun Sethi 		ret = -EINVAL;
686695093e3SVarun Sethi 	}
687695093e3SVarun Sethi 
688695093e3SVarun Sethi 	return ret;
689695093e3SVarun Sethi }
690695093e3SVarun Sethi 
691695093e3SVarun Sethi static void fsl_pamu_detach_device(struct iommu_domain *domain,
692695093e3SVarun Sethi 				   struct device *dev)
693695093e3SVarun Sethi {
694695093e3SVarun Sethi 	struct fsl_dma_domain *dma_domain = domain->priv;
695695093e3SVarun Sethi 	const u32 *prop;
696695093e3SVarun Sethi 	int len;
697695093e3SVarun Sethi 	struct pci_dev *pdev = NULL;
698695093e3SVarun Sethi 	struct pci_controller *pci_ctl;
699695093e3SVarun Sethi 
700695093e3SVarun Sethi 	/*
701695093e3SVarun Sethi 	 * Use LIODN of the PCI controller while detaching a
702695093e3SVarun Sethi 	 * PCI device.
703695093e3SVarun Sethi 	 */
704b3eb76d1SYijing Wang 	if (dev_is_pci(dev)) {
705695093e3SVarun Sethi 		pdev = to_pci_dev(dev);
706695093e3SVarun Sethi 		pci_ctl = pci_bus_to_host(pdev->bus);
707695093e3SVarun Sethi 		/*
708695093e3SVarun Sethi 		 * make dev point to pci controller device
709695093e3SVarun Sethi 		 * so we can get the LIODN programmed by
710695093e3SVarun Sethi 		 * u-boot.
711695093e3SVarun Sethi 		 */
712695093e3SVarun Sethi 		dev = pci_ctl->parent;
713695093e3SVarun Sethi 	}
714695093e3SVarun Sethi 
715695093e3SVarun Sethi 	prop = of_get_property(dev->of_node, "fsl,liodn", &len);
716695093e3SVarun Sethi 	if (prop)
717695093e3SVarun Sethi 		detach_device(dev, dma_domain);
718695093e3SVarun Sethi 	else
719695093e3SVarun Sethi 		pr_debug("missing fsl,liodn property at %s\n",
720695093e3SVarun Sethi 			 dev->of_node->full_name);
721695093e3SVarun Sethi }
722695093e3SVarun Sethi 
723695093e3SVarun Sethi static  int configure_domain_geometry(struct iommu_domain *domain, void *data)
724695093e3SVarun Sethi {
725695093e3SVarun Sethi 	struct iommu_domain_geometry *geom_attr = data;
726695093e3SVarun Sethi 	struct fsl_dma_domain *dma_domain = domain->priv;
727695093e3SVarun Sethi 	dma_addr_t geom_size;
728695093e3SVarun Sethi 	unsigned long flags;
729695093e3SVarun Sethi 
730695093e3SVarun Sethi 	geom_size = geom_attr->aperture_end - geom_attr->aperture_start + 1;
731695093e3SVarun Sethi 	/*
732695093e3SVarun Sethi 	 * Sanity check the geometry size. Also, we do not support
733695093e3SVarun Sethi 	 * DMA outside of the geometry.
734695093e3SVarun Sethi 	 */
735695093e3SVarun Sethi 	if (check_size(geom_size, geom_attr->aperture_start) ||
736695093e3SVarun Sethi 	    !geom_attr->force_aperture) {
737695093e3SVarun Sethi 		pr_debug("Invalid PAMU geometry attributes\n");
738695093e3SVarun Sethi 		return -EINVAL;
739695093e3SVarun Sethi 	}
740695093e3SVarun Sethi 
741695093e3SVarun Sethi 	spin_lock_irqsave(&dma_domain->domain_lock, flags);
742695093e3SVarun Sethi 	if (dma_domain->enabled) {
743695093e3SVarun Sethi 		pr_debug("Can't set geometry attributes as domain is active\n");
744695093e3SVarun Sethi 		spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
745695093e3SVarun Sethi 		return  -EBUSY;
746695093e3SVarun Sethi 	}
747695093e3SVarun Sethi 
748695093e3SVarun Sethi 	/* Copy the domain geometry information */
749695093e3SVarun Sethi 	memcpy(&domain->geometry, geom_attr,
750695093e3SVarun Sethi 	       sizeof(struct iommu_domain_geometry));
751695093e3SVarun Sethi 	dma_domain->geom_size = geom_size;
752695093e3SVarun Sethi 
753695093e3SVarun Sethi 	spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
754695093e3SVarun Sethi 
755695093e3SVarun Sethi 	return 0;
756695093e3SVarun Sethi }
757695093e3SVarun Sethi 
758695093e3SVarun Sethi /* Set the domain stash attribute */
759695093e3SVarun Sethi static int configure_domain_stash(struct fsl_dma_domain *dma_domain, void *data)
760695093e3SVarun Sethi {
761695093e3SVarun Sethi 	struct pamu_stash_attribute *stash_attr = data;
762695093e3SVarun Sethi 	unsigned long flags;
763695093e3SVarun Sethi 	int ret;
764695093e3SVarun Sethi 
765695093e3SVarun Sethi 	spin_lock_irqsave(&dma_domain->domain_lock, flags);
766695093e3SVarun Sethi 
767695093e3SVarun Sethi 	memcpy(&dma_domain->dma_stash, stash_attr,
768695093e3SVarun Sethi 	       sizeof(struct pamu_stash_attribute));
769695093e3SVarun Sethi 
770695093e3SVarun Sethi 	dma_domain->stash_id = get_stash_id(stash_attr->cache,
771695093e3SVarun Sethi 					    stash_attr->cpu);
772695093e3SVarun Sethi 	if (dma_domain->stash_id == ~(u32)0) {
773695093e3SVarun Sethi 		pr_debug("Invalid stash attributes\n");
774695093e3SVarun Sethi 		spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
775695093e3SVarun Sethi 		return -EINVAL;
776695093e3SVarun Sethi 	}
777695093e3SVarun Sethi 
778695093e3SVarun Sethi 	ret = update_domain_stash(dma_domain, dma_domain->stash_id);
779695093e3SVarun Sethi 
780695093e3SVarun Sethi 	spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
781695093e3SVarun Sethi 
782695093e3SVarun Sethi 	return ret;
783695093e3SVarun Sethi }
784695093e3SVarun Sethi 
785695093e3SVarun Sethi /* Configure domain dma state i.e. enable/disable DMA */
786695093e3SVarun Sethi static int configure_domain_dma_state(struct fsl_dma_domain *dma_domain, bool enable)
787695093e3SVarun Sethi {
788695093e3SVarun Sethi 	struct device_domain_info *info;
789695093e3SVarun Sethi 	unsigned long flags;
790695093e3SVarun Sethi 	int ret;
791695093e3SVarun Sethi 
792695093e3SVarun Sethi 	spin_lock_irqsave(&dma_domain->domain_lock, flags);
793695093e3SVarun Sethi 
794695093e3SVarun Sethi 	if (enable && !dma_domain->mapped) {
795695093e3SVarun Sethi 		pr_debug("Can't enable DMA domain without valid mapping\n");
796695093e3SVarun Sethi 		spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
797695093e3SVarun Sethi 		return -ENODEV;
798695093e3SVarun Sethi 	}
799695093e3SVarun Sethi 
800695093e3SVarun Sethi 	dma_domain->enabled = enable;
801cd70d465SEmil Medve 	list_for_each_entry(info, &dma_domain->devices, link) {
802695093e3SVarun Sethi 		ret = (enable) ? pamu_enable_liodn(info->liodn) :
803695093e3SVarun Sethi 			pamu_disable_liodn(info->liodn);
804695093e3SVarun Sethi 		if (ret)
805695093e3SVarun Sethi 			pr_debug("Unable to set dma state for liodn %d",
806695093e3SVarun Sethi 				 info->liodn);
807695093e3SVarun Sethi 	}
808695093e3SVarun Sethi 	spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
809695093e3SVarun Sethi 
810695093e3SVarun Sethi 	return 0;
811695093e3SVarun Sethi }
812695093e3SVarun Sethi 
813695093e3SVarun Sethi static int fsl_pamu_set_domain_attr(struct iommu_domain *domain,
814695093e3SVarun Sethi 				    enum iommu_attr attr_type, void *data)
815695093e3SVarun Sethi {
816695093e3SVarun Sethi 	struct fsl_dma_domain *dma_domain = domain->priv;
817695093e3SVarun Sethi 	int ret = 0;
818695093e3SVarun Sethi 
819695093e3SVarun Sethi 	switch (attr_type) {
820695093e3SVarun Sethi 	case DOMAIN_ATTR_GEOMETRY:
821695093e3SVarun Sethi 		ret = configure_domain_geometry(domain, data);
822695093e3SVarun Sethi 		break;
823695093e3SVarun Sethi 	case DOMAIN_ATTR_FSL_PAMU_STASH:
824695093e3SVarun Sethi 		ret = configure_domain_stash(dma_domain, data);
825695093e3SVarun Sethi 		break;
826695093e3SVarun Sethi 	case DOMAIN_ATTR_FSL_PAMU_ENABLE:
827695093e3SVarun Sethi 		ret = configure_domain_dma_state(dma_domain, *(int *)data);
828695093e3SVarun Sethi 		break;
829695093e3SVarun Sethi 	default:
830695093e3SVarun Sethi 		pr_debug("Unsupported attribute type\n");
831695093e3SVarun Sethi 		ret = -EINVAL;
832695093e3SVarun Sethi 		break;
833cd70d465SEmil Medve 	}
834695093e3SVarun Sethi 
835695093e3SVarun Sethi 	return ret;
836695093e3SVarun Sethi }
837695093e3SVarun Sethi 
838695093e3SVarun Sethi static int fsl_pamu_get_domain_attr(struct iommu_domain *domain,
839695093e3SVarun Sethi 				    enum iommu_attr attr_type, void *data)
840695093e3SVarun Sethi {
841695093e3SVarun Sethi 	struct fsl_dma_domain *dma_domain = domain->priv;
842695093e3SVarun Sethi 	int ret = 0;
843695093e3SVarun Sethi 
844695093e3SVarun Sethi 	switch (attr_type) {
845695093e3SVarun Sethi 	case DOMAIN_ATTR_FSL_PAMU_STASH:
846cd70d465SEmil Medve 		memcpy(data, &dma_domain->dma_stash,
847695093e3SVarun Sethi 		       sizeof(struct pamu_stash_attribute));
848695093e3SVarun Sethi 		break;
849695093e3SVarun Sethi 	case DOMAIN_ATTR_FSL_PAMU_ENABLE:
850695093e3SVarun Sethi 		*(int *)data = dma_domain->enabled;
851695093e3SVarun Sethi 		break;
852695093e3SVarun Sethi 	case DOMAIN_ATTR_FSL_PAMUV1:
853695093e3SVarun Sethi 		*(int *)data = DOMAIN_ATTR_FSL_PAMUV1;
854695093e3SVarun Sethi 		break;
855695093e3SVarun Sethi 	default:
856695093e3SVarun Sethi 		pr_debug("Unsupported attribute type\n");
857695093e3SVarun Sethi 		ret = -EINVAL;
858695093e3SVarun Sethi 		break;
859cd70d465SEmil Medve 	}
860695093e3SVarun Sethi 
861695093e3SVarun Sethi 	return ret;
862695093e3SVarun Sethi }
863695093e3SVarun Sethi 
864695093e3SVarun Sethi static struct iommu_group *get_device_iommu_group(struct device *dev)
865695093e3SVarun Sethi {
866695093e3SVarun Sethi 	struct iommu_group *group;
867695093e3SVarun Sethi 
868695093e3SVarun Sethi 	group = iommu_group_get(dev);
869695093e3SVarun Sethi 	if (!group)
870695093e3SVarun Sethi 		group = iommu_group_alloc();
871695093e3SVarun Sethi 
872695093e3SVarun Sethi 	return group;
873695093e3SVarun Sethi }
874695093e3SVarun Sethi 
875695093e3SVarun Sethi static  bool check_pci_ctl_endpt_part(struct pci_controller *pci_ctl)
876695093e3SVarun Sethi {
877695093e3SVarun Sethi 	u32 version;
878695093e3SVarun Sethi 
879695093e3SVarun Sethi 	/* Check the PCI controller version number by readding BRR1 register */
880695093e3SVarun Sethi 	version = in_be32(pci_ctl->cfg_addr + (PCI_FSL_BRR1 >> 2));
881695093e3SVarun Sethi 	version &= PCI_FSL_BRR1_VER;
882695093e3SVarun Sethi 	/* If PCI controller version is >= 0x204 we can partition endpoints */
883cd70d465SEmil Medve 	return version >= 0x204;
884695093e3SVarun Sethi }
885695093e3SVarun Sethi 
886695093e3SVarun Sethi /* Get iommu group information from peer devices or devices on the parent bus */
887695093e3SVarun Sethi static struct iommu_group *get_shared_pci_device_group(struct pci_dev *pdev)
888695093e3SVarun Sethi {
889695093e3SVarun Sethi 	struct pci_dev *tmp;
890695093e3SVarun Sethi 	struct iommu_group *group;
891695093e3SVarun Sethi 	struct pci_bus *bus = pdev->bus;
892695093e3SVarun Sethi 
893695093e3SVarun Sethi 	/*
894695093e3SVarun Sethi 	 * Traverese the pci bus device list to get
895695093e3SVarun Sethi 	 * the shared iommu group.
896695093e3SVarun Sethi 	 */
897695093e3SVarun Sethi 	while (bus) {
898695093e3SVarun Sethi 		list_for_each_entry(tmp, &bus->devices, bus_list) {
899695093e3SVarun Sethi 			if (tmp == pdev)
900695093e3SVarun Sethi 				continue;
901695093e3SVarun Sethi 			group = iommu_group_get(&tmp->dev);
902695093e3SVarun Sethi 			if (group)
903695093e3SVarun Sethi 				return group;
904695093e3SVarun Sethi 		}
905695093e3SVarun Sethi 
906695093e3SVarun Sethi 		bus = bus->parent;
907695093e3SVarun Sethi 	}
908695093e3SVarun Sethi 
909695093e3SVarun Sethi 	return NULL;
910695093e3SVarun Sethi }
911695093e3SVarun Sethi 
912695093e3SVarun Sethi static struct iommu_group *get_pci_device_group(struct pci_dev *pdev)
913695093e3SVarun Sethi {
914695093e3SVarun Sethi 	struct pci_controller *pci_ctl;
915695093e3SVarun Sethi 	bool pci_endpt_partioning;
916695093e3SVarun Sethi 	struct iommu_group *group = NULL;
917695093e3SVarun Sethi 
918695093e3SVarun Sethi 	pci_ctl = pci_bus_to_host(pdev->bus);
919695093e3SVarun Sethi 	pci_endpt_partioning = check_pci_ctl_endpt_part(pci_ctl);
920695093e3SVarun Sethi 	/* We can partition PCIe devices so assign device group to the device */
921695093e3SVarun Sethi 	if (pci_endpt_partioning) {
9227319ededSAlex Williamson 		group = iommu_group_get_for_dev(&pdev->dev);
923695093e3SVarun Sethi 
924695093e3SVarun Sethi 		/*
925695093e3SVarun Sethi 		 * PCIe controller is not a paritionable entity
926695093e3SVarun Sethi 		 * free the controller device iommu_group.
927695093e3SVarun Sethi 		 */
928695093e3SVarun Sethi 		if (pci_ctl->parent->iommu_group)
929695093e3SVarun Sethi 			iommu_group_remove_device(pci_ctl->parent);
930695093e3SVarun Sethi 	} else {
931695093e3SVarun Sethi 		/*
932695093e3SVarun Sethi 		 * All devices connected to the controller will share the
933695093e3SVarun Sethi 		 * PCI controllers device group. If this is the first
934695093e3SVarun Sethi 		 * device to be probed for the pci controller, copy the
935695093e3SVarun Sethi 		 * device group information from the PCI controller device
936695093e3SVarun Sethi 		 * node and remove the PCI controller iommu group.
937695093e3SVarun Sethi 		 * For subsequent devices, the iommu group information can
938695093e3SVarun Sethi 		 * be obtained from sibling devices (i.e. from the bus_devices
939695093e3SVarun Sethi 		 * link list).
940695093e3SVarun Sethi 		 */
941695093e3SVarun Sethi 		if (pci_ctl->parent->iommu_group) {
942695093e3SVarun Sethi 			group = get_device_iommu_group(pci_ctl->parent);
943695093e3SVarun Sethi 			iommu_group_remove_device(pci_ctl->parent);
944cd70d465SEmil Medve 		} else {
945695093e3SVarun Sethi 			group = get_shared_pci_device_group(pdev);
946695093e3SVarun Sethi 		}
947cd70d465SEmil Medve 	}
948695093e3SVarun Sethi 
9493170447cSVarun Sethi 	if (!group)
9503170447cSVarun Sethi 		group = ERR_PTR(-ENODEV);
9513170447cSVarun Sethi 
952695093e3SVarun Sethi 	return group;
953695093e3SVarun Sethi }
954695093e3SVarun Sethi 
955695093e3SVarun Sethi static int fsl_pamu_add_device(struct device *dev)
956695093e3SVarun Sethi {
9573170447cSVarun Sethi 	struct iommu_group *group = ERR_PTR(-ENODEV);
958695093e3SVarun Sethi 	struct pci_dev *pdev;
959695093e3SVarun Sethi 	const u32 *prop;
9605a9137a6SVarun Sethi 	int ret = 0, len;
961695093e3SVarun Sethi 
962695093e3SVarun Sethi 	/*
963695093e3SVarun Sethi 	 * For platform devices we allocate a separate group for
964695093e3SVarun Sethi 	 * each of the devices.
965695093e3SVarun Sethi 	 */
966b3eb76d1SYijing Wang 	if (dev_is_pci(dev)) {
967695093e3SVarun Sethi 		pdev = to_pci_dev(dev);
968695093e3SVarun Sethi 		/* Don't create device groups for virtual PCI bridges */
969695093e3SVarun Sethi 		if (pdev->subordinate)
970695093e3SVarun Sethi 			return 0;
971695093e3SVarun Sethi 
972695093e3SVarun Sethi 		group = get_pci_device_group(pdev);
973695093e3SVarun Sethi 
974695093e3SVarun Sethi 	} else {
975695093e3SVarun Sethi 		prop = of_get_property(dev->of_node, "fsl,liodn", &len);
976695093e3SVarun Sethi 		if (prop)
977695093e3SVarun Sethi 			group = get_device_iommu_group(dev);
978695093e3SVarun Sethi 	}
979695093e3SVarun Sethi 
9803170447cSVarun Sethi 	if (IS_ERR(group))
981695093e3SVarun Sethi 		return PTR_ERR(group);
982695093e3SVarun Sethi 
9835a9137a6SVarun Sethi 	/*
9845a9137a6SVarun Sethi 	 * Check if device has already been added to an iommu group.
9855a9137a6SVarun Sethi 	 * Group could have already been created for a PCI device in
9865a9137a6SVarun Sethi 	 * the iommu_group_get_for_dev path.
9875a9137a6SVarun Sethi 	 */
9885a9137a6SVarun Sethi 	if (!dev->iommu_group)
989695093e3SVarun Sethi 		ret = iommu_group_add_device(group, dev);
990695093e3SVarun Sethi 
991695093e3SVarun Sethi 	iommu_group_put(group);
992695093e3SVarun Sethi 	return ret;
993695093e3SVarun Sethi }
994695093e3SVarun Sethi 
995695093e3SVarun Sethi static void fsl_pamu_remove_device(struct device *dev)
996695093e3SVarun Sethi {
997695093e3SVarun Sethi 	iommu_group_remove_device(dev);
998695093e3SVarun Sethi }
999695093e3SVarun Sethi 
1000695093e3SVarun Sethi static int fsl_pamu_set_windows(struct iommu_domain *domain, u32 w_count)
1001695093e3SVarun Sethi {
1002695093e3SVarun Sethi 	struct fsl_dma_domain *dma_domain = domain->priv;
1003695093e3SVarun Sethi 	unsigned long flags;
1004695093e3SVarun Sethi 	int ret;
1005695093e3SVarun Sethi 
1006695093e3SVarun Sethi 	spin_lock_irqsave(&dma_domain->domain_lock, flags);
1007695093e3SVarun Sethi 	/* Ensure domain is inactive i.e. DMA should be disabled for the domain */
1008695093e3SVarun Sethi 	if (dma_domain->enabled) {
1009695093e3SVarun Sethi 		pr_debug("Can't set geometry attributes as domain is active\n");
1010695093e3SVarun Sethi 		spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
1011695093e3SVarun Sethi 		return  -EBUSY;
1012695093e3SVarun Sethi 	}
1013695093e3SVarun Sethi 
1014695093e3SVarun Sethi 	/* Ensure that the geometry has been set for the domain */
1015695093e3SVarun Sethi 	if (!dma_domain->geom_size) {
1016695093e3SVarun Sethi 		pr_debug("Please configure geometry before setting the number of windows\n");
1017695093e3SVarun Sethi 		spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
1018695093e3SVarun Sethi 		return -EINVAL;
1019695093e3SVarun Sethi 	}
1020695093e3SVarun Sethi 
1021695093e3SVarun Sethi 	/*
1022695093e3SVarun Sethi 	 * Ensure we have valid window count i.e. it should be less than
1023695093e3SVarun Sethi 	 * maximum permissible limit and should be a power of two.
1024695093e3SVarun Sethi 	 */
1025695093e3SVarun Sethi 	if (w_count > pamu_get_max_subwin_cnt() || !is_power_of_2(w_count)) {
1026695093e3SVarun Sethi 		pr_debug("Invalid window count\n");
1027695093e3SVarun Sethi 		spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
1028695093e3SVarun Sethi 		return -EINVAL;
1029695093e3SVarun Sethi 	}
1030695093e3SVarun Sethi 
1031695093e3SVarun Sethi 	ret = pamu_set_domain_geometry(dma_domain, &domain->geometry,
1032cd70d465SEmil Medve 				       w_count > 1 ? w_count : 0);
1033695093e3SVarun Sethi 	if (!ret) {
1034695093e3SVarun Sethi 		kfree(dma_domain->win_arr);
1035cd70d465SEmil Medve 		dma_domain->win_arr = kcalloc(w_count,
1036cd70d465SEmil Medve 					      sizeof(*dma_domain->win_arr),
1037cd70d465SEmil Medve 					      GFP_ATOMIC);
1038695093e3SVarun Sethi 		if (!dma_domain->win_arr) {
1039695093e3SVarun Sethi 			spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
1040695093e3SVarun Sethi 			return -ENOMEM;
1041695093e3SVarun Sethi 		}
1042695093e3SVarun Sethi 		dma_domain->win_cnt = w_count;
1043695093e3SVarun Sethi 	}
1044695093e3SVarun Sethi 	spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
1045695093e3SVarun Sethi 
1046695093e3SVarun Sethi 	return ret;
1047695093e3SVarun Sethi }
1048695093e3SVarun Sethi 
1049695093e3SVarun Sethi static u32 fsl_pamu_get_windows(struct iommu_domain *domain)
1050695093e3SVarun Sethi {
1051695093e3SVarun Sethi 	struct fsl_dma_domain *dma_domain = domain->priv;
1052695093e3SVarun Sethi 
1053695093e3SVarun Sethi 	return dma_domain->win_cnt;
1054695093e3SVarun Sethi }
1055695093e3SVarun Sethi 
1056b22f6434SThierry Reding static const struct iommu_ops fsl_pamu_ops = {
1057b7eb6785SJoerg Roedel 	.capable	= fsl_pamu_capable,
1058695093e3SVarun Sethi 	.domain_init	= fsl_pamu_domain_init,
1059695093e3SVarun Sethi 	.domain_destroy = fsl_pamu_domain_destroy,
1060695093e3SVarun Sethi 	.attach_dev	= fsl_pamu_attach_device,
1061695093e3SVarun Sethi 	.detach_dev	= fsl_pamu_detach_device,
1062695093e3SVarun Sethi 	.domain_window_enable = fsl_pamu_window_enable,
1063695093e3SVarun Sethi 	.domain_window_disable = fsl_pamu_window_disable,
1064695093e3SVarun Sethi 	.domain_get_windows = fsl_pamu_get_windows,
1065695093e3SVarun Sethi 	.domain_set_windows = fsl_pamu_set_windows,
1066695093e3SVarun Sethi 	.iova_to_phys	= fsl_pamu_iova_to_phys,
1067695093e3SVarun Sethi 	.domain_set_attr = fsl_pamu_set_domain_attr,
1068695093e3SVarun Sethi 	.domain_get_attr = fsl_pamu_get_domain_attr,
1069695093e3SVarun Sethi 	.add_device	= fsl_pamu_add_device,
1070695093e3SVarun Sethi 	.remove_device	= fsl_pamu_remove_device,
1071695093e3SVarun Sethi };
1072695093e3SVarun Sethi 
1073cd70d465SEmil Medve int __init pamu_domain_init(void)
1074695093e3SVarun Sethi {
1075695093e3SVarun Sethi 	int ret = 0;
1076695093e3SVarun Sethi 
1077695093e3SVarun Sethi 	ret = iommu_init_mempool();
1078695093e3SVarun Sethi 	if (ret)
1079695093e3SVarun Sethi 		return ret;
1080695093e3SVarun Sethi 
1081695093e3SVarun Sethi 	bus_set_iommu(&platform_bus_type, &fsl_pamu_ops);
1082695093e3SVarun Sethi 	bus_set_iommu(&pci_bus_type, &fsl_pamu_ops);
1083695093e3SVarun Sethi 
1084695093e3SVarun Sethi 	return ret;
1085695093e3SVarun Sethi }
1086