OsdMemory.c revision 69459
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: head/sys/dev/acpica/Osd/OsdMemory.c 69459 2000-12-01 10:19:28Z msmith $
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
42MALLOC_DEFINE(M_ACPICA, "acpica", "ACPI CA memory pool");
43
44void *
45AcpiOsAllocate(UINT32 Size)
46{
47    return(malloc(Size, M_ACPICA, M_NOWAIT));
48}
49
50void *
51AcpiOsCallocate (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
61void
62AcpiOsFree (void *Memory)
63{
64    free(Memory, M_ACPICA);
65}
66
67ACPI_STATUS
68AcpiOsMapMemory (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
76void
77AcpiOsUnmapMemory (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 */
86BOOLEAN
87AcpiOsReadable (void *Pointer, UINT32 Length)
88{
89    return(TRUE);
90}
91
92BOOLEAN
93AcpiOsWritable (void *Pointer, UINT32 Length)
94{
95    return(TRUE);
96}
97
98static __inline
99UINT32
100AcpiOsMemInX (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
126UINT8
127AcpiOsMemIn8 (ACPI_PHYSICAL_ADDRESS InAddr)
128{
129    return((UINT8)AcpiOsMemInX(1, InAddr));
130}
131
132UINT16
133AcpiOsMemIn16 (ACPI_PHYSICAL_ADDRESS InAddr)
134{
135    return((UINT16)AcpiOsMemInX(2, InAddr));
136}
137
138UINT32
139AcpiOsMemIn32 (ACPI_PHYSICAL_ADDRESS InAddr)
140{
141    return((UINT32)AcpiOsMemInX(4, InAddr));
142}
143
144static __inline
145void
146AcpiOsMemOutX (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
169void
170AcpiOsMemOut8 (ACPI_PHYSICAL_ADDRESS OutAddr, UINT8 Value)
171{
172    AcpiOsMemOutX(1, OutAddr, (UINT32)Value);
173}
174
175void
176AcpiOsMemOut16 (ACPI_PHYSICAL_ADDRESS OutAddr, UINT16 Value)
177{
178    AcpiOsMemOutX(2, OutAddr, (UINT32)Value);
179}
180
181void
182AcpiOsMemOut32 (ACPI_PHYSICAL_ADDRESS OutAddr, UINT32 Value)
183{
184    AcpiOsMemOutX(4, OutAddr, (UINT32)Value);
185}
186
187