geode_msr.c revision 00be8644
1ea7e5fe0Smrg/*
2ea7e5fe0Smrg * Copyright (c) 2008 Advanced Micro Devices, Inc.
3ea7e5fe0Smrg *
4ea7e5fe0Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5ea7e5fe0Smrg * copy of this software and associated documentation files (the "Software"),
6ea7e5fe0Smrg * to deal in the Software without restriction, including without limitation
7ea7e5fe0Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8ea7e5fe0Smrg * and/or sell copies of the Software, and to permit persons to whom the
9ea7e5fe0Smrg * Software is furnished to do so, subject to the following conditions:
10ea7e5fe0Smrg *
11ea7e5fe0Smrg * The above copyright notice and this permission notice shall be included in
12ea7e5fe0Smrg * all copies or substantial portions of the Software.
13ea7e5fe0Smrg *
14ea7e5fe0Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15ea7e5fe0Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16ea7e5fe0Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17ea7e5fe0Smrg * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18ea7e5fe0Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19ea7e5fe0Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20ea7e5fe0Smrg * DEALINGS IN THE SOFTWARE.
21ea7e5fe0Smrg *
22ea7e5fe0Smrg * Neither the name of the Advanced Micro Devices, Inc. nor the names of its
23ea7e5fe0Smrg * contributors may be used to endorse or promote products derived from this
24ea7e5fe0Smrg * software without specific prior written permission.
25ea7e5fe0Smrg */
26ea7e5fe0Smrg
27ea7e5fe0Smrg#ifdef HAVE_CONFIG_H
28ea7e5fe0Smrg#include "config.h"
29ea7e5fe0Smrg#endif
30ea7e5fe0Smrg
31f29dbc25Smrg#include <stdio.h>
32f29dbc25Smrg#include <unistd.h>
33f29dbc25Smrg#include <fcntl.h>
34f29dbc25Smrg#include <sys/types.h>
35f29dbc25Smrg#include <errno.h>
36621ff18cSmrg
37621ff18cSmrg#ifdef __OpenBSD__
38621ff18cSmrg#include <sys/ioctl.h>
39621ff18cSmrg#include <machine/amdmsr.h>
40621ff18cSmrg#endif
41621ff18cSmrg
4244802259Smrg#ifdef __FreeBSD__
4344802259Smrg#include <sys/ioctl.h>
4444802259Smrg#include <sys/cpuctl.h>
4544802259Smrg#endif
4644802259Smrg
4700be8644Schristos#include "xorg-server.h"
4800be8644Schristos
49f29dbc25Smrg#include "os.h"
50f29dbc25Smrg#include "geode.h"
51f29dbc25Smrg
52621ff18cSmrg#ifdef __OpenBSD__
53621ff18cSmrg#define _PATH_MSRDEV	"/dev/amdmsr"
54621ff18cSmrg#define X_PRIVSEP
5544802259Smrg#elif defined __FreeBSD__
5644802259Smrg#define _PATH_MSRDEV	"/dev/cpuctl0"
57621ff18cSmrg#else
58621ff18cSmrg#define _PATH_MSRDEV	"/dev/cpu/0/msr"
59621ff18cSmrg#endif
60621ff18cSmrg
61f29dbc25Smrgstatic int
62f29dbc25Smrg_msr_open(void)
63f29dbc25Smrg{
64f29dbc25Smrg    static int msrfd = 0;
65f29dbc25Smrg
66f29dbc25Smrg    if (msrfd == 0) {
67621ff18cSmrg#ifdef X_PRIVSEP
68621ff18cSmrg        msrfd = priv_open_device(_PATH_MSRDEV);
69621ff18cSmrg#else
70621ff18cSmrg        msrfd = open(_PATH_MSRDEV, O_RDWR);
71621ff18cSmrg#endif
72ea7e5fe0Smrg        if (msrfd == -1)
73621ff18cSmrg            FatalError("Unable to open %s: %s\n", _PATH_MSRDEV,
74621ff18cSmrg                strerror(errno));
75f29dbc25Smrg    }
76f29dbc25Smrg
77f29dbc25Smrg    return msrfd;
78f29dbc25Smrg}
79f29dbc25Smrg
80f29dbc25Smrgint
81f29dbc25SmrgGeodeReadMSR(unsigned long addr, unsigned long *lo, unsigned long *hi)
82f29dbc25Smrg{
83621ff18cSmrg#ifdef __OpenBSD__
84621ff18cSmrg    struct amdmsr_req req;
85621ff18cSmrg    int fd = _msr_open();
86621ff18cSmrg
87621ff18cSmrg    req.addr = addr;
88621ff18cSmrg
89621ff18cSmrg    if (ioctl(fd, RDMSR, &req) == -1)
90621ff18cSmrg	FatalError("Unable to read MSR at address %0x06x: %s\n", addr,
91621ff18cSmrg	    strerror(errno));
92621ff18cSmrg
93621ff18cSmrg    *hi = req.val >> 32;
94621ff18cSmrg    *lo = req.val & 0xffffffff;
9544802259Smrg#elif defined __FreeBSD__
9644802259Smrg    cpuctl_msr_args_t args;
9744802259Smrg    int fd = _msr_open();
9844802259Smrg
9944802259Smrg    args.msr = addr;
10044802259Smrg
10144802259Smrg    if (ioctl(fd, CPUCTL_RDMSR, &args) == -1)
10244802259Smrg	FatalError("Unable to read MSR at address %0x06x: %s\n", addr,
10344802259Smrg	    strerror(errno));
10444802259Smrg
10544802259Smrg    *hi = args.data >> 32;
10644802259Smrg    *lo = args.data & 0xffffffff;
107621ff18cSmrg#else
108f29dbc25Smrg    unsigned int data[2];
109f29dbc25Smrg    int fd = _msr_open();
110f29dbc25Smrg    int ret;
111f29dbc25Smrg
112f29dbc25Smrg    if (fd == -1)
113ea7e5fe0Smrg        return -1;
114f29dbc25Smrg
115ea7e5fe0Smrg    ret = lseek(fd, (off_t) addr, SEEK_SET);
116f29dbc25Smrg
117f29dbc25Smrg    if (ret == -1)
118ea7e5fe0Smrg        return -1;
119f29dbc25Smrg
120ea7e5fe0Smrg    ret = read(fd, (void *) data, sizeof(data));
121f29dbc25Smrg
122f29dbc25Smrg    if (ret != 8)
123ea7e5fe0Smrg        return -1;
124f29dbc25Smrg
125f29dbc25Smrg    *hi = data[1];
126f29dbc25Smrg    *lo = data[0];
127621ff18cSmrg#endif
128f29dbc25Smrg    return 0;
129f29dbc25Smrg}
130f29dbc25Smrg
131f29dbc25Smrgint
132f29dbc25SmrgGeodeWriteMSR(unsigned long addr, unsigned long lo, unsigned long hi)
133f29dbc25Smrg{
134621ff18cSmrg#ifdef __OpenBSD__
135621ff18cSmrg    struct amdmsr_req req;
136621ff18cSmrg    int fd = _msr_open();
137621ff18cSmrg
138621ff18cSmrg    req.addr = addr;
139621ff18cSmrg    req.val = (u_int64_t) hi << 32 | (u_int64_t)lo;
140621ff18cSmrg
141621ff18cSmrg    if (ioctl(fd, WRMSR, &req) == -1)
142621ff18cSmrg        FatalError("Unable to write MSR at address 0x%06x: %s\n", addr,
143621ff18cSmrg            strerror(errno));
14444802259Smrg#elif defined __FreeBSD__
14544802259Smrg    cpuctl_msr_args_t args;
14644802259Smrg    int fd = _msr_open();
14744802259Smrg
14844802259Smrg    args.msr = addr;
14944802259Smrg    args.data = (u_int64_t) hi << 32 | (u_int64_t)lo;
15044802259Smrg
15144802259Smrg    if (ioctl(fd, CPUCTL_WRMSR, &args) == -1)
15244802259Smrg        FatalError("Unable to write MSR at address 0x%06x: %s\n", addr,
15344802259Smrg            strerror(errno));
154621ff18cSmrg#else
155f29dbc25Smrg    unsigned int data[2];
156f29dbc25Smrg    int fd = _msr_open();
157f29dbc25Smrg
158f29dbc25Smrg    if (fd == -1)
159ea7e5fe0Smrg        return -1;
160f29dbc25Smrg
161ea7e5fe0Smrg    if (lseek(fd, (off_t) addr, SEEK_SET) == -1)
162ea7e5fe0Smrg        return -1;
163f29dbc25Smrg
164f29dbc25Smrg    data[0] = lo;
165f29dbc25Smrg    data[1] = hi;
166f29dbc25Smrg
167ea7e5fe0Smrg    if (write(fd, (void *) data, 8) != 8)
168ea7e5fe0Smrg        return -1;
169621ff18cSmrg#endif
170f29dbc25Smrg    return 0;
171f29dbc25Smrg}
172