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#pragma once
8
9#include <err.h>
10#include <stdint.h>
11
12#include <fbl/macros.h>
13
14namespace intel_iommu {
15
16// Manages the domain ID space for a given IOMMU.  This is not thread-safe.
17class DomainAllocator {
18public:
19    DomainAllocator();
20
21    // Get an unused domain ID.
22    // Returns ZX_ERR_NO_RESOURCES if one cannot be found.
23    zx_status_t Allocate(uint32_t* domain_id);
24
25    // Set the number of domain IDs this instance manages.  Panics if this call
26    // would reduce the max domain ID to below the current highest allocated one.
27    void set_num_domains(uint32_t num);
28
29private:
30    DISALLOW_COPY_ASSIGN_AND_MOVE(DomainAllocator);
31
32    uint32_t num_domains_;
33    uint32_t next_domain_id_;
34};
35
36} // namespace intel_iommu
37