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 |
Peter Stuge | 33fefca | 2009-01-26 01:33:02 +0000 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; version 2 of the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 20 | |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 21 | #include <sys/types.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <stdlib.h> |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 25 | #include <string.h> |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 26 | #include <errno.h> |
| 27 | #include "flash.h" |
| 28 | |
Stefan Reinauer | f79edb9 | 2009-01-26 01:23:31 +0000 | [diff] [blame] | 29 | #ifdef __DARWIN__ |
| 30 | #include <DirectIO/darwinio.h> |
| 31 | |
| 32 | #define MEM_DEV "DirectIO" |
| 33 | |
| 34 | void *sys_physmap(unsigned long phys_addr, size_t len) |
| 35 | { |
| 36 | return map_physical(phys_addr, len); |
| 37 | } |
| 38 | |
| 39 | void physunmap(void *virt_addr, size_t len) |
| 40 | { |
| 41 | unmap_physical(virt_addr, len); |
| 42 | } |
| 43 | |
| 44 | #else |
| 45 | #include <sys/mman.h> |
| 46 | |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 47 | #if defined (__sun) && (defined(__i386) || defined(__amd64)) |
| 48 | # define MEM_DEV "/dev/xsvc" |
| 49 | #else |
| 50 | # define MEM_DEV "/dev/mem" |
| 51 | #endif |
| 52 | |
| 53 | static int fd_mem = -1; |
| 54 | |
| 55 | void *sys_physmap(unsigned long phys_addr, size_t len) |
| 56 | { |
| 57 | void *virt_addr; |
| 58 | |
| 59 | if (-1 == fd_mem) { |
| 60 | /* Open the memory device UNCACHED. Important for MMIO. */ |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 61 | if (-1 == (fd_mem = open(MEM_DEV, O_RDWR | O_SYNC))) { |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 62 | perror("Critical error: open(" MEM_DEV ")"); |
Peter Stuge | 43438ba | 2009-01-26 02:04:19 +0000 | [diff] [blame] | 63 | exit(2); |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 67 | virt_addr = mmap(0, len, PROT_WRITE | PROT_READ, MAP_SHARED, |
| 68 | fd_mem, (off_t)phys_addr); |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 69 | return MAP_FAILED == virt_addr ? NULL : virt_addr; |
| 70 | } |
| 71 | |
| 72 | void physunmap(void *virt_addr, size_t len) |
| 73 | { |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 74 | if (len == 0) { |
| 75 | printf_debug("Not unmapping zero size at %p\n", virt_addr); |
| 76 | return; |
| 77 | } |
| 78 | |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 79 | munmap(virt_addr, len); |
| 80 | } |
Stefan Reinauer | f79edb9 | 2009-01-26 01:23:31 +0000 | [diff] [blame] | 81 | #endif |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 82 | |
| 83 | void *physmap(const char *descr, unsigned long phys_addr, size_t len) |
| 84 | { |
Stephan Guilloux | 6d08a3e | 2009-06-23 10:44:36 +0000 | [diff] [blame] | 85 | void *virt_addr; |
| 86 | |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 87 | if (len == 0) { |
Uwe Hermann | eaefb48 | 2009-05-17 22:57:34 +0000 | [diff] [blame] | 88 | printf_debug("Not mapping %s, zero size at 0x%08lx.\n", |
| 89 | descr, phys_addr); |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 90 | return NULL; |
| 91 | } |
| 92 | |
| 93 | if ((getpagesize() - 1) & len) { |
Uwe Hermann | eaefb48 | 2009-05-17 22:57:34 +0000 | [diff] [blame] | 94 | fprintf(stderr, "Mapping %s at 0x%08lx, unaligned size 0x%lx.\n", |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 95 | descr, phys_addr, (unsigned long)len); |
| 96 | } |
| 97 | |
| 98 | if ((getpagesize() - 1) & phys_addr) { |
Uwe Hermann | eaefb48 | 2009-05-17 22:57:34 +0000 | [diff] [blame] | 99 | fprintf(stderr, "Mapping %s, 0x%lx bytes at unaligned 0x%08lx.\n", |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 100 | descr, (unsigned long)len, phys_addr); |
| 101 | } |
| 102 | |
Stephan Guilloux | 6d08a3e | 2009-06-23 10:44:36 +0000 | [diff] [blame] | 103 | virt_addr = sys_physmap(phys_addr, len); |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 104 | |
| 105 | if (NULL == virt_addr) { |
| 106 | if (NULL == descr) |
| 107 | descr = "memory"; |
| 108 | fprintf(stderr, "Error accessing %s, 0x%lx bytes at 0x%08lx\n", descr, (unsigned long)len, phys_addr); |
| 109 | perror(MEM_DEV " mmap failed"); |
| 110 | if (EINVAL == errno) { |
| 111 | fprintf(stderr, "In Linux this error can be caused by the CONFIG_NONPROMISC_DEVMEM (<2.6.27),\n"); |
| 112 | fprintf(stderr, "CONFIG_STRICT_DEVMEM (>=2.6.27) and CONFIG_X86_PAT kernel options.\n"); |
| 113 | fprintf(stderr, "Please check if either is enabled in your kernel before reporting a failure.\n"); |
| 114 | fprintf(stderr, "You can override CONFIG_X86_PAT at boot with the nopat kernel parameter but\n"); |
| 115 | fprintf(stderr, "disabling the other option unfortunately requires a kernel recompile. Sorry!\n"); |
| 116 | } |
Peter Stuge | 43438ba | 2009-01-26 02:04:19 +0000 | [diff] [blame] | 117 | exit(3); |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | return virt_addr; |
| 121 | } |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 122 | |
| 123 | #ifdef __linux__ |
| 124 | /* |
| 125 | * Reading and writing to MSRs, however requires instructions rdmsr/wrmsr, |
| 126 | * which are ring0 privileged instructions so only the kernel can do the |
| 127 | * read/write. This function, therefore, requires that the msr kernel module |
| 128 | * be loaded to access these instructions from user space using device |
| 129 | * /dev/cpu/0/msr. |
| 130 | */ |
| 131 | |
| 132 | static int fd_msr = -1; |
| 133 | |
| 134 | msr_t rdmsr(int addr) |
| 135 | { |
| 136 | uint8_t buf[8]; |
| 137 | msr_t msr = { 0xffffffff, 0xffffffff }; |
| 138 | |
| 139 | if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) { |
| 140 | perror("Could not lseek() to MSR"); |
| 141 | close(fd_msr); |
| 142 | exit(1); |
| 143 | } |
| 144 | |
| 145 | if (read(fd_msr, buf, 8) == 8) { |
| 146 | msr.lo = *(uint32_t *)buf; |
| 147 | msr.hi = *(uint32_t *)(buf + 4); |
| 148 | |
| 149 | return msr; |
| 150 | } |
| 151 | |
| 152 | if (errno != EIO) { |
| 153 | // A severe error. |
| 154 | perror("Could not read() MSR"); |
| 155 | close(fd_msr); |
| 156 | exit(1); |
| 157 | } |
| 158 | |
| 159 | return msr; |
| 160 | } |
| 161 | |
| 162 | int wrmsr(int addr, msr_t msr) |
| 163 | { |
| 164 | if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) { |
| 165 | perror("Could not lseek() to MSR"); |
| 166 | close(fd_msr); |
| 167 | exit(1); |
| 168 | } |
| 169 | |
| 170 | if (write(fd_msr, &msr, 8) != 8 && errno != EIO) { |
| 171 | perror("Could not write() MSR"); |
| 172 | close(fd_msr); |
| 173 | exit(1); |
| 174 | } |
| 175 | |
| 176 | /* some MSRs must not be written */ |
| 177 | if (errno == EIO) |
| 178 | return -1; |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | int setup_cpu_msr(int cpu) |
| 184 | { |
| 185 | char msrfilename[64]; |
| 186 | memset(msrfilename, 0, 64); |
| 187 | sprintf(msrfilename, "/dev/cpu/%d/msr", cpu); |
| 188 | |
| 189 | if (fd_msr != -1) { |
| 190 | printf("MSR was already initialized\n"); |
| 191 | return -1; |
| 192 | } |
| 193 | |
| 194 | fd_msr = open(msrfilename, O_RDWR); |
| 195 | |
| 196 | if (fd_msr < 0) { |
| 197 | perror("Error while opening /dev/cpu/0/msr"); |
| 198 | printf("Did you run 'modprobe msr'?\n"); |
| 199 | return -1; |
| 200 | } |
| 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | void cleanup_cpu_msr(void) |
| 206 | { |
| 207 | if (fd_msr == -1) { |
| 208 | printf("No MSR initialized.\n"); |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | close(fd_msr); |
| 213 | |
| 214 | /* Clear MSR file descriptor */ |
| 215 | fd_msr = -1; |
| 216 | } |
| 217 | #else |
Stefan Reinauer | 173e3ea | 2009-08-19 10:46:23 +0000 | [diff] [blame] | 218 | #if defined(__FreeBSD__) || defined(__DragonFly__) |
| 219 | #include <sys/ioctl.h> |
| 220 | |
| 221 | typedef struct { |
| 222 | int msr; |
| 223 | uint64_t data; |
| 224 | } cpu_msr_args_t; |
| 225 | #define CPU_RDMSR _IOWR('c', 1, cpu_msr_args_t) |
| 226 | #define CPU_WRMSR _IOWR('c', 2, cpu_msr_args_t) |
| 227 | |
| 228 | static int fd_msr = -1; |
| 229 | |
| 230 | msr_t rdmsr(int addr) |
| 231 | { |
| 232 | cpu_msr_args_t args; |
| 233 | |
| 234 | msr_t msr = { 0xffffffff, 0xffffffff }; |
| 235 | |
| 236 | args.msr = addr; |
| 237 | |
| 238 | if (ioctl(fd_msr, CPU_RDMSR, &args) < 0) { |
| 239 | perror("CPU_RDMSR"); |
| 240 | close(fd_msr); |
| 241 | exit(1); |
| 242 | } |
| 243 | |
| 244 | msr.lo = args.data & 0xffffffff; |
| 245 | msr.hi = args.data >> 32; |
| 246 | |
| 247 | return msr; |
| 248 | } |
| 249 | |
| 250 | int wrmsr(int addr, msr_t msr) |
| 251 | { |
| 252 | cpu_msr_args_t args; |
| 253 | |
| 254 | args.msr = addr; |
| 255 | args.data = (((uint64_t)msr.hi) << 32) | msr.lo; |
| 256 | |
| 257 | if (ioctl(fd_msr, CPU_WRMSR, &args) < 0) { |
| 258 | perror("CPU_WRMSR"); |
| 259 | close(fd_msr); |
| 260 | exit(1); |
| 261 | } |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | int setup_cpu_msr(int cpu) |
| 267 | { |
| 268 | char msrfilename[64]; |
| 269 | memset(msrfilename, 0, 64); |
| 270 | sprintf(msrfilename, "/dev/cpu%d", cpu); |
| 271 | |
| 272 | if (fd_msr != -1) { |
| 273 | printf("MSR was already initialized\n"); |
| 274 | return -1; |
| 275 | } |
| 276 | |
| 277 | fd_msr = open(msrfilename, O_RDWR); |
| 278 | |
| 279 | if (fd_msr < 0) { |
| 280 | perror("Error while opening /dev/cpu0"); |
| 281 | printf("Did you install ports/sysutils/devcpu?\n"); |
| 282 | return -1; |
| 283 | } |
| 284 | |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | void cleanup_cpu_msr(void) |
| 289 | { |
| 290 | if (fd_msr == -1) { |
| 291 | printf("No MSR initialized.\n"); |
| 292 | return; |
| 293 | } |
| 294 | |
| 295 | close(fd_msr); |
| 296 | |
| 297 | /* Clear MSR file descriptor */ |
| 298 | fd_msr = -1; |
| 299 | } |
| 300 | |
| 301 | #else |
| 302 | |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 303 | #ifdef __DARWIN__ |
| 304 | int setup_cpu_msr(int cpu) |
| 305 | { |
| 306 | // Always succeed for now |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | void cleanup_cpu_msr(void) |
| 311 | { |
| 312 | // Nothing, yet. |
| 313 | } |
| 314 | #else |
| 315 | msr_t rdmsr(int addr) |
| 316 | { |
| 317 | msr_t ret = { 0xffffffff, 0xffffffff }; |
| 318 | |
| 319 | return ret; |
| 320 | } |
| 321 | |
| 322 | int wrmsr(int addr, msr_t msr) |
| 323 | { |
| 324 | return -1; |
| 325 | } |
| 326 | |
| 327 | int setup_cpu_msr(int cpu) |
| 328 | { |
| 329 | printf("No MSR support for your OS yet.\n"); |
| 330 | return -1; |
| 331 | } |
| 332 | |
| 333 | void cleanup_cpu_msr(void) |
| 334 | { |
| 335 | // Nothing, yet. |
| 336 | } |
| 337 | #endif |
| 338 | #endif |
Stefan Reinauer | 173e3ea | 2009-08-19 10:46:23 +0000 | [diff] [blame] | 339 | #endif |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 340 | |