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 | |
| 21 | #include <stdint.h> |
| 22 | #include <string.h> |
| 23 | #include <stdlib.h> |
Carl-Daniel Hailfinger | dcef67e | 2010-06-21 23:20:15 +0000 | [diff] [blame] | 24 | #include <sys/types.h> |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 25 | #if !defined (__DJGPP__) && !defined(__LIBPAYLOAD__) |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 26 | #include <unistd.h> |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 27 | #include <fcntl.h> |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 28 | #endif |
| 29 | #if !defined (__DJGPP__) |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 30 | #include <errno.h> |
Carl-Daniel Hailfinger | dcef67e | 2010-06-21 23:20:15 +0000 | [diff] [blame] | 31 | #endif |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 32 | #include "flash.h" |
| 33 | |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 34 | #if defined(__i386__) || defined(__x86_64__) |
| 35 | |
| 36 | /* sync primitive is not needed because x86 uses uncached accesses |
| 37 | * which have a strongly ordered memory model. |
| 38 | */ |
| 39 | static inline void sync_primitive(void) |
| 40 | { |
| 41 | } |
| 42 | |
Carl-Daniel Hailfinger | a5eecda | 2012-02-25 22:50:21 +0000 | [diff] [blame] | 43 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 44 | int io_fd; |
| 45 | #endif |
| 46 | |
| 47 | void get_io_perms(void) |
| 48 | { |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 49 | #if defined(__DJGPP__) || defined(__LIBPAYLOAD__) |
Carl-Daniel Hailfinger | dcef67e | 2010-06-21 23:20:15 +0000 | [diff] [blame] | 50 | /* We have full permissions by default. */ |
| 51 | return; |
| 52 | #else |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 53 | #if defined (__sun) && (defined(__i386) || defined(__amd64)) |
| 54 | if (sysi86(SI86V86, V86SC_IOPL, PS_IOPL) != 0) { |
Carl-Daniel Hailfinger | a5eecda | 2012-02-25 22:50:21 +0000 | [diff] [blame] | 55 | #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined (__DragonFly__) |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 56 | if ((io_fd = open("/dev/io", O_RDWR)) < 0) { |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 57 | #else |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 58 | if (iopl(3) != 0) { |
| 59 | #endif |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 60 | msg_perr("ERROR: Could not get I/O privileges (%s).\n" |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 61 | "You need to be root.\n", strerror(errno)); |
Carl-Daniel Hailfinger | b63b067 | 2010-07-02 17:12:50 +0000 | [diff] [blame] | 62 | #if defined (__OpenBSD__) |
| 63 | msg_perr("Please set securelevel=-1 in /etc/rc.securelevel " |
| 64 | "and reboot, or reboot into \n"); |
| 65 | msg_perr("single user mode.\n"); |
| 66 | #endif |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 67 | exit(1); |
| 68 | } |
Carl-Daniel Hailfinger | dcef67e | 2010-06-21 23:20:15 +0000 | [diff] [blame] | 69 | #endif |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void release_io_perms(void) |
| 73 | { |
Carl-Daniel Hailfinger | a5eecda | 2012-02-25 22:50:21 +0000 | [diff] [blame] | 74 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 75 | close(io_fd); |
| 76 | #endif |
| 77 | } |
| 78 | |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 79 | #elif defined(__powerpc__) || defined(__powerpc64__) || defined(__ppc__) || defined(__ppc64__) |
| 80 | |
| 81 | static inline void sync_primitive(void) |
| 82 | { |
| 83 | /* Prevent reordering and/or merging of reads/writes to hardware. |
| 84 | * Such reordering and/or merging would break device accesses which |
| 85 | * depend on the exact access order. |
| 86 | */ |
| 87 | asm("eieio" : : : "memory"); |
| 88 | } |
| 89 | |
| 90 | /* PCI port I/O is not yet implemented on PowerPC. */ |
| 91 | void get_io_perms(void) |
| 92 | { |
| 93 | } |
| 94 | |
| 95 | /* PCI port I/O is not yet implemented on PowerPC. */ |
| 96 | void release_io_perms(void) |
| 97 | { |
| 98 | } |
| 99 | |
| 100 | #elif defined (__mips) || defined (__mips__) || defined (_mips) || defined (mips) |
| 101 | |
| 102 | /* sync primitive is not needed because /dev/mem on MIPS uses uncached accesses |
| 103 | * in mode 2 which has a strongly ordered memory model. |
| 104 | */ |
| 105 | static inline void sync_primitive(void) |
| 106 | { |
| 107 | } |
| 108 | |
| 109 | /* PCI port I/O is not yet implemented on MIPS. */ |
| 110 | void get_io_perms(void) |
| 111 | { |
| 112 | } |
| 113 | |
| 114 | /* PCI port I/O is not yet implemented on MIPS. */ |
| 115 | void release_io_perms(void) |
| 116 | { |
| 117 | } |
| 118 | |
David Hendricks | b286da7 | 2012-02-13 00:35:35 +0000 | [diff] [blame] | 119 | #elif defined (__arm__) |
| 120 | |
| 121 | static inline void sync_primitive(void) |
| 122 | { |
| 123 | } |
| 124 | |
| 125 | void get_io_perms(void) |
| 126 | { |
| 127 | } |
| 128 | |
| 129 | void release_io_perms(void) |
| 130 | { |
| 131 | } |
| 132 | |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 133 | #else |
| 134 | |
| 135 | #error Unknown architecture |
| 136 | |
| 137 | #endif |
| 138 | |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 139 | void mmio_writeb(uint8_t val, void *addr) |
| 140 | { |
| 141 | *(volatile uint8_t *) addr = val; |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 142 | sync_primitive(); |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void mmio_writew(uint16_t val, void *addr) |
| 146 | { |
| 147 | *(volatile uint16_t *) addr = val; |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 148 | sync_primitive(); |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void mmio_writel(uint32_t val, void *addr) |
| 152 | { |
| 153 | *(volatile uint32_t *) addr = val; |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 154 | sync_primitive(); |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | uint8_t mmio_readb(void *addr) |
| 158 | { |
| 159 | return *(volatile uint8_t *) addr; |
| 160 | } |
| 161 | |
| 162 | uint16_t mmio_readw(void *addr) |
| 163 | { |
| 164 | return *(volatile uint16_t *) addr; |
| 165 | } |
| 166 | |
| 167 | uint32_t mmio_readl(void *addr) |
| 168 | { |
| 169 | return *(volatile uint32_t *) addr; |
| 170 | } |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 171 | |
Carl-Daniel Hailfinger | ccd71c2 | 2012-03-01 22:38:27 +0000 | [diff] [blame] | 172 | void mmio_readn(void *addr, uint8_t *buf, size_t len) |
| 173 | { |
| 174 | memcpy(buf, addr, len); |
| 175 | return; |
| 176 | } |
| 177 | |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 178 | void mmio_le_writeb(uint8_t val, void *addr) |
| 179 | { |
| 180 | mmio_writeb(cpu_to_le8(val), addr); |
| 181 | } |
| 182 | |
| 183 | void mmio_le_writew(uint16_t val, void *addr) |
| 184 | { |
| 185 | mmio_writew(cpu_to_le16(val), addr); |
| 186 | } |
| 187 | |
| 188 | void mmio_le_writel(uint32_t val, void *addr) |
| 189 | { |
| 190 | mmio_writel(cpu_to_le32(val), addr); |
| 191 | } |
| 192 | |
| 193 | uint8_t mmio_le_readb(void *addr) |
| 194 | { |
| 195 | return le_to_cpu8(mmio_readb(addr)); |
| 196 | } |
| 197 | |
| 198 | uint16_t mmio_le_readw(void *addr) |
| 199 | { |
| 200 | return le_to_cpu16(mmio_readw(addr)); |
| 201 | } |
| 202 | |
| 203 | uint32_t mmio_le_readl(void *addr) |
| 204 | { |
| 205 | return le_to_cpu32(mmio_readl(addr)); |
| 206 | } |
Carl-Daniel Hailfinger | 54ce73a | 2011-05-03 21:49:41 +0000 | [diff] [blame] | 207 | |
| 208 | enum mmio_write_type { |
| 209 | mmio_write_type_b, |
| 210 | mmio_write_type_w, |
| 211 | mmio_write_type_l, |
| 212 | }; |
| 213 | |
| 214 | struct undo_mmio_write_data { |
| 215 | void *addr; |
| 216 | int reg; |
| 217 | enum mmio_write_type type; |
| 218 | union { |
| 219 | uint8_t bdata; |
| 220 | uint16_t wdata; |
| 221 | uint32_t ldata; |
| 222 | }; |
| 223 | }; |
| 224 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 225 | int undo_mmio_write(void *p) |
Carl-Daniel Hailfinger | 54ce73a | 2011-05-03 21:49:41 +0000 | [diff] [blame] | 226 | { |
| 227 | struct undo_mmio_write_data *data = p; |
| 228 | msg_pdbg("Restoring MMIO space at %p\n", data->addr); |
| 229 | switch (data->type) { |
| 230 | case mmio_write_type_b: |
| 231 | mmio_writeb(data->bdata, data->addr); |
| 232 | break; |
| 233 | case mmio_write_type_w: |
| 234 | mmio_writew(data->wdata, data->addr); |
| 235 | break; |
| 236 | case mmio_write_type_l: |
| 237 | mmio_writel(data->ldata, data->addr); |
| 238 | break; |
| 239 | } |
| 240 | /* p was allocated in register_undo_mmio_write. */ |
| 241 | free(p); |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 242 | return 0; |
Carl-Daniel Hailfinger | 54ce73a | 2011-05-03 21:49:41 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | #define register_undo_mmio_write(a, c) \ |
| 246 | { \ |
| 247 | struct undo_mmio_write_data *undo_mmio_write_data; \ |
| 248 | undo_mmio_write_data = malloc(sizeof(struct undo_mmio_write_data)); \ |
Stefan Tauner | 269de35 | 2011-07-12 22:35:21 +0000 | [diff] [blame] | 249 | if (!undo_mmio_write_data) { \ |
| 250 | msg_gerr("Out of memory!\n"); \ |
| 251 | exit(1); \ |
| 252 | } \ |
Carl-Daniel Hailfinger | 54ce73a | 2011-05-03 21:49:41 +0000 | [diff] [blame] | 253 | undo_mmio_write_data->addr = a; \ |
| 254 | undo_mmio_write_data->type = mmio_write_type_##c; \ |
| 255 | undo_mmio_write_data->c##data = mmio_read##c(a); \ |
| 256 | register_shutdown(undo_mmio_write, undo_mmio_write_data); \ |
| 257 | } |
| 258 | |
| 259 | #define register_undo_mmio_writeb(a) register_undo_mmio_write(a, b) |
| 260 | #define register_undo_mmio_writew(a) register_undo_mmio_write(a, w) |
| 261 | #define register_undo_mmio_writel(a) register_undo_mmio_write(a, l) |
| 262 | |
| 263 | void rmmio_writeb(uint8_t val, void *addr) |
| 264 | { |
| 265 | register_undo_mmio_writeb(addr); |
| 266 | mmio_writeb(val, addr); |
| 267 | } |
| 268 | |
| 269 | void rmmio_writew(uint16_t val, void *addr) |
| 270 | { |
| 271 | register_undo_mmio_writew(addr); |
| 272 | mmio_writew(val, addr); |
| 273 | } |
| 274 | |
| 275 | void rmmio_writel(uint32_t val, void *addr) |
| 276 | { |
| 277 | register_undo_mmio_writel(addr); |
| 278 | mmio_writel(val, addr); |
| 279 | } |
| 280 | |
| 281 | void rmmio_le_writeb(uint8_t val, void *addr) |
| 282 | { |
| 283 | register_undo_mmio_writeb(addr); |
| 284 | mmio_le_writeb(val, addr); |
| 285 | } |
| 286 | |
| 287 | void rmmio_le_writew(uint16_t val, void *addr) |
| 288 | { |
| 289 | register_undo_mmio_writew(addr); |
| 290 | mmio_le_writew(val, addr); |
| 291 | } |
| 292 | |
| 293 | void rmmio_le_writel(uint32_t val, void *addr) |
| 294 | { |
| 295 | register_undo_mmio_writel(addr); |
| 296 | mmio_le_writel(val, addr); |
| 297 | } |
| 298 | |
| 299 | void rmmio_valb(void *addr) |
| 300 | { |
| 301 | register_undo_mmio_writeb(addr); |
| 302 | } |
| 303 | |
| 304 | void rmmio_valw(void *addr) |
| 305 | { |
| 306 | register_undo_mmio_writew(addr); |
| 307 | } |
| 308 | |
| 309 | void rmmio_vall(void *addr) |
| 310 | { |
| 311 | register_undo_mmio_writel(addr); |
| 312 | } |