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 __FBSDID("$FreeBSD: stable/9/sys/dev/acpica/Osd/OsdMemory.c 249132 2013-04-05 08:22:11Z mav $");
35
36 #include <contrib/dev/acpica/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 *
AcpiOsAllocate(ACPI_SIZE Size)46 AcpiOsAllocate(ACPI_SIZE Size)
47 {
48 return (malloc(Size, M_ACPICA, M_NOWAIT));
49 }
50
51 void
AcpiOsFree(void * Memory)52 AcpiOsFree(void *Memory)
53 {
54 free(Memory, M_ACPICA);
55 }
56
57 void *
AcpiOsMapMemory(ACPI_PHYSICAL_ADDRESS PhysicalAddress,ACPI_SIZE Length)58 AcpiOsMapMemory(ACPI_PHYSICAL_ADDRESS PhysicalAddress, ACPI_SIZE Length)
59 {
60 return (pmap_mapbios((vm_offset_t)PhysicalAddress, Length));
61 }
62
63 void
AcpiOsUnmapMemory(void * LogicalAddress,ACPI_SIZE Length)64 AcpiOsUnmapMemory(void *LogicalAddress, ACPI_SIZE Length)
65 {
66 pmap_unmapbios((vm_offset_t)LogicalAddress, Length);
67 }
68
69 ACPI_STATUS
AcpiOsGetPhysicalAddress(void * LogicalAddress,ACPI_PHYSICAL_ADDRESS * PhysicalAddress)70 AcpiOsGetPhysicalAddress(void *LogicalAddress,
71 ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
72 {
73 /* We can't necessarily do this, so cop out. */
74 return (AE_BAD_ADDRESS);
75 }
76
77 BOOLEAN
AcpiOsReadable(void * Pointer,ACPI_SIZE Length)78 AcpiOsReadable (void *Pointer, ACPI_SIZE Length)
79 {
80 return (TRUE);
81 }
82
83 BOOLEAN
AcpiOsWritable(void * Pointer,ACPI_SIZE Length)84 AcpiOsWritable (void *Pointer, ACPI_SIZE Length)
85 {
86 return (TRUE);
87 }
88
89 ACPI_STATUS
AcpiOsReadMemory(ACPI_PHYSICAL_ADDRESS Address,UINT32 * Value,UINT32 Width)90 AcpiOsReadMemory(ACPI_PHYSICAL_ADDRESS Address, UINT32 *Value, UINT32 Width)
91 {
92 void *LogicalAddress;
93
94 LogicalAddress = pmap_mapdev(Address, Width / 8);
95 if (LogicalAddress == NULL)
96 return (AE_NOT_EXIST);
97
98 switch (Width) {
99 case 8:
100 *Value = *(volatile uint8_t *)LogicalAddress;
101 break;
102 case 16:
103 *Value = *(volatile uint16_t *)LogicalAddress;
104 break;
105 case 32:
106 *Value = *(volatile uint32_t *)LogicalAddress;
107 break;
108 }
109
110 pmap_unmapdev((vm_offset_t)LogicalAddress, Width / 8);
111
112 return (AE_OK);
113 }
114
115 ACPI_STATUS
AcpiOsWriteMemory(ACPI_PHYSICAL_ADDRESS Address,UINT32 Value,UINT32 Width)116 AcpiOsWriteMemory(ACPI_PHYSICAL_ADDRESS Address, UINT32 Value, UINT32 Width)
117 {
118 void *LogicalAddress;
119
120 LogicalAddress = pmap_mapdev(Address, Width / 8);
121 if (LogicalAddress == NULL)
122 return (AE_NOT_EXIST);
123
124 switch (Width) {
125 case 8:
126 *(volatile uint8_t *)LogicalAddress = Value;
127 break;
128 case 16:
129 *(volatile uint16_t *)LogicalAddress = Value;
130 break;
131 case 32:
132 *(volatile uint32_t *)LogicalAddress = Value;
133 break;
134 }
135
136 pmap_unmapdev((vm_offset_t)LogicalAddress, Width / 8);
137
138 return (AE_OK);
139 }
140