aboutsummaryrefslogtreecommitdiff
path: root/src/models/lt_bus.h
blob: 86b181ee93236c0a85dc1d8ba491c7bb6945aa8c (plain) (blame)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#ifndef SYSC_PLAYGROUND_LT_BUS
#define SYSC_PLAYGROUND_LT_BUS

#include "utils/range.h"
#include "utils/sysc.h"
#include "utils/tlm_initiator_socket_tagged.h"
#include "utils/tlm_target_socket_tagged.h"
#include "utils/types.h"

#include <sysc/kernel/sc_event.h>

#include <algorithm>
#include <memory>
#include <vector>

// TLM bus lock extension.
//
// This extension is used to implemented the bus locking scheme.
// The protocol is as follows. Once an initiator sends a locked transaction,
// the bus will be locked by that initiator after all currently pending
// transactions are finished. The bus is locked until the locking initiator
// sends an unlocked transaction.
struct [[nodiscard]] bus_lock : tlm::tlm_extension<bus_lock> {
  bool is_lock{false};

  constexpr explicit bus_lock() = default;
  constexpr explicit bus_lock(const bus_lock&) = default;

  virtual tlm_extension_base* clone() const override {
    return new bus_lock(*this);
  }

  virtual void copy_from(const tlm_extension_base& ext) override {
    if (&ext == this) {
      // Copy from self, nop.
      return;
    }

    assert(typeid(this) == typeid(ext));
    const bus_lock& other = static_cast<const bus_lock&>(ext);

    is_lock = other.is_lock;
  }
};

class lt_bus : public sc_core::sc_module {
  using target_socket = tlm_target_socket_tagged<lt_bus>;
  using target_socket_ptr = std::unique_ptr<target_socket>;

  using initiator_socket = tlm_initiator_socket_tagged<lt_bus>;
  using initiator_socket_ptr = std::unique_ptr<initiator_socket>;

 public:
  explicit lt_bus(sc_core::sc_module_name nm)
      : sc_core::sc_module(std::move(nm)) {}

  // -- ATTACH BUS INITIATOR ---------------------------------------------------

  void attach_initiator(tlm::tlm_base_initiator_socket_b<>& init) {
    const usize id = m_initiators.size();
    const std::string name = "init" + std::to_string(id);
    {
      // Add current module on top of module stack for tlm sockets.
      scoped_push_hierarchy h(*this);

      // Add new target socket to connect BUS INITIATOR.
      m_initiators.push_back(std::make_unique<target_socket>(
          name.c_str(), id, this, &lt_bus::b_transport,
          &lt_bus::get_direct_mem_ptr, &lt_bus::transport_dbg));
    }

    // Bind sockets.
    auto& target = m_initiators.back();
    target->bind(init);
  }

  // -- ATTACH BUS TARGET ------------------------------------------------------

  void attach_target(tlm::tlm_base_target_socket_b<>& target,
                     u64 start,
                     u64 end) {
    assert(start <= end);
    const range addr{start, end};

    // Check if new range overlaps with any registered memory map range.
    for (const auto& mmap : m_mappings) {
      if (mmap.addr.overlaps(addr)) {
        std::fprintf(stderr,
                     "lt_bus: memory map conflict detected\n"
                     "old: %08lx - %08lx\n"
                     "new: %08lx - %08lx\n",
                     mmap.addr.start, mmap.addr.end, start, end);
        std::abort();
      }
    }

    const usize id = m_targets.size();
    const std::string name = "target" + std::to_string(id);
    {
      // Add current module on top of module stack for tlm sockets.
      scoped_push_hierarchy h(*this);

      // Add new initiator socket to connect BUS TARGET.
      m_targets.push_back(std::make_unique<initiator_socket>(
          name.c_str(), id, this, &lt_bus::invalidate_direct_mem_ptr));
    }

    // Bind sockets.
    auto& init = m_targets.back();
    init->bind(target);

    // Insert new mapping, id is equal to idx into socket vector.
    m_mappings.push_back({addr, id});
  }

  // -- SHOW_MMAP --------------------------------------------------------------

  void show_mmap() const {
    // Dump memory map.
    for (const auto& mmap : m_mappings) {
      std::printf("%08lx - %08lx :[%2ld] %s\n", mmap.addr.start, mmap.addr.end,
                  mmap.idx, m_targets[mmap.idx].get()->name());
    }
  }

 private:
  // -- TLM_BW_TRANSPORT_IF ----------------------------------------------------

  void invalidate_direct_mem_ptr(usize id,
                                 sc_dt::uint64 start,
                                 sc_dt::uint64 end) {
    assert(start <= end);

    for (const auto& mmap : m_mappings) {
      if (mmap.idx != id) {
        continue;
      }

      // Compute absolute start/end address based on memory map.
      const range abs_addr =
          compute_abs_mmap_address(range{start, end}, mmap.addr);

      for (auto& sock : m_initiators) {
        (*sock)->invalidate_direct_mem_ptr(abs_addr.start, abs_addr.end);
      }
    }
  }

  // -- TLM_FW_TRANSPORT_IF ----------------------------------------------------

