From patchwork Fri Oct 21 17:00:33 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ido Yariv X-Patchwork-Id: 9389673 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 4822B60762 for ; Fri, 21 Oct 2016 17:01:26 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2DD102A1D9 for ; Fri, 21 Oct 2016 17:01:26 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2273F2A1F0; Fri, 21 Oct 2016 17:01:26 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 2851E2A1D9 for ; Fri, 21 Oct 2016 17:01:24 +0000 (UTC) Received: from localhost ([::1]:33415 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bxdC7-0002Kj-6Z for patchwork-qemu-devel@patchwork.kernel.org; Fri, 21 Oct 2016 13:01:23 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40897) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bxdBk-0002KH-Qu for qemu-devel@nongnu.org; Fri, 21 Oct 2016 13:01:01 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bxdBg-0000Gj-V0 for qemu-devel@nongnu.org; Fri, 21 Oct 2016 13:01:00 -0400 Received: from mail-pf0-x242.google.com ([2607:f8b0:400e:c00::242]:33377) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1bxdBg-0000G0-NU for qemu-devel@nongnu.org; Fri, 21 Oct 2016 13:00:56 -0400 Received: by mail-pf0-x242.google.com with SMTP id i85so9121811pfa.0 for ; Fri, 21 Oct 2016 10:00:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wizery-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id; bh=fwvzfxwQ+TQgwrKyiWqOh7XoPNUzim+wVjdKdGwPIIU=; b=D7n6rsVM6zZidP1AT6r8w/AWa1UUvNyGF7E6NLfniCt9grRGzi/lSsNFRWj5rx4WaU NuMQiVZEyzMy55p2q/d7HiQNCRvqxN/ESNRu2UxeYwTIwmk5J5d0yA7Leb89nd9tS9rq 3tUFFOI1yYaFJll0gj1Qdtc3PqC9T1Uw05wtBPNrDdWLXTFalfd+0Emzxf4G3J0QekrR fdyYeDeQbdBLIyJthoZoNuk9rzC0N5J7TYW8MG6GOuZQwZU+klFfgVU1bpU4aJWEJs0m +7ydLTdLMYZtUSmKzmketeisvQZVTbw2rB6QHop/27V/FbIIypd5c/NMpjEg89Ou1hZM 6jqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=fwvzfxwQ+TQgwrKyiWqOh7XoPNUzim+wVjdKdGwPIIU=; b=jaxcmSg8fkXQe1eXO69UTovPLttCiwGNauWZCDB8QbapBX5IQdrmdTzP+Hlzf80Z4E dCmElwj+uOdAkIGO6irw9uNXasxHIK1+SiBiS5m96xKOlnAhBQyP4DRUkXVUsOXM+twJ Z8bjsRps/RPMD+RWSouaU1vi2UwkubKwL6Awx4kUB6+6jWZwxMfaskePKmhwuP/t1lsg GmCnmfHyX8kSGkcuu5QMj/Q6Lmzo12Prd3yoC/CWnojTHT7sU2HiyYCYoP7CFrgMz69w xqhWy4EuKlSYh47FC4d/87Dq+Amklx4SbsOd5Yd8G2YzKm5PNHpiMPrMkEf9afJuK9N0 naLQ== X-Gm-Message-State: ABUngvdWYNRdJBBeaU+7yl+YbWKRupfpdLFPSYpK64srC+jpGfXwipK+dxzhxRmjWK1ZkA== X-Received: by 10.99.167.15 with SMTP id d15mr2800721pgf.104.1477069254928; Fri, 21 Oct 2016 10:00:54 -0700 (PDT) Received: from WorkStation.amr.corp.intel.com (pool-173-70-53-223.nwrknj.fios.verizon.net. [173.70.53.223]) by smtp.gmail.com with ESMTPSA id 136sm6469236pfu.87.2016.10.21.10.00.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 21 Oct 2016 10:00:54 -0700 (PDT) From: Ido Yariv To: Alex Williamson Date: Fri, 21 Oct 2016 13:00:33 -0400 Message-Id: <1477069233-19180-1-git-send-email-ido@wizery.com> X-Mailer: git-send-email 2.5.5 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400e:c00::242 Subject: [Qemu-devel] [PATCH] vfio/pci: fix out-of-sync BAR information on reset X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: qemu-devel@nongnu.org, Ido Yariv Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP When a PCI device is reset, pci_do_device_reset resets all BAR addresses in the relevant PCIDevice's config buffer. The VFIO configuration space stays untouched, so the guest OS may choose to skip restoring the BAR addresses as they would seem intact. The PCI device may be left non-operational. One example of such a scenario is when the guest exits S3. Fix this by resetting the BAR addresses in the VFIO configuration space as well. Signed-off-by: Ido Yariv --- hw/vfio/pci.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index 65d30fd..9e1dee0 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -1922,11 +1922,41 @@ static void vfio_pci_pre_reset(VFIOPCIDevice *vdev) static void vfio_pci_post_reset(VFIOPCIDevice *vdev) { Error *err = NULL; + int nr; vfio_intx_enable(vdev, &err); if (err) { error_reportf_err(err, ERR_PREFIX, vdev->vbasedev.name); } + + for (nr = 0; nr < PCI_NUM_REGIONS - 1; ++nr) { + VFIOBAR *bar = &vdev->bars[nr]; + off_t addr = vdev->config_offset + PCI_BASE_ADDRESS_0 + (4 * nr); + uint32_t org = 0; + uint64_t val = 0; + uint32_t len; + + if (!bar->region.size) { + continue; + } + + if (pread(vdev->vbasedev.fd, &org, sizeof(org), addr) < 0) { + error_report("%s(%s) read bar %d failed: %m", __func__, + vdev->vbasedev.name, nr); + continue; + } + + val = le32_to_cpu(org); + val &= (bar->ioport ? ~PCI_BASE_ADDRESS_IO_MASK : + ~PCI_BASE_ADDRESS_MEM_MASK); + val = cpu_to_le64(val); + + len = bar->mem64 ? sizeof(uint64_t) : sizeof(uint32_t); + if (pwrite(vdev->vbasedev.fd, &val, len, addr) != len) { + error_report("%s(%s) reset bar %d failed: %m", __func__, + vdev->vbasedev.name, nr); + } + } } static bool vfio_pci_host_match(PCIHostDeviceAddress *addr, const char *name)