blob: e3075a8121fd250f55dd26f473a6bd868077be23 [file] [log] [blame]
Philipp Deppenwiesed8fe4432016-03-18 00:52:54 +01001/* intelmetool
2 *
3 * Copyright (C) 2013-2015 Damien Zammit <damien@zamaudio.com>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
Damien Zammitf0a91282019-02-23 12:38:15 +11008 * the License, or (at your option) any later version.
Philipp Deppenwiesed8fe4432016-03-18 00:52:54 +01009 *
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
16#include "mmap.h"
17#include <errno.h>
Paul Wise3c026992017-05-04 14:12:21 +080018#include <string.h>
Philipp Deppenwiesed8fe4432016-03-18 00:52:54 +010019
20#ifndef __DARWIN__
21int fd_mem;
22
Maximilian Schanderdf5b83f2017-10-28 18:33:07 +020023void *map_physical_exact(off_t phys_addr, void *mapto, size_t len)
24{
25 void *virt_addr;
26 int err;
Philipp Deppenwiesed8fe4432016-03-18 00:52:54 +010027
Maximilian Schanderdf5b83f2017-10-28 18:33:07 +020028 virt_addr = mmap(mapto, len, PROT_WRITE | PROT_READ,
29 MAP_SHARED | MAP_FIXED, fd_mem, phys_addr);
Philipp Deppenwiesed8fe4432016-03-18 00:52:54 +010030
Maximilian Schanderdf5b83f2017-10-28 18:33:07 +020031 if (virt_addr == MAP_FAILED) {
32 err = errno;
33 printf("Error mapping physical memory 0x%016jd [0x%zx] ERRNO=%d %s\n",
34 (intmax_t)phys_addr, len, err, strerror(err));
35 return NULL;
36 }
Philipp Deppenwiesed8fe4432016-03-18 00:52:54 +010037
Maximilian Schanderdf5b83f2017-10-28 18:33:07 +020038 return virt_addr;
Philipp Deppenwiesed8fe4432016-03-18 00:52:54 +010039}
40
Maximilian Schanderdf5b83f2017-10-28 18:33:07 +020041void *map_physical(off_t phys_addr, size_t len)
42{
43 void *virt_addr;
44 int err;
Philipp Deppenwiesed8fe4432016-03-18 00:52:54 +010045
Maximilian Schanderdf5b83f2017-10-28 18:33:07 +020046 virt_addr = mmap(NULL, len, PROT_WRITE | PROT_READ, MAP_SHARED, fd_mem, phys_addr);
Philipp Deppenwiesed8fe4432016-03-18 00:52:54 +010047
Maximilian Schanderdf5b83f2017-10-28 18:33:07 +020048 if (virt_addr == MAP_FAILED) {
49 err = errno;
50 printf("Error mapping physical memory 0x%016jd [0x%zx] ERRNO=%d %s\n",
51 (intmax_t)phys_addr, len, err, strerror(err));
52 return NULL;
53 }
Philipp Deppenwiesed8fe4432016-03-18 00:52:54 +010054
Maximilian Schanderdf5b83f2017-10-28 18:33:07 +020055 return virt_addr;
Philipp Deppenwiesed8fe4432016-03-18 00:52:54 +010056}
57
Maximilian Schanderdf5b83f2017-10-28 18:33:07 +020058void unmap_physical(void *virt_addr, size_t len)
59{
60 munmap(virt_addr, len);
Philipp Deppenwiesed8fe4432016-03-18 00:52:54 +010061}
62#endif