qualcomm/qbox
Loading...
Searching...
No Matches
gs_memory.h
1/*
2 * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All Rights Reserved.
3 * Author: GreenSocs 2022
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
8#ifndef _GREENSOCS_BASE_COMPONENTS_MEMORY_H
9#define _GREENSOCS_BASE_COMPONENTS_MEMORY_H
10
11#include <fstream>
12#include <memory>
13
14#include <cci_configuration>
15#include <systemc>
16#include <tlm>
17#include <tlm_utils/multi_passthrough_target_socket.h>
18#include <scp/report.h>
19#include <scp/helpers.h>
20
21#include <loader.h>
22#include <memory_services.h>
23
24#include <tlm-extensions/shmem_extension.h>
25#include <module_factory_registery.h>
26#include <tlm_sockets_buswidth.h>
27#include <unordered_map>
28
29#ifdef _WIN32
30#include <windows.h>
31#else
32#include <unistd.h>
33#include <fcntl.h>
34#include <sys/mman.h>
35#include <sys/stat.h>
36#endif
37
38namespace gs {
39
55#define ALIGNEDBITS 12
56
57template <unsigned int BUSWIDTH = DEFAULT_TLM_BUSWIDTH>
58class gs_memory : public sc_core::sc_module
59{
60 uint64_t m_size = 0;
61 uint64_t m_address;
62 bool m_address_valid = false;
63 bool m_relative_addresses;
64
65 SCP_LOGGER(());
66
67 // Templated on the power of 2 to use to divide the blocks up
68 template <unsigned int N = 2>
69 class SubBlock
70 {
71 SCP_LOGGER();
72 uint64_t m_len; // size of the bloc
73 uint64_t m_address; // this is an absolute address and this is the
74 // beginning of the bloc/Memory
75 gs_memory<BUSWIDTH>& m_mem;
76
77 uint8_t* m_ptr = nullptr;
78 std::array<std::unique_ptr<SubBlock>, (1 << N)> m_sub_blocks;
79 bool m_use_sub_blocks = false;
80
81 bool m_mapped = false;
82 ShmemIDExtension m_shmemID;
83 bool m_aligned = false;
84
85 public:
86 SubBlock(uint64_t address, uint64_t len, gs_memory& mem): m_len(len), m_address(address), m_mem(mem)
87 {
88 SCP_TRACE(())("Init");
89 }
90
91 void doreset()
92 {
93 SCP_WARN((), m_mem.name())("Reset (block at offset {:x})", m_address);
94 for (unsigned int i = 0; i < N; i++) {
95 if (m_sub_blocks[i]) m_sub_blocks[i]->doreset();
96 }
97 if (m_mem.p_init_mem && m_ptr) {
98 memset(m_ptr, m_mem.p_init_mem_val, m_len);
99 }
100 }
101
102 SubBlock& access(uint64_t address)
103 {
104 // address is the address of where we want to write/read
105 // len is the size of the data
106
107 if (m_ptr && !m_use_sub_blocks) {
108 assert(address >= m_address);
109 return *this;
110 }
111 if (m_len > m_mem.p_max_block_size) {
112 m_use_sub_blocks = true;
113 }
114
115 if (!m_use_sub_blocks) {
116 if (!((std::string)m_mem.p_mapfile).empty()) {
117 if ((m_ptr = MemoryServices::get().map_file(((std::string)(m_mem.p_mapfile)), m_len, m_address)) !=
118 nullptr) {
119 m_mapped = true;
120 return *this;
121 }
122 }
123 if (m_mem.p_shmem) {
124 std::stringstream shmname_stream;
125#if defined(_WIN32)
126 DWORD pid = GetCurrentProcessId();
127#else
128 pid_t pid = getpid();
129#endif
130 if (m_mem.p_shmem_prefix.get_value() != "")
131 shmname_stream << "/" << m_mem.p_shmem_prefix.get_value() << std::hex << pid;
132 else
133 shmname_stream << "/" << std::hex << pid << "-" << std::hex
134 << MemoryServices::get().get_shmem_seg_num();
135 if (shmname_stream.str().size() > 31) { /*PSHMNAMLEN in Mac OS*/
136 size_t hash = std::hash<std::string>{}(
137 shmname_stream.str()); // hash length is 16 hex digits in 64 bit machines.
138 shmname_stream.str(""); // clear
139 shmname_stream << "/" << std::hex << hash;
140 }
141 std::string shmname = shmname_stream.str();
142 int shm_fd = -1;
143 if ((m_ptr = MemoryServices::get().map_mem_create(shmname, m_len, &shm_fd)) != nullptr) {
144 m_mapped = true;
145 m_shmemID = ShmemIDExtension(shmname, (uint64_t)m_ptr, m_len, shm_fd);
146 return *this;
147 }
148 }
149
150 AllocatedMemory alloc_mem = MemoryServices::get().alloc(m_len);
151 if (alloc_mem.ptr != nullptr) {
152 m_ptr = alloc_mem.ptr;
153 m_aligned = alloc_mem.is_aligned;
154 if (m_mem.p_init_mem) memset(m_ptr, m_mem.p_init_mem_val, m_len);
155 return *this;
156 }
157
158 // else we failed to allocate, try with a smaller sub_block size.
159 m_use_sub_blocks = true;
160 }
161
162 if (m_len < m_mem.p_min_block_size) {
163 SCP_FATAL(m_mem.name()) << "Unable to allocate memory!"; // out of memory!
164 }
165
166 // return a index of sub_bloc
167 assert((m_len & ~(-1llu << N)) == 0);
168 uint64_t m_sub_size = m_len >> N;
169 int i = (address - m_address) / (m_sub_size);
170
171 if (!m_sub_blocks[i]) {
172 m_sub_blocks[i] = std::make_unique<SubBlock<N>>((i * m_sub_size) + m_address, m_sub_size, m_mem);
173 }
174 return m_sub_blocks[i]->access(address);
175 }
176
177 uint64_t read_sub_blocks(uint8_t* data, uint64_t offset, uint64_t len)
178 {
179 uint64_t block_offset = offset - m_address;
180 uint64_t block_len = m_len - block_offset;
181 uint64_t remain_len = (len < block_len) ? len : block_len;
182
183 memcpy(data, &m_ptr[block_offset], remain_len);
184
185 return remain_len;
186 }
187
188 uint64_t write_sub_blocks(const uint8_t* data, uint64_t offset, uint64_t len)
189 {
190 uint64_t block_offset = offset - m_address;
191 uint64_t block_len = m_len - block_offset;
192 uint64_t remain_len = (len < block_len) ? len : block_len;
193
194 memcpy(&m_ptr[block_offset], data, remain_len);
195
196 return remain_len;
197 }
198
199 uint8_t* get_ptr() { return m_ptr; }
200
201 uint64_t get_len() { return m_len; }
202
203 uint64_t get_address() { return m_address; }
204
205 ShmemIDExtension* get_extension()
206 {
207 if (m_shmemID.empty()) return nullptr;
208 return &m_shmemID;
209 }
210
211 void free_raw_alloc()
212 {
213 if (!m_ptr) {
214 return;
215 }
216 if (m_aligned) {
217#ifdef _WIN32
218 /*WINDOWS aligned alloc/free are done using _aligned_malloc/_aligned_free */
219 _aligned_free(m_ptr);
220#else
221 free(m_ptr);
222#endif
223 } else {
224 free(m_ptr);
225 }
226 }
227
228 ~SubBlock()
229 {
230 if (m_mapped) {
231 MemoryServices::get().unmap_file(m_ptr, m_len);
232 } else {
233 free_raw_alloc();
234 }
235 }
236 };
237
238private:
239 std::unique_ptr<gs_memory<BUSWIDTH>::SubBlock<>> m_sub_block;
240 cci::cci_broker_handle m_broker;
241
242protected:
243 virtual bool get_direct_mem_ptr(int id, tlm::tlm_generic_payload& txn, tlm::tlm_dmi& dmi_data)
244 {
245 if (!p_dmi) return false;
246 sc_dt::uint64 addr = txn.get_address();
247
248 if (!m_relative_addresses) {
249 if (addr < m_address) {
250 txn.set_response_status(tlm::TLM_ADDRESS_ERROR_RESPONSE);
251 return false;
252 }
253 addr -= m_address;
254 }
255 if (addr >= m_size) {
256 txn.set_response_status(tlm::TLM_ADDRESS_ERROR_RESPONSE);
257 return false;
258 }
259
260 SCP_TRACE(()) << " : DMI access to address "
261 << "0x" << std::hex << addr;
262
263 if (p_rom)
264 dmi_data.allow_read();
265 else
266 dmi_data.allow_read_write();
267
268 SubBlock<>& blk = m_sub_block->access(addr);
269
270 uint8_t* ptr = blk.get_ptr();
271 uint64_t size = blk.get_len();
272 uint64_t block_address = blk.get_address();
273
274 dmi_data.set_dmi_ptr(reinterpret_cast<unsigned char*>(ptr));
275 if (!m_relative_addresses) {
276 dmi_data.set_start_address(block_address + m_address);
277 dmi_data.set_end_address((block_address + m_address + size) - 1);
278 } else {
279 dmi_data.set_start_address(block_address);
280 dmi_data.set_end_address((block_address + size) - 1);
281 }
282 dmi_data.set_read_latency(p_latency);
283 dmi_data.set_write_latency(p_latency);
284
285 ShmemIDExtension* ext = blk.get_extension();
286 if (ext) {
287 txn.set_extension(ext);
288 }
289
290 return true;
291 }
292
293 virtual void b_transport(int id, tlm::tlm_generic_payload& txn, sc_core::sc_time& delay)
294 {
295 unsigned int len = txn.get_data_length();
296 unsigned char* ptr = txn.get_data_ptr();
297 sc_dt::uint64 addr = txn.get_address();
298 unsigned char* byt = txn.get_byte_enable_ptr();
299 unsigned int bel = txn.get_byte_enable_length();
300
301 if (txn.get_streaming_width() < len) {
302 SCP_FATAL(()) << "using streaming width: 0x" << std::hex << txn.get_streaming_width()
303 << " less than data length: 0x" << std::hex << len << " is not supported.";
304 }
305 SCP_INFO(()) << "Start b_transport :" << get_txn_command_str(txn) << " to address: 0x" << std::hex << addr
306 << " len: 0x" << std::hex << len;
307
308 if (!m_relative_addresses) {
309 if (addr < m_address) {
310 txn.set_response_status(tlm::TLM_ADDRESS_ERROR_RESPONSE);
311 return;
312 }
313 addr -= m_address;
314 }
315 if ((addr + len) > m_size) {
316 txn.set_response_status(tlm::TLM_ADDRESS_ERROR_RESPONSE);
317 return;
318 }
319
320 switch (txn.get_command()) {
321 case tlm::TLM_READ_COMMAND:
322 if (byt) {
323 for (unsigned int i = 0; i < len; i++)
324 if (byt[i % bel] == TLM_BYTE_ENABLED) {
325 if (!read(&(ptr[i]), (addr + i), 1)) {
326 SCP_FATAL(()) << "Address + length is out of range of the memory size";
327 }
328 }
329 } else {
330 if (!read(ptr, addr, len)) {
331 SCP_FATAL(()) << "Address + length is out of range of the memory size";
332 }
333 }
334 break;
335 case tlm::TLM_WRITE_COMMAND:
336 if (p_rom) {
337 txn.set_response_status(tlm::TLM_COMMAND_ERROR_RESPONSE);
338 return;
339 }
340 if (byt) {
341 for (unsigned int i = 0; i < len; i++)
342 if (byt[i % bel] == TLM_BYTE_ENABLED) {
343 if (!write(&(ptr[i]), (addr + i), 1)) {
344 SCP_FATAL(()) << "Address + length is out of range of the memory size";
345 }
346 }
347 } else {
348 if (!write(ptr, addr, len)) {
349 SCP_FATAL(()) << "Address + length is out of range of the memory size";
350 }
351 }
352 break;
353 default:
354 SCP_FATAL(()) << "TLM command not supported";
355 break;
356 }
357 delay += p_latency;
358 txn.set_response_status(tlm::TLM_OK_RESPONSE);
359 SCP_INFO(()) << "Completed b_transport :" << scp::scp_txn_tostring(txn);
360
361 if (p_dmi) txn.set_dmi_allowed(true);
362 }
363
364 const char* get_txn_command_str(const tlm::tlm_generic_payload& trans)
365 {
366 switch (trans.get_command()) {
367 case tlm::TLM_READ_COMMAND:
368 return "READ";
369 case tlm::TLM_WRITE_COMMAND:
370 return "WRITE";
371 case tlm::TLM_IGNORE_COMMAND:
372 return "IGNORE";
373 default:
374 break;
375 }
376 return "UNKNOWN";
377 }
378
379 virtual unsigned int transport_dbg(int id, tlm::tlm_generic_payload& txn)
380 {
381 unsigned int len = txn.get_data_length();
382 sc_dt::uint64 addr = txn.get_address();
383 sc_core::sc_time delay = sc_core::SC_ZERO_TIME;
384 SCP_INFO(()) << "Start b_transport dbg :" << get_txn_command_str(txn) << " to address: 0x" << std::hex << addr
385 << " len: 0x" << std::hex << len;
386 b_transport(id, txn, delay);
387 SCP_INFO(()) << "Completed b_transport dbg :" << get_txn_command_str(txn) << " to address: 0x" << std::hex
388 << addr << " len: 0x" << std::hex << len << " status: " << txn.get_response_string();
389 if (txn.get_response_status() == tlm::TLM_OK_RESPONSE)
390 return len;
391 else
392 return 0;
393 }
394
395 bool read(uint8_t* data, uint64_t offset, uint64_t len)
396 {
397 // force end of elaboration to ensure we fix the sizes
398 // this may happen if another model descides to load data into memory as
399 // part of it's initialization during before_end_of_elaboration.
400
401 if (!m_sub_block) before_end_of_elaboration();
402
403 uint64_t remain_len = 0;
404 uint64_t data_ptr_offset = 0;
405
406 if (offset + len > m_size) {
407 return false;
408 }
409
410 while (len > 0) {
411 SubBlock<>& blk = m_sub_block->access(offset + data_ptr_offset);
412
413 remain_len = blk.read_sub_blocks(&data[data_ptr_offset], offset + data_ptr_offset, len);
414
415 data_ptr_offset += remain_len;
416 len -= remain_len;
417 }
418
419 return true;
420 }
421 bool write(const uint8_t* data, uint64_t offset, uint64_t len)
422 {
423 if (!m_sub_block) before_end_of_elaboration();
424
425 uint64_t remain_len = 0;
426 uint64_t data_ptr_offset = 0;
427
428 if (offset + len > m_size) {
429 return false;
430 }
431
432 while (len > 0) {
433 SubBlock<>& blk = m_sub_block->access(offset + data_ptr_offset);
434
435 remain_len = blk.write_sub_blocks(&data[data_ptr_offset], offset + data_ptr_offset, len);
436
437 data_ptr_offset += remain_len;
438 len -= remain_len;
439 }
440
441 return true;
442 }
443
444public:
445 tlm_utils::multi_passthrough_target_socket<gs_memory<BUSWIDTH>, BUSWIDTH> socket;
447 cci::cci_param<bool> p_rom;
448 cci::cci_param<bool> p_dmi;
449 cci::cci_param<sc_core::sc_time> p_latency;
450 cci::cci_param<std::string> p_mapfile;
451 cci::cci_param<uint64_t> p_max_block_size;
452 cci::cci_param<uint64_t> p_min_block_size;
453 cci::cci_param<bool> p_shmem;
454 cci::cci_param<std::string> p_shmem_prefix;
455 cci::cci_param<bool> p_init_mem;
456 cci::cci_param<int> p_init_mem_val; // to match the signature of memset
457
458 gs::loader<> load;
459
460 // NB
461 // A size given by a config will always take precedence
462 // A size set on the constructor will take precedence over
463 // the size given on an e.g. 'add_target' in the router
464
465 gs_memory(sc_core::sc_module_name name, uint64_t _size = 0)
466 : m_sub_block(nullptr)
467 , m_broker(cci::cci_get_broker())
468 , socket("target_socket")
469 , reset("reset")
470 , p_rom("read_only", false, "Read Only memory (default false)")
471 , p_dmi("dmi_allow", true, "DMI allowed (default true)")
472 , p_latency("latency", sc_core::sc_time(10, sc_core::SC_NS), "Latency reported for DMI access")
473 , p_mapfile("map_file", "", "(optional) file to map this memory")
474 , p_max_block_size("max_block_size", 0x100000000, "Maximum size of the sub bloc")
475 , p_min_block_size("min_block_size", get_page_size(), "Minimum size of the sub bloc")
476 , p_shmem("shared_memory", false, "Allocate using shared memory")
477 , p_shmem_prefix("shared_memory_prefix", "", "(optional) prefix_for shared memory file")
478 , p_init_mem("init_mem", false, "Initialize allocated memory")
479 , p_init_mem_val("init_mem_val", 0, "Value to initialize memory to")
480 , load("load", [&](const uint8_t* data, uint64_t offset, uint64_t len) -> void {
481 if (!write(data, offset, len)) {
482 SCP_WARN(()) << " Offset : 0x" << std::hex << offset << " of the out of range";
483 }
484 })
485 {
486 SCP_DEBUG(()) << "Memory constructor";
487 MemoryServices::get().init(); // allow any init required
488 if (_size) {
489 std::string ts_name = std::string(sc_module::name()) + ".target_socket";
490 if (!m_broker.has_preset_value(ts_name + ".size")) {
491 m_broker.set_preset_cci_value(ts_name + ".size", cci::cci_value(_size));
492 }
493 }
494
495 socket.register_b_transport(this, &gs_memory::b_transport);
496 socket.register_transport_dbg(this, &gs_memory::transport_dbg);
497 socket.register_get_direct_mem_ptr(this, &gs_memory::get_direct_mem_ptr);
498
499 reset.register_value_changed_cb([&](bool value) {
500 if (value) {
501 SCP_WARN(()) << "Reset";
502 m_sub_block->doreset();
503 load.doreset(value);
504 }
505 });
506 }
507 static size_t get_page_size()
508 {
509 size_t page_size;
510#ifdef _WIN32
511 SYSTEM_INFO si;
512 GetSystemInfo(&si);
513 page_size = si.dwPageSize;
514#else
515 page_size = sysconf(_SC_PAGE_SIZE);
516#endif
517 return page_size;
518 }
519
520 void before_end_of_elaboration()
521 {
522 if (m_sub_block) {
523 return;
524 }
525
526 std::string ts_name = std::string(sc_module::name()) + ".target_socket";
527
528 m_address = base();
529 m_size = size();
530
531 m_sub_block = std::make_unique<gs_memory<BUSWIDTH>::SubBlock<>>(0, m_size, *this);
532
533 SCP_DEBUG(()) << "m_address: " << m_address;
534 SCP_DEBUG(()) << "m_size: " << m_size;
535
536 m_relative_addresses = true;
537 if (gs::cci_get<bool>(m_broker, ts_name + ".relative_addresses", m_relative_addresses)) {
538 m_broker.lock_preset_value(ts_name + ".relative_addresses");
539 }
540 }
541
542 gs_memory() = delete;
543 gs_memory(const gs_memory&) = delete;
544
545 ~gs_memory() {}
546
552 uint64_t size()
553 {
554 if (!m_size) {
555 std::string ts_name = std::string(sc_module::name()) + ".target_socket";
556 std::string ts_name_alias;
557 if (gs::cci_get<std::string>(m_broker, ts_name + ".0", ts_name_alias)) {
558 // deal with an alias
559 if (m_broker.has_preset_value(ts_name + ".size") &&
560 m_broker.get_preset_cci_value(ts_name + ".size").is_number()) {
561 SCP_WARN(())
562 ("The configuration alias provided ({}) will be ignored as a valid size is "
563 "also provided.",
564 ts_name);
565 } else {
566 ts_name = ts_name_alias;
567 if (ts_name[0] == '&') ts_name = (ts_name.erase(0, 1)) + ".target_socket";
568 }
569 }
570 m_size = gs::cci_get<uint64_t>(m_broker, ts_name + ".size");
571 }
572 return m_size;
573 }
574
580 uint64_t base()
581 {
582 if (!m_address_valid) {
583 std::string ts_name = std::string(sc_module::name()) + ".target_socket";
584 std::string ts_name_alias;
585 if (gs::cci_get<std::string>(m_broker, ts_name + ".0", ts_name_alias)) {
586 // deal with an alias
587 if (gs::cci_get<uint64_t>(m_broker, ts_name + ".address", m_address)) {
588 SCP_WARN(())
589 ("The configuration alias provided ({}) will be ignored as a valid address is "
590 "also provided.",
591 ts_name);
592 } else {
593 ts_name = ts_name_alias;
594 if (ts_name[0] == '&') ts_name = (ts_name.erase(0, 1)) + ".target_socket";
595 }
596 }
597 if (!m_broker.has_preset_value(ts_name + ".address")) {
598 m_address = 0; // fine for relative addressing
599 SCP_WARN(()) << "Can't find " << ts_name << ".address";
600 } else {
601 m_address = gs::cci_get<uint64_t>(m_broker, ts_name + ".address");
602 }
603 m_broker.lock_preset_value(ts_name + ".address");
604 m_address_valid = true;
605 }
606 return m_address;
607 }
608};
609} // namespace gs
610
611extern "C" void module_register();
612#endif
Definition target-signal-socket.h:163
Definition shmem_extension.h:24
uint64_t base()
this function returns the base address of the memory
Definition gs_memory.h:580
uint64_t size()
this function returns the size of the memory
Definition gs_memory.h:552
Definition loader.h:96
Tool which reads a Lua configuration file and sets parameters.
Definition biflow.cc:10
Definition memory_services.h:41