2007-06-19 22:19:07

by Keshavamurthy, Anil S

[permalink] [raw]
Subject: [Intel IOMMU 02/10] PCI generic helper function

When devices are under a p2p bridge, upstream
transactions get replaced by the device id of the bridge as it owns the
PCIE transaction. Hence its necessary to setup translations on behalf of the
bridge as well. Due to this limitation all devices under a p2p share the same
domain in a DMAR.

We just cache the type of device, if its a native PCIe device
or not for later use.

changes from previous posting
1) fixed mostly coding style issues nothing major

Signed-off-by: Anil S Keshavamurthy <[email protected]>
---
drivers/pci/pci.h | 1 +
drivers/pci/probe.c | 14 ++++++++++++++
drivers/pci/search.c | 30 ++++++++++++++++++++++++++++++
include/linux/pci.h | 2 ++
4 files changed, 47 insertions(+)

Index: linux-2.6.22-rc4-mm2/drivers/pci/pci.h
===================================================================
--- linux-2.6.22-rc4-mm2.orig/drivers/pci/pci.h 2007-06-18 15:44:45.000000000 -0700
+++ linux-2.6.22-rc4-mm2/drivers/pci/pci.h 2007-06-18 15:45:07.000000000 -0700
@@ -92,3 +92,4 @@
return NULL;
}

+struct pci_dev *pci_find_upstream_pcie_bridge(struct pci_dev *pdev);
Index: linux-2.6.22-rc4-mm2/drivers/pci/probe.c
===================================================================
--- linux-2.6.22-rc4-mm2.orig/drivers/pci/probe.c 2007-06-18 15:44:45.000000000 -0700
+++ linux-2.6.22-rc4-mm2/drivers/pci/probe.c 2007-06-18 15:45:07.000000000 -0700
@@ -851,6 +851,19 @@
kfree(pci_dev);
}

+static void set_pcie_port_type(struct pci_dev *pdev)
+{
+ int pos;
+ u16 reg16;
+
+ pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
+ if (!pos)
+ return;
+ pdev->is_pcie = 1;
+ pci_read_config_word(pdev, pos + PCI_EXP_FLAGS, &reg16);
+ pdev->pcie_type = (reg16 & PCI_EXP_FLAGS_TYPE) >> 4;
+}
+
/**
* pci_cfg_space_size - get the configuration space size of the PCI device.
* @dev: PCI device
@@ -965,6 +978,7 @@
dev->device = (l >> 16) & 0xffff;
dev->cfg_size = pci_cfg_space_size(dev);
dev->error_state = pci_channel_io_normal;
+ set_pcie_port_type(dev);

/* Assume 32-bit PCI; let 64-bit PCI cards (which are far rarer)
set this higher, assuming the system even supports it. */
Index: linux-2.6.22-rc4-mm2/drivers/pci/search.c
===================================================================
--- linux-2.6.22-rc4-mm2.orig/drivers/pci/search.c 2007-06-18 15:44:45.000000000 -0700
+++ linux-2.6.22-rc4-mm2/drivers/pci/search.c 2007-06-18 15:45:07.000000000 -0700
@@ -14,6 +14,36 @@
#include "pci.h"

DECLARE_RWSEM(pci_bus_sem);
+/*
+ * find the upstream PCIE-to-PCI bridge of a PCI device
+ * if the device is PCIE, return NULL
+ * if the device isn't connected to a PCIE bridge (that is its parent is a
+ * legacy PCI bridge and the bridge is directly connected to bus 0), return its
+ * parent
+ */
+struct pci_dev *
+pci_find_upstream_pcie_bridge(struct pci_dev *pdev)
+{
+ struct pci_dev *tmp = NULL;
+
+ if (pdev->is_pcie)
+ return NULL;
+ while (1) {
+ if (!pdev->bus->self)
+ break;
+ pdev = pdev->bus->self;
+ /* a p2p bridge */
+ if (!pdev->is_pcie) {
+ tmp = pdev;
+ continue;
+ }
+ /* PCI device should connect to a PCIE bridge */
+ BUG_ON(pdev->pcie_type != PCI_EXP_TYPE_PCI_BRIDGE);
+ return pdev;
+ }
+
+ return tmp;
+}

