7.PCIE配置空间读写软件

3 篇文章 3 订阅

软件-7.PCIE配置空间读写

软件-7.PCIE配置空间读写

  • 软件-7.PCIE配置空间读写
    • 软件读写配置空间
      • 驱动层接口
      • 原理分析
        • 驱动层代码接口
        • 驱动层接口与原理
          • 相关参考
          • 基础知识
          • raw_pci_ops 得初始化
          • raw_pci_ext_ops 得原理-ECAM
          • 如何获取ECAM得基地址?

软件读写配置空间

驱动层接口

inline int pci_read_config_byte(struct pci_dev *dev, int where, u8 *val); 
inline int pci_read_config_word(struct pci_dev *dev, int where, u16 *val); 
inline int pci_read_config_dword(struct pci_dev *dev, int where, u32 *val); 
inline int pci_write_config_byte(struct pci_dev *dev, int where, u8 val); 
inline int pci_write_config_word(struct pci_dev *dev, int where, u16 val); 
inline int pci_write_config_dword(struct pci_dev *dev, int where, u32 val);

根本就是调用 pci总线接口

int pci_bus_read_config_byte (struct pci_bus *bus, unsigned int devfn, int where, u8 *val);  //读字节
int pci_bus_read_config_word (struct pci_bus *bus, unsigned int devfn, int where, u16 *val);  //读字
int pci_bus_read_config_dword (struct pci_bus *bus, unsigned int devfn, int where, u32 *val); //读双字
int pci_bus_write_config_byte (struct pci_bus *bus, unsigned int devfn, int where, u8 val);  //写字节
int pci_bus_write_config_word (struct pci_bus *bus, unsigned int devfn, int where, u16  val); //写字
int pci_bus_write_config_dword (struct pci_bus *bus, unsigned int devfn, int where, u32  val); //写双字

在drivers\pci\access.c 中

int pci_bus_write_config_##size \
	(struct pci_bus *bus, unsigned int devfn, int pos, type value)	\
{									\
	int res;							\
	unsigned long flags;						\
	if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER;	\
	pci_lock_config(flags);						\
	res = bus->ops->write(bus, devfn, pos, len, value);		\                       // 重点
	pci_unlock_config(flags);					\
	return res;							\
}

所以主要代码是 : bus->ops

原理分析

驱动层代码接口

根总线读写操作接口初始化

pci_acpi_scan_root
    ->acpi_pci_root_create  // 重要参数: acpi_pci_root_ops
        ->pci_create_root_bus // bridge->ops=acpi_pci_root_ops.pci_ops, 也就是 pci_root_ops
        ->pci_register_host_bridge
            ->bus->ops = bridge->ops // acpi_pci_root_ops.ops, 也就是 pci_root_ops

所以bus得操作接口为 acpi_pci_root_ops

struct pci_ops pci_root_ops = {
	.read = pci_read,
	.write = pci_write,
};
static struct acpi_pci_root_ops acpi_pci_root_ops = {
	.pci_ops = &pci_root_ops,
};

所以只需要分析 pci_root_ops即可

int raw_pci_write(unsigned int domain, unsigned int bus, unsigned int devfn,
						int reg, int len, u32 val)
{
	if (domain == 0 && reg < 256 && raw_pci_ops)
		return raw_pci_ops->write(domain, bus, devfn, reg, len, val);       // 基础配置空间
	if (raw_pci_ext_ops)
		return raw_pci_ext_ops->write(domain, bus, devfn, reg, len, val);   // 扩展配置空间
	return -EINVAL;
}

所以可以得到结论:
pci读写 0x00-0xFF 使用raw_pci_ops
pci读写 0x100-0xFFF 使用 raw_pci_ext_ops

驱动层接口与原理

相关参考

基础知识

在X86架构上有关于这部分的描述:

10th Generation Intel® Core™ Processors, Datasheet Volume 1 of 2 中 P29页描述:

 

PCI Express 将配置空间扩展到每个设备/功能4K字节
PCI Express 配置空间分为 一个PCI兼容区域(就是前256个字节组成)和 一个扩展的PCIExpress 区域(就是 0x100-0xFFF)。

