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 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 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 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 */ 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 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 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 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 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 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 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 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 */ 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 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 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 */ 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 337695093e3SVarun Sethi static struct iommu_group *get_device_iommu_group(struct device *dev) 338695093e3SVarun Sethi { 339695093e3SVarun Sethi struct iommu_group *group; 340695093e3SVarun Sethi 341695093e3SVarun Sethi group = iommu_group_get(dev); 342695093e3SVarun Sethi if (!group) 343695093e3SVarun Sethi group = iommu_group_alloc(); 344695093e3SVarun Sethi 345695093e3SVarun Sethi return group; 346695093e3SVarun Sethi } 347695093e3SVarun Sethi 348695093e3SVarun Sethi static bool check_pci_ctl_endpt_part(struct pci_controller *pci_ctl) 349695093e3SVarun Sethi { 350695093e3SVarun Sethi u32 version; 351695093e3SVarun Sethi 352695093e3SVarun Sethi /* Check the PCI controller version number by readding BRR1 register */ 353695093e3SVarun Sethi version = in_be32(pci_ctl->cfg_addr + (PCI_FSL_BRR1 >> 2)); 354695093e3SVarun Sethi version &= PCI_FSL_BRR1_VER; 355695093e3SVarun Sethi /* If PCI controller version is >= 0x204 we can partition endpoints */ 356cd70d465SEmil Medve return version >= 0x204; 357695093e3SVarun Sethi } 358695093e3SVarun Sethi 359695093e3SVarun Sethi /* Get iommu group information from peer devices or devices on the parent bus */ 360695093e3SVarun Sethi static struct iommu_group *get_shared_pci_device_group(struct pci_dev *pdev) 361695093e3SVarun Sethi { 362695093e3SVarun Sethi struct pci_dev *tmp; 363695093e3SVarun Sethi struct iommu_group *group; 364695093e3SVarun Sethi struct pci_bus *bus = pdev->bus; 365695093e3SVarun Sethi 366695093e3SVarun Sethi /* 367695093e3SVarun Sethi * Traverese the pci bus device list to get 368695093e3SVarun Sethi * the shared iommu group. 369695093e3SVarun Sethi */ 370695093e3SVarun Sethi while (bus) { 371695093e3SVarun Sethi list_for_each_entry(tmp, &bus->devices, bus_list) { 372695093e3SVarun Sethi if (tmp == pdev) 373695093e3SVarun Sethi continue; 374695093e3SVarun Sethi group = iommu_group_get(&tmp->dev); 375695093e3SVarun Sethi if (group) 376695093e3SVarun Sethi return group; 377695093e3SVarun Sethi } 378695093e3SVarun Sethi 379695093e3SVarun Sethi bus = bus->parent; 380695093e3SVarun Sethi } 381695093e3SVarun Sethi 382695093e3SVarun Sethi return NULL; 383695093e3SVarun Sethi } 384695093e3SVarun Sethi 385695093e3SVarun Sethi static struct iommu_group *get_pci_device_group(struct pci_dev *pdev) 386695093e3SVarun Sethi { 387695093e3SVarun Sethi struct pci_controller *pci_ctl; 388bc46c229SColin Ian King bool pci_endpt_partitioning; 389695093e3SVarun Sethi struct iommu_group *group = NULL; 390695093e3SVarun Sethi 391695093e3SVarun Sethi pci_ctl = pci_bus_to_host(pdev->bus); 392bc46c229SColin Ian King pci_endpt_partitioning = check_pci_ctl_endpt_part(pci_ctl); 393695093e3SVarun Sethi /* We can partition PCIe devices so assign device group to the device */ 394bc46c229SColin Ian King if (pci_endpt_partitioning) { 395d5e58297SJoerg Roedel group = pci_device_group(&pdev->dev); 396695093e3SVarun Sethi 397695093e3SVarun Sethi /* 398695093e3SVarun Sethi * PCIe controller is not a paritionable entity 399695093e3SVarun Sethi * free the controller device iommu_group. 400695093e3SVarun Sethi */ 401695093e3SVarun Sethi if (pci_ctl->parent->iommu_group) 402695093e3SVarun Sethi iommu_group_remove_device(pci_ctl->parent); 403695093e3SVarun Sethi } else { 404695093e3SVarun Sethi /* 405695093e3SVarun Sethi * All devices connected to the controller will share the 406695093e3SVarun Sethi * PCI controllers device group. If this is the first 407695093e3SVarun Sethi * device to be probed for the pci controller, copy the 408695093e3SVarun Sethi * device group information from the PCI controller device 409695093e3SVarun Sethi * node and remove the PCI controller iommu group. 410695093e3SVarun Sethi * For subsequent devices, the iommu group information can 411695093e3SVarun Sethi * be obtained from sibling devices (i.e. from the bus_devices 412695093e3SVarun Sethi * link list). 413695093e3SVarun Sethi */ 414695093e3SVarun Sethi if (pci_ctl->parent->iommu_group) { 415695093e3SVarun Sethi group = get_device_iommu_group(pci_ctl->parent); 416695093e3SVarun Sethi iommu_group_remove_device(pci_ctl->parent); 417cd70d465SEmil Medve } else { 418695093e3SVarun Sethi group = get_shared_pci_device_group(pdev); 419695093e3SVarun Sethi } 420cd70d465SEmil Medve } 421695093e3SVarun Sethi 4223170447cSVarun Sethi if (!group) 4233170447cSVarun Sethi group = ERR_PTR(-ENODEV); 4243170447cSVarun Sethi 425695093e3SVarun Sethi return group; 426695093e3SVarun Sethi } 427695093e3SVarun Sethi 428d5e58297SJoerg Roedel static struct iommu_group *fsl_pamu_device_group(struct device *dev) 429695093e3SVarun Sethi { 4303170447cSVarun Sethi struct iommu_group *group = ERR_PTR(-ENODEV); 431d5e58297SJoerg Roedel int len; 432695093e3SVarun Sethi 433695093e3SVarun Sethi /* 434695093e3SVarun Sethi * For platform devices we allocate a separate group for 435695093e3SVarun Sethi * each of the devices. 436695093e3SVarun Sethi */ 437d5e58297SJoerg Roedel if (dev_is_pci(dev)) 438d5e58297SJoerg Roedel group = get_pci_device_group(to_pci_dev(dev)); 439d5e58297SJoerg Roedel else if (of_get_property(dev->of_node, "fsl,liodn", &len)) 440695093e3SVarun Sethi group = get_device_iommu_group(dev); 441d5e58297SJoerg Roedel 442d5e58297SJoerg Roedel return group; 443695093e3SVarun Sethi } 444695093e3SVarun Sethi 44552dd3ca4SJoerg Roedel static struct iommu_device *fsl_pamu_probe_device(struct device *dev) 446d5e58297SJoerg Roedel { 44752dd3ca4SJoerg Roedel return &pamu_iommu; 448695093e3SVarun Sethi } 449695093e3SVarun Sethi 450b22f6434SThierry Reding static const struct iommu_ops fsl_pamu_ops = { 451b7eb6785SJoerg Roedel .capable = fsl_pamu_capable, 4528d4bfe40SJoerg Roedel .domain_alloc = fsl_pamu_domain_alloc, 45352dd3ca4SJoerg Roedel .probe_device = fsl_pamu_probe_device, 454d5e58297SJoerg Roedel .device_group = fsl_pamu_device_group, 455*bb649412SJoerg Roedel .set_platform_dma_ops = fsl_pamu_set_platform_dma, 4569a630a4bSLu Baolu .default_domain_ops = &(const struct iommu_domain_ops) { 4579a630a4bSLu Baolu .attach_dev = fsl_pamu_attach_device, 4589a630a4bSLu Baolu .iova_to_phys = fsl_pamu_iova_to_phys, 4599a630a4bSLu Baolu .free = fsl_pamu_domain_free, 4609a630a4bSLu Baolu } 461695093e3SVarun Sethi }; 462695093e3SVarun Sethi 463cd70d465SEmil Medve int __init pamu_domain_init(void) 464695093e3SVarun Sethi { 465695093e3SVarun Sethi int ret = 0; 466695093e3SVarun Sethi 467695093e3SVarun Sethi ret = iommu_init_mempool(); 468695093e3SVarun Sethi if (ret) 469695093e3SVarun Sethi return ret; 470695093e3SVarun Sethi 4713ff2dcc0SJoerg Roedel ret = iommu_device_sysfs_add(&pamu_iommu, NULL, NULL, "iommu0"); 4723ff2dcc0SJoerg Roedel if (ret) 4733ff2dcc0SJoerg Roedel return ret; 4743ff2dcc0SJoerg Roedel 4752d471b20SRobin Murphy ret = iommu_device_register(&pamu_iommu, &fsl_pamu_ops, NULL); 4763ff2dcc0SJoerg Roedel if (ret) { 4773ff2dcc0SJoerg Roedel iommu_device_sysfs_remove(&pamu_iommu); 4783ff2dcc0SJoerg Roedel pr_err("Can't register iommu device\n"); 4793ff2dcc0SJoerg Roedel } 4803ff2dcc0SJoerg Roedel 481695093e3SVarun Sethi return ret; 482695093e3SVarun Sethi } 483