1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:18:11 +00:00
serenity/Kernel/Devices/Audio/IntelHDA/Controller.h
Jelle Raaijmakers 5c64686666 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.
2023-07-04 00:05:34 +02:00

105 lines
3.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (c) 2023, Jelle Raaijmakers <jelle@gmta.nl>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Error.h>
#include <AK/NonnullOwnPtr.h>
#include <AK/NonnullRefPtr.h>
#include <AK/RefPtr.h>
#include <AK/Vector.h>
#include <Kernel/Bus/PCI/Device.h>
#include <Kernel/Devices/Audio/Channel.h>
#include <Kernel/Devices/Audio/Controller.h>
#include <Kernel/Devices/Audio/IntelHDA/OutputPath.h>
#include <Kernel/Devices/Audio/IntelHDA/RingBuffer.h>
#include <Kernel/Interrupts/PCIIRQHandler.h>
#include <Kernel/Library/IOWindow.h>
namespace Kernel::Audio::IntelHDA {
// Specification: https://www.intel.com/content/dam/www/public/us/en/documents/product-specifications/high-definition-audio-specification.pdf
class Codec;
class Controller final
: public AudioController
, public PCI::Device
, public PCIIRQHandler {
public:
static ErrorOr<bool> probe(PCI::DeviceIdentifier const&);
static ErrorOr<NonnullRefPtr<AudioController>> create(PCI::DeviceIdentifier const&);
virtual ~Controller() = default;
// ^PCI::Device
virtual StringView device_name() const override { return "IntelHDA"sv; }
// ^PCIIRQHandler
virtual StringView purpose() const override { return "IntelHDA IRQ Handler"sv; }
ErrorOr<u32> send_command(u8 codec_address, u8 node_id, CodecControlVerb verb, u16 payload);
private:
static constexpr size_t fixed_audio_channel_index = 0;
// 3.3: High Definition Audio Controller Register Set
enum ControllerRegister : u8 {
GlobalCapabilities = 0x00,
VersionMinor = 0x02,
VersionMajor = 0x03,
GlobalControl = 0x08,
StateChangeStatus = 0x0e,
InterruptControl = 0x20,
InterruptStatus = 0x24,
CommandOutboundRingBufferOffset = 0x40,
ResponseInboundRingBufferOffset = 0x50,
StreamsOffset = 0x80,
};
// 3.3.7: GCTL Global Control
enum GlobalControlFlag : u32 {
ControllerReset = 1u << 0,
AcceptUnsolicitedResponseEnable = 1u << 8,
};
// 3.3.14: INTCTL Interrupt Control
enum InterruptControlFlag : u32 {
GlobalInterruptEnable = 1u << 31,
};
// 3.3.15: INTSTS Interrupt Status
enum InterruptStatusFlag : u32 {
GlobalInterruptStatus = 1u << 31,
};
Controller(PCI::DeviceIdentifier const&, NonnullOwnPtr<IOWindow>);
ErrorOr<void> initialize_codec(u8 codec_address);
ErrorOr<void> configure_output_route();
ErrorOr<void> reset();
// ^PCIIRQHandler
virtual bool handle_irq(RegisterState const&) override;
// ^AudioController
virtual RefPtr<AudioChannel> audio_channel(u32 index) const override;
virtual ErrorOr<size_t> write(size_t channel_index, UserOrKernelBuffer const& data, size_t length) override;
virtual ErrorOr<void> initialize(Badge<AudioManagement>) override;
virtual ErrorOr<void> set_pcm_output_sample_rate(size_t channel_index, u32 samples_per_second_rate) override;
virtual ErrorOr<u32> get_pcm_output_sample_rate(size_t channel_index) override;
NonnullOwnPtr<IOWindow> m_controller_io_window;
u8 m_number_of_output_streams;
u8 m_number_of_input_streams;
u8 m_number_of_bidirectional_streams;
OwnPtr<CommandOutboundRingBuffer> m_command_buffer;
OwnPtr<ResponseInboundRingBuffer> m_response_buffer;
Vector<NonnullRefPtr<Codec>> m_codecs {};
OwnPtr<OutputPath> m_output_path;
RefPtr<AudioChannel> m_audio_channel;
};
}