ia64/xen-unstable

changeset 16875:74a9bfccddba

vt-d: Do FLR of assigned devices with VT-d

Currently there is a pdev_flr() function to do FLR before device
assignment in qemu, but most of devices don't have FLR capability.
What's more, should do FLR before assignment and deassignment for
keeping correct device status. If the device doesn't have FLR
capablility, this patch implemented to enter D3hot and return to D0 to
do FLR. And exposed pdev_flr() in VT-d utils, then it can be invoked
by assignment and deassignment functions.

Signed-off-by: Weidong Han <weidong.han@intel.com>
Signed-off-by: Anthony Xu <anthony.xu@intel.com>
author Keir Fraser <keir.fraser@citrix.com>
date Thu Jan 24 14:39:38 2008 +0000 (2008-01-24)
parents bfea0518d630
children 6269a3ce7b83
files tools/ioemu/hw/pass-through.c xen/arch/x86/hvm/vmx/vtd/extern.h xen/arch/x86/hvm/vmx/vtd/intel-iommu.c xen/arch/x86/hvm/vmx/vtd/utils.c
line diff
     1.1 --- a/tools/ioemu/hw/pass-through.c	Thu Jan 24 14:38:01 2008 +0000
     1.2 +++ b/tools/ioemu/hw/pass-through.c	Thu Jan 24 14:39:38 2008 +0000
     1.3 @@ -56,56 +56,6 @@ static int next_bdf(char **str, int *seg
     1.4      return 1;
     1.5  }
     1.6  
     1.7 -uint8_t find_cap_offset(struct pci_dev *pci_dev, uint8_t cap)
     1.8 -{
     1.9 -    int id;
    1.10 -    int max_cap = 48;
    1.11 -    int pos = PCI_CAPABILITY_LIST;
    1.12 -    int status;
    1.13 -
    1.14 -    status = pci_read_byte(pci_dev, PCI_STATUS);
    1.15 -    if ( (status & PCI_STATUS_CAP_LIST) == 0 )
    1.16 -        return 0;
    1.17 -
    1.18 -    while ( max_cap-- )
    1.19 -    {
    1.20 -        pos = pci_read_byte(pci_dev, pos);
    1.21 -        if ( pos < 0x40 )
    1.22 -            break;
    1.23 -
    1.24 -        pos &= ~3;
    1.25 -        id = pci_read_byte(pci_dev, pos + PCI_CAP_LIST_ID);
    1.26 -
    1.27 -        if ( id == 0xff )
    1.28 -            break;
    1.29 -        if ( id == cap )
    1.30 -            return pos;
    1.31 -
    1.32 -        pos += PCI_CAP_LIST_NEXT;
    1.33 -    }
    1.34 -    return 0;
    1.35 -}
    1.36 -
    1.37 -void pdev_flr(struct pci_dev *pci_dev)
    1.38 -{
    1.39 -    int pos;
    1.40 -    int dev_cap;
    1.41 -    int dev_status;
    1.42 -
    1.43 -    pos = find_cap_offset(pci_dev, PCI_CAP_ID_EXP);
    1.44 -    if ( pos )
    1.45 -    {
    1.46 -        dev_cap = pci_read_long(pci_dev, pos + PCI_EXP_DEVCAP);
    1.47 -        if ( dev_cap & PCI_EXP_DEVCAP_FLR )
    1.48 -        {
    1.49 -            pci_write_word(pci_dev, pos + PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_FLR);
    1.50 -            do {
    1.51 -                dev_status = pci_read_long(pci_dev, pos + PCI_EXP_DEVSTA);
    1.52 -            } while (dev_status & PCI_EXP_DEVSTA_TRPND);
    1.53 -        }
    1.54 -    }
    1.55 -}
    1.56 -
    1.57  /* Being called each time a mmio region has been updated */
    1.58  void pt_iomem_map(PCIDevice *d, int i, uint32_t e_phys, uint32_t e_size,
    1.59                    int type)
    1.60 @@ -273,7 +223,7 @@ static int pt_register_regions(struct pt
    1.61      PCIDevice *d = &assigned_device->dev;
    1.62  
    1.63      /* Register PIO/MMIO BARs */
    1.64 -    for ( i=0; i < PCI_BAR_ENTRIES; i++ )
    1.65 +    for ( i = 0; i < PCI_BAR_ENTRIES; i++ )
    1.66      {
    1.67          if ( pci_dev->base_addr[i] )
    1.68          {
    1.69 @@ -358,9 +308,6 @@ struct pt_dev * register_real_device(PCI
    1.70  
    1.71      assigned_device->pci_dev = pci_dev;
    1.72  
    1.73 -    /* Issue PCIe FLR */
    1.74 -    pdev_flr(pci_dev);
    1.75 -
    1.76      /* Assign device */
    1.77      machine_bdf.reg = 0;
    1.78      machine_bdf.bus = r_bus;
     2.1 --- a/xen/arch/x86/hvm/vmx/vtd/extern.h	Thu Jan 24 14:38:01 2008 +0000
     2.2 +++ b/xen/arch/x86/hvm/vmx/vtd/extern.h	Thu Jan 24 14:39:38 2008 +0000
     2.3 @@ -34,6 +34,7 @@ extern struct ir_ctrl *ir_ctrl;
     2.4  void print_iommu_regs(struct acpi_drhd_unit *drhd);
     2.5  void print_vtd_entries(struct domain *d, struct iommu *iommu,
     2.6                         int bus, int devfn, unsigned long gmfn);
     2.7 +void pdev_flr(u8 bus, u8 devfn);
     2.8  
     2.9  int qinval_setup(struct iommu *iommu);
    2.10  int queue_invalidate_context(struct iommu *iommu,
     3.1 --- a/xen/arch/x86/hvm/vmx/vtd/intel-iommu.c	Thu Jan 24 14:38:01 2008 +0000
     3.2 +++ b/xen/arch/x86/hvm/vmx/vtd/intel-iommu.c	Thu Jan 24 14:39:38 2008 +0000
     3.3 @@ -1481,6 +1481,7 @@ void return_devices_to_dom0(struct domai
     3.4          dprintk(XENLOG_INFO VTDPREFIX,
     3.5                  "return_devices_to_dom0: bdf = %x:%x:%x\n",
     3.6                  pdev->bus, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
     3.7 +        pdev_flr(pdev->bus, pdev->devfn);
     3.8          reassign_device_ownership(d, dom0, pdev->bus, pdev->devfn);
     3.9      }
    3.10  
    3.11 @@ -1929,6 +1930,7 @@ int assign_device(struct domain *d, u8 b
    3.12               "assign_device: bus = %x dev = %x func = %x\n",
    3.13               bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
    3.14  
    3.15 +    pdev_flr(bus, devfn);
    3.16      reassign_device_ownership(dom0, d, bus, devfn);
    3.17  
    3.18      /* Setup rmrr identify mapping */
     4.1 --- a/xen/arch/x86/hvm/vmx/vtd/utils.c	Thu Jan 24 14:38:01 2008 +0000
     4.2 +++ b/xen/arch/x86/hvm/vmx/vtd/utils.c	Thu Jan 24 14:39:38 2008 +0000
     4.3 @@ -22,7 +22,7 @@
     4.4  #include <xen/irq.h>
     4.5  #include <xen/spinlock.h>
     4.6  #include <xen/sched.h>
     4.7 -#include <asm/delay.h>
     4.8 +#include <xen/delay.h>
     4.9  #include <asm/iommu.h>
    4.10  #include <asm/hvm/vmx/intel-iommu.h>
    4.11  #include "dmar.h"
    4.12 @@ -93,6 +93,101 @@ void disable_pmr(struct iommu *iommu)
    4.13              "Disabled protected memory registers\n");
    4.14  }
    4.15  
    4.16 +static u8 find_cap_offset(u8 bus, u8 dev, u8 func, u8 cap)
    4.17 +{
    4.18 +    u8 id;
    4.19 +    int max_cap = 48;
    4.20 +    u8 pos = PCI_CAPABILITY_LIST;
    4.21 +    u16 status;
    4.22 +
    4.23 +    status = read_pci_config_16(bus, dev, func, PCI_STATUS);
    4.24 +    if ( (status & PCI_STATUS_CAP_LIST) == 0 )
    4.25 +        return 0;
    4.26 +
    4.27 +    while ( max_cap-- )
    4.28 +    {
    4.29 +        pos = read_pci_config_byte(bus, dev, func, pos);
    4.30 +        if ( pos < 0x40 )
    4.31 +            break;
    4.32 +
    4.33 +        pos &= ~3;
    4.34 +        id = read_pci_config_byte(bus, dev, func, pos + PCI_CAP_LIST_ID);
    4.35 +
    4.36 +        if ( id == 0xff )
    4.37 +            break;
    4.38 +        else if ( id == cap )
    4.39 +            return pos;
    4.40 +
    4.41 +        pos += PCI_CAP_LIST_NEXT;
    4.42 +    }
    4.43 +
    4.44 +    return 0;
    4.45 +}
    4.46 +
    4.47 +#define PCI_D3hot   (3)
    4.48 +#define PCI_CONFIG_DWORD_SIZE   (64)
    4.49 +#define PCI_EXP_DEVCAP_FLR      (1 << 28)
    4.50 +#define PCI_EXP_DEVCTL_FLR      (1 << 15)
    4.51 +
    4.52 +void pdev_flr(u8 bus, u8 devfn)
    4.53 +{
    4.54 +    u8 pos;
    4.55 +    u32 dev_cap, dev_status, pm_ctl;
    4.56 +    int flr = 0;
    4.57 +    u8 dev = PCI_SLOT(devfn);
    4.58 +    u8 func = PCI_FUNC(devfn);
    4.59 +
    4.60 +    pos = find_cap_offset(bus, dev, func, PCI_CAP_ID_EXP);
    4.61 +    if ( pos != 0 )
    4.62 +    {
    4.63 +        dev_cap = read_pci_config(bus, dev, func, pos + PCI_EXP_DEVCAP);
    4.64 +        if ( dev_cap & PCI_EXP_DEVCAP_FLR )
    4.65 +        {
    4.66 +            write_pci_config(bus, dev, func,
    4.67 +                             pos + PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_FLR);
    4.68 +            do {
    4.69 +                dev_status = read_pci_config(bus, dev, func,
    4.70 +                                             pos + PCI_EXP_DEVSTA);
    4.71 +            } while ( dev_status & PCI_EXP_DEVSTA_TRPND );
    4.72 +
    4.73 +            flr = 1;
    4.74 +        }
    4.75 +    }
    4.76 +
    4.77 +    /* If this device doesn't support function level reset,
    4.78 +     * program device from D0 t0 D3hot, and then return to D0
    4.79 +     * to implement function level reset
    4.80 +     */
    4.81 +    if ( flr == 0 )
    4.82 +    {
    4.83 +        pos = find_cap_offset(bus, dev, func, PCI_CAP_ID_PM);
    4.84 +        if ( pos != 0 )
    4.85 +        {
    4.86 +            int i;
    4.87 +            u32 config[PCI_CONFIG_DWORD_SIZE];
    4.88 +            for ( i = 0; i < PCI_CONFIG_DWORD_SIZE; i++ )
    4.89 +                config[i] = read_pci_config(bus, dev, func, i*4);
    4.90 +
    4.91 +            /* Enter D3hot without soft reset */
    4.92 +            pm_ctl = read_pci_config(bus, dev, func, pos + PCI_PM_CTRL);
    4.93 +            pm_ctl |= PCI_PM_CTRL_NO_SOFT_RESET;
    4.94 +            pm_ctl &= ~PCI_PM_CTRL_STATE_MASK;
    4.95 +            pm_ctl |= PCI_D3hot;
    4.96 +            write_pci_config(bus, dev, func, pos + PCI_PM_CTRL, pm_ctl);
    4.97 +            mdelay(10);
    4.98 +
    4.99 +            /* From D3hot to D0 */
   4.100 +            write_pci_config(bus, dev, func, pos + PCI_PM_CTRL, 0);
   4.101 +            mdelay(10);
   4.102 +
   4.103 +            /* Write saved configurations to device */
   4.104 +            for ( i = 0; i < PCI_CONFIG_DWORD_SIZE; i++ )
   4.105 +                write_pci_config(bus, dev, func, i*4, config[i]);
   4.106 +
   4.107 +            flr = 1;
   4.108 +        }
   4.109 +    }
   4.110 +}
   4.111  
   4.112  void print_iommu_regs(struct acpi_drhd_unit *drhd)
   4.113  {