PCI前256字节配置空间:可以通过 PCI规范中定义的机制(就是 通过 0cf8-0cff : PCI conf1 两个ioport通过BDF来寻址访问 ) 或 使用PCI Express 增强配置机制(ECAM- PCI Express Enhanced Configuration Access Mechanism)访问机制来访问PCI兼容区域

PCI的0x100-0xFFF的ECAM访问,使用ioremap去访问PCI Express区域,这个属于硬件支持,基地址从ACPI来获取到
PCI Express 主机桥,将内存映射的PCI Express 配置空间访问从主机处理器转换为PCI Express 配置周期。为了保持与PCI配置寻址机制的兼容性,建议系统软件仅使用32位操作(32位对齐)访问增强的配置空间。有关PCI兼容和PCI Express 增强配置机制和事务规则的详细信息,请参阅《 PCI Express基本规范》。

raw_pci_ops 得初始化

初始化入口

// init.c:45:arch_initcall(pci_arch_init);
pci_arch_init
    // 《Linux那些事之PCI》P5中描述了三种PCI access mode,  
    //  内核中CONFIG_PCI_DIRECT这个宏有配直接Direct去访问
    pci_direct_probe();   // 初始化0xCF8和0xCFC,并初始化

    if (x86_init.pci.arch_init && !x86_init.pci.arch_init())  // 函数没实现,哈哈
        return 0;

    // pci_pcbios_init();  // CONFIG_PCI_BIOS--不配置,不用看
    pci_direct_init(type); // raw_pci_ops = raw_pci_ext_ops 预留读写pci配置空间的接口
pci_probe & PCI_PROBE_CONF1 # 判断,什么是CONF1和CONF2
request_region(0xCF8, 8, "PCI conf1") # 为什么使用0xCF8
pci_check_type1()	# 检测type1
raw_pci_ops = &pci_direct_conf1;  # !!! 初始化pci配置空间操作接口

结论(重点):
所以raw_pci_ops = &pci_direct_conf1; 可以看到 使用0xCF8和0xCFC访问PCI基础配置空间

inno@DEV-005:~$ sudo cat /proc/ioports  | grep "PCI conf"   # 申请到得IO接口
0cf8-0cff : PCI conf1

// arch/x86/direct.c中                                                     // 访问格式
#define PCI_CONF1_ADDRESS(bus, devfn, reg) \
	(0x80000000 | ((reg & 0xF00) << 16) | (bus << 16) \
	| (devfn << 8) | (reg & 0xFC))
outl(PCI_CONF1_ADDRESS(bus, devfn, reg), 0xCF8);  // 配置地址
u32 value = inl(0xCFC); // 读取配置

深入PCI与PCIe之二:软件篇 中描述了CONFIG_ADD_RESS得结构:

CONFIG_ADDRESS寄存器格式:
31 位:Enabled位。
23:16 位:总线编号。 // bus
15:11 位:设备编号。 // devfn[7:3]
10: 8 位:功能编号。 // devfn[2:0]
7: 2 位:配置空间寄存器编号。 // 配置空间偏移地址, 注:因为是32位端口,所以4字节访问。
1: 0 位:恒为“00”。这是因为CF8h、CFCh端口是32位端口。

raw_pci_ext_ops 得原理-ECAM

参考:

ECAM是访问PCIe配置空间一种机制,PCIe配置空间大小是4k
4kbyte寄存器地址空间,需要12bit bit 0~bit11
Function Number bit 12~bit 14
Device Number bit 15~bit 19
Bus Number bit 20~bit 27

如何访问一个PCIe设备的配置空间呢?
比如ECAM 基地址是0xd0000000

devmem 0xd0000000就是访问00:00.0 设备偏移0寄存器,就是Device ID和Vendor ID
devmem 0xd0100000就是访问01:00.0 设备偏移0寄存器

所以,这里重点就是看ECAM得初始化

pci_acpi_scan_root      // 主桥信息struct pci_root_info  和  struct pci_sysdata 初始化
    -> acpi_pci_root_create // ECAM初始化,主桥资源初始化
        ->  ops->init_info(info)  // 就是 acpi_pci_root_ops得 pci_acpi_root_init_info
        
pci_acpi_root_init_info
    -> setup_mcfg_map

