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 17 use crate::page_size::get_page_size; 18 19 /// Manages allocating system resources such as address space and interrupt numbers. 20 /// 21 /// # Example - Use the `SystemAddress` builder. 22 /// 23 /// ``` 24 /// # #[cfg(target_arch = "x86_64")] 25 /// # use vm_allocator::{GsiApic, SystemAllocator}; 26 /// # #[cfg(target_arch = "aarch64")] 27 /// # use vm_allocator::SystemAllocator; 28 /// # use vm_memory::{Address, GuestAddress, GuestUsize}; 29 /// let mut allocator = SystemAllocator::new( 30 /// #[cfg(target_arch = "x86_64")] GuestAddress(0x1000), 31 /// #[cfg(target_arch = "x86_64")] 0x10000, 32 /// GuestAddress(0x10000000), 0x10000000, 33 /// #[cfg(target_arch = "x86_64")] vec![GsiApic::new(5, 19)]).unwrap(); 34 /// #[cfg(target_arch = "x86_64")] 35 /// assert_eq!(allocator.allocate_irq(), Some(5)); 36 /// #[cfg(target_arch = "aarch64")] 37 /// assert_eq!(allocator.allocate_irq(), Some(32)); 38 /// #[cfg(target_arch = "x86_64")] 39 /// assert_eq!(allocator.allocate_irq(), Some(6)); 40 /// #[cfg(target_arch = "aarch64")] 41 /// assert_eq!(allocator.allocate_irq(), Some(33)); 42 /// assert_eq!(allocator.allocate_platform_mmio_addresses(None, 0x1000, Some(0x1000)), Some(GuestAddress(0x1fff_f000))); 43 /// 44 /// ``` 45 pub struct SystemAllocator { 46 #[cfg(target_arch = "x86_64")] 47 io_address_space: AddressAllocator, 48 platform_mmio_address_space: AddressAllocator, 49 gsi_allocator: GsiAllocator, 50 } 51 52 impl SystemAllocator { 53 /// Creates a new `SystemAllocator` for managing addresses and irq numbers. 54 /// Can return `None` if `base` + `size` overflows a u64 55 /// 56 /// * `io_base` - (X86) The starting address of IO memory. 57 /// * `io_size` - (X86) The size of IO memory. 58 /// * `platform_mmio_base` - The starting address of platform MMIO memory. 59 /// * `platform_mmio_size` - The size of platform MMIO memory. 60 /// * `apics` - (X86) Vector of APIC's. 61 /// 62 pub fn new( 63 #[cfg(target_arch = "x86_64")] io_base: GuestAddress, 64 #[cfg(target_arch = "x86_64")] io_size: GuestUsize, 65 platform_mmio_base: GuestAddress, 66 platform_mmio_size: GuestUsize, 67 #[cfg(target_arch = "x86_64")] apics: Vec<GsiApic>, 68 ) -> Option<Self> { 69 Some(SystemAllocator { 70 #[cfg(target_arch = "x86_64")] 71 io_address_space: AddressAllocator::new(io_base, io_size)?, 72 platform_mmio_address_space: AddressAllocator::new( 73 platform_mmio_base, 74 platform_mmio_size, 75 )?, 76 #[cfg(target_arch = "x86_64")] 77 gsi_allocator: GsiAllocator::new(apics), 78 #[cfg(target_arch = "aarch64")] 79 gsi_allocator: GsiAllocator::new(), 80 }) 81 } 82 83 /// Reserves the next available system irq number. 84 pub fn allocate_irq(&mut self) -> Option<u32> { 85 self.gsi_allocator.allocate_irq().ok() 86 } 87 88 /// Reserves the next available GSI. 89 pub fn allocate_gsi(&mut self) -> Option<u32> { 90 self.gsi_allocator.allocate_gsi().ok() 91 } 92 93 #[cfg(target_arch = "x86_64")] 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 #[cfg(target_arch = "x86_64")] 120 /// Free an IO address range. 121 /// We can only free a range if it matches exactly an already allocated range. 122 pub fn free_io_addresses(&mut self, address: GuestAddress, size: GuestUsize) { 123 self.io_address_space.free(address, size) 124 } 125 126 /// Free a platform MMIO address range. 127 /// We can only free a range if it matches exactly an already allocated range. 128 pub fn free_platform_mmio_addresses(&mut self, address: GuestAddress, size: GuestUsize) { 129 self.platform_mmio_address_space.free(address, size) 130 } 131 } 132