2021-02-26 01:25:45 -03:00
|
|
|
/*
|
2022-04-01 17:45:07 -03:00
|
|
|
* Copyright (c) 2021-2022, Liav A. <liavalb@hotmail.co.il>
|
2021-02-26 01:25:45 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-02-26 01:25:45 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <AK/Atomic.h>
|
2021-12-19 18:46:55 -03:00
|
|
|
#include <AK/BuiltinWrappers.h>
|
2021-02-26 01:25:45 -03:00
|
|
|
#include <AK/OwnPtr.h>
|
|
|
|
|
#include <AK/Types.h>
|
2022-09-02 05:23:32 -03:00
|
|
|
#include <Kernel/Arch/Delay.h>
|
Kernel/PCI: Simplify the entire subsystem
A couple of things were changed:
1. Semantic changes - PCI segments are now called PCI domains, to better
match what they are really. It's also the name that Linux gave, and it
seems that Wikipedia also uses this name.
We also remove PCI::ChangeableAddress, because it was used in the past
but now it's no longer being used.
2. There are no WindowedMMIOAccess or MMIOAccess classes anymore, as
they made a bunch of unnecessary complexity. Instead, Windowed access is
removed entirely (this was tested, but never was benchmarked), so we are
left with IO access and memory access options. The memory access option
is essentially mapping the PCI bus (from the chosen PCI domain), to
virtual memory as-is. This means that unless needed, at any time, there
is only one PCI bus being mapped, and this is changed if access to
another PCI bus in the same PCI domain is needed. For now, we don't
support mapping of different PCI buses from different PCI domains at the
same time, because basically it's still a non-issue for most machines
out there.
2. OOM-safety is increased, especially when constructing the Access
object. It means that we pre-allocating any needed resources, and we try
to find PCI domains (if requested to initialize memory access) after we
attempt to construct the Access object, so it's possible to fail at this
point "gracefully".
3. All PCI API functions are now separated into a different header file,
which means only "clients" of the PCI subsystem API will need to include
that header file.
4. Functional changes - we only allow now to enumerate the bus after
a hardware scan. This means that the old method "enumerate_hardware"
is removed, so, when initializing an Access object, the initializing
function must call rescan on it to force it to find devices. This makes
it possible to fail rescan, and also to defer it after construction from
both OOM-safety terms and hotplug capabilities.
2021-09-07 06:08:38 -03:00
|
|
|
#include <Kernel/Bus/PCI/API.h>
|
2022-04-01 17:45:07 -03:00
|
|
|
#include <Kernel/CommandLine.h>
|
2022-08-19 15:53:40 -03:00
|
|
|
#include <Kernel/Library/LockRefPtr.h>
|
2021-08-06 05:45:34 -03:00
|
|
|
#include <Kernel/Memory/MemoryManager.h>
|
2021-11-19 06:52:07 -03:00
|
|
|
#include <Kernel/Storage/ATA/AHCI/Controller.h>
|
|
|
|
|
#include <Kernel/Storage/ATA/AHCI/InterruptHandler.h>
|
2021-02-26 01:25:45 -03:00
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
2022-08-19 15:53:40 -03:00
|
|
|
UNMAP_AFTER_INIT NonnullLockRefPtr<AHCIController> AHCIController::initialize(PCI::DeviceIdentifier const& pci_device_identifier)
|
2021-02-26 01:25:45 -03:00
|
|
|
{
|
2022-08-19 15:53:40 -03:00
|
|
|
auto controller = adopt_lock_ref_if_nonnull(new (nothrow) AHCIController(pci_device_identifier)).release_nonnull();
|
2022-04-01 14:49:22 -03:00
|
|
|
controller->initialize_hba(pci_device_identifier);
|
|
|
|
|
return controller;
|
2021-02-26 01:25:45 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool AHCIController::reset()
|
|
|
|
|
{
|
2022-04-14 16:47:17 -03:00
|
|
|
dmesgln("{}: AHCI controller reset", pci_address());
|
|
|
|
|
{
|
|
|
|
|
SpinlockLocker locker(m_hba_control_lock);
|
|
|
|
|
hba().control_regs.ghc = 1;
|
|
|
|
|
|
|
|
|
|
dbgln_if(AHCI_DEBUG, "{}: AHCI Controller reset", pci_address());
|
|
|
|
|
|
|
|
|
|
full_memory_barrier();
|
|
|
|
|
size_t retry = 0;
|
|
|
|
|
|
|
|
|
|
// Note: The HBA is locked or hung if we waited more than 1 second!
|
|
|
|
|
while (true) {
|
|
|
|
|
if (retry > 1000)
|
|
|
|
|
return false;
|
|
|
|
|
if (!(hba().control_regs.ghc & 1))
|
|
|
|
|
break;
|
2022-09-02 05:23:32 -03:00
|
|
|
microseconds_delay(1000);
|
2022-04-14 16:47:17 -03:00
|
|
|
retry++;
|
|
|
|
|
}
|
|
|
|
|
// Note: Turn on AHCI HBA and Global HBA Interrupts.
|
|
|
|
|
full_memory_barrier();
|
|
|
|
|
hba().control_regs.ghc = (1 << 31) | (1 << 1);
|
|
|
|
|
full_memory_barrier();
|
|
|
|
|
}
|
2021-03-18 02:24:39 -03:00
|
|
|
|
2022-04-14 16:47:17 -03:00
|
|
|
// Note: According to the AHCI spec the PI register indicates which ports are exposed by the HBA.
|
|
|
|
|
// It is loaded by the BIOS. It indicates which ports that the HBA supports are available for software to use.
|
|
|
|
|
// For example, on an HBA that supports 6 ports as indicated in CAP.NP, only ports 1 and 3 could be available,
|
|
|
|
|
// with ports 0, 2, 4, and 5 being unavailable.
|
|
|
|
|
// Which means that even without clearing the AHCI ports array, we are never able to encounter
|
|
|
|
|
// a case that we would have stale left-over ports in there. We still clear the array
|
|
|
|
|
// for the sake of clarity and completeness, as it doesn't harm anything anyway.
|
|
|
|
|
m_ports.fill({});
|
2021-02-26 01:25:45 -03:00
|
|
|
|
2022-04-14 16:47:17 -03:00
|
|
|
auto implemented_ports = AHCI::MaskedBitField((u32 volatile&)(hba().control_regs.pi));
|
|
|
|
|
for (auto index : implemented_ports.to_vector()) {
|
|
|
|
|
auto port = AHCIPort::create(*this, m_hba_capabilities, static_cast<volatile AHCI::PortRegisters&>(hba().port_regs[index]), index).release_value_but_fixme_should_propagate_errors();
|
|
|
|
|
m_ports[index] = port;
|
|
|
|
|
port->reset();
|
2021-02-26 01:25:45 -03:00
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool AHCIController::shutdown()
|
|
|
|
|
{
|
|
|
|
|
TODO();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t AHCIController::devices_count() const
|
|
|
|
|
{
|
2022-04-14 16:47:17 -03:00
|
|
|
SpinlockLocker locker(m_hba_control_lock);
|
2021-02-26 01:25:45 -03:00
|
|
|
size_t count = 0;
|
2022-04-01 17:45:07 -03:00
|
|
|
for (auto port : m_ports) {
|
|
|
|
|
if (port && port->connected_device())
|
|
|
|
|
count++;
|
2021-02-26 01:25:45 -03:00
|
|
|
}
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
void AHCIController::start_request(ATADevice const& device, AsyncBlockDeviceRequest& request)
|
2021-02-26 01:25:45 -03:00
|
|
|
{
|
2022-04-01 17:45:07 -03:00
|
|
|
auto port = m_ports[device.ata_address().port];
|
2021-08-27 11:13:03 -03:00
|
|
|
VERIFY(port);
|
|
|
|
|
port->start_request(request);
|
2021-02-26 01:25:45 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AHCIController::complete_current_request(AsyncDeviceRequest::RequestResult)
|
|
|
|
|
{
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
volatile AHCI::PortRegisters& AHCIController::port(size_t port_number) const
|
|
|
|
|
{
|
|
|
|
|
VERIFY(port_number < (size_t)AHCI::Limits::MaxPorts);
|
|
|
|
|
return static_cast<volatile AHCI::PortRegisters&>(hba().port_regs[port_number]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
volatile AHCI::HBA& AHCIController::hba() const
|
|
|
|
|
{
|
|
|
|
|
return static_cast<volatile AHCI::HBA&>(*(volatile AHCI::HBA*)(m_hba_region->vaddr().as_ptr()));
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 14:52:37 -03:00
|
|
|
UNMAP_AFTER_INIT AHCIController::AHCIController(PCI::DeviceIdentifier const& pci_device_identifier)
|
2021-08-27 11:13:03 -03:00
|
|
|
: ATAController()
|
2021-09-23 04:50:45 -03:00
|
|
|
, PCI::Device(pci_device_identifier.address())
|
2021-05-28 18:50:56 -03:00
|
|
|
, m_hba_region(default_hba_region())
|
2022-04-01 17:45:07 -03:00
|
|
|
, m_hba_capabilities(capabilities())
|
2021-02-26 01:25:45 -03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AHCI::HBADefinedCapabilities AHCIController::capabilities() const
|
|
|
|
|
{
|
|
|
|
|
u32 capabilities = hba().control_regs.cap;
|
2021-03-17 22:03:09 -03:00
|
|
|
u32 extended_capabilities = hba().control_regs.cap2;
|
2021-03-18 02:24:39 -03:00
|
|
|
|
2021-07-21 20:30:24 -03:00
|
|
|
dbgln_if(AHCI_DEBUG, "{}: AHCI Controller Capabilities = {:#08x}, Extended Capabilities = {:#08x}", pci_address(), capabilities, extended_capabilities);
|
2021-03-18 02:24:39 -03:00
|
|
|
|
2021-02-26 01:25:45 -03:00
|
|
|
return (AHCI::HBADefinedCapabilities) {
|
|
|
|
|
(capabilities & 0b11111) + 1,
|
|
|
|
|
((capabilities >> 8) & 0b11111) + 1,
|
|
|
|
|
(u8)((capabilities >> 20) & 0b1111),
|
2021-03-17 21:55:05 -03:00
|
|
|
(capabilities & (u32)(AHCI::HBACapabilities::SXS)) != 0,
|
|
|
|
|
(capabilities & (u32)(AHCI::HBACapabilities::EMS)) != 0,
|
|
|
|
|
(capabilities & (u32)(AHCI::HBACapabilities::CCCS)) != 0,
|
|
|
|
|
(capabilities & (u32)(AHCI::HBACapabilities::PSC)) != 0,
|
|
|
|
|
(capabilities & (u32)(AHCI::HBACapabilities::SSC)) != 0,
|
|
|
|
|
(capabilities & (u32)(AHCI::HBACapabilities::PMD)) != 0,
|
|
|
|
|
(capabilities & (u32)(AHCI::HBACapabilities::FBSS)) != 0,
|
|
|
|
|
(capabilities & (u32)(AHCI::HBACapabilities::SPM)) != 0,
|
|
|
|
|
(capabilities & (u32)(AHCI::HBACapabilities::SAM)) != 0,
|
|
|
|
|
(capabilities & (u32)(AHCI::HBACapabilities::SCLO)) != 0,
|
|
|
|
|
(capabilities & (u32)(AHCI::HBACapabilities::SAL)) != 0,
|
|
|
|
|
(capabilities & (u32)(AHCI::HBACapabilities::SALP)) != 0,
|
|
|
|
|
(capabilities & (u32)(AHCI::HBACapabilities::SSS)) != 0,
|
|
|
|
|
(capabilities & (u32)(AHCI::HBACapabilities::SMPS)) != 0,
|
|
|
|
|
(capabilities & (u32)(AHCI::HBACapabilities::SSNTF)) != 0,
|
|
|
|
|
(capabilities & (u32)(AHCI::HBACapabilities::SNCQ)) != 0,
|
2021-03-17 22:03:09 -03:00
|
|
|
(capabilities & (u32)(AHCI::HBACapabilities::S64A)) != 0,
|
2021-05-15 14:20:49 -03:00
|
|
|
(extended_capabilities & (u32)(AHCI::HBACapabilitiesExtended::BOH)) != 0,
|
|
|
|
|
(extended_capabilities & (u32)(AHCI::HBACapabilitiesExtended::NVMP)) != 0,
|
2021-03-17 22:03:09 -03:00
|
|
|
(extended_capabilities & (u32)(AHCI::HBACapabilitiesExtended::APST)) != 0,
|
|
|
|
|
(extended_capabilities & (u32)(AHCI::HBACapabilitiesExtended::SDS)) != 0,
|
|
|
|
|
(extended_capabilities & (u32)(AHCI::HBACapabilitiesExtended::SADM)) != 0,
|
|
|
|
|
(extended_capabilities & (u32)(AHCI::HBACapabilitiesExtended::DESO)) != 0
|
2021-02-26 01:25:45 -03:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 14:52:37 -03:00
|
|
|
UNMAP_AFTER_INIT NonnullOwnPtr<Memory::Region> AHCIController::default_hba_region() const
|
2021-02-26 01:25:45 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
return MM.allocate_kernel_region(PhysicalAddress(PCI::get_BAR5(pci_address())).page_base(), Memory::page_round_up(sizeof(AHCI::HBA)).release_value_but_fixme_should_propagate_errors(), "AHCI HBA"sv, Memory::Region::Access::ReadWrite).release_value();
|
2021-02-26 01:25:45 -03:00
|
|
|
}
|
|
|
|
|
|
2022-03-16 16:15:15 -03:00
|
|
|
AHCIController::~AHCIController() = default;
|
2021-02-26 01:25:45 -03:00
|
|
|
|
2022-04-01 14:52:37 -03:00
|
|
|
UNMAP_AFTER_INIT void AHCIController::initialize_hba(PCI::DeviceIdentifier const& pci_device_identifier)
|
2021-02-26 01:25:45 -03:00
|
|
|
{
|
2021-03-18 02:24:39 -03:00
|
|
|
u32 version = hba().control_regs.version;
|
|
|
|
|
|
2021-02-26 01:25:45 -03:00
|
|
|
hba().control_regs.ghc = 0x80000000; // Ensure that HBA knows we are AHCI aware.
|
|
|
|
|
PCI::enable_interrupt_line(pci_address());
|
|
|
|
|
PCI::enable_bus_mastering(pci_address());
|
|
|
|
|
enable_global_interrupts();
|
2022-04-01 14:49:22 -03:00
|
|
|
|
2022-04-01 17:45:07 -03:00
|
|
|
auto implemented_ports = AHCI::MaskedBitField((u32 volatile&)(hba().control_regs.pi));
|
2022-04-01 17:48:46 -03:00
|
|
|
m_irq_handler = AHCIInterruptHandler::create(*this, pci_device_identifier.interrupt_line().value(), implemented_ports).release_value_but_fixme_should_propagate_errors();
|
2022-04-14 16:47:17 -03:00
|
|
|
reset();
|
|
|
|
|
dbgln_if(AHCI_DEBUG, "{}: AHCI Controller Version = {:#08x}", pci_address(), version);
|
|
|
|
|
dbgln("{}: AHCI command list entries count - {}", pci_address(), m_hba_capabilities.max_command_list_entries_count);
|
2022-04-01 17:45:07 -03:00
|
|
|
}
|
|
|
|
|
|
2022-04-01 17:48:46 -03:00
|
|
|
void AHCIController::handle_interrupt_for_port(Badge<AHCIInterruptHandler>, u32 port_index) const
|
2022-04-01 17:45:07 -03:00
|
|
|
{
|
|
|
|
|
auto port = m_ports[port_index];
|
|
|
|
|
VERIFY(port);
|
|
|
|
|
port->handle_interrupt();
|
2021-02-26 01:25:45 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AHCIController::disable_global_interrupts() const
|
|
|
|
|
{
|
|
|
|
|
hba().control_regs.ghc = hba().control_regs.ghc & 0xfffffffd;
|
|
|
|
|
}
|
|
|
|
|
void AHCIController::enable_global_interrupts() const
|
|
|
|
|
{
|
|
|
|
|
hba().control_regs.ghc = hba().control_regs.ghc | (1 << 1);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-19 15:53:40 -03:00
|
|
|
LockRefPtr<StorageDevice> AHCIController::device_by_port(u32 port_index) const
|
2021-02-26 01:25:45 -03:00
|
|
|
{
|
2022-04-14 16:47:17 -03:00
|
|
|
SpinlockLocker locker(m_hba_control_lock);
|
2022-04-01 17:45:07 -03:00
|
|
|
auto port = m_ports[port_index];
|
|
|
|
|
if (!port)
|
|
|
|
|
return {};
|
2022-04-14 16:47:17 -03:00
|
|
|
SpinlockLocker port_hard_locker(port->m_hard_lock);
|
2022-04-01 17:45:07 -03:00
|
|
|
return port->connected_device();
|
2021-02-26 01:25:45 -03:00
|
|
|
}
|
|
|
|
|
|
2022-08-19 15:53:40 -03:00
|
|
|
LockRefPtr<StorageDevice> AHCIController::device(u32 index) const
|
2021-02-26 01:25:45 -03:00
|
|
|
{
|
2022-08-19 15:53:40 -03:00
|
|
|
NonnullLockRefPtrVector<StorageDevice> connected_devices;
|
2021-06-25 10:49:33 -03:00
|
|
|
u32 pi = hba().control_regs.pi;
|
2021-12-19 18:46:55 -03:00
|
|
|
u32 bit = bit_scan_forward(pi);
|
2021-06-25 10:49:33 -03:00
|
|
|
while (bit) {
|
|
|
|
|
dbgln_if(AHCI_DEBUG, "Checking implemented port {}, pi {:b}", bit - 1, pi);
|
|
|
|
|
pi &= ~(1u << (bit - 1));
|
|
|
|
|
auto checked_device = device_by_port(bit - 1);
|
2021-12-19 18:46:55 -03:00
|
|
|
bit = bit_scan_forward(pi);
|
2021-02-26 01:25:45 -03:00
|
|
|
if (checked_device.is_null())
|
|
|
|
|
continue;
|
|
|
|
|
connected_devices.append(checked_device.release_nonnull());
|
|
|
|
|
}
|
2021-06-25 10:49:33 -03:00
|
|
|
dbgln_if(AHCI_DEBUG, "Connected device count: {}, Index: {}", connected_devices.size(), index);
|
2021-02-26 01:25:45 -03:00
|
|
|
if (index >= connected_devices.size())
|
|
|
|
|
return nullptr;
|
|
|
|
|
return connected_devices[index];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|