Solaris Source Insight: PCI bus driver moduls - npe Part 1

本文详细介绍了在SunOS内核中如何通过PCI总线驱动程序访问PCI设备的配置空间,包括使用标准DDI接口进行配置空间映射的过程及注意事项。

 Mon Nov  9 16:17:38 CST 2009

We have seen how PCI bus probe enuerate and configure the PCI system. Let's go ahead with the details of PCI bus drivers. The PCI leaf device driver is function related, we will never touch the details of the fucntionality of a specific device.

[root@blu-nhm-ep:~]modinfo | grep PCI
 15 fffffffffba46ce0   bfb0   -   1  pci_autoconfig (PCI BIOS interface)
 37 fffffffffbacd3f0   ce28 183   1  npe (Host to PCIe nexus driver)
 38 fffffffffbad95c8   5f50   -   1  pcihp (PCI nexus hotplug support)
 40 fffffffffbae14f0   bb00   -   1  pcie (PCIE: PCI framework)
 89 fffffffff7bff000   4c68 184   1  pcieb (PCIe to PCI nexus driver)
 90 fffffffff7999000   1d68  84   1  pci_pci (PCI to PCI bridge nexus driver)


npe - Host to PCIe nexus driver
===============================

It's a bus driver registered with the following data.

[i86pc/io/pciex/npe.c]
 149 struct dev_ops npe_ops = {
 150 |_______DEVO_REV,|______|_______/* devo_rev */
 151 |_______0,|_____|_______|_______/* refcnt  */
 152 |_______npe_info,|______|_______/* info */
 153 |_______nulldev,|_______|_______/* identify */
 154 |_______nulldev,|_______|_______/* probe */
 155 |_______npe_attach,|____|_______/* attach */
 156 |_______npe_detach,|____|_______/* detach */
 157 |_______nulldev,|_______|_______/* reset */
 158 |_______&npe_cb_ops,|___|_______/* driver operations */
 159 |_______&npe_bus_ops,|__|_______/* bus operations */
 160 |_______NULL,|__|_______|_______/* power */
 161 |_______ddi_quiesce_not_needed,||_______/* quiesce */
 162 };

Look at attach function firstly.

---- Question: ----
How to access configure space of a PCI device in drivers?

---- Answer: ----
You can use the standard DDI interfaces to access the configure space. The interfaces include at least the following functions.

ddi_regs_map_setup()
ddi_regs_map_free()
ddi_getX()

Here is a sample:

[i86pc/io/pciex/npe_misc.c]
330 void
331 npe_enable_htmsi_children(dev_info_t *dip)
332 {
333 |_______dev_info_t *cdip = ddi_get_child(dip);
334 |_______ddi_acc_handle_t cfg_hdl;
335
336 |_______for (; cdip != NULL; cdip = ddi_get_next_sibling(cdip)) {
337 |_______|_______if (pci_config_setup(cdip, &cfg_hdl) != DDI_SUCCESS) {
338 |_______|_______|_______cmn_err(CE_NOTE, "!npe_enable_htmsi_children: "
339 |_______|_______|_______    "pci_config_setup failed for %s",
340 |_______|_______|_______    ddi_node_name(cdip));
341 |_______|_______}
342
343 |_______|_______(void) npe_enable_htmsi(cfg_hdl);
344 |_______|_______pci_config_teardown(&cfg_hdl);
345 |_______}
346 }

In this function, pci_config_setup() is called firstly to set up the pci configure space mapping.

323 |_______reg = pci_config_get16(cfg_hdl, ptr + PCI_CAP_ID_REGS_OFF);
324 |_______reg |= PCI_HTCAP_MSIMAP_ENABLE;
325
326 |_______pci_config_put16(cfg_hdl, ptr + PCI_CAP_ID_REGS_OFF, reg);

in npe_enable_htmsi(), pci configure space access functions, such as pci_config_get16() are able to be called.

For more deepness, how does the code work?

