From patchwork Mon Dec 22 20:43:57 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Williamson X-Patchwork-Id: 42553 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wi0-f199.google.com (mail-wi0-f199.google.com [209.85.212.199]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 8802F25BA1 for ; Mon, 22 Dec 2014 20:49:00 +0000 (UTC) Received: by mail-wi0-f199.google.com with SMTP id bs8sf3366200wib.10 for ; Mon, 22 Dec 2014 12:48:59 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:date:message-id:in-reply-to :references:user-agent:mime-version:content-type :content-transfer-encoding:cc:subject:precedence:list-id :list-unsubscribe:list-archive:list-post:list-help:list-subscribe :errors-to:sender:x-original-sender :x-original-authentication-results:mailing-list; bh=iEZ3k8goHd26bsjWyzsWmvWjUpSoiP1Cxt0fswCO/Mc=; b=UMhdvHEOHaWqG7S7AxKSFu/CcnlHFKtpG+oZQt3A41A55c9mAHt0BC5KPEbWp+jvnu WtE7nHtfGXw+7Fo71PgxBPwqbIF6krTg+RQRcAYaHcw20Rh4em9rn7QJxkXS6k7SSwLE ejo/udivJp2SV8Iti0ceU9MKoeuudSIEgFprPRFRUlxXnl0/TRKd+kZLUyJojBdiNvwz v1XdrOCj3TgRUb8Qzkjmdyzi2Q0eUOna0CYdaBXYDJD4mvkhLZuD9StBbsjN7cBeZmJi tN+NdFE9bVfKtNyovgvdvTU9oa9dXfRlWH7sVdJoLRwc8dxIrF5kqZDVhLeZisq8iwkk z2zA== X-Gm-Message-State: ALoCoQntZ3IOOPNMgHRfrCr5kIKJhyS4o7c6EmXat2wt+A3kqXIeGFGXHsKIMnPD4RNoHJP2U9Ow X-Received: by 10.112.89.36 with SMTP id bl4mr432135lbb.1.1419281339803; Mon, 22 Dec 2014 12:48:59 -0800 (PST) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.19.130 with SMTP id f2ls1471242lae.21.gmail; Mon, 22 Dec 2014 12:48:59 -0800 (PST) X-Received: by 10.152.44.193 with SMTP id g1mr23582266lam.15.1419281339531; Mon, 22 Dec 2014 12:48:59 -0800 (PST) Received: from mail-la0-f51.google.com (mail-la0-f51.google.com. [209.85.215.51]) by mx.google.com with ESMTPS id xf7si2168210lbb.80.2014.12.22.12.48.59 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 22 Dec 2014 12:48:59 -0800 (PST) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.51 as permitted sender) client-ip=209.85.215.51; Received: by mail-la0-f51.google.com with SMTP id ms9so4574183lab.38 for ; Mon, 22 Dec 2014 12:48:59 -0800 (PST) X-Received: by 10.112.84.225 with SMTP id c1mr2630211lbz.22.1419281339350; Mon, 22 Dec 2014 12:48:59 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.142.69 with SMTP id ru5csp1141158lbb; Mon, 22 Dec 2014 12:48:58 -0800 (PST) X-Received: by 10.224.45.69 with SMTP id d5mr40549882qaf.56.1419281337473; Mon, 22 Dec 2014 12:48:57 -0800 (PST) Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id h10si21115261qgh.119.2014.12.22.12.48.56 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Mon, 22 Dec 2014 12:48:57 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Received: from localhost ([::1]:41895 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Y39uS-0006KH-H7 for patch@linaro.org; Mon, 22 Dec 2014 15:48:56 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37937) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Y39pl-0006yi-Lm for qemu-devel@nongnu.org; Mon, 22 Dec 2014 15:44:10 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Y39ph-0007kk-1n for qemu-devel@nongnu.org; Mon, 22 Dec 2014 15:44:05 -0500 Received: from mx1.redhat.com ([209.132.183.28]:35731) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Y39pg-0007ka-QV for qemu-devel@nongnu.org; Mon, 22 Dec 2014 15:44:00 -0500 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id sBMKhwSx018482 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL) for ; Mon, 22 Dec 2014 15:43:59 -0500 Received: from bling.home (ovpn-113-45.phx2.redhat.com [10.3.113.45]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id sBMKhvwB002316; Mon, 22 Dec 2014 15:43:58 -0500 From: Alex Williamson To: qemu-devel@nongnu.org Date: Mon, 22 Dec 2014 13:43:57 -0700 Message-ID: <20141222204357.31398.80700.stgit@bling.home> In-Reply-To: <20141222204144.31398.31803.stgit@bling.home> References: <20141222204144.31398.31803.stgit@bling.home> User-Agent: StGit/0.17-dirty MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: alex.williamson@redhat.com Subject: [Qemu-devel] [PULL 08/14] hw/vfio/pci: handle reset at VFIODevice 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+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: alex.williamson@redhat.com X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.51 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 From: Eric Auger Since we can potentially have both PCI and platform devices in the same VFIO group, this latter now owns a list of VFIODevices. A unified reset handler, vfio_reset_handler, is registered, looping through this VFIODevice list. 2 specialized operations are introduced (vfio_compute_needs_reset and vfio_hot_reset_multi): they allow to implement type specific behavior. also reset_works and needs_reset VFIOPCIDevice fields are moved into VFIODevice. Signed-off-by: Eric Auger Signed-off-by: Alex Williamson --- hw/vfio/pci.c | 93 +++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 63 insertions(+), 30 deletions(-) diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index be7b8ff..4ac5ad6 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -189,13 +189,24 @@ typedef struct VFIOMSIXInfo { void *mmap; } VFIOMSIXInfo; +typedef struct VFIODeviceOps VFIODeviceOps; + typedef struct VFIODevice { + QLIST_ENTRY(VFIODevice) next; struct VFIOGroup *group; char *name; int fd; int type; + bool reset_works; + bool needs_reset; + VFIODeviceOps *ops; } VFIODevice; +struct VFIODeviceOps { + void (*vfio_compute_needs_reset)(VFIODevice *vdev); + int (*vfio_hot_reset_multi)(VFIODevice *vdev); +}; + typedef struct VFIOPCIDevice { PCIDevice pdev; VFIODevice vbasedev; @@ -214,19 +225,16 @@ typedef struct VFIOPCIDevice { VFIOBAR bars[PCI_NUM_REGIONS - 1]; /* No ROM */ VFIOVGA vga; /* 0xa0000, 0x3b0, 0x3c0 */ PCIHostDeviceAddress host; - QLIST_ENTRY(VFIOPCIDevice) next; EventNotifier err_notifier; uint32_t features; #define VFIO_FEATURE_ENABLE_VGA_BIT 0 #define VFIO_FEATURE_ENABLE_VGA (1 << VFIO_FEATURE_ENABLE_VGA_BIT) int32_t bootindex; uint8_t pm_cap; - bool reset_works; bool has_vga; bool pci_aer; bool has_flr; bool has_pm_reset; - bool needs_reset; bool rom_read_failed; } VFIOPCIDevice; @@ -234,7 +242,7 @@ typedef struct VFIOGroup { int fd; int groupid; VFIOContainer *container; - QLIST_HEAD(, VFIOPCIDevice) device_list; + QLIST_HEAD(, VFIODevice) device_list; QLIST_ENTRY(VFIOGroup) next; QLIST_ENTRY(VFIOGroup) container_next; } VFIOGroup; @@ -3381,7 +3389,7 @@ static int vfio_pci_hot_reset(VFIOPCIDevice *vdev, bool single) single ? "one" : "multi"); vfio_pci_pre_reset(vdev); - vdev->needs_reset = false; + vdev->vbasedev.needs_reset = false; info = g_malloc0(sizeof(*info)); info->argsz = sizeof(*info); @@ -3418,6 +3426,7 @@ static int vfio_pci_hot_reset(VFIOPCIDevice *vdev, bool single) for (i = 0; i < info->count; i++) { PCIHostDeviceAddress host; VFIOPCIDevice *tmp; + VFIODevice *vbasedev_iter; host.domain = devices[i].segment; host.bus = devices[i].bus; @@ -3449,7 +3458,11 @@ static int vfio_pci_hot_reset(VFIOPCIDevice *vdev, bool single) } /* Prep dependent devices for reset and clear our marker. */ - QLIST_FOREACH(tmp, &group->device_list, next) { + QLIST_FOREACH(vbasedev_iter, &group->device_list, next) { + if (vbasedev_iter->type != VFIO_DEVICE_TYPE_PCI) { + continue; + } + tmp = container_of(vbasedev_iter, VFIOPCIDevice, vbasedev); if (vfio_pci_host_match(&host, &tmp->host)) { if (single) { error_report("vfio: found another in-use device " @@ -3459,7 +3472,7 @@ static int vfio_pci_hot_reset(VFIOPCIDevice *vdev, bool single) goto out_single; } vfio_pci_pre_reset(tmp); - tmp->needs_reset = false; + tmp->vbasedev.needs_reset = false; multi = true; break; } @@ -3512,6 +3525,7 @@ out: for (i = 0; i < info->count; i++) { PCIHostDeviceAddress host; VFIOPCIDevice *tmp; + VFIODevice *vbasedev_iter; host.domain = devices[i].segment; host.bus = devices[i].bus; @@ -3532,7 +3546,11 @@ out: break; } - QLIST_FOREACH(tmp, &group->device_list, next) { + QLIST_FOREACH(vbasedev_iter, &group->device_list, next) { + if (vbasedev_iter->type != VFIO_DEVICE_TYPE_PCI) { + continue; + } + tmp = container_of(vbasedev_iter, VFIOPCIDevice, vbasedev); if (vfio_pci_host_match(&host, &tmp->host)) { vfio_pci_post_reset(tmp); break; @@ -3566,28 +3584,40 @@ static int vfio_pci_hot_reset_one(VFIOPCIDevice *vdev) return vfio_pci_hot_reset(vdev, true); } -static int vfio_pci_hot_reset_multi(VFIOPCIDevice *vdev) +static int vfio_pci_hot_reset_multi(VFIODevice *vbasedev) { + VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev); return vfio_pci_hot_reset(vdev, false); } -static void vfio_pci_reset_handler(void *opaque) +static void vfio_pci_compute_needs_reset(VFIODevice *vbasedev) +{ + VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev); + if (!vbasedev->reset_works || (!vdev->has_flr && vdev->has_pm_reset)) { + vbasedev->needs_reset = true; + } +} + +static VFIODeviceOps vfio_pci_ops = { + .vfio_compute_needs_reset = vfio_pci_compute_needs_reset, + .vfio_hot_reset_multi = vfio_pci_hot_reset_multi, +}; + +static void vfio_reset_handler(void *opaque) { VFIOGroup *group; - VFIOPCIDevice *vdev; + VFIODevice *vbasedev; QLIST_FOREACH(group, &group_list, next) { - QLIST_FOREACH(vdev, &group->device_list, next) { - if (!vdev->reset_works || (!vdev->has_flr && vdev->has_pm_reset)) { - vdev->needs_reset = true; - } + QLIST_FOREACH(vbasedev, &group->device_list, next) { + vbasedev->ops->vfio_compute_needs_reset(vbasedev); } } QLIST_FOREACH(group, &group_list, next) { - QLIST_FOREACH(vdev, &group->device_list, next) { - if (vdev->needs_reset) { - vfio_pci_hot_reset_multi(vdev); + QLIST_FOREACH(vbasedev, &group->device_list, next) { + if (vbasedev->needs_reset) { + vbasedev->ops->vfio_hot_reset_multi(vbasedev); } } } @@ -3876,7 +3906,7 @@ static VFIOGroup *vfio_get_group(int groupid, AddressSpace *as) } if (QLIST_EMPTY(&group_list)) { - qemu_register_reset(vfio_pci_reset_handler, NULL); + qemu_register_reset(vfio_reset_handler, NULL); } QLIST_INSERT_HEAD(&group_list, group, next); @@ -3908,7 +3938,7 @@ static void vfio_put_group(VFIOGroup *group) g_free(group); if (QLIST_EMPTY(&group_list)) { - qemu_unregister_reset(vfio_pci_reset_handler, NULL); + qemu_unregister_reset(vfio_reset_handler, NULL); } } @@ -3931,7 +3961,7 @@ static int vfio_get_device(VFIOGroup *group, const char *name, vdev->vbasedev.fd = ret; vdev->vbasedev.group = group; - QLIST_INSERT_HEAD(&group->device_list, vdev, next); + QLIST_INSERT_HEAD(&group->device_list, &vdev->vbasedev, next); /* Sanity check device */ ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_GET_INFO, &dev_info); @@ -3948,7 +3978,7 @@ static int vfio_get_device(VFIOGroup *group, const char *name, goto error; } - vdev->reset_works = !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET); + vdev->vbasedev.reset_works = !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET); if (dev_info.num_regions < VFIO_PCI_CONFIG_REGION_INDEX + 1) { error_report("vfio: unexpected number of io regions %u", @@ -4059,7 +4089,7 @@ static int vfio_get_device(VFIOGroup *group, const char *name, error: if (ret) { - QLIST_REMOVE(vdev, next); + QLIST_REMOVE(&vdev->vbasedev, next); vdev->vbasedev.group = NULL; close(vdev->vbasedev.fd); } @@ -4068,7 +4098,7 @@ error: static void vfio_put_device(VFIOPCIDevice *vdev) { - QLIST_REMOVE(vdev, next); + QLIST_REMOVE(&vdev->vbasedev, next); vdev->vbasedev.group = NULL; trace_vfio_put_device(vdev->vbasedev.fd); close(vdev->vbasedev.fd); @@ -4186,7 +4216,8 @@ static void vfio_unregister_err_notifier(VFIOPCIDevice *vdev) static int vfio_initfn(PCIDevice *pdev) { - VFIOPCIDevice *pvdev, *vdev = DO_UPCAST(VFIOPCIDevice, pdev, pdev); + VFIOPCIDevice *vdev = DO_UPCAST(VFIOPCIDevice, pdev, pdev); + VFIODevice *vbasedev_iter; VFIOGroup *group; char path[PATH_MAX], iommu_group_path[PATH_MAX], *group_name; ssize_t len; @@ -4204,6 +4235,8 @@ static int vfio_initfn(PCIDevice *pdev) return -errno; } + vdev->vbasedev.ops = &vfio_pci_ops; + vdev->vbasedev.type = VFIO_DEVICE_TYPE_PCI; vdev->vbasedev.name = g_strdup_printf("%04x:%02x:%02x.%01x", vdev->host.domain, vdev->host.bus, @@ -4238,9 +4271,8 @@ static int vfio_initfn(PCIDevice *pdev) vdev->host.domain, vdev->host.bus, vdev->host.slot, vdev->host.function); - QLIST_FOREACH(pvdev, &group->device_list, next) { - if (strcmp(pvdev->vbasedev.name, vdev->vbasedev.name) == 0) { - + QLIST_FOREACH(vbasedev_iter, &group->device_list, next) { + if (strcmp(vbasedev_iter->name, vdev->vbasedev.name) == 0) { error_report("vfio: error: device %s is already attached", path); vfio_put_group(group); return -EBUSY; @@ -4368,7 +4400,8 @@ static void vfio_pci_reset(DeviceState *dev) vfio_pci_pre_reset(vdev); - if (vdev->reset_works && (vdev->has_flr || !vdev->has_pm_reset) && + if (vdev->vbasedev.reset_works && + (vdev->has_flr || !vdev->has_pm_reset) && !ioctl(vdev->vbasedev.fd, VFIO_DEVICE_RESET)) { trace_vfio_pci_reset_flr(vdev->host.domain, vdev->host.bus, vdev->host.slot, vdev->host.function); @@ -4381,7 +4414,7 @@ static void vfio_pci_reset(DeviceState *dev) } /* If nothing else works and the device supports PM reset, use it */ - if (vdev->reset_works && vdev->has_pm_reset && + if (vdev->vbasedev.reset_works && vdev->has_pm_reset && !ioctl(vdev->vbasedev.fd, VFIO_DEVICE_RESET)) { trace_vfio_pci_reset_pm(vdev->host.domain, vdev->host.bus, vdev->host.slot, vdev->host.function);