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 $
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
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
43 MALLOC_DEFINE(M_ACPICA
, "acpica", "ACPI CA memory pool");
45 struct acpi_memtrack
{
46 struct acpi_memtrack
*next
;
58 typedef struct acpi_memtrack
*acpi_memtrack_t
;
60 static acpi_memtrack_t acpi_mapbase
;
63 AcpiOsAllocate(ACPI_SIZE Size
)
65 return (kmalloc(Size
, M_ACPICA
, M_INTWAIT
));
69 AcpiOsFree(void *Memory
)
71 kfree(Memory
, M_ACPICA
);
76 _AcpiOsMapMemory(ACPI_PHYSICAL_ADDRESS Where
, ACPI_SIZE Length
,
77 const char *caller
, int line
)
80 AcpiOsMapMemory(ACPI_PHYSICAL_ADDRESS Where
, ACPI_SIZE Length
)
83 acpi_memtrack_t track
;
86 map
= pmap_mapdev((vm_offset_t
)Where
, Length
);
91 for (track
= acpi_mapbase
; track
!= NULL
; track
= track
->next
) {
92 if (track
->base
== map
)
99 track
= kmalloc(sizeof(*track
), M_ACPICA
, M_INTWAIT
);
100 track
->next
= acpi_mapbase
;
103 track
->size
= Length
;
104 #if ACPI_DEBUG_MEMMAP
106 track
->mapper
.func
= caller
;
107 track
->mapper
.line
= line
;
108 track
->unmapper
.func
= "";
109 track
->unmapper
.line
= 0;
111 acpi_mapbase
= track
;
116 #if ACPI_DEBUG_MEMMAP
118 _AcpiOsUnmapMemory(void *LogicalAddress
, ACPI_SIZE Length
,
119 const char *caller
, int line
)
122 AcpiOsUnmapMemory(void *LogicalAddress
, ACPI_SIZE Length
)
125 struct acpi_memtrack
**ptrack
;
126 acpi_memtrack_t track
;
129 for (ptrack
= &acpi_mapbase
; (track
= *ptrack
); ptrack
= &track
->next
) {
130 #if ACPI_DEBUG_MEMMAP
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
142 track
->unmapper
.func
= caller
;
143 track
->unmapper
.line
= line
;
145 kfree(track
, M_ACPICA
);
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
163 track
->unmapper
.func
= caller
;
164 track
->unmapper
.line
= line
;
166 kfree(track
, M_ACPICA
);
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
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.
212 AcpiOsReadable (void *Pointer
, ACPI_SIZE Length
)
218 AcpiOsWritable (void *Pointer
, ACPI_SIZE Length
)
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
);
234 *(u_int8_t
*)Value
= (*(volatile u_int8_t
*)LogicalAddress
);
237 *(u_int16_t
*)Value
= (*(volatile u_int16_t
*)LogicalAddress
);
240 *(u_int32_t
*)Value
= (*(volatile u_int32_t
*)LogicalAddress
);
243 *(u_int64_t
*)Value
= (*(volatile u_int64_t
*)LogicalAddress
);
246 /* debug trap goes here */
250 AcpiOsUnmapMemory(LogicalAddress
, Width
/ 8);
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
);
266 (*(volatile u_int8_t
*)LogicalAddress
) = Value
& 0xff;
269 (*(volatile u_int16_t
*)LogicalAddress
) = Value
& 0xffff;
272 (*(volatile u_int32_t
*)LogicalAddress
) = Value
& 0xffffffff;
275 (*(volatile u_int64_t
*)LogicalAddress
) = Value
;
278 /* debug trap goes here */
282 AcpiOsUnmapMemory(LogicalAddress
, Width
/ 8);
288 AcpiOsValidateAddress (UINT8 SpaceId
, ACPI_PHYSICAL_ADDRESS Address
,
293 return (AE_AML_ILLEGAL_ADDRESS
);