Patrick Georgi | ac95903 | 2020-05-05 22:49:26 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 2 | |
| 3 | #ifndef MSRTOOL_H |
| 4 | #define MSRTOOL_H |
| 5 | |
| 6 | #include <stdio.h> |
| 7 | #include <stdint.h> |
Stefan Reinauer | 37f3935 | 2009-09-01 10:03:01 +0000 | [diff] [blame] | 8 | #if (defined(__MACH__) && defined(__APPLE__)) |
Paul Menzel | a8843de | 2017-06-05 12:33:23 +0200 | [diff] [blame] | 9 | /* DirectHW is available here: https://www.coreboot.org/DirectHW */ |
Stefan Reinauer | 37f3935 | 2009-09-01 10:03:01 +0000 | [diff] [blame] | 10 | #define __DARWIN__ |
Stefan Reinauer | 9018b6e | 2011-03-14 09:08:27 +0000 | [diff] [blame] | 11 | #include <DirectHW/DirectHW.h> |
Stefan Reinauer | 37f3935 | 2009-09-01 10:03:01 +0000 | [diff] [blame] | 12 | #endif |
Andriy Gapon | d80e57c | 2009-11-28 05:21:42 +0000 | [diff] [blame] | 13 | #if defined(__FreeBSD__) |
| 14 | #include <sys/ioctl.h> |
| 15 | #include <sys/cpuctl.h> |
| 16 | #endif |
Peter Stuge | 0924dee | 2008-11-25 02:03:16 +0000 | [diff] [blame] | 17 | #include <pci/pci.h> |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 18 | |
| 19 | #define HEXCHARS "0123456789abcdefABCDEF" |
| 20 | |
| 21 | enum { |
| 22 | MSRTYPE_RDONLY, |
| 23 | MSRTYPE_RDWR, |
| 24 | MSRTYPE_WRONLY, |
| 25 | MSRTYPE_EOT |
| 26 | } MsrTypes; |
| 27 | |
| 28 | enum { |
| 29 | PRESENT_RSVD, |
| 30 | PRESENT_DEC, |
| 31 | PRESENT_BIN, |
| 32 | PRESENT_OCT, |
| 33 | PRESENT_HEX, |
Lubomir Rintel | 38686f1 | 2017-01-22 22:19:33 +0100 | [diff] [blame] | 34 | PRESENT_HEXDEC, |
| 35 | PRESENT_STR, |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 36 | } PresentTypes; |
| 37 | |
| 38 | struct msr { |
| 39 | uint32_t hi; |
| 40 | uint32_t lo; |
| 41 | }; |
| 42 | |
| 43 | struct msrbitvalues { |
| 44 | const struct msr value; |
| 45 | const char *text; |
| 46 | }; |
| 47 | |
| 48 | struct msrbits { |
| 49 | const uint8_t start; |
| 50 | const uint8_t size; |
| 51 | const char *name; |
| 52 | const char *desc; |
| 53 | const uint8_t present; |
| 54 | const struct msrbitvalues bitval[32]; |
| 55 | }; |
| 56 | |
| 57 | struct msrdef { |
| 58 | const uint32_t addr; |
| 59 | const uint8_t type; |
| 60 | const struct msr resetval; |
| 61 | const char *symbol; |
| 62 | const char *desc; |
| 63 | const struct msrbits bits[65]; |
| 64 | }; |
| 65 | |
| 66 | #define MSR1(lo) { 0, (lo) } |
| 67 | #define MSR2(hi,lo) { (hi), (lo) } |
| 68 | |
| 69 | #define BITVAL_EOT .text = NULL |
| 70 | #define BITVAL_ISEOT(bv) (NULL == (bv).text) |
| 71 | |
| 72 | #define BITS_EOT .size = 0 |
| 73 | #define BITS_ISEOT(b) (0 == (b).size) |
| 74 | |
| 75 | #define MSR_EOT .type = MSRTYPE_EOT |
| 76 | #define MSR_ISEOT(m) (MSRTYPE_EOT == (m).type) |
| 77 | |
| 78 | #define NOBITS {{ BITVAL_EOT }} |
| 79 | #define RESERVED "RSVD", "Reserved", PRESENT_HEXDEC, NOBITS |
| 80 | |
| 81 | #define MAX_CORES 8 |
| 82 | |
Anton Kochkov | 59b36f1 | 2012-07-21 07:29:48 +0400 | [diff] [blame] | 83 | typedef enum { |
Lubomir Rintel | fbf5759 | 2017-01-22 22:19:21 +0100 | [diff] [blame] | 84 | VENDOR_INTEL = 0x756e6547, |
| 85 | VENDOR_AMD = 0x68747541, |
Lubomir Rintel | 6cc4dea | 2017-01-22 22:20:04 +0100 | [diff] [blame] | 86 | VENDOR_CENTAUR = 0x746e6543, |
Anton Kochkov | 59b36f1 | 2012-07-21 07:29:48 +0400 | [diff] [blame] | 87 | } vendor_t; |
| 88 | |
| 89 | struct cpuid_t { |
| 90 | uint8_t family; |
| 91 | uint8_t model; |
| 92 | uint8_t stepping; |
| 93 | uint8_t ext_family; |
| 94 | uint8_t ext_model; |
| 95 | vendor_t vendor; |
| 96 | }; |
| 97 | |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 98 | struct targetdef { |
| 99 | const char *name; |
| 100 | const char *prettyname; |
Anton Kochkov | 59b36f1 | 2012-07-21 07:29:48 +0400 | [diff] [blame] | 101 | int (*probe)(const struct targetdef *target, const struct cpuid_t *id); |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 102 | const struct msrdef *msrs; |
| 103 | }; |
| 104 | |
| 105 | #define TARGET_EOT .name = NULL |
| 106 | #define TARGET_ISEOT(t) (NULL == (t).name) |
| 107 | |
| 108 | |
| 109 | enum SysModes { |
| 110 | SYS_RDONLY = 0, |
| 111 | SYS_WRONLY, |
| 112 | SYS_RDWR |
| 113 | }; |
| 114 | |
| 115 | struct sysdef { |
| 116 | const char *name; |
| 117 | const char *prettyname; |
| 118 | int (*probe)(const struct sysdef *system); |
| 119 | int (*open)(uint8_t cpu, enum SysModes mode); |
| 120 | int (*close)(uint8_t cpu); |
| 121 | int (*rdmsr)(uint8_t cpu, uint32_t addr, struct msr *val); |
| 122 | }; |
| 123 | |
| 124 | #define SYSTEM_EOT .name = NULL |
| 125 | #define SYSTEM_ISEOT(s) (NULL == (s).name) |
| 126 | |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 127 | extern const struct sysdef *sys; |
| 128 | |
| 129 | extern uint8_t targets_found; |
| 130 | extern const struct targetdef **targets; |
| 131 | |
| 132 | extern uint8_t reserved, verbose, quiet; |
| 133 | |
Peter Stuge | 0924dee | 2008-11-25 02:03:16 +0000 | [diff] [blame] | 134 | extern struct pci_access *pacc; |
| 135 | |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 136 | #define printf_quiet(x...) do { if (!quiet) fprintf(stderr,x); } while(0) |
| 137 | #define printf_verbose(x...) do { if (verbose && !quiet) fprintf(stderr,x); } while(0) |
| 138 | |
| 139 | #define SYSERROR(call, addr) do { \ |
| 140 | const struct msrdef *m = findmsrdef(addr); \ |
| 141 | if (m) \ |
| 142 | fprintf(stderr, "%s: " #call "(0x%08x) %s: %s\n", __func__, addr, m->symbol, strerror(errno)); \ |
| 143 | else \ |
| 144 | fprintf(stderr, "%s: " #call "(0x%08x): %s\n", __func__, addr, strerror(errno)); \ |
| 145 | } while (0); |
| 146 | |
| 147 | /* sys.c */ |
| 148 | struct cpuid_t *cpuid(void); |
Peter Stuge | 0924dee | 2008-11-25 02:03:16 +0000 | [diff] [blame] | 149 | struct pci_dev *pci_dev_find(uint16_t vendor, uint16_t device); |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 150 | |
| 151 | /* msrutils.c */ |
| 152 | void hexprint(FILE *f, const struct msr val, const uint8_t bits); |
Lubomir Rintel | 38686f1 | 2017-01-22 22:19:33 +0100 | [diff] [blame] | 153 | void strprint(FILE *f, const struct msr val, const uint8_t bits); |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 154 | int msr_eq(const struct msr a, const struct msr b); |
| 155 | struct msr msr_shl(const struct msr a, const uint8_t bits); |
| 156 | struct msr msr_shr(const struct msr a, const uint8_t bits); |
| 157 | void msr_and(struct msr *a, const struct msr b); |
| 158 | const struct msrdef *findmsrdef(const uint32_t addr); |
Marc Jones | 8f21076 | 2009-03-08 04:37:39 +0000 | [diff] [blame] | 159 | uint32_t msraddrbyname(const char *name); |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 160 | void dumpmsrdefs(const struct targetdef *t); |
| 161 | int dumpmsrdefsvals(FILE *f, const struct targetdef *t, const uint8_t cpu); |
Peter Stuge | 34f2907 | 2010-01-17 18:33:53 +0000 | [diff] [blame] | 162 | uint8_t str2msr(char *str, struct msr *msr, char **endptr); |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 163 | void decodemsr(const uint8_t cpu, const uint32_t addr, const struct msr val); |
| 164 | uint8_t diff_msr(FILE *fout, const uint32_t addr, const struct msr a, const struct msr b); |
| 165 | |
| 166 | |
| 167 | |
| 168 | /** system externs **/ |
| 169 | |
| 170 | /* linux.c */ |
| 171 | extern int linux_probe(const struct sysdef *system); |
| 172 | extern int linux_open(uint8_t cpu, enum SysModes mode); |
| 173 | extern int linux_close(uint8_t cpu); |
| 174 | extern int linux_rdmsr(uint8_t cpu, uint32_t addr, struct msr *val); |
| 175 | |
Stefan Reinauer | 37f3935 | 2009-09-01 10:03:01 +0000 | [diff] [blame] | 176 | /* darwin.c */ |
| 177 | extern int darwin_probe(const struct sysdef *system); |
| 178 | extern int darwin_open(uint8_t cpu, enum SysModes mode); |
| 179 | extern int darwin_close(uint8_t cpu); |
| 180 | extern int darwin_rdmsr(uint8_t cpu, uint32_t addr, struct msr *val); |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 181 | |
Andriy Gapon | d80e57c | 2009-11-28 05:21:42 +0000 | [diff] [blame] | 182 | /* freebsd.c */ |
| 183 | extern int freebsd_probe(const struct sysdef *system); |
| 184 | extern int freebsd_open(uint8_t cpu, enum SysModes mode); |
| 185 | extern int freebsd_close(uint8_t cpu); |
| 186 | extern int freebsd_rdmsr(uint8_t cpu, uint32_t addr, struct msr *val); |
| 187 | |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 188 | /** target externs **/ |
| 189 | |
Nils Jacobs | 58a901f | 2010-01-15 10:06:39 +0000 | [diff] [blame] | 190 | /* geodegx2.c */ |
Anton Kochkov | 59b36f1 | 2012-07-21 07:29:48 +0400 | [diff] [blame] | 191 | extern int geodegx2_probe(const struct targetdef *t, const struct cpuid_t *id); |
Nils Jacobs | 58a901f | 2010-01-15 10:06:39 +0000 | [diff] [blame] | 192 | extern const struct msrdef geodegx2_msrs[]; |
| 193 | |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 194 | /* geodelx.c */ |
Anton Kochkov | 59b36f1 | 2012-07-21 07:29:48 +0400 | [diff] [blame] | 195 | extern int geodelx_probe(const struct targetdef *t, const struct cpuid_t *id); |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 196 | extern const struct msrdef geodelx_msrs[]; |
| 197 | |
| 198 | /* cs5536.c */ |
Anton Kochkov | 59b36f1 | 2012-07-21 07:29:48 +0400 | [diff] [blame] | 199 | extern int cs5536_probe(const struct targetdef *t, const struct cpuid_t *id); |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 200 | extern const struct msrdef cs5536_msrs[]; |
| 201 | |
Marc Jones | 8f21076 | 2009-03-08 04:37:39 +0000 | [diff] [blame] | 202 | /* k8.c */ |
Anton Kochkov | 59b36f1 | 2012-07-21 07:29:48 +0400 | [diff] [blame] | 203 | extern int k8_probe(const struct targetdef *t, const struct cpuid_t *id); |
Marc Jones | 8f21076 | 2009-03-08 04:37:39 +0000 | [diff] [blame] | 204 | extern const struct msrdef k8_msrs[]; |
| 205 | |
Lubomir Rintel | 6cc4dea | 2017-01-22 22:20:04 +0100 | [diff] [blame] | 206 | /* via_c7.c */ |
| 207 | extern int via_c7_probe(const struct targetdef *t, const struct cpuid_t *id); |
| 208 | extern const struct msrdef via_c7_msrs[]; |
| 209 | |
Anton Kochkov | 7c634ae | 2011-06-20 23:14:22 +0400 | [diff] [blame] | 210 | /* intel_pentium3_early.c */ |
Anton Kochkov | 59b36f1 | 2012-07-21 07:29:48 +0400 | [diff] [blame] | 211 | extern int intel_pentium3_early_probe(const struct targetdef *t, const struct cpuid_t *id); |
Anton Kochkov | 7c634ae | 2011-06-20 23:14:22 +0400 | [diff] [blame] | 212 | extern const struct msrdef intel_pentium3_early_msrs[]; |
| 213 | |
| 214 | /* intel_pentium3.c */ |
Anton Kochkov | 59b36f1 | 2012-07-21 07:29:48 +0400 | [diff] [blame] | 215 | extern int intel_pentium3_probe(const struct targetdef *t, const struct cpuid_t *id); |
Anton Kochkov | 7c634ae | 2011-06-20 23:14:22 +0400 | [diff] [blame] | 216 | extern const struct msrdef intel_pentium3_msrs[]; |
| 217 | |
| 218 | /* intel_core1.c */ |
Anton Kochkov | 59b36f1 | 2012-07-21 07:29:48 +0400 | [diff] [blame] | 219 | extern int intel_core1_probe(const struct targetdef *t, const struct cpuid_t *id); |
Anton Kochkov | 7c634ae | 2011-06-20 23:14:22 +0400 | [diff] [blame] | 220 | extern const struct msrdef intel_core1_msrs[]; |
| 221 | |
| 222 | /* intel_core2_early.c */ |
Anton Kochkov | 59b36f1 | 2012-07-21 07:29:48 +0400 | [diff] [blame] | 223 | extern int intel_core2_early_probe(const struct targetdef *t, const struct cpuid_t *id); |
Anton Kochkov | 7c634ae | 2011-06-20 23:14:22 +0400 | [diff] [blame] | 224 | extern const struct msrdef intel_core2_early_msrs[]; |
| 225 | |
| 226 | /* intel_core2_later.c */ |
Anton Kochkov | 59b36f1 | 2012-07-21 07:29:48 +0400 | [diff] [blame] | 227 | extern int intel_core2_later_probe(const struct targetdef *t, const struct cpuid_t *id); |
Anton Kochkov | 7c634ae | 2011-06-20 23:14:22 +0400 | [diff] [blame] | 228 | extern const struct msrdef intel_core2_later_msrs[]; |
| 229 | |
| 230 | /* intel_pentium4_early.c */ |
Anton Kochkov | 59b36f1 | 2012-07-21 07:29:48 +0400 | [diff] [blame] | 231 | extern int intel_pentium4_early_probe(const struct targetdef *t, const struct cpuid_t *id); |
Anton Kochkov | 7c634ae | 2011-06-20 23:14:22 +0400 | [diff] [blame] | 232 | extern const struct msrdef intel_pentium4_early_msrs[]; |
| 233 | |
| 234 | /* intel_pentium4_later.c */ |
Anton Kochkov | 59b36f1 | 2012-07-21 07:29:48 +0400 | [diff] [blame] | 235 | extern int intel_pentium4_later_probe(const struct targetdef *t, const struct cpuid_t *id); |
Anton Kochkov | 7c634ae | 2011-06-20 23:14:22 +0400 | [diff] [blame] | 236 | extern const struct msrdef intel_pentium4_later_msrs[]; |
| 237 | |
Elyes HAOUAS | 6e6b36a | 2018-05-15 13:28:54 +0200 | [diff] [blame] | 238 | /* intel_pentium_d.c */ |
| 239 | extern int intel_pentium_d_probe(const struct targetdef *t, const struct cpuid_t *id); |
| 240 | extern const struct msrdef intel_pentium_d_msrs[]; |
| 241 | |
Anton Kochkov | 54c07a6 | 2012-07-04 07:35:45 +0400 | [diff] [blame] | 242 | /* intel_nehalem.c */ |
Anton Kochkov | 59b36f1 | 2012-07-21 07:29:48 +0400 | [diff] [blame] | 243 | extern int intel_nehalem_probe(const struct targetdef *t, const struct cpuid_t *id); |
Anton Kochkov | 54c07a6 | 2012-07-04 07:35:45 +0400 | [diff] [blame] | 244 | extern const struct msrdef intel_nehalem_msrs[]; |
| 245 | |
Olivier Langlois | ccc7d1f | 2013-06-03 01:30:25 -0400 | [diff] [blame] | 246 | /* intel_atom.c */ |
| 247 | extern int intel_atom_probe(const struct targetdef *t, const struct cpuid_t *id); |
| 248 | extern const struct msrdef intel_atom_msrs[]; |
| 249 | |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 250 | #endif /* MSRTOOL_H */ |