2 * Copyright (c) 2000 Mitsaru Iwasaki
3 * Copyright (c) 2000 Michael Smith
4 * Copyright (c) 2000 BSDi
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $FreeBSD: src/sys/dev/acpica/Osd/OsdMemory.c,v 1.11 2004/04/14 03:39:08 njl Exp $
32 * 6.2 : Memory Management
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
42 MALLOC_DEFINE(M_ACPICA, "acpica", "ACPI CA memory pool");
44 struct acpi_memtrack {
45 struct acpi_memtrack *next;
48 #ifdef ACPI_DEBUG_MEMMAP
57 typedef struct acpi_memtrack *acpi_memtrack_t;
59 static acpi_memtrack_t acpi_mapbase;
62 AcpiOsAllocate(ACPI_SIZE Size)
64 return (kmalloc(Size, M_ACPICA, M_INTWAIT));
68 AcpiOsFree(void *Memory)
70 kfree(Memory, M_ACPICA);
73 #ifdef ACPI_DEBUG_MEMMAP
75 _AcpiOsMapMemory(ACPI_PHYSICAL_ADDRESS Where, ACPI_SIZE Length,
76 const char *caller, int line)
79 AcpiOsMapMemory(ACPI_PHYSICAL_ADDRESS Where, ACPI_SIZE Length)
82 acpi_memtrack_t track;
85 map = pmap_mapdev((vm_offset_t)Where, Length);
89 #ifdef ACPI_DEBUG_MEMMAP
90 for (track = acpi_mapbase; track != NULL; track = track->next) {
91 if (track->base == map)
98 track = kmalloc(sizeof(*track), M_ACPICA, M_INTWAIT);
99 track->next = acpi_mapbase;
102 track->size = Length;
103 #ifdef ACPI_DEBUG_MEMMAP
105 track->mapper.func = caller;
106 track->mapper.line = line;
107 track->unmapper.func = "";
108 track->unmapper.line = 0;
110 acpi_mapbase = track;
115 #ifdef ACPI_DEBUG_MEMMAP
117 _AcpiOsUnmapMemory(void *LogicalAddress, ACPI_SIZE Length,
118 const char *caller, int line)
121 AcpiOsUnmapMemory(void *LogicalAddress, ACPI_SIZE Length)
124 struct acpi_memtrack **ptrack;
125 acpi_memtrack_t track;
128 for (ptrack = &acpi_mapbase; (track = *ptrack); ptrack = &track->next) {
129 #ifdef ACPI_DEBUG_MEMMAP
134 * Exact match, degenerate case
136 if (track->base == LogicalAddress && track->size == Length) {
137 *ptrack = track->next;
138 pmap_unmapdev((vm_offset_t)track->base, track->size);
139 #ifdef ACPI_DEBUG_MEMMAP
141 track->unmapper.func = caller;
142 track->unmapper.line = line;
144 kfree(track, M_ACPICA);
151 if ((char *)LogicalAddress <= (char *)track->base &&
152 (char *)LogicalAddress + Length >= (char *)track->base + track->size
154 *ptrack = track->next;
155 pmap_unmapdev((vm_offset_t)track->base, track->size);
156 kprintf("AcpiOsUnmapMemory: Warning, deallocation request too"
157 " large! %p/%08jx (actual was %p/%08jx)\n",
158 LogicalAddress, (intmax_t)Length,
159 track->base, (intmax_t)track->size);
160 #ifdef ACPI_DEBUG_MEMMAP
162 track->unmapper.func = caller;
163 track->unmapper.line = line;
165 kfree(track, M_ACPICA);
173 if ((char *)LogicalAddress + Length >= (char *)track->base &&
174 (char *)LogicalAddress < (char *)track->base + track->size
176 kprintf("AcpiOsUnmapMemory: Warning, deallocation did not "
177 "track allocation: %p/%08jx (actual was %p/%08jx)\n",
178 LogicalAddress, (intmax_t)Length,
179 track->base, (intmax_t)track->size);
182 kprintf("AcpiOsUnmapMemory: Warning, broken ACPI, bad unmap: %p/%08jx\n",
183 LogicalAddress, (intmax_t)Length);
184 #ifdef ACPI_DEBUG_MEMMAP
185 for (track = acpi_mapbase; track != NULL; track = track->next) {
186 if (track->freed && track->base == LogicalAddress) {
187 kprintf("%s: unmapping: %p/%08x, mapped by %s:%d,"
188 "last unmapped by %s:%d\n",
189 __func__, LogicalAddress, Length,
190 track->mapper.func, track->mapper.line,
191 track->unmapper.func, track->unmapper.line
199 AcpiOsGetPhysicalAddress(void *LogicalAddress,
200 ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
202 /* We can't necessarily do this, so cop out. */
203 return (AE_BAD_ADDRESS);
207 * There is no clean way to do this. We make the charitable assumption
208 * that callers will not pass garbage to us.
211 AcpiOsReadable (void *Pointer, ACPI_SIZE Length)
217 AcpiOsWritable (void *Pointer, ACPI_SIZE Length)
223 AcpiOsReadMemory(ACPI_PHYSICAL_ADDRESS Address, UINT32 *Value, UINT32 Width)
225 void *LogicalAddress;
227 LogicalAddress = AcpiOsMapMemory(Address, Width / 8);
228 if (LogicalAddress == NULL)
229 return (AE_NOT_EXIST);
233 *(u_int8_t *)Value = (*(volatile u_int8_t *)LogicalAddress);
236 *(u_int16_t *)Value = (*(volatile u_int16_t *)LogicalAddress);
239 *(u_int32_t *)Value = (*(volatile u_int32_t *)LogicalAddress);
242 *(u_int64_t *)Value = (*(volatile u_int64_t *)LogicalAddress);
245 /* debug trap goes here */
249 AcpiOsUnmapMemory(LogicalAddress, Width / 8);
255 AcpiOsWriteMemory(ACPI_PHYSICAL_ADDRESS Address, UINT32 Value, UINT32 Width)
257 void *LogicalAddress;
259 LogicalAddress = AcpiOsMapMemory(Address, Width / 8);
260 if (LogicalAddress == NULL)
261 return (AE_NOT_EXIST);
265 (*(volatile u_int8_t *)LogicalAddress) = Value & 0xff;
268 (*(volatile u_int16_t *)LogicalAddress) = Value & 0xffff;
271 (*(volatile u_int32_t *)LogicalAddress) = Value & 0xffffffff;
274 (*(volatile u_int64_t *)LogicalAddress) = Value;
277 /* debug trap goes here */
281 AcpiOsUnmapMemory(LogicalAddress, Width / 8);