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> |
| 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; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 19 | |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 20 | #include <sys/types.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <errno.h> |
| 25 | #include "flash.h" |
| 26 | |
Stefan Reinauer | f79edb9 | 2009-01-26 01:23:31 +0000 | [diff] [blame] | 27 | #ifdef __DARWIN__ |
| 28 | #include <DirectIO/darwinio.h> |
| 29 | |
| 30 | #define MEM_DEV "DirectIO" |
| 31 | |
| 32 | void *sys_physmap(unsigned long phys_addr, size_t len) |
| 33 | { |
| 34 | return map_physical(phys_addr, len); |
| 35 | } |
| 36 | |
| 37 | void physunmap(void *virt_addr, size_t len) |
| 38 | { |
| 39 | unmap_physical(virt_addr, len); |
| 40 | } |
| 41 | |
| 42 | #else |
| 43 | #include <sys/mman.h> |
| 44 | |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 45 | #if defined (__sun) && (defined(__i386) || defined(__amd64)) |
| 46 | # define MEM_DEV "/dev/xsvc" |
| 47 | #else |
| 48 | # define MEM_DEV "/dev/mem" |
| 49 | #endif |
| 50 | |
| 51 | static int fd_mem = -1; |
| 52 | |
| 53 | void *sys_physmap(unsigned long phys_addr, size_t len) |
| 54 | { |
| 55 | void *virt_addr; |
| 56 | |
| 57 | if (-1 == fd_mem) { |
| 58 | /* Open the memory device UNCACHED. Important for MMIO. */ |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 59 | if (-1 == (fd_mem = open(MEM_DEV, O_RDWR | O_SYNC))) { |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 60 | perror("Critical error: open(" MEM_DEV ")"); |
Peter Stuge | 43438ba | 2009-01-26 02:04:19 +0000 | [diff] [blame] | 61 | exit(2); |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 65 | virt_addr = mmap(0, len, PROT_WRITE | PROT_READ, MAP_SHARED, |
| 66 | fd_mem, (off_t)phys_addr); |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 67 | return MAP_FAILED == virt_addr ? NULL : virt_addr; |
| 68 | } |
| 69 | |
| 70 | void physunmap(void *virt_addr, size_t len) |
| 71 | { |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 72 | if (len == 0) { |
| 73 | printf_debug("Not unmapping zero size at %p\n", virt_addr); |
| 74 | return; |
| 75 | } |
| 76 | |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 77 | munmap(virt_addr, len); |
| 78 | } |
Stefan Reinauer | f79edb9 | 2009-01-26 01:23:31 +0000 | [diff] [blame] | 79 | #endif |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 80 | |
| 81 | void *physmap(const char *descr, unsigned long phys_addr, size_t len) |
| 82 | { |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 83 | if (len == 0) { |
| 84 | printf_debug("Not mapping %s, zero size at 0x%08lx\n", |
| 85 | descr, phys_addr); |
| 86 | return NULL; |
| 87 | } |
| 88 | |
| 89 | if ((getpagesize() - 1) & len) { |
| 90 | fprintf(stderr, "Mapping %s at 0x%08lx, unaligned size 0x%lx\n", |
| 91 | descr, phys_addr, (unsigned long)len); |
| 92 | } |
| 93 | |
| 94 | if ((getpagesize() - 1) & phys_addr) { |
| 95 | fprintf(stderr, "Mapping %s, 0x%lx bytes at unaligned 0x%08lx\n", |
| 96 | descr, (unsigned long)len, phys_addr); |
| 97 | } |
| 98 | |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 99 | void *virt_addr = sys_physmap(phys_addr, len); |
| 100 | |
| 101 | if (NULL == virt_addr) { |
| 102 | if (NULL == descr) |
| 103 | descr = "memory"; |
| 104 | fprintf(stderr, "Error accessing %s, 0x%lx bytes at 0x%08lx\n", descr, (unsigned long)len, phys_addr); |
| 105 | perror(MEM_DEV " mmap failed"); |
| 106 | if (EINVAL == errno) { |
| 107 | fprintf(stderr, "In Linux this error can be caused by the CONFIG_NONPROMISC_DEVMEM (<2.6.27),\n"); |
| 108 | fprintf(stderr, "CONFIG_STRICT_DEVMEM (>=2.6.27) and CONFIG_X86_PAT kernel options.\n"); |
| 109 | fprintf(stderr, "Please check if either is enabled in your kernel before reporting a failure.\n"); |
| 110 | fprintf(stderr, "You can override CONFIG_X86_PAT at boot with the nopat kernel parameter but\n"); |
| 111 | fprintf(stderr, "disabling the other option unfortunately requires a kernel recompile. Sorry!\n"); |
| 112 | } |
Peter Stuge | 43438ba | 2009-01-26 02:04:19 +0000 | [diff] [blame] | 113 | exit(3); |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | return virt_addr; |
| 117 | } |