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