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 <stdlib.h> |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 26 | #include <string.h> |
Carl-Daniel Hailfinger | 11990da | 2013-07-13 23:21:05 +0000 | [diff] [blame] | 27 | #include <errno.h> |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 28 | #include "flash.h" |
Patrick Georgi | 32508eb | 2012-07-20 20:35:14 +0000 | [diff] [blame] | 29 | #include "hwaccess.h" |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 30 | |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 31 | #if !defined(__DJGPP__) && !defined(__LIBPAYLOAD__) |
Carl-Daniel Hailfinger | 11990da | 2013-07-13 23:21:05 +0000 | [diff] [blame] | 32 | /* No file access needed/possible to get mmap access permissions or access MSR. */ |
Carl-Daniel Hailfinger | dcef67e | 2010-06-21 23:20:15 +0000 | [diff] [blame] | 33 | #include <sys/stat.h> |
| 34 | #include <fcntl.h> |
Carl-Daniel Hailfinger | dcef67e | 2010-06-21 23:20:15 +0000 | [diff] [blame] | 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 | |
Stefan Tauner | 305e0b9 | 2013-07-17 23:46:44 +0000 | [diff] [blame] | 45 | static void *map_first_meg(uintptr_t phys_addr, size_t len) |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 46 | { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 47 | if (realmem_map) |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 48 | return realmem_map + phys_addr; |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 49 | |
| 50 | realmem_map = valloc(1024 * 1024); |
| 51 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 52 | if (!realmem_map) |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 53 | return ERROR_PTR; |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 54 | |
| 55 | if (__djgpp_map_physical_memory(realmem_map, (1024 * 1024), 0)) { |
Carl-Daniel Hailfinger | 602de98 | 2010-10-05 23:21:51 +0000 | [diff] [blame] | 56 | free(realmem_map); |
| 57 | realmem_map = NULL; |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 58 | return ERROR_PTR; |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | return realmem_map + phys_addr; |
| 62 | } |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 63 | |
Stefan Tauner | 305e0b9 | 2013-07-17 23:46:44 +0000 | [diff] [blame] | 64 | static void *sys_physmap(uintptr_t phys_addr, size_t len) |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 65 | { |
| 66 | int ret; |
| 67 | __dpmi_meminfo mi; |
| 68 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 69 | /* Enable 4GB limit on DS descriptor. */ |
| 70 | if (!__djgpp_nearptr_enable()) |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 71 | return ERROR_PTR; |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 72 | |
| 73 | if ((phys_addr + len - 1) < (1024 * 1024)) { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 74 | /* We need to use another method to map first 1MB. */ |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 75 | return map_first_meg(phys_addr, len); |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | mi.address = phys_addr; |
| 79 | mi.size = len; |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 80 | ret = __dpmi_physical_address_mapping(&mi); |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 81 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 82 | if (ret != 0) |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 83 | return ERROR_PTR; |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 84 | |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 85 | return (void *) mi.address + __djgpp_conventional_base; |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | #define sys_physmap_rw_uncached sys_physmap |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 89 | #define sys_physmap_ro_cached sys_physmap |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 90 | |
| 91 | void physunmap(void *virt_addr, size_t len) |
| 92 | { |
| 93 | __dpmi_meminfo mi; |
| 94 | |
Carl-Daniel Hailfinger | 602de98 | 2010-10-05 23:21:51 +0000 | [diff] [blame] | 95 | /* There is no known way to unmap the first 1 MB. The DPMI server will |
| 96 | * do this for us on exit. |
| 97 | */ |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 98 | if ((virt_addr >= realmem_map) && |
| 99 | ((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 | |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 107 | #elif defined(__LIBPAYLOAD__) |
| 108 | #include <arch/virtual.h> |
| 109 | |
| 110 | #define MEM_DEV "" |
| 111 | |
Stefan Tauner | 305e0b9 | 2013-07-17 23:46:44 +0000 | [diff] [blame] | 112 | void *sys_physmap(uintptr_t phys_addr, size_t len) |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 113 | { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 114 | return (void *)phys_to_virt(phys_addr); |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | #define sys_physmap_rw_uncached sys_physmap |
| 118 | #define sys_physmap_ro_cached sys_physmap |
| 119 | |
| 120 | void physunmap(void *virt_addr, size_t len) |
| 121 | { |
| 122 | } |
Carl-Daniel Hailfinger | 60d9bd2 | 2012-08-09 23:34:41 +0000 | [diff] [blame] | 123 | #elif defined(__MACH__) && defined(__APPLE__) |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 124 | |
Stefan Reinauer | 83704c5 | 2011-03-18 22:00:15 +0000 | [diff] [blame] | 125 | #define MEM_DEV "DirectHW" |
Stefan Reinauer | f79edb9 | 2009-01-26 01:23:31 +0000 | [diff] [blame] | 126 | |
Stefan Tauner | 305e0b9 | 2013-07-17 23:46:44 +0000 | [diff] [blame] | 127 | static void *sys_physmap(uintptr_t phys_addr, size_t len) |
Stefan Reinauer | f79edb9 | 2009-01-26 01:23:31 +0000 | [diff] [blame] | 128 | { |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 129 | /* The short form of ?: is a GNU extension. |
| 130 | * FIXME: map_physical returns NULL both for errors and for success |
| 131 | * if the region is mapped at virtual address zero. If in doubt, report |
| 132 | * an error until a better interface exists. |
| 133 | */ |
| 134 | return map_physical(phys_addr, len) ? : ERROR_PTR; |
Stefan Reinauer | f79edb9 | 2009-01-26 01:23:31 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 137 | /* The OS X driver does not differentiate between mapping types. */ |
| 138 | #define sys_physmap_rw_uncached sys_physmap |
| 139 | #define sys_physmap_ro_cached sys_physmap |
| 140 | |
Stefan Reinauer | f79edb9 | 2009-01-26 01:23:31 +0000 | [diff] [blame] | 141 | void physunmap(void *virt_addr, size_t len) |
| 142 | { |
| 143 | unmap_physical(virt_addr, len); |
| 144 | } |
| 145 | |
| 146 | #else |
| 147 | #include <sys/mman.h> |
| 148 | |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 149 | #if defined (__sun) && (defined(__i386) || defined(__amd64)) |
| 150 | # define MEM_DEV "/dev/xsvc" |
| 151 | #else |
| 152 | # define MEM_DEV "/dev/mem" |
| 153 | #endif |
| 154 | |
| 155 | static int fd_mem = -1; |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 156 | static int fd_mem_cached = -1; |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 157 | |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 158 | /* For MMIO access. Must be uncached, doesn't make sense to restrict to ro. */ |
Stefan Tauner | 305e0b9 | 2013-07-17 23:46:44 +0000 | [diff] [blame] | 159 | static void *sys_physmap_rw_uncached(uintptr_t phys_addr, size_t len) |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 160 | { |
| 161 | void *virt_addr; |
| 162 | |
| 163 | if (-1 == fd_mem) { |
| 164 | /* Open the memory device UNCACHED. Important for MMIO. */ |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 165 | if (-1 == (fd_mem = open(MEM_DEV, O_RDWR | O_SYNC))) { |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 166 | msg_perr("Critical error: open(" MEM_DEV "): %s\n", strerror(errno)); |
Peter Stuge | 43438ba | 2009-01-26 02:04:19 +0000 | [diff] [blame] | 167 | exit(2); |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
Peter Huewe | 73f8ec8 | 2011-01-24 19:15:51 +0000 | [diff] [blame] | 171 | virt_addr = mmap(NULL, len, PROT_WRITE | PROT_READ, MAP_SHARED, |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 172 | fd_mem, (off_t)phys_addr); |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 173 | return MAP_FAILED == virt_addr ? ERROR_PTR : virt_addr; |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 176 | /* For reading DMI/coreboot/whatever tables. We should never write, and we |
| 177 | * do not care about caching. |
| 178 | */ |
Stefan Tauner | 305e0b9 | 2013-07-17 23:46:44 +0000 | [diff] [blame] | 179 | static void *sys_physmap_ro_cached(uintptr_t phys_addr, size_t len) |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 180 | { |
| 181 | void *virt_addr; |
| 182 | |
| 183 | if (-1 == fd_mem_cached) { |
| 184 | /* Open the memory device CACHED. */ |
| 185 | if (-1 == (fd_mem_cached = open(MEM_DEV, O_RDWR))) { |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 186 | msg_perr("Critical error: open(" MEM_DEV "): %s\n", strerror(errno)); |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 187 | exit(2); |
| 188 | } |
| 189 | } |
| 190 | |
Peter Huewe | 73f8ec8 | 2011-01-24 19:15:51 +0000 | [diff] [blame] | 191 | virt_addr = mmap(NULL, len, PROT_READ, MAP_SHARED, |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 192 | fd_mem_cached, (off_t)phys_addr); |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 193 | return MAP_FAILED == virt_addr ? ERROR_PTR : virt_addr; |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 196 | void physunmap(void *virt_addr, size_t len) |
| 197 | { |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 198 | if (len == 0) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 199 | msg_pspew("Not unmapping zero size at %p\n", virt_addr); |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 200 | return; |
| 201 | } |
| 202 | |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 203 | munmap(virt_addr, len); |
| 204 | } |
Stefan Reinauer | f79edb9 | 2009-01-26 01:23:31 +0000 | [diff] [blame] | 205 | #endif |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 206 | |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 207 | #define PHYSMAP_NOFAIL 0 |
| 208 | #define PHYSMAP_MAYFAIL 1 |
| 209 | #define PHYSMAP_RW 0 |
| 210 | #define PHYSMAP_RO 1 |
| 211 | |
Stefan Tauner | 305e0b9 | 2013-07-17 23:46:44 +0000 | [diff] [blame] | 212 | static void *physmap_common(const char *descr, uintptr_t phys_addr, |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 213 | size_t len, int mayfail, int readonly) |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 214 | { |
Stephan Guilloux | 6d08a3e | 2009-06-23 10:44:36 +0000 | [diff] [blame] | 215 | void *virt_addr; |
| 216 | |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 217 | if (len == 0) { |
Stefan Tauner | 305e0b9 | 2013-07-17 23:46:44 +0000 | [diff] [blame] | 218 | msg_pspew("Not mapping %s, zero size at 0x%0*" PRIxPTR ".\n", descr, PRIxPTR_WIDTH, phys_addr); |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 219 | return ERROR_PTR; |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 220 | } |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 221 | |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 222 | if ((getpagesize() - 1) & len) { |
Stefan Tauner | 0554ca5 | 2013-07-25 22:54:25 +0000 | [diff] [blame] | 223 | msg_perr("Mapping %s at 0x%0*" PRIxPTR ", unaligned size 0x%zx.\n", |
| 224 | descr, PRIxPTR_WIDTH, phys_addr, len); |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | if ((getpagesize() - 1) & phys_addr) { |
Stefan Tauner | 0554ca5 | 2013-07-25 22:54:25 +0000 | [diff] [blame] | 228 | msg_perr("Mapping %s, 0x%zx bytes at unaligned 0x%0*" PRIxPTR ".\n", |
| 229 | descr, len, PRIxPTR_WIDTH, phys_addr); |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 232 | if (readonly) |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 233 | virt_addr = sys_physmap_ro_cached(phys_addr, len); |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 234 | else |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 235 | virt_addr = sys_physmap_rw_uncached(phys_addr, len); |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 236 | |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 237 | if (ERROR_PTR == virt_addr) { |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 238 | if (NULL == descr) |
| 239 | descr = "memory"; |
Stefan Tauner | 0554ca5 | 2013-07-25 22:54:25 +0000 | [diff] [blame] | 240 | msg_perr("Error accessing %s, 0x%zx bytes at 0x%0*" PRIxPTR "\n", |
| 241 | descr, len, PRIxPTR_WIDTH, phys_addr); |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 242 | msg_perr(MEM_DEV " mmap failed: %s\n", strerror(errno)); |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 243 | #ifdef __linux__ |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 244 | if (EINVAL == errno) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 245 | msg_perr("In Linux this error can be caused by the CONFIG_NONPROMISC_DEVMEM (<2.6.27),\n"); |
| 246 | msg_perr("CONFIG_STRICT_DEVMEM (>=2.6.27) and CONFIG_X86_PAT kernel options.\n"); |
| 247 | msg_perr("Please check if either is enabled in your kernel before reporting a failure.\n"); |
| 248 | msg_perr("You can override CONFIG_X86_PAT at boot with the nopat kernel parameter but\n"); |
| 249 | 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] | 250 | } |
Carl-Daniel Hailfinger | b63b067 | 2010-07-02 17:12:50 +0000 | [diff] [blame] | 251 | #elif defined (__OpenBSD__) |
| 252 | msg_perr("Please set securelevel=-1 in /etc/rc.securelevel " |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 253 | "and reboot, or reboot into\n" |
| 254 | "single user mode.\n"); |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 255 | #endif |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 256 | if (!mayfail) |
| 257 | exit(3); |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | return virt_addr; |
| 261 | } |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 262 | |
Stefan Tauner | 305e0b9 | 2013-07-17 23:46:44 +0000 | [diff] [blame] | 263 | void *physmap(const char *descr, uintptr_t phys_addr, size_t len) |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 264 | { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 265 | return physmap_common(descr, phys_addr, len, PHYSMAP_NOFAIL, |
| 266 | PHYSMAP_RW); |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Stefan Tauner | 305e0b9 | 2013-07-17 23:46:44 +0000 | [diff] [blame] | 269 | void *physmap_try_ro(const char *descr, uintptr_t phys_addr, size_t len) |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 270 | { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 271 | return physmap_common(descr, phys_addr, len, PHYSMAP_MAYFAIL, |
| 272 | PHYSMAP_RO); |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Stefan Tauner | 6bf5fe7 | 2013-07-13 21:18:32 +0000 | [diff] [blame] | 275 | /* MSR abstraction implementations for Linux, OpenBSD, FreeBSD/Dragonfly, OSX, libpayload |
| 276 | * and a non-working default implemenation on the bottom. See also hwaccess.h for some (re)declarations. */ |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 277 | #if defined(__i386__) || defined(__x86_64__) |
| 278 | |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 279 | #ifdef __linux__ |
| 280 | /* |
| 281 | * Reading and writing to MSRs, however requires instructions rdmsr/wrmsr, |
| 282 | * which are ring0 privileged instructions so only the kernel can do the |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 283 | * read/write. This function, therefore, requires that the msr kernel module |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 284 | * be loaded to access these instructions from user space using device |
| 285 | * /dev/cpu/0/msr. |
| 286 | */ |
| 287 | |
| 288 | static int fd_msr = -1; |
| 289 | |
| 290 | msr_t rdmsr(int addr) |
| 291 | { |
Stefan Reinauer | 95e892b | 2009-09-04 13:57:07 +0000 | [diff] [blame] | 292 | uint32_t buf[2]; |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 293 | msr_t msr = { 0xffffffff, 0xffffffff }; |
| 294 | |
| 295 | if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) { |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 296 | msg_perr("Could not lseek() MSR: %s\n", strerror(errno)); |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 297 | close(fd_msr); |
| 298 | exit(1); |
| 299 | } |
| 300 | |
| 301 | if (read(fd_msr, buf, 8) == 8) { |
Stefan Reinauer | 95e892b | 2009-09-04 13:57:07 +0000 | [diff] [blame] | 302 | msr.lo = buf[0]; |
| 303 | msr.hi = buf[1]; |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 304 | return msr; |
| 305 | } |
| 306 | |
| 307 | if (errno != EIO) { |
| 308 | // A severe error. |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 309 | msg_perr("Could not read() MSR: %s\n", strerror(errno)); |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 310 | close(fd_msr); |
| 311 | exit(1); |
| 312 | } |
| 313 | |
| 314 | return msr; |
| 315 | } |
| 316 | |
| 317 | int wrmsr(int addr, msr_t msr) |
| 318 | { |
Anti Sullin | be24d81 | 2010-05-17 23:19:22 +0000 | [diff] [blame] | 319 | uint32_t buf[2]; |
| 320 | buf[0] = msr.lo; |
| 321 | buf[1] = msr.hi; |
| 322 | |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 323 | if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) { |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 324 | msg_perr("Could not lseek() MSR: %s\n", strerror(errno)); |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 325 | close(fd_msr); |
| 326 | exit(1); |
| 327 | } |
| 328 | |
Anti Sullin | be24d81 | 2010-05-17 23:19:22 +0000 | [diff] [blame] | 329 | if (write(fd_msr, buf, 8) != 8 && errno != EIO) { |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 330 | msg_perr("Could not write() MSR: %s\n", strerror(errno)); |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 331 | close(fd_msr); |
| 332 | exit(1); |
| 333 | } |
| 334 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 335 | /* Some MSRs must not be written. */ |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 336 | if (errno == EIO) |
| 337 | return -1; |
| 338 | |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | int setup_cpu_msr(int cpu) |
| 343 | { |
| 344 | char msrfilename[64]; |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 345 | memset(msrfilename, 0, sizeof(msrfilename)); |
| 346 | snprintf(msrfilename, sizeof(msrfilename), "/dev/cpu/%d/msr", cpu); |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 347 | |
| 348 | if (fd_msr != -1) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 349 | msg_pinfo("MSR was already initialized\n"); |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 350 | return -1; |
| 351 | } |
| 352 | |
| 353 | fd_msr = open(msrfilename, O_RDWR); |
| 354 | |
| 355 | if (fd_msr < 0) { |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 356 | msg_perr("Error while opening %s: %s\n", msrfilename, strerror(errno)); |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 357 | msg_pinfo("Did you run 'modprobe msr'?\n"); |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 358 | return -1; |
| 359 | } |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | void cleanup_cpu_msr(void) |
| 365 | { |
| 366 | if (fd_msr == -1) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 367 | msg_pinfo("No MSR initialized.\n"); |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 368 | return; |
| 369 | } |
| 370 | |
| 371 | close(fd_msr); |
| 372 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 373 | /* Clear MSR file descriptor. */ |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 374 | fd_msr = -1; |
| 375 | } |
Stefan Tauner | 6bf5fe7 | 2013-07-13 21:18:32 +0000 | [diff] [blame] | 376 | #elif defined(__OpenBSD__) /* This does only work for certain AMD Geode LX systems see amdmsr(4). */ |
| 377 | #include <sys/ioctl.h> |
| 378 | #include <machine/amdmsr.h> |
| 379 | |
| 380 | static int fd_msr = -1; |
| 381 | |
| 382 | msr_t rdmsr(int addr) |
| 383 | { |
| 384 | struct amdmsr_req args; |
| 385 | |
| 386 | msr_t msr = { 0xffffffff, 0xffffffff }; |
| 387 | |
| 388 | args.addr = (uint32_t)addr; |
| 389 | |
| 390 | if (ioctl(fd_msr, RDMSR, &args) < 0) { |
| 391 | msg_perr("Error while executing RDMSR ioctl: %s\n", strerror(errno)); |
| 392 | close(fd_msr); |
| 393 | exit(1); |
| 394 | } |
| 395 | |
| 396 | msr.lo = args.val & 0xffffffff; |
| 397 | msr.hi = args.val >> 32; |
| 398 | |
| 399 | return msr; |
| 400 | } |
| 401 | |
| 402 | int wrmsr(int addr, msr_t msr) |
| 403 | { |
| 404 | struct amdmsr_req args; |
| 405 | |
| 406 | args.addr = addr; |
| 407 | args.val = (((uint64_t)msr.hi) << 32) | msr.lo; |
| 408 | |
| 409 | if (ioctl(fd_msr, WRMSR, &args) < 0) { |
| 410 | msg_perr("Error while executing WRMSR ioctl: %s\n", strerror(errno)); |
| 411 | close(fd_msr); |
| 412 | exit(1); |
| 413 | } |
| 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | int setup_cpu_msr(int cpu) |
| 419 | { |
| 420 | char msrfilename[64]; |
| 421 | memset(msrfilename, 0, sizeof(msrfilename)); |
| 422 | snprintf(msrfilename, sizeof(msrfilename), "/dev/amdmsr"); |
| 423 | |
| 424 | if (fd_msr != -1) { |
| 425 | msg_pinfo("MSR was already initialized\n"); |
| 426 | return -1; |
| 427 | } |
| 428 | |
| 429 | fd_msr = open(msrfilename, O_RDWR); |
| 430 | |
| 431 | if (fd_msr < 0) { |
| 432 | msg_perr("Error while opening %s: %s\n", msrfilename, strerror(errno)); |
| 433 | return -1; |
| 434 | } |
| 435 | |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | void cleanup_cpu_msr(void) |
| 440 | { |
| 441 | if (fd_msr == -1) { |
| 442 | msg_pinfo("No MSR initialized.\n"); |
| 443 | return; |
| 444 | } |
| 445 | |
| 446 | close(fd_msr); |
| 447 | |
| 448 | /* Clear MSR file descriptor. */ |
| 449 | fd_msr = -1; |
| 450 | } |
| 451 | |
| 452 | #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) |
Stefan Reinauer | 173e3ea | 2009-08-19 10:46:23 +0000 | [diff] [blame] | 453 | #include <sys/ioctl.h> |
| 454 | |
| 455 | typedef struct { |
| 456 | int msr; |
| 457 | uint64_t data; |
| 458 | } cpu_msr_args_t; |
| 459 | #define CPU_RDMSR _IOWR('c', 1, cpu_msr_args_t) |
| 460 | #define CPU_WRMSR _IOWR('c', 2, cpu_msr_args_t) |
| 461 | |
| 462 | static int fd_msr = -1; |
| 463 | |
| 464 | msr_t rdmsr(int addr) |
| 465 | { |
| 466 | cpu_msr_args_t args; |
| 467 | |
| 468 | msr_t msr = { 0xffffffff, 0xffffffff }; |
| 469 | |
| 470 | args.msr = addr; |
| 471 | |
| 472 | if (ioctl(fd_msr, CPU_RDMSR, &args) < 0) { |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 473 | msg_perr("Error while executing CPU_RDMSR ioctl: %s\n", strerror(errno)); |
Stefan Reinauer | 173e3ea | 2009-08-19 10:46:23 +0000 | [diff] [blame] | 474 | close(fd_msr); |
| 475 | exit(1); |
| 476 | } |
| 477 | |
| 478 | msr.lo = args.data & 0xffffffff; |
| 479 | msr.hi = args.data >> 32; |
| 480 | |
| 481 | return msr; |
| 482 | } |
| 483 | |
| 484 | int wrmsr(int addr, msr_t msr) |
| 485 | { |
| 486 | cpu_msr_args_t args; |
| 487 | |
| 488 | args.msr = addr; |
| 489 | args.data = (((uint64_t)msr.hi) << 32) | msr.lo; |
| 490 | |
| 491 | if (ioctl(fd_msr, CPU_WRMSR, &args) < 0) { |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 492 | msg_perr("Error while executing CPU_WRMSR ioctl: %s\n", strerror(errno)); |
Stefan Reinauer | 173e3ea | 2009-08-19 10:46:23 +0000 | [diff] [blame] | 493 | close(fd_msr); |
| 494 | exit(1); |
| 495 | } |
| 496 | |
| 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | int setup_cpu_msr(int cpu) |
| 501 | { |
| 502 | char msrfilename[64]; |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 503 | memset(msrfilename, 0, sizeof(msrfilename)); |
| 504 | snprintf(msrfilename, sizeof(msrfilename), "/dev/cpu%d", cpu); |
Stefan Reinauer | 173e3ea | 2009-08-19 10:46:23 +0000 | [diff] [blame] | 505 | |
| 506 | if (fd_msr != -1) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 507 | msg_pinfo("MSR was already initialized\n"); |
Stefan Reinauer | 173e3ea | 2009-08-19 10:46:23 +0000 | [diff] [blame] | 508 | return -1; |
| 509 | } |
| 510 | |
| 511 | fd_msr = open(msrfilename, O_RDWR); |
| 512 | |
| 513 | if (fd_msr < 0) { |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 514 | msg_perr("Error while opening %s: %s\n", msrfilename, strerror(errno)); |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 515 | msg_pinfo("Did you install ports/sysutils/devcpu?\n"); |
Stefan Reinauer | 173e3ea | 2009-08-19 10:46:23 +0000 | [diff] [blame] | 516 | return -1; |
| 517 | } |
| 518 | |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | void cleanup_cpu_msr(void) |
| 523 | { |
| 524 | if (fd_msr == -1) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 525 | msg_pinfo("No MSR initialized.\n"); |
Stefan Reinauer | 173e3ea | 2009-08-19 10:46:23 +0000 | [diff] [blame] | 526 | return; |
| 527 | } |
| 528 | |
| 529 | close(fd_msr); |
| 530 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 531 | /* Clear MSR file descriptor. */ |
Stefan Reinauer | 173e3ea | 2009-08-19 10:46:23 +0000 | [diff] [blame] | 532 | fd_msr = -1; |
| 533 | } |
| 534 | |
Stefan Tauner | 6bf5fe7 | 2013-07-13 21:18:32 +0000 | [diff] [blame] | 535 | #elif defined(__MACH__) && defined(__APPLE__) |
| 536 | /* rdmsr() and wrmsr() are provided by DirectHW which needs neither setup nor cleanup. */ |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 537 | int setup_cpu_msr(int cpu) |
| 538 | { |
| 539 | // Always succeed for now |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | void cleanup_cpu_msr(void) |
| 544 | { |
| 545 | // Nothing, yet. |
| 546 | } |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 547 | #elif defined(__LIBPAYLOAD__) |
| 548 | msr_t libpayload_rdmsr(int addr) |
| 549 | { |
| 550 | msr_t msr; |
| 551 | unsigned long long val = _rdmsr(addr); |
| 552 | msr.lo = val & 0xffffffff; |
| 553 | msr.hi = val >> 32; |
| 554 | return msr; |
| 555 | } |
| 556 | |
| 557 | int libpayload_wrmsr(int addr, msr_t msr) |
| 558 | { |
| 559 | _wrmsr(addr, msr.lo | ((unsigned long long)msr.hi << 32)); |
Patrick Georgi | 97bc95c | 2011-03-08 07:17:44 +0000 | [diff] [blame] | 560 | return 0; |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 561 | } |
Carl-Daniel Hailfinger | 11990da | 2013-07-13 23:21:05 +0000 | [diff] [blame] | 562 | |
| 563 | int setup_cpu_msr(int cpu) |
| 564 | { |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | void cleanup_cpu_msr(void) |
| 569 | { |
| 570 | } |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 571 | #else |
Stefan Tauner | 6bf5fe7 | 2013-07-13 21:18:32 +0000 | [diff] [blame] | 572 | /* default MSR implementation */ |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 573 | msr_t rdmsr(int addr) |
| 574 | { |
| 575 | msr_t ret = { 0xffffffff, 0xffffffff }; |
| 576 | |
| 577 | return ret; |
| 578 | } |
| 579 | |
| 580 | int wrmsr(int addr, msr_t msr) |
| 581 | { |
| 582 | return -1; |
| 583 | } |
| 584 | |
| 585 | int setup_cpu_msr(int cpu) |
| 586 | { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 587 | msg_pinfo("No MSR support for your OS yet.\n"); |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 588 | return -1; |
| 589 | } |
| 590 | |
| 591 | void cleanup_cpu_msr(void) |
| 592 | { |
| 593 | // Nothing, yet. |
| 594 | } |
Stefan Tauner | 6bf5fe7 | 2013-07-13 21:18:32 +0000 | [diff] [blame] | 595 | #endif // OS switches for MSR code |
| 596 | #else // x86 |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 597 | /* Does MSR exist on non-x86 architectures? */ |
Stefan Tauner | 6bf5fe7 | 2013-07-13 21:18:32 +0000 | [diff] [blame] | 598 | #endif // arch switches for MSR code |