Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 83 additions & 7 deletions packages/river_hdl/lib/src/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'data_port.dart';

import 'core/csr.dart';
import 'core/mmu.dart';
import 'core/misaligned_load.dart';
import 'core/pipeline.dart';
import 'inferred_rom.dart';

Expand Down Expand Up @@ -328,6 +329,60 @@ class RiverCore extends BridgeModule {
config.mxlen.size,
config.mxlen.size,
);
// The current D-cache is virtually addressed before translation and has
// no physical PMA check on hits. Keep its existing alignment traps. Likewise
// do not advertise this path with unimplemented PMP/PBMT or H/OoO contexts.
// A platform opts in by describing safe, full-beat-readable RAM in its PMAs.
final enableMisalignedLoads =
config.l1cache == null &&
!config.hasHypervisor &&
config.executionMode != ExecutionMode.outOfOrder &&
config.mmu.pmp.entries == 0 &&
!config.mmu.hasPageBasedMemoryTypes &&
config.mmu.pagingModes.every(
(m) =>
m == RiscVPagingMode.bare ||
(config.mxlen == RiscVMxlen.rv64 && m == RiscVPagingMode.sv39),
) &&
wbConfig.dataWidth == config.mxlen.size &&
wbConfig.addressWidth == config.mxlen.size &&
wbConfig.effectiveSelWidth == config.mxlen.size ~/ 8 &&
config.mmu.pma.regions.any(
(r) =>
r.memoryType == HarborPmaMemoryType.memory &&
r.readable &&
r.idempotent &&
r.misalignedSupport &&
r.accessWidths.contains(config.mxlen.size ~/ 8),
);
final loadMisaligned = Logic(name: 'loadMisaligned');
final loadSize = Logic(name: 'loadSize', width: 3);
final loadPageFault = Logic(name: 'loadPageFault');
final mmuExecRead = enableMisalignedLoads
? DataPortInterface(config.mxlen.size, config.mxlen.size)
: pipeExecRead;
final loadUnit = enableMisalignedLoads
? MisalignedLoad(
clk,
reset,
pipeExecRead.en,
pipeExecRead.addr,
loadSize,
loadMisaligned,
mmuExecRead.done,
mmuExecRead.valid,
mmuExecRead.data,
loadPageFault,
)
: null;
if (loadUnit != null) {
mmuExecRead.en <= loadUnit.readEnable;
mmuExecRead.addr <= loadUnit.readAddress;
pipeExecRead.done <= loadUnit.done;
pipeExecRead.valid <= loadUnit.valid;
pipeExecRead.data <= loadUnit.data;
}

// Exec write: pipeline drives en/addr/data (with sized prefix), MMU responds done/valid
final pipeExecWrite = DataPortInterface(
config.mxlen.size + 7,
Expand Down Expand Up @@ -359,19 +414,29 @@ class RiverCore extends BridgeModule {
),
);

final dportEn = mux(execWriteActive, Const(1), pipeExecRead.en);
final dportEn = mux(execWriteActive, Const(1), mmuExecRead.en);
final dportAddr = mux(
execWriteActive,
pipeExecWrite.addr,
pipeExecRead.addr,
mmuExecRead.addr,
);
final dportWe = execWriteActive;
final dportWdata = mux(
execWriteActive,
writeValue.zeroExtend(config.mxlen.size),
Const(0, width: config.mxlen.size),
);
final dportSize = mux(execWriteActive, writeLog2Size, Const(2, width: 3));
final dportSize = mux(
execWriteActive,
writeLog2Size,
loadUnit == null
? Const(2, width: 3)
: mux(
loadUnit.restricted,
Const((config.mxlen.size ~/ 8).bitLength - 1, width: 3),
Const(2, width: 3),
),
);

