OsdHardware.c revision 210137
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 28/* 29 * 6.7 : Hardware Abstraction 30 */ 31 32#include <sys/cdefs.h> 33__FBSDID("$FreeBSD: head/sys/dev/acpica/Osd/OsdHardware.c 210137 2010-07-15 19:52:54Z jkim $"); 34 35#include <contrib/dev/acpica/include/acpi.h> 36 37#include <sys/bus.h> 38#include <sys/kernel.h> 39#include <machine/bus.h> 40#include <machine/pci_cfgreg.h> 41#include <dev/acpica/acpivar.h> 42#include <dev/pci/pcireg.h> 43 44/* 45 * ACPICA's rather gung-ho approach to hardware resource ownership is a little 46 * troublesome insofar as there is no easy way for us to know in advance 47 * exactly which I/O resources it's going to want to use. 48 * 49 * In order to deal with this, we ignore resource ownership entirely, and simply 50 * use the native I/O space accessor functionality. This is Evil, but it works. 51 * 52 * XXX use an intermediate #define for the tag/handle 53 */ 54 55#ifdef __i386__ 56#define ACPI_BUS_SPACE_IO I386_BUS_SPACE_IO 57#define ACPI_BUS_HANDLE 0 58#endif 59#ifdef __ia64__ 60#define ACPI_BUS_SPACE_IO IA64_BUS_SPACE_IO 61#define ACPI_BUS_HANDLE 0 62#endif 63#ifdef __amd64__ 64#define ACPI_BUS_SPACE_IO AMD64_BUS_SPACE_IO 65#define ACPI_BUS_HANDLE 0 66#endif 67 68ACPI_STATUS 69AcpiOsReadPort(ACPI_IO_ADDRESS InPort, UINT32 *Value, UINT32 Width) 70{ 71 72 switch (Width) { 73 case 8: 74 *Value = bus_space_read_1(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, InPort); 75 break; 76 case 16: 77 *Value = bus_space_read_2(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, InPort); 78 break; 79 case 32: 80 *Value = bus_space_read_4(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, InPort); 81 break; 82 } 83 84 return (AE_OK); 85} 86 87ACPI_STATUS 88AcpiOsWritePort(ACPI_IO_ADDRESS OutPort, UINT32 Value, UINT32 Width) 89{ 90 91 switch (Width) { 92 case 8: 93 bus_space_write_1(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, OutPort, Value); 94 break; 95 case 16: 96 bus_space_write_2(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, OutPort, Value); 97 break; 98 case 32: 99 bus_space_write_4(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, OutPort, Value); 100 break; 101 } 102 103 return (AE_OK); 104} 105 106ACPI_STATUS 107AcpiOsReadPciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, void *Value, 108 UINT32 Width) 109{ 110 111 if (Width == 64) 112 return (AE_SUPPORT); 113 114 if (!pci_cfgregopen()) 115 return (AE_NOT_EXIST); 116 117 *(UINT64 *)Value = pci_cfgregread(PciId->Bus, PciId->Device, 118 PciId->Function, Register, Width / 8); 119 120 return (AE_OK); 121} 122 123 124ACPI_STATUS 125AcpiOsWritePciConfiguration (ACPI_PCI_ID *PciId, UINT32 Register, 126 UINT64 Value, UINT32 Width) 127{ 128 129 if (Width == 64) 130 return (AE_SUPPORT); 131 132 if (!pci_cfgregopen()) 133 return (AE_NOT_EXIST); 134 135 pci_cfgregwrite(PciId->Bus, PciId->Device, PciId->Function, Register, 136 Value, Width / 8); 137 138 return (AE_OK); 139} 140 141/* 142 * Depth-first recursive case for finding the bus, given the slot/function. 143 */ 144static int 145acpi_bus_number(ACPI_HANDLE root, ACPI_HANDLE curr, ACPI_PCI_ID *PciId) 146{ 147 ACPI_HANDLE parent; 148 ACPI_STATUS status; 149 ACPI_OBJECT_TYPE type; 150 UINT32 adr; 151 int bus, slot, func, class, subclass, header; 152 153 /* Try to get the _BBN object of the root, otherwise assume it is 0. */ 154 bus = 0; 155 if (root == curr) { 156 status = acpi_GetInteger(root, "_BBN", &bus); 157 if (ACPI_FAILURE(status) && bootverbose) 158 printf("acpi_bus_number: root bus has no _BBN, assuming 0\n"); 159 return (bus); 160 } 161 status = AcpiGetParent(curr, &parent); 162 if (ACPI_FAILURE(status)) 163 return (bus); 164 165 /* First, recurse up the tree until we find the host bus. */ 166 bus = acpi_bus_number(root, parent, PciId); 167 168 /* Validate parent bus device type. */ 169 if (ACPI_FAILURE(AcpiGetType(parent, &type)) || type != ACPI_TYPE_DEVICE) { 170 printf("acpi_bus_number: not a device, type %d\n", type); 171 return (bus); 172 } 173 174 /* Get the parent's slot and function. */ 175 status = acpi_GetInteger(parent, "_ADR", &adr); 176 if (ACPI_FAILURE(status)) 177 return (bus); 178 slot = ACPI_HIWORD(adr); 179 func = ACPI_LOWORD(adr); 180 181 /* Is this a PCI-PCI or Cardbus-PCI bridge? */ 182 class = pci_cfgregread(bus, slot, func, PCIR_CLASS, 1); 183 if (class != PCIC_BRIDGE) 184 return (bus); 185 subclass = pci_cfgregread(bus, slot, func, PCIR_SUBCLASS, 1); 186 187 /* Find the header type, masking off the multifunction bit. */ 188 header = pci_cfgregread(bus, slot, func, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE; 189 if (header == PCIM_HDRTYPE_BRIDGE && subclass == PCIS_BRIDGE_PCI) 190 bus = pci_cfgregread(bus, slot, func, PCIR_SECBUS_1, 1); 191 else if (header == PCIM_HDRTYPE_CARDBUS && subclass == PCIS_BRIDGE_CARDBUS) 192 bus = pci_cfgregread(bus, slot, func, PCIR_SECBUS_2, 1); 193 return (bus); 194} 195 196/* 197 * Find the bus number for a device 198 * 199 * Device: handle for the PCI root bridge device 200 * Region: handle for the PCI configuration space operation region 201 * PciId: pointer to device slot and function, we fill out bus 202 */ 203void 204AcpiOsDerivePciId(ACPI_HANDLE Device, ACPI_HANDLE Region, ACPI_PCI_ID **PciId) 205{ 206 ACPI_HANDLE parent; 207 ACPI_STATUS status; 208 int bus; 209 210 if (pci_cfgregopen() == 0) 211 panic("AcpiOsDerivePciId unable to initialize pci bus"); 212 213 /* Try to read _BBN for bus number if we're at the root. */ 214 bus = 0; 215 if (Device == Region) { 216 status = acpi_GetInteger(Device, "_BBN", &bus); 217 if (ACPI_FAILURE(status) && bootverbose) 218 printf("AcpiOsDerivePciId: root bus has no _BBN, assuming 0\n"); 219 } 220 221 /* Get the parent handle and call the recursive case. */ 222 if (ACPI_SUCCESS(AcpiGetParent(Region, &parent))) 223 bus = acpi_bus_number(Device, parent, *PciId); 224 (*PciId)->Bus = bus; 225 if (bootverbose) { 226 printf("AcpiOsDerivePciId: %s -> bus %d dev %d func %d\n", 227 acpi_name(Region), (*PciId)->Bus, (*PciId)->Device, 228 (*PciId)->Function); 229 } 230} 231