static struct pci_bus *pci_do_find_bus(struct pci_bus *bus, unsigned char busnr)
{
Index: linux-2.6.22-rc4-mm2/include/linux/pci.h
===================================================================
--- linux-2.6.22-rc4-mm2.orig/include/linux/pci.h 2007-06-18 15:44:45.000000000 -0700
+++ linux-2.6.22-rc4-mm2/include/linux/pci.h 2007-06-18 15:45:07.000000000 -0700
@@ -140,6 +140,7 @@
unsigned short subsystem_device;
unsigned int class; /* 3 bytes: (base,sub,prog-if) */
u8 hdr_type; /* PCI header type (`multi' flag masked out) */
+ u8 pcie_type; /* PCI-E device/port type */
u8 rom_base_reg; /* which config register controls the ROM */
u8 pin; /* which interrupt pin this device uses */

@@ -182,6 +183,7 @@
unsigned int msi_enabled:1;
unsigned int msix_enabled:1;
unsigned int is_managed:1;
+ unsigned int is_pcie:1;
atomic_t enable_cnt; /* pci_enable_device has been called */

u32 saved_config_space[16]; /* config space saved at suspend time */

--


2007-06-26 05:50:05

by Andrew Morton

[permalink] [raw]
Subject: Re: [Intel IOMMU 02/10] PCI generic helper function

On Tue, 19 Jun 2007 14:37:03 -0700 "Keshavamurthy, Anil S" <[email protected]> wrote:

> +struct pci_dev *
> +pci_find_upstream_pcie_bridge(struct pci_dev *pdev)

You didn't need a newline there, but that's what the rest of that file
does. Hu hum.

> +{
> + struct pci_dev *tmp = NULL;
> +
> + if (pdev->is_pcie)
> + return NULL;
> + while (1) {
> + if (!pdev->bus->self)
> + break;
> + pdev = pdev->bus->self;
> + /* a p2p bridge */
> + if (!pdev->is_pcie) {
> + tmp = pdev;
> + continue;
> + }
> + /* PCI device should connect to a PCIE bridge */
> + BUG_ON(pdev->pcie_type != PCI_EXP_TYPE_PCI_BRIDGE);

I assume that if this bug triggers, we've found some broken hardware?

Going BUG seems like a pretty rude reaction to this, especially when it
would be so easy to drop a warning and then recover.


How's about this?

--- a/drivers/pci/search.c~intel-iommu-pci-generic-helper-function-fix
+++ a/drivers/pci/search.c
@@ -38,7 +38,11 @@ pci_find_upstream_pcie_bridge(struct pci
continue;
}
/* PCI device should connect to a PCIE bridge */
- BUG_ON(pdev->pcie_type != PCI_EXP_TYPE_PCI_BRIDGE);
+ if (pdev->pcie_type != PCI_EXP_TYPE_PCI_BRIDGE) {
+ /* Busted hardware? */
+ WARN_ON_ONCE(1);
+ return NULL;
+ }
return pdev;
}


2007-06-26 14:49:34

by Keshavamurthy, Anil S

[permalink] [raw]
Subject: Re: [Intel IOMMU 02/10] PCI generic helper function

On Mon, Jun 25, 2007 at 10:49:37PM -0700, Andrew Morton wrote:
> On Tue, 19 Jun 2007 14:37:03 -0700 "Keshavamurthy, Anil S" <[email protected]> wrote:
>
> > +struct pci_dev *
> > +pci_find_upstream_pcie_bridge(struct pci_dev *pdev)
>
> You didn't need a newline there, but that's what the rest of that file
> does. Hu hum.
>
> > +{
> > + struct pci_dev *tmp = NULL;
> > +
> > + if (pdev->is_pcie)
> > + return NULL;
> > + while (1) {
> > + if (!pdev->bus->self)
> > + break;
> > + pdev = pdev->bus->self;
> > + /* a p2p bridge */
> > + if (!pdev->is_pcie) {
> > + tmp = pdev;
> > + continue;
> > + }
> > + /* PCI device should connect to a PCIE bridge */
> > + BUG_ON(pdev->pcie_type != PCI_EXP_TYPE_PCI_BRIDGE);
>
> I assume that if this bug triggers, we've found some broken hardware?
>
> Going BUG seems like a pretty rude reaction to this, especially when it
> would be so easy to drop a warning and then recover.
>
>
> How's about this?
Looks good, thanks.

>
> --- a/drivers/pci/search.c~intel-iommu-pci-generic-helper-function-fix
> +++ a/drivers/pci/search.c
> @@ -38,7 +38,11 @@ pci_find_upstream_pcie_bridge(struct pci
> continue;
> }
> /* PCI device should connect to a PCIE bridge */
> - BUG_ON(pdev->pcie_type != PCI_EXP_TYPE_PCI_BRIDGE);
> + if (pdev->pcie_type != PCI_EXP_TYPE_PCI_BRIDGE) {
> + /* Busted hardware? */
> + WARN_ON_ONCE(1);
> + return NULL;
> + }
> return pdev;
> }
>