blob: 590ae2c18782393f2be1edff31ae9311beb2ae3a [file] [log] [blame]
Patrick Georgiea063cb2020-05-08 19:28:13 +02001/* inteltool - dump all registers on an Intel CPU + chipset based system */
Patrick Georgi7333a112020-05-08 20:48:04 +02002/* SPDX-License-Identifier: GPL-2.0-only */
Nico Huber99b02a12017-04-05 17:39:57 +02003
4#include <stdio.h>
5#include <stdlib.h>
6#include <stdint.h>
7#include <stdbool.h>
8#include <inttypes.h>
9#include <assert.h>
10#include "pcr.h"
11
12const uint8_t *sbbar = NULL;
13
14uint32_t read_pcr32(const uint8_t port, const uint16_t offset)
15{
16 assert(sbbar);
17 return *(const uint32_t *)(sbbar + (port << 16) + offset);
18}
19
Youness Alaouid8214d7e2018-03-13 16:58:52 -040020static void print_pcr_port(const uint8_t port)
21{
22 size_t i = 0;
23 uint32_t last_reg = 0;
24 bool last_printed = true;
25
26 printf("PCR port offset: 0x%06zx\n\n", (size_t)port << 16);
27
28 for (i = 0; i < PCR_PORT_SIZE; i += 4) {
29 const uint32_t reg = read_pcr32(port, i);
30 const bool rep = i && last_reg == reg;
31 if (!rep) {
32 if (!last_printed)
33 printf("*\n");
34 printf("0x%04zx: 0x%08"PRIx32"\n", i, reg);
35 }
36
37 last_reg = reg;
38 last_printed = !rep;
39 }
40 if (!last_printed)
41 printf("*\n");
42}
43
44void print_pcr_ports(struct pci_dev *const sb,
45 const uint8_t *const ports, const size_t count)
46{
47 size_t i;
48
49 pcr_init(sb);
50
51 for (i = 0; i < count; ++i) {
52 printf("\n========== PCR 0x%02x ==========\n\n", ports[i]);
53 print_pcr_port(ports[i]);
54 }
55}
56
Nico Huber99b02a12017-04-05 17:39:57 +020057void pcr_init(struct pci_dev *const sb)
58{
59 bool error_exit = false;
60 bool p2sb_revealed = false;
Nico Huber94473af2018-11-20 12:10:29 +010061 struct pci_dev *p2sb;
Thomas Heijligen725369f2019-02-19 10:51:34 +000062 bool use_p2sb = true;
63 pciaddr_t sbbar_phys;
Nico Huber99b02a12017-04-05 17:39:57 +020064
65 if (sbbar)
66 return;
67
Nico Huber94473af2018-11-20 12:10:29 +010068 switch (sb->device_id) {
69 case PCI_DEVICE_ID_INTEL_SUNRISEPOINT_PRE:
Felix Singer0a7543d2019-02-19 23:49:11 +010070 case PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_PRE:
71 case PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_U_BASE_SKL:
72 case PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_Y_PREM_SKL:
73 case PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_U_PREM_SKL:
74 case PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_U_BASE_KBL:
75 case PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_U_PREM_KBL:
76 case PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_Y_PREM_KBL:
Shaleen Jain2822d662019-01-02 11:15:16 +053077 case PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_U_IHDCP_BASE:
78 case PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_U_IHDCP_PREM:
79 case PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_Y_IHDCP_PREM:
Nico Huber94473af2018-11-20 12:10:29 +010080 case PCI_DEVICE_ID_INTEL_H110:
81 case PCI_DEVICE_ID_INTEL_H170:
82 case PCI_DEVICE_ID_INTEL_Z170:
83 case PCI_DEVICE_ID_INTEL_Q170:
84 case PCI_DEVICE_ID_INTEL_Q150:
85 case PCI_DEVICE_ID_INTEL_B150:
86 case PCI_DEVICE_ID_INTEL_C236:
87 case PCI_DEVICE_ID_INTEL_C232:
88 case PCI_DEVICE_ID_INTEL_QM170:
89 case PCI_DEVICE_ID_INTEL_HM170:
90 case PCI_DEVICE_ID_INTEL_CM236:
91 case PCI_DEVICE_ID_INTEL_HM175:
92 case PCI_DEVICE_ID_INTEL_QM175:
93 case PCI_DEVICE_ID_INTEL_CM238:
Timofey Komarov6c800822021-06-25 12:07:32 +030094 case PCI_DEVICE_ID_INTEL_H270:
95 case PCI_DEVICE_ID_INTEL_Z270:
96 case PCI_DEVICE_ID_INTEL_Q270:
97 case PCI_DEVICE_ID_INTEL_Q250:
98 case PCI_DEVICE_ID_INTEL_B250:
99 case PCI_DEVICE_ID_INTEL_Z370:
100 case PCI_DEVICE_ID_INTEL_H310C:
101 case PCI_DEVICE_ID_INTEL_X299:
Maxim Polyakovb89ce2e2019-08-17 14:54:02 +0300102 case PCI_DEVICE_ID_INTEL_C621:
Jingle Hsu4067fa32020-11-03 20:46:41 +0800103 case PCI_DEVICE_ID_INTEL_C621A:
Maxim Polyakovb89ce2e2019-08-17 14:54:02 +0300104 case PCI_DEVICE_ID_INTEL_C622:
105 case PCI_DEVICE_ID_INTEL_C624:
106 case PCI_DEVICE_ID_INTEL_C625:
107 case PCI_DEVICE_ID_INTEL_C626:
108 case PCI_DEVICE_ID_INTEL_C627:
109 case PCI_DEVICE_ID_INTEL_C628:
110 case PCI_DEVICE_ID_INTEL_C629:
111 case PCI_DEVICE_ID_INTEL_C624_SUPER:
112 case PCI_DEVICE_ID_INTEL_C627_SUPER_1:
113 case PCI_DEVICE_ID_INTEL_C621_SUPER:
114 case PCI_DEVICE_ID_INTEL_C627_SUPER_2:
115 case PCI_DEVICE_ID_INTEL_C628_SUPER:
Thomas Heijligenda027192019-01-12 19:20:50 +0100116 case PCI_DEVICE_ID_INTEL_DNV_LPC:
Nico Huber94473af2018-11-20 12:10:29 +0100117 p2sb = pci_get_dev(sb->access, 0, 0, 0x1f, 1);
118 break;
119 case PCI_DEVICE_ID_INTEL_APL_LPC:
120 p2sb = pci_get_dev(sb->access, 0, 0, 0x0d, 0);
121 break;
Thomas Heijligen725369f2019-02-19 10:51:34 +0000122 case PCI_DEVICE_ID_INTEL_H310:
123 case PCI_DEVICE_ID_INTEL_H370:
124 case PCI_DEVICE_ID_INTEL_Z390:
125 case PCI_DEVICE_ID_INTEL_Q370:
126 case PCI_DEVICE_ID_INTEL_B360:
127 case PCI_DEVICE_ID_INTEL_C246:
128 case PCI_DEVICE_ID_INTEL_C242:
129 case PCI_DEVICE_ID_INTEL_QM370:
130 case PCI_DEVICE_ID_INTEL_HM370:
131 case PCI_DEVICE_ID_INTEL_CM246:
Michał Żygowski8ac40f32021-07-09 16:00:16 +0200132 case PCI_DEVICE_ID_INTEL_Q570:
133 case PCI_DEVICE_ID_INTEL_Z590:
134 case PCI_DEVICE_ID_INTEL_H570:
135 case PCI_DEVICE_ID_INTEL_B560:
136 case PCI_DEVICE_ID_INTEL_H510:
137 case PCI_DEVICE_ID_INTEL_WM590:
138 case PCI_DEVICE_ID_INTEL_QM580:
139 case PCI_DEVICE_ID_INTEL_HM570:
140 case PCI_DEVICE_ID_INTEL_C252:
141 case PCI_DEVICE_ID_INTEL_C256:
142 case PCI_DEVICE_ID_INTEL_W580:
Matt DeVillier3c784452019-06-11 23:23:46 -0500143 case PCI_DEVICE_ID_INTEL_CANNONPOINT_LP_U_PREM:
Matt DeVillier62e883d2020-08-08 11:17:31 -0500144 case PCI_DEVICE_ID_INTEL_COMETPOINT_LP_U_PREM:
145 case PCI_DEVICE_ID_INTEL_COMETPOINT_LP_U_BASE:
Johanna Schander0174ea72020-01-04 15:14:59 +0100146 case PCI_DEVICE_ID_INTEL_ICELAKE_LP_U:
Michał Żygowski8ac40f32021-07-09 16:00:16 +0200147 case PCI_DEVICE_ID_INTEL_TIGERPOINT_U_SUPER:
148 case PCI_DEVICE_ID_INTEL_TIGERPOINT_U_PREM:
149 case PCI_DEVICE_ID_INTEL_TIGERPOINT_U_BASE:
150 case PCI_DEVICE_ID_INTEL_TIGERPOINT_Y_SUPER:
151 case PCI_DEVICE_ID_INTEL_TIGERPOINT_Y_PREM:
Thomas Heijligen725369f2019-02-19 10:51:34 +0000152 sbbar_phys = 0xfd000000;
153 use_p2sb = false;
154 break;
Nico Huber94473af2018-11-20 12:10:29 +0100155 default:
156 perror("Unknown LPC device.");
157 exit(1);
158 }
Nico Huber99b02a12017-04-05 17:39:57 +0200159
Thomas Heijligen725369f2019-02-19 10:51:34 +0000160 if (use_p2sb) {
161 if (!p2sb) {
162 perror("Can't allocate device node for P2SB.");
Nico Huber99b02a12017-04-05 17:39:57 +0200163 exit(1);
164 }
Nico Huber99b02a12017-04-05 17:39:57 +0200165
Thomas Heijligen725369f2019-02-19 10:51:34 +0000166 /* do not fill bases here, libpci refuses to refill later */
167 pci_fill_info(p2sb, PCI_FILL_IDENT);
168 if (p2sb->vendor_id == 0xffff && p2sb->device_id == 0xffff) {
169 printf("Trying to reveal Primary to Sideband Bridge "
170 "(P2SB),\nlet's hope the OS doesn't mind... ");
171 /* Do not use pci_write_long(). Bytes
172 surrounding 0xe0 must be maintained. */
173 pci_write_byte(p2sb, 0xe0 + 1, 0);
174
175 pci_fill_info(p2sb, PCI_FILL_IDENT | PCI_FILL_RESCAN);
176 if (p2sb->vendor_id != 0xffff ||
177 p2sb->device_id != 0xffff) {
178 printf("done.\n");
179 p2sb_revealed = true;
180 } else {
181 printf("failed.\n");
182 exit(1);
183 }
184 }
185 pci_fill_info(p2sb, PCI_FILL_BASES | PCI_FILL_CLASS);
186
187 sbbar_phys = p2sb->base_addr[0] & ~0xfULL;
188 }
189
Nico Huber99b02a12017-04-05 17:39:57 +0200190 printf("SBREG_BAR = 0x%08"PRIx64" (MEM)\n\n", (uint64_t)sbbar_phys);
191 sbbar = map_physical(sbbar_phys, SBBAR_SIZE);
192 if (sbbar == NULL) {
193 perror("Error mapping SBREG_BAR");
194 error_exit = true;
195 }
196
Thomas Heijligen725369f2019-02-19 10:51:34 +0000197 if (use_p2sb) {
198 if (p2sb_revealed) {
199 printf("Hiding Primary to Sideband Bridge (P2SB).\n");
200 pci_write_byte(p2sb, 0xe0 + 1, 1);
201 }
202 pci_free_dev(p2sb);
Nico Huber99b02a12017-04-05 17:39:57 +0200203 }
Nico Huber99b02a12017-04-05 17:39:57 +0200204
205 if (error_exit)
206 exit(1);
207}
208
209void pcr_cleanup(void)
210{
211 if (sbbar)
212 unmap_physical((void *)sbbar, SBBAR_SIZE);
213}