2 * Copyright (C) 2006 InnoTek Systemberatung GmbH
4 * This file is part of VirtualBox Open Source Edition (OSE), as
5 * available from http://www.virtualbox.org. This file is free software;
6 * you can redistribute it and/or modify it under the terms of the GNU
7 * General Public License as published by the Free Software Foundation,
8 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
9 * distribution. VirtualBox OSE is distributed in the hope that it will
10 * be useful, but WITHOUT ANY WARRANTY of any kind.
12 * If you received this file as part of a commercial VirtualBox
13 * distribution, then only the terms of your commercial VirtualBox
14 * license agreement apply instead of the previous paragraph.
16 * Contributions after 2012-01-13 are licensed under the terms of the
17 * GNU GPL, version 2 or (at your option) any later version.
20 #include "qemu/osdep.h"
21 #include "hw/audio/soundhw.h"
22 #include "audio/audio.h"
23 #include "hw/pci/pci.h"
24 #include "hw/qdev-properties.h"
25 #include "migration/vmstate.h"
26 #include "qemu/module.h"
27 #include "sysemu/dma.h"
31 AC97_Master_Volume_Mute
= 0x02,
32 AC97_Headphone_Volume_Mute
= 0x04,
33 AC97_Master_Volume_Mono_Mute
= 0x06,
34 AC97_Master_Tone_RL
= 0x08,
35 AC97_PC_BEEP_Volume_Mute
= 0x0A,
36 AC97_Phone_Volume_Mute
= 0x0C,
37 AC97_Mic_Volume_Mute
= 0x0E,
38 AC97_Line_In_Volume_Mute
= 0x10,
39 AC97_CD_Volume_Mute
= 0x12,
40 AC97_Video_Volume_Mute
= 0x14,
41 AC97_Aux_Volume_Mute
= 0x16,
42 AC97_PCM_Out_Volume_Mute
= 0x18,
43 AC97_Record_Select
= 0x1A,
44 AC97_Record_Gain_Mute
= 0x1C,
45 AC97_Record_Gain_Mic_Mute
= 0x1E,
46 AC97_General_Purpose
= 0x20,
47 AC97_3D_Control
= 0x22,
48 AC97_AC_97_RESERVED
= 0x24,
49 AC97_Powerdown_Ctrl_Stat
= 0x26,
50 AC97_Extended_Audio_ID
= 0x28,
51 AC97_Extended_Audio_Ctrl_Stat
= 0x2A,
52 AC97_PCM_Front_DAC_Rate
= 0x2C,
53 AC97_PCM_Surround_DAC_Rate
= 0x2E,
54 AC97_PCM_LFE_DAC_Rate
= 0x30,
55 AC97_PCM_LR_ADC_Rate
= 0x32,
56 AC97_MIC_ADC_Rate
= 0x34,
57 AC97_6Ch_Vol_C_LFE_Mute
= 0x36,
58 AC97_6Ch_Vol_L_R_Surround_Mute
= 0x38,
59 AC97_Vendor_Reserved
= 0x58,
60 AC97_Sigmatel_Analog
= 0x6c, /* We emulate a Sigmatel codec */
61 AC97_Sigmatel_Dac2Invert
= 0x6e, /* We emulate a Sigmatel codec */
62 AC97_Vendor_ID1
= 0x7c,
63 AC97_Vendor_ID2
= 0x7e
67 #define SR_FIFOE 16 /* rwc */
68 #define SR_BCIS 8 /* rwc */
69 #define SR_LVBCI 4 /* rwc */
70 #define SR_CELV 2 /* ro */
71 #define SR_DCH 1 /* ro */
72 #define SR_VALID_MASK ((1 << 5) - 1)
73 #define SR_WCLEAR_MASK (SR_FIFOE | SR_BCIS | SR_LVBCI)
74 #define SR_RO_MASK (SR_DCH | SR_CELV)
75 #define SR_INT_MASK (SR_FIFOE | SR_BCIS | SR_LVBCI)
77 #define CR_IOCE 16 /* rw */
78 #define CR_FEIE 8 /* rw */
79 #define CR_LVBIE 4 /* rw */
80 #define CR_RR 2 /* rw */
81 #define CR_RPBM 1 /* rw */
82 #define CR_VALID_MASK ((1 << 5) - 1)
83 #define CR_DONT_CLEAR_MASK (CR_IOCE | CR_FEIE | CR_LVBIE)
85 #define GC_WR 4 /* rw */
86 #define GC_CR 2 /* rw */
87 #define GC_VALID_MASK ((1 << 6) - 1)
89 #define GS_MD3 (1<<17) /* rw */
90 #define GS_AD3 (1<<16) /* rw */
91 #define GS_RCS (1<<15) /* rwc */
92 #define GS_B3S12 (1<<14) /* ro */
93 #define GS_B2S12 (1<<13) /* ro */
94 #define GS_B1S12 (1<<12) /* ro */
95 #define GS_S1R1 (1<<11) /* rwc */
96 #define GS_S0R1 (1<<10) /* rwc */
97 #define GS_S1CR (1<<9) /* ro */
98 #define GS_S0CR (1<<8) /* ro */
99 #define GS_MINT (1<<7) /* ro */
100 #define GS_POINT (1<<6) /* ro */
101 #define GS_PIINT (1<<5) /* ro */
102 #define GS_RSRVD ((1<<4)|(1<<3))
103 #define GS_MOINT (1<<2) /* ro */
104 #define GS_MIINT (1<<1) /* ro */
105 #define GS_GSCI 1 /* rwc */
106 #define GS_RO_MASK (GS_B3S12| \
117 #define GS_VALID_MASK ((1 << 18) - 1)
118 #define GS_WCLEAR_MASK (GS_RCS|GS_S1R1|GS_S0R1|GS_GSCI)
120 #define BD_IOC (1<<31)
121 #define BD_BUP (1<<30)
126 #define MUTE_SHIFT 15
128 #define TYPE_AC97 "AC97"
130 OBJECT_CHECK(AC97LinkState, (obj), TYPE_AC97)
149 typedef struct AC97BusMasterRegs
{
150 uint32_t bdbar
; /* rw 0 */
151 uint8_t civ
; /* ro 0 */
152 uint8_t lvi
; /* rw 0 */
153 uint16_t sr
; /* rw 1 */
154 uint16_t picb
; /* ro 0 */
155 uint8_t piv
; /* ro 0 */
156 uint8_t cr
; /* rw 0 */
157 unsigned int bd_valid
;
161 typedef struct AC97LinkState
{
168 AC97BusMasterRegs bm_regs
[3];
169 uint8_t mixer_data
[256];
171 SWVoiceOut
*voice_po
;
174 uint8_t silence
[128];
177 MemoryRegion io_nabm
;
186 #define dolog(...) AUD_log ("ac97", __VA_ARGS__)
191 #define MKREGS(prefix, start) \
193 prefix ## _BDBAR = start, \
194 prefix ## _CIV = start + 4, \
195 prefix ## _LVI = start + 5, \
196 prefix ## _SR = start + 6, \
197 prefix ## _PICB = start + 8, \
198 prefix ## _PIV = start + 10, \
199 prefix ## _CR = start + 11 \
209 MKREGS (PI
, PI_INDEX
* 16);
210 MKREGS (PO
, PO_INDEX
* 16);
211 MKREGS (MC
, MC_INDEX
* 16);
219 #define GET_BM(index) (((index) >> 4) & 3)
221 static void po_callback (void *opaque
, int free
);
222 static void pi_callback (void *opaque
, int avail
);
223 static void mc_callback (void *opaque
, int avail
);
225 static void warm_reset (AC97LinkState
*s
)
230 static void cold_reset (AC97LinkState
* s
)
235 static void fetch_bd (AC97LinkState
*s
, AC97BusMasterRegs
*r
)
239 pci_dma_read (&s
->dev
, r
->bdbar
+ r
->civ
* 8, b
, 8);
241 r
->bd
.addr
= le32_to_cpu (*(uint32_t *) &b
[0]) & ~3;
242 r
->bd
.ctl_len
= le32_to_cpu (*(uint32_t *) &b
[4]);
243 r
->picb
= r
->bd
.ctl_len
& 0xffff;
244 dolog ("bd %2d addr=%#x ctl=%#06x len=%#x(%d bytes)\n",
245 r
->civ
, r
->bd
.addr
, r
->bd
.ctl_len
>> 16,
246 r
->bd
.ctl_len
& 0xffff,
247 (r
->bd
.ctl_len
& 0xffff) << 1);
250 static void update_sr (AC97LinkState
*s
, AC97BusMasterRegs
*r
, uint32_t new_sr
)
254 uint32_t new_mask
= new_sr
& SR_INT_MASK
;
255 uint32_t old_mask
= r
->sr
& SR_INT_MASK
;
256 uint32_t masks
[] = {GS_PIINT
, GS_POINT
, GS_MINT
};
258 if (new_mask
^ old_mask
) {
259 /** @todo is IRQ deasserted when only one of status bits is cleared? */
265 if ((new_mask
& SR_LVBCI
) && (r
->cr
& CR_LVBIE
)) {
269 if ((new_mask
& SR_BCIS
) && (r
->cr
& CR_IOCE
)) {
278 dolog ("IOC%d LVB%d sr=%#x event=%d level=%d\n",
279 r
->sr
& SR_BCIS
, r
->sr
& SR_LVBCI
,
287 s
->glob_sta
|= masks
[r
- s
->bm_regs
];
288 dolog ("set irq level=1\n");
289 pci_irq_assert(&s
->dev
);
292 s
->glob_sta
&= ~masks
[r
- s
->bm_regs
];
293 dolog ("set irq level=0\n");
294 pci_irq_deassert(&s
->dev
);
298 static void voice_set_active (AC97LinkState
*s
, int bm_index
, int on
)
302 AUD_set_active_in (s
->voice_pi
, on
);
306 AUD_set_active_out (s
->voice_po
, on
);
310 AUD_set_active_in (s
->voice_mc
, on
);
314 AUD_log ("ac97", "invalid bm_index(%d) in voice_set_active", bm_index
);
319 static void reset_bm_regs (AC97LinkState
*s
, AC97BusMasterRegs
*r
)
321 dolog ("reset_bm_regs\n");
325 /** todo do we need to do that? */
326 update_sr (s
, r
, SR_DCH
);
329 r
->cr
= r
->cr
& CR_DONT_CLEAR_MASK
;
332 voice_set_active (s
, r
- s
->bm_regs
, 0);
333 memset (s
->silence
, 0, sizeof (s
->silence
));
336 static void mixer_store (AC97LinkState
*s
, uint32_t i
, uint16_t v
)
338 if (i
+ 2 > sizeof (s
->mixer_data
)) {
339 dolog ("mixer_store: index %d out of bounds %zd\n",
340 i
, sizeof (s
->mixer_data
));
344 s
->mixer_data
[i
+ 0] = v
& 0xff;
345 s
->mixer_data
[i
+ 1] = v
>> 8;
348 static uint16_t mixer_load (AC97LinkState
*s
, uint32_t i
)
350 uint16_t val
= 0xffff;
352 if (i
+ 2 > sizeof (s
->mixer_data
)) {
353 dolog ("mixer_load: index %d out of bounds %zd\n",
354 i
, sizeof (s
->mixer_data
));
357 val
= s
->mixer_data
[i
+ 0] | (s
->mixer_data
[i
+ 1] << 8);
363 static void open_voice (AC97LinkState
*s
, int index
, int freq
)
365 struct audsettings as
;
369 as
.fmt
= AUDIO_FORMAT_S16
;
373 s
->invalid_freq
[index
] = 0;
376 s
->voice_pi
= AUD_open_in (
387 s
->voice_po
= AUD_open_out (
398 s
->voice_mc
= AUD_open_in (
410 s
->invalid_freq
[index
] = freq
;
413 AUD_close_in (&s
->card
, s
->voice_pi
);
418 AUD_close_out (&s
->card
, s
->voice_po
);
423 AUD_close_in (&s
->card
, s
->voice_mc
);
430 static void reset_voices (AC97LinkState
*s
, uint8_t active
[LAST_INDEX
])
434 freq
= mixer_load (s
, AC97_PCM_LR_ADC_Rate
);
435 open_voice (s
, PI_INDEX
, freq
);
436 AUD_set_active_in (s
->voice_pi
, active
[PI_INDEX
]);
438 freq
= mixer_load (s
, AC97_PCM_Front_DAC_Rate
);
439 open_voice (s
, PO_INDEX
, freq
);
440 AUD_set_active_out (s
->voice_po
, active
[PO_INDEX
]);
442 freq
= mixer_load (s
, AC97_MIC_ADC_Rate
);
443 open_voice (s
, MC_INDEX
, freq
);
444 AUD_set_active_in (s
->voice_mc
, active
[MC_INDEX
]);
447 static void get_volume (uint16_t vol
, uint16_t mask
, int inverse
,
448 int *mute
, uint8_t *lvol
, uint8_t *rvol
)
450 *mute
= (vol
>> MUTE_SHIFT
) & 1;
451 *rvol
= (255 * (vol
& mask
)) / mask
;
452 *lvol
= (255 * ((vol
>> 8) & mask
)) / mask
;
460 static void update_combined_volume_out (AC97LinkState
*s
)
462 uint8_t lvol
, rvol
, plvol
, prvol
;
465 get_volume (mixer_load (s
, AC97_Master_Volume_Mute
), 0x3f, 1,
466 &mute
, &lvol
, &rvol
);
467 get_volume (mixer_load (s
, AC97_PCM_Out_Volume_Mute
), 0x1f, 1,
468 &pmute
, &plvol
, &prvol
);
471 lvol
= (lvol
* plvol
) / 255;
472 rvol
= (rvol
* prvol
) / 255;
474 AUD_set_volume_out (s
->voice_po
, mute
, lvol
, rvol
);
477 static void update_volume_in (AC97LinkState
*s
)
482 get_volume (mixer_load (s
, AC97_Record_Gain_Mute
), 0x0f, 0,
483 &mute
, &lvol
, &rvol
);
485 AUD_set_volume_in (s
->voice_pi
, mute
, lvol
, rvol
);
488 static void set_volume (AC97LinkState
*s
, int index
, uint32_t val
)
491 case AC97_Master_Volume_Mute
:
493 mixer_store (s
, index
, val
);
494 update_combined_volume_out (s
);
496 case AC97_PCM_Out_Volume_Mute
:
498 mixer_store (s
, index
, val
);
499 update_combined_volume_out (s
);
501 case AC97_Record_Gain_Mute
:
503 mixer_store (s
, index
, val
);
504 update_volume_in (s
);
509 static void record_select (AC97LinkState
*s
, uint32_t val
)
511 uint8_t rs
= val
& REC_MASK
;
512 uint8_t ls
= (val
>> 8) & REC_MASK
;
513 mixer_store (s
, AC97_Record_Select
, rs
| (ls
<< 8));
516 static void mixer_reset (AC97LinkState
*s
)
518 uint8_t active
[LAST_INDEX
];
520 dolog ("mixer_reset\n");
521 memset (s
->mixer_data
, 0, sizeof (s
->mixer_data
));
522 memset (active
, 0, sizeof (active
));
523 mixer_store (s
, AC97_Reset
, 0x0000); /* 6940 */
524 mixer_store (s
, AC97_Headphone_Volume_Mute
, 0x0000);
525 mixer_store (s
, AC97_Master_Volume_Mono_Mute
, 0x0000);
526 mixer_store (s
, AC97_Master_Tone_RL
, 0x0000);
527 mixer_store (s
, AC97_PC_BEEP_Volume_Mute
, 0x0000);
528 mixer_store (s
, AC97_Phone_Volume_Mute
, 0x0000);
529 mixer_store (s
, AC97_Mic_Volume_Mute
, 0x0000);
530 mixer_store (s
, AC97_Line_In_Volume_Mute
, 0x0000);
531 mixer_store (s
, AC97_CD_Volume_Mute
, 0x0000);
532 mixer_store (s
, AC97_Video_Volume_Mute
, 0x0000);
533 mixer_store (s
, AC97_Aux_Volume_Mute
, 0x0000);
534 mixer_store (s
, AC97_Record_Gain_Mic_Mute
, 0x0000);
535 mixer_store (s
, AC97_General_Purpose
, 0x0000);
536 mixer_store (s
, AC97_3D_Control
, 0x0000);
537 mixer_store (s
, AC97_Powerdown_Ctrl_Stat
, 0x000f);
540 * Sigmatel 9700 (STAC9700)
542 mixer_store (s
, AC97_Vendor_ID1
, 0x8384);
543 mixer_store (s
, AC97_Vendor_ID2
, 0x7600); /* 7608 */
545 mixer_store (s
, AC97_Extended_Audio_ID
, 0x0809);
546 mixer_store (s
, AC97_Extended_Audio_Ctrl_Stat
, 0x0009);
547 mixer_store (s
, AC97_PCM_Front_DAC_Rate
, 0xbb80);
548 mixer_store (s
, AC97_PCM_Surround_DAC_Rate
, 0xbb80);
549 mixer_store (s
, AC97_PCM_LFE_DAC_Rate
, 0xbb80);
550 mixer_store (s
, AC97_PCM_LR_ADC_Rate
, 0xbb80);
551 mixer_store (s
, AC97_MIC_ADC_Rate
, 0xbb80);
553 record_select (s
, 0);
554 set_volume (s
, AC97_Master_Volume_Mute
, 0x8000);
555 set_volume (s
, AC97_PCM_Out_Volume_Mute
, 0x8808);
556 set_volume (s
, AC97_Record_Gain_Mute
, 0x8808);
558 reset_voices (s
, active
);
565 static uint32_t nam_readb (void *opaque
, uint32_t addr
)
567 AC97LinkState
*s
= opaque
;
568 dolog ("U nam readb %#x\n", addr
);
573 static uint32_t nam_readw (void *opaque
, uint32_t addr
)
575 AC97LinkState
*s
= opaque
;
577 uint32_t index
= addr
;
579 val
= mixer_load (s
, index
);
583 static uint32_t nam_readl (void *opaque
, uint32_t addr
)
585 AC97LinkState
*s
= opaque
;
586 dolog ("U nam readl %#x\n", addr
);
595 static void nam_writeb (void *opaque
, uint32_t addr
, uint32_t val
)
597 AC97LinkState
*s
= opaque
;
598 dolog ("U nam writeb %#x <- %#x\n", addr
, val
);
602 static void nam_writew (void *opaque
, uint32_t addr
, uint32_t val
)
604 AC97LinkState
*s
= opaque
;
605 uint32_t index
= addr
;
611 case AC97_Powerdown_Ctrl_Stat
:
613 val
|= mixer_load (s
, index
) & 0xf;
614 mixer_store (s
, index
, val
);
616 case AC97_PCM_Out_Volume_Mute
:
617 case AC97_Master_Volume_Mute
:
618 case AC97_Record_Gain_Mute
:
619 set_volume (s
, index
, val
);
621 case AC97_Record_Select
:
622 record_select (s
, val
);
624 case AC97_Vendor_ID1
:
625 case AC97_Vendor_ID2
:
626 dolog ("Attempt to write vendor ID to %#x\n", val
);
628 case AC97_Extended_Audio_ID
:
629 dolog ("Attempt to write extended audio ID to %#x\n", val
);
631 case AC97_Extended_Audio_Ctrl_Stat
:
632 if (!(val
& EACS_VRA
)) {
633 mixer_store (s
, AC97_PCM_Front_DAC_Rate
, 0xbb80);
634 mixer_store (s
, AC97_PCM_LR_ADC_Rate
, 0xbb80);
635 open_voice (s
, PI_INDEX
, 48000);
636 open_voice (s
, PO_INDEX
, 48000);
638 if (!(val
& EACS_VRM
)) {
639 mixer_store (s
, AC97_MIC_ADC_Rate
, 0xbb80);
640 open_voice (s
, MC_INDEX
, 48000);
642 dolog ("Setting extended audio control to %#x\n", val
);
643 mixer_store (s
, AC97_Extended_Audio_Ctrl_Stat
, val
);
645 case AC97_PCM_Front_DAC_Rate
:
646 if (mixer_load (s
, AC97_Extended_Audio_Ctrl_Stat
) & EACS_VRA
) {
647 mixer_store (s
, index
, val
);
648 dolog ("Set front DAC rate to %d\n", val
);
649 open_voice (s
, PO_INDEX
, val
);
652 dolog ("Attempt to set front DAC rate to %d, "
653 "but VRA is not set\n",
657 case AC97_MIC_ADC_Rate
:
658 if (mixer_load (s
, AC97_Extended_Audio_Ctrl_Stat
) & EACS_VRM
) {
659 mixer_store (s
, index
, val
);
660 dolog ("Set MIC ADC rate to %d\n", val
);
661 open_voice (s
, MC_INDEX
, val
);
664 dolog ("Attempt to set MIC ADC rate to %d, "
665 "but VRM is not set\n",
669 case AC97_PCM_LR_ADC_Rate
:
670 if (mixer_load (s
, AC97_Extended_Audio_Ctrl_Stat
) & EACS_VRA
) {
671 mixer_store (s
, index
, val
);
672 dolog ("Set front LR ADC rate to %d\n", val
);
673 open_voice (s
, PI_INDEX
, val
);
676 dolog ("Attempt to set LR ADC rate to %d, but VRA is not set\n",
680 case AC97_Headphone_Volume_Mute
:
681 case AC97_Master_Volume_Mono_Mute
:
682 case AC97_Master_Tone_RL
:
683 case AC97_PC_BEEP_Volume_Mute
:
684 case AC97_Phone_Volume_Mute
:
685 case AC97_Mic_Volume_Mute
:
686 case AC97_Line_In_Volume_Mute
:
687 case AC97_CD_Volume_Mute
:
688 case AC97_Video_Volume_Mute
:
689 case AC97_Aux_Volume_Mute
:
690 case AC97_Record_Gain_Mic_Mute
:
691 case AC97_General_Purpose
:
692 case AC97_3D_Control
:
693 case AC97_Sigmatel_Analog
:
694 case AC97_Sigmatel_Dac2Invert
:
695 /* None of the features in these regs are emulated, so they are RO */
698 dolog ("U nam writew %#x <- %#x\n", addr
, val
);
699 mixer_store (s
, index
, val
);
704 static void nam_writel (void *opaque
, uint32_t addr
, uint32_t val
)
706 AC97LinkState
*s
= opaque
;
707 dolog ("U nam writel %#x <- %#x\n", addr
, val
);
712 * Native audio bus master
715 static uint32_t nabm_readb (void *opaque
, uint32_t addr
)
717 AC97LinkState
*s
= opaque
;
718 AC97BusMasterRegs
*r
= NULL
;
719 uint32_t index
= addr
;
724 dolog ("CAS %d\n", s
->cas
);
731 r
= &s
->bm_regs
[GET_BM (index
)];
733 dolog ("CIV[%d] -> %#x\n", GET_BM (index
), val
);
738 r
= &s
->bm_regs
[GET_BM (index
)];
740 dolog ("LVI[%d] -> %#x\n", GET_BM (index
), val
);
745 r
= &s
->bm_regs
[GET_BM (index
)];
747 dolog ("PIV[%d] -> %#x\n", GET_BM (index
), val
);
752 r
= &s
->bm_regs
[GET_BM (index
)];
754 dolog ("CR[%d] -> %#x\n", GET_BM (index
), val
);
759 r
= &s
->bm_regs
[GET_BM (index
)];
761 dolog ("SRb[%d] -> %#x\n", GET_BM (index
), val
);
764 dolog ("U nabm readb %#x -> %#x\n", addr
, val
);
770 static uint32_t nabm_readw (void *opaque
, uint32_t addr
)
772 AC97LinkState
*s
= opaque
;
773 AC97BusMasterRegs
*r
= NULL
;
774 uint32_t index
= addr
;
781 r
= &s
->bm_regs
[GET_BM (index
)];
783 dolog ("SR[%d] -> %#x\n", GET_BM (index
), val
);
788 r
= &s
->bm_regs
[GET_BM (index
)];
790 dolog ("PICB[%d] -> %#x\n", GET_BM (index
), val
);
793 dolog ("U nabm readw %#x -> %#x\n", addr
, val
);
799 static uint32_t nabm_readl (void *opaque
, uint32_t addr
)
801 AC97LinkState
*s
= opaque
;
802 AC97BusMasterRegs
*r
= NULL
;
803 uint32_t index
= addr
;
810 r
= &s
->bm_regs
[GET_BM (index
)];
812 dolog ("BMADDR[%d] -> %#x\n", GET_BM (index
), val
);
817 r
= &s
->bm_regs
[GET_BM (index
)];
818 val
= r
->civ
| (r
->lvi
<< 8) | (r
->sr
<< 16);
819 dolog ("CIV LVI SR[%d] -> %#x, %#x, %#x\n", GET_BM (index
),
820 r
->civ
, r
->lvi
, r
->sr
);
825 r
= &s
->bm_regs
[GET_BM (index
)];
826 val
= r
->picb
| (r
->piv
<< 16) | (r
->cr
<< 24);
827 dolog ("PICB PIV CR[%d] -> %#x %#x %#x %#x\n", GET_BM (index
),
828 val
, r
->picb
, r
->piv
, r
->cr
);
832 dolog ("glob_cnt -> %#x\n", val
);
835 val
= s
->glob_sta
| GS_S0CR
;
836 dolog ("glob_sta -> %#x\n", val
);
839 dolog ("U nabm readl %#x -> %#x\n", addr
, val
);
846 * Native audio bus master
849 static void nabm_writeb (void *opaque
, uint32_t addr
, uint32_t val
)
851 AC97LinkState
*s
= opaque
;
852 AC97BusMasterRegs
*r
= NULL
;
853 uint32_t index
= addr
;
858 r
= &s
->bm_regs
[GET_BM (index
)];
859 if ((r
->cr
& CR_RPBM
) && (r
->sr
& SR_DCH
)) {
860 r
->sr
&= ~(SR_DCH
| SR_CELV
);
862 r
->piv
= (r
->piv
+ 1) % 32;
866 dolog ("LVI[%d] <- %#x\n", GET_BM (index
), val
);
871 r
= &s
->bm_regs
[GET_BM (index
)];
873 reset_bm_regs (s
, r
);
876 r
->cr
= val
& CR_VALID_MASK
;
877 if (!(r
->cr
& CR_RPBM
)) {
878 voice_set_active (s
, r
- s
->bm_regs
, 0);
883 r
->piv
= (r
->piv
+ 1) % 32;
886 voice_set_active (s
, r
- s
->bm_regs
, 1);
889 dolog ("CR[%d] <- %#x (cr %#x)\n", GET_BM (index
), val
, r
->cr
);
894 r
= &s
->bm_regs
[GET_BM (index
)];
895 r
->sr
|= val
& ~(SR_RO_MASK
| SR_WCLEAR_MASK
);
896 update_sr (s
, r
, r
->sr
& ~(val
& SR_WCLEAR_MASK
));
897 dolog ("SR[%d] <- %#x (sr %#x)\n", GET_BM (index
), val
, r
->sr
);
900 dolog ("U nabm writeb %#x <- %#x\n", addr
, val
);
905 static void nabm_writew (void *opaque
, uint32_t addr
, uint32_t val
)
907 AC97LinkState
*s
= opaque
;
908 AC97BusMasterRegs
*r
= NULL
;
909 uint32_t index
= addr
;
914 r
= &s
->bm_regs
[GET_BM (index
)];
915 r
->sr
|= val
& ~(SR_RO_MASK
| SR_WCLEAR_MASK
);
916 update_sr (s
, r
, r
->sr
& ~(val
& SR_WCLEAR_MASK
));
917 dolog ("SR[%d] <- %#x (sr %#x)\n", GET_BM (index
), val
, r
->sr
);
920 dolog ("U nabm writew %#x <- %#x\n", addr
, val
);
925 static void nabm_writel (void *opaque
, uint32_t addr
, uint32_t val
)
927 AC97LinkState
*s
= opaque
;
928 AC97BusMasterRegs
*r
= NULL
;
929 uint32_t index
= addr
;
934 r
= &s
->bm_regs
[GET_BM (index
)];
936 dolog ("BDBAR[%d] <- %#x (bdbar %#x)\n",
937 GET_BM (index
), val
, r
->bdbar
);
944 if (!(val
& (GC_WR
| GC_CR
)))
945 s
->glob_cnt
= val
& GC_VALID_MASK
;
946 dolog ("glob_cnt <- %#x (glob_cnt %#x)\n", val
, s
->glob_cnt
);
949 s
->glob_sta
&= ~(val
& GS_WCLEAR_MASK
);
950 s
->glob_sta
|= (val
& ~(GS_WCLEAR_MASK
| GS_RO_MASK
)) & GS_VALID_MASK
;
951 dolog ("glob_sta <- %#x (glob_sta %#x)\n", val
, s
->glob_sta
);
954 dolog ("U nabm writel %#x <- %#x\n", addr
, val
);
959 static int write_audio (AC97LinkState
*s
, AC97BusMasterRegs
*r
,
962 uint8_t tmpbuf
[4096];
963 uint32_t addr
= r
->bd
.addr
;
964 uint32_t temp
= r
->picb
<< 1;
965 uint32_t written
= 0;
967 temp
= MIN (temp
, max
);
976 to_copy
= MIN (temp
, sizeof (tmpbuf
));
977 pci_dma_read (&s
->dev
, addr
, tmpbuf
, to_copy
);
978 copied
= AUD_write (s
->voice_po
, tmpbuf
, to_copy
);
979 dolog ("write_audio max=%x to_copy=%x copied=%x\n",
980 max
, to_copy
, copied
);
996 s
->last_samp
= *(uint32_t *) &tmpbuf
[to_copy
- 4];
1004 static void write_bup (AC97LinkState
*s
, int elapsed
)
1006 dolog ("write_bup\n");
1007 if (!(s
->bup_flag
& BUP_SET
)) {
1008 if (s
->bup_flag
& BUP_LAST
) {
1010 uint8_t *p
= s
->silence
;
1011 for (i
= 0; i
< sizeof (s
->silence
) / 4; i
++, p
+= 4) {
1012 *(uint32_t *) p
= s
->last_samp
;
1016 memset (s
->silence
, 0, sizeof (s
->silence
));
1018 s
->bup_flag
|= BUP_SET
;
1022 int temp
= MIN (elapsed
, sizeof (s
->silence
));
1024 int copied
= AUD_write (s
->voice_po
, s
->silence
, temp
);
1033 static int read_audio (AC97LinkState
*s
, AC97BusMasterRegs
*r
,
1036 uint8_t tmpbuf
[4096];
1037 uint32_t addr
= r
->bd
.addr
;
1038 uint32_t temp
= r
->picb
<< 1;
1041 SWVoiceIn
*voice
= (r
- s
->bm_regs
) == MC_INDEX
? s
->voice_mc
: s
->voice_pi
;
1043 temp
= MIN (temp
, max
);
1052 to_copy
= MIN (temp
, sizeof (tmpbuf
));
1053 acquired
= AUD_read (voice
, tmpbuf
, to_copy
);
1058 pci_dma_write (&s
->dev
, addr
, tmpbuf
, acquired
);
1068 static void transfer_audio (AC97LinkState
*s
, int index
, int elapsed
)
1070 AC97BusMasterRegs
*r
= &s
->bm_regs
[index
];
1073 if (s
->invalid_freq
[index
]) {
1074 AUD_log ("ac97", "attempt to use voice %d with invalid frequency %d\n",
1075 index
, s
->invalid_freq
[index
]);
1079 if (r
->sr
& SR_DCH
) {
1080 if (r
->cr
& CR_RPBM
) {
1083 write_bup (s
, elapsed
);
1090 while ((elapsed
>> 1) && !stop
) {
1094 dolog ("invalid bd\n");
1099 dolog ("fresh bd %d is empty %#x %#x\n",
1100 r
->civ
, r
->bd
.addr
, r
->bd
.ctl_len
);
1101 if (r
->civ
== r
->lvi
) {
1102 r
->sr
|= SR_DCH
; /* CELV? */
1108 r
->piv
= (r
->piv
+ 1) % 32;
1115 temp
= write_audio (s
, r
, elapsed
, &stop
);
1117 r
->picb
-= (temp
>> 1);
1122 temp
= read_audio (s
, r
, elapsed
, &stop
);
1124 r
->picb
-= (temp
>> 1);
1129 uint32_t new_sr
= r
->sr
& ~SR_CELV
;
1131 if (r
->bd
.ctl_len
& BD_IOC
) {
1135 if (r
->civ
== r
->lvi
) {
1136 dolog ("Underrun civ (%d) == lvi (%d)\n", r
->civ
, r
->lvi
);
1138 new_sr
|= SR_LVBCI
| SR_DCH
| SR_CELV
;
1140 s
->bup_flag
= (r
->bd
.ctl_len
& BD_BUP
) ? BUP_LAST
: 0;
1144 r
->piv
= (r
->piv
+ 1) % 32;
1148 update_sr (s
, r
, new_sr
);
1153 static void pi_callback (void *opaque
, int avail
)
1155 transfer_audio (opaque
, PI_INDEX
, avail
);
1158 static void mc_callback (void *opaque
, int avail
)
1160 transfer_audio (opaque
, MC_INDEX
, avail
);
1163 static void po_callback (void *opaque
, int free
)
1165 transfer_audio (opaque
, PO_INDEX
, free
);
1168 static const VMStateDescription vmstate_ac97_bm_regs
= {
1169 .name
= "ac97_bm_regs",
1171 .minimum_version_id
= 1,
1172 .fields
= (VMStateField
[]) {
1173 VMSTATE_UINT32 (bdbar
, AC97BusMasterRegs
),
1174 VMSTATE_UINT8 (civ
, AC97BusMasterRegs
),
1175 VMSTATE_UINT8 (lvi
, AC97BusMasterRegs
),
1176 VMSTATE_UINT16 (sr
, AC97BusMasterRegs
),
1177 VMSTATE_UINT16 (picb
, AC97BusMasterRegs
),
1178 VMSTATE_UINT8 (piv
, AC97BusMasterRegs
),
1179 VMSTATE_UINT8 (cr
, AC97BusMasterRegs
),
1180 VMSTATE_UINT32 (bd_valid
, AC97BusMasterRegs
),
1181 VMSTATE_UINT32 (bd
.addr
, AC97BusMasterRegs
),
1182 VMSTATE_UINT32 (bd
.ctl_len
, AC97BusMasterRegs
),
1183 VMSTATE_END_OF_LIST ()
1187 static int ac97_post_load (void *opaque
, int version_id
)
1189 uint8_t active
[LAST_INDEX
];
1190 AC97LinkState
*s
= opaque
;
1192 record_select (s
, mixer_load (s
, AC97_Record_Select
));
1193 set_volume (s
, AC97_Master_Volume_Mute
,
1194 mixer_load (s
, AC97_Master_Volume_Mute
));
1195 set_volume (s
, AC97_PCM_Out_Volume_Mute
,
1196 mixer_load (s
, AC97_PCM_Out_Volume_Mute
));
1197 set_volume (s
, AC97_Record_Gain_Mute
,
1198 mixer_load (s
, AC97_Record_Gain_Mute
));
1200 active
[PI_INDEX
] = !!(s
->bm_regs
[PI_INDEX
].cr
& CR_RPBM
);
1201 active
[PO_INDEX
] = !!(s
->bm_regs
[PO_INDEX
].cr
& CR_RPBM
);
1202 active
[MC_INDEX
] = !!(s
->bm_regs
[MC_INDEX
].cr
& CR_RPBM
);
1203 reset_voices (s
, active
);
1210 static bool is_version_2 (void *opaque
, int version_id
)
1212 return version_id
== 2;
1215 static const VMStateDescription vmstate_ac97
= {
1218 .minimum_version_id
= 2,
1219 .post_load
= ac97_post_load
,
1220 .fields
= (VMStateField
[]) {
1221 VMSTATE_PCI_DEVICE (dev
, AC97LinkState
),
1222 VMSTATE_UINT32 (glob_cnt
, AC97LinkState
),
1223 VMSTATE_UINT32 (glob_sta
, AC97LinkState
),
1224 VMSTATE_UINT32 (cas
, AC97LinkState
),
1225 VMSTATE_STRUCT_ARRAY (bm_regs
, AC97LinkState
, 3, 1,
1226 vmstate_ac97_bm_regs
, AC97BusMasterRegs
),
1227 VMSTATE_BUFFER (mixer_data
, AC97LinkState
),
1228 VMSTATE_UNUSED_TEST (is_version_2
, 3),
1229 VMSTATE_END_OF_LIST ()
1233 static uint64_t nam_read(void *opaque
, hwaddr addr
, unsigned size
)
1235 if ((addr
/ size
) > 256) {
1241 return nam_readb(opaque
, addr
);
1243 return nam_readw(opaque
, addr
);
1245 return nam_readl(opaque
, addr
);
1251 static void nam_write(void *opaque
, hwaddr addr
, uint64_t val
,
1254 if ((addr
/ size
) > 256) {
1260 nam_writeb(opaque
, addr
, val
);
1263 nam_writew(opaque
, addr
, val
);
1266 nam_writel(opaque
, addr
, val
);
1271 static const MemoryRegionOps ac97_io_nam_ops
= {
1275 .min_access_size
= 1,
1276 .max_access_size
= 4,
1278 .endianness
= DEVICE_LITTLE_ENDIAN
,
1281 static uint64_t nabm_read(void *opaque
, hwaddr addr
, unsigned size
)
1283 if ((addr
/ size
) > 64) {
1289 return nabm_readb(opaque
, addr
);
1291 return nabm_readw(opaque
, addr
);
1293 return nabm_readl(opaque
, addr
);
1299 static void nabm_write(void *opaque
, hwaddr addr
, uint64_t val
,
1302 if ((addr
/ size
) > 64) {
1308 nabm_writeb(opaque
, addr
, val
);
1311 nabm_writew(opaque
, addr
, val
);
1314 nabm_writel(opaque
, addr
, val
);
1320 static const MemoryRegionOps ac97_io_nabm_ops
= {
1322 .write
= nabm_write
,
1324 .min_access_size
= 1,
1325 .max_access_size
= 4,
1327 .endianness
= DEVICE_LITTLE_ENDIAN
,
1330 static void ac97_on_reset (DeviceState
*dev
)
1332 AC97LinkState
*s
= container_of(dev
, AC97LinkState
, dev
.qdev
);
1334 reset_bm_regs (s
, &s
->bm_regs
[0]);
1335 reset_bm_regs (s
, &s
->bm_regs
[1]);
1336 reset_bm_regs (s
, &s
->bm_regs
[2]);
1339 * Reset the mixer too. The Windows XP driver seems to rely on
1340 * this. At least it wants to read the vendor id before it resets
1341 * the codec manually.
1346 static void ac97_realize(PCIDevice
*dev
, Error
**errp
)
1348 AC97LinkState
*s
= AC97(dev
);
1349 uint8_t *c
= s
->dev
.config
;
1351 /* TODO: no need to override */
1352 c
[PCI_COMMAND
] = 0x00; /* pcicmd pci command rw, ro */
1353 c
[PCI_COMMAND
+ 1] = 0x00;
1356 c
[PCI_STATUS
] = PCI_STATUS_FAST_BACK
; /* pcists pci status rwc, ro */
1357 c
[PCI_STATUS
+ 1] = PCI_STATUS_DEVSEL_MEDIUM
>> 8;
1359 c
[PCI_CLASS_PROG
] = 0x00; /* pi programming interface ro */
1361 /* TODO set when bar is registered. no need to override. */
1362 /* nabmar native audio mixer base address rw */
1363 c
[PCI_BASE_ADDRESS_0
] = PCI_BASE_ADDRESS_SPACE_IO
;
1364 c
[PCI_BASE_ADDRESS_0
+ 1] = 0x00;
1365 c
[PCI_BASE_ADDRESS_0
+ 2] = 0x00;
1366 c
[PCI_BASE_ADDRESS_0
+ 3] = 0x00;
1368 /* TODO set when bar is registered. no need to override. */
1369 /* nabmbar native audio bus mastering base address rw */
1370 c
[PCI_BASE_ADDRESS_0
+ 4] = PCI_BASE_ADDRESS_SPACE_IO
;
1371 c
[PCI_BASE_ADDRESS_0
+ 5] = 0x00;
1372 c
[PCI_BASE_ADDRESS_0
+ 6] = 0x00;
1373 c
[PCI_BASE_ADDRESS_0
+ 7] = 0x00;
1375 c
[PCI_INTERRUPT_LINE
] = 0x00; /* intr_ln interrupt line rw */
1376 c
[PCI_INTERRUPT_PIN
] = 0x01; /* intr_pn interrupt pin ro */
1378 memory_region_init_io (&s
->io_nam
, OBJECT(s
), &ac97_io_nam_ops
, s
,
1380 memory_region_init_io (&s
->io_nabm
, OBJECT(s
), &ac97_io_nabm_ops
, s
,
1382 pci_register_bar (&s
->dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
, &s
->io_nam
);
1383 pci_register_bar (&s
->dev
, 1, PCI_BASE_ADDRESS_SPACE_IO
, &s
->io_nabm
);
1384 AUD_register_card ("ac97", &s
->card
);
1385 ac97_on_reset(DEVICE(s
));
1388 static void ac97_exit(PCIDevice
*dev
)
1390 AC97LinkState
*s
= AC97(dev
);
1392 AUD_close_in(&s
->card
, s
->voice_pi
);
1393 AUD_close_out(&s
->card
, s
->voice_po
);
1394 AUD_close_in(&s
->card
, s
->voice_mc
);
1395 AUD_remove_card(&s
->card
);
1398 static int ac97_init (PCIBus
*bus
)
1400 pci_create_simple(bus
, -1, TYPE_AC97
);
1404 static Property ac97_properties
[] = {
1405 DEFINE_AUDIO_PROPERTIES(AC97LinkState
, card
),
1406 DEFINE_PROP_END_OF_LIST (),
1409 static void ac97_class_init (ObjectClass
*klass
, void *data
)
1411 DeviceClass
*dc
= DEVICE_CLASS (klass
);
1412 PCIDeviceClass
*k
= PCI_DEVICE_CLASS (klass
);
1414 k
->realize
= ac97_realize
;
1415 k
->exit
= ac97_exit
;
1416 k
->vendor_id
= PCI_VENDOR_ID_INTEL
;
1417 k
->device_id
= PCI_DEVICE_ID_INTEL_82801AA_5
;
1419 k
->class_id
= PCI_CLASS_MULTIMEDIA_AUDIO
;
1420 set_bit(DEVICE_CATEGORY_SOUND
, dc
->categories
);
1421 dc
->desc
= "Intel 82801AA AC97 Audio";
1422 dc
->vmsd
= &vmstate_ac97
;
1423 device_class_set_props(dc
, ac97_properties
);
1424 dc
->reset
= ac97_on_reset
;
1427 static const TypeInfo ac97_info
= {
1429 .parent
= TYPE_PCI_DEVICE
,
1430 .instance_size
= sizeof (AC97LinkState
),
1431 .class_init
= ac97_class_init
,
1432 .interfaces
= (InterfaceInfo
[]) {
1433 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
1438 static void ac97_register_types (void)
1440 type_register_static (&ac97_info
);
1441 pci_register_soundhw("ac97", "Intel 82801AA AC97 Audio", ac97_init
);
1444 type_init (ac97_register_types
)