OsdMemory.c revision 167814
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: head/sys/dev/acpica/Osd/OsdMemory.c 167814 2007-03-22 18:16:43Z jkim $");
35
36#include <contrib/dev/acpica/acpi.h>
37
38#include <sys/kernel.h>
39#include <sys/malloc.h>
40#include <vm/vm.h>
41#include <vm/pmap.h>
42
43MALLOC_DEFINE(M_ACPICA, "acpica", "ACPI CA memory pool");
44
45void *
46AcpiOsAllocate(ACPI_SIZE Size)
47{
48    return (malloc(Size, M_ACPICA, M_NOWAIT));
49}
50
51void
52AcpiOsFree(void *Memory)
53{
54    free(Memory, M_ACPICA);
55}
56
57void *
58AcpiOsMapMemory(ACPI_PHYSICAL_ADDRESS PhysicalAddress, ACPI_NATIVE_UINT Length)
59{
60    return (pmap_mapbios((vm_offset_t)PhysicalAddress, Length));
61}
62
63void
64AcpiOsUnmapMemory(void *LogicalAddress, ACPI_SIZE Length)
65{
66    pmap_unmapbios((vm_offset_t)LogicalAddress, Length);
67}
68
69ACPI_STATUS
70AcpiOsGetPhysicalAddress(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
77ACPI_STATUS
78AcpiOsValidateInterface (char *Interface)
79{
80    return (AE_SUPPORT);
81}
82
83/*
84 * There is no clean way to do this.  We make the charitable assumption
85 * that callers will not pass garbage to us.
86 */
87ACPI_STATUS
88AcpiOsValidateAddress (UINT8 SpaceId, ACPI_PHYSICAL_ADDRESS Address,
89    ACPI_SIZE Length)
90{
91    return (AE_OK);
92}
93
94BOOLEAN
95AcpiOsReadable (void *Pointer, ACPI_SIZE Length)
96{
97    return (TRUE);
98}
99
100BOOLEAN
101AcpiOsWritable (void *Pointer, ACPI_SIZE Length)
102{
103    return (TRUE);
104}
105
106ACPI_STATUS
107AcpiOsReadMemory(ACPI_PHYSICAL_ADDRESS Address, UINT32 *Value, UINT32 Width)
108{
109    void	*LogicalAddress;
110
111    LogicalAddress = AcpiOsMapMemory(Address, Width / 8);
112    if (LogicalAddress == NULL)
113	return (AE_NOT_EXIST);
114
115    switch (Width) {
116    case 8:
117	*(u_int8_t *)Value = (*(volatile u_int8_t *)LogicalAddress);
118	break;
119    case 16:
120	*(u_int16_t *)Value = (*(volatile u_int16_t *)LogicalAddress);
121	break;
122    case 32:
123	*(u_int32_t *)Value = (*(volatile u_int32_t *)LogicalAddress);
124	break;
125    case 64:
126	*(u_int64_t *)Value = (*(volatile u_int64_t *)LogicalAddress);
127	break;
128    default:
129	/* debug trap goes here */
130	break;
131    }
132
133    AcpiOsUnmapMemory(LogicalAddress, Width / 8);
134
135    return (AE_OK);
136}
137
138ACPI_STATUS
139AcpiOsWriteMemory(ACPI_PHYSICAL_ADDRESS Address, UINT32 Value, UINT32 Width)
140{
141    void	*LogicalAddress;
142
143    LogicalAddress = AcpiOsMapMemory(Address, Width / 8);
144    if (LogicalAddress == NULL)
145	return (AE_NOT_EXIST);
146
147    switch (Width) {
148    case 8:
149	(*(volatile u_int8_t *)LogicalAddress) = Value & 0xff;
150	break;
151    case 16:
152	(*(volatile u_int16_t *)LogicalAddress) = Value & 0xffff;
153	break;
154    case 32:
155	(*(volatile u_int32_t *)LogicalAddress) = Value & 0xffffffff;
156	break;
157    case 64:
158	(*(volatile u_int64_t *)LogicalAddress) = Value;
159	break;
160    default:
161	/* debug trap goes here */
162	break;
163    }
164
165    AcpiOsUnmapMemory(LogicalAddress, Width / 8);
166
167    return (AE_OK);
168}
169