Line data Source code
1 : /**
2 : * @file CardWrapper.hpp FELIX's FlxCard library wrapper
3 : *
4 : * This is part of the DUNE DAQ , copyright 2020.
5 : * Licensing/copyright details are in the COPYING file that you should have
6 : * received with this code.
7 : */
8 : #ifndef FLXLIBS_SRC_CARDWRAPPER_HPP_
9 : #define FLXLIBS_SRC_CARDWRAPPER_HPP_
10 :
11 : // #include "flxlibs/felixcardreader/Nljs.hpp"
12 : // #include "flxlibs/felixcardreader/Structs.hpp"
13 :
14 : #include "appmodel/FelixInterface.hpp"
15 : #include "utilities/ReusableThread.hpp"
16 :
17 : #include "flxcard/FlxCard.h"
18 : #include "packetformat/block_format.hpp"
19 :
20 : #include <nlohmann/json.hpp>
21 :
22 : #include <atomic>
23 : #include <memory>
24 : #include <mutex>
25 : #include <string>
26 :
27 : namespace dunedaq::flxlibs {
28 :
29 : class CardWrapper
30 : {
31 : public:
32 : /**
33 : * @brief CardWrapper Constructor
34 : * @param name Instance name for this CardWrapper instance
35 : */
36 : CardWrapper(const appmodel::FelixInterface* cfg, std::vector<unsigned int>);
37 : ~CardWrapper();
38 : CardWrapper(const CardWrapper&) = delete; ///< CardWrapper is not copy-constructible
39 : CardWrapper& operator=(const CardWrapper&) = delete; ///< CardWrapper is not copy-assignable
40 : CardWrapper(CardWrapper&&) = delete; ///< CardWrapper is not move-constructible
41 : CardWrapper& operator=(CardWrapper&&) = delete; ///< CardWrapper is not move-assignable
42 :
43 : void configure();
44 : void start();
45 : void stop();
46 : void set_running(bool should_run);
47 :
48 : void graceful_stop();
49 :
50 0 : void set_block_addr_handler(std::function<void(uint64_t)>& handle) // NOLINT(build/unsigned)
51 : { // NOLINT
52 0 : m_handle_block_addr = std::bind(handle, std::placeholders::_1);
53 0 : m_block_addr_handler_available = true;
54 0 : }
55 :
56 : private:
57 : // Constants
58 : static constexpr size_t m_max_links_per_card = 6;
59 : // static constexpr size_t m_margin_blocks = 4;
60 : // static constexpr size_t m_block_threshold = 256;
61 : static constexpr size_t m_block_size = 4096; // felix::packetformat::BLOCKSIZE;
62 : static constexpr size_t m_dma_wraparound = FLX_DMA_WRAPAROUND;
63 :
64 : // Card
65 : void open_card();
66 : void close_card();
67 :
68 : // DMA
69 : int allocate_CMEM(uint8_t numa, u_long bsize, u_long* paddr, u_long* vaddr); // NOLINT
70 : void init_DMA();
71 : void start_DMA();
72 : void stop_DMA();
73 : uint64_t bytes_available(); // NOLINT
74 : void read_current_address();
75 :
76 : // Configuration and internals
77 :
78 : std::atomic<bool> m_run_marker;
79 : bool m_configured{ false };
80 : uint8_t m_card_id; // NOLINT
81 : uint8_t m_logical_unit; // NOLINT
82 : std::string m_card_id_str;
83 : uint8_t m_dma_id; // NOLINT
84 : size_t m_margin_blocks; // NOLINT
85 : size_t m_block_threshold; // NOLINT
86 : bool m_interrupt_mode; // NOLINT
87 : size_t m_poll_time; // NOLINT
88 : uint8_t m_numa_id; // NOLINT
89 : std::vector<unsigned int> m_links_enabled; // NOLINT
90 : std::string m_info_str;
91 :
92 : // Card object
93 : using UniqueFlxCard = std::unique_ptr<FlxCard>;
94 : UniqueFlxCard m_flx_card;
95 : std::mutex m_card_mutex;
96 :
97 : // DMA: CMEM
98 : std::size_t m_dma_memory_size; // size of CMEM (driver) memory to allocate
99 : int m_cmem_handle; // handle to the DMA memory block
100 : uint64_t m_virt_addr; // NOLINT virtual address of the DMA memory block
101 : uint64_t m_phys_addr; // NOLINT physical address of the DMA memory block
102 : uint64_t m_current_addr; // NOLINT pointer to the current write position for the card
103 : unsigned m_read_index; // NOLINT
104 : u_long m_destination; // u_long -> FlxCard.h
105 :
106 : // Processor
107 : inline static const std::string m_dma_processor_name = "flx-dma";
108 : std::atomic<bool> m_run_lock;
109 : utilities::ReusableThread m_dma_processor;
110 : std::function<void(uint64_t)> m_handle_block_addr; // NOLINT
111 : bool m_block_addr_handler_available{ false };
112 : void process_DMA();
113 : };
114 :
115 : } // namespace dunedaq::flxlibs
116 :
117 : #endif // FLXLIBS_SRC_CARDWRAPPER_HPP_
|