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