From mboxrd@z Thu Jan 1 00:00:00 1970 From: Jan Kiszka Subject: [PATCH v2] qemu-kvm: Make PC speaker emulation aware of in-kernel PIT Date: Tue, 12 May 2009 09:28:06 +0200 Message-ID: <4A092506.9090308@web.de> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="------------enig5E0D15E23C132E946ACA4170" Cc: kvm-devel To: Avi Kivity Return-path: Received: from fmmailgate02.web.de ([217.72.192.227]:53302 "EHLO fmmailgate02.web.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755953AbZELH2G (ORCPT ); Tue, 12 May 2009 03:28:06 -0400 Sender: kvm-owner@vger.kernel.org List-ID: This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enig5E0D15E23C132E946ACA4170 Content-Type: text/plain; charset=ISO-8859-15 Content-Transfer-Encoding: quoted-printable When using the in-kernel PIT the speaker emulation has to synchronize the PIT state with KVM. Enhance the existing speaker sound device and allow it to take over port 0x61 by using KVM_CREATE_PIT2 where available. This unbreaks -soundhw pcspk in KVM mode. Changes in v2: - rebased over qemu-kvm and KVM_CREATE_PIT2 - refactored hooks in pcspk Signed-off-by: Jan Kiszka --- hw/pcspk.c | 41 ++++++++++++++++++++++++++++++++++= ++++++ kvm/kernel/include/linux/kvm.h | 10 ++++++++++ kvm/libkvm/libkvm-x86.c | 26 ++++++++++++++++++------- 3 files changed, 69 insertions(+), 8 deletions(-) diff --git a/hw/pcspk.c b/hw/pcspk.c index ec1d0c6..14dd540 100644 --- a/hw/pcspk.c +++ b/hw/pcspk.c @@ -27,6 +27,8 @@ #include "isa.h" #include "audio/audio.h" #include "qemu-timer.h" +#include "i8254.h" +#include "qemu-kvm.h" =20 #define PCSPK_BUF_LEN 1792 #define PCSPK_SAMPLE_RATE 32000 @@ -48,6 +50,37 @@ typedef struct { static const char *s_spk =3D "pcspk"; static PCSpkState pcspk_state; =20 +#ifdef USE_KVM_PIT +static void kvm_get_pit_ch2(PITState *pit) +{ + struct kvm_pit_state pit_state; + + if (qemu_kvm_pit_in_kernel()) { + kvm_get_pit(kvm_context, &pit_state); + pit->channels[2].mode =3D pit_state.channels[2].mode; + pit->channels[2].count =3D pit_state.channels[2].count; + pit->channels[2].count_load_time =3D pit_state.channels[2].count= _load_time; + pit->channels[2].gate =3D pit_state.channels[2].gate; + } +} + +static void kvm_set_pit_ch2(PITState *pit) +{ + struct kvm_pit_state pit_state; + + if (qemu_kvm_pit_in_kernel()) { + pit_state.channels[2].mode =3D pit->channels[2].mode; + pit_state.channels[2].count =3D pit->channels[2].count; + pit_state.channels[2].count_load_time =3D pit->channels[2].count= _load_time; + pit_state.channels[2].gate =3D pit->channels[2].gate; + kvm_set_pit(kvm_context, &pit_state); + } +} +#else +static inline void kvm_get_pit_ch2(PITState *pit) { } +static inline void kvm_set_pit_ch2(PITState *pit) { } +#endif + static inline void generate_samples(PCSpkState *s) { unsigned int i; @@ -72,6 +105,8 @@ static void pcspk_callback(void *opaque, int free) PCSpkState *s =3D opaque; unsigned int n; =20 + kvm_get_pit_ch2(s->pit); + if (pit_get_mode(s->pit, 2) !=3D 3) return; =20 @@ -121,6 +156,8 @@ static uint32_t pcspk_ioport_read(void *opaque, uint3= 2_t addr) PCSpkState *s =3D opaque; int out; =20 + kvm_get_pit_ch2(s->pit); + s->dummy_refresh_clock ^=3D (1 << 4); out =3D pit_get_out(s->pit, 2, qemu_get_clock(vm_clock)) << 5; =20 @@ -132,6 +169,8 @@ static void pcspk_ioport_write(void *opaque, uint32_t= addr, uint32_t val) PCSpkState *s =3D opaque; const int gate =3D val & 1; =20 + kvm_get_pit_ch2(s->pit); + s->data_on =3D (val >> 1) & 1; pit_set_gate(s->pit, 2, gate); if (s->voice) { @@ -139,6 +178,8 @@ static void pcspk_ioport_write(void *opaque, uint32_t= addr, uint32_t val) s->play_pos =3D 0; AUD_set_active_out(s->voice, gate & s->data_on); } + + kvm_set_pit_ch2(s->pit); } =20 void pcspk_init(PITState *pit) diff --git a/kvm/kernel/include/linux/kvm.h b/kvm/kernel/include/linux/kv= m.h index f5e9d66..9ed4892 100644 --- a/kvm/kernel/include/linux/kvm.h +++ b/kvm/kernel/include/linux/kvm.h @@ -110,6 +110,14 @@ struct kvm_irqchip { } chip; }; =20 +/* for KVM_CREATE_PIT2 */ +struct kvm_pit_config { + __u32 flags; + __u32 pad; +}; + +#define KVM_PIT_SPEAKER_DUMMY 1 + #define KVM_EXIT_UNKNOWN 0 #define KVM_EXIT_EXCEPTION 1 #define KVM_EXIT_IO 2 @@ -455,6 +463,7 @@ struct kvm_trace_rec { #define KVM_CAP_ASSIGN_DEV_IRQ 29 /* Another bug in KVM_SET_USER_MEMORY_REGION fixed: */ #define KVM_CAP_JOIN_MEMORY_REGIONS_WORKS 30 +#define KVM_CAP_PIT2 31 =20 #ifdef KVM_CAP_IRQ_ROUTING =20 @@ -538,6 +547,7 @@ struct kvm_irq_routing { #define KVM_ASSIGN_SET_MSIX_ENTRY \ _IOW(KVMIO, 0x74, struct kvm_assigned_msix_entry) #define KVM_DEASSIGN_DEV_IRQ _IOW(KVMIO, 0x75, struct kvm_assigned= _irq) +#define KVM_CREATE_PIT2 _IOW(KVMIO, 0x76, struct kvm_pit_config) =20 /* * ioctls for vcpu fds diff --git a/kvm/libkvm/libkvm-x86.c b/kvm/libkvm/libkvm-x86.c index a2f6320..9c0d967 100644 --- a/kvm/libkvm/libkvm-x86.c +++ b/kvm/libkvm/libkvm-x86.c @@ -59,16 +59,26 @@ static int kvm_create_pit(kvm_context_t kvm) =20 kvm->pit_in_kernel =3D 0; if (!kvm->no_pit_creation) { - r =3D ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_PIT); - if (r > 0) { +#ifdef KVM_CAP_PIT2 + struct kvm_pit_config config =3D { .flags =3D 0 }; + + r =3D ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_PIT2); + if (r > 0) + r =3D ioctl(kvm->vm_fd, KVM_CREATE_PIT2, &config); + else +#endif + { + r =3D ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_PIT); + if (r <=3D 0) + return 0; + r =3D ioctl(kvm->vm_fd, KVM_CREATE_PIT); - if (r >=3D 0) - kvm->pit_in_kernel =3D 1; - else { - fprintf(stderr, "Create kernel PIC irqchip failed\n"); - return r; - } } + if (r < 0) { + fprintf(stderr, "Create kernel PIC irqchip failed\n"); + return r; + } + kvm->pit_in_kernel =3D 1; } #endif return 0; --------------enig5E0D15E23C132E946ACA4170 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.9 (GNU/Linux) Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org iEYEARECAAYFAkoJJQYACgkQniDOoMHTA+ktjwCfZCwTqMqhHVKVsFExxNGiwETj 8gQAn2PfN246GPU626z01eb3wwO+ChkG =n8sS -----END PGP SIGNATURE----- --------------enig5E0D15E23C132E946ACA4170--