1d94d71cbSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2695093e3SVarun Sethi /*
3695093e3SVarun Sethi  *
4695093e3SVarun Sethi  * Copyright (C) 2013 Freescale Semiconductor, Inc.
5695093e3SVarun Sethi  * Author: Varun Sethi <varun.sethi@freescale.com>
6695093e3SVarun Sethi  */
7695093e3SVarun Sethi 
8695093e3SVarun Sethi #define pr_fmt(fmt)    "fsl-pamu-domain: %s: " fmt, __func__
9695093e3SVarun Sethi 
10695093e3SVarun Sethi #include "fsl_pamu_domain.h"
11695093e3SVarun Sethi 
12cae8d1f5SChristophe Leroy #include <linux/platform_device.h>
13cd70d465SEmil Medve #include <sysdev/fsl_pci.h>
14cd70d465SEmil Medve 
15695093e3SVarun Sethi /*
16695093e3SVarun Sethi  * Global spinlock that needs to be held while
17695093e3SVarun Sethi  * configuring PAMU.
18695093e3SVarun Sethi  */
19695093e3SVarun Sethi static DEFINE_SPINLOCK(iommu_lock);
20695093e3SVarun Sethi 
21695093e3SVarun Sethi static struct kmem_cache *fsl_pamu_domain_cache;
22695093e3SVarun Sethi static struct kmem_cache *iommu_devinfo_cache;
23695093e3SVarun Sethi static DEFINE_SPINLOCK(device_domain_lock);
24695093e3SVarun Sethi 
253ff2dcc0SJoerg Roedel struct iommu_device pamu_iommu;	/* IOMMU core code handle */
263ff2dcc0SJoerg Roedel 
to_fsl_dma_domain(struct iommu_domain * dom)278d4bfe40SJoerg Roedel static struct fsl_dma_domain *to_fsl_dma_domain(struct iommu_domain *dom)
288d4bfe40SJoerg Roedel {
298d4bfe40SJoerg Roedel 	return container_of(dom, struct fsl_dma_domain, iommu_domain);
308d4bfe40SJoerg Roedel }
318d4bfe40SJoerg Roedel 
iommu_init_mempool(void)32695093e3SVarun Sethi static int __init iommu_init_mempool(void)
33695093e3SVarun Sethi {
34695093e3SVarun Sethi 	fsl_pamu_domain_cache = kmem_cache_create("fsl_pamu_domain",
35695093e3SVarun Sethi 						  sizeof(struct fsl_dma_domain),
36695093e3SVarun Sethi 						  0,
37695093e3SVarun Sethi 						  SLAB_HWCACHE_ALIGN,
38695093e3SVarun Sethi 						  NULL);
39695093e3SVarun Sethi 	if (!fsl_pamu_domain_cache) {
40695093e3SVarun Sethi 		pr_debug("Couldn't create fsl iommu_domain cache\n");
41695093e3SVarun Sethi 		return -ENOMEM;
42695093e3SVarun Sethi 	}
43695093e3SVarun Sethi 
44695093e3SVarun Sethi 	iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
45695093e3SVarun Sethi 						sizeof(struct device_domain_info),
46695093e3SVarun Sethi 						0,
47695093e3SVarun Sethi 						SLAB_HWCACHE_ALIGN,
48695093e3SVarun Sethi 						NULL);
49695093e3SVarun Sethi 	if (!iommu_devinfo_cache) {
50695093e3SVarun Sethi 		pr_debug("Couldn't create devinfo cache\n");
51695093e3SVarun Sethi 		kmem_cache_destroy(fsl_pamu_domain_cache);
52695093e3SVarun Sethi 		return -ENOMEM;
53695093e3SVarun Sethi 	}
54695093e3SVarun Sethi 
55695093e3SVarun Sethi 	return 0;
56695093e3SVarun Sethi }
57695093e3SVarun Sethi 
update_liodn_stash(int liodn,struct fsl_dma_domain * dma_domain,u32 val)58695093e3SVarun Sethi static int update_liodn_stash(int liodn, struct fsl_dma_domain *dma_domain,
59695093e3SVarun Sethi 			      u32 val)
60695093e3SVarun Sethi {
6184b6269cSJoerg Roedel 	int ret = 0;
62695093e3SVarun Sethi 	unsigned long flags;
63695093e3SVarun Sethi 
64695093e3SVarun Sethi 	spin_lock_irqsave(&iommu_lock, flags);
65ba58d121SChristoph Hellwig 	ret = pamu_update_paace_stash(liodn, val);
66695093e3SVarun Sethi 	if (ret) {
6784b6269cSJoerg Roedel 		pr_debug("Failed to update SPAACE for liodn %d\n ", liodn);
68695093e3SVarun Sethi 		spin_unlock_irqrestore(&iommu_lock, flags);
69695093e3SVarun Sethi 		return ret;
70695093e3SVarun Sethi 	}
71695093e3SVarun Sethi 
72695093e3SVarun Sethi 	spin_unlock_irqrestore(&iommu_lock, flags);
73695093e3SVarun Sethi 
74695093e3SVarun Sethi 	return ret;
75695093e3SVarun Sethi }
76695093e3SVarun Sethi 
77695093e3SVarun Sethi /* Set the geometry parameters for a LIODN */
pamu_set_liodn(struct fsl_dma_domain * dma_domain,struct device * dev,int liodn)78dae7747aSChristoph Hellwig static int pamu_set_liodn(struct fsl_dma_domain *dma_domain, struct device *dev,
79dae7747aSChristoph Hellwig 			  int liodn)
80695093e3SVarun Sethi {
81695093e3SVarun Sethi 	u32 omi_index = ~(u32)0;
82695093e3SVarun Sethi 	unsigned long flags;
83ba58d121SChristoph Hellwig 	int ret;
84695093e3SVarun Sethi 
85695093e3SVarun Sethi 	/*
86695093e3SVarun Sethi 	 * Configure the omi_index at the geometry setup time.
87695093e3SVarun Sethi 	 * This is a static value which depends on the type of
88695093e3SVarun Sethi 	 * device and would not change thereafter.
89695093e3SVarun Sethi 	 */
90695093e3SVarun Sethi 	get_ome_index(&omi_index, dev);
91695093e3SVarun Sethi 
92695093e3SVarun Sethi 	spin_lock_irqsave(&iommu_lock, flags);
93695093e3SVarun Sethi 	ret = pamu_disable_liodn(liodn);
94dae7747aSChristoph Hellwig 	if (ret)
95dae7747aSChristoph Hellwig 		goto out_unlock;
9657fa44beSChristoph Hellwig 	ret = pamu_config_ppaace(liodn, omi_index, dma_domain->stash_id, 0);
97dae7747aSChristoph Hellwig 	if (ret)
98dae7747aSChristoph Hellwig 		goto out_unlock;
9957fa44beSChristoph Hellwig 	ret = pamu_config_ppaace(liodn, ~(u32)0, dma_domain->stash_id,
100dae7747aSChristoph Hellwig 				 PAACE_AP_PERMS_QUERY | PAACE_AP_PERMS_UPDATE);
101dae7747aSChristoph Hellwig out_unlock:
102695093e3SVarun Sethi 	spin_unlock_irqrestore(&iommu_lock, flags);
103695093e3SVarun Sethi 	if (ret) {
104ba58d121SChristoph Hellwig 		pr_debug("PAACE configuration failed for liodn %d\n",
105cd70d465SEmil Medve 			 liodn);
106695093e3SVarun Sethi 	}
107695093e3SVarun Sethi 	return ret;
108695093e3SVarun Sethi }
109695093e3SVarun Sethi 
remove_device_ref(struct device_domain_info * info)110ba58d121SChristoph Hellwig static void remove_device_ref(struct device_domain_info *info)
111695093e3SVarun Sethi {
112695093e3SVarun Sethi 	unsigned long flags;
113695093e3SVarun Sethi 
114695093e3SVarun Sethi 	list_del(&info->link);
115695093e3SVarun Sethi 	spin_lock_irqsave(&iommu_lock, flags);
116695093e3SVarun Sethi 	pamu_disable_liodn(info->liodn);
117695093e3SVarun Sethi 	spin_unlock_irqrestore(&iommu_lock, flags);
118695093e3SVarun Sethi 	spin_lock_irqsave(&device_domain_lock, flags);
1192263d818SJoerg Roedel 	dev_iommu_priv_set(info->dev, NULL);
120695093e3SVarun Sethi 	kmem_cache_free(iommu_devinfo_cache, info);
121695093e3SVarun Sethi 	spin_unlock_irqrestore(&device_domain_lock, flags);
122695093e3SVarun Sethi }
123695093e3SVarun Sethi 
detach_device(struct device * dev,struct fsl_dma_domain * dma_domain)124695093e3SVarun Sethi static void detach_device(struct device *dev, struct fsl_dma_domain *dma_domain)
125695093e3SVarun Sethi {
126695093e3SVarun Sethi 	struct device_domain_info *info, *tmp;
127695093e3SVarun Sethi 	unsigned long flags;
128695093e3SVarun Sethi 
129695093e3SVarun Sethi 	spin_lock_irqsave(&dma_domain->domain_lock, flags);
130695093e3SVarun Sethi 	/* Remove the device from the domain device list */
131695093e3SVarun Sethi 	list_for_each_entry_safe(info, tmp, &dma_domain->devices, link) {
132695093e3SVarun Sethi 		if (!dev || (info->dev == dev))
133ba58d121SChristoph Hellwig 			remove_device_ref(info);
134695093e3SVarun Sethi 	}
135695093e3SVarun Sethi 	spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
136695093e3SVarun Sethi }
137695093e3SVarun Sethi 
attach_device(struct fsl_dma_domain * dma_domain,int liodn,struct device * dev)138695093e3SVarun Sethi static void attach_device(struct fsl_dma_domain *dma_domain, int liodn, struct device *dev)
139695093e3SVarun Sethi {
140695093e3SVarun Sethi 	struct device_domain_info *info, *old_domain_info;
141695093e3SVarun Sethi 	unsigned long flags;
142695093e3SVarun Sethi 
143695093e3SVarun Sethi 	spin_lock_irqsave(&device_domain_lock, flags);
144695093e3SVarun Sethi 	/*
145695093e3SVarun Sethi 	 * Check here if the device is already attached to domain or not.
146695093e3SVarun Sethi 	 * If the device is already attached to a domain detach it.
147695093e3SVarun Sethi 	 */
1482263d818SJoerg Roedel 	old_domain_info = dev_iommu_priv_get(dev);
149695093e3SVarun Sethi 	if (old_domain_info && old_domain_info->domain != dma_domain) {
150695093e3SVarun Sethi 		spin_unlock_irqrestore(&device_domain_lock, flags);
151695093e3SVarun Sethi 		detach_device(dev, old_domain_info->domain);
152695093e3SVarun Sethi 		spin_lock_irqsave(&device_domain_lock, flags);
153695093e3SVarun Sethi 	}
154695093e3SVarun Sethi 
155695093e3SVarun Sethi 	info = kmem_cache_zalloc(iommu_devinfo_cache, GFP_ATOMIC);
156695093e3SVarun Sethi 
157695093e3SVarun Sethi 	info->dev = dev;
158695093e3SVarun Sethi 	info->liodn = liodn;
159695093e3SVarun Sethi 	info->domain = dma_domain;
160695093e3SVarun Sethi 
161695093e3SVarun Sethi 	list_add(&info->link, &dma_domain->devices);
162695093e3SVarun Sethi 	/*
163695093e3SVarun Sethi 	 * In case of devices with multiple LIODNs just store
164695093e3SVarun Sethi 	 * the info for the first LIODN as all
165695093e3SVarun Sethi 	 * LIODNs share the same domain
166695093e3SVarun Sethi 	 */
1672263d818SJoerg Roedel 	if (!dev_iommu_priv_get(dev))
1682263d818SJoerg Roedel 		dev_iommu_priv_set(dev, info);
169695093e3SVarun Sethi 	spin_unlock_irqrestore(&device_domain_lock, flags);
170695093e3SVarun Sethi }
171695093e3SVarun Sethi 
fsl_pamu_iova_to_phys(struct iommu_domain * domain,dma_addr_t iova)172695093e3SVarun Sethi static phys_addr_t fsl_pamu_iova_to_phys(struct iommu_domain *domain,
173695093e3SVarun Sethi 					 dma_addr_t iova)
174695093e3SVarun Sethi {
175cd70d465SEmil Medve 	if (iova < domain->geometry.aperture_start ||
176cd70d465SEmil Medve 	    iova > domain->geometry.aperture_end)
177695093e3SVarun Sethi 		return 0;
178376dfd2aSChristoph Hellwig 	return iova;
179695093e3SVarun Sethi }
180695093e3SVarun Sethi 
fsl_pamu_capable(struct device * dev,enum iommu_cap cap)181359ad157SRobin Murphy static bool fsl_pamu_capable(struct device *dev, enum iommu_cap cap)
182695093e3SVarun Sethi {
183695093e3SVarun Sethi 	return cap == IOMMU_CAP_CACHE_COHERENCY;
184695093e3SVarun Sethi }
185695093e3SVarun Sethi 
fsl_pamu_domain_free(struct iommu_domain * domain)1868d4bfe40SJoerg Roedel static void fsl_pamu_domain_free(struct iommu_domain *domain)
187695093e3SVarun Sethi {
1888d4bfe40SJoerg Roedel 	struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain);
189695093e3SVarun Sethi 
190695093e3SVarun Sethi 	/* remove all the devices from the device list */
191695093e3SVarun Sethi 	detach_device(NULL, dma_domain);
192695093e3SVarun Sethi 	kmem_cache_free(fsl_pamu_domain_cache, dma_domain);
193695093e3SVarun Sethi }
194695093e3SVarun Sethi 
fsl_pamu_domain_alloc(unsigned type)1958d4bfe40SJoerg Roedel static struct iommu_domain *fsl_pamu_domain_alloc(unsigned type)
196695093e3SVarun Sethi {
197695093e3SVarun Sethi 	struct fsl_dma_domain *dma_domain;
198695093e3SVarun Sethi 
1998d4bfe40SJoerg Roedel 	if (type != IOMMU_DOMAIN_UNMANAGED)
2008d4bfe40SJoerg Roedel 		return NULL;
2018d4bfe40SJoerg Roedel 
202c8224508SChristoph Hellwig 	dma_domain = kmem_cache_zalloc(fsl_pamu_domain_cache, GFP_KERNEL);
203c8224508SChristoph Hellwig 	if (!dma_domain)
2048d4bfe40SJoerg Roedel 		return NULL;
205c8224508SChristoph Hellwig 
206c8224508SChristoph Hellwig 	dma_domain->stash_id = ~(u32)0;
207c8224508SChristoph Hellwig 	INIT_LIST_HEAD(&dma_domain->devices);
208c8224508SChristoph Hellwig 	spin_lock_init(&dma_domain->domain_lock);
209c8224508SChristoph Hellwig 
210c8224508SChristoph Hellwig 	/* default geometry 64 GB i.e. maximum system address */
2118d4bfe40SJoerg Roedel 	dma_domain->iommu_domain. geometry.aperture_start = 0;
2128d4bfe40SJoerg Roedel 	dma_domain->iommu_domain.geometry.aperture_end = (1ULL << 36) - 1;
2138d4bfe40SJoerg Roedel 	dma_domain->iommu_domain.geometry.force_aperture = true;
214695093e3SVarun Sethi 
2158d4bfe40SJoerg Roedel 	return &dma_domain->iommu_domain;
216695093e3SVarun Sethi }
217695093e3SVarun Sethi 
218695093e3SVarun Sethi /* Update stash destination for all LIODNs associated with the domain */
update_domain_stash(struct fsl_dma_domain * dma_domain,u32 val)219695093e3SVarun Sethi static int update_domain_stash(struct fsl_dma_domain *dma_domain, u32 val)
220695093e3SVarun Sethi {
221695093e3SVarun Sethi 	struct device_domain_info *info;
222695093e3SVarun Sethi 	int ret = 0;
223695093e3SVarun Sethi 
224695093e3SVarun Sethi 	list_for_each_entry(info, &dma_domain->devices, link) {
225695093e3SVarun Sethi 		ret = update_liodn_stash(info->liodn, dma_domain, val);
226695093e3SVarun Sethi 		if (ret)
227695093e3SVarun Sethi 			break;
228695093e3SVarun Sethi 	}
229695093e3SVarun Sethi 
230695093e3SVarun Sethi 	return ret;
231695093e3SVarun Sethi }
232695093e3SVarun Sethi 
fsl_pamu_attach_device(struct iommu_domain * domain,struct device * dev)233695093e3SVarun Sethi static int fsl_pamu_attach_device(struct iommu_domain *domain,
234695093e3SVarun Sethi 				  struct device *dev)
235695093e3SVarun Sethi {
2368d4bfe40SJoerg Roedel 	struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain);
23785e362caSChristoph Hellwig 	unsigned long flags;
23885e362caSChristoph Hellwig 	int len, ret = 0, i;
239695093e3SVarun Sethi 	const u32 *liodn;
240695093e3SVarun Sethi 	struct pci_dev *pdev = NULL;
241695093e3SVarun Sethi 	struct pci_controller *pci_ctl;
242695093e3SVarun Sethi 
243695093e3SVarun Sethi 	/*
244695093e3SVarun Sethi 	 * Use LIODN of the PCI controller while attaching a
245695093e3SVarun Sethi 	 * PCI device.
246695093e3SVarun Sethi 	 */
247b3eb76d1SYijing Wang 	if (dev_is_pci(dev)) {
248695093e3SVarun Sethi 		pdev = to_pci_dev(dev);
249695093e3SVarun Sethi 		pci_ctl = pci_bus_to_host(pdev->bus);
250695093e3SVarun Sethi 		/*
251695093e3SVarun Sethi 		 * make dev point to pci controller device
252695093e3SVarun Sethi 		 * so we can get the LIODN programmed by
253695093e3SVarun Sethi 		 * u-boot.
254695093e3SVarun Sethi 		 */
255695093e3SVarun Sethi 		dev = pci_ctl->parent;
256695093e3SVarun Sethi 	}
257695093e3SVarun Sethi 
258695093e3SVarun Sethi 	liodn = of_get_property(dev->of_node, "fsl,liodn", &len);
25985e362caSChristoph Hellwig 	if (!liodn) {
2606bd4f1c7SRob Herring 		pr_debug("missing fsl,liodn property at %pOF\n", dev->of_node);
261bd7ebb77SNicolin Chen 		return -ENODEV;
262695093e3SVarun Sethi 	}
263695093e3SVarun Sethi 
26485e362caSChristoph Hellwig 	spin_lock_irqsave(&dma_domain->domain_lock, flags);
26585e362caSChristoph Hellwig 	for (i = 0; i < len / sizeof(u32); i++) {
26685e362caSChristoph Hellwig 		/* Ensure that LIODN value is valid */
26785e362caSChristoph Hellwig 		if (liodn[i] >= PAACE_NUMBER_ENTRIES) {
26885e362caSChristoph Hellwig 			pr_debug("Invalid liodn %d, attach device failed for %pOF\n",
26985e362caSChristoph Hellwig 				 liodn[i], dev->of_node);
270bd7ebb77SNicolin Chen 			ret = -ENODEV;
27185e362caSChristoph Hellwig 			break;
27285e362caSChristoph Hellwig 		}
27385e362caSChristoph Hellwig 
27485e362caSChristoph Hellwig 		attach_device(dma_domain, liodn[i], dev);
27585e362caSChristoph Hellwig 		ret = pamu_set_liodn(dma_domain, dev, liodn[i]);
27685e362caSChristoph Hellwig 		if (ret)
27785e362caSChristoph Hellwig 			break;
2787d61cb6fSChristoph Hellwig 		ret = pamu_enable_liodn(liodn[i]);
2797d61cb6fSChristoph Hellwig 		if (ret)
2807d61cb6fSChristoph Hellwig 			break;
28185e362caSChristoph Hellwig 	}
28285e362caSChristoph Hellwig 	spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
283695093e3SVarun Sethi 	return ret;
284695093e3SVarun Sethi }
285695093e3SVarun Sethi 
fsl_pamu_set_platform_dma(struct device * dev)286c1fe9119SLu Baolu static void fsl_pamu_set_platform_dma(struct device *dev)
287695093e3SVarun Sethi {
288c1fe9119SLu Baolu 	struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
2898d4bfe40SJoerg Roedel 	struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain);
290695093e3SVarun Sethi 	const u32 *prop;
291695093e3SVarun Sethi 	int len;
292695093e3SVarun Sethi 	struct pci_dev *pdev = NULL;
293695093e3SVarun Sethi 	struct pci_controller *pci_ctl;
294695093e3SVarun Sethi 
295695093e3SVarun Sethi 	/*
296695093e3SVarun Sethi 	 * Use LIODN of the PCI controller while detaching a
297695093e3SVarun Sethi 	 * PCI device.
298695093e3SVarun Sethi 	 */
299b3eb76d1SYijing Wang 	if (dev_is_pci(dev)) {
300695093e3SVarun Sethi 		pdev = to_pci_dev(dev);
301695093e3SVarun Sethi 		pci_ctl = pci_bus_to_host(pdev->bus);
302695093e3SVarun Sethi 		/*
303695093e3SVarun Sethi 		 * make dev point to pci controller device
304695093e3SVarun Sethi 		 * so we can get the LIODN programmed by
305695093e3SVarun Sethi 		 * u-boot.
306695093e3SVarun Sethi 		 */
307695093e3SVarun Sethi 		dev = pci_ctl->parent;
308695093e3SVarun Sethi 	}
309695093e3SVarun Sethi 
310695093e3SVarun Sethi 	prop = of_get_property(dev->of_node, "fsl,liodn", &len);
311695093e3SVarun Sethi 	if (prop)
312695093e3SVarun Sethi 		detach_device(dev, dma_domain);
313695093e3SVarun Sethi 	else
3146bd4f1c7SRob Herring 		pr_debug("missing fsl,liodn property at %pOF\n", dev->of_node);
315695093e3SVarun Sethi }
316695093e3SVarun Sethi 
317695093e3SVarun Sethi /* Set the domain stash attribute */
fsl_pamu_configure_l1_stash(struct iommu_domain * domain,u32 cpu)3184eeb96f6SChristoph Hellwig int fsl_pamu_configure_l1_stash(struct iommu_domain *domain, u32 cpu)
319695093e3SVarun Sethi {
3204eeb96f6SChristoph Hellwig 	struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain);
321695093e3SVarun Sethi 	unsigned long flags;
322695093e3SVarun Sethi 	int ret;
323695093e3SVarun Sethi 
324695093e3SVarun Sethi 	spin_lock_irqsave(&dma_domain->domain_lock, flags);
3254eeb96f6SChristoph Hellwig 	dma_domain->stash_id = get_stash_id(PAMU_ATTR_CACHE_L1, cpu);
326695093e3SVarun Sethi 	if (dma_domain->stash_id == ~(u32)0) {
327695093e3SVarun Sethi 		pr_debug("Invalid stash attributes\n");
328695093e3SVarun Sethi 		spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
329695093e3SVarun Sethi 		return -EINVAL;
330695093e3SVarun Sethi 	}
331695093e3SVarun Sethi 	ret = update_domain_stash(dma_domain, dma_domain->stash_id);
332695093e3SVarun Sethi 	spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
333695093e3SVarun Sethi 
334695093e3SVarun Sethi 	return ret;
335695093e3SVarun Sethi }
336695093e3SVarun Sethi 
check_pci_ctl_endpt_part(struct pci_controller * pci_ctl)337695093e3SVarun Sethi static  bool check_pci_ctl_endpt_part(struct pci_controller *pci_ctl)
338695093e3SVarun Sethi {
339695093e3SVarun Sethi 	u32 version;
340695093e3SVarun Sethi 
341695093e3SVarun Sethi 	/* Check the PCI controller version number by readding BRR1 register */
342695093e3SVarun Sethi 	version = in_be32(pci_ctl->cfg_addr + (PCI_FSL_BRR1 >> 2));
343695093e3SVarun Sethi 	version &= PCI_FSL_BRR1_VER;
344695093e3SVarun Sethi 	/* If PCI controller version is >= 0x204 we can partition endpoints */
345cd70d465SEmil Medve 	return version >= 0x204;
346695093e3SVarun Sethi }
347695093e3SVarun Sethi 
fsl_pamu_device_group(struct device * dev)348d5e58297SJoerg Roedel static struct iommu_group *fsl_pamu_device_group(struct device *dev)
349695093e3SVarun Sethi {
350*139a57a9SJason Gunthorpe 	struct iommu_group *group;
351*139a57a9SJason Gunthorpe 	struct pci_dev *pdev;
352*139a57a9SJason Gunthorpe 
353695093e3SVarun Sethi 	/*
354*139a57a9SJason Gunthorpe 	 * For platform devices we allocate a separate group for each of the
355*139a57a9SJason Gunthorpe 	 * devices.
356695093e3SVarun Sethi 	 */
3577977a08eSJason Gunthorpe 	if (!dev_is_pci(dev))
3585f648972SJason Gunthorpe 		return generic_device_group(dev);
359d5e58297SJoerg Roedel 
360*139a57a9SJason Gunthorpe 	/*
361*139a57a9SJason Gunthorpe 	 * We can partition PCIe devices so assign device group to the device
362*139a57a9SJason Gunthorpe 	 */
363*139a57a9SJason Gunthorpe 	pdev = to_pci_dev(dev);
364*139a57a9SJason Gunthorpe 	if (check_pci_ctl_endpt_part(pci_bus_to_host(pdev->bus)))
365*139a57a9SJason Gunthorpe 		return pci_device_group(&pdev->dev);
366*139a57a9SJason Gunthorpe 
367*139a57a9SJason Gunthorpe 	/*
368*139a57a9SJason Gunthorpe 	 * All devices connected to the controller will share the same device
369*139a57a9SJason Gunthorpe 	 * group.
370*139a57a9SJason Gunthorpe 	 *
371*139a57a9SJason Gunthorpe 	 * Due to ordering between fsl_pamu_init() and fsl_pci_init() it is
372*139a57a9SJason Gunthorpe 	 * guaranteed that the pci_ctl->parent platform_device will have the
373*139a57a9SJason Gunthorpe 	 * iommu driver bound and will already have a group set. So we just
374*139a57a9SJason Gunthorpe 	 * re-use this group as the group for every device in the hose.
375*139a57a9SJason Gunthorpe 	 */
376*139a57a9SJason Gunthorpe 	group = iommu_group_get(pci_bus_to_host(pdev->bus)->parent);
377*139a57a9SJason Gunthorpe 	if (WARN_ON(!group))
378*139a57a9SJason Gunthorpe 		return ERR_PTR(-EINVAL);
379*139a57a9SJason Gunthorpe 	return group;
380695093e3SVarun Sethi }
381695093e3SVarun Sethi 
fsl_pamu_probe_device(struct device * dev)38252dd3ca4SJoerg Roedel static struct iommu_device *fsl_pamu_probe_device(struct device *dev)
383d5e58297SJoerg Roedel {
3847977a08eSJason Gunthorpe 	int len;
3857977a08eSJason Gunthorpe 
3867977a08eSJason Gunthorpe 	/*
3877977a08eSJason Gunthorpe 	 * uboot must fill the fsl,liodn for platform devices to be supported by
3887977a08eSJason Gunthorpe 	 * the iommu.
3897977a08eSJason Gunthorpe 	 */
3907977a08eSJason Gunthorpe 	if (!dev_is_pci(dev) &&
3917977a08eSJason Gunthorpe 	    !of_get_property(dev->of_node, "fsl,liodn", &len))
3927977a08eSJason Gunthorpe 		return ERR_PTR(-ENODEV);
3937977a08eSJason Gunthorpe 
39452dd3ca4SJoerg Roedel 	return &pamu_iommu;
395695093e3SVarun Sethi }
396695093e3SVarun Sethi 
397b22f6434SThierry Reding static const struct iommu_ops fsl_pamu_ops = {
398b7eb6785SJoerg Roedel 	.capable	= fsl_pamu_capable,
3998d4bfe40SJoerg Roedel 	.domain_alloc	= fsl_pamu_domain_alloc,
40052dd3ca4SJoerg Roedel 	.probe_device	= fsl_pamu_probe_device,
401d5e58297SJoerg Roedel 	.device_group   = fsl_pamu_device_group,
402bb649412SJoerg Roedel 	.set_platform_dma_ops = fsl_pamu_set_platform_dma,
4039a630a4bSLu Baolu 	.default_domain_ops = &(const struct iommu_domain_ops) {
4049a630a4bSLu Baolu 		.attach_dev	= fsl_pamu_attach_device,
4059a630a4bSLu Baolu 		.iova_to_phys	= fsl_pamu_iova_to_phys,
4069a630a4bSLu Baolu 		.free		= fsl_pamu_domain_free,
4079a630a4bSLu Baolu 	}
408695093e3SVarun Sethi };
409695093e3SVarun Sethi 
pamu_domain_init(void)410cd70d465SEmil Medve int __init pamu_domain_init(void)
411695093e3SVarun Sethi {
412695093e3SVarun Sethi 	int ret = 0;
413695093e3SVarun Sethi 
414695093e3SVarun Sethi 	ret = iommu_init_mempool();
415695093e3SVarun Sethi 	if (ret)
416695093e3SVarun Sethi 		return ret;
417695093e3SVarun Sethi 
4183ff2dcc0SJoerg Roedel 	ret = iommu_device_sysfs_add(&pamu_iommu, NULL, NULL, "iommu0");
4193ff2dcc0SJoerg Roedel 	if (ret)
4203ff2dcc0SJoerg Roedel 		return ret;
4213ff2dcc0SJoerg Roedel 
4222d471b20SRobin Murphy 	ret = iommu_device_register(&pamu_iommu, &fsl_pamu_ops, NULL);
4233ff2dcc0SJoerg Roedel 	if (ret) {
4243ff2dcc0SJoerg Roedel 		iommu_device_sysfs_remove(&pamu_iommu);
4253ff2dcc0SJoerg Roedel 		pr_err("Can't register iommu device\n");
4263ff2dcc0SJoerg Roedel 	}
4273ff2dcc0SJoerg Roedel 
428695093e3SVarun Sethi 	return ret;
429695093e3SVarun Sethi }
430