blob: 65d25698c8bececb282e7414bb8195d0b85c9c90 [file] [log] [blame]
Stefan Reinauer0593f212009-01-26 01:10:48 +00001#include <sys/mman.h>
2#include <sys/types.h>
3#include <sys/stat.h>
4#include <fcntl.h>
5#include <stdlib.h>
6#include <errno.h>
7#include "flash.h"
8
9#if defined (__sun) && (defined(__i386) || defined(__amd64))
10# define MEM_DEV "/dev/xsvc"
11#else
12# define MEM_DEV "/dev/mem"
13#endif
14
15static int fd_mem = -1;
16
17void *sys_physmap(unsigned long phys_addr, size_t len)
18{
19 void *virt_addr;
20
21 if (-1 == fd_mem) {
22 /* Open the memory device UNCACHED. Important for MMIO. */
23 if (-1 == (fd_mem = open(MEM_DEV, O_RDWR|O_SYNC))) {
24 perror("Critical error: open(" MEM_DEV ")");
25 exit(1);
26 }
27 }
28
29 virt_addr = mmap(0, len, PROT_WRITE|PROT_READ, MAP_SHARED, fd_mem, (off_t)phys_addr);
30 return MAP_FAILED == virt_addr ? NULL : virt_addr;
31}
32
33void physunmap(void *virt_addr, size_t len)
34{
35 munmap(virt_addr, len);
36}
37
38void *physmap(const char *descr, unsigned long phys_addr, size_t len)
39{
40 void *virt_addr = sys_physmap(phys_addr, len);
41
42 if (NULL == virt_addr) {
43 if (NULL == descr)
44 descr = "memory";
45 fprintf(stderr, "Error accessing %s, 0x%lx bytes at 0x%08lx\n", descr, (unsigned long)len, phys_addr);
46 perror(MEM_DEV " mmap failed");
47 if (EINVAL == errno) {
48 fprintf(stderr, "In Linux this error can be caused by the CONFIG_NONPROMISC_DEVMEM (<2.6.27),\n");
49 fprintf(stderr, "CONFIG_STRICT_DEVMEM (>=2.6.27) and CONFIG_X86_PAT kernel options.\n");
50 fprintf(stderr, "Please check if either is enabled in your kernel before reporting a failure.\n");
51 fprintf(stderr, "You can override CONFIG_X86_PAT at boot with the nopat kernel parameter but\n");
52 fprintf(stderr, "disabling the other option unfortunately requires a kernel recompile. Sorry!\n");
53 }
54 exit(1);
55 }
56
57 return virt_addr;
58}