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