Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
0002 /******************************************************************************
0003  *
0004  * Module Name: osunixmap - Unix OSL for file mappings
0005  *
0006  * Copyright (C) 2000 - 2022, Intel Corp.
0007  *
0008  *****************************************************************************/
0009 
0010 #include "acpidump.h"
0011 #include <unistd.h>
0012 #include <sys/mman.h>
0013 #ifdef _free_BSD
0014 #include <sys/param.h>
0015 #endif
0016 
0017 #define _COMPONENT          ACPI_OS_SERVICES
0018 ACPI_MODULE_NAME("osunixmap")
0019 
0020 #ifndef O_BINARY
0021 #define O_BINARY 0
0022 #endif
0023 #if defined(_dragon_fly) || defined(_free_BSD) || defined(_QNX)
0024 #define MMAP_FLAGS          MAP_SHARED
0025 #else
0026 #define MMAP_FLAGS          MAP_PRIVATE
0027 #endif
0028 #define SYSTEM_MEMORY       "/dev/mem"
0029 /*******************************************************************************
0030  *
0031  * FUNCTION:    acpi_os_get_page_size
0032  *
0033  * PARAMETERS:  None
0034  *
0035  * RETURN:      Page size of the platform.
0036  *
0037  * DESCRIPTION: Obtain page size of the platform.
0038  *
0039  ******************************************************************************/
0040 static acpi_size acpi_os_get_page_size(void)
0041 {
0042 
0043 #ifdef PAGE_SIZE
0044     return PAGE_SIZE;
0045 #else
0046     return sysconf(_SC_PAGESIZE);
0047 #endif
0048 }
0049 
0050 /******************************************************************************
0051  *
0052  * FUNCTION:    acpi_os_map_memory
0053  *
0054  * PARAMETERS:  where               - Physical address of memory to be mapped
0055  *              length              - How much memory to map
0056  *
0057  * RETURN:      Pointer to mapped memory. Null on error.
0058  *
0059  * DESCRIPTION: Map physical memory into local address space.
0060  *
0061  *****************************************************************************/
0062 
0063 void *acpi_os_map_memory(acpi_physical_address where, acpi_size length)
0064 {
0065     u8 *mapped_memory;
0066     acpi_physical_address offset;
0067     acpi_size page_size;
0068     int fd;
0069 
0070     fd = open(SYSTEM_MEMORY, O_RDONLY | O_BINARY);
0071     if (fd < 0) {
0072         fprintf(stderr, "Cannot open %s\n", SYSTEM_MEMORY);
0073         return (NULL);
0074     }
0075 
0076     /* Align the offset to use mmap */
0077 
0078     page_size = acpi_os_get_page_size();
0079     offset = where % page_size;
0080 
0081     /* Map the table header to get the length of the full table */
0082 
0083     mapped_memory = mmap(NULL, (length + offset), PROT_READ, MMAP_FLAGS,
0084                  fd, (where - offset));
0085     if (mapped_memory == MAP_FAILED) {
0086         fprintf(stderr, "Cannot map %s\n", SYSTEM_MEMORY);
0087         close(fd);
0088         return (NULL);
0089     }
0090 
0091     close(fd);
0092     return (ACPI_CAST8(mapped_memory + offset));
0093 }
0094 
0095 /******************************************************************************
0096  *
0097  * FUNCTION:    acpi_os_unmap_memory
0098  *
0099  * PARAMETERS:  where               - Logical address of memory to be unmapped
0100  *              length              - How much memory to unmap
0101  *
0102  * RETURN:      None.
0103  *
0104  * DESCRIPTION: Delete a previously created mapping. Where and Length must
0105  *              correspond to a previous mapping exactly.
0106  *
0107  *****************************************************************************/
0108 
0109 void acpi_os_unmap_memory(void *where, acpi_size length)
0110 {
0111     acpi_physical_address offset;
0112     acpi_size page_size;
0113 
0114     page_size = acpi_os_get_page_size();
0115     offset = ACPI_TO_INTEGER(where) % page_size;
0116     munmap((u8 *)where - offset, (length + offset));
0117 }