Peter Stuge | 33fefca | 2009-01-26 01:33:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2009 Peter Stuge <peter@stuge.se> |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 5 | * Copyright (C) 2009 coresystems GmbH |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 6 | * Copyright (C) 2010 Carl-Daniel Hailfinger |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 7 | * Copyright (C) 2010 Rudolf Marek <r.marek@assembler.cz> |
Peter Stuge | 33fefca | 2009-01-26 01:33:02 +0000 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; version 2 of the License. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | */ |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 22 | |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 23 | #include <sys/types.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <stdlib.h> |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 27 | #include <string.h> |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 28 | #include <errno.h> |
| 29 | #include "flash.h" |
| 30 | |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 31 | #ifdef __DJGPP__ |
| 32 | #include <dpmi.h> |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 33 | #include <sys/nearptr.h> |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 34 | |
| 35 | #define MEM_DEV "dpmi" |
| 36 | |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 37 | static void *realmem_map; |
| 38 | |
| 39 | static void *map_first_meg(unsigned long phys_addr, size_t len) |
| 40 | { |
| 41 | |
| 42 | if (realmem_map) { |
| 43 | return realmem_map + phys_addr; |
| 44 | } |
| 45 | |
| 46 | realmem_map = valloc(1024 * 1024); |
| 47 | |
| 48 | if (!realmem_map) { |
| 49 | return NULL; |
| 50 | } |
| 51 | |
| 52 | if (__djgpp_map_physical_memory(realmem_map, (1024 * 1024), 0)) { |
| 53 | return NULL; |
| 54 | } |
| 55 | |
| 56 | return realmem_map + phys_addr; |
| 57 | } |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 58 | |
| 59 | void *sys_physmap(unsigned long phys_addr, size_t len) |
| 60 | { |
| 61 | int ret; |
| 62 | __dpmi_meminfo mi; |
| 63 | |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 64 | /* enable 4GB limit on DS descriptor */ |
| 65 | if (!__djgpp_nearptr_enable()) { |
| 66 | return NULL; |
| 67 | } |
| 68 | |
| 69 | if ((phys_addr + len - 1) < (1024 * 1024)) { |
| 70 | /* we need to use another method to map first 1MB */ |
| 71 | return map_first_meg(phys_addr, len); |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | mi.address = phys_addr; |
| 75 | mi.size = len; |
| 76 | ret = __dpmi_physical_address_mapping (&mi); |
| 77 | |
| 78 | if (ret != 0) { |
| 79 | return NULL; |
| 80 | } |
| 81 | |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 82 | return (void *) mi.address + __djgpp_conventional_base; |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | #define sys_physmap_rw_uncached sys_physmap |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 86 | #define sys_physmap_ro_cached sys_physmap |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 87 | |
| 88 | void physunmap(void *virt_addr, size_t len) |
| 89 | { |
| 90 | __dpmi_meminfo mi; |
| 91 | |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 92 | /* we ignore unmaps for our first 1MB */ |
| 93 | if ((virt_addr >= realmem_map) && ((virt_addr + len) <= (realmem_map + (1024 * 1024)))) { |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 94 | return; |
| 95 | } |
| 96 | |
| 97 | mi.address = (unsigned long) virt_addr; |
| 98 | __dpmi_free_physical_address_mapping(&mi); |
| 99 | } |
| 100 | |
| 101 | #elif defined(__DARWIN__) |
| 102 | |
Stefan Reinauer | f79edb9 | 2009-01-26 01:23:31 +0000 | [diff] [blame] | 103 | #include <DirectIO/darwinio.h> |
| 104 | |
| 105 | #define MEM_DEV "DirectIO" |
| 106 | |
| 107 | void *sys_physmap(unsigned long phys_addr, size_t len) |
| 108 | { |
| 109 | return map_physical(phys_addr, len); |
| 110 | } |
| 111 | |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 112 | /* The OS X driver does not differentiate between mapping types. */ |
| 113 | #define sys_physmap_rw_uncached sys_physmap |
| 114 | #define sys_physmap_ro_cached sys_physmap |
| 115 | |
Stefan Reinauer | f79edb9 | 2009-01-26 01:23:31 +0000 | [diff] [blame] | 116 | void physunmap(void *virt_addr, size_t len) |
| 117 | { |
| 118 | unmap_physical(virt_addr, len); |
| 119 | } |
| 120 | |
| 121 | #else |
| 122 | #include <sys/mman.h> |
| 123 | |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 124 | #if defined (__sun) && (defined(__i386) || defined(__amd64)) |
| 125 | # define MEM_DEV "/dev/xsvc" |
| 126 | #else |
| 127 | # define MEM_DEV "/dev/mem" |
| 128 | #endif |
| 129 | |
| 130 | static int fd_mem = -1; |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 131 | static int fd_mem_cached = -1; |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 132 | |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 133 | /* For MMIO access. Must be uncached, doesn't make sense to restrict to ro. */ |
| 134 | void *sys_physmap_rw_uncached(unsigned long phys_addr, size_t len) |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 135 | { |
| 136 | void *virt_addr; |
| 137 | |
| 138 | if (-1 == fd_mem) { |
| 139 | /* Open the memory device UNCACHED. Important for MMIO. */ |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 140 | if (-1 == (fd_mem = open(MEM_DEV, O_RDWR | O_SYNC))) { |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 141 | perror("Critical error: open(" MEM_DEV ")"); |
Peter Stuge | 43438ba | 2009-01-26 02:04:19 +0000 | [diff] [blame] | 142 | exit(2); |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 146 | virt_addr = mmap(0, len, PROT_WRITE | PROT_READ, MAP_SHARED, |
| 147 | fd_mem, (off_t)phys_addr); |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 148 | return MAP_FAILED == virt_addr ? NULL : virt_addr; |
| 149 | } |
| 150 | |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 151 | /* For reading DMI/coreboot/whatever tables. We should never write, and we |
| 152 | * do not care about caching. |
| 153 | */ |
| 154 | void *sys_physmap_ro_cached(unsigned long phys_addr, size_t len) |
| 155 | { |
| 156 | void *virt_addr; |
| 157 | |
| 158 | if (-1 == fd_mem_cached) { |
| 159 | /* Open the memory device CACHED. */ |
| 160 | if (-1 == (fd_mem_cached = open(MEM_DEV, O_RDWR))) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 161 | msg_perr("Critical error: open(" MEM_DEV "): %s", strerror(errno)); |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 162 | exit(2); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | virt_addr = mmap(0, len, PROT_READ, MAP_SHARED, |
| 167 | fd_mem_cached, (off_t)phys_addr); |
| 168 | return MAP_FAILED == virt_addr ? NULL : virt_addr; |
| 169 | } |
| 170 | |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 171 | void physunmap(void *virt_addr, size_t len) |
| 172 | { |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 173 | if (len == 0) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 174 | msg_pspew("Not unmapping zero size at %p\n", virt_addr); |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 175 | return; |
| 176 | } |
| 177 | |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 178 | munmap(virt_addr, len); |
| 179 | } |
Stefan Reinauer | f79edb9 | 2009-01-26 01:23:31 +0000 | [diff] [blame] | 180 | #endif |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 181 | |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 182 | #define PHYSMAP_NOFAIL 0 |
| 183 | #define PHYSMAP_MAYFAIL 1 |
| 184 | #define PHYSMAP_RW 0 |
| 185 | #define PHYSMAP_RO 1 |
| 186 | |
| 187 | void *physmap_common(const char *descr, unsigned long phys_addr, size_t len, int mayfail, int readonly) |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 188 | { |
Stephan Guilloux | 6d08a3e | 2009-06-23 10:44:36 +0000 | [diff] [blame] | 189 | void *virt_addr; |
| 190 | |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 191 | if (len == 0) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 192 | msg_pspew("Not mapping %s, zero size at 0x%08lx.\n", |
Uwe Hermann | eaefb48 | 2009-05-17 22:57:34 +0000 | [diff] [blame] | 193 | descr, phys_addr); |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 194 | return NULL; |
| 195 | } |
| 196 | |
| 197 | if ((getpagesize() - 1) & len) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 198 | msg_perr("Mapping %s at 0x%08lx, unaligned size 0x%lx.\n", |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 199 | descr, phys_addr, (unsigned long)len); |
| 200 | } |
| 201 | |
| 202 | if ((getpagesize() - 1) & phys_addr) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 203 | msg_perr("Mapping %s, 0x%lx bytes at unaligned 0x%08lx.\n", |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 204 | descr, (unsigned long)len, phys_addr); |
| 205 | } |
| 206 | |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 207 | if (readonly) { |
| 208 | virt_addr = sys_physmap_ro_cached(phys_addr, len); |
| 209 | } else { |
| 210 | virt_addr = sys_physmap_rw_uncached(phys_addr, len); |
| 211 | } |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 212 | |
| 213 | if (NULL == virt_addr) { |
| 214 | if (NULL == descr) |
| 215 | descr = "memory"; |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 216 | msg_perr("Error accessing %s, 0x%lx bytes at 0x%08lx\n", descr, (unsigned long)len, phys_addr); |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 217 | perror(MEM_DEV " mmap failed"); |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 218 | #ifdef __linux__ |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 219 | if (EINVAL == errno) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 220 | msg_perr("In Linux this error can be caused by the CONFIG_NONPROMISC_DEVMEM (<2.6.27),\n"); |
| 221 | msg_perr("CONFIG_STRICT_DEVMEM (>=2.6.27) and CONFIG_X86_PAT kernel options.\n"); |
| 222 | msg_perr("Please check if either is enabled in your kernel before reporting a failure.\n"); |
| 223 | msg_perr("You can override CONFIG_X86_PAT at boot with the nopat kernel parameter but\n"); |
| 224 | msg_perr("disabling the other option unfortunately requires a kernel recompile. Sorry!\n"); |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 225 | } |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 226 | #endif |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 227 | if (!mayfail) |
| 228 | exit(3); |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | return virt_addr; |
| 232 | } |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 233 | |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 234 | void *physmap(const char *descr, unsigned long phys_addr, size_t len) |
| 235 | { |
| 236 | return physmap_common(descr, phys_addr, len, PHYSMAP_NOFAIL, PHYSMAP_RW); |
| 237 | } |
| 238 | |
| 239 | void *physmap_try_ro(const char *descr, unsigned long phys_addr, size_t len) |
| 240 | { |
| 241 | return physmap_common(descr, phys_addr, len, PHYSMAP_MAYFAIL, PHYSMAP_RO); |
| 242 | } |
| 243 | |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 244 | #if defined(__i386__) || defined(__x86_64__) |
| 245 | |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 246 | #ifdef __linux__ |
| 247 | /* |
| 248 | * Reading and writing to MSRs, however requires instructions rdmsr/wrmsr, |
| 249 | * which are ring0 privileged instructions so only the kernel can do the |
| 250 | * read/write. This function, therefore, requires that the msr kernel module |
| 251 | * be loaded to access these instructions from user space using device |
| 252 | * /dev/cpu/0/msr. |
| 253 | */ |
| 254 | |
| 255 | static int fd_msr = -1; |
| 256 | |
| 257 | msr_t rdmsr(int addr) |
| 258 | { |
Stefan Reinauer | 95e892b | 2009-09-04 13:57:07 +0000 | [diff] [blame] | 259 | uint32_t buf[2]; |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 260 | msr_t msr = { 0xffffffff, 0xffffffff }; |
| 261 | |
| 262 | if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) { |
| 263 | perror("Could not lseek() to MSR"); |
| 264 | close(fd_msr); |
| 265 | exit(1); |
| 266 | } |
| 267 | |
| 268 | if (read(fd_msr, buf, 8) == 8) { |
Stefan Reinauer | 95e892b | 2009-09-04 13:57:07 +0000 | [diff] [blame] | 269 | msr.lo = buf[0]; |
| 270 | msr.hi = buf[1]; |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 271 | |
| 272 | return msr; |
| 273 | } |
| 274 | |
| 275 | if (errno != EIO) { |
| 276 | // A severe error. |
| 277 | perror("Could not read() MSR"); |
| 278 | close(fd_msr); |
| 279 | exit(1); |
| 280 | } |
| 281 | |
| 282 | return msr; |
| 283 | } |
| 284 | |
| 285 | int wrmsr(int addr, msr_t msr) |
| 286 | { |
Anti Sullin | be24d81 | 2010-05-17 23:19:22 +0000 | [diff] [blame] | 287 | uint32_t buf[2]; |
| 288 | buf[0] = msr.lo; |
| 289 | buf[1] = msr.hi; |
| 290 | |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 291 | if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) { |
| 292 | perror("Could not lseek() to MSR"); |
| 293 | close(fd_msr); |
| 294 | exit(1); |
| 295 | } |
| 296 | |
Anti Sullin | be24d81 | 2010-05-17 23:19:22 +0000 | [diff] [blame] | 297 | if (write(fd_msr, buf, 8) != 8 && errno != EIO) { |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 298 | perror("Could not write() MSR"); |
| 299 | close(fd_msr); |
| 300 | exit(1); |
| 301 | } |
| 302 | |
| 303 | /* some MSRs must not be written */ |
| 304 | if (errno == EIO) |
| 305 | return -1; |
| 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | int setup_cpu_msr(int cpu) |
| 311 | { |
| 312 | char msrfilename[64]; |
| 313 | memset(msrfilename, 0, 64); |
| 314 | sprintf(msrfilename, "/dev/cpu/%d/msr", cpu); |
| 315 | |
| 316 | if (fd_msr != -1) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 317 | msg_pinfo("MSR was already initialized\n"); |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 318 | return -1; |
| 319 | } |
| 320 | |
| 321 | fd_msr = open(msrfilename, O_RDWR); |
| 322 | |
| 323 | if (fd_msr < 0) { |
| 324 | perror("Error while opening /dev/cpu/0/msr"); |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 325 | msg_pinfo("Did you run 'modprobe msr'?\n"); |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 326 | return -1; |
| 327 | } |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | void cleanup_cpu_msr(void) |
| 333 | { |
| 334 | if (fd_msr == -1) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 335 | msg_pinfo("No MSR initialized.\n"); |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 336 | return; |
| 337 | } |
| 338 | |
| 339 | close(fd_msr); |
| 340 | |
| 341 | /* Clear MSR file descriptor */ |
| 342 | fd_msr = -1; |
| 343 | } |
| 344 | #else |
Stefan Reinauer | 173e3ea | 2009-08-19 10:46:23 +0000 | [diff] [blame] | 345 | #if defined(__FreeBSD__) || defined(__DragonFly__) |
| 346 | #include <sys/ioctl.h> |
| 347 | |
| 348 | typedef struct { |
| 349 | int msr; |
| 350 | uint64_t data; |
| 351 | } cpu_msr_args_t; |
| 352 | #define CPU_RDMSR _IOWR('c', 1, cpu_msr_args_t) |
| 353 | #define CPU_WRMSR _IOWR('c', 2, cpu_msr_args_t) |
| 354 | |
| 355 | static int fd_msr = -1; |
| 356 | |
| 357 | msr_t rdmsr(int addr) |
| 358 | { |
| 359 | cpu_msr_args_t args; |
| 360 | |
| 361 | msr_t msr = { 0xffffffff, 0xffffffff }; |
| 362 | |
| 363 | args.msr = addr; |
| 364 | |
| 365 | if (ioctl(fd_msr, CPU_RDMSR, &args) < 0) { |
| 366 | perror("CPU_RDMSR"); |
| 367 | close(fd_msr); |
| 368 | exit(1); |
| 369 | } |
| 370 | |
| 371 | msr.lo = args.data & 0xffffffff; |
| 372 | msr.hi = args.data >> 32; |
| 373 | |
| 374 | return msr; |
| 375 | } |
| 376 | |
| 377 | int wrmsr(int addr, msr_t msr) |
| 378 | { |
| 379 | cpu_msr_args_t args; |
| 380 | |
| 381 | args.msr = addr; |
| 382 | args.data = (((uint64_t)msr.hi) << 32) | msr.lo; |
| 383 | |
| 384 | if (ioctl(fd_msr, CPU_WRMSR, &args) < 0) { |
| 385 | perror("CPU_WRMSR"); |
| 386 | close(fd_msr); |
| 387 | exit(1); |
| 388 | } |
| 389 | |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | int setup_cpu_msr(int cpu) |
| 394 | { |
| 395 | char msrfilename[64]; |
| 396 | memset(msrfilename, 0, 64); |
| 397 | sprintf(msrfilename, "/dev/cpu%d", cpu); |
| 398 | |
| 399 | if (fd_msr != -1) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 400 | msg_pinfo("MSR was already initialized\n"); |
Stefan Reinauer | 173e3ea | 2009-08-19 10:46:23 +0000 | [diff] [blame] | 401 | return -1; |
| 402 | } |
| 403 | |
| 404 | fd_msr = open(msrfilename, O_RDWR); |
| 405 | |
| 406 | if (fd_msr < 0) { |
| 407 | perror("Error while opening /dev/cpu0"); |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 408 | msg_pinfo("Did you install ports/sysutils/devcpu?\n"); |
Stefan Reinauer | 173e3ea | 2009-08-19 10:46:23 +0000 | [diff] [blame] | 409 | return -1; |
| 410 | } |
| 411 | |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | void cleanup_cpu_msr(void) |
| 416 | { |
| 417 | if (fd_msr == -1) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 418 | msg_pinfo("No MSR initialized.\n"); |
Stefan Reinauer | 173e3ea | 2009-08-19 10:46:23 +0000 | [diff] [blame] | 419 | return; |
| 420 | } |
| 421 | |
| 422 | close(fd_msr); |
| 423 | |
| 424 | /* Clear MSR file descriptor */ |
| 425 | fd_msr = -1; |
| 426 | } |
| 427 | |
| 428 | #else |
| 429 | |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 430 | #ifdef __DARWIN__ |
| 431 | int setup_cpu_msr(int cpu) |
| 432 | { |
| 433 | // Always succeed for now |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | void cleanup_cpu_msr(void) |
| 438 | { |
| 439 | // Nothing, yet. |
| 440 | } |
| 441 | #else |
| 442 | msr_t rdmsr(int addr) |
| 443 | { |
| 444 | msr_t ret = { 0xffffffff, 0xffffffff }; |
| 445 | |
| 446 | return ret; |
| 447 | } |
| 448 | |
| 449 | int wrmsr(int addr, msr_t msr) |
| 450 | { |
| 451 | return -1; |
| 452 | } |
| 453 | |
| 454 | int setup_cpu_msr(int cpu) |
| 455 | { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 456 | msg_pinfo("No MSR support for your OS yet.\n"); |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 457 | return -1; |
| 458 | } |
| 459 | |
| 460 | void cleanup_cpu_msr(void) |
| 461 | { |
| 462 | // Nothing, yet. |
| 463 | } |
| 464 | #endif |
| 465 | #endif |
Stefan Reinauer | 173e3ea | 2009-08-19 10:46:23 +0000 | [diff] [blame] | 466 | #endif |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 467 | #else |
| 468 | /* Does MSR exist on non-x86 architectures? */ |
| 469 | #endif |