1 // Copyright 2018 The Chromium OS Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 //! Manages system resources that can be allocated to VMs and their devices. 6 7 #[cfg(feature = "wl-dmabuf")] 8 extern crate gpu_buffer; 9 extern crate libc; 10 extern crate msg_socket; 11 extern crate sys_util; 12 13 use std::fmt::Display; 14 15 pub use crate::address_allocator::AddressAllocator; 16 pub use crate::gpu_allocator::{ 17 GpuAllocatorError, GpuMemoryAllocator, GpuMemoryDesc, GpuMemoryPlaneDesc, 18 }; 19 pub use crate::system_allocator::SystemAllocator; 20 21 mod address_allocator; 22 mod gpu_allocator; 23 mod system_allocator; 24 25 /// Used to tag SystemAllocator allocations. 26 #[derive(Debug, Eq, PartialEq, Hash)] 27 pub enum Alloc { 28 /// An anonymous resource allocation. 29 /// Should only be instantiated through `SystemAllocator::get_anon_alloc()`. 30 /// Avoid using these. Instead, use / create a more descriptive Alloc variant. 31 Anon(usize), 32 /// A PCI BAR region with associated bus, device, and bar numbers. 33 PciBar { bus: u8, dev: u8, bar: u8 }, 34 /// GPU render node region. 35 GpuRenderNode, 36 /// Pmem device region with associated device index. 37 PmemDevice(usize), 38 } 39 40 #[derive(Debug, Eq, PartialEq)] 41 pub enum Error { 42 AllocSizeZero, 43 BadAlignment, 44 CreateGpuAllocator(GpuAllocatorError), 45 ExistingAlloc(Alloc), 46 MissingDeviceAddresses, 47 MissingMMIOAddresses, 48 NoIoAllocator, 49 OutOfSpace, 50 PoolOverflow { base: u64, size: u64 }, 51 PoolSizeZero, 52 } 53 54 pub type Result<T> = std::result::Result<T, Error>; 55 56 impl Display for Error { fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result57 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { 58 use self::Error::*; 59 match self { 60 AllocSizeZero => write!(f, "Allocation cannot have size of 0"), 61 BadAlignment => write!(f, "Pool alignment must be a power of 2"), 62 CreateGpuAllocator(e) => write!(f, "Failed to create GPU allocator: {:?}", e), 63 ExistingAlloc(tag) => write!(f, "Alloc already exists: {:?}", tag), 64 MissingDeviceAddresses => write!(f, "Device address range not specified"), 65 MissingMMIOAddresses => write!(f, "MMIO address range not specified"), 66 NoIoAllocator => write!(f, "No IO address range specified"), 67 OutOfSpace => write!(f, "Out of space"), 68 PoolOverflow { base, size } => write!(f, "base={} + size={} overflows", base, size), 69 PoolSizeZero => write!(f, "Pool cannot have size of 0"), 70 } 71 } 72 } 73