mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Christian König" <ckoenig.leichtzumerken@gmail.com>
To: linaro-mm-sig@lists.linaro.org, linux-media@vger.kernel.org,
	dri-devel@lists.freedesktop.org, amd-gfx@lists.freedesktop.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH 3/8] PCI: Add pci_peer_traffic_supported()
Date: Sun, 25 Mar 2018 12:59:55 +0200	[thread overview]
Message-ID: <20180325110000.2238-3-christian.koenig@amd.com> (raw)
In-Reply-To: <20180325110000.2238-1-christian.koenig@amd.com>

From: "wdavis@nvidia.com" <wdavis@nvidia.com>

Add checks for topology and ACS configuration to determine whether or not
peer traffic should be supported between two PCI devices.

Signed-off-by: Will Davis <wdavis@nvidia.com>
Signed-off-by: Christian König <christian.koenig@amd.com>
---
 drivers/pci/pci.c   | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/pci.h |   1 +
 2 files changed, 113 insertions(+)

diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index f6a4dd10d9b0..efdca3c9dad1 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -28,6 +28,7 @@
 #include <linux/device.h>
 #include <linux/pm_runtime.h>
 #include <linux/pci_hotplug.h>
+#include <linux/iommu.h>
 #include <linux/vmalloc.h>
 #include <linux/pci-ats.h>
 #include <asm/setup.h>
@@ -5285,6 +5286,117 @@ void pci_ignore_hotplug(struct pci_dev *dev)
 }
 EXPORT_SYMBOL_GPL(pci_ignore_hotplug);
 
