* Fix support for followings:
[dragonfly.git] / sys / dev / acpica5 / Osd / OsdMemory.c
blob97da03518d28d74926d1789f68b637557cd8e0ec
1 /*-
2 * Copyright (c) 2000 Mitsaru Iwasaki
3 * Copyright (c) 2000 Michael Smith
4 * Copyright (c) 2000 BSDi
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
26 * SUCH DAMAGE.
28 * $FreeBSD: src/sys/dev/acpica/Osd/OsdMemory.c,v 1.11 2004/04/14 03:39:08 njl Exp $
29 * $DragonFly: src/sys/dev/acpica5/Osd/OsdMemory.c,v 1.6 2007/01/17 17:31:19 y0netan1 Exp $
33 * 6.2 : Memory Management
36 #include "acpi.h"
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <vm/vm.h>
41 #include <vm/pmap.h>
43 MALLOC_DEFINE(M_ACPICA, "acpica", "ACPI CA memory pool");
45 struct acpi_memtrack {
46 struct acpi_memtrack *next;
47 void *base;
48 ACPI_SIZE size;
49 #if ACPI_DEBUG_MEMMAP
50 int freed;
51 struct {
52 const char *func;
53 int line;
54 } mapper, unmapper;
55 #endif
58 typedef struct acpi_memtrack *acpi_memtrack_t;
60 static acpi_memtrack_t acpi_mapbase;
62 void *
63 AcpiOsAllocate(ACPI_SIZE Size)
65 return (kmalloc(Size, M_ACPICA, M_INTWAIT));
68 void
69 AcpiOsFree(void *Memory)
71 kfree(Memory, M_ACPICA);
74 #if ACPI_DEBUG_MEMMAP
75 void *
76 _AcpiOsMapMemory(ACPI_PHYSICAL_ADDRESS Where, ACPI_SIZE Length,
77 const char *caller, int line)
78 #else
79 void *
80 AcpiOsMapMemory(ACPI_PHYSICAL_ADDRESS Where, ACPI_SIZE Length)
81 #endif
83 acpi_memtrack_t track;
84 void *map;
86 map = pmap_mapdev((vm_offset_t)Where, Length);
87 if (map == NULL)
88 return(NULL);
89 else {
90 #if ACPI_DEBUG_MEMMAP
91 for (track = acpi_mapbase; track != NULL; track = track->next) {
92 if (track->base == map)
93 break;
95 #else
96 track = NULL;
97 #endif
98 if (track == NULL) {
99 track = kmalloc(sizeof(*track), M_ACPICA, M_INTWAIT);
100 track->next = acpi_mapbase;
101 track->base = map;
103 track->size = Length;
104 #if ACPI_DEBUG_MEMMAP
105 track->freed = 0;
106 track->mapper.func = caller;
107 track->mapper.line = line;
108 track->unmapper.func = "";
109 track->unmapper.line = 0;
110 #endif
111 acpi_mapbase = track;
113 return(map);
116 #if ACPI_DEBUG_MEMMAP
117 void
118 _AcpiOsUnmapMemory(void *LogicalAddress, ACPI_SIZE Length,
119 const char *caller, int line)
120 #else
121 void
122 AcpiOsUnmapMemory(void *LogicalAddress, ACPI_SIZE Length)
123 #endif
125 struct acpi_memtrack **ptrack;
126 acpi_memtrack_t track;
128 again:
129 for (ptrack = &acpi_mapbase; (track = *ptrack); ptrack = &track->next) {
130 #if ACPI_DEBUG_MEMMAP
131 if (track->freed)
132 continue;
133 #endif
135 * Exact match, degenerate case
137 if (track->base == LogicalAddress && track->size == Length) {
138 *ptrack = track->next;
139 pmap_unmapdev((vm_offset_t)track->base, track->size);
140 #if ACPI_DEBUG_MEMMAP
141 track->freed = 1;
142 track->unmapper.func = caller;
143 track->unmapper.line = line;
144 #else
145 kfree(track, M_ACPICA);
146 #endif
147 return;
150 * Completely covered
152 if ((char *)LogicalAddress <= (char *)track->base &&
153 (char *)LogicalAddress + Length >= (char *)track->base + track->size
155 *ptrack = track->next;
156 pmap_unmapdev((vm_offset_t)track->base, track->size);
157 kprintf("AcpiOsUnmapMemory: Warning, deallocation request too"
158 " large! %p/%08x (actual was %p/%08x)\n",
159 LogicalAddress, Length,
160 track->base, track->size);
161 #if ACPI_DEBUG_MEMMAP
162 track->freed = 1;
163 track->unmapper.func = caller;
164 track->unmapper.line = line;
165 #else
166 kfree(track, M_ACPICA);
167 #endif
168 goto again;
172 * Overlapping
174 if ((char *)LogicalAddress + Length >= (char *)track->base &&
175 (char *)LogicalAddress < (char *)track->base + track->size
177 kprintf("AcpiOsUnmapMemory: Warning, deallocation did not "
178 "track allocation: %p/%08x (actual was %p/%08x)\n",
179 LogicalAddress, Length,
180 track->base, track->size);
183 kprintf("AcpiOsUnmapMemory: Warning, broken ACPI, bad unmap: %p/%08x\n",
184 LogicalAddress, Length);
185 #if ACPI_DEBUG_MEMMAP
186 for (track = acpi_mapbase; track != NULL; track = track->next) {
187 if (track->freed && track->base == LogicalAddress) {
188 kprintf("%s: unmapping: %p/%08x, mapped by %s:%d,"
189 "last unmapped by %s:%d\n",
190 __func__, LogicalAddress, Length,
191 track->mapper.func, track->mapper.line,
192 track->unmapper.func, track->unmapper.line
196 #endif
199 ACPI_STATUS
200 AcpiOsGetPhysicalAddress(void *LogicalAddress,
201 ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
203 /* We can't necessarily do this, so cop out. */
204 return (AE_BAD_ADDRESS);
208 * There is no clean way to do this. We make the charitable assumption
209 * that callers will not pass garbage to us.
211 BOOLEAN
212 AcpiOsReadable (void *Pointer, ACPI_SIZE Length)
214 return (TRUE);
217 BOOLEAN
218 AcpiOsWritable (void *Pointer, ACPI_SIZE Length)
220 return (TRUE);
223 ACPI_STATUS
224 AcpiOsReadMemory(ACPI_PHYSICAL_ADDRESS Address, UINT32 *Value, UINT32 Width)
226 void *LogicalAddress;
228 LogicalAddress = AcpiOsMapMemory(Address, Width / 8);
229 if (LogicalAddress == NULL)
230 return (AE_NOT_EXIST);
232 switch (Width) {
233 case 8:
234 *(u_int8_t *)Value = (*(volatile u_int8_t *)LogicalAddress);
235 break;
236 case 16:
237 *(u_int16_t *)Value = (*(volatile u_int16_t *)LogicalAddress);
238 break;
239 case 32:
240 *(u_int32_t *)Value = (*(volatile u_int32_t *)LogicalAddress);
241 break;
242 case 64:
243 *(u_int64_t *)Value = (*(volatile u_int64_t *)LogicalAddress);
244 break;
245 default:
246 /* debug trap goes here */
247 break;
250 AcpiOsUnmapMemory(LogicalAddress, Width / 8);
252 return (AE_OK);
255 ACPI_STATUS
256 AcpiOsWriteMemory(ACPI_PHYSICAL_ADDRESS Address, UINT32 Value, UINT32 Width)
258 void *LogicalAddress;
260 LogicalAddress = AcpiOsMapMemory(Address, Width / 8);
261 if (LogicalAddress == NULL)
262 return (AE_NOT_EXIST);
264 switch (Width) {
265 case 8:
266 (*(volatile u_int8_t *)LogicalAddress) = Value & 0xff;
267 break;
268 case 16:
269 (*(volatile u_int16_t *)LogicalAddress) = Value & 0xffff;
270 break;
271 case 32:
272 (*(volatile u_int32_t *)LogicalAddress) = Value & 0xffffffff;
273 break;
274 case 64:
275 (*(volatile u_int64_t *)LogicalAddress) = Value;
276 break;
277 default:
278 /* debug trap goes here */
279 break;
282 AcpiOsUnmapMemory(LogicalAddress, Width / 8);
284 return (AE_OK);
287 ACPI_STATUS
288 AcpiOsValidateAddress (UINT8 SpaceId, ACPI_PHYSICAL_ADDRESS Address,
289 ACPI_SIZE Length)
291 #if 0
292 if (Address == 0)
293 return (AE_AML_ILLEGAL_ADDRESS);
294 /* XXX */
295 #endif
296 return (AE_OK);