]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - share/man/man9/pci.9
MFC r363988:
[FreeBSD/stable/9.git] / share / man / man9 / pci.9
1 .\"
2 .\" Copyright (c) 2005 Bruce M Simpson <bms@FreeBSD.org>
3 .\" All rights reserved.
4 .\"
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
7 .\" are met:
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.
13 .\"
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
24 .\" SUCH DAMAGE.
25 .\"
26 .\" $FreeBSD$
27 .\"
28 .Dd June 24, 2016
29 .Dt PCI 9
30 .Os
31 .Sh NAME
32 .Nm pci ,
33 .Nm pci_alloc_msi ,
34 .Nm pci_alloc_msix ,
35 .Nm pci_disable_busmaster ,
36 .Nm pci_disable_io ,
37 .Nm pci_enable_busmaster ,
38 .Nm pci_enable_io ,
39 .Nm pci_find_bsf ,
40 .Nm pci_find_cap ,
41 .Nm pci_find_dbsf ,
42 .Nm pci_find_device ,
43 .Nm pci_find_pcie_root_port ,
44 .Nm pci_get_max_payload ,
45 .Nm pci_get_max_read_req ,
46 .Nm pci_get_powerstate ,
47 .Nm pci_get_vpd_ident ,
48 .Nm pci_get_vpd_readonly ,
49 .Nm pci_msi_count ,
50 .Nm pci_msix_count ,
51 .Nm pci_pending_msix ,
52 .Nm pci_read_config ,
53 .Nm pci_release_msi ,
54 .Nm pci_remap_msix ,
55 .Nm pci_restore_state ,
56 .Nm pci_save_state ,
57 .Nm pci_set_max_read_req ,
58 .Nm pci_set_powerstate ,
59 .Nm pci_write_config ,
60 .Nm pcie_adjust_config ,
61 .Nm pcie_read_config ,
62 .Nm pcie_write_config
63 .Nd PCI bus interface
64 .Sh SYNOPSIS
65 .In sys/bus.h
66 .In dev/pci/pcireg.h
67 .In dev/pci/pcivar.h
68 .Ft int
69 .Fn pci_alloc_msi "device_t dev" "int *count"
70 .Ft int
71 .Fn pci_alloc_msix "device_t dev" "int *count"
72 .Ft int
73 .Fn pci_disable_busmaster "device_t dev"
74 .Ft int
75 .Fn pci_disable_io "device_t dev" "int space"
76 .Ft int
77 .Fn pci_enable_busmaster "device_t dev"
78 .Ft int
79 .Fn pci_enable_io "device_t dev" "int space"
80 .Ft device_t
81 .Fn pci_find_bsf "uint8_t bus" "uint8_t slot" "uint8_t func"
82 .Ft int
83 .Fn pci_find_cap "device_t dev" "int capability" "int *capreg"
84 .Ft device_t
85 .Fn pci_find_dbsf "uint32_t domain" "uint8_t bus" "uint8_t slot" "uint8_t func"
86 .Ft device_t
87 .Fn pci_find_device "uint16_t vendor" "uint16_t device"
88 .Ft device_t
89 .Fn pci_find_pcie_root_port "device_t dev"
90 .Ft int
91 .Fn pci_get_max_payload "device_t dev"
92 .Ft int
93 .Fn pci_get_max_read_req "device_t dev"
94 .Ft int
95 .Fn pci_get_powerstate "device_t dev"
96 .Ft int
97 .Fn pci_get_vpd_ident "device_t dev" "const char **identptr"
98 .Ft int
99 .Fn pci_get_vpd_readonly "device_t dev" "const char *kw" "const char **vptr"
100 .Ft int
101 .Fn pci_msi_count "device_t dev"
102 .Ft int
103 .Fn pci_msix_count "device_t dev"
104 .Ft int
105 .Fn pci_pending_msix "device_t dev" "u_int index"
106 .Ft uint32_t
107 .Fn pci_read_config "device_t dev" "int reg" "int width"
108 .Ft int
109 .Fn pci_release_msi "device_t dev"
110 .Ft int
111 .Fn pci_remap_msix "device_t dev" "int count" "const u_int *vectors"
112 .Ft void
113 .Fn pci_restore_state "device_t dev"
114 .Ft void
115 .Fn pci_save_state "device_t dev"
116 .Ft int
117 .Fn pci_set_max_read_req "device_t dev" "int size"
118 .Ft int
119 .Fn pci_set_powerstate "device_t dev" "int state"
120 .Ft void
121 .Fn pci_write_config "device_t dev" "int reg" "uint32_t val" "int width"
122 .Ft uint32_t
123 .Fo pcie_adjust_config
124 .Fa "device_t dev"
125 .Fa "int reg"
126 .Fa "uint32_t mask"
127 .Fa "uint32_t val"
128 .Fa "int width"
129 .Fc
130 .Ft uint32_t
131 .Fn pcie_read_config "device_t dev" "int reg" "int width"
132 .Ft void
133 .Fn pcie_write_config "device_t dev" "int reg" "uint32_t val" "int width"
134 .Sh DESCRIPTION
135 The
136 .Nm
137 set of functions are used for managing PCI devices.
138 The functions are split into several groups:
139 raw configuration access,
140 locating devices,
141 device information,
142 device configuration,
143 and
144 message signaled interrupts.
145 .Ss Raw Configuration Access
146 The
147 .Fn pci_read_config
148 function is used to read data from the PCI configuration
149 space of the device
150 .Fa dev ,
151 at offset
152 .Fa reg ,
153 with
154 .Fa width
155 specifying the size of the access.
156 .Pp
157 The
158 .Fn pci_write_config
159 function is used to write the value
160 .Fa val
161 to the PCI configuration
162 space of the device
163 .Fa dev ,
164 at offset
165 .Fa reg ,
166 with
167 .Fa width
168 specifying the size of the access.
169 .Pp
170 The
171 .Fn pcie_adjust_config
172 function is used to modify the value of a register in the PCI-express
173 capability register set of device
174 .Fa dev .
175 The offset
176 .Fa reg
177 specifies a relative offset in the register set with
178 .Fa width
179 specifying the size of the access.
180 The new value of the register is computed by modifying bits set in
181 .Fa mask
182 to the value in
183 .Fa val .
184 Any bits not specified in
185 .Fa mask
186 are preserved.
187 The previous value of the register is returned.
188 .Pp
189 The
190 .Fn pcie_read_config
191 function is used to read the value of a register in the PCI-express
192 capability register set of device
193 .Fa dev .
194 The offset
195 .Fa reg
196 specifies a relative offset in the register set with
197 .Fa width
198 specifying the size of the access.
199 .Pp
200 The
201 .Fn pcie_write_config
202 function is used to write the value
203 .Fa val
204 to a register in the PCI-express capability register set of device
205 .Fa dev .
206 The offset
207 .Fa reg
208 specifies a relative offset in the register set with
209 .Fa width
210 specifying the size of the access.
211 .Pp
212 .Em NOTE :
213 Device drivers should only use these functions for functionality that
214 is not available via another
215 .Fn pci
216 function.
217 .Ss Locating Devices
218 The
219 .Fn pci_find_bsf
220 function looks up the
221 .Vt device_t
222 of a PCI device, given its
223 .Fa bus ,
224 .Fa slot ,
225 and
226 .Fa func .
227 The
228 .Fa slot
229 number actually refers to the number of the device on the bus,
230 which does not necessarily indicate its geographic location
231 in terms of a physical slot.
232 Note that in case the system has multiple PCI domains,
233 the
234 .Fn pci_find_bsf
235 function only searches the first one.
236 Actually, it is equivalent to:
237 .Bd -literal -offset indent
238 pci_find_dbsf(0, bus, slot, func);
239 .Ed
240 .Pp
241 The
242 .Fn pci_find_dbsf
243 function looks up the
244 .Vt device_t
245 of a PCI device, given its
246 .Fa domain ,
247 .Fa bus ,
248 .Fa slot ,
249 and
250 .Fa func .
251 The
252 .Fa slot
253 number actually refers to the number of the device on the bus,
254 which does not necessarily indicate its geographic location
255 in terms of a physical slot.
256 .Pp
257 The
258 .Fn pci_find_device
259 function looks up the
260 .Vt device_t
261 of a PCI device, given its
262 .Fa vendor
263 and
264 .Fa device
265 IDs.
266 Note that there can be multiple matches for this search; this function
267 only returns the first matching device.
268 .Ss Device Information
269 The
270 .Fn pci_find_cap
271 function is used to locate the first instance of a PCI capability
272 register set for the device
273 .Fa dev .
274 The capability to locate is specified by ID via
275 .Fa capability .
276 Constant macros of the form
277 .Dv PCIY_xxx
278 for standard capability IDs are defined in
279 .In dev/pci/pcireg.h .
280 If the capability is found, then
281 .Fa *capreg
282 is set to the offset in configuration space of the capability register set,
283 and
284 .Fn pci_find_cap
285 returns zero.
286 If the capability is not found or the device does not support capabilities,
287 .Fn pci_find_cap
288 returns an error.
289 .Pp
290 The
291 .Fn pci_find_pcie_root_port
292 function walks up the PCI device hierarchy to locate the PCI-express root
293 port upstream of
294 .Fa dev .
295 If a root port is not found,
296 .Fn pci_find_pcie_root_port
297 returns
298 .Dv NULL .
299 .Pp
300 The
301 .Fn pci_get_vpd_ident
302 function is used to fetch a device's Vital Product Data
303 .Pq VPD
304 identifier string.
305 If the device
306 .Fa dev
307 supports VPD and provides an identifier string,
308 then
309 .Fa *identptr
310 is set to point at a read-only, null-terminated copy of the identifier
311 string,
312 and
313 .Fn pci_get_vpd_ident
314 returns zero.
315 If the device does not support VPD or does not provide an identifier
316 string,
317 then
318 .Fn pci_get_vpd_ident
319 returns an error.
320 .Pp
321 The
322 .Fn pci_get_vpd_readonly
323 function is used to fetch the value of a single VPD read-only keyword
324 for the device
325 .Fa dev .
326 The keyword to fetch is identified by the two character string
327 .Fa kw .
328 If the device supports VPD and provides a read-only value for the
329 requested keyword,
330 then
331 .Fa *vptr
332 is set to point at a read-only, null-terminated copy of the value,
333 and
334 .Fn pci_get_vpd_readonly
335 returns zero.
336 If the device does not support VPD or does not provide the requested
337 keyword,
338 then
339 .Fn pci_get_vpd_readonly
340 returns an error.
341 .Ss Device Configuration
342 The
343 .Fn pci_enable_busmaster
344 function enables PCI bus mastering for the device
345 .Fa dev ,
346 by setting the
347 .Dv PCIM_CMD_BUSMASTEREN
348 bit in the
349 .Dv PCIR_COMMAND
350 register.
351 The
352 .Fn pci_disable_busmaster
353 function clears this bit.
354 .Pp
355 The
356 .Fn pci_enable_io
357 function enables memory or I/O port address decoding for the device
358 .Fa dev ,
359 by setting the
360 .Dv PCIM_CMD_MEMEN
361 or
362 .Dv PCIM_CMD_PORTEN
363 bit in the
364 .Dv PCIR_COMMAND
365 register appropriately.
366 The
367 .Fn pci_disable_io
368 function clears the appropriate bit.
369 The
370 .Fa space
371 argument specifies which resource is affected; this can be either
372 .Dv SYS_RES_MEMORY
373 or
374 .Dv SYS_RES_IOPORT
375 as appropriate.
376 Device drivers should generally not use these routines directly.
377 The PCI bus will enable decoding automatically when a
378 .Dv SYS_RES_MEMORY
379 or
380 .Dv SYS_RES_IOPORT
381 resource is activated via
382 .Xr bus_alloc_resource 9
383 or
384 .Xr bus_activate_resource 9 .
385 .Pp
386 The
387 .Fn pci_get_max_payload
388 function returns the current maximum TLP payload size in bytes for a
389 PCI-express device.
390 If the
391 .Fa dev
392 device is not a PCI-express device,
393 .Fn pci_get_max_payload
394 returns zero.
395 .Pp
396 The
397 .Fn pci_get_max_read_req
398 function returns the current maximum read request size in bytes for a
399 PCI-express device.
400 If the
401 .Fa dev
402 device is not a PCI-express device,
403 .Fn pci_get_max_read_req
404 returns zero.
405 .Pp
406 The
407 .Fn pci_set_max_read_req
408 sets the PCI-express maximum read request size for
409 .Fa dev .
410 The requested
411 .Fa size
412 may be adjusted,
413 and
414 .Fn pci_set_max_read_req
415 returns the actual size set in bytes.
416 If the
417 .Fa dev
418 device is not a PCI-express device,
419 .Fn pci_set_max_read_req
420 returns zero.
421 .Pp
422 The
423 .Fn pci_get_powerstate
424 function returns the current power state of the device
425 .Fa dev .
426 If the device does not support power management capabilities, then the default
427 state of
428 .Dv PCI_POWERSTATE_D0
429 is returned.
430 The following power states are defined by PCI:
431 .Bl -hang -width ".Dv PCI_POWERSTATE_UNKNOWN"
432 .It Dv PCI_POWERSTATE_D0
433 State in which device is on and running.
434 It is receiving full power from the system and delivering
435 full functionality to the user.
436 .It Dv PCI_POWERSTATE_D1
437 Class-specific low-power state in which device context may or
438 may not be lost.
439 Busses in this state cannot do anything to the bus, to
440 force devices to lose context.
441 .It Dv PCI_POWERSTATE_D2
442 Class-specific low-power state in which device context may or
443 may not be lost.
444 Attains greater power savings than
445 .Dv PCI_POWERSTATE_D1 .
446 Busses in this state can cause devices to lose some context.
447 Devices
448 .Em must
449 be prepared for the bus to be in this state or higher.
450 .It Dv PCI_POWERSTATE_D3
451 State in which the device is off and not running.
452 Device context is lost, and power from the device can
453 be removed.
454 .It Dv PCI_POWERSTATE_UNKNOWN
455 State of the device is unknown.
456 .El
457 .Pp
458 The
459 .Fn pci_set_powerstate
460 function is used to transition the device
461 .Fa dev
462 to the PCI power state
463 .Fa state .
464 If the device does not support power management capabilities or
465 it does not support the specific power state
466 .Fa state ,
467 then the function will fail with
468 .Er EOPNOTSUPP .
469 .Pp
470 The
471 .Fn pci_save_state
472 and
473 .Fn pci_restore_state
474 functions can be used by a device driver to save and restore standard PCI
475 config registers.
476 The
477 .Fn pci_save_state
478 function must be invoked while the device has valid state before
479 .Fn pci_restore_state
480 can be used.
481 If the device is not in the fully-powered state
482 .Pq Dv PCI_POWERSTATE_D0
483 when
484 .Fn pci_restore_state
485 is invoked,
486 then the device will be transitioned to
487 .Dv PCI_POWERSTATE_D0
488 before any config registers are restored.
489 .Ss Message Signaled Interrupts
490 Message Signaled Interrupts
491 .Pq MSI
492 and
493 Enhanced Message Signaled Interrupts
494 .Pq MSI-X
495 are PCI capabilities that provide an alternate method for PCI
496 devices to signal interrupts.
497 The legacy INTx interrupt is available to PCI devices as a
498 .Dv SYS_RES_IRQ
499 resource with a resource ID of zero.
500 MSI and MSI-X interrupts are available to PCI devices as one or more
501 .Dv SYS_RES_IRQ
502 resources with resource IDs greater than zero.
503 A driver must ask the PCI bus to allocate MSI or MSI-X interrupts
504 using
505 .Fn pci_alloc_msi
506 or
507 .Fn pci_alloc_msix
508 before it can use MSI or MSI-X
509 .Dv SYS_RES_IRQ
510 resources.
511 A driver is not allowed to use the legacy INTx
512 .Dv SYS_RES_IRQ
513 resource if MSI or MSI-X interrupts have been allocated,
514 and attempts to allocate MSI or MSI-X interrupts will fail if the
515 driver is currently using the legacy INTx
516 .Dv SYS_RES_IRQ
517 resource.
518 A driver is only allowed to use either MSI or MSI-X,
519 but not both.
520 .Pp
521 The
522 .Fn pci_msi_count
523 function returns the maximum number of MSI messages supported by the
524 device
525 .Fa dev .
526 If the device does not support MSI,
527 then
528 .Fn pci_msi_count
529 returns zero.
530 .Pp
531 The
532 .Fn pci_alloc_msi
533 function attempts to allocate
534 .Fa *count
535 MSI messages for the device
536 .Fa dev .
537 The
538 .Fn pci_alloc_msi
539 function may allocate fewer messages than requested for various
540 reasons including requests for more messages than the device
541 .Fa dev
542 supports,
543 or if the system has a shortage of available MSI messages.
544 On success,
545 .Fa *count
546 is set to the number of messages allocated and
547 .Fn pci_alloc_msi
548 returns zero.
549 The
550 .Dv SYS_RES_IRQ
551 resources for the allocated messages will be available at consecutive
552 resource IDs beginning with one.
553 If
554 .Fn pci_alloc_msi
555 is not able to allocate any messages,
556 it returns an error.
557 Note that MSI only supports message counts that are powers of two;
558 requests to allocate a non-power of two count of messages will fail.
559 .Pp
560 The
561 .Fn pci_release_msi
562 function is used to release any allocated MSI or MSI-X messages back
563 to the system.
564 If any MSI or MSI-X
565 .Dv SYS_RES_IRQ
566 resources are allocated by the driver or have a configured interrupt
567 handler,
568 this function will fail with
569 .Er EBUSY .
570 The
571 .Fn pci_release_msi
572 function returns zero on success and an error on failure.
573 .Pp
574 The
575 .Fn pci_msix_count
576 function returns the maximum number of MSI-X messages supported by the
577 device
578 .Fa dev .
579 If the device does not support MSI-X,
580 then
581 .Fn pci_msix_count
582 returns zero.
583 .Pp
584 The
585 .Fn pci_alloc_msix
586 function attempts to allocate
587 .Fa *count
588 MSI-X messages for the device
589 .Fa dev .
590 The
591 .Fn pci_alloc_msix
592 function may allocate fewer messages than requested for various
593 reasons including requests for more messages than the device
594 .Fa dev
595 supports,
596 or if the system has a shortage of available MSI-X messages.
597 On success,
598 .Fa *count
599 is set to the number of messages allocated and
600 .Fn pci_alloc_msix
601 returns zero.
602 For MSI-X messages,
603 the resource ID for each
604 .Dv SYS_RES_IRQ
605 resource identifies the index in the MSI-X table of the
606 corresponding message.
607 A resource ID of one maps to the first index of the MSI-X table;
608 a resource ID two identifies the second index in the table, etc.
609 The
610 .Fn pci_alloc_msix
611 function assigns the
612 .Fa *count
613 messages allocated to the first
614 .Fa *count
615 table indicies.
616 If
617 .Fn pci_alloc_msix
618 is not able to allocate any messages,
619 it returns an error.
620 Unlike MSI,
621 MSI-X does not require message counts that are powers of two.
622 .Pp
623 The
624 .Fn pci_pending_msix
625 function examines the
626 .Fa dev
627 device's Pending Bit Array
628 .Pq PBA
629 to determine the pending status of the MSI-X message at table index
630 .Fa index .
631 If the indicated message is pending,
632 this function returns a non-zero value;
633 otherwise,
634 it returns zero.
635 Passing an invalid
636 .Fa index
637 to this function will result in undefined behavior.
638 .Pp
639 As mentioned in the description of
640 .Fn pci_alloc_msix ,
641 MSI-X messages are initially assigned to the first N table entries.
642 A driver may use a different distribution of available messages to
643 table entries via the
644 .Fn pci_remap_msix
645 function.
646 Note that this function must be called after a successful call to
647 .Fn pci_alloc_msix
648 but before any of the
649 .Dv SYS_RES_IRQ
650 resources are allocated.
651 The
652 .Fn pci_remap_msix
653 function returns zero on success,
654 or an error on failure.
655 .Pp
656 The
657 .Fa vectors
658 array should contain
659 .Fa count
660 message vectors.
661 The array maps directly to the MSI-X table in that the first entry in
662 the array specifies the message used for the first entry in the MSI-X
663 table,
664 the second entry in the array corresponds to the second entry in the
665 MSI-X table,
666 etc.
667 The vector value in each array index can either be zero to indicate
668 that no message should be assigned to the corresponding MSI-X table entry,
669 or it can be a number from one to N
670 .Po
671 where N is the count returned from the previous call to
672 .Fn pci_alloc_msix
673 .Pc
674 to indicate which of the allocated messages should be assigned to the
675 corresponding MSI-X table entry.
676 .Pp
677 If
678 .Fn pci_remap_msix
679 succeeds,
680 each MSI-X table entry with a non-zero vector will have an associated
681 .Dv SYS_RES_IRQ
682 resource whose resource ID corresponds to the table index as described
683 above for
684 .Fn pci_alloc_msix .
685 MSI-X table entries that with a vector of zero will not have an
686 associated
687 .Dv SYS_RES_IRQ
688 resource.
689 Additionally,
690 if any of the original messages allocated by
691 .Fn pci_alloc_msix
692 are not used in the new distribution of messages in the MSI-X table,
693 they will be released automatically.
694 Note that if a driver wishes to use fewer messages than were allocated by
695 .Fn pci_alloc_msix ,
696 the driver must use a single, contiguous range of messages beginning
697 with one in the new distribution.
698 The
699 .Fn pci_remap_msix
700 function will fail if this condition is not met.
701 .Sh IMPLEMENTATION NOTES
702 The
703 .Vt pci_addr_t
704 type varies according to the size of the PCI bus address
705 space on the target architecture.
706 .Sh SEE ALSO
707 .Xr pci 4 ,
708 .Xr pciconf 8 ,
709 .Xr bus_alloc_resource 9 ,
710 .Xr bus_dma 9 ,
711 .Xr bus_release_resource 9 ,
712 .Xr bus_setup_intr 9 ,
713 .Xr bus_teardown_intr 9 ,
714 .Xr devclass 9 ,
715 .Xr device 9 ,
716 .Xr driver 9 ,
717 .Xr rman 9
718 .Rs
719 .%B FreeBSD Developers' Handbook
720 .%T NewBus
721 .%U http://www.FreeBSD.org/doc/en_US.ISO8859-1/books/developers-handbook/
722 .Re
723 .Rs
724 .%A Shanley
725 .%A Anderson
726 .%B PCI System Architecture
727 .%N 2nd Edition
728 .%I Addison-Wesley
729 .%O ISBN 0-201-30974-2
730 .Re
731 .Sh AUTHORS
732 This manual page was written by
733 .An Bruce M Simpson Aq bms@FreeBSD.org
734 and
735 .An John Baldwin Aq jhb@FreeBSD.org .
736 .Sh BUGS
737 The kernel PCI code has a number of references to
738 .Dq "slot numbers" .
739 These do not refer to the geographic location of PCI devices,
740 but to the device number assigned by the combination of the PCI IDSEL
741 mechanism and the platform firmware.
742 This should be taken note of when working with the kernel PCI code.