xcp-1.6-updates/xen-4.1.hg

changeset 23314:10d5cc3094d6

xen, vtd: Fix device check for devices behind PCIe-to-PCI bridges

On some systems, requests devices behind a PCIe-to-PCI bridge all
appear to the IOMMU as though they come from from slot 0, function 0
on that device; so the mapping code much punch a hole for X:0.0 in the
IOMMU for such devices. When punching the hole, if that device has
already been mapped once, we simply need to check ownership to make
sure it's legal. To do so, domain_context_mapping_one() will look up
the device for the mapping with pci_get_pdev() and look for the owner.

However, if there is no device in X:0.0, this look up will fail.

Rather than returning -ENODEV in this situation (causing a failure in
mapping the device), try to get the domain ownership from the iommu
context mapping itself.

Signed-off-by: George Dunlap <george.dunlap@eu.citrix.com>
xen-unstable changeset: 23813:5535d7ce2673
xen-unstable date: Mon Sep 05 15:00:46 2011 +0100
author George Dunlap <george.dunlap@eu.citrix.com>
date Mon Jul 09 10:22:58 2012 +0100 (2012-07-09)
parents 9fe2a77b740d
children 3b50ea3909a8
files xen/drivers/passthrough/vtd/iommu.c
line diff
     1.1 --- a/xen/drivers/passthrough/vtd/iommu.c	Mon Jul 09 10:22:24 2012 +0100
     1.2 +++ b/xen/drivers/passthrough/vtd/iommu.c	Mon Jul 09 10:22:58 2012 +0100
     1.3 @@ -118,6 +118,27 @@ static int context_set_domain_id(struct 
     1.4      return 0;
     1.5  }
     1.6  
     1.7 +static int context_get_domain_id(struct context_entry *context,
     1.8 +                                 struct iommu *iommu)
     1.9 +{
    1.10 +    unsigned long dom_index, nr_dom;
    1.11 +    int domid = -1;
    1.12 +
    1.13 +    if (iommu && context)
    1.14 +    {
    1.15 +        nr_dom = cap_ndoms(iommu->cap);
    1.16 +
    1.17 +        dom_index = context_domain_id(*context);
    1.18 +
    1.19 +        if ( dom_index < nr_dom && iommu->domid_map)
    1.20 +            domid = iommu->domid_map[dom_index];
    1.21 +        else
    1.22 +            dprintk(XENLOG_DEBUG VTDPREFIX, "%s: dom_index %lu exceeds nr_dom %lu or iommu has no domid_map\n",
    1.23 +                    __func__, dom_index, nr_dom);
    1.24 +    }
    1.25 +    return domid;
    1.26 +}
    1.27 +
    1.28  static struct intel_iommu *__init alloc_intel_iommu(void)
    1.29  {
    1.30      struct intel_iommu *intel;
    1.31 @@ -1278,7 +1299,6 @@ int domain_context_mapping_one(
    1.32      struct hvm_iommu *hd = domain_hvm_iommu(domain);
    1.33      struct context_entry *context, *context_entries;
    1.34      u64 maddr, pgd_maddr;
    1.35 -    struct pci_dev *pdev = NULL;
    1.36      int agaw;
    1.37  
    1.38      ASSERT(spin_is_locked(&pcidevs_lock));
    1.39 @@ -1290,12 +1310,45 @@ int domain_context_mapping_one(
    1.40      if ( context_present(*context) )
    1.41      {
    1.42          int res = 0;
    1.43 +        struct pci_dev *pdev = NULL;
    1.44  
    1.45 +        /* First try to get domain ownership from device structure.  If that's
    1.46 +         * not available, try to read it from the context itself. */
    1.47          pdev = pci_get_pdev(bus, devfn);
    1.48 -        if (!pdev)
    1.49 -            res = -ENODEV;
    1.50 -        else if (pdev->domain != domain)
    1.51 -            res = -EINVAL;
    1.52 +        if ( pdev )
    1.53 +        {
    1.54 +            if ( pdev->domain != domain )
    1.55 +            {
    1.56 +                dprintk(XENLOG_INFO VTDPREFIX, "d%d: bdf = %x:%x.%x owned by d%d!",
    1.57 +                        domain->domain_id, 
    1.58 +                        bus, PCI_SLOT(devfn), PCI_FUNC(devfn),
    1.59 +                        (pdev->domain)
    1.60 +                        ? pdev->domain->domain_id : -1);
    1.61 +                res = -EINVAL;
    1.62 +            }
    1.63 +        }
    1.64 +        else
    1.65 +        {
    1.66 +            int cdomain;
    1.67 +            cdomain = context_get_domain_id(context, iommu);
    1.68 +            
    1.69 +            if ( cdomain < 0 )
    1.70 +            {
    1.71 +                dprintk(VTDPREFIX, "d%d: bdf = %x:%x.%x mapped, but can't find owner!\n",
    1.72 +                        domain->domain_id, 
    1.73 +                        bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
    1.74 +                res = -EINVAL;
    1.75 +            }
    1.76 +            else if ( cdomain != domain->domain_id )
    1.77 +            {
    1.78 +                dprintk(XENLOG_INFO VTDPREFIX, "d%d: bdf = %x:%x.%x already mapped to d%d!",
    1.79 +                        domain->domain_id, 
    1.80 +                        bus, PCI_SLOT(devfn), PCI_FUNC(devfn),
    1.81 +                        cdomain);
    1.82 +                res = -EINVAL;
    1.83 +            }
    1.84 +        }
    1.85 +
    1.86          unmap_vtd_domain_page(context_entries);
    1.87          spin_unlock(&iommu->lock);
    1.88          return res;