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> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #ifndef MSRTOOL_H |
| 21 | #define MSRTOOL_H |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <stdint.h> |
| 25 | |
| 26 | #define HEXCHARS "0123456789abcdefABCDEF" |
| 27 | |
| 28 | enum { |
| 29 | MSRTYPE_RDONLY, |
| 30 | MSRTYPE_RDWR, |
| 31 | MSRTYPE_WRONLY, |
| 32 | MSRTYPE_EOT |
| 33 | } MsrTypes; |
| 34 | |
| 35 | enum { |
| 36 | PRESENT_RSVD, |
| 37 | PRESENT_DEC, |
| 38 | PRESENT_BIN, |
| 39 | PRESENT_OCT, |
| 40 | PRESENT_HEX, |
| 41 | PRESENT_HEXDEC |
| 42 | } PresentTypes; |
| 43 | |
| 44 | struct msr { |
| 45 | uint32_t hi; |
| 46 | uint32_t lo; |
| 47 | }; |
| 48 | |
| 49 | struct msrbitvalues { |
| 50 | const struct msr value; |
| 51 | const char *text; |
| 52 | }; |
| 53 | |
| 54 | struct msrbits { |
| 55 | const uint8_t start; |
| 56 | const uint8_t size; |
| 57 | const char *name; |
| 58 | const char *desc; |
| 59 | const uint8_t present; |
| 60 | const struct msrbitvalues bitval[32]; |
| 61 | }; |
| 62 | |
| 63 | struct msrdef { |
| 64 | const uint32_t addr; |
| 65 | const uint8_t type; |
| 66 | const struct msr resetval; |
| 67 | const char *symbol; |
| 68 | const char *desc; |
| 69 | const struct msrbits bits[65]; |
| 70 | }; |
| 71 | |
| 72 | #define MSR1(lo) { 0, (lo) } |
| 73 | #define MSR2(hi,lo) { (hi), (lo) } |
| 74 | |
| 75 | #define BITVAL_EOT .text = NULL |
| 76 | #define BITVAL_ISEOT(bv) (NULL == (bv).text) |
| 77 | |
| 78 | #define BITS_EOT .size = 0 |
| 79 | #define BITS_ISEOT(b) (0 == (b).size) |
| 80 | |
| 81 | #define MSR_EOT .type = MSRTYPE_EOT |
| 82 | #define MSR_ISEOT(m) (MSRTYPE_EOT == (m).type) |
| 83 | |
| 84 | #define NOBITS {{ BITVAL_EOT }} |
| 85 | #define RESERVED "RSVD", "Reserved", PRESENT_HEXDEC, NOBITS |
| 86 | |
| 87 | #define MAX_CORES 8 |
| 88 | |
| 89 | struct targetdef { |
| 90 | const char *name; |
| 91 | const char *prettyname; |
| 92 | int (*probe)(const struct targetdef *target); |
| 93 | const struct msrdef *msrs; |
| 94 | }; |
| 95 | |
| 96 | #define TARGET_EOT .name = NULL |
| 97 | #define TARGET_ISEOT(t) (NULL == (t).name) |
| 98 | |
| 99 | |
| 100 | enum SysModes { |
| 101 | SYS_RDONLY = 0, |
| 102 | SYS_WRONLY, |
| 103 | SYS_RDWR |
| 104 | }; |
| 105 | |
| 106 | struct sysdef { |
| 107 | const char *name; |
| 108 | const char *prettyname; |
| 109 | int (*probe)(const struct sysdef *system); |
| 110 | int (*open)(uint8_t cpu, enum SysModes mode); |
| 111 | int (*close)(uint8_t cpu); |
| 112 | int (*rdmsr)(uint8_t cpu, uint32_t addr, struct msr *val); |
| 113 | }; |
| 114 | |
| 115 | #define SYSTEM_EOT .name = NULL |
| 116 | #define SYSTEM_ISEOT(s) (NULL == (s).name) |
| 117 | |
| 118 | |
| 119 | struct cpuid_t { |
| 120 | uint8_t family; |
| 121 | uint8_t model; |
| 122 | uint8_t stepping; |
| 123 | uint8_t ext_family; |
| 124 | uint8_t ext_model; |
| 125 | }; |
| 126 | |
| 127 | |
| 128 | extern const struct sysdef *sys; |
| 129 | |
| 130 | extern uint8_t targets_found; |
| 131 | extern const struct targetdef **targets; |
| 132 | |
| 133 | extern uint8_t reserved, verbose, quiet; |
| 134 | |
| 135 | #define printf_quiet(x...) do { if (!quiet) fprintf(stderr,x); } while(0) |
| 136 | #define printf_verbose(x...) do { if (verbose && !quiet) fprintf(stderr,x); } while(0) |
| 137 | |
| 138 | #define SYSERROR(call, addr) do { \ |
| 139 | const struct msrdef *m = findmsrdef(addr); \ |
| 140 | if (m) \ |
| 141 | fprintf(stderr, "%s: " #call "(0x%08x) %s: %s\n", __func__, addr, m->symbol, strerror(errno)); \ |
| 142 | else \ |
| 143 | fprintf(stderr, "%s: " #call "(0x%08x): %s\n", __func__, addr, strerror(errno)); \ |
| 144 | } while (0); |
| 145 | |
| 146 | /* sys.c */ |
| 147 | struct cpuid_t *cpuid(void); |
| 148 | |
| 149 | /* msrutils.c */ |
| 150 | void hexprint(FILE *f, const struct msr val, const uint8_t bits); |
| 151 | int msr_eq(const struct msr a, const struct msr b); |
| 152 | struct msr msr_shl(const struct msr a, const uint8_t bits); |
| 153 | struct msr msr_shr(const struct msr a, const uint8_t bits); |
| 154 | void msr_and(struct msr *a, const struct msr b); |
| 155 | const struct msrdef *findmsrdef(const uint32_t addr); |
| 156 | void dumpmsrdefs(const struct targetdef *t); |
| 157 | int dumpmsrdefsvals(FILE *f, const struct targetdef *t, const uint8_t cpu); |
| 158 | uint8_t str2msr(char *str, struct msr *msr); |
| 159 | void decodemsr(const uint8_t cpu, const uint32_t addr, const struct msr val); |
| 160 | uint8_t diff_msr(FILE *fout, const uint32_t addr, const struct msr a, const struct msr b); |
| 161 | |
| 162 | |
| 163 | |
| 164 | /** system externs **/ |
| 165 | |
| 166 | /* linux.c */ |
| 167 | extern int linux_probe(const struct sysdef *system); |
| 168 | extern int linux_open(uint8_t cpu, enum SysModes mode); |
| 169 | extern int linux_close(uint8_t cpu); |
| 170 | extern int linux_rdmsr(uint8_t cpu, uint32_t addr, struct msr *val); |
| 171 | |
| 172 | |
| 173 | /** target externs **/ |
| 174 | |
| 175 | /* geodelx.c */ |
| 176 | extern int geodelx_probe(const struct targetdef *t); |
| 177 | extern const struct msrdef geodelx_msrs[]; |
| 178 | |
| 179 | /* cs5536.c */ |
| 180 | extern int cs5536_probe(const struct targetdef *t); |
| 181 | extern const struct msrdef cs5536_msrs[]; |
| 182 | |
| 183 | #endif /* MSRTOOL_H */ |