mirror of
https://github.com/RGBCube/serenity
synced 2025-05-18 16:55:07 +00:00

This class is intended to replace all IOAddress usages in the Kernel codebase altogether. The idea is to ensure IO can be done in arch-specific manner that is determined mostly in compile-time, but to still be able to use most of the Kernel code in non-x86 builds. Specific devices that rely on x86-specific IO instructions are already placed in the Arch/x86 directory and are omitted for non-x86 builds. The reason this works so well is the fact that x86 IO space acts in a similar fashion to the traditional memory space being available in most CPU architectures - the x86 IO space is essentially just an array of bytes like the physical memory address space, but requires x86 IO instructions to load and store data. Therefore, many devices allow host software to interact with the hardware registers in both ways, with a noticeable trend even in the modern x86 hardware to move away from the old x86 IO space to exclusively using memory-mapped IO. Therefore, the IOWindow class encapsulates both methods for x86 builds. The idea is to allow PCI devices to be used in either way in x86 builds, so when trying to map an IOWindow on a PCI BAR, the Kernel will try to find the proper method being declared with the PCI BAR flags. For old PCI hardware on non-x86 builds this might turn into a problem as we can't use port mapped IO, so the Kernel will gracefully fail with ENOTSUP error code if that's the case, as there's really nothing we can do within such case. For general IO, the read{8,16,32} and write{8,16,32} methods are available as a convenient API for other places in the Kernel. There are simply no direct 64-bit IO API methods yet, as it's not needed right now and is not considered to be Arch-agnostic too - the x86 IO space doesn't support generating 64 bit cycle on IO bus and instead requires two 2 32-bit accesses. If for whatever reason it appears to be necessary to do IO in such manner, it could probably be added with some neat tricks to do so. It is recommended to use Memory::TypedMapping struct if direct 64 bit IO is actually needed.
156 lines
5.7 KiB
C++
156 lines
5.7 KiB
C++
/*
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
//
|
|
// Parallel ATA (PATA) controller driver
|
|
//
|
|
// This driver describes a logical PATA Channel. Each channel can connect up to 2
|
|
// IDE Hard Disk Drives. The drives themselves can be either the master drive (hd0)
|
|
// or the slave drive (hd1).
|
|
//
|
|
// More information about the ATA spec for PATA can be found here:
|
|
// ftp://ftp.seagate.com/acrobat/reference/111-1c.pdf
|
|
//
|
|
|
|
#pragma once
|
|
|
|
#include <AK/Error.h>
|
|
#include <Kernel/Devices/Device.h>
|
|
#include <Kernel/IOWindow.h>
|
|
#include <Kernel/Interrupts/IRQHandler.h>
|
|
#include <Kernel/Library/LockRefPtr.h>
|
|
#include <Kernel/Locking/Mutex.h>
|
|
#include <Kernel/Memory/PhysicalPage.h>
|
|
#include <Kernel/PhysicalAddress.h>
|
|
#include <Kernel/Random.h>
|
|
#include <Kernel/Storage/ATA/ATADevice.h>
|
|
#include <Kernel/Storage/ATA/ATAPort.h>
|
|
#include <Kernel/Storage/StorageDevice.h>
|
|
#include <Kernel/WaitQueue.h>
|
|
|
|
namespace Kernel {
|
|
|
|
class AsyncBlockDeviceRequest;
|
|
|
|
class IDEController;
|
|
#if ARCH(I386) || ARCH(X86_64)
|
|
class PCIIDELegacyModeController;
|
|
class ISAIDEController;
|
|
#endif
|
|
class IDEChannel
|
|
: public ATAPort
|
|
, public IRQHandler {
|
|
friend class IDEController;
|
|
|
|
public:
|
|
enum class ChannelType : u8 {
|
|
Primary,
|
|
Secondary
|
|
};
|
|
|
|
enum class DeviceType : u8 {
|
|
Master,
|
|
Slave,
|
|
};
|
|
|
|
struct IOWindowGroup {
|
|
IOWindowGroup(NonnullOwnPtr<IOWindow> io_window, NonnullOwnPtr<IOWindow> control_window, NonnullOwnPtr<IOWindow> m_bus_master_window)
|
|
: m_io_window(move(io_window))
|
|
, m_control_window(move(control_window))
|
|
, m_bus_master_window(move(m_bus_master_window))
|
|
{
|
|
}
|
|
|
|
IOWindowGroup(NonnullOwnPtr<IOWindow> io_window, NonnullOwnPtr<IOWindow> control_window)
|
|
: m_io_window(move(io_window))
|
|
, m_control_window(move(control_window))
|
|
{
|
|
}
|
|
|
|
// Disable default implementations that would use surprising integer promotion.
|
|
bool operator==(IOWindowGroup const&) const = delete;
|
|
bool operator<=(IOWindowGroup const&) const = delete;
|
|
bool operator>=(IOWindowGroup const&) const = delete;
|
|
bool operator<(IOWindowGroup const&) const = delete;
|
|
bool operator>(IOWindowGroup const&) const = delete;
|
|
|
|
IOWindow& io_window() const { return *m_io_window; };
|
|
IOWindow& control_window() const { return *m_control_window; }
|
|
IOWindow* bus_master_window() const { return m_bus_master_window.ptr(); }
|
|
|
|
private:
|
|
mutable NonnullOwnPtr<IOWindow> m_io_window;
|
|
mutable NonnullOwnPtr<IOWindow> m_control_window;
|
|
mutable OwnPtr<IOWindow> m_bus_master_window;
|
|
};
|
|
|
|
public:
|
|
static NonnullLockRefPtr<IDEChannel> create(IDEController const&, IOWindowGroup, ChannelType type);
|
|
static NonnullLockRefPtr<IDEChannel> create(IDEController const&, u8 irq, IOWindowGroup, ChannelType type);
|
|
|
|
virtual ~IDEChannel() override;
|
|
|
|
virtual StringView purpose() const override { return "PATA Channel"sv; }
|
|
|
|
#if ARCH(I386) || ARCH(X86_64)
|
|
ErrorOr<void> allocate_resources_for_pci_ide_controller(Badge<PCIIDELegacyModeController>, bool force_pio);
|
|
ErrorOr<void> allocate_resources_for_isa_ide_controller(Badge<ISAIDEController>);
|
|
#endif
|
|
|
|
private:
|
|
static constexpr size_t m_logical_sector_size = 512;
|
|
ErrorOr<void> allocate_resources(bool force_pio);
|
|
StringView channel_type_string() const;
|
|
|
|
virtual ErrorOr<void> disable() override { TODO(); }
|
|
virtual ErrorOr<void> power_on() override { TODO(); }
|
|
|
|
virtual ErrorOr<void> port_phy_reset() override;
|
|
bool select_device_and_wait_until_not_busy(DeviceType, size_t milliseconds_timeout);
|
|
|
|
virtual bool pio_capable() const override { return true; }
|
|
virtual bool dma_capable() const override { return m_dma_enabled; }
|
|
|
|
virtual size_t max_possible_devices_connected() const override { return 2; }
|
|
|
|
virtual ErrorOr<void> stop_busmastering() override;
|
|
virtual ErrorOr<void> start_busmastering(TransactionDirection) override;
|
|
virtual ErrorOr<void> force_busmastering_status_clean() override;
|
|
virtual ErrorOr<u8> busmastering_status() override;
|
|
virtual ErrorOr<void> prepare_transaction_with_busmastering(TransactionDirection, PhysicalAddress prdt_buffer) override;
|
|
virtual ErrorOr<void> initiate_transaction(TransactionDirection) override;
|
|
|
|
virtual ErrorOr<u8> task_file_status() override;
|
|
virtual ErrorOr<u8> task_file_error() override;
|
|
|
|
virtual ErrorOr<void> wait_if_busy_until_timeout(size_t timeout_in_milliseconds) override;
|
|
|
|
virtual ErrorOr<void> device_select(size_t device_index) override;
|
|
virtual ErrorOr<bool> detect_presence_on_selected_device() override;
|
|
|
|
virtual ErrorOr<void> enable_interrupts() override;
|
|
virtual ErrorOr<void> disable_interrupts() override;
|
|
|
|
virtual ErrorOr<void> force_clear_interrupts() override;
|
|
virtual ErrorOr<void> load_taskfile_into_registers(TaskFile const&, LBAMode lba_mode, size_t completion_timeout_in_milliseconds) override;
|
|
|
|
virtual ErrorOr<void> read_pio_data_to_buffer(UserOrKernelBuffer&, size_t block_offset, size_t words_count) override;
|
|
virtual ErrorOr<void> write_pio_data_from_buffer(UserOrKernelBuffer const&, size_t block_offset, size_t words_count) override;
|
|
|
|
IDEChannel(IDEController const&, IOWindowGroup, ChannelType type, NonnullOwnPtr<KBuffer> ata_identify_data_buffer);
|
|
IDEChannel(IDEController const&, u8 irq, IOWindowGroup, ChannelType type, NonnullOwnPtr<KBuffer> ata_identify_data_buffer);
|
|
//^ IRQHandler
|
|
virtual bool handle_irq(RegisterState const&) override;
|
|
|
|
// Data members
|
|
ChannelType m_channel_type { ChannelType::Primary };
|
|
|
|
bool m_dma_enabled { false };
|
|
bool m_interrupts_enabled { true };
|
|
|
|
IOWindowGroup m_io_window_group;
|
|
};
|
|
}
|