msix/kvm integration cleanups
cover msix/kvm integration cleanups
This cleans up msix/kvm integration a bit. The really important patch is the last one, which allows msix.o to be part of non-target-specific build.
Avi Kivity (9):
msix: avoid leaking kvm data on init failure
msix: make kvm specific initialization a function
msix: move kvm specific msix notify into a function
msix: move definitions from msix.c to msix.h
Add missing #include to hw/irq.h
msix: move kvm support functions to kvm-all.c and kvm-stub.c
msix: Move kvm_enabled() guards to kvm-all.c functions Protect qemu-kvm.h declarations with NEED_CPU_H Move msix.o build back to Makefile.objs
Makefile.objs | 4 +- Makefile.target | 4 - hw/irq.h | 2 + hw/msix.c | 173 ++++--------------------------------------------------- hw/msix.h | 26 ++++++++ kvm-all.c | 142 +++++++++++++++++++++++++++++++++++++++++++++ kvm-stub.c | 29 +++++++++ kvm.h | 16 +++++ qemu-kvm.h | 21 ++++++- 9 files changed, 247 insertions(+), 170 deletions(-)
Signature · 1 line
1.7.2.3
PATCH 1/9 msix: avoid leaking kvm data on init failure
Move initialization after we're certain to succeed, so we don't leak memory on failure.
--- hw/msix.c | 13 +++++++------ 1 files changed, 7 insertions(+), 6 deletions(-)
hw/msix.c +7 -6
@@ -373,12 +373,6 @@ int msix_init(struct PCIDevice *dev, unsigned short nentries,if (nentries >MSIX_MAX_ENTRIES )return -EINVAL ; -#ifdef KVM_CAP_IRQCHIP -if (kvm_enabled () &&kvm_irqchip_in_kernel ()) { - dev->msix_irq_entries =qemu_malloc (nentries * -sizeof *dev->msix_irq_entries); - } -#endif dev->msix_mask_notifier_opaque =qemu_mallocz (nentries *sizeof *dev->msix_mask_notifier_opaque); dev->msix_mask_notifier =NULL ;
@@ -400,6 +394,13 @@ int msix_init(struct PCIDevice *dev, unsigned short nentries,if (ret) goto err_config; +#ifdef KVM_CAP_IRQCHIP +if (kvm_enabled () &&kvm_irqchip_in_kernel ()) { + dev->msix_irq_entries =qemu_malloc (nentries * +sizeof *dev->msix_irq_entries); + } +#endif + dev->cap_present |=QEMU_PCI_CAP_MSIX ;return 0 ;
Signature · 1 line
1.7.2.3
PATCH 2/9 msix: make kvm specific initialization a function
In reply to Avi Kivity--- hw/msix.c | 19 +++++++++++++------ 1 files changed, 13 insertions(+), 6 deletions(-)
hw/msix.c +13 -6
@@ -48,6 +48,17 @@ int msix_supported;#ifdef CONFIG_KVM /* KVM specific MSIX helpers */ + +static void kvm_msix_init (PCIDevice *dev) +{ +#ifdef KVM_CAP_IRQCHIP +if (kvm_enabled () &&kvm_irqchip_in_kernel ()) { + dev->msix_irq_entries =qemu_malloc (dev->msix_entries_nr * +sizeof *dev->msix_irq_entries); + } +#endif +} +static void kvm_msix_free (PCIDevice *dev) {int vector, changed =0 ;
@@ -150,6 +161,7 @@ static void kvm_msix_del(PCIDevice *dev, unsigned vector) }#else +static void kvm_msix_init (PCIDevice *dev) {}static void kvm_msix_free (PCIDevice *dev) {}static void kvm_msix_update (PCIDevice *dev,int vector,int was_masked,int is_masked) {}
@@ -394,12 +406,7 @@ int msix_init(struct PCIDevice *dev, unsigned short nentries,if (ret) goto err_config; -#ifdef KVM_CAP_IRQCHIP -if (kvm_enabled () &&kvm_irqchip_in_kernel ()) { - dev->msix_irq_entries =qemu_malloc (nentries * -sizeof *dev->msix_irq_entries); - } -#endif +kvm_msix_init (dev); dev->cap_present |=QEMU_PCI_CAP_MSIX ;return 0 ;
Signature · 1 line
1.7.2.3
PATCH 3/9 msix: move kvm specific msix notify into a function
In reply to Avi Kivity--- hw/msix.c | 19 +++++++++++++++---- 1 files changed, 15 insertions(+), 4 deletions(-)
hw/msix.c +15 -4
@@ -159,6 +159,18 @@ static void kvm_msix_del(PCIDevice *dev, unsigned vector)kvm_del_routing_entry (kvm_context, &dev->msix_irq_entries[vector]); kvm_commit_irq_routes(kvm_context); } + +static bool kvm_msix_notify (PCIDevice *dev,unsigned vector) +{ +#ifdef KVM_CAP_IRQCHIP +if (kvm_enabled () &&kvm_irqchip_in_kernel ()) { +kvm_set_irq (dev->msix_irq_entries[vector].gsi,1 ,NULL ); +return true; + } +#endif +return false; +} +#else static void kvm_msix_init (PCIDevice *dev) {}
@@ -167,6 +179,8 @@ static void kvm_msix_update(PCIDevice *dev, int vector, int was_masked, int is_masked) {}static int kvm_msix_add (PCIDevice *dev,unsigned vector) {return -1 ; }static void kvm_msix_del (PCIDevice *dev,unsigned vector) {} +static bool kvm_msix_notify (PCIDevice *dev,unsigned vector) {return false; } +#endif /* Add MSI-X capability to the config space for the device. */
@@ -525,12 +539,9 @@ void msix_notify(PCIDevice *dev, unsigned vector)return ; } -#ifdef KVM_CAP_IRQCHIP -if (kvm_enabled () &&kvm_irqchip_in_kernel ()) { -kvm_set_irq (dev->msix_irq_entries[vector].gsi,1 ,NULL ); +if (kvm_msix_notify (dev, vector)) {return ; } -#endif address =pci_get_long (table_entry +MSIX_MSG_UPPER_ADDR ); address = (address <<32 ) |pci_get_long (table_entry +MSIX_MSG_ADDR );
Signature · 1 line
1.7.2.3
PATCH 4/9 msix: move definitions from msix.c to msix.h
In reply to Avi KivityThis allows us to reuse them from the kvm support code.
--- hw/msix.c | 27 --------------------------- hw/msix.h | 26 ++++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 27 deletions(-)
hw/msix.c +0 -27
@@ -16,33 +16,6 @@#include "pci.h" #include "kvm.h" -/* MSI-X capability structure */ -#define MSIX_TABLE_OFFSET 4 -#define MSIX_PBA_OFFSET 8 -#define MSIX_CAP_LENGTH 12 - -/* MSI enable bit and maskall bit are in byte 1 in FLAGS register */ -#define MSIX_CONTROL_OFFSET (PCI_MSIX_FLAGS + 1) -#define MSIX_ENABLE_MASK (PCI_MSIX_FLAGS_ENABLE >> 8) -#define MSIX_MASKALL_MASK (PCI_MSIX_FLAGS_MASKALL >> 8) - -/* MSI-X table format */ -#define MSIX_MSG_ADDR 0 -#define MSIX_MSG_UPPER_ADDR 4 -#define MSIX_MSG_DATA 8 -#define MSIX_VECTOR_CTRL 12 -#define MSIX_ENTRY_SIZE 16 -#define MSIX_VECTOR_MASK 0x1 - -/* How much space does an MSIX table need. */ -/* The spec requires giving the table structure -* a 4K aligned region all by itself. */ -#define MSIX_PAGE_SIZE 0x1000 -/* Reserve second half of the page for pending bits */ -#define MSIX_PAGE_PENDING (MSIX_PAGE_SIZE / 2) -#define MSIX_MAX_ENTRIES 32 - -/* Flag for interrupt controller to declare MSI-X support */ int msix_supported;
hw/msix.h +26 -0
@@ -4,6 +4,32 @@#include "qemu-common.h" #include "pci.h" +/* MSI-X capability structure */ +#define MSIX_TABLE_OFFSET 4 +#define MSIX_PBA_OFFSET 8 +#define MSIX_CAP_LENGTH 12 + +/* MSI enable bit and maskall bit are in byte 1 in FLAGS register */ +#define MSIX_CONTROL_OFFSET (PCI_MSIX_FLAGS + 1) +#define MSIX_ENABLE_MASK (PCI_MSIX_FLAGS_ENABLE >> 8) +#define MSIX_MASKALL_MASK (PCI_MSIX_FLAGS_MASKALL >> 8) + +/* MSI-X table format */ +#define MSIX_MSG_ADDR 0 +#define MSIX_MSG_UPPER_ADDR 4 +#define MSIX_MSG_DATA 8 +#define MSIX_VECTOR_CTRL 12 +#define MSIX_ENTRY_SIZE 16 +#define MSIX_VECTOR_MASK 0x1 + +/* How much space does an MSIX table need. */ +/* The spec requires giving the table structure +* a 4K aligned region all by itself. */ +#define MSIX_PAGE_SIZE 0x1000 +/* Reserve second half of the page for pending bits */ +#define MSIX_PAGE_PENDING (MSIX_PAGE_SIZE / 2) +#define MSIX_MAX_ENTRIES 32 +int msix_init (PCIDevice *pdev,unsigned short nentries,unsigned bar_nr,unsigned bar_size);
Signature · 1 line
1.7.2.3
PATCH 5/9 Add missing #include to hw/irq.h
In reply to Avi KivityNeeds qemu-common.h for qemu_irq.
--- hw/irq.h | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)
hw/irq.h +2 -0
@@ -1,6 +1,8 @@#ifndef QEMU_IRQ_H #define QEMU_IRQ_H +#include "qemu-common.h" +/* Generic IRQ/GPIO pin infrastructure. */ /* FIXME: Rmove one of these. */
Signature · 1 line
1.7.2.3
PATCH 7/9 msix: Move kvm_enabled() guards to kvm-all.c functions
In reply to Avi KivitySimplifies the msix code.
--- hw/msix.c | 22 ++++++++-------------- kvm-all.c | 18 ++++++++++++++++++ kvm-stub.c | 2 +- 3 files changed, 27 insertions(+), 15 deletions(-)
hw/msix.c +8 -14
@@ -159,9 +159,7 @@ static void msix_mmio_writel(void *opaque, target_phys_addr_t addr,int vector = offset /MSIX_ENTRY_SIZE ;int was_masked =msix_is_masked (dev, vector);pci_set_long (dev->msix_table_page + offset, val); -if (kvm_enabled () &&kvm_irqchip_in_kernel ()) { -kvm_msix_update (dev, vector, was_masked,msix_is_masked (dev, vector)); - } +kvm_msix_update (dev, vector, was_masked,msix_is_masked (dev, vector));if (was_masked !=msix_is_masked (dev, vector) && dev->msix_mask_notifier && dev->msix_mask_notifier_opaque[vector]) { int r = dev->msix_mask_notifier(dev, vector,
@@ -276,9 +274,7 @@ static void msix_free_irq_entries(PCIDevice *dev) {int vector; -if (kvm_enabled () &&kvm_irqchip_in_kernel ()) { -kvm_msix_free (dev); - } +kvm_msix_free (dev);for (vector =0 ; vector < dev->msix_entries_nr; ++vector) { dev->msix_entry_used[vector] =0 ;
@@ -413,12 +409,12 @@ int msix_vector_use(PCIDevice *dev, unsigned vector)if (dev->msix_entry_used[vector]) {return 0 ; } -if (kvm_enabled () &&kvm_irqchip_in_kernel ()) { - ret =kvm_msix_add (dev, vector); -if (ret) { -return ret; - } + + ret =kvm_msix_add (dev, vector); +if (ret) { +return ret; } + ++dev->msix_entry_used[vector];return 0 ; }
@@ -432,9 +428,7 @@ void msix_vector_unuse(PCIDevice *dev, unsigned vector)if (--dev->msix_entry_used[vector]) {return ; } -if (kvm_enabled () &&kvm_irqchip_in_kernel ()) { -kvm_msix_del (dev, vector); - } +kvm_msix_del (dev, vector);msix_clr_pending (dev, vector); }
kvm-all.c +18 -0
@@ -1353,6 +1353,11 @@ void kvm_msix_init(PCIDevice *dev)void kvm_msix_free (PCIDevice *dev) {int vector, changed =0 ; + +if (!(kvm_enabled () &&kvm_irqchip_in_kernel ())) { +return ; + } +for (vector =0 ; vector < dev->msix_entries_nr; ++vector) {if (dev->msix_entry_used[vector]) {kvm_del_routing_entry (kvm_context, &dev->msix_irq_entries[vector]);
@@ -1380,6 +1385,11 @@ void kvm_msix_update(PCIDevice *dev, int vector, {struct kvm_irq_routing_entry e = {}, *entry;int mask_cleared = was_masked && !is_masked; + +if (!(kvm_enabled () &&kvm_irqchip_in_kernel ())) { +return ; + } +/* It is only legal to change an entry when it is masked. Therefore, it is * enough to update the routing in kernel when mask is being cleared. */ if (!mask_cleared) {
@@ -1414,6 +1424,10 @@ int kvm_msix_add(PCIDevice *dev, unsigned vector)struct kvm_irq_routing_entry *entry = dev->msix_irq_entries + vector;int r; +if (!(kvm_enabled () &&kvm_irqchip_in_kernel ())) { +return 0 ; + } +if (!kvm_has_gsi_routing (kvm_context)) {fprintf (stderr,"Warning: no MSI-X support found. " "At least kernel 2.6.30 is required for MSI-X support.\n"
@@ -1444,6 +1458,10 @@ int kvm_msix_add(PCIDevice *dev, unsigned vector)void kvm_msix_del (PCIDevice *dev,unsigned vector) { +if (!(kvm_enabled () &&kvm_irqchip_in_kernel ())) { +return ; + } +if (dev->msix_entry_used[vector]) {return ; }
kvm-stub.c +1 -1
@@ -158,7 +158,7 @@ void kvm_msix_update(PCIDevice *dev, int vector,int kvm_msix_add (PCIDevice *dev,unsigned vector) { -return -1 ; +return 0 ; }void kvm_msix_del (PCIDevice *dev,unsigned vector)
Signature · 1 line
1.7.2.3
PATCH 8/9 Protect qemu-kvm.h declarations with NEED_CPU_H
In reply to Avi KivityTarget-specific definitions need to be qualified with NEED_CPU_H so kvm.h can be included from non-target-specific files.
--- kvm-stub.c | 1 + qemu-kvm.h | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletions(-)
kvm-stub.c +1 -0
@@ -169,3 +169,4 @@ bool kvm_msix_notify(PCIDevice *dev, unsigned vector) {return false; } +
qemu-kvm.h +20 -1
@@ -8,7 +8,9 @@#ifndef THE_ORIGINAL_AND_TRUE_QEMU_KVM_H #define THE_ORIGINAL_AND_TRUE_QEMU_KVM_H +#ifdef NEED_CPU_H #include "cpu.h" +#endif #include <signal.h> #include <stdlib.h>
@@ -794,6 +796,8 @@ typedef struct kvm_vcpu_context *kvm_vcpu_context_t;struct kvm_pit_state { }; +#ifdef NEED_CPU_H + static inline void kvm_inject_x86_mce(CPUState *cenv, int bank, uint64_t status, uint64_t mcg_status, uint64_t addr, uint64_t misc,
@@ -803,8 +807,9 @@ static inline void kvm_inject_x86_mce(CPUState *cenv, int bank,abort (); } -#endif /* !CONFIG_KVM */ +#endif +#endif /* !CONFIG_KVM */ /*! * \brief Create new KVM context
@@ -820,9 +825,11 @@ int kvm_init(int smp_cpus);int kvm_main_loop (void );int kvm_init_ap (void ); +#ifdef NEED_CPU_H int kvm_vcpu_inited (CPUState *env);void kvm_save_lapic (CPUState *env);void kvm_load_lapic (CPUState *env); +#endif void kvm_hpet_enable_kpit (void );void kvm_hpet_disable_kpit (void );
@@ -830,11 +837,13 @@ int kvm_set_irq(int irq, int level, int *status);int kvm_physical_memory_set_dirty_tracking (int enable); +#ifdef NEED_CPU_H void qemu_kvm_call_with_env (void (*func)(void *),void *data,CPUState *env);void qemu_kvm_cpuid_on_env (CPUState *env);void kvm_inject_interrupt (CPUState *env,int mask);void kvm_update_after_sipi (CPUState *env);void kvm_update_interrupt_request (CPUState *env); +#endif #ifndef CONFIG_USER_ONLY void *kvm_cpu_create_phys_mem (target_phys_addr_t start_addr,unsigned long size,int log,int writable);
@@ -850,6 +859,7 @@ int kvm_qemu_destroy_memory_alias(uint64_t phys_start);int kvm_arch_qemu_create_context (void ); +#ifdef NEED_CPU_H void kvm_arch_save_regs (CPUState *env);void kvm_arch_load_regs (CPUState *env,int level);int kvm_arch_has_work (CPUState *env);
@@ -858,12 +868,15 @@ int kvm_arch_try_push_interrupts(void *opaque);void kvm_arch_push_nmi (void *opaque);void kvm_arch_cpu_reset (CPUState *env);int kvm_set_boot_cpu_id (uint32_t id); +#endif void qemu_kvm_aio_wait_start (void );void qemu_kvm_aio_wait (void );void qemu_kvm_aio_wait_end (void ); +#ifdef NEED_CPU_H void kvm_tpr_access_report (CPUState *env,uint64_t rip,int is_write); +#endif int kvm_arch_init_irq_routing (void );
@@ -873,15 +886,20 @@ int kvm_mmio_write(void *opaque, uint64_t addr, uint8_t * data, int len);#ifdef CONFIG_KVM_DEVICE_ASSIGNMENT struct ioperm_data ; +#ifdef NEED_CPU_H void kvm_ioperm (CPUState *env,void *data);void kvm_add_ioperm_data (struct ioperm_data *data);void kvm_remove_ioperm_data (unsigned long start_port,unsigned long num);void kvm_arch_do_ioperm (void *_data);#endif +#endif #define ALIGN (x, y) (((x)+(y)-1) & ~((y)-1)) +#ifdef NEED_CPU_H #define BITMAP_SIZE (m) (ALIGN(((m)>>TARGET_PAGE_BITS), HOST_LONG_BITS) / 8) +#endif +#ifdef NEED_CPU_H #ifdef CONFIG_KVM #include "qemu-queue.h"
@@ -915,6 +933,7 @@ int handle_tpr_access(void *opaque, CPUState *env, uint64_t rip,#endif #define qemu_kvm_cpu_stop (env) do {} while(0)#endif +#endif #ifdef CONFIG_KVM
Signature · 1 line
1.7.2.3
PATCH 9/9 Move msix.o build back to Makefile.objs
In reply to Avi KivityNow that hw/msix.c does not depend on CONFIG_KVM, remove it from Makefile.target and put it back in Makefile.objs.
--- Makefile.objs | 4 +--- Makefile.target | 4 ---- 2 files changed, 1 insertions(+), 7 deletions(-)
Makefile.objs +1 -3
@@ -196,9 +196,7 @@ hw-obj-$(CONFIG_PIIX4) += piix4.o # PCI watchdog devices hw-obj-y += wdt_i6300esb.o -# MSI-X depends on kvm for interrupt injection, -# so moved it from Makefile.objs to Makefile.target for now -# hw-obj-y += msix.o +hw-obj-y += msix.o # PCI network cards hw-obj-y += ne2000.o
Makefile.target +0 -4
@@ -183,10 +183,6 @@ obj-y += rwhandler.o obj-$(CONFIG_KVM) += kvm.o kvm-all.o obj-$(CONFIG_NO_KVM) += kvm-stub.o -# MSI-X depends on kvm for interrupt injection, -# so moved it from Makefile.objs to Makefile.target for now -obj-y += msix.o - LIBS+=-lz QEMU_CFLAGS += $(VNC_TLS_CFLAGS)
Signature · 1 line
1.7.2.3
PATCH 6/9 msix: move kvm support functions to kvm-all.c and kvm-stub.c
In reply to Avi KivityThis removes the dependency on CONFIG_KVM.
--- hw/msix.c | 137 ------------------------------------------------------------ kvm-all.c | 124 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ kvm-stub.c | 28 ++++++++++++ kvm.h | 16 +++++++ 4 files changed, 168 insertions(+), 137 deletions(-)
hw/msix.c +0 -137
@@ -19,143 +19,6 @@/* Flag for interrupt controller to declare MSI-X support */ int msix_supported; -#ifdef CONFIG_KVM -/* KVM specific MSIX helpers */ - -static void kvm_msix_init (PCIDevice *dev) -{ -#ifdef KVM_CAP_IRQCHIP -if (kvm_enabled () &&kvm_irqchip_in_kernel ()) { - dev->msix_irq_entries =qemu_malloc (dev->msix_entries_nr * -sizeof *dev->msix_irq_entries); - } -#endif -} - -static void kvm_msix_free (PCIDevice *dev) -{ -int vector, changed =0 ; -for (vector =0 ; vector < dev->msix_entries_nr; ++vector) { -if (dev->msix_entry_used[vector]) { -kvm_del_routing_entry (kvm_context, &dev->msix_irq_entries[vector]); - changed =1 ; - } - } -if (changed) { -kvm_commit_irq_routes (kvm_context); - } -} - -static void kvm_msix_routing_entry (PCIDevice *dev,unsigned vector, -struct kvm_irq_routing_entry *entry) -{ -uint8_t *table_entry = dev->msix_table_page + vector *MSIX_ENTRY_SIZE ; - entry->type =KVM_IRQ_ROUTING_MSI ; - entry->flags =0 ; - entry->u.msi.address_lo =pci_get_long (table_entry +MSIX_MSG_ADDR ); - entry->u.msi.address_hi =pci_get_long (table_entry +MSIX_MSG_UPPER_ADDR ); - entry->u.msi.data =pci_get_long (table_entry +MSIX_MSG_DATA ); -} - -static void kvm_msix_update (PCIDevice *dev,int vector, -int was_masked,int is_masked) -{ -struct kvm_irq_routing_entry e = {}, *entry; -int mask_cleared = was_masked && !is_masked; -/* It is only legal to change an entry when it is masked. Therefore, it is -* enough to update the routing in kernel when mask is being cleared. */ -if (!mask_cleared) { -return ; - } -if (!dev->msix_entry_used[vector]) { -return ; - } - entry = dev->msix_irq_entries + vector; - e.gsi = entry->gsi; -kvm_msix_routing_entry (dev, vector, &e); -if (memcmp (&entry->u.msi, &e.u.msi,sizeof entry->u.msi)) { -int r; - r =kvm_update_routing_entry (kvm_context, entry, &e); -if (r) { -fprintf (stderr,"%s: kvm_update_routing_entry failed: %s\n" , __func__, -strerror (-r)); -exit (1 ); - } -memcpy (&entry->u.msi, &e.u.msi,sizeof entry->u.msi); - r =kvm_commit_irq_routes (kvm_context); -if (r) { -fprintf (stderr,"%s: kvm_commit_irq_routes failed: %s\n" , __func__, -strerror (-r)); -exit (1 ); - } - } -} - -static int kvm_msix_add (PCIDevice *dev,unsigned vector) -{ -struct kvm_irq_routing_entry *entry = dev->msix_irq_entries + vector; -int r; - -if (!kvm_has_gsi_routing (kvm_context)) { -fprintf (stderr,"Warning: no MSI-X support found. " -"At least kernel 2.6.30 is required for MSI-X support.\n" - ); -return -EOPNOTSUPP ; - } - - r =kvm_get_irq_route_gsi (kvm_context); -if (r <0 ) { -fprintf (stderr,"%s: kvm_get_irq_route_gsi failed: %s\n" , __func__,strerror (-r)); -return r; - } - entry->gsi = r; -kvm_msix_routing_entry (dev, vector, entry); - r =kvm_add_routing_entry (kvm_context, entry); -if (r <0 ) { -fprintf (stderr,"%s: kvm_add_routing_entry failed: %s\n" , __func__,strerror (-r)); -return r; - } - - r =kvm_commit_irq_routes (kvm_context); -if (r <0 ) { -fprintf (stderr,"%s: kvm_commit_irq_routes failed: %s\n" , __func__,strerror (-r)); -return r; - } -return 0 ; -} - -static void kvm_msix_del (PCIDevice *dev,unsigned vector) -{ -if (dev->msix_entry_used[vector]) { -return ; - } -kvm_del_routing_entry (kvm_context, &dev->msix_irq_entries[vector]); -kvm_commit_irq_routes (kvm_context); -} - -static bool kvm_msix_notify (PCIDevice *dev,unsigned vector) -{ -#ifdef KVM_CAP_IRQCHIP -if (kvm_enabled () &&kvm_irqchip_in_kernel ()) { -kvm_set_irq (dev->msix_irq_entries[vector].gsi,1 ,NULL ); -return true; - } -#endif -return false; -} - -#else - -static void kvm_msix_init (PCIDevice *dev) {} -static void kvm_msix_free (PCIDevice *dev) {} -static void kvm_msix_update (PCIDevice *dev,int vector, -int was_masked,int is_masked) {} -static int kvm_msix_add (PCIDevice *dev,unsigned vector) {return -1 ; } -static void kvm_msix_del (PCIDevice *dev,unsigned vector) {} -static bool kvm_msix_notify (PCIDevice *dev,unsigned vector) {return false; } - -#endif -/* Add MSI-X capability to the config space for the device. */ /* Given a bar and its size, add MSI-X table on top of it * and fill MSI-X capability in the config space.
kvm-all.c +124 -0
135 lines not on this page · raw message
kvm-stub.c +28 -0
@@ -13,6 +13,7 @@#include "qemu-common.h" #include "sysemu.h" #include "hw/hw.h" +#include "hw/pci.h" #include "exec-all.h" #include "gdbstub.h" #include "kvm.h"
@@ -141,3 +142,30 @@ int kvm_set_ioeventfd_mmio_long(int fd, uint32_t adr, uint32_t val, bool assign) {return -ENOSYS ; } + +void kvm_msix_init (PCIDevice *dev) +{ +} + +void kvm_msix_free (PCIDevice *dev) +{ +} + +void kvm_msix_update (PCIDevice *dev,int vector, +int was_masked,int is_masked) +{ +} + +int kvm_msix_add (PCIDevice *dev,unsigned vector) +{ +return -1 ; +} + +void kvm_msix_del (PCIDevice *dev,unsigned vector) +{ +} + +bool kvm_msix_notify (PCIDevice *dev,unsigned vector) +{ +return false; +}
kvm.h +16 -0
@@ -18,6 +18,9 @@#include "config-host.h" #include "qemu-queue.h" #include "qemu-kvm.h" +#ifndef __DYNGEN_EXEC_H__ +#include "hw/pci.h" +#endif #ifdef CONFIG_KVM #include <linux/kvm.h>
@@ -194,5 +197,18 @@ int kvm_set_irqfd(int gsi, int fd, bool assigned) }#endif +#ifndef __DYNGEN_EXEC_H__ +int kvm_set_ioeventfd_pio_word (int fd,uint16_t adr,uint16_t val,bool assign); + +void kvm_msix_init (PCIDevice *dev); +void kvm_msix_free (PCIDevice *dev); +void kvm_msix_update (PCIDevice *dev,int vector, +int was_masked,int is_masked); +int kvm_msix_add (PCIDevice *dev,unsigned vector); +void kvm_msix_del (PCIDevice *dev,unsigned vector); +bool kvm_msix_notify (PCIDevice *dev,unsigned vector); + +#endif +#endif
Signature · 1 line
1.7.2.3
Re: [PATCH 4/9] msix: move definitions from msix.c to msix.h
In reply to Avi KivityQuoted 3 lines · Avi Kivity
This allows us to reuse them from the kvm support code.
Signed-off-by Avi Kivity <[email protected]>
I would rather all dealings with MSI-X table stayed in one place. All we need is just the entry, so let's add APIs to retrieve MSIX address and data:
uint64_t msix_get_address(dev, vector)uint32_t msix_get_data(dev, vector)
and that will be enough for KVM.
Quoted 82 lines
--- hw/msix.c | 27 --------------------------- hw/msix.h | 26 ++++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 27 deletions(-)hw/msix.c +0
-27@@ -16,33 +16,6 @@#include "pci.h" #include "kvm.h" -/* MSI-X capability structure */ -#define MSIX_TABLE_OFFSET 4 -#define MSIX_PBA_OFFSET 8 -#define MSIX_CAP_LENGTH 12 - -/* MSI enable bit and maskall bit are in byte 1 in FLAGS register */ -#define MSIX_CONTROL_OFFSET (PCI_MSIX_FLAGS + 1) -#define MSIX_ENABLE_MASK (PCI_MSIX_FLAGS_ENABLE >> 8) -#define MSIX_MASKALL_MASK (PCI_MSIX_FLAGS_MASKALL >> 8) - -/* MSI-X table format */ -#define MSIX_MSG_ADDR 0 -#define MSIX_MSG_UPPER_ADDR 4 -#define MSIX_MSG_DATA 8 -#define MSIX_VECTOR_CTRL 12 -#define MSIX_ENTRY_SIZE 16 -#define MSIX_VECTOR_MASK 0x1 - -/* How much space does an MSIX table need. */ -/* The spec requires giving the table structure -* a 4K aligned region all by itself. */ -#define MSIX_PAGE_SIZE 0x1000 -/* Reserve second half of the page for pending bits */ -#define MSIX_PAGE_PENDING (MSIX_PAGE_SIZE / 2) -#define MSIX_MAX_ENTRIES 32 - -/* Flag for interrupt controller to declare MSI-X support */ int msix_supported;hw/msix.h +26
-0@@ -4,6 +4,32 @@#include "qemu-common.h" #include "pci.h" +/* MSI-X capability structure */ +#define MSIX_TABLE_OFFSET 4 +#define MSIX_PBA_OFFSET 8 +#define MSIX_CAP_LENGTH 12 + +/* MSI enable bit and maskall bit are in byte 1 in FLAGS register */ +#define MSIX_CONTROL_OFFSET (PCI_MSIX_FLAGS + 1) +#define MSIX_ENABLE_MASK (PCI_MSIX_FLAGS_ENABLE >> 8) +#define MSIX_MASKALL_MASK (PCI_MSIX_FLAGS_MASKALL >> 8) + +/* MSI-X table format */ +#define MSIX_MSG_ADDR 0 +#define MSIX_MSG_UPPER_ADDR 4 +#define MSIX_MSG_DATA 8 +#define MSIX_VECTOR_CTRL 12 +#define MSIX_ENTRY_SIZE 16 +#define MSIX_VECTOR_MASK 0x1 + +/* How much space does an MSIX table need. */ +/* The spec requires giving the table structure +* a 4K aligned region all by itself. */ +#define MSIX_PAGE_SIZE 0x1000 +/* Reserve second half of the page for pending bits */ +#define MSIX_PAGE_PENDING (MSIX_PAGE_SIZE / 2) +#define MSIX_MAX_ENTRIES 32 +int msix_init (PCIDevice *pdev,unsigned short nentries,unsigned bar_nr,unsigned bar_size);Signature · 1 line
1.7.2.3
Quoted 2 lines · Avi Kivity
This cleans up msix/kvm integration a bit. The really important patch is the last one, which allows msix.o to be part of non-target-specific build.
I actually thoought this later move should be done in a different way:
- add all functions msix uses to kvm-stub.c
- kvm_irq_routing_entry should also have a stub
I sent some minor comments in case you have a reason to prefer this way.
Quoted 24 lines
Avi Kivity (9):
msix: avoid leaking kvm data on init failure
msix: make kvm specific initialization a function
msix: move kvm specific msix notify into a function
msix: move definitions frommsix.ctomsix.h
Add missing #include tohw/irq.h
msix: move kvm support functions tokvm-all.candkvm-stub.c
msix: Movekvm_enabled()guards tokvm-all.cfunctions Protectqemu-kvm.hdeclarations withNEED_CPU_HMove msix.o build back to Makefile.objsMakefile.objs | 4 +- Makefile.target | 4 - hw/irq.h | 2 + hw/msix.c | 173 ++++--------------------------------------------------- hw/msix.h | 26 ++++++++ kvm-all.c | 142 +++++++++++++++++++++++++++++++++++++++++++++ kvm-stub.c | 29 +++++++++ kvm.h | 16 +++++ qemu-kvm.h | 21 ++++++- 9 files changed, 247 insertions(+), 170 deletions(-)Signature · 1 line
1.7.2.3
Re: [PATCH 8/9] Protect qemu-kvm.h declarations with NEED_CPU_H
In reply to Avi KivityQuoted 4 lines · Avi Kivity
Target-specific definitions need to be qualified with
NEED_CPU_Hsokvm.hcan be included from non-target-specific files.Signed-off-by Avi Kivity <[email protected]>
Long term, would be cleaner to split this into two files ...
Quoted 10 lines
--- kvm-stub.c | 1 + qemu-kvm.h | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletions(-)kvm-stub.c +0
-0
kvm-stub.c bool kvm_msix_notify(PCIDevice *dev, unsigned vector) · line 172
Quoted 4 lines
kvm-stub.c +1
-0@@ -169,3 +169,4 @@ bool kvm_msix_notify(PCIDevice *dev, unsigned vector) {return false; } +
intentional?
Quoted 115 lines
qemu-kvm.h +20
-1@@ -8,7 +8,9 @@#ifndef THE_ORIGINAL_AND_TRUE_QEMU_KVM_H #define THE_ORIGINAL_AND_TRUE_QEMU_KVM_H +#ifdef NEED_CPU_H #include "cpu.h" +#endif #include <signal.h> #include <stdlib.h> @@ -794,6 +796,8 @@ typedef struct kvm_vcpu_context *kvm_vcpu_context_t;struct kvm_pit_state { }; +#ifdef NEED_CPU_H + static inline void kvm_inject_x86_mce(CPUState *cenv, int bank, uint64_t status, uint64_t mcg_status, uint64_t addr, uint64_t misc,@@ -803,8 +807,9 @@ static inline void kvm_inject_x86_mce(CPUState *cenv, int bank,abort (); } -#endif /* !CONFIG_KVM */ +#endif +#endif /* !CONFIG_KVM */ /*! * \brief Create new KVM context @@ -820,9 +825,11 @@ int kvm_init(int smp_cpus);int kvm_main_loop (void );int kvm_init_ap (void ); +#ifdef NEED_CPU_H int kvm_vcpu_inited (CPUState *env);void kvm_save_lapic (CPUState *env);void kvm_load_lapic (CPUState *env); +#endif void kvm_hpet_enable_kpit (void );void kvm_hpet_disable_kpit (void );@@ -830,11 +837,13 @@ int kvm_set_irq(int irq, int level, int *status);int kvm_physical_memory_set_dirty_tracking (int enable); +#ifdef NEED_CPU_H void qemu_kvm_call_with_env (void (*func)(void *),void *data,CPUState *env);void qemu_kvm_cpuid_on_env (CPUState *env);void kvm_inject_interrupt (CPUState *env,int mask);void kvm_update_after_sipi (CPUState *env);void kvm_update_interrupt_request (CPUState *env); +#endif #ifndef CONFIG_USER_ONLY void *kvm_cpu_create_phys_mem (target_phys_addr_t start_addr,unsigned long size,int log,int writable);@@ -850,6 +859,7 @@ int kvm_qemu_destroy_memory_alias(uint64_t phys_start);int kvm_arch_qemu_create_context (void ); +#ifdef NEED_CPU_H void kvm_arch_save_regs (CPUState *env);void kvm_arch_load_regs (CPUState *env,int level);int kvm_arch_has_work (CPUState *env);@@ -858,12 +868,15 @@ int kvm_arch_try_push_interrupts(void *opaque);void kvm_arch_push_nmi (void *opaque);void kvm_arch_cpu_reset (CPUState *env);int kvm_set_boot_cpu_id (uint32_t id); +#endif void qemu_kvm_aio_wait_start (void );void qemu_kvm_aio_wait (void );void qemu_kvm_aio_wait_end (void ); +#ifdef NEED_CPU_H void kvm_tpr_access_report (CPUState *env,uint64_t rip,int is_write); +#endif int kvm_arch_init_irq_routing (void );@@ -873,15 +886,20 @@ int kvm_mmio_write(void *opaque, uint64_t addr, uint8_t * data, int len);#ifdef CONFIG_KVM_DEVICE_ASSIGNMENT struct ioperm_data ; +#ifdef NEED_CPU_H void kvm_ioperm (CPUState *env,void *data);void kvm_add_ioperm_data (struct ioperm_data *data);void kvm_remove_ioperm_data (unsigned long start_port,unsigned long num);void kvm_arch_do_ioperm (void *_data);#endif +#endif #define ALIGN (x, y) (((x)+(y)-1) & ~((y)-1)) +#ifdef NEED_CPU_H #define BITMAP_SIZE (m) (ALIGN(((m)>>TARGET_PAGE_BITS), HOST_LONG_BITS) / 8) +#endif +#ifdef NEED_CPU_H #ifdef CONFIG_KVM #include "qemu-queue.h" @@ -915,6 +933,7 @@ int handle_tpr_access(void *opaque, CPUState *env, uint64_t rip,#endif #define qemu_kvm_cpu_stop (env) do {} while(0)#endif +#endif #ifdef CONFIG_KVM Signature · 1 line
1.7.2.3
Re: [PATCH 4/9] msix: move definitions from msix.c to msix.h
In reply to Michael S. TsirkinQuoted 5 lines · Michael S. Tsirkin
Quoted 3 lines · Avi Kivity
This allows us to reuse them from the kvm support code.
Signed-off-by Avi Kivity<[email protected]>
Quoted 9 lines
I would rather all dealings with MSI-X table stayed in one place. All we need is just the entry, so let's add APIs to retrieve MSIX address and data:
uint64_tmsix_get_address(dev, vector)uint32_tmsix_get_data(dev, vector)and that will be enough for KVM.
Ok, will do.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 8/9] Protect qemu-kvm.h declarations with NEED_CPU_H
In reply to Michael S. TsirkinQuoted 7 lines · Michael S. Tsirkin
Quoted 4 lines · Avi Kivity
Target-specific definitions need to be qualified with
NEED_CPU_Hsokvm.hcan be included from non-target-specific files.Signed-off-by Avi Kivity<[email protected]>Long term, would be cleaner to split this into two files ...
Yes, this is a pain to deal with.
Quoted 15 lines
Quoted 10 lines
---
kvm-stub.c | 1 + qemu-kvm.h | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletions(-)diff --git
a/kvm-stub.cb/kvm-stub.c
index 37d2b7a..2e4bf00 100644
---a/kvm-stub.c
+++b/kvm-stub.c
@@ -169,3 +169,4 @@ boolkvm_msix_notify(PCIDevice *dev, unsigned vector)Quoted 4 lines
{ return false;}
+
Quoted 1 line
intentional?
Nope.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 6 lines · Michael S. Tsirkin
Quoted 2 lines · Avi Kivity
This cleans up msix/kvm integration a bit. The really important patch is the last one, which allows msix.o to be part of non-target-specific build.
I actually thoought this later move should be done in a different way:
- add all functions msix uses tokvm-stub.c
Isn't that what I did?
Quoted 4 lines
-
kvm_irq_routing_entryshould also have a stubI sent some minor comments in case you have a reason to prefer this way.
My motivation is really the last patch. If you explain what you'd like to see I'll try to do it.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 10 lines · Avi Kivity
Quoted 6 lines · Michael S. Tsirkin
Quoted 2 lines · Avi Kivity
This cleans up msix/kvm integration a bit. The really important patch is the last one, which allows msix.o to be part of non-target-specific build.
I actually thoought this later move should be done in a different way:
- add all functions msix uses tokvm-stub.cIsn't that what I did?
Quoted 7 lines
Quoted 4 lines
-
kvm_irq_routing_entryshould also have a stubI sent some minor comments in case you have a reason to prefer this way.
My motivation is really the last patch. If you explain what you'd like to see I'll try to do it.
Basically my idea was to avoid all ifdefs in msix.c *without changing it*, by stubbing out kvm APIs and structures we use there.
Signature · 1 line
MST
Quoted 10 lines · Avi Kivity
Quoted 6 lines · Michael S. Tsirkin
Quoted 2 lines · Avi Kivity
This cleans up msix/kvm integration a bit. The really important patch is the last one, which allows msix.o to be part of non-target-specific build.
I actually thoought this later move should be done in a different way:
- add all functions msix uses tokvm-stub.cIsn't that what I did?
Quoted 7 lines
Quoted 4 lines
-
kvm_irq_routing_entryshould also have a stubI sent some minor comments in case you have a reason to prefer this way.
My motivation is really the last patch. If you explain what you'd like to see I'll try to do it.
Still looking at this?
Quoted 2 lines
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 13 lines · Michael S. Tsirkin
Quoted 12 lines · Avi Kivity
Quoted 6 lines · Michael S. Tsirkin
Quoted 2 lines · Avi Kivity
This cleans up msix/kvm integration a bit. The really important patch is the last one, which allows msix.o to be part of non-target-specific build.
I actually thoought this later move should be done in a different way:
- add all functions msix uses tokvm-stub.cIsn't that what I did?
Quoted 2 lines
-
kvm_irq_routing_entryshould also have a stub
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
I sent some minor comments in case you have a reason to prefer this way.
My motivation is really the last patch. If you explain what you'd like to see I'll try to do it.
Still looking at this?
I plan to do this yes, when I get a bit of time.
Signature · 1 line
error compiling committee.c: too many arguments to function