1 .\" $NetBSD: pci.9,v 1.28 2009/04/17 18:28:39 cegger Exp $
3 .\" Copyright (c) 2001, 2003 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
6 .\" This code is derived from software contributed to The NetBSD Foundation
7 .\" by Gregory McGarry.
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\" notice, this list of conditions and the following disclaimer.
14 .\" 2. Redistributions in binary form must reproduce the above copyright
15 .\" notice, this list of conditions and the following disclaimer in the
16 .\" documentation and/or other materials provided with the distribution.
18 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 .\" POSSIBILITY OF SUCH DAMAGE.
39 .Nm pci_conf_capture ,
40 .Nm pci_conf_restore ,
42 .Nm pci_get_capability ,
49 .Nm pci_intr_establish ,
50 .Nm pci_intr_disestablish ,
51 .Nm pci_get_powerstate ,
52 .Nm pci_set_powerstate ,
56 .Nm pci_decompose_tag ,
62 .Nd Peripheral Component Interconnect
69 .Fn pci_activate "pci_chipset_tag_t pc" "pcitag_t tag" "device_t dev" \
70 "int (*wakeup)(pci_chipset_tag_t pc, pcitag_t tag" \
71 "\t\tdevice_t dev, pcireg_t reg)"
73 .Fn pci_conf_read "pci_chipset_tag_t pc" "pcitag_t tag" "int reg"
75 .Fn pci_conf_write "pci_chipset_tag_t pc" "pcitag_t tag" "int reg" \
78 .Fn pci_conf_print "pci_chipset_tag_t pc" "pcitag_t tag" \
79 "void (*func)(pci_chipset_tag_t, pcitag_t, const pcireg_t *)"
81 .Fn pci_conf_capture "pci_chipset_tag_t pc" "pcitag_t tag" \
82 "struct pci_conf_state *"
84 .Fn pci_conf_restore "pci_chipset_tag_t pc" "pcitag_t tag" \
85 "struct pci_conf_state *"
87 .Fn pci_find_device "struct pci_attach_args *pa" \
88 "int (*func)(struct pci_attach_args *)"
90 .Fn pci_get_capability "pci_chipset_tag_t pc" "pcitag_t tag" \
91 "int capid" "int *offsetp" "pcireg_t *valuep"
93 .Fn pci_mapreg_type "pci_chipset_tag_t pc" "pcitag_t tag" "int reg"
95 .Fn pci_mapreg_map "struct pci_attach_args *pa" "int reg" \
96 "pcireg_t type" "int busflags" "bus_space_tag_t *tagp" \
97 "bus_space_handle_t *handlep" "bus_addr_t *basep" "bus_size_t *sizep"
99 .Fn pci_mapreg_info "pci_chipset_tag_t pc" "pcitag_t tag" "int reg" \
100 "pcireg_t type" "bus_addr_t *basep" "bus_size_t *sizep" "int *flagsp"
102 .Fn pci_find_rom "struct pci_attach_args *pa" \
103 "bus_space_tag_t bst" "bus_space_handle_t bsh" "int code" \
104 "bus_space_handle_t *handlep" "bus_space_size_t *sizep"
106 .Fn pci_intr_map "struct pci_attach_args *pa" "pci_intr_handle_t *ih"
108 .Fn pci_intr_string "pci_chipset_tag_t pc" "pci_intr_handle_t ih"
109 .Ft const struct evcnt *
110 .Fn pci_intr_evcnt "pci_chipset_tag_t pc" "pci_intr_handle_t ih"
112 .Fn pci_intr_establish "pci_chipset_tag_t pc" "pci_intr_handle_t ih" \
113 "int level" "int (*handler)(void *)" "void *arg"
115 .Fn pci_intr_disestablish "pci_chipset_tag_t pc" "void *ih"
117 .Fn pci_set_powerstate "pci_chipset_tag_t pc" "pcitag_t tag" \
120 .Fn pci_get_powerstate "pci_chipset_tag_t pc" "pcitag_t tag" "pcireg_t *state"
122 .Fn pci_vpd_read "pci_chipset_tag_t pc" "pcitag_t tag" "int offset" \
123 "int count" "pcireg_t *data"
125 .Fn pci_vpd_write "pci_chipset_tag_t pc" "pcitag_t tag" "int offset" \
126 "int count" "pcireg_t *data"
128 .Fn pci_make_tag "pci_chipset_tag_t pc" "int bus" "int device" \
131 .Fn pci_decompose_tag "pci_chipset_tag_t pc" "pcitag_t tag" \
132 "int *busp" "int *devicep" "int *functionp"
134 .Fn pci_findvendor "pcireg_t id"
136 .Fn pci_devinfo "pcireg_t id" "pcireg_t class" "int show" "char *cp" "size_t len"
138 .Fn PCI_VENDOR "pcireg_t id"
140 .Fn PCI_PRODUCT "pcireg_t id"
142 .Fn PCI_REVISION "pcireg_t id"
144 The machine-independent
146 subsystem provides support for PCI devices.
148 The PCI bus was initially developed by Intel in the early 1990's to
149 replace the ISA bus for interfacing with their Pentium processor.
150 The PCI specification is widely regarded as well designed, and the
151 PCI bus has found widespread acceptance in machines ranging from
152 Apple's PowerPC-based systems to Sun's UltraSPARC-based machines.
154 The PCI bus is a multiplexed bus, allowing addresses and data on the same
155 pins for a reduced number of pins.
156 Data transfers can be 8-bit, 16-bit or 32-bit.
157 A 64-bit extended PCI bus is also defined.
158 Multi-byte transfers are little-endian.
159 The PCI bus operates up to 33MHz and any device on the bus can be
162 AGP is a version of PCI optimised for high-throughput data rates,
163 particularly for accelerated frame buffers.
165 The PCI bus is a "plug and play" bus, in the sense that devices can be
166 configured dynamically by software.
167 The PCI interface chip on a PCI device bus presents a small window
168 of registers into the PCI configuration space.
169 These registers contain information about the device such as the vendor
171 The configuration registers can also be written to by software to alter
172 how the device interfaces to the PCI bus.
173 An important register in the configuration space is the Base Address
175 The BAR is written to by software to map the device registers into a
176 window of processor address space.
177 Once this mapping is done, the device registers can be accessed relative
180 Drivers for devices attached to the
182 will make use of the following data types:
183 .Bl -tag -width compact
185 Configuration space register.
186 .It Fa pci_chipset_tag_t
187 Chipset tag for the PCI bus.
189 Configuration tag describing the location and function of the PCI
191 It contains the tuple
193 bus, device, function
195 .It Fa pci_intr_handle_t
196 The opaque handle describing an established interrupt handler.
197 .It Fa struct pci_attach_args
198 Devices have their identity recorded in this structure.
199 It contains the following members:
201 bus_space_tag_t pa_iot; /* pci i/o space tag */
202 bus_space_tag_t pa_memt; /* pci mem space tag */
203 bus_dma_tag_t pa_dmat; /* DMA tag */
204 pci_chipset_tag_t pa_pc;
205 int pa_flags; /* flags */
210 .It Fa struct pci_conf_state
211 Stores the PCI configuration state of a device.
212 It contains the following member:
214 pcireg_t reg[16]; /* pci conf register */
218 .Bl -tag -width compact
219 .It Fn pci_activate "pc" "tag" "dev" "fun"
220 Attempt to bring the device to state D0.
221 If the device is not in the D0 state call
223 to restore its state.
228 then restoring from state D3 is going to fail.
229 .It Fn pci_conf_read "pc" "tag" "reg"
232 in PCI configuration space.
235 is the PCI tag for the current device attached to PCI chipset
237 .It Fn pci_conf_write "pc" "tag" "reg" "val"
240 in PCI configuration space.
243 is the PCI tag for the current device attached to PCI chipset
245 .It Fn pci_conf_print "pc" "tag" "func"
246 Print out most of the registers in the PCI configuration for the
250 is the PCI tag for the current device attached to PCI chipset
254 is a function called by
256 to print the device-dependent registers.
257 This function is only useful for driver development and is usually
258 wrapped in pre-processor declarations.
259 .It Fn pci_conf_capture "pc" "tag" "pcs"
260 Capture PCI configuration space into structure
264 is the PCI tag for the current device attached to the PCI
267 .It Fn pci_conf_restore "pc" "tag" "pcs"
268 Restores PCI configuration space from structure
272 is the PCI tag for the current device attached to the PCI
275 .It Fn pci_find_device "pa" "func"
276 Find a device using a match function on all probed busses.
284 is filled in if the device is matched.
286 returns 1 if the device is matched, and zero otherwise.
287 This function is specifically for use by kernel modules
288 and its use otherwise is strongly discouraged.
289 .It Fn pci_get_capability "pc" "tag" "capid" "offsetp" "valuep"
290 Parse the device capability list in configuration space looking for
295 is not NULL, the register offset in configuration space is returned in
299 is not NULL, the value of the capability is returned in
303 is the PCI tag for the current device attached to PCI chipset
305 This function returns 1 if the capability was found.
306 If the capability was not found, it returns zero, and
311 .It Fn pci_mapreg_type "pc" "tag" "reg"
312 Interrogates the Base Address Register (BAR) in configuration space
315 and returns the default (or current) mapping type.
316 Valid returns values are:
317 .Bl -tag -width compact
318 .It Dv PCI_MAPREG_TYPE_IO
319 The mapping is to I/O address space.
320 .It Dv PCI_MAPREG_TYPE_MEM
321 The mapping is to memory address space.
322 .It Dv PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT
323 The mapping is to 64-bit memory address space.
324 .It Dv PCI_MAPREG_TYPE_ROM
325 The mapping is to ROM.
326 Note that in the current implementation,
327 .Dv PCI_MAPREG_TYPE_ROM
328 has the same numeric value as
329 .Dv PCI_MAPREG_TYPE_MEM .
334 is the PCI tag for the current device attached to PCI chipset
336 .It Fn pci_mapreg_map "pa" "reg" "type" "busflags" "tagp" "handlep" "basep" "sizep"
337 Maps the register windows for the device into kernel virtual address
339 This function is generally only called during the driver attach step
340 and takes a pointer to the
341 .Em struct pci_attach_args
344 The physical address of the mapping is in the Base Address Register
345 (BAR) in configuration space specified by
347 Valid values for the type of mapping
350 .Bl -tag -width compact
351 .It Dv PCI_MAPREG_TYPE_IO
352 The mapping should be to I/O address space.
353 .It Dv PCI_MAPREG_TYPE_MEM
354 The mapping should be to memory address space.
355 .It Dv PCI_MAPREG_TYPE_ROM
356 The mapping is to access ROM.
357 This type of mapping is only permitted when the value for
365 are bus-space flags passed to
367 to perform the mapping (see
369 The bus-space tag and handle for the mapped register window are
375 The bus-address and size of the mapping are returned in
388 does not define their return value.
389 This function returns zero on success and non-zero on error.
390 .It Fn pci_mapreg_info "pc" "tag" "reg" "type" "basep" "sizep" "flagsp"
391 Performs the same operations as
393 but doesn't actually map the register window into kernel virtual
395 Returns the bus-address, size and bus flags in
401 These return values can be used by
403 to actually map the register window into kernel virtual address space.
404 This function is useful for setting up the registers in configuration
405 space and deferring the mapping to a later time, such as in a
406 bus-independent attachment routine.
408 returns zero on success and non-zero on failure.
409 .It Fn pci_find_rom "pa" "bst" "bsh" "code" "handlep" "sizep"
410 Locates a suitable ROM image within a PCI expansion ROM previously mapped with
412 and creates a subregion for it with
413 .Fn bus_space_subregion .
418 arguments are the bus tag and handle obtained with the prior call to
420 Valid values for the image type
423 .Bl -tag -width compact
424 .It Dv PCI_ROM_CODE_TYPE_X86
425 Find a ROM image containing i386 executable code for use by PC BIOS.
426 .It Dv PCI_ROM_CODE_TYPE_OFW
427 Find a ROM image containing Forth code for use by Open Firmware.
428 .It Dv PCI_ROM_CODE_TYPE_HPPA
429 Find a ROM image containing HP PA/RISC executable code.
432 The created subregion will cover the entire selected ROM image, including
434 The handle to this subregion is returned in
436 The size of the image (and the corresponding subregion) is returned in
438 This function can only be used with expansion ROMs located at the
440 base address register (BAR).
441 .It Fn pci_intr_map "pa" "ih"
444 .It Fn pci_intr_string "pc" "ih"
447 .It Fn pci_intr_evcnt "pc" "ih"
450 .It Fn pci_intr_establish "pc" "ih" "level" "handler" "arg"
453 .It Fn pci_intr_disestablish "pc" "ih"
456 .It Fn pci_set_powerstate "pc" "tag" "newstate"
457 Set power state of the device to newstate.
462 .Bl -tag -width PCI_PMCSR_STATE_D0 -compact
463 .It Dv PCI_PMCSR_STATE_D0
464 .It Dv PCI_PMCSR_STATE_D1
465 .It Dv PCI_PMCSR_STATE_D2
466 .It Dv PCI_PMCSR_STATE_D3
468 .It Fn pci_get_powerstate "pc" "tag" "state"
469 Get current power state of the device.
470 .It Fn pci_vpd_read "pc" "tag" "offset" "count" "data"
473 32-bit words of Vital Product Data for the device starting at offset
475 into the buffer pointed to by
477 Returns 0 on success or non-zero if the device has no Vital Product Data
478 capability or if reading the Vital Product Data fails.
479 .It Fn pci_vpd_write "pc" "tag" "offset" "count" "data"
482 32-bit words of Vital Product Data for the device starting at offset
484 from the buffer pointed to by
486 Returns 0 on success or non-zero if the device has no Vital Product Data
487 capability of if writing the Vital Product Data fails.
488 .It Fn pci_make_tag "pc" "bus" "device" "function"
489 Create a new PCI tag for the PCI device specified by the tuple
491 bus, device, function
493 This function is not useful to the usual PCI device driver.
494 It is generally used by drivers of multi-function devices when
495 attaching other PCI device drivers to each function.
496 .It Fn pci_decompose_tag "pc" "tag" "busp" "devicep" "fnp"
497 Decompose the PCI tag
503 bus, device, function
506 .It Fn pci_findvendor "id"
507 Return the string of the vendor name for the device specified by
509 .It Fn pci_devinfo "id" "class" "show" "cp" "len"
510 Returns the description string from the in-kernel PCI database for the
515 The description string is returned in
517 the size of that storage is given in
521 specifies whether the PCI subsystem should report the string to the
523 .It Fn PCI_VENDOR "id"
524 Return the PCI vendor id for device
526 .It Fn PCI_PRODUCT "id"
527 Return the PCI product id for device
529 .It Fn PCI_REVISION "id"
530 Return the PCI product revision for device
533 .Sh AUTOCONFIGURATION
534 During autoconfiguration, a
536 driver will receive a pointer to
537 .Fa struct pci_attach_args
538 describing the device attaches to the PCI bus.
539 Drivers match the device using the
547 During the driver attach step, drivers can read the device
548 configuration space using
550 The meaning attached to registers in the PCI configuration space are
551 device-dependent, but will usually contain physical addresses of the
552 device register windows.
553 Device options can also be stored into the PCI configuration space using
555 For example, the driver can request support for bus-mastering DMA by
556 writing the option to the PCI configuration space.
558 Device capabilities can be queried using
559 .Fn pci_get_capability ,
560 and returns device-specific information which can be found in the PCI
561 configuration space to alter device operation.
563 After reading the physical addresses of the device register windows
564 from configuration space, these windows must be mapped into kernel
565 virtual address space using
567 Device registers can now be accessed using the standard bus-space API
571 Details of using PCI interrupts is described in
574 The PCI bus supports bus-mastering operations from any device on the bus.
575 The DMA facilities are accessed through the standard
578 To support DMA transfers from the device to the host, it is necessary
579 to enable bus-mastering in the PCI configuration space for the device.
581 During system shutdown, it is necessary to abort any DMA transfers in
582 progress by registering a shutdown hook (see
583 .Xr shutdownhook_establish 9 ) .
585 This section describes places within the
587 source tree where actual code implementing or using the
588 machine-independent PCI subsystem can be found.
589 All pathnames are relative to
592 The PCI subsystem itself is implemented within the files
593 .Pa sys/dev/pci/pci.c ,
594 .Pa sys/dev/pci/pci_subr.c ,
595 .Pa sys/dev/pci/pci_map.c ,
596 .Pa sys/dev/pci/pci_quirks.c ,
598 .Pa sys/dev/pci/pciconf.c .
599 Machine-dependent portions are implemented within the file
600 .Pa sys/arch/\*[Lt]arch\*[Gt]/pci/pci_machdep.c .
602 The database of known devices exists within the file
603 .Pa sys/dev/pci/pcidevs_data.h
604 and is generated automatically from the file
605 .Pa sys/dev/pci/pcidevs .
606 New vendor and product identifiers should be added to this file.
607 The database can be regenerated using the Makefile
608 .Pa sys/dev/pci/Makefile.pcidevs .
615 .Xr pci_configure_bus 9 ,
617 .Xr shutdownhook_establish 9
619 The machine-independent PCI subsystem appeared in