[common/os/sunpci.c]
  35 int
  36 pci_config_setup(dev_info_t *dip, ddi_acc_handle_t *handle)
  37 {
  38 |_______caddr_t|cfgaddr;
  39 |_______ddi_device_acc_attr_t attr;
  40
  41 |_______attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
  42 |_______attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
  43 |_______attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
  44
  45 |_______/* Check for fault management capabilities */
  46 |_______if (DDI_FM_ACC_ERR_CAP(ddi_fm_capable(dip))) {
  47 |_______|_______attr.devacc_attr_version = DDI_DEVICE_ATTR_V1;
  48 |_______|_______attr.devacc_attr_access = DDI_FLAGERR_ACC;
  49 |_______}
  50
  51 |_______return (ddi_regs_map_setup(dip, 0, &cfgaddr, 0, 0, &attr, handle));
  52 }

In pci_config_setup(), ddi_regs_map_setup() is called to set up a mapping for a register address space. If you read the man page for this fucntion, you will find the parameter description.

     int ddi_regs_map_setup(dev_info_t *dip, uint_t rnumber, caddr_t *addrp,
          offset_t offset, offset_t len, ddi_device_acc_attr_t *accattrp,
          ddi_acc_handle_t *handlep);

PARAMETERS
     dip         Pointer to the device's dev_info structure.

     rnumber     Index number to the register address space set.

     addrp       A platform-dependent value that, when  added  to
                 an  offset that is less than or equal to the len
                 parameter (see below), is used for the  dev_addr
                 argument   to   the  ddi_get,  ddi_mem_get,  and
                 ddi_io_get/put routines.

     offset      Offset into the register address space.

     len         Length to be mapped. If both len and  offset are 0, the
                 entire space is mapped.

     accattrp    Pointer to a device access  attribute  structure
                 of this mapping (see ddi_device_acc_attr(9S)).

     handlep     Pointer to a data access handle.

So line 51 is to set up mapping of the whole first register address space with the "attr" attribution. If you check add_reg_props() which was called during the first pass of pci enumeration,

[intel/io/pci/pci_boot.c]
2297 /*
2298  * Add the "reg" and "assigned-addresses" property
2299  */
2300 static int
2301 add_reg_props(dev_info_t *dip, uchar_t bus, uchar_t dev, uchar_t func,
2302     int config_op, int pciide)

You will find the first register set was encoded with bus, device and function numbers.

[intel/io/pci/pci_boot.c]
2327 |_______devloc = (uint_t)bus << 16 | (uint_t)dev << 11 | (uint_t)func << 8;
2328 |_______regs[0].pci_phys_hi = devloc;
2329 |_______nreg = 1;|______/* rest of regs[0] is all zero */

So, how ddi_regs_map_setup() manages to setup the mapping? It's simple as just call the bus driver's bus operation.

[common/os/sunddi.c]
7612 |_______result = ddi_map(dip, &mr, offset, len, addrp);

[common/os/sunddi.c]
 134 int
 135 ddi_map(dev_info_t *dp, ddi_map_req_t *mp, off_t offset,
 136     off_t len, caddr_t *addrp)
 137 {
 138 |_______dev_info_t *pdip;
 139
 140 |_______ASSERT(dp);
 141 |_______pdip = (dev_info_t *)DEVI(dp)->devi_parent;
 142 |_______return ((DEVI(pdip)->devi_ops->devo_bus_ops->bus_map)(pdip,
 143 |_______    dp, mp, offset, len, addrp));
 144 }

Oh, PCI bus operations are defined in the nexus driver such as npe. Let's check how npe handles it.

In summary, npe bus map function support three sorts of mappings: IO, Mem and configure space. If the request is for IO or memory, npe_bus_map() relies on its parent's (rootnex) bus map operation. If it's a configure space request, it depends on whether the device support MMIO of configure space.

