Rename malloc->kmalloc, free->kfree, and realloc->krealloc. Pass 1
[dragonfly.git] / sys / dev / acpica5 / Osd / OsdMemory.c
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  *
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.4 2006/09/05 00:55:36 dillon Exp $
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
43 MALLOC_DEFINE(M_ACPICA, "acpica", "ACPI CA memory pool");
44
45 struct acpi_memtrack {
46     struct acpi_memtrack *next;
47     void *base;
48     ACPI_SIZE size;
49 };
50
51 typedef struct acpi_memtrack *acpi_memtrack_t;
52
53 static acpi_memtrack_t acpi_mapbase;
54
55 void *
56 AcpiOsAllocate(ACPI_SIZE Size)
57 {
58     return (kmalloc(Size, M_ACPICA, M_INTWAIT));
59 }
60
61 void
62 AcpiOsFree(void *Memory)
63 {
64     kfree(Memory, M_ACPICA);
65 }
66
67 ACPI_STATUS
68 AcpiOsMapMemory(ACPI_PHYSICAL_ADDRESS PhysicalAddress, ACPI_SIZE Length,
69     void **LogicalAddress)
70 {
71     acpi_memtrack_t track;
72
73     *LogicalAddress = pmap_mapdev((vm_offset_t)PhysicalAddress, Length);
74     if (*LogicalAddress == NULL)
75         return(AE_BAD_ADDRESS);
76     else {
77         track = kmalloc(sizeof(struct acpi_memtrack), M_ACPICA, M_INTWAIT);
78         track->next = acpi_mapbase;
79         track->base = *LogicalAddress;
80         track->size = Length;
81         acpi_mapbase = track;
82     }
83     return(AE_OK);
84 }
85
86 void
87 AcpiOsUnmapMemory(void *LogicalAddress, ACPI_SIZE Length)
88 {
89     struct acpi_memtrack **ptrack;
90     acpi_memtrack_t track;
91
92 again:
93     for (ptrack = &acpi_mapbase; (track = *ptrack); ptrack = &track->next) {
94         /*
95          * Exact match, degenerate case
96          */
97         if (track->base == LogicalAddress && track->size == Length) {
98             *ptrack = track->next;
99             pmap_unmapdev((vm_offset_t)track->base, track->size);
100             kfree(track, M_ACPICA);
101             return;
102         }
103         /*
104          * Completely covered
105          */
106         if ((char *)LogicalAddress <= (char *)track->base &&
107             (char *)LogicalAddress + Length >= (char *)track->base + track->size
108         ) {
109             *ptrack = track->next;
110             pmap_unmapdev((vm_offset_t)track->base, track->size);
111             printf("AcpiOsUnmapMemory: Warning, deallocation request too"
112                    " large! %p/%08x (actual was %p/%08x)\n",
113                    LogicalAddress, Length,
114                    track->base, track->size);
115             kfree(track, M_ACPICA);
116             goto again;
117         }
118
119         /*
120          * Overlapping
121          */
122         if ((char *)LogicalAddress + Length >= (char *)track->base &&
123             (char *)LogicalAddress < (char *)track->base + track->size
124         ) {
125             printf("AcpiOsUnmapMemory: Warning, deallocation did not "
126                    "track allocation: %p/%08x (actual was %p/%08x)\n",
127                    LogicalAddress, Length,
128                    track->base, track->size);
129         }
130     }
131     printf("AcpiOsUnmapMemory: Warning, broken ACPI, bad unmap: %p/%08x\n",
132         LogicalAddress, Length);
133 }
134
135 ACPI_STATUS
136 AcpiOsGetPhysicalAddress(void *LogicalAddress,
137     ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
138 {
139     /* We can't necessarily do this, so cop out. */
140     return (AE_BAD_ADDRESS);
141 }
142
143 /*
144  * There is no clean way to do this.  We make the charitable assumption
145  * that callers will not pass garbage to us.
146  */
147 BOOLEAN
148 AcpiOsReadable (void *Pointer, ACPI_SIZE Length)
149 {
150     return (TRUE);
151 }
152
153 BOOLEAN
154 AcpiOsWritable (void *Pointer, ACPI_SIZE Length)
155 {
156     return (TRUE);
157 }
158
159 ACPI_STATUS
160 AcpiOsReadMemory(ACPI_PHYSICAL_ADDRESS Address, UINT32 *Value, UINT32 Width)
161 {
162     void        *LogicalAddress;
163
164     if (AcpiOsMapMemory(Address, Width / 8, &LogicalAddress) != AE_OK)
165         return (AE_NOT_EXIST);
166
167     switch (Width) {
168     case 8:
169         *(u_int8_t *)Value = (*(volatile u_int8_t *)LogicalAddress);
170         break;
171     case 16:
172         *(u_int16_t *)Value = (*(volatile u_int16_t *)LogicalAddress);
173         break;
174     case 32:
175         *(u_int32_t *)Value = (*(volatile u_int32_t *)LogicalAddress);
176         break;
177     case 64:
178         *(u_int64_t *)Value = (*(volatile u_int64_t *)LogicalAddress);
179         break;
180     default:
181         /* debug trap goes here */
182         break;
183     }
184
185     AcpiOsUnmapMemory(LogicalAddress, Width / 8);
186
187     return (AE_OK);
188 }
189
190 ACPI_STATUS
191 AcpiOsWriteMemory(ACPI_PHYSICAL_ADDRESS Address, UINT32 Value, UINT32 Width)
192 {
193     void        *LogicalAddress;
194
195     if (AcpiOsMapMemory(Address, Width / 8, &LogicalAddress) != AE_OK)
196         return (AE_NOT_EXIST);
197
198     switch (Width) {
199     case 8:
200         (*(volatile u_int8_t *)LogicalAddress) = Value & 0xff;
201         break;
202     case 16:
203         (*(volatile u_int16_t *)LogicalAddress) = Value & 0xffff;
204         break;
205     case 32:
206         (*(volatile u_int32_t *)LogicalAddress) = Value & 0xffffffff;
207         break;
208     case 64:
209         (*(volatile u_int64_t *)LogicalAddress) = Value;
210         break;
211     default:
212         /* debug trap goes here */
213         break;
214     }
215
216     AcpiOsUnmapMemory(LogicalAddress, Width / 8);
217
218     return (AE_OK);
219 }