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 %pOF\n",
623 liodn[i], dev->of_node);
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 %pOF\n", dev->of_node);
694 static void fsl_pamu_detach_device(struct iommu_domain *domain,
697 struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain);
700 struct pci_dev *pdev = NULL;
701 struct pci_controller *pci_ctl;
704 * Use LIODN of the PCI controller while detaching a
707 if (dev_is_pci(dev)) {
708 pdev = to_pci_dev(dev);
709 pci_ctl = pci_bus_to_host(pdev->bus);
711 * make dev point to pci controller device
712 * so we can get the LIODN programmed by
715 dev = pci_ctl->parent;
718 prop = of_get_property(dev->of_node, "fsl,liodn", &len);
720 detach_device(dev, dma_domain);
722 pr_debug("missing fsl,liodn property at %pOF\n", dev->of_node);
725 static int configure_domain_geometry(struct iommu_domain *domain, void *data)
727 struct iommu_domain_geometry *geom_attr = data;
728 struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain);
729 dma_addr_t geom_size;
732 geom_size = geom_attr->aperture_end - geom_attr->aperture_start + 1;
734 * Sanity check the geometry size. Also, we do not support
735 * DMA outside of the geometry.
737 if (check_size(geom_size, geom_attr->aperture_start) ||
738 !geom_attr->force_aperture) {
739 pr_debug("Invalid PAMU geometry attributes\n");
743 spin_lock_irqsave(&dma_domain->domain_lock, flags);
744 if (dma_domain->enabled) {
745 pr_debug("Can't set geometry attributes as domain is active\n");
746 spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
750 /* Copy the domain geometry information */
751 memcpy(&domain->geometry, geom_attr,
752 sizeof(struct iommu_domain_geometry));
753 dma_domain->geom_size = geom_size;
755 spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
760 /* Set the domain stash attribute */
761 static int configure_domain_stash(struct fsl_dma_domain *dma_domain, void *data)
763 struct pamu_stash_attribute *stash_attr = data;
767 spin_lock_irqsave(&dma_domain->domain_lock, flags);
769 memcpy(&dma_domain->dma_stash, stash_attr,
770 sizeof(struct pamu_stash_attribute));
772 dma_domain->stash_id = get_stash_id(stash_attr->cache,
774 if (dma_domain->stash_id == ~(u32)0) {
775 pr_debug("Invalid stash attributes\n");
776 spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
780 ret = update_domain_stash(dma_domain, dma_domain->stash_id);
782 spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
787 /* Configure domain dma state i.e. enable/disable DMA */
788 static int configure_domain_dma_state(struct fsl_dma_domain *dma_domain, bool enable)
790 struct device_domain_info *info;
794 spin_lock_irqsave(&dma_domain->domain_lock, flags);
796 if (enable && !dma_domain->mapped) {
797 pr_debug("Can't enable DMA domain without valid mapping\n");
798 spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
802 dma_domain->enabled = enable;
803 list_for_each_entry(info, &dma_domain->devices, link) {
804 ret = (enable) ? pamu_enable_liodn(info->liodn) :
805 pamu_disable_liodn(info->liodn);
807 pr_debug("Unable to set dma state for liodn %d",
810 spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
815 static int fsl_pamu_set_domain_attr(struct iommu_domain *domain,
816 enum iommu_attr attr_type, void *data)
818 struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain);
822 case DOMAIN_ATTR_GEOMETRY:
823 ret = configure_domain_geometry(domain, data);
825 case DOMAIN_ATTR_FSL_PAMU_STASH:
826 ret = configure_domain_stash(dma_domain, data);
828 case DOMAIN_ATTR_FSL_PAMU_ENABLE:
829 ret = configure_domain_dma_state(dma_domain, *(int *)data);
832 pr_debug("Unsupported attribute type\n");
840 static int fsl_pamu_get_domain_attr(struct iommu_domain *domain,
841 enum iommu_attr attr_type, void *data)
843 struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain);
847 case DOMAIN_ATTR_FSL_PAMU_STASH:
848 memcpy(data, &dma_domain->dma_stash,
849 sizeof(struct pamu_stash_attribute));
851 case DOMAIN_ATTR_FSL_PAMU_ENABLE:
852 *(int *)data = dma_domain->enabled;
854 case DOMAIN_ATTR_FSL_PAMUV1:
855 *(int *)data = DOMAIN_ATTR_FSL_PAMUV1;
858 pr_debug("Unsupported attribute type\n");
866 static struct iommu_group *get_device_iommu_group(struct device *dev)
868 struct iommu_group *group;
870 group = iommu_group_get(dev);
872 group = iommu_group_alloc();
877 static bool check_pci_ctl_endpt_part(struct pci_controller *pci_ctl)
881 /* Check the PCI controller version number by readding BRR1 register */
882 version = in_be32(pci_ctl->cfg_addr + (PCI_FSL_BRR1 >> 2));
883 version &= PCI_FSL_BRR1_VER;
884 /* If PCI controller version is >= 0x204 we can partition endpoints */
885 return version >= 0x204;
888 /* Get iommu group information from peer devices or devices on the parent bus */
889 static struct iommu_group *get_shared_pci_device_group(struct pci_dev *pdev)
892 struct iommu_group *group;
893 struct pci_bus *bus = pdev->bus;
896 * Traverese the pci bus device list to get
897 * the shared iommu group.
900 list_for_each_entry(tmp, &bus->devices, bus_list) {
903 group = iommu_group_get(&tmp->dev);
914 static struct iommu_group *get_pci_device_group(struct pci_dev *pdev)
916 struct pci_controller *pci_ctl;
917 bool pci_endpt_partioning;
918 struct iommu_group *group = NULL;
920 pci_ctl = pci_bus_to_host(pdev->bus);
921 pci_endpt_partioning = check_pci_ctl_endpt_part(pci_ctl);
922 /* We can partition PCIe devices so assign device group to the device */
923 if (pci_endpt_partioning) {
924 group = pci_device_group(&pdev->dev);
927 * PCIe controller is not a paritionable entity
928 * free the controller device iommu_group.
930 if (pci_ctl->parent->iommu_group)
931 iommu_group_remove_device(pci_ctl->parent);
934 * All devices connected to the controller will share the
935 * PCI controllers device group. If this is the first
936 * device to be probed for the pci controller, copy the
937 * device group information from the PCI controller device
938 * node and remove the PCI controller iommu group.
939 * For subsequent devices, the iommu group information can
940 * be obtained from sibling devices (i.e. from the bus_devices
943 if (pci_ctl->parent->iommu_group) {
944 group = get_device_iommu_group(pci_ctl->parent);
945 iommu_group_remove_device(pci_ctl->parent);
947 group = get_shared_pci_device_group(pdev);
952 group = ERR_PTR(-ENODEV);
957 static struct iommu_group *fsl_pamu_device_group(struct device *dev)
959 struct iommu_group *group = ERR_PTR(-ENODEV);
963 * For platform devices we allocate a separate group for
964 * each of the devices.
967 group = get_pci_device_group(to_pci_dev(dev));
968 else if (of_get_property(dev->of_node, "fsl,liodn", &len))
969 group = get_device_iommu_group(dev);
974 static int fsl_pamu_add_device(struct device *dev)
976 struct iommu_group *group;
978 group = iommu_group_get_for_dev(dev);
980 return PTR_ERR(group);
982 iommu_group_put(group);
987 static void fsl_pamu_remove_device(struct device *dev)
989 iommu_group_remove_device(dev);
992 static int fsl_pamu_set_windows(struct iommu_domain *domain, u32 w_count)
994 struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain);
998 spin_lock_irqsave(&dma_domain->domain_lock, flags);
999 /* Ensure domain is inactive i.e. DMA should be disabled for the domain */
1000 if (dma_domain->enabled) {
1001 pr_debug("Can't set geometry attributes as domain is active\n");
1002 spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
1006 /* Ensure that the geometry has been set for the domain */
1007 if (!dma_domain->geom_size) {
1008 pr_debug("Please configure geometry before setting the number of windows\n");
1009 spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
1014 * Ensure we have valid window count i.e. it should be less than
1015 * maximum permissible limit and should be a power of two.
1017 if (w_count > pamu_get_max_subwin_cnt() || !is_power_of_2(w_count)) {
1018 pr_debug("Invalid window count\n");
1019 spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
1023 ret = pamu_set_domain_geometry(dma_domain, &domain->geometry,
1024 w_count > 1 ? w_count : 0);
1026 kfree(dma_domain->win_arr);
1027 dma_domain->win_arr = kcalloc(w_count,
1028 sizeof(*dma_domain->win_arr),
1030 if (!dma_domain->win_arr) {
1031 spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
1034 dma_domain->win_cnt = w_count;
1036 spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
1041 static u32 fsl_pamu_get_windows(struct iommu_domain *domain)
1043 struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain);
1045 return dma_domain->win_cnt;
1048 static const struct iommu_ops fsl_pamu_ops = {
1049 .capable = fsl_pamu_capable,
1050 .domain_alloc = fsl_pamu_domain_alloc,
1051 .domain_free = fsl_pamu_domain_free,
1052 .attach_dev = fsl_pamu_attach_device,
1053 .detach_dev = fsl_pamu_detach_device,
1054 .domain_window_enable = fsl_pamu_window_enable,
1055 .domain_window_disable = fsl_pamu_window_disable,
1056 .domain_get_windows = fsl_pamu_get_windows,
1057 .domain_set_windows = fsl_pamu_set_windows,
1058 .iova_to_phys = fsl_pamu_iova_to_phys,
1059 .domain_set_attr = fsl_pamu_set_domain_attr,
1060 .domain_get_attr = fsl_pamu_get_domain_attr,
1061 .add_device = fsl_pamu_add_device,
1062 .remove_device = fsl_pamu_remove_device,
1063 .device_group = fsl_pamu_device_group,
1066 int __init pamu_domain_init(void)
1070 ret = iommu_init_mempool();
1074 bus_set_iommu(&platform_bus_type, &fsl_pamu_ops);
1075 bus_set_iommu(&pci_bus_type, &fsl_pamu_ops);