1 // Copyright 2018 The Chromium OS Authors. All rights reserved. 2 // Copyright © 2019 Intel Corporation 3 // 4 // Portions Copyright 2017 The Chromium OS Authors. All rights reserved. 5 // Use of this source code is governed by a BSD-style license that can be 6 // found in the LICENSE-BSD-3-Clause file. 7 // 8 // SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause 9 10 use vm_memory::{GuestAddress, GuestUsize}; 11 12 use crate::address::AddressAllocator; 13 use crate::gsi::GsiAllocator; 14 #[cfg(target_arch = "x86_64")] 15 use crate::gsi::GsiApic; 16 use crate::page_size::get_page_size; 17 18 /// Manages allocating system resources such as address space and interrupt numbers. 19 /// 20 /// # Example - Use the `SystemAddress` builder. 21 /// 22 /// ``` 23 /// # #[cfg(target_arch = "x86_64")] 24 /// # use vm_allocator::{GsiApic, SystemAllocator}; 25 /// # #[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))] 26 /// # use vm_allocator::SystemAllocator; 27 /// # use vm_memory::{Address, GuestAddress, GuestUsize}; 28 /// let mut allocator = SystemAllocator::new( 29 /// GuestAddress(0x1000), 30 /// 0x10000, 31 /// GuestAddress(0x10000000), 0x10000000, 32 /// #[cfg(target_arch = "x86_64")] vec![GsiApic::new(5, 19)]).unwrap(); 33 /// #[cfg(target_arch = "x86_64")] 34 /// assert_eq!(allocator.allocate_irq(), Some(5)); 35 /// #[cfg(target_arch = "aarch64")] 36 /// assert_eq!(allocator.allocate_irq(), Some(32)); 37 /// #[cfg(target_arch = "riscv64")] 38 /// assert_eq!(allocator.allocate_irq(), Some(0)); 39 /// #[cfg(target_arch = "x86_64")] 40 /// assert_eq!(allocator.allocate_irq(), Some(6)); 41 /// #[cfg(target_arch = "aarch64")] 42 /// assert_eq!(allocator.allocate_irq(), Some(33)); 43 /// #[cfg(target_arch = "riscv64")] 44 /// assert_eq!(allocator.allocate_irq(), Some(1)); 45 /// assert_eq!(allocator.allocate_platform_mmio_addresses(None, 0x1000, Some(0x1000)), Some(GuestAddress(0x1fff_f000))); 46 /// 47 /// ``` 48 pub struct SystemAllocator { 49 io_address_space: AddressAllocator, 50 platform_mmio_address_space: AddressAllocator, 51 gsi_allocator: GsiAllocator, 52 } 53 54 impl SystemAllocator { 55 /// Creates a new `SystemAllocator` for managing addresses and irq numbers. 56 /// Can return `None` if `base` + `size` overflows a u64 57 /// 58 /// * `io_base` - (X86) The starting address of IO memory. 59 /// * `io_size` - (X86) The size of IO memory. 60 /// * `platform_mmio_base` - The starting address of platform MMIO memory. 61 /// * `platform_mmio_size` - The size of platform MMIO memory. 62 /// * `apics` - (X86) Vector of APIC's. 63 /// 64 pub fn new( 65 io_base: GuestAddress, 66 io_size: GuestUsize, 67 platform_mmio_base: GuestAddress, 68 platform_mmio_size: GuestUsize, 69 #[cfg(target_arch = "x86_64")] apics: Vec<GsiApic>, 70 ) -> Option<Self> { 71 Some(SystemAllocator { 72 io_address_space: AddressAllocator::new(io_base, io_size)?, 73 platform_mmio_address_space: AddressAllocator::new( 74 platform_mmio_base, 75 platform_mmio_size, 76 )?, 77 #[cfg(target_arch = "x86_64")] 78 gsi_allocator: GsiAllocator::new(apics), 79 #[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))] 80 gsi_allocator: GsiAllocator::new(), 81 }) 82 } 83 84 /// Reserves the next available system irq number. 85 pub fn allocate_irq(&mut self) -> Option<u32> { 86 self.gsi_allocator.allocate_irq().ok() 87 } 88 89 /// Reserves the next available GSI. 90 pub fn allocate_gsi(&mut self) -> Option<u32> { 91 self.gsi_allocator.allocate_gsi().ok() 92 } 93 94 /// Reserves a section of `size` bytes of IO address space. 95 pub fn allocate_io_addresses( 96 &mut self, 97 address: Option<GuestAddress>, 98 size: GuestUsize, 99 align_size: Option<GuestUsize>, 100 ) -> Option<GuestAddress> { 101 self.io_address_space 102 .allocate(address, size, Some(align_size.unwrap_or(0x1))) 103 } 104 105 /// Reserves a section of `size` bytes of platform MMIO address space. 106 pub fn allocate_platform_mmio_addresses( 107 &mut self, 108 address: Option<GuestAddress>, 109 size: GuestUsize, 110 align_size: Option<GuestUsize>, 111 ) -> Option<GuestAddress> { 112 self.platform_mmio_address_space.allocate( 113 address, 114 size, 115 Some(align_size.unwrap_or_else(get_page_size)), 116 ) 117 } 118 119 /// Free an IO address range. 120 /// We can only free a range if it matches exactly an already allocated range. 121 pub fn free_io_addresses(&mut self, address: GuestAddress, size: GuestUsize) { 122 self.io_address_space.free(address, size) 123 } 124 125 /// Free a platform MMIO address range. 126 /// We can only free a range if it matches exactly an already allocated range. 127 pub fn free_platform_mmio_addresses(&mut self, address: GuestAddress, size: GuestUsize) { 128 self.platform_mmio_address_space.free(address, size) 129 } 130 } 131