OsdHardware.c revision 114977
1/*- 2 * Copyright (c) 2000, 2001 Michael Smith 3 * Copyright (c) 2000 BSDi 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * $FreeBSD: head/sys/dev/acpica/Osd/OsdHardware.c 114977 2003-05-13 16:59:46Z jhb $ 28 */ 29 30/* 31 * 6.7 : Hardware Abstraction 32 */ 33 34#include "acpi.h" 35 36#include <machine/bus_pio.h> 37#include <machine/bus.h> 38#include <machine/pci_cfgreg.h> 39#if __FreeBSD_version >= 500000 40#include <dev/pci/pcireg.h> 41#else 42#include <pci/pcireg.h> 43#endif 44 45/* 46 * ACPICA's rather gung-ho approach to hardware resource ownership is a little 47 * troublesome insofar as there is no easy way for us to know in advance 48 * exactly which I/O resources it's going to want to use. 49 * 50 * In order to deal with this, we ignore resource ownership entirely, and simply 51 * use the native I/O space accessor functionality. This is Evil, but it works. 52 * 53 * XXX use an intermediate #define for the tag/handle 54 */ 55 56#ifdef __i386__ 57#define ACPI_BUS_SPACE_IO I386_BUS_SPACE_IO 58#define ACPI_BUS_HANDLE 0 59#endif 60#ifdef __ia64__ 61#define ACPI_BUS_SPACE_IO IA64_BUS_SPACE_IO 62#define ACPI_BUS_HANDLE 0 63#endif 64 65ACPI_STATUS 66AcpiOsReadPort ( 67 ACPI_IO_ADDRESS InPort, 68 void *Value, 69 UINT32 Width) 70{ 71 switch (Width) { 72 case 8: 73 *(u_int8_t *)Value = bus_space_read_1(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, InPort); 74 break; 75 case 16: 76 *(u_int16_t *)Value = bus_space_read_2(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, InPort); 77 break; 78 case 32: 79 *(u_int32_t *)Value = bus_space_read_4(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, InPort); 80 break; 81 default: 82 /* debug trap goes here */ 83 break; 84 } 85 86 return(AE_OK); 87} 88 89ACPI_STATUS 90AcpiOsWritePort ( 91 ACPI_IO_ADDRESS OutPort, 92 ACPI_INTEGER Value, 93 UINT32 Width) 94{ 95 switch (Width) { 96 case 8: 97 bus_space_write_1(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, OutPort, Value); 98 break; 99 case 16: 100 bus_space_write_2(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, OutPort, Value); 101 break; 102 case 32: 103 bus_space_write_4(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, OutPort, Value); 104 break; 105 default: 106 /* debug trap goes here */ 107 break; 108 } 109 110 return(AE_OK); 111} 112 113ACPI_STATUS 114AcpiOsReadPciConfiguration ( 115 ACPI_PCI_ID *PciId, 116 UINT32 Register, 117 void *Value, 118 UINT32 Width) 119{ 120 u_int32_t byte_width = Width / 8; 121 u_int32_t val; 122 123 if (!pci_cfgregopen()) 124 return(AE_NOT_EXIST); 125 126 val = pci_cfgregread(PciId->Bus, PciId->Device, PciId->Function, Register, byte_width); 127 switch (Width) { 128 case 8: 129 *(u_int8_t *)Value = val & 0xff; 130 break; 131 case 16: 132 *(u_int16_t *)Value = val & 0xffff; 133 break; 134 case 32: 135 *(u_int32_t *)Value = val; 136 break; 137 default: 138 /* debug trap goes here */ 139 break; 140 } 141 142 143 return(AE_OK); 144} 145 146 147ACPI_STATUS 148AcpiOsWritePciConfiguration ( 149 ACPI_PCI_ID *PciId, 150 UINT32 Register, 151 ACPI_INTEGER Value, 152 UINT32 Width) 153{ 154 u_int32_t byte_width = Width / 8; 155 156 if (!pci_cfgregopen()) 157 return(AE_NOT_EXIST); 158 159 pci_cfgregwrite(PciId->Bus, PciId->Device, PciId->Function, Register, Value, byte_width); 160 161 return(AE_OK); 162} 163 164/* XXX should use acpivar.h but too many include dependencies */ 165extern ACPI_STATUS acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int 166 *number); 167 168/* 169 * Depth-first recursive case for finding the bus, given the slot/function. 170 */ 171static int 172acpi_bus_number(ACPI_HANDLE root, ACPI_HANDLE curr, ACPI_PCI_ID *PciId) 173{ 174 ACPI_HANDLE parent; 175 ACPI_OBJECT_TYPE type; 176 UINT32 adr; 177 int bus, slot, func, class, subclass, header; 178 179 /* Try to get the _BBN object of the root, otherwise assume it is 0 */ 180 bus = 0; 181 if (root == curr) { 182 if (ACPI_FAILURE(acpi_EvaluateInteger(root, "_BBN", &bus)) && 183 bootverbose) 184 printf("acpi_bus_number: root bus has no _BBN, assuming 0\n"); 185 return (bus); 186 } 187 if (ACPI_FAILURE(AcpiGetParent(curr, &parent))) 188 return (bus); 189 190 /* First, recurse up the tree until we find the host bus */ 191 bus = acpi_bus_number(root, parent, PciId); 192 193 /* Validate parent bus device type */ 194 if (ACPI_FAILURE(AcpiGetType(parent, &type)) || type != ACPI_TYPE_DEVICE) { 195 printf("acpi_bus_number: not a device, type %d\n", type); 196 return (bus); 197 } 198 /* Get the parent's slot and function */ 199 if (ACPI_FAILURE(acpi_EvaluateInteger(parent, "_ADR", &adr))) { 200 printf("acpi_bus_number: can't get _ADR\n"); 201 return (bus); 202 } 203 slot = ACPI_HIWORD(adr); 204 func = ACPI_LOWORD(adr); 205 206 /* Is this a PCI-PCI or Cardbus-PCI bridge? */ 207 class = pci_cfgregread(bus, slot, func, PCIR_CLASS, 1); 208 if (class != PCIC_BRIDGE) 209 return (bus); 210 subclass = pci_cfgregread(bus, slot, func, PCIR_SUBCLASS, 1); 211 /* Find the header type, masking off the multifunction bit */ 212 header = pci_cfgregread(bus, slot, func, PCIR_HEADERTYPE, 1) & 0x7f; 213 if (header == 1 && subclass == PCIS_BRIDGE_PCI) 214 bus = pci_cfgregread(bus, slot, func, PCIR_SECBUS_1, 1); 215 if (header == 2 && subclass == PCIS_BRIDGE_CARDBUS) 216 bus = pci_cfgregread(bus, slot, func, PCIR_SECBUS_2, 1); 217 return (bus); 218} 219 220/* 221 * Find the bus number for a device 222 * 223 * rhandle: handle for the root bus 224 * chandle: handle for the device 225 * PciId: pointer to device slot and function, we fill out bus 226 */ 227void 228AcpiOsDerivePciId ( 229 ACPI_HANDLE rhandle, 230 ACPI_HANDLE chandle, 231 ACPI_PCI_ID **PciId) 232{ 233 ACPI_HANDLE parent; 234 int bus; 235 236 if (pci_cfgregopen() == 0) 237 panic("AcpiOsDerivePciId unable to initialize pci bus"); 238 239 /* Try to read _BBN for bus number if we're at the root */ 240 bus = 0; 241 if (rhandle == chandle) { 242 if (ACPI_FAILURE(acpi_EvaluateInteger(rhandle, "_BBN", &bus)) && 243 bootverbose) 244 printf("AcpiOsDerivePciId: root bus has no _BBN, assuming 0\n"); 245 } 246 /* 247 * Get the parent handle and call the recursive case. It is not 248 * clear why we seem to be getting a chandle that points to a child 249 * of the desired slot/function but passing in the parent handle 250 * here works. 251 */ 252 if (ACPI_SUCCESS(AcpiGetParent(chandle, &parent))) 253 bus = acpi_bus_number(rhandle, parent, *PciId); 254 (*PciId)->Bus = bus; 255 if (bootverbose) { 256 printf("AcpiOsDerivePciId: bus %d dev %d func %d\n", 257 (*PciId)->Bus, (*PciId)->Device, (*PciId)->Function); 258 } 259} 260