From 0eb79b43c888481d65379a87da487ee0aef5b2fe Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 31 Oct 2026 15:16:37 +0800 Subject: [PATCH] 20261031 --- zebu_pcie5_vhost_demo/README.md | 117 +++++ zebu_pcie5_vhost_demo/cfg/scemi.parameters | 13 + zebu_pcie5_vhost_demo/cfg/zebu_compile.utf | 27 ++ zebu_pcie5_vhost_demo/rtl/filelist.f | 5 + zebu_pcie5_vhost_demo/rtl/pcie5_demo_top.sv | 127 ++++++ zebu_pcie5_vhost_demo/rtl/pcie5_ep_core.sv | 422 ++++++++++++++++++ zebu_pcie5_vhost_demo/rtl/pcie_pkg.sv | 41 ++ zebu_pcie5_vhost_demo/scripts/clean.csh | 7 + zebu_pcie5_vhost_demo/scripts/compile.csh | 32 ++ zebu_pcie5_vhost_demo/scripts/env.csh | 45 ++ zebu_pcie5_vhost_demo/scripts/run.csh | 23 + zebu_pcie5_vhost_demo/sw/vhost/Makefile | 27 ++ zebu_pcie5_vhost_demo/sw/vhost/main.cpp | 152 +++++++ .../sw/vhost/pcie_vhost_api.h | 41 ++ .../sw/vhost/pcie_vhost_zebu.cpp | 208 +++++++++ zebu_pcie5_vhost_demo/tb/pcie_vhost_xtor.sv | 204 +++++++++ 16 files changed, 1491 insertions(+) create mode 100644 zebu_pcie5_vhost_demo/README.md create mode 100644 zebu_pcie5_vhost_demo/cfg/scemi.parameters create mode 100644 zebu_pcie5_vhost_demo/cfg/zebu_compile.utf create mode 100644 zebu_pcie5_vhost_demo/rtl/filelist.f create mode 100644 zebu_pcie5_vhost_demo/rtl/pcie5_demo_top.sv create mode 100644 zebu_pcie5_vhost_demo/rtl/pcie5_ep_core.sv create mode 100644 zebu_pcie5_vhost_demo/rtl/pcie_pkg.sv create mode 100644 zebu_pcie5_vhost_demo/scripts/clean.csh create mode 100644 zebu_pcie5_vhost_demo/scripts/compile.csh create mode 100644 zebu_pcie5_vhost_demo/scripts/env.csh create mode 100644 zebu_pcie5_vhost_demo/scripts/run.csh create mode 100644 zebu_pcie5_vhost_demo/sw/vhost/Makefile create mode 100644 zebu_pcie5_vhost_demo/sw/vhost/main.cpp create mode 100644 zebu_pcie5_vhost_demo/sw/vhost/pcie_vhost_api.h create mode 100644 zebu_pcie5_vhost_demo/sw/vhost/pcie_vhost_zebu.cpp create mode 100644 zebu_pcie5_vhost_demo/tb/pcie_vhost_xtor.sv diff --git a/zebu_pcie5_vhost_demo/README.md b/zebu_pcie5_vhost_demo/README.md new file mode 100644 index 0000000..d064275 --- /dev/null +++ b/zebu_pcie5_vhost_demo/README.md @@ -0,0 +1,117 @@ +# ZeBu Server 4 PCIe Gen5 Virtual Host 仿真 Demo + +基于 Synopsys ZeBu Server 4 的 PCIe 5.0 仿真示例工程:DUT 是一个 PCIe Gen5 +Endpoint 示例设计(配置空间 + BAR 寄存器 + 片上 RAM + 简单 DMA 引擎), +主机侧不使用真实 Root Complex 硬件,而是通过 **Virtual Host(vhost)** 方式: +运行在工作站上的 C++ 测试程序经 SCE-MI transactor 把 PCIe 事务(Config / +Memory TLP)送入仿真中的 DUT,完成枚举、BAR 访问、DMA 搬运和 MSI 中断验证。 + +链路在 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 方式的 +> 标准做法。 + +## 架构 + +``` + 工作站 (csh) ZeBu Server 4 +┌────────────────────────────┐ SCE-MI ┌──────────────────────────────┐ +│ sw/vhost/main.cpp │ Message │ tb/pcie_vhost_xtor.sv │ +│ - 枚举/配置空间读写 │ In/Out Port │ - 消息 <-> TLP 转换 │ +│ - BAR scratch 测试 │◄────────────►│ - 时钟控制 (ClockControl) │ +│ - DMA H2D / D2H + 数据比对 │ │ │ TLP │ +│ - MSI 中断等待 │ │ ▼ │ +│ pcie_vhost_api (SCE-MI C++)│ │ rtl/pcie5_ep_core.sv (DUT) │ +└────────────────────────────┘ │ - PCIe 配置空间 (Gen5 Cap) │ + │ - BAR0 寄存器 / BAR2 RAM │ + │ - DMA 引擎 / MSI │ + └──────────────────────────────┘ +``` + +## 目录结构 + +``` +zebu_pcie5_vhost_demo/ +├── README.md +├── scripts/ # 全部 csh 脚本 +│ ├── env.csh # 环境变量(ZEBU_ROOT / license / 路径,按站点修改) +│ ├── compile.csh # zCui 编译 DUT + transactor +│ ├── run.csh # 编译并运行 vhost C++ 测试程序 +│ └── clean.csh +├── cfg/ +│ ├── zebu_compile.utf # zCui 编译入口(UTF,与站点 ZeBu IP 示例同构) +│ └── scemi.parameters # SCE-MI 运行时参数 +├── rtl/ +│ ├── filelist.f +│ ├── pcie_pkg.sv # TLP 类型/参数定义 +│ ├── pcie5_ep_core.sv # DUT:PCIe Gen5 Endpoint 示例核 +│ └── pcie5_demo_top.sv # 仿真顶层 +├── tb/ +│ └── pcie_vhost_xtor.sv # SCE-MI transactor(vhost 桥) +└── sw/vhost/ + ├── pcie_vhost_api.h # vhost 高层 C++ API + ├── pcie_vhost_zebu.cpp # SCE-MI 后端实现 + ├── main.cpp # 测试主程序 + └── Makefile +``` + +## 前置条件 + +- ZeBu Server 4 硬件及对应版本的 zCui / 运行时(`ZEBU_ROOT`) +- ZeBu 编译与运行时 license;如使用 Synopsys 官方 PCIe Virtual Device + 许可包,可用其替换本 demo 的通用 transactor(接口对齐方式见 tb/ 注释) +- 工作站装有 GCC(编译 C++ vhost 程序)、csh/tcsh + +## 使用步骤(csh) + +```csh +cd zebu_pcie5_vhost_demo +source scripts/env.csh # 先按本机环境改 ZEBU_ROOT 和 license +./scripts/compile.csh # zCui 编译,产物在 zebu.work/ +./scripts/run.csh # 运行 vhost 测试,输出 PASS/FAIL +./scripts/clean.csh # 清理 +``` + +## 测试内容(sw/vhost/main.cpp) + +1. **链路校验**:读 PCIe Link Status,期望 Speed = 6(Gen5 32 GT/s)、Width = x16 +2. **枚举**:读配置空间 0x00,期望 VID/DID = 0x16C3 / 0x5805;读 Class Code、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`。 + +## DUT 寄存器表(BAR0) + +| 偏移 | 名称 | 说明 | +|------|------|------| +| 0x00 | ID | 只读,固定 0x50354531("PE51") | +| 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 前端报 SceMi* 模块未定义,需要把 $ZEBU_ROOT 下的 SCE-MI + SystemVerilog 库路径补进 zebu_compile.utf 的 vcs_exec_command(.utf 里有注释)。 +- vhost 侧的"主机内存地址"是 vhost 模型维护的虚拟主机地址空间,DMA 测试 + 数据由 C++ 程序在该地址空间内准备和校验,不涉及工作站真实物理内存 DMA。 +- 本 demo 在普通工作站上无法独立运行(需要 ZeBu 硬件与 license),RTL / + transactor / C++ 均按可综合、可编译标准编写,交付后请在 ZeBu 环境联调。 diff --git a/zebu_pcie5_vhost_demo/cfg/scemi.parameters b/zebu_pcie5_vhost_demo/cfg/scemi.parameters new file mode 100644 index 0000000..4a67329 --- /dev/null +++ b/zebu_pcie5_vhost_demo/cfg/scemi.parameters @@ -0,0 +1,13 @@ +# SCE-MI runtime parameters for PCIe Gen5 vhost demo +# ZeBu 运行时通过 SCEMI_PARAMETERS 环境变量或 -parameters 指定本文件。 + +SYSTEM { + # ZeBu board 配置标识,与 zebu.work 中编译产物保持一致(按站点修改) + BoardName zebu_pcie5_demo + RuntimeDirectory ./zebu.work +} + +# vhost transactor 的消息端口(与 tb/pcie_vhost_xtor.sv 中的实例名对应) +XtorList { + pcie_vhost_xtor +} diff --git a/zebu_pcie5_vhost_demo/cfg/zebu_compile.utf b/zebu_pcie5_vhost_demo/cfg/zebu_compile.utf new file mode 100644 index 0000000..478749c --- /dev/null +++ b/zebu_pcie5_vhost_demo/cfg/zebu_compile.utf @@ -0,0 +1,27 @@ +##--------------------------------------------------------------------- +## PCIe Gen5 vhost demo - zCui UTF compile entry +## 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 +##--------------------------------------------------------------------- + +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 \ + $PCIE_DEMO_HOME/rtl/pcie5_demo_top.sv \ + $PCIE_DEMO_HOME/tb/pcie_vhost_xtor.sv \ + } + +## Board architecture file: provided by the site environment (env.csh) +architecture_file -filename "$env(FILE_CONF)" +zemi3 -timestamp true + +## NOTE: SceMi* modules are picked up automatically from +## $VCS_HOME/etc/unizfe/verilog by the UNIZFE flow (verified on site). + +grid_cmd -submit $env(REMOTECMD) -delete {} +synthesis -max_loop_iterations 20000 diff --git a/zebu_pcie5_vhost_demo/rtl/filelist.f b/zebu_pcie5_vhost_demo/rtl/filelist.f new file mode 100644 index 0000000..b1e998f --- /dev/null +++ b/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/rtl/pcie5_demo_top.sv b/zebu_pcie5_vhost_demo/rtl/pcie5_demo_top.sv new file mode 100644 index 0000000..0a4fb84 --- /dev/null +++ b/zebu_pcie5_vhost_demo/rtl/pcie5_demo_top.sv @@ -0,0 +1,127 @@ +// ============================================================================ +// pcie5_demo_top.sv +// Emulation top level for the PCIe Gen5 vhost demo on ZeBu Server 4. +// Instantiates the endpoint core and the vhost transactor; the transactor +// drives the transaction-level TLP ports and the DUT clock. +// ============================================================================ +`timescale 1ns/1ps + +module pcie5_demo_top + import pcie_pkg::*; +( +); + + logic dut_clk; + logic rst_n; + + // 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), + .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/rtl/pcie5_ep_core.sv b/zebu_pcie5_vhost_demo/rtl/pcie5_ep_core.sv new file mode 100644 index 0000000..7f10d1f --- /dev/null +++ b/zebu_pcie5_vhost_demo/rtl/pcie5_ep_core.sv @@ -0,0 +1,422 @@ +// ============================================================================ +// 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. + task automatic mem_write_beat(input logic [63:0] addr, + input logic [127:0] data, + input logic is_ram); + if (is_ram) begin + for (int i = 0; i < 4; i++) + dev_ram[(addr[15:2] + i[13:0])] <= data[i*32 +: 32]; + end else begin + for (int i = 0; i < 4; i++) 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 + 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 + mem_write_beat(req_addr, req_data, addr_is_ram_now); + 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); + 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/rtl/pcie_pkg.sv b/zebu_pcie5_vhost_demo/rtl/pcie_pkg.sv new file mode 100644 index 0000000..2bbc92c --- /dev/null +++ b/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/scripts/clean.csh b/zebu_pcie5_vhost_demo/scripts/clean.csh new file mode 100644 index 0000000..8620581 --- /dev/null +++ b/zebu_pcie5_vhost_demo/scripts/clean.csh @@ -0,0 +1,7 @@ +#!/bin/csh -f +# 清理编译产物 +cd ${PCIE_DEMO_HOME} +rm -rf zebu.work +cd sw/vhost && make clean +cd ${PCIE_DEMO_HOME} +echo "clean done" diff --git a/zebu_pcie5_vhost_demo/scripts/compile.csh b/zebu_pcie5_vhost_demo/scripts/compile.csh new file mode 100644 index 0000000..ff05a2d --- /dev/null +++ b/zebu_pcie5_vhost_demo/scripts/compile.csh @@ -0,0 +1,32 @@ +#!/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` ===" +echo "产物目录: ${ZEBU_WORK}" diff --git a/zebu_pcie5_vhost_demo/scripts/env.csh b/zebu_pcie5_vhost_demo/scripts/env.csh new file mode 100644 index 0000000..9a5ee83 --- /dev/null +++ b/zebu_pcie5_vhost_demo/scripts/env.csh @@ -0,0 +1,45 @@ +#!/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(示例:沿用站点已有设置时可注释掉)---- +# setenv SNPSLMD_LICENSE_FILE 27000@license-server +# 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 "" + +# ---- 本工程变量 ---- +setenv PCIE_DEMO_HOME `pwd` +setenv ZEBU_WORK ${PCIE_DEMO_HOME}/zebu.work +setenv SCEMI_PARAMETERS ${PCIE_DEMO_HOME}/cfg/scemi.parameters + +echo "ZEBU_ROOT = ${ZEBU_ROOT}" +echo "PCIE_DEMO_HOME = ${PCIE_DEMO_HOME}" +echo "ZEBU_WORK = ${ZEBU_WORK}" +which zCui diff --git a/zebu_pcie5_vhost_demo/scripts/run.csh b/zebu_pcie5_vhost_demo/scripts/run.csh new file mode 100644 index 0000000..94a7465 --- /dev/null +++ b/zebu_pcie5_vhost_demo/scripts/run.csh @@ -0,0 +1,23 @@ +#!/bin/csh -f +# 编译 vhost C++ 测试程序并连接 ZeBu 运行测试 +# 前置:已 source scripts/env.csh 且 compile.csh 执行成功 + +if (! $?ZEBU_ROOT) then + echo "ERROR: ZEBU_ROOT 未设置,请先 source scripts/env.csh" + exit 1 +endif + +cd ${PCIE_DEMO_HOME}/sw/vhost +make clean +make +if ($status != 0) then + echo "ERROR: vhost 程序编译失败" + exit 1 +endif + +cd ${PCIE_DEMO_HOME} +echo "=== vhost test start: `date` ===" +./sw/vhost/pcie_vhost_test +set rc = $status +echo "=== vhost test end: `date` (rc=${rc}) ===" +exit ${rc} diff --git a/zebu_pcie5_vhost_demo/sw/vhost/Makefile b/zebu_pcie5_vhost_demo/sw/vhost/Makefile new file mode 100644 index 0000000..70165d3 --- /dev/null +++ b/zebu_pcie5_vhost_demo/sw/vhost/Makefile @@ -0,0 +1,27 @@ +# vhost C++ test program build +# Requires ZEBU_ROOT (source scripts/env.csh first). +# SCE-MI include/lib paths follow a standard ZeBu installation layout; +# adjust SCEMI_INC / SCEMI_LIB if the site layout differs. + +CXX ?= g++ +CXXFLAGS ?= -O2 -g -std=c++17 -Wall + +SCEMI_INC := $(ZEBU_ROOT)/share/scemi/include +SCEMI_LIB := $(ZEBU_ROOT)/lib + +TARGET := pcie_vhost_test +SRCS := main.cpp pcie_vhost_zebu.cpp +OBJS := $(SRCS:.cpp=.o) + +all: $(TARGET) + +$(TARGET): $(OBJS) + $(CXX) $(CXXFLAGS) -o $@ $(OBJS) -L$(SCEMI_LIB) -lSceMi -lpthread + +%.o: %.cpp pcie_vhost_api.h + $(CXX) $(CXXFLAGS) -I$(SCEMI_INC) -c $< -o $@ + +clean: + rm -f $(OBJS) $(TARGET) + +.PHONY: all clean diff --git a/zebu_pcie5_vhost_demo/sw/vhost/main.cpp b/zebu_pcie5_vhost_demo/sw/vhost/main.cpp new file mode 100644 index 0000000..08a77b0 --- /dev/null +++ b/zebu_pcie5_vhost_demo/sw/vhost/main.cpp @@ -0,0 +1,152 @@ +// ============================================================================ +// 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 + +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 + +int main() { + const char* params = std::getenv("SCEMI_PARAMETERS"); + if (!params) params = "cfg/scemi.parameters"; + + PcieVHost host; + std::printf("== PCIe Gen5 vhost demo (ZeBu Server 4) ==\n"); + if (!host.open(params)) { + 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"); + + // ------------------------------------------------------------------ + host.close(); + std::printf("== %s (errors=%d) ==\n", + g_errors == 0 ? "TEST PASSED" : "TEST FAILED", g_errors); + return g_errors == 0 ? 0 : 1; +} diff --git a/zebu_pcie5_vhost_demo/sw/vhost/pcie_vhost_api.h b/zebu_pcie5_vhost_demo/sw/vhost/pcie_vhost_api.h new file mode 100644 index 0000000..64de1a2 --- /dev/null +++ b/zebu_pcie5_vhost_demo/sw/vhost/pcie_vhost_api.h @@ -0,0 +1,41 @@ +// ============================================================================ +// 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 SCE-MI 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(); + + // Connect to the emulation (SCE-MI parameters file) and disconnect. + bool open(const char* parametersFile); + 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); + + // 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/sw/vhost/pcie_vhost_zebu.cpp b/zebu_pcie5_vhost_demo/sw/vhost/pcie_vhost_zebu.cpp new file mode 100644 index 0000000..de2e3c9 --- /dev/null +++ b/zebu_pcie5_vhost_demo/sw/vhost/pcie_vhost_zebu.cpp @@ -0,0 +1,208 @@ +// ============================================================================ +// pcie_vhost_zebu.cpp +// SCE-MI backend of the PCIe Gen5 Virtual Host. +// +// - Sends host requests (Cfg/Mem Rd/Wr) to the transactor in ZeBu and +// collects completions. +// - Services DMA requests issued by the DUT against a virtual host memory +// image kept in this process (no real physical DMA is involved). +// - Delivers MSI events to waitMsi(). +// +// Message layouts must match tb/pcie_vhost_xtor.sv. +// The SCE-MI calls follow the SCE-MI 2.x C++ binding shipped with ZeBu; +// port instance paths must match the hierarchy in pcie5_demo_top.sv. +// ============================================================================ +#include "pcie_vhost_api.h" + +#include +#include +#include +#include +#include + +// SCE-MI C++ API (from $ZEBU_ROOT) +#include "SceMi.hh" +#include "SceMiMessagePort.hh" + +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 +}; + +// ---- request message packing: 256 bit = 8 x 32 bit words ---- +// w0: [1:0] kind, [9:2] tag, [19:10] len_dw, [31:20] addr[11:0] +// w1: addr[43:12], w2: addr[63:44] in [19:0] +// w3..w6: data[127:0], w7: [0] last +struct ReqMsg { uint32_t w[8]; }; +struct CplMsg { uint32_t w[5]; }; // 144 bit payload in 5 words + +ReqMsg packReq(uint32_t kind, uint8_t tag, uint32_t lenDw, uint64_t addr, + const uint32_t* data4dw, bool last) { + ReqMsg m{}; + m.w[0] = (kind & 3u) | (uint32_t(tag) << 2) | ((lenDw & 0x3FFu) << 10) | + (uint32_t(addr & 0xFFFu) << 20); + m.w[1] = uint32_t((addr >> 12) & 0xFFFFFFFFu); + m.w[2] = uint32_t((addr >> 44) & 0xFFFFFu); + if (data4dw) memcpy(&m.w[3], data4dw, 16); + m.w[7] = last ? 1u : 0u; + return m; +} + +void unpackCpl(const CplMsg& m, uint32_t* data4dw, uint8_t& tag, + bool& last, uint32_t& status) { + if (data4dw) memcpy(data4dw, &m.w[0], 16); + tag = uint8_t((m.w[4] >> 0) & 0xFFu); + last = ((m.w[4] >> 8) & 1u) != 0; + status = (m.w[4] >> 9) & 0x7u; +} + +constexpr uint64_t kHostMemSize = 16ull * 1024 * 1024; // 16 MB vhost memory + +} // namespace + +struct PcieVHost::Impl { + SceMi* scemi = nullptr; + SceMiMessageInPortProxy* reqIn = nullptr; // host -> DUT requests + SceMiMessageOutPortProxy* cplOut = nullptr; // DUT -> host completions + SceMiMessageOutPortProxy* dmaReqOut = nullptr; // DUT DMA requests + SceMiMessageInPortProxy* dmaCplIn = nullptr; // host -> DUT DMA cpls + SceMiMessageOutPortProxy* irqOut = nullptr; // MSI events + std::vector hostMem; // virtual host memory + uint8_t nextTag = 1; + bool msiPending = false; + + // Service one pending DMA request from the DUT, if any. + void pumpDma() { + ReqMsg req; + if (!dmaReqOut->Receive(&req, nullptr)) return; // nothing pending + uint32_t kind = req.w[0] & 3u; + uint32_t lenDw = (req.w[0] >> 10) & 0x3FFu; + uint64_t addr = (uint64_t(req.w[0]) >> 20) | + (uint64_t(req.w[1]) << 12) | + (uint64_t(req.w[2] & 0xFFFFFu) << 44); + if (kind == TLP_MEM_WR) { + // DUT writes host memory: one beat per message in this demo + if (addr + 16 <= kHostMemSize) + memcpy(&hostMem[size_t(addr)], &req.w[3], 16); + } else if (kind == TLP_MEM_RD) { + CplMsg cpl{}; + if (addr + 16 <= kHostMemSize) + memcpy(&cpl.w[0], &hostMem[size_t(addr)], 16); + cpl.w[4] = (uint32_t(0xA5) /*tag*/) | (1u << 8) /*last*/; + dmaCplIn->Send(&cpl); + } + } + + void pumpIrq() { + uint8_t ev = 0; + if (irqOut->Receive(&ev, nullptr) && ev == 0x01) msiPending = true; + } + void pump() { pumpDma(); pumpIrq(); } +}; + +PcieVHost::PcieVHost() : impl_(new Impl()) {} +PcieVHost::~PcieVHost() { close(); delete impl_; } + +bool PcieVHost::open(const char* parametersFile) { + SceMiParameters params(parametersFile); + impl_->scemi = SceMi::Initialize(0, nullptr, ¶ms); + if (!impl_->scemi) { + std::fprintf(stderr, "SceMi::Initialize failed (parameters: %s)\n", + parametersFile); + return false; + } + const char* base = "pcie5_demo_top.u_xtor"; + char path[128]; + std::snprintf(path, sizeof path, "%s.u_req_in", base); + impl_->reqIn = new SceMiMessageInPortProxy(path, ¶ms); + std::snprintf(path, sizeof path, "%s.u_cpl_out", base); + impl_->cplOut = new SceMiMessageOutPortProxy(path, ¶ms); + std::snprintf(path, sizeof path, "%s.u_dma_req_out", base); + impl_->dmaReqOut = new SceMiMessageOutPortProxy(path, ¶ms); + std::snprintf(path, sizeof path, "%s.u_dma_cpl_in", base); + impl_->dmaCplIn = new SceMiMessageInPortProxy(path, ¶ms); + std::snprintf(path, sizeof path, "%s.u_irq_out", base); + impl_->irqOut = new SceMiMessageOutPortProxy(path, ¶ms); + + impl_->reqIn->Bind(); + impl_->cplOut->Bind(); + impl_->dmaReqOut->Bind(); + impl_->dmaCplIn->Bind(); + impl_->irqOut->Bind(); + + impl_->hostMem.assign(size_t(kHostMemSize), 0); + return true; +} + +void PcieVHost::close() { + if (impl_->scemi) { + SceMi::Shutdown(); + impl_->scemi = nullptr; + } +} + +// ---- generic request/completion exchange --------------------------------- +// Sends one request beat; for reads, collects completion beats into outData. +void xfer(PcieVHost::Impl* im, uint32_t kind, uint64_t addr, + const uint32_t* wrData, uint32_t* rdData, uint32_t lenDw) { + uint8_t tag = im->nextTag++; + uint32_t doneDw = 0; + while (doneDw < lenDw) { + uint32_t beatDw = (lenDw - doneDw > 4) ? 4 : (lenDw - doneDw); + bool last = (doneDw + beatDw == lenDw); + ReqMsg req = packReq(kind, tag, lenDw, addr + doneDw * 4, + wrData ? wrData + doneDw : nullptr, last); + im->reqIn->Send(&req); + if (kind == TLP_CFG_RD || kind == TLP_MEM_RD) { + CplMsg cpl; uint8_t ctag; bool clast; uint32_t cst; + // Block until the completion arrives; service DMA meanwhile. + for (;;) { + if (im->cplOut->Receive(&cpl, nullptr)) break; + im->pump(); + std::this_thread::sleep_for(std::chrono::microseconds(100)); + } + unpackCpl(cpl, rdData + doneDw, ctag, clast, cst); + } + doneDw += beatDw; + } +} + +uint32_t PcieVHost::cfgRead(uint32_t offset) { + uint32_t data = 0; + xfer(impl_, TLP_CFG_RD, offset, nullptr, &data, 1); + return data; +} +void PcieVHost::cfgWrite(uint32_t offset, uint32_t data) { + xfer(impl_, TLP_CFG_WR, offset, &data, nullptr, 1); +} +void PcieVHost::memWrite(uint64_t addr, const uint32_t* data, uint32_t lenDw) { + xfer(impl_, TLP_MEM_WR, addr, data, nullptr, lenDw); +} +void PcieVHost::memRead(uint64_t addr, uint32_t* data, uint32_t lenDw) { + xfer(impl_, 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) + 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) + 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_->pump(); + if (impl_->msiPending) { + impl_->msiPending = false; + return true; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + return false; +} diff --git a/zebu_pcie5_vhost_demo/tb/pcie_vhost_xtor.sv b/zebu_pcie5_vhost_demo/tb/pcie_vhost_xtor.sv new file mode 100644 index 0000000..420a142 --- /dev/null +++ b/zebu_pcie5_vhost_demo/tb/pcie_vhost_xtor.sv @@ -0,0 +1,204 @@ +// ============================================================================ +// pcie_vhost_xtor.sv +// SCE-MI transactor bridging the software Virtual Host (C++ on workstation) +// and the PCIe Gen5 endpoint core inside ZeBu Server 4. +// +// Port/parameter usage matches the ZeBu-flavor SCE-MI models shipped in +// $VCS_HOME/etc/unizfe/verilog (Q-2020.03-SP2-1-Z): +// - SceMiClockControl provides Uclock/Ureset and takes ReadyForCclock. +// - SceMiClockPort generates the controlled Cclock/Creset for the DUT. +// - SceMiMessageOutPort: TransmitReady is an INPUT (producer valid), +// ReceiveReady is an OUTPUT (port can accept the message). +// +// Message formats (must match sw/vhost/pcie_vhost_zebu.cpp): +// Request message (host -> DUT), 256 bit: +// [1:0] kind, [9:2] tag, [19:10] len_dw, [83:20] addr (64b), +// [211:84] data (up to 4 DW), [212] last +// Completion message (DUT -> host), 144 bit: +// [127:0] data, [135:128] tag, [136] last, [139:137] status +// DMA requests/completions reuse the same formats on dedicated ports. +// IRQ event port: 8 bit, value 0x01 = MSI fired by the DUT. +// ============================================================================ +`timescale 1ns/1ps + +module pcie_vhost_xtor + import pcie_pkg::*; +( + output logic dut_clk, + 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 +); + + // ------------------------------------------------------------------ + // SCE-MI clock infrastructure (ZeBu flavor) + // ------------------------------------------------------------------ + logic uclock, ureset; + logic dut_creset; + + SceMiClockControl #( + .ClockNum (1) + ) u_clock_ctrl ( + .Uclock (uclock), + .Ureset (ureset), + .ReadyForCclock (1'b1), // simple xtor: always ready + .CclockEnabled (), + .ReadyForCclockNegEdge (1'b0), + .CclockNegEdgeEnabled () + ); + + SceMiClockPort #( + .ClockNum (1), + .RatioNumerator (1), + .RatioDenominator (1), + .ResetCycles (16) + ) u_clock_port ( + .Cclock (dut_clk), + .Creset (dut_creset) + ); + + assign rst_n = ~dut_creset; // Creset is active high + + // ------------------------------------------------------------------ + // 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 + + // ------------------------------------------------------------------ + // SCE-MI message ports + // ------------------------------------------------------------------ + logic req_in_xmit_ready; + logic [255:0] req_in_msg; + SceMiMessageInPort #(.PortWidth(256)) u_req_in ( + .ReceiveReady (req_ready && rst_n), + .Message (req_in_msg), + .TransmitReady (req_in_xmit_ready) + ); + + logic [143:0] cpl_out_msg; + SceMiMessageOutPort #(.PortWidth(144)) u_cpl_out ( + .TransmitReady (cpl_valid), + .ReceiveReady (cpl_ready), + .Message (cpl_out_msg) + ); + + logic [255:0] dma_req_out_msg; + SceMiMessageOutPort #(.PortWidth(256)) u_dma_req_out ( + .TransmitReady (dma_req_valid), + .ReceiveReady (dma_req_ready), + .Message (dma_req_out_msg) + ); + + logic dma_cpl_in_xmit_ready; + logic [143:0] dma_cpl_in_msg; + SceMiMessageInPort #(.PortWidth(144)) u_dma_cpl_in ( + .ReceiveReady (dma_cpl_ready && rst_n), + .Message (dma_cpl_in_msg), + .TransmitReady (dma_cpl_in_xmit_ready) + ); + + // MSI event: hold a sticky pending bit until the port accepts it + logic irq_pending; + logic irq_out_rcv_ready; + SceMiMessageOutPort #(.PortWidth(8)) u_irq_out ( + .TransmitReady (irq_pending), + .ReceiveReady (irq_out_rcv_ready), + .Message (8'h01) + ); + + 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 (irq_pending && irq_out_rcv_ready) begin + irq_pending <= 1'b0; + end + end + end + + // ------------------------------------------------------------------ + // Host request -> DUT (one message = one TLP beat) + // ------------------------------------------------------------------ + assign req_valid = req_in_xmit_ready && rst_n; + assign req_kind = tlp_kind_e'(req_in_msg[1:0]); + assign req_tag = req_in_msg[9:2]; + assign req_len_dw = req_in_msg[19:10]; + assign req_addr = req_in_msg[83:20]; + assign req_data = req_in_msg[211:84]; + assign req_last = req_in_msg[212]; + + // ------------------------------------------------------------------ + // DUT completion -> host + // ------------------------------------------------------------------ + assign cpl_out_msg = {4'h0, cpl_status, cpl_last, cpl_tag, cpl_data}; + + // ------------------------------------------------------------------ + // DUT DMA request -> host memory model + // ------------------------------------------------------------------ + assign dma_req_out_msg = {43'h0, dma_req_last, dma_req_data, dma_req_addr, + dma_req_len_dw, dma_req_tag, 2'(dma_req_kind)}; + + // ------------------------------------------------------------------ + // Host completion -> DUT DMA engine + // ------------------------------------------------------------------ + assign dma_cpl_valid = dma_cpl_in_xmit_ready && rst_n; + assign dma_cpl_data = dma_cpl_in_msg[127:0]; + assign dma_cpl_tag = dma_cpl_in_msg[135:128]; + assign dma_cpl_last = dma_cpl_in_msg[136]; + +endmodule : pcie_vhost_xtor