Skip to content

Commit 7644f4b

Browse files
committed
Limit dma page and superpage sizes for the CRORC
1 parent f0f04ac commit 7644f4b

File tree

2 files changed

+59
-48
lines changed

2 files changed

+59
-48
lines changed

src/Crorc/CrorcDmaChannel.cxx

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ CrorcDmaChannel::CrorcDmaChannel(const Parameters& parameters)
3131
: DmaChannelPdaBase(parameters, allowedChannels()), //
3232
//mPdaBar(getRocPciDevice().getPciDevice(), getChannelNumber()), // Initialize main DMA channel BAR
3333
//mPdaBar2(getRocPciDevice().getPciDevice(), 2), // Initialize BAR 2
34-
mPageSize(parameters.getDmaPageSize().get_value_or(8*1024)), // 8 kB default for uniformity with CRU
34+
mPageSize(parameters.getDmaPageSize().get_value_or(DMA_PAGE_SIZE)), // 8 kB default for uniformity with CRU
3535
mInitialResetLevel(ResetLevel::Internal), // It's good to reset at least the card channel in general
3636
mNoRDYRX(false), // Not sure
3737
mUseFeeAddress(false), // Not sure
@@ -46,16 +46,22 @@ CrorcDmaChannel::CrorcDmaChannel(const Parameters& parameters)
4646
mUseContinuousReadout(parameters.getReadoutMode().is_initialized() ?
4747
parameters.getReadoutModeRequired() == ReadoutMode::Continuous : false)
4848
{
49-
// Prep for BAR
50-
auto bar = ChannelFactory().getBar(parameters);
51-
crorcBar = std::move(std::dynamic_pointer_cast<CrorcBar> (bar)); // Initialize bar0
52-
49+
// Check that the DMA page is valid
50+
if (mPageSize != DMA_PAGE_SIZE) {
51+
BOOST_THROW_EXCEPTION(CrorcException() << ErrorInfo::Message("CRORC only supports 8KiB DMA page size")
52+
<< ErrorInfo::DmaPageSize(mPageSize));
53+
}
54+
5355
// Check that the loopback is valid. If not throw
5456
if (mLoopbackMode == LoopbackMode::Ddg) {
5557
BOOST_THROW_EXCEPTION(CruException() << ErrorInfo::Message("CRORC does not support given loopback mode")
5658
<< ErrorInfo::LoopbackMode(mLoopbackMode));
5759
}
5860

61+
// Prep for BAR
62+
auto bar = ChannelFactory().getBar(parameters);
63+
crorcBar = std::move(std::dynamic_pointer_cast<CrorcBar> (bar)); // Initialize bar0
64+
5965
// Create and register our ReadyFIFO buffer
6066
log("Initializing ReadyFIFO DMA buffer", InfoLogger::InfoLogger::Debug);
6167
{
@@ -95,8 +101,8 @@ void CrorcDmaChannel::deviceStartDma()
95101
{
96102
log("DMA start deferred until enough superpages available");
97103

98-
mFifoBack = 0;
99-
mFifoSize = 0;
104+
mFreeFifoBack = 0;
105+
mFreeFifoSize = 0;
100106
mReadyQueue.clear();
101107
mTransferQueue.clear();
102108
mPendingDmaStart = true;
@@ -135,8 +141,9 @@ void CrorcDmaChannel::startPendingDma()
135141
for (size_t j = 0; j < READYFIFO_ENTRIES; j++) {
136142
auto offset = superpageToPush.getOffset() + j * mPageSize;
137143
pushFreeFifoPage(j, getBusOffsetAddress(offset));
138-
mFifoSize = 128;
144+
//mFreeFifoSize++;
139145
}
146+
mFreeFifoSize = READYFIFO_ENTRIES;
140147

141148
if (mGeneratorEnabled) {
142149
log("Starting data generator");
@@ -158,23 +165,29 @@ void CrorcDmaChannel::startPendingDma()
158165

159166
std::this_thread::sleep_for(100ms);
160167

161-
/// Fixed wait for initial pages TODO polling wait with timeout
162-
while ((dataArrived(READYFIFO_ENTRIES - 1) != DataArrivalStatus::WholeArrived)) {
163-
std::this_thread::sleep_for(100ms);
164-
log("Initial pages not arrived", InfoLogger::InfoLogger::Warning);
165-
}
168+
if (true) { // Kostas: Temp if to try and remove special handling of first superpage
169+
/// Fixed wait for initial pages TODO polling wait with timeout
170+
while ((dataArrived(READYFIFO_ENTRIES - 1) != DataArrivalStatus::WholeArrived)) {
171+
std::this_thread::sleep_for(100ms);
172+
log("Initial pages not arrived", InfoLogger::InfoLogger::Warning);
173+
}
166174

167-
// TODO: Move this read to fillSuperpages...
168-
auto superpageToReadout = mTransferQueue.front(); // Read the first Superpage
169-
mReadyQueue.push_back(superpageToReadout);
175+
// TODO: Move this read to fillSuperpages...
176+
auto superpageToReadout = mTransferQueue.front(); // Read the first Superpage
177+
//superpageToReadout.setReceived(READYFIFO_ENTRIES * mPageSize);
178+
//superpageToReadout.setReady(true);
179+
mReadyQueue.push_back(superpageToReadout);
170180

171-
getReadyFifoUser()->reset(); // Reset ReadyFifo
172-
mFifoBack = 0; // Reset FreeFifo references
173-
mFifoSize = 0;
181+
getReadyFifoUser()->reset(); // Reset ReadyFifo
182+
mFreeFifoBack = 0; // Reset FreeFifo references
183+
mFreeFifoSize = 0;
174184

175-
mTransferQueue.pop_front(); // Pop the Superpage after resets; don't introduce race conditions
185+
mPendingDmaStart = false;
186+
mTransferQueue.pop_front(); // Pop the Superpage after resets; don't introduce race conditions
187+
} else {
188+
mPendingDmaStart = false;
189+
}
176190

177-
mPendingDmaStart = false;
178191
log("DMA started");
179192
}
180193

@@ -299,24 +312,17 @@ auto CrorcDmaChannel::getSuperpage() -> Superpage
299312
void CrorcDmaChannel::pushSuperpage(Superpage superpage)
300313
{
301314
checkSuperpage(superpage);
302-
constexpr size_t MIN_SIZE = 1*1024*1024;
303-
constexpr size_t MAX_SIZE = 2*1024*1024;
304-
305-
if (superpage.getSize() > MAX_SIZE) {
306-
BOOST_THROW_EXCEPTION(CrorcException()
307-
<< ErrorInfo::Message("Could not enqueue superpage, C-RORC backend does not support superpage sizes above 2 MiB"));
308-
}
309315

310-
if (!Utilities::isMultiple(superpage.getSize(), MIN_SIZE)) {
316+
if (superpage.getSize() != SUPERPAGE_SIZE) {
311317
BOOST_THROW_EXCEPTION(CrorcException()
312-
<< ErrorInfo::Message("Could not enqueue superpage, C-RORC backend requires superpage size multiple of 1 MiB"));
318+
<< ErrorInfo::Message("Could not enqueue superpage, the C-RORC backend only supports superpage sizes of 1 MiB"));
313319
}
314320

315321
if (mTransferQueue.size() >= TRANSFER_QUEUE_CAPACITY) {
316322
BOOST_THROW_EXCEPTION(Exception() << ErrorInfo::Message("Could not push superpage, transfer queue was full"));
317323
}
318324

319-
if (mFifoSize >= READYFIFO_ENTRIES) {
325+
if (mFreeFifoSize >= READYFIFO_ENTRIES) {
320326
BOOST_THROW_EXCEPTION(Exception()
321327
<< ErrorInfo::Message("Could not push superpage, firmware queue was full (this should never happen)"));
322328
}
@@ -331,8 +337,8 @@ void CrorcDmaChannel::pushSuperpage(Superpage superpage)
331337
for (int i = 0; i < READYFIFO_ENTRIES; i++) { //TODO: *always* push 128 FIFO entries
332338
auto busAddress = getBusOffsetAddress(superpage.getOffset() + i * mPageSize);
333339
pushFreeFifoPage(i, busAddress);
334-
mFifoSize++;
335340
}
341+
mFreeFifoSize = READYFIFO_ENTRIES;;
336342
mTransferQueue.push_back(superpage);
337343
}
338344

@@ -362,15 +368,15 @@ void CrorcDmaChannel::fillSuperpages()
362368
auto isArrived = [&](int descriptorIndex) {return dataArrived(descriptorIndex) == DataArrivalStatus::WholeArrived;};
363369
auto resetDescriptor = [&](int descriptorIndex) {getReadyFifoUser()->entries[descriptorIndex].reset();};
364370

365-
while (mFifoSize > 0) {
366-
if (isArrived(mFifoBack)) {
367-
uint32_t length = getReadyFifoUser()->entries[mFifoBack].getSize();
368-
resetDescriptor(mFifoBack);
371+
while (mFreeFifoSize > 0) {
372+
if (isArrived(mFreeFifoBack)) {
373+
uint32_t length = getReadyFifoUser()->entries[mFreeFifoBack].getSize();
374+
resetDescriptor(mFreeFifoBack);
369375

370-
mFifoSize--;
371-
mFifoBack = (mFifoBack + 1) % READYFIFO_ENTRIES;
376+
mFreeFifoSize--;
377+
mFreeFifoBack = (mFreeFifoBack + 1) % READYFIFO_ENTRIES;
372378

373-
if (mFifoSize == 0) { // Push the superpage after all of the 128 fifo entries are pushed
379+
if (mFreeFifoBack == 0) { // Push the superpage after all of the 128 fifo entries are pushed
374380
auto superpage = mTransferQueue.front();
375381
superpage.setReceived(mPageSize * READYFIFO_ENTRIES); //TODO: Always full pages?
376382
superpage.setReady(true);

src/Crorc/CrorcDmaChannel.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,19 @@ class CrorcDmaChannel final : public DmaChannelPdaBase
5656
virtual void deviceResetChannel(ResetLevel::type resetLevel) override;
5757

5858
private:
59+
/// Superpage size supported by the CRORC backend
60+
static constexpr size_t SUPERPAGE_SIZE = 1*1024*1024;
61+
62+
/// DMA page size
63+
static constexpr size_t DMA_PAGE_SIZE = 8*1024;
64+
65+
/// Max amount of superpages in the transfer queue (i.e. pending transfer).
66+
static constexpr size_t TRANSFER_QUEUE_CAPACITY = SUPERPAGE_SIZE / (READYFIFO_ENTRIES * DMA_PAGE_SIZE);
67+
5968
/// Max amount of superpages in the ready queue (i.e. finished transfer).
6069
/// This is an arbitrary size, can easily be increased if more headroom is needed.
61-
static constexpr size_t READY_QUEUE_CAPACITY = READYFIFO_ENTRIES / READYFIFO_ENTRIES; //Single 1Mi Superpage
62-
// TODO: Update to handle larger than 1Mi Superpages
63-
// A superpage should contain as many dma pages as possible
70+
static constexpr size_t READY_QUEUE_CAPACITY = TRANSFER_QUEUE_CAPACITY + 1; //Allow for some leeway
6471

65-
/// Max amount of superpages in the transfer queue (i.e. pending transfer).
66-
static constexpr size_t TRANSFER_QUEUE_CAPACITY = READY_QUEUE_CAPACITY;
6772

6873
/// Minimum number of superpages needed to bootstrap DMA
6974
//static constexpr size_t DMA_START_REQUIRED_SUPERPAGES = 1;
@@ -108,9 +113,9 @@ class CrorcDmaChannel final : public DmaChannelPdaBase
108113
DataArrivalStatus::type dataArrived(int index);
109114

110115
/// Get front index of FIFO
111-
int getFifoFront() const
116+
int getFreeFifoFront() const
112117
{
113-
return (mFifoBack + mFifoSize) % READYFIFO_ENTRIES;
118+
return (mFreeFifoBack + mFreeFifoSize) % READYFIFO_ENTRIES;
114119
};
115120

116121
CrorcBar* getBar()
@@ -137,10 +142,10 @@ class CrorcDmaChannel final : public DmaChannelPdaBase
137142
uintptr_t mReadyFifoAddressBus;
138143

139144
/// Back index of the firmware FIFO
140-
int mFifoBack = 0;
145+
int mFreeFifoBack = 0;
141146

142147
/// Amount of elements in the firmware FIFO
143-
int mFifoSize = 0;
148+
int mFreeFifoSize = 0;
144149

145150
/// Queue for superpages that are pushed to the firmware FIFO
146151
SuperpageQueue mTransferQueue {TRANSFER_QUEUE_CAPACITY};

0 commit comments

Comments
 (0)