OsdMemory.c revision 92118
1264790Sbapt/*-
2264790Sbapt * Copyright (c) 2000 Mitsaru Iwasaki
3264790Sbapt * Copyright (c) 2000 Michael Smith
4264790Sbapt * Copyright (c) 2000 BSDi
5264790Sbapt * All rights reserved.
6264790Sbapt *
7264790Sbapt * Redistribution and use in source and binary forms, with or without
8264790Sbapt * modification, are permitted provided that the following conditions
9264790Sbapt * are met:
10264790Sbapt * 1. Redistributions of source code must retain the above copyright
11264790Sbapt *    notice, this list of conditions and the following disclaimer.
12264790Sbapt * 2. Redistributions in binary form must reproduce the above copyright
13264790Sbapt *    notice, this list of conditions and the following disclaimer in the
14264790Sbapt *    documentation and/or other materials provided with the distribution.
15264790Sbapt *
16264790Sbapt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17264790Sbapt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18264790Sbapt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19264790Sbapt * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20264790Sbapt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21264790Sbapt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22264790Sbapt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23264790Sbapt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24264790Sbapt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25264790Sbapt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26264790Sbapt * SUCH DAMAGE.
27264790Sbapt *
28264790Sbapt *	$FreeBSD: head/sys/dev/acpica/Osd/OsdMemory.c 92118 2002-03-12 00:10:40Z peter $
29264790Sbapt */
30264790Sbapt
31264790Sbapt/*
32264790Sbapt * 6.2 : Memory Management
33264790Sbapt */
34264790Sbapt
35264790Sbapt#include "acpi.h"
36264790Sbapt
37264790Sbapt#include <sys/kernel.h>
38264790Sbapt#include <sys/malloc.h>
39264790Sbapt#include <vm/vm.h>
40264790Sbapt#include <vm/pmap.h>
41264790Sbapt
42264790Sbaptstatic MALLOC_DEFINE(M_ACPICA, "acpica", "ACPI CA memory pool");
43264790Sbapt
44264790Sbaptvoid *
45264790SbaptAcpiOsAllocate(ACPI_SIZE Size)
46264790Sbapt{
47    return(malloc(Size, M_ACPICA, M_NOWAIT));
48}
49
50void
51AcpiOsFree (void *Memory)
52{
53    free(Memory, M_ACPICA);
54}
55
56ACPI_STATUS
57AcpiOsMapMemory (ACPI_PHYSICAL_ADDRESS PhysicalAddress, ACPI_SIZE Length, void **LogicalAddress)
58{
59    *LogicalAddress = pmap_mapdev((vm_offset_t)PhysicalAddress, Length);
60    if (*LogicalAddress == NULL)
61	return(AE_BAD_ADDRESS);
62    return(AE_OK);
63}
64
65void
66AcpiOsUnmapMemory (void *LogicalAddress, ACPI_SIZE Length)
67{
68    pmap_unmapdev((vm_offset_t)LogicalAddress, Length);
69}
70
71ACPI_STATUS
72AcpiOsGetPhysicalAddress(void *LogicalAddress, ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
73{
74    /* we can't necessarily do this, so cop out */
75    return(AE_BAD_ADDRESS);
76}
77
78/*
79 * There is no clean way to do this.  We make the charitable assumption
80 * that callers will not pass garbage to us.
81 */
82BOOLEAN
83AcpiOsReadable (void *Pointer, UINT32 Length)
84{
85    return(TRUE);
86}
87
88BOOLEAN
89AcpiOsWritable (void *Pointer, UINT32 Length)
90{
91    return(TRUE);
92}
93
94ACPI_STATUS
95AcpiOsReadMemory (
96    ACPI_PHYSICAL_ADDRESS	Address,
97    void			*Value,
98    UINT32			Width)
99{
100    void	*LogicalAddress;
101
102    if (AcpiOsMapMemory(Address, Width / 8, &LogicalAddress) != AE_OK) {
103	return(AE_NOT_EXIST);
104    }
105
106    switch (Width) {
107    case 8:
108	*(u_int8_t *)Value = (*(volatile u_int8_t *)LogicalAddress);
109	break;
110    case 16:
111	*(u_int16_t *)Value = (*(volatile u_int16_t *)LogicalAddress);
112	break;
113    case 32:
114	*(u_int32_t *)Value = (*(volatile u_int32_t *)LogicalAddress);
115	break;
116    case 64:
117	*(u_int64_t *)Value = (*(volatile u_int64_t *)LogicalAddress);
118	break;
119    default:
120	/* debug trap goes here */
121    }
122
123    AcpiOsUnmapMemory(LogicalAddress, Width / 8);
124
125    return(AE_OK);
126}
127
128ACPI_STATUS
129AcpiOsWriteMemory (
130    ACPI_PHYSICAL_ADDRESS	Address,
131    ACPI_INTEGER		Value,
132    UINT32			Width)
133{
134    void	*LogicalAddress;
135
136    if (AcpiOsMapMemory(Address, Width / 8, &LogicalAddress) != AE_OK) {
137	return(AE_NOT_EXIST);
138    }
139
140    switch (Width) {
141    case 8:
142	(*(volatile u_int8_t *)LogicalAddress) = Value & 0xff;
143	break;
144    case 16:
145	(*(volatile u_int16_t *)LogicalAddress) = Value & 0xffff;
146	break;
147    case 32:
148	(*(volatile u_int32_t *)LogicalAddress) = Value & 0xffffffff;
149	break;
150    case 64:
151	(*(volatile u_int64_t *)LogicalAddress) = Value;
152	break;
153    default:
154	/* debug trap goes here */
155    }
156
157    AcpiOsUnmapMemory(LogicalAddress, Width / 8);
158
159    return(AE_OK);
160}
161