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