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