aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/include/uhdlib/rfnoc/epid_allocator.hpp
blob: 482cb1daccc042af0f40576caba156b2902c6551 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
// Copyright 2019 Ettus Research, a National Instruments Brand
//
// SPDX-License-Identifier: GPL-3.0-or-later
//

#ifndef INCLUDED_LIBUHD_EPID_ALLOCATOR_HPP
#define INCLUDED_LIBUHD_EPID_ALLOCATOR_HPP

#include <uhdlib/rfnoc/mgmt_portal.hpp>
#include <uhdlib/rfnoc/rfnoc_common.hpp>
#include <map>
#include <memory>
#include <mutex>

namespace uhd { namespace rfnoc {

/*! A class that is responsible for allocating and keeping track of endpoint
 * IDs. There shall be one instance of this class per rfnoc_graph
 */
class epid_allocator
{
public:
    using sptr = std::shared_ptr<epid_allocator>;

    epid_allocator(sep_id_t start_epid = 1);
    epid_allocator(const epid_allocator& rhs) = delete;
    epid_allocator(epid_allocator&& rhs)      = delete;

    /*! \brief Allocate an EPID for the specified endpoint.
     * Does not initialize the specified endpoint (ideal for SW endpoints).
     *
     * \param addr The physical address (device, instance) of the stream endpoint
     * \return The allocated EPID
     */
    sep_id_t allocate_epid(const sep_addr_t& addr);

    /*! \brief Allocate an EPID for the specified endpoint.
     * Also initialize the specified endpoint.
     *
     * \param addr The physical address (device, instance) of the stream endpoint
     * \param mgmt_portal The management portal to use for initializing the SEP/EPID
     * \param chdr_ctrl_xport The ctrl xport to use for initializing the SEP/EPID
     * \return The allocated EPID
     */
    sep_id_t allocate_epid(
        const sep_addr_t& addr, mgmt::mgmt_portal& mgmt_portal, chdr_ctrl_xport& xport);

    /*! \brief Get a pre-allocated EPID. Throws an exception is not allocated
     *
     * \param addr The physical address (device, instance) of the stream endpoint
     * \return The allocated EPID
     */
    sep_id_t get_epid(const sep_addr_t& addr);

    /*! \brief Lookup an EPID and return the address associated with it.
     *
     * \param epid The allocated EPID
     * \return The physical address (device, instance) of the stream endpoint
     */
    sep_addr_t lookup_addr(const sep_id_t& epid) const;

    /*! \brief Deallocate the specified EPID.
     *
     * \param epid The EPID to deallocate
     */
    void deallocate_epid(sep_id_t epid);

private:
    std::map<sep_addr_t, sep_id_t> _epid_map;
    std::map<sep_id_t, sep_addr_t> _addr_map;
    sep_id_t _next_epid;
    mutable std::mutex _mutex;
};

}} /* namespace uhd::rfnoc */

#endif /* INCLUDED_LIBUHD_EPID_ALLOCATOR_HPP */