1 // Copyright 2018 The Fuchsia Authors
2 //
3 // Use of this source code is governed by a MIT-style
4 // license that can be found in the LICENSE file or at
5 // https://opensource.org/licenses/MIT
6 
7 #include "domain_allocator.h"
8 
9 #include <assert.h>
10 
11 namespace intel_iommu {
12 
DomainAllocator()13 DomainAllocator::DomainAllocator()
14         // Note that next_domain_id_ starts at 1, since under some conditions 0 is
15         // an invalid domain ID (i.e. if CM is set in the capability register).
16         : num_domains_(0), next_domain_id_(1) { }
17 
Allocate(uint32_t * domain_id)18 zx_status_t DomainAllocator::Allocate(uint32_t* domain_id) {
19     if (next_domain_id_ >= num_domains_) {
20         return ZX_ERR_NO_RESOURCES;
21     }
22 
23     // This allocator should be enough, since the hardware should have enough
24     // domain IDs for each device hanging off of it. If we start deallocating
25     // Context Entries, we'll need to make this allocator more sophisticated to
26     // manage the ID reuse.
27     *domain_id = next_domain_id_++;
28     return ZX_OK;
29 }
30 
set_num_domains(uint32_t num)31 void DomainAllocator::set_num_domains(uint32_t num) {
32     ASSERT(num >= next_domain_id_);
33     num_domains_ = num;
34 }
35 
36 } // namespace intel_iommu
37