From patchwork Fri May 11 22:56:02 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Williamson X-Patchwork-Id: 158658 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 160A7B7020 for ; Sat, 12 May 2012 10:45:45 +1000 (EST) Received: from localhost ([::1]:39255 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ST0Ss-0004NH-RH for incoming@patchwork.ozlabs.org; Fri, 11 May 2012 20:45:42 -0400 Received: from eggs.gnu.org ([208.118.235.92]:37357) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SSyl1-0001j7-Pe for qemu-devel@nongnu.org; Fri, 11 May 2012 18:56:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SSykz-00084I-Ua for qemu-devel@nongnu.org; Fri, 11 May 2012 18:56:19 -0400 Received: from mx1.redhat.com ([209.132.183.28]:1566) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SSykz-00084E-MD for qemu-devel@nongnu.org; Fri, 11 May 2012 18:56:17 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q4BMu4ol005184 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 11 May 2012 18:56:04 -0400 Received: from bling.home ([10.3.113.8]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id q4BMu2G2003190; Fri, 11 May 2012 18:56:02 -0400 From: Alex Williamson To: benh@kernel.crashing.org, aik@ozlabs.ru, david@gibson.dropbear.id.au, joerg.roedel@amd.com, dwmw2@infradead.org Date: Fri, 11 May 2012 16:56:02 -0600 Message-ID: <20120511225602.30496.80438.stgit@bling.home> In-Reply-To: <20120511222148.30496.68571.stgit@bling.home> References: <20120511222148.30496.68571.stgit@bling.home> User-Agent: StGIT/0.14.3 MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.132.183.28 X-Mailman-Approved-At: Fri, 11 May 2012 20:45:24 -0400 Cc: aafabbri@cisco.com, alex.williamson@redhat.com, kvm@vger.kernel.org, B07421@freescale.com, linux-pci@vger.kernel.org, konrad.wilk@oracle.com, agraf@suse.de, qemu-devel@nongnu.org, chrisw@sous-sol.org, B08248@freescale.com, iommu@lists.linux-foundation.org, avi@redhat.com, gregkh@linuxfoundation.org, bhelgaas@google.com, linux-kernel@vger.kernel.org, benve@cisco.com Subject: [Qemu-devel] [PATCH 05/13] pci: New pci_acs_enabled() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org In a PCIe environment, transactions aren't always required to reach the root bus before being re-routed. Peer-to-peer DMA may actually not be seen by the IOMMU in these cases. For IOMMU groups, we want to provide IOMMU drivers a way to detect these restrictions. Provided with a PCI device, pci_acs_enabled returns the furthest downstream device with a complete PCI ACS chain. This information can then be used in grouping to create fully isolated groups. ACS chain logic extracted from libvirt. Signed-off-by: Alex Williamson --- drivers/pci/pci.c | 43 +++++++++++++++++++++++++++++++++++++++++++ include/linux/pci.h | 1 + 2 files changed, 44 insertions(+), 0 deletions(-) diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index 111569c..d7f05ce 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -2358,6 +2358,49 @@ void pci_enable_acs(struct pci_dev *dev) pci_write_config_word(dev, pos + PCI_ACS_CTRL, ctrl); } +#define PCI_EXT_CAP_ACS_ENABLED (PCI_ACS_SV | PCI_ACS_RR | \ + PCI_ACS_CR | PCI_ACS_UF) + +/** + * pci_acs_enabled - test ACS support in downstream chain + * @dev: starting PCI device + * + * Returns the furthest downstream device with an unbroken ACS chain. If + * ACS is enabled throughout the chain, the returned device is the same as + * the one passed in. + */ +struct pci_dev *pci_acs_enabled(struct pci_dev *dev) +{ + struct pci_dev *acs_dev; + int pos; + u16 ctrl; + + if (!pci_is_root_bus(dev->bus)) + acs_dev = pci_acs_enabled(dev->bus->self); + else + return dev; + + /* If the chain is already broken, pass on the device */ + if (acs_dev != dev->bus->self) + return acs_dev; + + if (!pci_is_pcie(dev) || (dev->class >> 8) != PCI_CLASS_BRIDGE_PCI) + return dev; + + if (dev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) + return dev; + + pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ACS); + if (!pos) + return acs_dev; + + pci_read_config_word(dev, pos + PCI_ACS_CTRL, &ctrl); + if ((ctrl & PCI_EXT_CAP_ACS_ENABLED) != PCI_EXT_CAP_ACS_ENABLED) + return acs_dev; + + return dev; +} + /** * pci_swizzle_interrupt_pin - swizzle INTx for device behind bridge * @dev: the PCI device diff --git a/include/linux/pci.h b/include/linux/pci.h index 9910b5c..dc25da3 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -1586,6 +1586,7 @@ static inline bool pci_is_pcie(struct pci_dev *dev) } void pci_request_acs(void); +struct pci_dev *pci_acs_enabled(struct pci_dev *dev); #define PCI_VPD_LRDT 0x80 /* Large Resource Data Type */