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(target_arch = "aarch64")] 26 /// # use vm_allocator::SystemAllocator; 27 /// # use vm_memory::{Address, GuestAddress, GuestUsize}; 28 /// let mut allocator = SystemAllocator::new( 29 /// #[cfg(target_arch = "x86_64")] GuestAddress(0x1000), 30 /// #[cfg(target_arch = "x86_64")] 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 = "x86_64")] 38 /// assert_eq!(allocator.allocate_irq(), Some(6)); 39 /// #[cfg(target_arch = "aarch64")] 40 /// assert_eq!(allocator.allocate_irq(), Some(33)); 41 /// assert_eq!(allocator.allocate_platform_mmio_addresses(None, 0x1000, Some(0x1000)), Some(GuestAddress(0x1fff_f000))); 42 /// 43 /// ``` 44 pub struct SystemAllocator { 45 #[cfg(target_arch = "x86_64")] 46 io_address_space: AddressAllocator, 47 platform_mmio_address_space: AddressAllocator, 48 gsi_allocator: GsiAllocator, 49 } 50 51 impl SystemAllocator { 52 /// Creates a new `SystemAllocator` for managing addresses and irq numbers. 53 /// Can return `None` if `base` + `size` overflows a u64 54 /// 55 /// * `io_base` - (X86) The starting address of IO memory. 56 /// * `io_size` - (X86) The size of IO memory. 57 /// * `platform_mmio_base` - The starting address of platform MMIO memory. 58 /// * `platform_mmio_size` - The size of platform MMIO memory. 59 /// * `apics` - (X86) Vector of APIC's. 60 /// 61 pub fn new( 62 #[cfg(target_arch = "x86_64")] io_base: GuestAddress, 63 #[cfg(target_arch = "x86_64")] io_size: GuestUsize, 64 platform_mmio_base: GuestAddress, 65 platform_mmio_size: GuestUsize, 66 #[cfg(target_arch = "x86_64")] apics: Vec<GsiApic>, 67 ) -> Option<Self> { 68 Some(SystemAllocator { 69 #[cfg(target_arch = "x86_64")] 70 io_address_space: AddressAllocator::new(io_base, io_size)?, 71 platform_mmio_address_space: AddressAllocator::new( 72 platform_mmio_base, 73 platform_mmio_size, 74 )?, 75 #[cfg(target_arch = "x86_64")] 76 gsi_allocator: GsiAllocator::new(apics), 77 #[cfg(target_arch = "aarch64")] 78 gsi_allocator: GsiAllocator::new(), 79 }) 80 } 81 82 /// Reserves the next available system irq number. 83 pub fn allocate_irq(&mut self) -> Option<u32> { 84 self.gsi_allocator.allocate_irq().ok() 85 } 86 87 /// Reserves the next available GSI. 88 pub fn allocate_gsi(&mut self) -> Option<u32> { 89 self.gsi_allocator.allocate_gsi().ok() 90 } 91 92 #[cfg(target_arch = "x86_64")] 93 /// Reserves a section of `size` bytes of IO address space. 94 pub fn allocate_io_addresses( 95 &mut self, 96 address: Option<GuestAddress>, 97 size: GuestUsize, 98 align_size: Option<GuestUsize>, 99 ) -> Option<GuestAddress> { 100 self.io_address_space 101 .allocate(address, size, Some(align_size.unwrap_or(0x1))) 102 } 103 104 /// Reserves a section of `size` bytes of platform MMIO address space. 105 pub fn allocate_platform_mmio_addresses( 106 &mut self, 107 address: Option<GuestAddress>, 108 size: GuestUsize, 109 align_size: Option<GuestUsize>, 110 ) -> Option<GuestAddress> { 111 self.platform_mmio_address_space.allocate( 112 address, 113 size, 114 Some(align_size.unwrap_or_else(get_page_size)), 115 ) 116 } 117 118 #[cfg(target_arch = "x86_64")] 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