2 .\" Copyright (c) 2005 Bruce M Simpson <bms@FreeBSD.org>
3 .\" All rights reserved.
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\" notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\" notice, this list of conditions and the following disclaimer in the
12 .\" documentation and/or other materials provided with the distribution.
14 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 .Nm pci_disable_busmaster ,
37 .Nm pci_enable_busmaster ,
45 .Nm pci_find_pcie_root_port ,
47 .Nm pci_get_max_payload ,
48 .Nm pci_get_max_read_req ,
49 .Nm pci_get_powerstate ,
50 .Nm pci_get_vpd_ident ,
51 .Nm pci_get_vpd_readonly ,
53 .Nm pci_iov_attach_name ,
57 .Nm pci_msix_pba_bar ,
58 .Nm pci_msix_table_bar ,
59 .Nm pci_pending_msix ,
63 .Nm pci_restore_state ,
65 .Nm pci_set_max_read_req ,
66 .Nm pci_set_powerstate ,
67 .Nm pci_write_config ,
68 .Nm pcie_adjust_config ,
69 .Nm pcie_read_config ,
77 .Fn pci_alloc_msi "device_t dev" "int *count"
79 .Fn pci_alloc_msix "device_t dev" "int *count"
81 .Fn pci_disable_busmaster "device_t dev"
83 .Fn pci_disable_io "device_t dev" "int space"
85 .Fn pci_enable_busmaster "device_t dev"
87 .Fn pci_enable_io "device_t dev" "int space"
89 .Fn pci_find_bsf "uint8_t bus" "uint8_t slot" "uint8_t func"
91 .Fn pci_find_cap "device_t dev" "int capability" "int *capreg"
93 .Fn pci_find_dbsf "uint32_t domain" "uint8_t bus" "uint8_t slot" "uint8_t func"
95 .Fn pci_find_device "uint16_t vendor" "uint16_t device"
97 .Fn pci_find_extcap "device_t dev" "int capability" "int *capreg"
99 .Fn pci_find_htcap "device_t dev" "int capability" "int *capreg"
101 .Fn pci_find_pcie_root_port "device_t dev"
103 .Fn pci_get_id "device_t dev" "enum pci_id_type type" "uintptr_t *id"
105 .Fn pci_get_max_payload "device_t dev"
107 .Fn pci_get_max_read_req "device_t dev"
109 .Fn pci_get_powerstate "device_t dev"
111 .Fn pci_get_vpd_ident "device_t dev" "const char **identptr"
113 .Fn pci_get_vpd_readonly "device_t dev" "const char *kw" "const char **vptr"
115 .Fn pci_msi_count "device_t dev"
117 .Fn pci_msix_count "device_t dev"
119 .Fn pci_msix_pba_bar "device_t dev"
121 .Fn pci_msix_table_bar "device_t dev"
123 .Fn pci_pending_msix "device_t dev" "u_int index"
125 .Fn pci_read_config "device_t dev" "int reg" "int width"
127 .Fn pci_release_msi "device_t dev"
129 .Fn pci_remap_msix "device_t dev" "int count" "const u_int *vectors"
131 .Fn pci_restore_state "device_t dev"
133 .Fn pci_save_state "device_t dev"
135 .Fn pci_set_max_read_req "device_t dev" "int size"
137 .Fn pci_set_powerstate "device_t dev" "int state"
139 .Fn pci_write_config "device_t dev" "int reg" "uint32_t val" "int width"
141 .Fo pcie_adjust_config
149 .Fn pcie_read_config "device_t dev" "int reg" "int width"
151 .Fn pcie_write_config "device_t dev" "int reg" "uint32_t val" "int width"
152 .In dev/pci/pci_iov.h
154 .Fn pci_iov_attach "device_t dev" "nvlist_t *pf_schema" "nvlist_t *vf_schema"
156 .Fo pci_iov_attach_name
158 .Fa "nvlist_t *pf_schema"
159 .Fa "nvlist_t *vf_schema"
160 .Fa "const char *fmt"
164 .Fn pci_iov_detach "device_t dev"
168 set of functions are used for managing PCI devices.
169 The functions are split into several groups:
170 raw configuration access,
173 device configuration,
175 message signaled interrupts.
176 .Ss Raw Configuration Access
179 function is used to read data from the PCI configuration
186 specifying the size of the access.
190 function is used to write the value
192 to the PCI configuration
199 specifying the size of the access.
202 .Fn pcie_adjust_config
203 function is used to modify the value of a register in the PCI-express
204 capability register set of device
208 specifies a relative offset in the register set with
210 specifying the size of the access.
211 The new value of the register is computed by modifying bits set in
215 Any bits not specified in
218 The previous value of the register is returned.
222 function is used to read the value of a register in the PCI-express
223 capability register set of device
227 specifies a relative offset in the register set with
229 specifying the size of the access.
232 .Fn pcie_write_config
233 function is used to write the value
235 to a register in the PCI-express capability register set of device
239 specifies a relative offset in the register set with
241 specifying the size of the access.
244 Device drivers should only use these functions for functionality that
245 is not available via another
251 function looks up the
253 of a PCI device, given its
260 number actually refers to the number of the device on the bus,
261 which does not necessarily indicate its geographic location
262 in terms of a physical slot.
263 Note that in case the system has multiple PCI domains,
266 function only searches the first one.
267 Actually, it is equivalent to:
268 .Bd -literal -offset indent
269 pci_find_dbsf(0, bus, slot, func);
274 function looks up the
276 of a PCI device, given its
284 number actually refers to the number of the device on the bus,
285 which does not necessarily indicate its geographic location
286 in terms of a physical slot.
290 function looks up the
292 of a PCI device, given its
297 Note that there can be multiple matches for this search; this function
298 only returns the first matching device.
299 .Ss Device Information
302 function is used to locate the first instance of a PCI capability
303 register set for the device
305 The capability to locate is specified by ID via
307 Constant macros of the form
309 for standard capability IDs are defined in
310 .In dev/pci/pcireg.h .
311 If the capability is found, then
313 is set to the offset in configuration space of the capability register set,
317 If the capability is not found or the device does not support capabilities,
323 function is used to locate the first instance of a PCI-express
324 extended capability register set for the device
326 The extended capability to locate is specified by ID via
328 Constant macros of the form
330 for standard extended capability IDs are defined in
331 .In dev/pci/pcireg.h .
332 If the extended capability is found, then
334 is set to the offset in configuration space of the extended capability
338 If the extended capability is not found or the device is not a
345 function is used to locate the first instance of a HyperTransport capability
346 register set for the device
348 The capability to locate is specified by type via
350 Constant macros of the form
352 for standard HyperTransport capability types are defined in
353 .In dev/pci/pcireg.h .
354 If the capability is found, then
356 is set to the offset in configuration space of the capability register set,
360 If the capability is not found or the device is not a HyperTransport device,
365 .Fn pci_find_pcie_root_port
366 function walks up the PCI device hierarchy to locate the PCI-express root
369 If a root port is not found,
370 .Fn pci_find_pcie_root_port
376 function is used to read an identifier from a device.
379 flag is used to specify which identifier to read.
380 The following flags are supported:
381 .Bl -hang -width ".Dv PCI_ID_RID"
383 Read the routing identifier for the device.
385 Read the MSI routing ID.
386 This is needed by some interrupt controllers to route MSI and MSI-X interrupts.
390 .Fn pci_get_vpd_ident
391 function is used to fetch a device's Vital Product Data
396 supports VPD and provides an identifier string,
399 is set to point at a read-only, null-terminated copy of the identifier
402 .Fn pci_get_vpd_ident
404 If the device does not support VPD or does not provide an identifier
407 .Fn pci_get_vpd_ident
411 .Fn pci_get_vpd_readonly
412 function is used to fetch the value of a single VPD read-only keyword
415 The keyword to fetch is identified by the two character string
417 If the device supports VPD and provides a read-only value for the
421 is set to point at a read-only, null-terminated copy of the value,
423 .Fn pci_get_vpd_readonly
425 If the device does not support VPD or does not provide the requested
428 .Fn pci_get_vpd_readonly
430 .Ss Device Configuration
432 .Fn pci_enable_busmaster
433 function enables PCI bus mastering for the device
436 .Dv PCIM_CMD_BUSMASTEREN
441 .Fn pci_disable_busmaster
442 function clears this bit.
446 function enables memory or I/O port address decoding for the device
454 register appropriately.
457 function clears the appropriate bit.
460 argument specifies which resource is affected; this can be either
465 Device drivers should generally not use these routines directly.
466 The PCI bus will enable decoding automatically when a
470 resource is activated via
471 .Xr bus_alloc_resource 9
473 .Xr bus_activate_resource 9 .
476 .Fn pci_get_max_payload
477 function returns the current maximum TLP payload size in bytes for a
481 device is not a PCI-express device,
482 .Fn pci_get_max_payload
486 .Fn pci_get_max_read_req
487 function returns the current maximum read request size in bytes for a
491 device is not a PCI-express device,
492 .Fn pci_get_max_read_req
496 .Fn pci_set_max_read_req
497 sets the PCI-express maximum read request size for
503 .Fn pci_set_max_read_req
504 returns the actual size set in bytes.
507 device is not a PCI-express device,
508 .Fn pci_set_max_read_req
512 .Fn pci_get_powerstate
513 function returns the current power state of the device
515 If the device does not support power management capabilities, then the default
517 .Dv PCI_POWERSTATE_D0
519 The following power states are defined by PCI:
520 .Bl -hang -width ".Dv PCI_POWERSTATE_UNKNOWN"
521 .It Dv PCI_POWERSTATE_D0
522 State in which device is on and running.
523 It is receiving full power from the system and delivering
524 full functionality to the user.
525 .It Dv PCI_POWERSTATE_D1
526 Class-specific low-power state in which device context may or
528 Busses in this state cannot do anything to the bus, to
529 force devices to lose context.
530 .It Dv PCI_POWERSTATE_D2
531 Class-specific low-power state in which device context may or
533 Attains greater power savings than
534 .Dv PCI_POWERSTATE_D1 .
535 Busses in this state can cause devices to lose some context.
538 be prepared for the bus to be in this state or higher.
539 .It Dv PCI_POWERSTATE_D3
540 State in which the device is off and not running.
541 Device context is lost, and power from the device can
543 .It Dv PCI_POWERSTATE_UNKNOWN
544 State of the device is unknown.
548 .Fn pci_set_powerstate
549 function is used to transition the device
551 to the PCI power state
553 If the device does not support power management capabilities or
554 it does not support the specific power state
556 then the function will fail with
561 function is used to advertise that the given device
562 .Pq and associated device driver
563 supports PCI Single-Root I/O Virtualization
565 A driver that supports SR-IOV must implement the
571 This function should be called during the
574 If this function returns an error, it is recommended that the device driver
575 still successfully attaches, but runs with SR-IOV disabled.
580 parameters are used to define what device-specific configuration parameters the
581 device driver accepts when SR-IOV is enabled for the Physical Function
583 and for individual Virtual Functions
588 for details on how to construct the schema.
593 is invalid or specifies parameter names that conflict with parameter names that
596 will return an error and SR-IOV will not be available on the PF device.
597 If a driver does not accept configuration parameters for either the PF device
598 or the VF devices, the driver must pass an empty schema for that device.
599 The SR-IOV infrastructure takes ownership of the
603 and is responsible for freeing them.
604 The driver must never free the schemas itself.
607 .Fn pci_iov_attach_name
608 function is a variant of
610 that allows the name of the associated character device in
616 function uses the name of
622 function is used to advise the SR-IOV infrastructure that the driver for the
623 given device is attempting to detach and that all SR-IOV resources for the
624 device must be released.
625 This function must be called during the
629 was successfully called on the device and
631 has not subsequently been called on the device and returned no error.
632 If this function returns an error, the
634 method must fail and return an error, as detaching the PF driver while VF
635 devices are active would cause system instability.
636 This function is safe to call and will always succeed if
638 previously failed with an error on the given device, or if
640 was never called on the device.
645 .Fn pci_restore_state
646 functions can be used by a device driver to save and restore standard PCI
650 function must be invoked while the device has valid state before
651 .Fn pci_restore_state
653 If the device is not in the fully-powered state
654 .Pq Dv PCI_POWERSTATE_D0
656 .Fn pci_restore_state
658 then the device will be transitioned to
659 .Dv PCI_POWERSTATE_D0
660 before any config registers are restored.
661 .Ss Message Signaled Interrupts
662 Message Signaled Interrupts
665 Enhanced Message Signaled Interrupts
667 are PCI capabilities that provide an alternate method for PCI
668 devices to signal interrupts.
669 The legacy INTx interrupt is available to PCI devices as a
671 resource with a resource ID of zero.
672 MSI and MSI-X interrupts are available to PCI devices as one or more
674 resources with resource IDs greater than zero.
675 A driver must ask the PCI bus to allocate MSI or MSI-X interrupts
680 before it can use MSI or MSI-X
683 A driver is not allowed to use the legacy INTx
685 resource if MSI or MSI-X interrupts have been allocated,
686 and attempts to allocate MSI or MSI-X interrupts will fail if the
687 driver is currently using the legacy INTx
690 A driver is only allowed to use either MSI or MSI-X,
695 function returns the maximum number of MSI messages supported by the
698 If the device does not support MSI,
705 function attempts to allocate
707 MSI messages for the device
711 function may allocate fewer messages than requested for various
712 reasons including requests for more messages than the device
715 or if the system has a shortage of available MSI messages.
718 is set to the number of messages allocated and
723 resources for the allocated messages will be available at consecutive
724 resource IDs beginning with one.
727 is not able to allocate any messages,
729 Note that MSI only supports message counts that are powers of two;
730 requests to allocate a non-power of two count of messages will fail.
734 function is used to release any allocated MSI or MSI-X messages back
738 resources are allocated by the driver or have a configured interrupt
740 this function will fail with
744 function returns zero on success and an error on failure.
748 function returns the maximum number of MSI-X messages supported by the
751 If the device does not support MSI-X,
758 function returns the offset in configuration space of the Base Address Register
760 containing the MSI-X Pending Bit Array (PBA) for device
762 The returned value can be used as the resource ID with
763 .Xr bus_alloc_resource 9
765 .Xr bus_release_resource 9
767 If the device does not support MSI-X,
773 .Fn pci_msix_table_bar
774 function returns the offset in configuration space of the BAR
775 containing the MSI-X vector table for device
777 The returned value can be used as the resource ID with
778 .Xr bus_alloc_resource 9
780 .Xr bus_release_resource 9
782 If the device does not support MSI-X,
784 .Fn pci_msix_table_bar
789 function attempts to allocate
791 MSI-X messages for the device
795 function may allocate fewer messages than requested for various
796 reasons including requests for more messages than the device
799 or if the system has a shortage of available MSI-X messages.
802 is set to the number of messages allocated and
806 the resource ID for each
808 resource identifies the index in the MSI-X table of the
809 corresponding message.
810 A resource ID of one maps to the first index of the MSI-X table;
811 a resource ID two identifies the second index in the table, etc.
816 messages allocated to the first
821 is not able to allocate any messages,
824 MSI-X does not require message counts that are powers of two.
826 The BARs containing the MSI-X vector table and PBA must be
828 .Xr bus_alloc_resource 9
831 and must not be released until after calling
832 .Fn pci_release_msi .
833 Note that the vector table and PBA may be stored in the same BAR or in
838 function examines the
841 to determine the pending status of the MSI-X message at table index
843 If the indicated message is pending,
844 this function returns a non-zero value;
849 to this function will result in undefined behavior.
851 As mentioned in the description of
853 MSI-X messages are initially assigned to the first N table entries.
854 A driver may use a different distribution of available messages to
855 table entries via the
858 Note that this function must be called after a successful call to
860 but before any of the
862 resources are allocated.
865 function returns zero on success,
866 or an error on failure.
873 The array maps directly to the MSI-X table in that the first entry in
874 the array specifies the message used for the first entry in the MSI-X
876 the second entry in the array corresponds to the second entry in the
879 The vector value in each array index can either be zero to indicate
880 that no message should be assigned to the corresponding MSI-X table entry,
881 or it can be a number from one to N
883 where N is the count returned from the previous call to
886 to indicate which of the allocated messages should be assigned to the
887 corresponding MSI-X table entry.
892 each MSI-X table entry with a non-zero vector will have an associated
894 resource whose resource ID corresponds to the table index as described
897 MSI-X table entries that with a vector of zero will not have an
902 if any of the original messages allocated by
904 are not used in the new distribution of messages in the MSI-X table,
905 they will be released automatically.
906 Note that if a driver wishes to use fewer messages than were allocated by
908 the driver must use a single, contiguous range of messages beginning
909 with one in the new distribution.
912 function will fail if this condition is not met.
916 .Xr bus_alloc_resource 9 ,
918 .Xr bus_release_resource 9 ,
919 .Xr bus_setup_intr 9 ,
920 .Xr bus_teardown_intr 9 ,
926 .%B FreeBSD Developers' Handbook
928 .%U http://www.FreeBSD.org/doc/en_US.ISO8859-1/books/developers-handbook/
933 .%B PCI System Architecture
936 .%O ISBN 0-201-30974-2
940 This manual page was written by
941 .An Bruce M Simpson Aq Mt bms@FreeBSD.org
943 .An John Baldwin Aq Mt jhb@FreeBSD.org .
945 The kernel PCI code has a number of references to
947 These do not refer to the geographic location of PCI devices,
948 but to the device number assigned by the combination of the PCI IDSEL
949 mechanism and the platform firmware.
950 This should be taken note of when working with the kernel PCI code.
952 The PCI bus driver should allocate the MSI-X vector table and PBA internally
953 as necessary rather than requiring the caller to do so.