setup_mcfg_map

pci_mmcfg_late_init();
	 // #define ACPI_SIG_MCFG           "MCFG"	/* PCI Memory Mapped Configuration table */
	acpi_table_parse(ACPI_SIG_MCFG, pci_mcfg_parse);  // "ACPI中关于MCFG的描述"
    
if (raw_pci_ext_ops == NULL)
			raw_pci_ext_ops = &pci_mmcfg;       // !!! 初始化接口

如何获取ECAM得基地址?

方式一:打印ACPI表

sudo apt-get install -y iasl acpica-tools
mkdir -p  testacpi  && cd testacpi
acpidump > acpidump.out     # 将ACPI表二进制打印到文件
acpixtract -a acpidump.out  # 解析acpi表,生成各个dat文件
iasl -d mcfg.dat            # iasl会解析acpi 二进制表,生成xxx.dsl描述文件
cat mcfg.dsl 					# 可以查看mcfg的配置文件

比如Intel,我这里看到的是 0x8000_0000,
Start BusNum=00, End BusNum=ff, 所以所有总线的ECAM都在这个空间,按照ECAM地址空间依次偏移即可。

cat /proc/ioport | grep 80000000
80000000-8FFFFFFF  PCI MMCONFIG [bus 00 - ff]

方式二:看内核启动打印

比如这个ECAM的基地址是0xe0000000
[    0.111732] PCI: MMCONFIG for domain 0000 [bus 00-ff] at [mem 0xe0000000-0xefffffff] (base 0xe0000000)
[    0.111734] PCI: MMCONFIG at [mem 0xe0000000-0xefffffff] reserved in E820

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的示例代码,用于在 Linux 系统中通过访问 PCIe 配置空间来读取和写入寄存器值: ``` #include <linux/pci.h> /* 定义 PCI 设备 ID */ #define MY_VENDOR_ID 0x1234 #define MY_DEVICE_ID 0x5678 /* 定义需要读写的寄存器地址 */ #define MY_CONFIG_REG_OFFSET 0x10 /* 访问 PCIe 配置空间 */ void access_pcie_config_space(struct pci_dev *dev) { u32 value; /* 检查设备是否匹配 */ if (dev->vendor != MY_VENDOR_ID || dev->device != MY_DEVICE_ID) return; /* 读取寄存器值 */ pci_read_config_dword(dev, MY_CONFIG_REG_OFFSET, &value); printk(KERN_INFO "Read value from config space: 0x%x\n", value); /* 写入寄存器值 */ value = 0x12345678; pci_write_config_dword(dev, MY_CONFIG_REG_OFFSET, value); printk(KERN_INFO "Write value to config space: 0x%x\n", value); } /* 遍历 PCI 总线上的所有设备 */ static int pcie_driver_probe(struct pci_dev *dev, const struct pci_device_id *id) { /* 注册驱动回调函数 */ pci_read_config_dword(dev, PCI_VENDOR_ID, &vendor); pci_read_config_dword(dev, PCI_DEVICE_ID, &device); access_pcie_config_space(dev); return 0; } /* 定义 PCI 设备 ID 列表 */ static const struct pci_device_id pcie_driver_id_table[] = { { PCI_DEVICE(MY_VENDOR_ID, MY_DEVICE_ID), }, { 0, } }; /* 定义 Linux 驱动结构体 */ static struct pci_driver pcie_driver = { .name = "pcie_driver", .id_table = pcie_driver_id_table, .probe = pcie_driver_probe, }; /* 注册 Linux 驱动 */ module_pci_driver(pcie_driver); ``` 在上面的代码中,`access_pcie_config_space` 函数用于读取和写入 PCIe 配置空间中的寄存器值。它首先检查设备的厂商 ID 和设备 ID 是否匹配,然后使用 `pci_read_config_dword` 和 `pci_write_config_dword` 函数读取和写入寄存器值。 在 `pcie_driver_probe` 函数中,我们遍历 PCI 总线上的所有设备,并在匹配的设备上调用 `access_pcie_config_space` 函数。 最后,我们定义了一个 PCI 设备 ID 列表,并使用 `module_pci_driver` 宏来注册 Linux 驱动。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值