blob: fa37c8507ad41fd8138122bcb1d2a6c351bc4340 [file] [log] [blame]
Peter Stugedad1e302008-11-22 17:13:36 +00001/*
2 * This file is part of msrtool.
3 *
4 * Copyright (c) 2008 Peter Stuge <peter@stuge.se>
Stefan Reinauer37f39352009-09-01 10:03:01 +00005 * Copyright (c) 2009 coresystems GmbH
Peter Stugedad1e302008-11-22 17:13:36 +00006 *
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 Reinauer37f39352009-09-01 10:03:01 +000026#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 Stuge0924dee2008-11-25 02:03:16 +000031#include <pci/pci.h>
Peter Stugedad1e302008-11-22 17:13:36 +000032
33#define HEXCHARS "0123456789abcdefABCDEF"
34
35enum {
36 MSRTYPE_RDONLY,
37 MSRTYPE_RDWR,
38 MSRTYPE_WRONLY,
39 MSRTYPE_EOT
40} MsrTypes;
41
42enum {
43 PRESENT_RSVD,
44 PRESENT_DEC,
45 PRESENT_BIN,
46 PRESENT_OCT,
47 PRESENT_HEX,
48 PRESENT_HEXDEC
49} PresentTypes;
50
51struct msr {
52 uint32_t hi;
53 uint32_t lo;
54};
55
56struct msrbitvalues {
57 const struct msr value;
58 const char *text;
59};
60
61struct 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
70struct 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
96struct 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
107enum SysModes {
108 SYS_RDONLY = 0,
109 SYS_WRONLY,
110 SYS_RDWR
111};
112
113struct 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
126struct 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
135extern const struct sysdef *sys;
136
137extern uint8_t targets_found;
138extern const struct targetdef **targets;
139
140extern uint8_t reserved, verbose, quiet;
141
Peter Stuge0924dee2008-11-25 02:03:16 +0000142extern struct pci_access *pacc;
143
Peter Stugedad1e302008-11-22 17:13:36 +0000144#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 */
156struct cpuid_t *cpuid(void);
Peter Stuge0924dee2008-11-25 02:03:16 +0000157struct pci_dev *pci_dev_find(uint16_t vendor, uint16_t device);
Peter Stugedad1e302008-11-22 17:13:36 +0000158
159/* msrutils.c */
160void hexprint(FILE *f, const struct msr val, const uint8_t bits);
161int msr_eq(const struct msr a, const struct msr b);
162struct msr msr_shl(const struct msr a, const uint8_t bits);
163struct msr msr_shr(const struct msr a, const uint8_t bits);
164void msr_and(struct msr *a, const struct msr b);
165const struct msrdef *findmsrdef(const uint32_t addr);
Marc Jones8f210762009-03-08 04:37:39 +0000166uint32_t msraddrbyname(const char *name);
Peter Stugedad1e302008-11-22 17:13:36 +0000167void dumpmsrdefs(const struct targetdef *t);
168int dumpmsrdefsvals(FILE *f, const struct targetdef *t, const uint8_t cpu);
169uint8_t str2msr(char *str, struct msr *msr);
170void decodemsr(const uint8_t cpu, const uint32_t addr, const struct msr val);
171uint8_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 */
178extern int linux_probe(const struct sysdef *system);
179extern int linux_open(uint8_t cpu, enum SysModes mode);
180extern int linux_close(uint8_t cpu);
181extern int linux_rdmsr(uint8_t cpu, uint32_t addr, struct msr *val);
182
Stefan Reinauer37f39352009-09-01 10:03:01 +0000183/* darwin.c */
184extern int darwin_probe(const struct sysdef *system);
185extern int darwin_open(uint8_t cpu, enum SysModes mode);
186extern int darwin_close(uint8_t cpu);
187extern int darwin_rdmsr(uint8_t cpu, uint32_t addr, struct msr *val);
Peter Stugedad1e302008-11-22 17:13:36 +0000188
189/** target externs **/
190
191/* geodelx.c */
192extern int geodelx_probe(const struct targetdef *t);
193extern const struct msrdef geodelx_msrs[];
194
195/* cs5536.c */
196extern int cs5536_probe(const struct targetdef *t);
197extern const struct msrdef cs5536_msrs[];
198
Marc Jones8f210762009-03-08 04:37:39 +0000199/* k8.c */
200extern int k8_probe(const struct targetdef *t);
201extern const struct msrdef k8_msrs[];
202
Peter Stugedad1e302008-11-22 17:13:36 +0000203#endif /* MSRTOOL_H */