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