[i86pc/io/pciex/npe_misc.c]
285 /*
286  * Checks to see if MMCFG is supported.
287  * Returns: TRUE if MMCFG is supported, FALSE if not.
288  *
289  * If a device is attached to a parent whose "dev_type" is "pciex",
290  * the device will support MMCFG access.  Otherwise, use legacy IOCFG access.
291  *
292  * Enable Legacy PCI config space access for AMD K8 north bridges.
293  *|_____Host bridge: AMD HyperTransport Technology Configuration
294  *|_____Host bridge: AMD Address Map
295  *|_____Host bridge: AMD DRAM Controller
296  *|_____Host bridge: AMD Miscellaneous Control
297  * These devices do not support MMCFG access.
298  */
299 boolean_t
300 npe_is_mmcfg_supported(dev_info_t *dip)
301 {
302 |_______int vendor_id, device_id;
303
304 |_______vendor_id = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
305 |_______    "vendor-id", -1);
306 |_______device_id = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
307 |_______    "device-id", -1);
308
309 |_______return !(npe_child_is_pci(dip) ||
310 |_______    IS_BAD_AMD_NTBRIDGE(vendor_id, device_id));
311 }

If it has MMIO supported configure space, it will be treated as a memory mapping request. Otherwise, the lagacy methods are used.

[i86pc/io/pciex/npe.c]
 386 /*
 387  * Configure the access handle for standard configuration space
 388  * access (see pci_fm_acc_setup for code that initializes the
 389  * access-function pointers).
 390  */
 391 static int
 392 npe_setup_std_pcicfg_acc(dev_info_t *rdip, ddi_map_req_t *mp,
 393     ddi_acc_hdl_t *hp, off_t offset, off_t len)
 394 {
 395 |_______int ret;
 396
 397 |_______if ((ret = pci_fm_acc_setup(hp, offset, len)) ==
 398 |_______    DDI_SUCCESS) {
 399 |_______|_______if (DDI_FM_ACC_ERR_CAP(ddi_fm_capable(rdip)) &&
 400 |_______|_______    mp->map_handlep->ah_acc.devacc_attr_access
 401 |_______|_______    != DDI_DEFAULT_ACC) {
 402 |_______|_______|_______ndi_fmc_insert(rdip, ACC_HANDLE,
 403 |_______|_______|_______    (void *)mp->map_handlep, NULL);
 404 |_______|_______}
 405 |_______}
 406 |_______return (ret);
 407 }
 
in pci_fm_acc_setup(), the get/put function pointers are assigned. It always set cautious mechanism for config space gets, while set those for put according to the attributes specified in devacc_attr_access of ddi_device_acc_attr structure.

[man ddi_device_acc_attr]
152      The values defined for devacc_attr_access are:
153
154      DDI_DEFAULT_ACC     If an I/O fault occurs, the system  will
155                          take  the default action, which might be
156                          to panic.
157
158
159      DDI_FLAGERR_ACC     Using  this  value  indicates  that  the
160                          driver  is  hardened:  able to cope with
161                          the incorrect results of I/O  operations
162                          that might result from an I/O fault. The
163                          value also  indicates  that  the  driver
164                          will use ddi_fm_acc_err_get(9F) to check
165                          access handles for faults on  a  regular
166                          basis.
167
168                          If possible, the system should not panic
169                          on such an I/O fault, but should instead
170                          mark the I/O handle  through  which  the
171                          access was made as having faulted.
172
173                          This value is  advisory:  it  tells  the
174                          system  that  the driver can continue in
175                          the face of I/O faults. The  value  does
176                          not  guarantee  that the system will not
177                          panic, as that depends on the nature  of
178                          the  fault  and  the capabilities of the
179                          system. It is quite  legitimate  for  an
180                          implementation  to  ignore this flag and
181                          panic anyway.
182
183
184      DDI_CAUTIOUS_ACC    This value indicates that an  I/O  fault
185                          is  anticipated and should be handled as
186                          gracefully as possible. For example, the
187                          framework  should  not  print  a console
188                          message.
 

