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