xref: /dragonfly/sys/dev/acpica/Osd/OsdMemory.c (revision 2d8a3be7)
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.10.2.1 2003/08/22 20:49:21 jhb Exp $
29  *      $DragonFly: src/sys/dev/acpica/Osd/Attic/OsdMemory.c,v 1.1 2003/09/24 03:32:16 drhodus 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 static MALLOC_DEFINE(M_ACPICA, "acpica", "ACPI CA memory pool");
44 
45 void *
46 AcpiOsAllocate(ACPI_SIZE Size)
47 {
48     return(malloc(Size, M_ACPICA, M_NOWAIT));
49 }
50 
51 void
52 AcpiOsFree (void *Memory)
53 {
54     free(Memory, M_ACPICA);
55 }
56 
57 ACPI_STATUS
58 AcpiOsMapMemory (ACPI_PHYSICAL_ADDRESS PhysicalAddress, ACPI_SIZE Length, void **LogicalAddress)
59 {
60     *LogicalAddress = pmap_mapdev((vm_offset_t)PhysicalAddress, Length);
61     if (*LogicalAddress == NULL)
62 	return(AE_BAD_ADDRESS);
63     return(AE_OK);
64 }
65 
66 void
67 AcpiOsUnmapMemory (void *LogicalAddress, ACPI_SIZE Length)
68 {
69     pmap_unmapdev((vm_offset_t)LogicalAddress, Length);
70 }
71 
72 ACPI_STATUS
73 AcpiOsGetPhysicalAddress(void *LogicalAddress, ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
74 {
75     /* we can't necessarily do this, so cop out */
76     return(AE_BAD_ADDRESS);
77 }
78 
79 /*
80  * There is no clean way to do this.  We make the charitable assumption
81  * that callers will not pass garbage to us.
82  */
83 BOOLEAN
84 AcpiOsReadable (void *Pointer, UINT32 Length)
85 {
86     return(TRUE);
87 }
88 
89 BOOLEAN
90 AcpiOsWritable (void *Pointer, UINT32 Length)
91 {
92     return(TRUE);
93 }
94 
95 ACPI_STATUS
96 AcpiOsReadMemory (
97     ACPI_PHYSICAL_ADDRESS	Address,
98     void			*Value,
99     UINT32			Width)
100 {
101     void	*LogicalAddress;
102 
103     if (AcpiOsMapMemory(Address, Width / 8, &LogicalAddress) != AE_OK) {
104 	return(AE_NOT_EXIST);
105     }
106 
107     switch (Width) {
108     case 8:
109 	*(u_int8_t *)Value = (*(volatile u_int8_t *)LogicalAddress);
110 	break;
111     case 16:
112 	*(u_int16_t *)Value = (*(volatile u_int16_t *)LogicalAddress);
113 	break;
114     case 32:
115 	*(u_int32_t *)Value = (*(volatile u_int32_t *)LogicalAddress);
116 	break;
117     case 64:
118 	*(u_int64_t *)Value = (*(volatile u_int64_t *)LogicalAddress);
119 	break;
120     default:
121 	/* debug trap goes here */
122 	break;
123     }
124 
125     AcpiOsUnmapMemory(LogicalAddress, Width / 8);
126 
127     return(AE_OK);
128 }
129 
130 ACPI_STATUS
131 AcpiOsWriteMemory (
132     ACPI_PHYSICAL_ADDRESS	Address,
133     ACPI_INTEGER		Value,
134     UINT32			Width)
135 {
136     void	*LogicalAddress;
137 
138     if (AcpiOsMapMemory(Address, Width / 8, &LogicalAddress) != AE_OK) {
139 	return(AE_NOT_EXIST);
140     }
141 
142     switch (Width) {
143     case 8:
144 	(*(volatile u_int8_t *)LogicalAddress) = Value & 0xff;
145 	break;
146     case 16:
147 	(*(volatile u_int16_t *)LogicalAddress) = Value & 0xffff;
148 	break;
149     case 32:
150 	(*(volatile u_int32_t *)LogicalAddress) = Value & 0xffffffff;
151 	break;
152     case 64:
153 	(*(volatile u_int64_t *)LogicalAddress) = Value;
154 	break;
155     default:
156 	/* debug trap goes here */
157 	break;
158     }
159 
160     AcpiOsUnmapMemory(LogicalAddress, Width / 8);
161 
162     return(AE_OK);
163 }
164