> ## Documentation Index
> Fetch the complete documentation index at: https://dragonwingdocs-staging.qualcomm.com/llms.txt
> Use this file to discover all available pages before exploring further.

# PCIe

PCIe(Peripheral Component Interconnect Express)使用双向连接同时发送和接收信息。设备之间的路径称为**链路(Link)**,由一个或多个称为\*\*通道(Lane)\*\*的发送/接收对组成。

<img src="https://mintcdn.com/qualcomm-staging/5j1H6H_NhFpsAqlq/Ubuntu/images/peripheral-interfaces/pcie_device_connection_link.png?fit=max&auto=format&n=5j1H6H_NhFpsAqlq&q=85&s=ff9a68a9194233cb99d28feb389e1ae7" width="578" height="121" data-path="Ubuntu/images/peripheral-interfaces/pcie_device_connection_link.png" />

## 硬件规格

### IQ-8275 PCIe 控制器

| 参数         | PCIe RC1                                                                 | PCIe RC0                                                                 |
| ---------- | ------------------------------------------------------------------------ | ------------------------------------------------------------------------ |
| **速率**     | Gen4 2 通道(8 GT/s)                                                        | Gen4 4 通道(8 GT/s)                                                        |
| **配置空间**   | 0x40100000(1 MB)                                                         | 0x60100000(1 MB)                                                         |
| **I/O 空间** | 0x40200000(1 MB)                                                         | 0x60200000(1 MB)                                                         |
| **BAR 空间** | 0x40300000(509 MB)                                                       | 0x60300000(509 MB)                                                       |
| **电源轨**    | vreg\_l5a (VDD\_A\_PCIE\_0\_CORE), vreg\_l1c (VDD\_A\_PCIE\_0\_PLL\_1P2) | vreg\_l5a (VDD\_A\_PCIE\_1\_CORE), vreg\_l1c (VDD\_A\_PCIE\_1\_PLL\_1P2) |
| **中断**     | MSI 和 PCI legacy                                                         | MSI 和 PCI legacy                                                         |
| **电源管理**   | ASPM (L1/L1ss, L0s)                                                      | ASPM (L1/L1ss, L0s)                                                      |

### EVK 硬件配置

* **PCIe0**:Gen4 2 通道 — M.2 E key(Wi-Fi)或扩展(可通过 SW1-1 开关选择)
* **PCIe1**:Gen4 4 通道 — PCIe x4 插槽或扩展(可通过 SW1-2 开关选择)

<img src="https://mintcdn.com/qualcomm-staging/5j1H6H_NhFpsAqlq/Ubuntu/images/peripheral-interfaces/pcie_interface_block_diagram.png?fit=max&auto=format&n=5j1H6H_NhFpsAqlq&q=85&s=104f9fa9b84a16bbdd3f8a4f1af9d599" width="955" height="601" data-path="Ubuntu/images/peripheral-interfaces/pcie_interface_block_diagram.png" />

## 架构

<img src="https://mintcdn.com/qualcomm-staging/5j1H6H_NhFpsAqlq/Ubuntu/images/peripheral-interfaces/pcie_layer_architecture_link.png?fit=max&auto=format&n=5j1H6H_NhFpsAqlq&q=85&s=bcfc0d1bc3a3b219ff252b8124f15690" width="754" height="790" data-path="Ubuntu/images/peripheral-interfaces/pcie_layer_architecture_link.png" />

| 层         | 关键特性                           |
| --------- | ------------------------------ |
| **物理层**   | 链路训练、8b/10b 编码、串行转换            |
| **数据链路层** | DLLP 组装、LCRC 生成、ACK/NAK 重放     |
| **事务层**   | TLP 组装、ECRC、流量控制、QoS(TC→VC 映射) |

### PCIe 连接类型

| 类型                    | 说明                |
| --------------------- | ----------------- |
| **Root Complex (RC)** | 将 CPU 连接到 PCIe 拓扑 |
| **Switch**            | 在超过 2 个端口之间路由数据包  |
| **Bridge**            | 连接不同的总线           |
| **Endpoint (EP)**     | 仅有上行端口的叶子设备       |

