diff options
author | Xin LI <delphij@FreeBSD.org> | 2006-03-15 19:54:49 +0000 |
---|---|---|
committer | Xin LI <delphij@FreeBSD.org> | 2006-03-15 19:54:49 +0000 |
commit | d6aa1908eb7dac606d9c0c4f4c46abb47f205022 (patch) | |
tree | 8fefbf336a4556ffff6e7e3e7748f1986b8f7d8b /zh_CN.GB2312/books/arch-handbook/newbus | |
parent | 639205b60c99afefb07be3bccecf1b6eb04c0629 (diff) |
Notes
Diffstat (limited to 'zh_CN.GB2312/books/arch-handbook/newbus')
-rw-r--r-- | zh_CN.GB2312/books/arch-handbook/newbus/chapter.sgml | 339 |
1 files changed, 339 insertions, 0 deletions
diff --git a/zh_CN.GB2312/books/arch-handbook/newbus/chapter.sgml b/zh_CN.GB2312/books/arch-handbook/newbus/chapter.sgml new file mode 100644 index 0000000000..b9eb5741ae --- /dev/null +++ b/zh_CN.GB2312/books/arch-handbook/newbus/chapter.sgml @@ -0,0 +1,339 @@ +<!-- + The FreeBSD Documentation Project + The FreeBSD Simplified Chinese Project + + Original Revision: 1.9 + $FreeBSD$ + + Originally by: Jeroen Ruigrok van der Warven + Date: newbus-draft.txt,v 1.8 2001/01/25 08:01:08 + Copyright (c) 2000 Jeroen Ruigrok van der Warven (asmodai@wxs.nl) + Copyright (c) 2002 Hiten Mahesh Pandya (hiten@uk.FreeBSD.org) + + Future Additions: + + o Expand the information about device_t + o Add information about the bus_* functions. + o Add information about bus specific (e.g. PCI) functions. + o Add a reference section for additional information. + o Add more newbus related structures and typedefs. + o Add a 'Terminology' section. + o Add information on resource manager functions, busspace + manager functions, newbus events related functions. + o More cleanup ... ! + + Provided under the FreeBSD Documentation License. +--> +<chapter id="newbus"> + <chapterinfo> + <authorgroup> + <author> + <firstname>Jeroen</firstname> + <surname>Ruigrok van der Werven (asmodai)</surname> + <affiliation><address><email>asmodai@FreeBSD.org</email></address> + </affiliation> + <contrib>&cnproj.written.by;</contrib> + </author> + <author> + <firstname>Hiten</firstname> + <surname>Pandya</surname> + <affiliation><address><email>hiten@uk.FreeBSD.org</email></address> + </affiliation> + </author> + </authorgroup> + <authorgroup> + <author> + &author.cn.spellar; + <contrib>&cnproj.translated.by;</contrib> + </author> + </authorgroup> + </chapterinfo> + <title>Newbus</title> + + <para><emphasis>特别感谢Matthew N. Dodd, Warner Losh, Bill Paul, + Doug Rabson, Mike Smith, Peter Wemm and Scott Long</emphasis>.</para> + + <para>本章详细解释了Newbus设备框架。</para> + <sect1 id="newbus-devdrivers"> + <title>设备驱动程序</title> + <sect2> + <title>设备驱动程序的目的</title> + + <indexterm><primary>device driver(设备驱动程序)</primary></indexterm> + <indexterm><primary>device driver(设备驱动程序)</primary><secondary>introduction(介绍)</secondary></indexterm> + <para>设备驱动程序是软件组件,它在内核关于外围设备(例如,磁盘、网络 + 适配卡)的通用视图和外围设备的实际实现之间提供了接口。 + <emphasis>设备驱动程序接口(DDI)</emphasis>是内核与设备驱动程序组件 + 之间定义的接口。 + </para> + </sect2> + + <sect2> + <title>设备驱动程序的类型</title> + <para>在&unix;那个时代,FreeBSD也从中延续而来,定义了四种类型的 + 设备:</para> + + <itemizedlist> + <listitem><para>块设备驱动程序</para></listitem> + <listitem><para>字符设备驱动程序</para></listitem> + <listitem><para>网络设备驱动程序</para></listitem> + <listitem><para>伪设备驱动程序</para></listitem> + </itemizedlist> + + <indexterm><primary>block devices(块设备)</primary></indexterm> + + <para><emphasis>块设备</emphasis>以使用固定大小的[数据]块的方式运行。 + 这种类型的驱动程序依赖所谓的 + <emphasis>缓冲区缓存(buffer cache)</emphasis>,其目的 + 是在内存中的专用区域缓存访问过的数据块。这种缓冲区缓存常常基于后台写 + (write-behind),这意味着数据在内存中被修改后,当系统进行其周期性 + 磁盘刷新时才会被同步到磁盘,从而优化写操作。</para> + </sect2> + + <sect2> + <title>字符设备</title> + + <indexterm><primary>character devices(字符设备)</primary></indexterm> + + <para>然而,在FreeBSD 4.0版本以及后续版本中, + 块设备和字符设备的区别变得不存在了。</para> + </sect2> + </sect1> + + <sect1 id="newbus-overview"> + <!-- + Real title: + Newbus, Busspace and the Resource Manager, an Explanation of the Possibilities + --> + <title>Newbus概览</title> + + <indexterm><primary>Newbus</primary></indexterm> + + <para><emphasis>Newbus</emphasis>实现了一种基于抽象层的新型总线结构, + 可以在FreeBSD 3.0中看到这种总线结构的介绍,当时Alpha的移植被导入到 + 代码树中。直到4.0它才成为设备驱动程序使用的默认系统。其目的是为主机 + 系统提供给<emphasis>操作系统</emphasis>的各种总线和设备的互连提供更加 + 面向对象的方法。</para> + + <para>其主要特性包括:</para> + + <itemizedlist> + <listitem><para>动态连接</para></listitem> + <listitem><para>驱动程序容易模块化</para></listitem> + <listitem><para>伪总线</para></listitem> + </itemizedlist> + + <para>最显著的改变之一是从平面和特殊系统演变为设备树布局。</para> + + <para>顶层驻留的是<emphasis><quote>根</quote></emphasis>设备,它作为 + 父设备,所有其他设备挂接在它上面。对于每个结构,通常<quote>根</quote> + 只有单个孩子,其上连接着诸如<emphasis>host-to-PCI桥</emphasis> + 等东西。对于x86,这种<quote>根</quote>设备为 + <emphasis><quote>nexus</quote></emphasis>设备,对于Alpha,Alpha的各种 + 不同型号有不同的顶层设备,对应不同的硬件芯片组,包括 + <emphasis>lca</emphasis>,<emphasis>apecs</emphasis>, + <emphasis>cia</emphasis>和<emphasis>tsunami</emphasis>。</para> + + <para>Newbus上下文中的设备表示系统中的单个硬件实体。例如,每个PCI设备被 + 表示为一个Newbus设备。系统中的任何设备可以有孩子;有孩子的设备通常被 + 称为<emphasis><quote>bus</quote></emphasis>。系统中常用总线的例子就是 + ISA和PCI,他们各自管理连接到ISA和PCI总线上的设备列表。</para> + + <para>通常,不同类型的总线之间的连接被表示为 + <emphasis><quote>桥</quote></emphasis>设备,它的孩子就是它所连接的 + 总线。一个例子就是<emphasis>PCI-to-PCI桥</emphasis>,它在父PCI总线上被 + 表示为<emphasis><devicename>pcibN</devicename></emphasis>,而用它的孩子 + <emphasis><devicename>pciN</devicename></emphasis>表示连接在它上面的 + 总线。这种布局简化了PCI总线树的实现,允许公共代码同时用于顶层和桥接的 + 总线。</para> + + <para>Newbus结构中的每个设备请求它的父设备来为其映射资源。父设备接着请求 + 它的父设备,直到到达nexus。因此,基本上nexus是Newbus系统中唯一知道所有 + 资源的部分。</para> + + <tip><para>ISA设备可能想在<literal>0x230</literal>映射其IO端口,因此它向其 + 父设备请求,这种情况下是ISA总线。ISA总线将它交给PCI-to-ISA桥,PCI-to-ISA + 桥接着请求PCI总线,PCI总线到达host-to-PCI桥,最后到达nexus。这种向上 + 过渡的优美之处在于可以有空间来变换请求。对<literal>0x230</literal>IO端口 + 的请求在<acronym>MIPS</acronym>机器上可以被PCI桥变成 + <literal>0xb0000230</literal>处的内存映射。</para></tip> + + <para>资源分配可以在设备树的任何地方加以控制。例如,在很多Alpha平台上, + ISA中断与PCI中断是单独管理的,对ISA中断的资源分配是由Alpha的ISA总线设备 + 管理的。在IA-32上,ISA和PCI中断都由顶层的nexus设备管理。对于两种移植, + 内存和端口地址空间由单个实体管理 - 在IA-32上是nexus,在Alpha(例如,CIA + 或tsunami)上是相关的芯片组驱动程序。</para> + + <para>为了规范化对内存和端口映射资源的访问,Newbus整合了NetBSD的 + <literal>bus_space</literal> API。他们提供了单一的API来代替inb/outb + 和直接内存读写。这样做的优势在于单个驱动程序就可以使用内存映射寄存器 + 或端口映射寄存器(有些硬件支持两者)。</para> + + <para>这种支持被合并到了资源分配机制中。分配资源时,驱动程序可以从资源 + 中检取关联的<structfield>bus_space_tag_t</structfield>和 + <structfield>bus_space_handle_t</structfield>。</para> + + <para>Newbus也允许在专用于此目的的文件中定义接口方法。这些是 + <filename>.m</filename>文件,可以在<filename>src/sys</filename> + 目录树中找到。</para> + + <para>Newbus系统的核心是可扩展的<quote>基于对象编程(object-based + programming)</quote>的模型。系统中的每个设备具有它所支持的一个方法表。 + 系统和其他设备使用这些方法来控制设备并请求服务。设备所支持的不同方法 + 被定义为多个<quote>接口</quote>。<quote>接口</quote>只是 + 设备实现的一组相关的方法。</para> + + <para>在Newbus系统中,设备方法是通过系统中的各种设备驱动程序提供的。当 + <emphasis>自动配置(auto-configuration)</emphasis>期间设备被连接(attach) + 到驱动程序,它使用驱动程序声明的方法表。以后设备可以从其驱动程序 + <emphasis>分离(detach)</emphasis>,并 + <emphasis>重新连接(re-attach)</emphasis>到具有新方法表的新驱动程序。这就 + 允许驱动程序的动态替换,而动态替换对于驱动程序的开发非常有用。</para> + + <para>接口通过与文件系统中用于定义vnode操作的语言相似的接口定义语言来 + 描述。接口被保存在方法文件中(通常命名为<filename>foo_if.m</filename>)。 + </para> + + <example> + <title>Newbus的方法</title> + <programlisting> + # Foo 子系统/驱动程序(注释...) + + INTERFACE foo + + METHOD int doit { + device_t dev; + }; + + # 如果没有通过DEVMETHOD()提供一个方法,则DEFAULT为将会被使用的方法 + + METHOD void doit_to_child { + device_t dev; + driver_t child; + } DEFAULT doit_generic_to_child; + </programlisting> + </example> + + <para>当接口被编译后,它产生一个头文件 + <quote><filename>foo_if.h</filename></quote>,其中包含函数声明:</para> + + <programlisting> + int FOO_DOIT(device_t dev); + int FOO_DOIT_TO_CHILD(device_t dev, device_t child); + </programlisting> + + <para>伴随自动产生的头文件,也会创建一个源文件 + <quote><filename>foo_if.c</filename></quote>;其中包含一些函数的实现, + 这些函数用于在对象方法表中查找相关函数的位置并调用那个函数。</para> + + <para>系统定义了两个主要接口。第一个基本接口被称为 + <emphasis><quote>设备(device)</quote></emphasis>,并包括与所有设备相关 + 的方法。<emphasis><quote>设备(device)</quote></emphasis>接口中的方法 + 包括<emphasis><quote>探测(probe)</quote></emphasis>, + <emphasis><quote>连接(attach)</quote></emphasis>和 + <emphasis><quote>分离(detach)</quote></emphasis>,他们用来控制硬件的侦测, + 以及<emphasis><quote>关闭(shutdown)</quote></emphasis>, + <emphasis><quote>挂起(suspend)</quote></emphasis>和 + <emphasis><quote>恢复(resume)</quote></emphasis>,他们用于关键事件通知。 + </para> + + <para>另一个,更加复杂接口是<emphasis><quote>bus</quote></emphasis>。 + 此接口包含的方法适用于带有孩子的设备,包括访问总线特定的每设备信息 + <footnote><para>&man.bus.generic.read.ivar.9; and + &man.bus.generic.write.ivar.9;</para></footnote>,事件通知 + (<emphasis><literal>child_detached</literal></emphasis>, + <emphasis><literal>driver_added</literal></emphasis>)和响应管理 + (<emphasis><literal>alloc_resource</literal></emphasis>, + <emphasis><literal>activate_resource</literal></emphasis>, + <emphasis><literal>deactivate_resource</literal></emphasis>, + <emphasis><literal>release_resource</literal></emphasis>)。</para> + + <para><quote>bus</quote>接口中的很多方法为总线设备的某些孩子执行服务。 + 这些方法通常使用前两个参量指定提供服务的总线和请求服务的子设备。为了 + 简化设备驱动程序代码,这些方法中的很多都有访问者(accessor)函数,访问者 + 函数用来查找父设备并调用父设备上的方法。例如,方法 + <literal>BUS_TEARDOWN_INTR(device_t dev, device_t child, ...)</literal> + 可以使用函数 + <literal>bus_teardown_intr(device_t child, ...)</literal>来调用。</para> + + <para>系统中的某些总线类型提供了额外接口以提供对总线特定功能的访问。 + 例如,PCI总线驱动程序定义了<quote>pci</quote>接口,此接口有两个方法 + <emphasis><literal>read_config</literal></emphasis>和 + <emphasis><literal>write_config</literal></emphasis>,用于访问PCI设备 + 的配置寄存器。</para> + </sect1> + + <sect1 id="newbus-api"> + <title>Newbus API</title> + <para>由于Newbus API非常庞大,本节努力将它文档化。本文档的下一版本会 + 带来更多信息。</para> + + <sect2> + <title>源代码目录树中的重要位置</title> + + <para><filename>src/sys/[arch]/[arch]</filename> - 特定机器结构的 + 内核代码位于这个目录。例如<literal>i386</literal>结构或 + <literal>SPARC64</literal>结构。</para> + + <para><filename>src/sys/dev/[bus]</filename> - 支持特定 + <literal>[bus]</literal>的设备位于这个目录。</para> + + <para><filename>src/sys/dev/pci</filename> - PCI总线支持代码位于 + 这个目录。</para> + + <para><filename>src/sys/[isa|pci]</filename> - PCI/ISA设备驱动程序 + 位于这个目录。FreeBSD<literal>4.0</literal>版本中,PCI/ISA支持代码 + 过去存在于这个目录中。</para> + </sect2> + + <sect2> + <title>重要结构和类型定义</title> + <para><literal>devclass_t</literal> - 这是指向 + <literal>struct devclass</literal>的指针的类型定义。</para> + + <para><literal>device_method_t</literal> - 与 + <literal>kobj_method_t</literal>相同(参看 + <filename>src/sys/kobj.h</filename>)。</para> + + <para><literal>device_t</literal> - 这是指向 + <literal>struct device</literal>的指针的类型定义。 + <literal>device_t</literal> 表示系统中的设备。它是内核对象。 + 实现细节参看<filename>src/sys/sys/bus_private.h</filename>。</para> + + <para><literal>driver_t</literal> - 这是一个类型定义,它引用 + <literal>struct driver</literal>。 + <literal>driver</literal>结构是一类 + <literal>device(设备)</literal>内核对象;它也保存着驱动程序的私有数据。 + </para> + + <figure> + <title><emphasis>driver_t</emphasis>实现</title> + <programlisting> + struct driver { + KOBJ_CLASS_FIELDS; + void *priv; /* 驱动程序私有数据 */ + }; + </programlisting> + </figure> + + <para><literal>device_state_t</literal>是一个枚举类型,即 + <literal>device_state</literal>。它包含Newbus设备在自动配置前后 + 可能的状态。</para> + + <figure> + <title>设备状态<emphasis>device_state_t</emphasis></title> + <programlisting> + /* + * src/sys/sys/bus.h + */ + typedef enum device_state { + DS_NOTPRESENT, /* 未探测或探测失败 */ + DS_ALIVE, /* 探测成功 */ + DS_ATTACHED, /* 调用了连接方法 */ + DS_BUSY /* 设备已打开 */ + } device_state_t; + </programlisting> + </figure> + </sect2> + </sect1> +</chapter> |