OsdMemory.c revision 117534
167760Smsmith/*-
269459Smsmith * Copyright (c) 2000 Mitsaru Iwasaki
367760Smsmith * Copyright (c) 2000 Michael Smith
467760Smsmith * Copyright (c) 2000 BSDi
567760Smsmith * All rights reserved.
667760Smsmith *
767760Smsmith * Redistribution and use in source and binary forms, with or without
867760Smsmith * modification, are permitted provided that the following conditions
967760Smsmith * are met:
1067760Smsmith * 1. Redistributions of source code must retain the above copyright
1167760Smsmith *    notice, this list of conditions and the following disclaimer.
1267760Smsmith * 2. Redistributions in binary form must reproduce the above copyright
1367760Smsmith *    notice, this list of conditions and the following disclaimer in the
1467760Smsmith *    documentation and/or other materials provided with the distribution.
1567760Smsmith *
1667760Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1767760Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1867760Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1967760Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2067760Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2167760Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2267760Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2367760Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2467760Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2567760Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2667760Smsmith * SUCH DAMAGE.
2767760Smsmith *
2867760Smsmith *	$FreeBSD: head/sys/dev/acpica/Osd/OsdMemory.c 117534 2003-07-14 02:42:15Z marcel $
2967760Smsmith */
3067760Smsmith
3167760Smsmith/*
3267760Smsmith * 6.2 : Memory Management
3367760Smsmith */
3467760Smsmith
3567760Smsmith#include "acpi.h"
3667760Smsmith
3767760Smsmith#include <sys/kernel.h>
3867760Smsmith#include <sys/malloc.h>
3967760Smsmith#include <vm/vm.h>
4067760Smsmith#include <vm/pmap.h>
4167760Smsmith
4269776Smsmithstatic MALLOC_DEFINE(M_ACPICA, "acpica", "ACPI CA memory pool");
4367760Smsmith
4467760Smsmithvoid *
4592118SpeterAcpiOsAllocate(ACPI_SIZE Size)
4667760Smsmith{
4767760Smsmith    return(malloc(Size, M_ACPICA, M_NOWAIT));
4867760Smsmith}
4967760Smsmith
5067760Smsmithvoid
5167760SmsmithAcpiOsFree (void *Memory)
5267760Smsmith{
5367760Smsmith    free(Memory, M_ACPICA);
5467760Smsmith}
5567760Smsmith
5667760SmsmithACPI_STATUS
5792118SpeterAcpiOsMapMemory (ACPI_PHYSICAL_ADDRESS PhysicalAddress, ACPI_SIZE Length, void **LogicalAddress)
5867760Smsmith{
5967760Smsmith    *LogicalAddress = pmap_mapdev((vm_offset_t)PhysicalAddress, Length);
6067760Smsmith    if (*LogicalAddress == NULL)
6167760Smsmith	return(AE_BAD_ADDRESS);
6267760Smsmith    return(AE_OK);
6367760Smsmith}
6467760Smsmith
6567760Smsmithvoid
6692118SpeterAcpiOsUnmapMemory (void *LogicalAddress, ACPI_SIZE Length)
6767760Smsmith{
6867760Smsmith    pmap_unmapdev((vm_offset_t)LogicalAddress, Length);
6967760Smsmith}
7067760Smsmith
7180071SmsmithACPI_STATUS
7280071SmsmithAcpiOsGetPhysicalAddress(void *LogicalAddress, ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
7380071Smsmith{
7480071Smsmith    /* we can't necessarily do this, so cop out */
7580071Smsmith    return(AE_BAD_ADDRESS);
7680071Smsmith}
7780071Smsmith
7867760Smsmith/*
7967760Smsmith * There is no clean way to do this.  We make the charitable assumption
8067760Smsmith * that callers will not pass garbage to us.
8167760Smsmith */
8267760SmsmithBOOLEAN
83117534SmarcelAcpiOsReadable (void *Pointer, ACPI_SIZE Length)
8467760Smsmith{
8567760Smsmith    return(TRUE);
8667760Smsmith}
8767760Smsmith
8867760SmsmithBOOLEAN
89117534SmarcelAcpiOsWritable (void *Pointer, ACPI_SIZE Length)
9067760Smsmith{
9167760Smsmith    return(TRUE);
9267760Smsmith}
9367760Smsmith
9480071SmsmithACPI_STATUS
9580071SmsmithAcpiOsReadMemory (
9680071Smsmith    ACPI_PHYSICAL_ADDRESS	Address,
97117530Snjl    UINT32			*Value,
9880071Smsmith    UINT32			Width)
9969459Smsmith{
10069459Smsmith    void	*LogicalAddress;
10169459Smsmith
10280071Smsmith    if (AcpiOsMapMemory(Address, Width / 8, &LogicalAddress) != AE_OK) {
10380071Smsmith	return(AE_NOT_EXIST);
10469459Smsmith    }
10569459Smsmith
10680071Smsmith    switch (Width) {
10780071Smsmith    case 8:
10880071Smsmith	*(u_int8_t *)Value = (*(volatile u_int8_t *)LogicalAddress);
10969459Smsmith	break;
11080071Smsmith    case 16:
11180071Smsmith	*(u_int16_t *)Value = (*(volatile u_int16_t *)LogicalAddress);
11269459Smsmith	break;
11380071Smsmith    case 32:
11480071Smsmith	*(u_int32_t *)Value = (*(volatile u_int32_t *)LogicalAddress);
11569459Smsmith	break;
11680071Smsmith    case 64:
11780071Smsmith	*(u_int64_t *)Value = (*(volatile u_int64_t *)LogicalAddress);
11880071Smsmith	break;
11980071Smsmith    default:
12080071Smsmith	/* debug trap goes here */
12192666Speter	break;
12269459Smsmith    }
12369459Smsmith
12480071Smsmith    AcpiOsUnmapMemory(LogicalAddress, Width / 8);
12569459Smsmith
12680071Smsmith    return(AE_OK);
12769459Smsmith}
12869459Smsmith
12980071SmsmithACPI_STATUS
13080071SmsmithAcpiOsWriteMemory (
13180071Smsmith    ACPI_PHYSICAL_ADDRESS	Address,
132117530Snjl    UINT32			Value,
13380071Smsmith    UINT32			Width)
13469459Smsmith{
13569459Smsmith    void	*LogicalAddress;
13669459Smsmith
13780071Smsmith    if (AcpiOsMapMemory(Address, Width / 8, &LogicalAddress) != AE_OK) {
13880071Smsmith	return(AE_NOT_EXIST);
13969459Smsmith    }
14069459Smsmith
14180071Smsmith    switch (Width) {
14280071Smsmith    case 8:
14369459Smsmith	(*(volatile u_int8_t *)LogicalAddress) = Value & 0xff;
14469459Smsmith	break;
14580071Smsmith    case 16:
14669459Smsmith	(*(volatile u_int16_t *)LogicalAddress) = Value & 0xffff;
14769459Smsmith	break;
14880071Smsmith    case 32:
14980071Smsmith	(*(volatile u_int32_t *)LogicalAddress) = Value & 0xffffffff;
15069459Smsmith	break;
15180071Smsmith    case 64:
15280071Smsmith	(*(volatile u_int64_t *)LogicalAddress) = Value;
15380071Smsmith	break;
15480071Smsmith    default:
15580071Smsmith	/* debug trap goes here */
15692666Speter	break;
15769459Smsmith    }
15869459Smsmith
15980071Smsmith    AcpiOsUnmapMemory(LogicalAddress, Width / 8);
16069459Smsmith
16180071Smsmith    return(AE_OK);
16269459Smsmith}
163