  void b_transport(usize idx,
                   tlm::tlm_generic_payload& tx,
                   sc_core::sc_time& t) {
    if (const auto r = decode(tx)) {
      const auto do_tx = [&]() {
        const auto tx_start = tx.get_address();
        assert(r.start <= tx_start);

        tx.set_address(tx_start - r.start);
        (*r.sock)->b_transport(tx, t);
        tx.set_address(tx_start);
      };

      while (m_bl.is_locked && (m_bl.idx != idx)) {
        wait(m_bl.ev_no_pending_tx);
      }

      const auto* ext = tx.get_extension<bus_lock>();
      if (ext && ext->is_lock) {
        assert(!m_bl.is_locked);

        m_bl.is_locked = true;
        m_bl.idx = idx;

        if (m_bl.pending_tx) {
          wait(m_bl.ev_no_pending_tx);
        }
        assert(m_bl.pending_tx == 0);

        // Invalidate DMI pointers for the whole address space. This forces all
        // initiators on the slow path and through the bus, which can than
        // enforce the exclusive access.
        for (auto& sock : m_initiators) {
          (*sock)->invalidate_direct_mem_ptr(0, -1ull);
        }

        do_tx();
        assert(m_bl.pending_tx == 0);
      } else {
        assert(!m_bl.is_locked || (m_bl.is_locked && m_bl.idx == idx));

        m_bl.pending_tx++;
        do_tx();
        m_bl.pending_tx--;

        if (m_bl.is_locked && m_bl.idx == idx) {
          assert(m_bl.pending_tx == 0);
          m_bl.is_locked = false;
        }

        if (m_bl.pending_tx == 0) {
          m_bl.ev_no_pending_tx.notify(sc_core::SC_ZERO_TIME);
        }
      }
    } else {
      tx.set_response_status(tlm::TLM_ADDRESS_ERROR_RESPONSE);
    }
  }

  bool get_direct_mem_ptr(usize,
                          tlm::tlm_generic_payload& tx,
                          tlm::tlm_dmi& dmi) {
    if (m_bl.is_locked) {
      return false;
    }

    if (const auto r = decode(tx)) {
      const auto tx_start = tx.get_address();
      assert(r.start <= tx_start);

      tx.set_address(tx_start - r.start);
      const bool ok = (*r.sock)->get_direct_mem_ptr(tx, dmi);
      tx.set_address(tx_start);

      // Early return, dmi request failed, no need to fixup dmi addresses.
      if (!ok) {
        return false;
      }

      // Compute absolute start/end address based on memory map.
      const range abs_addr = compute_abs_mmap_address(
          range{dmi.get_start_address(), dmi.get_end_address()},
          range{r.start, r.end});

      // Update dmi payload with absolute addresses.
      dmi.set_start_address(abs_addr.start);
      dmi.set_end_address(abs_addr.end);

      return true;
    }
    return false;
  }

  unsigned int transport_dbg(usize, tlm::tlm_generic_payload& tx) {
    unsigned int ret = 0;
    if (const auto r = decode(tx)) {
      const auto tx_start = tx.get_address();
      assert(r.start <= tx_start);

      tx.set_address(tx_start - r.start);
      ret = (*r.sock)->transport_dbg(tx);
      tx.set_address(tx_start);
    }
    return ret;
  }

  // -- DECODE BUS TARGET ------------------------------------------------------

  struct decode_result {
    initiator_socket* sock{nullptr};
    u64 start{0ull};
    u64 end{0ull};

    constexpr explicit operator bool() const {
      return sock != nullptr;
    }
  };

  decode_result decode(range addr) const {
    for (const auto& mmap : m_mappings) {
      if (mmap.addr.contains(addr)) {
        return {m_targets[mmap.idx].get(), mmap.addr.start, mmap.addr.end};
      }
    }
    return {};
  }

  decode_result decode(const tlm::tlm_generic_payload& tx) const {
    const u64 start = tx.get_address();
    const u64 end = start + tx.get_data_length() - 1;
    return decode(range{start, end});
  }

  // -- COMPUTE ABSOLUTE MEMORY MAP ADDRESS ------------------------------------

  static range compute_abs_mmap_address(range rel, range mmap) {
    // Compute absolute start address.
    const sc_dt::uint64 abs_start = mmap.start + rel.start;
    // Start address must be in bounds of the memory map mapping.
    assert(abs_start <= mmap.end);

    // Compute absolute end address. Limit by memory map if range exceeded.
    const auto comp_abs_end = [&mmap](sc_dt::uint64 abs_end) -> sc_dt::uint64 {
      if (abs_end > mmap.end /* exceeds mapping */ ||
          abs_end < mmap.start /* wraps around */) {
        return mmap.end;
      }
      return abs_end;
    };
    const sc_dt::uint64 abs_end = comp_abs_end(mmap.start + rel.end);

    return range{abs_start, abs_end};
  }

  // -- SC_MODULE CALLBACKS ----------------------------------------------------

  virtual void start_of_simulation() override {
    // Sort memory mappings by start address.
    std::sort(m_mappings.begin(), m_mappings.end(),
              [](const mapping& lhs, const mapping& rhs) {
                return lhs.addr.start < rhs.addr.start;
              });
  }

  // -- LOCAL CLASSES ----------------------------------------------------------

  struct mapping {
    range addr;
    usize idx;
  };

  // -- MEMBER -----------------------------------------------------------------

  // TARGET sockets to bind BUS INITIATORS against.
  std::vector<target_socket_ptr> m_initiators;
  // INITIATOR sockets to bind BUS TARGET against.
  std::vector<initiator_socket_ptr> m_targets;
  // Address range mappings to BUS TARGETs (m_tragets).
  std::vector<mapping> m_mappings;

  struct bus_lock_state {
    bool is_locked{false};
    usize idx{0};
    usize pending_tx{0};
    sc_core::sc_event ev_no_pending_tx;
  } m_bl;
};

#endif