1 /*- 2 * Copyright (c) 2003 Nate Lawson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/kernel.h> 32 #include <sys/bus.h> 33 #include <sys/sbuf.h> 34 35 #include <machine/bus.h> 36 #include <machine/resource.h> 37 #include <sys/rman.h> 38 39 #include <contrib/dev/acpica/include/acpi.h> 40 41 #include <dev/acpica/acpivar.h> 42 43 /* 44 * Package manipulation convenience functions 45 */ 46 47 int 48 acpi_PkgInt(ACPI_OBJECT *res, int idx, UINT64 *dst) 49 { 50 ACPI_OBJECT *obj; 51 52 obj = &res->Package.Elements[idx]; 53 if (obj == NULL || obj->Type != ACPI_TYPE_INTEGER) 54 return (EINVAL); 55 *dst = obj->Integer.Value; 56 57 return (0); 58 } 59 60 int 61 acpi_PkgInt32(ACPI_OBJECT *res, int idx, uint32_t *dst) 62 { 63 UINT64 tmp; 64 int error; 65 66 error = acpi_PkgInt(res, idx, &tmp); 67 if (error == 0) 68 *dst = (uint32_t)tmp; 69 70 return (error); 71 } 72 73 int 74 acpi_PkgStr(ACPI_OBJECT *res, int idx, void *dst, size_t size) 75 { 76 ACPI_OBJECT *obj; 77 void *ptr; 78 size_t length; 79 80 obj = &res->Package.Elements[idx]; 81 if (obj == NULL) 82 return (EINVAL); 83 bzero(dst, sizeof(dst)); 84 85 switch (obj->Type) { 86 case ACPI_TYPE_STRING: 87 ptr = obj->String.Pointer; 88 length = obj->String.Length; 89 break; 90 case ACPI_TYPE_BUFFER: 91 ptr = obj->Buffer.Pointer; 92 length = obj->Buffer.Length; 93 break; 94 default: 95 return (EINVAL); 96 } 97 98 /* Make sure string will fit, including terminating NUL */ 99 if (++length > size) 100 return (E2BIG); 101 102 strlcpy(dst, ptr, length); 103 return (0); 104 } 105 106 int 107 acpi_PkgGas(device_t dev, ACPI_OBJECT *res, int idx, int *type, int *rid, 108 struct resource **dst, u_int flags) 109 { 110 ACPI_GENERIC_ADDRESS gas; 111 ACPI_OBJECT *obj; 112 113 obj = &res->Package.Elements[idx]; 114 if (obj == NULL || obj->Type != ACPI_TYPE_BUFFER || 115 obj->Buffer.Length < sizeof(ACPI_GENERIC_ADDRESS) + 3) 116 return (EINVAL); 117 118 memcpy(&gas, obj->Buffer.Pointer + 3, sizeof(gas)); 119 120 return (acpi_bus_alloc_gas(dev, type, rid, &gas, dst, flags)); 121 } 122 123 int 124 acpi_PkgFFH_IntelCpu(ACPI_OBJECT *res, int idx, int *vendor, int *class, 125 uint64_t *address, int *accsize) 126 { 127 ACPI_GENERIC_ADDRESS gas; 128 ACPI_OBJECT *obj; 129 130 obj = &res->Package.Elements[idx]; 131 if (obj == NULL || obj->Type != ACPI_TYPE_BUFFER || 132 obj->Buffer.Length < sizeof(ACPI_GENERIC_ADDRESS) + 3) 133 return (EINVAL); 134 135 memcpy(&gas, obj->Buffer.Pointer + 3, sizeof(gas)); 136 if (gas.SpaceId != ACPI_ADR_SPACE_FIXED_HARDWARE) 137 return (ERESTART); 138 *vendor = gas.BitWidth; 139 *class = gas.BitOffset; 140 *address = gas.Address; 141 *accsize = gas.AccessWidth; 142 return (0); 143 } 144 145 ACPI_HANDLE 146 acpi_GetReference(ACPI_HANDLE scope, ACPI_OBJECT *obj) 147 { 148 ACPI_HANDLE h; 149 150 if (obj == NULL) 151 return (NULL); 152 153 switch (obj->Type) { 154 case ACPI_TYPE_LOCAL_REFERENCE: 155 case ACPI_TYPE_ANY: 156 h = obj->Reference.Handle; 157 break; 158 case ACPI_TYPE_STRING: 159 /* 160 * The String object usually contains a fully-qualified path, so 161 * scope can be NULL. 162 * 163 * XXX This may not always be the case. 164 */ 165 if (ACPI_FAILURE(AcpiGetHandle(scope, obj->String.Pointer, &h))) 166 h = NULL; 167 break; 168 default: 169 h = NULL; 170 break; 171 } 172 173 return (h); 174 } 175