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