From patchwork Fri Feb 13 09:49:34 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Han, Weidong" X-Patchwork-Id: 6978 Received: from vger.kernel.org (vger.kernel.org [209.132.176.167]) by demeter.kernel.org (8.14.2/8.14.2) with ESMTP id n1D9ngYt002862 for ; Fri, 13 Feb 2009 09:49:43 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752010AbZBMJtl (ORCPT ); Fri, 13 Feb 2009 04:49:41 -0500 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1752248AbZBMJtk (ORCPT ); Fri, 13 Feb 2009 04:49:40 -0500 Received: from mga02.intel.com ([134.134.136.20]:41652 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752010AbZBMJtk (ORCPT ); Fri, 13 Feb 2009 04:49:40 -0500 Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga101.jf.intel.com with ESMTP; 13 Feb 2009 01:45:06 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="4.38,201,1233561600"; d="scan'208,223";a="386534533" Received: from azsmsx602.amr.corp.intel.com ([10.2.121.201]) by orsmga002.jf.intel.com with ESMTP; 13 Feb 2009 01:58:31 -0800 Received: from pdsmsx602.ccr.corp.intel.com (172.16.12.184) by azsmsx602.amr.corp.intel.com (10.2.121.201) with Microsoft SMTP Server (TLS) id 8.1.311.2; Fri, 13 Feb 2009 02:49:37 -0700 Received: from pdsmsx503.ccr.corp.intel.com ([172.16.12.95]) by pdsmsx602.ccr.corp.intel.com ([172.16.12.184]) with mapi; Fri, 13 Feb 2009 17:49:36 +0800 From: "Han, Weidong" To: "'Avi Kivity'" CC: "'kvm@vger.kernel.org'" , "'Mark McLoughlin'" Date: Fri, 13 Feb 2009 17:49:34 +0800 Subject: [PATCH 5/7] [V3] kvm: qemu: wrap assign_device and assign_irq Thread-Topic: [PATCH 5/7] [V3] kvm: qemu: wrap assign_device and assign_irq Thread-Index: AcmNwGBaWGc4Exg5TWuyf30eEA5tiA== Message-ID: <715D42877B251141A38726ABF5CABF2C01959AFEBA@pdsmsx503.ccr.corp.intel.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: yes X-MS-TNEF-Correlator: acceptlanguage: en-US MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org just wrap assign_device and assign_irq, no functional changes Acked-by: Mark McLoughlin Signed-off-by: Weidong Han --- qemu/hw/device-assignment.c | 121 +++++++++++++++++++++++++------------------ 1 files changed, 70 insertions(+), 51 deletions(-) diff --git a/qemu/hw/device-assignment.c b/qemu/hw/device-assignment.c index 6046fdd..5341ef2 100644 --- a/qemu/hw/device-assignment.c +++ b/qemu/hw/device-assignment.c @@ -487,6 +487,68 @@ static uint32_t calc_assigned_dev_id(uint8_t bus, uint8_t devfn) return (uint32_t)bus << 8 | (uint32_t)devfn; } +static int assign_device(AssignedDevInfo *adev) +{ + struct kvm_assigned_pci_dev assigned_dev_data; + AssignedDevice *dev = adev->assigned_dev; + int r; + + memset(&assigned_dev_data, 0, sizeof(assigned_dev_data)); + assigned_dev_data.assigned_dev_id = + calc_assigned_dev_id(dev->h_busnr, dev->h_devfn); + assigned_dev_data.busnr = dev->h_busnr; + assigned_dev_data.devfn = dev->h_devfn; + +#ifdef KVM_CAP_IOMMU + /* We always enable the IOMMU if present + * (or when not disabled on the command line) + */ + r = kvm_check_extension(kvm_context, KVM_CAP_IOMMU); + if (r && !adev->disable_iommu) + assigned_dev_data.flags |= KVM_DEV_ASSIGN_ENABLE_IOMMU; +#endif + + r = kvm_assign_pci_device(kvm_context, &assigned_dev_data); + if (r < 0) + fprintf(stderr, "Failed to assign device \"%s\" : %s\n", + adev->name, strerror(-r)); + return r; +} + +static int assign_irq(AssignedDevInfo *adev) +{ + struct kvm_assigned_irq assigned_irq_data; + AssignedDevice *dev = adev->assigned_dev; + int irq, r = 0; + + irq = pci_map_irq(&dev->dev, dev->intpin); + irq = piix_get_irq(irq); + +#ifdef TARGET_IA64 + irq = ipf_map_irq(&dev->dev, irq); +#endif + + if (dev->girq == irq) + return r; + + memset(&assigned_irq_data, 0, sizeof(assigned_irq_data)); + assigned_irq_data.assigned_dev_id = + calc_assigned_dev_id(dev->h_busnr, dev->h_devfn); + assigned_irq_data.guest_irq = irq; + assigned_irq_data.host_irq = dev->real_device.irq; + r = kvm_assign_irq(kvm_context, &assigned_irq_data); + if (r < 0) { + fprintf(stderr, "Failed to assign irq for \"%s\": %s\n", + adev->name, strerror(-r)); + fprintf(stderr, "Perhaps you are assigning a device " + "that shares an IRQ with another device?\n"); + return r; + } + + dev->girq = irq; + return r; +} + /* The pci config space got updated. Check if irq numbers have changed * for our devices */ @@ -497,37 +559,11 @@ void assigned_dev_update_irqs() adev = LIST_FIRST(&adev_head); while (adev) { AssignedDevInfo *next = LIST_NEXT(adev, next); - AssignedDevice *assigned_dev = adev->assigned_dev; - int irq, r; - - irq = pci_map_irq(&assigned_dev->dev, assigned_dev->intpin); - irq = piix_get_irq(irq); - -#ifdef TARGET_IA64 - irq = ipf_map_irq(&assigned_dev->dev, irq); -#endif + int r; - if (irq != assigned_dev->girq) { - struct kvm_assigned_irq assigned_irq_data; - - memset(&assigned_irq_data, 0, sizeof(assigned_irq_data)); - assigned_irq_data.assigned_dev_id = - calc_assigned_dev_id(assigned_dev->h_busnr, - (uint8_t) assigned_dev->h_devfn); - assigned_irq_data.guest_irq = irq; - assigned_irq_data.host_irq = assigned_dev->real_device.irq; - r = kvm_assign_irq(kvm_context, &assigned_irq_data); - if (r < 0) { - fprintf(stderr, "Failed to assign irq for \"%s\": %s\n", - adev->name, strerror(-r)); - fprintf(stderr, "Perhaps you are assigning a device " - "that shares an IRQ with another device?\n"); - free_assigned_device(adev); - adev = next; - continue; - } - assigned_dev->girq = irq; - } + r = assign_irq(adev); + if (r < 0) + free_assigned_device(adev); adev = next; } @@ -576,27 +612,10 @@ struct PCIDevice *init_assigned_device(AssignedDevInfo *adev, PCIBus *bus) dev->h_busnr = adev->bus; dev->h_devfn = PCI_DEVFN(adev->dev, adev->func); - memset(&assigned_dev_data, 0, sizeof(assigned_dev_data)); - assigned_dev_data.assigned_dev_id = - calc_assigned_dev_id(dev->h_busnr, (uint32_t)dev->h_devfn); - assigned_dev_data.busnr = dev->h_busnr; - assigned_dev_data.devfn = dev->h_devfn; - -#ifdef KVM_CAP_IOMMU - /* We always enable the IOMMU if present - * (or when not disabled on the command line) - */ - r = kvm_check_extension(kvm_context, KVM_CAP_IOMMU); - if (r && !adev->disable_iommu) - assigned_dev_data.flags |= KVM_DEV_ASSIGN_ENABLE_IOMMU; -#endif - - r = kvm_assign_pci_device(kvm_context, &assigned_dev_data); - if (r < 0) { - fprintf(stderr, "Failed to assign device \"%s\" : %s\n", - adev->name, strerror(-r)); - goto out; - } + /* assign device to guest */ + r = assign_device(adev); + if (r < 0) + goto out; return &dev->dev;