1fd660059SMike Smith /*- 2c434440bSMike Smith * Copyright (c) 2000 Mitsaru Iwasaki 3fd660059SMike Smith * Copyright (c) 2000 Michael Smith 4fd660059SMike Smith * Copyright (c) 2000 BSDi 5fd660059SMike Smith * All rights reserved. 6fd660059SMike Smith * 7fd660059SMike Smith * Redistribution and use in source and binary forms, with or without 8fd660059SMike Smith * modification, are permitted provided that the following conditions 9fd660059SMike Smith * are met: 10fd660059SMike Smith * 1. Redistributions of source code must retain the above copyright 11fd660059SMike Smith * notice, this list of conditions and the following disclaimer. 12fd660059SMike Smith * 2. Redistributions in binary form must reproduce the above copyright 13fd660059SMike Smith * notice, this list of conditions and the following disclaimer in the 14fd660059SMike Smith * documentation and/or other materials provided with the distribution. 15fd660059SMike Smith * 16fd660059SMike Smith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17fd660059SMike Smith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18fd660059SMike Smith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19fd660059SMike Smith * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20fd660059SMike Smith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21fd660059SMike Smith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22fd660059SMike Smith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23fd660059SMike Smith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24fd660059SMike Smith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25fd660059SMike Smith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26fd660059SMike Smith * SUCH DAMAGE. 27fd660059SMike Smith * 28fd660059SMike Smith * $FreeBSD$ 29fd660059SMike Smith */ 30fd660059SMike Smith 31fd660059SMike Smith /* 32fd660059SMike Smith * 6.2 : Memory Management 33fd660059SMike Smith */ 34fd660059SMike Smith 35fd660059SMike Smith #include "acpi.h" 36fd660059SMike Smith 37fd660059SMike Smith #include <sys/kernel.h> 38fd660059SMike Smith #include <sys/malloc.h> 39fd660059SMike Smith #include <vm/vm.h> 40fd660059SMike Smith #include <vm/pmap.h> 41fd660059SMike Smith 42fd660059SMike Smith MALLOC_DEFINE(M_ACPICA, "acpica", "ACPI CA memory pool"); 43fd660059SMike Smith 44fd660059SMike Smith void * 45fd660059SMike Smith AcpiOsAllocate(UINT32 Size) 46fd660059SMike Smith { 47fd660059SMike Smith return(malloc(Size, M_ACPICA, M_NOWAIT)); 48fd660059SMike Smith } 49fd660059SMike Smith 50fd660059SMike Smith void * 51fd660059SMike Smith AcpiOsCallocate (UINT32 Size) 52fd660059SMike Smith { 53fd660059SMike Smith void *alloc; 54fd660059SMike Smith 55fd660059SMike Smith alloc = malloc(Size, M_ACPICA, M_NOWAIT); 56fd660059SMike Smith if (alloc != NULL) 57fd660059SMike Smith bzero(alloc, Size); 58fd660059SMike Smith return(alloc); 59fd660059SMike Smith } 60fd660059SMike Smith 61fd660059SMike Smith void 62fd660059SMike Smith AcpiOsFree (void *Memory) 63fd660059SMike Smith { 64fd660059SMike Smith free(Memory, M_ACPICA); 65fd660059SMike Smith } 66fd660059SMike Smith 67fd660059SMike Smith ACPI_STATUS 68c434440bSMike Smith AcpiOsMapMemory (ACPI_PHYSICAL_ADDRESS PhysicalAddress, UINT32 Length, void **LogicalAddress) 69fd660059SMike Smith { 70fd660059SMike Smith *LogicalAddress = pmap_mapdev((vm_offset_t)PhysicalAddress, Length); 71fd660059SMike Smith if (*LogicalAddress == NULL) 72fd660059SMike Smith return(AE_BAD_ADDRESS); 73fd660059SMike Smith return(AE_OK); 74fd660059SMike Smith } 75fd660059SMike Smith 76fd660059SMike Smith void 77fd660059SMike Smith AcpiOsUnmapMemory (void *LogicalAddress, UINT32 Length) 78fd660059SMike Smith { 79fd660059SMike Smith pmap_unmapdev((vm_offset_t)LogicalAddress, Length); 80fd660059SMike Smith } 81fd660059SMike Smith 82fd660059SMike Smith /* 83fd660059SMike Smith * There is no clean way to do this. We make the charitable assumption 84fd660059SMike Smith * that callers will not pass garbage to us. 85fd660059SMike Smith */ 86fd660059SMike Smith BOOLEAN 87fd660059SMike Smith AcpiOsReadable (void *Pointer, UINT32 Length) 88fd660059SMike Smith { 89fd660059SMike Smith return(TRUE); 90fd660059SMike Smith } 91fd660059SMike Smith 92fd660059SMike Smith BOOLEAN 93fd660059SMike Smith AcpiOsWritable (void *Pointer, UINT32 Length) 94fd660059SMike Smith { 95fd660059SMike Smith return(TRUE); 96fd660059SMike Smith } 97fd660059SMike Smith 98c434440bSMike Smith static __inline 99c434440bSMike Smith UINT32 100c434440bSMike Smith AcpiOsMemInX (UINT32 Length, ACPI_PHYSICAL_ADDRESS InAddr) 101c434440bSMike Smith { 102c434440bSMike Smith UINT32 Value; 103c434440bSMike Smith void *LogicalAddress; 104c434440bSMike Smith 105c434440bSMike Smith if (AcpiOsMapMemory(InAddr, Length, &LogicalAddress) != AE_OK) { 106c434440bSMike Smith return(0); 107c434440bSMike Smith } 108c434440bSMike Smith 109c434440bSMike Smith switch (Length) { 110c434440bSMike Smith case 1: 111c434440bSMike Smith Value = (*(volatile u_int8_t *)LogicalAddress) & 0xff; 112c434440bSMike Smith break; 113c434440bSMike Smith case 2: 114c434440bSMike Smith Value = (*(volatile u_int16_t *)LogicalAddress) & 0xffff; 115c434440bSMike Smith break; 116c434440bSMike Smith case 4: 117c434440bSMike Smith Value = (*(volatile u_int32_t *)LogicalAddress); 118c434440bSMike Smith break; 119c434440bSMike Smith } 120c434440bSMike Smith 121c434440bSMike Smith AcpiOsUnmapMemory(LogicalAddress, Length); 122c434440bSMike Smith 123c434440bSMike Smith return(Value); 124c434440bSMike Smith } 125c434440bSMike Smith 126c434440bSMike Smith UINT8 127c434440bSMike Smith AcpiOsMemIn8 (ACPI_PHYSICAL_ADDRESS InAddr) 128c434440bSMike Smith { 129c434440bSMike Smith return((UINT8)AcpiOsMemInX(1, InAddr)); 130c434440bSMike Smith } 131c434440bSMike Smith 132c434440bSMike Smith UINT16 133c434440bSMike Smith AcpiOsMemIn16 (ACPI_PHYSICAL_ADDRESS InAddr) 134c434440bSMike Smith { 135c434440bSMike Smith return((UINT16)AcpiOsMemInX(2, InAddr)); 136c434440bSMike Smith } 137c434440bSMike Smith 138c434440bSMike Smith UINT32 139c434440bSMike Smith AcpiOsMemIn32 (ACPI_PHYSICAL_ADDRESS InAddr) 140c434440bSMike Smith { 141c434440bSMike Smith return((UINT32)AcpiOsMemInX(4, InAddr)); 142c434440bSMike Smith } 143c434440bSMike Smith 144c434440bSMike Smith static __inline 145c434440bSMike Smith void 146c434440bSMike Smith AcpiOsMemOutX (UINT32 Length, ACPI_PHYSICAL_ADDRESS OutAddr, UINT32 Value) 147c434440bSMike Smith { 148c434440bSMike Smith void *LogicalAddress; 149c434440bSMike Smith 150c434440bSMike Smith if (AcpiOsMapMemory(OutAddr, Length, &LogicalAddress) != AE_OK) { 151c434440bSMike Smith return; 152c434440bSMike Smith } 153c434440bSMike Smith 154c434440bSMike Smith switch (Length) { 155c434440bSMike Smith case 1: 156c434440bSMike Smith (*(volatile u_int8_t *)LogicalAddress) = Value & 0xff; 157c434440bSMike Smith break; 158c434440bSMike Smith case 2: 159c434440bSMike Smith (*(volatile u_int16_t *)LogicalAddress) = Value & 0xffff; 160c434440bSMike Smith break; 161c434440bSMike Smith case 4: 162c434440bSMike Smith (*(volatile u_int32_t *)LogicalAddress) = Value; 163c434440bSMike Smith break; 164c434440bSMike Smith } 165c434440bSMike Smith 166c434440bSMike Smith AcpiOsUnmapMemory(LogicalAddress, Length); 167c434440bSMike Smith } 168c434440bSMike Smith 169c434440bSMike Smith void 170c434440bSMike Smith AcpiOsMemOut8 (ACPI_PHYSICAL_ADDRESS OutAddr, UINT8 Value) 171c434440bSMike Smith { 172c434440bSMike Smith AcpiOsMemOutX(1, OutAddr, (UINT32)Value); 173c434440bSMike Smith } 174c434440bSMike Smith 175c434440bSMike Smith void 176c434440bSMike Smith AcpiOsMemOut16 (ACPI_PHYSICAL_ADDRESS OutAddr, UINT16 Value) 177c434440bSMike Smith { 178c434440bSMike Smith AcpiOsMemOutX(2, OutAddr, (UINT32)Value); 179c434440bSMike Smith } 180c434440bSMike Smith 181c434440bSMike Smith void 182c434440bSMike Smith AcpiOsMemOut32 (ACPI_PHYSICAL_ADDRESS OutAddr, UINT32 Value) 183c434440bSMike Smith { 184c434440bSMike Smith AcpiOsMemOutX(4, OutAddr, (UINT32)Value); 185c434440bSMike Smith } 186c434440bSMike Smith 187