2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2, as
4 * published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15 * Copyright (C) 2013 Freescale Semiconductor, Inc.
16 * Author: Varun Sethi <varun.sethi@freescale.com>
20 #define pr_fmt(fmt) "fsl-pamu-domain: %s: " fmt, __func__
22 #include "fsl_pamu_domain.h"
24 #include <sysdev/fsl_pci.h>
27 * Global spinlock that needs to be held while
30 static DEFINE_SPINLOCK(iommu_lock);
32 static struct kmem_cache *fsl_pamu_domain_cache;
33 static struct kmem_cache *iommu_devinfo_cache;
34 static DEFINE_SPINLOCK(device_domain_lock);
36 static struct fsl_dma_domain *to_fsl_dma_domain(struct iommu_domain *dom)
38 return container_of(dom, struct fsl_dma_domain, iommu_domain);
41 static int __init iommu_init_mempool(void)
43 fsl_pamu_domain_cache = kmem_cache_create("fsl_pamu_domain",
44 sizeof(struct fsl_dma_domain),
48 if (!fsl_pamu_domain_cache) {
49 pr_debug("Couldn't create fsl iommu_domain cache\n");
53 iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
54 sizeof(struct device_domain_info),
58 if (!iommu_devinfo_cache) {
59 pr_debug("Couldn't create devinfo cache\n");
60 kmem_cache_destroy(fsl_pamu_domain_cache);
67 static phys_addr_t get_phys_addr(struct fsl_dma_domain *dma_domain, dma_addr_t iova)
69 u32 win_cnt = dma_domain->win_cnt;
70 struct dma_window *win_ptr = &dma_domain->win_arr[0];
71 struct iommu_domain_geometry *geom;
73 geom = &dma_domain->iommu_domain.geometry;
75 if (!win_cnt || !dma_domain->geom_size) {
76 pr_debug("Number of windows/geometry not configured for the domain\n");
82 dma_addr_t subwin_iova;
85 subwin_size = dma_domain->geom_size >> ilog2(win_cnt);
86 subwin_iova = iova & ~(subwin_size - 1);
87 wnd = (subwin_iova - geom->aperture_start) >> ilog2(subwin_size);
88 win_ptr = &dma_domain->win_arr[wnd];
92 return win_ptr->paddr + (iova & (win_ptr->size - 1));
97 static int map_subwins(int liodn, struct fsl_dma_domain *dma_domain)
99 struct dma_window *sub_win_ptr = &dma_domain->win_arr[0];
101 unsigned long rpn, flags;
103 for (i = 0; i < dma_domain->win_cnt; i++) {
104 if (sub_win_ptr[i].valid) {
105 rpn = sub_win_ptr[i].paddr >> PAMU_PAGE_SHIFT;
106 spin_lock_irqsave(&iommu_lock, flags);
107 ret = pamu_config_spaace(liodn, dma_domain->win_cnt, i,
111 dma_domain->snoop_id,
112 dma_domain->stash_id,
114 sub_win_ptr[i].prot);
115 spin_unlock_irqrestore(&iommu_lock, flags);
117 pr_debug("SPAACE configuration failed for liodn %d\n",
127 static int map_win(int liodn, struct fsl_dma_domain *dma_domain)
130 struct dma_window *wnd = &dma_domain->win_arr[0];
131 phys_addr_t wnd_addr = dma_domain->iommu_domain.geometry.aperture_start;
134 spin_lock_irqsave(&iommu_lock, flags);
135 ret = pamu_config_ppaace(liodn, wnd_addr,
138 wnd->paddr >> PAMU_PAGE_SHIFT,
139 dma_domain->snoop_id, dma_domain->stash_id,
141 spin_unlock_irqrestore(&iommu_lock, flags);
143 pr_debug("PAACE configuration failed for liodn %d\n", liodn);
148 /* Map the DMA window corresponding to the LIODN */
149 static int map_liodn(int liodn, struct fsl_dma_domain *dma_domain)
151 if (dma_domain->win_cnt > 1)
152 return map_subwins(liodn, dma_domain);
154 return map_win(liodn, dma_domain);
157 /* Update window/subwindow mapping for the LIODN */
158 static int update_liodn(int liodn, struct fsl_dma_domain *dma_domain, u32 wnd_nr)
161 struct dma_window *wnd = &dma_domain->win_arr[wnd_nr];
164 spin_lock_irqsave(&iommu_lock, flags);
165 if (dma_domain->win_cnt > 1) {
166 ret = pamu_config_spaace(liodn, dma_domain->win_cnt, wnd_nr,
169 wnd->paddr >> PAMU_PAGE_SHIFT,
170 dma_domain->snoop_id,
171 dma_domain->stash_id,
172 (wnd_nr > 0) ? 1 : 0,
175 pr_debug("Subwindow reconfiguration failed for liodn %d\n",
178 phys_addr_t wnd_addr;
180 wnd_addr = dma_domain->iommu_domain.geometry.aperture_start;
182 ret = pamu_config_ppaace(liodn, wnd_addr,
185 wnd->paddr >> PAMU_PAGE_SHIFT,
186 dma_domain->snoop_id, dma_domain->stash_id,
189 pr_debug("Window reconfiguration failed for liodn %d\n",
193 spin_unlock_irqrestore(&iommu_lock, flags);
198 static int update_liodn_stash(int liodn, struct fsl_dma_domain *dma_domain,
204 spin_lock_irqsave(&iommu_lock, flags);
205 if (!dma_domain->win_arr) {
206 pr_debug("Windows not configured, stash destination update failed for liodn %d\n",
208 spin_unlock_irqrestore(&iommu_lock, flags);
212 for (i = 0; i < dma_domain->win_cnt; i++) {
213 ret = pamu_update_paace_stash(liodn, i, val);
215 pr_debug("Failed to update SPAACE %d field for liodn %d\n ",
217 spin_unlock_irqrestore(&iommu_lock, flags);
222 spin_unlock_irqrestore(&iommu_lock, flags);
227 /* Set the geometry parameters for a LIODN */
228 static int pamu_set_liodn(int liodn, struct device *dev,
229 struct fsl_dma_domain *dma_domain,
230 struct iommu_domain_geometry *geom_attr,
233 phys_addr_t window_addr, window_size;
234 phys_addr_t subwin_size;
236 u32 omi_index = ~(u32)0;
240 * Configure the omi_index at the geometry setup time.
241 * This is a static value which depends on the type of
242 * device and would not change thereafter.
244 get_ome_index(&omi_index, dev);
246 window_addr = geom_attr->aperture_start;
247 window_size = dma_domain->geom_size;
249 spin_lock_irqsave(&iommu_lock, flags);
250 ret = pamu_disable_liodn(liodn);
252 ret = pamu_config_ppaace(liodn, window_addr, window_size, omi_index,
253 0, dma_domain->snoop_id,
254 dma_domain->stash_id, win_cnt, 0);
255 spin_unlock_irqrestore(&iommu_lock, flags);
257 pr_debug("PAACE configuration failed for liodn %d, win_cnt =%d\n",
263 subwin_size = window_size >> ilog2(win_cnt);
264 for (i = 0; i < win_cnt; i++) {
265 spin_lock_irqsave(&iommu_lock, flags);
266 ret = pamu_disable_spaace(liodn, i);
268 ret = pamu_config_spaace(liodn, win_cnt, i,
269 subwin_size, omi_index,
270 0, dma_domain->snoop_id,
271 dma_domain->stash_id,
273 spin_unlock_irqrestore(&iommu_lock, flags);
275 pr_debug("SPAACE configuration failed for liodn %d\n",
285 static int check_size(u64 size, dma_addr_t iova)
288 * Size must be a power of two and at least be equal
291 if ((size & (size - 1)) || size < PAMU_PAGE_SIZE) {
292 pr_debug("Size too small or not a power of two\n");
296 /* iova must be page size aligned */
297 if (iova & (size - 1)) {
298 pr_debug("Address is not aligned with window size\n");
305 static struct fsl_dma_domain *iommu_alloc_dma_domain(void)
307 struct fsl_dma_domain *domain;
309 domain = kmem_cache_zalloc(fsl_pamu_domain_cache, GFP_KERNEL);
313 domain->stash_id = ~(u32)0;
314 domain->snoop_id = ~(u32)0;
315 domain->win_cnt = pamu_get_max_subwin_cnt();
316 domain->geom_size = 0;
318 INIT_LIST_HEAD(&domain->devices);
320 spin_lock_init(&domain->domain_lock);
325 static void remove_device_ref(struct device_domain_info *info, u32 win_cnt)
329 list_del(&info->link);
330 spin_lock_irqsave(&iommu_lock, flags);
332 pamu_free_subwins(info->liodn);
333 pamu_disable_liodn(info->liodn);
334 spin_unlock_irqrestore(&iommu_lock, flags);
335 spin_lock_irqsave(&device_domain_lock, flags);
336 info->dev->archdata.iommu_domain = NULL;
337 kmem_cache_free(iommu_devinfo_cache, info);
338 spin_unlock_irqrestore(&device_domain_lock, flags);
341 static void detach_device(struct device *dev, struct fsl_dma_domain *dma_domain)
343 struct device_domain_info *info, *tmp;
346 spin_lock_irqsave(&dma_domain->domain_lock, flags);
347 /* Remove the device from the domain device list */
348 list_for_each_entry_safe(info, tmp, &dma_domain->devices, link) {
349 if (!dev || (info->dev == dev))
350 remove_device_ref(info, dma_domain->win_cnt);
352 spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
355 static void attach_device(struct fsl_dma_domain *dma_domain, int liodn, struct device *dev)
357 struct device_domain_info *info, *old_domain_info;
360 spin_lock_irqsave(&device_domain_lock, flags);
362 * Check here if the device is already attached to domain or not.
363 * If the device is already attached to a domain detach it.
365 old_domain_info = dev->archdata.iommu_domain;
366 if (old_domain_info && old_domain_info->domain != dma_domain) {
367 spin_unlock_irqrestore(&device_domain_lock, flags);
368 detach_device(dev, old_domain_info->domain);
369 spin_lock_irqsave(&device_domain_lock, flags);
372 info = kmem_cache_zalloc(iommu_devinfo_cache, GFP_ATOMIC);
376 info->domain = dma_domain;
378 list_add(&info->link, &dma_domain->devices);
380 * In case of devices with multiple LIODNs just store
381 * the info for the first LIODN as all
382 * LIODNs share the same domain
384 if (!dev->archdata.iommu_domain)
385 dev->archdata.iommu_domain = info;
386 spin_unlock_irqrestore(&device_domain_lock, flags);
389 static phys_addr_t fsl_pamu_iova_to_phys(struct iommu_domain *domain,
392 struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain);
394 if (iova < domain->geometry.aperture_start ||
395 iova > domain->geometry.aperture_end)
398 return get_phys_addr(dma_domain, iova);
401 static bool fsl_pamu_capable(enum iommu_cap cap)
403 return cap == IOMMU_CAP_CACHE_COHERENCY;
406 static void fsl_pamu_domain_free(struct iommu_domain *domain)
408 struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain);
410 /* remove all the devices from the device list */
411 detach_device(NULL, dma_domain);
413 dma_domain->enabled = 0;
414 dma_domain->mapped = 0;
416 kmem_cache_free(fsl_pamu_domain_cache, dma_domain);
419 static struct iommu_domain *fsl_pamu_domain_alloc(unsigned type)
421 struct fsl_dma_domain *dma_domain;
423 if (type != IOMMU_DOMAIN_UNMANAGED)
426 dma_domain = iommu_alloc_dma_domain();
428 pr_debug("dma_domain allocation failed\n");
431 /* defaul geometry 64 GB i.e. maximum system address */
432 dma_domain->iommu_domain. geometry.aperture_start = 0;
433 dma_domain->iommu_domain.geometry.aperture_end = (1ULL << 36) - 1;
434 dma_domain->iommu_domain.geometry.force_aperture = true;
436 return &dma_domain->iommu_domain;
439 /* Configure geometry settings for all LIODNs associated with domain */
440 static int pamu_set_domain_geometry(struct fsl_dma_domain *dma_domain,
441 struct iommu_domain_geometry *geom_attr,
444 struct device_domain_info *info;
447 list_for_each_entry(info, &dma_domain->devices, link) {
448 ret = pamu_set_liodn(info->liodn, info->dev, dma_domain,
457 /* Update stash destination for all LIODNs associated with the domain */
458 static int update_domain_stash(struct fsl_dma_domain *dma_domain, u32 val)
460 struct device_domain_info *info;
463 list_for_each_entry(info, &dma_domain->devices, link) {
464 ret = update_liodn_stash(info->liodn, dma_domain, val);
472 /* Update domain mappings for all LIODNs associated with the domain */
473 static int update_domain_mapping(struct fsl_dma_domain *dma_domain, u32 wnd_nr)
475 struct device_domain_info *info;
478 list_for_each_entry(info, &dma_domain->devices, link) {
479 ret = update_liodn(info->liodn, dma_domain, wnd_nr);
486 static int disable_domain_win(struct fsl_dma_domain *dma_domain, u32 wnd_nr)
488 struct device_domain_info *info;
491 list_for_each_entry(info, &dma_domain->devices, link) {
492 if (dma_domain->win_cnt == 1 && dma_domain->enabled) {
493 ret = pamu_disable_liodn(info->liodn);
495 dma_domain->enabled = 0;
497 ret = pamu_disable_spaace(info->liodn, wnd_nr);
504 static void fsl_pamu_window_disable(struct iommu_domain *domain, u32 wnd_nr)
506 struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain);
510 spin_lock_irqsave(&dma_domain->domain_lock, flags);
511 if (!dma_domain->win_arr) {
512 pr_debug("Number of windows not configured\n");
513 spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
517 if (wnd_nr >= dma_domain->win_cnt) {
518 pr_debug("Invalid window index\n");
519 spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
523 if (dma_domain->win_arr[wnd_nr].valid) {
524 ret = disable_domain_win(dma_domain, wnd_nr);
526 dma_domain->win_arr[wnd_nr].valid = 0;
527 dma_domain->mapped--;
531 spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
534 static int fsl_pamu_window_enable(struct iommu_domain *domain, u32 wnd_nr,
535 phys_addr_t paddr, u64 size, int prot)
537 struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain);
538 struct dma_window *wnd;
544 if (prot & IOMMU_READ)
545 pamu_prot |= PAACE_AP_PERMS_QUERY;
546 if (prot & IOMMU_WRITE)
547 pamu_prot |= PAACE_AP_PERMS_UPDATE;
549 spin_lock_irqsave(&dma_domain->domain_lock, flags);
550 if (!dma_domain->win_arr) {
551 pr_debug("Number of windows not configured\n");
552 spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
556 if (wnd_nr >= dma_domain->win_cnt) {
557 pr_debug("Invalid window index\n");
558 spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
562 win_size = dma_domain->geom_size >> ilog2(dma_domain->win_cnt);
563 if (size > win_size) {
564 pr_debug("Invalid window size\n");
565 spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
569 if (dma_domain->win_cnt == 1) {
570 if (dma_domain->enabled) {
571 pr_debug("Disable the window before updating the mapping\n");
572 spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
576 ret = check_size(size, domain->geometry.aperture_start);
578 pr_debug("Aperture start not aligned to the size\n");
579 spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
584 wnd = &dma_domain->win_arr[wnd_nr];
588 wnd->prot = pamu_prot;
590 ret = update_domain_mapping(dma_domain, wnd_nr);
593 dma_domain->mapped++;
596 pr_debug("Disable the window before updating the mapping\n");
600 spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
606 * Attach the LIODN to the DMA domain and configure the geometry
607 * and window mappings.
609 static int handle_attach_device(struct fsl_dma_domain *dma_domain,
610 struct device *dev, const u32 *liodn,
614 struct iommu_domain *domain = &dma_domain->iommu_domain;
618 spin_lock_irqsave(&dma_domain->domain_lock, flags);
619 for (i = 0; i < num; i++) {
620 /* Ensure that LIODN value is valid */
621 if (liodn[i] >= PAACE_NUMBER_ENTRIES) {
622 pr_debug("Invalid liodn %d, attach device failed for %s\n",
623 liodn[i], dev->of_node->full_name);
628 attach_device(dma_domain, liodn[i], dev);
630 * Check if geometry has already been configured
631 * for the domain. If yes, set the geometry for
634 if (dma_domain->win_arr) {
635 u32 win_cnt = dma_domain->win_cnt > 1 ? dma_domain->win_cnt : 0;
637 ret = pamu_set_liodn(liodn[i], dev, dma_domain,
638 &domain->geometry, win_cnt);
641 if (dma_domain->mapped) {
643 * Create window/subwindow mapping for
646 ret = map_liodn(liodn[i], dma_domain);
652 spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
657 static int fsl_pamu_attach_device(struct iommu_domain *domain,
660 struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain);
664 struct pci_dev *pdev = NULL;
665 struct pci_controller *pci_ctl;
668 * Use LIODN of the PCI controller while attaching a
671 if (dev_is_pci(dev)) {
672 pdev = to_pci_dev(dev);
673 pci_ctl = pci_bus_to_host(pdev->bus);
675 * make dev point to pci controller device
676 * so we can get the LIODN programmed by
679 dev = pci_ctl->parent;
682 liodn = of_get_property(dev->of_node, "fsl,liodn", &len);
684 liodn_cnt = len / sizeof(u32);
685 ret = handle_attach_device(dma_domain, dev, liodn, liodn_cnt);
687 pr_debug("missing fsl,liodn property at %s\n",
688 dev->of_node->full_name);
695 static void fsl_pamu_detach_device(struct iommu_domain *domain,
698 struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain);
701 struct pci_dev *pdev = NULL;
702 struct pci_controller *pci_ctl;
705 * Use LIODN of the PCI controller while detaching a
708 if (dev_is_pci(dev)) {
709 pdev = to_pci_dev(dev);
710 pci_ctl = pci_bus_to_host(pdev->bus);
712 * make dev point to pci controller device
713 * so we can get the LIODN programmed by
716 dev = pci_ctl->parent;
719 prop = of_get_property(dev->of_node, "fsl,liodn", &len);
721 detach_device(dev, dma_domain);
723 pr_debug("missing fsl,liodn property at %s\n",
724 dev->of_node->full_name);
727 static int configure_domain_geometry(struct iommu_domain *domain, void *data)
729 struct iommu_domain_geometry *geom_attr = data;
730 struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain);
731 dma_addr_t geom_size;
734 geom_size = geom_attr->aperture_end - geom_attr->aperture_start + 1;
736 * Sanity check the geometry size. Also, we do not support
737 * DMA outside of the geometry.
739 if (check_size(geom_size, geom_attr->aperture_start) ||
740 !geom_attr->force_aperture) {
741 pr_debug("Invalid PAMU geometry attributes\n");
745 spin_lock_irqsave(&dma_domain->domain_lock, flags);
746 if (dma_domain->enabled) {
747 pr_debug("Can't set geometry attributes as domain is active\n");
748 spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
752 /* Copy the domain geometry information */
753 memcpy(&domain->geometry, geom_attr,
754 sizeof(struct iommu_domain_geometry));
755 dma_domain->geom_size = geom_size;
757 spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
762 /* Set the domain stash attribute */
763 static int configure_domain_stash(struct fsl_dma_domain *dma_domain, void *data)
765 struct pamu_stash_attribute *stash_attr = data;
769 spin_lock_irqsave(&dma_domain->domain_lock, flags);
771 memcpy(&dma_domain->dma_stash, stash_attr,
772 sizeof(struct pamu_stash_attribute));
774 dma_domain->stash_id = get_stash_id(stash_attr->cache,
776 if (dma_domain->stash_id == ~(u32)0) {
777 pr_debug("Invalid stash attributes\n");
778 spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
782 ret = update_domain_stash(dma_domain, dma_domain->stash_id);
784 spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
789 /* Configure domain dma state i.e. enable/disable DMA */
790 static int configure_domain_dma_state(struct fsl_dma_domain *dma_domain, bool enable)
792 struct device_domain_info *info;
796 spin_lock_irqsave(&dma_domain->domain_lock, flags);
798 if (enable && !dma_domain->mapped) {
799 pr_debug("Can't enable DMA domain without valid mapping\n");
800 spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
804 dma_domain->enabled = enable;
805 list_for_each_entry(info, &dma_domain->devices, link) {
806 ret = (enable) ? pamu_enable_liodn(info->liodn) :
807 pamu_disable_liodn(info->liodn);
809 pr_debug("Unable to set dma state for liodn %d",
812 spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
817 static int fsl_pamu_set_domain_attr(struct iommu_domain *domain,
818 enum iommu_attr attr_type, void *data)
820 struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain);
824 case DOMAIN_ATTR_GEOMETRY:
825 ret = configure_domain_geometry(domain, data);
827 case DOMAIN_ATTR_FSL_PAMU_STASH:
828 ret = configure_domain_stash(dma_domain, data);
830 case DOMAIN_ATTR_FSL_PAMU_ENABLE:
831 ret = configure_domain_dma_state(dma_domain, *(int *)data);
834 pr_debug("Unsupported attribute type\n");
842 static int fsl_pamu_get_domain_attr(struct iommu_domain *domain,
843 enum iommu_attr attr_type, void *data)
845 struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain);
849 case DOMAIN_ATTR_FSL_PAMU_STASH:
850 memcpy(data, &dma_domain->dma_stash,
851 sizeof(struct pamu_stash_attribute));
853 case DOMAIN_ATTR_FSL_PAMU_ENABLE:
854 *(int *)data = dma_domain->enabled;
856 case DOMAIN_ATTR_FSL_PAMUV1:
857 *(int *)data = DOMAIN_ATTR_FSL_PAMUV1;
860 pr_debug("Unsupported attribute type\n");
868 static struct iommu_group *get_device_iommu_group(struct device *dev)
870 struct iommu_group *group;
872 group = iommu_group_get(dev);
874 group = iommu_group_alloc();
879 static bool check_pci_ctl_endpt_part(struct pci_controller *pci_ctl)
883 /* Check the PCI controller version number by readding BRR1 register */
884 version = in_be32(pci_ctl->cfg_addr + (PCI_FSL_BRR1 >> 2));
885 version &= PCI_FSL_BRR1_VER;
886 /* If PCI controller version is >= 0x204 we can partition endpoints */
887 return version >= 0x204;
890 /* Get iommu group information from peer devices or devices on the parent bus */
891 static struct iommu_group *get_shared_pci_device_group(struct pci_dev *pdev)
894 struct iommu_group *group;
895 struct pci_bus *bus = pdev->bus;
898 * Traverese the pci bus device list to get
899 * the shared iommu group.
902 list_for_each_entry(tmp, &bus->devices, bus_list) {
905 group = iommu_group_get(&tmp->dev);
916 static struct iommu_group *get_pci_device_group(struct pci_dev *pdev)
918 struct pci_controller *pci_ctl;
919 bool pci_endpt_partioning;
920 struct iommu_group *group = NULL;
922 pci_ctl = pci_bus_to_host(pdev->bus);
923 pci_endpt_partioning = check_pci_ctl_endpt_part(pci_ctl);
924 /* We can partition PCIe devices so assign device group to the device */
925 if (pci_endpt_partioning) {
926 group = iommu_group_get_for_dev(&pdev->dev);
929 * PCIe controller is not a paritionable entity
930 * free the controller device iommu_group.
932 if (pci_ctl->parent->iommu_group)
933 iommu_group_remove_device(pci_ctl->parent);
936 * All devices connected to the controller will share the
937 * PCI controllers device group. If this is the first
938 * device to be probed for the pci controller, copy the
939 * device group information from the PCI controller device
940 * node and remove the PCI controller iommu group.
941 * For subsequent devices, the iommu group information can
942 * be obtained from sibling devices (i.e. from the bus_devices
945 if (pci_ctl->parent->iommu_group) {
946 group = get_device_iommu_group(pci_ctl->parent);
947 iommu_group_remove_device(pci_ctl->parent);
949 group = get_shared_pci_device_group(pdev);
954 group = ERR_PTR(-ENODEV);
959 static int fsl_pamu_add_device(struct device *dev)
961 struct iommu_group *group = ERR_PTR(-ENODEV);
962 struct pci_dev *pdev;
967 * For platform devices we allocate a separate group for
968 * each of the devices.
970 if (dev_is_pci(dev)) {
971 pdev = to_pci_dev(dev);
972 /* Don't create device groups for virtual PCI bridges */
973 if (pdev->subordinate)
976 group = get_pci_device_group(pdev);
979 prop = of_get_property(dev->of_node, "fsl,liodn", &len);
981 group = get_device_iommu_group(dev);
985 return PTR_ERR(group);
988 * Check if device has already been added to an iommu group.
989 * Group could have already been created for a PCI device in
990 * the iommu_group_get_for_dev path.
992 if (!dev->iommu_group)
993 ret = iommu_group_add_device(group, dev);
995 iommu_group_put(group);
999 static void fsl_pamu_remove_device(struct device *dev)
1001 iommu_group_remove_device(dev);
1004 static int fsl_pamu_set_windows(struct iommu_domain *domain, u32 w_count)
1006 struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain);
1007 unsigned long flags;
1010 spin_lock_irqsave(&dma_domain->domain_lock, flags);
1011 /* Ensure domain is inactive i.e. DMA should be disabled for the domain */
1012 if (dma_domain->enabled) {
1013 pr_debug("Can't set geometry attributes as domain is active\n");
1014 spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
1018 /* Ensure that the geometry has been set for the domain */
1019 if (!dma_domain->geom_size) {
1020 pr_debug("Please configure geometry before setting the number of windows\n");
1021 spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
1026 * Ensure we have valid window count i.e. it should be less than
1027 * maximum permissible limit and should be a power of two.
1029 if (w_count > pamu_get_max_subwin_cnt() || !is_power_of_2(w_count)) {
1030 pr_debug("Invalid window count\n");
1031 spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
1035 ret = pamu_set_domain_geometry(dma_domain, &domain->geometry,
1036 w_count > 1 ? w_count : 0);
1038 kfree(dma_domain->win_arr);
1039 dma_domain->win_arr = kcalloc(w_count,
1040 sizeof(*dma_domain->win_arr),
1042 if (!dma_domain->win_arr) {
1043 spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
1046 dma_domain->win_cnt = w_count;
1048 spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
1053 static u32 fsl_pamu_get_windows(struct iommu_domain *domain)
1055 struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain);
1057 return dma_domain->win_cnt;
1060 static const struct iommu_ops fsl_pamu_ops = {
1061 .capable = fsl_pamu_capable,
1062 .domain_alloc = fsl_pamu_domain_alloc,
1063 .domain_free = fsl_pamu_domain_free,
1064 .attach_dev = fsl_pamu_attach_device,
1065 .detach_dev = fsl_pamu_detach_device,
1066 .domain_window_enable = fsl_pamu_window_enable,
1067 .domain_window_disable = fsl_pamu_window_disable,
1068 .domain_get_windows = fsl_pamu_get_windows,
1069 .domain_set_windows = fsl_pamu_set_windows,
1070 .iova_to_phys = fsl_pamu_iova_to_phys,
1071 .domain_set_attr = fsl_pamu_set_domain_attr,
1072 .domain_get_attr = fsl_pamu_get_domain_attr,
1073 .add_device = fsl_pamu_add_device,
1074 .remove_device = fsl_pamu_remove_device,
1077 int __init pamu_domain_init(void)
1081 ret = iommu_init_mempool();
1085 bus_set_iommu(&platform_bus_type, &fsl_pamu_ops);
1086 bus_set_iommu(&pci_bus_type, &fsl_pamu_ops);