07_PCI驱动程序框架

This commit is contained in:
weidongshan
2021-12-29 17:02:59 +08:00
parent eb9c2f49a4
commit b003d6c448
24 changed files with 518 additions and 76 deletions

View File

@@ -11,9 +11,15 @@
* [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. 总线设备驱动模型
### 1. PCI驱动框架
![image-20211227180031140](pic/10_PCI_PCIe/51_pci_driver_block.png)
@@ -53,76 +59,3 @@
};
```
#### 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,134 @@
## 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);
```

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 MiB

View File

@@ -419,12 +419,19 @@ git clone https://e.coding.net/weidongshan/linux/doc_and_source_for_drivers.git
```shell
05_PCIe设备的配置过程
```
```
* 2021.11.30 发布"PCI和PCIe子系统"
```shell
06_PCIe路由方式
```
```
* 2021.12.29 发布"PCI和PCIe子系统"
```shell
07_PCI驱动程序框架
```
## 6. 联系方式
* 官网http://www.100ask.net
@@ -437,6 +444,10 @@ git clone https://e.coding.net/weidongshan/linux/doc_and_source_for_drivers.git
![](wechat.jpg)
```
```
```
```

View File

@@ -0,0 +1,169 @@
## 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》
### 1. 三种路由方式
数据传输时,最先要确定的是:怎么找到对方?
所谓"路由"就是怎么找到对方PCIe协议中有三种路由方式
* 基于ID的路由
* 基于地址的路由
* 隐式路由
TLP中怎么表示自己使用哪种路由TLP头部就表明了
![image-20211130150826548](pic/10_PCI_PCIe/47_tlp_head_detail.png)
访问PCIe设备时要先配置才能读写数据
* 配置读、配置写:使用**基于ID的路由**,就是使用<Bus, Device, Function>来寻找对方。配置成功后每个PCIe设备都有自己的PCIe地址空间了。
* 内存读、内存写或者IO读、IO写
* 发出报文给对方:使用**基于地址的路由**
* 对方返回数据或者返回状态时:使用**基于ID的路由**
* 各类消息,比如中断、广播等:使用**隐式路由**
### 2. 基于ID的路由
下图来自《PCI Express_ Base Specification Revision 4.0 Version 0.3 ( PDFDrive ).pdf》
![image-20211130093259137](pic/10_PCI_PCIe/40_id_routing_header.png)
TLP中含有<Bus number, Device number, Function number>这就是ID。
对PCIe设备进行配置读、配置写的时候使用的就是**基于ID的路由**,这在上节视频已经介绍了。
配置成功后每个PCIe设备包括虚拟的PCIe桥都分配到了地址空间
* PCIe设备(Endpoint):地址空间是自己的,别的设备访问这个地址范围时,就是想访问它
* PCIe桥地址空间是下面的PCIe设备的别的设备访问这个地址范围时就是想访问它下面的设备
#### 2.1 PCIe设备(Endpoint)的配置空间
Endpoint的配置空间里有"Base Address Regiseters"
* 一开始这些寄存器用来声明需要多大的地址空间、是内存空间还是IO空间
* 被配置后:系统软件为它分配出地址空间,把首地址写入这些寄存器
下图来自《PCI Express_ Base Specification Revision 4.0 Version 0.3 ( PDFDrive ).pdf》
![image-20211130101850489](pic/10_PCI_PCIe/41_type0_conf_reg.png)
#### 2.2 PCIe桥的配置空间
PCIe桥的配置空间里有"Primary Bus Number、Secondary Bus Number、Subordinate Bus Number"
* 配置PCIe桥的时候系统软件会填充这些信息
* 对于ID路由将根据这些信息转发报文
PCIe桥的配置空间里有"Memory Base、Prefetchable Memory Base、I/O Base"
* 表示该桥下游所有设备使用的三组空间范围
* 存储器空间
* 可预取的存储器空间
* I/O空间
* 对于地址路由,将根据这些信息转发报文
下图来自《PCI Express_ Base Specification Revision 4.0 Version 0.3 ( PDFDrive ).pdf》
![image-20211130104824521](pic/10_PCI_PCIe/42_type1_conf_reg.png)
#### 2.3 示例
看视频。
配置时使用ID路由返回报文时也使用ID路由。
### 3. 基于地址的路由
PCIe设备(EndPoint)被配置后,它记录有分配给它的基地址。
PCIe桥也记录有下游PCI子树的基地址。
* PCIe桥监测总线上的信号根据TLP里的地址决定是否转发到自己下面的总线上
* PCIe设备监测总线上的信号根据TLP里的地址决定是否访问自己
* PCIe设备发出回应的报文时使用**基于ID的路由**
#### 3.1 内存读写/IO读写
下图来自《PCI Express_ Base Specification Revision 4.0 Version 0.3 ( PDFDrive ).pdf》
![image-20211130140541294](pic/10_PCI_PCIe/43_address_routing_header.png)
#### 3.2 完成报文
上图里面的Requester ID、TAG被称为"Transaction ID"。
下图来自《PCI EXPRESS体系结构导读 (王齐)》:
![image-20211130141649133](pic/10_PCI_PCIe/44_transaction_id.png)
主设备要给EndPoint的内存写数据它发出"内存写报文",不需要对方回应。
主设备要读EndPoint的内存数据它发出"内存读报文",需要对方回应。
主设备要给EndPoint的IO写数据它发出"IO写报文",需要对方回应。
主设备要读EndPoint的IO数据它发出"IO读报文",需要对方回应。
* PCIe设备要回应时回应谁给"Requester ID",使用**基于ID的路由**
* 发起PCIe传输的设备(主设备)对每次传输都分配一个独一的Tag并且在硬件内部保存当前TLP
* 接收到回应报文后才会根据Tag清除掉内存中保存数据
* 如果没接收到回应或者失败了会把硬件中保存的TLP重新发送出去
回应的完成报文,可以含有数据,也可以不含数据,格式如下:
![image-20211130145638718](pic/10_PCI_PCIe/45_complete.png)
#### 3.3 示例
看视频讲解下图来自《PCI EXPRESS体系结构导读 (王齐)》:
![image-20211130145747638](pic/10_PCI_PCIe/46_address_routing_example.png)
### 4. 隐式路由
消息报文的头部格式如下:
![image-20211130151847726](pic/10_PCI_PCIe/49_msg_header.png)
消息报文中头部的Type字段里低3位表示隐式路由方式
* 000路由到RC
* 001使用地址路由(使用地址路由的消息不常见)
* 010使用ID路由
* 011来自RC的广播报文底下的PCIe桥会向下游转发此消息
* 100本地消息消息到达目的设备后结束不会再次转发
* 101路由到RC仅用于电源管理
![image-20211130150943095](pic/10_PCI_PCIe/48_message_routing.png)

Binary file not shown.

View File

@@ -0,0 +1,61 @@
## 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>;
...... /* 省略 */
};
```

View File

@@ -0,0 +1,134 @@
## 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);
```

Binary file not shown.

After

Width:  |  Height:  |  Size: 139 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 75 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 189 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 167 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 88 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 356 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 74 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 250 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 MiB