ladybird/Kernel/Bus/VirtIO/Console.h

83 lines
2.1 KiB
C
Raw Normal View History

/*
* Copyright (c) 2021, Kyle Pereira <hey@xylepereira.me>
Kernel: Finish base implementation of VirtQueues This commit includes a lot of small changes and additions needed to finalize the base implementation of VirtIOQueues and VirtDevices: * The device specific driver implementation now has to handle setting up the queues it needs before letting the base device class know it finised initialization * Supplying buffers to VirtQueues is now done via ScatterGatherLists instead of arbitary buffer pointers - this ensures the pointers are physical and allows us to follow the specification in regards to the requirement that individual descriptors must point to physically contiguous buffers. This can be further improved in the future by implementating support for the Indirect-Descriptors feature (as defined by the specification) to reduce descriptor usage for very fragmented buffers. * When supplying buffers to a VirtQueue the driver must supply a (temporarily-)unique token (usually the supplied buffer's virtual address) to ensure the driver can discern which buffer has finished processing by the device in the case in which the device does not offer the F_IN_ORDER feature. * Device drivers now handle queue updates (supplied buffers being returned from the device) by implementing a single pure virtual method instead of setting a seperate callback for each queue * Two new VirtQueue methods were added to allow the device driver to either discard or get used/returned buffers from the device by cleanly removing them off the descriptor chain (This also allows the VirtQueue implementation to reuse those freed descriptors) This also includes the necessary changes to the VirtIOConsole implementation to match these interface changes. Co-authored-by: Sahan <sahan.h.fernando@gmail.com>
2021-04-15 12:39:48 +03:00
* Copyright (c) 2021, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <Kernel/Bus/VirtIO/ConsolePort.h>
#include <Kernel/Bus/VirtIO/Device.h>
#include <Kernel/Memory/RingBuffer.h>
namespace Kernel::VirtIO {
class Console
: public VirtIO::Device
, public RefCounted<Console> {
friend VirtIO::ConsolePort;
public:
static NonnullRefPtr<Console> must_create(PCI::DeviceIdentifier const&);
virtual ~Console() override = default;
virtual StringView purpose() const override { return class_name(); }
unsigned device_id() const
{
return m_device_id;
}
virtual void initialize() override;
private:
virtual StringView class_name() const override { return "VirtIOConsole"sv; }
explicit Console(PCI::DeviceIdentifier const&);
enum class ControlEvent : u16 {
DeviceReady = 0,
DeviceAdd = 1,
PortReady = 3,
ConsolePort = 4,
PortOpen = 6,
};
struct [[gnu::packed]] ControlMessage {
u32 id;
u16 event;
u16 value;
enum class Status : u16 {
Success = 1,
Failure = 0
};
enum class PortStatus : u16 {
Open = 1,
Close = 0
};
};
constexpr static u16 CONTROL_RECEIVEQ = 2;
constexpr static u16 CONTROL_TRANSMITQ = 3;
constexpr static size_t CONTROL_MESSAGE_SIZE = sizeof(ControlMessage);
constexpr static size_t CONTROL_BUFFER_SIZE = CONTROL_MESSAGE_SIZE * 32;
virtual bool handle_device_config_change() override;
Kernel: Finish base implementation of VirtQueues This commit includes a lot of small changes and additions needed to finalize the base implementation of VirtIOQueues and VirtDevices: * The device specific driver implementation now has to handle setting up the queues it needs before letting the base device class know it finised initialization * Supplying buffers to VirtQueues is now done via ScatterGatherLists instead of arbitary buffer pointers - this ensures the pointers are physical and allows us to follow the specification in regards to the requirement that individual descriptors must point to physically contiguous buffers. This can be further improved in the future by implementating support for the Indirect-Descriptors feature (as defined by the specification) to reduce descriptor usage for very fragmented buffers. * When supplying buffers to a VirtQueue the driver must supply a (temporarily-)unique token (usually the supplied buffer's virtual address) to ensure the driver can discern which buffer has finished processing by the device in the case in which the device does not offer the F_IN_ORDER feature. * Device drivers now handle queue updates (supplied buffers being returned from the device) by implementing a single pure virtual method instead of setting a seperate callback for each queue * Two new VirtQueue methods were added to allow the device driver to either discard or get used/returned buffers from the device by cleanly removing them off the descriptor chain (This also allows the VirtQueue implementation to reuse those freed descriptors) This also includes the necessary changes to the VirtIOConsole implementation to match these interface changes. Co-authored-by: Sahan <sahan.h.fernando@gmail.com>
2021-04-15 12:39:48 +03:00
virtual void handle_queue_update(u16 queue_index) override;
Vector<RefPtr<ConsolePort>> m_ports;
void setup_multiport();
void process_control_message(ControlMessage message);
void write_control_message(ControlMessage message);
void send_open_control_message(unsigned port_number, bool open);
unsigned m_device_id;
OwnPtr<Memory::RingBuffer> m_control_transmit_buffer;
OwnPtr<Memory::RingBuffer> m_control_receive_buffer;
WaitQueue m_control_wait_queue;
Kernel: Finish base implementation of VirtQueues This commit includes a lot of small changes and additions needed to finalize the base implementation of VirtIOQueues and VirtDevices: * The device specific driver implementation now has to handle setting up the queues it needs before letting the base device class know it finised initialization * Supplying buffers to VirtQueues is now done via ScatterGatherLists instead of arbitary buffer pointers - this ensures the pointers are physical and allows us to follow the specification in regards to the requirement that individual descriptors must point to physically contiguous buffers. This can be further improved in the future by implementating support for the Indirect-Descriptors feature (as defined by the specification) to reduce descriptor usage for very fragmented buffers. * When supplying buffers to a VirtQueue the driver must supply a (temporarily-)unique token (usually the supplied buffer's virtual address) to ensure the driver can discern which buffer has finished processing by the device in the case in which the device does not offer the F_IN_ORDER feature. * Device drivers now handle queue updates (supplied buffers being returned from the device) by implementing a single pure virtual method instead of setting a seperate callback for each queue * Two new VirtQueue methods were added to allow the device driver to either discard or get used/returned buffers from the device by cleanly removing them off the descriptor chain (This also allows the VirtQueue implementation to reuse those freed descriptors) This also includes the necessary changes to the VirtIOConsole implementation to match these interface changes. Co-authored-by: Sahan <sahan.h.fernando@gmail.com>
2021-04-15 12:39:48 +03:00
static unsigned next_device_id;
};
}