diff --git a/docs/ucie_bringup.md b/docs/ucie_bringup.md new file mode 100644 index 0000000..e5ffd83 --- /dev/null +++ b/docs/ucie_bringup.md @@ -0,0 +1,307 @@ +# UCIe bring-up on Iris + +Iris has two UCIe links. This page covers what's specific to Iris: the links' address map, how +software reaches them, which clocks come in on bumps, and how bumps map to links. + +How to clock, reset and bring up PhyTest, the IP's link tester, is in the UCIe IP's own guide: +[`ucie/docs/phytest_bringup.md`](../ucie/docs/phytest_bringup.md). Read its section 3 +("The register port, and how not to hang it") before touching a link. + +Paths are from the repository root. `ucie/...` is the UCIe submodule. + +--- + +## 1. The two links + +Both links are chiplet-router ports with 16 lanes (`src/Iris.scala`). Their +`UcieTLParams` differ only in `address` and `moduleId`. + +| | Link 0 | Link 1 | +|---|---|---| +| IrisTop IO | `c2c_ucie0` | `c2c_ucie1` | +| Chiplet-router port (and `program_router` `port` argument) | 0 | 1 | +| `UcieTLParams.address` (`base`) | `0x200000` | `0x208000` | +| PhyTest and lane registers (`UcieTLRegs`, PHY test clock) | `0x200000`–`0x203BFF` | `0x208000`–`0x20BBFF` | +| Controller controls and UCIe spec registers (`UcieController`, TX sideband clock) | `0x203C00`–`0x207FFF` | `0x20BC00`–`0x20FFFF` | +| Clock and reset registers (`UcieClkRegs`, chip clock) | `0x210000`–`0x210FFF` | `0x218000`–`0x218FFF` | +| Module (`moduleId`) | `UcieTL` (0) | `UcieTL_1` (1) | + +Everything outside these windows is unmapped, including `0x211000`–`0x217FFF` and `0x219000` up. +Iris removed the error device from every bus, so an unmapped access +gets no error response. **It hangs** (inference: nothing on the bus can answer it). + +### Registers you'll touch first + +Offsets come from `software/ucie.h`, which is the authority. Regenerate it whenever the `ucie` +submodule moves (section 5). + +| Register | Offset | Link 0 | Link 1 | Safe before `ucieRst` = 0? | +|---|---|---|---|---| +| `UCIE_MAIN_CLK_SEL` | `0x10000` | `0x210000` | `0x218000` | yes | +| `UCIE_PHY_TEST_CLK_BYPASS_EN` | `0x10038` | `0x210038` | `0x218038` | yes | +| `UCIE_CLK_EN` / `UCIE_RX_CLK_EN` | `0x10048` / `0x10050` | `0x210048` / `0x210050` | `0x218048` / `0x218050` | yes | +| `UCIE_CLK_CFG_BUSY` | `0x10088` | `0x210088` | `0x218088` | yes | +| `UCIE_CLK_CFG_UNGATE_REQ` | `0x100A0` | `0x2100A0` | `0x2180A0` | yes | +| `UCIE_PLL_LOCK_OBSERVED` | `0x100A8` | `0x2100A8` | `0x2180A8` | yes | +| `UCIE_RST` (reset value 1) | `0x100B0` | `0x2100B0` | `0x2180B0` | yes | +| `UCIE_PHY_TEST_CLK_EN` | `0x100D8` | `0x2100D8` | `0x2180D8` | yes | +| `UCIE_TX_TEST_STATE` | `0x128` | `0x200128` | `0x208128` | **no**: needs `ucieRst` = 0 and a running PHY test clock | +| `UCIE_RX_IDLE_WORDS_OBSERVED` | `0x3C8` | `0x2003C8` | `0x2083C8` | no | +| `UCIE_TX_TEST_FIFO_UNDERFLOW` / `UCIE_RX_TEST_FIFO_OVERFLOW` | `0x3868` / `0x3870` | `0x203868` / `0x203870` | `0x20B868` / `0x20B870` | no | + +Other Iris addresses that bring-up programs use (`software/router.h`): the +chiplet-router table at `0x4000` (32 bytes per entry, 4 entries), and the chip ID register at +`0x4080`. + +--- + +## 2. How software reaches the registers + +Each link's register port is on the SoC's **control bus** (CBUS), coupled there in +`src/Iris.scala`. It does not go through the chiplet router. `ChipletRoutingKey.controlBusWhere` +only names which bus that is. The link's chip clock is the `uncore` clock, shared with every bus at +500 MHz. + +There are three ways in: + +1. **The cores.** C programs include `software/ucie.h`. Every helper takes `base` (`0x200000` or + `0x208000`) and uses `reg_write64`/`reg_read64` (`software/mmio.h`). +2. **TSI over serial TileLink.** + - IrisTop's `serial_tl` port carries 16-bit phits on the tapeout config, 32-bit with `sim`. + `serial_tl.clock_in` clocks it from outside. + - Its requests enter at the front bus and reach the UCIe port through FBUS → SBUS → CBUS. Its + manager side serves off-chip memory at `0x8000_0000`, 4 GiB. + - The host-side `TSIToTileLink` issues a single 8-byte Get or Put only for an 8-byte-aligned, + two-word request (`TSIToTileLink` in testchipip). Use exactly that + for UCIe registers. +3. **JTAG.** + - The debug module has system-bus access (`WithJtagDTM`, `WithDebugSBA`), 64 bits wide. + - Use 64-bit accesses, such as OpenOCD `write_memory`/`read_memory` with width 64. + - Abstract commands through a halted hart also work, unless the control bus is wedged. + +**Use 64-bit accesses.** +- Writes to fields wider than 32 bits (`txctl_N_tile`, `clkPhaseSel`, seeds, packet counts, + `txDataChunkIn0/1`, `sbTxPacket`) are silently ignored unless the whole field is in the byte mask. +- A 32-bit read of a 64-bit counter returns half of it. + +**Hang hazards, and recovery on Iris.** +- These hang a request forever: + - an access to a link's PhyTest or controller window while that domain is in reset (`ucieRst` = 1 + is the power-on state) or unclocked; + - an access to an unmapped address; + - an access to a remote address (the off-chip range at `0x8_0000_0000`) + while the link isn't carrying TileLink. +- A hung UCIe access blocks the control bus (inferred from the bus structure): both links' + registers, the router table and chip ID, the clock divider, CLINT, PLIC, the boot ROM, the debug + module's hart-facing ROM, and the periphery bus with the UART. TSI and JTAG system-bus access can + still reach memory. +- JTAG can't reset the chip: IrisTop doesn't use the debug module's `ndreset`. + **Only `reset` recovers.** Set up the bench so the host can drive + Iris's reset pin. +- Rules: + - Only one agent touches a link at a time. Halt the cores before poking UCIe registers from TSI + or JTAG. + - Never access a link's `0x0000`–`0x7FFF` window until that link's `UcieClkRegs` checks pass + (`phytest_bringup.md`, sections 3 and 4). + +--- + +## 3. Clocks that come from bumps + +Each link has its own PHY clock bumps, under `c2c_uciek.phy`. + +| Bump (per link) | What it feeds | When it must be driven | Harness (`IrisSpec`) | +|---|---|---|---| +| `phy.bypassClk` | The UCIe main clock while `mainClkSel` = 3 (reset). Also source 2 for the PHY test and sideband dividers. ≤ 16 GHz. | Always, for the reset clocking. Sets the TX line rate: lane rate = 2 × bypass ÷ `txClkDiv`. | 8000 MHz (16 GT/s at the reset ÷1) | +| `phy.digitalBypassClk` | The PHY test clock while `phyTestClkBypassEn` = 1 (reset). Runs the PhyTest register block, PhyTest and every RX AFE controller. | **Before any access to the link's PhyTest window**, unless software first moves the PHY test clock onto a PLL. Must be faster than every packet clock in use (`phytest_bringup.md` §1.3). | 500 MHz. This **equals** the 16 GT/s packet clock (8 GHz/16): no margin. The UCIe repo's own harness uses 800 MHz. | +| `phy.sidebandBypassClk` | The TX sideband clock while `sbClkBypassEn` = 1 (reset). Runs the UCIe controller, its registers and every sideband serializer. | Before any access to the controller window, or any sideband use. | 800 MHz | +| `phy.refClk` | The 100 MHz reference for PLL8 (×80) and PLL12 (×120). | Only when a PLL is used. | 100 MHz | +| `phy.rxClkP`, `phy.rxClkN`, `phy.sbRxClk` | Forwarded clocks from the partner (or the same link, looped back). | n/a | Looped back or ringed (section 4) | + +Chip-level clock pins are separate from the UCIe ones: +- `clock` feeds every SoC clock group through the programmable `TLClockDivider`, including the + UCIe chip clock. +- `serial_tl.clock_in` clocks the serial TileLink port. +- `jtag.TCK` clocks JTAG. +- `clock_tap` is an output tapping an SoC clock, not a UCIe clock. + +**Bench notes for Iris:** +- At the reset clocking, supply `bypassClk`, `digitalBypassClk` and `sidebandBypassClk` on every + link you bring up. The PLLs need `refClk` instead. Software moves onto them through + `UcieClkRegs`, which is safe while the link is still in reset. +- With an 8 GHz bypass at ÷1 the packet clock is 500 MHz, so an 800 MHz `digitalBypassClk` covers + up to 24 GT/s. Otherwise switch the PHY test clock to PLL8 ÷8 (1 GHz). +- Bring up first at ÷4 (4 GT/s off an 8 GHz bypass), not on a slow bypass clock + (`phytest_bringup.md` §1.2 and step 5). +- PhyTest never needs the sideband clock. But an unclocked sideband domain wedges the bus if + anything touches the controller window (`0x203C00`/`0x20BC00` and up). + +--- + +## 4. Bump and link mapping + +- `c2c_ucie0` is `system.d2d_port_ios(0)`, chiplet-router port 0: the `UcieTL` at `0x200000`. +- `c2c_ucie1` is `system.d2d_port_ios(1)`, port 1: the `UcieTL_1` at `0x208000`. + +Port order is the order of `ChipletRoutingParams.ports`. + +Each `c2c_uciek` has two parts: +- `phy`: `txData[16]`, `txValid`, `txTrack`, `txClkP`, `txClkN`, `rxData[16]`, `rxValid`, + `rxTrack`, `rxClkP`, `rxClkN`, `sbTxClk`, `sbTxData`, `sbRxClk`, `sbRxData`, plus the four clock + inputs of section 3. +- `debug`: the observation outputs `mux0` and `mux1`, each watching any of ten clocks and data + taps through its own 16:1 mux (`UCIE_DEBUG_MUX_SEL`), and `txData`, the TX debug lane. Each is + off until software enables its driver. + +Chisel flattens the names with underscores, for example `c2c_ucie0_phy_bypassClk`. + +**Lane order on the bumps** is PHY lane order: `txData[i]`/`rxData[i]` are data lanes 0-15. Valid, +track and the forwarded clock have their own bumps. + +**Harness connectivity** (`test/src/IrisSpec.scala`, `connectRingLink` and the code around it): +- **One chip:** each link loops onto itself (`UcieBumpsIO.loopback`): TX data, valid, track, the + forwarded clock and the sideband go to the same link's RX. `run_lfsr(base_k, n)` is + self-consistent there. +- **N chips:** a ring. Chip `i`'s `c2c_ucie1` connects to chip `i+1`'s `c2c_ucie0`, all signals, + both ways (`UcieBumpsIO.connect`). With two chips, chip 0 link 1 ↔ chip 1 link 0 and chip 1 + link 1 ↔ chip 0 link 0. Each die's TX goes to the *other* die, so its RX must be armed before the + other die transmits (`phytest_bringup.md` step 7, topology B). + +**Routing.** `program_router(entry, chip_id, port)` sends a chip ID to port 0 or 1 +(`software/router.h`). In a two-chip ring both ports reach the other chip, so either works. + +--- + +## 5. Building and running bring-up programs + +- **Toolchain.** Install `riscv64-unknown-elf-gcc`, and set `RISCV` for the simulator build + (`test/src/Utils.scala`). +- **Build every program** (`PROGRAMS` in `software/Makefile`): `make -C software`. They link + against `htif_nano.specs`, so `printf` and the exit code go over HTIF and need a host serving + it: SimTSI in simulation, the lab TSI host on silicon. +- **Regenerate `software/ucie.h`:** `make -C software ucie.h` or + `./mill runMain edu.berkeley.cs.iris.GenUcieHeader software/ucie.h`. + It uses mill, so don't run it during another mill build in the same checkout. It refuses to emit + a header if the two links' register layouts differ. +- **Run in simulation (VCS):** `./mill test.testOnly edu.berkeley.cs.iris.IrisSpec -- -z ""`. + `Utils.simulateTopWithBinaries` builds the harness, compiles VCS and runs the binaries. + `debug = true` dumps FSDB. +- **Run on silicon.** The TSI host loads the ELF into off-chip memory over `serial_tl`. The chip + boots from the boot ROM at `0x10000` and the boot-address register. + The host services HTIF. The bench-side procedure isn't in this repository. +- **Existing UCIe programs:** + - `software/ucie-loopback.c` runs `setup_ucie` on both links, puts both mainbands in TileLink + mode, and does remote reads and writes through the router. It tests the TileLink path, not + PhyTest, and needs both links working. + - `software/ucie-digital-loopback.c` runs `setup_ucie_digital` on both links. That aligns each + RX's words to its own TX's through PhyTest (`align_rx_words`, `ucie/docs/phytest_bringup.md` + §8.4), prints the RX UI delay it set, then trains the UCIe controller. It spins forever if + link training never completes. On the harness both links delay their RX lanes 20 UI, train, + and carry remote reads and writes. + - Neither is a first-silicon program. + - The PhyTest programs `software/phytest-loopback.c`, `phytest-clocks.c` and `phytest-two-chip.c` + (shared helpers in `software/phytest.h`) are in section 7. + +--- + +## 6. Iris-specific bring-up notes + +- **One link at a time.** The two links share the chip clock, the chip reset and the control bus. + Their clocking, resets and `UcieClkRegs` are separate, but a hang on either link's registers + blocks both (section 2). +- **Per link, the order is:** + 1. Ping `UcieClkRegs` (base + `0x10000`). + 2. Configure clocks if not on the bypass pins. + 3. Check that `UCIE_CLK_METER_PHY_TEST` and `UCIE_CLK_METER_SB` read nonzero, then write + `ucieRst` = 0. + 4. A single read of `UCIE_TX_TEST_STATE`. + 5. `setup_ucie`, which turns on the TX and RX clocks (`UCIE_CLK_EN`, `UCIE_RX_CLK_EN`), then + the overrides in `phytest_bringup.md` step 4. + 6. Set the rate and phase, and check `UCIE_CLK_METER_TX_DIV` and `_RX_DIV`. + 7. Run the on-chip loopback lane. + 8. Run the mainband LFSR, after an injected error has shown the counters count. + 9. Check the sideband with the sideband tester (`phytest_bringup.md` step 10). + 10. Run gated-clock bursts at the UCIe controller's postamble (step 11). + 11. Hand the link to the UCIe controller (step 12): `setup_ucie_digital` on a link looped back + onto itself; by hand between two chips, since `align_rx_words` can't measure across them. +- **Lessons from Kodiak, the previous chip** (`phytest_bringup.md` steps 4-9). The generated + helpers already apply them: observation pad drivers off during link tests (`setup_ucie` leaves + them off; `enable_debug_drivers` turns them on for a scope), a distinct LFSR seed per lane + (`seed_lfsrs`), and `run_lfsr` waiting until the packet count stops climbing. Kodiak's RX + hand-over and vref settings don't apply: Iris's RX front end is different. +- **Physical design and lab checks this page can't settle:** + - IrisConfig emits the UCIe behavioral models (`includeDefaultModels = true`). Confirm the PD + flow replaces the analog ones with the macros. + - `digitalBypassClk`, `sidebandBypassClk` and `refClk` have no ESD cell in the UCIe RTL. + Confirm the pad ring provides one. + - Confirm with analog what termination `zen` = 1 with `zctl` = 0 gives (`phytest_bringup.md` + step 4). + +--- + +## 7. Simulation tests and bring-up programs + +The PhyTest bring-up programs in `software/` are the same in simulation and in the lab. They run +on the core and reach both links at their real MMIO bases. Each check prints what it expected and +what it read instead of stopping. The program exits nonzero if any check failed. + +| Program | Topology | What it covers | +|---|---|---| +| `phytest-loopback.c` | each link looped back onto itself (the single-chip harness, or a loopback board) | On both links: clock meters against the board clocks; LFSR run with distinct per-lane seeds; injected errors counted exactly once; `rxSelfSync` with every RX seed wrong; valid moved onto track at both ends; gated-clock bursts the UCIe way after the training in `phytest_bringup.md` §8.3, parking alternately and always low, with the forwarded clock centred (`PT_CENTRED_DELAY_TAPS` in `phytest.h`: 51 on the harness, the trained code on silicon); no RX words while parked; manual pattern into the capture SRAM; error-triggered capture; `rxStartMode` immediate; track counters; the loopback lane and its FIFO flags; per-lane fixed and inverted sources; one injected error found by lane, UI and class; the observation bumps (on for that test only) and the TX debug lane; the sideband tester | +| `phytest-clocks.c` | UCIe0 looped back | TX divider at /1, /2, /4 off the bypass pin; PHY test clock onto PLL8 /8, stopping the program (instead of hanging the bus) if a moved clock doesn't run; main clock onto PLL8 and PLL12; a PHY test clock too slow for the link, which must set both FIFO flags (below), and the recovery; TX queue capped at 8 words with the PHY test clock at twice the packet clock (at equal rates 8 runs dry and 10 holds); sideband clock onto PLL8 /10; every clock meter after every move, and no PLL lock lost | +| `phytest-two-chip.c` | two chips, each one's UCIe1 wired to the other's UCIe0 (the two-chip ring, or two chips on a package) | an LFSR run each way; each RX hears only the TX wired to it; `rxManualCheck` scoring the partner's manual pattern against this chip's TX pattern SRAM, with one reference word deliberately wrong | + +**A slow PHY test clock starves this die's TX and overflows its RX.** When a run's next data word +isn't queued in time, `TxClkGater` sends an idle word instead. The receiving RX then gets an idle +word mid-run, or with the clock gated, the burst split in two. With the clock running, the RX still +takes a word every packet clock, more than a slow PHY test clock drains. On a looped-back link, +`UCIE_TX_TEST_FIFO_UNDERFLOW` and `UCIE_RX_TEST_FIFO_OVERFLOW` both set. Between two dies, the RX +overflow shows on the die with the slower PHY test clock, whichever die sends. + +Every wait in `phytest.h` is bounded in core cycles (`PT_TIMEOUT`). A bound counted in MMIO reads +is far too short on silicon next to a slow divided clock. + +### How the two-chip program keeps in step + +Each chip arms the RX it is about to be sent to and tells its partner with a sideband packet on +that link. It starts its own TX only after the partner has said the same of the RX that TX feeds. +The chips need no host or shared clock, and needn't start together: +- A chip resends its message every `RESEND_CYCLES` until the partner acknowledges it on the same + link. A message sent before the partner has set up that link (still in `ucieRst`, or before + `pt_sb_reset`) is lost, and a resend replaces it. +- A packet that isn't one of the program's messages means the receiver came out of reset + mid-packet. The chip resets its receiver to realign, and the next resend gets through. +- A chip waits up to `HANDSHAKE_TIMEOUT` (1000 × `PT_TIMEOUT`, about 2 s at 1 GHz) for each + phase, then skips it. + +So start both chips within about a second of each other. + +### Running them in simulation + +```bash +export RISCV= +export PATH=$RISCV/bin:$PATH +make -C software phytest-loopback.riscv phytest-clocks.riscv phytest-two-chip.riscv +./mill test.testOnly edu.berkeley.cs.iris.IrisSpec -- -z "should run phytest" +``` + +VCS compiles its own C with the host `gcc`. GCC 14 rejects it +(`rmapats.c: implicit declaration of function`), so put GCC 13 or older first on `PATH` for the +sim. + +The tests run on `IrisConfig(sim = true)` unless noted: +- `should run phytest loopback test`. It also checks that every observation bump of both links + toggled while the program had them on. +- `should run phytest loopback test on the tapeout config`: `IrisConfig(sim = false)`, serial TL at + 100 MHz, the netlist's configuration. +- `should run phytest clocks test`. +- `should run phytest two-chip test`, on two chips. + +### What simulation does not show + +- The lanes are the behavioral models (`includeDefaultModels`): a shift register per lane. No + code, termination or delay setting makes a lane fail. The AMS models in `ucie/verilog` cover + that. +- The harness clocks are fixed (section 3): 8 GHz main bypass, 500 MHz PHY test bypass, 800 MHz + sideband bypass, 100 MHz PLL reference. +- The observation bumps are only checked for toggling, not for what they carry. diff --git a/software/Makefile b/software/Makefile index 8cbeb9e..2863e9e 100644 --- a/software/Makefile +++ b/software/Makefile @@ -37,7 +37,7 @@ VEC_ARCHFLAGS = -march=rv64gcv_zfh_zvfh -mabi=$(ABI) CFLAGS = -std=gnu99 -O2 -fno-common -fno-builtin-printf -Wall -specs=htif_nano.specs -Iinclude LDFLAGS = -specs=htif_nano.specs -PROGRAMS = hello hello0 hello1 router ring-hello ucie-loopback ucie-digital-loopback tacit-always tacit-dma ucie-vec-memcpy multihart-vec-amo clock-div +PROGRAMS = hello hello0 hello1 router ring-hello ucie-loopback ucie-digital-loopback tacit-always tacit-dma ucie-vec-memcpy multihart-vec-amo clock-div phytest-loopback phytest-clocks phytest-two-chip .DEFAULT_GOAL := default @@ -54,6 +54,10 @@ PROGRAMS = hello hello0 hello1 router ring-hello ucie-loopback ucie-digital-loop %.riscv: %.o $(GCC) $(LDFLAGS) $< -o $@ +# The PhyTest programs share their helpers through a header. Not ucie.h, +# which is phony: naming it would regenerate it on every build. +phytest-loopback.o phytest-clocks.o phytest-two-chip.o: phytest.h + ucie-vec-memcpy.o: ucie-vec-memcpy.c $(GCC) $(CFLAGS) $(VEC_ARCHFLAGS) -c $< -o $@ diff --git a/software/phytest-clocks.c b/software/phytest-clocks.c new file mode 100644 index 0000000..14f61ee --- /dev/null +++ b/software/phytest-clocks.c @@ -0,0 +1,165 @@ +// PhyTest on UCIe0 with its bumps looped back, through the clock setups of +// bring-up: the TX divider, the main clock on each PLL, the PHY test and +// sideband clocks on a PLL, and a PHY test clock too slow for the link, which +// the crossing flags must report. +// +// As in bring-up, every change ends with `pt_setup`, which restarts the block +// on its new clocks. + +#include "phytest.h" + +#define N_PACKETS 64 + +// `MainClkSel`, `PhyTestClkSrcSel` and the divider codes, from the clocking +// tile's interface. +#define MAIN_PLL8 0 +#define MAIN_PLL12 1 +#define MAIN_BYPASS 3 +#define TX_DIV1 0 +#define TX_DIV2 1 +#define TX_DIV4 2 +#define SRC_PLL8 0 +#define SRC_PLL12 1 +#define PHY_TEST_DIV8 4 +#define SB_DIV10 2 + +static void move_main_clk(uintptr_t base, uint64_t sel, uint64_t div) { + set_main_clk(base, sel, div); + pt_wait_clk_cfg(base); + pt_setup(base); +} + +// After an apply that moved the PHY test or sideband clock: both must run +// before `pt_setup` touches their register windows, or the bus hangs. If +// either is stopped (a PLL that won't lock), end the apply, put both back on +// their bypass pins, and stop the program. +static void require_clocks(uintptr_t base, const char *what) { + int busy = !pt_wait_clk_cfg(base); + uint64_t pt = pt_meter_mhz(base, UCIE_CLK_METER_PHY_TEST, PT_CHIP_MHZ); + uint64_t sb = pt_meter_mhz(base, UCIE_CLK_METER_SB, PT_CHIP_MHZ); + if (!busy && pt != 0 && sb != 0) return; + PT_CHECK(0, "%s: apply busy %d, PHY test %lu MHz, sideband %lu MHz", what, + busy, pt, sb); + release_clk_gate(base); + pt_wr(base, UCIE_PHY_TEST_CLK_BYPASS_EN, 1); + pt_wr(base, UCIE_SB_CLK_BYPASS_EN, 1); + apply_clk_cfg(base); + release_clk_gate(base); + pt_finish("phytest-clocks"); +} + +// The packet clocks, TX and looped-back RX, from the clock meters. +static void check_packet_clocks(uintptr_t base, uint64_t mhz) { + pt_check_meter(base, UCIE_CLK_METER_TX_DIV, mhz, "TX divided"); + pt_check_meter(base, UCIE_CLK_METER_RX_DIV, mhz, "RX divided"); +} + +static void test_sideband_on_pll(uintptr_t base) { + // 800 MHz, /10 off PLL8, which this apply turns on if it isn't already. + pt_wr(base, UCIE_PLL8_EN, 1); + pt_wr(base, UCIE_SB_CLK_SRC_SEL, SRC_PLL8); + pt_wr(base, UCIE_SB_CLK_DIV, SB_DIV10); + pt_wr(base, UCIE_SB_CLK_BYPASS_EN, 0); + apply_clk_cfg(base); + require_clocks(base, "sideband onto PLL8 /10"); + pt_setup(base); + const uint64_t packet = 0x5a5a00000000c10cULL; + pt_sb_send(base, packet); + uint64_t got; + if (pt_sb_recv(base, &got)) PT_CHECK_EQ("sideband packet off PLL8", got, packet); +} + +int main(void) { + const uintptr_t base = UCIE0_REG_BASE; + + pt_begin("ucie0", "bypass clock /1"); + pt_setup(base); + pt_check_meter(base, UCIE_CLK_METER_PHY_TEST, 500, "PHY test on its bypass pin"); + pt_check_meter(base, UCIE_CLK_METER_SB, 800, "sideband on its bypass pin"); + check_packet_clocks(base, 500); + pt_lfsr_check(base, N_PACKETS, "bypass /1"); + + pt_begin("ucie0", "bypass clock /2"); + move_main_clk(base, MAIN_BYPASS, TX_DIV2); + check_packet_clocks(base, 250); + pt_lfsr_check(base, N_PACKETS, "bypass /2"); + + pt_begin("ucie0", "bypass clock /4"); + move_main_clk(base, MAIN_BYPASS, TX_DIV4); + check_packet_clocks(base, 125); + pt_lfsr_check(base, N_PACKETS, "bypass /4"); + + // PLL8 up, and the PHY test clock onto it, /8 = 1 GHz. The apply + // holds the PHY test clock's gate shut until PLL8 has locked. + pt_begin("ucie0", "PHY test clock onto PLL8 /8"); + use_internal_clk(base, SRC_PLL8, PHY_TEST_DIV8); + require_clocks(base, "PHY test clock onto PLL8 /8"); + pt_wait_eq(base, UCIE_PLL_LOCK_OBSERVED, 1, "PLL8 to lock"); + pt_setup(base); + pt_check_meter(base, UCIE_CLK_METER_PHY_TEST, 1000, "PHY test on PLL8 /8"); + pt_lfsr_check(base, N_PACKETS, "PHY test clock on PLL8, main on bypass"); + + pt_begin("ucie0", "main clock onto PLL8 /1"); + move_main_clk(base, MAIN_PLL8, TX_DIV1); + check_packet_clocks(base, 500); + pt_lfsr_check(base, N_PACKETS, "main on PLL8 /1"); + + // The TX queue capped below its reset value of 16, for the lowest latency, + // with the PHY test clock at twice the packet clock. The closer the two + // clocks, the larger the cap must be. At equal rates, 8 runs the queue dry + // and 10 is the smallest that works. + pt_begin("ucie0", "TX queue capped at 8 words"); + pt_wr(base, UCIE_TX_QUEUE_LIMIT, 8); + pt_lfsr_check(base, N_PACKETS, "TX queue capped at 8 words"); + pt_wr(base, UCIE_TX_QUEUE_LIMIT, 16); + + pt_begin("ucie0", "main clock onto PLL12 /1"); + move_main_clk(base, MAIN_PLL12, TX_DIV1); + check_packet_clocks(base, 750); + pt_lfsr_check(base, N_PACKETS, "main on PLL12 /1"); + + // The PHY test clock back on its bypass pin, which the harness runs at + // 500 MHz. The 12 GHz lanes move a word every 1.33 ns, so the TX queue runs + // dry partway through the run and its underflow flag must be set. The PHY + // sends an idle word for each missing word and keeps the forwarded clock + // running. The looped-back RX still takes a word every packet clock, faster + // than the PHY test clock can drain, so the RX queue overflows too. The two + // packet clocks meter the same. + pt_begin("ucie0", "PHY test clock too slow for the link"); + pt_wr(base, UCIE_PHY_TEST_CLK_BYPASS_EN, 1); + apply_clk_cfg(base); + pt_wait_clk_cfg(base); + pt_setup(base); + pt_set_seeds(base, pt_seed, pt_seed); + pt_arm(base, UCIE_TX_TEST_MODE_LFSR, N_PACKETS); + pt_wr(base, UCIE_TX_EXECUTE, 1); + pt_wait_eq(base, UCIE_TX_TEST_STATE, UCIE_TX_TEST_STATE_DONE, + "TX run to finish"); + pt_wait_cycles(1024); + PT_CHECK_EQ("TX test FIFO underflow with a slow PHY test clock", + pt_rd(base, UCIE_TX_TEST_FIFO_UNDERFLOW), 1); + PT_CHECK_EQ("RX test FIFO overflow with a slow PHY test clock", + pt_rd(base, UCIE_RX_TEST_FIFO_OVERFLOW), 1); + { + uint64_t tx = pt_meter_mhz(base, UCIE_CLK_METER_TX_DIV, PT_CHIP_MHZ); + uint64_t rx = pt_meter_mhz(base, UCIE_CLK_METER_RX_DIV, PT_CHIP_MHZ); + PT_CHECK(rx * 50 >= tx * 49 && rx * 50 <= tx * 51, + "RX packet clock %lu MHz, TX %lu MHz", rx, tx); + } + + // And back: a datapath reset on a fast enough clock clears both. + pt_begin("ucie0", "PHY test clock back onto PLL8 /8"); + pt_wr(base, UCIE_PHY_TEST_CLK_BYPASS_EN, 0); + apply_clk_cfg(base); + require_clocks(base, "PHY test clock back onto PLL8 /8"); + move_main_clk(base, MAIN_PLL8, TX_DIV1); + pt_lfsr_check(base, N_PACKETS, "after the slow PHY test clock"); + + pt_begin("ucie0", "sideband clock onto PLL8 /10"); + test_sideband_on_pll(base); + pt_check_meter(base, UCIE_CLK_METER_SB, 800, "sideband on PLL8 /10"); + PT_CHECK_EQ("PLL lock lost", pt_rd(base, UCIE_PLL_LOCK_LOST), 0); + + pt_finish("phytest-clocks"); + return 0; +} diff --git a/software/phytest-loopback.c b/software/phytest-loopback.c new file mode 100644 index 0000000..8f4e3b5 --- /dev/null +++ b/software/phytest-loopback.c @@ -0,0 +1,500 @@ +// PhyTest over every UCIe port, with each port's bumps looped back onto +// itself: the single-chip harness in simulation, or a loopback board. +// +// Each test leaves the registers it changed as `setup_ucie` left them, so +// they run in any order and any subset. + +#include "phytest.h" + +#define N_PACKETS 64 + +static uint64_t zero(int lane) { return 0; } + +// ===== LFSR ===== + +// At the TX queue's reset cap of 16 words, which on the Iris harness, with the +// PHY test clock at the packet clock, is where a cap has the least room. +static void test_lfsr(uintptr_t base) { + PT_CHECK_EQ("TX queue cap reset value", pt_rd(base, UCIE_TX_QUEUE_LIMIT), 16); + pt_set_seeds(base, pt_seed, pt_seed); + pt_arm(base, UCIE_TX_TEST_MODE_LFSR, N_PACKETS); + pt_go(base, N_PACKETS); + pt_check_errors(base, PT_MAINBAND_MASK, zero, "LFSR run"); + pt_check_fifo_flags(base, "LFSR run"); +} + +// One injected error on a data lane and one on track, counted exactly once +// each. Infinite, and read under `rxPauseCounters`, as a lab run would be. +static int inject_lane; +static uint64_t one_on_injected(int lane) { + return lane == inject_lane || lane == UCIE_TRACK_LANE; +} + +static void test_inject(uintptr_t base) { + pt_set_seeds(base, pt_seed, pt_seed); + pt_arm(base, UCIE_TX_TEST_MODE_LFSR, 0); + // A strobe before the run hits its first packet. + inject_lane = 3; + pt_wr(base, UCIE_TX_INJECT_LANE, inject_lane); + pt_wr(base, UCIE_TX_INJECT_ERROR, 1); + pt_go(base, 0); + pt_wait_ge(base, UCIE_RX_PACKETS_RECEIVED, 16, "RX packets"); + pt_wr(base, UCIE_TX_INJECT_LANE, UCIE_TRACK_LANE); + pt_wr(base, UCIE_TX_INJECT_ERROR, 1); + uint64_t r = pt_rd(base, UCIE_RX_PACKETS_RECEIVED); + pt_wait_ge(base, UCIE_RX_PACKETS_RECEIVED, r + 16, "RX packets"); + pt_wr(base, UCIE_RX_PAUSE_COUNTERS, 1); + pt_wait_cycles(64); + pt_check_errors(base, PT_MAINBAND_MASK, one_on_injected, "injected errors"); + pt_wr(base, UCIE_RX_PAUSE_COUNTERS, 0); + pt_wr(base, UCIE_TX_INJECT_LANE, 0); + reset_fsms(base); +} + +// Scored against its own history with the RX seeds wrong on every lane. +static uint64_t wrong_seed(int lane) { return pt_seed(lane + 7); } + +static void test_self_sync(uintptr_t base) { + pt_set_seeds(base, pt_seed, wrong_seed); + pt_wr(base, UCIE_RX_SELF_SYNC, 1); + pt_arm(base, UCIE_TX_TEST_MODE_LFSR, N_PACKETS); + pt_go(base, N_PACKETS); + pt_check_errors(base, PT_MAINBAND_MASK, zero, "self-synchronized run"); + // The early count is still the seeded check, so it sees the wrong seeds. + PT_CHECK(pt_errors_early(base, 0) > 0, + "seeded early count read clean against the wrong seed"); + pt_wr(base, UCIE_RX_SELF_SYNC, 0); +} + +// Valid moved onto track at both ends, the workaround for a broken valid +// lane. Track then carries valid and is scored against it. +static void test_valid_on_track(uintptr_t base) { + pt_set_seeds(base, pt_seed, pt_seed); + pt_wr(base, UCIE_TX_VALID_LANE_SEL, UCIE_TRACK_VALID_LANE_SEL); + pt_wr(base, UCIE_RX_VALID_LANE_SEL, UCIE_TRACK_VALID_LANE_SEL); + pt_arm(base, UCIE_TX_TEST_MODE_LFSR, N_PACKETS); + pt_go(base, N_PACKETS); + pt_check_errors(base, PT_MAINBAND_MASK, zero, "valid on track"); + pt_wr(base, UCIE_TX_VALID_LANE_SEL, UCIE_DEFAULT_VALID_LANE_SEL); + pt_wr(base, UCIE_RX_VALID_LANE_SEL, UCIE_DEFAULT_VALID_LANE_SEL); +} + +// Bursts with the forwarded clock gated between them, as the UCIe controller +// parks it. The RX lanes deserialize only on clock edges, so this stops and +// restarts the real receiver. +// Gating uses the UCIe reset values: no preamble, one packet of postamble. +// The clock parks high and low in turn, then always low. One packet is enough +// after the RX dividers are realigned behind the parked clock and one 40 UI +// gap moves the RX word boundary so every burst ends 23 UI into an RX word +// (ucie/docs/phytest_bringup.md, section 8). With no preamble, the first edge +// after a park must sample the burst's first data UI, so the test centres the +// forwarded clock and restores it afterwards. +static void test_gated_bursts(uintptr_t base) { + const uint64_t n = 24; + const uint64_t phase = pt_rd(base, UCIE_CLK_PHASE_SEL); + PT_CHECK_EQ("preamble reset value", pt_rd(base, UCIE_TX_CLK_PREAMBLE), 0); + PT_CHECK_EQ("postamble reset value", pt_rd(base, UCIE_TX_CLK_POSTAMBLE), + 32 / UCIE_CLK_POSTAMBLE_UI); + PT_CHECK_EQ("park alternation reset value", + pt_rd(base, UCIE_TX_CLK_PARK_ALTERNATE), 1); + pt_set_seeds(base, pt_seed, pt_seed); + pt_wr(base, UCIE_RX_BURST_MODE, 1); + for (int alternate = 1; alternate >= 0; alternate--) { + pt_wr(base, UCIE_TX_CLK_PARK_ALTERNATE, alternate); + // Gate only after `pt_arm`, because its RX datapath reset needs clock + // edges to release. Then realign the dividers behind the parked clock, so + // the next burst's first edge samples its first data UI. + pt_arm(base, UCIE_TX_TEST_MODE_LFSR, 1); + pt_wr(base, UCIE_TX_CLK_GATE, 1); + pt_wait_cycles(256); + set_clk_en(base, 0); + set_rx_clk_en(base, 0); + set_global_delay(base, PT_CENTRED_DELAY_TAPS); + reset_dividers(base); + set_clk_en(base, 1); + set_rx_clk_en(base, 1); + pt_wr(base, UCIE_TX_CLK_POSTAMBLE, 40 / UCIE_CLK_POSTAMBLE_UI); + pt_go(base, 1); + pt_wr(base, UCIE_TX_CLK_POSTAMBLE, 32 / UCIE_CLK_POSTAMBLE_UI); + + // The bursts. A clock that runs or parks for whole packets moves no word + // boundary, so the FSMs can reset on a running clock in between. + pt_wr(base, UCIE_TX_CLK_GATE, 0); + pt_wr(base, UCIE_TX_BURST_PACKETS, 6); + pt_wr(base, UCIE_TX_IDLE_PACKETS, 10); + pt_arm(base, UCIE_TX_TEST_MODE_LFSR, n); + pt_wr(base, UCIE_TX_CLK_GATE, 1); + pt_wait_cycles(256); + uint64_t w0 = pt_rd(base, UCIE_RX_IDLE_WORDS_OBSERVED); + pt_wait_cycles(1024); + PT_CHECK_EQ("idle words with the clock parked", + pt_rd(base, UCIE_RX_IDLE_WORDS_OBSERVED), w0); + pt_go(base, n); + const char *what = + alternate ? "gated bursts, parking high and low" : "gated bursts"; + pt_check_errors(base, PT_MAINBAND_MASK, zero, what); + pt_check_fifo_flags(base, what); + pt_wr(base, UCIE_TX_CLK_GATE, 0); + pt_wr(base, UCIE_TX_BURST_PACKETS, 0); + pt_wr(base, UCIE_TX_IDLE_PACKETS, 0); + } + pt_wr(base, UCIE_TX_CLK_PARK_ALTERNATE, 1); + pt_wr(base, UCIE_RX_BURST_MODE, 0); + set_clk_en(base, 0); + set_rx_clk_en(base, 0); + pt_wr(base, UCIE_CLK_PHASE_SEL, phase); + apply_clk_cfg(base); + pt_wait_clk_cfg(base); + reset_dividers(base); + set_clk_en(base, 1); + set_rx_clk_en(base, 1); + reset_fsms(base); +} + +// ===== Manual patterns and the capture SRAM ===== + +#define PERIOD 5 +static const int checked_lanes[] = {0, 9, UCIE_TRACK_LANE - 3, UCIE_VALID_LANE, + UCIE_TRACK_LANE}; +#define N_CHECKED (sizeof(checked_lanes) / sizeof(checked_lanes[0])) + +static void check_capture(uintptr_t base, int first, const char *what) { + for (unsigned i = 0; i < N_CHECKED; i++) { + int lane = checked_lanes[i]; + for (int e = 0; e < UCIE_SRAM_PACKETS; e++) { + uint32_t got = pt_captured(base, lane, e); + uint32_t want = pt_pattern_word((first + e) % PERIOD, lane); + PT_CHECK(got == want, "%s: lane %d entry %d read 0x%x, sent 0x%x", what, + lane, e, got, want); + } + } +} + +static void test_manual_capture(uintptr_t base) { + reset_fsms(base); + pt_load_pattern(base, PERIOD, pt_pattern_word); + pt_arm(base, UCIE_TX_TEST_MODE_MANUAL, UCIE_SRAM_PACKETS); + pt_go(base, UCIE_SRAM_PACKETS); + check_capture(base, 0, "manual capture"); + + // From packet `start` on, past the first lap of the SRAM. + const int start = UCIE_SRAM_PACKETS + 7; + pt_wr(base, UCIE_RX_CAPTURE_START, start); + pt_arm(base, UCIE_TX_TEST_MODE_MANUAL, start + UCIE_SRAM_PACKETS); + pt_go(base, start + UCIE_SRAM_PACKETS); + check_capture(base, start, "capture start"); + pt_wr(base, UCIE_RX_CAPTURE_START, 0); + pt_wr(base, UCIE_TX_MANUAL_REPEAT_PERIOD, 0); +} + +// An error-triggered capture. Two lanes send the same stream, so one shows +// what the other should have received. The SRAM must hold exactly the one +// flipped bit. +#define TWIN 0 +#define VICTIM 1 +static uint64_t twin_seed(int lane) { + return pt_seed(lane == VICTIM ? TWIN : lane); +} +static uint64_t one_on_victim(int lane) { return lane == VICTIM; } + +static void test_error_capture(uintptr_t base) { + const uint64_t post = 4; + pt_set_seeds(base, twin_seed, twin_seed); + pt_wr(base, UCIE_RX_CAPTURE_MODE, UCIE_CAPTURE_MODE_ERROR); + pt_wr(base, UCIE_RX_CAPTURE_POST_TRIGGER, post); + pt_wr(base, UCIE_RX_TRIGGER_LANE_MASK, PT_ALL_LANES_MASK); + pt_arm(base, UCIE_TX_TEST_MODE_LFSR, 0); + pt_go(base, 0); + pt_wait_ge(base, UCIE_RX_PACKETS_RECEIVED, 2 * UCIE_SRAM_PACKETS, + "RX packets"); + PT_CHECK_EQ("triggered before any error", + pt_rd(base, UCIE_RX_CAPTURE_TRIGGERED), 0); + // Read before the strobe: the injected packet can arrive before a read + // issued after it returns. + uint64_t r = pt_rd(base, UCIE_RX_PACKETS_RECEIVED); + pt_wr(base, UCIE_TX_INJECT_LANE, VICTIM); + pt_wr(base, UCIE_TX_INJECT_ERROR, 1); + pt_wait_ge(base, UCIE_RX_PACKETS_RECEIVED, r + 2 * UCIE_SRAM_PACKETS, + "RX packets"); + pt_wr(base, UCIE_RX_PAUSE_COUNTERS, 1); + if (pt_wait_eq(base, UCIE_RX_CAPTURE_TRIGGERED, 1, "capture trigger")) { + uint64_t t = pt_rd(base, UCIE_RX_CAPTURE_TRIGGER_PACKET); + PT_CHECK(t >= r, "trigger packet %lu is before the injection at %lu", t, r); + for (int e = 0; e < UCIE_SRAM_PACKETS; e++) { + uint32_t diff = pt_captured(base, TWIN, e) ^ pt_captured(base, VICTIM, e); + uint32_t want = (uint64_t)e == t % UCIE_SRAM_PACKETS ? 1 : 0; + PT_CHECK(diff == want, "entry %d: twin lanes differ by 0x%x, expected 0x%x", + e, diff, want); + } + } + pt_check_errors(base, PT_MAINBAND_MASK, one_on_victim, "error capture run"); + pt_wr(base, UCIE_RX_PAUSE_COUNTERS, 0); + // Back to the reset values: half the SRAM after the trigger, every lane. + pt_wr(base, UCIE_RX_CAPTURE_MODE, UCIE_CAPTURE_MODE_FIRST); + pt_wr(base, UCIE_RX_CAPTURE_POST_TRIGGER, UCIE_SRAM_PACKETS / 2); + pt_wr(base, UCIE_RX_TRIGGER_LANE_MASK, (1ULL << UCIE_NUM_TEST_LANES) - 1); + pt_wr(base, UCIE_TX_INJECT_LANE, 0); + reset_fsms(base); +} + +// The RX starting on whatever arrives, with nothing on valid. +static void test_immediate_start(uintptr_t base) { + pt_wr(base, UCIE_RX_DATA_MODE, UCIE_DATA_MODE_INFINITE); + reset_fsms(base); + pt_wait_cycles(512); + PT_CHECK_EQ("packets with the TX idle", pt_rd(base, UCIE_RX_PACKETS_RECEIVED), + 0); + pt_wr(base, UCIE_RX_START_MODE, UCIE_RX_START_MODE_IMMEDIATE); + pt_wait_ge(base, UCIE_RX_PACKETS_RECEIVED, 8, "immediate-start packets"); + pt_wr(base, UCIE_RX_START_MODE, UCIE_RX_START_MODE_VALID); + reset_fsms(base); +} + +// ===== Track ===== + +static void count_track(uintptr_t base, uint64_t word, uint64_t *ones, + uint64_t *zeros, uint64_t *even) { + pt_wr(base, UCIE_TX_TRACK_WORD, word); + pt_wait_cycles(256); + pt_wr(base, UCIE_TRACK_CNT_RST, 1); + pt_wait_cycles(2048); + pt_wr(base, UCIE_TRACK_CNT_HOLD, 1); + pt_wait_cycles(64); + *ones = pt_rd(base, UCIE_TRACK_ONES); + *zeros = pt_rd(base, UCIE_TRACK_ZEROS); + *even = pt_rd(base, UCIE_TRACK_ONES_EVEN); + pt_wr(base, UCIE_TRACK_CNT_HOLD, 0); +} + +static void test_track(uintptr_t base) { + uint64_t ones, zeros, even; + pt_wr(base, UCIE_TX_TRACK_CLK, 1); + // Sixteen ones in a row: half on even UIs whatever the RX word boundary. + count_track(base, 0x0000ffffULL, &ones, &zeros, &even); + PT_CHECK(ones > 0 && ones == zeros && 2 * even == ones, + "track 0x0000ffff: ones %lu zeros %lu even ones %lu", ones, zeros, + even); + // A clock: every one on an even UI, or every one on an odd UI. + count_track(base, UCIE_DEFAULT_TRACK, &ones, &zeros, &even); + PT_CHECK(ones > 0 && ones == zeros && (even == ones || even == 0), + "track clock: ones %lu zeros %lu even ones %lu", ones, zeros, even); + printf("[%s] track clock lands on %s UIs\n", pt_port, + even == ones ? "even" : "odd"); + pt_wr(base, UCIE_TX_TRACK_CLK, 0); + pt_wr(base, UCIE_TX_TRACK_WORD, UCIE_DEFAULT_TRACK); +} + +// ===== Loopback lane, debug lane, sideband ===== + +static uint32_t loopback_word(int row, int lane) { + // Bit 0 set on the first row: the RX frames on the first one. + if (lane == UCIE_LOOPBACK_LANE) return ((uint32_t)(row + 1) * 0x9e3779b9u) | 1; + return 0; +} + +static void test_loopback_lane(uintptr_t base) { + const int n = 2 * UCIE_SRAM_PACKETS; + reset_fsms(base); + pt_load_pattern(base, PERIOD, loopback_word); + pt_wr(base, UCIE_TEST_TARGET, UCIE_TEST_TARGET_LOOPBACK); + // Every target change needs a datapath reset; `reset_fsms` does one. + pt_wr(base, UCIE_TX_TEST_MODE, UCIE_TX_TEST_MODE_MANUAL); + pt_wr(base, UCIE_TX_DATA_MODE, UCIE_DATA_MODE_FINITE); + pt_wr(base, UCIE_RX_DATA_MODE, UCIE_DATA_MODE_FINITE); + pt_wr(base, UCIE_TX_PACKETS_TO_SEND, n); + pt_wr(base, UCIE_RX_PACKETS_TO_RECEIVE, n); + reset_fsms(base); + pt_wait_cycles(256); + pt_go(base, n); + for (int e = 0; e < UCIE_SRAM_PACKETS; e++) { + uint32_t got = pt_captured(base, UCIE_LOOPBACK_LANE, e); + uint32_t want = loopback_word(e % PERIOD, UCIE_LOOPBACK_LANE); + PT_CHECK(got == want, "loopback lane entry %d read 0x%x, sent 0x%x", e, got, + want); + } + PT_CHECK_EQ("loopback RX FIFO overflow", + pt_rd(base, UCIE_LOOPBACK_RX_FIFO_OVERFLOW), 0); + PT_CHECK_EQ("loopback TX FIFO underflow", + pt_rd(base, UCIE_LOOPBACK_TX_FIFO_UNDERFLOW), 0); + pt_wr(base, UCIE_TEST_TARGET, UCIE_TEST_TARGET_MAINBAND); + pt_wr(base, UCIE_TX_MANUAL_REPEAT_PERIOD, 0); + reset_fsms(base); +} + +// The observation bumps, which `setup_ucie` leaves off. Only this test turns +// them on and drives each: the TX data debug lane runs, `mux0` watches the TX +// divided clock, and `mux1` watches RX data lane 0, which gets traffic. The +// simulation harness checks every bump toggled; on the bench, probe them with +// a scope now. The debug lane only drives a bump, so software can check only +// that it runs to completion without starving. +static void test_observation(uintptr_t base) { + enable_debug_drivers(base, 1); + pt_wr(base, UCIE_DEBUG_MUX_SEL, UCIE_DEBUG_MUX_TX_DIV_CLK); + pt_wr(base, UCIE_DEBUG_MUX_SEL + UCIE_DEBUG_MUX_SEL_WIDTH, + UCIE_DEBUG_MUX_RX_DATA_LSB); + pt_wr(base, UCIE_DEBUG_TX_TEST_MODE, UCIE_TX_TEST_MODE_LFSR); + pt_wr(base, UCIE_DEBUG_TX_DATA_MODE, UCIE_DATA_MODE_FINITE); + pt_wr(base, UCIE_DEBUG_TX_PACKETS_TO_SEND, N_PACKETS); + pt_wr(base, UCIE_DEBUG_TX_FSM_RST, 1); + pt_wr(base, UCIE_DEBUG_TX_EXECUTE, 1); + pt_wait_eq(base, UCIE_DEBUG_TX_TEST_STATE, UCIE_TX_TEST_STATE_DONE, + "debug TX run"); + PT_CHECK_EQ("debug TX packets sent", pt_rd(base, UCIE_DEBUG_TX_PACKETS_SENT), + N_PACKETS); + PT_CHECK_EQ("debug TX FIFO underflow", + pt_rd(base, UCIE_TX_DEBUG_FIFO_UNDERFLOW), 0); + pt_wr(base, UCIE_DEBUG_TX_FSM_RST, 1); + pt_lfsr_check(base, N_PACKETS, "LFSR run with the observation drivers on"); + pt_wr(base, UCIE_DEBUG_MUX_SEL, 0); + pt_wr(base, UCIE_DEBUG_MUX_SEL + UCIE_DEBUG_MUX_SEL_WIDTH, 0); + enable_debug_drivers(base, 0); +} + +// ===== Per-lane sources and error diagnostics ===== + +#define FIXED_LANE 5 +#define INVERTED_LANE 7 + +static void set_lane_sources(uintptr_t base, int tx, int rx, uint64_t fixed, + uint64_t inverted, uint64_t word) { + if (tx) { + pt_wr(base, UCIE_TX_LANE_FIXED, fixed); + pt_wr(base, UCIE_TX_LANE_INVERT, inverted); + pt_wr(base, UCIE_TX_FIXED_WORD, word); + } + if (rx) { + pt_wr(base, UCIE_RX_LANE_FIXED, fixed); + pt_wr(base, UCIE_RX_LANE_INVERT, inverted); + pt_wr(base, UCIE_RX_FIXED_WORD, word); + } +} + +static uint64_t err_class_sum(uintptr_t base, int rise) { + return rise ? pt_rd(base, UCIE_RX_ERR_RISE_ONE) + + pt_rd(base, UCIE_RX_ERR_RISE_ZERO) + : pt_rd(base, UCIE_RX_ERR_FALL_ONE) + + pt_rd(base, UCIE_RX_ERR_FALL_ZERO); +} + +static uint64_t err_positions(uintptr_t base, int lane) { + pt_wr(base, UCIE_RX_ERR_POS_LANE, lane); + return pt_rd(base, UCIE_RX_ERR_POS_MASK); +} + +// Victims and aggressors. First one lane is held at a static level and one is +// inverted, and the RX expects the same, so the run is clean. Then the RX +// reads the inverted lane as plain, so every bit is wrong, half on each +// sampling edge. +static void test_lane_sources(uintptr_t base) { + const uint64_t fixed = 1ULL << FIXED_LANE; + const uint64_t inverted = (1ULL << INVERTED_LANE) | (1ULL << UCIE_TRACK_LANE); + set_lane_sources(base, 1, 1, fixed, inverted, 0); + pt_lfsr_check(base, N_PACKETS, "fixed and inverted lanes, RX matching"); + PT_CHECK_EQ("lanes flagged errored", pt_rd(base, UCIE_RX_LANE_ERRORED), 0); + + set_lane_sources(base, 0, 1, fixed, 1ULL << UCIE_TRACK_LANE, 0); + pt_wr(base, UCIE_RX_ERR_CLASS_LANE, INVERTED_LANE); + pt_set_seeds(base, pt_seed, pt_seed); + pt_arm(base, UCIE_TX_TEST_MODE_LFSR, N_PACKETS); + pt_go(base, N_PACKETS); + PT_CHECK_EQ("errors on the inverted lane read plain", + pt_errors(base, INVERTED_LANE), 32 * N_PACKETS); + PT_CHECK_EQ("those errors on rising-edge UIs", err_class_sum(base, 1), + 16 * N_PACKETS); + PT_CHECK_EQ("those errors on falling-edge UIs", err_class_sum(base, 0), + 16 * N_PACKETS); + PT_CHECK_EQ("lanes flagged errored", pt_rd(base, UCIE_RX_LANE_ERRORED), + 1ULL << INVERTED_LANE); + PT_CHECK_EQ("error positions on the inverted lane", + err_positions(base, INVERTED_LANE), 0xffffffffULL); + set_lane_sources(base, 1, 1, 0, 0, 0); + pt_wr(base, UCIE_RX_ERR_CLASS_LANE, 0); +} + +// One injected error, found by lane, by UI and as exactly one classified +// error. Which edge it lands on depends on where the RX framed. +static void test_error_location(uintptr_t base) { + const int lane = 3; + pt_set_seeds(base, pt_seed, pt_seed); + pt_wr(base, UCIE_RX_ERR_CLASS_LANE, lane); + pt_arm(base, UCIE_TX_TEST_MODE_LFSR, N_PACKETS); + pt_wr(base, UCIE_TX_INJECT_LANE, lane); + pt_wr(base, UCIE_TX_INJECT_ERROR, 1); + pt_go(base, N_PACKETS); + PT_CHECK_EQ("injected error count", pt_errors(base, lane), 1); + PT_CHECK_EQ("lanes flagged errored", pt_rd(base, UCIE_RX_LANE_ERRORED), + 1ULL << lane); + // Bit 0 of the packet, the bit injection flips. + PT_CHECK_EQ("error positions", err_positions(base, lane), 1); + PT_CHECK_EQ("classified errors", + err_class_sum(base, 1) + err_class_sum(base, 0), 1); + pt_wr(base, UCIE_RX_ERR_CLASS_LANE, 0); + pt_wr(base, UCIE_TX_INJECT_LANE, 0); +} + +// The clock meters, which read the harness's clocks: an 8 GHz main clock +// (500 MHz packet clocks), and the PHY test and sideband clocks on their +// bypass pins at 500 and 800 MHz. +static void test_meters(uintptr_t base) { + pt_check_meter(base, UCIE_CLK_METER_PHY_TEST, 500, "PHY test"); + pt_check_meter(base, UCIE_CLK_METER_SB, 800, "sideband"); + pt_check_meter(base, UCIE_CLK_METER_TX_DIV, 500, "TX divided"); + pt_check_meter(base, UCIE_CLK_METER_RX_DIV, 500, "RX divided"); + PT_CHECK_EQ("controller RX FIFO overflow", + pt_rd(base, UCIE_CONTROLLER_RX_FIFO_OVERFLOW), 0); + PT_CHECK_EQ("PLL lock lost", pt_rd(base, UCIE_PLL_LOCK_LOST), 0); +} + +static void test_sideband(uintptr_t base) { + const uint64_t packets[] = {0x0123456789abcdefULL, 0xfedcba9876543210ULL}; + pt_sb_reset(base); + for (int i = 0; i < 2; i++) pt_sb_send(base, packets[i]); + for (int i = 0; i < 2; i++) { + uint64_t got; + if (pt_sb_recv(base, &got)) PT_CHECK_EQ("sideband packet", got, packets[i]); + } + pt_wait_cycles(64); + PT_CHECK_EQ("sideband RX empty", pt_rd(base, UCIE_SB_RX_VALID), 0); + PT_CHECK_EQ("sideband RX overflow", pt_rd(base, UCIE_SB_RX_OVERFLOW), 0); +} + +typedef struct { + const char *name; + void (*fn)(uintptr_t); +} test_t; + +static const test_t tests[] = { + {"clock meters", test_meters}, + {"LFSR run", test_lfsr}, + {"error injection", test_inject}, + {"self-synchronized check", test_self_sync}, + {"valid moved onto track", test_valid_on_track}, + {"gated-clock bursts", test_gated_bursts}, + {"manual pattern and capture", test_manual_capture}, + {"error-triggered capture", test_error_capture}, + {"immediate start", test_immediate_start}, + {"track counters", test_track}, + {"loopback lane", test_loopback_lane}, + {"per-lane pattern sources", test_lane_sources}, + {"error location and class", test_error_location}, + {"observation bumps and the TX debug lane", test_observation}, + {"sideband", test_sideband}, +}; + +int main(void) { + const struct { + const char *name; + uintptr_t base; + } ports[] = {{"ucie0", UCIE0_REG_BASE}, {"ucie1", UCIE1_REG_BASE}}; + + for (int p = 0; p < 2; p++) { + pt_begin(ports[p].name, "setup"); + pt_setup(ports[p].base); + for (unsigned t = 0; t < sizeof(tests) / sizeof(tests[0]); t++) { + pt_begin(ports[p].name, tests[t].name); + tests[t].fn(ports[p].base); + } + } + pt_finish("phytest-loopback"); + return 0; +} diff --git a/software/phytest-two-chip.c b/software/phytest-two-chip.c new file mode 100644 index 0000000..e623652 --- /dev/null +++ b/software/phytest-two-chip.c @@ -0,0 +1,149 @@ +// PhyTest between two chips, each running this program. UCIe1 of each chip +// is wired to UCIe0 of the other: the two-chip ring in simulation, or two +// chips on a package. +// +// Each direction is tested in its own phase. A chip starts a TX only after +// the partner reports, over the sideband of the same link, that the RX it +// feeds is armed. Each report is resent until the partner acknowledges it, so +// the chips may start at different times. The programs need nothing else to +// stay in step: no shared clock, no host, no matching code timing. +// +// my UCIe1 TX --> partner UCIe0 RX my UCIe0 sideband --> partner UCIe1 +// my UCIe0 TX --> partner UCIe1 RX my UCIe1 sideband --> partner UCIe0 + +#include "phytest.h" + +#define N_PACKETS 64 +#define PERIOD 5 +#define N_MANUAL (8 * PERIOD) + +#define ARMED(phase) (0x5054000041524d00ULL | (phase)) +#define ACKED(phase) (0x5054000041434b00ULL | (phase)) +#define IS_MESSAGE(p) \ + (((p) & ~0xffULL) == ARMED(0) || ((p) & ~0xffULL) == ACKED(0)) + +// Core cycles between resends of an unacknowledged message, and the longest +// a handshake waits for the partner. A message sent before the partner has +// set up its link is lost, so only a resend reaches a partner that started +// late. +#define RESEND_CYCLES 50000ULL +#define HANDSHAKE_TIMEOUT (1000 * PT_TIMEOUT) + +// Pops a sideband packet from `base` if one is waiting. A packet that isn't +// one of ours means the receiver came out of reset mid-packet and counts +// bits out of step, so realign it. The sender's next resend gets through. +static int sb_poll(uintptr_t base, uint64_t *packet) { + if (!pt_rd(base, UCIE_SB_RX_VALID)) return 0; + *packet = pt_rd(base, UCIE_SB_RX_PACKET); + pt_wr(base, UCIE_SB_RX_POP, 1); + if (IS_MESSAGE(*packet)) return 1; + pt_wr(base, UCIE_SB_RX_RST, 1); + return 0; +} + +// Tells the partner, on the sideband of `rx`'s link, that `rx` is armed, and +// resends until the partner acknowledges on that link. Meanwhile waits on the +// sideband of `tx`'s link for the partner to say the RX that `tx` feeds is +// armed, and acknowledges every copy. Done when both have happened. A packet +// for another phase is a late copy, or the partner running ahead. It is +// dropped; if it matters, a resend brings it back. +static int handshake(uintptr_t rx, uintptr_t tx, uint64_t phase) { + int acked = 0, armed = 0, sent = 0; + uint64_t start = rdcycle(), last_send = 0, got; + while (!(acked && armed)) { + uint64_t now = rdcycle(); + if (now - start > HANDSHAKE_TIMEOUT) { + PT_CHECK(0, "partner never %s phase %lu", + armed ? "acknowledged" : "armed its RX for", phase); + return 0; + } + if (!acked && (!sent || now - last_send > RESEND_CYCLES)) { + if (!pt_sb_send(rx, ARMED(phase))) return 0; + last_send = now; + sent = 1; + } + if (sb_poll(rx, &got) && got == ACKED(phase)) acked = 1; + if (sb_poll(tx, &got) && got == ARMED(phase)) { + armed = 1; + if (!pt_sb_send(tx, ACKED(phase))) return 0; + } + } + return 1; +} + +// One direction: an LFSR run from `tx` here to the partner, while `rx` here +// takes the partner's run the same way round. +static void lfsr_phase(uintptr_t tx, uintptr_t rx, uintptr_t idle_rx, + uint64_t phase, const char *what) { + pt_set_seeds(tx, pt_seed, pt_seed); + pt_set_seeds(rx, pt_seed, pt_seed); + pt_arm(rx, UCIE_TX_TEST_MODE_LFSR, N_PACKETS); + pt_arm(tx, UCIE_TX_TEST_MODE_LFSR, N_PACKETS); + if (!handshake(rx, tx, phase)) return; + pt_wr(tx, UCIE_TX_EXECUTE, 1); + pt_wait_eq(tx, UCIE_TX_TEST_STATE, UCIE_TX_TEST_STATE_DONE, "TX run"); + pt_wait_ge(rx, UCIE_RX_PACKETS_RECEIVED, N_PACKETS, "RX packets"); + PT_CHECK_EQ("RX packets received", pt_rd(rx, UCIE_RX_PACKETS_RECEIVED), + N_PACKETS); + pt_check_errors(rx, PT_MAINBAND_MASK, pt_zero, what); + pt_check_fifo_flags(rx, what); + pt_check_fifo_flags(tx, what); + // The other link carried nothing this phase, so its RX framed nothing: + // each RX hears only the TX it is wired to. + PT_CHECK_EQ("packets on the link with no traffic", + pt_rd(idle_rx, UCIE_RX_PACKETS_RECEIVED), 0); +} + +// The reference the RX scores against: the partner's pattern, with one word +// wrong by two bits on purpose, so the check has to find exactly those. +#define WRONG_ROW 3 +#define WRONG_LANE 2 +static uint32_t reference_word(int row, int lane) { + uint32_t w = pt_pattern_word(row, lane); + return row == WRONG_ROW && lane == WRONG_LANE ? w ^ 0x5 : w; +} +static uint64_t manual_errors(int lane) { + return lane == WRONG_LANE ? 2 * (N_MANUAL / PERIOD) : 0; +} + +// A manual pattern from UCIe1 here, scored by the partner's UCIe0 against +// the copy in its own TX pattern SRAM. +static void manual_check_phase(uintptr_t tx, uintptr_t rx, uint64_t phase) { + reset_fsms(tx); + reset_fsms(rx); + pt_load_pattern(tx, PERIOD, pt_pattern_word); + pt_load_pattern(rx, PERIOD, reference_word); + pt_wr(rx, UCIE_RX_MANUAL_CHECK, 1); + pt_arm(rx, UCIE_TX_TEST_MODE_MANUAL, N_MANUAL); + pt_arm(tx, UCIE_TX_TEST_MODE_MANUAL, N_MANUAL); + if (handshake(rx, tx, phase)) { + pt_wr(tx, UCIE_TX_EXECUTE, 1); + pt_wait_eq(tx, UCIE_TX_TEST_STATE, UCIE_TX_TEST_STATE_DONE, "TX run"); + pt_wait_ge(rx, UCIE_RX_PACKETS_RECEIVED, N_MANUAL, "RX packets"); + pt_wait_cycles(256); + pt_check_errors(rx, PT_MAINBAND_MASK, manual_errors, "manual pattern check"); + } + pt_wr(rx, UCIE_RX_MANUAL_CHECK, 0); + pt_wr(rx, UCIE_TX_MANUAL_REPEAT_PERIOD, 0); + pt_wr(tx, UCIE_TX_MANUAL_REPEAT_PERIOD, 0); +} + +int main(void) { + const uintptr_t u0 = UCIE0_REG_BASE, u1 = UCIE1_REG_BASE; + + pt_begin("both", "setup"); + pt_setup(u0); + pt_setup(u1); + + pt_begin("ucie1->partner ucie0", "LFSR"); + lfsr_phase(u1, u0, u1, 1, "LFSR into ucie0"); + + pt_begin("ucie0->partner ucie1", "LFSR"); + lfsr_phase(u0, u1, u0, 2, "LFSR into ucie1"); + + pt_begin("ucie1->partner ucie0", "manual pattern check"); + manual_check_phase(u1, u0, 3); + + pt_finish("phytest-two-chip"); + return 0; +} diff --git a/software/phytest.h b/software/phytest.h new file mode 100644 index 0000000..cf7f891 --- /dev/null +++ b/software/phytest.h @@ -0,0 +1,328 @@ +// PhyTest bring-up helpers, on top of the generated ucie.h. +// +// For the lab and for simulation. Every wait is bounded. A failed check +// prints what it expected and what it read, then continues, so one run +// reports every problem. A program calls `pt_finish()` at the end; it exits +// nonzero if any check failed. + +#ifndef __PHYTEST_H__ +#define __PHYTEST_H__ + +#include +#include +#include +#include +#include "mmio.h" +#include "ucie.h" + +#define UCIE0_REG_BASE 0x200000UL +#define UCIE1_REG_BASE 0x208000UL + +// Lane groups of four lanes each in the TX pattern SRAM. +#define PT_LANE_GROUPS ((UCIE_NUM_TEST_LANES + 3) / 4) +// The mainband lanes a run scores: the data lanes, valid and track. The +// loopback lane is only scored with `UCIE_TEST_TARGET_LOOPBACK`. +#define PT_MAINBAND_LANES (UCIE_TRACK_LANE + 1) +#define PT_ALL_LANES_MASK ((1ULL << UCIE_NUM_TEST_LANES) - 1) + +// Bound on every wait, in core cycles, not reads: on silicon an MMIO read is +// fast next to a slow divided clock. The bound is generous. +#define PT_TIMEOUT 2000000ULL + +// The chip clock the UCIe clock register block runs on, in MHz: the rate the +// clock meters count against. 500 MHz on Iris out of reset. +#ifndef PT_CHIP_MHZ +#define PT_CHIP_MHZ 500ULL +#endif + +// Global delay taps that centre the forwarded clock on the data of a +// looped-back link at 16 GT/s on the Iris harness (as in the UCIe repo's tests +// and `setup_ucie_digital`). The reset value, the longest delay, works for a +// free-running clock but leaves a gated burst's first data UI with no edge. +// On silicon, override it with the code mainband training finds. +#ifndef PT_CENTRED_DELAY_TAPS +#define PT_CENTRED_DELAY_TAPS 51ULL +#endif + +static int pt_failures = 0; +static const char *pt_port = ""; + +#define PT_CHECK(cond, ...) \ + do { \ + if (!(cond)) { \ + printf("FAIL [%s] %s:%d: ", pt_port, __FILE__, __LINE__); \ + printf(__VA_ARGS__); \ + printf("\n"); \ + pt_failures++; \ + } \ + } while (0) + +#define PT_CHECK_EQ(what, got, want) \ + do { \ + uint64_t _g = (got), _w = (want); \ + PT_CHECK(_g == _w, "%s: read 0x%lx, expected 0x%lx", (what), _g, _w); \ + } while (0) + +static inline uint64_t pt_rd(uintptr_t base, uint64_t reg) { + return reg_read64(base + reg); +} + +static inline void pt_wr(uintptr_t base, uint64_t reg, uint64_t v) { + reg_write64(base + reg, v); +} + +static inline void pt_wait_cycles(uint64_t n) { + uint64_t start = rdcycle(); + while (rdcycle() - start < n) { + } +} + +// Polls `reg` until it reads at least `want`. Returns whether it got there. +static inline int pt_wait_ge(uintptr_t base, uint64_t reg, uint64_t want, + const char *what) { + uint64_t start = rdcycle(); + uint64_t v; + while ((v = pt_rd(base, reg)) < want) { + if (rdcycle() - start > PT_TIMEOUT) { + PT_CHECK(0, "timed out waiting for %s: read %lu, wanted %lu", what, v, + want); + return 0; + } + } + return 1; +} + +static inline int pt_wait_eq(uintptr_t base, uint64_t reg, uint64_t want, + const char *what) { + uint64_t start = rdcycle(); + uint64_t v; + while ((v = pt_rd(base, reg)) != want) { + if (rdcycle() - start > PT_TIMEOUT) { + PT_CHECK(0, "timed out waiting for %s: read 0x%lx, wanted 0x%lx", what, + v, want); + return 0; + } + } + return 1; +} + +// Waits for `n` more RX idle words, which shows the RX datapath is out of +// reset and a forwarded clock is arriving. The counter is a free-running +// 32-bit count, so the difference is taken modulo 2^32. +static inline int pt_wait_idle_words(uintptr_t base, uint32_t n) { + uint32_t w0 = (uint32_t)pt_rd(base, UCIE_RX_IDLE_WORDS_OBSERVED); + uint64_t start = rdcycle(); + while ((uint32_t)((uint32_t)pt_rd(base, UCIE_RX_IDLE_WORDS_OBSERVED) - w0) < + n) { + if (rdcycle() - start > PT_TIMEOUT) { + PT_CHECK(0, "no idle words arriving: RX datapath held, or no clock"); + return 0; + } + } + return 1; +} + +// Waits for the clocking block to finish an apply. +static inline int pt_wait_clk_cfg(uintptr_t base) { + return pt_wait_eq(base, UCIE_CLK_CFG_BUSY, 0, "clock config apply"); +} + +// ===== Seeds ===== + +// Distinct per lane, so a lane scored against another lane's reference, or +// two lanes swapped anywhere between the TX and the RX, reads as errors. +static inline uint64_t pt_seed(int lane) { + return UCIE_TRAIN_LFSR_SEED + (uint64_t)lane * 0x1111ULL; +} + +static inline void pt_set_seeds(uintptr_t base, uint64_t (*tx)(int), + uint64_t (*rx)(int)) { + for (int lane = 0; lane < UCIE_NUM_TEST_LANES; lane++) { + pt_wr(base, UCIE_TX_LFSR_SEED + lane * UCIE_TX_LFSR_SEED_WIDTH, tx(lane)); + pt_wr(base, UCIE_RX_LFSR_SEED + lane * UCIE_RX_LFSR_SEED_WIDTH, rx(lane)); + } +} + +// ===== Error counts ===== + +static inline uint64_t pt_errors(uintptr_t base, int lane) { + return pt_rd(base, UCIE_RX_BIT_ERRORS + lane * UCIE_RX_BIT_ERRORS_WIDTH); +} + +static inline uint64_t pt_errors_early(uintptr_t base, int lane) { + return pt_rd(base, + UCIE_RX_BIT_ERRORS_EARLY + lane * UCIE_RX_BIT_ERRORS_EARLY_WIDTH); +} + +// Checks the nominal count of every lane in `mask` against `want(lane)`. +static inline void pt_check_errors(uintptr_t base, uint64_t mask, + uint64_t (*want)(int), const char *what) { + for (int lane = 0; lane < UCIE_NUM_TEST_LANES; lane++) { + if (!((mask >> lane) & 1)) continue; + uint64_t got = pt_errors(base, lane), w = want ? want(lane) : 0; + PT_CHECK(got == w, "%s: lane %d counted %lu errors, expected %lu", what, + lane, got, w); + } +} + +#define PT_MAINBAND_MASK ((1ULL << PT_MAINBAND_LANES) - 1) + +// The crossings between the PHY test clock and the lane clocks. Either flag +// set means words were dropped, which reads like bit errors. +static inline void pt_check_fifo_flags(uintptr_t base, const char *what) { + PT_CHECK(pt_rd(base, UCIE_TX_TEST_FIFO_UNDERFLOW) == 0, + "%s: TX test FIFO underflowed", what); + PT_CHECK(pt_rd(base, UCIE_RX_TEST_FIFO_OVERFLOW) == 0, + "%s: RX test FIFO overflowed", what); +} + +// ===== Clock meters ===== + +// Reads a clock meter (`UCIE_CLK_METER_*`) in MHz, given the chip clock the +// meters count against. The meters respond in any link state, so this works +// before the PhyTest registers do. +static inline uint64_t pt_meter_mhz(uintptr_t base, uint64_t reg, + uint64_t chip_mhz) { + pt_wait_cycles(4 * UCIE_CLK_METER_WINDOW_CYCLES); + return pt_rd(base, reg) * chip_mhz / UCIE_CLK_METER_WINDOW_CYCLES; +} + +// Checks a meter reads `mhz` to within 2%. +static inline void pt_check_meter(uintptr_t base, uint64_t reg, uint64_t mhz, + const char *what) { + uint64_t got = pt_meter_mhz(base, reg, PT_CHIP_MHZ); + PT_CHECK(got * 50 >= mhz * 49 && got * 50 <= mhz * 51, + "%s clock meter reads %lu MHz, expected %lu MHz", what, got, mhz); +} + +// ===== Runs ===== + +// Programs a finite mainband run of `n` packets each way, then resets both +// FSMs, which reseeds the LFSRs. It starts nothing. Set options that need the +// RX datapath out of reset (`txClkGate`) between this and `pt_go`. +static inline void pt_arm(uintptr_t base, uint64_t mode, uint64_t n) { + pt_wr(base, UCIE_TEST_TARGET, UCIE_TEST_TARGET_MAINBAND); + pt_wr(base, UCIE_TX_TEST_MODE, mode); + pt_wr(base, UCIE_TX_DATA_MODE, + n ? UCIE_DATA_MODE_FINITE : UCIE_DATA_MODE_INFINITE); + pt_wr(base, UCIE_RX_DATA_MODE, + n ? UCIE_DATA_MODE_FINITE : UCIE_DATA_MODE_INFINITE); + pt_wr(base, UCIE_TX_PACKETS_TO_SEND, n); + pt_wr(base, UCIE_RX_PACKETS_TO_RECEIVE, n); + reset_fsms(base); + pt_wait_idle_words(base, 4); +} + +// Starts the TX, and for a finite run waits for both ends to finish. +static inline int pt_go(uintptr_t base, uint64_t n) { + pt_wr(base, UCIE_TX_EXECUTE, 1); + if (!n) return 1; + int ok = pt_wait_eq(base, UCIE_TX_TEST_STATE, UCIE_TX_TEST_STATE_DONE, + "TX run to finish"); + ok &= pt_wait_ge(base, UCIE_RX_PACKETS_RECEIVED, n, "RX packets"); + PT_CHECK_EQ("TX packets sent", pt_rd(base, UCIE_TX_PACKETS_SENT), n); + PT_CHECK_EQ("RX packets received", pt_rd(base, UCIE_RX_PACKETS_RECEIVED), n); + return ok; +} + +static inline uint64_t pt_zero(int lane) { return 0; } + +// A finite LFSR run with distinct seeds, expected clean on every mainband +// lane and with no words dropped at either crossing. +static inline void pt_lfsr_check(uintptr_t base, uint64_t n, const char *what) { + pt_set_seeds(base, pt_seed, pt_seed); + pt_arm(base, UCIE_TX_TEST_MODE_LFSR, n); + pt_go(base, n); + pt_check_errors(base, PT_MAINBAND_MASK, pt_zero, what); + pt_check_fifo_flags(base, what); +} + +// ===== Pattern SRAM and capture SRAM ===== + +static inline void pt_write_chunk(uintptr_t base, int group, int row, + const uint32_t w[4]) { + pt_wr(base, UCIE_TX_DATA_LANE_GROUP, group); + pt_wr(base, UCIE_TX_DATA_OFFSET, row); + pt_wr(base, UCIE_TX_DATA_CHUNK_IN0, ((uint64_t)w[1] << 32) | w[0]); + pt_wr(base, UCIE_TX_DATA_CHUNK_IN1, ((uint64_t)w[3] << 32) | w[2]); + pt_wr(base, UCIE_TX_WRITE_CHUNK, 1); +} + +// Loads `rows` rows of `word(row, lane)` into the TX pattern SRAM and sets +// the repeat period. The TX FSM has to be idle: it only takes writes there. +static inline void pt_load_pattern(uintptr_t base, int rows, + uint32_t (*word)(int, int)) { + for (int r = 0; r < rows; r++) { + for (int g = 0; g < PT_LANE_GROUPS; g++) { + uint32_t w[4]; + for (int i = 0; i < 4; i++) { + int lane = 4 * g + i; + w[i] = lane < UCIE_NUM_TEST_LANES ? word(r, lane) : 0; + } + pt_write_chunk(base, g, r, w); + } + } + pt_wr(base, UCIE_TX_MANUAL_REPEAT_PERIOD, rows); +} + +static inline uint32_t pt_captured(uintptr_t base, int lane, int entry) { + pt_wr(base, UCIE_RX_DATA_LANE, lane); + pt_wr(base, UCIE_RX_DATA_OFFSET, entry); + return (uint32_t)pt_rd(base, UCIE_RX_DATA_CHUNK); +} + +// A manual pattern word, distinct per row and lane, with the framing waveform +// on valid. The loopback lane's slot is zero because a mainband run reads +// that lane as zero. A loopback-lane run overrides it. +static inline uint32_t pt_pattern_word(int row, int lane) { + if (lane == UCIE_VALID_LANE) return (uint32_t)UCIE_DEFAULT_VALID; + if (lane == UCIE_LOOPBACK_LANE) return 0; + return (uint32_t)(row + 1) * 0x9e3779b9u + 7u * (uint32_t)lane; +} + +// ===== Sideband ===== + +static inline void pt_sb_reset(uintptr_t base) { + pt_wr(base, UCIE_SIDEBAND_MODE, UCIE_BAND_MODE_MANUAL); + pt_wr(base, UCIE_SB_RX_RST, 1); + pt_wait_cycles(64); +} + +static inline int pt_sb_send(uintptr_t base, uint64_t packet) { + if (!pt_wait_eq(base, UCIE_SB_TX_BUSY, 0, "sideband TX idle")) return 0; + pt_wr(base, UCIE_SB_TX_PACKET, packet); + pt_wr(base, UCIE_SB_TX_SEND, 1); + return 1; +} + +// Waits for a sideband packet and pops it. Returns 0 on a timeout. +static inline int pt_sb_recv(uintptr_t base, uint64_t *packet) { + if (!pt_wait_eq(base, UCIE_SB_RX_VALID, 1, "sideband packet")) return 0; + *packet = pt_rd(base, UCIE_SB_RX_PACKET); + pt_wr(base, UCIE_SB_RX_POP, 1); + return 1; +} + +// ===== Setup ===== + +// `setup_ucie` with the sideband receiver cleared, so that a program starts +// from a known state on both bands. +static inline void pt_setup(uintptr_t base) { + setup_ucie(base); + pt_sb_reset(base); +} + +static inline void pt_begin(const char *port, const char *test) { + pt_port = port; + printf("[%s] %s (cycle %lu)\n", port, test, rdcycle()); +} + +static inline void pt_finish(const char *program) { + if (pt_failures) { + printf("%s: %d check(s) FAILED\n", program, pt_failures); + exit(1); + } + printf("%s: all checks passed\n", program); +} + +#endif diff --git a/software/ucie-digital-loopback.c b/software/ucie-digital-loopback.c index 379ca75..bf48318 100644 --- a/software/ucie-digital-loopback.c +++ b/software/ucie-digital-loopback.c @@ -8,6 +8,12 @@ #include "router.h" #include "ucie.h" +// The forwarded clock's global delay, centred on the data: the code training +// found on silicon, 51 taps on the simulation harness. +#ifndef CENTRED_DELAY_TAPS +#define CENTRED_DELAY_TAPS 51ULL +#endif + #define UCIE0_REG_BASE 0x200000UL #define UCIE1_REG_BASE 0x208000UL #define ROUTER_MMIO 0x4000 @@ -52,9 +58,15 @@ int main(void) { printf("Got chip ID: %d\n", chip_id); printf("Setting up UCIe0 (digital controller)\n"); + set_global_delay(UCIE0_REG_BASE, CENTRED_DELAY_TAPS); setup_ucie_digital(UCIE0_REG_BASE); + printf("UCIe0 RX lanes held back %ld UI to align words\n", + reg_read64(UCIE0_REG_BASE + UCIE_RX_UI_DELAY)); printf("Setting up UCIe1 (digital controller)\n"); + set_global_delay(UCIE1_REG_BASE, CENTRED_DELAY_TAPS); setup_ucie_digital(UCIE1_REG_BASE); + printf("UCIe1 RX lanes held back %ld UI to align words\n", + reg_read64(UCIE1_REG_BASE + UCIE_RX_UI_DELAY)); printf("Testing UCIe at port 0\n"); program_router(0, 1, 0); diff --git a/software/ucie.h b/software/ucie.h index 05cd280..7a82da4 100644 --- a/software/ucie.h +++ b/software/ucie.h @@ -89,7 +89,7 @@ #define UCIE_DEBUG_MUX_SEL 0x3700ULL #define UCIE_DEBUG_MUX_SEL_WIDTH 0x8ULL #define UCIE_TX_VALID 0x3710ULL -#define UCIE_RX_LFSR_VALID 0x3718ULL +#define UCIE_RX_VALID 0x3718ULL #define UCIE_CONTROLLER_SEL 0x3720ULL #define UCIE_MAINBAND_MODE 0x3728ULL #define UCIE_SIDEBAND_MODE 0x3730ULL @@ -106,6 +106,63 @@ #define UCIE_SB_RX_OVERFLOW 0x3788ULL #define UCIE_SB_RX_RST 0x3790ULL #define UCIE_SB_TL_RX_OVERFLOW 0x3798ULL +#define UCIE_RX_START_MODE 0x37a0ULL +#define UCIE_RX_BURST_MODE 0x37a8ULL +#define UCIE_RX_SELF_SYNC 0x37b0ULL +#define UCIE_RX_MONITOR 0x37b8ULL +#define UCIE_RX_CAPTURE_MODE 0x37c0ULL +#define UCIE_RX_CAPTURE_POST_TRIGGER 0x37c8ULL +#define UCIE_RX_TRIGGER_LANE_MASK 0x37d0ULL +#define UCIE_RX_CAPTURE_TRIGGERED 0x37d8ULL +#define UCIE_RX_CAPTURE_TRIGGER_PACKET 0x37e0ULL +#define UCIE_TX_BURST_PACKETS 0x37e8ULL +#define UCIE_TX_IDLE_PACKETS 0x37f0ULL +#define UCIE_TX_CLK_GATE 0x37f8ULL +#define UCIE_TX_CLK_PREAMBLE 0x3800ULL +#define UCIE_TX_CLK_POSTAMBLE 0x3808ULL +#define UCIE_TX_TRACK_CLK 0x3810ULL +#define UCIE_TX_INJECT_ERROR 0x3818ULL +#define UCIE_TX_INJECT_LANE 0x3820ULL +#define UCIE_TRACK_ONES 0x3828ULL +#define UCIE_TRACK_ZEROS 0x3830ULL +#define UCIE_TRACK_CNT_RST 0x3838ULL +#define UCIE_TRACK_CNT_HOLD 0x3840ULL +#define UCIE_TX_CLK_DIRECT 0x3848ULL +#define UCIE_TX_TEST_FIFO_UNDERFLOW 0x3850ULL +#define UCIE_RX_TEST_FIFO_OVERFLOW 0x3858ULL +#define UCIE_LOOPBACK_RX_FIFO_OVERFLOW 0x3860ULL +#define UCIE_TX_TRACK_WORD 0x3868ULL +#define UCIE_RX_CAPTURE_START 0x3870ULL +#define UCIE_LOOPBACK_TX_FIFO_UNDERFLOW 0x3878ULL +#define UCIE_TX_DEBUG_FIFO_UNDERFLOW 0x3880ULL +#define UCIE_TRACK_ONES_EVEN 0x3888ULL +#define UCIE_RX_MANUAL_CHECK 0x3890ULL +#define UCIE_TX_LANE_FIXED 0x3898ULL +#define UCIE_TX_LANE_INVERT 0x38a0ULL +#define UCIE_TX_FIXED_WORD 0x38a8ULL +#define UCIE_RX_LANE_FIXED 0x38b0ULL +#define UCIE_RX_LANE_INVERT 0x38b8ULL +#define UCIE_RX_FIXED_WORD 0x38c0ULL +#define UCIE_RX_LANE_ERRORED 0x38c8ULL +#define UCIE_RX_ERR_POS_LANE 0x38d0ULL +#define UCIE_RX_ERR_POS_MASK 0x38d8ULL +#define UCIE_RX_ERR_CLASS_LANE 0x38e0ULL +#define UCIE_RX_ERR_RISE_ONE 0x38e8ULL +#define UCIE_RX_ERR_RISE_ZERO 0x38f0ULL +#define UCIE_RX_ERR_FALL_ONE 0x38f8ULL +#define UCIE_RX_ERR_FALL_ZERO 0x3900ULL +#define UCIE_CONTROLLER_RX_FIFO_OVERFLOW 0x3908ULL +#define UCIE_TX_QUEUE_LIMIT 0x3910ULL +#define UCIE_TX_CLK_PARK_ALTERNATE 0x3918ULL +#define UCIE_RX_FRAME_OFFSET 0x3920ULL +#define UCIE_TX_UI_DELAY 0x3928ULL +#define UCIE_TX_UI_DELAY_WIDTH 0x8ULL +#define UCIE_RX_UI_DELAY 0x39c8ULL +#define UCIE_RX_UI_DELAY_WIDTH 0x8ULL +#define UCIE_RX_VALID_START_THRESHOLD 0x3a58ULL +#define UCIE_DEBUG_TXCTL_SAMPLE_NEGEDGE 0x3a60ULL +#define UCIE_RX_AUTO_SEED 0x3a68ULL +#define UCIE_TL_RX_FIFO_OVERFLOW 0x3a70ULL #define UCIE_PCIE_EXT_CAP_ID 0x4000ULL #define UCIE_DVSEC_VENDOR_ID 0x4004ULL #define UCIE_DVSEC_ID 0x4008ULL @@ -181,9 +238,10 @@ #define UCIE_VENDOR_DEBUG_UNLOCK 0x7010ULL #define UCIE_VENDOR_FORCE_LINK_ACTIVE 0x7014ULL #define UCIE_VENDOR_TS_OVERRIDE_EN 0x7018ULL -#define UCIE_UCIE_LINK_RESET 0x3c00ULL -#define UCIE_UCIE_PWR_GOOD 0x3c08ULL -#define UCIE_UCIE_RETRY_TRAINING_AMT 0x3c10ULL +#define UCIE_LINK_RESET 0x3c00ULL +#define UCIE_PWR_GOOD 0x3c08ULL +#define UCIE_RETRY_TRAINING_AMT 0x3c10ULL +#define UCIE_CONTROLLER_RST 0x3c18ULL #define UCIE_MAIN_CLK_SEL 0x10000ULL #define UCIE_PLL8_EN 0x10008ULL #define UCIE_PLL12_EN 0x10010ULL @@ -206,7 +264,7 @@ #define UCIE_CLK_CFG_UNGATE_DELAY 0x10098ULL #define UCIE_CLK_CFG_UNGATE_REQ 0x100a0ULL #define UCIE_PLL_LOCK_OBSERVED 0x100a8ULL -#define UCIE_UCIE_RST 0x100b0ULL +#define UCIE_RST 0x100b0ULL #define UCIE_PHY_TEST_CLK_SRC_SEL 0x100b8ULL #define UCIE_SB_CLK_SRC_SEL 0x100c0ULL #define UCIE_TX_DIVIDER_RST 0x100c8ULL @@ -219,7 +277,32 @@ #define UCIE_MANUAL_CLKS_READY 0x10100ULL #define UCIE_MANUAL_CLKS_READY_FREQ_SEL 0x10108ULL #define UCIE_MANUAL_CLKS_UNGATED 0x10110ULL -#define UCIE_RATE_CFG 0x10180ULL +#define UCIE_CLK_METER_PHY_TEST 0x10118ULL +#define UCIE_CLK_METER_SB 0x10120ULL +#define UCIE_CLK_METER_TX_DIV 0x10128ULL +#define UCIE_CLK_METER_RX_DIV 0x10130ULL +#define UCIE_PLL_LOCK_LOST 0x10138ULL +#define UCIE_CLK_GATE_EDGE_SEL 0x10140ULL +#define UCIE_CLK_PHASE_AUTO_APPLY 0x10148ULL +#define UCIE_PLL8_AUTO_EN 0x10150ULL +#define UCIE_PLL8_KP 0x10158ULL +#define UCIE_PLL8_KI 0x10160ULL +#define UCIE_PLL8_CODE_INIT 0x10168ULL +#define UCIE_PLL8_CODE_BYP 0x10170ULL +#define UCIE_PLL8_DBIAS 0x10178ULL +#define UCIE_PLL12_AUTO_EN 0x10180ULL +#define UCIE_PLL12_KP 0x10188ULL +#define UCIE_PLL12_KI 0x10190ULL +#define UCIE_PLL12_CODE_INIT 0x10198ULL +#define UCIE_PLL12_CODE_BYP 0x101a0ULL +#define UCIE_PLL12_DBIAS 0x101a8ULL +#define UCIE_PLL8_DCOARSE 0x101b0ULL +#define UCIE_PLL8_CODE_FULL 0x101b8ULL +#define UCIE_PLL8_CODE_INT 0x101c0ULL +#define UCIE_PLL12_CODE_FULL 0x101c8ULL +#define UCIE_PLL12_CODE_INT 0x101d0ULL +#define UCIE_CLK_CFG_GATE_DWELL 0x101d8ULL +#define UCIE_RATE_CFG 0x10200ULL #define UCIE_RATE_CFG_WIDTH 0x8ULL // === Constants === @@ -236,21 +319,38 @@ #define UCIE_CONTROLLER_SEL_UCIE 0x1ULL #define UCIE_BAND_MODE_MANUAL 0x0ULL #define UCIE_BAND_MODE_TL 0x1ULL +#define UCIE_RX_START_MODE_VALID 0x0ULL +#define UCIE_RX_START_MODE_IMMEDIATE 0x1ULL +#define UCIE_CAPTURE_MODE_FIRST 0x0ULL +#define UCIE_CAPTURE_MODE_ERROR 0x1ULL #define UCIE_DEFAULT_CLK_P 0x55555555ULL #define UCIE_DEFAULT_CLK_N 0xaaaaaaaaULL #define UCIE_DEFAULT_VALID 0xf0f0f0fULL #define UCIE_DEFAULT_TRACK 0x55555555ULL #define UCIE_ENABLE_DRIVER_CTL 0x3ffeULL #define UCIE_ENABLE_TX_CTL 0x3fffff000000000ULL +#define UCIE_DISABLE_DRIVER_CTL 0x1ULL +#define UCIE_DISABLE_TX_CTL 0xf000000000ULL +#define UCIE_TX_CTL_DELAY_MASK 0xffffffffULL #define UCIE_TX_CTL_DELAY_LSB 0x0ULL #define UCIE_TX_CTL_ENP_LSB 0x31ULL #define UCIE_TX_CTL_ENN_LSB 0x28ULL #define UCIE_TX_CTL_ENP_EQ_LSB 0x24ULL #define UCIE_TX_CTL_ENN_EQ_LSB 0x20ULL -#define UCIE_TRAIN_LFSR_SEED 0x123456789abcdefULL +#define UCIE_TRAIN_LFSR_SEED 0x8123456789abcdefULL +#define UCIE_LFSR_SEED_LANE_STEP 0x1111ULL #define UCIE_TRAIN_DATA_LANES 0x10ULL #define UCIE_TRAIN_VALID_LANE 0x10ULL #define UCIE_TRAIN_SCORE_LANES 0x11ULL +#define UCIE_NUM_TEST_LANES 0x13ULL +#define UCIE_VALID_LANE 0x10ULL +#define UCIE_TRACK_LANE 0x11ULL +#define UCIE_LOOPBACK_LANE 0x12ULL +#define UCIE_DEFAULT_VALID_LANE_SEL 0x10ULL +#define UCIE_TRACK_VALID_LANE_SEL 0x11ULL +#define UCIE_SRAM_PACKETS 0x40ULL +#define UCIE_CLK_POSTAMBLE_UI 0x2ULL +#define UCIE_CLK_METER_WINDOW_CYCLES 0x400ULL #define UCIE_RATE_CFG_MAIN_CLK_SEL_LSB 0x5ULL #define UCIE_RATE_CFG_TX_CLK_DIV_LSB 0x3ULL #define UCIE_TRAIN_EYE_FINE_POINTS 0x4ULL @@ -267,6 +367,16 @@ #define UCIE_CLK_UNGATE_SRC_PLL_LOCK 0x0ULL #define UCIE_CLK_UNGATE_SRC_MMIO 0x1ULL #define UCIE_CLK_UNGATE_SRC_DELAY 0x2ULL +#define UCIE_DEBUG_MUX_SB_TX_CLK 0x0ULL +#define UCIE_DEBUG_MUX_TX_DIV_CLK 0x1ULL +#define UCIE_DEBUG_MUX_RX_DIV_CLK 0x2ULL +#define UCIE_DEBUG_MUX_DIGITAL_BYPASS_CLK 0x3ULL +#define UCIE_DEBUG_MUX_SIDEBAND_BYPASS_CLK 0x4ULL +#define UCIE_DEBUG_MUX_REF_CLK 0x5ULL +#define UCIE_DEBUG_MUX_RX_DATA_LSB 0x6ULL +#define UCIE_DEBUG_MUX_TX_DATA_LSB 0x7ULL +#define UCIE_DEBUG_MUX_TEST_TX_PAD_CLK 0x8ULL +#define UCIE_DEBUG_MUX_TEST_RX_PAD_CLK 0x9ULL // === Helper functions === static inline void reset_datapath(uintptr_t base) { @@ -274,8 +384,8 @@ static inline void reset_datapath(uintptr_t base) { reg_write64(base + UCIE_RX_DATAPATH_RST, 0x1ULL); } static inline void reset_fsms(uintptr_t base) { - reset_datapath(base); reg_write64(base + UCIE_TX_FSM_RST, 0x1ULL); + reset_datapath(base); reg_write64(base + UCIE_RX_FSM_RST, 0x1ULL); reg_write64(base + UCIE_DEBUG_TX_FSM_RST, 0x1ULL); assert(reg_read64(base + UCIE_TX_TEST_STATE) == (UCIE_TX_TEST_STATE_IDLE)); @@ -305,11 +415,17 @@ static inline void set_clk_manual(uintptr_t base, uint64_t en) { } static inline void apply_clk_cfg(uintptr_t base) { reg_write64(base + UCIE_CLK_CFG_APPLY, 0x1ULL); - for (int w = 0; w < 256; w++) { + { uint64_t busy; - busy = reg_read64(base + UCIE_CLK_CFG_BUSY); - if (busy == 0) { - break; + busy = 0x1ULL; + for (int w = 0; w < 65536; w++) { + busy = reg_read64(base + UCIE_CLK_CFG_BUSY); + if (busy == 0) { + break; + } + } + if (busy != 0) { + printf("apply_clk_cfg: the apply never finished; a PLL it waits on may not be locking (see clkMeter* and pllLockObserved)\n"); } } } @@ -321,8 +437,8 @@ static inline void release_clk_gate(uintptr_t base) { reg_write64(base + UCIE_CLK_CFG_UNGATE_REQ, 0x1ULL); } static inline void reset_ucie(uintptr_t base) { - reg_write64(base + UCIE_UCIE_RST, 0x1ULL); - reg_write64(base + UCIE_UCIE_RST, 0x0ULL); + reg_write64(base + UCIE_RST, 0x1ULL); + reg_write64(base + UCIE_RST, 0x0ULL); } static inline void reset_dividers(uintptr_t base) { reg_write64(base + UCIE_TX_DIVIDER_RST, 0x1ULL); @@ -331,20 +447,27 @@ static inline void reset_dividers(uintptr_t base) { reg_write64(base + UCIE_RX_DIVIDER_RST, 0x0ULL); } static inline void set_main_clk(uintptr_t base, uint64_t src, uint64_t div) { + if (src == 0 || src == 2) { + reg_write64(base + UCIE_PLL8_EN, 0x1ULL); + } + if (src == 1) { + reg_write64(base + UCIE_PLL12_EN, 0x1ULL); + } reg_write64(base + UCIE_MAIN_CLK_SEL, src); reg_write64(base + UCIE_TX_CLK_DIV, div); - reg_write64(base + UCIE_CLK_EN, 0x1ULL); apply_clk_cfg(base); } static inline void set_tx_phase(uintptr_t base, uint64_t half_cycles) { - reg_write64(base + UCIE_CLK_EN, 0x0ULL); reg_write64(base + UCIE_TX_CLK_PHASE, half_cycles); - reg_write64(base + UCIE_CLK_EN, 0x1ULL); + apply_clk_cfg(base); } static inline void use_internal_clk(uintptr_t base, uint64_t phytest_src, uint64_t phytest_div) { - reg_write64(base + UCIE_PLL8_EN, 0x1ULL); - reg_write64(base + UCIE_PLL12_EN, 0x1ULL); - reg_write64(base + UCIE_X2_EN, 0x1ULL); + if (phytest_src == 0) { + reg_write64(base + UCIE_PLL8_EN, 0x1ULL); + } + if (phytest_src == 1) { + reg_write64(base + UCIE_PLL12_EN, 0x1ULL); + } reg_write64(base + UCIE_PHY_TEST_CLK_SRC_SEL, phytest_src); reg_write64(base + UCIE_PHY_TEST_CLK_DIV, phytest_div); reg_write64(base + UCIE_PHY_TEST_CLK_BYPASS_EN, 0x0ULL); @@ -354,20 +477,33 @@ static inline void set_rx_clk_en(uintptr_t base, uint64_t en) { reg_write64(base + UCIE_RX_CLK_EN, en); } static inline void set_global_delay(uintptr_t base, uint64_t taps) { - reg_write64(base + UCIE_CLK_PHASE_SEL, ((0x1ULL << taps) - 0x1ULL)); + reg_write64(base + UCIE_CLK_PHASE_SEL, ((taps >= 64) ? ~0x0ULL : ((0x1ULL << taps) - 0x1ULL))); + apply_clk_cfg(base); } static inline void set_tx_delay(uintptr_t base, uint64_t lane, uint64_t taps) { - write_txctl(base, lane, UCIE_TXCTL_TILE_OFS, (UCIE_ENABLE_TX_CTL | (((0x1ULL << taps) - 0x1ULL) << UCIE_TX_CTL_DELAY_LSB))); + { + uint64_t tile; + tile = reg_read64(base + UCIE_TXCTL + lane * UCIE_TXCTL_WIDTH + UCIE_TXCTL_TILE_OFS); + write_txctl(base, lane, UCIE_TXCTL_TILE_OFS, ((tile & ~UCIE_TX_CTL_DELAY_MASK) | ((((taps >= 64) ? ~0x0ULL : ((0x1ULL << taps) - 0x1ULL)) << UCIE_TX_CTL_DELAY_LSB) & UCIE_TX_CTL_DELAY_MASK))); + } } static inline void set_rx_vref(uintptr_t base, uint64_t lane, uint64_t code) { write_rxctl(base, lane, UCIE_RXCTL_VREF_SEL_OFS, code); } static inline void set_rx_delay(uintptr_t base, uint64_t lane, uint64_t taps) { - write_rxctl(base, lane, UCIE_RXCTL_DCTRL_OFS, ((0x1ULL << taps) - 0x1ULL)); + write_rxctl(base, lane, UCIE_RXCTL_DCTRL_OFS, ((taps >= 64) ? ~0x0ULL : ((0x1ULL << taps) - 0x1ULL))); } +static inline void enable_debug_drivers(uintptr_t base, uint64_t en) { + for (int i = 0; i < 2; i++) { + reg_write64(base + UCIE_DEBUG_DRIVERCTL + 8 * i, (en ? UCIE_ENABLE_DRIVER_CTL : UCIE_DISABLE_DRIVER_CTL)); + } + reg_write64(base + UCIE_DEBUG_TXCTL_TILE, (en ? UCIE_ENABLE_TX_CTL : UCIE_DISABLE_TX_CTL)); +} static inline void setup_ucie(uintptr_t base) { + set_clk_en(base, 0x0ULL); + set_rx_clk_en(base, 0x0ULL); reset_ucie(base); for (int lane = 0; lane < 21; lane++) { write_txctl(base, lane, UCIE_TXCTL_TILE_OFS, UCIE_ENABLE_TX_CTL); @@ -377,14 +513,8 @@ static inline void setup_ucie(uintptr_t base) { reg_write64(base + UCIE_TX_CLK_P, UCIE_DEFAULT_CLK_P); reg_write64(base + UCIE_TX_CLK_N, UCIE_DEFAULT_CLK_N); reg_write64(base + UCIE_TX_VALID, UCIE_DEFAULT_VALID); - reg_write64(base + UCIE_RX_LFSR_VALID, UCIE_DEFAULT_VALID); - for (int i = 0; i < 2; i++) { - reg_write64(base + UCIE_DEBUG_DRIVERCTL + 8 * i, UCIE_ENABLE_DRIVER_CTL); - } - reg_write64(base + UCIE_DEBUG_TXCTL_TILE, UCIE_ENABLE_TX_CTL); - set_clk_en(base, 0x0ULL); - set_rx_clk_en(base, 0x0ULL); - reset_dividers(base); + reg_write64(base + UCIE_RX_VALID, UCIE_DEFAULT_VALID); + enable_debug_drivers(base, 0x0ULL); set_clk_en(base, 0x1ULL); set_rx_clk_en(base, 0x1ULL); reg_write64(base + UCIE_CONTROLLER_SEL, UCIE_CONTROLLER_SEL_PHYTEST); @@ -393,23 +523,115 @@ static inline void setup_ucie(uintptr_t base) { reset_fsms(base); } +static inline void seed_lfsrs(uintptr_t base) { + for (int i = 0; i < 19; i++) { + reg_write64(base + UCIE_TX_LFSR_SEED + i * UCIE_TX_LFSR_SEED_WIDTH, (UCIE_TRAIN_LFSR_SEED + i * UCIE_LFSR_SEED_LANE_STEP)); + reg_write64(base + UCIE_RX_LFSR_SEED + i * UCIE_RX_LFSR_SEED_WIDTH, (UCIE_TRAIN_LFSR_SEED + i * UCIE_LFSR_SEED_LANE_STEP)); + } +} + +static inline void run_lfsr(uintptr_t base, uint64_t packets) { + reg_write64(base + UCIE_TEST_TARGET, UCIE_TEST_TARGET_MAINBAND); + reg_write64(base + UCIE_TX_TEST_MODE, UCIE_TX_TEST_MODE_LFSR); + reg_write64(base + UCIE_TX_DATA_MODE, UCIE_DATA_MODE_FINITE); + reg_write64(base + UCIE_RX_DATA_MODE, UCIE_DATA_MODE_FINITE); + reg_write64(base + UCIE_RX_PACKETS_TO_RECEIVE, packets); + reg_write64(base + UCIE_TX_PACKETS_TO_SEND, packets); + reset_fsms(base); + { + uint64_t settle_w; + settle_w = 0x0ULL; + for (int s = 0; s < 4096; s++) { + settle_w = reg_read64(base + UCIE_RX_IDLE_WORDS_OBSERVED); + if (settle_w >= 4) { + break; + } + } + if (settle_w < 4) { + printf("run_lfsr: the RX is handing over no words; no forwarded clock is arriving (see clkMeterRxDiv)\n"); + } + } + reg_write64(base + UCIE_TX_EXECUTE, 0x1ULL); + { + uint64_t r; + uint64_t prev; + uint64_t stalls; + r = 0x0ULL; + prev = ~0x0ULL; + stalls = 0x0ULL; + for (int w = 0; w < 1024; w++) { + r = reg_read64(base + UCIE_RX_PACKETS_RECEIVED); + if (r >= packets) { + break; + } + if (r != prev || r == 0) { + stalls = 0x0ULL; + } + if (r == prev && r != 0) { + stalls = stalls + 0x1ULL; + } + prev = r; + if (stalls >= 32) { + break; + } + } + if (r < packets) { + printf("run_lfsr: the receiver stopped short of the packets asked for\n"); + } + } +} + +static inline void align_rx_words(uintptr_t base) { + reg_write64(base + UCIE_CONTROLLER_SEL, UCIE_CONTROLLER_SEL_PHYTEST); + reg_write64(base + UCIE_MAINBAND_MODE, UCIE_BAND_MODE_MANUAL); + reg_write64(base + UCIE_TX_CLK_P, UCIE_DEFAULT_CLK_P); + reg_write64(base + UCIE_TX_CLK_N, UCIE_DEFAULT_CLK_N); + reg_write64(base + UCIE_TX_VALID, UCIE_DEFAULT_VALID); + reg_write64(base + UCIE_RX_VALID, UCIE_DEFAULT_VALID); + for (int lane = 0; lane < 18; lane++) { + reg_write64(base + UCIE_RX_UI_DELAY + lane * UCIE_RX_UI_DELAY_WIDTH, 0x0ULL); + } + seed_lfsrs(base); + run_lfsr(base, 0x4ULL); + { + uint64_t received; + uint64_t offset; + received = reg_read64(base + UCIE_RX_PACKETS_RECEIVED); + offset = reg_read64(base + UCIE_RX_FRAME_OFFSET); + if (received == 0) { + printf("align_rx_words: the RX framed nothing, so rxUiDelay stays at 0\n"); + } + if (received != 0) { + for (int lane = 0; lane < 18; lane++) { + reg_write64(base + UCIE_RX_UI_DELAY + lane * UCIE_RX_UI_DELAY_WIDTH, (32 - offset) % 32); + } + } + } +} + static inline void setup_ucie_digital(uintptr_t base) { - reg_write64(base + UCIE_UCIE_RST, 0x0ULL); + reg_write64(base + UCIE_RST, 0x0ULL); set_clk_en(base, 0x0ULL); set_rx_clk_en(base, 0x0ULL); - set_global_delay(base, 0x1dULL); reset_dividers(base); set_clk_en(base, 0x1ULL); set_rx_clk_en(base, 0x1ULL); for (int lane = 0; lane < 20; lane++) { - write_txctl(base, lane, UCIE_TXCTL_TILE_OFS, UCIE_ENABLE_TX_CTL); + { + uint64_t tile; + tile = reg_read64(base + UCIE_TXCTL + lane * UCIE_TXCTL_WIDTH + UCIE_TXCTL_TILE_OFS); + write_txctl(base, lane, UCIE_TXCTL_TILE_OFS, ((tile & UCIE_TX_CTL_DELAY_MASK) | (UCIE_ENABLE_TX_CTL & ~UCIE_TX_CTL_DELAY_MASK))); + } } for (int lane = 0; lane < 18; lane++) { write_rxctl(base, lane, UCIE_RXCTL_ZEN_OFS, 0x1ULL); write_rxctl(base, lane, UCIE_RXCTL_ZCTL_OFS, 0x0ULL); } + align_rx_words(base); + reg_write64(base + UCIE_TX_CLK_POSTAMBLE, 0x20ULL); reg_write64(base + UCIE_CONTROLLER_SEL, UCIE_CONTROLLER_SEL_UCIE); reset_datapath(base); + reg_write64(base + UCIE_CONTROLLER_RST, 0x0ULL); { uint64_t linkControl; linkControl = reg_read64(base + UCIE_RAW_FORMAT_ENABLE); @@ -417,43 +639,17 @@ static inline void setup_ucie_digital(uintptr_t base) { } reg_write64(base + UCIE_UNCORR_ERR_MASK, 0x0ULL); reg_write64(base + UCIE_CORR_ERR_MASK, 0x0ULL); - while (1) { + { uint64_t linkStatus; - linkStatus = reg_read64(base + UCIE_RAW_FORMAT_ENABLED); - if ((linkStatus >> 0xfULL) & 0x1ULL) { - break; + linkStatus = 0x0ULL; + for (int w = 0; w < 16777216; w++) { + linkStatus = reg_read64(base + UCIE_RAW_FORMAT_ENABLED); + if ((linkStatus >> 0xfULL) & 0x1ULL) { + break; + } } - } -} - -static inline void seed_lfsrs(uintptr_t base) { - for (int i = 0; i < 19; i++) { - reg_write64(base + UCIE_TX_LFSR_SEED + i * UCIE_TX_LFSR_SEED_WIDTH, UCIE_TRAIN_LFSR_SEED); - reg_write64(base + UCIE_RX_LFSR_SEED + i * UCIE_RX_LFSR_SEED_WIDTH, UCIE_TRAIN_LFSR_SEED); - } -} - -static inline void run_lfsr(uintptr_t base, uint64_t packets) { - reset_fsms(base); - for (int s = 0; s < 64; s++) { - uint64_t settle_w; - settle_w = reg_read64(base + UCIE_RX_IDLE_WORDS_OBSERVED); - if (settle_w >= 4) { - break; - } - } - reg_write64(base + UCIE_TEST_TARGET, UCIE_TEST_TARGET_MAINBAND); - reg_write64(base + UCIE_TX_TEST_MODE, UCIE_TX_TEST_MODE_LFSR); - reg_write64(base + UCIE_TX_DATA_MODE, UCIE_DATA_MODE_FINITE); - reg_write64(base + UCIE_RX_DATA_MODE, UCIE_DATA_MODE_FINITE); - reg_write64(base + UCIE_RX_PACKETS_TO_RECEIVE, packets); - reg_write64(base + UCIE_TX_PACKETS_TO_SEND, packets); - reg_write64(base + UCIE_TX_EXECUTE, 0x1ULL); - for (int w = 0; w < 16; w++) { - uint64_t r; - r = reg_read64(base + UCIE_RX_PACKETS_RECEIVED); - if (r >= packets) { - break; + if (((linkStatus >> 0xfULL) & 0x1ULL) == 0) { + printf("setup_ucie_digital: link training never brought the link up\n"); } } } diff --git a/test/src/IrisSpec.scala b/test/src/IrisSpec.scala index 829a304..749f531 100644 --- a/test/src/IrisSpec.scala +++ b/test/src/IrisSpec.scala @@ -62,6 +62,33 @@ class ClockSourceAtFreqMHz(val freqMHz: Double) ) } +/** Counts transitions on a UCIe link's observation bumps and reports them at + * the end of simulation. A test uses this to check that the bumps bring-up + * probes with a scope are driven at the chip's pins. + */ +class DebugBumpMonitor(name: String) + extends BlackBox(Map("NAME" -> StringParam(name))) + with HasBlackBoxInline { + val io = IO(new Bundle { + val mux0 = Input(Bool()) + val mux1 = Input(Bool()) + val txData = Input(Bool()) + }) + setInline( + "DebugBumpMonitor.sv", + """module DebugBumpMonitor #(parameter NAME = "") ( + | input mux0, input mux1, input txData); + | longint unsigned n_mux0 = 0, n_mux1 = 0, n_txData = 0; + | always @(mux0) n_mux0++; + | always @(mux1) n_mux1++; + | always @(txData) n_txData++; + | final $display("Debug bumps %s: mux0 %0d mux1 %0d txData %0d", + | NAME, n_mux0, n_mux1, n_txData); + |endmodule + |""".stripMargin + ) +} + /** Prints a clock's period whenever it settles on a new value: after three * consecutive posedge-to-posedge intervals of the same length, and only if * that length differs from the last one printed. Tests grep the log for the @@ -285,11 +312,16 @@ class TestHarness( // The PHY bumps carry a single-ended mainband bypass clock; the PLL // reference pair and its RDAC bias are no longer bumps. - Seq(chiptop.c2c_ucie0, chiptop.c2c_ucie1).foreach { ucie => + Seq(chiptop.c2c_ucie0, chiptop.c2c_ucie1).zipWithIndex.foreach { case (ucie, i) => ucie.phy.bypassClk := ucieBypassClock ucie.phy.digitalBypassClk := ucieDigitalBypassClock ucie.phy.sidebandBypassClk := ucieSidebandBypassClock ucie.phy.refClk := ucieRefClock + val monitor = Module(new DebugBumpMonitor(s"chip$chipId.ucie$i")) + monitor.io.mux0 := ucie.debug.mux0 + monitor.io.mux1 := ucie.debug.mux1 + // Absent on a link built without the TX data debug lane. + monitor.io.txData := ucie.debug.txData.getOrElse(false.B) } Seq(chiptop.c2c_ucie0, chiptop.c2c_ucie1) @@ -534,6 +566,71 @@ class IrisSpec extends AnyFunSpec { ) } + // The PhyTest programs bring-up runs on the chip (docs/ucie_bringup.md), + // run by the core through the SoC bus and UCIe register map, over the + // behavioral lanes and clocking tile. + // The loopback program turns every observation driver on for one LFSR run, + // with `mux0` on the TX divided clock and `mux1` on an RX data lane, and + // drives the TX debug lane. So every debug bump of both links must toggle, + // which shows the scope points reach the pins. + def checkDebugBumps(simLog: Path): Unit = + for (link <- Seq("chip0.ucie0", "chip0.ucie1")) { + val counts = Utils.debugBumpCounts(simLog, link) + assert(counts.keySet == Set("mux0", "mux1", "txData")) + for ((bump, n) <- counts) { + assert(n > 64, s"$link debug bump $bump only moved $n times") + } + } + + it("should run phytest loopback test") { + implicit val p = new IrisConfig(sim = true) + checkDebugBumps( + Utils.simulateTopWithBinaries( + Utils.buildRoot / "Iris_should_run_phytest_loopback_test", + nChips = 1, + binaryPaths = Seq(Utils.root / "software/phytest-loopback.riscv"), + fast = true + ) + ) + } + + it("should run phytest loopback test on the tapeout config") { + // The configuration that is taped out (16b serial TL, no TL monitors), + // with the serial TL on a 100 MHz external clock. + implicit val p = new IrisConfig(sim = false) + checkDebugBumps( + Utils.simulateTopWithBinaries( + Utils.buildRoot / "Iris_should_run_phytest_loopback_test_on_the_tapeout_config", + nChips = 1, + binaryPaths = Seq(Utils.root / "software/phytest-loopback.riscv"), + fast = true, + serialTLFreqMHz = Some(100) + ) + ) + } + + it("should run phytest clocks test") { + implicit val p = new IrisConfig(sim = true) + Utils.simulateTopWithBinaries( + Utils.buildRoot / "Iris_should_run_phytest_clocks_test", + nChips = 1, + binaryPaths = Seq(Utils.root / "software/phytest-clocks.riscv"), + fast = true + ) + } + + it("should run phytest two-chip test") { + implicit val p = new IrisConfig(sim = true) + // Both chips run the one binary; the ring wires UCIe1 of each to UCIe0 + // of the other. + Utils.simulateTopWithBinaries( + Utils.buildRoot / "Iris_should_run_phytest_two_chip_test", + nChips = 2, + binaryPaths = Seq(Utils.root / "software/phytest-two-chip.riscv"), + fast = true + ) + } + it("should run tacit always trace workload") { implicit val p = new IrisConfig(sim = true) val workDir = Utils.buildRoot / "Iris_should_run_tacit_always_trace" diff --git a/test/src/Utils.scala b/test/src/Utils.scala index 2967585..ac95247 100644 --- a/test/src/Utils.scala +++ b/test/src/Utils.scala @@ -172,10 +172,9 @@ script -e -f -c "./simulation +permissive +vcs+thread+16 +dramsim +dramsim_ini_d simScript ).call(stdout = os.Inherit, stderr = os.Inherit, cwd = simDir) - // The simulator's exit status does not say whether the test passed: VCS - // ends with status 0 after a `$fatal`, whether from SimTop's timeout or - // from a failed assertion. SimTop prints its success line only when the - // chips report success, so the log decides it. + // The simulator's exit status does not show a pass: VCS exits 0 after a + // `$fatal`, from SimTop's timeout or a failed assertion. SimTop prints its + // success line only when the chips report success, so check the log. val simLog = os.read(simDir / "simulation.log") val failure = Seq("Assertion failed", "Fatal:", "Test timed out!").find(simLog.contains) @@ -197,4 +196,17 @@ script -e -f -c "./simulation +permissive +vcs+thread+16 +dramsim +dramsim_ini_d assert(log.contains(l), s"simulation log does not contain \"$l\"; see $simLog") } } + + /** The transition counts `DebugBumpMonitor` reported for one link, by bump + * name. + */ + def debugBumpCounts(simLog: Path, link: String): Map[String, BigInt] = { + val line = os.read.lines(simLog) + .find(_.startsWith(s"Debug bumps $link:")) + .getOrElse(throw new AssertionError(s"no debug bump report for $link; see $simLog")) + "(\\w+) (\\d+)".r + .findAllMatchIn(line.stripPrefix(s"Debug bumps $link:")) + .map(m => m.group(1) -> BigInt(m.group(2))) + .toMap + } } diff --git a/ucie b/ucie index c46f8d7..07a0c22 160000 --- a/ucie +++ b/ucie @@ -1 +1 @@ -Subproject commit c46f8d7a92ae0f0b3ca7bf23c40353eea995fa18 +Subproject commit 07a0c22bd39e577e561e109b340a1b21bc0c775e