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 - 2023, Intel Corp.
8 *****************************************************************************/
14 #include <sys/param.h>
17 #define _COMPONENT ACPI_OS_SERVICES
18 ACPI_MODULE_NAME("osunixmap")
23 #if defined(_dragon_fly) || defined(_free_BSD) || defined(_QNX)
24 #define MMAP_FLAGS MAP_SHARED
26 #define MMAP_FLAGS MAP_PRIVATE
28 #define SYSTEM_MEMORY "/dev/mem"
29 /*******************************************************************************
31 * FUNCTION: acpi_os_get_page_size
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)
46 return sysconf(_SC_PAGESIZE);
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)
66 acpi_physical_address offset;
70 fd = open(SYSTEM_MEMORY, O_RDONLY | O_BINARY);
72 fprintf(stderr, "Cannot open %s\n", SYSTEM_MEMORY);
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);
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
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;
114 page_size = acpi_os_get_page_size();
115 offset = ACPI_TO_INTEGER(where) % page_size;
116 munmap((u8 *)where - offset, (length + offset));