2 * Copyright (c) 2007 Yahoo!, Inc.
4 * Written by: John Baldwin <jhb@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the names of any co-contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $FreeBSD: src/usr.sbin/pciconf/cap.c,v 1.11 2010/09/16 16:03:12 jhb Exp $
33 #include <sys/types.h>
37 #include <sys/agpio.h>
38 #include <sys/pciio.h>
40 #include <dev/agp/agpreg.h>
41 #include <bus/pci/pcireg.h>
45 static void list_ecaps(int fd
, struct pci_conf
*p
);
48 cap_power(int fd
, struct pci_conf
*p
, uint8_t ptr
)
52 cap
= read_config(fd
, &p
->pc_sel
, ptr
+ PCIR_POWER_CAP
, 2);
53 status
= read_config(fd
, &p
->pc_sel
, ptr
+ PCIR_POWER_STATUS
, 2);
54 printf("powerspec %d supports D0%s%s D3 current D%d",
56 cap
& PCIM_PCAP_D1SUPP
? " D1" : "",
57 cap
& PCIM_PCAP_D2SUPP
? " D2" : "",
58 status
& PCIM_PSTAT_DMASK
);
62 cap_agp(int fd
, struct pci_conf
*p
, uint8_t ptr
)
64 uint32_t status
, command
;
66 status
= read_config(fd
, &p
->pc_sel
, ptr
+ AGP_STATUS
, 4);
67 command
= read_config(fd
, &p
->pc_sel
, ptr
+ AGP_CAPID
, 4);
69 if (AGP_MODE_GET_MODE_3(status
)) {
71 if (AGP_MODE_GET_RATE(status
) & AGP_MODE_V3_RATE_8x
)
73 if (AGP_MODE_GET_RATE(status
) & AGP_MODE_V3_RATE_4x
)
76 if (AGP_MODE_GET_RATE(status
) & AGP_MODE_V2_RATE_4x
)
78 if (AGP_MODE_GET_RATE(status
) & AGP_MODE_V2_RATE_2x
)
80 if (AGP_MODE_GET_RATE(status
) & AGP_MODE_V2_RATE_1x
)
83 if (AGP_MODE_GET_SBA(status
))
85 if (AGP_MODE_GET_AGP(command
)) {
86 printf("enabled at ");
87 if (AGP_MODE_GET_MODE_3(command
)) {
89 switch (AGP_MODE_GET_RATE(command
)) {
90 case AGP_MODE_V3_RATE_8x
:
93 case AGP_MODE_V3_RATE_4x
:
98 switch (AGP_MODE_GET_RATE(command
)) {
99 case AGP_MODE_V2_RATE_4x
:
102 case AGP_MODE_V2_RATE_2x
:
105 case AGP_MODE_V2_RATE_1x
:
109 if (AGP_MODE_GET_SBA(command
))
116 cap_vpd(int fd __unused
, struct pci_conf
*p __unused
, uint8_t ptr __unused
)
123 cap_msi(int fd
, struct pci_conf
*p
, uint8_t ptr
)
128 ctrl
= read_config(fd
, &p
->pc_sel
, ptr
+ PCIR_MSI_CTRL
, 2);
129 msgnum
= 1 << ((ctrl
& PCIM_MSICTRL_MMC_MASK
) >> 1);
130 printf("MSI supports %d message%s%s%s ", msgnum
,
131 (msgnum
== 1) ? "" : "s",
132 (ctrl
& PCIM_MSICTRL_64BIT
) ? ", 64 bit" : "",
133 (ctrl
& PCIM_MSICTRL_VECTOR
) ? ", vector masks" : "");
134 if (ctrl
& PCIM_MSICTRL_MSI_ENABLE
) {
135 msgnum
= 1 << ((ctrl
& PCIM_MSICTRL_MME_MASK
) >> 4);
136 printf("enabled with %d message%s", msgnum
,
137 (msgnum
== 1) ? "" : "s");
142 cap_pcix(int fd
, struct pci_conf
*p
, uint8_t ptr
)
145 int comma
, max_splits
= 0, max_burst_read
= 0;
147 status
= read_config(fd
, &p
->pc_sel
, ptr
+ PCIXR_STATUS
, 4);
149 if (status
& PCIXM_STATUS_64BIT
)
151 if ((p
->pc_hdr
& PCIM_HDRTYPE
) == 1)
153 if ((p
->pc_hdr
& PCIM_HDRTYPE
) != 1 || (status
& (PCIXM_STATUS_133CAP
|
154 PCIXM_STATUS_266CAP
| PCIXM_STATUS_533CAP
)) != 0)
157 if (status
& PCIXM_STATUS_133CAP
) {
158 printf("%s 133MHz", comma
? "," : "");
161 if (status
& PCIXM_STATUS_266CAP
) {
162 printf("%s 266MHz", comma
? "," : "");
165 if (status
& PCIXM_STATUS_533CAP
) {
166 printf("%s 533MHz", comma
? "," : "");
169 if ((p
->pc_hdr
& PCIM_HDRTYPE
) == 1)
171 switch (status
& PCIXM_STATUS_MAX_READ
) {
172 case PCIXM_STATUS_MAX_READ_512
:
173 max_burst_read
= 512;
175 case PCIXM_STATUS_MAX_READ_1024
:
176 max_burst_read
= 1024;
178 case PCIXM_STATUS_MAX_READ_2048
:
179 max_burst_read
= 2048;
181 case PCIXM_STATUS_MAX_READ_4096
:
182 max_burst_read
= 4096;
185 switch (status
& PCIXM_STATUS_MAX_SPLITS
) {
186 case PCIXM_STATUS_MAX_SPLITS_1
:
189 case PCIXM_STATUS_MAX_SPLITS_2
:
192 case PCIXM_STATUS_MAX_SPLITS_3
:
195 case PCIXM_STATUS_MAX_SPLITS_4
:
198 case PCIXM_STATUS_MAX_SPLITS_8
:
201 case PCIXM_STATUS_MAX_SPLITS_12
:
204 case PCIXM_STATUS_MAX_SPLITS_16
:
207 case PCIXM_STATUS_MAX_SPLITS_32
:
211 printf("%s %d burst read, %d split transaction%s", comma
? "," : "",
212 max_burst_read
, max_splits
, max_splits
== 1 ? "" : "s");
216 cap_ht(int fd
, struct pci_conf
*p
, uint8_t ptr
)
221 command
= read_config(fd
, &p
->pc_sel
, ptr
+ PCIR_HT_COMMAND
, 2);
223 if ((command
& 0xe000) == PCIM_HTCAP_SLAVE
)
225 else if ((command
& 0xe000) == PCIM_HTCAP_HOST
)
228 switch (command
& PCIM_HTCMD_CAP_MASK
) {
229 case PCIM_HTCAP_SWITCH
:
232 case PCIM_HTCAP_INTERRUPT
:
235 case PCIM_HTCAP_REVISION_ID
:
236 printf("revision ID");
238 case PCIM_HTCAP_UNITID_CLUMPING
:
239 printf("unit ID clumping");
241 case PCIM_HTCAP_EXT_CONFIG_SPACE
:
242 printf("extended config space");
244 case PCIM_HTCAP_ADDRESS_MAPPING
:
245 printf("address mapping");
247 case PCIM_HTCAP_MSI_MAPPING
:
248 printf("MSI %saddress window %s at 0x",
249 command
& PCIM_HTCMD_MSI_FIXED
? "fixed " : "",
250 command
& PCIM_HTCMD_MSI_ENABLE
? "enabled" :
252 if (command
& PCIM_HTCMD_MSI_FIXED
)
255 reg
= read_config(fd
, &p
->pc_sel
,
256 ptr
+ PCIR_HTMSI_ADDRESS_HI
, 4);
259 reg
= read_config(fd
, &p
->pc_sel
,
260 ptr
+ PCIR_HTMSI_ADDRESS_LO
, 4);
264 case PCIM_HTCAP_DIRECT_ROUTE
:
265 printf("direct route");
267 case PCIM_HTCAP_VCSET
:
270 case PCIM_HTCAP_RETRY_MODE
:
271 printf("retry mode");
273 case PCIM_HTCAP_X86_ENCODING
:
274 printf("X86 encoding");
277 printf("unknown %02x", command
);
283 cap_vendor(int fd
, struct pci_conf
*p
, uint8_t ptr
)
287 length
= read_config(fd
, &p
->pc_sel
, ptr
+ PCIR_VENDOR_LENGTH
, 1);
288 printf("vendor (length %d)", length
);
289 if (p
->pc_vendor
== 0x8086) {
293 version
= read_config(fd
, &p
->pc_sel
, ptr
+ PCIR_VENDOR_DATA
,
295 printf(" Intel cap %d version %d", version
>> 4, version
& 0xf);
296 if (version
>> 4 == 1 && length
== 12) {
297 /* Feature Detection */
302 fvec
= read_config(fd
, &p
->pc_sel
, ptr
+
303 PCIR_VENDOR_DATA
+ 5, 4);
304 printf("\n\t\t features:");
305 if (fvec
& (1 << 0)) {
309 fvec
= read_config(fd
, &p
->pc_sel
, ptr
+
310 PCIR_VENDOR_DATA
+ 1, 4);
311 if (fvec
& (1 << 21)) {
312 printf("%s Quick Resume", comma
? "," : "");
315 if (fvec
& (1 << 18)) {
316 printf("%s SATA RAID-5", comma
? "," : "");
319 if (fvec
& (1 << 9)) {
320 printf("%s Mobile", comma
? "," : "");
323 if (fvec
& (1 << 7)) {
324 printf("%s 6 PCI-e x1 slots", comma
? "," : "");
327 printf("%s 4 PCI-e x1 slots", comma
? "," : "");
330 if (fvec
& (1 << 5)) {
331 printf("%s SATA RAID-0/1/10", comma
? "," : "");
334 if (fvec
& (1 << 3)) {
335 printf("%s SATA AHCI", comma
? "," : "");
343 cap_debug(int fd
, struct pci_conf
*p
, uint8_t ptr
)
347 debug_port
= read_config(fd
, &p
->pc_sel
, ptr
+ PCIR_DEBUG_PORT
, 2);
348 printf("EHCI Debug Port at offset 0x%x in map 0x%x", debug_port
&
349 PCIM_DEBUG_PORT_OFFSET
, PCIR_BAR(debug_port
>> 13));
353 cap_subvendor(int fd
, struct pci_conf
*p
, uint8_t ptr
)
357 id
= read_config(fd
, &p
->pc_sel
, ptr
+ PCIR_SUBVENDCAP_ID
, 4);
358 printf("PCI Bridge card=0x%08x", id
);
361 #define MAX_PAYLOAD(field) (128 << (field))
364 cap_express(int fd
, struct pci_conf
*p
, uint8_t ptr
)
369 flags
= read_config(fd
, &p
->pc_sel
, ptr
+ PCIER_CAPABILITY
, 2);
370 printf("PCI-Express %d ", flags
& PCIEM_CAP_VER_MASK
);
371 switch (flags
& PCIEM_CAP_PORT_TYPE
) {
375 case PCIE_LEG_END_POINT
:
376 printf("legacy endpoint");
381 case PCIE_UP_STREAM_PORT
:
382 printf("upstream port");
384 case PCIE_DOWN_STREAM_PORT
:
385 printf("downstream port");
387 case PCIE_PCIE2PCI_BRIDGE
:
388 printf("PCI bridge");
390 case PCIE_PCI2PCIE_BRIDGE
:
391 printf("PCI to PCIe bridge");
393 case PCIE_ROOT_END_POINT
:
394 printf("root endpoint");
396 case PCIE_ROOT_EVT_COLL
:
397 printf("event collector");
400 printf("type %d", (flags
& PCIEM_CAP_PORT_TYPE
) >> 8);
403 if (flags
& PCIEM_CAP_IRQ_MSGNO
)
404 printf(" IRQ %d", (flags
& PCIEM_CAP_IRQ_MSGNO
) >> 8);
405 val
= read_config(fd
, &p
->pc_sel
, ptr
+ PCIER_DEVCAP
, 4);
406 flags
= read_config(fd
, &p
->pc_sel
, ptr
+ PCIER_DEVCTRL
, 2);
407 printf(" max data %d(%d)",
408 MAX_PAYLOAD((flags
& PCIEM_DEVCAP_MAX_PAYLOAD
) >> 5),
409 MAX_PAYLOAD(val
& PCIEM_DEVCAP_MAX_PAYLOAD
));
410 val
= read_config(fd
, &p
->pc_sel
, ptr
+ PCIER_LINKCAP
, 4);
411 flags
= read_config(fd
, &p
->pc_sel
, ptr
+ PCIER_LINKSTAT
, 2);
412 printf(" link x%d(x%d)", (flags
& PCIEM_LNKSTAT_WIDTH
) >> 4,
413 (val
& PCIEM_LNKCAP_MAXW_MASK
) >> 4);
417 cap_msix(int fd
, struct pci_conf
*p
, uint8_t ptr
)
421 int msgnum
, table_bar
, pba_bar
;
423 ctrl
= read_config(fd
, &p
->pc_sel
, ptr
+ PCIR_MSIX_CTRL
, 2);
424 msgnum
= (ctrl
& PCIM_MSIXCTRL_TABLE_SIZE
) + 1;
425 val
= read_config(fd
, &p
->pc_sel
, ptr
+ PCIR_MSIX_TABLE
, 4);
426 table_bar
= PCIR_BAR(val
& PCIM_MSIX_BIR_MASK
);
427 val
= read_config(fd
, &p
->pc_sel
, ptr
+ PCIR_MSIX_PBA
, 4);
428 pba_bar
= PCIR_BAR(val
& PCIM_MSIX_BIR_MASK
);
429 printf("MSI-X supports %d message%s ", msgnum
,
430 (msgnum
== 1) ? "" : "s");
431 if (table_bar
== pba_bar
)
432 printf("in map 0x%x", table_bar
);
434 printf("in maps 0x%x and 0x%x", table_bar
, pba_bar
);
435 if (ctrl
& PCIM_MSIXCTRL_MSIX_ENABLE
)
440 cap_sata(__unused
int fd
, __unused
struct pci_conf
*p
, __unused
uint8_t ptr
)
443 printf("SATA Index-Data Pair");
447 cap_pciaf(int fd
, struct pci_conf
*p
, uint8_t ptr
)
451 cap
= read_config(fd
, &p
->pc_sel
, ptr
+ PCIR_PCIAF_CAP
, 1);
452 printf("PCI Advanced Features:%s%s",
453 cap
& PCIM_PCIAFCAP_FLR
? " FLR" : "",
454 cap
& PCIM_PCIAFCAP_TP
? " TP" : "");
458 list_caps(int fd
, struct pci_conf
*p
)
464 /* Are capabilities present for this device? */
465 sta
= read_config(fd
, &p
->pc_sel
, PCIR_STATUS
, 2);
466 if (!(sta
& PCIM_STATUS_CAPPRESENT
))
469 switch (p
->pc_hdr
& PCIM_HDRTYPE
) {
470 case PCIM_HDRTYPE_NORMAL
:
471 case PCIM_HDRTYPE_BRIDGE
:
474 case PCIM_HDRTYPE_CARDBUS
:
475 ptr
= PCIR_CAP_PTR_2
;
478 errx(1, "list_caps: bad header type");
481 /* Walk the capability list. */
483 ptr
= read_config(fd
, &p
->pc_sel
, ptr
, 1);
484 while (ptr
!= 0 && ptr
!= 0xff) {
485 cap
= read_config(fd
, &p
->pc_sel
, ptr
+ PCICAP_ID
, 1);
486 printf(" cap %02x[%02x] = ", cap
, ptr
);
489 cap_power(fd
, p
, ptr
);
501 cap_pcix(fd
, p
, ptr
);
507 cap_vendor(fd
, p
, ptr
);
510 cap_debug(fd
, p
, ptr
);
513 cap_subvendor(fd
, p
, ptr
);
517 cap_express(fd
, p
, ptr
);
520 cap_msix(fd
, p
, ptr
);
523 cap_sata(fd
, p
, ptr
);
526 cap_pciaf(fd
, p
, ptr
);
533 ptr
= read_config(fd
, &p
->pc_sel
, ptr
+ PCICAP_NEXTPTR
, 1);
540 /* From <sys/systm.h>. */
541 static __inline
uint32_t
542 bitcount32(uint32_t x
)
545 x
= (x
& 0x55555555) + ((x
& 0xaaaaaaaa) >> 1);
546 x
= (x
& 0x33333333) + ((x
& 0xcccccccc) >> 2);
547 x
= (x
+ (x
>> 4)) & 0x0f0f0f0f;
549 x
= (x
+ (x
>> 16)) & 0x000000ff;
554 ecap_aer(int fd
, struct pci_conf
*p
, uint16_t ptr
, uint8_t ver
)
558 printf("AER %d", ver
);
561 sta
= read_config(fd
, &p
->pc_sel
, ptr
+ PCIR_AER_UC_STATUS
, 4);
562 mask
= read_config(fd
, &p
->pc_sel
, ptr
+ PCIR_AER_UC_SEVERITY
, 4);
563 printf(" %d fatal", bitcount32(sta
& mask
));
564 printf(" %d non-fatal", bitcount32(sta
& ~mask
));
565 sta
= read_config(fd
, &p
->pc_sel
, ptr
+ PCIR_AER_COR_STATUS
, 4);
566 printf(" %d corrected", bitcount32(sta
));
570 ecap_vc(int fd
, struct pci_conf
*p
, uint16_t ptr
, uint8_t ver
)
574 printf("VC %d", ver
);
577 cap1
= read_config(fd
, &p
->pc_sel
, ptr
+ PCIR_VC_CAP1
, 4);
578 printf(" max VC%d", cap1
& PCIM_VC_CAP1_EXT_COUNT
);
579 if ((cap1
& PCIM_VC_CAP1_LOWPRI_EXT_COUNT
) != 0)
580 printf(" lowpri VC0-VC%d",
581 (cap1
& PCIM_VC_CAP1_LOWPRI_EXT_COUNT
) >> 4);
585 ecap_sernum(int fd
, struct pci_conf
*p
, uint16_t ptr
, uint8_t ver
)
589 printf("Serial %d", ver
);
592 low
= read_config(fd
, &p
->pc_sel
, ptr
+ PCIR_SERIAL_LOW
, 4);
593 high
= read_config(fd
, &p
->pc_sel
, ptr
+ PCIR_SERIAL_HIGH
, 4);
594 printf(" %08x%08x", high
, low
);
598 list_ecaps(int fd
, struct pci_conf
*p
)
604 ecap
= read_config(fd
, &p
->pc_sel
, ptr
, 4);
605 if (ecap
== 0xffffffff || ecap
== 0)
608 printf("ecap %04x[%03x] = ", PCI_EXTCAP_ID(ecap
), ptr
);
609 switch (PCI_EXTCAP_ID(ecap
)) {
611 ecap_aer(fd
, p
, ptr
, PCI_EXTCAP_VER(ecap
));
614 ecap_vc(fd
, p
, ptr
, PCI_EXTCAP_VER(ecap
));
617 ecap_sernum(fd
, p
, ptr
, PCI_EXTCAP_VER(ecap
));
620 printf("unknown %d", PCI_EXTCAP_VER(ecap
));
624 ptr
= PCI_EXTCAP_NEXTPTR(ecap
);
627 ecap
= read_config(fd
, &p
->pc_sel
, ptr
, 4);