## 软件驱动配置

### 内核配置

```
CONFIG_HOTPLUG_PCI=y
CONFIG_PCI_HOST_GENERIC=y
CONFIG_PCIE_DW_PLAT_HOST=y
CONFIG_PCIE_QCOM=y
CONFIG_PCI_ENDPOINT=y
CONFIG_PCIEASPM=y
CONFIG_PCIEASPM_POWER_SUPERSAVE=y
CONFIG_BLK_DEV_NVME=y
CONFIG_NVME_CORE=y
```

### 设备树源文件

| 平台                 | 文件                                      |
| ------------------ | --------------------------------------- |
| Dragonwing IQ-8275 | `arch/arm64/boot/dts/qcom/sa8295p.dtsi` |

### 关键驱动文件

| 组件            | 路径                                                  |
| ------------- | --------------------------------------------------- |
| PCIe 驱动       | `drivers/pci/controller/dwc/pcie-qcom.c`            |
| DesignWare 主机 | `drivers/pci/controller/dwc/pcie-designware-host.c` |
| PHY 驱动        | `drivers/phy/qualcomm/phy-qcom-qmp-pcie.c`          |

## 验证

```bash theme={null}
lspci
lspci -vv | grep -i "lnk"
cat /sys/bus/pci/devices/*/current_link_speed
cat /sys/bus/pci/devices/*/current_link_width
```

### NVMe over PCIe

<img src="https://mintcdn.com/qualcomm-staging/_dNg1YW-w4ZOFPhw/Ubuntu/images/peripheral-interfaces/IQ9075_NVME_over_PCIe.png?fit=max&auto=format&n=_dNg1YW-w4ZOFPhw&q=85&s=1317750bfb9e03be1471c93f7e4c6b3a" width="1684" height="939" data-path="Ubuntu/images/peripheral-interfaces/IQ9075_NVME_over_PCIe.png" />

```bash theme={null}
lspci | grep -i nvme
ls /dev/nvme*
nvme list
```

## 调试

```bash theme={null}
sudo su
echo 8 > /proc/sys/kernel/printk
echo "file pcie-qcom.c +p" > /sys/kernel/debug/dynamic_debug/control
dmesg | grep -i pcie
```

## 故障排除

<AccordionGroup>
  <Accordion title="链路训练失败">
    ```bash theme={null}
    dmesg | grep -i phy | grep -i pcie
    cat /sys/kernel/debug/regulator/regulator_summary | grep -i pcie
    ```

    * 检查 PHY 电源轨和时钟配置
    * 验证设备树设置
  </Accordion>

  <Accordion title="设备未被枚举">
    ```bash theme={null}
    lspci -tv
    dmesg | grep -i pcie
    ```

    * 验证 DIP 开关位置(SW1-1、SW1-2)
    * 检查端点设备的电源供应
  </Accordion>

  <Accordion title="性能问题">
    ```bash theme={null}
    lspci -vv | grep -i "lnk"
    ```

    * 验证链路是否运行在预期的 Gen4 速率
    * 如果 ASPM 设置过于激进,请进行调整
  </Accordion>
</AccordionGroup>

## 快速参考

```bash theme={null}
lspci                                              # List all PCIe devices
lspci -vv                                          # Detailed device info
lspci -tv                                          # Tree view
dmesg | grep -i pcie                               # Kernel messages
cat /sys/bus/pci/devices/*/current_link_speed      # Link speeds
```

## 资源

* [内核 PCIe 文档](https://www.kernel.org/doc/html/latest/PCI/index.html)
* [PCIe 规范](https://pcisig.com/specifications/pciexpress/technical_library/pciexpress_whitepaper.pdf)
* [Qualcomm PCIe 驱动](https://github.com/torvalds/linux/blob/master/drivers/pci/controller/dwc/pcie-qcom.c)
