Working AHCI DMA IPC from yellowstone to denali.
We have a weird bug in context switching where a process's rsp0 ends up pointing at the wrong value sometimes which crashes the OS.
This commit is contained in:
parent
ccfe1b15ab
commit
e5da93757a
11 changed files with 171 additions and 84 deletions
|
|
@ -96,7 +96,7 @@ struct CommandTable {
|
|||
uint8_t command_fis[64];
|
||||
uint8_t atapi_command[16];
|
||||
uint8_t reserved[48];
|
||||
PhysicalRegionDescriptor prds[65535];
|
||||
PhysicalRegionDescriptor prdt[65535];
|
||||
} __attribute__((packed));
|
||||
|
||||
typedef enum {
|
||||
|
|
|
|||
|
|
@ -4,12 +4,6 @@
|
|||
#include <string.h>
|
||||
#include <zcall.h>
|
||||
|
||||
namespace {
|
||||
|
||||
void HandleIdent(AhciDevice* dev) { dev->HandleIdentify(); }
|
||||
|
||||
} // namespace
|
||||
|
||||
AhciDevice::AhciDevice(AhciPort* port) : port_struct_(port) {
|
||||
if ((port_struct_->sata_status & 0x103) != 0x103) {
|
||||
return;
|
||||
|
|
@ -38,59 +32,25 @@ AhciDevice::AhciDevice(AhciPort* port) : port_struct_(port) {
|
|||
reinterpret_cast<CommandTable*>(command_structures_.vaddr() + ct_off);
|
||||
|
||||
port_struct_->interrupt_enable = 0xFFFFFFFF;
|
||||
|
||||
if (port_struct_->signature == 0x101) {
|
||||
SendIdentify();
|
||||
}
|
||||
}
|
||||
|
||||
z_err_t AhciDevice::SendIdentify() {
|
||||
HostToDeviceRegisterFis fis{
|
||||
.fis_type = FIS_TYPE_REG_H2D,
|
||||
.pmp_and_c = 0x80,
|
||||
.command = 0xEC,
|
||||
.featurel = 0,
|
||||
z_err_t AhciDevice::IssueCommand(Command* command) {
|
||||
command->PopulateFis(command_table_->command_fis);
|
||||
command->PopulatePrdt(command_table_->prdt);
|
||||
|
||||
.lba0 = 0,
|
||||
.lba1 = 0,
|
||||
.lba2 = 0,
|
||||
.device = 0,
|
||||
|
||||
.lba3 = 0,
|
||||
.lba4 = 0,
|
||||
.lba5 = 0,
|
||||
.featureh = 0,
|
||||
|
||||
.count = 0,
|
||||
.icc = 0,
|
||||
.control = 0,
|
||||
|
||||
.reserved = 0,
|
||||
};
|
||||
|
||||
command_list_->command_headers[0].command = (sizeof(fis) / 2) & 0x1F;
|
||||
command_list_->command_headers[0].command =
|
||||
(sizeof(HostToDeviceRegisterFis) / 2) & 0x1F;
|
||||
command_list_->command_headers[0].prd_table_length = 1;
|
||||
command_list_->command_headers[0].prd_byte_count = 0;
|
||||
|
||||
memcpy(command_table_->command_fis, &fis, sizeof(fis));
|
||||
commands_[0] = command;
|
||||
|
||||
commands_[0].region = MappedMemoryRegion::ContiguousPhysical(512);
|
||||
// commands_[0].callback = HandleIdent;
|
||||
|
||||
command_table_->prds[0].region_address = commands_[0].region.paddr();
|
||||
command_table_->prds[0].byte_count = 512;
|
||||
|
||||
port_struct_->command_issue |= 1;
|
||||
commands_issued_ |= 1;
|
||||
port_struct_->command_issue |= 1;
|
||||
|
||||
return Z_OK;
|
||||
}
|
||||
|
||||
void AhciDevice::HandleIdentify() {
|
||||
dbgln("Handling Idenify");
|
||||
uint16_t* ident = reinterpret_cast<uint16_t*>(commands_[0].region.vaddr());
|
||||
dbgln("Ident: %x", ident[0]);
|
||||
}
|
||||
|
||||
void AhciDevice::DumpInfo() {
|
||||
dbgln("Comlist: %lx", port_struct_->command_list_base);
|
||||
dbgln("FIS: %lx", port_struct_->fis_base);
|
||||
|
|
@ -116,11 +76,15 @@ void AhciDevice::HandleIrq() {
|
|||
port_struct_->interrupt_status = int_status;
|
||||
|
||||
uint32_t commands_finished = commands_issued_ & ~port_struct_->command_issue;
|
||||
dbgln("finished %x", commands_finished);
|
||||
dbgln("status %x", int_status);
|
||||
dbgln("Issued %x, %x", commands_issued_, port_struct_->command_issue);
|
||||
|
||||
// FIXME: Pass error codes to the callback.
|
||||
for (uint64_t i = 0; i < 32; i++) {
|
||||
if (commands_finished & (1 << i)) {
|
||||
// commands_[i].callback(this);
|
||||
commands_issued_ &= ~(1 << i);
|
||||
commands_[i]->Callback();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -134,6 +98,7 @@ void AhciDevice::HandleIrq() {
|
|||
}
|
||||
if (fis.error) {
|
||||
dbgln("D2H err: %x", fis.error);
|
||||
dbgln("status: %x", fis.status);
|
||||
}
|
||||
}
|
||||
if (int_status & 0x2) {
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
#include <zerrors.h>
|
||||
|
||||
#include "ahci/ahci.h"
|
||||
#include "ahci/command.h"
|
||||
|
||||
class AhciDevice {
|
||||
public:
|
||||
|
|
@ -15,11 +16,13 @@ class AhciDevice {
|
|||
|
||||
bool IsInit() { return port_struct_ != nullptr && command_structures_; }
|
||||
|
||||
z_err_t SendIdentify();
|
||||
void HandleIdentify();
|
||||
z_err_t IssueCommand(Command* command);
|
||||
|
||||
void HandleIrq();
|
||||
|
||||
AhciDevice(const AhciDevice&) = delete;
|
||||
AhciDevice& operator=(const AhciDevice&) = delete;
|
||||
|
||||
private:
|
||||
AhciPort* port_struct_ = nullptr;
|
||||
MappedMemoryRegion command_structures_;
|
||||
|
|
@ -28,10 +31,6 @@ class AhciDevice {
|
|||
ReceivedFis* received_fis_ = nullptr;
|
||||
CommandTable* command_table_ = nullptr;
|
||||
|
||||
struct Command {
|
||||
MappedMemoryRegion region;
|
||||
// std::function<void(MappedMemoryRegion)> callback;
|
||||
};
|
||||
Command commands_[32];
|
||||
uint32_t commands_issued_ = 0;
|
||||
Command* commands_[32];
|
||||
volatile uint32_t commands_issued_ = 0;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -33,16 +33,16 @@ z_err_t AhciDriver::Init() {
|
|||
return Z_OK;
|
||||
}
|
||||
|
||||
z_err_t AhciDriver::GetDevice(uint64_t id, AhciDevice& device) {
|
||||
z_err_t AhciDriver::GetDevice(uint64_t id, AhciDevice** device) {
|
||||
if (id >= 32) {
|
||||
return Z_ERR_INVALID;
|
||||
}
|
||||
|
||||
if (!devices_[id].IsInit()) {
|
||||
if (devices_[id] != nullptr && !devices_[id]->IsInit()) {
|
||||
return Z_ERR_NOT_FOUND;
|
||||
}
|
||||
|
||||
device = devices_[id];
|
||||
*device = devices_[id];
|
||||
return Z_OK;
|
||||
}
|
||||
|
||||
|
|
@ -126,24 +126,27 @@ void AhciDriver::DumpCapabilities() {
|
|||
|
||||
void AhciDriver::DumpPorts() {
|
||||
for (uint64_t i = 0; i < 6; i++) {
|
||||
AhciDevice& dev = devices_[i];
|
||||
if (!dev.IsInit()) {
|
||||
AhciDevice* dev = devices_[i];
|
||||
if (dev == nullptr || !dev->IsInit()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
dbgln("");
|
||||
dbgln("Port %u:", i);
|
||||
dev.DumpInfo();
|
||||
dev->DumpInfo();
|
||||
}
|
||||
}
|
||||
|
||||
void AhciDriver::InterruptLoop() {
|
||||
dbgln("Starting interrupt loop");
|
||||
while (true) {
|
||||
uint64_t type, bytes, caps;
|
||||
check(ZPortRecv(irq_port_cap_, 0, 0, 0, 0, &type, &bytes, &caps));
|
||||
for (uint64_t i = 0; i < 32; i++) {
|
||||
if (devices_[i].IsInit() && (ahci_hba_->interrupt_status & (1 << i))) {
|
||||
devices_[i].HandleIrq();
|
||||
if (devices_[i] != nullptr && devices_[i]->IsInit() &&
|
||||
(ahci_hba_->interrupt_status & (1 << i))) {
|
||||
dbgln("Interrupt for %u", i);
|
||||
devices_[i]->HandleIrq();
|
||||
ahci_hba_->interrupt_status &= ~(1 << i);
|
||||
}
|
||||
}
|
||||
|
|
@ -212,7 +215,7 @@ z_err_t AhciDriver::LoadDevices() {
|
|||
}
|
||||
uint64_t port_addr =
|
||||
reinterpret_cast<uint64_t>(ahci_hba_) + 0x100 + (0x80 * i);
|
||||
devices_[i] = AhciDevice(reinterpret_cast<AhciPort*>(port_addr));
|
||||
devices_[i] = new AhciDevice(reinterpret_cast<AhciPort*>(port_addr));
|
||||
}
|
||||
return Z_OK;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ class AhciDriver {
|
|||
|
||||
void InterruptLoop();
|
||||
|
||||
z_err_t GetDevice(uint64_t id, AhciDevice& device);
|
||||
z_err_t GetDevice(uint64_t id, AhciDevice** device);
|
||||
|
||||
void DumpCapabilities();
|
||||
void DumpPorts();
|
||||
|
|
@ -24,7 +24,7 @@ class AhciDriver {
|
|||
AhciHba* ahci_hba_ = nullptr;
|
||||
|
||||
// TODO: Allocate these dynamically.
|
||||
AhciDevice devices_[32];
|
||||
AhciDevice* devices_[32];
|
||||
|
||||
Thread irq_thread_;
|
||||
uint64_t irq_port_cap_ = 0;
|
||||
|
|
|
|||
49
sys/denali/ahci/command.cpp
Normal file
49
sys/denali/ahci/command.cpp
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
#include "ahci/command.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "ahci/ahci.h"
|
||||
|
||||
Command::~Command() {}
|
||||
|
||||
DmaReadCommand::DmaReadCommand(uint64_t lba, uint64_t sector_cnt,
|
||||
DmaCallback callback)
|
||||
: lba_(lba), sector_cnt_(sector_cnt), callback_(callback) {
|
||||
region_ = MappedMemoryRegion::ContiguousPhysical(sector_cnt * 512);
|
||||
}
|
||||
|
||||
DmaReadCommand::~DmaReadCommand() {}
|
||||
|
||||
void DmaReadCommand::PopulateFis(uint8_t* command_fis) {
|
||||
HostToDeviceRegisterFis fis{
|
||||
.fis_type = FIS_TYPE_REG_H2D,
|
||||
.pmp_and_c = 0x80,
|
||||
.command = 0x25,
|
||||
.featurel = 0,
|
||||
|
||||
.lba0 = static_cast<uint8_t>(lba_ & 0xFF),
|
||||
.lba1 = static_cast<uint8_t>((lba_ >> 8) & 0xFF),
|
||||
.lba2 = static_cast<uint8_t>((lba_ >> 16) & 0xFF),
|
||||
.device = (1 << 6), // ATA LBA Mode
|
||||
|
||||
.lba3 = static_cast<uint8_t>((lba_ >> 24) & 0xFF),
|
||||
.lba4 = static_cast<uint8_t>((lba_ >> 32) & 0xFF),
|
||||
.lba5 = static_cast<uint8_t>((lba_ >> 40) & 0xFF),
|
||||
.featureh = 0,
|
||||
|
||||
.count = static_cast<uint16_t>(sector_cnt_),
|
||||
.icc = 0,
|
||||
.control = 0,
|
||||
|
||||
.reserved = 0,
|
||||
};
|
||||
|
||||
uint64_t bytes = sector_cnt_ * 512;
|
||||
|
||||
memcpy(command_fis, &fis, sizeof(fis));
|
||||
}
|
||||
void DmaReadCommand::PopulatePrdt(PhysicalRegionDescriptor* prdt) {
|
||||
prdt[0].region_address = region_.paddr();
|
||||
prdt[0].byte_count = region_.size();
|
||||
}
|
||||
void DmaReadCommand::Callback() { callback_(lba_, sector_cnt_, region_.cap()); }
|
||||
33
sys/denali/ahci/command.h
Normal file
33
sys/denali/ahci/command.h
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#pragma once
|
||||
|
||||
#include <mammoth/memory_region.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "ahci/ahci.h"
|
||||
|
||||
class Command {
|
||||
public:
|
||||
virtual ~Command();
|
||||
virtual void PopulateFis(uint8_t* command_fis) = 0;
|
||||
virtual void PopulatePrdt(PhysicalRegionDescriptor* prdt) = 0;
|
||||
virtual void Callback() = 0;
|
||||
};
|
||||
|
||||
class DmaReadCommand : public Command {
|
||||
public:
|
||||
typedef void (*DmaCallback)(uint64_t, uint64_t, uint64_t);
|
||||
DmaReadCommand(uint64_t lba, uint64_t sector_cnt, DmaCallback callback);
|
||||
|
||||
virtual ~DmaReadCommand() override;
|
||||
|
||||
void PopulateFis(uint8_t* command_fis) override;
|
||||
void PopulatePrdt(PhysicalRegionDescriptor* prdt) override;
|
||||
|
||||
void Callback() override;
|
||||
|
||||
private:
|
||||
uint64_t lba_;
|
||||
uint64_t sector_cnt_;
|
||||
DmaCallback callback_;
|
||||
MappedMemoryRegion region_;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue