From patchwork Tue Sep 9 07:31:05 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Auger Eric X-Patchwork-Id: 37030 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-yk0-f197.google.com (mail-yk0-f197.google.com [209.85.160.197]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 313E320491 for ; Tue, 9 Sep 2014 07:31:47 +0000 (UTC) Received: by mail-yk0-f197.google.com with SMTP id 131sf7705053ykp.0 for ; Tue, 09 Sep 2014 00:31:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-unsubscribe; bh=ynNXnYQ0X5CXKLLMZor2owcHPeGb6cpYU4SKA0ozqnA=; b=cUxDPpI7IjCFSI9B1ySLDHfr2efgSH+Bs91PCVpo+MpyaSMciHYQxR8HMAQ25XKY6M MBz5i6Kv0qDyI5QAeaY/2MU/ywyW4EEukUfWlWP/NzSTiDdAhbHiSo0TArJIrV/joW1d /+c0KhnzMe1DnuXPTfLaAJVIX7/97oyRWdvA3tupREAj/qNgo5Fyt4H/wJRPvRtHRaQE wFklCRga06irBEPqRLf6vDaKlHX5YfiFNIwKLdgTq2lhkkpCKIcbgCyBAwKFn5003QF4 /lEFMtdNZvQkluIPeeyRGv9xSpUvYnlUQBfLl0+4E5/O+0bZ9Krn2w4CEXeTJM7T9Z0E NTUg== X-Gm-Message-State: ALoCoQmwcZIdGBJV/bC1r+dbV7ekdH2pcRrTiTmnn4inrq337yCYtfLTmJgnBbgjGxbiN+fwcCpj X-Received: by 10.236.15.167 with SMTP id f27mr20881902yhf.37.1410247907043; Tue, 09 Sep 2014 00:31:47 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.81.200 with SMTP id f66ls2040743qgd.24.gmail; Tue, 09 Sep 2014 00:31:46 -0700 (PDT) X-Received: by 10.52.246.198 with SMTP id xy6mr24524334vdc.7.1410247906925; Tue, 09 Sep 2014 00:31:46 -0700 (PDT) Received: from mail-vc0-f179.google.com (mail-vc0-f179.google.com [209.85.220.179]) by mx.google.com with ESMTPS id h9si5535209vdk.39.2014.09.09.00.31.46 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 09 Sep 2014 00:31:46 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.179 as permitted sender) client-ip=209.85.220.179; Received: by mail-vc0-f179.google.com with SMTP id im17so2193794vcb.24 for ; Tue, 09 Sep 2014 00:31:46 -0700 (PDT) X-Received: by 10.220.97.138 with SMTP id l10mr14590002vcn.25.1410247906837; Tue, 09 Sep 2014 00:31:46 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.221.45.67 with SMTP id uj3csp244023vcb; Tue, 9 Sep 2014 00:31:46 -0700 (PDT) X-Received: by 10.194.93.7 with SMTP id cq7mr41229655wjb.58.1410247905754; Tue, 09 Sep 2014 00:31:45 -0700 (PDT) Received: from mail-wg0-f45.google.com (mail-wg0-f45.google.com [74.125.82.45]) by mx.google.com with ESMTPS id u15si17173756wij.49.2014.09.09.00.31.45 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 09 Sep 2014 00:31:45 -0700 (PDT) Received-SPF: pass (google.com: domain of eric.auger@linaro.org designates 74.125.82.45 as permitted sender) client-ip=74.125.82.45; Received: by mail-wg0-f45.google.com with SMTP id z12so2264018wgg.16 for ; Tue, 09 Sep 2014 00:31:45 -0700 (PDT) X-Received: by 10.180.95.135 with SMTP id dk7mr27632964wib.68.1410247905335; Tue, 09 Sep 2014 00:31:45 -0700 (PDT) Received: from midway01-04-00.lavalab ([88.98.47.97]) by mx.google.com with ESMTPSA id s1sm14513743wiw.6.2014.09.09.00.31.44 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 09 Sep 2014 00:31:44 -0700 (PDT) From: Eric Auger To: eric.auger@st.com, christoffer.dall@linaro.org, qemu-devel@nongnu.org, a.rigo@virtualopensystems.com, kim.phillips@freescale.com, marc.zyngier@arm.com, manish.jaggi@caviumnetworks.com, joel.schopp@amd.com, agraf@suse.de, peter.maydell@linaro.org, pbonzini@redhat.com, afaerber@suse.de Cc: eric.auger@linaro.org, kvmarm@lists.cs.columbia.edu, patches@linaro.org, will.deacon@arm.com, Bharat.Bhushan@freescale.com, stuart.yoder@freescale.com, a.motakis@virtualopensystems.com, alex.williamson@redhat.com Subject: [PATCH v6 05/16] hw/vfio/pci: split vfio_get_device Date: Tue, 9 Sep 2014 08:31:05 +0100 Message-Id: <1410247876-4967-6-git-send-email-eric.auger@linaro.org> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1410247876-4967-1-git-send-email-eric.auger@linaro.org> References: <1410247876-4967-1-git-send-email-eric.auger@linaro.org> X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: eric.auger@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.179 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , vfio_get_device now takes a VFIODevice as argument. The function is split into 2 parts: vfio_get_device which is generic and vfio_populate_device which is bus specific. 3 new fields are introduced in VFIODevice to store dev_info. vfio_put_base_device is created. --- v5->v6: - simplifies the split for vfio_get_device: vfio_check_device, vfio_populate_regions, vfio_populate_interrupts are now gathered into a unique specialization function dubbed vfio_populate_device v4->v5: - cleanup up of error handling and get/put operations in vfio_check_device, vfio_populate_regions, vfio_populate_interrupts and vfio_get_device. - correct misuse of errno - vfio_populate_regions always returns 0 - VFIODevice .name deallocation done in vfio_put_device instead of vfio_put_base_device - vfio_put_base_device done at vfio_get_device level. Signed-off-by: Eric Auger --- hw/vfio/pci.c | 130 +++++++++++++++++++++++++++++++++++----------------------- trace-events | 10 ++--- 2 files changed, 83 insertions(+), 57 deletions(-) diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index 5e34504..d48ca04 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -205,12 +205,16 @@ typedef struct VFIODevice { bool reset_works; bool needs_reset; VFIODeviceOps *ops; + unsigned int num_irqs; + unsigned int num_regions; + unsigned int flags; } VFIODevice; struct VFIODeviceOps { bool (*vfio_compute_needs_reset)(VFIODevice *vdev); int (*vfio_hot_reset_multi)(VFIODevice *vdev); void (*vfio_eoi)(VFIODevice *vdev); + int (*vfio_populate_device)(VFIODevice *vdev); }; typedef struct VFIOPCIDevice { @@ -297,6 +301,8 @@ static uint32_t vfio_pci_read_config(PCIDevice *pdev, uint32_t addr, int len); static void vfio_pci_write_config(PCIDevice *pdev, uint32_t addr, uint32_t val, int len); static void vfio_mmap_set_enabled(VFIOPCIDevice *vdev, bool enabled); +static void vfio_put_base_device(VFIODevice *vbasedev); +static int vfio_populate_device(VFIODevice *vbasedev); /* * Common VFIO interrupt disable @@ -3611,6 +3617,7 @@ static VFIODeviceOps vfio_pci_ops = { .vfio_compute_needs_reset = vfio_pci_compute_needs_reset, .vfio_hot_reset_multi = vfio_pci_hot_reset_multi, .vfio_eoi = vfio_eoi, + .vfio_populate_device = vfio_populate_device, }; static void vfio_reset_handler(void *opaque) @@ -3952,70 +3959,45 @@ static void vfio_put_group(VFIOGroup *group) } } -static int vfio_get_device(VFIOGroup *group, const char *name, - VFIOPCIDevice *vdev) +static int vfio_populate_device(VFIODevice *vbasedev) { - struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) }; + VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev); struct vfio_region_info reg_info = { .argsz = sizeof(reg_info) }; struct vfio_irq_info irq_info = { .argsz = sizeof(irq_info) }; - int ret, i; - - ret = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, name); - if (ret < 0) { - error_report("vfio: error getting device %s from group %d: %m", - name, group->groupid); - error_printf("Verify all devices in group %d are bound to vfio-pci " - "or pci-stub and not already in use\n", group->groupid); - return ret; - } - - vdev->vbasedev.fd = ret; - vdev->vbasedev.group = group; - QLIST_INSERT_HEAD(&group->device_list, &vdev->vbasedev, next); + int i, ret = -1; /* Sanity check device */ - ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_GET_INFO, &dev_info); - if (ret) { - error_report("vfio: error getting device info: %m"); - goto error; - } - - trace_vfio_get_device_irq(name, dev_info.flags, - dev_info.num_regions, dev_info.num_irqs); - - if (!(dev_info.flags & VFIO_DEVICE_FLAGS_PCI)) { + if (!(vbasedev->flags & VFIO_DEVICE_FLAGS_PCI)) { error_report("vfio: Um, this isn't a PCI device"); goto error; } - vdev->vbasedev.reset_works = !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET); - - if (dev_info.num_regions < VFIO_PCI_CONFIG_REGION_INDEX + 1) { + if (vbasedev->num_regions < VFIO_PCI_CONFIG_REGION_INDEX + 1) { error_report("vfio: unexpected number of io regions %u", - dev_info.num_regions); + vbasedev->num_regions); goto error; } - if (dev_info.num_irqs < VFIO_PCI_MSIX_IRQ_INDEX + 1) { - error_report("vfio: unexpected number of irqs %u", dev_info.num_irqs); + if (vbasedev->num_irqs < VFIO_PCI_MSIX_IRQ_INDEX + 1) { + error_report("vfio: unexpected number of irqs %u", vbasedev->num_irqs); goto error; } for (i = VFIO_PCI_BAR0_REGION_INDEX; i < VFIO_PCI_ROM_REGION_INDEX; i++) { reg_info.index = i; - ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_GET_REGION_INFO, ®_info); + ret = ioctl(vbasedev->fd, VFIO_DEVICE_GET_REGION_INFO, ®_info); if (ret) { error_report("vfio: Error getting region %d info: %m", i); goto error; } - trace_vfio_get_device_region(name, i, - (unsigned long)reg_info.size, - (unsigned long)reg_info.offset, - (unsigned long)reg_info.flags); + trace_vfio_populate_device_region(vbasedev->name, i, + (unsigned long)reg_info.size, + (unsigned long)reg_info.offset, + (unsigned long)reg_info.flags); - vdev->bars[i].region.vbasedev = &vdev->vbasedev; + vdev->bars[i].region.vbasedev = vbasedev; vdev->bars[i].region.flags = reg_info.flags; vdev->bars[i].region.size = reg_info.size; vdev->bars[i].region.fd_offset = reg_info.offset; @@ -4031,9 +4013,10 @@ static int vfio_get_device(VFIOGroup *group, const char *name, goto error; } - trace_vfio_get_device_config(name, (unsigned long)reg_info.size, - (unsigned long)reg_info.offset, - (unsigned long)reg_info.flags); + trace_vfio_populate_device_config(vdev->vbasedev.name, + (unsigned long)reg_info.size, + (unsigned long)reg_info.offset, + (unsigned long)reg_info.flags); vdev->config_size = reg_info.size; if (vdev->config_size == PCI_CONFIG_SPACE_SIZE) { @@ -4042,7 +4025,7 @@ static int vfio_get_device(VFIOGroup *group, const char *name, vdev->config_offset = reg_info.offset; if ((vdev->features & VFIO_FEATURE_ENABLE_VGA) && - dev_info.num_regions > VFIO_PCI_VGA_REGION_INDEX) { + vbasedev->num_regions > VFIO_PCI_VGA_REGION_INDEX) { struct vfio_region_info vga_info = { .argsz = sizeof(vga_info), .index = VFIO_PCI_VGA_REGION_INDEX, @@ -4086,7 +4069,7 @@ static int vfio_get_device(VFIOGroup *group, const char *name, ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info); if (ret) { /* This can fail for an old kernel or legacy PCI dev */ - trace_vfio_get_device_get_irq_info_failure(); + trace_vfio_populate_device_get_irq_info_failure(); ret = 0; } else if (irq_info.count == 1) { vdev->pci_aer = true; @@ -4098,25 +4081,68 @@ static int vfio_get_device(VFIOGroup *group, const char *name, } error: + return ret; +} + +static int vfio_get_device(VFIOGroup *group, const char *name, + VFIODevice *vbasedev) +{ + struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) }; + int ret; + + ret = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, name); + if (ret < 0) { + error_report("vfio: error getting device %s from group %d: %m", + name, group->groupid); + error_printf("Verify all devices in group %d are bound to vfio- " + "or pci-stub and not already in use\n", group->groupid); + return ret; + } + + vbasedev->fd = ret; + vbasedev->group = group; + QLIST_INSERT_HEAD(&group->device_list, vbasedev, next); + + ret = ioctl(vbasedev->fd, VFIO_DEVICE_GET_INFO, &dev_info); + if (ret) { + error_report("vfio: error getting device info: %m"); + goto error; + } + + vbasedev->num_irqs = dev_info.num_irqs; + vbasedev->num_regions = dev_info.num_regions; + vbasedev->flags = dev_info.flags; + + trace_vfio_get_device(name, dev_info.flags, + dev_info.num_regions, dev_info.num_irqs); + + vbasedev->reset_works = !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET); + + ret = vbasedev->ops->vfio_populate_device(vbasedev); + +error: if (ret) { - QLIST_REMOVE(&vdev->vbasedev, next); - vdev->vbasedev.group = NULL; - close(vdev->vbasedev.fd); + vfio_put_base_device(vbasedev); } return ret; } +void vfio_put_base_device(VFIODevice *vbasedev) +{ + QLIST_REMOVE(vbasedev, next); + vbasedev->group = NULL; + trace_vfio_put_base_device(vbasedev->fd); + close(vbasedev->fd); +} + static void vfio_put_device(VFIOPCIDevice *vdev) { - QLIST_REMOVE(&vdev->vbasedev, next); - vdev->vbasedev.group = NULL; - trace_vfio_put_device(vdev->vbasedev.fd); - close(vdev->vbasedev.fd); g_free(vdev->vbasedev.name); if (vdev->msix) { g_free(vdev->msix); vdev->msix = NULL; } + vfio_put_base_device(&vdev->vbasedev); } static void vfio_err_notifier_handler(void *opaque) @@ -4289,7 +4315,7 @@ static int vfio_initfn(PCIDevice *pdev) } } - ret = vfio_get_device(group, path, vdev); + ret = vfio_get_device(group, path, &vdev->vbasedev); if (ret) { error_report("vfio: failed to get device %s", path); vfio_put_group(group); diff --git a/trace-events b/trace-events index 0b46a42..bcdffac 100644 --- a/trace-events +++ b/trace-events @@ -1356,10 +1356,10 @@ vfio_pci_hot_reset(int domain, int bus, int slot, int fn, const char *type) " (% vfio_pci_hot_reset_has_dep_devices(int domain, int bus, int slot, int fn) "%04x:%02x:%02x.%x: hot reset dependent devices:" vfio_pci_hot_reset_dep_devices(int domain, int bus, int slot, int function, int group_id) "\t%04x:%02x:%02x.%x group %d" vfio_pci_hot_reset_result(int domain, int bus, int slot, int fn, const char *result) "%04x:%02x:%02x.%x hot reset: %s" -vfio_get_device_region(const char *region_name, int index, unsigned long size, unsigned long offset, unsigned long flags) "Device %s region %d:\n size: 0x%lx, offset: 0x%lx, flags: 0x%lx" -vfio_get_device_config(const char *name, unsigned long size, unsigned long offset, unsigned long flags) "Device %s config:\n size: 0x%lx, offset: 0x%lx, flags: 0x%lx" -vfio_get_device_get_irq_info_failure(void) "VFIO_DEVICE_GET_IRQ_INFO failure: %m" -vfio_get_device_irq(const char *name, unsigned flags, unsigned num_regions, unsigned num_irqs) "Device %s flags: %u, regions: %u, irgs: %u" +vfio_populate_device_region(const char *region_name, int index, unsigned long size, unsigned long offset, unsigned long flags) "Device %s region %d:\n size: 0x%lx, offset: 0x%lx, flags: 0x%lx" +vfio_populate_device_config(const char *name, unsigned long size, unsigned long offset, unsigned long flags) "Device %s config:\n size: 0x%lx, offset: 0x%lx, flags: 0x%lx" +vfio_populate_device_get_irq_info_failure(void) "VFIO_DEVICE_GET_IRQ_INFO failure: %m" +vfio_get_device(const char *name, unsigned flags, unsigned num_regions, unsigned num_irqs) "Device %s flags: %u, regions: %u, irgs: %u" vfio_initfn(int domain, int bus, int slot, int fn, int group_id) " (%04x:%02x:%02x.%x) group %d" vfio_pci_reset(int domain, int bus, int slot, int fn) " (%04x:%02x:%02x.%x)" vfio_pci_reset_flr(int domain, int bus, int slot, int fn) "%04x:%02x:%02x.%x FLR/VFIO_DEVICE_RESET" @@ -1375,7 +1375,7 @@ vfio_listener_region_del_skip(uint64_t start, uint64_t end) "SKIPPING region_del vfio_listener_region_del(uint64_t start, uint64_t end) "region_del %"PRIx64" - %"PRIx64 vfio_disconnect_container(int fd) "close container->fd=%d" vfio_put_group(int fd) "close group->fd=%d" -vfio_put_device(int fd) "close vdev->fd=%d" +vfio_put_base_device(int fd) "close vdev->fd=%d" #hw/acpi/memory_hotplug.c mhp_acpi_invalid_slot_selected(uint32_t slot) "0x%"PRIx32