physmap.c 3.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * This file is part of the flashrom project.
 *
 * Copyright (C) 2009 Peter Stuge <peter@stuge.se>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */
19

20 21 22 23 24 25 26
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include "flash.h"

Stefan Reinauer's avatar
Stefan Reinauer committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#ifdef __DARWIN__
#include <DirectIO/darwinio.h>

#define MEM_DEV "DirectIO"

void *sys_physmap(unsigned long phys_addr, size_t len)
{
	return map_physical(phys_addr, len);
}

void physunmap(void *virt_addr, size_t len)
{
	unmap_physical(virt_addr, len);
}

#else
#include <sys/mman.h>

45 46 47 48 49 50 51 52 53 54 55 56 57 58
#if defined (__sun) && (defined(__i386) || defined(__amd64))
#  define MEM_DEV "/dev/xsvc"
#else
#  define MEM_DEV "/dev/mem"
#endif

static int fd_mem = -1;

void *sys_physmap(unsigned long phys_addr, size_t len)
{
	void *virt_addr;

	if (-1 == fd_mem) {
		/* Open the memory device UNCACHED. Important for MMIO. */
59
		if (-1 == (fd_mem = open(MEM_DEV, O_RDWR | O_SYNC))) {
60
			perror("Critical error: open(" MEM_DEV ")");
61
			exit(2);
62 63 64
		}
	}

65 66
	virt_addr = mmap(0, len, PROT_WRITE | PROT_READ, MAP_SHARED,
			 fd_mem, (off_t)phys_addr);
67 68 69 70 71
	return MAP_FAILED == virt_addr ? NULL : virt_addr;
}

void physunmap(void *virt_addr, size_t len)
{
72 73 74 75 76
	if (len == 0) {
		printf_debug("Not unmapping zero size at %p\n", virt_addr);
		return;
	}
		
77 78
	munmap(virt_addr, len);
}
Stefan Reinauer's avatar
Stefan Reinauer committed
79
#endif
80 81 82

void *physmap(const char *descr, unsigned long phys_addr, size_t len)
{
83 84
	void *virt_addr;

85
	if (len == 0) {
86 87
		printf_debug("Not mapping %s, zero size at 0x%08lx.\n",
			     descr, phys_addr);
88 89 90 91
		return NULL;
	}
		
	if ((getpagesize() - 1) & len) {
92
		fprintf(stderr, "Mapping %s at 0x%08lx, unaligned size 0x%lx.\n",
93 94 95 96
			descr, phys_addr, (unsigned long)len);
	}

	if ((getpagesize() - 1) & phys_addr) {
97
		fprintf(stderr, "Mapping %s, 0x%lx bytes at unaligned 0x%08lx.\n",
98 99 100
			descr, (unsigned long)len, phys_addr);
	}

101
	virt_addr = sys_physmap(phys_addr, len);
102 103 104 105 106 107 108 109 110 111 112 113 114

	if (NULL == virt_addr) {
		if (NULL == descr)
			descr = "memory";
		fprintf(stderr, "Error accessing %s, 0x%lx bytes at 0x%08lx\n", descr, (unsigned long)len, phys_addr);
		perror(MEM_DEV " mmap failed");
		if (EINVAL == errno) {
			fprintf(stderr, "In Linux this error can be caused by the CONFIG_NONPROMISC_DEVMEM (<2.6.27),\n");
			fprintf(stderr, "CONFIG_STRICT_DEVMEM (>=2.6.27) and CONFIG_X86_PAT kernel options.\n");
			fprintf(stderr, "Please check if either is enabled in your kernel before reporting a failure.\n");
			fprintf(stderr, "You can override CONFIG_X86_PAT at boot with the nopat kernel parameter but\n");
			fprintf(stderr, "disabling the other option unfortunately requires a kernel recompile. Sorry!\n");
		}
115
		exit(3);
116 117 118 119
	}

	return virt_addr;
}