WIP FPC-III support
[linux/fpc-iii.git] / tools / power / acpi / os_specific / service_layers / osunixmap.c
blobc565546e85bce7eeb2c908b1d53804a03b1e245b
1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
4 * Module Name: osunixmap - Unix OSL for file mappings
6 * Copyright (C) 2000 - 2020, Intel Corp.
8 *****************************************************************************/
10 #include "acpidump.h"
11 #include <unistd.h>
12 #include <sys/mman.h>
13 #ifdef _free_BSD
14 #include <sys/param.h>
15 #endif
17 #define _COMPONENT ACPI_OS_SERVICES
18 ACPI_MODULE_NAME("osunixmap")
20 #ifndef O_BINARY
21 #define O_BINARY 0
22 #endif
23 #if defined(_dragon_fly) || defined(_free_BSD) || defined(_QNX)
24 #define MMAP_FLAGS MAP_SHARED
25 #else
26 #define MMAP_FLAGS MAP_PRIVATE
27 #endif
28 #define SYSTEM_MEMORY "/dev/mem"
29 /*******************************************************************************
31 * FUNCTION: acpi_os_get_page_size
33 * PARAMETERS: None
35 * RETURN: Page size of the platform.
37 * DESCRIPTION: Obtain page size of the platform.
39 ******************************************************************************/
40 static acpi_size acpi_os_get_page_size(void)
43 #ifdef PAGE_SIZE
44 return PAGE_SIZE;
45 #else
46 return sysconf(_SC_PAGESIZE);
47 #endif
50 /******************************************************************************
52 * FUNCTION: acpi_os_map_memory
54 * PARAMETERS: where - Physical address of memory to be mapped
55 * length - How much memory to map
57 * RETURN: Pointer to mapped memory. Null on error.
59 * DESCRIPTION: Map physical memory into local address space.
61 *****************************************************************************/
63 void *acpi_os_map_memory(acpi_physical_address where, acpi_size length)
65 u8 *mapped_memory;
66 acpi_physical_address offset;
67 acpi_size page_size;
68 int fd;
70 fd = open(SYSTEM_MEMORY, O_RDONLY | O_BINARY);
71 if (fd < 0) {
72 fprintf(stderr, "Cannot open %s\n", SYSTEM_MEMORY);
73 return (NULL);
76 /* Align the offset to use mmap */
78 page_size = acpi_os_get_page_size();
79 offset = where % page_size;
81 /* Map the table header to get the length of the full table */
83 mapped_memory = mmap(NULL, (length + offset), PROT_READ, MMAP_FLAGS,
84 fd, (where - offset));
85 if (mapped_memory == MAP_FAILED) {
86 fprintf(stderr, "Cannot map %s\n", SYSTEM_MEMORY);
87 close(fd);
88 return (NULL);
91 close(fd);
92 return (ACPI_CAST8(mapped_memory + offset));
95 /******************************************************************************
97 * FUNCTION: acpi_os_unmap_memory
99 * PARAMETERS: where - Logical address of memory to be unmapped
100 * length - How much memory to unmap
102 * RETURN: None.
104 * DESCRIPTION: Delete a previously created mapping. Where and Length must
105 * correspond to a previous mapping exactly.
107 *****************************************************************************/
109 void acpi_os_unmap_memory(void *where, acpi_size length)
111 acpi_physical_address offset;
112 acpi_size page_size;
114 page_size = acpi_os_get_page_size();
115 offset = ACPI_TO_INTEGER(where) % page_size;
116 munmap((u8 *)where - offset, (length + offset));