OsdMemory.c revision 87036
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 87036 2001-11-28 04:36:29Z 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
42static MALLOC_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
82ACPI_STATUS
83AcpiOsGetPhysicalAddress(void *LogicalAddress, ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
84{
85    /* we can't necessarily do this, so cop out */
86    return(AE_BAD_ADDRESS);
87}
88
89/*
90 * There is no clean way to do this.  We make the charitable assumption
91 * that callers will not pass garbage to us.
92 */
93BOOLEAN
94AcpiOsReadable (void *Pointer, UINT32 Length)
95{
96    return(TRUE);
97}
98
99BOOLEAN
100AcpiOsWritable (void *Pointer, UINT32 Length)
101{
102    return(TRUE);
103}
104
105ACPI_STATUS
106AcpiOsReadMemory (
107    ACPI_PHYSICAL_ADDRESS	Address,
108    void			*Value,
109    UINT32			Width)
110{
111    void	*LogicalAddress;
112
113    if (AcpiOsMapMemory(Address, Width / 8, &LogicalAddress) != AE_OK) {
114	return(AE_NOT_EXIST);
115    }
116
117    switch (Width) {
118    case 8:
119	*(u_int8_t *)Value = (*(volatile u_int8_t *)LogicalAddress);
120	break;
121    case 16:
122	*(u_int16_t *)Value = (*(volatile u_int16_t *)LogicalAddress);
123	break;
124    case 32:
125	*(u_int32_t *)Value = (*(volatile u_int32_t *)LogicalAddress);
126	break;
127    case 64:
128	*(u_int64_t *)Value = (*(volatile u_int64_t *)LogicalAddress);
129	break;
130    default:
131	/* debug trap goes here */
132    }
133
134    AcpiOsUnmapMemory(LogicalAddress, Width / 8);
135
136    return(AE_OK);
137}
138
139ACPI_STATUS
140AcpiOsWriteMemory (
141    ACPI_PHYSICAL_ADDRESS	Address,
142    ACPI_INTEGER		Value,
143    UINT32			Width)
144{
145    void	*LogicalAddress;
146
147    if (AcpiOsMapMemory(Address, Width / 8, &LogicalAddress) != AE_OK) {
148	return(AE_NOT_EXIST);
149    }
150
151    switch (Width) {
152    case 8:
153	(*(volatile u_int8_t *)LogicalAddress) = Value & 0xff;
154	break;
155    case 16:
156	(*(volatile u_int16_t *)LogicalAddress) = Value & 0xffff;
157	break;
158    case 32:
159	(*(volatile u_int32_t *)LogicalAddress) = Value & 0xffffffff;
160	break;
161    case 64:
162	(*(volatile u_int64_t *)LogicalAddress) = Value;
163	break;
164    default:
165	/* debug trap goes here */
166    }
167
168    AcpiOsUnmapMemory(LogicalAddress, Width / 8);
169
170    return(AE_OK);
171}
172