xref: /freebsd/sys/dev/acpica/Osd/OsdMemory.c (revision 1b6c76a2fe091c74f08427e6c870851025a9cf67)
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$
29  */
30 
31 /*
32  * 6.2 : Memory Management
33  */
34 
35 #include "acpi.h"
36 
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <vm/vm.h>
40 #include <vm/pmap.h>
41 
42 static MALLOC_DEFINE(M_ACPICA, "acpica", "ACPI CA memory pool");
43 
44 void *
45 AcpiOsAllocate(UINT32 Size)
46 {
47     return(malloc(Size, M_ACPICA, M_NOWAIT));
48 }
49 
50 void *
51 AcpiOsCallocate (UINT32 Size)
52 {
53     void	*alloc;
54 
55     alloc = malloc(Size, M_ACPICA, M_NOWAIT);
56     if (alloc != NULL)
57 	bzero(alloc, Size);
58     return(alloc);
59 }
60 
61 void
62 AcpiOsFree (void *Memory)
63 {
64     free(Memory, M_ACPICA);
65 }
66 
67 ACPI_STATUS
68 AcpiOsMapMemory (ACPI_PHYSICAL_ADDRESS PhysicalAddress, UINT32 Length, void **LogicalAddress)
69 {
70     *LogicalAddress = pmap_mapdev((vm_offset_t)PhysicalAddress, Length);
71     if (*LogicalAddress == NULL)
72 	return(AE_BAD_ADDRESS);
73     return(AE_OK);
74 }
75 
76 void
77 AcpiOsUnmapMemory (void *LogicalAddress, UINT32 Length)
78 {
79     pmap_unmapdev((vm_offset_t)LogicalAddress, Length);
80 }
81 
82 /*
83  * There is no clean way to do this.  We make the charitable assumption
84  * that callers will not pass garbage to us.
85  */
86 BOOLEAN
87 AcpiOsReadable (void *Pointer, UINT32 Length)
88 {
89     return(TRUE);
90 }
91 
92 BOOLEAN
93 AcpiOsWritable (void *Pointer, UINT32 Length)
94 {
95     return(TRUE);
96 }
97 
98 static __inline
99 UINT32
100 AcpiOsMemInX (UINT32 Length, ACPI_PHYSICAL_ADDRESS InAddr)
101 {
102     UINT32	Value;
103     void	*LogicalAddress;
104 
105     if (AcpiOsMapMemory(InAddr, Length, &LogicalAddress) != AE_OK) {
106 	return(0);
107     }
108 
109     switch (Length) {
110     case 1:
111 	Value = (*(volatile u_int8_t *)LogicalAddress) & 0xff;
112 	break;
113     case 2:
114 	Value = (*(volatile u_int16_t *)LogicalAddress) & 0xffff;
115 	break;
116     case 4:
117 	Value = (*(volatile u_int32_t *)LogicalAddress);
118 	break;
119     }
120 
121     AcpiOsUnmapMemory(LogicalAddress, Length);
122 
123     return(Value);
124 }
125 
126 UINT8
127 AcpiOsMemIn8 (ACPI_PHYSICAL_ADDRESS InAddr)
128 {
129     return((UINT8)AcpiOsMemInX(1, InAddr));
130 }
131 
132 UINT16
133 AcpiOsMemIn16 (ACPI_PHYSICAL_ADDRESS InAddr)
134 {
135     return((UINT16)AcpiOsMemInX(2, InAddr));
136 }
137 
138 UINT32
139 AcpiOsMemIn32 (ACPI_PHYSICAL_ADDRESS InAddr)
140 {
141     return((UINT32)AcpiOsMemInX(4, InAddr));
142 }
143 
144 static __inline
145 void
146 AcpiOsMemOutX (UINT32 Length, ACPI_PHYSICAL_ADDRESS OutAddr, UINT32 Value)
147 {
148     void	*LogicalAddress;
149 
150     if (AcpiOsMapMemory(OutAddr, Length, &LogicalAddress) != AE_OK) {
151 	return;
152     }
153 
154     switch (Length) {
155     case 1:
156 	(*(volatile u_int8_t *)LogicalAddress) = Value & 0xff;
157 	break;
158     case 2:
159 	(*(volatile u_int16_t *)LogicalAddress) = Value & 0xffff;
160 	break;
161     case 4:
162 	(*(volatile u_int32_t *)LogicalAddress) = Value;
163 	break;
164     }
165 
166     AcpiOsUnmapMemory(LogicalAddress, Length);
167 }
168 
169 void
170 AcpiOsMemOut8 (ACPI_PHYSICAL_ADDRESS OutAddr, UINT8 Value)
171 {
172     AcpiOsMemOutX(1, OutAddr, (UINT32)Value);
173 }
174 
175 void
176 AcpiOsMemOut16 (ACPI_PHYSICAL_ADDRESS OutAddr, UINT16 Value)
177 {
178     AcpiOsMemOutX(2, OutAddr, (UINT32)Value);
179 }
180 
181 void
182 AcpiOsMemOut32 (ACPI_PHYSICAL_ADDRESS OutAddr, UINT32 Value)
183 {
184     AcpiOsMemOutX(4, OutAddr, (UINT32)Value);
185 }
186 
187