+static bool pci_peer_host_traffic_supported(struct pci_host_bridge *host)
+{
+	struct pci_dev *bridge = pci_get_slot(host->bus, PCI_DEVFN(0, 0));
+	unsigned short vendor, device;
+
+	if (!bridge)
+		return false;
+
+	vendor = bridge->vendor;
+	device = bridge->device;
+	pci_dev_put(bridge);
+
+	/* AMD ZEN host bridges can do peer to peer */
+	if (vendor == PCI_VENDOR_ID_AMD && device == 0x1450)
+		return true;
+
+	/* TODO: Extend that to a proper whitelist */
+	return false;
+}
+
+bool pci_peer_traffic_supported(struct pci_dev *dev, struct pci_dev *peer)
+{
+	struct pci_dev *rp_dev, *rp_peer, *common_upstream;
+	struct pci_host_bridge *peer_host_bridge;
+	struct pci_host_bridge *dev_host_bridge;
+	int pos;
+	u16 cap;
+
+	/* This is tricky enough, focus on PCIe for now */
+	if (!pci_is_pcie(dev) || !pci_is_pcie(peer))
+		return false;
+
+	/*
+	 * Disallow the peer-to-peer traffic if the devices do not share a
+	 * host bridge. The PCI specifications does not make any guarantees
+	 * about P2P capabilities between devices under separate domains.
+	 *
+	 * PCI Local Bus Specification Revision 3.0, section 3.10:
+	 *    "Peer-to-peer transactions crossing multiple host bridges
+	 *     PCI host bridges may, but are not required to, support PCI
+	 *     peer-to-peer transactions that traverse multiple PCI host
+	 *     bridges."
+	 */
+	dev_host_bridge = pci_find_host_bridge(dev->bus);
+	peer_host_bridge = pci_find_host_bridge(peer->bus);
+	if (dev_host_bridge != peer_host_bridge)
+		return pci_peer_host_traffic_supported(dev_host_bridge);
+
+	rp_dev = pcie_find_root_port(dev);
+	rp_peer = pcie_find_root_port(peer);
+	common_upstream = pci_find_common_upstream_dev(dev, peer);
+
+	/*
+	 * Access Control Services (ACS) Checks
+	 *
+	 * ACS has a capability bit for P2P Request Redirects (RR),
+	 * but unfortunately it doesn't tell us much about the real
+	 * capabilities of the hardware.
+	 *
+	 * PCI Express Base Specification Revision 3.0, section
+	 * 6.12.1.1:
+	 *    "ACS P2P Request Redirect: must be implemented by Root
+	 *     Ports that support peer-to-peer traffic with other
+	 *     Root Ports; [80]"
+	 * but
+	 *    "[80] Root Port indication of ACS P2P Request Redirect
+	 *     or ACS P2P Completion Redirect support does not imply
+	 *     any particular level of peer-to-peer support by the
+	 *     Root Complex, or that peer-to-peer traffic is
+	 *     supported at all"
+	 */
+
+	/*
+	 * If ACS is not implemented, we have no idea about P2P
+	 * support. Optimistically allow this if there is a common
+	 * upstream device.
+	 */
+	pos = pci_find_ext_capability(rp_dev, PCI_EXT_CAP_ID_ACS);
+	if (!pos)
+		return common_upstream != NULL;
+
+	/*
+	 * If the devices are under the same root port and have a common
+	 * upstream device, allow if the root port is further upstream
+	 * from the common upstream device and the common upstream
+	 * device has Upstream Forwarding disabled, or if the root port
+	 * is the common upstream device and ACS is not implemented.
+	 */
+	pci_read_config_word(rp_dev, pos + PCI_ACS_CAP, &cap);
+	if ((rp_dev == rp_peer && common_upstream) &&
+	    (((common_upstream != rp_dev) &&
+	      !pci_acs_enabled(common_upstream, PCI_ACS_UF)) ||
+	     ((common_upstream == rp_dev) && ((cap & PCI_ACS_RR) == 0))))
+		return true;
+
+	/*
+	 * If ACS RR is implemented and disabled, allow only if the
+	 * devices are under the same root port.
+	 */
+	if (cap & PCI_ACS_RR && !pci_acs_enabled(rp_dev, PCI_ACS_RR))
+		return rp_dev == rp_peer;
+
+	/*
+	 * If ACS RR is not implemented, or is implemented and enabled,
+	 * only allow if there's a translation agent enabled to do the
+	 * redirect.
+	 */
+	return iommu_present(&pci_bus_type);
+}
+EXPORT_SYMBOL(pci_peer_traffic_supported);
+
 resource_size_t __weak pcibios_default_alignment(void)
 {
 	return 0;
diff --git a/include/linux/pci.h b/include/linux/pci.h
index 0d29f5cdcb04..3c8eaa505991 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -921,6 +921,7 @@ void pci_remove_root_bus(struct pci_bus *bus);
 void pci_setup_cardbus(struct pci_bus *bus);
 void pcibios_setup_bridge(struct pci_bus *bus, unsigned long type);
 void pci_sort_breadthfirst(void);
+bool pci_peer_traffic_supported(struct pci_dev *dev, struct pci_dev *peer);
 #define dev_is_pci(d) ((d)->bus == &pci_bus_type)
 #define dev_is_pf(d) ((dev_is_pci(d) ? to_pci_dev(d)->is_physfn : false))
 
-- 
2.14.1

  parent reply	other threads:[~2018-03-25 11:00 UTC|newest]

Thread overview: 83+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-25 10:59 [PATCH 1/8] lib/scatterlist: add sg_set_dma_addr() helper Christian König
2018-03-25 10:59 ` [PATCH 2/8] PCI: Add pci_find_common_upstream_dev() Christian König
2018-03-28 12:38   ` Christoph Hellwig
2018-03-28 15:07     ` Christian König
2018-03-28 15:47       ` Logan Gunthorpe
2018-03-28 16:02         ` Christian König
2018-03-28 16:25           ` Logan Gunthorpe
2018-03-28 18:28             ` Christian König
2018-03-28 18:57               ` Logan Gunthorpe
2018-03-28 19:44                 ` Christian König
2018-03-28 19:53                   ` Logan Gunthorpe
2018-03-29 11:44                     ` Christian König
2018-03-29 15:45                       ` Logan Gunthorpe
2018-03-29 16:10                         ` Christian König
2018-03-29 16:25                           ` Logan Gunthorpe
2018-03-29 18:15                             ` Christian König
2018-03-30  1:58                             ` Jerome Glisse
2018-03-30  6:33                               ` Christoph Hellwig
2018-03-30 15:25                                 ` Jerome Glisse
2018-03-30 18:46                               ` Logan Gunthorpe
2018-03-30 19:45                                 ` Jerome Glisse
2018-04-02 17:02                                   ` Logan Gunthorpe
2018-04-02 17:20                                     ` Jerome Glisse
2018-04-02 17:37                                       ` Logan Gunthorpe
2018-04-02 19:16                                         ` Jerome Glisse
2018-04-02 19:32                                           ` Logan Gunthorpe
2018-04-02 19:45                                             ` Jerome Glisse
     [not found]                     ` <CADnq5_P-z=Noos_jaME9_CERri3C-m2hPPvx2bArr36O=1FnrA@mail.gmail.com>
2018-03-29 14:37                       ` Alex Deucher
2018-03-25 10:59 ` Christian König [this message]
2018-03-25 10:59 ` [PATCH 4/8] dma-buf: add peer2peer flag Christian König
2018-03-29  6:57   ` Daniel Vetter
2018-03-29 11:34     ` Christian König
2018-04-03  9:09       ` Daniel Vetter
2018-04-03 17:06         ` Jerome Glisse
2018-04-03 18:08           ` Daniel Vetter
2018-04-16 12:39             ` Christoph Hellwig
2018-04-16 13:38               ` Daniel Vetter
2018-04-19  8:16                 ` Christoph Hellwig
2018-04-20  7:13                   ` Daniel Vetter
2018-04-20  8:58                     ` Christian König
2018-04-20 10:17                       ` Christoph Hellwig
2018-04-20 10:44                         ` Christian König
2018-04-20 12:46                           ` Christoph Hellwig
2018-04-20 15:21                             ` [Linaro-mm-sig] " Daniel Vetter
2018-04-24 18:48                               ` Christoph Hellwig
2018-04-24 19:32                                 ` Daniel Vetter
2018-04-25  5:48                                   ` Christoph Hellwig
2018-04-25  6:10                                     ` Alex Deucher
2018-04-25  6:13                                     ` Daniel Vetter
2018-04-25  6:23                                       ` Daniel Vetter
2018-04-25  6:43                                         ` Christoph Hellwig
2018-04-25  7:02                                           ` Daniel Vetter
2018-04-25  7:09                                             ` Christoph Hellwig
2018-04-25  7:30                                               ` Daniel Vetter
2018-04-25  7:56                                                 ` Thierry Reding
2018-04-25  8:55                                                   ` Christoph Hellwig
2018-04-25  7:43                                               ` Thierry Reding
2018-04-25  7:41                                           ` Thierry Reding
2018-04-25  8:54                                             ` noveau vs arm dma ops Christoph Hellwig
2018-04-25  9:25                                               ` Russell King - ARM Linux
2018-04-25 10:04                                               ` Daniel Vetter
2018-04-25 15:33                                                 ` Christoph Hellwig
2018-04-25 21:35                                                   ` Daniel Vetter
2018-04-25 23:26                                                     ` Russell King - ARM Linux
2018-04-26  9:17                                                       ` Daniel Vetter
2018-04-26  9:09                                                     ` Christoph Hellwig
2018-04-26  9:45                                                       ` Daniel Vetter
2018-04-26 11:12                                                       ` Russell King - ARM Linux
2018-04-25 22:54                                                   ` Russell King - ARM Linux
2018-04-26  9:13                                                     ` Christoph Hellwig
2018-04-26  9:20                                                     ` [Linaro-mm-sig] " Daniel Vetter
2018-04-26  9:24                                                       ` Christoph Hellwig
2018-04-26  9:39                                                         ` Daniel Vetter
2018-04-25  6:24                                       ` [Linaro-mm-sig] [PATCH 4/8] dma-buf: add peer2peer flag Alex Deucher
2018-04-25  6:41                                         ` Christoph Hellwig
2018-04-25 17:44                                           ` Alex Deucher
2018-04-25 18:38                                             ` Dan Williams
2018-05-04 12:45                                             ` Lucas Stach
2018-03-25 10:59 ` [PATCH 5/8] drm/amdgpu: print DMA-buf status in debugfs Christian König
2018-03-25 10:59 ` [PATCH 6/8] drm/amdgpu: note that we can handle peer2peer DMA-buf Christian König
2018-03-25 10:59 ` [PATCH 7/8] drm/amdgpu: add amdgpu_gem_attach Christian König
2018-03-25 11:00 ` [PATCH 8/8] drm/amdgpu: add support for exporting VRAM using DMA-buf Christian König
2018-03-28 12:37 ` [PATCH 1/8] lib/scatterlist: add sg_set_dma_addr() helper Christoph Hellwig

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180325110000.2238-3-christian.koenig@amd.com \
    --to=ckoenig.leichtzumerken@gmail.com \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=linaro-mm-sig@lists.linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®