1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 01:17:35 +00:00

Kernel+AudioServer: Use interrupts for Intel HDA audio buffer completion

We used to not care about stopping an audio output stream for Intel HDA
since AudioServer would continuously send new buffers to play. Since
707f5ac150ef858760eb9faa52b9ba80c50c4262 however, that has changed.

Intel HDA now uses interrupts to detect when each buffer was completed
by the device, and uses a simple heuristic to detect whether a buffer
underrun has occurred so it can stop the output stream.

This was tested on Qemu's Intel HDA (Linux x86_64) and a bare metal MSI
Starship/Matisse HD Audio Controller.
This commit is contained in:
Jelle Raaijmakers 2023-06-27 16:07:05 +02:00
parent 2e474e8c18
commit 5c64686666
5 changed files with 120 additions and 25 deletions

View file

@ -6,6 +6,7 @@
#pragma once
#include <AK/Badge.h>
#include <AK/Error.h>
#include <AK/NonnullOwnPtr.h>
#include <AK/OwnPtr.h>
@ -13,6 +14,7 @@
#include <Kernel/Devices/Audio/IntelHDA/Format.h>
#include <Kernel/Library/IOWindow.h>
#include <Kernel/Locking/SpinlockProtected.h>
#include <Kernel/Tasks/WaitQueue.h>
namespace Kernel::Audio::IntelHDA {
@ -20,12 +22,15 @@ class Stream {
public:
static constexpr u32 cyclic_buffer_size_in_ms = 40;
virtual ~Stream();
u8 stream_number() const { return m_stream_number; }
bool running() const { return m_running; }
u32 sample_rate() const { return m_format_parameters.sample_rate; }
void start();
ErrorOr<void> stop();
virtual ErrorOr<void> handle_interrupt(Badge<Controller>) = 0;
ErrorOr<void> set_format(FormatParameters);
@ -49,6 +54,24 @@ protected:
enum StreamControlFlag : u32 {
StreamReset = 1u << 0,
StreamRun = 1u << 1,
InterruptOnCompletionEnable = 1u << 2,
};
// 3.3.36 : Input/Output/Bidirectional Stream Descriptor Status
enum StreamStatusFlag : u8 {
BufferCompletionInterruptStatus = 1u << 2,
};
// 3.6.3: Buffer Descriptor List Entry
enum BufferDescriptorEntryFlag : u32 {
InterruptOnCompletion = 1u << 0,
};
// 3.6.3: Buffer Descriptor List Entry
struct BufferDescriptorEntry {
u64 address;
u32 size;
BufferDescriptorEntryFlag flags;
};
Stream(NonnullOwnPtr<IOWindow> stream_io_window, u8 stream_number)
@ -57,8 +80,6 @@ protected:
{
}
~Stream();
u32 read_control();
void write_control(u32);
@ -70,6 +91,7 @@ protected:
OwnPtr<Memory::Region> m_buffer_descriptor_list;
SpinlockProtected<OwnPtr<Memory::Region>, LockRank::None> m_buffers;
size_t m_buffer_position { 0 };
WaitQueue m_irq_queue;
bool m_running { false };
FormatParameters m_format_parameters;
};
@ -83,6 +105,11 @@ public:
return adopt_nonnull_own_or_enomem(new (nothrow) OutputStream(move(stream_io_window), stream_number));
}
~OutputStream() = default;
// ^Stream
ErrorOr<void> handle_interrupt(Badge<Controller>) override;
ErrorOr<size_t> write(UserOrKernelBuffer const&, size_t);
private:
@ -96,6 +123,8 @@ private:
// not intended for them."
VERIFY(stream_number >= 1);
}
u32 m_last_link_position { 0 };
};
// FIXME: implement InputStream and BidirectionalStream