gem5  v22.0.0.2
mmio_reader.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021 Advanced Micro Devices, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived from this
17  * software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef __DEV_AMDGPU_MMIO_READER_HH__
33 #define __DEV_AMDGPU_MMIO_READER_HH__
34 
35 #include <cstdint>
36 #include <list>
37 #include <string>
38 #include <tuple>
39 #include <unordered_map>
40 #include <vector>
41 
42 #include "base/logging.hh"
43 #include "mem/packet.hh"
44 
45 namespace gem5
46 {
47 
63 {
64  private:
70  const uint64_t BAR0 = 0x2400000000;
71  const uint64_t BAR2 = 0x2200000000;
72  const uint32_t BAR5 = 0xecf00000;
73  const uint32_t ROM = 0xc0000;
74 
75  /* Sizes based on Vega Frontier Edition */
76  const uint64_t BAR0_SIZE = 0x400000000; // 16GB
77  const uint64_t BAR2_SIZE = 0x200000; // 2MB
78  const uint32_t BAR5_SIZE = 0x80000; // 512kB
79  const uint32_t ROM_SIZE = 0x20000; // 128kB
80 
90  /* trace_entry containing (3), (4), and (5). */
91  typedef std::tuple<uint64_t, std::tuple<char, uint64_t>> trace_entry_t;
92 
93  /* Trace entries are recorded as a list for each offset in a given BAR. */
94  typedef std::unordered_map<uint32_t, std::list<trace_entry_t>> trace_BAR_t;
95 
96  /* There are 7 BARs (BAR0-BAR5 + expansion ROM) */
98 
99  /* Indexes used to print driver loading progress. */
100  uint64_t trace_index = 0;
101  uint64_t trace_final_index = 0;
102  uint64_t trace_cur_index = 0;
103 
104  /* An entry in the MMIO trace. */
105  struct MmioTrace
106  {
107  char event;
108  uint16_t size;
109  uint16_t bar;
110  uint64_t addr;
111  uint64_t data;
112  uint64_t index;
113  } mtrace;
114 
115  /* Lines in the MMIO trace we care about begin with R, W, or UNKNOWN. */
116  bool
118  {
119  return tokens[0] == "R";
120  }
121 
122  bool
124  {
125  return tokens[0] == "W";
126  }
127 
128  bool
130  {
131  return tokens[0] == "UNKNOWN";
132  }
133 
134  /* Checks if this line of trace is W/R/UNKNOWN */
135  bool
137  {
138  return tokens[0] == "R" || tokens[0] == "W" || tokens[0] == "UNKNOWN";
139  }
140 
141  /* Checks if this line of trace is in a BAR we care about (0, 2, 5) */
142  bool
144  {
145  uint64_t addr = strtoull(tokens[4].c_str(), nullptr, 16);
146  uint16_t bar = traceGetBAR(addr);
147  return (bar == 0 || bar == 2 || bar == 5);
148  }
149 
150  uint8_t
151  traceGetBAR(uint64_t addr)
152  {
153  if (BAR0 <= addr && addr < (BAR0 + BAR0_SIZE)) {
154  return 0;
155  } else if (BAR2 <= addr && addr < (BAR2 + BAR2_SIZE)) {
156  return 2;
157  } else if (BAR5 <= addr && addr < (BAR5 + BAR5_SIZE)) {
158  return 5;
159  } else if (ROM <= addr && addr < (ROM + ROM_SIZE)) {
160  return 6;
161  } else {
162  return -1;
163  }
164  }
165 
166  uint64_t
168  {
169  if (addr >= BAR0 && addr < (BAR0 + BAR0_SIZE)) {
170  return addr - BAR0;
171  } else if (addr >= BAR2 && addr < (BAR2 + BAR2_SIZE)) {
172  return addr - BAR2;
173  } else if (addr >= BAR5 && addr < (BAR5 + BAR5_SIZE)) {
174  return addr - BAR5;
175  } else if (addr >= ROM && addr < (ROM + ROM_SIZE)) {
176  return addr - ROM;
177  } else {
178  panic("Can't find offset for the address in MMIO trace!");
179  }
180  }
181 
182  void
184  {
185  if (traceIsRead(tokens) || traceIsWrite(tokens)) {
186  mtrace.event = traceIsRead(tokens) ? 'R' : 'W';
187  mtrace.size = strtoul(tokens[1].c_str(), nullptr, 10);
188  mtrace.addr = strtoull(tokens[4].c_str(), nullptr, 16);
189  mtrace.data = strtoull(tokens[5].c_str(), nullptr, 16);
190  }
191  else if (traceIsUnknown(tokens)) {
192  mtrace.event = 'U';
193  mtrace.size = 0;
194  mtrace.addr = strtoull(tokens[3].c_str(), nullptr, 16);
195  mtrace.data = 0;
196  }
198 
200  trace_index++;
201  }
202 
203  void
205  {
206  trace_entry_t trace_entry;
207  trace_entry = std::make_tuple(mtrace.index,
208  std::make_tuple(mtrace.event, mtrace.data));
209 
210  uint16_t barnum = mtrace.bar;
211  uint64_t offset = traceGetOffset(mtrace.addr);
212 
213  trace_BARs[barnum][offset].push_back(trace_entry);
214  }
215 
216  public:
219 
226  void readMMIOTrace(std::string trace_file);
227 
236  void readFromTrace(PacketPtr pkt, int barnum, Addr offset);
237 
247  void writeFromTrace(PacketPtr pkt, int barnum, Addr offset);
248 };
249 
250 } // namespace gem5
251 
252 #endif // __DEV_AMDGPU_MMIO_READER_HH__
gem5::AMDMMIOReader::MmioTrace::data
uint64_t data
Definition: mmio_reader.hh:111
gem5::AMDMMIOReader::~AMDMMIOReader
~AMDMMIOReader()
Definition: mmio_reader.hh:218
gem5::AMDMMIOReader::ROM
const uint32_t ROM
Definition: mmio_reader.hh:73
gem5::AMDMMIOReader::trace_BAR_t
std::unordered_map< uint32_t, std::list< trace_entry_t > > trace_BAR_t
Definition: mmio_reader.hh:94
gem5::AMDMMIOReader::mtrace
struct gem5::AMDMMIOReader::MmioTrace mtrace
gem5::AMDMMIOReader::BAR2
const uint64_t BAR2
Definition: mmio_reader.hh:71
gem5::AMDMMIOReader::isRelevant
bool isRelevant(std::vector< std::string > tokens)
Definition: mmio_reader.hh:143
gem5::AMDMMIOReader::trace_final_index
uint64_t trace_final_index
Definition: mmio_reader.hh:101
gem5::AMDMMIOReader::MmioTrace
Definition: mmio_reader.hh:105
gem5::AMDMMIOReader::writeFromTrace
void writeFromTrace(PacketPtr pkt, int barnum, Addr offset)
Get the next MMIO write from the trace file to an offset in a BAR and compare the value with the data...
Definition: mmio_reader.cc:102
gem5::AMDMMIOReader::BAR5_SIZE
const uint32_t BAR5_SIZE
Definition: mmio_reader.hh:78
std::vector< std::string >
gem5::AMDMMIOReader::readMMIOTrace
void readMMIOTrace(std::string trace_file)
Read an MMIO trace gathered from a real system and place the MMIO values read and written into the MM...
Definition: mmio_reader.cc:44
gem5::AMDMMIOReader::MmioTrace::bar
uint16_t bar
Definition: mmio_reader.hh:109
gem5::AMDMMIOReader::traceIsUnknown
bool traceIsUnknown(std::vector< std::string > tokens) const
Definition: mmio_reader.hh:129
packet.hh
gem5::AMDMMIOReader::MmioTrace::event
char event
Definition: mmio_reader.hh:107
gem5::AMDMMIOReader::BAR0_SIZE
const uint64_t BAR0_SIZE
Definition: mmio_reader.hh:76
gem5::AMDMMIOReader::trace_cur_index
uint64_t trace_cur_index
Definition: mmio_reader.hh:102
gem5::AMDMMIOReader::MmioTrace::size
uint16_t size
Definition: mmio_reader.hh:108
gem5::Packet
A Packet is used to encapsulate a transfer between two objects in the memory system (e....
Definition: packet.hh:291
gem5::AMDMMIOReader::MmioTrace::addr
uint64_t addr
Definition: mmio_reader.hh:110
gem5::AMDMMIOReader::isIO
bool isIO(std::vector< std::string > tokens) const
Definition: mmio_reader.hh:136
gem5::ArmISA::offset
Bitfield< 23, 0 > offset
Definition: types.hh:144
gem5::AMDMMIOReader::traceIsRead
bool traceIsRead(std::vector< std::string > tokens) const
Definition: mmio_reader.hh:117
gem5::AMDMMIOReader
Helper class to read Linux kernel MMIO trace from amdgpu modprobes.
Definition: mmio_reader.hh:62
gem5::Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:147
gem5::AMDMMIOReader::traceParseTokens
void traceParseTokens(std::vector< std::string > tokens)
Definition: mmio_reader.hh:183
gem5::AMDMMIOReader::BAR2_SIZE
const uint64_t BAR2_SIZE
Definition: mmio_reader.hh:77
gem5::AMDMMIOReader::trace_BARs
trace_BAR_t trace_BARs[6]
Definition: mmio_reader.hh:97
gem5::AMDMMIOReader::readFromTrace
void readFromTrace(PacketPtr pkt, int barnum, Addr offset)
Get the next MMIO read from the trace file to an offset in a BAR and write the value to the packet pr...
Definition: mmio_reader.cc:76
gem5::AMDMMIOReader::AMDMMIOReader
AMDMMIOReader()
Definition: mmio_reader.hh:217
gem5::AMDMMIOReader::MmioTrace::index
uint64_t index
Definition: mmio_reader.hh:112
logging.hh
gem5::AMDMMIOReader::trace_index
uint64_t trace_index
Definition: mmio_reader.hh:100
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: gpu_translation_state.hh:37
gem5::AMDMMIOReader::BAR0
const uint64_t BAR0
These are the BAR values from the system where the trace is collected.
Definition: mmio_reader.hh:70
gem5::AMDMMIOReader::traceIsWrite
bool traceIsWrite(std::vector< std::string > tokens) const
Definition: mmio_reader.hh:123
gem5::AMDMMIOReader::ROM_SIZE
const uint32_t ROM_SIZE
Definition: mmio_reader.hh:79
gem5::AMDMMIOReader::trace_entry_t
std::tuple< uint64_t, std::tuple< char, uint64_t > > trace_entry_t
The information we want from each relevant line of trace are: (1) The BAR number where the accessed a...
Definition: mmio_reader.hh:91
gem5::AMDMMIOReader::traceGetOffset
uint64_t traceGetOffset(uint64_t addr)
Definition: mmio_reader.hh:167
gem5::AMDMMIOReader::traceGetBAR
uint8_t traceGetBAR(uint64_t addr)
Definition: mmio_reader.hh:151
gem5::AMDMMIOReader::recordMtrace
void recordMtrace()
Definition: mmio_reader.hh:204
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:178
gem5::X86ISA::addr
Bitfield< 3 > addr
Definition: types.hh:84
gem5::AMDMMIOReader::BAR5
const uint32_t BAR5
Definition: mmio_reader.hh:72

Generated on Thu Jul 28 2022 13:32:30 for gem5 by doxygen 1.8.17