[Mammoth] Get rid of last uses of MappedMemoryRegion.

This commit is contained in:
Drew Galbraith 2023-11-19 20:48:23 -08:00
parent d41a565721
commit 344e84c313
6 changed files with 20 additions and 56 deletions

View File

@ -3,34 +3,6 @@
#include <stdint.h> #include <stdint.h>
#include <ztypes.h> #include <ztypes.h>
class MappedMemoryRegion {
public:
// FIXME: Introduce optional type to contain error or.
static MappedMemoryRegion DirectPhysical(uint64_t phys_addr, uint64_t size);
static MappedMemoryRegion Default(uint64_t size);
MappedMemoryRegion() {}
// TODO: Disallow copy before doing any cleanup here.
~MappedMemoryRegion() {}
uint64_t paddr() { return paddr_; }
uint64_t vaddr() { return vaddr_; }
uint64_t size() { return size_; }
uint64_t cap() { return vmmo_cap_; }
operator bool() { return vmmo_cap_ != 0; }
private:
MappedMemoryRegion(uint64_t vmmo_cap, uint64_t paddr, uint64_t vaddr,
uint64_t size)
: vmmo_cap_(vmmo_cap), paddr_(paddr), vaddr_(vaddr), size_(size) {}
uint64_t vmmo_cap_ = 0;
uint64_t paddr_ = 0;
uint64_t vaddr_ = 0;
uint64_t size_ = 0;
};
/* /*
* Memory Region class that unmaps its memory and releases its * Memory Region class that unmaps its memory and releases its
* capability when it goes out of scope. * capability when it goes out of scope.
@ -50,6 +22,7 @@ class OwnedMemoryRegion {
static OwnedMemoryRegion FromCapability(z_cap_t vmmo_cap); static OwnedMemoryRegion FromCapability(z_cap_t vmmo_cap);
// TODO: Consider making this its own class. // TODO: Consider making this its own class.
static OwnedMemoryRegion ContiguousPhysical(uint64_t size, uint64_t* paddr); static OwnedMemoryRegion ContiguousPhysical(uint64_t size, uint64_t* paddr);
static OwnedMemoryRegion DirectPhysical(uint64_t paddr, uint64_t size);
uint64_t vaddr() { return vaddr_; } uint64_t vaddr() { return vaddr_; }
uint64_t size() { return size_; } uint64_t size() { return size_; }
@ -65,5 +38,6 @@ class OwnedMemoryRegion {
: vmmo_cap_(vmmo_cap), vaddr_(vaddr), size_(size) {} : vmmo_cap_(vmmo_cap), vaddr_(vaddr), size_(size) {}
uint64_t vmmo_cap_ = 0; uint64_t vmmo_cap_ = 0;
uint64_t vaddr_ = 0; uint64_t vaddr_ = 0;
// TODO: We may want to differentiate between VMMO size and mapped size?
uint64_t size_ = 0; uint64_t size_ = 0;
}; };

View File

@ -5,27 +5,6 @@
#include "mammoth/debug.h" #include "mammoth/debug.h"
#include "mammoth/init.h" #include "mammoth/init.h"
MappedMemoryRegion MappedMemoryRegion::DirectPhysical(uint64_t paddr,
uint64_t size) {
uint64_t vmmo_cap;
check(ZMemoryObjectCreatePhysical(paddr, size, &vmmo_cap));
uint64_t vaddr;
check(ZAddressSpaceMap(gSelfVmasCap, 0, vmmo_cap, &vaddr));
return MappedMemoryRegion(vmmo_cap, paddr, vaddr, size);
}
MappedMemoryRegion MappedMemoryRegion::Default(uint64_t size) {
uint64_t vmmo_cap;
check(ZMemoryObjectCreate(size, &vmmo_cap));
uint64_t vaddr;
check(ZAddressSpaceMap(gSelfVmasCap, 0, vmmo_cap, &vaddr));
return MappedMemoryRegion(vmmo_cap, 0, vaddr, size);
}
OwnedMemoryRegion::OwnedMemoryRegion(OwnedMemoryRegion&& other) OwnedMemoryRegion::OwnedMemoryRegion(OwnedMemoryRegion&& other)
: OwnedMemoryRegion(other.vmmo_cap_, other.vaddr_, other.size_) { : OwnedMemoryRegion(other.vmmo_cap_, other.vaddr_, other.size_) {
other.vmmo_cap_ = 0; other.vmmo_cap_ = 0;
@ -74,6 +53,16 @@ OwnedMemoryRegion OwnedMemoryRegion::ContiguousPhysical(uint64_t size,
return OwnedMemoryRegion(vmmo_cap, vaddr, size); return OwnedMemoryRegion(vmmo_cap, vaddr, size);
} }
OwnedMemoryRegion OwnedMemoryRegion::DirectPhysical(uint64_t paddr,
uint64_t size) {
uint64_t vmmo_cap;
check(ZMemoryObjectCreatePhysical(paddr, size, &vmmo_cap));
uint64_t vaddr;
check(ZAddressSpaceMap(gSelfVmasCap, 0, vmmo_cap, &vaddr));
return OwnedMemoryRegion(vmmo_cap, vaddr, size);
}
z_cap_t OwnedMemoryRegion::DuplicateCap() { z_cap_t OwnedMemoryRegion::DuplicateCap() {
z_cap_t cap; z_cap_t cap;
check(ZCapDuplicate(vmmo_cap_, kZionPerm_All, &cap)); check(ZCapDuplicate(vmmo_cap_, kZionPerm_All, &cap));

View File

@ -192,7 +192,7 @@ glcr::ErrorCode AhciDriver::RegisterIrq() {
glcr::ErrorCode AhciDriver::LoadHbaRegisters() { glcr::ErrorCode AhciDriver::LoadHbaRegisters() {
ahci_region_ = ahci_region_ =
MappedMemoryRegion::DirectPhysical(pci_device_header_->abar, 0x1100); OwnedMemoryRegion ::DirectPhysical(pci_device_header_->abar, 0x1100);
ahci_hba_ = reinterpret_cast<AhciHba*>(ahci_region_.vaddr()); ahci_hba_ = reinterpret_cast<AhciHba*>(ahci_region_.vaddr());
num_ports_ = (ahci_hba_->capabilities & 0x1F) + 1; num_ports_ = (ahci_hba_->capabilities & 0x1F) + 1;
num_commands_ = ((ahci_hba_->capabilities & 0x1F00) >> 8) + 1; num_commands_ = ((ahci_hba_->capabilities & 0x1F00) >> 8) + 1;

View File

@ -24,7 +24,7 @@ class AhciDriver {
private: private:
OwnedMemoryRegion pci_region_; OwnedMemoryRegion pci_region_;
PciDeviceHeader* pci_device_header_ = nullptr; PciDeviceHeader* pci_device_header_ = nullptr;
MappedMemoryRegion ahci_region_; OwnedMemoryRegion ahci_region_;
AhciHba* ahci_hba_ = nullptr; AhciHba* ahci_hba_ = nullptr;
// TODO: Allocate these dynamically. // TODO: Allocate these dynamically.

View File

@ -1,12 +1,10 @@
#include "framebuffer/framebuffer.h" #include "framebuffer/framebuffer.h"
#include <mammoth/memory_region.h>
Framebuffer::Framebuffer(const FramebufferInfo& info) : fb_info_(info) { Framebuffer::Framebuffer(const FramebufferInfo& info) : fb_info_(info) {
uint64_t buff_size_bytes = fb_info_.height() * fb_info_.pitch(); uint64_t buff_size_bytes = fb_info_.height() * fb_info_.pitch();
MappedMemoryRegion region = MappedMemoryRegion::DirectPhysical( fb_memory_ = OwnedMemoryRegion::DirectPhysical(fb_info_.address_phys(),
fb_info_.address_phys(), buff_size_bytes); buff_size_bytes);
fb_ = reinterpret_cast<uint32_t*>(region.vaddr()); fb_ = reinterpret_cast<uint32_t*>(fb_memory_.vaddr());
} }
void Framebuffer::DrawPixel(uint32_t row, uint32_t col, uint32_t pixel) { void Framebuffer::DrawPixel(uint32_t row, uint32_t col, uint32_t pixel) {

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <mammoth/memory_region.h>
#include <yellowstone/yellowstone.yunq.h> #include <yellowstone/yellowstone.yunq.h>
class Framebuffer { class Framebuffer {
@ -12,5 +13,7 @@ class Framebuffer {
// FIXME: Implement Yunq copy or move so we // FIXME: Implement Yunq copy or move so we
// don't have to store a reference here. // don't have to store a reference here.
const FramebufferInfo& fb_info_; const FramebufferInfo& fb_info_;
OwnedMemoryRegion fb_memory_;
uint32_t* fb_; uint32_t* fb_;
}; };