-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy pathmmio.h
195 lines (177 loc) · 5.03 KB
/
mmio.h
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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2012-2022, Intel Corporation
// written by Roman Dementiev
// Patrick Konsor
//
#pragma once
/*! \file mmio.h
\brief Interface to access memory mapped IO registers
*/
#include "types.h"
#ifdef _MSC_VER
#include "windows.h"
#include "winpmem\winpmem.h"
#include "Winmsrdriver\msrstruct.h"
#else
#include <unistd.h>
#endif
#include "mutex.h"
#include "utils.h"
#include <memory>
namespace pcm {
class CoreAffinityScope // sets core affinity if core >= 0, nop otherwise
{
std::shared_ptr<TemporalThreadAffinity> affinity{nullptr};
CoreAffinityScope(const CoreAffinityScope&) = delete;
CoreAffinityScope& operator = (const CoreAffinityScope&) = delete;
public:
CoreAffinityScope(const int core)
: affinity((core >= 0) ? std::make_shared<TemporalThreadAffinity>(core) : nullptr)
{
}
};
#ifdef _MSC_VER
class MMIORangeInterface
{
public:
virtual uint32 read32(uint64 offset) = 0;
virtual uint64 read64(uint64 offset) = 0;
virtual void write32(uint64 offset, uint32 val) = 0;
virtual void write64(uint64 offset, uint64 val) = 0;
virtual ~MMIORangeInterface() {}
};
class WinPmemMMIORange : public MMIORangeInterface
{
static std::shared_ptr<WinPmem> pmem;
static Mutex mutex;
static bool writeSupported;
uint64 startAddr;
template <class T>
void writeInternal(uint64 offset, T val)
{
if (!writeSupported)
{
std::cerr << "PCM Error: MMIORange writes are not supported by the driver\n";
return;
}
if (readonly)
{
std::cerr << "PCM Error: attempting to write to a read-only MMIORange\n";
return;
}
mutex.lock();
pmem->write(startAddr + offset, val);
mutex.unlock();
}
template <class T>
void readInternal(uint64 offset, T & res)
{
mutex.lock();
pmem->read(startAddr + offset, res);
mutex.unlock();
}
const bool readonly;
public:
WinPmemMMIORange(uint64 baseAddr_, uint64 size_, bool readonly_ = true);
uint32 read32(uint64 offset)
{
uint32 result = 0;
readInternal(offset, result);
return result;
}
uint64 read64(uint64 offset)
{
uint64 result = 0;
readInternal(offset, result);
return result;
}
void write32(uint64 offset, uint32 val)
{
writeInternal(offset, val);
}
void write64(uint64 offset, uint64 val)
{
writeInternal(offset, val);
}
};
class OwnMMIORange : public MMIORangeInterface
{
HANDLE hDriver;
char * mmapAddr;
const int core;
OwnMMIORange(const OwnMMIORange&) = delete;
OwnMMIORange& operator = (const OwnMMIORange&) = delete;
public:
OwnMMIORange( const uint64 baseAddr_,
const uint64 size_,
const bool readonly_ = true,
const int core_ = -1);
uint32 read32(uint64 offset);
uint64 read64(uint64 offset);
void write32(uint64 offset, uint32 val);
void write64(uint64 offset, uint64 val);
~OwnMMIORange();
};
class MMIORange
{
std::shared_ptr<MMIORangeInterface> impl;
const bool silent;
MMIORange(const MMIORange &) = delete;
MMIORange & operator = (const MMIORange &) = delete;
public:
MMIORange( const uint64 baseAddr_,
const uint64 size_,
const bool readonly_ = true,
const bool silent_ = false,
const int core = -1);
uint32 read32(uint64 offset)
{
warnAlignment<4>("MMIORange::read32", silent, offset);
return impl->read32(offset);
}
uint64 read64(uint64 offset)
{
warnAlignment<8>("MMIORange::read64", silent, offset);
return impl->read64(offset);
}
void write32(uint64 offset, uint32 val)
{
warnAlignment<4>("MMIORange::write32", silent, offset);
impl->write32(offset, val);
}
void write64(uint64 offset, uint64 val)
{
warnAlignment<8>("MMIORange::write64", silent, offset);
impl->write64(offset, val);
}
};
#elif defined(__APPLE__) || defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__)
class MMIORange
{
#ifndef __APPLE__
int32 fd;
#endif
char * mmapAddr;
const uint64 size;
#ifndef __APPLE__
const bool readonly;
#endif
const bool silent;
const int core;
MMIORange(const MMIORange &) = delete;
MMIORange & operator = (const MMIORange &) = delete;
public:
MMIORange( const uint64 baseAddr_,
const uint64 size_,
const bool readonly_ = true,
const bool silent_ = false,
const int core_ = -1);
uint32 read32(uint64 offset);
uint64 read64(uint64 offset);
void write32(uint64 offset, uint32 val);
void write64(uint64 offset, uint64 val);
~MMIORange();
};
#endif
void mmio_memcpy(void * dest, const uint64 src, const size_t n, const bool checkFailures, const bool silent = false);
} // namespace pcm