qemu-kvm: Couple in-kernel PIT enabling to in-kernel irqchip
[qemu-kvm.git] / hw / pcspk.c
blobc9f311d64f05b92d31e25b58623d73d44ffc0630
1 /*
2 * QEMU PC speaker emulation
4 * Copyright (c) 2006 Joachim Henke
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "hw.h"
26 #include "pc.h"
27 #include "isa.h"
28 #include "audio/audio.h"
29 #include "qemu-timer.h"
30 #include "i8254.h"
31 #include "qemu-kvm.h"
33 #define PCSPK_BUF_LEN 1792
34 #define PCSPK_SAMPLE_RATE 32000
35 #define PCSPK_MAX_FREQ (PCSPK_SAMPLE_RATE >> 1)
36 #define PCSPK_MIN_COUNT ((PIT_FREQ + PCSPK_MAX_FREQ - 1) / PCSPK_MAX_FREQ)
38 typedef struct {
39 uint8_t sample_buf[PCSPK_BUF_LEN];
40 QEMUSoundCard card;
41 SWVoiceOut *voice;
42 ISADevice *pit;
43 unsigned int pit_count;
44 unsigned int samples;
45 unsigned int play_pos;
46 int data_on;
47 int dummy_refresh_clock;
48 } PCSpkState;
50 static const char *s_spk = "pcspk";
51 static PCSpkState pcspk_state;
53 #ifdef CONFIG_KVM_PIT
54 static void kvm_get_pit_ch2(ISADevice *dev,
55 struct kvm_pit_state *inkernel_state)
57 struct PITState *pit = DO_UPCAST(struct PITState, dev, dev);
58 struct kvm_pit_state pit_state;
60 if (kvm_enabled() && kvm_irqchip_in_kernel()) {
61 kvm_get_pit(kvm_state, &pit_state);
62 pit->channels[2].mode = pit_state.channels[2].mode;
63 pit->channels[2].count = pit_state.channels[2].count;
64 pit->channels[2].count_load_time = pit_state.channels[2].count_load_time;
65 pit->channels[2].gate = pit_state.channels[2].gate;
66 if (inkernel_state) {
67 memcpy(inkernel_state, &pit_state, sizeof(*inkernel_state));
72 static void kvm_set_pit_ch2(ISADevice *dev,
73 struct kvm_pit_state *inkernel_state)
75 struct PITState *pit = DO_UPCAST(struct PITState, dev, dev);
77 if (kvm_enabled() && kvm_irqchip_in_kernel()) {
78 inkernel_state->channels[2].mode = pit->channels[2].mode;
79 inkernel_state->channels[2].count = pit->channels[2].count;
80 inkernel_state->channels[2].count_load_time =
81 pit->channels[2].count_load_time;
82 inkernel_state->channels[2].gate = pit->channels[2].gate;
83 kvm_set_pit(kvm_state, inkernel_state);
86 #else
87 static inline void kvm_get_pit_ch2(ISADevice *dev,
88 struct kvm_pit_state *inkernel_state) { }
89 static inline void kvm_set_pit_ch2(ISADevice *dev,
90 struct kvm_pit_state *inkernel_state) { }
91 #endif
93 static inline void generate_samples(PCSpkState *s)
95 unsigned int i;
97 if (s->pit_count) {
98 const uint32_t m = PCSPK_SAMPLE_RATE * s->pit_count;
99 const uint32_t n = ((uint64_t)PIT_FREQ << 32) / m;
101 /* multiple of wavelength for gapless looping */
102 s->samples = (PCSPK_BUF_LEN * PIT_FREQ / m * m / (PIT_FREQ >> 1) + 1) >> 1;
103 for (i = 0; i < s->samples; ++i)
104 s->sample_buf[i] = (64 & (n * i >> 25)) - 32;
105 } else {
106 s->samples = PCSPK_BUF_LEN;
107 for (i = 0; i < PCSPK_BUF_LEN; ++i)
108 s->sample_buf[i] = 128; /* silence */
112 static void pcspk_callback(void *opaque, int free)
114 PCSpkState *s = opaque;
115 unsigned int n;
117 kvm_get_pit_ch2(s->pit, NULL);
119 if (pit_get_mode(s->pit, 2) != 3)
120 return;
122 n = pit_get_initial_count(s->pit, 2);
123 /* avoid frequencies that are not reproducible with sample rate */
124 if (n < PCSPK_MIN_COUNT)
125 n = 0;
127 if (s->pit_count != n) {
128 s->pit_count = n;
129 s->play_pos = 0;
130 generate_samples(s);
133 while (free > 0) {
134 n = audio_MIN(s->samples - s->play_pos, (unsigned int)free);
135 n = AUD_write(s->voice, &s->sample_buf[s->play_pos], n);
136 if (!n)
137 break;
138 s->play_pos = (s->play_pos + n) % s->samples;
139 free -= n;
143 int pcspk_audio_init(ISABus *bus)
145 PCSpkState *s = &pcspk_state;
146 struct audsettings as = {PCSPK_SAMPLE_RATE, 1, AUD_FMT_U8, 0};
148 AUD_register_card(s_spk, &s->card);
150 s->voice = AUD_open_out(&s->card, s->voice, s_spk, s, pcspk_callback, &as);
151 if (!s->voice) {
152 AUD_log(s_spk, "Could not open voice\n");
153 return -1;
156 return 0;
159 static uint32_t pcspk_ioport_read(void *opaque, uint32_t addr)
161 PCSpkState *s = opaque;
162 int out;
164 kvm_get_pit_ch2(s->pit, NULL);
166 s->dummy_refresh_clock ^= (1 << 4);
167 out = pit_get_out(s->pit, 2, qemu_get_clock_ns(vm_clock)) << 5;
169 return pit_get_gate(s->pit, 2) | (s->data_on << 1) | s->dummy_refresh_clock | out;
172 static void pcspk_ioport_write(void *opaque, uint32_t addr, uint32_t val)
174 struct kvm_pit_state inkernel_state;
175 PCSpkState *s = opaque;
176 const int gate = val & 1;
178 kvm_get_pit_ch2(s->pit, &inkernel_state);
180 s->data_on = (val >> 1) & 1;
181 pit_set_gate(s->pit, 2, gate);
182 if (s->voice) {
183 if (gate) /* restart */
184 s->play_pos = 0;
185 AUD_set_active_out(s->voice, gate & s->data_on);
188 kvm_set_pit_ch2(s->pit, &inkernel_state);
191 void pcspk_init(ISADevice *pit)
193 PCSpkState *s = &pcspk_state;
195 s->pit = pit;
196 register_ioport_read(0x61, 1, 1, pcspk_ioport_read, s);
197 register_ioport_write(0x61, 1, 1, pcspk_ioport_write, s);