[i86pc/io/pci/pci_common.c]
1151 |_______/*
1152 |_______ * always use cautious mechanism for config space gets
1153 |_______ */
1154 |_______ap->ahi_get8 = i_ddi_caut_get8;
1155 |_______ap->ahi_get16 = i_ddi_caut_get16;
1156 |_______ap->ahi_get32 = i_ddi_caut_get32;
1157 |_______ap->ahi_get64 = i_ddi_caut_get64;
1158 |_______ap->ahi_rep_get8 = i_ddi_caut_rep_get8;
1159 |_______ap->ahi_rep_get16 = i_ddi_caut_rep_get16;
1160 |_______ap->ahi_rep_get32 = i_ddi_caut_rep_get32;
1161 |_______ap->ahi_rep_get64 = i_ddi_caut_rep_get64;
1162 |_______if (hp->ah_acc.devacc_attr_access == DDI_CAUTIOUS_ACC) {
1163 |_______|_______ap->ahi_put8 = i_ddi_caut_put8;
1164 |_______|_______ap->ahi_put16 = i_ddi_caut_put16;
1165 |_______|_______ap->ahi_put32 = i_ddi_caut_put32;
1166 |_______|_______ap->ahi_put64 = i_ddi_caut_put64;
1167 |_______|_______ap->ahi_rep_put8 = i_ddi_caut_rep_put8;
1168 |_______|_______ap->ahi_rep_put16 = i_ddi_caut_rep_put16;
1169 |_______|_______ap->ahi_rep_put32 = i_ddi_caut_rep_put32;
1170 |_______|_______ap->ahi_rep_put64 = i_ddi_caut_rep_put64;
1171 |_______} else {
1172 |_______|_______ap->ahi_put8 = pci_config_wr8;
1173 |_______|_______ap->ahi_put16 = pci_config_wr16;
1174 |_______|_______ap->ahi_put32 = pci_config_wr32;
1175 |_______|_______ap->ahi_put64 = pci_config_wr64;
1176 |_______|_______ap->ahi_rep_put8 = pci_config_rep_wr8;
1177 |_______|_______ap->ahi_rep_put16 = pci_config_rep_wr16;
1178 |_______|_______ap->ahi_rep_put32 = pci_config_rep_wr32;
1179 |_______|_______ap->ahi_rep_put64 = pci_config_rep_wr64;
1180 |_______}

For cautious mechanism, the configure space access request will be handled as a bus_ctl request.

[common/os/sunddi.c]
 682 /*
 683  * Request bus_ctl parent to handle a bus_ctl request
 684  *
 685  * (The sparc version is in sparc_ddi.s)
 686  */
 687 int
 688 ddi_ctlops(dev_info_t *d, dev_info_t *r, ddi_ctl_enum_t op, void *a, void *v)
 689 {
 690 |_______int (*fp)();
 691
 692 |_______if (!d || !r)
 693 |_______|_______return (DDI_FAILURE);
 694
 695 |_______if ((d = (dev_info_t *)DEVI(d)->devi_bus_ctl) == NULL)
 696 |_______|_______return (DDI_FAILURE);
 697
 698 |_______fp = DEVI(d)->devi_ops->devo_bus_ops->bus_ctl;
 699 |_______return ((*fp)(d, r, op, a, v));
 700 }

In npe_bus_ctl(), the folowing path deals with pci configure space put/get.

[i86pc/io/pciex/npe.c]
 775 |_______case DDI_CTLOPS_PEEK:
 776 |_______case DDI_CTLOPS_POKE:
 777 |_______|_______return (pci_common_peekpoke(dip, rdip, ctlop, arg, result));

At last, the request is handled with one of the following functions.

pci_getb_func()
pci_getw_func()
pci_getl_func()
pci_putb_func()
pci_putw_func()
pci_putl_func()

These interfaces are defined in ./i86pc/os/pci_cfgspace.c.

[./i86pc/os/pci_cfgspace.c]
 56 /*
 57  * These function pointers lead to the actual implementation routines
 58  * for configuration space access.  Normally they lead to either the
 59  * pci_mech1_* or pci_mech2_* routines, but they can also lead to
 60  * routines that work around chipset bugs.
 61  */
 62 uint8_t (*pci_getb_func)(int bus, int dev, int func, int reg);
 63 uint16_t (*pci_getw_func)(int bus, int dev, int func, int reg);
 64 uint32_t (*pci_getl_func)(int bus, int dev, int func, int reg);
 65 void (*pci_putb_func)(int bus, int dev, int func, int reg, uint8_t val);
 66 void (*pci_putw_func)(int bus, int dev, int func, int reg, uint16_t val);
 67 void (*pci_putl_func)(int bus, int dev, int func, int reg, uint32_t val);

