Commit d61a4ce8 authored by Gerd Hoffmann's avatar Gerd Hoffmann Committed by malc

Add Intel HD Audio support to qemu.

This patch adds three devices to qemu:

intel-hda
	Intel HD Audio Controller, the PCI device.  Provides a HDA bus.
	Emulates ICH6 at the moment.  Adding a ICH9 PCIE
	variant shouldn't be hard.

hda-duplex
	HDA Codec.  Attaches to the HDA bus.  Supports 16bit stereo,
	rates 16k -> 96k, playback, recording and volume control
	(with CONFIG_MIXEMU=y).

hda-output
	HDA Codec without recording support.  Subset of the hda-duplex
	codec.  Use this if you don't want your guests access your mic.

Usage: add '-device intel-hda -device hda-duplex' to your command line.

Tested guests:
 * Linux works.
 * Win7 works.
 * DOS (mpxplay) works.
 * WinXP doesn't work.

[ v2 changes ]
 * Fixed endianess, big endian hosts work now.
 * Fixed some emulation bugs.
 * Added immediate command emulation.
 * Added vmstate support.
 * Make it behave like all other sound card drivers:
   - can be configured via '--audio-card-list=hda'
   - can be added to a VM using '-soundhw hda'
 * Code style fixups.
 * Zapped guest-triggerable asserts.
 * Handle partial reads/writes of audio data correctly.

