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 |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 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__)) |
| 27 | /* DirectIO is available here: http://www.coresystems.de/en/directio */ |
| 28 | #define __DARWIN__ |
| 29 | #include <DirectIO/darwinio.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, |
| 48 | PRESENT_HEXDEC |
| 49 | } PresentTypes; |
| 50 | |
| 51 | struct msr { |
| 52 | uint32_t hi; |
| 53 | uint32_t lo; |
| 54 | }; |
| 55 | |
| 56 | struct msrbitvalues { |
| 57 | const struct msr value; |
| 58 | const char *text; |
| 59 | }; |
| 60 | |
| 61 | struct msrbits { |
| 62 | const uint8_t start; |
| 63 | const uint8_t size; |
| 64 | const char *name; |
| 65 | const char *desc; |
| 66 | const uint8_t present; |
| 67 | const struct msrbitvalues bitval[32]; |
| 68 | }; |
| 69 | |
| 70 | struct msrdef { |
| 71 | const uint32_t addr; |
| 72 | const uint8_t type; |
| 73 | const struct msr resetval; |
| 74 | const char *symbol; |
| 75 | const char *desc; |
| 76 | const struct msrbits bits[65]; |
| 77 | }; |
| 78 | |
| 79 | #define MSR1(lo) { 0, (lo) } |
| 80 | #define MSR2(hi,lo) { (hi), (lo) } |
| 81 | |
| 82 | #define BITVAL_EOT .text = NULL |
| 83 | #define BITVAL_ISEOT(bv) (NULL == (bv).text) |
| 84 | |
| 85 | #define BITS_EOT .size = 0 |
| 86 | #define BITS_ISEOT(b) (0 == (b).size) |
| 87 | |
| 88 | #define MSR_EOT .type = MSRTYPE_EOT |
| 89 | #define MSR_ISEOT(m) (MSRTYPE_EOT == (m).type) |
| 90 | |
| 91 | #define NOBITS {{ BITVAL_EOT }} |
| 92 | #define RESERVED "RSVD", "Reserved", PRESENT_HEXDEC, NOBITS |
| 93 | |
| 94 | #define MAX_CORES 8 |
| 95 | |
| 96 | struct targetdef { |
| 97 | const char *name; |
| 98 | const char *prettyname; |
| 99 | int (*probe)(const struct targetdef *target); |
| 100 | const struct msrdef *msrs; |
| 101 | }; |
| 102 | |
| 103 | #define TARGET_EOT .name = NULL |
| 104 | #define TARGET_ISEOT(t) (NULL == (t).name) |
| 105 | |
| 106 | |
| 107 | enum SysModes { |
| 108 | SYS_RDONLY = 0, |
| 109 | SYS_WRONLY, |
| 110 | SYS_RDWR |
| 111 | }; |
| 112 | |
| 113 | struct sysdef { |
| 114 | const char *name; |
| 115 | const char *prettyname; |
| 116 | int (*probe)(const struct sysdef *system); |
| 117 | int (*open)(uint8_t cpu, enum SysModes mode); |
| 118 | int (*close)(uint8_t cpu); |
| 119 | int (*rdmsr)(uint8_t cpu, uint32_t addr, struct msr *val); |
| 120 | }; |
| 121 | |
| 122 | #define SYSTEM_EOT .name = NULL |
| 123 | #define SYSTEM_ISEOT(s) (NULL == (s).name) |
| 124 | |
| 125 | |
| 126 | struct cpuid_t { |
| 127 | uint8_t family; |
| 128 | uint8_t model; |
| 129 | uint8_t stepping; |
| 130 | uint8_t ext_family; |
| 131 | uint8_t ext_model; |
| 132 | }; |
| 133 | |
| 134 | |
| 135 | extern const struct sysdef *sys; |
| 136 | |
| 137 | extern uint8_t targets_found; |
| 138 | extern const struct targetdef **targets; |
| 139 | |
| 140 | extern uint8_t reserved, verbose, quiet; |
| 141 | |
Peter Stuge | 0924dee | 2008-11-25 02:03:16 +0000 | [diff] [blame] | 142 | extern struct pci_access *pacc; |
| 143 | |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 144 | #define printf_quiet(x...) do { if (!quiet) fprintf(stderr,x); } while(0) |
| 145 | #define printf_verbose(x...) do { if (verbose && !quiet) fprintf(stderr,x); } while(0) |
| 146 | |
| 147 | #define SYSERROR(call, addr) do { \ |
| 148 | const struct msrdef *m = findmsrdef(addr); \ |
| 149 | if (m) \ |
| 150 | fprintf(stderr, "%s: " #call "(0x%08x) %s: %s\n", __func__, addr, m->symbol, strerror(errno)); \ |
| 151 | else \ |
| 152 | fprintf(stderr, "%s: " #call "(0x%08x): %s\n", __func__, addr, strerror(errno)); \ |
| 153 | } while (0); |
| 154 | |
| 155 | /* sys.c */ |
| 156 | struct cpuid_t *cpuid(void); |
Peter Stuge | 0924dee | 2008-11-25 02:03:16 +0000 | [diff] [blame] | 157 | struct pci_dev *pci_dev_find(uint16_t vendor, uint16_t device); |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 158 | |
| 159 | /* msrutils.c */ |
| 160 | void hexprint(FILE *f, const struct msr val, const uint8_t bits); |
| 161 | int msr_eq(const struct msr a, const struct msr b); |
| 162 | struct msr msr_shl(const struct msr a, const uint8_t bits); |
| 163 | struct msr msr_shr(const struct msr a, const uint8_t bits); |
| 164 | void msr_and(struct msr *a, const struct msr b); |
| 165 | const struct msrdef *findmsrdef(const uint32_t addr); |
Marc Jones | 8f21076 | 2009-03-08 04:37:39 +0000 | [diff] [blame] | 166 | uint32_t msraddrbyname(const char *name); |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 167 | void dumpmsrdefs(const struct targetdef *t); |
| 168 | int dumpmsrdefsvals(FILE *f, const struct targetdef *t, const uint8_t cpu); |
| 169 | uint8_t str2msr(char *str, struct msr *msr); |
| 170 | void decodemsr(const uint8_t cpu, const uint32_t addr, const struct msr val); |
| 171 | uint8_t diff_msr(FILE *fout, const uint32_t addr, const struct msr a, const struct msr b); |
| 172 | |
| 173 | |
| 174 | |
| 175 | /** system externs **/ |
| 176 | |
| 177 | /* linux.c */ |
| 178 | extern int linux_probe(const struct sysdef *system); |
| 179 | extern int linux_open(uint8_t cpu, enum SysModes mode); |
| 180 | extern int linux_close(uint8_t cpu); |
| 181 | extern int linux_rdmsr(uint8_t cpu, uint32_t addr, struct msr *val); |
| 182 | |
Stefan Reinauer | 37f3935 | 2009-09-01 10:03:01 +0000 | [diff] [blame] | 183 | /* darwin.c */ |
| 184 | extern int darwin_probe(const struct sysdef *system); |
| 185 | extern int darwin_open(uint8_t cpu, enum SysModes mode); |
| 186 | extern int darwin_close(uint8_t cpu); |
| 187 | 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] | 188 | |
| 189 | /** target externs **/ |
| 190 | |
| 191 | /* geodelx.c */ |
| 192 | extern int geodelx_probe(const struct targetdef *t); |
| 193 | extern const struct msrdef geodelx_msrs[]; |
| 194 | |
| 195 | /* cs5536.c */ |
| 196 | extern int cs5536_probe(const struct targetdef *t); |
| 197 | extern const struct msrdef cs5536_msrs[]; |
| 198 | |
Marc Jones | 8f21076 | 2009-03-08 04:37:39 +0000 | [diff] [blame] | 199 | /* k8.c */ |
| 200 | extern int k8_probe(const struct targetdef *t); |
| 201 | extern const struct msrdef k8_msrs[]; |
| 202 | |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 203 | #endif /* MSRTOOL_H */ |