geode_msr.c revision ea7e5fe0
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>
36f29dbc25Smrg#include "os.h"
37f29dbc25Smrg#include "geode.h"
38f29dbc25Smrg
39f29dbc25Smrgstatic int
40f29dbc25Smrg_msr_open(void)
41f29dbc25Smrg{
42f29dbc25Smrg    static int msrfd = 0;
43f29dbc25Smrg
44f29dbc25Smrg    if (msrfd == 0) {
45ea7e5fe0Smrg        msrfd = open("/dev/cpu/0/msr", O_RDWR);
46ea7e5fe0Smrg        if (msrfd == -1)
47ea7e5fe0Smrg            ErrorF("Unable to open /dev/cpu/0/msr: %d\n", errno);
48f29dbc25Smrg    }
49f29dbc25Smrg
50f29dbc25Smrg    return msrfd;
51f29dbc25Smrg}
52f29dbc25Smrg
53f29dbc25Smrgint
54f29dbc25SmrgGeodeReadMSR(unsigned long addr, unsigned long *lo, unsigned long *hi)
55f29dbc25Smrg{
56f29dbc25Smrg    unsigned int data[2];
57f29dbc25Smrg    int fd = _msr_open();
58f29dbc25Smrg    int ret;
59f29dbc25Smrg
60f29dbc25Smrg    if (fd == -1)
61ea7e5fe0Smrg        return -1;
62f29dbc25Smrg
63ea7e5fe0Smrg    ret = lseek(fd, (off_t) addr, SEEK_SET);
64f29dbc25Smrg
65f29dbc25Smrg    if (ret == -1)
66ea7e5fe0Smrg        return -1;
67f29dbc25Smrg
68ea7e5fe0Smrg    ret = read(fd, (void *) data, sizeof(data));
69f29dbc25Smrg
70f29dbc25Smrg    if (ret != 8)
71ea7e5fe0Smrg        return -1;
72f29dbc25Smrg
73f29dbc25Smrg    *hi = data[1];
74f29dbc25Smrg    *lo = data[0];
75f29dbc25Smrg
76f29dbc25Smrg    return 0;
77f29dbc25Smrg}
78f29dbc25Smrg
79f29dbc25Smrgint
80f29dbc25SmrgGeodeWriteMSR(unsigned long addr, unsigned long lo, unsigned long hi)
81f29dbc25Smrg{
82f29dbc25Smrg    unsigned int data[2];
83f29dbc25Smrg    int fd = _msr_open();
84f29dbc25Smrg
85f29dbc25Smrg    if (fd == -1)
86ea7e5fe0Smrg        return -1;
87f29dbc25Smrg
88ea7e5fe0Smrg    if (lseek(fd, (off_t) addr, SEEK_SET) == -1)
89ea7e5fe0Smrg        return -1;
90f29dbc25Smrg
91f29dbc25Smrg    data[0] = lo;
92f29dbc25Smrg    data[1] = hi;
93f29dbc25Smrg
94ea7e5fe0Smrg    if (write(fd, (void *) data, 8) != 8)
95ea7e5fe0Smrg        return -1;
96f29dbc25Smrg
97f29dbc25Smrg    return 0;
98f29dbc25Smrg}
99