xref: /freebsd/sys/dev/acpica/Osd/OsdMemory.c (revision 8fa113e5fc65fe6abc757f0089f477a87ee4d185)
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 ACPI_STATUS
83 AcpiOsGetPhysicalAddress(void *LogicalAddress, ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
84 {
85     /* we can't necessarily do this, so cop out */
86     return(AE_BAD_ADDRESS);
87 }
88 
89 /*
90  * There is no clean way to do this.  We make the charitable assumption
91  * that callers will not pass garbage to us.
92  */
93 BOOLEAN
94 AcpiOsReadable (void *Pointer, UINT32 Length)
95 {
96     return(TRUE);
97 }
98 
99 BOOLEAN
100 AcpiOsWritable (void *Pointer, UINT32 Length)
101 {
102     return(TRUE);
103 }
104 
105 ACPI_STATUS
106 AcpiOsReadMemory (
107     ACPI_PHYSICAL_ADDRESS	Address,
108     void			*Value,
109     UINT32			Width)
110 {
111     void	*LogicalAddress;
112 
113     if (AcpiOsMapMemory(Address, Width / 8, &LogicalAddress) != AE_OK) {
114 	return(AE_NOT_EXIST);
115     }
116 
117     switch (Width) {
118     case 8:
119 	*(u_int8_t *)Value = (*(volatile u_int8_t *)LogicalAddress);
120 	break;
121     case 16:
122 	*(u_int16_t *)Value = (*(volatile u_int16_t *)LogicalAddress);
123 	break;
124     case 32:
125 	*(u_int32_t *)Value = (*(volatile u_int32_t *)LogicalAddress);
126 	break;
127     case 64:
128 	*(u_int64_t *)Value = (*(volatile u_int64_t *)LogicalAddress);
129 	break;
130     default:
131 	/* debug trap goes here */
132     }
133 
134     AcpiOsUnmapMemory(LogicalAddress, Width / 8);
135 
136     return(AE_OK);
137 }
138 
139 ACPI_STATUS
140 AcpiOsWriteMemory (
141     ACPI_PHYSICAL_ADDRESS	Address,
142     ACPI_INTEGER		Value,
143     UINT32			Width)
144 {
145     void	*LogicalAddress;
146 
147     if (AcpiOsMapMemory(Address, Width / 8, &LogicalAddress) != AE_OK) {
148 	return(AE_NOT_EXIST);
149     }
150 
151     switch (Width) {
152     case 8:
153 	(*(volatile u_int8_t *)LogicalAddress) = Value & 0xff;
154 	break;
155     case 16:
156 	(*(volatile u_int16_t *)LogicalAddress) = Value & 0xffff;
157 	break;
158     case 32:
159 	(*(volatile u_int32_t *)LogicalAddress) = Value & 0xffffffff;
160 	break;
161     case 64:
162 	(*(volatile u_int64_t *)LogicalAddress) = Value;
163 	break;
164     default:
165 	/* debug trap goes here */
166     }
167 
168     AcpiOsUnmapMemory(LogicalAddress, Width / 8);
169 
170     return(AE_OK);
171 }
172