// Copyright 2018 The Chromium OS Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. //! Manages system resources that can be allocated to VMs and their devices. use msg_socket::MsgOnSocket; use std::fmt::Display; pub use crate::address_allocator::AddressAllocator; pub use crate::gpu_allocator::{ GpuAllocatorError, GpuMemoryAllocator, GpuMemoryDesc, GpuMemoryPlaneDesc, }; pub use crate::system_allocator::{MmioType, SystemAllocator}; mod address_allocator; mod gpu_allocator; mod system_allocator; /// Used to tag SystemAllocator allocations. #[derive(Debug, Eq, PartialEq, Hash, MsgOnSocket, Copy, Clone)] pub enum Alloc { /// An anonymous resource allocation. /// Should only be instantiated through `SystemAllocator::get_anon_alloc()`. /// Avoid using these. Instead, use / create a more descriptive Alloc variant. Anon(usize), /// A PCI BAR region with associated bus, device, function and bar numbers. PciBar { bus: u8, dev: u8, func: u8, bar: u8 }, /// GPU render node region. GpuRenderNode, /// Pmem device region with associated device index. PmemDevice(usize), /// pstore region. Pstore, } #[derive(Debug, Eq, PartialEq)] pub enum Error { AllocSizeZero, BadAlignment, CreateGpuAllocator(GpuAllocatorError), ExistingAlloc(Alloc), MissingHighMMIOAddresses, MissingLowMMIOAddresses, NoIoAllocator, OutOfSpace, PoolOverflow { base: u64, size: u64 }, PoolSizeZero, RegionOverlap { base: u64, size: u64 }, BadAlloc(Alloc), } pub type Result = std::result::Result; impl Display for Error { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { use self::Error::*; match self { AllocSizeZero => write!(f, "Allocation cannot have size of 0"), BadAlignment => write!(f, "Pool alignment must be a power of 2"), CreateGpuAllocator(e) => write!(f, "Failed to create GPU allocator: {:?}", e), ExistingAlloc(tag) => write!(f, "Alloc already exists: {:?}", tag), MissingHighMMIOAddresses => write!(f, "High MMIO address range not specified"), MissingLowMMIOAddresses => write!(f, "Low MMIO address range not specified"), NoIoAllocator => write!(f, "No IO address range specified"), OutOfSpace => write!(f, "Out of space"), PoolOverflow { base, size } => write!(f, "base={} + size={} overflows", base, size), PoolSizeZero => write!(f, "Pool cannot have size of 0"), RegionOverlap { base, size } => { write!(f, "Overlapping region base={} size={}", base, size) } BadAlloc(tag) => write!(f, "Alloc does not exists: {:?}", tag), } } }