AMD64 - Fix format conversions and other warnings.
[dragonfly.git] / sys / dev / acpica5 / Osd / OsdMemory.c
CommitLineData
5ed44076
MD
1/*-
2 * Copyright (c) 2000 Mitsaru Iwasaki
3 * Copyright (c) 2000 Michael Smith
4 * Copyright (c) 2000 BSDi
5 * All rights reserved.
6 *
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.
15 *
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.
27 *
f9d8cd12 28 * $FreeBSD: src/sys/dev/acpica/Osd/OsdMemory.c,v 1.11 2004/04/14 03:39:08 njl Exp $
e1eeedd0 29 * $DragonFly: src/sys/dev/acpica5/Osd/OsdMemory.c,v 1.6 2007/01/17 17:31:19 y0netan1 Exp $
5ed44076
MD
30 */
31
32/*
33 * 6.2 : Memory Management
34 */
35
36#include "acpi.h"
37
38#include <sys/kernel.h>
39#include <sys/malloc.h>
40#include <vm/vm.h>
41#include <vm/pmap.h>
42
f9d8cd12 43MALLOC_DEFINE(M_ACPICA, "acpica", "ACPI CA memory pool");
5ed44076 44
9d905e1a
MD
45struct acpi_memtrack {
46 struct acpi_memtrack *next;
47 void *base;
48 ACPI_SIZE size;
e1eeedd0
YT
49#if ACPI_DEBUG_MEMMAP
50 int freed;
51 struct {
52 const char *func;
53 int line;
54 } mapper, unmapper;
55#endif
9d905e1a
MD
56};
57
58typedef struct acpi_memtrack *acpi_memtrack_t;
59
60static acpi_memtrack_t acpi_mapbase;
61
5ed44076
MD
62void *
63AcpiOsAllocate(ACPI_SIZE Size)
64{
efda3bd0 65 return (kmalloc(Size, M_ACPICA, M_INTWAIT));
5ed44076
MD
66}
67
68void
f9d8cd12 69AcpiOsFree(void *Memory)
5ed44076 70{
efda3bd0 71 kfree(Memory, M_ACPICA);
5ed44076
MD
72}
73
e1eeedd0
YT
74#if ACPI_DEBUG_MEMMAP
75void *
76_AcpiOsMapMemory(ACPI_PHYSICAL_ADDRESS Where, ACPI_SIZE Length,
77 const char *caller, int line)
78#else
79void *
80AcpiOsMapMemory(ACPI_PHYSICAL_ADDRESS Where, ACPI_SIZE Length)
81#endif
5ed44076 82{
9d905e1a 83 acpi_memtrack_t track;
e1eeedd0 84 void *map;
9d905e1a 85
e1eeedd0
YT
86 map = pmap_mapdev((vm_offset_t)Where, Length);
87 if (map == NULL)
88 return(NULL);
f9d8cd12 89 else {
e1eeedd0
YT
90#if ACPI_DEBUG_MEMMAP
91 for (track = acpi_mapbase; track != NULL; track = track->next) {
92 if (track->base == map)
93 break;
94 }
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;
102 }
9d905e1a 103 track->size = Length;
e1eeedd0
YT
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
9d905e1a
MD
111 acpi_mapbase = track;
112 }
e1eeedd0 113 return(map);
5ed44076
MD
114}
115
e1eeedd0
YT
116#if ACPI_DEBUG_MEMMAP
117void
118_AcpiOsUnmapMemory(void *LogicalAddress, ACPI_SIZE Length,
119 const char *caller, int line)
120#else
5ed44076 121void
f9d8cd12 122AcpiOsUnmapMemory(void *LogicalAddress, ACPI_SIZE Length)
e1eeedd0 123#endif
5ed44076 124{
9d905e1a
MD
125 struct acpi_memtrack **ptrack;
126 acpi_memtrack_t track;
127
128again:
129 for (ptrack = &acpi_mapbase; (track = *ptrack); ptrack = &track->next) {
e1eeedd0
YT
130#if ACPI_DEBUG_MEMMAP
131 if (track->freed)
132 continue;
133#endif
9d905e1a
MD
134 /*
135 * Exact match, degenerate case
136 */
137 if (track->base == LogicalAddress && track->size == Length) {
138 *ptrack = track->next;
139 pmap_unmapdev((vm_offset_t)track->base, track->size);
e1eeedd0
YT
140#if ACPI_DEBUG_MEMMAP
141 track->freed = 1;
142 track->unmapper.func = caller;
143 track->unmapper.line = line;
144#else
efda3bd0 145 kfree(track, M_ACPICA);
e1eeedd0 146#endif
9d905e1a
MD
147 return;
148 }
149 /*
150 * Completely covered
151 */
152 if ((char *)LogicalAddress <= (char *)track->base &&
153 (char *)LogicalAddress + Length >= (char *)track->base + track->size
154 ) {
155 *ptrack = track->next;
156 pmap_unmapdev((vm_offset_t)track->base, track->size);
e3869ec7 157 kprintf("AcpiOsUnmapMemory: Warning, deallocation request too"
bfc09ba0
MD
158 " large! %p/%08jx (actual was %p/%08jx)\n",
159 LogicalAddress, (intmax_t)Length,
160 track->base, (intmax_t)track->size);
e1eeedd0
YT
161#if ACPI_DEBUG_MEMMAP
162 track->freed = 1;
163 track->unmapper.func = caller;
164 track->unmapper.line = line;
165#else
efda3bd0 166 kfree(track, M_ACPICA);
e1eeedd0 167#endif
9d905e1a
MD
168 goto again;
169 }
170
171 /*
172 * Overlapping
173 */
174 if ((char *)LogicalAddress + Length >= (char *)track->base &&
175 (char *)LogicalAddress < (char *)track->base + track->size
176 ) {
e3869ec7 177 kprintf("AcpiOsUnmapMemory: Warning, deallocation did not "
bfc09ba0
MD
178 "track allocation: %p/%08jx (actual was %p/%08jx)\n",
179 LogicalAddress, (intmax_t)Length,
180 track->base, (intmax_t)track->size);
9d905e1a
MD
181 }
182 }
bfc09ba0
MD
183 kprintf("AcpiOsUnmapMemory: Warning, broken ACPI, bad unmap: %p/%08jx\n",
184 LogicalAddress, (intmax_t)Length);
e1eeedd0
YT
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
193 );
194 }
195 }
196#endif
5ed44076
MD
197}
198
199ACPI_STATUS
f9d8cd12
MD
200AcpiOsGetPhysicalAddress(void *LogicalAddress,
201 ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
5ed44076 202{
f9d8cd12
MD
203 /* We can't necessarily do this, so cop out. */
204 return (AE_BAD_ADDRESS);
5ed44076
MD
205}
206
207/*
208 * There is no clean way to do this. We make the charitable assumption
209 * that callers will not pass garbage to us.
210 */
211BOOLEAN
212AcpiOsReadable (void *Pointer, ACPI_SIZE Length)
213{
f9d8cd12 214 return (TRUE);
5ed44076
MD
215}
216
217BOOLEAN
218AcpiOsWritable (void *Pointer, ACPI_SIZE Length)
219{
f9d8cd12 220 return (TRUE);
5ed44076
MD
221}
222
223ACPI_STATUS
f9d8cd12 224AcpiOsReadMemory(ACPI_PHYSICAL_ADDRESS Address, UINT32 *Value, UINT32 Width)
5ed44076
MD
225{
226 void *LogicalAddress;
227
e1eeedd0
YT
228 LogicalAddress = AcpiOsMapMemory(Address, Width / 8);
229 if (LogicalAddress == NULL)
f9d8cd12 230 return (AE_NOT_EXIST);
5ed44076
MD
231
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;
248 }
249
250 AcpiOsUnmapMemory(LogicalAddress, Width / 8);
251
f9d8cd12 252 return (AE_OK);
5ed44076
MD
253}
254
255ACPI_STATUS
f9d8cd12 256AcpiOsWriteMemory(ACPI_PHYSICAL_ADDRESS Address, UINT32 Value, UINT32 Width)
5ed44076
MD
257{
258 void *LogicalAddress;
259
e1eeedd0
YT
260 LogicalAddress = AcpiOsMapMemory(Address, Width / 8);
261 if (LogicalAddress == NULL)
f9d8cd12 262 return (AE_NOT_EXIST);
5ed44076
MD
263
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;
280 }
281
282 AcpiOsUnmapMemory(LogicalAddress, Width / 8);
283
f9d8cd12 284 return (AE_OK);
5ed44076 285}