From patchwork Tue Sep 28 09:44:11 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sheng Yang X-Patchwork-Id: 214342 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id o8S9iPYs008701 for ; Tue, 28 Sep 2010 09:44:25 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758568Ab0I1JoV (ORCPT ); Tue, 28 Sep 2010 05:44:21 -0400 Received: from mga02.intel.com ([134.134.136.20]:28977 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1758768Ab0I1JoT (ORCPT ); Tue, 28 Sep 2010 05:44:19 -0400 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga101.jf.intel.com with ESMTP; 28 Sep 2010 02:44:18 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="4.57,246,1283756400"; d="scan'208";a="661857236" Received: from syang10-desktop.sh.intel.com (HELO syang10-desktop) ([10.239.13.14]) by orsmga001.jf.intel.com with ESMTP; 28 Sep 2010 02:44:18 -0700 Received: from yasker by syang10-desktop with local (Exim 4.71) (envelope-from ) id 1P0WjN-0006UI-95; Tue, 28 Sep 2010 17:44:13 +0800 From: Sheng Yang To: Avi Kivity , Marcelo Tosatti Cc: kvm@vger.kernel.org, Sheng Yang Subject: [PATCH 2/3] qemu-kvm: device assignment: Some clean up for MSI-X code Date: Tue, 28 Sep 2010 17:44:11 +0800 Message-Id: <1285667052-24907-3-git-send-email-sheng@linux.intel.com> X-Mailer: git-send-email 1.7.0.4 In-Reply-To: <1285667052-24907-1-git-send-email-sheng@linux.intel.com> References: <1285667052-24907-1-git-send-email-sheng@linux.intel.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter1.kernel.org [140.211.167.41]); Tue, 28 Sep 2010 09:44:25 +0000 (UTC) diff --git a/hw/device-assignment.c b/hw/device-assignment.c index 87f7418..4edae52 100644 --- a/hw/device-assignment.c +++ b/hw/device-assignment.c @@ -1129,15 +1129,10 @@ static void assigned_dev_update_msi(PCIDevice *pci_dev, unsigned int ctrl_pos) #endif #ifdef KVM_CAP_DEVICE_MSIX -static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev) +static int get_msix_entries_max_nr(AssignedDevice *adev) { - AssignedDevice *adev = container_of(pci_dev, AssignedDevice, dev); - uint16_t entries_nr = 0, entries_max_nr; - int pos = 0, i, r = 0; - uint32_t msg_addr, msg_upper_addr, msg_data, msg_ctrl; - struct kvm_assigned_msix_nr msix_nr; - struct kvm_assigned_msix_entry msix_entry; - void *va = adev->msix_table_page; + int pos, entries_max_nr; + PCIDevice *pci_dev = &adev->dev; if (adev->cap.available & ASSIGNED_DEVICE_CAP_MSI) pos = pci_dev->cap.start + PCI_CAPABILITY_CONFIG_MSI_LENGTH; @@ -1148,6 +1143,17 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev) entries_max_nr &= PCI_MSIX_TABSIZE; entries_max_nr += 1; + return entries_max_nr; +} + +static int get_msix_valid_entries_nr(AssignedDevice *adev, + uint16_t entries_max_nr) +{ + void *va = adev->msix_table_page; + uint32_t msg_data, msg_ctrl; + uint16_t entries_nr = 0; + int i; + /* Get the usable entry number for allocating */ for (i = 0; i < entries_max_nr; i++) { memcpy(&msg_ctrl, va + i * 16 + 12, 4); @@ -1157,11 +1163,20 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev) continue; entries_nr ++; } + return entries_nr; +} + +static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev, + uint16_t entries_nr, + uint16_t entries_max_nr) +{ + AssignedDevice *adev = container_of(pci_dev, AssignedDevice, dev); + int i, r = 0; + uint32_t msg_addr, msg_upper_addr, msg_data, msg_ctrl; + struct kvm_assigned_msix_nr msix_nr; + struct kvm_assigned_msix_entry msix_entry; + void *va = adev->msix_table_page; - if (entries_nr == 0) { - fprintf(stderr, "MSI-X entry number is zero!\n"); - return -EINVAL; - } msix_nr.assigned_dev_id = calc_assigned_dev_id(adev->h_segnr, adev->h_busnr, (uint8_t)adev->h_devfn); msix_nr.entry_nr = entries_nr; @@ -1203,8 +1218,8 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev) adev->entry[entries_nr].u.msi.address_lo = msg_addr; adev->entry[entries_nr].u.msi.address_hi = msg_upper_addr; adev->entry[entries_nr].u.msi.data = msg_data; - DEBUG("MSI-X data 0x%x, MSI-X addr_lo 0x%x\n!", msg_data, msg_addr); - kvm_add_routing_entry(kvm_context, &adev->entry[entries_nr]); + DEBUG("MSI-X data 0x%x, MSI-X addr_lo 0x%x\n", msg_data, msg_addr); + kvm_add_routing_entry(kvm_context, &adev->entry[entries_nr]); msix_entry.gsi = adev->entry[entries_nr].gsi; msix_entry.entry = i; @@ -1226,12 +1241,12 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev) return r; } -static void assigned_dev_update_msix(PCIDevice *pci_dev, unsigned int ctrl_pos) +static void assigned_dev_update_msix(PCIDevice *pci_dev, int enable_msix) { struct kvm_assigned_irq assigned_irq_data; AssignedDevice *assigned_dev = container_of(pci_dev, AssignedDevice, dev); - uint16_t *ctrl_word = (uint16_t *)(pci_dev->config + ctrl_pos); int r; + uint16_t entries_nr, entries_max_nr; memset(&assigned_irq_data, 0, sizeof assigned_irq_data); assigned_irq_data.assigned_dev_id = @@ -1242,8 +1257,7 @@ static void assigned_dev_update_msix(PCIDevice *pci_dev, unsigned int ctrl_pos) * try to catch this by only deassigning irqs if the guest is using * MSIX or intends to start. */ if ((assigned_dev->irq_requested_type & KVM_DEV_IRQ_GUEST_MSIX) || - (*ctrl_word & PCI_MSIX_ENABLE)) { - + enable_msix) { assigned_irq_data.flags = assigned_dev->irq_requested_type; free_dev_irq_entries(assigned_dev); r = kvm_deassign_irq(kvm_context, &assigned_irq_data); @@ -1254,14 +1268,25 @@ static void assigned_dev_update_msix(PCIDevice *pci_dev, unsigned int ctrl_pos) assigned_dev->irq_requested_type = 0; } - if (*ctrl_word & PCI_MSIX_ENABLE) { - assigned_irq_data.flags = KVM_DEV_IRQ_HOST_MSIX | - KVM_DEV_IRQ_GUEST_MSIX; - - if (assigned_dev_update_msix_mmio(pci_dev) < 0) { + entries_max_nr = get_msix_entries_max_nr(assigned_dev); + if (entries_max_nr == 0) { + fprintf(stderr, "assigned_dev_update_msix: MSI-X entries_max_nr == 0"); + return; + } + entries_nr = get_msix_valid_entries_nr(assigned_dev, entries_max_nr); + if (entries_nr == 0) { + if (enable_msix) + fprintf(stderr, "MSI-X entry number is zero!\n"); + return; + } + if (enable_msix) { + if (assigned_dev_update_msix_mmio(pci_dev, + entries_nr, entries_max_nr) < 0) { perror("assigned_dev_update_msix_mmio"); return; } + assigned_irq_data.flags = KVM_DEV_IRQ_HOST_MSIX | + KVM_DEV_IRQ_GUEST_MSIX; if (kvm_assign_irq(kvm_context, &assigned_irq_data) < 0) { perror("assigned_dev_enable_msix: assign irq"); return; @@ -1277,6 +1302,7 @@ static void assigned_device_pci_cap_write_config(PCIDevice *pci_dev, uint32_t ad { AssignedDevice *assigned_dev = container_of(pci_dev, AssignedDevice, dev); unsigned int pos = pci_dev->cap.start, ctrl_pos; + uint16_t *ctrl_word; pci_default_cap_write_config(pci_dev, address, val, len); #ifdef KVM_CAP_IRQ_ROUTING @@ -1293,7 +1319,8 @@ static void assigned_device_pci_cap_write_config(PCIDevice *pci_dev, uint32_t ad ctrl_pos = pos + 3; if (address <= ctrl_pos && address + len > ctrl_pos) { ctrl_pos--; /* control is word long */ - assigned_dev_update_msix(pci_dev, ctrl_pos); + ctrl_word = (uint16_t *)(pci_dev->config + ctrl_pos); + assigned_dev_update_msix(pci_dev, (*ctrl_word & PCI_MSIX_ENABLE)); } pos += PCI_CAPABILITY_CONFIG_MSIX_LENGTH; }