These gobal pointers were initialized at pci_check().

[./i86pc/os/pci_cfgspace.c]
 98 /*
 99  * This code determines if this system supports PCI and which
100  * type of configuration access method is used
101  */
102
103 static int
104 pci_check(void)
105 {

which is called in mlsetup() at boot time. mlsetup() contains routines called right before main(). Interposing this function before main() allows us to call it in a machine-independent fashion.

Okay, back to the topic. For non-cautious mechanism, it will directly call pci configure space access methods.

pci_getb_func()
pci_getw_func()
pci_getl_func()
pci_putb_func()
pci_putw_func()
pci_putl_func()

That means, in current kernel there is no difference between the different configure access attribute in "devacc_attr_access".

---- End of Q/A ----

To be continued ... ....

基于51单片机,实现对直流电机的调速、测速以及正反转控制。项目包含完整的仿真文件、源程序、原理图和PCB设计文件,适合学习和实践51单片机在电机控制方面的应用。 功能特点 调速控制:通过按键调整PWM占空比,实现电机的速度调节。 测速功能:采用霍尔传感器非接触式测速,实时显示电机转速。 正反转控制:通过按键切换电机的正转和反转状态。 LCD显示:使用LCD1602液晶显示屏,显示当前的转速和PWM占空比。 硬件组成 主控制器:STC89C51/52单片机(与AT89S51/52、AT89C51/52通用)。 测速传感器:霍尔传感器,用于非接触式测速。 显示模块:LCD1602液晶显示屏,显示转速和占空比。 电机驱动:采用双H桥电路,控制电机的正反转和调速。 软件设计 编程语言:C语言。 开发环境:Keil uVision。 仿真工具:Proteus。 使用说明 液晶屏显示: 第一行显示电机转速(单位:转/分)。 第二行显示PWM占空比(0~100%)。 按键功能: 1键:加速键,短按占空比加1,长按连续加。 2键:减速键,短按占空比减1,长按连续减。 3键:反转切换键,按下后电机反转。 4键:正转切换键,按下后电机正转。 5键:开始暂停键,按一下开始,再按一下暂停。 注意事项 磁铁和霍尔元件的距离应保持在2mm左右,过近可能会在电机转动时碰到霍尔元件,过远则可能导致霍尔元件无法检测到磁铁。 资源文件 仿真文件:Proteus仿真文件,用于模拟电机控制系统的运行。 源程序:Keil uVision项目文件,包含完整的C语言源代码。 原理图:电路设计原理图,详细展示了各模块的连接方式。 PCB设计:PCB布局文件,可用于实际电路板的制作。
【四旋翼无人机】具备螺旋桨倾斜机构的全驱动四旋翼无人机:建模与控制研究(Matlab代码、Simulink仿真实现)内容概要:本文围绕具备螺旋桨倾斜机构的全驱动四旋翼无人机展开研究,重点进行了系统建模与控制策略的设计与仿真验证。通过引入螺旋桨倾斜机构,该无人机能够实现全向力矢量控制,从而具备更强的姿态调节能力和六自由度全驱动特性,克服传统四旋翼欠驱动限制。研究内容涵盖动力学建模、控制系统设计(如PID、MPC等)、Matlab/Simulink环境下的仿真验证,并可能涉及轨迹跟踪、抗干扰能力及稳定性分析,旨在提升无人机在复杂环境下的机动性与控制精度。; 适合人群:具备一定控制理论基础和Matlab/Simulink仿真能力的研究生、科研人员及从事无人机系统开发的工程师,尤其适合研究先进无人机控制算法的技术人员。; 使用场景及目标:①深入理解全驱动四旋翼无人机的动力学建模方法;②掌握基于Matlab/Simulink的无人机控制系统设计与仿真流程;③复现硕士论文级别的研究成果,为科研项目或学术论文提供技术支持与参考。; 阅读建议:建议结合提供的Matlab代码与Simulink模型进行实践操作,重点关注建模推导过程与控制器参数调优,同时可扩展研究不同控制算法的性能对比,以深化对全驱动系统控制机制的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值