xref: /freebsd/sys/dev/acpica/Osd/OsdMemory.c (revision 61e21613)
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 
29 /*
30  * 6.2 : Memory Management
31  */
32 
33 #include <sys/cdefs.h>
34 #include <contrib/dev/acpica/include/acpi.h>
35 
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <vm/vm.h>
39 #include <vm/pmap.h>
40 
41 static MALLOC_DEFINE(M_ACPICA, "acpica", "ACPI CA memory pool");
42 
43 void *
44 AcpiOsAllocate(ACPI_SIZE Size)
45 {
46     return (malloc(Size, M_ACPICA, M_NOWAIT));
47 }
48 
49 void
50 AcpiOsFree(void *Memory)
51 {
52     free(Memory, M_ACPICA);
53 }
54 
55 void *
56 AcpiOsMapMemory(ACPI_PHYSICAL_ADDRESS PhysicalAddress, ACPI_SIZE Length)
57 {
58     return (pmap_mapbios((vm_offset_t)PhysicalAddress, Length));
59 }
60 
61 void
62 AcpiOsUnmapMemory(void *LogicalAddress, ACPI_SIZE Length)
63 {
64     pmap_unmapbios(LogicalAddress, Length);
65 }
66 
67 ACPI_STATUS
68 AcpiOsGetPhysicalAddress(void *LogicalAddress,
69     ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
70 {
71     /* We can't necessarily do this, so cop out. */
72     return (AE_BAD_ADDRESS);
73 }
74 
75 BOOLEAN
76 AcpiOsReadable (void *Pointer, ACPI_SIZE Length)
77 {
78     return (TRUE);
79 }
80 
81 BOOLEAN
82 AcpiOsWritable (void *Pointer, ACPI_SIZE Length)
83 {
84     return (TRUE);
85 }
86 
87 ACPI_STATUS
88 AcpiOsReadMemory(ACPI_PHYSICAL_ADDRESS Address, UINT64 *Value, UINT32 Width)
89 {
90     void	*LogicalAddress;
91 
92     LogicalAddress = pmap_mapdev(Address, Width / 8);
93     if (LogicalAddress == NULL)
94 	return (AE_NOT_EXIST);
95 
96     switch (Width) {
97     case 8:
98 	*Value = *(volatile uint8_t *)LogicalAddress;
99 	break;
100     case 16:
101 	*Value = *(volatile uint16_t *)LogicalAddress;
102 	break;
103     case 32:
104 	*Value = *(volatile uint32_t *)LogicalAddress;
105 	break;
106     case 64:
107 	*Value = *(volatile uint64_t *)LogicalAddress;
108 	break;
109     }
110 
111     pmap_unmapdev(LogicalAddress, Width / 8);
112 
113     return (AE_OK);
114 }
115 
116 ACPI_STATUS
117 AcpiOsWriteMemory(ACPI_PHYSICAL_ADDRESS Address, UINT64 Value, UINT32 Width)
118 {
119     void	*LogicalAddress;
120 
121     LogicalAddress = pmap_mapdev(Address, Width / 8);
122     if (LogicalAddress == NULL)
123 	return (AE_NOT_EXIST);
124 
125     switch (Width) {
126     case 8:
127 	*(volatile uint8_t *)LogicalAddress = Value;
128 	break;
129     case 16:
130 	*(volatile uint16_t *)LogicalAddress = Value;
131 	break;
132     case 32:
133 	*(volatile uint32_t *)LogicalAddress = Value;
134 	break;
135     case 64:
136 	*(volatile uint64_t *)LogicalAddress = Value;
137 	break;
138     }
139 
140     pmap_unmapdev(LogicalAddress, Width / 8);
141 
142     return (AE_OK);
143 }
144