OsdMemory.c revision 150003
1238438Sdteske/*-
2238438Sdteske * Copyright (c) 2000 Mitsaru Iwasaki
3278489Sdteske * Copyright (c) 2000 Michael Smith
4252980Sdteske * Copyright (c) 2000 BSDi
5238438Sdteske * All rights reserved.
6238438Sdteske *
7238438Sdteske * Redistribution and use in source and binary forms, with or without
8238438Sdteske * modification, are permitted provided that the following conditions
9238438Sdteske * are met:
10238438Sdteske * 1. Redistributions of source code must retain the above copyright
11238438Sdteske *    notice, this list of conditions and the following disclaimer.
12238438Sdteske * 2. Redistributions in binary form must reproduce the above copyright
13238438Sdteske *    notice, this list of conditions and the following disclaimer in the
14238438Sdteske *    documentation and/or other materials provided with the distribution.
15238438Sdteske *
16252987Sdteske * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17238438Sdteske * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18238438Sdteske * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19238438Sdteske * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20252987Sdteske * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21238438Sdteske * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22238438Sdteske * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23238438Sdteske * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24238438Sdteske * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25238438Sdteske * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26238438Sdteske * SUCH DAMAGE.
27238438Sdteske */
28278489Sdteske
29278489Sdteske/*
30238438Sdteske * 6.2 : Memory Management
31278489Sdteske */
32238438Sdteske
33238438Sdteske#include <sys/cdefs.h>
34238438Sdteske__FBSDID("$FreeBSD: head/sys/dev/acpica/Osd/OsdMemory.c 150003 2005-09-11 18:39:03Z obrien $");
35238438Sdteske
36238438Sdteske#include <contrib/dev/acpica/acpi.h>
37238438Sdteske
38238438Sdteske#include <sys/kernel.h>
39238438Sdteske#include <sys/malloc.h>
40238438Sdteske#include <vm/vm.h>
41238438Sdteske#include <vm/pmap.h>
42238438Sdteske
43238438SdteskeMALLOC_DEFINE(M_ACPICA, "acpica", "ACPI CA memory pool");
44238438Sdteske
45238438Sdteskevoid *
46238438SdteskeAcpiOsAllocate(ACPI_SIZE Size)
47238438Sdteske{
48238438Sdteske    return (malloc(Size, M_ACPICA, M_NOWAIT));
49278489Sdteske}
50278489Sdteske
51278489Sdteskevoid
52278489SdteskeAcpiOsFree(void *Memory)
53238438Sdteske{
54238438Sdteske    free(Memory, M_ACPICA);
55278489Sdteske}
56238438Sdteske
57238438SdteskeACPI_STATUS
58238438SdteskeAcpiOsMapMemory(ACPI_PHYSICAL_ADDRESS PhysicalAddress, ACPI_SIZE Length,
59238438Sdteske    void **LogicalAddress)
60238438Sdteske{
61238438Sdteske    *LogicalAddress = pmap_mapdev((vm_offset_t)PhysicalAddress, Length);
62238438Sdteske    if (*LogicalAddress == NULL)
63238438Sdteske	return (AE_BAD_ADDRESS);
64238438Sdteske    return (AE_OK);
65238438Sdteske}
66238438Sdteske
67278489Sdteskevoid
68238438SdteskeAcpiOsUnmapMemory(void *LogicalAddress, ACPI_SIZE Length)
69278489Sdteske{
70278489Sdteske    pmap_unmapdev((vm_offset_t)LogicalAddress, Length);
71278489Sdteske}
72278489Sdteske
73278489SdteskeACPI_STATUS
74278489SdteskeAcpiOsGetPhysicalAddress(void *LogicalAddress,
75278489Sdteske    ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
76278489Sdteske{
77278489Sdteske    /* We can't necessarily do this, so cop out. */
78278489Sdteske    return (AE_BAD_ADDRESS);
79278489Sdteske}
80278489Sdteske
81278489Sdteske/*
82238438Sdteske * There is no clean way to do this.  We make the charitable assumption
83238438Sdteske * that callers will not pass garbage to us.
84238438Sdteske */
85278489SdteskeBOOLEAN
86278489SdteskeAcpiOsReadable (void *Pointer, ACPI_SIZE Length)
87238438Sdteske{
88278489Sdteske    return (TRUE);
89278489Sdteske}
90278489Sdteske
91278489SdteskeBOOLEAN
92238438SdteskeAcpiOsWritable (void *Pointer, ACPI_SIZE Length)
93238438Sdteske{
94238438Sdteske    return (TRUE);
95238438Sdteske}
96278489Sdteske
97278489SdteskeACPI_STATUS
98278489SdteskeAcpiOsReadMemory(ACPI_PHYSICAL_ADDRESS Address, UINT32 *Value, UINT32 Width)
99238438Sdteske{
100238438Sdteske    void	*LogicalAddress;
101278489Sdteske
102278489Sdteske    if (AcpiOsMapMemory(Address, Width / 8, &LogicalAddress) != AE_OK)
103244675Sdteske	return (AE_NOT_EXIST);
104244675Sdteske
105238438Sdteske    switch (Width) {
106    case 8:
107	*(u_int8_t *)Value = (*(volatile u_int8_t *)LogicalAddress);
108	break;
109    case 16:
110	*(u_int16_t *)Value = (*(volatile u_int16_t *)LogicalAddress);
111	break;
112    case 32:
113	*(u_int32_t *)Value = (*(volatile u_int32_t *)LogicalAddress);
114	break;
115    case 64:
116	*(u_int64_t *)Value = (*(volatile u_int64_t *)LogicalAddress);
117	break;
118    default:
119	/* debug trap goes here */
120	break;
121    }
122
123    AcpiOsUnmapMemory(LogicalAddress, Width / 8);
124
125    return (AE_OK);
126}
127
128ACPI_STATUS
129AcpiOsWriteMemory(ACPI_PHYSICAL_ADDRESS Address, UINT32 Value, UINT32 Width)
130{
131    void	*LogicalAddress;
132
133    if (AcpiOsMapMemory(Address, Width / 8, &LogicalAddress) != AE_OK)
134	return (AE_NOT_EXIST);
135
136    switch (Width) {
137    case 8:
138	(*(volatile u_int8_t *)LogicalAddress) = Value & 0xff;
139	break;
140    case 16:
141	(*(volatile u_int16_t *)LogicalAddress) = Value & 0xffff;
142	break;
143    case 32:
144	(*(volatile u_int32_t *)LogicalAddress) = Value & 0xffffffff;
145	break;
146    case 64:
147	(*(volatile u_int64_t *)LogicalAddress) = Value;
148	break;
149    default:
150	/* debug trap goes here */
151	break;
152    }
153
154    AcpiOsUnmapMemory(LogicalAddress, Width / 8);
155
156    return (AE_OK);
157}
158