From 28bab4d56701e2b8b51835144096a4dc22d22eb9 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 2 Nov 2026 14:54:39 +0800 Subject: [PATCH] zebu_pcie5_vhost_demo_complete_20261101 --- zebu_pcie5_vhost_demo/cfg/designFeatures | 4 + .../zebu_pcie5_vhost_demo/README.md | 166 +++++++ .../cfg/zebu_compile.utf | 38 ++ .../zebu_pcie5_vhost_demo/rtl/filelist.f | 5 + .../rtl/pcie5_demo_top.sv | 137 ++++++ .../rtl/pcie5_ep_core.sv | 437 ++++++++++++++++++ .../zebu_pcie5_vhost_demo/rtl/pcie_pkg.sv | 41 ++ .../zebu_pcie5_vhost_demo/scripts/clean.csh | 7 + .../zebu_pcie5_vhost_demo/scripts/compile.csh | 48 ++ .../zebu_pcie5_vhost_demo/scripts/env.csh | 53 +++ .../zebu_pcie5_vhost_demo/scripts/run.csh | 63 +++ .../zebu_pcie5_vhost_demo/sw/vhost/Makefile | 43 ++ .../zebu_pcie5_vhost_demo/sw/vhost/main.cpp | 373 +++++++++++++++ .../sw/vhost/pcie_vhost_api.h | 51 ++ .../sw/vhost/pcie_vhost_zebu.cpp | 431 +++++++++++++++++ .../tb/pcie_vhost_xtor.sv | 287 ++++++++++++ 16 files changed, 2184 insertions(+) create mode 100644 zebu_pcie5_vhost_demo/cfg/designFeatures create mode 100644 zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/README.md create mode 100644 zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/cfg/zebu_compile.utf create mode 100644 zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/rtl/filelist.f create mode 100644 zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/rtl/pcie5_demo_top.sv create mode 100644 zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/rtl/pcie5_ep_core.sv create mode 100644 zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/rtl/pcie_pkg.sv create mode 100644 zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/scripts/clean.csh create mode 100644 zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/scripts/compile.csh create mode 100644 zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/scripts/env.csh create mode 100644 zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/scripts/run.csh create mode 100644 zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/sw/vhost/Makefile create mode 100644 zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/sw/vhost/main.cpp create mode 100644 zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/sw/vhost/pcie_vhost_api.h create mode 100644 zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/sw/vhost/pcie_vhost_zebu.cpp create mode 100644 zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/tb/pcie_vhost_xtor.sv diff --git a/zebu_pcie5_vhost_demo/cfg/designFeatures b/zebu_pcie5_vhost_demo/cfg/designFeatures new file mode 100644 index 0000000..e99bdd5 --- /dev/null +++ b/zebu_pcie5_vhost_demo/cfg/designFeatures @@ -0,0 +1,4 @@ +$U0.M0.pcie5_demo_top.dut_clk.Waveform = "-_"; +$U0.M0.pcie5_demo_top.dut_clk.Mode = "controlled"; +$U0.M0.pcie5_demo_top.dut_clk.GroupName = "dut_clk"; +$U0.M0.pcie5_demo_top.dut_clk.VirtualFrequency = 1; diff --git a/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/README.md b/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/README.md new file mode 100644 index 0000000..d0a3714 --- /dev/null +++ b/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/README.md @@ -0,0 +1,166 @@ +# ZeBu Server 4 PCIe Gen5 Virtual Host 仿真 Demo(ZEMI3/DPI 版) + +基于 Synopsys ZeBu Server 4 的 PCIe 5.0 仿真示例工程:DUT 是一个 PCIe Gen5 +Endpoint 示例设计(配置空间 + BAR 寄存器 + 片上 RAM + 简单 DMA 引擎), +主机侧不使用真实 Root Complex 硬件,而是通过 **Virtual Host(vhost)** 方式: +独立运行的 C++ 测试程序经 ZEMI3Manager 自行开板(与站点 B2B 的 proxyTB +同一模式,不走 zRci),通过 ZEMI3/DPI transactor 把 PCIe 事务 +(Config / Memory TLP)送入仿真中的 DUT,完成枚举、BAR 访问、DMA 搬运 +和 MSI 中断验证。 + +> 为什么不用 SCE-MI:本站点 license 服务器上没有 zs_SceMi 特征(实测), +> SCE-MI 程序无法启动;站点的生产 PCIe 工程(官方 xtor_pcie_svs B2B) +> 走的是 ZEMI3/DPI 流程(zs_Emi3/zs_Emi3Runtime 授权齐全),本 demo 与其 +> 保持一致。EP 核 RTL 与 SCE-MI 版完全相同,只换了 transactor 与软件层。 + +链路在 vhost 模型中协商为 **Gen5(32.0 GT/s)x16**:DUT 配置空间的 PCIe +Capability 中 Link Capabilities.Max Link Speed = 6(32 GT/s),Link Status +回读协商速率/宽度,测试程序会校验这一点。 + +> 说明:硬件仿真中 PCIe 链路是事务级虚拟链路,不存在真实 32GT/s 串行信号, +> Gen5 体现在协议能力(配置空间、TLP、链路能力协商)层面,这是 vhost 方式的 +> 标准做法。 + +## 架构 + +``` + 测试程序 (工作站) ZeBu Server 4 +┌────────────────────────────┐ ZEMI3/DPI ┌──────────────────────────────┐ +│ pcie_vhost_tb (独立程序) │ UDPI FIFO │ tb/pcie_vhost_xtor.sv │ +│ - main.cpp │ swtohw / │ - (* zebu_zemi3_xtor *) │ +│ ZEMI3Manager 开板 │ hwtosw │ - UDPI FIFO <-> TLP 转换 │ +│ + 测试序列 │◄────────────►│ - zceiClockPort 提供时钟 │ +│ - pcie_vhost_zebu.cpp │ │ │ TLP │ +│ UDPI 收发 + 主机内存模型 │ │ ▼ │ +└────────────────────────────┘ │ rtl/pcie5_ep_core.sv (DUT) │ + │ - PCIe 配置空间 (Gen5 Cap) │ + │ - BAR0 寄存器 / BAR2 RAM │ + │ - DMA 引擎 / MSI │ + └──────────────────────────────┘ +``` + +SW/HW 之间是统一的 256 位消息([255:248] 为消息类型):SW→HW 为 TLP 请求 / +DMA 完成,HW→SW 为完成 / DMA 请求 / MSI 事件;位布局见 +`tb/pcie_vhost_xtor.sv` 与 `sw/vhost/pcie_vhost_zebu.cpp` 的文件头注释。 + +## 目录结构 + +``` +zebu_pcie5_vhost_demo/ +├── README.md +├── scripts/ # 全部 csh 脚本 +│ ├── env.csh # 环境变量(ZEBU_ROOT / ZEBU_IP_ROOT / license) +│ ├── compile.csh # zCui 编译 + dpixtor.mk 生成 xtor 软件胶水 +│ ├── run.csh # 编译 pcie_vhost_tb 并运行测试 +│ └── clean.csh +├── cfg/ +│ ├── zebu_compile.utf # zCui 编译入口(UTF,与站点 ZeBu IP 示例同构) +│ └── designFeatures # 受控时钟特征(ZEMI3Manager::open 必需) +├── rtl/ +│ ├── filelist.f +│ ├── pcie_pkg.sv # TLP 类型/参数定义 +│ ├── pcie5_ep_core.sv # DUT:PCIe Gen5 Endpoint 示例核 +│ └── pcie5_demo_top.sv # 仿真顶层(含 zceiClockPort) +├── tb/ +│ └── pcie_vhost_xtor.sv # ZEMI3 transactor(vhost 桥,拼 UDPI 积木) +└── sw/vhost/ + ├── pcie_vhost_api.h # vhost 高层 C++ API + ├── pcie_vhost_zebu.cpp # ZEMI3/DPI 后端(UDPI 收发 + 主机内存模型) + ├── main.cpp # 测试序列 + ZEMI3Manager 开板入口(main) + └── Makefile # 编 pcie_vhost_tb(链生成胶水 .so + libZebu/ZEMI3/Xtor) +``` + +## 前置条件 + +- ZeBu Server 4 硬件及对应版本的 zCui 运行时库(`ZEBU_ROOT`) +- ZeBu license:zs_Compil / zs_Emi3 / zs_Emi3Runtime 等(站点已有); + Vivado Implementation/xcvu440(XILINXD_LICENSE_FILE,env.csh 已按站点填好) +- ZeBu IP 包(`ZEBU_IP_ROOT`,提供 UDPI 模块 zebu_vs_simple_*_udpi) +- 工作站装有 GCC、csh/tcsh + +## 使用步骤(csh) + +```csh +cd zebu_pcie5_vhost_demo +source scripts/env.csh # 先按本机环境改 ZEBU_ROOT 和 license +./scripts/compile.csh # zCui 编译 + 生成 pcie_vhost_xtor.so/.h +./scripts/run.csh # 运行 vhost 测试,退出码即 PASS/FAIL +./scripts/clean.csh # 清理 +``` + +编译分两步(都在 compile.csh 里):zCui 完成硬件编译并由 zemi3 生成 +transactor 胶水工程,随后在内层 `zebu.work/zebu.work` 里执行 +`make -f dpixtor.mk`(ZeBu 文档 11.42 的指定位置),把胶水编成 +`zebu.work/zebu.work/pcie_vhost_xtor.so`、同目录生成 `pcie_vhost_xtor.h`; +内层 zebu.work 是运行时数据库目录,测试程序以 `-w` 指向它打开。 +测试程序编译时会 include 这份生成头,DPI 函数签名不匹配会直接编译报错。 + +## 测试内容(sw/vhost/main.cpp) + +1. **链路校验**:读 PCIe Link Status,期望 Speed = 6(Gen5 32 GT/s)、Width = x16 +2. **枚举**:读配置空间 0x00,期望 VID/DID = 0x16C3 / 0x5805;BAR 探测与赋值 +3. **BAR0 寄存器测试**:ID 寄存器只读校验、SCRATCH 寄存器多组 pattern 读写 +4. **BAR2 RAM 测试**:经 Memory Write/Read TLP 写入 4KB 数据并回读比对 +5. **DMA 测试**:主机内存 -> 设备 RAM(H2D)、设备 RAM -> 主机内存(D2H), + 全量数据比对;DMA 完成后等待 MSI 中断并校验 IRQ 状态寄存器 + +全部通过时结尾打印 `TEST PASSED`,run.csh 退出码为 0。 + +**实测状态(2026-11-01,bj1zhost1 / ZeBu Server 4)**:五项测试全部通过, +run.csh 退出码 0。D2H 阶段可观察到 256 拍 MEM_WR 携带设备 RAM 数据到达 +vhost 主机内存、MSI 第二次触发,数据全量比对一致。 + +调试输出默认关闭:`setenv VH_DEBUG 1` 后再跑 run.csh,会恢复 [dbg]/[dma] +原始消息转储、DMA 服务计数与测试结尾的 BAR0 寄存器转储。 + +## DUT 寄存器表(BAR0) + +| 偏移 | 名称 | 说明 | +|------|------|------| +| 0x00 | ID | 只读,固定 0x50354531("P5E1") | +| 0x04 | VERSION | 只读,0x00010000 | +| 0x08 | CTRL | bit0 DMA 启动(自清零),bit1 中断使能,bit2 DMA 方向(0=H2D,1=D2H) | +| 0x0C | STATUS | bit0 DMA busy,bit1 DMA done,bit2 link up | +| 0x10 | SCRATCH | 读写 scratch | +| 0x14 | IRQ_STATUS | bit0 DMA 完成中断,写 1 清除 | +| 0x18 | DMA_HOST_ADDR_LO | 主机内存地址低 32 位 | +| 0x1C | DMA_HOST_ADDR_HI | 主机内存地址高 32 位 | +| 0x20 | DMA_LEN | 搬运字节数(4 的倍数) | +| 0x24 | DMA_DEV_OFFSET | 设备 RAM 内偏移 | + +BAR2:64KB 设备 RAM(Memory 空间)。 + +## 注意事项 + +- 编译入口 `cfg/zebu_compile.utf` 按站点 ZeBu IP 示例(example_wrapper.utf) + 的结构编写,compile.csh 以 `zCui -c -n -u cfg/zebu_compile.utf -w $ZEBU_WORK` + 方式调用。UTF 里的 `FILE_CONF`(板卡架构文件)和 `REMOTECMD`(grid + 提交命令)在 `scripts/env.csh` 中按站点环境填好;VCS 必须用与 ZeBu + Q-2020.03 配套的 Q-2020.03-SP2-1-Z(compile.csh 已写死)。 +- 测试程序与胶水 .so 都用 `-D_GLIBCXX_USE_CXX11_ABI=0` 编译(ZeBu 库的 + C++ ABI 较旧,站点 B2B bench 同样处理);compile.csh 已把该选项传给 + dpixtor.mk 的 CXXFLAGS。测试程序以 `-rdynamic` 链接,好让胶水 .so 能 + 解析到程序里的 UDPI import 函数。 +- 软件流程与站点 B2B 的 proxyTB 一致:独立程序用 + `ZEMI3Manager::open -> buildXtorList -> init -> start` 开板, + transactor 由 manager 按编译生成的 `xtor_dpi.lst` 自动加载(本 demo + 为 `pcie_vhost_xtor pcie5_demo_top.u_xtor` 一行)。注意不要在用户代码 + 里再对同一 xtor 调 addXtor/initXtors——会重复注册端口 + (ZHW0141E port already connected)并导致 zRci/程序崩溃; + XtorScheduler 只需设置参数(useZemiManager 等),本站点 bench 也是 + 只设参数不调 addXtor。早期版本曾走 zRci + tb.so 钩子流程,正是栽在 + 这里,已弃用。 +- 运行时布局(run.csh 自动生成 `run/` 目录):`zebu.work` 是指向内层 + 运行时数据库目录(`${ZEBU_WORK}/zebu.work`)的符号链接,测试程序以 + `-w ./zebu.work` 打开;程序退出码即测试结果。 +- 板卡初始化偶发失败:本站点实测 zServer 开板曾多次报 + "Cannot initialize the ZeBu Server-4 board"(检测日志提示 U0.M1 + 子模块 A/B missing),重试后可成功,属板卡状态问题,与设计无关; + `zRscManager -sysstat` 只报资源占用(free/used),不做物理探测, + 不能用来判断模块在位。 +- vhost 侧的"主机内存地址"是 vhost 模型维护的虚拟主机地址空间,DMA 测试 + 数据由 C++ 程序在该地址空间内准备和校验,不涉及工作站真实物理内存 DMA。 +- 本 demo 在普通工作站上无法独立运行(需要 ZeBu 硬件与 license),交付后 + 请在 ZeBu 环境联调;首次联调重点看三处:zCui 是否为 xtor 生成 + dpixtor.mk、生成的 pcie_vhost_xtor.h 中 UDPI 函数原型、测试程序链接 + 时的未定义符号(按报错补链即可,如 XtorScheduler 在 libZebuXtor)。 diff --git a/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/cfg/zebu_compile.utf b/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/cfg/zebu_compile.utf new file mode 100644 index 0000000..5569723 --- /dev/null +++ b/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/cfg/zebu_compile.utf @@ -0,0 +1,38 @@ +##--------------------------------------------------------------------- +## PCIe Gen5 vhost demo - zCui UTF compile entry (ZEMI3/DPI version) +## Structure follows the site ZeBu IP example (example_wrapper.utf): +## vcs_exec_command -> architecture_file -> zemi3 -> grid_cmd -> synthesis +## Invoked by scripts/compile.csh as: +## zCui -c -n -u cfg/zebu_compile.utf -w $ZEBU_WORK +## The vhost transactor carries (* zebu_zemi3_xtor *); zemi3 detects it +## and generates the SW glue (dpixtor.mk + pcie_vhost_xtor.h/.cc) into +## the work directory. UDPI building blocks come from $ZEBU_IP_ROOT. +## NOTE: the common $ZEBU_IP_ROOT/vlog/vcs only has the UDPI wrappers; +## the FIFO submodules (zebu_vs_from_sw_fifo / zebu_vs_to_sw_fifo / +## vs_memory) only exist inside each XTOR package's vlog/vcs, so all +## UDPI files below are taken from one package dir (CAN_FD, v16.0). +##--------------------------------------------------------------------- + +vcs_exec_command { vcs -full64 \ + -sverilog \ + -timescale=1ns/1ps \ + -hw_top=pcie5_demo_top \ + $PCIE_DEMO_HOME/rtl/pcie_pkg.sv \ + $PCIE_DEMO_HOME/rtl/pcie5_ep_core.sv \ + $ZEBU_IP_ROOT/XTOR/CAN_FD.Q-2021.03/vlog/vcs/vs_memory.sv \ + $ZEBU_IP_ROOT/XTOR/CAN_FD.Q-2021.03/vlog/vcs/vs_fifo_udpi.sv \ + $ZEBU_IP_ROOT/XTOR/CAN_FD.Q-2021.03/vlog/vcs/zebu_vs_from_sw_fifo.sv \ + $ZEBU_IP_ROOT/XTOR/CAN_FD.Q-2021.03/vlog/vcs/zebu_vs_to_sw_fifo.sv \ + $ZEBU_IP_ROOT/XTOR/CAN_FD.Q-2021.03/vlog/vcs/zebu_vs_simple_rst_and_clk_udpi.sv \ + $ZEBU_IP_ROOT/XTOR/CAN_FD.Q-2021.03/vlog/vcs/zebu_vs_simple_swtohw_fifo_udpi.sv \ + $ZEBU_IP_ROOT/XTOR/CAN_FD.Q-2021.03/vlog/vcs/zebu_vs_simple_hwtosw_fifo_udpi.sv \ + $PCIE_DEMO_HOME/tb/pcie_vhost_xtor.sv \ + $PCIE_DEMO_HOME/rtl/pcie5_demo_top.sv \ + } + +## Board architecture file: provided by the site environment (env.csh) +architecture_file -filename "$env(FILE_CONF)" +zemi3 -timestamp true + +grid_cmd -submit $env(REMOTECMD) -delete {} +synthesis -max_loop_iterations 20000 diff --git a/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/rtl/filelist.f b/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/rtl/filelist.f new file mode 100644 index 0000000..b1e998f --- /dev/null +++ b/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/rtl/filelist.f @@ -0,0 +1,5 @@ ++incdir+../rtl +pcie_pkg.sv +pcie5_ep_core.sv +pcie5_demo_top.sv +../tb/pcie_vhost_xtor.sv diff --git a/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/rtl/pcie5_demo_top.sv b/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/rtl/pcie5_demo_top.sv new file mode 100644 index 0000000..351d23a --- /dev/null +++ b/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/rtl/pcie5_demo_top.sv @@ -0,0 +1,137 @@ +// ============================================================================ +// pcie5_demo_top.sv +// Emulation top level for the PCIe Gen5 vhost demo on ZeBu Server 4. +// Instantiates the endpoint core and the ZEMI3 vhost transactor. +// The DUT clock is a ZeBu controlled clock from a zceiClockPort (the +// standard clocking for ZEMI3 transactors, same as the site B2B design). +// ============================================================================ +`timescale 1ns/1ps + +module pcie5_demo_top + import pcie_pkg::*; +( +); + + logic dut_clk; + logic dut_creset; // active high, from the clock port + logic rst_n; + + // Controlled clock + reset provided by ZeBu + zceiClockPort u_dut_clk ( + .cclock (dut_clk), + .creset (dut_creset), + .cresetn () + ); + + // Link state driven by the vhost transactor after (virtual) link training + logic [3:0] link_speed; + logic [5:0] link_width; + logic link_up; + + // Downstream TLP channel + logic req_valid, req_ready; + tlp_kind_e req_kind; + logic [63:0] req_addr; + logic [9:0] req_len_dw; + logic [127:0] req_data; + logic req_last; + logic [7:0] req_tag; + + // Completion channel + logic cpl_valid, cpl_ready; + logic [127:0] cpl_data; + logic [7:0] cpl_tag; + logic cpl_last; + logic [2:0] cpl_status; + + // DMA upstream channels + logic dma_req_valid, dma_req_ready; + tlp_kind_e dma_req_kind; + logic [63:0] dma_req_addr; + logic [9:0] dma_req_len_dw; + logic [127:0] dma_req_data; + logic dma_req_last; + logic [7:0] dma_req_tag; + logic dma_cpl_valid, dma_cpl_ready; + logic [127:0] dma_cpl_data; + logic [7:0] dma_cpl_tag; + logic dma_cpl_last; + + logic irq_msi; + + // ------------------------------------------------------------------ + pcie5_ep_core u_ep ( + .clk (dut_clk), + .rst_n (rst_n), + .link_speed (link_speed), + .link_width (link_width), + .link_up_in (link_up), + .req_valid (req_valid), + .req_ready (req_ready), + .req_kind (req_kind), + .req_addr (req_addr), + .req_len_dw (req_len_dw), + .req_data (req_data), + .req_last (req_last), + .req_tag (req_tag), + .cpl_valid (cpl_valid), + .cpl_ready (cpl_ready), + .cpl_data (cpl_data), + .cpl_tag (cpl_tag), + .cpl_last (cpl_last), + .cpl_status (cpl_status), + .dma_req_valid (dma_req_valid), + .dma_req_ready (dma_req_ready), + .dma_req_kind (dma_req_kind), + .dma_req_addr (dma_req_addr), + .dma_req_len_dw(dma_req_len_dw), + .dma_req_data (dma_req_data), + .dma_req_last (dma_req_last), + .dma_req_tag (dma_req_tag), + .dma_cpl_valid (dma_cpl_valid), + .dma_cpl_ready (dma_cpl_ready), + .dma_cpl_data (dma_cpl_data), + .dma_cpl_tag (dma_cpl_tag), + .dma_cpl_last (dma_cpl_last), + .irq_msi (irq_msi) + ); + + // ------------------------------------------------------------------ + pcie_vhost_xtor u_xtor ( + .dut_clk (dut_clk), + .dut_creset (dut_creset), + .rst_n (rst_n), + .link_speed (link_speed), + .link_width (link_width), + .link_up (link_up), + .req_valid (req_valid), + .req_ready (req_ready), + .req_kind (req_kind), + .req_addr (req_addr), + .req_len_dw (req_len_dw), + .req_data (req_data), + .req_last (req_last), + .req_tag (req_tag), + .cpl_valid (cpl_valid), + .cpl_ready (cpl_ready), + .cpl_data (cpl_data), + .cpl_tag (cpl_tag), + .cpl_last (cpl_last), + .cpl_status (cpl_status), + .dma_req_valid (dma_req_valid), + .dma_req_ready (dma_req_ready), + .dma_req_kind (dma_req_kind), + .dma_req_addr (dma_req_addr), + .dma_req_len_dw(dma_req_len_dw), + .dma_req_data (dma_req_data), + .dma_req_last (dma_req_last), + .dma_req_tag (dma_req_tag), + .dma_cpl_valid (dma_cpl_valid), + .dma_cpl_ready (dma_cpl_ready), + .dma_cpl_data (dma_cpl_data), + .dma_cpl_tag (dma_cpl_tag), + .dma_cpl_last (dma_cpl_last), + .irq_msi (irq_msi) + ); + +endmodule : pcie5_demo_top diff --git a/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/rtl/pcie5_ep_core.sv b/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/rtl/pcie5_ep_core.sv new file mode 100644 index 0000000..17249ee --- /dev/null +++ b/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/rtl/pcie5_ep_core.sv @@ -0,0 +1,437 @@ +// ============================================================================ +// pcie5_ep_core.sv +// Demo PCIe Gen5 Endpoint core for ZeBu Server 4 virtual-host emulation. +// +// Transaction-level TLP interface (one beat = up to 4 DW): +// - Downstream port: requests from the virtual host (CfgRd/Wr, MemRd/Wr) +// - Completion port: read completions back to the virtual host +// - Upstream port: DMA requests issued by this endpoint + their cpls +// +// Features: +// - PCIe config space with Gen5 PCIe Capability +// (LinkCap Max Speed = 6 -> 32.0 GT/s, Max Width = x16, MSI capability) +// - BAR0: control/status register block (see README register table) +// - BAR2: 64 KB device RAM, also the DMA target buffer +// - Simple DMA engine: host memory <-> device RAM, MSI on completion +// +// NOTE: all sequential logic lives in ONE always_ff. VCS rejects variables +// (e.g. dev_ram, irq_status) driven from multiple always_ff blocks (ICPD), +// so the request FSM and the DMA FSM share a single process, and the +// helper tasks below are only called from that process. +// ============================================================================ +`timescale 1ns/1ps + +module pcie5_ep_core + import pcie_pkg::*; +( + input logic clk, + input logic rst_n, + + // Link state reported by the virtual host (negotiated Gen5 x16) + input logic [3:0] link_speed, // 6 = 32.0 GT/s + input logic [5:0] link_width, // 16 = x16 + input logic link_up_in, + + // ---- Downstream TLP request port (from vhost) ---- + input logic req_valid, + output logic req_ready, + input tlp_kind_e req_kind, + input logic [63:0] req_addr, + input logic [9:0] req_len_dw, // length in DW (config = 1) + input logic [127:0] req_data, // write data beat (up to 4 DW) + input logic req_last, + input logic [7:0] req_tag, + + // ---- Completion port (to vhost) ---- + output logic cpl_valid, + input logic cpl_ready, + output logic [127:0] cpl_data, + output logic [7:0] cpl_tag, + output logic cpl_last, + output logic [2:0] cpl_status, + + // ---- Upstream DMA request port (EP as requester) ---- + output logic dma_req_valid, + input logic dma_req_ready, + output tlp_kind_e dma_req_kind, // TLP_MEM_RD / TLP_MEM_WR only + output logic [63:0] dma_req_addr, // host memory address + output logic [9:0] dma_req_len_dw, + output logic [127:0] dma_req_data, + output logic dma_req_last, + output logic [7:0] dma_req_tag, + + // ---- Upstream DMA completion port ---- + input logic dma_cpl_valid, + output logic dma_cpl_ready, + input logic [127:0] dma_cpl_data, + input logic [7:0] dma_cpl_tag, + input logic dma_cpl_last, + + // ---- Interrupt ---- + output logic irq_msi // 1-cycle pulse when MSI fires +); + + // ------------------------------------------------------------------------ + // Device RAM: 64 KB (BAR2), 32-bit words + // ------------------------------------------------------------------------ + logic [31:0] dev_ram [0:16383]; + + // ------------------------------------------------------------------------ + // Config space software-visible state + // ------------------------------------------------------------------------ + logic [15:0] cfg_command; + logic [31:0] cfg_bar0, cfg_bar1; + logic [31:0] cfg_bar2, cfg_bar3; + logic [31:0] msi_addr_lo, msi_addr_hi; + logic [15:0] msi_data; + logic msi_enable; + + // ------------------------------------------------------------------------ + // BAR0 registers + // ------------------------------------------------------------------------ + logic [31:0] reg_scratch; + logic [31:0] reg_dma_addr_lo, reg_dma_addr_hi; + logic [31:0] reg_dma_len, reg_dma_offset; + logic reg_irq_enable; + logic reg_dma_dir; // 0 = host->device, 1 = device->host + logic irq_status_dma; // IRQ_STATUS bit0 + logic rw_start_dma; // CTRL bit0 write strobe + + // ------------------------------------------------------------------------ + // Request FSM state + // ------------------------------------------------------------------------ + typedef enum logic [1:0] { S_IDLE, S_WRITE, S_READ_CPL } state_e; + state_e state; + logic [63:0] cur_addr; + logic [9:0] beats_left; // beats remaining incl. current cpl beat + logic [7:0] cur_tag; + logic cur_is_ram; // window of the in-flight memory request + + // Window decode for a fresh request (BAR2 window = device RAM) + logic addr_is_ram_now; + assign addr_is_ram_now = (cfg_bar2 != 0) && + (req_addr[31:16] == cfg_bar2[31:16]); + + // ------------------------------------------------------------------------ + // DMA engine state + // ------------------------------------------------------------------------ + typedef enum logic [2:0] { + DMA_IDLE, DMA_H2D_REQ, DMA_H2D_CPL, DMA_D2H_REQ, DMA_DONE + } dma_state_e; + dma_state_e dma_state; + logic [31:0] dma_bytes_left; + logic [31:0] dma_cur_host; + logic [31:0] dma_cur_dev; + logic dma_busy, dma_done; + + localparam logic [7:0] DMA_TAG = 8'hA5; + + assign dma_busy = (dma_state != DMA_IDLE) && (dma_state != DMA_DONE); + assign dma_req_tag = DMA_TAG; + assign req_ready = (state == S_IDLE) || (state == S_WRITE); + assign dma_cpl_ready = (dma_state == DMA_H2D_CPL); + + // ------------------------------------------------------------------------ + // Config space read function (one DW) + // ------------------------------------------------------------------------ + function automatic logic [31:0] cfg_read(input logic [11:0] off); + unique case (off) + 12'h000: cfg_read = {PCIE_DID, PCIE_VID}; + 12'h004: cfg_read = {16'h0010, cfg_command}; + 12'h008: cfg_read = 32'h0880_0001; + 12'h00C: cfg_read = 32'h0000_0010; + 12'h010: cfg_read = cfg_bar0; + 12'h014: cfg_read = cfg_bar1; + 12'h018: cfg_read = cfg_bar2; + 12'h01C: cfg_read = cfg_bar3; + 12'h02C: cfg_read = 32'h16C3_0001; + 12'h034: cfg_read = 32'h0000_0040; + 12'h03C: cfg_read = 32'h0000_0105; + // ---- PCIe Capability (offset 0x40) ---- + 12'h040: cfg_read = {16'h0002, 8'h70, 8'h10}; + 12'h044: cfg_read = 32'h0000_0821; + 12'h048: cfg_read = 32'h0000_0000; + 12'h04C: cfg_read = {16'h0000, 2'b00, LINK_WIDTH_X16, LINK_SPEED_GEN5, + 4'h0}; // LinkCap: Gen5 x16 + 12'h050: cfg_read = {14'h0000, 2'b00, link_width, 2'b00, link_speed, + 4'h0}; // LinkCtl/LinkSta + // ---- MSI Capability (offset 0x70) ---- + 12'h070: cfg_read = {16'h8000, 8'h00, 8'h05} | + (msi_enable ? 32'h0001_0000 : 32'h0); + 12'h074: cfg_read = msi_addr_lo; + 12'h078: cfg_read = msi_addr_hi; + 12'h07C: cfg_read = {16'h0000, msi_data}; + default: cfg_read = 32'h0000_0000; + endcase + endfunction + + // Config write (one DW, full-word only in this demo) + task automatic cfg_write(input logic [11:0] off, input logic [31:0] data); + unique case (off) + 12'h004: cfg_command <= data[15:0]; + 12'h010: cfg_bar0 <= data & 32'hFFFF_F000; + 12'h014: cfg_bar1 <= data; + 12'h018: cfg_bar2 <= data & 32'hFFFF_0000; + 12'h01C: cfg_bar3 <= data; + 12'h070: msi_enable <= data[16]; + 12'h074: msi_addr_lo <= data; + 12'h078: msi_addr_hi <= data; + 12'h07C: msi_data <= data[15:0]; + default: ; + endcase + endtask + + // ------------------------------------------------------------------------ + // BAR0 register read function + // ------------------------------------------------------------------------ + function automatic logic [31:0] reg_read(input logic [11:0] off); + unique case (off) + REG_ID: reg_read = REG_ID_VALUE; + REG_VERSION: reg_read = REG_VER_VALUE; + REG_CTRL: reg_read = {29'h0, reg_dma_dir, reg_irq_enable, 1'b0}; + REG_STATUS: reg_read = {29'h0, link_up_in, dma_done, dma_busy}; + REG_SCRATCH: reg_read = reg_scratch; + REG_IRQ_STATUS: reg_read = {31'h0, irq_status_dma}; + REG_DMA_ADDR_LO: reg_read = reg_dma_addr_lo; + REG_DMA_ADDR_HI: reg_read = reg_dma_addr_hi; + REG_DMA_LEN: reg_read = reg_dma_len; + REG_DMA_OFFSET: reg_read = reg_dma_offset; + default: reg_read = 32'h0000_0000; + endcase + endfunction + + // Write one 4-DW beat into BAR0 registers or device RAM. + // The vhost passes absolute PCIe addresses; BAR bases are aligned to + // their window sizes, so addr[15:2] is the RAM word index and + // addr[11:0] is the register offset. + // n_dw: number of valid payload dwords in this beat. The vhost + // sends single-dword register writes zero-padded to a full beat; + // writing the padding would clobber the following registers (a + // W1C write to IRQ_STATUS once zeroed DMA_ADDR_LO and DMA_LEN this + // way, which silently blocked every D2H DMA start). + task automatic mem_write_beat(input logic [63:0] addr, + input logic [127:0] data, + input logic is_ram, + input int n_dw); + if (is_ram) begin + // Loop bound stays a compile-time constant (ZeBu SIMON rejects + // non-unrollable loops); n_dw gates the body instead. + for (int i = 0; i < 4; i++) + if (i < n_dw) + dev_ram[(addr[15:2] + i[13:0])] <= data[i*32 +: 32]; + end else begin + for (int i = 0; i < 4; i++) begin + if (i < n_dw) begin + unique case (addr[11:0] + i*4) + REG_SCRATCH: reg_scratch <= data[i*32 +: 32]; + REG_CTRL: begin + reg_irq_enable <= data[i*32+1]; + reg_dma_dir <= data[i*32+2]; + if (data[i*32+0]) rw_start_dma <= 1'b1; + end + REG_IRQ_STATUS: if (data[i*32+0]) irq_status_dma <= 1'b0; // W1C + REG_DMA_ADDR_LO: reg_dma_addr_lo <= data[i*32 +: 32]; + REG_DMA_ADDR_HI: reg_dma_addr_hi <= data[i*32 +: 32]; + REG_DMA_LEN: reg_dma_len <= data[i*32 +: 32]; + REG_DMA_OFFSET: reg_dma_offset <= data[i*32 +: 32]; + default: ; + endcase + end + end + end + endtask + + // Produce one completion beat for a memory read. + task automatic mem_read_beat(input logic [63:0] addr, + input logic is_ram, + input logic last_flag); + cpl_valid <= 1'b1; + cpl_tag <= cur_tag; + cpl_status <= CPL_SC; + cpl_last <= last_flag; + if (is_ram) begin + for (int i = 0; i < 4; i++) + cpl_data[i*32 +: 32] <= dev_ram[(addr[15:2] + i[13:0])]; + end else begin + for (int i = 0; i < 4; i++) + cpl_data[i*32 +: 32] <= reg_read(addr[11:0] + i*4); + end + endtask + + // ------------------------------------------------------------------------ + // Single sequential process: request FSM + DMA FSM + // ------------------------------------------------------------------------ + always_ff @(posedge clk or negedge rst_n) begin + if (!rst_n) begin + state <= S_IDLE; + cur_addr <= '0; + cur_tag <= '0; + cur_is_ram <= 1'b0; + beats_left <= '0; + cpl_valid <= 1'b0; + cpl_data <= '0; + cpl_tag <= '0; + cpl_last <= 1'b0; + cpl_status <= CPL_SC; + cfg_command <= '0; + cfg_bar0 <= '0; + cfg_bar1 <= '0; + cfg_bar2 <= '0; + cfg_bar3 <= '0; + msi_addr_lo <= '0; + msi_addr_hi <= '0; + msi_data <= '0; + msi_enable <= 1'b0; + reg_scratch <= '0; + reg_dma_addr_lo <= '0; + reg_dma_addr_hi <= '0; + reg_dma_len <= '0; + reg_dma_offset <= '0; + reg_irq_enable <= 1'b0; + reg_dma_dir <= 1'b0; + rw_start_dma <= 1'b0; + irq_status_dma <= 1'b0; + dma_state <= DMA_IDLE; + dma_done <= 1'b0; + dma_bytes_left <= '0; + dma_cur_host <= '0; + dma_cur_dev <= '0; + dma_req_valid <= 1'b0; + dma_req_kind <= TLP_MEM_RD; + dma_req_addr <= '0; + dma_req_len_dw <= '0; + dma_req_data <= '0; + dma_req_last <= 1'b0; + irq_msi <= 1'b0; + end else begin + rw_start_dma <= 1'b0; // strobe default (task may set it below) + irq_msi <= 1'b0; // pulse default (DMA_DONE may set it below) + if (cpl_valid && cpl_ready) cpl_valid <= 1'b0; + + // ------------------------------------------------------------ + // Request FSM (downstream host requests) + // ------------------------------------------------------------ + unique case (state) + S_IDLE: begin + if (req_valid) begin + cur_addr <= req_addr; + cur_tag <= req_tag; + cur_is_ram <= addr_is_ram_now; + unique case (req_kind) + TLP_CFG_WR: cfg_write(req_addr[11:0], req_data[31:0]); + TLP_CFG_RD: begin + cpl_valid <= 1'b1; + cpl_data <= {96'h0, cfg_read(req_addr[11:0])}; + cpl_tag <= req_tag; + cpl_last <= 1'b1; + cpl_status <= CPL_SC; + beats_left <= 10'd1; + state <= S_READ_CPL; + end + TLP_MEM_WR: begin + // First beat carries min(len,4) valid dwords; a + // length of 0 encodes 1024 dwords, also a full beat. + mem_write_beat(req_addr, req_data, addr_is_ram_now, + (req_len_dw == 10'd0 || req_len_dw > 10'd4) + ? 4 : int'(req_len_dw)); + if (!req_last) state <= S_WRITE; + end + TLP_MEM_RD: begin + beats_left <= (req_len_dw + 10'd3) >> 2; + mem_read_beat(req_addr, addr_is_ram_now, + ((req_len_dw + 10'd3) >> 2) <= 10'd1); + state <= S_READ_CPL; + end + endcase + end + end + S_WRITE: begin + if (req_valid) begin + cur_addr <= cur_addr + 64'd16; + mem_write_beat(cur_addr + 64'd16, req_data, cur_is_ram, 4); + if (req_last) state <= S_IDLE; + end + end + S_READ_CPL: begin + if (cpl_valid && cpl_ready) begin + if (beats_left <= 10'd1) begin + state <= S_IDLE; + end else begin + beats_left <= beats_left - 10'd1; + cur_addr <= cur_addr + 64'd16; + mem_read_beat(cur_addr + 64'd16, cur_is_ram, + (beats_left <= 10'd2)); + end + end + end + default: state <= S_IDLE; + endcase + + // ------------------------------------------------------------ + // DMA FSM (runs after the request FSM in this process, so a + // DMA completion set of irq_status wins over a same-cycle W1C) + // ------------------------------------------------------------ + unique case (dma_state) + DMA_IDLE: begin + dma_req_valid <= 1'b0; + if (rw_start_dma && (reg_dma_len != 0)) begin + dma_done <= 1'b0; + dma_bytes_left <= reg_dma_len; + dma_cur_host <= reg_dma_addr_lo; + dma_cur_dev <= reg_dma_offset; + dma_state <= reg_dma_dir ? DMA_D2H_REQ : DMA_H2D_REQ; + end + end + DMA_H2D_REQ: begin + dma_req_valid <= 1'b1; + dma_req_kind <= TLP_MEM_RD; + dma_req_addr <= {reg_dma_addr_hi, dma_cur_host}; + dma_req_len_dw <= (dma_bytes_left >= 32'd16) ? 10'd4 + : dma_bytes_left[11:2]; + dma_req_last <= 1'b1; + if (dma_req_valid && dma_req_ready) begin + dma_req_valid <= 1'b0; + dma_state <= DMA_H2D_CPL; + end + end + DMA_H2D_CPL: begin + if (dma_cpl_valid) begin + for (int i = 0; i < 4; i++) + dev_ram[(dma_cur_dev[15:2] + i[13:0])] <= dma_cpl_data[i*32 +: 32]; + dma_cur_dev <= dma_cur_dev + 32'd16; + dma_cur_host <= dma_cur_host + 32'd16; + dma_bytes_left <= (dma_bytes_left >= 32'd16) ? dma_bytes_left - 32'd16 + : 32'd0; + dma_state <= (dma_bytes_left <= 32'd16) ? DMA_DONE : DMA_H2D_REQ; + end + end + DMA_D2H_REQ: begin + dma_req_valid <= 1'b1; + dma_req_kind <= TLP_MEM_WR; + dma_req_addr <= {reg_dma_addr_hi, dma_cur_host}; + dma_req_len_dw <= (dma_bytes_left >= 32'd16) ? 10'd4 + : dma_bytes_left[11:2]; + dma_req_last <= 1'b1; + for (int i = 0; i < 4; i++) + dma_req_data[i*32 +: 32] <= dev_ram[(dma_cur_dev[15:2] + i[13:0])]; + if (dma_req_valid && dma_req_ready) begin + dma_req_valid <= 1'b0; + dma_cur_dev <= dma_cur_dev + 32'd16; + dma_cur_host <= dma_cur_host + 32'd16; + dma_bytes_left <= (dma_bytes_left >= 32'd16) ? dma_bytes_left - 32'd16 + : 32'd0; + dma_state <= (dma_bytes_left <= 32'd16) ? DMA_DONE : DMA_D2H_REQ; + end + end + DMA_DONE: begin + dma_done <= 1'b1; + irq_status_dma <= 1'b1; + if (reg_irq_enable && msi_enable) irq_msi <= 1'b1; + dma_state <= DMA_IDLE; + end + default: dma_state <= DMA_IDLE; + endcase + end + end + +endmodule : pcie5_ep_core diff --git a/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/rtl/pcie_pkg.sv b/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/rtl/pcie_pkg.sv new file mode 100644 index 0000000..2bbc92c --- /dev/null +++ b/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/rtl/pcie_pkg.sv @@ -0,0 +1,41 @@ +// PCIe Gen5 vhost demo - shared definitions +package pcie_pkg; + + // TLP request kinds used on the transaction-level interface + // between the vhost transactor and the endpoint core. + typedef enum logic [1:0] { + TLP_CFG_RD = 2'd0, + TLP_CFG_WR = 2'd1, + TLP_MEM_RD = 2'd2, + TLP_MEM_WR = 2'd3 + } tlp_kind_e; + + // Completion status + localparam logic [2:0] CPL_SC = 3'd0; // successful completion + localparam logic [2:0] CPL_UR = 3'd1; // unsupported request + localparam logic [2:0] CPL_CRS = 3'd2; // configuration retry status + + // Demo endpoint identity + localparam logic [15:0] PCIE_VID = 16'h16C3; // Synopsys vendor ID (demo) + localparam logic [15:0] PCIE_DID = 16'h5805; // demo Gen5 endpoint + + // PCIe link capability encodings (PCIe Base Spec) + localparam logic [3:0] LINK_SPEED_GEN5 = 4'd6; // 32.0 GT/s + localparam logic [5:0] LINK_WIDTH_X16 = 6'd16; + + // BAR0 register offsets + localparam logic [11:0] REG_ID = 12'h000; + localparam logic [11:0] REG_VERSION = 12'h004; + localparam logic [11:0] REG_CTRL = 12'h008; + localparam logic [11:0] REG_STATUS = 12'h00C; + localparam logic [11:0] REG_SCRATCH = 12'h010; + localparam logic [11:0] REG_IRQ_STATUS = 12'h014; + localparam logic [11:0] REG_DMA_ADDR_LO = 12'h018; + localparam logic [11:0] REG_DMA_ADDR_HI = 12'h01C; + localparam logic [11:0] REG_DMA_LEN = 12'h020; + localparam logic [11:0] REG_DMA_OFFSET = 12'h024; + + localparam logic [31:0] REG_ID_VALUE = 32'h5035_4531; // "P5E1" + localparam logic [31:0] REG_VER_VALUE = 32'h0001_0000; + +endpackage : pcie_pkg diff --git a/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/scripts/clean.csh b/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/scripts/clean.csh new file mode 100644 index 0000000..1d9c1ba --- /dev/null +++ b/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/scripts/clean.csh @@ -0,0 +1,7 @@ +#!/bin/csh -f +# 清理编译产物 +cd ${PCIE_DEMO_HOME} +rm -rf zebu.work run +cd sw/vhost && make clean +cd ${PCIE_DEMO_HOME} +echo "clean done" diff --git a/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/scripts/compile.csh b/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/scripts/compile.csh new file mode 100644 index 0000000..77259f1 --- /dev/null +++ b/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/scripts/compile.csh @@ -0,0 +1,48 @@ +#!/bin/csh -f +# 用 zCui 批处理编译 DUT + transactor 到 ZeBu Server 4 +# 调用方式与站点 ZeBu IP 示例一致: zCui -c -n -u .utf -w +# 前置:已 source scripts/env.csh + +# ---- 版本配套(写死在脚本里,避免依赖当前 shell 的状态)---- +# ZeBu Q-2020.03 的 zCui 必须用 VCS Q-2020.03-SP2-1-Z 的 UTF translator; +# 若 shell 里 vcs 指向新版(如 X-2025.06),会报 +# "Unknown option 'process_env_var' specified to register_app_var"。 +setenv VCS_HOME /eda_tools/synopsys/vcs/Q-2020.03-SP2-1-Z +setenv PATH ${VCS_HOME}/bin:${PATH} +# B2B 流程里 REMOTECMD 总是被传(可为空);UTF 里 $env(REMOTECMD) 要求它必须存在 +if (! $?REMOTECMD) setenv REMOTECMD "" +# 板卡架构文件:默认用在跑的 B2B 工程已验证的那份,shell 里已设则不覆盖 +if (! $?FILE_CONF) setenv FILE_CONF /eda_tools/synopsys/zebu/zebu_system_dir/config/zse_configuration.tcl + +if (! $?ZEBU_ROOT) then + echo "ERROR: ZEBU_ROOT 未设置,请先 source scripts/env.csh" + exit 1 +endif + +cd ${PCIE_DEMO_HOME} + +echo "=== zCui compile start: `date` ===" +echo "vcs in use: `which vcs`" +zCui -c -n -u cfg/zebu_compile.utf -w ${ZEBU_WORK} +if ($status != 0) then + echo "ERROR: zCui 编译失败,查看 ${ZEBU_WORK} 下的编译日志" + exit 1 +endif +echo "=== zCui compile done: `date` ===" + +# ---- 编译 ZEMI3 transactor 的软件胶水 (.so + .h) ---- +# 按 ZeBu 文档(User Guide 11.42)在内层 zebu.work 里执行 dpixtor.mk: +# 内层 zebu.work 才是运行时数据库目录,pcie_vhost_xtor.so / .h 生成在 +# 那里,run 阶段测试程序 -w 指向的运行时数据库就是它。 +# 生成的 Makefile 用 ZEBU_ROOT 取头文件/库;CXXFLAGS 追加旧 ABI 定义, +# 与 ZeBu 库及测试程序保持一致(站点 B2B bench 同样用 ABI=0)。 +echo "=== build ZEMI3 xtor software (dpixtor.mk) ===" +cd ${ZEBU_WORK}/zebu.work +make -f dpixtor.mk clean all CXXFLAGS="-O -D_GLIBCXX_USE_CXX11_ABI=0" +if ($status != 0) then + echo "ERROR: dpixtor.mk 编译失败" + exit 1 +endif +cd ${PCIE_DEMO_HOME} +ls -l ${ZEBU_WORK}/zebu.work/pcie_vhost_xtor.so ${ZEBU_WORK}/zebu.work/pcie_vhost_xtor.h +echo "产物目录: ${ZEBU_WORK}" diff --git a/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/scripts/env.csh b/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/scripts/env.csh new file mode 100644 index 0000000..19059e2 --- /dev/null +++ b/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/scripts/env.csh @@ -0,0 +1,53 @@ +#!/bin/csh +# ZeBu Server 4 + PCIe Gen5 vhost demo 环境设置(csh/tcsh) +# 使用前按本机实际安装路径修改 ZEBU_ROOT 与 license 设置。 + +# ---- ZeBu 安装路径 ---- +setenv ZEBU_ROOT /eda_tools/synopsys/zebu/Q-2020.03-SP1 +setenv PATH ${ZEBU_ROOT}/bin:${PATH} +if ($?LD_LIBRARY_PATH) then + setenv LD_LIBRARY_PATH ${ZEBU_ROOT}/lib:${LD_LIBRARY_PATH} +else + setenv LD_LIBRARY_PATH ${ZEBU_ROOT}/lib +endif + +# zCui 需要 libQtWebKit.so.4:ZeBu 捆绑在 ZeBu_Vivado 目录下,但只有 +# .so.4.5.3 实体文件、没有 .so.4 链接。首次使用先建一个链接目录: +# mkdir -p ~/zebu_qt_lib +# ln -s ${ZEBU_ROOT}/ZeBu_Vivado_2020.1_patched05/lib/lnx64.o/libQtWebKit.so.4.5.3 ~/zebu_qt_lib/libQtWebKit.so.4 +setenv LD_LIBRARY_PATH ~/zebu_qt_lib:${ZEBU_ROOT}/ZeBu_Vivado_2020.1_patched05/lib/lnx64.o:${LD_LIBRARY_PATH} + +# ---- 配套 VCS(重要)---- +# zCui 的 UTF translator 取自 VCS 安装。ZeBu Q-2020.03 必须配同代的 +# VCS Q-2020.03-SP2-1-Z(-Z 版);若 PATH 里的 vcs 是新版(如 X-2025.06), +# translator 会生成 zCui 不认识的 register_app_var -process_env_var 而报错。 +setenv VCS_HOME /eda_tools/synopsys/vcs/Q-2020.03-SP2-1-Z +setenv PATH ${VCS_HOME}/bin:${PATH} + +# ---- License(站点实测值,取自 zebu.work/dumpenv.txt,2026-10-31)---- +# Xilinx (Vivado Implementation/xcvu440): 2100@10.88.0.5 (xilinxd) +# Synopsys (ZeBu/VCS/zs_*): 27000@10.88.0.6 + 27020@10.88.0.5 (snpslmd) +# 缺 XILINXD 时 FPGA 编译会死在 opt_design (Common 17-345)。 +setenv XILINXD_LICENSE_FILE 2100@10.88.0.5 +setenv SNPSLMD_LICENSE_FILE 27000@10.88.0.6:27020@10.88.0.5 +setenv LM_LICENSE_FILE ${SNPSLMD_LICENSE_FILE} + +# ---- zCui UTF 入口需要的站点环境(值取自在跑的 PCIe 工程 dumpenv)---- +# FILE_CONF:板卡架构文件(architecture_file 用) +# REMOTECMD:grid 提交命令,该工程实际为空(本地执行),保持一致 +setenv FILE_CONF /eda_tools/synopsys/zebu/zebu_system_dir/config/zse_configuration.tcl +setenv REMOTECMD "" + +# ---- ZeBu IP 包(UDPI 积木 + 官方 xtor 所在,ZEMI3 流程用)---- +# 本站点无 zs_SceMi 授权,demo 走 ZEMI3/DPI(与生产 B2B 工程一致)。 +setenv ZEBU_IP_ROOT /eda_tools/synopsys/zebu_ip + +# ---- 本工程变量 ---- +setenv PCIE_DEMO_HOME `pwd` +setenv ZEBU_WORK ${PCIE_DEMO_HOME}/zebu.work + +echo "ZEBU_ROOT = ${ZEBU_ROOT}" +echo "ZEBU_IP_ROOT = ${ZEBU_IP_ROOT}" +echo "PCIE_DEMO_HOME = ${PCIE_DEMO_HOME}" +echo "ZEBU_WORK = ${ZEBU_WORK}" +which zCui diff --git a/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/scripts/run.csh b/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/scripts/run.csh new file mode 100644 index 0000000..a6f106a --- /dev/null +++ b/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/scripts/run.csh @@ -0,0 +1,63 @@ +#!/bin/csh -f +# 编译独立测试程序并运行 vhost 测试(ZEMI3/DPI 独立程序流程, +# 与站点 B2B 的 proxyTB 一致:ZEMI3Manager::open 自行开板,不走 zRci) +# 前置:已 source scripts/env.csh 且 compile.csh 执行成功 +# +# 运行目录布局(run/): +# zebu.work -> ${ZEBU_WORK}/zebu.work 的符号链接(内层是运行时 +# 数据库目录,测试程序以 -w ./zebu.work 打开) +# pcie_vhost_tb 测试程序(main 在 sw/vhost/main.cpp 底部) +# designFeatures 时钟特征文件(ZEMI3Manager::open 要求存在, +# 缺失会报 LDEF0156E/LUI3411E;内容按 cfg/designFeatures) +# 程序退出码即测试结果(0 = TEST PASSED)。 + +if (! $?ZEBU_ROOT) then + echo "ERROR: ZEBU_ROOT 未设置,请先 source scripts/env.csh" + exit 1 +endif +if (! -f ${ZEBU_WORK}/zebu.work/pcie_vhost_xtor.so) then + echo "ERROR: ${ZEBU_WORK}/zebu.work/pcie_vhost_xtor.so 不存在,请先执行 compile.csh" + exit 1 +endif + +cd ${PCIE_DEMO_HOME}/sw/vhost +make clean +make ZEBU_WORK=${ZEBU_WORK}/zebu.work +if ($status != 0) then + echo "ERROR: pcie_vhost_tb 编译失败" + exit 1 +endif + +cd ${PCIE_DEMO_HOME} +rm -rf run +mkdir run +cd run +ln -s ${ZEBU_WORK}/zebu.work zebu.work +cp ../sw/vhost/pcie_vhost_tb . +cp ../cfg/designFeatures . + +set window = 0 +if ($#argv > 0) then + if ("$argv[1]" == "-x") set window = 1 +endif +if ($window == 1) then + if (! $?DISPLAY) then + echo "ERROR: DISPLAY 未设置,无法弹出窗口(SSH 登录需带 -X/-Y 做 X 转发);请改用 ./scripts/run.csh -i 在当前终端调试" + exit 1 + endif + which xterm >& /dev/null + if ($status != 0) then + echo "ERROR: 本机没有 xterm,无法弹出窗口;请改用 ./scripts/run.csh -i" + exit 1 + endif + echo "=== 在新 xterm 窗口启动 vhost 调试控制台(在窗口里输 quit 结束;-hold 保证程序退出后窗口不关,可看报错)===" + xterm -hold -title "PCIe vhost UART debug" -e ./pcie_vhost_tb -w ./zebu.work -i & + exit 0 +endif + +echo "=== vhost test start: `date` ===" +./pcie_vhost_tb -w ./zebu.work $argv:q +set rc = $status +echo "=== vhost test end: `date` (rc=${rc}) ===" +echo "提示: 交互调试 ./scripts/run.csh -i(当前终端变 vhost> 控制台),弹窗调试 ./scripts/run.csh -x(需 DISPLAY)" +exit ${rc} diff --git a/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/sw/vhost/Makefile b/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/sw/vhost/Makefile new file mode 100644 index 0000000..96aefed --- /dev/null +++ b/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/sw/vhost/Makefile @@ -0,0 +1,43 @@ +# Standalone testbench build for the ZEMI3/DPI vhost demo. +# +# The testbench is a standalone executable (same pattern as the site +# B2B bench, ~/PCIe/C++/Makefile): it opens the board itself through +# ZEMI3Manager::open, so no zRci / tb.so is involved. It links against: +# - the generated transactor glue $(ZEBU_WORK)/pcie_vhost_xtor.so +# (built by compile.csh via dpixtor.mk; its header pcie_vhost_xtor.h +# also lives in $(ZEBU_WORK) and is included by pcie_vhost_zebu.cpp) +# - ZeBu runtime libs (libZebu, libZebuZEMI3, libZebuXtor) +# -rdynamic is required so the transactor glue can resolve the UDPI +# import functions implemented by pcie_vhost_zebu.cpp in the executable. +# ABI: ZeBu Q-2020.03 libraries are built with the pre-C++11 string ABI, +# so -D_GLIBCXX_USE_CXX11_ABI=0 is required (same as the site B2B bench). +# +# ZEBU_WORK must point at the inner zebu.work runtime database directory +# (absolute path). + +ZEBU_ROOT ?= /eda_tools/synopsys/zebu/Q-2020.03-SP1 +ZEBU_IP_ROOT ?= /eda_tools/synopsys/zebu_ip +ZEBU_WORK ?= ../../zebu.work/zebu.work + +CXX ?= g++ +CXXFLAGS ?= -O2 -g -std=gnu++11 -fPIC -fexceptions -Wall \ + -Wno-write-strings -D_GLIBCXX_USE_CXX11_ABI=0 + +TARGET := pcie_vhost_tb +SRCS := main.cpp pcie_vhost_zebu.cpp + +all: $(TARGET) + +$(TARGET): $(SRCS) pcie_vhost_api.h + $(CXX) $(CXXFLAGS) -I$(ZEBU_ROOT)/include -I$(ZEBU_IP_ROOT)/include \ + -I$(ZEBU_IP_ROOT)/XTOR/CAN_FD.Q-2021.03/include -I$(ZEBU_WORK) \ + -rdynamic -o $@ $(SRCS) \ + $(ZEBU_WORK)/pcie_vhost_xtor.so \ + -L$(ZEBU_ROOT)/lib -lZebu -lZebuZEMI3 \ + -L$(ZEBU_IP_ROOT)/lib -lZebuVpi -lZebuXtor \ + -lpthread -ldl -lrt -lz + +clean: + rm -f $(TARGET) tb.so + +.PHONY: all clean diff --git a/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/sw/vhost/main.cpp b/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/sw/vhost/main.cpp new file mode 100644 index 0000000..4eb0a42 --- /dev/null +++ b/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/sw/vhost/main.cpp @@ -0,0 +1,373 @@ +// ============================================================================ +// main.cpp - PCIe Gen5 Virtual Host demo test (ZeBu Server 4) +// +// Test sequence: +// 1. Link check : PCIe Link Status must report Gen5 (32 GT/s) x16 +// 2. Enumeration : VID/DID, class code, BAR sizing/assignment +// 3. BAR0 test : ID register + SCRATCH patterns +// 4. BAR2 test : 4 KB memory write/read compare through the vhost +// 5. DMA test : host->device and device->host, 4 KB, data compare, +// MSI on completion +// Prints TEST PASSED / TEST FAILED at the end. +// ============================================================================ +#include "pcie_vhost_api.h" + +#include +#include +#include +#include +#include + +#include "libZebuZEMI3.hh" +#include "XtorScheduler.hh" +#include "svdpi.h" + +namespace { + +int g_errors = 0; + +void check(bool cond, const char* what) { + std::printf(" [%s] %s\n", cond ? " OK " : "FAIL", what); + if (!cond) ++g_errors; +} + +// BAR assignments used by this demo +constexpr uint32_t kBar0Base = 0xF0000000u; +constexpr uint32_t kBar2Base = 0xF1000000u; + +// BAR0 register offsets (see README) +constexpr uint32_t REG_ID = 0x00, REG_CTRL = 0x08, REG_STATUS = 0x0C, + REG_SCRATCH = 0x10, REG_IRQ_STATUS = 0x14, + REG_DMA_ADDR_LO = 0x18, REG_DMA_ADDR_HI = 0x1C, + REG_DMA_LEN = 0x20, REG_DMA_OFFSET = 0x24; + +uint32_t bar0Read(PcieVHost& h, uint32_t off) { + uint32_t v = 0; + h.memRead(kBar0Base + off, &v, 1); + return v; +} +void bar0Write(PcieVHost& h, uint32_t off, uint32_t v) { + h.memWrite(kBar0Base + off, &v, 1); +} + +} // namespace + +// The demo test itself; the board connection is already up when this runs +// (see main() at the bottom of this file). +int run_pcie_demo_test() { + PcieVHost host; + std::printf("== PCIe Gen5 vhost demo (ZeBu Server 4, ZEMI3/DPI) ==\n"); + if (!host.open(nullptr)) { + std::printf("TEST FAILED (cannot connect to emulation)\n"); + return 1; + } + + // ------------------------------------------------------------------ + std::printf("[1] Link status (expect Gen5 x16)\n"); + uint32_t lnkSta = host.cfgRead(0x50) & 0xFFFF; + uint32_t speed = (lnkSta >> 4) & 0xF; + uint32_t width = (lnkSta >> 10) & 0x3F; + std::printf(" LinkSta = 0x%04x (speed=%u, width=x%u)\n", lnkSta, speed, width); + check(speed == 6, "link speed is Gen5 (32.0 GT/s)"); + check(width == 16, "link width is x16"); + + // ------------------------------------------------------------------ + std::printf("[2] Enumeration\n"); + uint32_t id = host.cfgRead(0x00); + std::printf(" VID/DID = 0x%04x/0x%04x\n", id & 0xFFFF, id >> 16); + check((id & 0xFFFF) == 0x16C3 && (id >> 16) == 0x5805, "VID/DID match demo EP"); + + // Size and assign BARs (write all-ones, read mask, then assign base) + host.cfgWrite(0x10, 0xFFFFFFFFu); + uint32_t bar0Mask = host.cfgRead(0x10); + host.cfgWrite(0x18, 0xFFFFFFFFu); + uint32_t bar2Mask = host.cfgRead(0x18); + std::printf(" BAR0 mask = 0x%08x, BAR2 mask = 0x%08x\n", bar0Mask, bar2Mask); + host.cfgWrite(0x10, kBar0Base); + host.cfgWrite(0x14, 0); // BAR0 is 64-bit: high half + host.cfgWrite(0x18, kBar2Base); + host.cfgWrite(0x1C, 0); + // Enable memory space + bus master in Command register + uint32_t cmd = host.cfgRead(0x04); + host.cfgWrite(0x04, (cmd & 0xFFFF0000u) | 0x0006u); + + // Configure MSI: enable, message address/data + host.cfgWrite(0x74, 0xFEE00000u); // MSI message address low + host.cfgWrite(0x78, 0x00000000u); // MSI message address high + host.cfgWrite(0x7C, 0x00004021u); // MSI message data + host.cfgWrite(0x70, host.cfgRead(0x70) | (1u << 16)); // MSI enable + + // ------------------------------------------------------------------ + std::printf("[3] BAR0 register test\n"); + check(bar0Read(host, REG_ID) == 0x50354531u, "ID register == 'P5E1'"); + const uint32_t patterns[] = {0x00000000u, 0xFFFFFFFFu, 0xA5A5A5A5u, + 0x12345678u}; + bool scratchOk = true; + for (uint32_t p : patterns) { + bar0Write(host, REG_SCRATCH, p); + if (bar0Read(host, REG_SCRATCH) != p) scratchOk = false; + } + check(scratchOk, "SCRATCH register patterns"); + + // ------------------------------------------------------------------ + std::printf("[4] BAR2 device RAM test (4 KB)\n"); + std::vector wbuf(1024), rbuf(1024, 0); + for (size_t i = 0; i < wbuf.size(); ++i) wbuf[i] = 0xC0FFEE00u + uint32_t(i); + host.memWrite(kBar2Base, wbuf.data(), uint32_t(wbuf.size())); + host.memRead(kBar2Base, rbuf.data(), uint32_t(rbuf.size())); + check(wbuf == rbuf, "BAR2 write/read data compare"); + + // ------------------------------------------------------------------ + std::printf("[5] DMA test (4 KB, H2D then D2H, MSI on done)\n"); + constexpr uint64_t kHostAddr = 0x100000; // in vhost memory + std::vector src(1024), dst(1024, 0), chk(1024, 0); + for (size_t i = 0; i < src.size(); ++i) src[i] = 0xD15EA5E0u + uint32_t(i) * 7; + host.hostWrite(kHostAddr, src.data(), uint32_t(src.size())); + + // Program DMA registers: host addr, length, device offset 0 + bar0Write(host, REG_DMA_ADDR_LO, uint32_t(kHostAddr & 0xFFFFFFFFu)); + bar0Write(host, REG_DMA_ADDR_HI, uint32_t(kHostAddr >> 32)); + bar0Write(host, REG_DMA_LEN, 4096); + bar0Write(host, REG_DMA_OFFSET, 0); + + // H2D: CTRL bit1 irq_en=1, bit2 dir=0, bit0 start=1 + bar0Write(host, REG_CTRL, 0x3); + check(host.waitMsi(5000), "MSI received after H2D DMA"); + check((bar0Read(host, REG_IRQ_STATUS) & 1u) != 0, "IRQ_STATUS dma_done set"); + bar0Write(host, REG_IRQ_STATUS, 1); // W1C clear + host.memRead(kBar2Base, chk.data(), uint32_t(chk.size())); + check(chk == src, "H2D DMA data landed in device RAM"); + + // D2H: dir=1, into a cleared host buffer region + constexpr uint64_t kHostAddr2 = 0x200000; + bar0Write(host, REG_DMA_ADDR_LO, uint32_t(kHostAddr2 & 0xFFFFFFFFu)); + bar0Write(host, REG_DMA_ADDR_HI, uint32_t(kHostAddr2 >> 32)); + bar0Write(host, REG_CTRL, 0x7); + check(host.waitMsi(5000), "MSI received after D2H DMA"); + bar0Write(host, REG_IRQ_STATUS, 1); + host.hostRead(kHostAddr2, dst.data(), uint32_t(dst.size())); + check(dst == src, "D2H DMA data matches device RAM content"); + + // Optional EP register dump at the end of the test (VH_DEBUG + // only): STATUS bit0=busy bit1=done, CTRL bit2=dir; handy when + // a DMA phase fails - busy=1 -> FSM stuck mid-transfer, + // busy=0/done=0 -> the DMA start never took effect. + if (std::getenv("VH_DEBUG")) { + const struct { const char* name; uint32_t off; } kRegs[] = { + {"CTRL", REG_CTRL}, {"STATUS", REG_STATUS}, + {"IRQ_STATUS", REG_IRQ_STATUS}, {"DMA_ADDR_LO", REG_DMA_ADDR_LO}, + {"DMA_LEN", REG_DMA_LEN}, {"DMA_OFFSET", REG_DMA_OFFSET}}; + for (size_t i = 0; i < sizeof(kRegs) / sizeof(kRegs[0]); ++i) + std::fprintf(stderr, "[dma] reg %-11s = 0x%08x\n", kRegs[i].name, + bar0Read(host, kRegs[i].off)); + } + + // ------------------------------------------------------------------ + host.close(); + std::printf("== %s (errors=%d) ==\n", + g_errors == 0 ? "TEST PASSED" : "TEST FAILED", g_errors); + return g_errors == 0 ? 0 : 1; +} + +// ============================================================================ +// Interactive debug shell (the "UART console" of the virtual host): +// the board stays up and the terminal becomes a command console, so the +// design can be poked by hand after (or instead of) the batch test. +// Numbers are parsed with base 0 (0x prefix = hex). +// link link status (cfg 0x50) +// crd config space read, one dword +// cwr config space write +// rd BAR2 device RAM read at offset, one dword +// wr BAR2 device RAM write at offset +// dump BAR2 read of n dwords +// test run the full batch test +// quit leave the shell (board is closed afterwards) +// ============================================================================ +int run_vhost_shell() { + PcieVHost host; + if (!host.open(nullptr)) { + std::printf("shell: cannot connect to emulation\n"); + return 1; + } + std::printf("vhost debug shell (UART console). Type 'help'.\n"); + char line[256]; + for (;;) { + std::printf("vhost> "); + std::fflush(stdout); + if (!std::fgets(line, sizeof(line), stdin)) break; // EOF + char cmd[32] = {0}; + unsigned long a1 = 0, a2 = 0; + int n = std::sscanf(line, "%31s %li %li", cmd, + (long*)&a1, (long*)&a2); + if (n < 1) continue; + if (!std::strcmp(cmd, "quit") || !std::strcmp(cmd, "exit")) break; + if (!std::strcmp(cmd, "help")) { + std::printf("link | crd | cwr | " + "rd | wr | dump | " + "test | quit\n"); + } else if (!std::strcmp(cmd, "link")) { + uint32_t s = host.cfgRead(0x50); + std::printf("cfg[0x50] = 0x%08x LinkSta=0x%04x speed=%u width=x%u\n", + s, s & 0xFFFF, (s >> 4) & 0xF, (s >> 26) & 0x3F); + } else if (!std::strcmp(cmd, "crd") && n >= 2) { + std::printf("cfg[0x%lx] = 0x%08x\n", a1, + host.cfgRead(uint32_t(a1))); + } else if (!std::strcmp(cmd, "cwr") && n >= 3) { + host.cfgWrite(uint32_t(a1), uint32_t(a2)); + std::printf("cfg[0x%lx] <- 0x%08lx\n", a1, a2); + } else if (!std::strcmp(cmd, "rd") && n >= 2) { + uint32_t v = 0; + host.memRead(kBar2Base + uint32_t(a1), &v, 1); + std::printf("bar2[0x%lx] = 0x%08x\n", a1, v); + } else if (!std::strcmp(cmd, "wr") && n >= 3) { + uint32_t v = uint32_t(a2); + host.memWrite(kBar2Base + uint32_t(a1), &v, 1); + std::printf("bar2[0x%lx] <- 0x%08lx\n", a1, a2); + } else if (!std::strcmp(cmd, "dump") && n >= 3) { + std::vector buf; + buf.resize(size_t(a2)); + host.memRead(kBar2Base + uint32_t(a1), buf.data(), uint32_t(a2)); + for (unsigned long i = 0; i < a2; ++i) { + if (i % 4 == 0) std::printf(" 0x%06lx:", a1 + i * 4); + std::printf(" %08x", buf[size_t(i)]); + if (i % 4 == 3 || i + 1 == a2) std::printf("\n"); + } + } else if (!std::strcmp(cmd, "test")) { + run_pcie_demo_test(); + } else { + std::printf("unknown command, type 'help'\n"); + } + } + host.close(); + return 0; +} + +// ============================================================================ +// Field-mapping probe (-p): config reads whose (addr, tag) are chosen +// so the endpoint's response identifies what it actually saw - the +// returned dword is a distinct constant per config offset, and the +// completion tag echoes the request tag the endpoint received. +// Reference constants: 0x00=580516c3 0x04=00100000 0x08=08800001 +// 0x0C=00000010 0x2C=16c30001 0x34=00000040 0x3C=00000105 +// 0x40=00027010 0x44=00000821 0x4C=00004060 0x50=00004060 (others 0) +// ============================================================================ +int run_probe() { + PcieVHost host; + if (!host.open(nullptr)) { + std::printf("probe: cannot connect to emulation\n"); + return 1; + } + std::printf("== field-mapping probe ==\n"); + for (int i = 0; i <= 20; ++i) { + uint32_t addr = uint32_t(i * 4); + uint8_t tag = uint8_t(0x10 + i); + uint8_t ctag = 0xEE; + uint32_t raw[8] = {0, 0, 0, 0, 0, 0, 0, 0}; + uint32_t data = host.probeCfgRead(addr, tag, ctag, raw); + std::printf("probe sent addr=0x%02x tag=0x%02x -> data=0x%08x " + "cpltag=0x%02x raw=%08x %08x %08x %08x %08x %08x %08x %08x\n", + addr, tag, data, ctag, + raw[0], raw[1], raw[2], raw[3], + raw[4], raw[5], raw[6], raw[7]); + std::fflush(stdout); + } + host.close(); + std::printf("== probe done ==\n"); + return 0; +} + +// ============================================================================ +// Standalone ZEMI3 driver (same pattern as the site B2B bench, +// ~/PCIe/C++/proxyTBDpi.cc): ZEMI3Manager::open -> buildXtorList -> init +// -> start, then run the test. No zRci, no XtorScheduler addXtor: the +// manager loads the transactors listed in xtor_dpi.lst itself, so +// registering them again from user code would double-connect the ports. +// +// Usage: pcie_vhost_tb [-w zebuWork] [-f designFeatures] [-i] +// -w path to the runtime database (the inner zebu.work directory) +// -f path to the designFeatures file (defaults are used when absent) +// -i interactive debug shell instead of the batch test (the board +// stays up; the terminal acts as the vhost UART console) +// The process exit code is the test result (0 = TEST PASSED). +// ============================================================================ +int main(int argc, char** argv) { + const char* zebuWork = "zebu.work"; + const char* designFeatures = "designFeatures"; + bool interactive = false; + bool probe = false; + int opt; + while ((opt = getopt(argc, argv, "w:f:ip")) != -1) { + switch (opt) { + case 'w': zebuWork = optarg; break; + case 'f': designFeatures = optarg; break; + case 'i': interactive = true; break; + case 'p': probe = true; break; + default: + std::fprintf(stderr, + "usage: %s [-w zebuWork] [-f designFeatures] [-i]\n", + argv[0]); + return 2; + } + } + + ZEMI3Manager* zemi3 = ZEMI3Manager::open(zebuWork, designFeatures); + if (zemi3 == nullptr) { + std::fprintf(stderr, "[vh] FATAL: ZEMI3Manager::open(%s) failed\n", zebuWork); + return 2; + } + zemi3->buildXtorList(); // load the xtors from xtor_dpi.lst + sleep(2); + + // Scheduler parameters, same values as the site bench. The scheduler + // itself is only needed by DPI task-based xtors; the UDPI callbacks of + // this demo are dispatched by the ZEMI3 runtime directly. + ZEBU_IP::XtorScheduler* sched = ZEBU_IP::XtorScheduler::get(); + ZEBU_IP::XtorSchedParams_t* params = sched->getDefaultParams(); + params->useVcsSimulation = false; + params->useZemiManager = true; + params->noParams = true; + params->zebuInitCb = nullptr; + params->zebuInitCbParams = nullptr; + + if (zemi3->init()) { + std::fprintf(stderr, "[vh] FATAL: ZEMI3Manager::init failed\n"); + zemi3->terminate(); + zemi3->close(); + return 2; + } + + // Set the DPI scope of this thread to the transactor instance + // (ZeBu User Guide 11.6.1: "Before calling an export function, the + // current scope has to be properly set ... using svSetScope"). + // The scope is only registered once init() has connected the + // transactor ports, so this must come after init(). Without it, + // the first export call aborts with ZXTOR0030F. + const char* scopeNames[] = {"pcie5_demo_top.u_xtor.u_s2h_fifo", + "pcie5_demo_top.u_xtor.u_rst_clk", + "pcie5_demo_top.u_xtor.u_h2s_fifo", + "pcie5_demo_top.u_xtor"}; + bool scopeSet = false; + for (const char* name : scopeNames) { + if (svScope s = svGetScopeFromName(name)) { + svSetScope(s); + std::fprintf(stderr, "[vh] DPI scope set to %s\n", name); + scopeSet = true; + break; + } + } + if (!scopeSet) { + std::fprintf(stderr, "[vh] WARNING: no DPI scope found for the xtor\n"); + } + + zemi3->start(); + std::fprintf(stderr, "[vh] board up, transactor initialized\n"); + + int rc; + if (probe) rc = run_probe(); + else if (interactive) rc = run_vhost_shell(); + else rc = run_pcie_demo_test(); + std::fflush(stdout); + zemi3->terminate(); + zemi3->close(); + return rc; +} diff --git a/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/sw/vhost/pcie_vhost_api.h b/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/sw/vhost/pcie_vhost_api.h new file mode 100644 index 0000000..b58626d --- /dev/null +++ b/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/sw/vhost/pcie_vhost_api.h @@ -0,0 +1,51 @@ +// ============================================================================ +// pcie_vhost_api.h +// High-level C++ API of the PCIe Gen5 Virtual Host used by the demo test. +// The backend (pcie_vhost_zebu.cpp) talks ZEMI3/DPI (UDPI FIFOs) to the +// transactor in ZeBu and models the virtual host memory used by the +// DUT's DMA engine. +// ============================================================================ +#pragma once + +#include +#include + +class PcieVHost { +public: + PcieVHost(); + ~PcieVHost(); + + // Prepare the host model. The board connection itself is owned by + // main() (ZEMI3Manager) and is brought up before the test starts; + // the argument is unused in this backend. + bool open(const char* unused); + void close(); + + // PCIe configuration space access (offset = byte offset, 32-bit DWORDs). + uint32_t cfgRead (uint32_t offset); + void cfgWrite(uint32_t offset, uint32_t data); + + // Debug probe: config read with an explicit tag; reports the + // completion tag and the raw 256-bit completion words. Used by the + // field-mapping probe (pcie_vhost_tb -p) to see exactly which + // (addr, tag) the endpoint acted on. + uint32_t probeCfgRead(uint32_t offset, uint8_t tag, uint8_t& cplTag, + uint32_t* rawW); + + // Memory space access. addr is the absolute PCIe address + // (BAR base + offset). Data is transferred in 4-DW beats. + void memWrite(uint64_t addr, const uint32_t* data, uint32_t lenDw); + void memRead (uint64_t addr, uint32_t* data, uint32_t lenDw); + + // Virtual host memory backing store (used as DMA source/destination). + void hostWrite(uint64_t addr, const uint32_t* data, uint32_t lenDw); + void hostRead (uint64_t addr, uint32_t* data, uint32_t lenDw); + + // Wait for an MSI from the DUT; pumps the DMA service while waiting. + // Returns true if an MSI arrived within timeoutMs. + bool waitMsi(uint32_t timeoutMs); + +private: + struct Impl; + Impl* impl_; +}; diff --git a/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/sw/vhost/pcie_vhost_zebu.cpp b/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/sw/vhost/pcie_vhost_zebu.cpp new file mode 100644 index 0000000..6e219c2 --- /dev/null +++ b/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/sw/vhost/pcie_vhost_zebu.cpp @@ -0,0 +1,431 @@ +// ============================================================================ +// pcie_vhost_zebu.cpp +// ZEMI3/DPI backend of the PCIe Gen5 Virtual Host. +// +// The site has no SCE-MI runtime license (zs_SceMi), so the SW side uses +// the ZEMI3/DPI flow of the site's production B2B design: +// - This code is linked into the standalone testbench executable, +// which opens the board itself via ZEMI3Manager (see main()). +// - HW -> SW: the transactor's hwtosw UDPI FIFO calls the DPI import +// ZEBU_VS_SNPS_UDPI__output_read_hw(), implemented below; messages +// are queued for the test thread. +// - SW -> HW: the test thread calls the DPI export +// ZEBU_VS_SNPS_UDPI__fromSwtoHwData() to push messages into the +// transactor's swtohw UDPI FIFO. +// - Function prototypes come from the generated header +// "pcie_vhost_xtor.h" (produced by zCui/dpixtor.mk into zebu.work), +// so any signature drift is a compile error here, not a guess. +// - The rst/clk UDPI imports (clkTick/unlockSw/rst_assert/rst_deassert) +// are implemented as no-ops; clocks free-run once the manager is +// started and the UDPI export machinery synchronizes SW/HW. +// +// Unified 256-bit message format (must match tb/pcie_vhost_xtor.sv): +// bits [255:248] = message type +// SW -> HW : 0 = TLP request, 1 = DMA completion +// HW -> SW : 0 = completion, 1 = DMA request, 2 = MSI event +// TLP request layout: [1:0] kind, [9:2] tag, [19:10] len_dw, +// [83:20] addr, [211:84] data, [212] last +// Completion layout: [127:0] data, [135:128] tag, [136] last, +// [139:137] status +// Note the request data field starts at bit 84 (not word aligned); +// the putField/getField helpers below handle the crossing. +// ============================================================================ +#include "pcie_vhost_api.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// Generated ZEMI3 transactor header (in zebu.work, added via -I): +// declares the UDPI import/export functions used below, includes +// svdpi.h and libZebuZEMI3.hh. +#include "pcie_vhost_xtor.h" + +namespace { + +// TLP kinds (mirror of pcie_pkg::tlp_kind_e) +enum TlpKind : uint32_t { + TLP_CFG_RD = 0, TLP_CFG_WR = 1, TLP_MEM_RD = 2, TLP_MEM_WR = 3 +}; + +constexpr uint64_t kHostMemSize = 16ull * 1024 * 1024; // 16 MB vhost memory +constexpr int kXferTimeoutMs = 30000; + +// Message types at bits [255:248] +constexpr uint32_t kMsgReq = 0; // SW->HW TLP request / HW->SW completion +constexpr uint32_t kMsgDmaCpl = 1; // SW->HW DMA completion +constexpr uint32_t kMsgDmaReq = 1; // HW->SW DMA request +constexpr uint32_t kMsgIrq = 2; // HW->SW MSI event + +struct Msg { uint32_t w[8]; }; // one 256-bit message + +uint32_t msgType(const Msg& m) { return (m.w[7] >> 24) & 0xFFu; } + +// ---- HW -> SW receive queues (filled from DPI import callbacks) ---------- +std::mutex g_mtx; +std::condition_variable g_cv; +std::queue g_cplQ; // completions for host requests +std::queue g_dmaReqQ; // DMA requests from the DUT +bool g_msiPending = false; + +// Verbose [dbg]/[dma] traces are off by default; setenv VH_DEBUG 1 +// re-enables them (raw message dumps, DMA service counters, MSI +// arrivals, waitMsi timeout accounting). They were the bring-up +// diagnostics for the s2h data-path and D2H investigations. +bool vhDebug() { + static const bool on = std::getenv("VH_DEBUG") != nullptr; + return on; +} + +// DMA service counters (read by the vhDebug-gated prints). +std::atomic g_dmaRdCnt{0}, g_dmaWrCnt{0}, g_msiCnt{0}; + +void pushRx(const uint32_t* w) { + static int rxDbg = 0; + if (vhDebug() && rxDbg < 8) { + std::fprintf(stderr, + "[dbg] RX%d %08x %08x %08x %08x %08x %08x %08x %08x\n", rxDbg, + w[0], w[1], w[2], w[3], w[4], w[5], w[6], w[7]); + ++rxDbg; + } + Msg m; + std::memcpy(m.w, w, sizeof(m.w)); + { + std::lock_guard lk(g_mtx); + switch (msgType(m)) { + case kMsgIrq: g_msiPending = true; + ++g_msiCnt; + if (vhDebug()) + std::fprintf(stderr, "[dma] MSI event arrived (total %ld)\n", + long(g_msiCnt)); + break; + case kMsgDmaReq: g_dmaReqQ.push(m); break; + default: g_cplQ.push(m); break; + } + } + g_cv.notify_all(); +} + +bool popQueue(std::queue& q, Msg& out) { + std::lock_guard lk(g_mtx); + if (q.empty()) return false; + out = q.front(); + q.pop(); + return true; +} + +// ---- DPI scope ------------------------------------------------------------- +// Export calls must run with the DPI scope of the leaf module that owns +// the export set on the calling thread (ZeBu User Guide 11.6.1). Each +// UDPI submodule has its OWN scope: fromSwtoHwData belongs to +// u_s2h_fifo only. main() resolves that scope by name after init() and +// svSetScope()s it on the test thread; do NOT substitute a scope +// captured from an import callback (those live in u_rst_clk / +// u_h2s_fifo) - calling the export under a foreign scope aborts with +// ZXTOR0031F "Exported function not found in current scope". + +// ---- bit-field helpers over a little-endian word array ------------------- +void putField(uint32_t* w, unsigned lsb, uint32_t value) { + unsigned wi = lsb / 32, off = lsb % 32; + w[wi] |= value << off; + if (off) w[wi + 1] |= value >> (32 - off); +} +uint32_t getField(const uint32_t* w, unsigned lsb) { + unsigned wi = lsb / 32, off = lsb % 32; + uint32_t r = w[wi] >> off; + if (off) r |= w[wi + 1] << (32 - off); + return r; +} + +// ---- request/completion packing (offsets per xtor) ------------------------ +Msg packReq(uint32_t kind, uint8_t tag, uint32_t lenDw, uint64_t addr, + const uint32_t* data4dw, bool last) { + Msg m{}; + m.w[0] = (kind & 3u) | (uint32_t(tag) << 2) | ((lenDw & 0x3FFu) << 10); + putField(m.w, 20, uint32_t(addr)); // addr[31:0] -> bits [51:20] + putField(m.w, 52, uint32_t(addr >> 32)); // addr[63:32] -> bits [83:52] + if (data4dw) { + putField(m.w, 84, data4dw[0]); + putField(m.w, 116, data4dw[1]); + putField(m.w, 148, data4dw[2]); + putField(m.w, 180, data4dw[3]); + } + if (last) m.w[6] |= (1u << 20); // bit 212 + return m; // w[7] = 0 -> mtype 0 +} + +void unpackReq(const Msg& m, uint32_t& kind, uint8_t& tag, uint32_t& lenDw, + uint64_t& addr, uint32_t* data4dw) { + kind = m.w[0] & 3u; + tag = uint8_t((m.w[0] >> 2) & 0xFFu); + lenDw = (m.w[0] >> 10) & 0x3FFu; + addr = uint64_t(getField(m.w, 20)) | (uint64_t(getField(m.w, 52)) << 32); + if (data4dw) { + data4dw[0] = getField(m.w, 84); + data4dw[1] = getField(m.w, 116); + data4dw[2] = getField(m.w, 148); + data4dw[3] = getField(m.w, 180); + } +} + +Msg packCpl(const uint32_t* data4dw, uint8_t tag, bool last, + uint32_t status, uint32_t mtype) { + Msg m{}; + if (data4dw) std::memcpy(m.w, data4dw, 16); + m.w[4] = uint32_t(tag) | (last ? (1u << 8) : 0u) | ((status & 7u) << 9); + m.w[7] = (mtype & 0xFFu) << 24; // bits [255:248] + return m; +} + +void unpackCpl(const Msg& m, uint32_t* data4dw, uint8_t& tag, + bool& last, uint32_t& status) { + if (data4dw) std::memcpy(data4dw, m.w, 16); + tag = uint8_t(m.w[4] & 0xFFu); + last = ((m.w[4] >> 8) & 1u) != 0; + status = (m.w[4] >> 9) & 7u; +} + +// Send one 256-bit message to the HW transactor (blocking DPI export; +// valid_size is in DATA_WIDTH units -> 1 for one 256-bit message). +// NOTE: an earlier word-reversal experiment (r9) was DISPROVEN by the +// message accounting (with reversal every message degenerated into a +// config read: 804 imports ~= 805 exports; without it, writes stay +// posted: 533 ~= read beats only). The field corruption is subtler; +// PcieVHost::probeCfgRead (main -p) maps it exactly. Send raw order. +void sendToHw(const Msg& m) { + static int txDbg = 0; + if (vhDebug() && txDbg < 8) { + std::fprintf(stderr, + "[dbg] TX%d %08x %08x %08x %08x %08x %08x %08x %08x\n", txDbg, + m.w[0], m.w[1], m.w[2], m.w[3], m.w[4], m.w[5], m.w[6], m.w[7]); + ++txDbg; + } + ZEBU_VS_SNPS_UDPI__fromSwtoHwData( + reinterpret_cast(m.w), 1, 0); +} + +} // namespace + +// ============================================================================ +// DPI import functions implemented by the SW side. +// Called by the HW transactor (through the generated ZEMI3 glue) on the +// runtime's service thread; keep them short and non-blocking. +// ============================================================================ +extern "C" void ZEBU_VS_SNPS_UDPI__output_read_hw( + const svBitVecVal* output_data, int valid_data_size, int queue_num) { + (void)valid_data_size; + (void)queue_num; + uint32_t w[8]; + std::memcpy(w, output_data, sizeof(w)); + pushRx(w); +} + +extern "C" void ZEBU_VS_SNPS_UDPI__clkTick() {} +extern "C" void ZEBU_VS_SNPS_UDPI__unlockSw() {} +extern "C" int ZEBU_VS_SNPS_UDPI__rst_assert() { return 0; } +extern "C" int ZEBU_VS_SNPS_UDPI__rst_deassert() { return 0; } + +// ============================================================================ +struct PcieVHost::Impl { + std::vector hostMem; // virtual host memory + uint8_t nextTag = 1; + bool opened = false; + + // Service one pending DMA request from the DUT, if any. + void serviceDma() { + Msg req; + while (popQueue(g_dmaReqQ, req)) { + uint32_t kind, lenDw; uint8_t tag; uint64_t addr; + uint32_t data[4]; + unpackReq(req, kind, tag, lenDw, addr, data); + (void)tag; (void)lenDw; + if (kind == TLP_MEM_WR) { + // DUT writes host memory: one beat per message in this demo + long n = ++g_dmaWrCnt; + if (vhDebug() && (n <= 4 || (n % 64) == 0)) + std::fprintf(stderr, + "[dma] WR #%ld addr=0x%llx lenDw=%u data=%08x %08x %08x %08x\n", + n, (unsigned long long)addr, lenDw, + data[0], data[1], data[2], data[3]); + if (addr + 16 <= kHostMemSize) + std::memcpy(&hostMem[size_t(addr)], data, 16); + } else if (kind == TLP_MEM_RD) { + ++g_dmaRdCnt; + uint32_t cdata[4] = {0, 0, 0, 0}; + if (addr + 16 <= kHostMemSize) + std::memcpy(cdata, &hostMem[size_t(addr)], 16); + sendToHw(packCpl(cdata, 0xA5, true, 0, kMsgDmaCpl)); + } + } + } + + // Field-mapping probe: one config read with a caller-chosen tag; + // returns the completion's data dword and reports the completion + // tag and the raw completion words, so the caller can see exactly + // which (addr, tag) the endpoint acted on. + uint32_t probeRead(uint64_t addr, uint8_t tag, uint8_t& ctagOut, + uint32_t* rawW) { + sendToHw(packReq(TLP_CFG_RD, tag, 1, addr, nullptr, true)); + auto deadline = std::chrono::steady_clock::now() + + std::chrono::milliseconds(kXferTimeoutMs); + Msg cpl; + bool got = false; + while (!got) { + serviceDma(); + { + std::unique_lock lk(g_mtx); + if (g_cv.wait_for(lk, std::chrono::milliseconds(10), + [] { return !g_cplQ.empty(); })) { + cpl = g_cplQ.front(); + g_cplQ.pop(); + got = true; + } + } + if (!got && std::chrono::steady_clock::now() > deadline) { + std::fprintf(stderr, "probe: completion timeout\n"); + ctagOut = 0xFF; + if (rawW) std::memset(rawW, 0, 32); + return 0xDEAD0000u; + } + } + uint8_t ctag; bool clast; uint32_t cst; + uint32_t data[4] = {0, 0, 0, 0}; + unpackCpl(cpl, data, ctag, clast, cst); + (void)clast; (void)cst; + ctagOut = ctag; + if (rawW) std::memcpy(rawW, cpl.w, 32); + return data[0]; + } + + // Generic request/completion exchange: one request beat per message; + // for reads, collects the completion beats into rdData. + void xfer(uint32_t kind, uint64_t addr, const uint32_t* wrData, + uint32_t* rdData, uint32_t lenDw) { + uint8_t tag = nextTag++; + uint32_t doneDw = 0; + while (doneDw < lenDw) { + uint32_t beatDw = (lenDw - doneDw > 4) ? 4 : (lenDw - doneDw); + bool last = (doneDw + beatDw == lenDw); + // packReq always reads 4 data dwords; hand it a zero-padded + // beat buffer so short final beats never read past the + // caller's data (cfgWrite passes a single dword!). + uint32_t beat[4] = {0, 0, 0, 0}; + if (wrData) std::memcpy(beat, wrData + doneDw, beatDw * 4); + sendToHw(packReq(kind, tag, lenDw, addr + doneDw * 4, + wrData ? beat : nullptr, last)); + if (kind == TLP_CFG_RD || kind == TLP_MEM_RD) { + auto deadline = std::chrono::steady_clock::now() + + std::chrono::milliseconds(kXferTimeoutMs); + Msg cpl; + bool got = false; + while (!got) { + serviceDma(); + { + std::unique_lock lk(g_mtx); + if (g_cv.wait_for(lk, std::chrono::milliseconds(10), + [] { return !g_cplQ.empty(); })) { + cpl = g_cplQ.front(); + g_cplQ.pop(); + got = true; + } + } + if (!got && std::chrono::steady_clock::now() > deadline) { + std::fprintf(stderr, + "xfer: completion timeout (kind=%u addr=0x%llx)\n", + kind, (unsigned long long)addr); + if (rdData) rdData[doneDw] = 0; + return; + } + } + uint8_t ctag; bool clast; uint32_t cst; + // unpackCpl() copies a fixed 16 bytes; never let it + // write straight into the caller's buffer - a 1-dword + // read (bar0Read/cfgRead pass a single uint32_t) would + // smash 12 bytes of the caller's stack. Stage through + // a local beat buffer and copy only beatDw dwords. + uint32_t cdata[4] = {0, 0, 0, 0}; + unpackCpl(cpl, cdata, ctag, clast, cst); + if (rdData) std::memcpy(rdData + doneDw, cdata, beatDw * 4); + } + doneDw += beatDw; + } + } +}; + +PcieVHost::PcieVHost() : impl_(new Impl()) {} +PcieVHost::~PcieVHost() { close(); delete impl_; } + +bool PcieVHost::open(const char* /*unused: board is owned by main()*/) { + // The board connection and transactor init happen in main() + // (ZEMI3Manager::open/init/start) before the test starts. + impl_->hostMem.assign(size_t(kHostMemSize), 0); + { + std::lock_guard lk(g_mtx); + while (!g_cplQ.empty()) g_cplQ.pop(); + while (!g_dmaReqQ.empty()) g_dmaReqQ.pop(); + g_msiPending = false; + } + impl_->opened = true; + // Let link training inside the DUT finish (32 clocks after reset). + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + return true; +} + +void PcieVHost::close() { impl_->opened = false; } + +uint32_t PcieVHost::probeCfgRead(uint32_t offset, uint8_t tag, + uint8_t& cplTag, uint32_t* rawW) { + return impl_->probeRead(offset, tag, cplTag, rawW); +} + +uint32_t PcieVHost::cfgRead(uint32_t offset) { + uint32_t data = 0; + impl_->xfer(TLP_CFG_RD, offset, nullptr, &data, 1); + return data; +} +void PcieVHost::cfgWrite(uint32_t offset, uint32_t data) { + impl_->xfer(TLP_CFG_WR, offset, &data, nullptr, 1); +} +void PcieVHost::memWrite(uint64_t addr, const uint32_t* data, uint32_t lenDw) { + impl_->xfer(TLP_MEM_WR, addr, data, nullptr, lenDw); +} +void PcieVHost::memRead(uint64_t addr, uint32_t* data, uint32_t lenDw) { + impl_->xfer(TLP_MEM_RD, addr, nullptr, data, lenDw); +} + +void PcieVHost::hostWrite(uint64_t addr, const uint32_t* data, uint32_t lenDw) { + if (addr + lenDw * 4 <= kHostMemSize) + std::memcpy(&impl_->hostMem[size_t(addr)], data, lenDw * 4); +} +void PcieVHost::hostRead(uint64_t addr, uint32_t* data, uint32_t lenDw) { + if (addr + lenDw * 4 <= kHostMemSize) + std::memcpy(data, &impl_->hostMem[size_t(addr)], lenDw * 4); +} + +bool PcieVHost::waitMsi(uint32_t timeoutMs) { + auto deadline = std::chrono::steady_clock::now() + + std::chrono::milliseconds(timeoutMs); + while (std::chrono::steady_clock::now() < deadline) { + impl_->serviceDma(); + { + std::lock_guard lk(g_mtx); + if (g_msiPending) { + g_msiPending = false; + return true; + } + } + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + if (vhDebug()) + std::fprintf(stderr, "[dma] waitMsi TIMEOUT: serviced rd=%ld wr=%ld, msiEvents=%ld\n", + long(g_dmaRdCnt), long(g_dmaWrCnt), long(g_msiCnt)); + return false; +} diff --git a/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/tb/pcie_vhost_xtor.sv b/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/tb/pcie_vhost_xtor.sv new file mode 100644 index 0000000..b668866 --- /dev/null +++ b/zebu_pcie5_vhost_demo_complete_20261101/zebu_pcie5_vhost_demo/tb/pcie_vhost_xtor.sv @@ -0,0 +1,287 @@ +// ============================================================================ +// pcie_vhost_xtor.sv (ZEMI3/DPI version) +// ZEMI3 transactor bridging the software Virtual Host (C++ running in the +// zRci runtime as tb.so) and the PCIe Gen5 endpoint core in ZeBu Server 4. +// +// The site has no SCE-MI runtime license (zs_SceMi), so this demo follows +// the site's production ZEMI3/DPI flow (same pattern as the official +// xtor_pcie_svs B2B design): +// - This module carries (* zebu_zemi3_xtor *); zCui/zemi3 auto-detects it +// and generates the C++ glue (.h / .so via dpixtor.mk). +// - HW/SW communication uses Synopsys UDPI building blocks from +// $ZEBU_IP_ROOT/vlog/vcs: +// zebu_vs_simple_rst_and_clk_udpi - SW clock/reset services +// zebu_vs_simple_swtohw_fifo_udpi - SW -> HW message FIFO +// zebu_vs_simple_hwtosw_fifo_udpi - HW -> SW message FIFO +// - DUT clock/reset come from a zceiClockPort in the top level. +// +// Both directions use unified 256-bit messages; bits [255:248] are the +// message type (must match sw/vhost/pcie_vhost_zebu.cpp): +// SW -> HW : 0 = TLP request (legacy request layout in [212:0]) +// 1 = DMA completion (legacy completion layout in [143:0]) +// HW -> SW : 0 = completion (legacy completion layout in [139:0]) +// 1 = DMA request (legacy request layout in [212:0]) +// 2 = MSI event +// Legacy request layout: [1:0] kind, [9:2] tag, [19:10] len_dw, +// [83:20] addr (64b), [211:84] data, [212] last +// Legacy completion layout: [127:0] data, [135:128] tag, [136] last, +// [139:137] status +// ============================================================================ +`timescale 1ns/1ps + +(* zebu_zemi3_xtor *) +module pcie_vhost_xtor + import pcie_pkg::*; +( + input logic dut_clk, + input logic dut_creset, // active high, from zceiClockPort + output logic rst_n, + output logic [3:0] link_speed, + output logic [5:0] link_width, + output logic link_up, + + // DUT downstream request channel + output logic req_valid, + input logic req_ready, + output tlp_kind_e req_kind, + output logic [63:0] req_addr, + output logic [9:0] req_len_dw, + output logic [127:0] req_data, + output logic req_last, + output logic [7:0] req_tag, + + // DUT completion channel + input logic cpl_valid, + output logic cpl_ready, + input logic [127:0] cpl_data, + input logic [7:0] cpl_tag, + input logic cpl_last, + input logic [2:0] cpl_status, + + // DUT DMA request channel (EP -> host) + input logic dma_req_valid, + output logic dma_req_ready, + input tlp_kind_e dma_req_kind, + input logic [63:0] dma_req_addr, + input logic [9:0] dma_req_len_dw, + input logic [127:0] dma_req_data, + input logic dma_req_last, + input logic [7:0] dma_req_tag, + + // DUT DMA completion channel (host -> EP) + output logic dma_cpl_valid, + input logic dma_cpl_ready, + output logic [127:0] dma_cpl_data, + output logic [7:0] dma_cpl_tag, + output logic dma_cpl_last, + + input logic irq_msi +); + + assign rst_n = ~dut_creset; + + // ------------------------------------------------------------------ + // Virtual link training: link comes up at Gen5 x16 shortly after reset + // ------------------------------------------------------------------ + logic [5:0] link_cnt; + always_ff @(posedge dut_clk or posedge dut_creset) begin + if (dut_creset) begin + link_cnt <= '0; + link_up <= 1'b0; + link_speed <= 4'd0; + link_width <= 6'd0; + end else if (!link_up) begin + link_cnt <= link_cnt + 6'd1; + if (link_cnt == 6'd31) begin + link_up <= 1'b1; + link_speed <= LINK_SPEED_GEN5; // 32.0 GT/s + link_width <= LINK_WIDTH_X16; // x16 + end + end + end + + // ------------------------------------------------------------------ + // RST/CLK UDPI: gives the SW side clock counting, reset notification + // (rst_assert/rst_deassert imports) and runClk_sw clock control. + // ------------------------------------------------------------------ + logic [63:0] clk_cycle; + logic reset_done; + zebu_vs_simple_rst_and_clk_udpi u_rst_clk ( + .clock (dut_clk), + .active_high_reset (dut_creset), + .reset_done (reset_done), + .clkCycle (clk_cycle) + ); + + // ------------------------------------------------------------------ + // SW -> HW FIFO (UDPI). One 256-bit message per SW write + // (DATA_WIDTH=256, DPI_MSG_DATA_MULTIPLE=1, valid_size=1 per message). + // Note: buf_data_o is a registered (1-cycle-latency) read of the + // FIFO head; it lags buf_empty by one cycle after a write into an + // empty FIFO. The consumer below qualifies it with a head_ok flag. + // ------------------------------------------------------------------ + logic [255:0] s2h_data; + logic s2h_empty, s2h_full, s2h_rd_en; + zebu_vs_simple_swtohw_fifo_udpi #( + .DATA_WIDTH (256), + .DPI_MSG_DATA_MULTIPLE (1), + .BUFFER_SIZE_DATA_MULTIPLE (64) + ) u_s2h_fifo ( + .clk (dut_clk), + .rst (dut_creset), + .buf_rd_en (s2h_rd_en), + .buf_data_o(s2h_data), + .buf_full (s2h_full), + .buf_empty (s2h_empty) + ); + + // Head-of-FIFO consumption. The UDPI swtohw FIFO read port is the + // subtle part of this design; its exact semantics (from the vendor + // sources zebu_vs_from_sw_fifo / vs_fifo_udpi / vs_memory) are: + // radd = buf_rd_en ? (rd_pointer + 1) : rd_pointer (combinational) + // buf_data_o is REGISTERED: data_o(cycle N) = mem[radd(cycle N-1)] + // Consequences: + // - After a write into an EMPTY fifo, buf_empty deasserts one + // cycle BEFORE buf_data_o shows the new head (the data_o + // register still holds the pre-write value, 0, for one cycle). + // Capturing in that first non-empty cycle latches a zero word + // and pops the real message away unseen. (This bug cost two + // board compiles: every strictly-serialised request hits it, + // because the FIFO drains to empty between messages.) + // - After a pop cycle (buf_rd_en high), the next head is valid + // on buf_data_o in the immediately following cycle. + // head_ok therefore qualifies buf_data_o: it is 0 during the first + // non-empty cycle after an empty period and 1 whenever buf_data_o + // is known to show the true head. A second qualification, + // data_settled (buf_data_o unchanged from last cycle), covers the + // remaining read-after-write corner: a write that lands in the + // head+1 slot in the same cycle as a pop makes buf_data_o show the + // pre-write value (0) for one cycle even though head_ok is 1 + // (streaming posted writes can hit this). Capture only when both + // qualifications hold (head_ok=1, data settled, rd_en low), then + // pulse buf_rd_en for one clock to pop. + logic head_ok; + logic [255:0] s2h_data_q; + logic req_h_valid; + logic [255:0] req_h_msg; + logic cpl_h_valid; + logic [143:0] cpl_h_msg; + + wire data_settled = (s2h_data == s2h_data_q); + wire head_is_cpl = (s2h_data[255:248] == 8'd1); + wire can_req = ~s2h_empty & head_ok & data_settled & ~head_is_cpl & ~req_h_valid; + wire can_cpl = ~s2h_empty & head_ok & data_settled & head_is_cpl & ~cpl_h_valid; + // Takes are inhibited while a pop pulse is in flight (s2h_rd_en + // high): during that cycle the read address already points at the + // next word. + wire take = (can_req | can_cpl) & ~s2h_rd_en; + + always_ff @(posedge dut_clk or posedge dut_creset) begin + if (dut_creset) begin + s2h_rd_en <= 1'b0; + head_ok <= 1'b0; + s2h_data_q <= 256'b0; + req_h_valid <= 1'b0; + cpl_h_valid <= 1'b0; + end else begin + s2h_data_q <= s2h_data; + // head_ok for the NEXT cycle: after a pop, the following + // cycle's buf_data_o is the new head; while non-empty and not + // popping, buf_data_o settles to the head by the next cycle; + // while empty, buf_data_o is meaningless and the first + // non-empty cycle must not be trusted. + if (s2h_rd_en) head_ok <= 1'b1; + else if (~s2h_empty) head_ok <= 1'b1; + else head_ok <= 1'b0; + s2h_rd_en <= take; // one-clock pop pulse following a capture + if (take) begin + if (can_req) begin + req_h_msg <= s2h_data; + req_h_valid <= 1'b1; + end else begin + cpl_h_msg <= s2h_data[143:0]; + cpl_h_valid <= 1'b1; + end + end else begin + if (req_valid & req_ready) req_h_valid <= 1'b0; + if (dma_cpl_valid & dma_cpl_ready) cpl_h_valid <= 1'b0; + end + end + end + + // ------------------------------------------------------------------ + // Host request -> DUT (one message = one TLP beat) + // ------------------------------------------------------------------ + assign req_valid = req_h_valid & rst_n; + assign req_kind = tlp_kind_e'(req_h_msg[1:0]); + assign req_tag = req_h_msg[9:2]; + assign req_len_dw = req_h_msg[19:10]; + assign req_addr = req_h_msg[83:20]; + assign req_data = req_h_msg[211:84]; + assign req_last = req_h_msg[212]; + + // ------------------------------------------------------------------ + // Host completion -> DUT DMA engine + // ------------------------------------------------------------------ + assign dma_cpl_valid = cpl_h_valid & rst_n; + assign dma_cpl_data = cpl_h_msg[127:0]; + assign dma_cpl_tag = cpl_h_msg[135:128]; + assign dma_cpl_last = cpl_h_msg[136]; + + // ------------------------------------------------------------------ + // HW -> SW FIFO (UDPI): completions, DMA requests and MSI events are + // arbitrated into the single message channel (cpl > dma_req > irq). + // With DATA_WIDTH=256 / MULTIPLE=1 each pushed word triggers one + // output_read_hw import call in software. + // ------------------------------------------------------------------ + logic [255:0] h2s_data; + logic h2s_valid; + zebu_vs_simple_hwtosw_fifo_udpi #( + .DATA_WIDTH (256), + .DPI_MSG_DATA_MULTIPLE (1) + ) u_h2s_fifo ( + .clk (dut_clk), + .rst (dut_creset), + .data_in (h2s_data), + .data_valid (h2s_valid), + .flush_data (1'b0) + ); + + // MSI event: hold a sticky pending bit until it is sent to SW + logic irq_pending; + logic grant_cpl, grant_dma, grant_irq; + assign grant_cpl = cpl_valid; + assign grant_dma = ~cpl_valid & dma_req_valid; + assign grant_irq = ~cpl_valid & ~dma_req_valid & irq_pending; + + always_ff @(posedge dut_clk or posedge dut_creset) begin + if (dut_creset) begin + irq_pending <= 1'b0; + end else begin + if (irq_msi) begin + irq_pending <= 1'b1; + end else if (grant_irq) begin + irq_pending <= 1'b0; + end + end + end + + assign h2s_valid = grant_cpl | grant_dma | grant_irq; + assign cpl_ready = grant_cpl; + assign dma_req_ready = grant_dma; + + always_comb begin + if (grant_cpl) begin + // mtype 0 at [255:248], legacy completion layout in [139:0] + h2s_data = {8'd0, 108'h0, cpl_status, cpl_last, cpl_tag, cpl_data}; + end else if (grant_dma) begin + // mtype 1 at [255:248], legacy request layout in [212:0] + h2s_data = {8'd1, 35'h0, dma_req_last, dma_req_data, dma_req_addr, + dma_req_len_dw, dma_req_tag, 2'(dma_req_kind)}; + end else begin + // mtype 2: MSI event + h2s_data = {8'd2, 248'h0}; + end + end + +endmodule : pcie_vhost_xtor