Cc: malc <av1474@comtv.ru>
Signed-off-by: default avatarGerd Hoffmann <kraxel@redhat.com>
Signed-off-by: default avatarmalc <av1474@comtv.ru>
parent cdae5cfb
......@@ -253,6 +253,7 @@ sound-obj-$(CONFIG_AC97) += ac97.o
sound-obj-$(CONFIG_ADLIB) += fmopl.o adlib.o
sound-obj-$(CONFIG_GUS) += gus.o gusemu_hal.o gusemu_mixer.o
sound-obj-$(CONFIG_CS4231A) += cs4231a.o
sound-obj-$(CONFIG_HDA) += intel-hda.o hda-audio.o
adlib.o fmopl.o: QEMU_CFLAGS += -DBUILD_Y8950=0
hw-obj-$(CONFIG_SOUND) += $(sound-obj-y)
......
......@@ -499,6 +499,16 @@ struct soundhw soundhw[] = {
},
#endif
#ifdef CONFIG_HDA
{
"hda",
"Intel HD Audio",
0,
0,
{ .init_pci = intel_hda_and_codec_init }
},
#endif
#endif /* HAS_AUDIO_CHOICE */
{ NULL, NULL, 0, 0, { NULL } }
......
......@@ -76,8 +76,8 @@ sparc_cpu=""
cross_prefix=""
cc="gcc"
audio_drv_list=""
audio_card_list="ac97 es1370 sb16"
audio_possible_cards="ac97 es1370 sb16 cs4231a adlib gus"
audio_card_list="ac97 es1370 sb16 hda"
audio_possible_cards="ac97 es1370 sb16 cs4231a adlib gus hda"
block_drv_whitelist=""
host_cc="gcc"
ar="ar"
......
......@@ -15,3 +15,6 @@ int ac97_init(PCIBus *buf);
/* cs4231a.c */
int cs4231a_init(qemu_irq *pic);
/* intel-hda.c + hda-audio.c */
int intel_hda_and_codec_init(PCIBus *bus);
/*
* Copyright (C) 2010 Red Hat, Inc.
*
* written by Gerd Hoffmann <kraxel@redhat.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 or
* (at your option) version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "hw.h"
#include "pci.h"
#include "intel-hda.h"
#include "intel-hda-defs.h"
#include "audio/audio.h"
/* -------------------------------------------------------------------------- */
typedef struct desc_param {
uint32_t id;
uint32_t val;
} desc_param;
typedef struct desc_node {
uint32_t nid;
const char *name;
const desc_param *params;
uint32_t nparams;
uint32_t config;
uint32_t pinctl;
uint32_t *conn;
uint32_t stindex;
} desc_node;
typedef struct desc_codec {
const char *name;
uint32_t iid;
const desc_node *nodes;
uint32_t nnodes;
} desc_codec;
static const desc_param* hda_codec_find_param(const desc_node *node, uint32_t id)
{
int i;
for (i = 0; i < node->nparams; i++) {
if (node->params[i].id == id) {
return &node->params[i];
}
}
return NULL;
}
static const desc_node* hda_codec_find_node(const desc_codec *codec, uint32_t nid)
{
int i;
for (i = 0; i < codec->nnodes; i++) {
if (codec->nodes[i].nid == nid) {
return &codec->nodes[i];
}
}
return NULL;
}
static void hda_codec_parse_fmt(uint32_t format, struct audsettings *as)
{
if (format & AC_FMT_TYPE_NON_PCM) {
return;
}
as->freq = (format & AC_FMT_BASE_44K) ? 44100 : 48000;
switch ((format & AC_FMT_MULT_MASK) >> AC_FMT_MULT_SHIFT) {
case 1: as->freq *= 2; break;
case 2: as->freq *= 3; break;
case 3: as->freq *= 4; break;
}
switch ((format & AC_FMT_DIV_MASK) >> AC_FMT_DIV_SHIFT) {
case 1: as->freq /= 2; break;
case 2: as->freq /= 3; break;
case 3: as->freq /= 4; break;
case 4: as->freq /= 5; break;
case 5: as->freq /= 6; break;
case 6: as->freq /= 7; break;
case 7: as->freq /= 8; break;
}
switch (format & AC_FMT_BITS_MASK) {
case AC_FMT_BITS_8: as->fmt = AUD_FMT_S8; break;
case AC_FMT_BITS_16: as->fmt = AUD_FMT_S16; break;
case AC_FMT_BITS_32: as->fmt = AUD_FMT_S32; break;
}
as->nchannels = ((format & AC_FMT_CHAN_MASK) >> AC_FMT_CHAN_SHIFT) + 1;
}
/* -------------------------------------------------------------------------- */
/*
* HDA codec descriptions
*/
/* some defines */
#define QEMU_HDA_ID_VENDOR 0x1af4
#define QEMU_HDA_ID_OUTPUT ((QEMU_HDA_ID_VENDOR << 16) | 0x10)
#define QEMU_HDA_ID_DUPLEX ((QEMU_HDA_ID_VENDOR << 16) | 0x20)
#define QEMU_HDA_PCM_FORMATS (AC_SUPPCM_BITS_16 | \
0x1fc /* 16 -> 96 kHz */)
#define QEMU_HDA_AMP_NONE (0)
#define QEMU_HDA_AMP_STEPS 0x4a
#ifdef CONFIG_MIXEMU
#define QEMU_HDA_AMP_CAPS \
(AC_AMPCAP_MUTE | \
(QEMU_HDA_AMP_STEPS << AC_AMPCAP_OFFSET_SHIFT) | \
(QEMU_HDA_AMP_STEPS << AC_AMPCAP_NUM_STEPS_SHIFT) | \
(3 << AC_AMPCAP_STEP_SIZE_SHIFT))
#else
#define QEMU_HDA_AMP_CAPS QEMU_HDA_AMP_NONE
#endif
/* common: audio output widget */
static const desc_param common_params_audio_dac[] = {
{
.id = AC_PAR_AUDIO_WIDGET_CAP,
.val = ((AC_WID_AUD_OUT << AC_WCAP_TYPE_SHIFT) |
AC_WCAP_FORMAT_OVRD |
AC_WCAP_AMP_OVRD |
AC_WCAP_OUT_AMP |
AC_WCAP_STEREO),
},{
.id = AC_PAR_PCM,
.val = QEMU_HDA_PCM_FORMATS,
},{
.id = AC_PAR_STREAM,
.val = AC_SUPFMT_PCM,
},{
.id = AC_PAR_AMP_IN_CAP,
.val = QEMU_HDA_AMP_NONE,
},{
.id = AC_PAR_AMP_OUT_CAP,
.val = QEMU_HDA_AMP_CAPS,
},
};
/* common: pin widget (line-out) */
static const desc_param common_params_audio_lineout[] = {
{
.id = AC_PAR_AUDIO_WIDGET_CAP,
.val = ((AC_WID_PIN << AC_WCAP_TYPE_SHIFT) |
AC_WCAP_CONN_LIST |
AC_WCAP_STEREO),
},{
.id = AC_PAR_PIN_CAP,
.val = AC_PINCAP_OUT,
},{
.id = AC_PAR_CONNLIST_LEN,
.val = 1,
},{
.id = AC_PAR_AMP_IN_CAP,
.val = QEMU_HDA_AMP_NONE,
},{
.id = AC_PAR_AMP_OUT_CAP,
.val = QEMU_HDA_AMP_NONE,
},
};
/* output: root node */
static const desc_param output_params_root[] = {
{
.id = AC_PAR_VENDOR_ID,
.val = QEMU_HDA_ID_OUTPUT,
},{
.id = AC_PAR_SUBSYSTEM_ID,
.val = QEMU_HDA_ID_OUTPUT,
},{
.id = AC_PAR_REV_ID,
.val = 0x00100101,
},{
.id = AC_PAR_NODE_COUNT,
.val = 0x00010001,
},
};
/* output: audio function */
static const desc_param output_params_audio_func[] = {
{
.id = AC_PAR_FUNCTION_TYPE,
.val = AC_GRP_AUDIO_FUNCTION,
},{
.id = AC_PAR_SUBSYSTEM_ID,
.val = QEMU_HDA_ID_OUTPUT,
},{
.id = AC_PAR_NODE_COUNT,
.val = 0x00020002,
},{
.id = AC_PAR_PCM,
.val = QEMU_HDA_PCM_FORMATS,
},{
.id = AC_PAR_STREAM,
.val = AC_SUPFMT_PCM,
},{
.id = AC_PAR_AMP_IN_CAP,
.val = QEMU_HDA_AMP_NONE,
},{
.id = AC_PAR_AMP_OUT_CAP,
.val = QEMU_HDA_AMP_NONE,
},{
.id = AC_PAR_GPIO_CAP,
.val = 0,
},{
.id = AC_PAR_AUDIO_FG_CAP,
.val = 0x00000808,
},{
.id = AC_PAR_POWER_STATE,
.val = 0,
},
};
/* output: nodes */
static const desc_node output_nodes[] = {
{
.nid = AC_NODE_ROOT,
.name = "root",
.params = output_params_root,
.nparams = ARRAY_SIZE(output_params_root),
},{
.nid = 1,
.name = "func",
.params = output_params_audio_func,
.nparams = ARRAY_SIZE(output_params_audio_func),
},{
.nid = 2,
.name = "dac",
.params = common_params_audio_dac,
.nparams = ARRAY_SIZE(common_params_audio_dac),
.stindex = 0,
},{
.nid = 3,
.name = "out",
.params = common_params_audio_lineout,
.nparams = ARRAY_SIZE(common_params_audio_lineout),
.config = ((AC_JACK_PORT_COMPLEX << AC_DEFCFG_PORT_CONN_SHIFT) |
(AC_JACK_LINE_OUT << AC_DEFCFG_DEVICE_SHIFT) |
(AC_JACK_CONN_UNKNOWN << AC_DEFCFG_CONN_TYPE_SHIFT) |
(AC_JACK_COLOR_GREEN << AC_DEFCFG_COLOR_SHIFT) |
0x10),
.pinctl = AC_PINCTL_OUT_EN,
.conn = (uint32_t[]) { 2 },
}
};
/* output: codec */
static const desc_codec output = {
.name = "output",
.iid = QEMU_HDA_ID_OUTPUT,
.nodes = output_nodes,
.nnodes = ARRAY_SIZE(output_nodes),
};
/* duplex: root node */
static const desc_param duplex_params_root[] = {
{
.id = AC_PAR_VENDOR_ID,
.val = QEMU_HDA_ID_DUPLEX,
},{
.id = AC_PAR_SUBSYSTEM_ID,
.val = QEMU_HDA_ID_DUPLEX,
},{
.id = AC_PAR_REV_ID,
.val = 0x00100101,
},{
.id = AC_PAR_NODE_COUNT,
.val = 0x00010001,
},
};
/* duplex: audio input widget */
static const desc_param duplex_params_audio_adc[] = {
{
.id = AC_PAR_AUDIO_WIDGET_CAP,
.val = ((AC_WID_AUD_IN << AC_WCAP_TYPE_SHIFT) |
AC_WCAP_CONN_LIST |
AC_WCAP_FORMAT_OVRD |
AC_WCAP_AMP_OVRD |
AC_WCAP_IN_AMP |
AC_WCAP_STEREO),
},{
.id = AC_PAR_CONNLIST_LEN,
.val = 1,
},{
.id = AC_PAR_PCM,
.val = QEMU_HDA_PCM_FORMATS,
},{
.id = AC_PAR_STREAM,
.val = AC_SUPFMT_PCM,
},{
.id = AC_PAR_AMP_IN_CAP,
.val = QEMU_HDA_AMP_CAPS,
},{
.id = AC_PAR_AMP_OUT_CAP,
.val = QEMU_HDA_AMP_NONE,
},
};
/* duplex: pin widget (line-in) */
static const desc_param duplex_params_audio_linein[] = {
{
.id = AC_PAR_AUDIO_WIDGET_CAP,
.val = ((AC_WID_PIN << AC_WCAP_TYPE_SHIFT) |
AC_WCAP_STEREO),
},{
.id = AC_PAR_PIN_CAP,
.val = AC_PINCAP_IN,
},{
.id = AC_PAR_AMP_IN_CAP,
.val = QEMU_HDA_AMP_NONE,
},{
.id = AC_PAR_AMP_OUT_CAP,
.val = QEMU_HDA_AMP_NONE,
},
};
/* duplex: audio function */
static const desc_param duplex_params_audio_func[] = {
{
.id = AC_PAR_FUNCTION_TYPE,
.val = AC_GRP_AUDIO_FUNCTION,
},{
.id = AC_PAR_SUBSYSTEM_ID,
.val = QEMU_HDA_ID_DUPLEX,
},{
.id = AC_PAR_NODE_COUNT,
.val = 0x00020004,
},{
.id = AC_PAR_PCM,
.val = QEMU_HDA_PCM_FORMATS,
},{
.id = AC_PAR_STREAM,
.val = AC_SUPFMT_PCM,
},{
.id = AC_PAR_AMP_IN_CAP,
.val = QEMU_HDA_AMP_NONE,
},{
.id = AC_PAR_AMP_OUT_CAP,
.val = QEMU_HDA_AMP_NONE,
},{
.id = AC_PAR_GPIO_CAP,
.val = 0,
},{
.id = AC_PAR_AUDIO_FG_CAP,
.val = 0x00000808,
},{
.id = AC_PAR_POWER_STATE,
.val = 0,
},
};
/* duplex: nodes */
static const desc_node duplex_nodes[] = {
{
.nid = AC_NODE_ROOT,
.name = "root",
.params = duplex_params_root,
.nparams = ARRAY_SIZE(duplex_params_root),
},{
.nid = 1,
.name = "func",
.params = duplex_params_audio_func,
.nparams = ARRAY_SIZE(duplex_params_audio_func),
},{
.nid = 2,
.name = "dac",
.params = common_params_audio_dac,
.nparams = ARRAY_SIZE(common_params_audio_dac),
.stindex = 0,
},{
.nid = 3,
.name = "out",
.params = common_params_audio_lineout,
.nparams = ARRAY_SIZE(common_params_audio_lineout),
.config = ((AC_JACK_PORT_COMPLEX << AC_DEFCFG_PORT_CONN_SHIFT) |
(AC_JACK_LINE_OUT << AC_DEFCFG_DEVICE_SHIFT) |
(AC_JACK_CONN_UNKNOWN << AC_DEFCFG_CONN_TYPE_SHIFT) |
(AC_JACK_COLOR_GREEN << AC_DEFCFG_COLOR_SHIFT) |
0x10),
.pinctl = AC_PINCTL_OUT_EN,
.conn = (uint32_t[]) { 2 },
},{
.nid = 4,
.name = "adc",
.params = duplex_params_audio_adc,
.nparams = ARRAY_SIZE(duplex_params_audio_adc),
.stindex = 1,
.conn = (uint32_t[]) { 5 },
},{
.nid = 5,
.name = "in",
.params = duplex_params_audio_linein,
.nparams = ARRAY_SIZE(duplex_params_audio_linein),
.config = ((AC_JACK_PORT_COMPLEX << AC_DEFCFG_PORT_CONN_SHIFT) |
(AC_JACK_LINE_IN << AC_DEFCFG_DEVICE_SHIFT) |
(AC_JACK_CONN_UNKNOWN << AC_DEFCFG_CONN_TYPE_SHIFT) |
(AC_JACK_COLOR_RED << AC_DEFCFG_COLOR_SHIFT) |
0x20),
.pinctl = AC_PINCTL_IN_EN,
}
};
/* duplex: codec */
static const desc_codec duplex = {
.name = "duplex",
.iid = QEMU_HDA_ID_DUPLEX,
.nodes = duplex_nodes,
.nnodes = ARRAY_SIZE(duplex_nodes),
};
/* -------------------------------------------------------------------------- */
static const char *fmt2name[] = {
[ AUD_FMT_U8 ] = "PCM-U8",
[ AUD_FMT_S8 ] = "PCM-S8",
[ AUD_FMT_U16 ] = "PCM-U16",
[ AUD_FMT_S16 ] = "PCM-S16",
[ AUD_FMT_U32 ] = "PCM-U32",
[ AUD_FMT_S32 ] = "PCM-S32",
};
typedef struct HDAAudioState HDAAudioState;
typedef struct HDAAudioStream HDAAudioStream;
struct HDAAudioStream {
HDAAudioState *state;
const desc_node *node;
bool output, running;
uint32_t stream;
uint32_t channel;
uint32_t format;
uint32_t gain_left, gain_right;
bool mute_left, mute_right;
struct audsettings as;
union {
SWVoiceIn *in;
SWVoiceOut *out;
} voice;
uint8_t buf[HDA_BUFFER_SIZE];
uint32_t bpos;
};
struct HDAAudioState {
HDACodecDevice hda;
const char *name;
QEMUSoundCard card;
const desc_codec *desc;
HDAAudioStream st[4];
bool running[16];
/* properties */
uint32_t debug;
};
static void hda_audio_input_cb(void *opaque, int avail)
{
HDAAudioStream *st = opaque;
int recv = 0;
int len;
bool rc;
while (avail - recv >= sizeof(st->buf)) {
if (st->bpos != sizeof(st->buf)) {
len = AUD_read(st->voice.in, st->buf + st->bpos,
sizeof(st->buf) - st->bpos);
st->bpos += len;
recv += len;
if (st->bpos != sizeof(st->buf)) {
break;
}
}
rc = hda_codec_xfer(&st->state->hda, st->stream, false,
st->buf, sizeof(st->buf));
if (!rc) {
break;
}
st->bpos = 0;
}
}
static void hda_audio_output_cb(void *opaque, int avail)
{
HDAAudioStream *st = opaque;
int sent = 0;
int len;
bool rc;
while (avail - sent >= sizeof(st->buf)) {
if (st->bpos == sizeof(st->buf)) {
rc = hda_codec_xfer(&st->state->hda, st->stream, true,
st->buf, sizeof(st->buf));
if (!rc) {
break;
}
st->bpos = 0;
}
len = AUD_write(st->voice.out, st->buf + st->bpos,
sizeof(st->buf) - st->bpos);
st->bpos += len;
sent += len;
if (st->bpos != sizeof(st->buf)) {
break;
}
}
}
static void hda_audio_set_running(HDAAudioStream *st, bool running)
{
if (st->node == NULL) {
return;
}
if (st->running == running) {
return;
}
st->running = running;
dprint(st->state, 1, "%s: %s (stream %d)\n", st->node->name,
st->running ? "on" : "off", st->stream);
if (st->output) {
AUD_set_active_out(st->voice.out, st->running);
} else {
AUD_set_active_in(st->voice.in, st->running);
}
}
static void hda_audio_set_amp(HDAAudioStream *st)
{
bool muted;
uint32_t left, right;
if (st->node == NULL) {
return;
}
muted = st->mute_left && st->mute_right;
left = st->mute_left ? 0 : st->gain_left;
right = st->mute_right ? 0 : st->gain_right;
left = left * 255 / QEMU_HDA_AMP_STEPS;
right = right * 255 / QEMU_HDA_AMP_STEPS;
if (st->output) {
AUD_set_volume_out(st->voice.out, muted, left, right);
} else {
AUD_set_volume_in(st->voice.in, muted, left, right);
}
}
static void hda_audio_setup(HDAAudioStream *st)
{
if (st->node == NULL) {
return;
}
dprint(st->state, 1, "%s: format: %d x %s @ %d Hz\n",
st->node->name, st->as.nchannels,
fmt2name[st->as.fmt], st->as.freq);
if (st->output) {
st->voice.out = AUD_open_out(&st->state->card, st->voice.out,
st->node->name, st,
hda_audio_output_cb, &st->as);
} else {
st->voice.in = AUD_open_in(&st->state->card, st->voice.in,
st->node->name, st,