From patchwork Wed Jun 5 16:35:00 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Michael S. Tsirkin" X-Patchwork-Id: 249138 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 1211F2C007B for ; Thu, 6 Jun 2013 02:35:01 +1000 (EST) Received: from localhost ([::1]:53605 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UkGfq-0006CA-Bk for incoming@patchwork.ozlabs.org; Wed, 05 Jun 2013 12:34:58 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53591) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UkGfV-0006Bx-DE for qemu-devel@nongnu.org; Wed, 05 Jun 2013 12:34:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UkGfP-0005yh-7w for qemu-devel@nongnu.org; Wed, 05 Jun 2013 12:34:37 -0400 Received: from mx1.redhat.com ([209.132.183.28]:47818) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UkGfO-0005yc-VL for qemu-devel@nongnu.org; Wed, 05 Jun 2013 12:34:31 -0400 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r55GYS8V008599 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 5 Jun 2013 12:34:28 -0400 Received: from redhat.com (vpn-203-36.tlv.redhat.com [10.35.203.36]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id r55GYOvI024907; Wed, 5 Jun 2013 12:34:25 -0400 Date: Wed, 5 Jun 2013 19:35:00 +0300 From: "Michael S. Tsirkin" To: Rusty Russell , Stephen Hemminger , Jason Wang , Paolo Bonzini , virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, qemu-devel@nongnu.org Message-ID: <20130605163500.GA3393@redhat.com> MIME-Version: 1.0 Content-Disposition: inline X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH RFC] virtio-pci: support config layout in BAR1 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 Some setups don't support enabling BAR0 (IO BAR). Reasons range from CPU limitations (e.g. on some powerpc setups) to architecture limmitations (e.g. a setup with >15 PCI bridges, with one virtio device behind each, on x86). PCI Express spec made IO optional, so future guests will disable IO for a device in more and more configurations. This patch makes it possible for host to mirror the config in BAR1, such that these setups can work properly. Guests with old drivers can't be fixed, they will continue to work as well (or as bad) as they did previously. For this reason, changing revision id appears unnecessary - it would break setups that previously worked, partially. Future work on re-organizing layout won't conflict with this patch - it can use a different BAR or put config at an offset, or update revision. Signed-off-by: Michael S. Tsirkin --- drivers/virtio/virtio_pci.c | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/drivers/virtio/virtio_pci.c b/drivers/virtio/virtio_pci.c index a7ce730..03564fe 100644 --- a/drivers/virtio/virtio_pci.c +++ b/drivers/virtio/virtio_pci.c @@ -675,6 +675,33 @@ static void virtio_pci_release_dev(struct device *_d) */ } +/* Map a BAR. But carefully: make sure we don't overlap the MSI-X table */ +static void __iomem * virtio_pci_iomap(struct pci_dev *pci_dev, int bar) +{ + int msix_cap = pci_find_capability(pci_dev, PCI_CAP_ID_MSIX); + if (msix_cap) { + u32 offset; + u8 bir; + pci_read_config_dword(pci_dev, msix_cap + PCI_MSIX_TABLE, + &offset); + bir = (u8)(offset & PCI_MSIX_TABLE_BIR); + offset &= PCI_MSIX_TABLE_OFFSET; + /* Spec says table offset is in a 4K page all by itself */ + if (bir == bar && offset < 4096) + return NULL; + + pci_read_config_dword(pci_dev, msix_cap + PCI_MSIX_PBA, + &offset); + bir = (u8)(offset & PCI_MSIX_PBA_BIR); + offset &= PCI_MSIX_PBA_OFFSET; + /* Spec says table offset is in a 4K page all by itself. */ + if (bir == bar && offset < 4096) + return NULL; + } + /* 4K is enough for all devices at the moment. */ + return pci_iomap(pci_dev, 0, 4096); +} + /* the PCI probing function */ static int virtio_pci_probe(struct pci_dev *pci_dev, const struct pci_device_id *id) @@ -716,7 +743,10 @@ static int virtio_pci_probe(struct pci_dev *pci_dev, if (err) goto out_enable_device; - vp_dev->ioaddr = pci_iomap(pci_dev, 0, 0); + vp_dev->ioaddr = virtio_pci_iomap(pci_dev, 0); + /* Failed to map BAR0? Try with BAR1. */ + if (vp_dev->ioaddr == NULL) + vp_dev->ioaddr = virtio_pci_iomap(pci_dev, 1); if (vp_dev->ioaddr == NULL) { err = -ENOMEM; goto out_req_regions;