添加: 08_RK3399_PCIe芯片手册解读

This commit is contained in:
weidongshan
2021-12-31 18:24:06 +08:00
parent 457fa86d8f
commit 0f8a141d85
42 changed files with 170438 additions and 155 deletions

View File

@@ -13,7 +13,7 @@
开发板资料:
* 芯片手册Rockchip RK3399TRM V1.3 Part2.pdf
* 芯片手册Rockchip RK3399TRM V1.3 Part2.pdf 《Chapter 17 PCIe Controller》
```shell
doc_and_source_for_drivers\IMX6ULL\doc_pic\
@@ -48,13 +48,7 @@ AXI相关
实际上我们可以更深入一点下图是STM32MP157的总线结构图(其他芯片没有画出总线结构图)
![image-20211230133623252](pic/10_PCI_PCIe/57_stm32mp157_bus.png)
本节视频我们只关心AXI总线高速设备之间通过AXI总线连接。Master和Slave是多对多的关系它们之间读、写可以同时进行的内部结构图如下
实际芯片中CPU与外设之间的连接更加复杂高速设备之间通过AXI总线连接。AXI总线总传输数据的双方分为Master和SlaveMaster发起传输Slave回应传输。Master和Slave是多对多的关系它们之间读、写可以同时进行的内部结构图如下
![image-20211230133754834](pic/10_PCI_PCIe/52_axi.png)
@@ -110,26 +104,211 @@ RK3399的PCIe控制器就是挂在AXI总线上在芯片手册中可以看到
### 2. 地址空间
### 2. 地址空间和寄存器介绍
#### 2.1 想达到的目的
使用PCIe时我们编程时想达到这个目的
* CPU读写某个地址就可以读写某个PCIe设备的配置空间
![image-20211231103527138](pic/10_PCI_PCIe/56_addr_space_for_config_rw.png)
* CPU读写某个地址就可以读写某个PCIe设备的内存、寄存器
![image-20211231103805041](pic/10_PCI_PCIe/58_addr_space_for_mem_rw.png)
简单地说就是把CPU发出的addr转换为右边的TLP头部PCI地址、头部的其他信息。
这涉及两部分:
* 怎么把CPU地址转换为PCI地址
* 怎么提供TLP头部信息中的其他部分
#### 2.2 地址空间
RK3399访问PCIe控制器时CPU地址空间可以分为
* Client Register Set地址范围 0xFD000000~0xFD7FFFFF
* Core Register Set :地址范围 0xFD800000~0xFDFFFFFF
* Region 00xF8000000~0xF9FFFFFF , 32MB
* Region 10xFA000000~0xFA0FFFFF1MB
* Region 20xFA100000~0xFA1FFFFF1MB
* Client Register Set地址范围 0xFD000000~0xFD7FFFFF比如选择PCIe协议的版本(Gen1/Gen2)、电源控制等
* Core Register Set :地址范围 0xFD800000~0xFDFFFFFF,所谓核心寄存器就是用来进行设置地址映射的寄存器等
* Region 00xF8000000~0xF9FFFFFF , 32MB用于访问外接的PCIe设备的配置空间
* Region 10xFA000000~0xFA0FFFFF1MB,用于地址转换
* Region 20xFA100000~0xFA1FFFFF1MB,用于地址转换
* ……
* Region 320xFBF00000~0xFBFFFFFF1MB
* Region 320xFBF00000~0xFBFFFFFF1MB,用于地址转换
其中Region 0大小为32MBRegion1~31大小分别为1MB。
### 3. 访问流程
CPU访问Region 0的地址时将会导致PCIe控制器发出读写配置空间的TLP。
CPU访问Region 1~32的地址时将会导致PCIe控制器发出读写内存、IO空间的TLP。
#### 2.3 寄存器介绍
CPU访问一个地址导致PCIe控制器发出TLP。TLP里含有PCIe地址、其他信息。
这些寄存器必定涉及这2部分
* 地址转换把CPU地址转换为PCIe地址
* 提供TLP的其他信息
Region0、Region1~32每个Region都有类似的寄存器。
一个Region可以用于读写配置空间可以用于读写内存空间、可以用于读写IO空间还可以用于读写消息。
这由Region对应的寄存器决定。
每个Region都有一样寄存器以Region 0为例有6个寄存器
![image-20211231114439910](pic/10_PCI_PCIe/61_region_regs.png)
CPU访问某个Region时它是想干嘛
* 读写配置空间、发出对应TLP
* 读写内存空间、发出对应TLP
* 读写IO空间、发出对应TLP
* 读写消息、发出对应TLP
到底是发出哪种TLP由Region对应的ob_desc0寄存器决定
| ob_desc0[3:0] | 作用 |
| ------------- | ----------------------------------- |
| 1010 | 发出的TLP用于访问Type 0的配置空间 |
| 1011 | 发出的TLP用于访问Type 1的配置空间 |
| 0010 | 发出的TLP用于读写内存空间 |
| 0110 | 发出的TLP用于读写IO空间 |
| 1100 | 发出的TLP是"Normal Message" |
| 1101 | 发出的TLP是"Vendor-Defined Message" |
CPU访问某个Region时最终都是要发出TLPTLP的内容怎么确定
* 地址信息ob_addr0/1把CPU地址转换为PCIe地址提供TLP里面的地址信息
* 其他信息ob_desc0/1/2/3提供TLP的其他信息
##### 2.3.1 用于配置空间
Region0一般用于读写配置空间它对应的寄存器如下
![image-20211231110755624](pic/10_PCI_PCIe/59_outband_tlp_reg_for_config.png)
##### 2.3.2 用于内存和IO
![image-20211231110828136](pic/10_PCI_PCIe/60_outband_tlp_reg_for_mem_io.png)
### 3. 访问示例
#### 3.1 配置空间读写示例
要读写设备的配置空间首先要定位Bus/Dev/Function/Reg
![image-20211231160616105](pic/10_PCI_PCIe/64_config_devs.png)
怎么发出这些"Bus/Dev/Function/Register"信息?如下图所示:
![image-20211231115744245](pic/10_PCI_PCIe/62_config_rw_example.png)
当Region 0的寄存器ob_desc0[3:0]被配置为读写配置空间时, CPU发出Region 0的地址地址里面隐含有这些信息
* Buscpu_addr[27:20]
* Devcpu_addr[19:15]
* Funcpu_addr[14:12]
* Regcpu_addr[11:0]
使用过程步骤如下。
##### 3.1.1 配置Region 0用于读写配置空间
![image-20211231165052119](pic/10_PCI_PCIe/65_ob_desc0_for_config.png)
##### 3.1.2 配置Region 0地址转换
比如我们可以设置bit[5:0]为27意味着cpu_addr[27:0]这28条地址线都会传入TLP。
![image-20211231170423183](pic/10_PCI_PCIe/66_ob_addr0_for_config.png)
##### 3.1.3 CPU读写Region 0的地址
Region 0的地址范围是0xF8000000~0xF9FFFFFF。
CPU想访问这个设备Bus=bus,Dev=dev,Fun=fun,Reg=reg那么CPU读写这个地址即可
```shell
0xF8000000 + (bus<<20) | (dev<<15) | (fun<<12) | (reg)
```
#### 3.2 MEM/IO读写示例
![image-20211231115915400](pic/10_PCI_PCIe/63_me_io_rw_example.png)
##### 3.2.1 配置Region 1用于内存读写
![image-20211231171244046](pic/10_PCI_PCIe/67_ob_desc0_for_mem_rw.png)
##### 3.2.2 配置Region 1地址转换
![image-20211231171609085](pic/10_PCI_PCIe/68_ob_addr0_for_mem_rw.png)
addr0、addr1寄存器里保存的是PCIe地址也就是CPU发出这个Region的CPU地址后将会转换为某个PCI地址。
怎么转换由addr0、addr1决定。
Region 1的CPU地址范围是0xFA000000~0xFA0FFFFF是1M空间。
我们一般会让PCI地址等于CPU地址所以这样设置
* addr0
* [5:0]等于19表示CPU_ADDR[19:0]共20位地址传入TLP
* [31:8]等于0xFA0000
* addr1设置为0
如上设置后CPU读写地址时0xFA0?????就会转换为PCI地址0xFA0?????,转换过程如下:
```shell
pci_addr = cpu_addr[19:0] | (addr0[31:20] << 20) | (addr1<<32)
= 0x????? + (0xFA0 << 20) | (0 << 32)
= 0xFA0?????
```
#### 3.1 配置空间读写流程
#### 3.2 MEM/IO读写流程

View File

@@ -47,20 +47,77 @@
compatible = "rockchip,rk3399-pcie";
#address-cells = <3>;
#size-cells = <2>;
aspm-no-l0s;
clocks = <&cru ACLK_PCIE>, <&cru ACLK_PERF_PCIE>,
<&cru PCLK_PCIE>, <&cru SCLK_PCIE_PM>;
clock-names = "aclk", "aclk-perf",
"hclk", "pm";
bus-range = <0x0 0x1f>;
...... /* 省略 */
max-link-speed = <1>;
linux,pci-domain = <0>;
msi-map = <0x0 &its 0x0 0x1000>;
interrupts = <GIC_SPI 49 IRQ_TYPE_LEVEL_HIGH 0>,
<GIC_SPI 50 IRQ_TYPE_LEVEL_HIGH 0>,
<GIC_SPI 51 IRQ_TYPE_LEVEL_HIGH 0>;
interrupt-names = "sys", "legacy", "client";
#interrupt-cells = <1>;
interrupt-map-mask = <0 0 0 7>;
interrupt-map = <0 0 0 1 &pcie0_intc 0>,
<0 0 0 2 &pcie0_intc 1>,
<0 0 0 3 &pcie0_intc 2>,
<0 0 0 4 &pcie0_intc 3>;
phys = <&pcie_phy>;
phy-names = "pcie-phy";
ranges = <0x83000000 0x0 0xfa000000 0x0 0xfa000000 0x0 0x1e00000
0x81000000 0x0 0xfbe00000 0x0 0xfbe00000 0x0 0x100000>;
reg = <0x0 0xf8000000 0x0 0x2000000>,
<0x0 0xfd000000 0x0 0x1000000>;
...... /* 省略 */
};
reg-names = "axi-base", "apb-base";
resets = <&cru SRST_PCIE_CORE>, <&cru SRST_PCIE_MGMT>,
<&cru SRST_PCIE_MGMT_STICKY>, <&cru SRST_PCIE_PIPE>,
<&cru SRST_PCIE_PM>, <&cru SRST_P_PCIE>,
<&cru SRST_A_PCIE>;
reset-names = "core", "mgmt", "mgmt-sticky", "pipe",
"pm", "pclk", "aclk";
status = "disabled";
pcie0_intc: interrupt-controller {
interrupt-controller;
#address-cells = <0>;
#interrupt-cells = <1>;
};
};
```
#### 2.1 设备树解析过程
##### 2.1.1 地址解析
寄存器地址:
```c
regs = platform_get_resource_byname(pdev,
IORESOURCE_MEM,
"apb-base"); // 0xfd000000
rockchip->apb_base = devm_ioremap_resource(dev, regs);
```
ECAM地址[PCIe ECAM介绍](https://zhuanlan.zhihu.com/p/176988002)
```c
regs = platform_get_resource_byname(pdev,
IORESOURCE_MEM,
"axi-base"); // 0xf8000000
rockchip->reg_base = devm_ioremap_resource(dev, regs);
```
```c
rockchip_pcie_probe
resource_size_t io_base;
@@ -109,6 +166,139 @@ rockchip_pcie_probe
##### 2.1.2 地址映射
MEM空间映射
```c
// rockchip->mem_bus_addr = 0xfa000000
// rockchip->mem_size = 0x1e00000
// 设置Region1、2、……的映射关系
for (reg_no = 0; reg_no < (rockchip->mem_size >> 20); reg_no++) {
err = rockchip_pcie_prog_ob_atu(rockchip, reg_no + 1,
AXI_WRAPPER_MEM_WRITE,
20 - 1,
rockchip->mem_bus_addr +
(reg_no << 20),
0);
```
IO空间映射
```c
offset = rockchip->mem_size >> 20;
for (reg_no = 0; reg_no < (rockchip->io_size >> 20); reg_no++) {
err = rockchip_pcie_prog_ob_atu(rockchip,
reg_no + 1 + offset,
AXI_WRAPPER_IO_WRITE,
20 - 1,
rockchip->io_bus_addr +
(reg_no << 20),
0);
if (err) {
dev_err(dev, "program RC io outbound ATU failed\n");
return err;
}
}
```
Message空间映射
```c
/* assign message regions */
rockchip_pcie_prog_ob_atu(rockchip, reg_no + 1 + offset,
AXI_WRAPPER_NOR_MSG,
20 - 1, 0, 0);
rockchip->msg_bus_addr = rockchip->mem_bus_addr +
((reg_no + offset) << 20);
```
```c
static int rockchip_pcie_prog_ob_atu(struct rockchip_pcie *rockchip,
int region_no, int type, u8 num_pass_bits,
u32 lower_addr, u32 upper_addr)
{
u32 ob_addr_0;
u32 ob_addr_1;
u32 ob_desc_0;
u32 aw_offset;
if (region_no >= MAX_AXI_WRAPPER_REGION_NUM)
return -EINVAL;
if (num_pass_bits + 1 < 8)
return -EINVAL;
if (num_pass_bits > 63)
return -EINVAL;
if (region_no == 0) {
if (AXI_REGION_0_SIZE < (2ULL << num_pass_bits))
return -EINVAL;
}
if (region_no != 0) {
if (AXI_REGION_SIZE < (2ULL << num_pass_bits))
return -EINVAL;
}
aw_offset = (region_no << OB_REG_SIZE_SHIFT);
ob_addr_0 = num_pass_bits & PCIE_CORE_OB_REGION_ADDR0_NUM_BITS;
ob_addr_0 |= lower_addr & PCIE_CORE_OB_REGION_ADDR0_LO_ADDR;
ob_addr_1 = upper_addr;
ob_desc_0 = (1 << 23 | type);
rockchip_pcie_write(rockchip, ob_addr_0,
PCIE_CORE_OB_REGION_ADDR0 + aw_offset);
rockchip_pcie_write(rockchip, ob_addr_1,
PCIE_CORE_OB_REGION_ADDR1 + aw_offset);
rockchip_pcie_write(rockchip, ob_desc_0,
PCIE_CORE_OB_REGION_DESC0 + aw_offset);
rockchip_pcie_write(rockchip, 0,
PCIE_CORE_OB_REGION_DESC1 + aw_offset);
return 0;
}
```
##### 2.1.3 配置
Region 0
```c
rockchip_pcie_write(rockchip,
(RC_REGION_0_ADDR_TRANS_L + RC_REGION_0_PASS_BITS),
PCIE_CORE_OB_REGION_ADDR0);
rockchip_pcie_write(rockchip, RC_REGION_0_ADDR_TRANS_H,
PCIE_CORE_OB_REGION_ADDR1);
rockchip_pcie_write(rockchip, 0x0080000a, PCIE_CORE_OB_REGION_DESC0);
rockchip_pcie_write(rockchip, 0x0, PCIE_CORE_OB_REGION_DESC1);
```
读写配置空间:
```c
busdev = PCIE_ECAM_ADDR(bus->number, PCI_SLOT(devfn),
PCI_FUNC(devfn), where);
```
#### 2.2 扫描总线过程
```c

Binary file not shown.

After

Width:  |  Height:  |  Size: 122 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 268 KiB

After

Width:  |  Height:  |  Size: 268 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 112 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 194 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 206 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 74 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 227 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 213 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 263 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 144 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 198 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 153 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 157 KiB

View File

@@ -431,6 +431,12 @@ git clone https://e.coding.net/weidongshan/linux/doc_and_source_for_drivers.git
```shell
07_PCI驱动程序框架
```
* 2021.12.31 发布"PCI和PCIe子系统"
```shell
08_RK3399_PCIe芯片手册解读
```
## 6. 联系方式

View File

@@ -1,134 +0,0 @@
## PCI驱动程序框架
参考资料:
* 《PCI Express Technology 3.0》Mike Jackson, Ravi Budruk; MindShare, Inc.
* [《PCIe扫盲系列博文》](http://blog.chinaaet.com/justlxy/p/5100053251)作者Felix这是对《PCI Express Technology》的理解与翻译
* 《PCI EXPRESS体系结构导读 (王齐)》
* 《PCI Express_ Base Specification Revision 4.0 Version 0.3 ( PDFDrive )》
* 《NCB-PCI_Express_Base_5.0r1.0-2019-05-22》
* [SOC中AXI总线是如何连接的](https://zhuanlan.zhihu.com/p/157137488)
* [AXI总线整理总结](https://blog.csdn.net/tristan_tian/article/details/89393045)
* [PCIe中MSI和MSI-X中断机制](https://blog.csdn.net/pieces_thinking/article/details/119431791)
开发板资料:
* https://wiki.t-firefly.com/zh_CN/ROC-RK3399-PC-PLUS/
### 1. PCI驱动框架
![image-20211227180031140](pic/10_PCI_PCIe/51_pci_driver_block.png)
### 2. RK3399驱动
怎么找到驱动?
* 在内核目录下根据芯片名字找到文件:`drivers\pci\host\pcie-rockchip.c`
* 看到如下代码:
```c
static const struct of_device_id rockchip_pcie_of_match[] = {
{ .compatible = "rockchip,rk3399-pcie", },
{}
};
```
* 在内核`arch/arm64/boot/dts`下搜:`grep "rockchip,rk3399-pcie" * -nr`
* 找到设备树文件:`arch/arm64/boot/dts/rk3399.dtsi`,代码如下:
```shell
pcie0: pcie@f8000000 {
compatible = "rockchip,rk3399-pcie";
#address-cells = <3>;
#size-cells = <2>;
bus-range = <0x0 0x1f>;
...... /* 省略 */
phys = <&pcie_phy>;
phy-names = "pcie-phy";
ranges = <0x83000000 0x0 0xfa000000 0x0 0xfa000000 0x0 0x1e00000
0x81000000 0x0 0xfbe00000 0x0 0xfbe00000 0x0 0x100000>;
reg = <0x0 0xf8000000 0x0 0x2000000>,
<0x0 0xfd000000 0x0 0x1000000>;
...... /* 省略 */
};
```
#### 2.1 设备树解析过程
```c
rockchip_pcie_probe
resource_size_t io_base;
LIST_HEAD(res); // 资源链表
// 解析设备树获得PCI host bridge的资源(CPU地址空间、PCI地址空间、大小)
err = of_pci_get_host_bridge_resources(dev->of_node, 0, 0xff, &res, &io_base);
// 解析 bus-range
err = of_pci_parse_bus_range(dev, bus_range);
pci_add_resource(resources, bus_range);
// 解析 ranges
of_pci_range_parser_init
parser->range = of_get_property(node, "ranges", &rlen);
for_each_of_pci_range(&parser, &range) {// 解析range
// 把range转换为resource
// res->flags = range->flags;
// res->start = range->cpu_addr;
// res->end = res->start + range->size - 1;
err = of_pci_range_to_resource(&range, dev, res);
// 在链表中增加一项
// 注意第3个参数: offset = cpu_addr - pci_addr
pci_add_resource_offset(resources, res, res->start - range.pci_addr);
}
/* Get the I/O and memory ranges from DT */
resource_list_for_each_entry(win, &res) {
rockchip->io_bus_addr = io->start - win->offset; // 0xfbe00000,pci addr
rockchip->mem_bus_addr = mem->start - win->offset; // 0xfba00000, pci addr
rockchip->root_bus_nr = win->res->start; // 0
}
bus = pci_scan_root_bus(&pdev->dev, 0, &rockchip_pcie_ops, rockchip, &res);
pci_bus_add_devices(bus);
```
`for_each_of_pci_range`解析设备树过程:
![image-20211224164144628](pic/10_PCI_PCIe/50_parse_range.png)
#### 2.2 扫描总线过程
```c
rockchip_pcie_probe
bus = pci_scan_root_bus(&pdev->dev, 0, &rockchip_pcie_ops, rockchip, &res);
pci_scan_root_bus_msi
pci_scan_child_bus
pci_scan_slot
dev = pci_scan_single_device(bus, devfn);
dev = pci_scan_device(bus, devfn);
pci_setup_device
pci_read_bases(dev, 6, PCI_ROM_ADDRESS);
pci_device_add(dev, bus);
pci_bus_size_bridges(bus);
pci_bus_assign_resources(bus);
list_for_each_entry(child, &bus->children, node)
pcie_bus_configure_settings(child);
pci_bus_add_devices(bus);
```

View File

@@ -0,0 +1,314 @@
## RK3399_PCIe芯片手册解读
参考资料:
* 《PCI Express Technology 3.0》Mike Jackson, Ravi Budruk; MindShare, Inc.
* [《PCIe扫盲系列博文》](http://blog.chinaaet.com/justlxy/p/5100053251)作者Felix这是对《PCI Express Technology》的理解与翻译
* 《PCI EXPRESS体系结构导读 (王齐)》
* 《PCI Express_ Base Specification Revision 4.0 Version 0.3 ( PDFDrive )》
* 《NCB-PCI_Express_Base_5.0r1.0-2019-05-22》
* [SOC中AXI总线是如何连接的](https://zhuanlan.zhihu.com/p/157137488)
* [AXI总线整理总结](https://blog.csdn.net/tristan_tian/article/details/89393045)
* [PCIe中MSI和MSI-X中断机制](https://blog.csdn.net/pieces_thinking/article/details/119431791)
开发板资料:
* 芯片手册Rockchip RK3399TRM V1.3 Part2.pdf 《Chapter 17 PCIe Controller》
```shell
doc_and_source_for_drivers\IMX6ULL\doc_pic\
10_PCI_PCIe\芯片手册\RK3399
Rockchip RK3399TRM V1.3 Part2.pdf
```
* https://wiki.t-firefly.com/zh_CN/ROC-RK3399-PC-PLUS/
AXI相关
* ug1037-vivado-axi-reference-guide.pdf
```shell
doc_and_source_for_drivers\IMX6ULL\doc_pic\10_PCI_PCIe\协议\AXI
ug1037-vivado-axi-reference-guide.pdf
```
* [AXI总线你需要知道的事儿](https://zhuanlan.zhihu.com/p/96804919)
* [AXI总线整理总结](https://blog.csdn.net/tristan_tian/article/details/89393045)
* [AMBA、AHB、APB、AXI总线介绍及对比](https://zhuanlan.zhihu.com/p/161077476)
### 1. AXI总线
#### 1.1 连接方式
我们一直使用这个图来简化CPU与外设之间的连接
![](pic/10_PCI_PCIe/06_address_space.png)
实际芯片中CPU与外设之间的连接更加复杂高速设备之间通过AXI总线连接。AXI总线总传输数据的双方分为Master和SlaveMaster发起传输Slave回应传输。Master和Slave是多对多的关系它们之间读、写可以同时进行的内部结构图如下
![image-20211230133754834](pic/10_PCI_PCIe/52_axi.png)
#### 1.2 五个通道
在AXI总线中读写可以同时进行有5个通道
* 读:
* 读地址通道:传输读操作的地址
* 读数据通道:传输读到的数据
* 写:
* 写地址通道:传输写操作的地址
* 写数据通道:传输要写的数据
* 写响应通道:传输写操作的结果
![image-20211230141124832](pic/10_PCI_PCIe/53_axi_read.png)
![image-20211230141144288](pic/10_PCI_PCIe/54_axi_write.png)
| 通道名称 | 通道功能 | 数据流向 |
| -------------- | -------------------------------------- | ---------- |
| read address | 读地址通道 | 主机->从机 |
| read data | 读数据通道(包括数据通道和读响应通道) | 从机->主机 |
| write address | 写地址通道 | 主机->从机 |
| write data | 写数据通道 | 主机->从机 |
| write response | 写响应通道 | 从机->主机 |
#### 1.3 信号线
我们只列出本节视频关心的信号线:
| 信号 | AXI4 | AXI4-Lite |
| ------ | -------------------------------- | -------------------------------- |
| AWADDR | 写地址通道地址线最多可达64位 | 写地址通道地址线最多可达64位 |
| WDATA | 写数据通道数据线32~1024位 | 写数据通道数据线32位 |
| ARADDR | 读地址通道地址线最多可达64位 | 读地址通道地址线最多可达64位 |
| RDATA | 读数据通道数据线32~1024位 | 写数据通道数据线32位 |
#### 1.4 PCIe控制器
RK3399的PCIe控制器就是挂在AXI总线上在芯片手册中可以看到
* AWADDRAXI总线的写地址通道地址线简单理解就是CPU要写PCIe控制器时发出的地址线
* ARADDR AXI总线的读地址通道地址线简单理解就是CPU要读PCIe控制器时发出的地址线
![image-20211230144101706](pic/10_PCI_PCIe/55_rk3399_pcie.png)
### 2. 地址空间和寄存器介绍
#### 2.1 想达到的目的
使用PCIe时我们编程时想达到这个目的
* CPU读写某个地址就可以读写某个PCIe设备的配置空间
![image-20211231103527138](pic/10_PCI_PCIe/56_addr_space_for_config_rw.png)
* CPU读写某个地址就可以读写某个PCIe设备的内存、寄存器
![image-20211231103805041](pic/10_PCI_PCIe/58_addr_space_for_mem_rw.png)
简单地说就是把CPU发出的addr转换为右边的TLP头部PCI地址、头部的其他信息。
这涉及两部分:
* 怎么把CPU地址转换为PCI地址
* 怎么提供TLP头部信息中的其他部分
#### 2.2 地址空间
RK3399访问PCIe控制器时CPU地址空间可以分为
* Client Register Set地址范围 0xFD000000~0xFD7FFFFF比如选择PCIe协议的版本(Gen1/Gen2)、电源控制等
* Core Register Set :地址范围 0xFD800000~0xFDFFFFFF所谓核心寄存器就是用来进行设置地址映射的寄存器等
* Region 00xF8000000~0xF9FFFFFF , 32MB用于访问外接的PCIe设备的配置空间
* Region 10xFA000000~0xFA0FFFFF1MB用于地址转换
* Region 20xFA100000~0xFA1FFFFF1MB用于地址转换
* ……
* Region 320xFBF00000~0xFBFFFFFF1MB用于地址转换
其中Region 0大小为32MBRegion1~31大小分别为1MB。
CPU访问Region 0的地址时将会导致PCIe控制器发出读写配置空间的TLP。
CPU访问Region 1~32的地址时将会导致PCIe控制器发出读写内存、IO空间的TLP。
#### 2.3 寄存器介绍
CPU访问一个地址导致PCIe控制器发出TLP。TLP里含有PCIe地址、其他信息。
这些寄存器必定涉及这2部分
* 地址转换把CPU地址转换为PCIe地址
* 提供TLP的其他信息
Region0、Region1~32每个Region都有类似的寄存器。
一个Region可以用于读写配置空间可以用于读写内存空间、可以用于读写IO空间还可以用于读写消息。
这由Region对应的寄存器决定。
每个Region都有一样寄存器以Region 0为例有6个寄存器
![image-20211231114439910](pic/10_PCI_PCIe/61_region_regs.png)
CPU访问某个Region时它是想干嘛
* 读写配置空间、发出对应TLP
* 读写内存空间、发出对应TLP
* 读写IO空间、发出对应TLP
* 读写消息、发出对应TLP
到底是发出哪种TLP由Region对应的ob_desc0寄存器决定
| ob_desc0[3:0] | 作用 |
| ------------- | ----------------------------------- |
| 1010 | 发出的TLP用于访问Type 0的配置空间 |
| 1011 | 发出的TLP用于访问Type 1的配置空间 |
| 0010 | 发出的TLP用于读写内存空间 |
| 0110 | 发出的TLP用于读写IO空间 |
| 1100 | 发出的TLP是"Normal Message" |
| 1101 | 发出的TLP是"Vendor-Defined Message" |
CPU访问某个Region时最终都是要发出TLPTLP的内容怎么确定
* 地址信息ob_addr0/1把CPU地址转换为PCIe地址提供TLP里面的地址信息
* 其他信息ob_desc0/1/2/3提供TLP的其他信息
##### 2.3.1 用于配置空间
Region0一般用于读写配置空间它对应的寄存器如下
![image-20211231110755624](pic/10_PCI_PCIe/59_outband_tlp_reg_for_config.png)
##### 2.3.2 用于内存和IO
![image-20211231110828136](pic/10_PCI_PCIe/60_outband_tlp_reg_for_mem_io.png)
### 3. 访问示例
#### 3.1 配置空间读写示例
要读写设备的配置空间首先要定位Bus/Dev/Function/Reg
![image-20211231160616105](pic/10_PCI_PCIe/64_config_devs.png)
怎么发出这些"Bus/Dev/Function/Register"信息?如下图所示:
![image-20211231115744245](pic/10_PCI_PCIe/62_config_rw_example.png)
当Region 0的寄存器ob_desc0[3:0]被配置为读写配置空间时, CPU发出Region 0的地址地址里面隐含有这些信息
* Buscpu_addr[27:20]
* Devcpu_addr[19:15]
* Funcpu_addr[14:12]
* Regcpu_addr[11:0]
使用过程步骤如下。
##### 3.1.1 配置Region 0用于读写配置空间
![image-20211231165052119](pic/10_PCI_PCIe/65_ob_desc0_for_config.png)
##### 3.1.2 配置Region 0地址转换
比如我们可以设置bit[5:0]为27意味着cpu_addr[27:0]这28条地址线都会传入TLP。
![image-20211231170423183](pic/10_PCI_PCIe/66_ob_addr0_for_config.png)
##### 3.1.3 CPU读写Region 0的地址
Region 0的地址范围是0xF8000000~0xF9FFFFFF。
CPU想访问这个设备Bus=bus,Dev=dev,Fun=fun,Reg=reg那么CPU读写这个地址即可
```shell
0xF8000000 + (bus<<20) | (dev<<15) | (fun<<12) | (reg)
```
#### 3.2 MEM/IO读写示例
![image-20211231115915400](pic/10_PCI_PCIe/63_me_io_rw_example.png)
##### 3.2.1 配置Region 1用于内存读写
![image-20211231171244046](pic/10_PCI_PCIe/67_ob_desc0_for_mem_rw.png)
##### 3.2.2 配置Region 1地址转换
![image-20211231171609085](pic/10_PCI_PCIe/68_ob_addr0_for_mem_rw.png)
addr0、addr1寄存器里保存的是PCIe地址也就是CPU发出这个Region的CPU地址后将会转换为某个PCI地址。
怎么转换由addr0、addr1决定。
Region 1的CPU地址范围是0xFA000000~0xFA0FFFFF是1M空间。
我们一般会让PCI地址等于CPU地址所以这样设置
* addr0
* [5:0]等于19表示CPU_ADDR[19:0]共20位地址传入TLP
* [31:8]等于0xFA0000
* addr1设置为0
如上设置后CPU读写地址时0xFA0?????就会转换为PCI地址0xFA0?????,转换过程如下:
```shell
pci_addr = cpu_addr[19:0] | (addr0[31:20] << 20) | (addr1<<32)
= 0x????? + (0xFA0 << 20) | (0 << 32)
= 0xFA0?????
```

View File

@@ -0,0 +1,324 @@
## PCI驱动程序框架
参考资料:
* 《PCI Express Technology 3.0》Mike Jackson, Ravi Budruk; MindShare, Inc.
* [《PCIe扫盲系列博文》](http://blog.chinaaet.com/justlxy/p/5100053251)作者Felix这是对《PCI Express Technology》的理解与翻译
* 《PCI EXPRESS体系结构导读 (王齐)》
* 《PCI Express_ Base Specification Revision 4.0 Version 0.3 ( PDFDrive )》
* 《NCB-PCI_Express_Base_5.0r1.0-2019-05-22》
* [SOC中AXI总线是如何连接的](https://zhuanlan.zhihu.com/p/157137488)
* [AXI总线整理总结](https://blog.csdn.net/tristan_tian/article/details/89393045)
* [PCIe中MSI和MSI-X中断机制](https://blog.csdn.net/pieces_thinking/article/details/119431791)
开发板资料:
* https://wiki.t-firefly.com/zh_CN/ROC-RK3399-PC-PLUS/
### 1. PCI驱动框架
![image-20211227180031140](pic/10_PCI_PCIe/51_pci_driver_block.png)
### 2. RK3399驱动
怎么找到驱动?
* 在内核目录下根据芯片名字找到文件:`drivers\pci\host\pcie-rockchip.c`
* 看到如下代码:
```c
static const struct of_device_id rockchip_pcie_of_match[] = {
{ .compatible = "rockchip,rk3399-pcie", },
{}
};
```
* 在内核`arch/arm64/boot/dts`下搜:`grep "rockchip,rk3399-pcie" * -nr`
* 找到设备树文件:`arch/arm64/boot/dts/rk3399.dtsi`,代码如下:
```shell
pcie0: pcie@f8000000 {
compatible = "rockchip,rk3399-pcie";
#address-cells = <3>;
#size-cells = <2>;
aspm-no-l0s;
clocks = <&cru ACLK_PCIE>, <&cru ACLK_PERF_PCIE>,
<&cru PCLK_PCIE>, <&cru SCLK_PCIE_PM>;
clock-names = "aclk", "aclk-perf",
"hclk", "pm";
bus-range = <0x0 0x1f>;
max-link-speed = <1>;
linux,pci-domain = <0>;
msi-map = <0x0 &its 0x0 0x1000>;
interrupts = <GIC_SPI 49 IRQ_TYPE_LEVEL_HIGH 0>,
<GIC_SPI 50 IRQ_TYPE_LEVEL_HIGH 0>,
<GIC_SPI 51 IRQ_TYPE_LEVEL_HIGH 0>;
interrupt-names = "sys", "legacy", "client";
#interrupt-cells = <1>;
interrupt-map-mask = <0 0 0 7>;
interrupt-map = <0 0 0 1 &pcie0_intc 0>,
<0 0 0 2 &pcie0_intc 1>,
<0 0 0 3 &pcie0_intc 2>,
<0 0 0 4 &pcie0_intc 3>;
phys = <&pcie_phy>;
phy-names = "pcie-phy";
ranges = <0x83000000 0x0 0xfa000000 0x0 0xfa000000 0x0 0x1e00000
0x81000000 0x0 0xfbe00000 0x0 0xfbe00000 0x0 0x100000>;
reg = <0x0 0xf8000000 0x0 0x2000000>,
<0x0 0xfd000000 0x0 0x1000000>;
reg-names = "axi-base", "apb-base";
resets = <&cru SRST_PCIE_CORE>, <&cru SRST_PCIE_MGMT>,
<&cru SRST_PCIE_MGMT_STICKY>, <&cru SRST_PCIE_PIPE>,
<&cru SRST_PCIE_PM>, <&cru SRST_P_PCIE>,
<&cru SRST_A_PCIE>;
reset-names = "core", "mgmt", "mgmt-sticky", "pipe",
"pm", "pclk", "aclk";
status = "disabled";
pcie0_intc: interrupt-controller {
interrupt-controller;
#address-cells = <0>;
#interrupt-cells = <1>;
};
};
```
#### 2.1 设备树解析过程
##### 2.1.1 地址解析
寄存器地址:
```c
regs = platform_get_resource_byname(pdev,
IORESOURCE_MEM,
"apb-base"); // 0xfd000000
rockchip->apb_base = devm_ioremap_resource(dev, regs);
```
ECAM地址[PCIe ECAM介绍](https://zhuanlan.zhihu.com/p/176988002)
```c
regs = platform_get_resource_byname(pdev,
IORESOURCE_MEM,
"axi-base"); // 0xf8000000
rockchip->reg_base = devm_ioremap_resource(dev, regs);
```
```c
rockchip_pcie_probe
resource_size_t io_base;
LIST_HEAD(res); // 资源链表
// 解析设备树获得PCI host bridge的资源(CPU地址空间、PCI地址空间、大小)
err = of_pci_get_host_bridge_resources(dev->of_node, 0, 0xff, &res, &io_base);
// 解析 bus-range
err = of_pci_parse_bus_range(dev, bus_range);
pci_add_resource(resources, bus_range);
// 解析 ranges
of_pci_range_parser_init
parser->range = of_get_property(node, "ranges", &rlen);
for_each_of_pci_range(&parser, &range) {// 解析range
// 把range转换为resource
// res->flags = range->flags;
// res->start = range->cpu_addr;
// res->end = res->start + range->size - 1;
err = of_pci_range_to_resource(&range, dev, res);
// 在链表中增加一项
// 注意第3个参数: offset = cpu_addr - pci_addr
pci_add_resource_offset(resources, res, res->start - range.pci_addr);
}
/* Get the I/O and memory ranges from DT */
resource_list_for_each_entry(win, &res) {
rockchip->io_bus_addr = io->start - win->offset; // 0xfbe00000,pci addr
rockchip->mem_bus_addr = mem->start - win->offset; // 0xfba00000, pci addr
rockchip->root_bus_nr = win->res->start; // 0
}
bus = pci_scan_root_bus(&pdev->dev, 0, &rockchip_pcie_ops, rockchip, &res);
pci_bus_add_devices(bus);
```
`for_each_of_pci_range`解析设备树过程:
![image-20211224164144628](pic/10_PCI_PCIe/50_parse_range.png)
##### 2.1.2 地址映射
MEM空间映射
```c
// rockchip->mem_bus_addr = 0xfa000000
// rockchip->mem_size = 0x1e00000
// 设置Region1、2、……的映射关系
for (reg_no = 0; reg_no < (rockchip->mem_size >> 20); reg_no++) {
err = rockchip_pcie_prog_ob_atu(rockchip, reg_no + 1,
AXI_WRAPPER_MEM_WRITE,
20 - 1,
rockchip->mem_bus_addr +
(reg_no << 20),
0);
```
IO空间映射
```c
offset = rockchip->mem_size >> 20;
for (reg_no = 0; reg_no < (rockchip->io_size >> 20); reg_no++) {
err = rockchip_pcie_prog_ob_atu(rockchip,
reg_no + 1 + offset,
AXI_WRAPPER_IO_WRITE,
20 - 1,
rockchip->io_bus_addr +
(reg_no << 20),
0);
if (err) {
dev_err(dev, "program RC io outbound ATU failed\n");
return err;
}
}
```
Message空间映射
```c
/* assign message regions */
rockchip_pcie_prog_ob_atu(rockchip, reg_no + 1 + offset,
AXI_WRAPPER_NOR_MSG,
20 - 1, 0, 0);
rockchip->msg_bus_addr = rockchip->mem_bus_addr +
((reg_no + offset) << 20);
```
```c
static int rockchip_pcie_prog_ob_atu(struct rockchip_pcie *rockchip,
int region_no, int type, u8 num_pass_bits,
u32 lower_addr, u32 upper_addr)
{
u32 ob_addr_0;
u32 ob_addr_1;
u32 ob_desc_0;
u32 aw_offset;
if (region_no >= MAX_AXI_WRAPPER_REGION_NUM)
return -EINVAL;
if (num_pass_bits + 1 < 8)
return -EINVAL;
if (num_pass_bits > 63)
return -EINVAL;
if (region_no == 0) {
if (AXI_REGION_0_SIZE < (2ULL << num_pass_bits))
return -EINVAL;
}
if (region_no != 0) {
if (AXI_REGION_SIZE < (2ULL << num_pass_bits))
return -EINVAL;
}
aw_offset = (region_no << OB_REG_SIZE_SHIFT);
ob_addr_0 = num_pass_bits & PCIE_CORE_OB_REGION_ADDR0_NUM_BITS;
ob_addr_0 |= lower_addr & PCIE_CORE_OB_REGION_ADDR0_LO_ADDR;
ob_addr_1 = upper_addr;
ob_desc_0 = (1 << 23 | type);
rockchip_pcie_write(rockchip, ob_addr_0,
PCIE_CORE_OB_REGION_ADDR0 + aw_offset);
rockchip_pcie_write(rockchip, ob_addr_1,
PCIE_CORE_OB_REGION_ADDR1 + aw_offset);
rockchip_pcie_write(rockchip, ob_desc_0,
PCIE_CORE_OB_REGION_DESC0 + aw_offset);
rockchip_pcie_write(rockchip, 0,
PCIE_CORE_OB_REGION_DESC1 + aw_offset);
return 0;
}
```
##### 2.1.3 配置
Region 0
```c
rockchip_pcie_write(rockchip,
(RC_REGION_0_ADDR_TRANS_L + RC_REGION_0_PASS_BITS),
PCIE_CORE_OB_REGION_ADDR0);
rockchip_pcie_write(rockchip, RC_REGION_0_ADDR_TRANS_H,
PCIE_CORE_OB_REGION_ADDR1);
rockchip_pcie_write(rockchip, 0x0080000a, PCIE_CORE_OB_REGION_DESC0);
rockchip_pcie_write(rockchip, 0x0, PCIE_CORE_OB_REGION_DESC1);
```
读写配置空间:
```c
busdev = PCIE_ECAM_ADDR(bus->number, PCI_SLOT(devfn),
PCI_FUNC(devfn), where);
```
#### 2.2 扫描总线过程
```c
rockchip_pcie_probe
bus = pci_scan_root_bus(&pdev->dev, 0, &rockchip_pcie_ops, rockchip, &res);
pci_scan_root_bus_msi
pci_scan_child_bus
pci_scan_slot
dev = pci_scan_single_device(bus, devfn);
dev = pci_scan_device(bus, devfn);
pci_setup_device
pci_read_bases(dev, 6, PCI_ROM_ADDRESS);
pci_device_add(dev, bus);
pci_bus_size_bridges(bus);
pci_bus_assign_resources(bus);
list_for_each_entry(child, &bus->children, node)
pcie_bus_configure_settings(child);
pci_bus_add_devices(bus);
```

Binary file not shown.

After

Width:  |  Height:  |  Size: 121 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 68 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 122 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 268 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 112 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 194 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 206 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 74 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 227 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 213 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 263 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 144 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 198 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 153 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 157 KiB

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long