Path: blob/master/tools/power/acpi/os_specific/service_layers/osunixmap.c
26292 views
// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.01/******************************************************************************2*3* Module Name: osunixmap - Unix OSL for file mappings4*5* Copyright (C) 2000 - 2025, Intel Corp.6*7*****************************************************************************/89#include "acpidump.h"10#include <unistd.h>11#include <sys/mman.h>12#ifdef _free_BSD13#include <sys/param.h>14#endif1516#define _COMPONENT ACPI_OS_SERVICES17ACPI_MODULE_NAME("osunixmap")1819#ifndef O_BINARY20#define O_BINARY 021#endif22#if defined(_dragon_fly) || defined(_free_BSD) || defined(_QNX)23#define MMAP_FLAGS MAP_SHARED24#else25#define MMAP_FLAGS MAP_PRIVATE26#endif27#define SYSTEM_MEMORY "/dev/mem"28/*******************************************************************************29*30* FUNCTION: acpi_os_get_page_size31*32* PARAMETERS: None33*34* RETURN: Page size of the platform.35*36* DESCRIPTION: Obtain page size of the platform.37*38******************************************************************************/39static acpi_size acpi_os_get_page_size(void)40{4142#ifdef PAGE_SIZE43return PAGE_SIZE;44#else45return sysconf(_SC_PAGESIZE);46#endif47}4849/******************************************************************************50*51* FUNCTION: acpi_os_map_memory52*53* PARAMETERS: where - Physical address of memory to be mapped54* length - How much memory to map55*56* RETURN: Pointer to mapped memory. Null on error.57*58* DESCRIPTION: Map physical memory into local address space.59*60*****************************************************************************/6162void *acpi_os_map_memory(acpi_physical_address where, acpi_size length)63{64u8 *mapped_memory;65acpi_physical_address offset;66acpi_size page_size;67int fd;6869fd = open(SYSTEM_MEMORY, O_RDONLY | O_BINARY);70if (fd < 0) {71fprintf(stderr, "Cannot open %s\n", SYSTEM_MEMORY);72return (NULL);73}7475/* Align the offset to use mmap */7677page_size = acpi_os_get_page_size();78offset = where % page_size;7980/* Map the table header to get the length of the full table */8182mapped_memory = mmap(NULL, (length + offset), PROT_READ, MMAP_FLAGS,83fd, (where - offset));84if (mapped_memory == MAP_FAILED) {85fprintf(stderr, "Cannot map %s\n", SYSTEM_MEMORY);86close(fd);87return (NULL);88}8990close(fd);91return (ACPI_CAST8(mapped_memory + offset));92}9394/******************************************************************************95*96* FUNCTION: acpi_os_unmap_memory97*98* PARAMETERS: where - Logical address of memory to be unmapped99* length - How much memory to unmap100*101* RETURN: None.102*103* DESCRIPTION: Delete a previously created mapping. Where and Length must104* correspond to a previous mapping exactly.105*106*****************************************************************************/107108void acpi_os_unmap_memory(void *where, acpi_size length)109{110acpi_physical_address offset;111acpi_size page_size;112113page_size = acpi_os_get_page_size();114offset = ACPI_TO_INTEGER(where) % page_size;115munmap((u8 *)where - offset, (length + offset));116}117118119