blob: 49e1bfc6491f68140f489449e651acbaf18c2446 [file] [log] [blame]
Peter Stuge33fefca2009-01-26 01:33:02 +00001/*
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 Hermann7b2969b2009-04-15 10:52:49 +000019
Stefan Reinauer0593f212009-01-26 01:10:48 +000020#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 Reinauerf79edb92009-01-26 01:23:31 +000027#ifdef __DARWIN__
28#include <DirectIO/darwinio.h>
29
30#define MEM_DEV "DirectIO"
31
32void *sys_physmap(unsigned long phys_addr, size_t len)
33{
34 return map_physical(phys_addr, len);
35}
36
37void 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 Reinauer0593f212009-01-26 01:10:48 +000045#if defined (__sun) && (defined(__i386) || defined(__amd64))
46# define MEM_DEV "/dev/xsvc"
47#else
48# define MEM_DEV "/dev/mem"
49#endif
50
51static int fd_mem = -1;
52
53void *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 Hermann7b2969b2009-04-15 10:52:49 +000059 if (-1 == (fd_mem = open(MEM_DEV, O_RDWR | O_SYNC))) {
Stefan Reinauer0593f212009-01-26 01:10:48 +000060 perror("Critical error: open(" MEM_DEV ")");
Peter Stuge43438ba2009-01-26 02:04:19 +000061 exit(2);
Stefan Reinauer0593f212009-01-26 01:10:48 +000062 }
63 }
64
Uwe Hermann7b2969b2009-04-15 10:52:49 +000065 virt_addr = mmap(0, len, PROT_WRITE | PROT_READ, MAP_SHARED,
66 fd_mem, (off_t)phys_addr);
Stefan Reinauer0593f212009-01-26 01:10:48 +000067 return MAP_FAILED == virt_addr ? NULL : virt_addr;
68}
69
70void physunmap(void *virt_addr, size_t len)
71{
72 munmap(virt_addr, len);
73}
Stefan Reinauerf79edb92009-01-26 01:23:31 +000074#endif
Stefan Reinauer0593f212009-01-26 01:10:48 +000075
76void *physmap(const char *descr, unsigned long phys_addr, size_t len)
77{
78 void *virt_addr = sys_physmap(phys_addr, len);
79
80 if (NULL == virt_addr) {
81 if (NULL == descr)
82 descr = "memory";
83 fprintf(stderr, "Error accessing %s, 0x%lx bytes at 0x%08lx\n", descr, (unsigned long)len, phys_addr);
84 perror(MEM_DEV " mmap failed");
85 if (EINVAL == errno) {
86 fprintf(stderr, "In Linux this error can be caused by the CONFIG_NONPROMISC_DEVMEM (<2.6.27),\n");
87 fprintf(stderr, "CONFIG_STRICT_DEVMEM (>=2.6.27) and CONFIG_X86_PAT kernel options.\n");
88 fprintf(stderr, "Please check if either is enabled in your kernel before reporting a failure.\n");
89 fprintf(stderr, "You can override CONFIG_X86_PAT at boot with the nopat kernel parameter but\n");
90 fprintf(stderr, "disabling the other option unfortunately requires a kernel recompile. Sorry!\n");
91 }
Peter Stuge43438ba2009-01-26 02:04:19 +000092 exit(3);
Stefan Reinauer0593f212009-01-26 01:10:48 +000093 }
94
95 return virt_addr;
96}