Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2009,2010 Carl-Daniel Hailfinger |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 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 | */ |
| 20 | |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 21 | #include "platform.h" |
Peter Lemenkov | 6282966 | 2012-12-29 19:26:55 +0000 | [diff] [blame] | 22 | |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 23 | #include <stdint.h> |
| 24 | #include <string.h> |
| 25 | #include <stdlib.h> |
Carl-Daniel Hailfinger | 11990da | 2013-07-13 23:21:05 +0000 | [diff] [blame] | 26 | #include <errno.h> |
Carl-Daniel Hailfinger | dcef67e | 2010-06-21 23:20:15 +0000 | [diff] [blame] | 27 | #include <sys/types.h> |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 28 | #if !defined (__DJGPP__) && !defined(__LIBPAYLOAD__) |
Carl-Daniel Hailfinger | 11990da | 2013-07-13 23:21:05 +0000 | [diff] [blame] | 29 | /* No file access needed/possible to get hardware access permissions. */ |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 30 | #include <unistd.h> |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 31 | #include <fcntl.h> |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 32 | #endif |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 33 | #include "flash.h" |
Patrick Georgi | 32508eb | 2012-07-20 20:35:14 +0000 | [diff] [blame] | 34 | #include "hwaccess.h" |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 35 | |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 36 | #if !(IS_LINUX || IS_MACOSX || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__DJGPP__) || defined(__LIBPAYLOAD__) || defined(__sun)) |
| 37 | #error "Unknown operating system" |
| 38 | #endif |
| 39 | |
Stefan Tauner | 95b4b6d | 2013-07-13 20:55:33 +0000 | [diff] [blame] | 40 | #define USE_IOPL (IS_LINUX || IS_MACOSX || defined(__NetBSD__) || defined(__OpenBSD__)) |
| 41 | #define USE_DEV_IO (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)) |
| 42 | |
| 43 | #if IS_X86 && USE_DEV_IO |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 44 | int io_fd; |
| 45 | #endif |
| 46 | |
Peter Lemenkov | 6282966 | 2012-12-29 19:26:55 +0000 | [diff] [blame] | 47 | /* Prevent reordering and/or merging of reads/writes to hardware. |
| 48 | * Such reordering and/or merging would break device accesses which depend on the exact access order. |
| 49 | */ |
| 50 | static inline void sync_primitive(void) |
Carl-Daniel Hailfinger | d6bb828 | 2012-07-21 17:27:08 +0000 | [diff] [blame] | 51 | { |
Stefan Tauner | fb2d77c | 2015-02-10 08:03:10 +0000 | [diff] [blame] | 52 | /* This is not needed for... |
| 53 | * - x86: uses uncached accesses which have a strongly ordered memory model. |
| 54 | * - MIPS: uses uncached accesses in mode 2 on /dev/mem which has also a strongly ordered memory model. |
| 55 | * - ARM: uses a strongly ordered memory model for device memories. |
| 56 | * |
| 57 | * See also https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/memory-barriers.txt |
Peter Lemenkov | 6282966 | 2012-12-29 19:26:55 +0000 | [diff] [blame] | 58 | */ |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 59 | #if IS_PPC // cf. http://lxr.free-electrons.com/source/arch/powerpc/include/asm/barrier.h |
Peter Lemenkov | 6282966 | 2012-12-29 19:26:55 +0000 | [diff] [blame] | 60 | asm("eieio" : : : "memory"); |
Stefan Tauner | fb2d77c | 2015-02-10 08:03:10 +0000 | [diff] [blame] | 61 | #elif IS_SPARC |
| 62 | #if defined(__sparc_v9__) || defined(__sparcv9) |
| 63 | /* Sparc V9 CPUs support three different memory orderings that range from x86-like TSO to PowerPC-like |
| 64 | * RMO. The modes can be switched at runtime thus to make sure we maintain the right order of access we |
| 65 | * use the strongest hardware memory barriers that exist on Sparc V9. */ |
| 66 | asm volatile ("membar #Sync" ::: "memory"); |
| 67 | #elif defined(__sparc_v8__) || defined(__sparcv8) |
| 68 | /* On SPARC V8 there is no RMO just PSO and that does not apply to I/O accesses... but if V8 code is run |
| 69 | * on V9 CPUs it might apply... or not... we issue a write barrier anyway. That's the most suitable |
| 70 | * operation in the V8 instruction set anyway. If you know better then please tell us. */ |
| 71 | asm volatile ("stbar"); |
| 72 | #else |
| 73 | #error Unknown and/or unsupported SPARC instruction set version detected. |
| 74 | #endif |
Peter Lemenkov | 6282966 | 2012-12-29 19:26:55 +0000 | [diff] [blame] | 75 | #endif |
| 76 | } |
| 77 | |
Carl-Daniel Hailfinger | 8225868 | 2013-01-08 22:49:12 +0000 | [diff] [blame] | 78 | #if IS_X86 && !(defined(__DJGPP__) || defined(__LIBPAYLOAD__)) |
Peter Lemenkov | 6282966 | 2012-12-29 19:26:55 +0000 | [diff] [blame] | 79 | static int release_io_perms(void *p) |
| 80 | { |
Carl-Daniel Hailfinger | 8225868 | 2013-01-08 22:49:12 +0000 | [diff] [blame] | 81 | #if defined (__sun) |
Carl-Daniel Hailfinger | d6bb828 | 2012-07-21 17:27:08 +0000 | [diff] [blame] | 82 | sysi86(SI86V86, V86SC_IOPL, 0); |
Stefan Tauner | 95b4b6d | 2013-07-13 20:55:33 +0000 | [diff] [blame] | 83 | #elif USE_DEV_IO |
Carl-Daniel Hailfinger | d6bb828 | 2012-07-21 17:27:08 +0000 | [diff] [blame] | 84 | close(io_fd); |
Stefan Tauner | 95b4b6d | 2013-07-13 20:55:33 +0000 | [diff] [blame] | 85 | #elif USE_IOPL |
Carl-Daniel Hailfinger | d6bb828 | 2012-07-21 17:27:08 +0000 | [diff] [blame] | 86 | iopl(0); |
| 87 | #endif |
Carl-Daniel Hailfinger | d6bb828 | 2012-07-21 17:27:08 +0000 | [diff] [blame] | 88 | return 0; |
| 89 | } |
Carl-Daniel Hailfinger | 8225868 | 2013-01-08 22:49:12 +0000 | [diff] [blame] | 90 | #endif |
Carl-Daniel Hailfinger | d6bb828 | 2012-07-21 17:27:08 +0000 | [diff] [blame] | 91 | |
| 92 | /* Get I/O permissions with automatic permission release on shutdown. */ |
| 93 | int rget_io_perms(void) |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 94 | { |
Carl-Daniel Hailfinger | 8225868 | 2013-01-08 22:49:12 +0000 | [diff] [blame] | 95 | #if IS_X86 && !(defined(__DJGPP__) || defined(__LIBPAYLOAD__)) |
| 96 | #if defined (__sun) |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 97 | if (sysi86(SI86V86, V86SC_IOPL, PS_IOPL) != 0) { |
Stefan Tauner | 95b4b6d | 2013-07-13 20:55:33 +0000 | [diff] [blame] | 98 | #elif USE_DEV_IO |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 99 | if ((io_fd = open("/dev/io", O_RDWR)) < 0) { |
Stefan Tauner | 95b4b6d | 2013-07-13 20:55:33 +0000 | [diff] [blame] | 100 | #elif USE_IOPL |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 101 | if (iopl(3) != 0) { |
| 102 | #endif |
Stefan Tauner | 95b4b6d | 2013-07-13 20:55:33 +0000 | [diff] [blame] | 103 | msg_perr("ERROR: Could not get I/O privileges (%s).\n", strerror(errno)); |
| 104 | msg_perr("You need to be root.\n"); |
Carl-Daniel Hailfinger | b63b067 | 2010-07-02 17:12:50 +0000 | [diff] [blame] | 105 | #if defined (__OpenBSD__) |
Stefan Tauner | 95b4b6d | 2013-07-13 20:55:33 +0000 | [diff] [blame] | 106 | msg_perr("If you are root already please set securelevel=-1 in /etc/rc.securelevel and\n" |
| 107 | "reboot, or reboot into single user mode.\n"); |
| 108 | #elif defined(__NetBSD__) |
| 109 | msg_perr("If you are root already please reboot into single user mode or make sure\n" |
| 110 | "that your kernel configuration has the option INSECURE enabled.\n"); |
Carl-Daniel Hailfinger | b63b067 | 2010-07-02 17:12:50 +0000 | [diff] [blame] | 111 | #endif |
Carl-Daniel Hailfinger | d6bb828 | 2012-07-21 17:27:08 +0000 | [diff] [blame] | 112 | return 1; |
| 113 | } else { |
| 114 | register_shutdown(release_io_perms, NULL); |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 115 | } |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 116 | #else |
Carl-Daniel Hailfinger | 8225868 | 2013-01-08 22:49:12 +0000 | [diff] [blame] | 117 | /* DJGPP and libpayload environments have full PCI port I/O permissions by default. */ |
| 118 | /* PCI port I/O support is unimplemented on PPC/MIPS and unavailable on ARM. */ |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 119 | #endif |
Peter Lemenkov | 6282966 | 2012-12-29 19:26:55 +0000 | [diff] [blame] | 120 | return 0; |
| 121 | } |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 122 | |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 123 | void mmio_writeb(uint8_t val, void *addr) |
| 124 | { |
| 125 | *(volatile uint8_t *) addr = val; |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 126 | sync_primitive(); |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void mmio_writew(uint16_t val, void *addr) |
| 130 | { |
| 131 | *(volatile uint16_t *) addr = val; |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 132 | sync_primitive(); |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void mmio_writel(uint32_t val, void *addr) |
| 136 | { |
| 137 | *(volatile uint32_t *) addr = val; |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 138 | sync_primitive(); |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | uint8_t mmio_readb(void *addr) |
| 142 | { |
| 143 | return *(volatile uint8_t *) addr; |
| 144 | } |
| 145 | |
| 146 | uint16_t mmio_readw(void *addr) |
| 147 | { |
| 148 | return *(volatile uint16_t *) addr; |
| 149 | } |
| 150 | |
| 151 | uint32_t mmio_readl(void *addr) |
| 152 | { |
| 153 | return *(volatile uint32_t *) addr; |
| 154 | } |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 155 | |
Carl-Daniel Hailfinger | ccd71c2 | 2012-03-01 22:38:27 +0000 | [diff] [blame] | 156 | void mmio_readn(void *addr, uint8_t *buf, size_t len) |
| 157 | { |
| 158 | memcpy(buf, addr, len); |
| 159 | return; |
| 160 | } |
| 161 | |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 162 | void mmio_le_writeb(uint8_t val, void *addr) |
| 163 | { |
| 164 | mmio_writeb(cpu_to_le8(val), addr); |
| 165 | } |
| 166 | |
| 167 | void mmio_le_writew(uint16_t val, void *addr) |
| 168 | { |
| 169 | mmio_writew(cpu_to_le16(val), addr); |
| 170 | } |
| 171 | |
| 172 | void mmio_le_writel(uint32_t val, void *addr) |
| 173 | { |
| 174 | mmio_writel(cpu_to_le32(val), addr); |
| 175 | } |
| 176 | |
| 177 | uint8_t mmio_le_readb(void *addr) |
| 178 | { |
| 179 | return le_to_cpu8(mmio_readb(addr)); |
| 180 | } |
| 181 | |
| 182 | uint16_t mmio_le_readw(void *addr) |
| 183 | { |
| 184 | return le_to_cpu16(mmio_readw(addr)); |
| 185 | } |
| 186 | |
| 187 | uint32_t mmio_le_readl(void *addr) |
| 188 | { |
| 189 | return le_to_cpu32(mmio_readl(addr)); |
| 190 | } |
Carl-Daniel Hailfinger | 54ce73a | 2011-05-03 21:49:41 +0000 | [diff] [blame] | 191 | |
| 192 | enum mmio_write_type { |
| 193 | mmio_write_type_b, |
| 194 | mmio_write_type_w, |
| 195 | mmio_write_type_l, |
| 196 | }; |
| 197 | |
| 198 | struct undo_mmio_write_data { |
| 199 | void *addr; |
| 200 | int reg; |
| 201 | enum mmio_write_type type; |
| 202 | union { |
| 203 | uint8_t bdata; |
| 204 | uint16_t wdata; |
| 205 | uint32_t ldata; |
| 206 | }; |
| 207 | }; |
| 208 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 209 | int undo_mmio_write(void *p) |
Carl-Daniel Hailfinger | 54ce73a | 2011-05-03 21:49:41 +0000 | [diff] [blame] | 210 | { |
| 211 | struct undo_mmio_write_data *data = p; |
| 212 | msg_pdbg("Restoring MMIO space at %p\n", data->addr); |
| 213 | switch (data->type) { |
| 214 | case mmio_write_type_b: |
| 215 | mmio_writeb(data->bdata, data->addr); |
| 216 | break; |
| 217 | case mmio_write_type_w: |
| 218 | mmio_writew(data->wdata, data->addr); |
| 219 | break; |
| 220 | case mmio_write_type_l: |
| 221 | mmio_writel(data->ldata, data->addr); |
| 222 | break; |
| 223 | } |
| 224 | /* p was allocated in register_undo_mmio_write. */ |
| 225 | free(p); |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 226 | return 0; |
Carl-Daniel Hailfinger | 54ce73a | 2011-05-03 21:49:41 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | #define register_undo_mmio_write(a, c) \ |
| 230 | { \ |
| 231 | struct undo_mmio_write_data *undo_mmio_write_data; \ |
| 232 | undo_mmio_write_data = malloc(sizeof(struct undo_mmio_write_data)); \ |
Stefan Tauner | 269de35 | 2011-07-12 22:35:21 +0000 | [diff] [blame] | 233 | if (!undo_mmio_write_data) { \ |
| 234 | msg_gerr("Out of memory!\n"); \ |
| 235 | exit(1); \ |
| 236 | } \ |
Carl-Daniel Hailfinger | 54ce73a | 2011-05-03 21:49:41 +0000 | [diff] [blame] | 237 | undo_mmio_write_data->addr = a; \ |
| 238 | undo_mmio_write_data->type = mmio_write_type_##c; \ |
| 239 | undo_mmio_write_data->c##data = mmio_read##c(a); \ |
| 240 | register_shutdown(undo_mmio_write, undo_mmio_write_data); \ |
| 241 | } |
| 242 | |
| 243 | #define register_undo_mmio_writeb(a) register_undo_mmio_write(a, b) |
| 244 | #define register_undo_mmio_writew(a) register_undo_mmio_write(a, w) |
| 245 | #define register_undo_mmio_writel(a) register_undo_mmio_write(a, l) |
| 246 | |
| 247 | void rmmio_writeb(uint8_t val, void *addr) |
| 248 | { |
| 249 | register_undo_mmio_writeb(addr); |
| 250 | mmio_writeb(val, addr); |
| 251 | } |
| 252 | |
| 253 | void rmmio_writew(uint16_t val, void *addr) |
| 254 | { |
| 255 | register_undo_mmio_writew(addr); |
| 256 | mmio_writew(val, addr); |
| 257 | } |
| 258 | |
| 259 | void rmmio_writel(uint32_t val, void *addr) |
| 260 | { |
| 261 | register_undo_mmio_writel(addr); |
| 262 | mmio_writel(val, addr); |
| 263 | } |
| 264 | |
| 265 | void rmmio_le_writeb(uint8_t val, void *addr) |
| 266 | { |
| 267 | register_undo_mmio_writeb(addr); |
| 268 | mmio_le_writeb(val, addr); |
| 269 | } |
| 270 | |
| 271 | void rmmio_le_writew(uint16_t val, void *addr) |
| 272 | { |
| 273 | register_undo_mmio_writew(addr); |
| 274 | mmio_le_writew(val, addr); |
| 275 | } |
| 276 | |
| 277 | void rmmio_le_writel(uint32_t val, void *addr) |
| 278 | { |
| 279 | register_undo_mmio_writel(addr); |
| 280 | mmio_le_writel(val, addr); |
| 281 | } |
| 282 | |
| 283 | void rmmio_valb(void *addr) |
| 284 | { |
| 285 | register_undo_mmio_writeb(addr); |
| 286 | } |
| 287 | |
| 288 | void rmmio_valw(void *addr) |
| 289 | { |
| 290 | register_undo_mmio_writew(addr); |
| 291 | } |
| 292 | |
| 293 | void rmmio_vall(void *addr) |
| 294 | { |
| 295 | register_undo_mmio_writel(addr); |
| 296 | } |