小型 PCIE TLP 写入的原子性

Atomicity of small PCIE TLP writes

对于如何从软件进程的角度实现从面向常规内存的 PCIe 设备的卡到主机写入是否有任何保证,其中单个 TLP 写入完全包含在单个 CPU 缓存行中?

我想知道我的设备可能会写入一些数据字后跟一个字节以指示该结构现在有效(例如事件完成)的情况,例如:

struct PCIE_COMPLETION_T {
    uint64_t  data_a;
    uint64_t  data_b;
    uint64_t  data_c;
    uint64_t  data_d;
    uint8_t   valid;
} alignas(SYSTEM_CACHE_LINE_SIZE);

我能否使用单个 TLP 来编写此结构,以便当软件看到有效成员更改为 1(之前已被软件清零)时,其他数据成员是否也将反映值我写了而不是以前的值?

目前我正在执行 2 次写入,首先写入数据,然后将其标记为有效,这没有任何明显的竞争条件,但当然会增加不必要的开销。

我在该站点上看到的最相关的问题似乎是 ,尽管这似乎与 TLP 的相对顺序有关。

仔细阅读 PCIe 3.0 规范,我没有发现任何似乎明确涵盖我的担忧的内容,我不认为我特别需要 AtomicOps。鉴于我只关心与 x86-64 系统的交互,我也仔细阅读了 Intel 体系结构指南,但也没有更清楚。

直觉上,这样的写入似乎应该可以被原子地感知——尤其是当它被称为事务时——但同样我找不到太多明确确认的文档方式查看(我也不确定我需要看什么,可能是 CPU 供应商?)。我还想知道这样的方案是否可以扩展到多个高速缓存行——即如果有效位于从同一 TLP 事务写入的第二个高速缓存行上,我可以确保第一个不迟于第二个被感知吗?

写入可能会被分解成更小的单元,小到双字,但如果是,则必须按照递增的地址顺序观察它们。

PCIe 修订版 4,第 2.4.3 节:

If a single write transaction containing multiple DWs and the Relaxed Ordering bit Clear is accepted by a Completer, the observed ordering of the updates to locations within the Completer's data buffer must be in increasing address order. This semantic is required in case a PCI or PCI-X Bridge along the path combines multiple write transactions into the single one. However, the observed granularity of the updates to the Completer's data buffer is outside the scope of this specification.

While not required by this specification, it is strongly recommended that host platforms guarantee that when a PCI Express write updates host memory, the update granularity observed by a host CPU will not be smaller than a DW.

As an example of update ordering and granularity, if a Requester writes a QW to host memory, in some cases a host CPU reading that QW from host memory could observe the first DW updated and the second DW containing the old value.

我没有修订版 3 的副本,但我怀疑该语言也在该修订版中。为了帮助您找到它,第 2.4 节是“事务排序”,第 2.4.3 节是“写入事务提供的更新排序和粒度”。