// Wishbone responses from the external bus
final wbAckExt = Logic(name: 'wbAckExt');
Expand Down Expand Up @@ -563,6 +628,7 @@ class RiverCore extends BridgeModule {
wbDatMisoExt,
mmuConfig: config.mmu,
busConfig: wbConfig,
restrictedRead: loadUnit?.restricted,
wbErr: wbErrExt,
satpMode: config.mmu.hasPaging ? satpModeWire : null,
satpRoot: config.mmu.hasPaging ? satpRootWire : null,
Expand Down Expand Up @@ -664,9 +730,9 @@ class RiverCore extends BridgeModule {
(dcache.respValid | dcache.respFault) & execWriteActive;
pipeExecWrite.valid <= dcache.respValid & execWriteActive;
} else {
pipeExecRead.done <= mmu.dportDone & ~execWriteActive;
pipeExecRead.valid <= mmu.dportValid & ~execWriteActive;
pipeExecRead.data <= mmu.dportRdata;
mmuExecRead.done <= mmu.dportDone & ~execWriteActive;
mmuExecRead.valid <= mmu.dportValid & ~execWriteActive;
mmuExecRead.data <= mmu.dportRdata;

pipeExecWrite.done <= mmu.dportDone & execWriteActive;
pipeExecWrite.valid <= mmu.dportValid & execWriteActive;
Expand Down Expand Up @@ -1517,6 +1583,8 @@ class RiverCore extends BridgeModule {
interruptCause = interruptCauseReg.named('interruptCause');
}

loadPageFault <= mmu.dportFault;

// Pipeline.
pipeline = RiverPipeline(
clk,
Expand Down Expand Up @@ -1569,11 +1637,17 @@ class RiverCore extends BridgeModule {
mstateen0Se0: csrs?.mstateen0Se0,
hstateen0Se0: csrs?.hstateen0Se0,
memFaultGuest: config.hasHypervisor ? mmu.dportFaultGuest : null,
enableMisalignedLoads: enableMisalignedLoads,
loadFaultTval: loadUnit?.faultAddress,
memAccessFault: !handleAccessFaults
? null
: (useDCache
? dcache!.respFaultIsAccess
: mmu.dportDone & ~mmu.dportValid & ~mmu.dportFault),
: mux(
loadMisaligned,
loadUnit?.accessFault ?? Const(0),
mmu.dportDone & ~mmu.dportValid & ~mmu.dportFault,
)),
ifetchAccessFault: !handleAccessFaults
? null
: (useICache
Expand Down Expand Up @@ -1622,6 +1696,8 @@ class RiverCore extends BridgeModule {
// machine at random.
icFlush <= pipeline.fence;
mmuTlbFlush <= pipeline.fence;
loadMisaligned <= pipeline.output('misalignedLoad');
loadSize <= pipeline.output('loadSize');
if (useDCache) dFlush <= pipeline.fence;

// An access is guest-translated when the core is virtualized OR the current
Expand Down
94 changes: 89 additions & 5 deletions packages/river_hdl/lib/src/core/exec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,11 @@ abstract class ExecutionUnit extends Module {
final int vlen;
final bool hasSupervisor;
final bool hasUser;
final bool enableMisalignedLoads;
Logic get misalignedLoad => output('misalignedLoad');
Logic get loadSize => output('loadSize');
late final Logic _misalignedLoadAllowed;
late final Logic? _loadFaultTval;
// When true the mul family uses the shared multi-cycle IterativeMultiplier
// (chunk multiply reused per cycle) instead of a single-cycle partial-product
// tree. The area sign is config-dependent (measured both ways: iterative loses
Expand Down Expand Up @@ -601,6 +606,8 @@ abstract class ExecutionUnit extends Module {
DataPortInterface? microcodeRead,
this.hasSupervisor = false,
this.hasUser = false,
this.enableMisalignedLoads = false,
Logic? loadFaultTval,
this.useIterativeMul = true,
required this.microcode,
required this.mxlen,
Expand Down Expand Up @@ -657,6 +664,15 @@ abstract class ExecutionUnit extends Module {
fetchAccessFault ?? Const(0),
);
_memAccessFault = addInput('memAccessFault', memAccessFault ?? Const(0));
_loadFaultTval = loadFaultTval == null
? null
: addInput('loadFaultTval', loadFaultTval, width: mxlen.size);
addOutput('misalignedLoad');
addOutput('loadSize', width: 3);
if (!enableMisalignedLoads) {
misalignedLoad <= Const(0);
loadSize <= Const(2, width: 3);
}
_fetchFaultTval = fetchFaultTval == null
? null
: addInput('fetchFaultTval', fetchFaultTval, width: mxlen.size);
Expand All @@ -670,6 +686,32 @@ abstract class ExecutionUnit extends Module {
width: microcode.opIndexWidth,
);

const integerLoads = {
'lb',
'lbu',
'lh',
'lhu',
'lw',
'lwu',
'ld',
'c.lw',
'c.ld',
'c.lwsp',
'c.ldsp',
'c.lh',
'c.lhu',
'c.lbu',
};
Logic allowed = Const(0);
if (enableMisalignedLoads) {
for (final entry in microcode.execLookup.entries) {
if (integerLoads.contains(entry.value.mnemonic)) {
allowed |= instrIndex.eq(entry.key);
}
}
}
_misalignedLoadAllowed = allowed;

instrTypeMap = Map.fromEntries(
instrTypeMap.entries.map(
(entry) => MapEntry(entry.key, addInput(entry.value.name, entry.value)),
Expand Down Expand Up @@ -1265,6 +1307,7 @@ abstract class ExecutionUnit extends Module {
mopStep < 0,
done < 0,
fpFlags < 0,
if (enableMisalignedLoads) ...[misalignedLoad < 0, loadSize < 2],
output('trap') < 0,
output('trapInterrupt') < 0,
output('trapEpc') < currentPc,
Expand Down Expand Up @@ -1474,6 +1517,7 @@ abstract class ExecutionUnit extends Module {
this.csrWrite!.data < 0,
],
fence < 0,
if (enableMisalignedLoads) ...[misalignedLoad < 0, loadSize < 2],
interruptHold < 0,
nextPc < currentPc,
nextSp < currentSp,
Expand Down Expand Up @@ -1744,6 +1788,13 @@ abstract class ExecutionUnit extends Module {
if (t == Trap.instructionPageFault && _fetchFaultTval != null) {
tval = _fetchFaultTval;
}
if (t == Trap.loadPageFault && _loadFaultTval != null) {
tval = mux(
misalignedLoad,
_loadFaultTval,
tval ?? Const(0, width: mxlen.size),
);
}
return rawTrap(trapInterrupt, causeCode, tval, suffix);
}

Expand Down Expand Up @@ -1784,6 +1835,8 @@ class DynamicExecutionUnit extends ExecutionUnit {
DataPortInterface microcodeRead, {
super.hasSupervisor,
super.hasUser,
super.enableMisalignedLoads,
super.loadFaultTval,
required super.microcode,
required super.mxlen,
super.vlen = 128,
Expand Down Expand Up @@ -3339,10 +3392,25 @@ class DynamicExecutionUnit extends ExecutionUnit {
[
If(
((opBase + imm) &
Const(size.bytes - 1, width: mxlen.size))
.neq(0),
Const(
size.bytes - 1,
width: mxlen.size,
))
.neq(0) &
~_misalignedLoadAllowed,
then: doTrap(Trap.misalignedLoad, opBase + imm),
orElse: [
if (enableMisalignedLoads) ...[
misalignedLoad <
((opBase + imm) &
Const(
size.bytes - 1,
width: mxlen.size,
))
.neq(0),
loadSize <
Const(size.bytes.bitLength - 1, width: 3),
],
memRead.en < 1,
memRead.addr < (opBase + imm),
],
Expand Down Expand Up @@ -4031,6 +4099,8 @@ class StaticExecutionUnit extends ExecutionUnit {
super.rs2Read,
super.rdWrite, {
super.hasSupervisor = false,
super.enableMisalignedLoads,
super.loadFaultTval,
super.hasUser = false,
required super.microcode,
required super.mxlen,
Expand Down Expand Up @@ -5366,16 +5436,30 @@ class StaticExecutionUnit extends ExecutionUnit {
final byteOff = addr & Const(busBytes - 1, width: mxlen.size);
final shifted =
memRead.data >> (byteOff * Const(8, width: mxlen.size));
final raw = shifted.slice(mop.size.bits - 1, 0);
final raw = mux(
unaligned & _misalignedLoadAllowed,
memRead.data,
shifted,
).slice(mop.size.bits - 1, 0);

steps.add(
CaseItem(Const(i, width: maxLen.bitLength), [
If(
unaligned,
unaligned & ~_misalignedLoadAllowed,
then: doTrap(Trap.misalignedLoad, addr, '_${op.mnemonic}'),
orElse: [
if (enableMisalignedLoads) ...[
misalignedLoad < unaligned,
loadSize <
Const(mop.size.bytes.bitLength - 1, width: 3),
],
memRead.en < 1,
memRead.addr < alignedAddr,
memRead.addr <
mux(
unaligned & _misalignedLoadAllowed,
addr,
alignedAddr,
),
mopStep < mopStep + 1,
],
),
Expand Down
Loading
Loading